raspberry.patch 2.9 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224732257322673227732287322973230732317323273233732347323573236732377323873239732407324173242732437324473245732467324773248732497325073251732527325373254732557325673257732587325973260732617326273263732647326573266732677326873269732707327173272732737327473275732767327773278732797328073281732827328373284732857328673287732887328973290732917329273293732947329573296732977329873299733007330173302733037330473305733067330773308733097331073311733127331373314733157331673317733187331973320733217332273323733247332573326733277332873329733307333173332733337333473335733367333773338733397334073341733427334373344733457334673347733487334973350733517335273353733547335573356733577335873359733607336173362733637336473365733667336773368733697337073371733727337373374733757337673377733787337973380733817338273383733847338573386733877338873389733907339173392733937339473395733967339773398733997340073401734027340373404734057340673407734087340973410734117341273413734147341573416734177341873419734207342173422734237342473425734267342773428734297343073431734327343373434734357343673437734387343973440734417344273443734447344573446734477344873449734507345173452734537345473455734567345773458734597346073461734627346373464734657346673467734687346973470734717347273473734747347573476734777347873479734807348173482734837348473485734867348773488734897349073491734927349373494734957349673497734987349973500735017350273503735047350573506735077350873509735107351173512735137351473515735167351773518735197352073521735227352373524735257352673527735287352973530735317353273533735347353573536735377353873539735407354173542735437354473545735467354773548735497355073551735527355373554735557355673557735587355973560735617356273563735647356573566735677356873569735707357173572735737357473575735767357773578735797358073581735827358373584735857358673587735887358973590735917359273593735947359573596735977359873599736007360173602736037360473605736067360773608736097361073611736127361373614736157361673617736187361973620736217362273623736247362573626736277362873629736307363173632736337363473635736367363773638736397364073641736427364373644736457364673647736487364973650736517365273653736547365573656736577365873659736607366173662736637366473665736667366773668736697367073671736727367373674736757367673677736787367973680736817368273683736847368573686736877368873689736907369173692736937369473695736967369773698736997370073701737027370373704737057370673707737087370973710737117371273713737147371573716737177371873719737207372173722737237372473725737267372773728737297373073731737327373373734737357373673737737387373973740737417374273743737447374573746737477374873749737507375173752737537375473755737567375773758737597376073761737627376373764737657376673767737687376973770737717377273773737747377573776737777377873779737807378173782737837378473785737867378773788737897379073791737927379373794737957379673797737987379973800738017380273803738047380573806738077380873809738107381173812738137381473815738167381773818738197382073821738227382373824738257382673827738287382973830738317383273833738347383573836738377383873839738407384173842738437384473845738467384773848738497385073851738527385373854738557385673857738587385973860738617386273863738647386573866738677386873869738707387173872738737387473875738767387773878738797388073881738827388373884738857388673887738887388973890738917389273893738947389573896738977389873899739007390173902739037390473905739067390773908739097391073911739127391373914739157391673917739187391973920739217392273923739247392573926739277392873929739307393173932739337393473935739367393773938739397394073941739427394373944739457394673947739487394973950739517395273953739547395573956739577395873959739607396173962739637396473965739667396773968739697397073971739727397373974739757397673977739787397973980739817398273983739847398573986739877398873989739907399173992739937399473995739967399773998739997400074001740027400374004740057400674007740087400974010740117401274013740147401574016740177401874019740207402174022740237402474025740267402774028740297403074031740327403374034740357403674037740387403974040740417404274043740447404574046740477404874049740507405174052740537405474055740567405774058740597406074061740627406374064740657406674067740687406974070740717407274073740747407574076740777407874079740807408174082740837408474085740867408774088740897409074091740927409374094740957409674097740987409974100741017410274103741047410574106741077410874109741107411174112741137411474115741167411774118741197412074121741227412374124741257412674127741287412974130741317413274133741347413574136741377413874139741407414174142741437414474145741467414774148741497415074151741527415374154741557415674157741587415974160741617416274163741647416574166741677416874169741707417174172741737417474175741767417774178741797418074181741827418374184741857418674187741887418974190741917419274193741947419574196741977419874199742007420174202742037420474205742067420774208742097421074211742127421374214742157421674217742187421974220742217422274223742247422574226742277422874229742307423174232742337423474235742367423774238742397424074241742427424374244742457424674247742487424974250742517425274253742547425574256742577425874259742607426174262742637426474265742667426774268742697427074271742727427374274742757427674277742787427974280742817428274283742847428574286742877428874289742907429174292742937429474295742967429774298742997430074301743027430374304743057430674307743087430974310743117431274313743147431574316743177431874319743207432174322743237432474325743267432774328743297433074331743327433374334743357433674337743387433974340743417434274343743447434574346743477434874349743507435174352743537435474355743567435774358743597436074361743627436374364743657436674367743687436974370743717437274373743747437574376743777437874379743807438174382743837438474385743867438774388743897439074391743927439374394743957439674397743987439974400744017440274403744047440574406744077440874409744107441174412744137441474415744167441774418744197442074421744227442374424744257442674427744287442974430744317443274433744347443574436744377443874439744407444174442744437444474445744467444774448744497445074451744527445374454744557445674457744587445974460744617446274463744647446574466744677446874469744707447174472744737447474475744767447774478744797448074481744827448374484744857448674487744887448974490744917449274493744947449574496744977449874499745007450174502745037450474505745067450774508745097451074511745127451374514745157451674517745187451974520745217452274523745247452574526745277452874529745307453174532745337453474535745367453774538745397454074541745427454374544745457454674547745487454974550745517455274553745547455574556745577455874559745607456174562745637456474565745667456774568745697457074571745727457374574745757457674577745787457974580745817458274583745847458574586745877458874589745907459174592745937459474595745967459774598745997460074601746027460374604746057460674607746087460974610746117461274613746147461574616746177461874619746207462174622746237462474625746267462774628746297463074631746327463374634746357463674637746387463974640746417464274643746447464574646746477464874649746507465174652746537465474655746567465774658746597466074661746627466374664746657466674667746687466974670746717467274673746747467574676746777467874679746807468174682746837468474685746867468774688746897469074691746927469374694746957469674697746987469974700747017470274703747047470574706747077470874709747107471174712747137471474715747167471774718747197472074721747227472374724747257472674727747287472974730747317473274733747347473574736747377473874739747407474174742747437474474745747467474774748747497475074751747527475374754747557475674757747587475974760747617476274763747647476574766747677476874769747707477174772747737477474775747767477774778747797478074781747827478374784747857478674787747887478974790747917479274793747947479574796747977479874799748007480174802748037480474805748067480774808748097481074811748127481374814748157481674817748187481974820748217482274823748247482574826748277482874829748307483174832748337483474835748367483774838748397484074841748427484374844748457484674847748487484974850748517485274853748547485574856748577485874859748607486174862748637486474865748667486774868748697487074871748727487374874748757487674877748787487974880748817488274883748847488574886748877488874889748907489174892748937489474895748967489774898748997490074901749027490374904749057490674907749087490974910749117491274913749147491574916749177491874919749207492174922749237492474925749267492774928749297493074931749327493374934749357493674937749387493974940749417494274943749447494574946749477494874949749507495174952749537495474955749567495774958749597496074961749627496374964749657496674967749687496974970749717497274973749747497574976749777497874979749807498174982749837498474985749867498774988749897499074991749927499374994749957499674997749987499975000750017500275003750047500575006750077500875009750107501175012750137501475015750167501775018750197502075021750227502375024750257502675027750287502975030750317503275033750347503575036750377503875039750407504175042750437504475045750467504775048750497505075051750527505375054750557505675057750587505975060750617506275063750647506575066750677506875069750707507175072750737507475075750767507775078750797508075081750827508375084750857508675087750887508975090750917509275093750947509575096750977509875099751007510175102751037510475105751067510775108751097511075111751127511375114751157511675117751187511975120751217512275123751247512575126751277512875129751307513175132751337513475135751367513775138751397514075141751427514375144751457514675147751487514975150751517515275153751547515575156751577515875159751607516175162751637516475165751667516775168751697517075171751727517375174751757517675177751787517975180751817518275183751847518575186751877518875189751907519175192751937519475195751967519775198751997520075201752027520375204752057520675207752087520975210752117521275213752147521575216752177521875219752207522175222752237522475225752267522775228752297523075231752327523375234752357523675237752387523975240752417524275243752447524575246752477524875249752507525175252752537525475255752567525775258752597526075261752627526375264752657526675267752687526975270752717527275273752747527575276752777527875279752807528175282752837528475285752867528775288752897529075291752927529375294752957529675297752987529975300753017530275303753047530575306753077530875309753107531175312753137531475315753167531775318753197532075321753227532375324753257532675327753287532975330753317533275333753347533575336753377533875339753407534175342753437534475345753467534775348753497535075351753527535375354753557535675357753587535975360753617536275363753647536575366753677536875369753707537175372753737537475375753767537775378753797538075381753827538375384753857538675387753887538975390753917539275393753947539575396753977539875399754007540175402754037540475405754067540775408754097541075411754127541375414754157541675417754187541975420754217542275423754247542575426754277542875429754307543175432754337543475435754367543775438754397544075441754427544375444754457544675447754487544975450754517545275453754547545575456754577545875459754607546175462754637546475465754667546775468754697547075471754727547375474754757547675477754787547975480754817548275483754847548575486754877548875489754907549175492754937549475495754967549775498754997550075501755027550375504755057550675507755087550975510755117551275513755147551575516755177551875519755207552175522755237552475525755267552775528755297553075531755327553375534755357553675537755387553975540755417554275543755447554575546755477554875549755507555175552755537555475555755567555775558755597556075561755627556375564755657556675567755687556975570755717557275573755747557575576755777557875579755807558175582755837558475585755867558775588755897559075591755927559375594755957559675597755987559975600756017560275603756047560575606756077560875609756107561175612756137561475615756167561775618756197562075621756227562375624756257562675627756287562975630756317563275633756347563575636756377563875639756407564175642756437564475645756467564775648756497565075651756527565375654756557565675657756587565975660756617566275663756647566575666756677566875669756707567175672756737567475675756767567775678756797568075681756827568375684756857568675687756887568975690756917569275693756947569575696756977569875699757007570175702757037570475705757067570775708757097571075711757127571375714757157571675717757187571975720757217572275723757247572575726757277572875729757307573175732757337573475735757367573775738757397574075741757427574375744757457574675747757487574975750757517575275753757547575575756757577575875759757607576175762757637576475765757667576775768757697577075771757727577375774757757577675777757787577975780757817578275783757847578575786757877578875789757907579175792757937579475795757967579775798757997580075801758027580375804758057580675807758087580975810758117581275813758147581575816758177581875819758207582175822758237582475825758267582775828758297583075831758327583375834758357583675837758387583975840758417584275843758447584575846758477584875849758507585175852758537585475855758567585775858758597586075861758627586375864758657586675867758687586975870758717587275873758747587575876758777587875879758807588175882758837588475885758867588775888758897589075891758927589375894758957589675897758987589975900759017590275903759047590575906759077590875909759107591175912759137591475915759167591775918759197592075921759227592375924759257592675927759287592975930759317593275933759347593575936759377593875939759407594175942759437594475945759467594775948759497595075951759527595375954759557595675957759587595975960759617596275963759647596575966759677596875969759707597175972759737597475975759767597775978759797598075981759827598375984759857598675987759887598975990759917599275993759947599575996759977599875999760007600176002760037600476005760067600776008760097601076011760127601376014760157601676017760187601976020760217602276023760247602576026760277602876029760307603176032760337603476035760367603776038760397604076041760427604376044760457604676047760487604976050760517605276053760547605576056760577605876059760607606176062760637606476065760667606776068760697607076071760727607376074760757607676077760787607976080760817608276083760847608576086760877608876089760907609176092760937609476095760967609776098760997610076101761027610376104761057610676107761087610976110761117611276113761147611576116761177611876119761207612176122761237612476125761267612776128761297613076131761327613376134761357613676137761387613976140761417614276143761447614576146761477614876149761507615176152761537615476155761567615776158761597616076161761627616376164761657616676167761687616976170761717617276173761747617576176761777617876179761807618176182761837618476185761867618776188761897619076191761927619376194761957619676197761987619976200762017620276203762047620576206762077620876209762107621176212762137621476215762167621776218762197622076221762227622376224762257622676227762287622976230762317623276233762347623576236762377623876239762407624176242762437624476245762467624776248762497625076251762527625376254762557625676257762587625976260762617626276263762647626576266762677626876269762707627176272762737627476275762767627776278762797628076281762827628376284762857628676287762887628976290762917629276293762947629576296762977629876299763007630176302763037630476305763067630776308763097631076311763127631376314763157631676317763187631976320763217632276323763247632576326763277632876329763307633176332763337633476335763367633776338763397634076341763427634376344763457634676347763487634976350763517635276353763547635576356763577635876359763607636176362763637636476365763667636776368763697637076371763727637376374763757637676377763787637976380763817638276383763847638576386763877638876389763907639176392763937639476395763967639776398763997640076401764027640376404764057640676407764087640976410764117641276413764147641576416764177641876419764207642176422764237642476425764267642776428764297643076431764327643376434764357643676437764387643976440764417644276443764447644576446764477644876449764507645176452764537645476455764567645776458764597646076461764627646376464764657646676467764687646976470764717647276473764747647576476764777647876479764807648176482764837648476485764867648776488764897649076491764927649376494764957649676497764987649976500765017650276503765047650576506765077650876509765107651176512765137651476515765167651776518765197652076521765227652376524765257652676527765287652976530765317653276533765347653576536765377653876539765407654176542765437654476545765467654776548765497655076551765527655376554765557655676557765587655976560765617656276563765647656576566765677656876569765707657176572765737657476575765767657776578765797658076581765827658376584765857658676587765887658976590765917659276593765947659576596765977659876599766007660176602766037660476605766067660776608766097661076611766127661376614766157661676617766187661976620766217662276623766247662576626766277662876629766307663176632766337663476635766367663776638766397664076641766427664376644766457664676647766487664976650766517665276653766547665576656766577665876659766607666176662766637666476665766667666776668766697667076671766727667376674766757667676677766787667976680766817668276683766847668576686766877668876689766907669176692766937669476695766967669776698766997670076701767027670376704767057670676707767087670976710767117671276713767147671576716767177671876719767207672176722767237672476725767267672776728767297673076731767327673376734767357673676737767387673976740767417674276743767447674576746767477674876749767507675176752767537675476755767567675776758767597676076761767627676376764767657676676767767687676976770767717677276773767747677576776767777677876779767807678176782767837678476785767867678776788767897679076791767927679376794767957679676797767987679976800768017680276803768047680576806768077680876809768107681176812768137681476815768167681776818768197682076821768227682376824768257682676827768287682976830768317683276833768347683576836768377683876839768407684176842768437684476845768467684776848768497685076851768527685376854768557685676857768587685976860768617686276863768647686576866768677686876869768707687176872768737687476875768767687776878768797688076881768827688376884768857688676887768887688976890768917689276893768947689576896768977689876899769007690176902769037690476905769067690776908769097691076911769127691376914769157691676917769187691976920769217692276923769247692576926769277692876929769307693176932769337693476935769367693776938769397694076941769427694376944769457694676947769487694976950769517695276953769547695576956769577695876959769607696176962769637696476965769667696776968769697697076971769727697376974769757697676977769787697976980769817698276983769847698576986769877698876989769907699176992769937699476995769967699776998769997700077001770027700377004770057700677007770087700977010770117701277013770147701577016770177701877019770207702177022770237702477025770267702777028770297703077031770327703377034770357703677037770387703977040770417704277043770447704577046770477704877049770507705177052770537705477055770567705777058770597706077061770627706377064770657706677067770687706977070770717707277073770747707577076770777707877079770807708177082770837708477085770867708777088770897709077091770927709377094770957709677097770987709977100771017710277103771047710577106771077710877109771107711177112771137711477115771167711777118771197712077121771227712377124771257712677127771287712977130771317713277133771347713577136771377713877139771407714177142771437714477145771467714777148771497715077151771527715377154771557715677157771587715977160771617716277163771647716577166771677716877169771707717177172771737717477175771767717777178771797718077181771827718377184771857718677187771887718977190771917719277193771947719577196771977719877199772007720177202772037720477205772067720777208772097721077211772127721377214772157721677217772187721977220772217722277223772247722577226772277722877229772307723177232772337723477235772367723777238772397724077241772427724377244772457724677247772487724977250772517725277253772547725577256772577725877259772607726177262772637726477265772667726777268772697727077271772727727377274772757727677277772787727977280772817728277283772847728577286772877728877289772907729177292772937729477295772967729777298772997730077301773027730377304773057730677307773087730977310773117731277313773147731577316773177731877319773207732177322773237732477325773267732777328773297733077331773327733377334773357733677337773387733977340773417734277343773447734577346773477734877349773507735177352773537735477355773567735777358773597736077361773627736377364773657736677367773687736977370773717737277373773747737577376773777737877379773807738177382773837738477385773867738777388773897739077391773927739377394773957739677397773987739977400774017740277403774047740577406774077740877409774107741177412774137741477415774167741777418774197742077421774227742377424774257742677427774287742977430774317743277433774347743577436774377743877439774407744177442774437744477445774467744777448774497745077451774527745377454774557745677457774587745977460774617746277463774647746577466774677746877469774707747177472774737747477475774767747777478774797748077481774827748377484774857748677487774887748977490774917749277493774947749577496774977749877499775007750177502775037750477505775067750777508775097751077511775127751377514775157751677517775187751977520775217752277523775247752577526775277752877529775307753177532775337753477535775367753777538775397754077541775427754377544775457754677547775487754977550775517755277553775547755577556775577755877559775607756177562775637756477565775667756777568775697757077571775727757377574775757757677577775787757977580775817758277583775847758577586775877758877589775907759177592775937759477595775967759777598775997760077601776027760377604776057760677607776087760977610776117761277613776147761577616776177761877619776207762177622776237762477625776267762777628776297763077631776327763377634776357763677637776387763977640776417764277643776447764577646776477764877649776507765177652776537765477655776567765777658776597766077661776627766377664776657766677667776687766977670776717767277673776747767577676776777767877679776807768177682776837768477685776867768777688776897769077691776927769377694776957769677697776987769977700777017770277703777047770577706777077770877709777107771177712777137771477715777167771777718777197772077721777227772377724777257772677727777287772977730777317773277733777347773577736777377773877739777407774177742777437774477745777467774777748777497775077751777527775377754777557775677757777587775977760777617776277763777647776577766777677776877769777707777177772777737777477775777767777777778777797778077781777827778377784777857778677787777887778977790777917779277793777947779577796777977779877799778007780177802778037780477805778067780777808778097781077811778127781377814778157781677817778187781977820778217782277823778247782577826778277782877829778307783177832778337783477835778367783777838778397784077841778427784377844778457784677847778487784977850778517785277853778547785577856778577785877859778607786177862778637786477865778667786777868778697787077871778727787377874778757787677877778787787977880778817788277883778847788577886778877788877889778907789177892778937789477895778967789777898778997790077901779027790377904779057790677907779087790977910779117791277913779147791577916779177791877919779207792177922779237792477925779267792777928779297793077931779327793377934779357793677937779387793977940779417794277943779447794577946779477794877949779507795177952779537795477955779567795777958779597796077961779627796377964779657796677967779687796977970779717797277973779747797577976779777797877979779807798177982779837798477985779867798777988779897799077991779927799377994779957799677997779987799978000780017800278003780047800578006780077800878009780107801178012780137801478015780167801778018780197802078021780227802378024780257802678027780287802978030780317803278033780347803578036780377803878039780407804178042780437804478045780467804778048780497805078051780527805378054780557805678057780587805978060780617806278063780647806578066780677806878069780707807178072780737807478075780767807778078780797808078081780827808378084780857808678087780887808978090780917809278093780947809578096780977809878099781007810178102781037810478105781067810778108781097811078111781127811378114781157811678117781187811978120781217812278123781247812578126781277812878129781307813178132781337813478135781367813778138781397814078141781427814378144781457814678147781487814978150781517815278153781547815578156781577815878159781607816178162781637816478165781667816778168781697817078171781727817378174781757817678177781787817978180781817818278183781847818578186781877818878189781907819178192781937819478195781967819778198781997820078201782027820378204782057820678207782087820978210782117821278213782147821578216782177821878219782207822178222782237822478225782267822778228782297823078231782327823378234782357823678237782387823978240782417824278243782447824578246782477824878249782507825178252782537825478255782567825778258782597826078261782627826378264782657826678267782687826978270782717827278273782747827578276782777827878279782807828178282782837828478285782867828778288782897829078291782927829378294782957829678297782987829978300783017830278303783047830578306783077830878309783107831178312783137831478315783167831778318783197832078321783227832378324783257832678327783287832978330783317833278333783347833578336783377833878339783407834178342783437834478345783467834778348783497835078351783527835378354783557835678357783587835978360783617836278363783647836578366783677836878369783707837178372783737837478375783767837778378783797838078381783827838378384783857838678387783887838978390783917839278393783947839578396783977839878399784007840178402784037840478405784067840778408784097841078411784127841378414784157841678417784187841978420784217842278423784247842578426784277842878429784307843178432784337843478435784367843778438784397844078441784427844378444784457844678447784487844978450784517845278453784547845578456784577845878459784607846178462784637846478465784667846778468784697847078471784727847378474784757847678477784787847978480784817848278483784847848578486784877848878489784907849178492784937849478495784967849778498784997850078501785027850378504785057850678507785087850978510785117851278513785147851578516785177851878519785207852178522785237852478525785267852778528785297853078531785327853378534785357853678537785387853978540785417854278543785447854578546785477854878549785507855178552785537855478555785567855778558785597856078561785627856378564785657856678567785687856978570785717857278573785747857578576785777857878579785807858178582785837858478585785867858778588785897859078591785927859378594785957859678597785987859978600786017860278603786047860578606786077860878609786107861178612786137861478615786167861778618786197862078621786227862378624786257862678627786287862978630786317863278633786347863578636786377863878639786407864178642786437864478645786467864778648786497865078651786527865378654786557865678657786587865978660786617866278663786647866578666786677866878669786707867178672786737867478675786767867778678786797868078681786827868378684786857868678687786887868978690786917869278693786947869578696786977869878699787007870178702787037870478705787067870778708787097871078711787127871378714787157871678717787187871978720787217872278723787247872578726787277872878729787307873178732787337873478735787367873778738787397874078741787427874378744787457874678747787487874978750787517875278753787547875578756787577875878759787607876178762787637876478765787667876778768787697877078771787727877378774787757877678777787787877978780787817878278783787847878578786787877878878789787907879178792787937879478795787967879778798787997880078801788027880378804788057880678807788087880978810788117881278813788147881578816788177881878819788207882178822788237882478825788267882778828788297883078831788327883378834788357883678837788387883978840788417884278843788447884578846788477884878849788507885178852788537885478855788567885778858788597886078861788627886378864788657886678867788687886978870788717887278873788747887578876788777887878879788807888178882788837888478885788867888778888788897889078891788927889378894788957889678897788987889978900789017890278903789047890578906789077890878909789107891178912789137891478915789167891778918789197892078921789227892378924789257892678927789287892978930789317893278933789347893578936789377893878939789407894178942789437894478945789467894778948789497895078951789527895378954789557895678957789587895978960789617896278963789647896578966789677896878969789707897178972789737897478975789767897778978789797898078981789827898378984789857898678987789887898978990789917899278993789947899578996789977899878999790007900179002790037900479005790067900779008790097901079011790127901379014790157901679017790187901979020790217902279023790247902579026790277902879029790307903179032790337903479035790367903779038790397904079041790427904379044790457904679047790487904979050790517905279053790547905579056790577905879059790607906179062790637906479065790667906779068790697907079071790727907379074790757907679077790787907979080790817908279083790847908579086790877908879089790907909179092790937909479095790967909779098790997910079101791027910379104791057910679107791087910979110791117911279113791147911579116791177911879119791207912179122791237912479125791267912779128791297913079131791327913379134791357913679137791387913979140791417914279143791447914579146791477914879149791507915179152791537915479155791567915779158791597916079161791627916379164791657916679167791687916979170791717917279173791747917579176791777917879179791807918179182791837918479185791867918779188791897919079191791927919379194791957919679197791987919979200792017920279203792047920579206792077920879209792107921179212792137921479215792167921779218792197922079221792227922379224792257922679227792287922979230792317923279233792347923579236792377923879239792407924179242792437924479245792467924779248792497925079251792527925379254792557925679257792587925979260792617926279263792647926579266792677926879269792707927179272792737927479275792767927779278792797928079281792827928379284792857928679287792887928979290792917929279293792947929579296792977929879299793007930179302793037930479305793067930779308793097931079311793127931379314793157931679317793187931979320793217932279323793247932579326793277932879329793307933179332793337933479335793367933779338793397934079341793427934379344793457934679347793487934979350793517935279353793547935579356793577935879359793607936179362793637936479365793667936779368793697937079371793727937379374793757937679377793787937979380793817938279383793847938579386793877938879389793907939179392793937939479395793967939779398793997940079401794027940379404794057940679407794087940979410794117941279413794147941579416794177941879419794207942179422794237942479425794267942779428794297943079431794327943379434794357943679437794387943979440794417944279443794447944579446794477944879449794507945179452794537945479455794567945779458794597946079461794627946379464794657946679467794687946979470794717947279473794747947579476794777947879479794807948179482794837948479485794867948779488794897949079491794927949379494794957949679497794987949979500795017950279503795047950579506795077950879509795107951179512795137951479515795167951779518795197952079521795227952379524795257952679527795287952979530795317953279533795347953579536795377953879539795407954179542795437954479545795467954779548795497955079551795527955379554795557955679557795587955979560795617956279563795647956579566795677956879569795707957179572795737957479575795767957779578795797958079581795827958379584795857958679587795887958979590795917959279593795947959579596795977959879599796007960179602796037960479605796067960779608796097961079611796127961379614796157961679617796187961979620796217962279623796247962579626796277962879629796307963179632796337963479635796367963779638796397964079641796427964379644796457964679647796487964979650796517965279653796547965579656796577965879659796607966179662796637966479665796667966779668796697967079671796727967379674796757967679677796787967979680796817968279683796847968579686796877968879689796907969179692796937969479695796967969779698796997970079701797027970379704797057970679707797087970979710797117971279713797147971579716797177971879719797207972179722797237972479725797267972779728797297973079731797327973379734797357973679737797387973979740797417974279743797447974579746797477974879749797507975179752797537975479755797567975779758797597976079761797627976379764797657976679767797687976979770797717977279773797747977579776797777977879779797807978179782797837978479785797867978779788797897979079791797927979379794797957979679797797987979979800798017980279803798047980579806798077980879809798107981179812798137981479815798167981779818798197982079821798227982379824798257982679827798287982979830798317983279833798347983579836798377983879839798407984179842798437984479845798467984779848798497985079851798527985379854798557985679857798587985979860798617986279863798647986579866798677986879869798707987179872798737987479875798767987779878798797988079881798827988379884798857988679887798887988979890798917989279893798947989579896798977989879899799007990179902799037990479905799067990779908799097991079911799127991379914799157991679917799187991979920799217992279923799247992579926799277992879929799307993179932799337993479935799367993779938799397994079941799427994379944799457994679947799487994979950799517995279953799547995579956799577995879959799607996179962799637996479965799667996779968799697997079971799727997379974799757997679977799787997979980799817998279983799847998579986799877998879989799907999179992799937999479995799967999779998799998000080001800028000380004800058000680007800088000980010800118001280013800148001580016800178001880019800208002180022800238002480025800268002780028800298003080031800328003380034800358003680037800388003980040800418004280043800448004580046800478004880049800508005180052800538005480055800568005780058800598006080061800628006380064800658006680067800688006980070800718007280073800748007580076800778007880079800808008180082800838008480085800868008780088800898009080091800928009380094800958009680097800988009980100801018010280103801048010580106801078010880109801108011180112801138011480115801168011780118801198012080121801228012380124801258012680127801288012980130801318013280133801348013580136801378013880139801408014180142801438014480145801468014780148801498015080151801528015380154801558015680157801588015980160801618016280163801648016580166801678016880169801708017180172801738017480175801768017780178801798018080181801828018380184801858018680187801888018980190801918019280193801948019580196801978019880199802008020180202802038020480205802068020780208802098021080211802128021380214802158021680217802188021980220802218022280223802248022580226802278022880229802308023180232802338023480235802368023780238802398024080241802428024380244802458024680247802488024980250802518025280253802548025580256802578025880259802608026180262802638026480265802668026780268802698027080271802728027380274802758027680277802788027980280802818028280283802848028580286802878028880289802908029180292802938029480295802968029780298802998030080301803028030380304803058030680307803088030980310803118031280313803148031580316803178031880319803208032180322803238032480325803268032780328803298033080331803328033380334803358033680337803388033980340803418034280343803448034580346803478034880349803508035180352803538035480355803568035780358803598036080361803628036380364803658036680367803688036980370803718037280373803748037580376803778037880379803808038180382803838038480385803868038780388803898039080391803928039380394803958039680397803988039980400804018040280403804048040580406804078040880409804108041180412804138041480415804168041780418804198042080421804228042380424804258042680427804288042980430804318043280433804348043580436804378043880439804408044180442804438044480445804468044780448804498045080451804528045380454804558045680457804588045980460804618046280463804648046580466804678046880469804708047180472804738047480475804768047780478804798048080481804828048380484804858048680487804888048980490804918049280493804948049580496804978049880499805008050180502805038050480505805068050780508805098051080511805128051380514805158051680517805188051980520805218052280523805248052580526805278052880529805308053180532805338053480535805368053780538805398054080541805428054380544805458054680547805488054980550805518055280553805548055580556805578055880559805608056180562805638056480565805668056780568805698057080571805728057380574805758057680577805788057980580805818058280583805848058580586805878058880589805908059180592805938059480595805968059780598805998060080601806028060380604806058060680607806088060980610806118061280613806148061580616806178061880619806208062180622806238062480625806268062780628806298063080631806328063380634806358063680637806388063980640806418064280643806448064580646806478064880649806508065180652806538065480655806568065780658806598066080661806628066380664806658066680667806688066980670806718067280673806748067580676806778067880679806808068180682806838068480685806868068780688806898069080691806928069380694806958069680697806988069980700807018070280703807048070580706807078070880709807108071180712807138071480715807168071780718807198072080721807228072380724807258072680727807288072980730807318073280733807348073580736807378073880739807408074180742807438074480745807468074780748807498075080751807528075380754807558075680757807588075980760807618076280763807648076580766807678076880769807708077180772807738077480775807768077780778807798078080781807828078380784807858078680787807888078980790807918079280793807948079580796807978079880799808008080180802808038080480805808068080780808808098081080811808128081380814808158081680817808188081980820808218082280823808248082580826808278082880829808308083180832808338083480835808368083780838808398084080841808428084380844808458084680847808488084980850808518085280853808548085580856808578085880859808608086180862808638086480865808668086780868808698087080871808728087380874808758087680877808788087980880808818088280883808848088580886808878088880889808908089180892808938089480895808968089780898808998090080901809028090380904809058090680907809088090980910809118091280913809148091580916809178091880919809208092180922809238092480925809268092780928809298093080931809328093380934809358093680937809388093980940809418094280943809448094580946809478094880949809508095180952809538095480955809568095780958809598096080961809628096380964809658096680967809688096980970809718097280973809748097580976809778097880979809808098180982809838098480985809868098780988809898099080991809928099380994809958099680997809988099981000810018100281003810048100581006810078100881009810108101181012810138101481015810168101781018810198102081021810228102381024810258102681027810288102981030810318103281033810348103581036810378103881039810408104181042810438104481045810468104781048810498105081051810528105381054810558105681057810588105981060810618106281063810648106581066810678106881069810708107181072810738107481075810768107781078810798108081081810828108381084810858108681087810888108981090810918109281093810948109581096810978109881099811008110181102811038110481105811068110781108811098111081111811128111381114811158111681117811188111981120811218112281123811248112581126811278112881129811308113181132811338113481135811368113781138811398114081141811428114381144811458114681147811488114981150811518115281153811548115581156811578115881159811608116181162811638116481165811668116781168811698117081171811728117381174811758117681177811788117981180811818118281183811848118581186811878118881189811908119181192811938119481195811968119781198811998120081201812028120381204812058120681207812088120981210812118121281213812148121581216812178121881219812208122181222812238122481225812268122781228812298123081231812328123381234812358123681237812388123981240812418124281243812448124581246812478124881249812508125181252812538125481255812568125781258812598126081261812628126381264812658126681267812688126981270812718127281273812748127581276812778127881279812808128181282812838128481285812868128781288812898129081291812928129381294812958129681297812988129981300813018130281303813048130581306813078130881309813108131181312813138131481315813168131781318813198132081321813228132381324813258132681327813288132981330813318133281333813348133581336813378133881339813408134181342813438134481345813468134781348813498135081351813528135381354813558135681357813588135981360813618136281363813648136581366813678136881369813708137181372813738137481375813768137781378813798138081381813828138381384813858138681387813888138981390813918139281393813948139581396813978139881399814008140181402814038140481405814068140781408814098141081411814128141381414814158141681417814188141981420814218142281423814248142581426814278142881429814308143181432814338143481435814368143781438814398144081441814428144381444814458144681447814488144981450814518145281453814548145581456814578145881459814608146181462814638146481465814668146781468814698147081471814728147381474814758147681477814788147981480814818148281483814848148581486814878148881489814908149181492814938149481495814968149781498814998150081501815028150381504815058150681507815088150981510815118151281513815148151581516815178151881519815208152181522815238152481525815268152781528815298153081531815328153381534815358153681537815388153981540815418154281543815448154581546815478154881549815508155181552815538155481555815568155781558815598156081561815628156381564815658156681567815688156981570815718157281573815748157581576815778157881579815808158181582815838158481585815868158781588815898159081591815928159381594815958159681597815988159981600816018160281603816048160581606816078160881609816108161181612816138161481615816168161781618816198162081621816228162381624816258162681627816288162981630816318163281633816348163581636816378163881639816408164181642816438164481645816468164781648816498165081651816528165381654816558165681657816588165981660816618166281663816648166581666816678166881669816708167181672816738167481675816768167781678816798168081681816828168381684816858168681687816888168981690816918169281693816948169581696816978169881699817008170181702817038170481705817068170781708817098171081711817128171381714817158171681717817188171981720817218172281723817248172581726817278172881729817308173181732817338173481735817368173781738817398174081741817428174381744817458174681747817488174981750817518175281753817548175581756817578175881759817608176181762817638176481765817668176781768817698177081771817728177381774817758177681777817788177981780817818178281783817848178581786817878178881789817908179181792817938179481795817968179781798817998180081801818028180381804818058180681807818088180981810818118181281813818148181581816818178181881819818208182181822818238182481825818268182781828818298183081831818328183381834818358183681837818388183981840818418184281843818448184581846818478184881849818508185181852818538185481855818568185781858818598186081861818628186381864818658186681867818688186981870818718187281873818748187581876818778187881879818808188181882818838188481885818868188781888818898189081891818928189381894818958189681897818988189981900819018190281903819048190581906819078190881909819108191181912819138191481915819168191781918819198192081921819228192381924819258192681927819288192981930819318193281933819348193581936819378193881939819408194181942819438194481945819468194781948819498195081951819528195381954819558195681957819588195981960819618196281963819648196581966819678196881969819708197181972819738197481975819768197781978819798198081981819828198381984819858198681987819888198981990819918199281993819948199581996819978199881999820008200182002820038200482005820068200782008820098201082011820128201382014820158201682017820188201982020820218202282023820248202582026820278202882029820308203182032820338203482035820368203782038820398204082041820428204382044820458204682047820488204982050820518205282053820548205582056820578205882059820608206182062820638206482065820668206782068820698207082071820728207382074820758207682077820788207982080820818208282083820848208582086820878208882089820908209182092820938209482095820968209782098820998210082101821028210382104821058210682107821088210982110821118211282113821148211582116821178211882119821208212182122821238212482125821268212782128821298213082131821328213382134821358213682137821388213982140821418214282143821448214582146821478214882149821508215182152821538215482155821568215782158821598216082161821628216382164821658216682167821688216982170821718217282173821748217582176821778217882179821808218182182821838218482185821868218782188821898219082191821928219382194821958219682197821988219982200822018220282203822048220582206822078220882209822108221182212822138221482215822168221782218822198222082221822228222382224822258222682227822288222982230822318223282233822348223582236822378223882239822408224182242822438224482245822468224782248822498225082251822528225382254822558225682257822588225982260822618226282263822648226582266822678226882269822708227182272822738227482275822768227782278822798228082281822828228382284822858228682287822888228982290822918229282293822948229582296822978229882299823008230182302823038230482305823068230782308823098231082311823128231382314823158231682317823188231982320823218232282323823248232582326823278232882329823308233182332823338233482335823368233782338823398234082341823428234382344823458234682347823488234982350823518235282353823548235582356823578235882359823608236182362823638236482365823668236782368823698237082371823728237382374823758237682377823788237982380823818238282383823848238582386823878238882389823908239182392823938239482395823968239782398823998240082401824028240382404824058240682407824088240982410824118241282413824148241582416824178241882419824208242182422824238242482425824268242782428824298243082431824328243382434824358243682437824388243982440824418244282443824448244582446824478244882449824508245182452824538245482455824568245782458824598246082461824628246382464824658246682467824688246982470824718247282473824748247582476824778247882479824808248182482824838248482485824868248782488824898249082491824928249382494824958249682497824988249982500825018250282503825048250582506825078250882509825108251182512825138251482515825168251782518825198252082521825228252382524825258252682527825288252982530825318253282533825348253582536825378253882539825408254182542825438254482545825468254782548825498255082551825528255382554825558255682557825588255982560825618256282563825648256582566825678256882569825708257182572825738257482575825768257782578825798258082581825828258382584825858258682587825888258982590825918259282593825948259582596825978259882599826008260182602826038260482605826068260782608826098261082611826128261382614826158261682617826188261982620826218262282623826248262582626826278262882629826308263182632826338263482635826368263782638826398264082641826428264382644826458264682647826488264982650826518265282653826548265582656826578265882659826608266182662826638266482665826668266782668826698267082671826728267382674826758267682677826788267982680826818268282683826848268582686826878268882689826908269182692826938269482695826968269782698826998270082701827028270382704827058270682707827088270982710827118271282713827148271582716827178271882719827208272182722827238272482725827268272782728827298273082731827328273382734827358273682737827388273982740827418274282743827448274582746827478274882749827508275182752827538275482755827568275782758827598276082761827628276382764827658276682767827688276982770827718277282773827748277582776827778277882779827808278182782827838278482785827868278782788827898279082791827928279382794827958279682797827988279982800828018280282803828048280582806828078280882809828108281182812828138281482815828168281782818828198282082821828228282382824828258282682827828288282982830828318283282833828348283582836828378283882839828408284182842828438284482845828468284782848828498285082851828528285382854828558285682857828588285982860828618286282863828648286582866828678286882869828708287182872828738287482875828768287782878828798288082881828828288382884828858288682887828888288982890828918289282893828948289582896828978289882899829008290182902829038290482905829068290782908829098291082911829128291382914829158291682917829188291982920829218292282923829248292582926829278292882929829308293182932829338293482935829368293782938829398294082941829428294382944829458294682947829488294982950829518295282953829548295582956829578295882959829608296182962829638296482965829668296782968829698297082971829728297382974829758297682977829788297982980829818298282983829848298582986829878298882989829908299182992829938299482995829968299782998829998300083001830028300383004830058300683007830088300983010830118301283013830148301583016830178301883019830208302183022830238302483025830268302783028830298303083031830328303383034830358303683037830388303983040830418304283043830448304583046830478304883049830508305183052830538305483055830568305783058830598306083061830628306383064830658306683067830688306983070830718307283073830748307583076830778307883079830808308183082830838308483085830868308783088830898309083091830928309383094830958309683097830988309983100831018310283103831048310583106831078310883109831108311183112831138311483115831168311783118831198312083121831228312383124831258312683127831288312983130831318313283133831348313583136831378313883139831408314183142831438314483145831468314783148831498315083151831528315383154831558315683157831588315983160831618316283163831648316583166831678316883169831708317183172831738317483175831768317783178831798318083181831828318383184831858318683187831888318983190831918319283193831948319583196831978319883199832008320183202832038320483205832068320783208832098321083211832128321383214832158321683217832188321983220832218322283223832248322583226832278322883229832308323183232832338323483235832368323783238832398324083241832428324383244832458324683247832488324983250832518325283253832548325583256832578325883259832608326183262832638326483265832668326783268832698327083271832728327383274832758327683277832788327983280832818328283283832848328583286832878328883289832908329183292832938329483295832968329783298832998330083301833028330383304833058330683307833088330983310833118331283313833148331583316833178331883319833208332183322833238332483325833268332783328833298333083331833328333383334833358333683337833388333983340833418334283343833448334583346833478334883349833508335183352833538335483355833568335783358833598336083361833628336383364833658336683367833688336983370833718337283373833748337583376833778337883379833808338183382833838338483385833868338783388833898339083391833928339383394833958339683397833988339983400834018340283403834048340583406834078340883409834108341183412834138341483415834168341783418834198342083421834228342383424834258342683427834288342983430834318343283433834348343583436834378343883439834408344183442834438344483445834468344783448834498345083451834528345383454834558345683457834588345983460834618346283463834648346583466834678346883469834708347183472834738347483475834768347783478834798348083481834828348383484834858348683487834888348983490834918349283493834948349583496834978349883499835008350183502835038350483505835068350783508835098351083511835128351383514835158351683517835188351983520835218352283523835248352583526835278352883529835308353183532835338353483535835368353783538835398354083541835428354383544835458354683547835488354983550835518355283553835548355583556835578355883559835608356183562835638356483565835668356783568835698357083571835728357383574835758357683577835788357983580835818358283583835848358583586835878358883589835908359183592835938359483595835968359783598835998360083601836028360383604836058360683607836088360983610836118361283613836148361583616836178361883619836208362183622836238362483625836268362783628836298363083631836328363383634836358363683637836388363983640836418364283643836448364583646836478364883649836508365183652836538365483655836568365783658836598366083661836628366383664836658366683667836688366983670836718367283673836748367583676836778367883679836808368183682836838368483685836868368783688836898369083691836928369383694836958369683697836988369983700837018370283703837048370583706837078370883709837108371183712837138371483715837168371783718837198372083721837228372383724837258372683727837288372983730837318373283733837348373583736837378373883739837408374183742837438374483745837468374783748837498375083751837528375383754837558375683757837588375983760837618376283763837648376583766837678376883769837708377183772837738377483775837768377783778837798378083781837828378383784837858378683787837888378983790837918379283793837948379583796837978379883799838008380183802838038380483805838068380783808838098381083811838128381383814838158381683817838188381983820838218382283823838248382583826838278382883829838308383183832838338383483835838368383783838838398384083841838428384383844838458384683847838488384983850838518385283853838548385583856838578385883859838608386183862838638386483865838668386783868838698387083871838728387383874838758387683877838788387983880838818388283883838848388583886838878388883889838908389183892838938389483895838968389783898838998390083901839028390383904839058390683907839088390983910839118391283913839148391583916839178391883919839208392183922839238392483925839268392783928839298393083931839328393383934839358393683937839388393983940839418394283943839448394583946839478394883949839508395183952839538395483955839568395783958839598396083961839628396383964839658396683967839688396983970839718397283973839748397583976839778397883979839808398183982839838398483985839868398783988839898399083991839928399383994839958399683997839988399984000840018400284003840048400584006840078400884009840108401184012840138401484015840168401784018840198402084021840228402384024840258402684027840288402984030840318403284033840348403584036840378403884039840408404184042840438404484045840468404784048840498405084051840528405384054840558405684057840588405984060840618406284063840648406584066840678406884069840708407184072840738407484075840768407784078840798408084081840828408384084840858408684087840888408984090840918409284093840948409584096840978409884099841008410184102841038410484105841068410784108841098411084111841128411384114841158411684117841188411984120841218412284123841248412584126841278412884129841308413184132841338413484135841368413784138841398414084141841428414384144841458414684147841488414984150841518415284153841548415584156841578415884159841608416184162841638416484165841668416784168841698417084171841728417384174841758417684177841788417984180841818418284183841848418584186841878418884189841908419184192841938419484195841968419784198841998420084201842028420384204842058420684207842088420984210842118421284213842148421584216842178421884219842208422184222842238422484225842268422784228842298423084231842328423384234842358423684237842388423984240842418424284243842448424584246842478424884249842508425184252842538425484255842568425784258842598426084261842628426384264842658426684267842688426984270842718427284273842748427584276842778427884279842808428184282842838428484285842868428784288842898429084291842928429384294842958429684297842988429984300843018430284303843048430584306843078430884309843108431184312843138431484315843168431784318843198432084321843228432384324843258432684327843288432984330843318433284333843348433584336843378433884339843408434184342843438434484345843468434784348843498435084351843528435384354843558435684357843588435984360843618436284363843648436584366843678436884369843708437184372843738437484375843768437784378843798438084381843828438384384843858438684387843888438984390843918439284393843948439584396843978439884399844008440184402844038440484405844068440784408844098441084411844128441384414844158441684417844188441984420844218442284423844248442584426844278442884429844308443184432844338443484435844368443784438844398444084441844428444384444844458444684447844488444984450844518445284453844548445584456844578445884459844608446184462844638446484465844668446784468844698447084471844728447384474844758447684477844788447984480844818448284483844848448584486844878448884489844908449184492844938449484495844968449784498844998450084501845028450384504845058450684507845088450984510845118451284513845148451584516845178451884519845208452184522845238452484525845268452784528845298453084531845328453384534845358453684537845388453984540845418454284543845448454584546845478454884549845508455184552845538455484555845568455784558845598456084561845628456384564845658456684567845688456984570845718457284573845748457584576845778457884579845808458184582845838458484585845868458784588845898459084591845928459384594845958459684597845988459984600846018460284603846048460584606846078460884609846108461184612846138461484615846168461784618846198462084621846228462384624846258462684627846288462984630846318463284633846348463584636846378463884639846408464184642846438464484645846468464784648846498465084651846528465384654846558465684657846588465984660846618466284663846648466584666846678466884669846708467184672846738467484675846768467784678846798468084681846828468384684846858468684687846888468984690846918469284693846948469584696846978469884699847008470184702847038470484705847068470784708847098471084711847128471384714847158471684717847188471984720847218472284723847248472584726847278472884729847308473184732847338473484735847368473784738847398474084741847428474384744847458474684747847488474984750847518475284753847548475584756847578475884759847608476184762847638476484765847668476784768847698477084771847728477384774847758477684777847788477984780847818478284783847848478584786847878478884789847908479184792847938479484795847968479784798847998480084801848028480384804848058480684807848088480984810848118481284813848148481584816848178481884819848208482184822848238482484825848268482784828848298483084831848328483384834848358483684837848388483984840848418484284843848448484584846848478484884849848508485184852848538485484855848568485784858848598486084861848628486384864848658486684867848688486984870848718487284873848748487584876848778487884879848808488184882848838488484885848868488784888848898489084891848928489384894848958489684897848988489984900849018490284903849048490584906849078490884909849108491184912849138491484915849168491784918849198492084921849228492384924849258492684927849288492984930849318493284933849348493584936849378493884939849408494184942849438494484945849468494784948849498495084951849528495384954849558495684957849588495984960849618496284963849648496584966849678496884969849708497184972849738497484975849768497784978849798498084981849828498384984849858498684987849888498984990849918499284993849948499584996849978499884999850008500185002850038500485005850068500785008850098501085011850128501385014850158501685017850188501985020850218502285023850248502585026850278502885029850308503185032850338503485035850368503785038850398504085041850428504385044850458504685047850488504985050850518505285053850548505585056850578505885059850608506185062850638506485065850668506785068850698507085071850728507385074850758507685077850788507985080850818508285083850848508585086850878508885089850908509185092850938509485095850968509785098850998510085101851028510385104851058510685107851088510985110851118511285113851148511585116851178511885119851208512185122851238512485125851268512785128851298513085131851328513385134851358513685137851388513985140851418514285143851448514585146851478514885149851508515185152851538515485155851568515785158851598516085161851628516385164851658516685167851688516985170851718517285173851748517585176851778517885179851808518185182851838518485185851868518785188851898519085191851928519385194851958519685197851988519985200852018520285203852048520585206852078520885209852108521185212852138521485215852168521785218852198522085221852228522385224852258522685227852288522985230852318523285233852348523585236852378523885239852408524185242852438524485245852468524785248852498525085251852528525385254852558525685257852588525985260852618526285263852648526585266852678526885269852708527185272852738527485275852768527785278852798528085281852828528385284852858528685287852888528985290852918529285293852948529585296852978529885299853008530185302853038530485305853068530785308853098531085311853128531385314853158531685317853188531985320853218532285323853248532585326853278532885329853308533185332853338533485335853368533785338853398534085341853428534385344853458534685347853488534985350853518535285353853548535585356853578535885359853608536185362853638536485365853668536785368853698537085371853728537385374853758537685377853788537985380853818538285383853848538585386853878538885389853908539185392853938539485395853968539785398853998540085401854028540385404854058540685407854088540985410854118541285413854148541585416854178541885419854208542185422854238542485425854268542785428854298543085431854328543385434854358543685437854388543985440854418544285443854448544585446854478544885449854508545185452854538545485455854568545785458854598546085461854628546385464854658546685467854688546985470854718547285473854748547585476854778547885479854808548185482854838548485485854868548785488854898549085491854928549385494854958549685497854988549985500855018550285503855048550585506855078550885509855108551185512855138551485515855168551785518855198552085521855228552385524855258552685527855288552985530855318553285533855348553585536855378553885539855408554185542855438554485545855468554785548855498555085551855528555385554855558555685557855588555985560855618556285563855648556585566855678556885569855708557185572855738557485575855768557785578855798558085581855828558385584855858558685587855888558985590855918559285593855948559585596855978559885599856008560185602856038560485605856068560785608856098561085611856128561385614856158561685617856188561985620856218562285623856248562585626856278562885629856308563185632856338563485635856368563785638856398564085641856428564385644856458564685647856488564985650856518565285653856548565585656856578565885659856608566185662856638566485665856668566785668856698567085671856728567385674856758567685677856788567985680856818568285683856848568585686856878568885689856908569185692856938569485695856968569785698856998570085701857028570385704857058570685707857088570985710857118571285713857148571585716857178571885719857208572185722857238572485725857268572785728857298573085731857328573385734857358573685737857388573985740857418574285743857448574585746857478574885749857508575185752857538575485755857568575785758857598576085761857628576385764857658576685767857688576985770857718577285773857748577585776857778577885779857808578185782857838578485785857868578785788857898579085791857928579385794857958579685797857988579985800858018580285803858048580585806858078580885809858108581185812858138581485815858168581785818858198582085821858228582385824858258582685827858288582985830858318583285833858348583585836858378583885839858408584185842858438584485845858468584785848858498585085851858528585385854858558585685857858588585985860858618586285863858648586585866858678586885869858708587185872858738587485875858768587785878858798588085881858828588385884858858588685887858888588985890858918589285893858948589585896858978589885899859008590185902859038590485905859068590785908859098591085911859128591385914859158591685917859188591985920859218592285923859248592585926859278592885929859308593185932859338593485935859368593785938859398594085941859428594385944859458594685947859488594985950859518595285953859548595585956859578595885959859608596185962859638596485965859668596785968859698597085971859728597385974859758597685977859788597985980859818598285983859848598585986859878598885989859908599185992859938599485995859968599785998859998600086001860028600386004860058600686007860088600986010860118601286013860148601586016860178601886019860208602186022860238602486025860268602786028860298603086031860328603386034860358603686037860388603986040860418604286043860448604586046860478604886049860508605186052860538605486055860568605786058860598606086061860628606386064860658606686067860688606986070860718607286073860748607586076860778607886079860808608186082860838608486085860868608786088860898609086091860928609386094860958609686097860988609986100861018610286103861048610586106861078610886109861108611186112861138611486115861168611786118861198612086121861228612386124861258612686127861288612986130861318613286133861348613586136861378613886139861408614186142861438614486145861468614786148861498615086151861528615386154861558615686157861588615986160861618616286163861648616586166861678616886169861708617186172861738617486175861768617786178861798618086181861828618386184861858618686187861888618986190861918619286193861948619586196861978619886199862008620186202862038620486205862068620786208862098621086211862128621386214862158621686217862188621986220862218622286223862248622586226862278622886229862308623186232862338623486235862368623786238862398624086241862428624386244862458624686247862488624986250862518625286253862548625586256862578625886259862608626186262862638626486265862668626786268862698627086271862728627386274862758627686277862788627986280862818628286283862848628586286862878628886289862908629186292862938629486295862968629786298862998630086301863028630386304863058630686307863088630986310863118631286313863148631586316863178631886319863208632186322863238632486325863268632786328863298633086331863328633386334863358633686337863388633986340863418634286343863448634586346863478634886349863508635186352863538635486355863568635786358863598636086361863628636386364863658636686367863688636986370863718637286373863748637586376863778637886379863808638186382863838638486385863868638786388863898639086391863928639386394863958639686397863988639986400864018640286403864048640586406864078640886409864108641186412864138641486415864168641786418864198642086421864228642386424864258642686427864288642986430864318643286433864348643586436864378643886439864408644186442864438644486445864468644786448864498645086451864528645386454864558645686457864588645986460864618646286463864648646586466864678646886469864708647186472864738647486475864768647786478864798648086481864828648386484864858648686487864888648986490864918649286493864948649586496864978649886499865008650186502865038650486505865068650786508865098651086511865128651386514865158651686517865188651986520865218652286523865248652586526865278652886529865308653186532865338653486535865368653786538865398654086541865428654386544865458654686547865488654986550865518655286553865548655586556865578655886559865608656186562865638656486565865668656786568865698657086571865728657386574865758657686577865788657986580865818658286583865848658586586865878658886589865908659186592865938659486595865968659786598865998660086601866028660386604866058660686607866088660986610866118661286613866148661586616866178661886619866208662186622866238662486625866268662786628866298663086631866328663386634866358663686637866388663986640866418664286643866448664586646866478664886649866508665186652866538665486655866568665786658866598666086661866628666386664866658666686667866688666986670866718667286673866748667586676866778667886679866808668186682866838668486685866868668786688866898669086691866928669386694866958669686697866988669986700867018670286703867048670586706867078670886709867108671186712867138671486715867168671786718867198672086721867228672386724867258672686727867288672986730867318673286733867348673586736867378673886739867408674186742867438674486745867468674786748867498675086751867528675386754867558675686757867588675986760867618676286763867648676586766867678676886769867708677186772867738677486775867768677786778867798678086781867828678386784867858678686787867888678986790867918679286793867948679586796867978679886799868008680186802868038680486805868068680786808868098681086811868128681386814868158681686817868188681986820868218682286823868248682586826868278682886829868308683186832868338683486835868368683786838868398684086841868428684386844868458684686847868488684986850868518685286853868548685586856868578685886859868608686186862868638686486865868668686786868868698687086871868728687386874868758687686877868788687986880868818688286883868848688586886868878688886889868908689186892868938689486895868968689786898868998690086901869028690386904869058690686907869088690986910869118691286913869148691586916869178691886919869208692186922869238692486925869268692786928869298693086931869328693386934869358693686937869388693986940869418694286943869448694586946869478694886949869508695186952869538695486955869568695786958869598696086961869628696386964869658696686967869688696986970869718697286973869748697586976869778697886979869808698186982869838698486985869868698786988869898699086991869928699386994869958699686997869988699987000870018700287003870048700587006870078700887009870108701187012870138701487015870168701787018870198702087021870228702387024870258702687027870288702987030870318703287033870348703587036870378703887039870408704187042870438704487045870468704787048870498705087051870528705387054870558705687057870588705987060870618706287063870648706587066870678706887069870708707187072870738707487075870768707787078870798708087081870828708387084870858708687087870888708987090870918709287093870948709587096870978709887099871008710187102871038710487105871068710787108871098711087111871128711387114871158711687117871188711987120871218712287123871248712587126871278712887129871308713187132871338713487135871368713787138871398714087141871428714387144871458714687147871488714987150871518715287153871548715587156871578715887159871608716187162871638716487165871668716787168871698717087171871728717387174871758717687177871788717987180871818718287183871848718587186871878718887189871908719187192871938719487195871968719787198871998720087201872028720387204872058720687207872088720987210872118721287213872148721587216872178721887219872208722187222872238722487225872268722787228872298723087231872328723387234872358723687237872388723987240872418724287243872448724587246872478724887249872508725187252872538725487255872568725787258872598726087261872628726387264872658726687267872688726987270872718727287273872748727587276872778727887279872808728187282872838728487285872868728787288872898729087291872928729387294872958729687297872988729987300873018730287303873048730587306873078730887309873108731187312873138731487315873168731787318873198732087321873228732387324873258732687327873288732987330873318733287333873348733587336873378733887339873408734187342873438734487345873468734787348873498735087351873528735387354873558735687357873588735987360873618736287363873648736587366873678736887369873708737187372873738737487375873768737787378873798738087381873828738387384873858738687387873888738987390873918739287393873948739587396873978739887399874008740187402874038740487405874068740787408874098741087411874128741387414874158741687417874188741987420874218742287423874248742587426874278742887429874308743187432874338743487435874368743787438874398744087441874428744387444874458744687447874488744987450874518745287453874548745587456874578745887459874608746187462874638746487465874668746787468874698747087471874728747387474874758747687477874788747987480874818748287483874848748587486874878748887489874908749187492874938749487495874968749787498874998750087501875028750387504875058750687507875088750987510875118751287513875148751587516875178751887519875208752187522875238752487525875268752787528875298753087531875328753387534875358753687537875388753987540875418754287543875448754587546875478754887549875508755187552875538755487555875568755787558875598756087561875628756387564875658756687567875688756987570875718757287573875748757587576875778757887579875808758187582875838758487585875868758787588875898759087591875928759387594875958759687597875988759987600876018760287603876048760587606876078760887609876108761187612876138761487615876168761787618876198762087621876228762387624876258762687627876288762987630876318763287633876348763587636876378763887639876408764187642876438764487645876468764787648876498765087651876528765387654876558765687657876588765987660876618766287663876648766587666876678766887669876708767187672876738767487675876768767787678876798768087681876828768387684876858768687687876888768987690876918769287693876948769587696876978769887699877008770187702877038770487705877068770787708877098771087711877128771387714877158771687717877188771987720877218772287723877248772587726877278772887729877308773187732877338773487735877368773787738877398774087741877428774387744877458774687747877488774987750877518775287753877548775587756877578775887759877608776187762877638776487765877668776787768877698777087771877728777387774877758777687777877788777987780877818778287783877848778587786877878778887789877908779187792877938779487795877968779787798877998780087801878028780387804878058780687807878088780987810878118781287813878148781587816878178781887819878208782187822878238782487825878268782787828878298783087831878328783387834878358783687837878388783987840878418784287843878448784587846878478784887849878508785187852878538785487855878568785787858878598786087861878628786387864878658786687867878688786987870878718787287873878748787587876878778787887879878808788187882878838788487885878868788787888878898789087891878928789387894878958789687897878988789987900879018790287903879048790587906879078790887909879108791187912879138791487915879168791787918879198792087921879228792387924879258792687927879288792987930879318793287933879348793587936879378793887939879408794187942879438794487945879468794787948879498795087951879528795387954879558795687957879588795987960879618796287963879648796587966879678796887969879708797187972879738797487975879768797787978879798798087981879828798387984879858798687987879888798987990879918799287993879948799587996879978799887999880008800188002880038800488005880068800788008880098801088011880128801388014880158801688017880188801988020880218802288023880248802588026880278802888029880308803188032880338803488035880368803788038880398804088041880428804388044880458804688047880488804988050880518805288053880548805588056880578805888059880608806188062880638806488065880668806788068880698807088071880728807388074880758807688077880788807988080880818808288083880848808588086880878808888089880908809188092880938809488095880968809788098880998810088101881028810388104881058810688107881088810988110881118811288113881148811588116881178811888119881208812188122881238812488125881268812788128881298813088131881328813388134881358813688137881388813988140881418814288143881448814588146881478814888149881508815188152881538815488155881568815788158881598816088161881628816388164881658816688167881688816988170881718817288173881748817588176881778817888179881808818188182881838818488185881868818788188881898819088191881928819388194881958819688197881988819988200882018820288203882048820588206882078820888209882108821188212882138821488215882168821788218882198822088221882228822388224882258822688227882288822988230882318823288233882348823588236882378823888239882408824188242882438824488245882468824788248882498825088251882528825388254882558825688257882588825988260882618826288263882648826588266882678826888269882708827188272882738827488275882768827788278882798828088281882828828388284882858828688287882888828988290882918829288293882948829588296882978829888299883008830188302883038830488305883068830788308883098831088311883128831388314883158831688317883188831988320883218832288323883248832588326883278832888329883308833188332883338833488335883368833788338883398834088341883428834388344883458834688347883488834988350883518835288353883548835588356883578835888359883608836188362883638836488365883668836788368883698837088371883728837388374883758837688377883788837988380883818838288383883848838588386883878838888389883908839188392883938839488395883968839788398883998840088401884028840388404884058840688407884088840988410884118841288413884148841588416884178841888419884208842188422884238842488425884268842788428884298843088431884328843388434884358843688437884388843988440884418844288443884448844588446884478844888449884508845188452884538845488455884568845788458884598846088461884628846388464884658846688467884688846988470884718847288473884748847588476884778847888479884808848188482884838848488485884868848788488884898849088491884928849388494884958849688497884988849988500885018850288503885048850588506885078850888509885108851188512885138851488515885168851788518885198852088521885228852388524885258852688527885288852988530885318853288533885348853588536885378853888539885408854188542885438854488545885468854788548885498855088551885528855388554885558855688557885588855988560885618856288563885648856588566885678856888569885708857188572885738857488575885768857788578885798858088581885828858388584885858858688587885888858988590885918859288593885948859588596885978859888599886008860188602886038860488605886068860788608886098861088611886128861388614886158861688617886188861988620886218862288623886248862588626886278862888629886308863188632886338863488635886368863788638886398864088641886428864388644886458864688647886488864988650886518865288653886548865588656886578865888659886608866188662886638866488665886668866788668886698867088671886728867388674886758867688677886788867988680886818868288683886848868588686886878868888689886908869188692886938869488695886968869788698886998870088701887028870388704887058870688707887088870988710887118871288713887148871588716887178871888719887208872188722887238872488725887268872788728887298873088731887328873388734887358873688737887388873988740887418874288743887448874588746887478874888749887508875188752887538875488755887568875788758887598876088761887628876388764887658876688767887688876988770887718877288773887748877588776887778877888779887808878188782887838878488785887868878788788887898879088791887928879388794887958879688797887988879988800888018880288803888048880588806888078880888809888108881188812888138881488815888168881788818888198882088821888228882388824888258882688827888288882988830888318883288833888348883588836888378883888839888408884188842888438884488845888468884788848888498885088851888528885388854888558885688857888588885988860888618886288863888648886588866888678886888869888708887188872888738887488875888768887788878888798888088881888828888388884888858888688887888888888988890888918889288893888948889588896888978889888899889008890188902889038890488905889068890788908889098891088911889128891388914889158891688917889188891988920889218892288923889248892588926889278892888929889308893188932889338893488935889368893788938889398894088941889428894388944889458894688947889488894988950889518895288953889548895588956889578895888959889608896188962889638896488965889668896788968889698897088971889728897388974889758897688977889788897988980889818898288983889848898588986889878898888989889908899188992889938899488995889968899788998889998900089001890028900389004890058900689007890088900989010890118901289013890148901589016890178901889019890208902189022890238902489025890268902789028890298903089031890328903389034890358903689037890388903989040890418904289043890448904589046890478904889049890508905189052890538905489055890568905789058890598906089061890628906389064890658906689067890688906989070890718907289073890748907589076890778907889079890808908189082890838908489085890868908789088890898909089091890928909389094890958909689097890988909989100891018910289103891048910589106891078910889109891108911189112891138911489115891168911789118891198912089121891228912389124891258912689127891288912989130891318913289133891348913589136891378913889139891408914189142891438914489145891468914789148891498915089151891528915389154891558915689157891588915989160891618916289163891648916589166891678916889169891708917189172891738917489175891768917789178891798918089181891828918389184891858918689187891888918989190891918919289193891948919589196891978919889199892008920189202892038920489205892068920789208892098921089211892128921389214892158921689217892188921989220892218922289223892248922589226892278922889229892308923189232892338923489235892368923789238892398924089241892428924389244892458924689247892488924989250892518925289253892548925589256892578925889259892608926189262892638926489265892668926789268892698927089271892728927389274892758927689277892788927989280892818928289283892848928589286892878928889289892908929189292892938929489295892968929789298892998930089301893028930389304893058930689307893088930989310893118931289313893148931589316893178931889319893208932189322893238932489325893268932789328893298933089331893328933389334893358933689337893388933989340893418934289343893448934589346893478934889349893508935189352893538935489355893568935789358893598936089361893628936389364893658936689367893688936989370893718937289373893748937589376893778937889379893808938189382893838938489385893868938789388893898939089391893928939389394893958939689397893988939989400894018940289403894048940589406894078940889409894108941189412894138941489415894168941789418894198942089421894228942389424894258942689427894288942989430894318943289433894348943589436894378943889439894408944189442894438944489445894468944789448894498945089451894528945389454894558945689457894588945989460894618946289463894648946589466894678946889469894708947189472894738947489475894768947789478894798948089481894828948389484894858948689487894888948989490894918949289493894948949589496894978949889499895008950189502895038950489505895068950789508895098951089511895128951389514895158951689517895188951989520895218952289523895248952589526895278952889529895308953189532895338953489535895368953789538895398954089541895428954389544895458954689547895488954989550895518955289553895548955589556895578955889559895608956189562895638956489565895668956789568895698957089571895728957389574895758957689577895788957989580895818958289583895848958589586895878958889589895908959189592895938959489595895968959789598895998960089601896028960389604896058960689607896088960989610896118961289613896148961589616896178961889619896208962189622896238962489625896268962789628896298963089631896328963389634896358963689637896388963989640896418964289643896448964589646896478964889649896508965189652896538965489655896568965789658896598966089661896628966389664896658966689667896688966989670896718967289673896748967589676896778967889679896808968189682896838968489685896868968789688896898969089691896928969389694896958969689697896988969989700897018970289703897048970589706897078970889709897108971189712897138971489715897168971789718897198972089721897228972389724897258972689727897288972989730897318973289733897348973589736897378973889739897408974189742897438974489745897468974789748897498975089751897528975389754897558975689757897588975989760897618976289763897648976589766897678976889769897708977189772897738977489775897768977789778897798978089781897828978389784897858978689787897888978989790897918979289793897948979589796897978979889799898008980189802898038980489805898068980789808898098981089811898128981389814898158981689817898188981989820898218982289823898248982589826898278982889829898308983189832898338983489835898368983789838898398984089841898428984389844898458984689847898488984989850898518985289853898548985589856898578985889859898608986189862898638986489865898668986789868898698987089871898728987389874898758987689877898788987989880898818988289883898848988589886898878988889889898908989189892898938989489895898968989789898898998990089901899028990389904899058990689907899088990989910899118991289913899148991589916899178991889919899208992189922899238992489925899268992789928899298993089931899328993389934899358993689937899388993989940899418994289943899448994589946899478994889949899508995189952899538995489955899568995789958899598996089961899628996389964899658996689967899688996989970899718997289973899748997589976899778997889979899808998189982899838998489985899868998789988899898999089991899928999389994899958999689997899988999990000900019000290003900049000590006900079000890009900109001190012900139001490015900169001790018900199002090021900229002390024900259002690027900289002990030900319003290033900349003590036900379003890039900409004190042900439004490045900469004790048900499005090051900529005390054900559005690057900589005990060900619006290063900649006590066900679006890069900709007190072900739007490075900769007790078900799008090081900829008390084900859008690087900889008990090900919009290093900949009590096900979009890099901009010190102901039010490105901069010790108901099011090111901129011390114901159011690117901189011990120901219012290123901249012590126901279012890129901309013190132901339013490135901369013790138901399014090141901429014390144901459014690147901489014990150901519015290153901549015590156901579015890159901609016190162901639016490165901669016790168901699017090171901729017390174901759017690177901789017990180901819018290183901849018590186901879018890189901909019190192901939019490195901969019790198901999020090201902029020390204902059020690207902089020990210902119021290213902149021590216902179021890219902209022190222902239022490225902269022790228902299023090231902329023390234902359023690237902389023990240902419024290243902449024590246902479024890249902509025190252902539025490255902569025790258902599026090261902629026390264902659026690267902689026990270902719027290273902749027590276902779027890279902809028190282902839028490285902869028790288902899029090291902929029390294902959029690297902989029990300903019030290303903049030590306903079030890309903109031190312903139031490315903169031790318903199032090321903229032390324903259032690327903289032990330903319033290333903349033590336903379033890339903409034190342903439034490345903469034790348903499035090351903529035390354903559035690357903589035990360903619036290363903649036590366903679036890369903709037190372903739037490375903769037790378903799038090381903829038390384903859038690387903889038990390903919039290393903949039590396903979039890399904009040190402904039040490405904069040790408904099041090411904129041390414904159041690417904189041990420904219042290423904249042590426904279042890429904309043190432904339043490435904369043790438904399044090441904429044390444904459044690447904489044990450904519045290453904549045590456904579045890459904609046190462904639046490465904669046790468904699047090471904729047390474904759047690477904789047990480904819048290483904849048590486904879048890489904909049190492904939049490495904969049790498904999050090501905029050390504905059050690507905089050990510905119051290513905149051590516905179051890519905209052190522905239052490525905269052790528905299053090531905329053390534905359053690537905389053990540905419054290543905449054590546905479054890549905509055190552905539055490555905569055790558905599056090561905629056390564905659056690567905689056990570905719057290573905749057590576905779057890579905809058190582905839058490585905869058790588905899059090591905929059390594905959059690597905989059990600906019060290603906049060590606906079060890609906109061190612906139061490615906169061790618906199062090621906229062390624906259062690627906289062990630906319063290633906349063590636906379063890639906409064190642906439064490645906469064790648906499065090651906529065390654906559065690657906589065990660906619066290663906649066590666906679066890669906709067190672906739067490675906769067790678906799068090681906829068390684906859068690687906889068990690906919069290693906949069590696906979069890699907009070190702907039070490705907069070790708907099071090711907129071390714907159071690717907189071990720907219072290723907249072590726907279072890729907309073190732907339073490735907369073790738907399074090741907429074390744907459074690747907489074990750907519075290753907549075590756907579075890759907609076190762907639076490765907669076790768907699077090771907729077390774907759077690777907789077990780907819078290783907849078590786907879078890789907909079190792907939079490795907969079790798907999080090801908029080390804908059080690807908089080990810908119081290813908149081590816908179081890819908209082190822908239082490825908269082790828908299083090831908329083390834908359083690837908389083990840908419084290843908449084590846908479084890849908509085190852908539085490855908569085790858908599086090861908629086390864908659086690867908689086990870908719087290873908749087590876908779087890879908809088190882908839088490885908869088790888908899089090891908929089390894908959089690897908989089990900909019090290903909049090590906909079090890909909109091190912909139091490915909169091790918909199092090921909229092390924909259092690927909289092990930909319093290933909349093590936909379093890939909409094190942909439094490945909469094790948909499095090951909529095390954909559095690957909589095990960909619096290963909649096590966909679096890969909709097190972909739097490975909769097790978909799098090981909829098390984909859098690987909889098990990909919099290993909949099590996909979099890999910009100191002910039100491005910069100791008910099101091011910129101391014910159101691017910189101991020910219102291023910249102591026910279102891029910309103191032910339103491035910369103791038910399104091041910429104391044910459104691047910489104991050910519105291053910549105591056910579105891059910609106191062910639106491065910669106791068910699107091071910729107391074910759107691077910789107991080910819108291083910849108591086910879108891089910909109191092910939109491095910969109791098910999110091101911029110391104911059110691107911089110991110911119111291113911149111591116911179111891119911209112191122911239112491125911269112791128911299113091131911329113391134911359113691137911389113991140911419114291143911449114591146911479114891149911509115191152911539115491155911569115791158911599116091161911629116391164911659116691167911689116991170911719117291173911749117591176911779117891179911809118191182911839118491185911869118791188911899119091191911929119391194911959119691197911989119991200912019120291203912049120591206912079120891209912109121191212912139121491215912169121791218912199122091221912229122391224912259122691227912289122991230912319123291233912349123591236912379123891239912409124191242912439124491245912469124791248912499125091251912529125391254912559125691257912589125991260912619126291263912649126591266912679126891269912709127191272912739127491275912769127791278912799128091281912829128391284912859128691287912889128991290912919129291293912949129591296912979129891299913009130191302913039130491305913069130791308913099131091311913129131391314913159131691317913189131991320913219132291323913249132591326913279132891329913309133191332913339133491335913369133791338913399134091341913429134391344913459134691347913489134991350913519135291353913549135591356913579135891359913609136191362913639136491365913669136791368913699137091371913729137391374913759137691377913789137991380913819138291383913849138591386913879138891389913909139191392913939139491395913969139791398913999140091401914029140391404914059140691407914089140991410914119141291413914149141591416914179141891419914209142191422914239142491425914269142791428914299143091431914329143391434914359143691437914389143991440914419144291443914449144591446914479144891449914509145191452914539145491455914569145791458914599146091461914629146391464914659146691467914689146991470914719147291473914749147591476914779147891479914809148191482914839148491485914869148791488914899149091491914929149391494914959149691497914989149991500915019150291503915049150591506915079150891509915109151191512915139151491515915169151791518915199152091521915229152391524915259152691527915289152991530915319153291533915349153591536915379153891539915409154191542915439154491545915469154791548915499155091551915529155391554915559155691557915589155991560915619156291563915649156591566915679156891569915709157191572915739157491575915769157791578915799158091581915829158391584915859158691587915889158991590915919159291593915949159591596915979159891599916009160191602916039160491605916069160791608916099161091611916129161391614916159161691617916189161991620916219162291623916249162591626916279162891629916309163191632916339163491635916369163791638916399164091641916429164391644916459164691647916489164991650916519165291653916549165591656916579165891659916609166191662916639166491665916669166791668916699167091671916729167391674916759167691677916789167991680916819168291683916849168591686916879168891689916909169191692916939169491695916969169791698916999170091701917029170391704917059170691707917089170991710917119171291713917149171591716917179171891719917209172191722917239172491725917269172791728917299173091731917329173391734917359173691737917389173991740917419174291743917449174591746917479174891749917509175191752917539175491755917569175791758917599176091761917629176391764917659176691767917689176991770917719177291773917749177591776917779177891779917809178191782917839178491785917869178791788917899179091791917929179391794917959179691797917989179991800918019180291803918049180591806918079180891809918109181191812918139181491815918169181791818918199182091821918229182391824918259182691827918289182991830918319183291833918349183591836918379183891839918409184191842918439184491845918469184791848918499185091851918529185391854918559185691857918589185991860918619186291863918649186591866918679186891869918709187191872918739187491875918769187791878918799188091881918829188391884918859188691887918889188991890918919189291893918949189591896918979189891899919009190191902919039190491905919069190791908919099191091911919129191391914919159191691917919189191991920919219192291923919249192591926919279192891929919309193191932919339193491935919369193791938919399194091941919429194391944919459194691947919489194991950919519195291953919549195591956919579195891959919609196191962919639196491965919669196791968919699197091971919729197391974919759197691977919789197991980919819198291983919849198591986919879198891989919909199191992919939199491995919969199791998919999200092001920029200392004920059200692007920089200992010920119201292013920149201592016920179201892019920209202192022920239202492025920269202792028920299203092031920329203392034920359203692037920389203992040920419204292043920449204592046920479204892049920509205192052920539205492055920569205792058920599206092061920629206392064920659206692067920689206992070920719207292073920749207592076920779207892079920809208192082920839208492085920869208792088920899209092091920929209392094920959209692097920989209992100921019210292103921049210592106921079210892109921109211192112921139211492115921169211792118921199212092121921229212392124921259212692127921289212992130921319213292133921349213592136921379213892139921409214192142921439214492145921469214792148921499215092151921529215392154921559215692157921589215992160921619216292163921649216592166921679216892169921709217192172921739217492175921769217792178921799218092181921829218392184921859218692187921889218992190921919219292193921949219592196921979219892199922009220192202922039220492205922069220792208922099221092211922129221392214922159221692217922189221992220922219222292223922249222592226922279222892229922309223192232922339223492235922369223792238922399224092241922429224392244922459224692247922489224992250922519225292253922549225592256922579225892259922609226192262922639226492265922669226792268922699227092271922729227392274922759227692277922789227992280922819228292283922849228592286922879228892289922909229192292922939229492295922969229792298922999230092301923029230392304923059230692307923089230992310923119231292313923149231592316923179231892319923209232192322923239232492325923269232792328923299233092331923329233392334923359233692337923389233992340923419234292343923449234592346923479234892349923509235192352923539235492355923569235792358923599236092361923629236392364923659236692367923689236992370923719237292373923749237592376923779237892379923809238192382923839238492385923869238792388923899239092391923929239392394923959239692397923989239992400924019240292403924049240592406924079240892409924109241192412924139241492415924169241792418924199242092421924229242392424924259242692427924289242992430924319243292433924349243592436924379243892439924409244192442924439244492445924469244792448924499245092451924529245392454924559245692457924589245992460924619246292463924649246592466924679246892469924709247192472924739247492475924769247792478924799248092481924829248392484924859248692487924889248992490924919249292493924949249592496924979249892499925009250192502925039250492505925069250792508925099251092511925129251392514925159251692517925189251992520925219252292523925249252592526925279252892529925309253192532925339253492535925369253792538925399254092541925429254392544925459254692547925489254992550925519255292553925549255592556925579255892559925609256192562925639256492565925669256792568925699257092571925729257392574925759257692577925789257992580925819258292583925849258592586925879258892589925909259192592925939259492595925969259792598925999260092601926029260392604926059260692607926089260992610926119261292613926149261592616926179261892619926209262192622926239262492625926269262792628926299263092631926329263392634926359263692637926389263992640926419264292643926449264592646926479264892649926509265192652926539265492655926569265792658926599266092661926629266392664926659266692667926689266992670926719267292673926749267592676926779267892679926809268192682926839268492685926869268792688926899269092691926929269392694926959269692697926989269992700927019270292703927049270592706927079270892709927109271192712927139271492715927169271792718927199272092721927229272392724927259272692727927289272992730927319273292733927349273592736927379273892739927409274192742927439274492745927469274792748927499275092751927529275392754927559275692757927589275992760927619276292763927649276592766927679276892769927709277192772927739277492775927769277792778927799278092781927829278392784927859278692787927889278992790927919279292793927949279592796927979279892799928009280192802928039280492805928069280792808928099281092811928129281392814928159281692817928189281992820928219282292823928249282592826928279282892829928309283192832928339283492835928369283792838928399284092841928429284392844928459284692847928489284992850928519285292853928549285592856928579285892859928609286192862928639286492865928669286792868928699287092871928729287392874928759287692877928789287992880928819288292883928849288592886928879288892889928909289192892928939289492895928969289792898928999290092901929029290392904929059290692907929089290992910929119291292913929149291592916929179291892919929209292192922929239292492925929269292792928929299293092931929329293392934929359293692937929389293992940929419294292943929449294592946929479294892949929509295192952929539295492955929569295792958929599296092961929629296392964929659296692967929689296992970929719297292973929749297592976929779297892979929809298192982929839298492985929869298792988929899299092991929929299392994929959299692997929989299993000930019300293003930049300593006930079300893009930109301193012930139301493015930169301793018930199302093021930229302393024930259302693027930289302993030930319303293033930349303593036930379303893039930409304193042930439304493045930469304793048930499305093051930529305393054930559305693057930589305993060930619306293063930649306593066930679306893069930709307193072930739307493075930769307793078930799308093081930829308393084930859308693087930889308993090930919309293093930949309593096930979309893099931009310193102931039310493105931069310793108931099311093111931129311393114931159311693117931189311993120931219312293123931249312593126931279312893129931309313193132931339313493135931369313793138931399314093141931429314393144931459314693147931489314993150931519315293153931549315593156931579315893159931609316193162931639316493165931669316793168931699317093171931729317393174931759317693177931789317993180931819318293183931849318593186931879318893189931909319193192931939319493195931969319793198931999320093201932029320393204932059320693207932089320993210932119321293213932149321593216932179321893219932209322193222932239322493225932269322793228932299323093231932329323393234932359323693237932389323993240932419324293243932449324593246932479324893249932509325193252932539325493255932569325793258932599326093261932629326393264932659326693267932689326993270932719327293273932749327593276932779327893279932809328193282932839328493285932869328793288932899329093291932929329393294932959329693297932989329993300933019330293303933049330593306933079330893309933109331193312933139331493315933169331793318933199332093321933229332393324933259332693327933289332993330933319333293333933349333593336933379333893339933409334193342933439334493345933469334793348933499335093351933529335393354933559335693357933589335993360933619336293363933649336593366933679336893369933709337193372933739337493375933769337793378933799338093381933829338393384933859338693387933889338993390933919339293393933949339593396933979339893399934009340193402934039340493405934069340793408934099341093411934129341393414934159341693417934189341993420934219342293423934249342593426934279342893429934309343193432934339343493435934369343793438934399344093441934429344393444934459344693447934489344993450934519345293453934549345593456934579345893459934609346193462934639346493465934669346793468934699347093471934729347393474934759347693477934789347993480934819348293483934849348593486934879348893489934909349193492934939349493495934969349793498934999350093501935029350393504935059350693507935089350993510935119351293513935149351593516935179351893519935209352193522935239352493525935269352793528935299353093531935329353393534935359353693537935389353993540935419354293543935449354593546935479354893549935509355193552935539355493555935569355793558935599356093561935629356393564935659356693567935689356993570935719357293573935749357593576935779357893579935809358193582935839358493585935869358793588935899359093591935929359393594935959359693597935989359993600936019360293603936049360593606936079360893609936109361193612936139361493615936169361793618936199362093621936229362393624936259362693627936289362993630936319363293633936349363593636936379363893639936409364193642936439364493645936469364793648936499365093651936529365393654936559365693657936589365993660936619366293663936649366593666936679366893669936709367193672936739367493675936769367793678936799368093681936829368393684936859368693687936889368993690936919369293693936949369593696936979369893699937009370193702937039370493705937069370793708937099371093711937129371393714937159371693717937189371993720937219372293723937249372593726937279372893729937309373193732937339373493735937369373793738937399374093741937429374393744937459374693747937489374993750937519375293753937549375593756937579375893759937609376193762937639376493765937669376793768937699377093771937729377393774937759377693777937789377993780937819378293783937849378593786937879378893789937909379193792937939379493795937969379793798937999380093801938029380393804938059380693807938089380993810938119381293813938149381593816938179381893819938209382193822938239382493825938269382793828938299383093831938329383393834938359383693837938389383993840938419384293843938449384593846938479384893849938509385193852938539385493855938569385793858938599386093861938629386393864938659386693867938689386993870938719387293873938749387593876938779387893879938809388193882938839388493885938869388793888938899389093891938929389393894938959389693897938989389993900939019390293903939049390593906939079390893909939109391193912939139391493915939169391793918939199392093921939229392393924939259392693927939289392993930939319393293933939349393593936939379393893939939409394193942939439394493945939469394793948939499395093951939529395393954939559395693957939589395993960939619396293963939649396593966939679396893969939709397193972939739397493975939769397793978939799398093981939829398393984939859398693987939889398993990939919399293993939949399593996939979399893999940009400194002940039400494005940069400794008940099401094011940129401394014940159401694017940189401994020940219402294023940249402594026940279402894029940309403194032940339403494035940369403794038940399404094041940429404394044940459404694047940489404994050940519405294053940549405594056940579405894059940609406194062940639406494065940669406794068940699407094071940729407394074940759407694077940789407994080940819408294083940849408594086940879408894089940909409194092940939409494095940969409794098940999410094101941029410394104941059410694107941089410994110941119411294113941149411594116941179411894119941209412194122941239412494125941269412794128941299413094131941329413394134941359413694137941389413994140941419414294143941449414594146941479414894149941509415194152941539415494155941569415794158941599416094161941629416394164941659416694167941689416994170941719417294173941749417594176941779417894179941809418194182941839418494185941869418794188941899419094191941929419394194941959419694197941989419994200942019420294203942049420594206942079420894209942109421194212942139421494215942169421794218942199422094221942229422394224942259422694227942289422994230942319423294233942349423594236942379423894239942409424194242942439424494245942469424794248942499425094251942529425394254942559425694257942589425994260942619426294263942649426594266942679426894269942709427194272942739427494275942769427794278942799428094281942829428394284942859428694287942889428994290942919429294293942949429594296942979429894299943009430194302943039430494305943069430794308943099431094311943129431394314943159431694317943189431994320943219432294323943249432594326943279432894329943309433194332943339433494335943369433794338943399434094341943429434394344943459434694347943489434994350943519435294353943549435594356943579435894359943609436194362943639436494365943669436794368943699437094371943729437394374943759437694377943789437994380943819438294383943849438594386943879438894389943909439194392943939439494395943969439794398943999440094401944029440394404944059440694407944089440994410944119441294413944149441594416944179441894419944209442194422944239442494425944269442794428944299443094431944329443394434944359443694437944389443994440944419444294443944449444594446944479444894449944509445194452944539445494455944569445794458944599446094461944629446394464944659446694467944689446994470944719447294473944749447594476944779447894479944809448194482944839448494485944869448794488944899449094491944929449394494944959449694497944989449994500945019450294503945049450594506945079450894509945109451194512945139451494515945169451794518945199452094521945229452394524945259452694527945289452994530945319453294533945349453594536945379453894539945409454194542945439454494545945469454794548945499455094551945529455394554945559455694557945589455994560945619456294563945649456594566945679456894569945709457194572945739457494575945769457794578945799458094581945829458394584945859458694587945889458994590945919459294593945949459594596945979459894599946009460194602946039460494605946069460794608946099461094611946129461394614946159461694617946189461994620946219462294623946249462594626946279462894629946309463194632946339463494635946369463794638946399464094641946429464394644946459464694647946489464994650946519465294653946549465594656946579465894659946609466194662946639466494665946669466794668946699467094671946729467394674946759467694677946789467994680946819468294683946849468594686946879468894689946909469194692946939469494695946969469794698946999470094701947029470394704947059470694707947089470994710947119471294713947149471594716947179471894719947209472194722947239472494725947269472794728947299473094731947329473394734947359473694737947389473994740947419474294743947449474594746947479474894749947509475194752947539475494755947569475794758947599476094761947629476394764947659476694767947689476994770947719477294773947749477594776947779477894779947809478194782947839478494785947869478794788947899479094791947929479394794947959479694797947989479994800948019480294803948049480594806948079480894809948109481194812948139481494815948169481794818948199482094821948229482394824948259482694827948289482994830948319483294833948349483594836948379483894839948409484194842948439484494845948469484794848948499485094851948529485394854948559485694857948589485994860948619486294863948649486594866948679486894869948709487194872948739487494875948769487794878948799488094881948829488394884948859488694887948889488994890948919489294893948949489594896948979489894899949009490194902949039490494905949069490794908949099491094911949129491394914949159491694917949189491994920949219492294923949249492594926949279492894929949309493194932949339493494935949369493794938949399494094941949429494394944949459494694947949489494994950949519495294953949549495594956949579495894959949609496194962949639496494965949669496794968949699497094971949729497394974949759497694977949789497994980949819498294983949849498594986949879498894989949909499194992949939499494995949969499794998949999500095001950029500395004950059500695007950089500995010950119501295013950149501595016950179501895019950209502195022950239502495025950269502795028950299503095031950329503395034950359503695037950389503995040950419504295043950449504595046950479504895049950509505195052950539505495055950569505795058950599506095061950629506395064950659506695067950689506995070950719507295073950749507595076950779507895079950809508195082950839508495085950869508795088950899509095091950929509395094950959509695097950989509995100951019510295103951049510595106951079510895109951109511195112951139511495115951169511795118951199512095121951229512395124951259512695127951289512995130951319513295133951349513595136951379513895139951409514195142951439514495145951469514795148951499515095151951529515395154951559515695157951589515995160951619516295163951649516595166951679516895169951709517195172951739517495175951769517795178951799518095181951829518395184951859518695187951889518995190951919519295193951949519595196951979519895199952009520195202952039520495205952069520795208952099521095211952129521395214952159521695217952189521995220952219522295223952249522595226952279522895229952309523195232952339523495235952369523795238952399524095241952429524395244952459524695247952489524995250952519525295253952549525595256952579525895259952609526195262952639526495265952669526795268952699527095271952729527395274952759527695277952789527995280952819528295283952849528595286952879528895289952909529195292952939529495295952969529795298952999530095301953029530395304953059530695307953089530995310953119531295313953149531595316953179531895319953209532195322953239532495325953269532795328953299533095331953329533395334953359533695337953389533995340953419534295343953449534595346953479534895349953509535195352953539535495355953569535795358953599536095361953629536395364953659536695367953689536995370953719537295373953749537595376953779537895379953809538195382953839538495385953869538795388953899539095391953929539395394953959539695397953989539995400954019540295403954049540595406954079540895409954109541195412954139541495415954169541795418954199542095421954229542395424954259542695427954289542995430954319543295433954349543595436954379543895439954409544195442954439544495445954469544795448954499545095451954529545395454954559545695457954589545995460954619546295463954649546595466954679546895469954709547195472954739547495475954769547795478954799548095481954829548395484954859548695487954889548995490954919549295493954949549595496954979549895499955009550195502955039550495505955069550795508955099551095511955129551395514955159551695517955189551995520955219552295523955249552595526955279552895529955309553195532955339553495535955369553795538955399554095541955429554395544955459554695547955489554995550955519555295553955549555595556955579555895559955609556195562955639556495565955669556795568955699557095571955729557395574955759557695577955789557995580955819558295583955849558595586955879558895589955909559195592955939559495595955969559795598955999560095601956029560395604956059560695607956089560995610956119561295613956149561595616956179561895619956209562195622956239562495625956269562795628956299563095631956329563395634956359563695637956389563995640956419564295643956449564595646956479564895649956509565195652956539565495655956569565795658956599566095661956629566395664956659566695667956689566995670956719567295673956749567595676956779567895679956809568195682956839568495685956869568795688956899569095691956929569395694956959569695697956989569995700957019570295703957049570595706957079570895709957109571195712957139571495715957169571795718957199572095721957229572395724957259572695727957289572995730957319573295733957349573595736957379573895739957409574195742957439574495745957469574795748957499575095751957529575395754957559575695757957589575995760957619576295763957649576595766957679576895769957709577195772957739577495775957769577795778957799578095781957829578395784957859578695787957889578995790957919579295793957949579595796957979579895799958009580195802958039580495805958069580795808958099581095811958129581395814958159581695817958189581995820958219582295823958249582595826958279582895829958309583195832958339583495835958369583795838958399584095841958429584395844958459584695847958489584995850958519585295853958549585595856958579585895859958609586195862958639586495865958669586795868958699587095871958729587395874958759587695877958789587995880958819588295883958849588595886958879588895889958909589195892958939589495895958969589795898958999590095901959029590395904959059590695907959089590995910959119591295913959149591595916959179591895919959209592195922959239592495925959269592795928959299593095931959329593395934959359593695937959389593995940959419594295943959449594595946959479594895949959509595195952959539595495955959569595795958959599596095961959629596395964959659596695967959689596995970959719597295973959749597595976959779597895979959809598195982959839598495985959869598795988959899599095991959929599395994959959599695997959989599996000960019600296003960049600596006960079600896009960109601196012960139601496015960169601796018960199602096021960229602396024960259602696027960289602996030960319603296033960349603596036960379603896039960409604196042960439604496045960469604796048960499605096051960529605396054960559605696057960589605996060960619606296063960649606596066960679606896069960709607196072960739607496075960769607796078960799608096081960829608396084960859608696087960889608996090960919609296093960949609596096960979609896099961009610196102961039610496105961069610796108961099611096111961129611396114961159611696117961189611996120961219612296123961249612596126961279612896129961309613196132961339613496135961369613796138961399614096141961429614396144961459614696147961489614996150961519615296153961549615596156961579615896159961609616196162961639616496165961669616796168961699617096171961729617396174961759617696177961789617996180961819618296183961849618596186961879618896189961909619196192961939619496195961969619796198961999620096201962029620396204962059620696207962089620996210962119621296213962149621596216962179621896219962209622196222962239622496225962269622796228962299623096231962329623396234962359623696237962389623996240962419624296243962449624596246962479624896249962509625196252962539625496255962569625796258962599626096261962629626396264962659626696267962689626996270962719627296273962749627596276962779627896279962809628196282962839628496285962869628796288962899629096291962929629396294962959629696297962989629996300963019630296303963049630596306963079630896309963109631196312963139631496315963169631796318963199632096321963229632396324963259632696327963289632996330963319633296333963349633596336963379633896339963409634196342963439634496345963469634796348963499635096351963529635396354963559635696357963589635996360963619636296363963649636596366963679636896369963709637196372963739637496375963769637796378963799638096381963829638396384963859638696387963889638996390963919639296393963949639596396963979639896399964009640196402964039640496405964069640796408964099641096411964129641396414964159641696417964189641996420964219642296423964249642596426964279642896429964309643196432964339643496435964369643796438964399644096441964429644396444964459644696447964489644996450964519645296453964549645596456964579645896459964609646196462964639646496465964669646796468964699647096471964729647396474964759647696477964789647996480964819648296483964849648596486964879648896489964909649196492964939649496495964969649796498964999650096501965029650396504965059650696507965089650996510965119651296513965149651596516965179651896519965209652196522965239652496525965269652796528965299653096531965329653396534965359653696537965389653996540965419654296543965449654596546965479654896549965509655196552965539655496555965569655796558965599656096561965629656396564965659656696567965689656996570965719657296573965749657596576965779657896579965809658196582965839658496585965869658796588965899659096591965929659396594965959659696597965989659996600966019660296603966049660596606966079660896609966109661196612966139661496615966169661796618966199662096621966229662396624966259662696627966289662996630966319663296633966349663596636966379663896639966409664196642966439664496645966469664796648966499665096651966529665396654966559665696657966589665996660966619666296663966649666596666966679666896669966709667196672966739667496675966769667796678966799668096681966829668396684966859668696687966889668996690966919669296693966949669596696966979669896699967009670196702967039670496705967069670796708967099671096711967129671396714967159671696717967189671996720967219672296723967249672596726967279672896729967309673196732967339673496735967369673796738967399674096741967429674396744967459674696747967489674996750967519675296753967549675596756967579675896759967609676196762967639676496765967669676796768967699677096771967729677396774967759677696777967789677996780967819678296783967849678596786967879678896789967909679196792967939679496795967969679796798967999680096801968029680396804968059680696807968089680996810968119681296813968149681596816968179681896819968209682196822968239682496825968269682796828968299683096831968329683396834968359683696837968389683996840968419684296843968449684596846968479684896849968509685196852968539685496855968569685796858968599686096861968629686396864968659686696867968689686996870968719687296873968749687596876968779687896879968809688196882968839688496885968869688796888968899689096891968929689396894968959689696897968989689996900969019690296903969049690596906969079690896909969109691196912969139691496915969169691796918969199692096921969229692396924969259692696927969289692996930969319693296933969349693596936969379693896939969409694196942969439694496945969469694796948969499695096951969529695396954969559695696957969589695996960969619696296963969649696596966969679696896969969709697196972969739697496975969769697796978969799698096981969829698396984969859698696987969889698996990969919699296993969949699596996969979699896999970009700197002970039700497005970069700797008970099701097011970129701397014970159701697017970189701997020970219702297023970249702597026970279702897029970309703197032970339703497035970369703797038970399704097041970429704397044970459704697047970489704997050970519705297053970549705597056970579705897059970609706197062970639706497065970669706797068970699707097071970729707397074970759707697077970789707997080970819708297083970849708597086970879708897089970909709197092970939709497095970969709797098970999710097101971029710397104971059710697107971089710997110971119711297113971149711597116971179711897119971209712197122971239712497125971269712797128971299713097131971329713397134971359713697137971389713997140971419714297143971449714597146971479714897149971509715197152971539715497155971569715797158971599716097161971629716397164971659716697167971689716997170971719717297173971749717597176971779717897179971809718197182971839718497185971869718797188971899719097191971929719397194971959719697197971989719997200972019720297203972049720597206972079720897209972109721197212972139721497215972169721797218972199722097221972229722397224972259722697227972289722997230972319723297233972349723597236972379723897239972409724197242972439724497245972469724797248972499725097251972529725397254972559725697257972589725997260972619726297263972649726597266972679726897269972709727197272972739727497275972769727797278972799728097281972829728397284972859728697287972889728997290972919729297293972949729597296972979729897299973009730197302973039730497305973069730797308973099731097311973129731397314973159731697317973189731997320973219732297323973249732597326973279732897329973309733197332973339733497335973369733797338973399734097341973429734397344973459734697347973489734997350973519735297353973549735597356973579735897359973609736197362973639736497365973669736797368973699737097371973729737397374973759737697377973789737997380973819738297383973849738597386973879738897389973909739197392973939739497395973969739797398973999740097401974029740397404974059740697407974089740997410974119741297413974149741597416974179741897419974209742197422974239742497425974269742797428974299743097431974329743397434974359743697437974389743997440974419744297443974449744597446974479744897449974509745197452974539745497455974569745797458974599746097461974629746397464974659746697467974689746997470974719747297473974749747597476974779747897479974809748197482974839748497485974869748797488974899749097491974929749397494974959749697497974989749997500975019750297503975049750597506975079750897509975109751197512975139751497515975169751797518975199752097521975229752397524975259752697527975289752997530975319753297533975349753597536975379753897539975409754197542975439754497545975469754797548975499755097551975529755397554975559755697557975589755997560975619756297563975649756597566975679756897569975709757197572975739757497575975769757797578975799758097581975829758397584975859758697587975889758997590975919759297593975949759597596975979759897599976009760197602976039760497605976069760797608976099761097611976129761397614976159761697617976189761997620976219762297623976249762597626976279762897629976309763197632976339763497635976369763797638976399764097641976429764397644976459764697647976489764997650976519765297653976549765597656976579765897659976609766197662976639766497665976669766797668976699767097671976729767397674976759767697677976789767997680976819768297683976849768597686976879768897689976909769197692976939769497695976969769797698976999770097701977029770397704977059770697707977089770997710977119771297713977149771597716977179771897719977209772197722977239772497725977269772797728977299773097731977329773397734977359773697737977389773997740977419774297743977449774597746977479774897749977509775197752977539775497755977569775797758977599776097761977629776397764977659776697767977689776997770977719777297773977749777597776977779777897779977809778197782977839778497785977869778797788977899779097791977929779397794977959779697797977989779997800978019780297803978049780597806978079780897809978109781197812978139781497815978169781797818978199782097821978229782397824978259782697827978289782997830978319783297833978349783597836978379783897839978409784197842978439784497845978469784797848978499785097851978529785397854978559785697857978589785997860978619786297863978649786597866978679786897869978709787197872978739787497875978769787797878978799788097881978829788397884978859788697887978889788997890978919789297893978949789597896978979789897899979009790197902979039790497905979069790797908979099791097911979129791397914979159791697917979189791997920979219792297923979249792597926979279792897929979309793197932979339793497935979369793797938979399794097941979429794397944979459794697947979489794997950979519795297953979549795597956979579795897959979609796197962979639796497965979669796797968979699797097971979729797397974979759797697977979789797997980979819798297983979849798597986979879798897989979909799197992979939799497995979969799797998979999800098001980029800398004980059800698007980089800998010980119801298013980149801598016980179801898019980209802198022980239802498025980269802798028980299803098031980329803398034980359803698037980389803998040980419804298043980449804598046980479804898049980509805198052980539805498055980569805798058980599806098061980629806398064980659806698067980689806998070980719807298073980749807598076980779807898079980809808198082980839808498085980869808798088980899809098091980929809398094980959809698097980989809998100981019810298103981049810598106981079810898109981109811198112981139811498115981169811798118981199812098121981229812398124981259812698127981289812998130981319813298133981349813598136981379813898139981409814198142981439814498145981469814798148981499815098151981529815398154981559815698157981589815998160981619816298163981649816598166981679816898169981709817198172981739817498175981769817798178981799818098181981829818398184981859818698187981889818998190981919819298193981949819598196981979819898199982009820198202982039820498205982069820798208982099821098211982129821398214982159821698217982189821998220982219822298223982249822598226982279822898229982309823198232982339823498235982369823798238982399824098241982429824398244982459824698247982489824998250982519825298253982549825598256982579825898259982609826198262982639826498265982669826798268982699827098271982729827398274982759827698277982789827998280982819828298283982849828598286982879828898289982909829198292982939829498295982969829798298982999830098301983029830398304983059830698307983089830998310983119831298313983149831598316983179831898319983209832198322983239832498325983269832798328983299833098331983329833398334983359833698337983389833998340983419834298343983449834598346983479834898349983509835198352983539835498355983569835798358983599836098361983629836398364983659836698367983689836998370983719837298373983749837598376983779837898379983809838198382983839838498385983869838798388983899839098391983929839398394983959839698397983989839998400984019840298403984049840598406984079840898409984109841198412984139841498415984169841798418984199842098421984229842398424984259842698427984289842998430984319843298433984349843598436984379843898439984409844198442984439844498445984469844798448984499845098451984529845398454984559845698457984589845998460984619846298463984649846598466984679846898469984709847198472984739847498475984769847798478984799848098481984829848398484984859848698487984889848998490984919849298493984949849598496984979849898499985009850198502985039850498505985069850798508985099851098511985129851398514985159851698517985189851998520985219852298523985249852598526985279852898529985309853198532985339853498535985369853798538985399854098541985429854398544985459854698547985489854998550985519855298553985549855598556985579855898559985609856198562985639856498565985669856798568985699857098571985729857398574985759857698577985789857998580985819858298583985849858598586985879858898589985909859198592985939859498595985969859798598985999860098601986029860398604986059860698607986089860998610986119861298613986149861598616986179861898619986209862198622986239862498625986269862798628986299863098631986329863398634986359863698637986389863998640986419864298643986449864598646986479864898649986509865198652986539865498655986569865798658986599866098661986629866398664986659866698667986689866998670986719867298673986749867598676986779867898679986809868198682986839868498685986869868798688986899869098691986929869398694986959869698697986989869998700987019870298703987049870598706987079870898709987109871198712987139871498715987169871798718987199872098721987229872398724987259872698727987289872998730987319873298733987349873598736987379873898739987409874198742987439874498745987469874798748987499875098751987529875398754987559875698757987589875998760987619876298763987649876598766987679876898769987709877198772987739877498775987769877798778987799878098781987829878398784987859878698787987889878998790987919879298793987949879598796987979879898799988009880198802988039880498805988069880798808988099881098811988129881398814988159881698817988189881998820988219882298823988249882598826988279882898829988309883198832988339883498835988369883798838988399884098841988429884398844988459884698847988489884998850988519885298853988549885598856988579885898859988609886198862988639886498865988669886798868988699887098871988729887398874988759887698877988789887998880988819888298883988849888598886988879888898889988909889198892988939889498895988969889798898988999890098901989029890398904989059890698907989089890998910989119891298913989149891598916989179891898919989209892198922989239892498925989269892798928989299893098931989329893398934989359893698937989389893998940989419894298943989449894598946989479894898949989509895198952989539895498955989569895798958989599896098961989629896398964989659896698967989689896998970989719897298973989749897598976989779897898979989809898198982989839898498985989869898798988989899899098991989929899398994989959899698997989989899999000990019900299003990049900599006990079900899009990109901199012990139901499015990169901799018990199902099021990229902399024990259902699027990289902999030990319903299033990349903599036990379903899039990409904199042990439904499045990469904799048990499905099051990529905399054990559905699057990589905999060990619906299063990649906599066990679906899069990709907199072990739907499075990769907799078990799908099081990829908399084990859908699087990889908999090990919909299093990949909599096990979909899099991009910199102991039910499105991069910799108991099911099111991129911399114991159911699117991189911999120991219912299123991249912599126991279912899129991309913199132991339913499135991369913799138991399914099141991429914399144991459914699147991489914999150991519915299153991549915599156991579915899159991609916199162991639916499165991669916799168991699917099171991729917399174991759917699177991789917999180991819918299183991849918599186991879918899189991909919199192991939919499195991969919799198991999920099201992029920399204992059920699207992089920999210992119921299213992149921599216992179921899219992209922199222992239922499225992269922799228992299923099231992329923399234992359923699237992389923999240992419924299243992449924599246992479924899249992509925199252992539925499255992569925799258992599926099261992629926399264992659926699267992689926999270992719927299273992749927599276992779927899279992809928199282992839928499285992869928799288992899929099291992929929399294992959929699297992989929999300993019930299303993049930599306993079930899309993109931199312993139931499315993169931799318993199932099321993229932399324993259932699327993289932999330993319933299333993349933599336993379933899339993409934199342993439934499345993469934799348993499935099351993529935399354993559935699357993589935999360993619936299363993649936599366993679936899369993709937199372993739937499375993769937799378993799938099381993829938399384993859938699387993889938999390993919939299393993949939599396993979939899399994009940199402994039940499405994069940799408994099941099411994129941399414994159941699417994189941999420994219942299423994249942599426994279942899429994309943199432994339943499435994369943799438994399944099441994429944399444994459944699447994489944999450994519945299453994549945599456994579945899459994609946199462994639946499465994669946799468994699947099471994729947399474994759947699477994789947999480994819948299483994849948599486994879948899489994909949199492994939949499495994969949799498994999950099501995029950399504995059950699507995089950999510995119951299513995149951599516995179951899519995209952199522995239952499525995269952799528995299953099531995329953399534995359953699537995389953999540995419954299543995449954599546995479954899549995509955199552995539955499555995569955799558995599956099561995629956399564995659956699567995689956999570995719957299573995749957599576995779957899579995809958199582995839958499585995869958799588995899959099591995929959399594995959959699597995989959999600996019960299603996049960599606996079960899609996109961199612996139961499615996169961799618996199962099621996229962399624996259962699627996289962999630996319963299633996349963599636996379963899639996409964199642996439964499645996469964799648996499965099651996529965399654996559965699657996589965999660996619966299663996649966599666996679966899669996709967199672996739967499675996769967799678996799968099681996829968399684996859968699687996889968999690996919969299693996949969599696996979969899699997009970199702997039970499705997069970799708997099971099711997129971399714997159971699717997189971999720997219972299723997249972599726997279972899729997309973199732997339973499735997369973799738997399974099741997429974399744997459974699747997489974999750997519975299753997549975599756997579975899759997609976199762997639976499765997669976799768997699977099771997729977399774997759977699777997789977999780997819978299783997849978599786997879978899789997909979199792997939979499795997969979799798997999980099801998029980399804998059980699807998089980999810998119981299813998149981599816998179981899819998209982199822998239982499825998269982799828998299983099831998329983399834998359983699837998389983999840998419984299843998449984599846998479984899849998509985199852998539985499855998569985799858998599986099861998629986399864998659986699867998689986999870998719987299873998749987599876998779987899879998809988199882998839988499885998869988799888998899989099891998929989399894998959989699897998989989999900999019990299903999049990599906999079990899909999109991199912999139991499915999169991799918999199992099921999229992399924999259992699927999289992999930999319993299933999349993599936999379993899939999409994199942999439994499945999469994799948999499995099951999529995399954999559995699957999589995999960999619996299963999649996599966999679996899969999709997199972999739997499975999769997799978999799998099981999829998399984999859998699987999889998999990999919999299993999949999599996999979999899999100000100001100002100003100004100005100006100007100008100009100010100011100012100013100014100015100016100017100018100019100020100021100022100023100024100025100026100027100028100029100030100031100032100033100034100035100036100037100038100039100040100041100042100043100044100045100046100047100048100049100050100051100052100053100054100055100056100057100058100059100060100061100062100063100064100065100066100067100068100069100070100071100072100073100074100075100076100077100078100079100080100081100082100083100084100085100086100087100088100089100090100091100092100093100094100095100096100097100098100099100100100101100102100103100104100105100106100107100108100109100110100111100112100113100114100115100116100117100118100119100120100121100122100123100124100125100126100127100128100129100130100131100132100133100134100135100136100137100138100139100140100141100142100143100144100145100146100147100148100149100150100151100152100153100154100155100156100157100158100159100160100161100162100163100164100165100166100167100168100169100170100171100172100173100174100175100176100177100178100179100180100181100182100183100184100185100186100187100188100189100190100191100192100193100194100195100196100197100198100199100200100201100202100203100204100205100206100207100208100209100210100211100212100213100214100215100216100217100218100219100220100221100222100223100224100225100226100227100228100229100230100231100232100233100234100235100236100237100238100239100240100241100242100243100244100245100246100247100248100249100250100251100252100253100254100255100256100257100258100259100260100261100262100263100264100265100266100267100268100269100270100271100272100273100274100275100276100277100278100279100280100281100282100283100284100285100286100287100288100289100290100291100292100293100294100295100296100297100298100299100300100301100302100303100304100305100306100307100308100309100310100311100312100313100314100315100316100317100318100319100320100321100322100323100324100325100326100327100328100329100330100331100332100333100334100335100336100337100338100339100340100341100342100343100344100345100346100347100348100349100350100351100352100353100354100355100356100357100358100359100360100361100362100363100364100365100366100367100368100369100370100371100372100373100374100375100376100377100378100379100380100381100382100383100384100385100386100387100388100389100390100391100392100393100394100395100396100397100398100399100400100401100402100403100404100405100406100407100408100409100410100411100412100413100414100415100416100417100418100419100420100421100422100423100424100425100426100427100428100429100430100431100432100433100434100435100436100437100438100439100440100441100442100443100444100445100446100447100448100449100450100451100452100453100454100455100456100457100458100459100460100461100462100463100464100465100466100467100468100469100470100471100472100473100474100475100476100477100478100479100480100481100482100483100484100485100486100487100488100489100490100491100492100493100494100495100496100497100498100499100500100501100502100503100504100505100506100507100508100509100510100511100512100513100514100515100516100517100518100519100520100521100522100523100524100525100526100527100528100529100530100531100532100533100534100535100536100537100538100539100540100541100542100543100544100545100546100547100548100549100550100551100552100553100554100555100556100557100558100559100560100561100562100563100564100565100566100567100568100569100570100571100572100573100574100575100576100577100578100579100580100581100582100583100584100585100586100587100588100589100590100591100592100593100594100595100596100597100598100599100600100601100602100603100604100605100606100607100608100609100610100611100612100613100614100615100616100617100618100619100620100621100622100623100624100625100626100627100628100629100630100631100632100633100634100635100636100637100638100639100640100641100642100643100644100645100646100647100648100649100650100651100652100653100654100655100656100657100658100659100660100661100662100663100664100665100666100667100668100669100670100671100672100673100674100675100676100677100678100679100680100681100682100683100684100685100686100687100688100689100690100691100692100693100694100695100696100697100698100699100700100701100702100703100704100705100706100707100708100709100710100711100712100713100714100715100716100717100718100719100720100721100722100723100724100725100726100727100728100729100730100731100732100733100734100735100736100737100738100739100740100741100742100743100744100745100746100747100748100749100750100751100752100753100754100755100756100757100758100759100760100761100762100763100764100765100766100767100768100769100770100771100772100773100774100775100776100777100778100779100780100781100782100783100784100785100786100787100788100789100790100791100792100793100794100795100796100797100798100799100800100801100802100803100804100805100806100807100808100809100810100811100812100813100814100815100816100817100818100819100820100821100822100823100824100825100826100827100828100829100830100831100832100833100834100835100836100837100838100839100840100841100842100843100844100845100846100847100848100849100850100851100852100853100854100855100856100857100858100859100860100861100862100863100864100865100866100867100868100869100870100871100872100873100874100875100876100877100878100879100880100881100882100883100884100885100886100887100888100889100890100891100892100893100894100895100896100897100898100899100900100901100902100903100904100905100906100907100908100909100910100911100912100913100914100915100916100917100918100919100920100921100922100923100924100925100926100927100928100929100930100931100932100933100934100935100936100937100938100939100940100941100942100943100944100945100946100947100948100949100950100951100952100953100954100955100956100957100958100959100960100961100962100963100964100965100966100967100968100969100970100971100972100973100974100975100976100977100978100979100980100981100982100983100984100985100986100987100988100989100990100991100992100993100994100995100996100997100998100999101000101001101002101003101004101005101006101007101008101009101010101011101012101013101014101015101016101017101018101019101020101021101022101023101024101025101026101027101028101029101030101031101032101033101034101035101036101037101038101039101040101041101042101043101044101045101046101047101048101049101050101051101052101053101054101055101056101057101058101059101060101061101062101063101064101065101066101067101068
  1. diff -Nur linux-3.13.6/arch/arm/configs/bcmrpi_cutdown_defconfig linux-raspberry-pi/arch/arm/configs/bcmrpi_cutdown_defconfig
  2. --- linux-3.13.6/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 16:54:55.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.13.6/arch/arm/configs/bcmrpi_defconfig linux-raspberry-pi/arch/arm/configs/bcmrpi_defconfig
  509. --- linux-3.13.6/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 16:54:55.000000000 +0100
  511. @@ -0,0 +1,1089 @@
  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_MEMCG=y
  533. +CONFIG_BLK_CGROUP=y
  534. +CONFIG_NAMESPACES=y
  535. +CONFIG_SCHED_AUTOGROUP=y
  536. +CONFIG_RELAY=y
  537. +CONFIG_BLK_DEV_INITRD=y
  538. +CONFIG_EMBEDDED=y
  539. +# CONFIG_COMPAT_BRK is not set
  540. +CONFIG_PROFILING=y
  541. +CONFIG_OPROFILE=m
  542. +CONFIG_KPROBES=y
  543. +CONFIG_JUMP_LABEL=y
  544. +CONFIG_MODULES=y
  545. +CONFIG_MODULE_UNLOAD=y
  546. +CONFIG_MODVERSIONS=y
  547. +CONFIG_MODULE_SRCVERSION_ALL=y
  548. +CONFIG_BLK_DEV_THROTTLING=y
  549. +CONFIG_PARTITION_ADVANCED=y
  550. +CONFIG_MAC_PARTITION=y
  551. +CONFIG_CFQ_GROUP_IOSCHED=y
  552. +CONFIG_ARCH_BCM2708=y
  553. +CONFIG_PREEMPT=y
  554. +CONFIG_AEABI=y
  555. +CONFIG_CLEANCACHE=y
  556. +CONFIG_FRONTSWAP=y
  557. +CONFIG_CMA=y
  558. +CONFIG_UACCESS_WITH_MEMCPY=y
  559. +CONFIG_SECCOMP=y
  560. +CONFIG_CC_STACKPROTECTOR=y
  561. +CONFIG_ZBOOT_ROM_TEXT=0x0
  562. +CONFIG_ZBOOT_ROM_BSS=0x0
  563. +CONFIG_CMDLINE="console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  564. +CONFIG_KEXEC=y
  565. +CONFIG_CPU_FREQ=y
  566. +CONFIG_CPU_FREQ_STAT=m
  567. +CONFIG_CPU_FREQ_STAT_DETAILS=y
  568. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  569. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  570. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  571. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  572. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  573. +CONFIG_CPU_IDLE=y
  574. +CONFIG_VFP=y
  575. +CONFIG_BINFMT_MISC=m
  576. +CONFIG_NET=y
  577. +CONFIG_PACKET=y
  578. +CONFIG_UNIX=y
  579. +CONFIG_XFRM_USER=y
  580. +CONFIG_NET_KEY=m
  581. +CONFIG_INET=y
  582. +CONFIG_IP_MULTICAST=y
  583. +CONFIG_IP_ADVANCED_ROUTER=y
  584. +CONFIG_IP_MULTIPLE_TABLES=y
  585. +CONFIG_IP_ROUTE_MULTIPATH=y
  586. +CONFIG_IP_ROUTE_VERBOSE=y
  587. +CONFIG_IP_PNP=y
  588. +CONFIG_IP_PNP_DHCP=y
  589. +CONFIG_IP_PNP_RARP=y
  590. +CONFIG_NET_IPIP=m
  591. +CONFIG_NET_IPGRE_DEMUX=m
  592. +CONFIG_NET_IPGRE=m
  593. +CONFIG_IP_MROUTE=y
  594. +CONFIG_IP_MROUTE_MULTIPLE_TABLES=y
  595. +CONFIG_IP_PIMSM_V1=y
  596. +CONFIG_IP_PIMSM_V2=y
  597. +CONFIG_SYN_COOKIES=y
  598. +CONFIG_INET_AH=m
  599. +CONFIG_INET_ESP=m
  600. +CONFIG_INET_IPCOMP=m
  601. +CONFIG_INET_XFRM_MODE_TRANSPORT=m
  602. +CONFIG_INET_XFRM_MODE_TUNNEL=m
  603. +CONFIG_INET_XFRM_MODE_BEET=m
  604. +CONFIG_INET_LRO=m
  605. +CONFIG_INET_DIAG=m
  606. +CONFIG_INET6_AH=m
  607. +CONFIG_INET6_ESP=m
  608. +CONFIG_INET6_IPCOMP=m
  609. +CONFIG_IPV6_TUNNEL=m
  610. +CONFIG_IPV6_MULTIPLE_TABLES=y
  611. +CONFIG_IPV6_MROUTE=y
  612. +CONFIG_IPV6_MROUTE_MULTIPLE_TABLES=y
  613. +CONFIG_IPV6_PIMSM_V2=y
  614. +CONFIG_NETFILTER=y
  615. +CONFIG_NF_CONNTRACK=m
  616. +CONFIG_NF_CONNTRACK_ZONES=y
  617. +CONFIG_NF_CONNTRACK_EVENTS=y
  618. +CONFIG_NF_CONNTRACK_TIMESTAMP=y
  619. +CONFIG_NF_CT_PROTO_DCCP=m
  620. +CONFIG_NF_CT_PROTO_UDPLITE=m
  621. +CONFIG_NF_CONNTRACK_AMANDA=m
  622. +CONFIG_NF_CONNTRACK_FTP=m
  623. +CONFIG_NF_CONNTRACK_H323=m
  624. +CONFIG_NF_CONNTRACK_IRC=m
  625. +CONFIG_NF_CONNTRACK_NETBIOS_NS=m
  626. +CONFIG_NF_CONNTRACK_SNMP=m
  627. +CONFIG_NF_CONNTRACK_PPTP=m
  628. +CONFIG_NF_CONNTRACK_SANE=m
  629. +CONFIG_NF_CONNTRACK_SIP=m
  630. +CONFIG_NF_CONNTRACK_TFTP=m
  631. +CONFIG_NF_CT_NETLINK=m
  632. +CONFIG_NETFILTER_XT_SET=m
  633. +CONFIG_NETFILTER_XT_TARGET_AUDIT=m
  634. +CONFIG_NETFILTER_XT_TARGET_CHECKSUM=m
  635. +CONFIG_NETFILTER_XT_TARGET_CLASSIFY=m
  636. +CONFIG_NETFILTER_XT_TARGET_CONNMARK=m
  637. +CONFIG_NETFILTER_XT_TARGET_DSCP=m
  638. +CONFIG_NETFILTER_XT_TARGET_HMARK=m
  639. +CONFIG_NETFILTER_XT_TARGET_IDLETIMER=m
  640. +CONFIG_NETFILTER_XT_TARGET_LED=m
  641. +CONFIG_NETFILTER_XT_TARGET_LOG=m
  642. +CONFIG_NETFILTER_XT_TARGET_MARK=m
  643. +CONFIG_NETFILTER_XT_TARGET_NFLOG=m
  644. +CONFIG_NETFILTER_XT_TARGET_NFQUEUE=m
  645. +CONFIG_NETFILTER_XT_TARGET_NOTRACK=m
  646. +CONFIG_NETFILTER_XT_TARGET_TEE=m
  647. +CONFIG_NETFILTER_XT_TARGET_TPROXY=m
  648. +CONFIG_NETFILTER_XT_TARGET_TRACE=m
  649. +CONFIG_NETFILTER_XT_TARGET_TCPMSS=m
  650. +CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP=m
  651. +CONFIG_NETFILTER_XT_MATCH_ADDRTYPE=m
  652. +CONFIG_NETFILTER_XT_MATCH_BPF=m
  653. +CONFIG_NETFILTER_XT_MATCH_CLUSTER=m
  654. +CONFIG_NETFILTER_XT_MATCH_COMMENT=m
  655. +CONFIG_NETFILTER_XT_MATCH_CONNBYTES=m
  656. +CONFIG_NETFILTER_XT_MATCH_CONNLABEL=m
  657. +CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=m
  658. +CONFIG_NETFILTER_XT_MATCH_CONNMARK=m
  659. +CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m
  660. +CONFIG_NETFILTER_XT_MATCH_CPU=m
  661. +CONFIG_NETFILTER_XT_MATCH_DCCP=m
  662. +CONFIG_NETFILTER_XT_MATCH_DEVGROUP=m
  663. +CONFIG_NETFILTER_XT_MATCH_DSCP=m
  664. +CONFIG_NETFILTER_XT_MATCH_ESP=m
  665. +CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=m
  666. +CONFIG_NETFILTER_XT_MATCH_HELPER=m
  667. +CONFIG_NETFILTER_XT_MATCH_IPRANGE=m
  668. +CONFIG_NETFILTER_XT_MATCH_IPVS=m
  669. +CONFIG_NETFILTER_XT_MATCH_LENGTH=m
  670. +CONFIG_NETFILTER_XT_MATCH_LIMIT=m
  671. +CONFIG_NETFILTER_XT_MATCH_MAC=m
  672. +CONFIG_NETFILTER_XT_MATCH_MARK=m
  673. +CONFIG_NETFILTER_XT_MATCH_MULTIPORT=m
  674. +CONFIG_NETFILTER_XT_MATCH_NFACCT=m
  675. +CONFIG_NETFILTER_XT_MATCH_OSF=m
  676. +CONFIG_NETFILTER_XT_MATCH_OWNER=m
  677. +CONFIG_NETFILTER_XT_MATCH_POLICY=m
  678. +CONFIG_NETFILTER_XT_MATCH_PHYSDEV=m
  679. +CONFIG_NETFILTER_XT_MATCH_PKTTYPE=m
  680. +CONFIG_NETFILTER_XT_MATCH_QUOTA=m
  681. +CONFIG_NETFILTER_XT_MATCH_RATEEST=m
  682. +CONFIG_NETFILTER_XT_MATCH_REALM=m
  683. +CONFIG_NETFILTER_XT_MATCH_RECENT=m
  684. +CONFIG_NETFILTER_XT_MATCH_SOCKET=m
  685. +CONFIG_NETFILTER_XT_MATCH_STATE=m
  686. +CONFIG_NETFILTER_XT_MATCH_STATISTIC=m
  687. +CONFIG_NETFILTER_XT_MATCH_STRING=m
  688. +CONFIG_NETFILTER_XT_MATCH_TCPMSS=m
  689. +CONFIG_NETFILTER_XT_MATCH_TIME=m
  690. +CONFIG_NETFILTER_XT_MATCH_U32=m
  691. +CONFIG_IP_SET=m
  692. +CONFIG_IP_SET_BITMAP_IP=m
  693. +CONFIG_IP_SET_BITMAP_IPMAC=m
  694. +CONFIG_IP_SET_BITMAP_PORT=m
  695. +CONFIG_IP_SET_HASH_IP=m
  696. +CONFIG_IP_SET_HASH_IPPORT=m
  697. +CONFIG_IP_SET_HASH_IPPORTIP=m
  698. +CONFIG_IP_SET_HASH_IPPORTNET=m
  699. +CONFIG_IP_SET_HASH_NET=m
  700. +CONFIG_IP_SET_HASH_NETPORT=m
  701. +CONFIG_IP_SET_HASH_NETIFACE=m
  702. +CONFIG_IP_SET_LIST_SET=m
  703. +CONFIG_IP_VS=m
  704. +CONFIG_IP_VS_PROTO_TCP=y
  705. +CONFIG_IP_VS_PROTO_UDP=y
  706. +CONFIG_IP_VS_PROTO_ESP=y
  707. +CONFIG_IP_VS_PROTO_AH=y
  708. +CONFIG_IP_VS_PROTO_SCTP=y
  709. +CONFIG_IP_VS_RR=m
  710. +CONFIG_IP_VS_WRR=m
  711. +CONFIG_IP_VS_LC=m
  712. +CONFIG_IP_VS_WLC=m
  713. +CONFIG_IP_VS_LBLC=m
  714. +CONFIG_IP_VS_LBLCR=m
  715. +CONFIG_IP_VS_DH=m
  716. +CONFIG_IP_VS_SH=m
  717. +CONFIG_IP_VS_SED=m
  718. +CONFIG_IP_VS_NQ=m
  719. +CONFIG_IP_VS_FTP=m
  720. +CONFIG_IP_VS_PE_SIP=m
  721. +CONFIG_NF_CONNTRACK_IPV4=m
  722. +CONFIG_IP_NF_IPTABLES=m
  723. +CONFIG_IP_NF_MATCH_AH=m
  724. +CONFIG_IP_NF_MATCH_ECN=m
  725. +CONFIG_IP_NF_MATCH_TTL=m
  726. +CONFIG_IP_NF_FILTER=m
  727. +CONFIG_IP_NF_TARGET_REJECT=m
  728. +CONFIG_IP_NF_TARGET_ULOG=m
  729. +CONFIG_NF_NAT_IPV4=m
  730. +CONFIG_IP_NF_TARGET_MASQUERADE=m
  731. +CONFIG_IP_NF_TARGET_NETMAP=m
  732. +CONFIG_IP_NF_TARGET_REDIRECT=m
  733. +CONFIG_IP_NF_MANGLE=m
  734. +CONFIG_IP_NF_TARGET_ECN=m
  735. +CONFIG_IP_NF_TARGET_TTL=m
  736. +CONFIG_IP_NF_RAW=m
  737. +CONFIG_IP_NF_ARPTABLES=m
  738. +CONFIG_IP_NF_ARPFILTER=m
  739. +CONFIG_IP_NF_ARP_MANGLE=m
  740. +CONFIG_NF_CONNTRACK_IPV6=m
  741. +CONFIG_IP6_NF_IPTABLES=m
  742. +CONFIG_IP6_NF_MATCH_AH=m
  743. +CONFIG_IP6_NF_MATCH_EUI64=m
  744. +CONFIG_IP6_NF_MATCH_FRAG=m
  745. +CONFIG_IP6_NF_MATCH_OPTS=m
  746. +CONFIG_IP6_NF_MATCH_HL=m
  747. +CONFIG_IP6_NF_MATCH_IPV6HEADER=m
  748. +CONFIG_IP6_NF_MATCH_MH=m
  749. +CONFIG_IP6_NF_MATCH_RT=m
  750. +CONFIG_IP6_NF_TARGET_HL=m
  751. +CONFIG_IP6_NF_FILTER=m
  752. +CONFIG_IP6_NF_TARGET_REJECT=m
  753. +CONFIG_IP6_NF_MANGLE=m
  754. +CONFIG_IP6_NF_RAW=m
  755. +CONFIG_NF_NAT_IPV6=m
  756. +CONFIG_IP6_NF_TARGET_MASQUERADE=m
  757. +CONFIG_IP6_NF_TARGET_NPT=m
  758. +CONFIG_BRIDGE_NF_EBTABLES=m
  759. +CONFIG_BRIDGE_EBT_BROUTE=m
  760. +CONFIG_BRIDGE_EBT_T_FILTER=m
  761. +CONFIG_BRIDGE_EBT_T_NAT=m
  762. +CONFIG_BRIDGE_EBT_802_3=m
  763. +CONFIG_BRIDGE_EBT_AMONG=m
  764. +CONFIG_BRIDGE_EBT_ARP=m
  765. +CONFIG_BRIDGE_EBT_IP=m
  766. +CONFIG_BRIDGE_EBT_IP6=m
  767. +CONFIG_BRIDGE_EBT_LIMIT=m
  768. +CONFIG_BRIDGE_EBT_MARK=m
  769. +CONFIG_BRIDGE_EBT_PKTTYPE=m
  770. +CONFIG_BRIDGE_EBT_STP=m
  771. +CONFIG_BRIDGE_EBT_VLAN=m
  772. +CONFIG_BRIDGE_EBT_ARPREPLY=m
  773. +CONFIG_BRIDGE_EBT_DNAT=m
  774. +CONFIG_BRIDGE_EBT_MARK_T=m
  775. +CONFIG_BRIDGE_EBT_REDIRECT=m
  776. +CONFIG_BRIDGE_EBT_SNAT=m
  777. +CONFIG_BRIDGE_EBT_LOG=m
  778. +CONFIG_BRIDGE_EBT_ULOG=m
  779. +CONFIG_BRIDGE_EBT_NFLOG=m
  780. +CONFIG_SCTP_COOKIE_HMAC_SHA1=y
  781. +CONFIG_L2TP=m
  782. +CONFIG_BRIDGE=m
  783. +CONFIG_VLAN_8021Q=m
  784. +CONFIG_VLAN_8021Q_GVRP=y
  785. +CONFIG_ATALK=m
  786. +CONFIG_NET_SCHED=y
  787. +CONFIG_NET_SCH_CBQ=m
  788. +CONFIG_NET_SCH_HTB=m
  789. +CONFIG_NET_SCH_HFSC=m
  790. +CONFIG_NET_SCH_PRIO=m
  791. +CONFIG_NET_SCH_MULTIQ=m
  792. +CONFIG_NET_SCH_RED=m
  793. +CONFIG_NET_SCH_SFB=m
  794. +CONFIG_NET_SCH_SFQ=m
  795. +CONFIG_NET_SCH_TEQL=m
  796. +CONFIG_NET_SCH_TBF=m
  797. +CONFIG_NET_SCH_GRED=m
  798. +CONFIG_NET_SCH_DSMARK=m
  799. +CONFIG_NET_SCH_NETEM=m
  800. +CONFIG_NET_SCH_DRR=m
  801. +CONFIG_NET_SCH_MQPRIO=m
  802. +CONFIG_NET_SCH_CHOKE=m
  803. +CONFIG_NET_SCH_QFQ=m
  804. +CONFIG_NET_SCH_CODEL=m
  805. +CONFIG_NET_SCH_FQ_CODEL=m
  806. +CONFIG_NET_SCH_INGRESS=m
  807. +CONFIG_NET_SCH_PLUG=m
  808. +CONFIG_NET_CLS_BASIC=m
  809. +CONFIG_NET_CLS_TCINDEX=m
  810. +CONFIG_NET_CLS_ROUTE4=m
  811. +CONFIG_NET_CLS_FW=m
  812. +CONFIG_NET_CLS_U32=m
  813. +CONFIG_CLS_U32_MARK=y
  814. +CONFIG_NET_CLS_RSVP=m
  815. +CONFIG_NET_CLS_RSVP6=m
  816. +CONFIG_NET_CLS_FLOW=m
  817. +CONFIG_NET_CLS_CGROUP=m
  818. +CONFIG_NET_EMATCH=y
  819. +CONFIG_NET_EMATCH_CMP=m
  820. +CONFIG_NET_EMATCH_NBYTE=m
  821. +CONFIG_NET_EMATCH_U32=m
  822. +CONFIG_NET_EMATCH_META=m
  823. +CONFIG_NET_EMATCH_TEXT=m
  824. +CONFIG_NET_EMATCH_IPSET=m
  825. +CONFIG_NET_CLS_ACT=y
  826. +CONFIG_NET_ACT_POLICE=m
  827. +CONFIG_NET_ACT_GACT=m
  828. +CONFIG_GACT_PROB=y
  829. +CONFIG_NET_ACT_MIRRED=m
  830. +CONFIG_NET_ACT_IPT=m
  831. +CONFIG_NET_ACT_NAT=m
  832. +CONFIG_NET_ACT_PEDIT=m
  833. +CONFIG_NET_ACT_SIMP=m
  834. +CONFIG_NET_ACT_SKBEDIT=m
  835. +CONFIG_NET_ACT_CSUM=m
  836. +CONFIG_BATMAN_ADV=m
  837. +CONFIG_OPENVSWITCH=m
  838. +CONFIG_NET_PKTGEN=m
  839. +CONFIG_HAMRADIO=y
  840. +CONFIG_AX25=m
  841. +CONFIG_NETROM=m
  842. +CONFIG_ROSE=m
  843. +CONFIG_MKISS=m
  844. +CONFIG_6PACK=m
  845. +CONFIG_BPQETHER=m
  846. +CONFIG_BAYCOM_SER_FDX=m
  847. +CONFIG_BAYCOM_SER_HDX=m
  848. +CONFIG_YAM=m
  849. +CONFIG_IRDA=m
  850. +CONFIG_IRLAN=m
  851. +CONFIG_IRNET=m
  852. +CONFIG_IRCOMM=m
  853. +CONFIG_IRDA_ULTRA=y
  854. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  855. +CONFIG_IRDA_FAST_RR=y
  856. +CONFIG_IRTTY_SIR=m
  857. +CONFIG_KINGSUN_DONGLE=m
  858. +CONFIG_KSDAZZLE_DONGLE=m
  859. +CONFIG_KS959_DONGLE=m
  860. +CONFIG_USB_IRDA=m
  861. +CONFIG_SIGMATEL_FIR=m
  862. +CONFIG_MCS_FIR=m
  863. +CONFIG_BT=m
  864. +CONFIG_BT_RFCOMM=m
  865. +CONFIG_BT_RFCOMM_TTY=y
  866. +CONFIG_BT_BNEP=m
  867. +CONFIG_BT_BNEP_MC_FILTER=y
  868. +CONFIG_BT_BNEP_PROTO_FILTER=y
  869. +CONFIG_BT_HIDP=m
  870. +CONFIG_BT_HCIBTUSB=m
  871. +CONFIG_BT_HCIBCM203X=m
  872. +CONFIG_BT_HCIBPA10X=m
  873. +CONFIG_BT_HCIBFUSB=m
  874. +CONFIG_BT_HCIVHCI=m
  875. +CONFIG_BT_MRVL=m
  876. +CONFIG_BT_MRVL_SDIO=m
  877. +CONFIG_BT_ATH3K=m
  878. +CONFIG_BT_WILINK=m
  879. +CONFIG_CFG80211=m
  880. +CONFIG_CFG80211_WEXT=y
  881. +CONFIG_MAC80211=m
  882. +CONFIG_MAC80211_RC_PID=y
  883. +CONFIG_MAC80211_MESH=y
  884. +CONFIG_WIMAX=m
  885. +CONFIG_RFKILL=m
  886. +CONFIG_RFKILL_INPUT=y
  887. +CONFIG_NET_9P=m
  888. +CONFIG_NFC=m
  889. +CONFIG_NFC_PN533=m
  890. +CONFIG_DEVTMPFS=y
  891. +CONFIG_DEVTMPFS_MOUNT=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_CHR_DEV_SG=m
  905. +CONFIG_SCSI_MULTI_LUN=y
  906. +CONFIG_SCSI_ISCSI_ATTRS=y
  907. +CONFIG_ISCSI_TCP=m
  908. +CONFIG_ISCSI_BOOT_SYSFS=m
  909. +CONFIG_MD=y
  910. +CONFIG_MD_LINEAR=m
  911. +CONFIG_MD_RAID0=m
  912. +CONFIG_BLK_DEV_DM=m
  913. +CONFIG_DM_CRYPT=m
  914. +CONFIG_DM_SNAPSHOT=m
  915. +CONFIG_DM_MIRROR=m
  916. +CONFIG_DM_LOG_USERSPACE=m
  917. +CONFIG_DM_RAID=m
  918. +CONFIG_DM_ZERO=m
  919. +CONFIG_DM_DELAY=m
  920. +CONFIG_NETDEVICES=y
  921. +CONFIG_BONDING=m
  922. +CONFIG_DUMMY=m
  923. +CONFIG_IFB=m
  924. +CONFIG_MACVLAN=m
  925. +CONFIG_NETCONSOLE=m
  926. +CONFIG_TUN=m
  927. +CONFIG_MDIO_BITBANG=m
  928. +CONFIG_PPP=m
  929. +CONFIG_PPP_BSDCOMP=m
  930. +CONFIG_PPP_DEFLATE=m
  931. +CONFIG_PPP_FILTER=y
  932. +CONFIG_PPP_MPPE=m
  933. +CONFIG_PPP_MULTILINK=y
  934. +CONFIG_PPPOE=m
  935. +CONFIG_PPPOL2TP=m
  936. +CONFIG_PPP_ASYNC=m
  937. +CONFIG_PPP_SYNC_TTY=m
  938. +CONFIG_SLIP=m
  939. +CONFIG_SLIP_COMPRESSED=y
  940. +CONFIG_SLIP_SMART=y
  941. +CONFIG_USB_CATC=m
  942. +CONFIG_USB_KAWETH=m
  943. +CONFIG_USB_PEGASUS=m
  944. +CONFIG_USB_RTL8150=m
  945. +CONFIG_USB_RTL8152=m
  946. +CONFIG_USB_USBNET=y
  947. +CONFIG_USB_NET_AX8817X=m
  948. +CONFIG_USB_NET_AX88179_178A=m
  949. +CONFIG_USB_NET_CDCETHER=m
  950. +CONFIG_USB_NET_CDC_EEM=m
  951. +CONFIG_USB_NET_CDC_NCM=m
  952. +CONFIG_USB_NET_CDC_MBIM=m
  953. +CONFIG_USB_NET_DM9601=m
  954. +CONFIG_USB_NET_SMSC75XX=m
  955. +CONFIG_USB_NET_SMSC95XX=y
  956. +CONFIG_USB_NET_GL620A=m
  957. +CONFIG_USB_NET_NET1080=m
  958. +CONFIG_USB_NET_PLUSB=m
  959. +CONFIG_USB_NET_MCS7830=m
  960. +CONFIG_USB_NET_CDC_SUBSET=m
  961. +CONFIG_USB_ALI_M5632=y
  962. +CONFIG_USB_AN2720=y
  963. +CONFIG_USB_EPSON2888=y
  964. +CONFIG_USB_KC2190=y
  965. +CONFIG_USB_NET_ZAURUS=m
  966. +CONFIG_USB_NET_CX82310_ETH=m
  967. +CONFIG_USB_NET_KALMIA=m
  968. +CONFIG_USB_NET_QMI_WWAN=m
  969. +CONFIG_USB_NET_INT51X1=m
  970. +CONFIG_USB_IPHETH=m
  971. +CONFIG_USB_SIERRA_NET=m
  972. +CONFIG_USB_VL600=m
  973. +CONFIG_LIBERTAS_THINFIRM=m
  974. +CONFIG_LIBERTAS_THINFIRM_USB=m
  975. +CONFIG_AT76C50X_USB=m
  976. +CONFIG_USB_ZD1201=m
  977. +CONFIG_USB_NET_RNDIS_WLAN=m
  978. +CONFIG_RTL8187=m
  979. +CONFIG_MAC80211_HWSIM=m
  980. +CONFIG_ATH_CARDS=m
  981. +CONFIG_ATH9K=m
  982. +CONFIG_ATH9K_HTC=m
  983. +CONFIG_CARL9170=m
  984. +CONFIG_ATH6KL=m
  985. +CONFIG_ATH6KL_USB=m
  986. +CONFIG_AR5523=m
  987. +CONFIG_B43=m
  988. +# CONFIG_B43_PHY_N is not set
  989. +CONFIG_B43LEGACY=m
  990. +CONFIG_HOSTAP=m
  991. +CONFIG_LIBERTAS=m
  992. +CONFIG_LIBERTAS_USB=m
  993. +CONFIG_LIBERTAS_SDIO=m
  994. +CONFIG_P54_COMMON=m
  995. +CONFIG_P54_USB=m
  996. +CONFIG_RT2X00=m
  997. +CONFIG_RT2500USB=m
  998. +CONFIG_RT73USB=m
  999. +CONFIG_RT2800USB=m
  1000. +CONFIG_RT2800USB_RT3573=y
  1001. +CONFIG_RT2800USB_RT53XX=y
  1002. +CONFIG_RT2800USB_RT55XX=y
  1003. +CONFIG_RT2800USB_UNKNOWN=y
  1004. +CONFIG_RTL8192CU=m
  1005. +CONFIG_ZD1211RW=m
  1006. +CONFIG_MWIFIEX=m
  1007. +CONFIG_MWIFIEX_SDIO=m
  1008. +CONFIG_WIMAX_I2400M_USB=m
  1009. +CONFIG_INPUT_POLLDEV=m
  1010. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1011. +CONFIG_INPUT_JOYDEV=m
  1012. +CONFIG_INPUT_EVDEV=m
  1013. +# CONFIG_INPUT_KEYBOARD is not set
  1014. +# CONFIG_INPUT_MOUSE is not set
  1015. +CONFIG_INPUT_JOYSTICK=y
  1016. +CONFIG_JOYSTICK_IFORCE=m
  1017. +CONFIG_JOYSTICK_IFORCE_USB=y
  1018. +CONFIG_JOYSTICK_XPAD=m
  1019. +CONFIG_JOYSTICK_XPAD_FF=y
  1020. +CONFIG_INPUT_MISC=y
  1021. +CONFIG_INPUT_AD714X=m
  1022. +CONFIG_INPUT_ATI_REMOTE2=m
  1023. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1024. +CONFIG_INPUT_POWERMATE=m
  1025. +CONFIG_INPUT_YEALINK=m
  1026. +CONFIG_INPUT_CM109=m
  1027. +CONFIG_INPUT_UINPUT=m
  1028. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1029. +CONFIG_INPUT_ADXL34X=m
  1030. +CONFIG_INPUT_CMA3000=m
  1031. +CONFIG_SERIO=m
  1032. +CONFIG_SERIO_RAW=m
  1033. +CONFIG_GAMEPORT=m
  1034. +CONFIG_GAMEPORT_NS558=m
  1035. +CONFIG_GAMEPORT_L4=m
  1036. +# CONFIG_LEGACY_PTYS is not set
  1037. +# CONFIG_DEVKMEM is not set
  1038. +CONFIG_SERIAL_AMBA_PL011=y
  1039. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1040. +CONFIG_TTY_PRINTK=y
  1041. +CONFIG_HW_RANDOM=y
  1042. +CONFIG_HW_RANDOM_BCM2708=m
  1043. +CONFIG_RAW_DRIVER=y
  1044. +CONFIG_BRCM_CHAR_DRIVERS=y
  1045. +CONFIG_BCM_VC_CMA=y
  1046. +CONFIG_I2C=y
  1047. +CONFIG_I2C_CHARDEV=m
  1048. +CONFIG_I2C_BCM2708=m
  1049. +CONFIG_SPI=y
  1050. +CONFIG_SPI_BCM2708=m
  1051. +CONFIG_SPI_SPIDEV=y
  1052. +CONFIG_GPIO_SYSFS=y
  1053. +CONFIG_W1=m
  1054. +CONFIG_W1_MASTER_DS2490=m
  1055. +CONFIG_W1_MASTER_DS2482=m
  1056. +CONFIG_W1_MASTER_DS1WM=m
  1057. +CONFIG_W1_MASTER_GPIO=m
  1058. +CONFIG_W1_SLAVE_THERM=m
  1059. +CONFIG_W1_SLAVE_SMEM=m
  1060. +CONFIG_W1_SLAVE_DS2408=m
  1061. +CONFIG_W1_SLAVE_DS2413=m
  1062. +CONFIG_W1_SLAVE_DS2423=m
  1063. +CONFIG_W1_SLAVE_DS2431=m
  1064. +CONFIG_W1_SLAVE_DS2433=m
  1065. +CONFIG_W1_SLAVE_DS2760=m
  1066. +CONFIG_W1_SLAVE_DS2780=m
  1067. +CONFIG_W1_SLAVE_DS2781=m
  1068. +CONFIG_W1_SLAVE_DS28E04=m
  1069. +CONFIG_W1_SLAVE_BQ27000=m
  1070. +CONFIG_BATTERY_DS2760=m
  1071. +# CONFIG_HWMON is not set
  1072. +CONFIG_THERMAL=y
  1073. +CONFIG_THERMAL_BCM2835=y
  1074. +CONFIG_WATCHDOG=y
  1075. +CONFIG_BCM2708_WDT=m
  1076. +CONFIG_MEDIA_SUPPORT=m
  1077. +CONFIG_MEDIA_CAMERA_SUPPORT=y
  1078. +CONFIG_MEDIA_ANALOG_TV_SUPPORT=y
  1079. +CONFIG_MEDIA_DIGITAL_TV_SUPPORT=y
  1080. +CONFIG_MEDIA_RADIO_SUPPORT=y
  1081. +CONFIG_MEDIA_RC_SUPPORT=y
  1082. +CONFIG_MEDIA_CONTROLLER=y
  1083. +CONFIG_LIRC=m
  1084. +CONFIG_RC_DEVICES=y
  1085. +CONFIG_RC_ATI_REMOTE=m
  1086. +CONFIG_IR_IMON=m
  1087. +CONFIG_IR_MCEUSB=m
  1088. +CONFIG_IR_REDRAT3=m
  1089. +CONFIG_IR_STREAMZAP=m
  1090. +CONFIG_IR_IGUANA=m
  1091. +CONFIG_IR_TTUSBIR=m
  1092. +CONFIG_RC_LOOPBACK=m
  1093. +CONFIG_IR_GPIO_CIR=m
  1094. +CONFIG_MEDIA_USB_SUPPORT=y
  1095. +CONFIG_USB_VIDEO_CLASS=m
  1096. +CONFIG_USB_M5602=m
  1097. +CONFIG_USB_STV06XX=m
  1098. +CONFIG_USB_GL860=m
  1099. +CONFIG_USB_GSPCA_BENQ=m
  1100. +CONFIG_USB_GSPCA_CONEX=m
  1101. +CONFIG_USB_GSPCA_CPIA1=m
  1102. +CONFIG_USB_GSPCA_ETOMS=m
  1103. +CONFIG_USB_GSPCA_FINEPIX=m
  1104. +CONFIG_USB_GSPCA_JEILINJ=m
  1105. +CONFIG_USB_GSPCA_JL2005BCD=m
  1106. +CONFIG_USB_GSPCA_KINECT=m
  1107. +CONFIG_USB_GSPCA_KONICA=m
  1108. +CONFIG_USB_GSPCA_MARS=m
  1109. +CONFIG_USB_GSPCA_MR97310A=m
  1110. +CONFIG_USB_GSPCA_NW80X=m
  1111. +CONFIG_USB_GSPCA_OV519=m
  1112. +CONFIG_USB_GSPCA_OV534=m
  1113. +CONFIG_USB_GSPCA_OV534_9=m
  1114. +CONFIG_USB_GSPCA_PAC207=m
  1115. +CONFIG_USB_GSPCA_PAC7302=m
  1116. +CONFIG_USB_GSPCA_PAC7311=m
  1117. +CONFIG_USB_GSPCA_SE401=m
  1118. +CONFIG_USB_GSPCA_SN9C2028=m
  1119. +CONFIG_USB_GSPCA_SN9C20X=m
  1120. +CONFIG_USB_GSPCA_SONIXB=m
  1121. +CONFIG_USB_GSPCA_SONIXJ=m
  1122. +CONFIG_USB_GSPCA_SPCA500=m
  1123. +CONFIG_USB_GSPCA_SPCA501=m
  1124. +CONFIG_USB_GSPCA_SPCA505=m
  1125. +CONFIG_USB_GSPCA_SPCA506=m
  1126. +CONFIG_USB_GSPCA_SPCA508=m
  1127. +CONFIG_USB_GSPCA_SPCA561=m
  1128. +CONFIG_USB_GSPCA_SPCA1528=m
  1129. +CONFIG_USB_GSPCA_SQ905=m
  1130. +CONFIG_USB_GSPCA_SQ905C=m
  1131. +CONFIG_USB_GSPCA_SQ930X=m
  1132. +CONFIG_USB_GSPCA_STK014=m
  1133. +CONFIG_USB_GSPCA_STV0680=m
  1134. +CONFIG_USB_GSPCA_SUNPLUS=m
  1135. +CONFIG_USB_GSPCA_T613=m
  1136. +CONFIG_USB_GSPCA_TOPRO=m
  1137. +CONFIG_USB_GSPCA_TV8532=m
  1138. +CONFIG_USB_GSPCA_VC032X=m
  1139. +CONFIG_USB_GSPCA_VICAM=m
  1140. +CONFIG_USB_GSPCA_XIRLINK_CIT=m
  1141. +CONFIG_USB_GSPCA_ZC3XX=m
  1142. +CONFIG_USB_PWC=m
  1143. +CONFIG_VIDEO_CPIA2=m
  1144. +CONFIG_USB_ZR364XX=m
  1145. +CONFIG_USB_STKWEBCAM=m
  1146. +CONFIG_USB_S2255=m
  1147. +CONFIG_USB_SN9C102=m
  1148. +CONFIG_VIDEO_PVRUSB2=m
  1149. +CONFIG_VIDEO_HDPVR=m
  1150. +CONFIG_VIDEO_TLG2300=m
  1151. +CONFIG_VIDEO_USBVISION=m
  1152. +CONFIG_VIDEO_AU0828=m
  1153. +CONFIG_VIDEO_CX231XX=m
  1154. +CONFIG_VIDEO_CX231XX_ALSA=m
  1155. +CONFIG_VIDEO_CX231XX_DVB=m
  1156. +CONFIG_VIDEO_TM6000=m
  1157. +CONFIG_VIDEO_TM6000_ALSA=m
  1158. +CONFIG_VIDEO_TM6000_DVB=m
  1159. +CONFIG_DVB_USB=m
  1160. +CONFIG_DVB_USB_A800=m
  1161. +CONFIG_DVB_USB_DIBUSB_MB=m
  1162. +CONFIG_DVB_USB_DIBUSB_MB_FAULTY=y
  1163. +CONFIG_DVB_USB_DIBUSB_MC=m
  1164. +CONFIG_DVB_USB_DIB0700=m
  1165. +CONFIG_DVB_USB_UMT_010=m
  1166. +CONFIG_DVB_USB_CXUSB=m
  1167. +CONFIG_DVB_USB_M920X=m
  1168. +CONFIG_DVB_USB_DIGITV=m
  1169. +CONFIG_DVB_USB_VP7045=m
  1170. +CONFIG_DVB_USB_VP702X=m
  1171. +CONFIG_DVB_USB_GP8PSK=m
  1172. +CONFIG_DVB_USB_NOVA_T_USB2=m
  1173. +CONFIG_DVB_USB_TTUSB2=m
  1174. +CONFIG_DVB_USB_DTT200U=m
  1175. +CONFIG_DVB_USB_OPERA1=m
  1176. +CONFIG_DVB_USB_AF9005=m
  1177. +CONFIG_DVB_USB_AF9005_REMOTE=m
  1178. +CONFIG_DVB_USB_PCTV452E=m
  1179. +CONFIG_DVB_USB_DW2102=m
  1180. +CONFIG_DVB_USB_CINERGY_T2=m
  1181. +CONFIG_DVB_USB_DTV5100=m
  1182. +CONFIG_DVB_USB_FRIIO=m
  1183. +CONFIG_DVB_USB_AZ6027=m
  1184. +CONFIG_DVB_USB_TECHNISAT_USB2=m
  1185. +CONFIG_DVB_USB_V2=m
  1186. +CONFIG_DVB_USB_AF9015=m
  1187. +CONFIG_DVB_USB_AF9035=m
  1188. +CONFIG_DVB_USB_ANYSEE=m
  1189. +CONFIG_DVB_USB_AU6610=m
  1190. +CONFIG_DVB_USB_AZ6007=m
  1191. +CONFIG_DVB_USB_CE6230=m
  1192. +CONFIG_DVB_USB_EC168=m
  1193. +CONFIG_DVB_USB_GL861=m
  1194. +CONFIG_DVB_USB_IT913X=m
  1195. +CONFIG_DVB_USB_LME2510=m
  1196. +CONFIG_DVB_USB_MXL111SF=m
  1197. +CONFIG_DVB_USB_RTL28XXU=m
  1198. +CONFIG_SMS_USB_DRV=m
  1199. +CONFIG_DVB_B2C2_FLEXCOP_USB=m
  1200. +CONFIG_VIDEO_EM28XX=m
  1201. +CONFIG_VIDEO_EM28XX_ALSA=m
  1202. +CONFIG_VIDEO_EM28XX_DVB=m
  1203. +CONFIG_V4L_PLATFORM_DRIVERS=y
  1204. +CONFIG_VIDEO_BCM2835=y
  1205. +CONFIG_VIDEO_BCM2835_MMAL=m
  1206. +CONFIG_RADIO_SI470X=y
  1207. +CONFIG_USB_SI470X=m
  1208. +CONFIG_I2C_SI470X=m
  1209. +CONFIG_USB_MR800=m
  1210. +CONFIG_USB_DSBR=m
  1211. +CONFIG_RADIO_SHARK=m
  1212. +CONFIG_RADIO_SHARK2=m
  1213. +CONFIG_RADIO_SI4713=m
  1214. +CONFIG_USB_KEENE=m
  1215. +CONFIG_USB_MA901=m
  1216. +CONFIG_RADIO_TEA5764=m
  1217. +CONFIG_RADIO_SAA7706H=m
  1218. +CONFIG_RADIO_TEF6862=m
  1219. +CONFIG_RADIO_WL1273=m
  1220. +CONFIG_RADIO_WL128X=m
  1221. +CONFIG_FB=y
  1222. +CONFIG_FB_BCM2708=y
  1223. +# CONFIG_BACKLIGHT_GENERIC is not set
  1224. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1225. +CONFIG_LOGO=y
  1226. +# CONFIG_LOGO_LINUX_MONO is not set
  1227. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1228. +CONFIG_SOUND=y
  1229. +CONFIG_SND=m
  1230. +CONFIG_SND_SEQUENCER=m
  1231. +CONFIG_SND_SEQ_DUMMY=m
  1232. +CONFIG_SND_MIXER_OSS=m
  1233. +CONFIG_SND_PCM_OSS=m
  1234. +CONFIG_SND_SEQUENCER_OSS=y
  1235. +CONFIG_SND_HRTIMER=m
  1236. +CONFIG_SND_DUMMY=m
  1237. +CONFIG_SND_ALOOP=m
  1238. +CONFIG_SND_VIRMIDI=m
  1239. +CONFIG_SND_MTPAV=m
  1240. +CONFIG_SND_SERIAL_U16550=m
  1241. +CONFIG_SND_MPU401=m
  1242. +CONFIG_SND_BCM2835=m
  1243. +CONFIG_SND_USB_AUDIO=m
  1244. +CONFIG_SND_USB_UA101=m
  1245. +CONFIG_SND_USB_CAIAQ=m
  1246. +CONFIG_SND_USB_CAIAQ_INPUT=y
  1247. +CONFIG_SND_USB_6FIRE=m
  1248. +CONFIG_SND_SOC=m
  1249. +CONFIG_SND_SOC_DMAENGINE_PCM=y
  1250. +CONFIG_SND_SOC_GENERIC_DMAENGINE_PCM=y
  1251. +CONFIG_SND_SOC_WM8804=m
  1252. +CONFIG_SND_BCM2708_SOC_I2S=m
  1253. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
  1254. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
  1255. +CONFIG_SND_BCM2708_SOC_RPI_DAC=m
  1256. +CONFIG_SND_SOC_I2C_AND_SPI=m
  1257. +CONFIG_SND_SOC_PCM5102A=m
  1258. +CONFIG_SND_SOC_PCM1794A=m
  1259. +CONFIG_SOUND_PRIME=m
  1260. +CONFIG_HIDRAW=y
  1261. +CONFIG_HID_A4TECH=m
  1262. +CONFIG_HID_ACRUX=m
  1263. +CONFIG_HID_APPLE=m
  1264. +CONFIG_HID_BELKIN=m
  1265. +CONFIG_HID_CHERRY=m
  1266. +CONFIG_HID_CHICONY=m
  1267. +CONFIG_HID_CYPRESS=m
  1268. +CONFIG_HID_DRAGONRISE=m
  1269. +CONFIG_HID_EMS_FF=m
  1270. +CONFIG_HID_ELECOM=m
  1271. +CONFIG_HID_EZKEY=m
  1272. +CONFIG_HID_HOLTEK=m
  1273. +CONFIG_HID_KEYTOUCH=m
  1274. +CONFIG_HID_KYE=m
  1275. +CONFIG_HID_UCLOGIC=m
  1276. +CONFIG_HID_WALTOP=m
  1277. +CONFIG_HID_GYRATION=m
  1278. +CONFIG_HID_TWINHAN=m
  1279. +CONFIG_HID_KENSINGTON=m
  1280. +CONFIG_HID_LCPOWER=m
  1281. +CONFIG_HID_LOGITECH=m
  1282. +CONFIG_HID_MAGICMOUSE=m
  1283. +CONFIG_HID_MICROSOFT=m
  1284. +CONFIG_HID_MONTEREY=m
  1285. +CONFIG_HID_MULTITOUCH=m
  1286. +CONFIG_HID_NTRIG=m
  1287. +CONFIG_HID_ORTEK=m
  1288. +CONFIG_HID_PANTHERLORD=m
  1289. +CONFIG_HID_PETALYNX=m
  1290. +CONFIG_HID_PICOLCD=m
  1291. +CONFIG_HID_ROCCAT=m
  1292. +CONFIG_HID_SAMSUNG=m
  1293. +CONFIG_HID_SONY=m
  1294. +CONFIG_HID_SPEEDLINK=m
  1295. +CONFIG_HID_SUNPLUS=m
  1296. +CONFIG_HID_GREENASIA=m
  1297. +CONFIG_HID_SMARTJOYPLUS=m
  1298. +CONFIG_HID_TOPSEED=m
  1299. +CONFIG_HID_THINGM=m
  1300. +CONFIG_HID_THRUSTMASTER=m
  1301. +CONFIG_HID_WACOM=m
  1302. +CONFIG_HID_WIIMOTE=m
  1303. +CONFIG_HID_ZEROPLUS=m
  1304. +CONFIG_HID_ZYDACRON=m
  1305. +CONFIG_HID_PID=y
  1306. +CONFIG_USB_HIDDEV=y
  1307. +CONFIG_USB=y
  1308. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1309. +CONFIG_USB_MON=m
  1310. +CONFIG_USB_DWCOTG=y
  1311. +CONFIG_USB_PRINTER=m
  1312. +CONFIG_USB_STORAGE=y
  1313. +CONFIG_USB_STORAGE_REALTEK=m
  1314. +CONFIG_USB_STORAGE_DATAFAB=m
  1315. +CONFIG_USB_STORAGE_FREECOM=m
  1316. +CONFIG_USB_STORAGE_ISD200=m
  1317. +CONFIG_USB_STORAGE_USBAT=m
  1318. +CONFIG_USB_STORAGE_SDDR09=m
  1319. +CONFIG_USB_STORAGE_SDDR55=m
  1320. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1321. +CONFIG_USB_STORAGE_ALAUDA=m
  1322. +CONFIG_USB_STORAGE_ONETOUCH=m
  1323. +CONFIG_USB_STORAGE_KARMA=m
  1324. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1325. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1326. +CONFIG_USB_MDC800=m
  1327. +CONFIG_USB_MICROTEK=m
  1328. +CONFIG_USB_SERIAL=m
  1329. +CONFIG_USB_SERIAL_GENERIC=y
  1330. +CONFIG_USB_SERIAL_AIRCABLE=m
  1331. +CONFIG_USB_SERIAL_ARK3116=m
  1332. +CONFIG_USB_SERIAL_BELKIN=m
  1333. +CONFIG_USB_SERIAL_CH341=m
  1334. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1335. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1336. +CONFIG_USB_SERIAL_CP210X=m
  1337. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1338. +CONFIG_USB_SERIAL_EMPEG=m
  1339. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1340. +CONFIG_USB_SERIAL_VISOR=m
  1341. +CONFIG_USB_SERIAL_IPAQ=m
  1342. +CONFIG_USB_SERIAL_IR=m
  1343. +CONFIG_USB_SERIAL_EDGEPORT=m
  1344. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1345. +CONFIG_USB_SERIAL_F81232=m
  1346. +CONFIG_USB_SERIAL_GARMIN=m
  1347. +CONFIG_USB_SERIAL_IPW=m
  1348. +CONFIG_USB_SERIAL_IUU=m
  1349. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1350. +CONFIG_USB_SERIAL_KEYSPAN=m
  1351. +CONFIG_USB_SERIAL_KLSI=m
  1352. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1353. +CONFIG_USB_SERIAL_MCT_U232=m
  1354. +CONFIG_USB_SERIAL_METRO=m
  1355. +CONFIG_USB_SERIAL_MOS7720=m
  1356. +CONFIG_USB_SERIAL_MOS7840=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_SAFE=m
  1364. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1365. +CONFIG_USB_SERIAL_SYMBOL=m
  1366. +CONFIG_USB_SERIAL_TI=m
  1367. +CONFIG_USB_SERIAL_CYBERJACK=m
  1368. +CONFIG_USB_SERIAL_XIRCOM=m
  1369. +CONFIG_USB_SERIAL_OPTION=m
  1370. +CONFIG_USB_SERIAL_OMNINET=m
  1371. +CONFIG_USB_SERIAL_OPTICON=m
  1372. +CONFIG_USB_SERIAL_XSENS_MT=m
  1373. +CONFIG_USB_SERIAL_WISHBONE=m
  1374. +CONFIG_USB_SERIAL_ZTE=m
  1375. +CONFIG_USB_SERIAL_SSU100=m
  1376. +CONFIG_USB_SERIAL_QT2=m
  1377. +CONFIG_USB_SERIAL_DEBUG=m
  1378. +CONFIG_USB_EMI62=m
  1379. +CONFIG_USB_EMI26=m
  1380. +CONFIG_USB_ADUTUX=m
  1381. +CONFIG_USB_SEVSEG=m
  1382. +CONFIG_USB_RIO500=m
  1383. +CONFIG_USB_LEGOTOWER=m
  1384. +CONFIG_USB_LCD=m
  1385. +CONFIG_USB_LED=m
  1386. +CONFIG_USB_CYPRESS_CY7C63=m
  1387. +CONFIG_USB_CYTHERM=m
  1388. +CONFIG_USB_IDMOUSE=m
  1389. +CONFIG_USB_FTDI_ELAN=m
  1390. +CONFIG_USB_APPLEDISPLAY=m
  1391. +CONFIG_USB_LD=m
  1392. +CONFIG_USB_TRANCEVIBRATOR=m
  1393. +CONFIG_USB_IOWARRIOR=m
  1394. +CONFIG_USB_TEST=m
  1395. +CONFIG_USB_ISIGHTFW=m
  1396. +CONFIG_USB_YUREX=m
  1397. +CONFIG_MMC=y
  1398. +CONFIG_MMC_BLOCK_MINORS=32
  1399. +CONFIG_MMC_SDHCI=y
  1400. +CONFIG_MMC_SDHCI_PLTFM=y
  1401. +CONFIG_MMC_SDHCI_BCM2708=y
  1402. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1403. +CONFIG_MMC_SPI=m
  1404. +CONFIG_LEDS_GPIO=m
  1405. +CONFIG_LEDS_TRIGGER_TIMER=y
  1406. +CONFIG_LEDS_TRIGGER_ONESHOT=y
  1407. +CONFIG_LEDS_TRIGGER_HEARTBEAT=y
  1408. +CONFIG_LEDS_TRIGGER_BACKLIGHT=y
  1409. +CONFIG_LEDS_TRIGGER_CPU=y
  1410. +CONFIG_LEDS_TRIGGER_GPIO=y
  1411. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=y
  1412. +CONFIG_LEDS_TRIGGER_TRANSIENT=m
  1413. +CONFIG_LEDS_TRIGGER_CAMERA=m
  1414. +CONFIG_RTC_CLASS=y
  1415. +# CONFIG_RTC_HCTOSYS is not set
  1416. +CONFIG_RTC_DRV_DS1307=m
  1417. +CONFIG_RTC_DRV_DS1374=m
  1418. +CONFIG_RTC_DRV_DS1672=m
  1419. +CONFIG_RTC_DRV_DS3232=m
  1420. +CONFIG_RTC_DRV_MAX6900=m
  1421. +CONFIG_RTC_DRV_RS5C372=m
  1422. +CONFIG_RTC_DRV_ISL1208=m
  1423. +CONFIG_RTC_DRV_ISL12022=m
  1424. +CONFIG_RTC_DRV_X1205=m
  1425. +CONFIG_RTC_DRV_PCF8523=m
  1426. +CONFIG_RTC_DRV_PCF8563=m
  1427. +CONFIG_RTC_DRV_PCF8583=m
  1428. +CONFIG_RTC_DRV_M41T80=m
  1429. +CONFIG_RTC_DRV_BQ32K=m
  1430. +CONFIG_RTC_DRV_S35390A=m
  1431. +CONFIG_RTC_DRV_FM3130=m
  1432. +CONFIG_RTC_DRV_RX8581=m
  1433. +CONFIG_RTC_DRV_RX8025=m
  1434. +CONFIG_RTC_DRV_EM3027=m
  1435. +CONFIG_RTC_DRV_RV3029C2=m
  1436. +CONFIG_RTC_DRV_M41T93=m
  1437. +CONFIG_RTC_DRV_M41T94=m
  1438. +CONFIG_RTC_DRV_DS1305=m
  1439. +CONFIG_RTC_DRV_DS1390=m
  1440. +CONFIG_RTC_DRV_MAX6902=m
  1441. +CONFIG_RTC_DRV_R9701=m
  1442. +CONFIG_RTC_DRV_RS5C348=m
  1443. +CONFIG_RTC_DRV_DS3234=m
  1444. +CONFIG_RTC_DRV_PCF2123=m
  1445. +CONFIG_RTC_DRV_RX4581=m
  1446. +CONFIG_DMADEVICES=y
  1447. +CONFIG_DMA_BCM2708=m
  1448. +CONFIG_DMA_ENGINE=y
  1449. +CONFIG_DMA_VIRTUAL_CHANNELS=m
  1450. +CONFIG_UIO=m
  1451. +CONFIG_UIO_PDRV_GENIRQ=m
  1452. +CONFIG_STAGING=y
  1453. +CONFIG_W35UND=m
  1454. +CONFIG_PRISM2_USB=m
  1455. +CONFIG_R8712U=m
  1456. +CONFIG_VT6656=m
  1457. +CONFIG_SPEAKUP=m
  1458. +CONFIG_SPEAKUP_SYNTH_SOFT=m
  1459. +CONFIG_STAGING_MEDIA=y
  1460. +CONFIG_DVB_AS102=m
  1461. +CONFIG_LIRC_STAGING=y
  1462. +CONFIG_LIRC_IGORPLUGUSB=m
  1463. +CONFIG_LIRC_IMON=m
  1464. +CONFIG_LIRC_RPI=m
  1465. +CONFIG_LIRC_SASEM=m
  1466. +CONFIG_LIRC_SERIAL=m
  1467. +# CONFIG_IOMMU_SUPPORT is not set
  1468. +CONFIG_EXT4_FS=y
  1469. +CONFIG_EXT4_FS_POSIX_ACL=y
  1470. +CONFIG_EXT4_FS_SECURITY=y
  1471. +CONFIG_REISERFS_FS=m
  1472. +CONFIG_REISERFS_FS_XATTR=y
  1473. +CONFIG_REISERFS_FS_POSIX_ACL=y
  1474. +CONFIG_REISERFS_FS_SECURITY=y
  1475. +CONFIG_JFS_FS=m
  1476. +CONFIG_JFS_POSIX_ACL=y
  1477. +CONFIG_JFS_SECURITY=y
  1478. +CONFIG_JFS_STATISTICS=y
  1479. +CONFIG_XFS_FS=m
  1480. +CONFIG_XFS_QUOTA=y
  1481. +CONFIG_XFS_POSIX_ACL=y
  1482. +CONFIG_XFS_RT=y
  1483. +CONFIG_GFS2_FS=m
  1484. +CONFIG_OCFS2_FS=m
  1485. +CONFIG_BTRFS_FS=m
  1486. +CONFIG_BTRFS_FS_POSIX_ACL=y
  1487. +CONFIG_NILFS2_FS=m
  1488. +CONFIG_FANOTIFY=y
  1489. +CONFIG_QFMT_V1=m
  1490. +CONFIG_QFMT_V2=m
  1491. +CONFIG_AUTOFS4_FS=y
  1492. +CONFIG_FUSE_FS=m
  1493. +CONFIG_CUSE=m
  1494. +CONFIG_FSCACHE=y
  1495. +CONFIG_FSCACHE_STATS=y
  1496. +CONFIG_FSCACHE_HISTOGRAM=y
  1497. +CONFIG_CACHEFILES=y
  1498. +CONFIG_ISO9660_FS=m
  1499. +CONFIG_JOLIET=y
  1500. +CONFIG_ZISOFS=y
  1501. +CONFIG_UDF_FS=m
  1502. +CONFIG_MSDOS_FS=y
  1503. +CONFIG_VFAT_FS=y
  1504. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1505. +CONFIG_NTFS_FS=m
  1506. +CONFIG_NTFS_RW=y
  1507. +CONFIG_TMPFS=y
  1508. +CONFIG_TMPFS_POSIX_ACL=y
  1509. +CONFIG_CONFIGFS_FS=y
  1510. +CONFIG_ECRYPT_FS=m
  1511. +CONFIG_HFS_FS=m
  1512. +CONFIG_HFSPLUS_FS=m
  1513. +CONFIG_SQUASHFS=m
  1514. +CONFIG_SQUASHFS_XATTR=y
  1515. +CONFIG_SQUASHFS_LZO=y
  1516. +CONFIG_SQUASHFS_XZ=y
  1517. +CONFIG_F2FS_FS=y
  1518. +CONFIG_NFS_FS=y
  1519. +CONFIG_NFS_V3_ACL=y
  1520. +CONFIG_NFS_V4=y
  1521. +CONFIG_ROOT_NFS=y
  1522. +CONFIG_NFS_FSCACHE=y
  1523. +CONFIG_NFSD=m
  1524. +CONFIG_NFSD_V3_ACL=y
  1525. +CONFIG_NFSD_V4=y
  1526. +CONFIG_CIFS=m
  1527. +CONFIG_CIFS_WEAK_PW_HASH=y
  1528. +CONFIG_CIFS_XATTR=y
  1529. +CONFIG_CIFS_POSIX=y
  1530. +CONFIG_9P_FS=m
  1531. +CONFIG_9P_FS_POSIX_ACL=y
  1532. +CONFIG_NLS_DEFAULT="utf8"
  1533. +CONFIG_NLS_CODEPAGE_437=y
  1534. +CONFIG_NLS_CODEPAGE_737=m
  1535. +CONFIG_NLS_CODEPAGE_775=m
  1536. +CONFIG_NLS_CODEPAGE_850=m
  1537. +CONFIG_NLS_CODEPAGE_852=m
  1538. +CONFIG_NLS_CODEPAGE_855=m
  1539. +CONFIG_NLS_CODEPAGE_857=m
  1540. +CONFIG_NLS_CODEPAGE_860=m
  1541. +CONFIG_NLS_CODEPAGE_861=m
  1542. +CONFIG_NLS_CODEPAGE_862=m
  1543. +CONFIG_NLS_CODEPAGE_863=m
  1544. +CONFIG_NLS_CODEPAGE_864=m
  1545. +CONFIG_NLS_CODEPAGE_865=m
  1546. +CONFIG_NLS_CODEPAGE_866=m
  1547. +CONFIG_NLS_CODEPAGE_869=m
  1548. +CONFIG_NLS_CODEPAGE_936=m
  1549. +CONFIG_NLS_CODEPAGE_950=m
  1550. +CONFIG_NLS_CODEPAGE_932=m
  1551. +CONFIG_NLS_CODEPAGE_949=m
  1552. +CONFIG_NLS_CODEPAGE_874=m
  1553. +CONFIG_NLS_ISO8859_8=m
  1554. +CONFIG_NLS_CODEPAGE_1250=m
  1555. +CONFIG_NLS_CODEPAGE_1251=m
  1556. +CONFIG_NLS_ASCII=y
  1557. +CONFIG_NLS_ISO8859_1=m
  1558. +CONFIG_NLS_ISO8859_2=m
  1559. +CONFIG_NLS_ISO8859_3=m
  1560. +CONFIG_NLS_ISO8859_4=m
  1561. +CONFIG_NLS_ISO8859_5=m
  1562. +CONFIG_NLS_ISO8859_6=m
  1563. +CONFIG_NLS_ISO8859_7=m
  1564. +CONFIG_NLS_ISO8859_9=m
  1565. +CONFIG_NLS_ISO8859_13=m
  1566. +CONFIG_NLS_ISO8859_14=m
  1567. +CONFIG_NLS_ISO8859_15=m
  1568. +CONFIG_NLS_KOI8_R=m
  1569. +CONFIG_NLS_KOI8_U=m
  1570. +CONFIG_DLM=m
  1571. +CONFIG_PRINTK_TIME=y
  1572. +CONFIG_BOOT_PRINTK_DELAY=y
  1573. +CONFIG_DEBUG_FS=y
  1574. +CONFIG_DEBUG_MEMORY_INIT=y
  1575. +CONFIG_DETECT_HUNG_TASK=y
  1576. +CONFIG_TIMER_STATS=y
  1577. +# CONFIG_DEBUG_PREEMPT is not set
  1578. +CONFIG_LATENCYTOP=y
  1579. +# CONFIG_KPROBE_EVENT is not set
  1580. +CONFIG_KGDB=y
  1581. +CONFIG_KGDB_KDB=y
  1582. +CONFIG_KDB_KEYBOARD=y
  1583. +CONFIG_STRICT_DEVMEM=y
  1584. +CONFIG_CRYPTO_USER=m
  1585. +CONFIG_CRYPTO_NULL=m
  1586. +CONFIG_CRYPTO_CRYPTD=m
  1587. +CONFIG_CRYPTO_CBC=y
  1588. +CONFIG_CRYPTO_XTS=m
  1589. +CONFIG_CRYPTO_XCBC=m
  1590. +CONFIG_CRYPTO_SHA1_ARM=m
  1591. +CONFIG_CRYPTO_SHA512=m
  1592. +CONFIG_CRYPTO_TGR192=m
  1593. +CONFIG_CRYPTO_WP512=m
  1594. +CONFIG_CRYPTO_AES_ARM=m
  1595. +CONFIG_CRYPTO_CAST5=m
  1596. +CONFIG_CRYPTO_DES=y
  1597. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  1598. +# CONFIG_CRYPTO_HW is not set
  1599. +CONFIG_CRC_ITU_T=y
  1600. +CONFIG_LIBCRC32C=y
  1601. diff -Nur linux-3.13.6/arch/arm/configs/bcmrpi_emergency_defconfig linux-raspberry-pi/arch/arm/configs/bcmrpi_emergency_defconfig
  1602. --- linux-3.13.6/arch/arm/configs/bcmrpi_emergency_defconfig 1970-01-01 01:00:00.000000000 +0100
  1603. +++ linux-raspberry-pi/arch/arm/configs/bcmrpi_emergency_defconfig 2014-03-11 16:51:54.000000000 +0100
  1604. @@ -0,0 +1,532 @@
  1605. +CONFIG_EXPERIMENTAL=y
  1606. +# CONFIG_LOCALVERSION_AUTO is not set
  1607. +CONFIG_SYSVIPC=y
  1608. +CONFIG_POSIX_MQUEUE=y
  1609. +CONFIG_BSD_PROCESS_ACCT=y
  1610. +CONFIG_BSD_PROCESS_ACCT_V3=y
  1611. +CONFIG_FHANDLE=y
  1612. +CONFIG_AUDIT=y
  1613. +CONFIG_IKCONFIG=y
  1614. +CONFIG_IKCONFIG_PROC=y
  1615. +CONFIG_BLK_DEV_INITRD=y
  1616. +CONFIG_INITRAMFS_SOURCE="../target_fs"
  1617. +CONFIG_CGROUP_FREEZER=y
  1618. +CONFIG_CGROUP_DEVICE=y
  1619. +CONFIG_CGROUP_CPUACCT=y
  1620. +CONFIG_RESOURCE_COUNTERS=y
  1621. +CONFIG_BLK_CGROUP=y
  1622. +CONFIG_NAMESPACES=y
  1623. +CONFIG_SCHED_AUTOGROUP=y
  1624. +CONFIG_EMBEDDED=y
  1625. +# CONFIG_COMPAT_BRK is not set
  1626. +CONFIG_SLAB=y
  1627. +CONFIG_PROFILING=y
  1628. +CONFIG_OPROFILE=m
  1629. +CONFIG_KPROBES=y
  1630. +CONFIG_MODULES=y
  1631. +CONFIG_MODULE_UNLOAD=y
  1632. +CONFIG_MODVERSIONS=y
  1633. +CONFIG_MODULE_SRCVERSION_ALL=y
  1634. +# CONFIG_BLK_DEV_BSG is not set
  1635. +CONFIG_BLK_DEV_THROTTLING=y
  1636. +CONFIG_CFQ_GROUP_IOSCHED=y
  1637. +CONFIG_ARCH_BCM2708=y
  1638. +CONFIG_NO_HZ=y
  1639. +CONFIG_HIGH_RES_TIMERS=y
  1640. +CONFIG_AEABI=y
  1641. +CONFIG_SECCOMP=y
  1642. +CONFIG_CC_STACKPROTECTOR=y
  1643. +CONFIG_ZBOOT_ROM_TEXT=0x0
  1644. +CONFIG_ZBOOT_ROM_BSS=0x0
  1645. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext3 rootwait"
  1646. +CONFIG_KEXEC=y
  1647. +CONFIG_CPU_IDLE=y
  1648. +CONFIG_VFP=y
  1649. +CONFIG_BINFMT_MISC=m
  1650. +CONFIG_NET=y
  1651. +CONFIG_PACKET=y
  1652. +CONFIG_UNIX=y
  1653. +CONFIG_XFRM_USER=y
  1654. +CONFIG_NET_KEY=m
  1655. +CONFIG_INET=y
  1656. +CONFIG_IP_MULTICAST=y
  1657. +CONFIG_IP_PNP=y
  1658. +CONFIG_IP_PNP_DHCP=y
  1659. +CONFIG_IP_PNP_RARP=y
  1660. +CONFIG_SYN_COOKIES=y
  1661. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  1662. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  1663. +# CONFIG_INET_XFRM_MODE_BEET is not set
  1664. +# CONFIG_INET_LRO is not set
  1665. +# CONFIG_INET_DIAG is not set
  1666. +# CONFIG_IPV6 is not set
  1667. +CONFIG_NET_PKTGEN=m
  1668. +CONFIG_IRDA=m
  1669. +CONFIG_IRLAN=m
  1670. +CONFIG_IRCOMM=m
  1671. +CONFIG_IRDA_ULTRA=y
  1672. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  1673. +CONFIG_IRDA_FAST_RR=y
  1674. +CONFIG_IRTTY_SIR=m
  1675. +CONFIG_KINGSUN_DONGLE=m
  1676. +CONFIG_KSDAZZLE_DONGLE=m
  1677. +CONFIG_KS959_DONGLE=m
  1678. +CONFIG_USB_IRDA=m
  1679. +CONFIG_SIGMATEL_FIR=m
  1680. +CONFIG_MCS_FIR=m
  1681. +CONFIG_BT=m
  1682. +CONFIG_BT_L2CAP=y
  1683. +CONFIG_BT_SCO=y
  1684. +CONFIG_BT_RFCOMM=m
  1685. +CONFIG_BT_RFCOMM_TTY=y
  1686. +CONFIG_BT_BNEP=m
  1687. +CONFIG_BT_BNEP_MC_FILTER=y
  1688. +CONFIG_BT_BNEP_PROTO_FILTER=y
  1689. +CONFIG_BT_HIDP=m
  1690. +CONFIG_BT_HCIBTUSB=m
  1691. +CONFIG_BT_HCIBCM203X=m
  1692. +CONFIG_BT_HCIBPA10X=m
  1693. +CONFIG_BT_HCIBFUSB=m
  1694. +CONFIG_BT_HCIVHCI=m
  1695. +CONFIG_BT_MRVL=m
  1696. +CONFIG_BT_MRVL_SDIO=m
  1697. +CONFIG_BT_ATH3K=m
  1698. +CONFIG_CFG80211=m
  1699. +CONFIG_MAC80211=m
  1700. +CONFIG_MAC80211_RC_PID=y
  1701. +CONFIG_MAC80211_MESH=y
  1702. +CONFIG_WIMAX=m
  1703. +CONFIG_NET_9P=m
  1704. +CONFIG_NFC=m
  1705. +CONFIG_NFC_PN533=m
  1706. +CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug"
  1707. +CONFIG_BLK_DEV_LOOP=y
  1708. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  1709. +CONFIG_BLK_DEV_NBD=m
  1710. +CONFIG_BLK_DEV_RAM=y
  1711. +CONFIG_CDROM_PKTCDVD=m
  1712. +CONFIG_MISC_DEVICES=y
  1713. +CONFIG_SCSI=y
  1714. +# CONFIG_SCSI_PROC_FS is not set
  1715. +CONFIG_BLK_DEV_SD=y
  1716. +CONFIG_BLK_DEV_SR=m
  1717. +CONFIG_SCSI_MULTI_LUN=y
  1718. +# CONFIG_SCSI_LOWLEVEL is not set
  1719. +CONFIG_MD=y
  1720. +CONFIG_NETDEVICES=y
  1721. +CONFIG_TUN=m
  1722. +CONFIG_PHYLIB=m
  1723. +CONFIG_MDIO_BITBANG=m
  1724. +CONFIG_NET_ETHERNET=y
  1725. +# CONFIG_NETDEV_1000 is not set
  1726. +# CONFIG_NETDEV_10000 is not set
  1727. +CONFIG_LIBERTAS_THINFIRM=m
  1728. +CONFIG_LIBERTAS_THINFIRM_USB=m
  1729. +CONFIG_AT76C50X_USB=m
  1730. +CONFIG_USB_ZD1201=m
  1731. +CONFIG_USB_NET_RNDIS_WLAN=m
  1732. +CONFIG_RTL8187=m
  1733. +CONFIG_MAC80211_HWSIM=m
  1734. +CONFIG_ATH_COMMON=m
  1735. +CONFIG_ATH9K=m
  1736. +CONFIG_ATH9K_HTC=m
  1737. +CONFIG_CARL9170=m
  1738. +CONFIG_B43=m
  1739. +CONFIG_B43LEGACY=m
  1740. +CONFIG_HOSTAP=m
  1741. +CONFIG_IWM=m
  1742. +CONFIG_LIBERTAS=m
  1743. +CONFIG_LIBERTAS_USB=m
  1744. +CONFIG_LIBERTAS_SDIO=m
  1745. +CONFIG_P54_COMMON=m
  1746. +CONFIG_P54_USB=m
  1747. +CONFIG_RT2X00=m
  1748. +CONFIG_RT2500USB=m
  1749. +CONFIG_RT73USB=m
  1750. +CONFIG_RT2800USB=m
  1751. +CONFIG_RT2800USB_RT53XX=y
  1752. +CONFIG_RTL8192CU=m
  1753. +CONFIG_WL1251=m
  1754. +CONFIG_WL12XX_MENU=m
  1755. +CONFIG_ZD1211RW=m
  1756. +CONFIG_MWIFIEX=m
  1757. +CONFIG_MWIFIEX_SDIO=m
  1758. +CONFIG_WIMAX_I2400M_USB=m
  1759. +CONFIG_USB_CATC=m
  1760. +CONFIG_USB_KAWETH=m
  1761. +CONFIG_USB_PEGASUS=m
  1762. +CONFIG_USB_RTL8150=m
  1763. +CONFIG_USB_USBNET=y
  1764. +CONFIG_USB_NET_AX8817X=m
  1765. +CONFIG_USB_NET_CDCETHER=m
  1766. +CONFIG_USB_NET_CDC_EEM=m
  1767. +CONFIG_USB_NET_DM9601=m
  1768. +CONFIG_USB_NET_SMSC75XX=m
  1769. +CONFIG_USB_NET_SMSC95XX=y
  1770. +CONFIG_USB_NET_GL620A=m
  1771. +CONFIG_USB_NET_NET1080=m
  1772. +CONFIG_USB_NET_PLUSB=m
  1773. +CONFIG_USB_NET_MCS7830=m
  1774. +CONFIG_USB_NET_CDC_SUBSET=m
  1775. +CONFIG_USB_ALI_M5632=y
  1776. +CONFIG_USB_AN2720=y
  1777. +CONFIG_USB_KC2190=y
  1778. +# CONFIG_USB_NET_ZAURUS is not set
  1779. +CONFIG_USB_NET_CX82310_ETH=m
  1780. +CONFIG_USB_NET_KALMIA=m
  1781. +CONFIG_USB_NET_INT51X1=m
  1782. +CONFIG_USB_IPHETH=m
  1783. +CONFIG_USB_SIERRA_NET=m
  1784. +CONFIG_USB_VL600=m
  1785. +CONFIG_PPP=m
  1786. +CONFIG_PPP_ASYNC=m
  1787. +CONFIG_PPP_SYNC_TTY=m
  1788. +CONFIG_PPP_DEFLATE=m
  1789. +CONFIG_PPP_BSDCOMP=m
  1790. +CONFIG_SLIP=m
  1791. +CONFIG_SLIP_COMPRESSED=y
  1792. +CONFIG_NETCONSOLE=m
  1793. +CONFIG_INPUT_POLLDEV=m
  1794. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1795. +CONFIG_INPUT_JOYDEV=m
  1796. +CONFIG_INPUT_EVDEV=m
  1797. +# CONFIG_INPUT_KEYBOARD is not set
  1798. +# CONFIG_INPUT_MOUSE is not set
  1799. +CONFIG_INPUT_MISC=y
  1800. +CONFIG_INPUT_AD714X=m
  1801. +CONFIG_INPUT_ATI_REMOTE=m
  1802. +CONFIG_INPUT_ATI_REMOTE2=m
  1803. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1804. +CONFIG_INPUT_POWERMATE=m
  1805. +CONFIG_INPUT_YEALINK=m
  1806. +CONFIG_INPUT_CM109=m
  1807. +CONFIG_INPUT_UINPUT=m
  1808. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1809. +CONFIG_INPUT_ADXL34X=m
  1810. +CONFIG_INPUT_CMA3000=m
  1811. +CONFIG_SERIO=m
  1812. +CONFIG_SERIO_RAW=m
  1813. +CONFIG_GAMEPORT=m
  1814. +CONFIG_GAMEPORT_NS558=m
  1815. +CONFIG_GAMEPORT_L4=m
  1816. +CONFIG_VT_HW_CONSOLE_BINDING=y
  1817. +# CONFIG_LEGACY_PTYS is not set
  1818. +# CONFIG_DEVKMEM is not set
  1819. +CONFIG_SERIAL_AMBA_PL011=y
  1820. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1821. +# CONFIG_HW_RANDOM is not set
  1822. +CONFIG_RAW_DRIVER=y
  1823. +CONFIG_GPIO_SYSFS=y
  1824. +# CONFIG_HWMON is not set
  1825. +CONFIG_WATCHDOG=y
  1826. +CONFIG_BCM2708_WDT=m
  1827. +# CONFIG_MFD_SUPPORT is not set
  1828. +CONFIG_FB=y
  1829. +CONFIG_FB_BCM2708=y
  1830. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1831. +CONFIG_LOGO=y
  1832. +# CONFIG_LOGO_LINUX_MONO is not set
  1833. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1834. +CONFIG_SOUND=y
  1835. +CONFIG_SND=m
  1836. +CONFIG_SND_SEQUENCER=m
  1837. +CONFIG_SND_SEQ_DUMMY=m
  1838. +CONFIG_SND_MIXER_OSS=m
  1839. +CONFIG_SND_PCM_OSS=m
  1840. +CONFIG_SND_SEQUENCER_OSS=y
  1841. +CONFIG_SND_HRTIMER=m
  1842. +CONFIG_SND_DUMMY=m
  1843. +CONFIG_SND_ALOOP=m
  1844. +CONFIG_SND_VIRMIDI=m
  1845. +CONFIG_SND_MTPAV=m
  1846. +CONFIG_SND_SERIAL_U16550=m
  1847. +CONFIG_SND_MPU401=m
  1848. +CONFIG_SND_BCM2835=m
  1849. +CONFIG_SND_USB_AUDIO=m
  1850. +CONFIG_SND_USB_UA101=m
  1851. +CONFIG_SND_USB_CAIAQ=m
  1852. +CONFIG_SND_USB_6FIRE=m
  1853. +CONFIG_SOUND_PRIME=m
  1854. +CONFIG_HID_PID=y
  1855. +CONFIG_USB_HIDDEV=y
  1856. +CONFIG_HID_A4TECH=m
  1857. +CONFIG_HID_ACRUX=m
  1858. +CONFIG_HID_APPLE=m
  1859. +CONFIG_HID_BELKIN=m
  1860. +CONFIG_HID_CHERRY=m
  1861. +CONFIG_HID_CHICONY=m
  1862. +CONFIG_HID_CYPRESS=m
  1863. +CONFIG_HID_DRAGONRISE=m
  1864. +CONFIG_HID_EMS_FF=m
  1865. +CONFIG_HID_ELECOM=m
  1866. +CONFIG_HID_EZKEY=m
  1867. +CONFIG_HID_HOLTEK=m
  1868. +CONFIG_HID_KEYTOUCH=m
  1869. +CONFIG_HID_KYE=m
  1870. +CONFIG_HID_UCLOGIC=m
  1871. +CONFIG_HID_WALTOP=m
  1872. +CONFIG_HID_GYRATION=m
  1873. +CONFIG_HID_TWINHAN=m
  1874. +CONFIG_HID_KENSINGTON=m
  1875. +CONFIG_HID_LCPOWER=m
  1876. +CONFIG_HID_LOGITECH=m
  1877. +CONFIG_HID_MAGICMOUSE=m
  1878. +CONFIG_HID_MICROSOFT=m
  1879. +CONFIG_HID_MONTEREY=m
  1880. +CONFIG_HID_MULTITOUCH=m
  1881. +CONFIG_HID_NTRIG=m
  1882. +CONFIG_HID_ORTEK=m
  1883. +CONFIG_HID_PANTHERLORD=m
  1884. +CONFIG_HID_PETALYNX=m
  1885. +CONFIG_HID_PICOLCD=m
  1886. +CONFIG_HID_QUANTA=m
  1887. +CONFIG_HID_ROCCAT=m
  1888. +CONFIG_HID_SAMSUNG=m
  1889. +CONFIG_HID_SONY=m
  1890. +CONFIG_HID_SPEEDLINK=m
  1891. +CONFIG_HID_SUNPLUS=m
  1892. +CONFIG_HID_GREENASIA=m
  1893. +CONFIG_HID_SMARTJOYPLUS=m
  1894. +CONFIG_HID_TOPSEED=m
  1895. +CONFIG_HID_THRUSTMASTER=m
  1896. +CONFIG_HID_WACOM=m
  1897. +CONFIG_HID_WIIMOTE=m
  1898. +CONFIG_HID_ZEROPLUS=m
  1899. +CONFIG_HID_ZYDACRON=m
  1900. +CONFIG_USB=y
  1901. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1902. +CONFIG_USB_MON=m
  1903. +CONFIG_USB_DWCOTG=y
  1904. +CONFIG_USB_STORAGE=y
  1905. +CONFIG_USB_STORAGE_REALTEK=m
  1906. +CONFIG_USB_STORAGE_DATAFAB=m
  1907. +CONFIG_USB_STORAGE_FREECOM=m
  1908. +CONFIG_USB_STORAGE_ISD200=m
  1909. +CONFIG_USB_STORAGE_USBAT=m
  1910. +CONFIG_USB_STORAGE_SDDR09=m
  1911. +CONFIG_USB_STORAGE_SDDR55=m
  1912. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1913. +CONFIG_USB_STORAGE_ALAUDA=m
  1914. +CONFIG_USB_STORAGE_ONETOUCH=m
  1915. +CONFIG_USB_STORAGE_KARMA=m
  1916. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1917. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1918. +CONFIG_USB_UAS=y
  1919. +CONFIG_USB_LIBUSUAL=y
  1920. +CONFIG_USB_MDC800=m
  1921. +CONFIG_USB_MICROTEK=m
  1922. +CONFIG_USB_SERIAL=m
  1923. +CONFIG_USB_SERIAL_GENERIC=y
  1924. +CONFIG_USB_SERIAL_AIRCABLE=m
  1925. +CONFIG_USB_SERIAL_ARK3116=m
  1926. +CONFIG_USB_SERIAL_BELKIN=m
  1927. +CONFIG_USB_SERIAL_CH341=m
  1928. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1929. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1930. +CONFIG_USB_SERIAL_CP210X=m
  1931. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1932. +CONFIG_USB_SERIAL_EMPEG=m
  1933. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1934. +CONFIG_USB_SERIAL_FUNSOFT=m
  1935. +CONFIG_USB_SERIAL_VISOR=m
  1936. +CONFIG_USB_SERIAL_IPAQ=m
  1937. +CONFIG_USB_SERIAL_IR=m
  1938. +CONFIG_USB_SERIAL_EDGEPORT=m
  1939. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1940. +CONFIG_USB_SERIAL_GARMIN=m
  1941. +CONFIG_USB_SERIAL_IPW=m
  1942. +CONFIG_USB_SERIAL_IUU=m
  1943. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1944. +CONFIG_USB_SERIAL_KEYSPAN=m
  1945. +CONFIG_USB_SERIAL_KLSI=m
  1946. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1947. +CONFIG_USB_SERIAL_MCT_U232=m
  1948. +CONFIG_USB_SERIAL_MOS7720=m
  1949. +CONFIG_USB_SERIAL_MOS7840=m
  1950. +CONFIG_USB_SERIAL_MOTOROLA=m
  1951. +CONFIG_USB_SERIAL_NAVMAN=m
  1952. +CONFIG_USB_SERIAL_PL2303=m
  1953. +CONFIG_USB_SERIAL_OTI6858=m
  1954. +CONFIG_USB_SERIAL_QCAUX=m
  1955. +CONFIG_USB_SERIAL_QUALCOMM=m
  1956. +CONFIG_USB_SERIAL_SPCP8X5=m
  1957. +CONFIG_USB_SERIAL_HP4X=m
  1958. +CONFIG_USB_SERIAL_SAFE=m
  1959. +CONFIG_USB_SERIAL_SIEMENS_MPI=m
  1960. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1961. +CONFIG_USB_SERIAL_SYMBOL=m
  1962. +CONFIG_USB_SERIAL_TI=m
  1963. +CONFIG_USB_SERIAL_CYBERJACK=m
  1964. +CONFIG_USB_SERIAL_XIRCOM=m
  1965. +CONFIG_USB_SERIAL_OPTION=m
  1966. +CONFIG_USB_SERIAL_OMNINET=m
  1967. +CONFIG_USB_SERIAL_OPTICON=m
  1968. +CONFIG_USB_SERIAL_VIVOPAY_SERIAL=m
  1969. +CONFIG_USB_SERIAL_ZIO=m
  1970. +CONFIG_USB_SERIAL_SSU100=m
  1971. +CONFIG_USB_SERIAL_DEBUG=m
  1972. +CONFIG_USB_EMI62=m
  1973. +CONFIG_USB_EMI26=m
  1974. +CONFIG_USB_ADUTUX=m
  1975. +CONFIG_USB_SEVSEG=m
  1976. +CONFIG_USB_RIO500=m
  1977. +CONFIG_USB_LEGOTOWER=m
  1978. +CONFIG_USB_LCD=m
  1979. +CONFIG_USB_LED=m
  1980. +CONFIG_USB_CYPRESS_CY7C63=m
  1981. +CONFIG_USB_CYTHERM=m
  1982. +CONFIG_USB_IDMOUSE=m
  1983. +CONFIG_USB_FTDI_ELAN=m
  1984. +CONFIG_USB_APPLEDISPLAY=m
  1985. +CONFIG_USB_LD=m
  1986. +CONFIG_USB_TRANCEVIBRATOR=m
  1987. +CONFIG_USB_IOWARRIOR=m
  1988. +CONFIG_USB_TEST=m
  1989. +CONFIG_USB_ISIGHTFW=m
  1990. +CONFIG_USB_YUREX=m
  1991. +CONFIG_MMC=y
  1992. +CONFIG_MMC_SDHCI=y
  1993. +CONFIG_MMC_SDHCI_PLTFM=y
  1994. +CONFIG_MMC_SDHCI_BCM2708=y
  1995. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1996. +CONFIG_LEDS_GPIO=y
  1997. +CONFIG_LEDS_TRIGGER_TIMER=m
  1998. +CONFIG_LEDS_TRIGGER_HEARTBEAT=m
  1999. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=m
  2000. +CONFIG_UIO=m
  2001. +CONFIG_UIO_PDRV=m
  2002. +CONFIG_UIO_PDRV_GENIRQ=m
  2003. +# CONFIG_IOMMU_SUPPORT is not set
  2004. +CONFIG_EXT4_FS=y
  2005. +CONFIG_EXT4_FS_POSIX_ACL=y
  2006. +CONFIG_EXT4_FS_SECURITY=y
  2007. +CONFIG_REISERFS_FS=m
  2008. +CONFIG_REISERFS_FS_XATTR=y
  2009. +CONFIG_REISERFS_FS_POSIX_ACL=y
  2010. +CONFIG_REISERFS_FS_SECURITY=y
  2011. +CONFIG_JFS_FS=m
  2012. +CONFIG_JFS_POSIX_ACL=y
  2013. +CONFIG_JFS_SECURITY=y
  2014. +CONFIG_JFS_STATISTICS=y
  2015. +CONFIG_XFS_FS=m
  2016. +CONFIG_XFS_QUOTA=y
  2017. +CONFIG_XFS_POSIX_ACL=y
  2018. +CONFIG_XFS_RT=y
  2019. +CONFIG_GFS2_FS=m
  2020. +CONFIG_OCFS2_FS=m
  2021. +CONFIG_BTRFS_FS=m
  2022. +CONFIG_BTRFS_FS_POSIX_ACL=y
  2023. +CONFIG_NILFS2_FS=m
  2024. +CONFIG_FANOTIFY=y
  2025. +CONFIG_AUTOFS4_FS=y
  2026. +CONFIG_FUSE_FS=m
  2027. +CONFIG_CUSE=m
  2028. +CONFIG_FSCACHE=y
  2029. +CONFIG_FSCACHE_STATS=y
  2030. +CONFIG_FSCACHE_HISTOGRAM=y
  2031. +CONFIG_CACHEFILES=y
  2032. +CONFIG_ISO9660_FS=m
  2033. +CONFIG_JOLIET=y
  2034. +CONFIG_ZISOFS=y
  2035. +CONFIG_UDF_FS=m
  2036. +CONFIG_MSDOS_FS=y
  2037. +CONFIG_VFAT_FS=y
  2038. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  2039. +CONFIG_NTFS_FS=m
  2040. +CONFIG_TMPFS=y
  2041. +CONFIG_TMPFS_POSIX_ACL=y
  2042. +CONFIG_CONFIGFS_FS=y
  2043. +CONFIG_SQUASHFS=m
  2044. +CONFIG_SQUASHFS_XATTR=y
  2045. +CONFIG_SQUASHFS_LZO=y
  2046. +CONFIG_SQUASHFS_XZ=y
  2047. +CONFIG_NFS_FS=y
  2048. +CONFIG_NFS_V3=y
  2049. +CONFIG_NFS_V3_ACL=y
  2050. +CONFIG_NFS_V4=y
  2051. +CONFIG_ROOT_NFS=y
  2052. +CONFIG_NFS_FSCACHE=y
  2053. +CONFIG_CIFS=m
  2054. +CONFIG_CIFS_WEAK_PW_HASH=y
  2055. +CONFIG_CIFS_XATTR=y
  2056. +CONFIG_CIFS_POSIX=y
  2057. +CONFIG_9P_FS=m
  2058. +CONFIG_9P_FS_POSIX_ACL=y
  2059. +CONFIG_PARTITION_ADVANCED=y
  2060. +CONFIG_MAC_PARTITION=y
  2061. +CONFIG_EFI_PARTITION=y
  2062. +CONFIG_NLS_DEFAULT="utf8"
  2063. +CONFIG_NLS_CODEPAGE_437=y
  2064. +CONFIG_NLS_CODEPAGE_737=m
  2065. +CONFIG_NLS_CODEPAGE_775=m
  2066. +CONFIG_NLS_CODEPAGE_850=m
  2067. +CONFIG_NLS_CODEPAGE_852=m
  2068. +CONFIG_NLS_CODEPAGE_855=m
  2069. +CONFIG_NLS_CODEPAGE_857=m
  2070. +CONFIG_NLS_CODEPAGE_860=m
  2071. +CONFIG_NLS_CODEPAGE_861=m
  2072. +CONFIG_NLS_CODEPAGE_862=m
  2073. +CONFIG_NLS_CODEPAGE_863=m
  2074. +CONFIG_NLS_CODEPAGE_864=m
  2075. +CONFIG_NLS_CODEPAGE_865=m
  2076. +CONFIG_NLS_CODEPAGE_866=m
  2077. +CONFIG_NLS_CODEPAGE_869=m
  2078. +CONFIG_NLS_CODEPAGE_936=m
  2079. +CONFIG_NLS_CODEPAGE_950=m
  2080. +CONFIG_NLS_CODEPAGE_932=m
  2081. +CONFIG_NLS_CODEPAGE_949=m
  2082. +CONFIG_NLS_CODEPAGE_874=m
  2083. +CONFIG_NLS_ISO8859_8=m
  2084. +CONFIG_NLS_CODEPAGE_1250=m
  2085. +CONFIG_NLS_CODEPAGE_1251=m
  2086. +CONFIG_NLS_ASCII=y
  2087. +CONFIG_NLS_ISO8859_1=m
  2088. +CONFIG_NLS_ISO8859_2=m
  2089. +CONFIG_NLS_ISO8859_3=m
  2090. +CONFIG_NLS_ISO8859_4=m
  2091. +CONFIG_NLS_ISO8859_5=m
  2092. +CONFIG_NLS_ISO8859_6=m
  2093. +CONFIG_NLS_ISO8859_7=m
  2094. +CONFIG_NLS_ISO8859_9=m
  2095. +CONFIG_NLS_ISO8859_13=m
  2096. +CONFIG_NLS_ISO8859_14=m
  2097. +CONFIG_NLS_ISO8859_15=m
  2098. +CONFIG_NLS_KOI8_R=m
  2099. +CONFIG_NLS_KOI8_U=m
  2100. +CONFIG_NLS_UTF8=m
  2101. +CONFIG_PRINTK_TIME=y
  2102. +CONFIG_DETECT_HUNG_TASK=y
  2103. +CONFIG_TIMER_STATS=y
  2104. +CONFIG_DEBUG_STACK_USAGE=y
  2105. +CONFIG_DEBUG_INFO=y
  2106. +CONFIG_DEBUG_MEMORY_INIT=y
  2107. +CONFIG_BOOT_PRINTK_DELAY=y
  2108. +CONFIG_LATENCYTOP=y
  2109. +CONFIG_SYSCTL_SYSCALL_CHECK=y
  2110. +CONFIG_IRQSOFF_TRACER=y
  2111. +CONFIG_SCHED_TRACER=y
  2112. +CONFIG_STACK_TRACER=y
  2113. +CONFIG_BLK_DEV_IO_TRACE=y
  2114. +CONFIG_FUNCTION_PROFILER=y
  2115. +CONFIG_KGDB=y
  2116. +CONFIG_KGDB_KDB=y
  2117. +CONFIG_KDB_KEYBOARD=y
  2118. +CONFIG_STRICT_DEVMEM=y
  2119. +CONFIG_CRYPTO_AUTHENC=m
  2120. +CONFIG_CRYPTO_SEQIV=m
  2121. +CONFIG_CRYPTO_CBC=y
  2122. +CONFIG_CRYPTO_HMAC=y
  2123. +CONFIG_CRYPTO_XCBC=m
  2124. +CONFIG_CRYPTO_MD5=y
  2125. +CONFIG_CRYPTO_SHA1=y
  2126. +CONFIG_CRYPTO_SHA256=m
  2127. +CONFIG_CRYPTO_SHA512=m
  2128. +CONFIG_CRYPTO_TGR192=m
  2129. +CONFIG_CRYPTO_WP512=m
  2130. +CONFIG_CRYPTO_CAST5=m
  2131. +CONFIG_CRYPTO_DES=y
  2132. +CONFIG_CRYPTO_DEFLATE=m
  2133. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  2134. +# CONFIG_CRYPTO_HW is not set
  2135. +CONFIG_CRC_ITU_T=y
  2136. +CONFIG_LIBCRC32C=y
  2137. diff -Nur linux-3.13.6/arch/arm/configs/bcmrpi_quick_defconfig linux-raspberry-pi/arch/arm/configs/bcmrpi_quick_defconfig
  2138. --- linux-3.13.6/arch/arm/configs/bcmrpi_quick_defconfig 1970-01-01 01:00:00.000000000 +0100
  2139. +++ linux-raspberry-pi/arch/arm/configs/bcmrpi_quick_defconfig 2014-03-11 16:51:54.000000000 +0100
  2140. @@ -0,0 +1,197 @@
  2141. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  2142. +CONFIG_LOCALVERSION="-quick"
  2143. +# CONFIG_LOCALVERSION_AUTO is not set
  2144. +# CONFIG_SWAP is not set
  2145. +CONFIG_SYSVIPC=y
  2146. +CONFIG_POSIX_MQUEUE=y
  2147. +CONFIG_NO_HZ=y
  2148. +CONFIG_HIGH_RES_TIMERS=y
  2149. +CONFIG_IKCONFIG=y
  2150. +CONFIG_IKCONFIG_PROC=y
  2151. +CONFIG_KALLSYMS_ALL=y
  2152. +CONFIG_EMBEDDED=y
  2153. +CONFIG_PERF_EVENTS=y
  2154. +# CONFIG_COMPAT_BRK is not set
  2155. +CONFIG_SLAB=y
  2156. +CONFIG_MODULES=y
  2157. +CONFIG_MODULE_UNLOAD=y
  2158. +CONFIG_MODVERSIONS=y
  2159. +CONFIG_MODULE_SRCVERSION_ALL=y
  2160. +# CONFIG_BLK_DEV_BSG is not set
  2161. +CONFIG_ARCH_BCM2708=y
  2162. +CONFIG_PREEMPT=y
  2163. +CONFIG_AEABI=y
  2164. +CONFIG_UACCESS_WITH_MEMCPY=y
  2165. +CONFIG_ZBOOT_ROM_TEXT=0x0
  2166. +CONFIG_ZBOOT_ROM_BSS=0x0
  2167. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  2168. +CONFIG_CPU_FREQ=y
  2169. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  2170. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  2171. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  2172. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  2173. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  2174. +CONFIG_CPU_IDLE=y
  2175. +CONFIG_VFP=y
  2176. +CONFIG_BINFMT_MISC=y
  2177. +CONFIG_NET=y
  2178. +CONFIG_PACKET=y
  2179. +CONFIG_UNIX=y
  2180. +CONFIG_INET=y
  2181. +CONFIG_IP_MULTICAST=y
  2182. +CONFIG_IP_PNP=y
  2183. +CONFIG_IP_PNP_DHCP=y
  2184. +CONFIG_IP_PNP_RARP=y
  2185. +CONFIG_SYN_COOKIES=y
  2186. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  2187. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  2188. +# CONFIG_INET_XFRM_MODE_BEET is not set
  2189. +# CONFIG_INET_LRO is not set
  2190. +# CONFIG_INET_DIAG is not set
  2191. +# CONFIG_IPV6 is not set
  2192. +# CONFIG_WIRELESS is not set
  2193. +CONFIG_DEVTMPFS=y
  2194. +CONFIG_DEVTMPFS_MOUNT=y
  2195. +CONFIG_BLK_DEV_LOOP=y
  2196. +CONFIG_BLK_DEV_RAM=y
  2197. +CONFIG_SCSI=y
  2198. +# CONFIG_SCSI_PROC_FS is not set
  2199. +# CONFIG_SCSI_LOWLEVEL is not set
  2200. +CONFIG_NETDEVICES=y
  2201. +# CONFIG_NET_VENDOR_BROADCOM is not set
  2202. +# CONFIG_NET_VENDOR_CIRRUS is not set
  2203. +# CONFIG_NET_VENDOR_FARADAY is not set
  2204. +# CONFIG_NET_VENDOR_INTEL is not set
  2205. +# CONFIG_NET_VENDOR_MARVELL is not set
  2206. +# CONFIG_NET_VENDOR_MICREL is not set
  2207. +# CONFIG_NET_VENDOR_NATSEMI is not set
  2208. +# CONFIG_NET_VENDOR_SEEQ is not set
  2209. +# CONFIG_NET_VENDOR_STMICRO is not set
  2210. +# CONFIG_NET_VENDOR_WIZNET is not set
  2211. +CONFIG_USB_USBNET=y
  2212. +# CONFIG_USB_NET_AX8817X is not set
  2213. +# CONFIG_USB_NET_CDCETHER is not set
  2214. +# CONFIG_USB_NET_CDC_NCM is not set
  2215. +CONFIG_USB_NET_SMSC95XX=y
  2216. +# CONFIG_USB_NET_NET1080 is not set
  2217. +# CONFIG_USB_NET_CDC_SUBSET is not set
  2218. +# CONFIG_USB_NET_ZAURUS is not set
  2219. +# CONFIG_WLAN is not set
  2220. +# CONFIG_INPUT_MOUSEDEV is not set
  2221. +CONFIG_INPUT_EVDEV=y
  2222. +# CONFIG_INPUT_KEYBOARD is not set
  2223. +# CONFIG_INPUT_MOUSE is not set
  2224. +# CONFIG_SERIO is not set
  2225. +CONFIG_VT_HW_CONSOLE_BINDING=y
  2226. +# CONFIG_LEGACY_PTYS is not set
  2227. +# CONFIG_DEVKMEM is not set
  2228. +CONFIG_SERIAL_AMBA_PL011=y
  2229. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  2230. +CONFIG_TTY_PRINTK=y
  2231. +CONFIG_HW_RANDOM=y
  2232. +CONFIG_HW_RANDOM_BCM2708=y
  2233. +CONFIG_RAW_DRIVER=y
  2234. +CONFIG_THERMAL=y
  2235. +CONFIG_THERMAL_BCM2835=y
  2236. +CONFIG_WATCHDOG=y
  2237. +CONFIG_BCM2708_WDT=y
  2238. +CONFIG_REGULATOR=y
  2239. +CONFIG_REGULATOR_DEBUG=y
  2240. +CONFIG_REGULATOR_FIXED_VOLTAGE=y
  2241. +CONFIG_REGULATOR_VIRTUAL_CONSUMER=y
  2242. +CONFIG_REGULATOR_USERSPACE_CONSUMER=y
  2243. +CONFIG_FB=y
  2244. +CONFIG_FB_BCM2708=y
  2245. +CONFIG_FRAMEBUFFER_CONSOLE=y
  2246. +CONFIG_LOGO=y
  2247. +# CONFIG_LOGO_LINUX_MONO is not set
  2248. +# CONFIG_LOGO_LINUX_VGA16 is not set
  2249. +CONFIG_SOUND=y
  2250. +CONFIG_SND=y
  2251. +CONFIG_SND_BCM2835=y
  2252. +# CONFIG_SND_USB is not set
  2253. +CONFIG_USB=y
  2254. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  2255. +CONFIG_USB_DWCOTG=y
  2256. +CONFIG_MMC=y
  2257. +CONFIG_MMC_SDHCI=y
  2258. +CONFIG_MMC_SDHCI_PLTFM=y
  2259. +CONFIG_MMC_SDHCI_BCM2708=y
  2260. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  2261. +CONFIG_NEW_LEDS=y
  2262. +CONFIG_LEDS_CLASS=y
  2263. +CONFIG_LEDS_TRIGGERS=y
  2264. +# CONFIG_IOMMU_SUPPORT is not set
  2265. +CONFIG_EXT4_FS=y
  2266. +CONFIG_EXT4_FS_POSIX_ACL=y
  2267. +CONFIG_EXT4_FS_SECURITY=y
  2268. +CONFIG_AUTOFS4_FS=y
  2269. +CONFIG_FSCACHE=y
  2270. +CONFIG_CACHEFILES=y
  2271. +CONFIG_MSDOS_FS=y
  2272. +CONFIG_VFAT_FS=y
  2273. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  2274. +CONFIG_TMPFS=y
  2275. +CONFIG_TMPFS_POSIX_ACL=y
  2276. +CONFIG_CONFIGFS_FS=y
  2277. +# CONFIG_MISC_FILESYSTEMS is not set
  2278. +CONFIG_NFS_FS=y
  2279. +CONFIG_NFS_V3_ACL=y
  2280. +CONFIG_NFS_V4=y
  2281. +CONFIG_ROOT_NFS=y
  2282. +CONFIG_NFS_FSCACHE=y
  2283. +CONFIG_NLS_DEFAULT="utf8"
  2284. +CONFIG_NLS_CODEPAGE_437=y
  2285. +CONFIG_NLS_CODEPAGE_737=y
  2286. +CONFIG_NLS_CODEPAGE_775=y
  2287. +CONFIG_NLS_CODEPAGE_850=y
  2288. +CONFIG_NLS_CODEPAGE_852=y
  2289. +CONFIG_NLS_CODEPAGE_855=y
  2290. +CONFIG_NLS_CODEPAGE_857=y
  2291. +CONFIG_NLS_CODEPAGE_860=y
  2292. +CONFIG_NLS_CODEPAGE_861=y
  2293. +CONFIG_NLS_CODEPAGE_862=y
  2294. +CONFIG_NLS_CODEPAGE_863=y
  2295. +CONFIG_NLS_CODEPAGE_864=y
  2296. +CONFIG_NLS_CODEPAGE_865=y
  2297. +CONFIG_NLS_CODEPAGE_866=y
  2298. +CONFIG_NLS_CODEPAGE_869=y
  2299. +CONFIG_NLS_CODEPAGE_936=y
  2300. +CONFIG_NLS_CODEPAGE_950=y
  2301. +CONFIG_NLS_CODEPAGE_932=y
  2302. +CONFIG_NLS_CODEPAGE_949=y
  2303. +CONFIG_NLS_CODEPAGE_874=y
  2304. +CONFIG_NLS_ISO8859_8=y
  2305. +CONFIG_NLS_CODEPAGE_1250=y
  2306. +CONFIG_NLS_CODEPAGE_1251=y
  2307. +CONFIG_NLS_ASCII=y
  2308. +CONFIG_NLS_ISO8859_1=y
  2309. +CONFIG_NLS_ISO8859_2=y
  2310. +CONFIG_NLS_ISO8859_3=y
  2311. +CONFIG_NLS_ISO8859_4=y
  2312. +CONFIG_NLS_ISO8859_5=y
  2313. +CONFIG_NLS_ISO8859_6=y
  2314. +CONFIG_NLS_ISO8859_7=y
  2315. +CONFIG_NLS_ISO8859_9=y
  2316. +CONFIG_NLS_ISO8859_13=y
  2317. +CONFIG_NLS_ISO8859_14=y
  2318. +CONFIG_NLS_ISO8859_15=y
  2319. +CONFIG_NLS_UTF8=y
  2320. +CONFIG_PRINTK_TIME=y
  2321. +CONFIG_DEBUG_FS=y
  2322. +CONFIG_DETECT_HUNG_TASK=y
  2323. +# CONFIG_DEBUG_PREEMPT is not set
  2324. +# CONFIG_DEBUG_BUGVERBOSE is not set
  2325. +# CONFIG_FTRACE is not set
  2326. +CONFIG_KGDB=y
  2327. +CONFIG_KGDB_KDB=y
  2328. +# CONFIG_ARM_UNWIND is not set
  2329. +CONFIG_CRYPTO_CBC=y
  2330. +CONFIG_CRYPTO_HMAC=y
  2331. +CONFIG_CRYPTO_MD5=y
  2332. +CONFIG_CRYPTO_SHA1=y
  2333. +CONFIG_CRYPTO_DES=y
  2334. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  2335. +# CONFIG_CRYPTO_HW is not set
  2336. +CONFIG_CRC_ITU_T=y
  2337. +CONFIG_LIBCRC32C=y
  2338. diff -Nur linux-3.13.6/arch/arm/include/asm/fiq.h linux-raspberry-pi/arch/arm/include/asm/fiq.h
  2339. --- linux-3.13.6/arch/arm/include/asm/fiq.h 2014-03-07 07:07:02.000000000 +0100
  2340. +++ linux-raspberry-pi/arch/arm/include/asm/fiq.h 2014-03-11 16:51:54.000000000 +0100
  2341. @@ -42,6 +42,7 @@
  2342. /* helpers defined in fiqasm.S: */
  2343. extern void __set_fiq_regs(unsigned long const *regs);
  2344. extern void __get_fiq_regs(unsigned long *regs);
  2345. +extern void __FIQ_Branch(unsigned long *regs);
  2346. static inline void set_fiq_regs(struct pt_regs const *regs)
  2347. {
  2348. diff -Nur linux-3.13.6/arch/arm/Kconfig linux-raspberry-pi/arch/arm/Kconfig
  2349. --- linux-3.13.6/arch/arm/Kconfig 2014-03-07 07:07:02.000000000 +0100
  2350. +++ linux-raspberry-pi/arch/arm/Kconfig 2014-03-11 16:54:55.000000000 +0100
  2351. @@ -373,6 +373,24 @@
  2352. This enables support for systems based on Atmel
  2353. AT91RM9200 and AT91SAM9* processors.
  2354. +config ARCH_BCM2708
  2355. + bool "Broadcom BCM2708 family"
  2356. + select CPU_V6
  2357. + select ARM_AMBA
  2358. + select HAVE_CLK
  2359. + select HAVE_SCHED_CLOCK
  2360. + select NEED_MACH_GPIO_H
  2361. + select NEED_MACH_MEMORY_H
  2362. + select CLKDEV_LOOKUP
  2363. + select ARCH_HAS_CPUFREQ
  2364. + select GENERIC_CLOCKEVENTS
  2365. + select ARM_ERRATA_411920
  2366. + select MACH_BCM2708
  2367. + select VC4
  2368. + select FIQ
  2369. + help
  2370. + This enables support for Broadcom BCM2708 boards.
  2371. +
  2372. config ARCH_CLPS711X
  2373. bool "Cirrus Logic CLPS711x/EP721x/EP731x-based"
  2374. select ARCH_REQUIRE_GPIOLIB
  2375. @@ -1020,6 +1038,7 @@
  2376. source "arch/arm/mach-vt8500/Kconfig"
  2377. source "arch/arm/mach-w90x900/Kconfig"
  2378. +source "arch/arm/mach-bcm2708/Kconfig"
  2379. source "arch/arm/mach-zynq/Kconfig"
  2380. diff -Nur linux-3.13.6/arch/arm/Kconfig.debug linux-raspberry-pi/arch/arm/Kconfig.debug
  2381. --- linux-3.13.6/arch/arm/Kconfig.debug 2014-03-07 07:07:02.000000000 +0100
  2382. +++ linux-raspberry-pi/arch/arm/Kconfig.debug 2014-03-11 16:54:55.000000000 +0100
  2383. @@ -882,6 +882,14 @@
  2384. options; the platform specific options are deprecated
  2385. and will be soon removed.
  2386. + config DEBUG_BCM2708_UART0
  2387. + bool "Broadcom BCM2708 UART0 (PL011)"
  2388. + depends on MACH_BCM2708
  2389. + help
  2390. + Say Y here if you want the debug print routines to direct
  2391. + their output to UART 0. The port must have been initialised
  2392. + by the boot-loader before use.
  2393. +
  2394. endchoice
  2395. config DEBUG_EXYNOS_UART
  2396. diff -Nur linux-3.13.6/arch/arm/kernel/fiqasm.S linux-raspberry-pi/arch/arm/kernel/fiqasm.S
  2397. --- linux-3.13.6/arch/arm/kernel/fiqasm.S 2014-03-07 07:07:02.000000000 +0100
  2398. +++ linux-raspberry-pi/arch/arm/kernel/fiqasm.S 2014-03-11 16:51:54.000000000 +0100
  2399. @@ -25,6 +25,9 @@
  2400. ENTRY(__set_fiq_regs)
  2401. mov r2, #PSR_I_BIT | PSR_F_BIT | FIQ_MODE
  2402. mrs r1, cpsr
  2403. +@@@@@@@@@@@@@@@ hack: enable the fiq here to keep usb driver happy
  2404. + and r1, #~PSR_F_BIT
  2405. +@@@@@@@@@@@@@@@ endhack: (need to find better place for this to happen)
  2406. msr cpsr_c, r2 @ select FIQ mode
  2407. mov r0, r0 @ avoid hazard prior to ARMv4
  2408. ldmia r0!, {r8 - r12}
  2409. @@ -47,3 +50,7 @@
  2410. mov r0, r0 @ avoid hazard prior to ARMv4
  2411. mov pc, lr
  2412. ENDPROC(__get_fiq_regs)
  2413. +
  2414. +ENTRY(__FIQ_Branch)
  2415. + mov pc, r8
  2416. +ENDPROC(__FIQ_Branch)
  2417. diff -Nur linux-3.13.6/arch/arm/kernel/fiq.c linux-raspberry-pi/arch/arm/kernel/fiq.c
  2418. --- linux-3.13.6/arch/arm/kernel/fiq.c 2014-03-07 07:07:02.000000000 +0100
  2419. +++ linux-raspberry-pi/arch/arm/kernel/fiq.c 2014-03-11 16:51:54.000000000 +0100
  2420. @@ -142,6 +142,7 @@
  2421. EXPORT_SYMBOL(set_fiq_handler);
  2422. EXPORT_SYMBOL(__set_fiq_regs); /* defined in fiqasm.S */
  2423. EXPORT_SYMBOL(__get_fiq_regs); /* defined in fiqasm.S */
  2424. +EXPORT_SYMBOL(__FIQ_Branch); /* defined in fiqasm.S */
  2425. EXPORT_SYMBOL(claim_fiq);
  2426. EXPORT_SYMBOL(release_fiq);
  2427. EXPORT_SYMBOL(enable_fiq);
  2428. diff -Nur linux-3.13.6/arch/arm/kernel/process.c linux-raspberry-pi/arch/arm/kernel/process.c
  2429. --- linux-3.13.6/arch/arm/kernel/process.c 2014-03-07 07:07:02.000000000 +0100
  2430. +++ linux-raspberry-pi/arch/arm/kernel/process.c 2014-03-11 16:54:55.000000000 +0100
  2431. @@ -176,6 +176,16 @@
  2432. default_idle();
  2433. }
  2434. +char bcm2708_reboot_mode = 'h';
  2435. +
  2436. +int __init reboot_setup(char *str)
  2437. +{
  2438. + bcm2708_reboot_mode = str[0];
  2439. + return 1;
  2440. +}
  2441. +
  2442. +__setup("reboot=", reboot_setup);
  2443. +
  2444. /*
  2445. * Called by kexec, immediately prior to machine_kexec().
  2446. *
  2447. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/armctrl.c linux-raspberry-pi/arch/arm/mach-bcm2708/armctrl.c
  2448. --- linux-3.13.6/arch/arm/mach-bcm2708/armctrl.c 1970-01-01 01:00:00.000000000 +0100
  2449. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/armctrl.c 2014-03-11 16:51:54.000000000 +0100
  2450. @@ -0,0 +1,219 @@
  2451. +/*
  2452. + * linux/arch/arm/mach-bcm2708/armctrl.c
  2453. + *
  2454. + * Copyright (C) 2010 Broadcom
  2455. + *
  2456. + * This program is free software; you can redistribute it and/or modify
  2457. + * it under the terms of the GNU General Public License as published by
  2458. + * the Free Software Foundation; either version 2 of the License, or
  2459. + * (at your option) any later version.
  2460. + *
  2461. + * This program is distributed in the hope that it will be useful,
  2462. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2463. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2464. + * GNU General Public License for more details.
  2465. + *
  2466. + * You should have received a copy of the GNU General Public License
  2467. + * along with this program; if not, write to the Free Software
  2468. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2469. + */
  2470. +#include <linux/init.h>
  2471. +#include <linux/list.h>
  2472. +#include <linux/io.h>
  2473. +#include <linux/version.h>
  2474. +#include <linux/syscore_ops.h>
  2475. +#include <linux/interrupt.h>
  2476. +
  2477. +#include <asm/mach/irq.h>
  2478. +#include <mach/hardware.h>
  2479. +#include "armctrl.h"
  2480. +
  2481. +/* For support of kernels >= 3.0 assume only one VIC for now*/
  2482. +static unsigned int remap_irqs[(INTERRUPT_ARASANSDIO + 1) - INTERRUPT_JPEG] = {
  2483. + INTERRUPT_VC_JPEG,
  2484. + INTERRUPT_VC_USB,
  2485. + INTERRUPT_VC_3D,
  2486. + INTERRUPT_VC_DMA2,
  2487. + INTERRUPT_VC_DMA3,
  2488. + INTERRUPT_VC_I2C,
  2489. + INTERRUPT_VC_SPI,
  2490. + INTERRUPT_VC_I2SPCM,
  2491. + INTERRUPT_VC_SDIO,
  2492. + INTERRUPT_VC_UART,
  2493. + INTERRUPT_VC_ARASANSDIO
  2494. +};
  2495. +
  2496. +static void armctrl_mask_irq(struct irq_data *d)
  2497. +{
  2498. + static const unsigned int disables[4] = {
  2499. + ARM_IRQ_DIBL1,
  2500. + ARM_IRQ_DIBL2,
  2501. + ARM_IRQ_DIBL3,
  2502. + 0
  2503. + };
  2504. +
  2505. + if (d->irq >= FIQ_START) {
  2506. + writel(0, __io_address(ARM_IRQ_FAST));
  2507. + } else {
  2508. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  2509. + writel(1 << (data & 0x1f), __io_address(disables[(data >> 5) & 0x3]));
  2510. + }
  2511. +}
  2512. +
  2513. +static void armctrl_unmask_irq(struct irq_data *d)
  2514. +{
  2515. + static const unsigned int enables[4] = {
  2516. + ARM_IRQ_ENBL1,
  2517. + ARM_IRQ_ENBL2,
  2518. + ARM_IRQ_ENBL3,
  2519. + 0
  2520. + };
  2521. +
  2522. + if (d->irq >= FIQ_START) {
  2523. + unsigned int data =
  2524. + (unsigned int)irq_get_chip_data(d->irq) - FIQ_START;
  2525. + writel(0x80 | data, __io_address(ARM_IRQ_FAST));
  2526. + } else {
  2527. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  2528. + writel(1 << (data & 0x1f), __io_address(enables[(data >> 5) & 0x3]));
  2529. + }
  2530. +}
  2531. +
  2532. +#if defined(CONFIG_PM)
  2533. +
  2534. +/* for kernels 3.xx use the new syscore_ops apis but for older kernels use the sys dev class */
  2535. +
  2536. +/* Static defines
  2537. + * struct armctrl_device - VIC PM device (< 3.xx)
  2538. + * @sysdev: The system device which is registered. (< 3.xx)
  2539. + * @irq: The IRQ number for the base of the VIC.
  2540. + * @base: The register base for the VIC.
  2541. + * @resume_sources: A bitmask of interrupts for resume.
  2542. + * @resume_irqs: The IRQs enabled for resume.
  2543. + * @int_select: Save for VIC_INT_SELECT.
  2544. + * @int_enable: Save for VIC_INT_ENABLE.
  2545. + * @soft_int: Save for VIC_INT_SOFT.
  2546. + * @protect: Save for VIC_PROTECT.
  2547. + */
  2548. +struct armctrl_info {
  2549. + void __iomem *base;
  2550. + int irq;
  2551. + u32 resume_sources;
  2552. + u32 resume_irqs;
  2553. + u32 int_select;
  2554. + u32 int_enable;
  2555. + u32 soft_int;
  2556. + u32 protect;
  2557. +} armctrl;
  2558. +
  2559. +static int armctrl_suspend(void)
  2560. +{
  2561. + return 0;
  2562. +}
  2563. +
  2564. +static void armctrl_resume(void)
  2565. +{
  2566. + return;
  2567. +}
  2568. +
  2569. +/**
  2570. + * armctrl_pm_register - Register a VIC for later power management control
  2571. + * @base: The base address of the VIC.
  2572. + * @irq: The base IRQ for the VIC.
  2573. + * @resume_sources: bitmask of interrupts allowed for resume sources.
  2574. + *
  2575. + * For older kernels (< 3.xx) do -
  2576. + * Register the VIC with the system device tree so that it can be notified
  2577. + * of suspend and resume requests and ensure that the correct actions are
  2578. + * taken to re-instate the settings on resume.
  2579. + */
  2580. +static void __init armctrl_pm_register(void __iomem * base, unsigned int irq,
  2581. + u32 resume_sources)
  2582. +{
  2583. + armctrl.base = base;
  2584. + armctrl.resume_sources = resume_sources;
  2585. + armctrl.irq = irq;
  2586. +}
  2587. +
  2588. +static int armctrl_set_wake(struct irq_data *d, unsigned int on)
  2589. +{
  2590. + unsigned int off = d->irq & 31;
  2591. + u32 bit = 1 << off;
  2592. +
  2593. + if (!(bit & armctrl.resume_sources))
  2594. + return -EINVAL;
  2595. +
  2596. + if (on)
  2597. + armctrl.resume_irqs |= bit;
  2598. + else
  2599. + armctrl.resume_irqs &= ~bit;
  2600. +
  2601. + return 0;
  2602. +}
  2603. +
  2604. +#else
  2605. +static inline void armctrl_pm_register(void __iomem * base, unsigned int irq,
  2606. + u32 arg1)
  2607. +{
  2608. +}
  2609. +
  2610. +#define armctrl_suspend NULL
  2611. +#define armctrl_resume NULL
  2612. +#define armctrl_set_wake NULL
  2613. +#endif /* CONFIG_PM */
  2614. +
  2615. +static struct syscore_ops armctrl_syscore_ops = {
  2616. + .suspend = armctrl_suspend,
  2617. + .resume = armctrl_resume,
  2618. +};
  2619. +
  2620. +/**
  2621. + * armctrl_syscore_init - initicall to register VIC pm functions
  2622. + *
  2623. + * This is called via late_initcall() to register
  2624. + * the resources for the VICs due to the early
  2625. + * nature of the VIC's registration.
  2626. +*/
  2627. +static int __init armctrl_syscore_init(void)
  2628. +{
  2629. + register_syscore_ops(&armctrl_syscore_ops);
  2630. + return 0;
  2631. +}
  2632. +
  2633. +late_initcall(armctrl_syscore_init);
  2634. +
  2635. +static struct irq_chip armctrl_chip = {
  2636. + .name = "ARMCTRL",
  2637. + .irq_ack = armctrl_mask_irq,
  2638. + .irq_mask = armctrl_mask_irq,
  2639. + .irq_unmask = armctrl_unmask_irq,
  2640. + .irq_set_wake = armctrl_set_wake,
  2641. +};
  2642. +
  2643. +/**
  2644. + * armctrl_init - initialise a vectored interrupt controller
  2645. + * @base: iomem base address
  2646. + * @irq_start: starting interrupt number, must be muliple of 32
  2647. + * @armctrl_sources: bitmask of interrupt sources to allow
  2648. + * @resume_sources: bitmask of interrupt sources to allow for resume
  2649. + */
  2650. +int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  2651. + u32 armctrl_sources, u32 resume_sources)
  2652. +{
  2653. + unsigned int irq;
  2654. +
  2655. + for (irq = 0; irq < NR_IRQS; irq++) {
  2656. + unsigned int data = irq;
  2657. + if (irq >= INTERRUPT_JPEG && irq <= INTERRUPT_ARASANSDIO)
  2658. + data = remap_irqs[irq - INTERRUPT_JPEG];
  2659. +
  2660. + irq_set_chip(irq, &armctrl_chip);
  2661. + irq_set_chip_data(irq, (void *)data);
  2662. + irq_set_handler(irq, handle_level_irq);
  2663. + set_irq_flags(irq, IRQF_VALID | IRQF_PROBE | IRQF_DISABLED);
  2664. + }
  2665. +
  2666. + armctrl_pm_register(base, irq_start, resume_sources);
  2667. + init_FIQ(FIQ_START);
  2668. + return 0;
  2669. +}
  2670. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/armctrl.h linux-raspberry-pi/arch/arm/mach-bcm2708/armctrl.h
  2671. --- linux-3.13.6/arch/arm/mach-bcm2708/armctrl.h 1970-01-01 01:00:00.000000000 +0100
  2672. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/armctrl.h 2014-03-11 16:51:54.000000000 +0100
  2673. @@ -0,0 +1,27 @@
  2674. +/*
  2675. + * linux/arch/arm/mach-bcm2708/armctrl.h
  2676. + *
  2677. + * Copyright (C) 2010 Broadcom
  2678. + *
  2679. + * This program is free software; you can redistribute it and/or modify
  2680. + * it under the terms of the GNU General Public License as published by
  2681. + * the Free Software Foundation; either version 2 of the License, or
  2682. + * (at your option) any later version.
  2683. + *
  2684. + * This program is distributed in the hope that it will be useful,
  2685. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2686. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2687. + * GNU General Public License for more details.
  2688. + *
  2689. + * You should have received a copy of the GNU General Public License
  2690. + * along with this program; if not, write to the Free Software
  2691. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2692. + */
  2693. +
  2694. +#ifndef __BCM2708_ARMCTRL_H
  2695. +#define __BCM2708_ARMCTRL_H
  2696. +
  2697. +extern int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  2698. + u32 armctrl_sources, u32 resume_sources);
  2699. +
  2700. +#endif
  2701. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/bcm2708.c linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708.c
  2702. --- linux-3.13.6/arch/arm/mach-bcm2708/bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  2703. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708.c 2014-03-11 16:54:55.000000000 +0100
  2704. @@ -0,0 +1,1129 @@
  2705. +/*
  2706. + * linux/arch/arm/mach-bcm2708/bcm2708.c
  2707. + *
  2708. + * Copyright (C) 2010 Broadcom
  2709. + *
  2710. + * This program is free software; you can redistribute it and/or modify
  2711. + * it under the terms of the GNU General Public License as published by
  2712. + * the Free Software Foundation; either version 2 of the License, or
  2713. + * (at your option) any later version.
  2714. + *
  2715. + * This program is distributed in the hope that it will be useful,
  2716. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2717. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2718. + * GNU General Public License for more details.
  2719. + *
  2720. + * You should have received a copy of the GNU General Public License
  2721. + * along with this program; if not, write to the Free Software
  2722. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2723. + */
  2724. +
  2725. +#include <linux/init.h>
  2726. +#include <linux/device.h>
  2727. +#include <linux/dma-mapping.h>
  2728. +#include <linux/serial_8250.h>
  2729. +#include <linux/platform_device.h>
  2730. +#include <linux/syscore_ops.h>
  2731. +#include <linux/interrupt.h>
  2732. +#include <linux/amba/bus.h>
  2733. +#include <linux/amba/clcd.h>
  2734. +#include <linux/clockchips.h>
  2735. +#include <linux/cnt32_to_63.h>
  2736. +#include <linux/io.h>
  2737. +#include <linux/module.h>
  2738. +#include <linux/spi/spi.h>
  2739. +#include <linux/w1-gpio.h>
  2740. +
  2741. +#include <linux/version.h>
  2742. +#include <linux/clkdev.h>
  2743. +#include <asm/system.h>
  2744. +#include <mach/hardware.h>
  2745. +#include <asm/irq.h>
  2746. +#include <linux/leds.h>
  2747. +#include <asm/mach-types.h>
  2748. +#include <linux/sched_clock.h>
  2749. +
  2750. +#include <asm/mach/arch.h>
  2751. +#include <asm/mach/flash.h>
  2752. +#include <asm/mach/irq.h>
  2753. +#include <asm/mach/time.h>
  2754. +#include <asm/mach/map.h>
  2755. +
  2756. +#include <mach/timex.h>
  2757. +#include <mach/dma.h>
  2758. +#include <mach/vcio.h>
  2759. +#include <mach/system.h>
  2760. +
  2761. +#include <linux/delay.h>
  2762. +
  2763. +#include "bcm2708.h"
  2764. +#include "armctrl.h"
  2765. +#include "clock.h"
  2766. +
  2767. +#ifdef CONFIG_BCM_VC_CMA
  2768. +#include <linux/broadcom/vc_cma.h>
  2769. +#endif
  2770. +
  2771. +
  2772. +/* Effectively we have an IOMMU (ARM<->VideoCore map) that is set up to
  2773. + * give us IO access only to 64Mbytes of physical memory (26 bits). We could
  2774. + * represent this window by setting our dmamasks to 26 bits but, in fact
  2775. + * we're not going to use addresses outside this range (they're not in real
  2776. + * memory) so we don't bother.
  2777. + *
  2778. + * In the future we might include code to use this IOMMU to remap other
  2779. + * physical addresses onto VideoCore memory then the use of 32-bits would be
  2780. + * more legitimate.
  2781. + */
  2782. +#define DMA_MASK_BITS_COMMON 32
  2783. +
  2784. +// use GPIO 4 for the one-wire GPIO pin, if enabled
  2785. +#define W1_GPIO 4
  2786. +
  2787. +/* command line parameters */
  2788. +static unsigned boardrev, serial;
  2789. +static unsigned uart_clock;
  2790. +static unsigned disk_led_gpio = 16;
  2791. +static unsigned disk_led_active_low = 1;
  2792. +static unsigned reboot_part = 0;
  2793. +
  2794. +static void __init bcm2708_init_led(void);
  2795. +
  2796. +void __init bcm2708_init_irq(void)
  2797. +{
  2798. + armctrl_init(__io_address(ARMCTRL_IC_BASE), 0, 0, 0);
  2799. +}
  2800. +
  2801. +static struct map_desc bcm2708_io_desc[] __initdata = {
  2802. + {
  2803. + .virtual = IO_ADDRESS(ARMCTRL_BASE),
  2804. + .pfn = __phys_to_pfn(ARMCTRL_BASE),
  2805. + .length = SZ_4K,
  2806. + .type = MT_DEVICE},
  2807. + {
  2808. + .virtual = IO_ADDRESS(UART0_BASE),
  2809. + .pfn = __phys_to_pfn(UART0_BASE),
  2810. + .length = SZ_4K,
  2811. + .type = MT_DEVICE},
  2812. + {
  2813. + .virtual = IO_ADDRESS(UART1_BASE),
  2814. + .pfn = __phys_to_pfn(UART1_BASE),
  2815. + .length = SZ_4K,
  2816. + .type = MT_DEVICE},
  2817. + {
  2818. + .virtual = IO_ADDRESS(DMA_BASE),
  2819. + .pfn = __phys_to_pfn(DMA_BASE),
  2820. + .length = SZ_4K,
  2821. + .type = MT_DEVICE},
  2822. + {
  2823. + .virtual = IO_ADDRESS(MCORE_BASE),
  2824. + .pfn = __phys_to_pfn(MCORE_BASE),
  2825. + .length = SZ_4K,
  2826. + .type = MT_DEVICE},
  2827. + {
  2828. + .virtual = IO_ADDRESS(ST_BASE),
  2829. + .pfn = __phys_to_pfn(ST_BASE),
  2830. + .length = SZ_4K,
  2831. + .type = MT_DEVICE},
  2832. + {
  2833. + .virtual = IO_ADDRESS(USB_BASE),
  2834. + .pfn = __phys_to_pfn(USB_BASE),
  2835. + .length = SZ_128K,
  2836. + .type = MT_DEVICE},
  2837. + {
  2838. + .virtual = IO_ADDRESS(PM_BASE),
  2839. + .pfn = __phys_to_pfn(PM_BASE),
  2840. + .length = SZ_4K,
  2841. + .type = MT_DEVICE},
  2842. + {
  2843. + .virtual = IO_ADDRESS(GPIO_BASE),
  2844. + .pfn = __phys_to_pfn(GPIO_BASE),
  2845. + .length = SZ_4K,
  2846. + .type = MT_DEVICE}
  2847. +};
  2848. +
  2849. +void __init bcm2708_map_io(void)
  2850. +{
  2851. + iotable_init(bcm2708_io_desc, ARRAY_SIZE(bcm2708_io_desc));
  2852. +}
  2853. +
  2854. +/* The STC is a free running counter that increments at the rate of 1MHz */
  2855. +#define STC_FREQ_HZ 1000000
  2856. +
  2857. +static inline uint32_t timer_read(void)
  2858. +{
  2859. + /* STC: a free running counter that increments at the rate of 1MHz */
  2860. + return readl(__io_address(ST_BASE + 0x04));
  2861. +}
  2862. +
  2863. +static unsigned long bcm2708_read_current_timer(void)
  2864. +{
  2865. + return timer_read();
  2866. +}
  2867. +
  2868. +static u32 notrace bcm2708_read_sched_clock(void)
  2869. +{
  2870. + return timer_read();
  2871. +}
  2872. +
  2873. +static cycle_t clksrc_read(struct clocksource *cs)
  2874. +{
  2875. + return timer_read();
  2876. +}
  2877. +
  2878. +static struct clocksource clocksource_stc = {
  2879. + .name = "stc",
  2880. + .rating = 300,
  2881. + .read = clksrc_read,
  2882. + .mask = CLOCKSOURCE_MASK(32),
  2883. + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  2884. +};
  2885. +
  2886. +unsigned long frc_clock_ticks32(void)
  2887. +{
  2888. + return timer_read();
  2889. +}
  2890. +
  2891. +static void __init bcm2708_clocksource_init(void)
  2892. +{
  2893. + if (clocksource_register_hz(&clocksource_stc, STC_FREQ_HZ)) {
  2894. + printk(KERN_ERR "timer: failed to initialize clock "
  2895. + "source %s\n", clocksource_stc.name);
  2896. + }
  2897. +}
  2898. +
  2899. +
  2900. +/*
  2901. + * These are fixed clocks.
  2902. + */
  2903. +static struct clk ref24_clk = {
  2904. + .rate = UART0_CLOCK, /* The UART is clocked at 3MHz via APB_CLK */
  2905. +};
  2906. +
  2907. +static struct clk osc_clk = {
  2908. +#ifdef CONFIG_ARCH_BCM2708_CHIPIT
  2909. + .rate = 27000000,
  2910. +#else
  2911. + .rate = 500000000, /* ARM clock is set from the VideoCore booter */
  2912. +#endif
  2913. +};
  2914. +
  2915. +/* warning - the USB needs a clock > 34MHz */
  2916. +
  2917. +static struct clk sdhost_clk = {
  2918. +#ifdef CONFIG_ARCH_BCM2708_CHIPIT
  2919. + .rate = 4000000, /* 4MHz */
  2920. +#else
  2921. + .rate = 250000000, /* 250MHz */
  2922. +#endif
  2923. +};
  2924. +
  2925. +static struct clk_lookup lookups[] = {
  2926. + { /* UART0 */
  2927. + .dev_id = "dev:f1",
  2928. + .clk = &ref24_clk,
  2929. + },
  2930. + { /* USB */
  2931. + .dev_id = "bcm2708_usb",
  2932. + .clk = &osc_clk,
  2933. + }, { /* SPI */
  2934. + .dev_id = "bcm2708_spi.0",
  2935. + .clk = &sdhost_clk,
  2936. + }, { /* BSC0 */
  2937. + .dev_id = "bcm2708_i2c.0",
  2938. + .clk = &sdhost_clk,
  2939. + }, { /* BSC1 */
  2940. + .dev_id = "bcm2708_i2c.1",
  2941. + .clk = &sdhost_clk,
  2942. + }
  2943. +};
  2944. +
  2945. +#define UART0_IRQ { IRQ_UART, 0 /*NO_IRQ*/ }
  2946. +#define UART0_DMA { 15, 14 }
  2947. +
  2948. +AMBA_DEVICE(uart0, "dev:f1", UART0, NULL);
  2949. +
  2950. +static struct amba_device *amba_devs[] __initdata = {
  2951. + &uart0_device,
  2952. +};
  2953. +
  2954. +static struct resource bcm2708_dmaman_resources[] = {
  2955. + {
  2956. + .start = DMA_BASE,
  2957. + .end = DMA_BASE + SZ_4K - 1,
  2958. + .flags = IORESOURCE_MEM,
  2959. + }
  2960. +};
  2961. +
  2962. +static struct platform_device bcm2708_dmaman_device = {
  2963. + .name = BCM_DMAMAN_DRIVER_NAME,
  2964. + .id = 0, /* first bcm2708_dma */
  2965. + .resource = bcm2708_dmaman_resources,
  2966. + .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
  2967. +};
  2968. +
  2969. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  2970. +static struct w1_gpio_platform_data w1_gpio_pdata = {
  2971. + .pin = W1_GPIO,
  2972. + .is_open_drain = 0,
  2973. +};
  2974. +
  2975. +static struct platform_device w1_device = {
  2976. + .name = "w1-gpio",
  2977. + .id = -1,
  2978. + .dev.platform_data = &w1_gpio_pdata,
  2979. +};
  2980. +#endif
  2981. +
  2982. +static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  2983. +
  2984. +static struct platform_device bcm2708_fb_device = {
  2985. + .name = "bcm2708_fb",
  2986. + .id = -1, /* only one bcm2708_fb */
  2987. + .resource = NULL,
  2988. + .num_resources = 0,
  2989. + .dev = {
  2990. + .dma_mask = &fb_dmamask,
  2991. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  2992. + },
  2993. +};
  2994. +
  2995. +static struct plat_serial8250_port bcm2708_uart1_platform_data[] = {
  2996. + {
  2997. + .mapbase = UART1_BASE + 0x40,
  2998. + .irq = IRQ_AUX,
  2999. + .uartclk = 125000000,
  3000. + .regshift = 2,
  3001. + .iotype = UPIO_MEM,
  3002. + .flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_SKIP_TEST,
  3003. + .type = PORT_8250,
  3004. + },
  3005. + {},
  3006. +};
  3007. +
  3008. +static struct platform_device bcm2708_uart1_device = {
  3009. + .name = "serial8250",
  3010. + .id = PLAT8250_DEV_PLATFORM,
  3011. + .dev = {
  3012. + .platform_data = bcm2708_uart1_platform_data,
  3013. + },
  3014. +};
  3015. +
  3016. +static struct resource bcm2708_usb_resources[] = {
  3017. + [0] = {
  3018. + .start = USB_BASE,
  3019. + .end = USB_BASE + SZ_128K - 1,
  3020. + .flags = IORESOURCE_MEM,
  3021. + },
  3022. + [1] = {
  3023. + .start = MPHI_BASE,
  3024. + .end = MPHI_BASE + SZ_4K - 1,
  3025. + .flags = IORESOURCE_MEM,
  3026. + },
  3027. + [2] = {
  3028. + .start = IRQ_HOSTPORT,
  3029. + .end = IRQ_HOSTPORT,
  3030. + .flags = IORESOURCE_IRQ,
  3031. + },
  3032. +};
  3033. +
  3034. +bool fiq_fix_enable = true;
  3035. +
  3036. +static struct resource bcm2708_usb_resources_no_fiq_fix[] = {
  3037. + [0] = {
  3038. + .start = USB_BASE,
  3039. + .end = USB_BASE + SZ_128K - 1,
  3040. + .flags = IORESOURCE_MEM,
  3041. + },
  3042. + [1] = {
  3043. + .start = IRQ_USB,
  3044. + .end = IRQ_USB,
  3045. + .flags = IORESOURCE_IRQ,
  3046. + },
  3047. +};
  3048. +
  3049. +static u64 usb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3050. +
  3051. +static struct platform_device bcm2708_usb_device = {
  3052. + .name = "bcm2708_usb",
  3053. + .id = -1, /* only one bcm2708_usb */
  3054. + .resource = bcm2708_usb_resources,
  3055. + .num_resources = ARRAY_SIZE(bcm2708_usb_resources),
  3056. + .dev = {
  3057. + .dma_mask = &usb_dmamask,
  3058. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3059. + },
  3060. +};
  3061. +
  3062. +static struct resource bcm2708_vcio_resources[] = {
  3063. + [0] = { /* mailbox/semaphore/doorbell access */
  3064. + .start = MCORE_BASE,
  3065. + .end = MCORE_BASE + SZ_4K - 1,
  3066. + .flags = IORESOURCE_MEM,
  3067. + },
  3068. +};
  3069. +
  3070. +static u64 vcio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3071. +
  3072. +static struct platform_device bcm2708_vcio_device = {
  3073. + .name = BCM_VCIO_DRIVER_NAME,
  3074. + .id = -1, /* only one VideoCore I/O area */
  3075. + .resource = bcm2708_vcio_resources,
  3076. + .num_resources = ARRAY_SIZE(bcm2708_vcio_resources),
  3077. + .dev = {
  3078. + .dma_mask = &vcio_dmamask,
  3079. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3080. + },
  3081. +};
  3082. +
  3083. +#ifdef CONFIG_BCM2708_GPIO
  3084. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  3085. +
  3086. +static struct resource bcm2708_gpio_resources[] = {
  3087. + [0] = { /* general purpose I/O */
  3088. + .start = GPIO_BASE,
  3089. + .end = GPIO_BASE + SZ_4K - 1,
  3090. + .flags = IORESOURCE_MEM,
  3091. + },
  3092. +};
  3093. +
  3094. +static u64 gpio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3095. +
  3096. +static struct platform_device bcm2708_gpio_device = {
  3097. + .name = BCM_GPIO_DRIVER_NAME,
  3098. + .id = -1, /* only one VideoCore I/O area */
  3099. + .resource = bcm2708_gpio_resources,
  3100. + .num_resources = ARRAY_SIZE(bcm2708_gpio_resources),
  3101. + .dev = {
  3102. + .dma_mask = &gpio_dmamask,
  3103. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3104. + },
  3105. +};
  3106. +#endif
  3107. +
  3108. +static struct resource bcm2708_systemtimer_resources[] = {
  3109. + [0] = { /* system timer access */
  3110. + .start = ST_BASE,
  3111. + .end = ST_BASE + SZ_4K - 1,
  3112. + .flags = IORESOURCE_MEM,
  3113. + },
  3114. + {
  3115. + .start = IRQ_TIMER3,
  3116. + .end = IRQ_TIMER3,
  3117. + .flags = IORESOURCE_IRQ,
  3118. + }
  3119. +
  3120. +};
  3121. +
  3122. +static u64 systemtimer_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3123. +
  3124. +static struct platform_device bcm2708_systemtimer_device = {
  3125. + .name = "bcm2708_systemtimer",
  3126. + .id = -1, /* only one VideoCore I/O area */
  3127. + .resource = bcm2708_systemtimer_resources,
  3128. + .num_resources = ARRAY_SIZE(bcm2708_systemtimer_resources),
  3129. + .dev = {
  3130. + .dma_mask = &systemtimer_dmamask,
  3131. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3132. + },
  3133. +};
  3134. +
  3135. +#ifdef CONFIG_MMC_SDHCI_BCM2708 /* Arasan emmc SD */
  3136. +static struct resource bcm2708_emmc_resources[] = {
  3137. + [0] = {
  3138. + .start = EMMC_BASE,
  3139. + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
  3140. + /* the memory map actually makes SZ_4K available */
  3141. + .flags = IORESOURCE_MEM,
  3142. + },
  3143. + [1] = {
  3144. + .start = IRQ_ARASANSDIO,
  3145. + .end = IRQ_ARASANSDIO,
  3146. + .flags = IORESOURCE_IRQ,
  3147. + },
  3148. +};
  3149. +
  3150. +static u64 bcm2708_emmc_dmamask = 0xffffffffUL;
  3151. +
  3152. +struct platform_device bcm2708_emmc_device = {
  3153. + .name = "bcm2708_sdhci",
  3154. + .id = 0,
  3155. + .num_resources = ARRAY_SIZE(bcm2708_emmc_resources),
  3156. + .resource = bcm2708_emmc_resources,
  3157. + .dev = {
  3158. + .dma_mask = &bcm2708_emmc_dmamask,
  3159. + .coherent_dma_mask = 0xffffffffUL},
  3160. +};
  3161. +#endif /* CONFIG_MMC_SDHCI_BCM2708 */
  3162. +
  3163. +static struct resource bcm2708_powerman_resources[] = {
  3164. + [0] = {
  3165. + .start = PM_BASE,
  3166. + .end = PM_BASE + SZ_256 - 1,
  3167. + .flags = IORESOURCE_MEM,
  3168. + },
  3169. +};
  3170. +
  3171. +static u64 powerman_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3172. +
  3173. +struct platform_device bcm2708_powerman_device = {
  3174. + .name = "bcm2708_powerman",
  3175. + .id = 0,
  3176. + .num_resources = ARRAY_SIZE(bcm2708_powerman_resources),
  3177. + .resource = bcm2708_powerman_resources,
  3178. + .dev = {
  3179. + .dma_mask = &powerman_dmamask,
  3180. + .coherent_dma_mask = 0xffffffffUL},
  3181. +};
  3182. +
  3183. +
  3184. +static struct platform_device bcm2708_alsa_devices[] = {
  3185. + [0] = {
  3186. + .name = "bcm2835_AUD0",
  3187. + .id = 0, /* first audio device */
  3188. + .resource = 0,
  3189. + .num_resources = 0,
  3190. + },
  3191. + [1] = {
  3192. + .name = "bcm2835_AUD1",
  3193. + .id = 1, /* second audio device */
  3194. + .resource = 0,
  3195. + .num_resources = 0,
  3196. + },
  3197. + [2] = {
  3198. + .name = "bcm2835_AUD2",
  3199. + .id = 2, /* third audio device */
  3200. + .resource = 0,
  3201. + .num_resources = 0,
  3202. + },
  3203. + [3] = {
  3204. + .name = "bcm2835_AUD3",
  3205. + .id = 3, /* forth audio device */
  3206. + .resource = 0,
  3207. + .num_resources = 0,
  3208. + },
  3209. + [4] = {
  3210. + .name = "bcm2835_AUD4",
  3211. + .id = 4, /* fifth audio device */
  3212. + .resource = 0,
  3213. + .num_resources = 0,
  3214. + },
  3215. + [5] = {
  3216. + .name = "bcm2835_AUD5",
  3217. + .id = 5, /* sixth audio device */
  3218. + .resource = 0,
  3219. + .num_resources = 0,
  3220. + },
  3221. + [6] = {
  3222. + .name = "bcm2835_AUD6",
  3223. + .id = 6, /* seventh audio device */
  3224. + .resource = 0,
  3225. + .num_resources = 0,
  3226. + },
  3227. + [7] = {
  3228. + .name = "bcm2835_AUD7",
  3229. + .id = 7, /* eighth audio device */
  3230. + .resource = 0,
  3231. + .num_resources = 0,
  3232. + },
  3233. +};
  3234. +
  3235. +static struct resource bcm2708_spi_resources[] = {
  3236. + {
  3237. + .start = SPI0_BASE,
  3238. + .end = SPI0_BASE + SZ_256 - 1,
  3239. + .flags = IORESOURCE_MEM,
  3240. + }, {
  3241. + .start = IRQ_SPI,
  3242. + .end = IRQ_SPI,
  3243. + .flags = IORESOURCE_IRQ,
  3244. + }
  3245. +};
  3246. +
  3247. +
  3248. +static u64 bcm2708_spi_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3249. +static struct platform_device bcm2708_spi_device = {
  3250. + .name = "bcm2708_spi",
  3251. + .id = 0,
  3252. + .num_resources = ARRAY_SIZE(bcm2708_spi_resources),
  3253. + .resource = bcm2708_spi_resources,
  3254. + .dev = {
  3255. + .dma_mask = &bcm2708_spi_dmamask,
  3256. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON)},
  3257. +};
  3258. +
  3259. +#ifdef CONFIG_BCM2708_SPIDEV
  3260. +static struct spi_board_info bcm2708_spi_devices[] = {
  3261. +#ifdef CONFIG_SPI_SPIDEV
  3262. + {
  3263. + .modalias = "spidev",
  3264. + .max_speed_hz = 500000,
  3265. + .bus_num = 0,
  3266. + .chip_select = 0,
  3267. + .mode = SPI_MODE_0,
  3268. + }, {
  3269. + .modalias = "spidev",
  3270. + .max_speed_hz = 500000,
  3271. + .bus_num = 0,
  3272. + .chip_select = 1,
  3273. + .mode = SPI_MODE_0,
  3274. + }
  3275. +#endif
  3276. +};
  3277. +#endif
  3278. +
  3279. +static struct resource bcm2708_bsc0_resources[] = {
  3280. + {
  3281. + .start = BSC0_BASE,
  3282. + .end = BSC0_BASE + SZ_256 - 1,
  3283. + .flags = IORESOURCE_MEM,
  3284. + }, {
  3285. + .start = INTERRUPT_I2C,
  3286. + .end = INTERRUPT_I2C,
  3287. + .flags = IORESOURCE_IRQ,
  3288. + }
  3289. +};
  3290. +
  3291. +static struct platform_device bcm2708_bsc0_device = {
  3292. + .name = "bcm2708_i2c",
  3293. + .id = 0,
  3294. + .num_resources = ARRAY_SIZE(bcm2708_bsc0_resources),
  3295. + .resource = bcm2708_bsc0_resources,
  3296. +};
  3297. +
  3298. +
  3299. +static struct resource bcm2708_bsc1_resources[] = {
  3300. + {
  3301. + .start = BSC1_BASE,
  3302. + .end = BSC1_BASE + SZ_256 - 1,
  3303. + .flags = IORESOURCE_MEM,
  3304. + }, {
  3305. + .start = INTERRUPT_I2C,
  3306. + .end = INTERRUPT_I2C,
  3307. + .flags = IORESOURCE_IRQ,
  3308. + }
  3309. +};
  3310. +
  3311. +static struct platform_device bcm2708_bsc1_device = {
  3312. + .name = "bcm2708_i2c",
  3313. + .id = 1,
  3314. + .num_resources = ARRAY_SIZE(bcm2708_bsc1_resources),
  3315. + .resource = bcm2708_bsc1_resources,
  3316. +};
  3317. +
  3318. +static struct platform_device bcm2835_hwmon_device = {
  3319. + .name = "bcm2835_hwmon",
  3320. +};
  3321. +
  3322. +static struct platform_device bcm2835_thermal_device = {
  3323. + .name = "bcm2835_thermal",
  3324. +};
  3325. +
  3326. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  3327. +static struct resource bcm2708_i2s_resources[] = {
  3328. + {
  3329. + .start = I2S_BASE,
  3330. + .end = I2S_BASE + 0x20,
  3331. + .flags = IORESOURCE_MEM,
  3332. + },
  3333. + {
  3334. + .start = PCM_CLOCK_BASE,
  3335. + .end = PCM_CLOCK_BASE + 0x02,
  3336. + .flags = IORESOURCE_MEM,
  3337. + }
  3338. +};
  3339. +
  3340. +static struct platform_device bcm2708_i2s_device = {
  3341. + .name = "bcm2708-i2s",
  3342. + .id = 0,
  3343. + .num_resources = ARRAY_SIZE(bcm2708_i2s_resources),
  3344. + .resource = bcm2708_i2s_resources,
  3345. +};
  3346. +#endif
  3347. +
  3348. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  3349. +static struct platform_device snd_hifiberry_dac_device = {
  3350. + .name = "snd-hifiberry-dac",
  3351. + .id = 0,
  3352. + .num_resources = 0,
  3353. +};
  3354. +
  3355. +static struct platform_device snd_pcm5102a_codec_device = {
  3356. + .name = "pcm5102a-codec",
  3357. + .id = -1,
  3358. + .num_resources = 0,
  3359. +};
  3360. +#endif
  3361. +
  3362. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  3363. +static struct platform_device snd_hifiberry_digi_device = {
  3364. + .name = "snd-hifiberry-digi",
  3365. + .id = 0,
  3366. + .num_resources = 0,
  3367. +};
  3368. +
  3369. +static struct i2c_board_info __initdata snd_wm8804_i2c_devices[] = {
  3370. + {
  3371. + I2C_BOARD_INFO("wm8804", 0x3b)
  3372. + },
  3373. +};
  3374. +
  3375. +#endif
  3376. +
  3377. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  3378. +static struct platform_device snd_rpi_dac_device = {
  3379. + .name = "snd-rpi-dac",
  3380. + .id = 0,
  3381. + .num_resources = 0,
  3382. +};
  3383. +
  3384. +static struct platform_device snd_pcm1794a_codec_device = {
  3385. + .name = "pcm1794a-codec",
  3386. + .id = -1,
  3387. + .num_resources = 0,
  3388. +};
  3389. +#endif
  3390. +
  3391. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_MBED_MODULE
  3392. +static struct platform_device snd_rpi_mbed_device = {
  3393. + .name = "snd-rpi-mbed",
  3394. + .id = 0,
  3395. + .num_resources = 0,
  3396. +};
  3397. +
  3398. +
  3399. +static struct i2c_board_info __initdata snd_rpi_mbed_i2c_devices[] = {
  3400. + {
  3401. + I2C_BOARD_INFO("tlv320aic23", 0x1b)
  3402. + },
  3403. +};
  3404. +#endif
  3405. +
  3406. +
  3407. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_TDA1541A_MODULE
  3408. +static struct platform_device snd_rpi_tda1541a_device = {
  3409. + .name = "snd-rpi-tda1541a",
  3410. + .id = 0,
  3411. + .num_resources = 0,
  3412. +};
  3413. +
  3414. +static struct platform_device snd_rpi_tda1541a_codec_device = {
  3415. + .name = "tda1541a-codec",
  3416. + .id = -1,
  3417. + .num_resources = 0,
  3418. +};
  3419. +#endif
  3420. +
  3421. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_PROTO_MODULE
  3422. +static struct platform_device snd_rpi_proto_device = {
  3423. + .name = "snd-rpi-proto",
  3424. + .id = 0,
  3425. + .num_resources = 0,
  3426. +};
  3427. +
  3428. +static struct i2c_board_info __initdata snd_rpi_proto_i2c_devices[] = {
  3429. + {
  3430. + I2C_BOARD_INFO("wm8731", 0x1a)
  3431. + },
  3432. +};
  3433. +#endif
  3434. +
  3435. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_CS534X_MODULE
  3436. +static struct platform_device snd_rpi_cs534x_device = {
  3437. + .name = "snd-rpi-cs534x",
  3438. + .id = 0,
  3439. + .num_resources = 0,
  3440. +};
  3441. +
  3442. +static struct platform_device snd_rpi_cs534x_codec_device = {
  3443. + .name = "cs534x-codec",
  3444. + .id = -1,
  3445. + .num_resources = 0,
  3446. +};
  3447. +
  3448. +#endif
  3449. +
  3450. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_ESS9018_MODULE
  3451. +static struct platform_device snd_rpi_ess9018_device = {
  3452. + .name = "snd-rpi-ess9018",
  3453. + .id = 0,
  3454. + .num_resources = 0,
  3455. +};
  3456. +
  3457. +static struct platform_device snd_rpi_ess9018_codec_device = {
  3458. + .name = "ess9018-codec",
  3459. + .id = -1,
  3460. + .num_resources = 0,
  3461. +};
  3462. +#endif
  3463. +
  3464. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_PCM5102A_MODULE
  3465. +static struct platform_device snd_rpi_pcm5102a_device = {
  3466. + .name = "snd-rpi-pcm5102a",
  3467. + .id = 0,
  3468. + .num_resources = 0,
  3469. +};
  3470. +
  3471. +static struct platform_device snd_rpi_pcm5102a_codec_device = {
  3472. + .name = "pcm5102a-codec",
  3473. + .id = -1,
  3474. + .num_resources = 0,
  3475. +};
  3476. +#endif
  3477. +
  3478. +int __init bcm_register_device(struct platform_device *pdev)
  3479. +{
  3480. + int ret;
  3481. +
  3482. + ret = platform_device_register(pdev);
  3483. + if (ret)
  3484. + pr_debug("Unable to register platform device '%s': %d\n",
  3485. + pdev->name, ret);
  3486. +
  3487. + return ret;
  3488. +}
  3489. +
  3490. +int calc_rsts(int partition)
  3491. +{
  3492. + return PM_PASSWORD |
  3493. + ((partition & (1 << 0)) << 0) |
  3494. + ((partition & (1 << 1)) << 1) |
  3495. + ((partition & (1 << 2)) << 2) |
  3496. + ((partition & (1 << 3)) << 3) |
  3497. + ((partition & (1 << 4)) << 4) |
  3498. + ((partition & (1 << 5)) << 5);
  3499. +}
  3500. +
  3501. +static void bcm2708_restart(enum reboot_mode mode, const char *cmd)
  3502. +{
  3503. + extern char bcm2708_reboot_mode;
  3504. + uint32_t pm_rstc, pm_wdog;
  3505. + uint32_t timeout = 10;
  3506. + uint32_t pm_rsts = 0;
  3507. +
  3508. + if(bcm2708_reboot_mode == 'q')
  3509. + {
  3510. + // NOOBS < 1.3 booting with reboot=q
  3511. + pm_rsts = readl(__io_address(PM_RSTS));
  3512. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRQ_SET;
  3513. + }
  3514. + else if(bcm2708_reboot_mode == 'p')
  3515. + {
  3516. + // NOOBS < 1.3 halting
  3517. + pm_rsts = readl(__io_address(PM_RSTS));
  3518. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRH_SET;
  3519. + }
  3520. + else
  3521. + {
  3522. + pm_rsts = calc_rsts(reboot_part);
  3523. + }
  3524. +
  3525. + writel(pm_rsts, __io_address(PM_RSTS));
  3526. +
  3527. + /* Setup watchdog for reset */
  3528. + pm_rstc = readl(__io_address(PM_RSTC));
  3529. +
  3530. + pm_wdog = PM_PASSWORD | (timeout & PM_WDOG_TIME_SET); // watchdog timer = timer clock / 16; need password (31:16) + value (11:0)
  3531. + pm_rstc = PM_PASSWORD | (pm_rstc & PM_RSTC_WRCFG_CLR) | PM_RSTC_WRCFG_FULL_RESET;
  3532. +
  3533. + writel(pm_wdog, __io_address(PM_WDOG));
  3534. + writel(pm_rstc, __io_address(PM_RSTC));
  3535. +}
  3536. +
  3537. +/* 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 */
  3538. +static void bcm2708_power_off(void)
  3539. +{
  3540. + extern char bcm2708_reboot_mode;
  3541. + if(bcm2708_reboot_mode == 'q')
  3542. + {
  3543. + // NOOBS < v1.3
  3544. + bcm2708_restart('p', "");
  3545. + }
  3546. + else
  3547. + {
  3548. + /* partition 63 is special code for HALT the bootloader knows not to boot*/
  3549. + reboot_part = 63;
  3550. + /* continue with normal reset mechanism */
  3551. + bcm2708_restart(0, "");
  3552. + }
  3553. +}
  3554. +
  3555. +void __init bcm2708_init(void)
  3556. +{
  3557. + int i;
  3558. +
  3559. +#if defined(CONFIG_BCM_VC_CMA)
  3560. + vc_cma_early_init();
  3561. +#endif
  3562. + printk("bcm2708.uart_clock = %d\n", uart_clock);
  3563. + pm_power_off = bcm2708_power_off;
  3564. +
  3565. + if (uart_clock)
  3566. + lookups[0].clk->rate = uart_clock;
  3567. +
  3568. + for (i = 0; i < ARRAY_SIZE(lookups); i++)
  3569. + clkdev_add(&lookups[i]);
  3570. +
  3571. + bcm_register_device(&bcm2708_dmaman_device);
  3572. + bcm_register_device(&bcm2708_vcio_device);
  3573. +#ifdef CONFIG_BCM2708_GPIO
  3574. + bcm_register_device(&bcm2708_gpio_device);
  3575. +#endif
  3576. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  3577. + platform_device_register(&w1_device);
  3578. +#endif
  3579. + bcm_register_device(&bcm2708_systemtimer_device);
  3580. + bcm_register_device(&bcm2708_fb_device);
  3581. + if (!fiq_fix_enable)
  3582. + {
  3583. + bcm2708_usb_device.resource = bcm2708_usb_resources_no_fiq_fix;
  3584. + bcm2708_usb_device.num_resources = ARRAY_SIZE(bcm2708_usb_resources_no_fiq_fix);
  3585. + }
  3586. + bcm_register_device(&bcm2708_usb_device);
  3587. + bcm_register_device(&bcm2708_uart1_device);
  3588. + bcm_register_device(&bcm2708_powerman_device);
  3589. +
  3590. +#ifdef CONFIG_MMC_SDHCI_BCM2708
  3591. + bcm_register_device(&bcm2708_emmc_device);
  3592. +#endif
  3593. + bcm2708_init_led();
  3594. + for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
  3595. + bcm_register_device(&bcm2708_alsa_devices[i]);
  3596. +
  3597. + bcm_register_device(&bcm2708_spi_device);
  3598. + bcm_register_device(&bcm2708_bsc0_device);
  3599. + bcm_register_device(&bcm2708_bsc1_device);
  3600. +
  3601. + bcm_register_device(&bcm2835_hwmon_device);
  3602. + bcm_register_device(&bcm2835_thermal_device);
  3603. +
  3604. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  3605. + bcm_register_device(&bcm2708_i2s_device);
  3606. +#endif
  3607. +
  3608. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  3609. + bcm_register_device(&snd_hifiberry_dac_device);
  3610. + bcm_register_device(&snd_pcm5102a_codec_device);
  3611. +#endif
  3612. +
  3613. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  3614. + bcm_register_device(&snd_hifiberry_digi_device);
  3615. + i2c_register_board_info(1, snd_wm8804_i2c_devices, ARRAY_SIZE(snd_wm8804_i2c_devices));
  3616. +#endif
  3617. +
  3618. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  3619. + bcm_register_device(&snd_rpi_dac_device);
  3620. + bcm_register_device(&snd_pcm1794a_codec_device);
  3621. +#endif
  3622. +
  3623. +
  3624. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_MBED_MODULE
  3625. + bcm_register_device(&snd_rpi_mbed_device);
  3626. + i2c_register_board_info(1, snd_rpi_mbed_i2c_devices, ARRAY_SIZE(snd_rpi_mbed_i2c_devices));
  3627. +#endif
  3628. +
  3629. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_CS534X_MODULE
  3630. + bcm_register_device(&snd_rpi_cs534x_device);
  3631. + bcm_register_device(&snd_rpi_cs534x_codec_device);
  3632. +#endif
  3633. +
  3634. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_TDA1541A_MODULE
  3635. + bcm_register_device(&snd_rpi_tda1541a_device);
  3636. + bcm_register_device(&snd_rpi_tda1541a_codec_device);
  3637. +#endif
  3638. +
  3639. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_PROTO_MODULE
  3640. + bcm_register_device(&snd_rpi_proto_device);
  3641. + i2c_register_board_info(1, snd_rpi_proto_i2c_devices, ARRAY_SIZE(snd_rpi_proto_i2c_devices));
  3642. +#endif
  3643. +
  3644. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_ESS9018_MODULE
  3645. + bcm_register_device(&snd_rpi_ess9018_device);
  3646. + bcm_register_device(&snd_rpi_ess9018_codec_device);
  3647. +#endif
  3648. +
  3649. +#ifdef CONFIG_SND_BCM2708_SOC_RPI_CODEC_PCM5102A_MODULE
  3650. + bcm_register_device(&snd_rpi_pcm5102a_device);
  3651. + bcm_register_device(&snd_rpi_pcm5102a_codec_device);
  3652. +#endif
  3653. +
  3654. + for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
  3655. + struct amba_device *d = amba_devs[i];
  3656. + amba_device_register(d, &iomem_resource);
  3657. + }
  3658. + system_rev = boardrev;
  3659. + system_serial_low = serial;
  3660. +
  3661. +#ifdef CONFIG_BCM2708_SPIDEV
  3662. + spi_register_board_info(bcm2708_spi_devices,
  3663. + ARRAY_SIZE(bcm2708_spi_devices));
  3664. +#endif
  3665. +}
  3666. +
  3667. +static void timer_set_mode(enum clock_event_mode mode,
  3668. + struct clock_event_device *clk)
  3669. +{
  3670. + switch (mode) {
  3671. + case CLOCK_EVT_MODE_ONESHOT: /* Leave the timer disabled, .set_next_event will enable it */
  3672. + case CLOCK_EVT_MODE_SHUTDOWN:
  3673. + break;
  3674. + case CLOCK_EVT_MODE_PERIODIC:
  3675. +
  3676. + case CLOCK_EVT_MODE_UNUSED:
  3677. + case CLOCK_EVT_MODE_RESUME:
  3678. +
  3679. + default:
  3680. + printk(KERN_ERR "timer_set_mode: unhandled mode:%d\n",
  3681. + (int)mode);
  3682. + break;
  3683. + }
  3684. +
  3685. +}
  3686. +
  3687. +static int timer_set_next_event(unsigned long cycles,
  3688. + struct clock_event_device *unused)
  3689. +{
  3690. + unsigned long stc;
  3691. +
  3692. + stc = readl(__io_address(ST_BASE + 0x04));
  3693. + writel(stc + cycles, __io_address(ST_BASE + 0x18)); /* stc3 */
  3694. + return 0;
  3695. +}
  3696. +
  3697. +static struct clock_event_device timer0_clockevent = {
  3698. + .name = "timer0",
  3699. + .shift = 32,
  3700. + .features = CLOCK_EVT_FEAT_ONESHOT,
  3701. + .set_mode = timer_set_mode,
  3702. + .set_next_event = timer_set_next_event,
  3703. +};
  3704. +
  3705. +/*
  3706. + * IRQ handler for the timer
  3707. + */
  3708. +static irqreturn_t bcm2708_timer_interrupt(int irq, void *dev_id)
  3709. +{
  3710. + struct clock_event_device *evt = &timer0_clockevent;
  3711. +
  3712. + writel(1 << 3, __io_address(ST_BASE + 0x00)); /* stcs clear timer int */
  3713. +
  3714. + evt->event_handler(evt);
  3715. +
  3716. + return IRQ_HANDLED;
  3717. +}
  3718. +
  3719. +static struct irqaction bcm2708_timer_irq = {
  3720. + .name = "BCM2708 Timer Tick",
  3721. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  3722. + .handler = bcm2708_timer_interrupt,
  3723. +};
  3724. +
  3725. +/*
  3726. + * Set up timer interrupt, and return the current time in seconds.
  3727. + */
  3728. +
  3729. +static struct delay_timer bcm2708_delay_timer = {
  3730. + .read_current_timer = bcm2708_read_current_timer,
  3731. + .freq = STC_FREQ_HZ,
  3732. +};
  3733. +
  3734. +static void __init bcm2708_timer_init(void)
  3735. +{
  3736. + /* init high res timer */
  3737. + bcm2708_clocksource_init();
  3738. +
  3739. + /*
  3740. + * Initialise to a known state (all timers off)
  3741. + */
  3742. + writel(0, __io_address(ARM_T_CONTROL));
  3743. + /*
  3744. + * Make irqs happen for the system timer
  3745. + */
  3746. + setup_irq(IRQ_TIMER3, &bcm2708_timer_irq);
  3747. +
  3748. + setup_sched_clock(bcm2708_read_sched_clock, 32, STC_FREQ_HZ);
  3749. +
  3750. + timer0_clockevent.mult =
  3751. + div_sc(STC_FREQ_HZ, NSEC_PER_SEC, timer0_clockevent.shift);
  3752. + timer0_clockevent.max_delta_ns =
  3753. + clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  3754. + timer0_clockevent.min_delta_ns =
  3755. + clockevent_delta2ns(0xf, &timer0_clockevent);
  3756. +
  3757. + timer0_clockevent.cpumask = cpumask_of(0);
  3758. + clockevents_register_device(&timer0_clockevent);
  3759. +
  3760. + register_current_timer_delay(&bcm2708_delay_timer);
  3761. +}
  3762. +
  3763. +#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
  3764. +#include <linux/leds.h>
  3765. +
  3766. +static struct gpio_led bcm2708_leds[] = {
  3767. + [0] = {
  3768. + .gpio = 16,
  3769. + .name = "led0",
  3770. + .default_trigger = "mmc0",
  3771. + .active_low = 1,
  3772. + },
  3773. +};
  3774. +
  3775. +static struct gpio_led_platform_data bcm2708_led_pdata = {
  3776. + .num_leds = ARRAY_SIZE(bcm2708_leds),
  3777. + .leds = bcm2708_leds,
  3778. +};
  3779. +
  3780. +static struct platform_device bcm2708_led_device = {
  3781. + .name = "leds-gpio",
  3782. + .id = -1,
  3783. + .dev = {
  3784. + .platform_data = &bcm2708_led_pdata,
  3785. + },
  3786. +};
  3787. +
  3788. +static void __init bcm2708_init_led(void)
  3789. +{
  3790. + bcm2708_leds[0].gpio = disk_led_gpio;
  3791. + bcm2708_leds[0].active_low = disk_led_active_low;
  3792. + platform_device_register(&bcm2708_led_device);
  3793. +}
  3794. +#else
  3795. +static inline void bcm2708_init_led(void)
  3796. +{
  3797. +}
  3798. +#endif
  3799. +
  3800. +void __init bcm2708_init_early(void)
  3801. +{
  3802. + /*
  3803. + * Some devices allocate their coherent buffers from atomic
  3804. + * context. Increase size of atomic coherent pool to make sure such
  3805. + * the allocations won't fail.
  3806. + */
  3807. + init_dma_coherent_pool_size(SZ_4M);
  3808. +}
  3809. +
  3810. +static void __init board_reserve(void)
  3811. +{
  3812. +#if defined(CONFIG_BCM_VC_CMA)
  3813. + vc_cma_reserve();
  3814. +#endif
  3815. +}
  3816. +
  3817. +MACHINE_START(BCM2708, "BCM2708")
  3818. + /* Maintainer: Broadcom Europe Ltd. */
  3819. + .map_io = bcm2708_map_io,
  3820. + .init_irq = bcm2708_init_irq,
  3821. + .init_time = bcm2708_timer_init,
  3822. + .init_machine = bcm2708_init,
  3823. + .init_early = bcm2708_init_early,
  3824. + .reserve = board_reserve,
  3825. + .restart = bcm2708_restart,
  3826. +MACHINE_END
  3827. +
  3828. +module_param(boardrev, uint, 0644);
  3829. +module_param(serial, uint, 0644);
  3830. +module_param(uart_clock, uint, 0644);
  3831. +module_param(disk_led_gpio, uint, 0644);
  3832. +module_param(disk_led_active_low, uint, 0644);
  3833. +module_param(reboot_part, uint, 0644);
  3834. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/bcm2708_gpio.c linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708_gpio.c
  3835. --- linux-3.13.6/arch/arm/mach-bcm2708/bcm2708_gpio.c 1970-01-01 01:00:00.000000000 +0100
  3836. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708_gpio.c 2014-03-11 16:51:54.000000000 +0100
  3837. @@ -0,0 +1,361 @@
  3838. +/*
  3839. + * linux/arch/arm/mach-bcm2708/bcm2708_gpio.c
  3840. + *
  3841. + * Copyright (C) 2010 Broadcom
  3842. + *
  3843. + * This program is free software; you can redistribute it and/or modify
  3844. + * it under the terms of the GNU General Public License version 2 as
  3845. + * published by the Free Software Foundation.
  3846. + *
  3847. + */
  3848. +
  3849. +#include <linux/spinlock.h>
  3850. +#include <linux/module.h>
  3851. +#include <linux/list.h>
  3852. +#include <linux/io.h>
  3853. +#include <linux/irq.h>
  3854. +#include <linux/interrupt.h>
  3855. +#include <linux/slab.h>
  3856. +#include <mach/gpio.h>
  3857. +#include <linux/gpio.h>
  3858. +#include <linux/platform_device.h>
  3859. +#include <mach/platform.h>
  3860. +
  3861. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  3862. +#define DRIVER_NAME BCM_GPIO_DRIVER_NAME
  3863. +#define BCM_GPIO_USE_IRQ 1
  3864. +
  3865. +#define GPIOFSEL(x) (0x00+(x)*4)
  3866. +#define GPIOSET(x) (0x1c+(x)*4)
  3867. +#define GPIOCLR(x) (0x28+(x)*4)
  3868. +#define GPIOLEV(x) (0x34+(x)*4)
  3869. +#define GPIOEDS(x) (0x40+(x)*4)
  3870. +#define GPIOREN(x) (0x4c+(x)*4)
  3871. +#define GPIOFEN(x) (0x58+(x)*4)
  3872. +#define GPIOHEN(x) (0x64+(x)*4)
  3873. +#define GPIOLEN(x) (0x70+(x)*4)
  3874. +#define GPIOAREN(x) (0x7c+(x)*4)
  3875. +#define GPIOAFEN(x) (0x88+(x)*4)
  3876. +#define GPIOUD(x) (0x94+(x)*4)
  3877. +#define GPIOUDCLK(x) (0x98+(x)*4)
  3878. +
  3879. +enum { GPIO_FSEL_INPUT, GPIO_FSEL_OUTPUT,
  3880. + GPIO_FSEL_ALT5, GPIO_FSEL_ALT_4,
  3881. + GPIO_FSEL_ALT0, GPIO_FSEL_ALT1,
  3882. + GPIO_FSEL_ALT2, GPIO_FSEL_ALT3,
  3883. +};
  3884. +
  3885. + /* Each of the two spinlocks protects a different set of hardware
  3886. + * regiters and data structurs. This decouples the code of the IRQ from
  3887. + * the GPIO code. This also makes the case of a GPIO routine call from
  3888. + * the IRQ code simpler.
  3889. + */
  3890. +static DEFINE_SPINLOCK(lock); /* GPIO registers */
  3891. +
  3892. +struct bcm2708_gpio {
  3893. + struct list_head list;
  3894. + void __iomem *base;
  3895. + struct gpio_chip gc;
  3896. + unsigned long rising;
  3897. + unsigned long falling;
  3898. + unsigned long high;
  3899. + unsigned long low;
  3900. +};
  3901. +
  3902. +static int bcm2708_set_function(struct gpio_chip *gc, unsigned offset,
  3903. + int function)
  3904. +{
  3905. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3906. + unsigned long flags;
  3907. + unsigned gpiodir;
  3908. + unsigned gpio_bank = offset / 10;
  3909. + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
  3910. +
  3911. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set_function %p (%d,%d)\n", gc, offset, function);
  3912. + if (offset >= ARCH_NR_GPIOS)
  3913. + return -EINVAL;
  3914. +
  3915. + spin_lock_irqsave(&lock, flags);
  3916. +
  3917. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  3918. + gpiodir &= ~(7 << gpio_field_offset);
  3919. + gpiodir |= function << gpio_field_offset;
  3920. + writel(gpiodir, gpio->base + GPIOFSEL(gpio_bank));
  3921. + spin_unlock_irqrestore(&lock, flags);
  3922. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  3923. +
  3924. + return 0;
  3925. +}
  3926. +
  3927. +static int bcm2708_gpio_dir_in(struct gpio_chip *gc, unsigned offset)
  3928. +{
  3929. + return bcm2708_set_function(gc, offset, GPIO_FSEL_INPUT);
  3930. +}
  3931. +
  3932. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
  3933. +static int bcm2708_gpio_dir_out(struct gpio_chip *gc, unsigned offset,
  3934. + int value)
  3935. +{
  3936. + int ret;
  3937. + ret = bcm2708_set_function(gc, offset, GPIO_FSEL_OUTPUT);
  3938. + if (ret >= 0)
  3939. + bcm2708_gpio_set(gc, offset, value);
  3940. + return ret;
  3941. +}
  3942. +
  3943. +static int bcm2708_gpio_get(struct gpio_chip *gc, unsigned offset)
  3944. +{
  3945. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3946. + unsigned gpio_bank = offset / 32;
  3947. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  3948. + unsigned lev;
  3949. +
  3950. + if (offset >= ARCH_NR_GPIOS)
  3951. + return 0;
  3952. + lev = readl(gpio->base + GPIOLEV(gpio_bank));
  3953. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_get %p (%d)=%d\n", gc, offset, 0x1 & (lev>>gpio_field_offset));
  3954. + return 0x1 & (lev >> gpio_field_offset);
  3955. +}
  3956. +
  3957. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  3958. +{
  3959. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  3960. + unsigned gpio_bank = offset / 32;
  3961. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  3962. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set %p (%d=%d)\n", gc, offset, value);
  3963. + if (offset >= ARCH_NR_GPIOS)
  3964. + return;
  3965. + if (value)
  3966. + writel(1 << gpio_field_offset, gpio->base + GPIOSET(gpio_bank));
  3967. + else
  3968. + writel(1 << gpio_field_offset, gpio->base + GPIOCLR(gpio_bank));
  3969. +}
  3970. +
  3971. +/*************************************************************************************************************************
  3972. + * bcm2708 GPIO IRQ
  3973. + */
  3974. +
  3975. +#if BCM_GPIO_USE_IRQ
  3976. +
  3977. +static int bcm2708_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  3978. +{
  3979. + return gpio_to_irq(gpio);
  3980. +}
  3981. +
  3982. +static int bcm2708_gpio_irq_set_type(struct irq_data *d, unsigned type)
  3983. +{
  3984. + unsigned irq = d->irq;
  3985. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  3986. +
  3987. + gpio->rising &= ~(1 << irq_to_gpio(irq));
  3988. + gpio->falling &= ~(1 << irq_to_gpio(irq));
  3989. + gpio->high &= ~(1 << irq_to_gpio(irq));
  3990. + gpio->low &= ~(1 << irq_to_gpio(irq));
  3991. +
  3992. + if (type & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  3993. + return -EINVAL;
  3994. +
  3995. + if (type & IRQ_TYPE_EDGE_RISING)
  3996. + gpio->rising |= (1 << irq_to_gpio(irq));
  3997. + if (type & IRQ_TYPE_EDGE_FALLING)
  3998. + gpio->falling |= (1 << irq_to_gpio(irq));
  3999. + if (type & IRQ_TYPE_LEVEL_HIGH)
  4000. + gpio->high |= (1 << irq_to_gpio(irq));
  4001. + if (type & IRQ_TYPE_LEVEL_LOW)
  4002. + gpio->low |= (1 << irq_to_gpio(irq));
  4003. + return 0;
  4004. +}
  4005. +
  4006. +static void bcm2708_gpio_irq_mask(struct irq_data *d)
  4007. +{
  4008. + unsigned irq = d->irq;
  4009. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  4010. + unsigned gn = irq_to_gpio(irq);
  4011. + unsigned gb = gn / 32;
  4012. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  4013. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  4014. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  4015. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  4016. +
  4017. + gn = gn % 32;
  4018. +
  4019. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  4020. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  4021. + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
  4022. + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
  4023. +}
  4024. +
  4025. +static void bcm2708_gpio_irq_unmask(struct irq_data *d)
  4026. +{
  4027. + unsigned irq = d->irq;
  4028. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  4029. + unsigned gn = irq_to_gpio(irq);
  4030. + unsigned gb = gn / 32;
  4031. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  4032. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  4033. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  4034. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  4035. +
  4036. + gn = gn % 32;
  4037. +
  4038. + writel(1 << gn, gpio->base + GPIOEDS(gb));
  4039. +
  4040. + if (gpio->rising & (1 << gn)) {
  4041. + writel(rising | (1 << gn), gpio->base + GPIOREN(gb));
  4042. + } else {
  4043. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  4044. + }
  4045. +
  4046. + if (gpio->falling & (1 << gn)) {
  4047. + writel(falling | (1 << gn), gpio->base + GPIOFEN(gb));
  4048. + } else {
  4049. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  4050. + }
  4051. +
  4052. + if (gpio->high & (1 << gn)) {
  4053. + writel(high | (1 << gn), gpio->base + GPIOHEN(gb));
  4054. + } else {
  4055. + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
  4056. + }
  4057. +
  4058. + if (gpio->low & (1 << gn)) {
  4059. + writel(low | (1 << gn), gpio->base + GPIOLEN(gb));
  4060. + } else {
  4061. + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
  4062. + }
  4063. +}
  4064. +
  4065. +static struct irq_chip bcm2708_irqchip = {
  4066. + .name = "GPIO",
  4067. + .irq_enable = bcm2708_gpio_irq_unmask,
  4068. + .irq_disable = bcm2708_gpio_irq_mask,
  4069. + .irq_unmask = bcm2708_gpio_irq_unmask,
  4070. + .irq_mask = bcm2708_gpio_irq_mask,
  4071. + .irq_set_type = bcm2708_gpio_irq_set_type,
  4072. +};
  4073. +
  4074. +static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
  4075. +{
  4076. + unsigned long edsr;
  4077. + unsigned bank;
  4078. + int i;
  4079. + unsigned gpio;
  4080. + for (bank = 0; bank <= 1; bank++) {
  4081. + edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
  4082. + for_each_set_bit(i, &edsr, 32) {
  4083. + gpio = i + bank * 32;
  4084. + generic_handle_irq(gpio_to_irq(gpio));
  4085. + }
  4086. + writel(0xffffffff, __io_address(GPIO_BASE) + GPIOEDS(bank));
  4087. + }
  4088. + return IRQ_HANDLED;
  4089. +}
  4090. +
  4091. +static struct irqaction bcm2708_gpio_irq = {
  4092. + .name = "BCM2708 GPIO catchall handler",
  4093. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  4094. + .handler = bcm2708_gpio_interrupt,
  4095. +};
  4096. +
  4097. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  4098. +{
  4099. + unsigned irq;
  4100. +
  4101. + ucb->gc.to_irq = bcm2708_gpio_to_irq;
  4102. +
  4103. + for (irq = GPIO_IRQ_START; irq < (GPIO_IRQ_START + GPIO_IRQS); irq++) {
  4104. + irq_set_chip_data(irq, ucb);
  4105. + irq_set_chip(irq, &bcm2708_irqchip);
  4106. + set_irq_flags(irq, IRQF_VALID);
  4107. + }
  4108. + setup_irq(IRQ_GPIO3, &bcm2708_gpio_irq);
  4109. +}
  4110. +
  4111. +#else
  4112. +
  4113. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  4114. +{
  4115. +}
  4116. +
  4117. +#endif /* #if BCM_GPIO_USE_IRQ ***************************************************************************************************************** */
  4118. +
  4119. +static int bcm2708_gpio_probe(struct platform_device *dev)
  4120. +{
  4121. + struct bcm2708_gpio *ucb;
  4122. + struct resource *res;
  4123. + int err = 0;
  4124. +
  4125. + printk(KERN_INFO DRIVER_NAME ": bcm2708_gpio_probe %p\n", dev);
  4126. +
  4127. + ucb = kzalloc(sizeof(*ucb), GFP_KERNEL);
  4128. + if (NULL == ucb) {
  4129. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  4130. + "mailbox memory\n");
  4131. + err = -ENOMEM;
  4132. + goto err;
  4133. + }
  4134. +
  4135. + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  4136. +
  4137. + platform_set_drvdata(dev, ucb);
  4138. + ucb->base = __io_address(GPIO_BASE);
  4139. +
  4140. + ucb->gc.label = "bcm2708_gpio";
  4141. + ucb->gc.base = 0;
  4142. + ucb->gc.ngpio = ARCH_NR_GPIOS;
  4143. + ucb->gc.owner = THIS_MODULE;
  4144. +
  4145. + ucb->gc.direction_input = bcm2708_gpio_dir_in;
  4146. + ucb->gc.direction_output = bcm2708_gpio_dir_out;
  4147. + ucb->gc.get = bcm2708_gpio_get;
  4148. + ucb->gc.set = bcm2708_gpio_set;
  4149. + ucb->gc.can_sleep = 0;
  4150. +
  4151. + bcm2708_gpio_irq_init(ucb);
  4152. +
  4153. + err = gpiochip_add(&ucb->gc);
  4154. + if (err)
  4155. + goto err;
  4156. +
  4157. +err:
  4158. + return err;
  4159. +
  4160. +}
  4161. +
  4162. +static int bcm2708_gpio_remove(struct platform_device *dev)
  4163. +{
  4164. + int err = 0;
  4165. + struct bcm2708_gpio *ucb = platform_get_drvdata(dev);
  4166. +
  4167. + printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_remove %p\n", dev);
  4168. +
  4169. + err = gpiochip_remove(&ucb->gc);
  4170. +
  4171. + platform_set_drvdata(dev, NULL);
  4172. + kfree(ucb);
  4173. +
  4174. + return err;
  4175. +}
  4176. +
  4177. +static struct platform_driver bcm2708_gpio_driver = {
  4178. + .probe = bcm2708_gpio_probe,
  4179. + .remove = bcm2708_gpio_remove,
  4180. + .driver = {
  4181. + .name = "bcm2708_gpio"},
  4182. +};
  4183. +
  4184. +static int __init bcm2708_gpio_init(void)
  4185. +{
  4186. + return platform_driver_register(&bcm2708_gpio_driver);
  4187. +}
  4188. +
  4189. +static void __exit bcm2708_gpio_exit(void)
  4190. +{
  4191. + platform_driver_unregister(&bcm2708_gpio_driver);
  4192. +}
  4193. +
  4194. +module_init(bcm2708_gpio_init);
  4195. +module_exit(bcm2708_gpio_exit);
  4196. +
  4197. +MODULE_DESCRIPTION("Broadcom BCM2708 GPIO driver");
  4198. +MODULE_LICENSE("GPL");
  4199. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/bcm2708.h linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708.h
  4200. --- linux-3.13.6/arch/arm/mach-bcm2708/bcm2708.h 1970-01-01 01:00:00.000000000 +0100
  4201. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/bcm2708.h 2014-03-11 16:54:55.000000000 +0100
  4202. @@ -0,0 +1,49 @@
  4203. +/*
  4204. + * linux/arch/arm/mach-bcm2708/bcm2708.h
  4205. + *
  4206. + * BCM2708 machine support header
  4207. + *
  4208. + * Copyright (C) 2010 Broadcom
  4209. + *
  4210. + * This program is free software; you can redistribute it and/or modify
  4211. + * it under the terms of the GNU General Public License as published by
  4212. + * the Free Software Foundation; either version 2 of the License, or
  4213. + * (at your option) any later version.
  4214. + *
  4215. + * This program is distributed in the hope that it will be useful,
  4216. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4217. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4218. + * GNU General Public License for more details.
  4219. + *
  4220. + * You should have received a copy of the GNU General Public License
  4221. + * along with this program; if not, write to the Free Software
  4222. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4223. + */
  4224. +
  4225. +#ifndef __BCM2708_BCM2708_H
  4226. +#define __BCM2708_BCM2708_H
  4227. +
  4228. +#include <linux/amba/bus.h>
  4229. +
  4230. +extern void __init bcm2708_init(void);
  4231. +extern void __init bcm2708_init_irq(void);
  4232. +extern void __init bcm2708_map_io(void);
  4233. +extern struct sys_timer bcm2708_timer;
  4234. +extern unsigned int mmc_status(struct device *dev);
  4235. +
  4236. +#define AMBA_DEVICE(name, busid, base, plat) \
  4237. +static struct amba_device name##_device = { \
  4238. + .dev = { \
  4239. + .coherent_dma_mask = ~0, \
  4240. + .init_name = busid, \
  4241. + .platform_data = plat, \
  4242. + }, \
  4243. + .res = { \
  4244. + .start = base##_BASE, \
  4245. + .end = (base##_BASE) + SZ_4K - 1,\
  4246. + .flags = IORESOURCE_MEM, \
  4247. + }, \
  4248. + .irq = base##_IRQ, \
  4249. +}
  4250. +
  4251. +#endif
  4252. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/clock.c linux-raspberry-pi/arch/arm/mach-bcm2708/clock.c
  4253. --- linux-3.13.6/arch/arm/mach-bcm2708/clock.c 1970-01-01 01:00:00.000000000 +0100
  4254. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/clock.c 2014-03-11 16:51:54.000000000 +0100
  4255. @@ -0,0 +1,61 @@
  4256. +/*
  4257. + * linux/arch/arm/mach-bcm2708/clock.c
  4258. + *
  4259. + * Copyright (C) 2010 Broadcom
  4260. + *
  4261. + * This program is free software; you can redistribute it and/or modify
  4262. + * it under the terms of the GNU General Public License as published by
  4263. + * the Free Software Foundation; either version 2 of the License, or
  4264. + * (at your option) any later version.
  4265. + *
  4266. + * This program is distributed in the hope that it will be useful,
  4267. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4268. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4269. + * GNU General Public License for more details.
  4270. + *
  4271. + * You should have received a copy of the GNU General Public License
  4272. + * along with this program; if not, write to the Free Software
  4273. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4274. + */
  4275. +#include <linux/module.h>
  4276. +#include <linux/kernel.h>
  4277. +#include <linux/device.h>
  4278. +#include <linux/list.h>
  4279. +#include <linux/errno.h>
  4280. +#include <linux/err.h>
  4281. +#include <linux/string.h>
  4282. +#include <linux/clk.h>
  4283. +#include <linux/mutex.h>
  4284. +
  4285. +#include <asm/clkdev.h>
  4286. +
  4287. +#include "clock.h"
  4288. +
  4289. +int clk_enable(struct clk *clk)
  4290. +{
  4291. + return 0;
  4292. +}
  4293. +EXPORT_SYMBOL(clk_enable);
  4294. +
  4295. +void clk_disable(struct clk *clk)
  4296. +{
  4297. +}
  4298. +EXPORT_SYMBOL(clk_disable);
  4299. +
  4300. +unsigned long clk_get_rate(struct clk *clk)
  4301. +{
  4302. + return clk->rate;
  4303. +}
  4304. +EXPORT_SYMBOL(clk_get_rate);
  4305. +
  4306. +long clk_round_rate(struct clk *clk, unsigned long rate)
  4307. +{
  4308. + return clk->rate;
  4309. +}
  4310. +EXPORT_SYMBOL(clk_round_rate);
  4311. +
  4312. +int clk_set_rate(struct clk *clk, unsigned long rate)
  4313. +{
  4314. + return -EIO;
  4315. +}
  4316. +EXPORT_SYMBOL(clk_set_rate);
  4317. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/clock.h linux-raspberry-pi/arch/arm/mach-bcm2708/clock.h
  4318. --- linux-3.13.6/arch/arm/mach-bcm2708/clock.h 1970-01-01 01:00:00.000000000 +0100
  4319. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/clock.h 2014-03-11 16:51:54.000000000 +0100
  4320. @@ -0,0 +1,24 @@
  4321. +/*
  4322. + * linux/arch/arm/mach-bcm2708/clock.h
  4323. + *
  4324. + * Copyright (C) 2010 Broadcom
  4325. + *
  4326. + * This program is free software; you can redistribute it and/or modify
  4327. + * it under the terms of the GNU General Public License as published by
  4328. + * the Free Software Foundation; either version 2 of the License, or
  4329. + * (at your option) any later version.
  4330. + *
  4331. + * This program is distributed in the hope that it will be useful,
  4332. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4333. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4334. + * GNU General Public License for more details.
  4335. + *
  4336. + * You should have received a copy of the GNU General Public License
  4337. + * along with this program; if not, write to the Free Software
  4338. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4339. + */
  4340. +struct module;
  4341. +
  4342. +struct clk {
  4343. + unsigned long rate;
  4344. +};
  4345. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/dma.c linux-raspberry-pi/arch/arm/mach-bcm2708/dma.c
  4346. --- linux-3.13.6/arch/arm/mach-bcm2708/dma.c 1970-01-01 01:00:00.000000000 +0100
  4347. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/dma.c 2014-03-11 16:51:54.000000000 +0100
  4348. @@ -0,0 +1,407 @@
  4349. +/*
  4350. + * linux/arch/arm/mach-bcm2708/dma.c
  4351. + *
  4352. + * Copyright (C) 2010 Broadcom
  4353. + *
  4354. + * This program is free software; you can redistribute it and/or modify
  4355. + * it under the terms of the GNU General Public License version 2 as
  4356. + * published by the Free Software Foundation.
  4357. + */
  4358. +
  4359. +#include <linux/slab.h>
  4360. +#include <linux/device.h>
  4361. +#include <linux/platform_device.h>
  4362. +#include <linux/module.h>
  4363. +#include <linux/scatterlist.h>
  4364. +
  4365. +#include <mach/dma.h>
  4366. +#include <mach/irqs.h>
  4367. +
  4368. +/*****************************************************************************\
  4369. + * *
  4370. + * Configuration *
  4371. + * *
  4372. +\*****************************************************************************/
  4373. +
  4374. +#define CACHE_LINE_MASK 31
  4375. +#define DRIVER_NAME BCM_DMAMAN_DRIVER_NAME
  4376. +#define DEFAULT_DMACHAN_BITMAP 0x10 /* channel 4 only */
  4377. +
  4378. +/* valid only for channels 0 - 14, 15 has its own base address */
  4379. +#define BCM2708_DMA_CHAN(n) ((n)<<8) /* base address */
  4380. +#define BCM2708_DMA_CHANIO(dma_base, n) \
  4381. + ((void __iomem *)((char *)(dma_base)+BCM2708_DMA_CHAN(n)))
  4382. +
  4383. +
  4384. +/*****************************************************************************\
  4385. + * *
  4386. + * DMA Auxilliary Functions *
  4387. + * *
  4388. +\*****************************************************************************/
  4389. +
  4390. +/* A DMA buffer on an arbitrary boundary may separate a cache line into a
  4391. + section inside the DMA buffer and another section outside it.
  4392. + Even if we flush DMA buffers from the cache there is always the chance that
  4393. + during a DMA someone will access the part of a cache line that is outside
  4394. + the DMA buffer - which will then bring in unwelcome data.
  4395. + Without being able to dictate our own buffer pools we must insist that
  4396. + DMA buffers consist of a whole number of cache lines.
  4397. +*/
  4398. +
  4399. +extern int
  4400. +bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len)
  4401. +{
  4402. + int i;
  4403. +
  4404. + for (i = 0; i < sg_len; i++) {
  4405. + if (sg_ptr[i].offset & CACHE_LINE_MASK ||
  4406. + sg_ptr[i].length & CACHE_LINE_MASK)
  4407. + return 0;
  4408. + }
  4409. +
  4410. + return 1;
  4411. +}
  4412. +EXPORT_SYMBOL_GPL(bcm_sg_suitable_for_dma);
  4413. +
  4414. +extern void
  4415. +bcm_dma_start(void __iomem *dma_chan_base, dma_addr_t control_block)
  4416. +{
  4417. + dsb(); /* ARM data synchronization (push) operation */
  4418. +
  4419. + writel(control_block, dma_chan_base + BCM2708_DMA_ADDR);
  4420. + writel(BCM2708_DMA_ACTIVE, dma_chan_base + BCM2708_DMA_CS);
  4421. +}
  4422. +
  4423. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base)
  4424. +{
  4425. + dsb();
  4426. +
  4427. + /* ugly busy wait only option for now */
  4428. + while (readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE)
  4429. + cpu_relax();
  4430. +}
  4431. +
  4432. +EXPORT_SYMBOL_GPL(bcm_dma_start);
  4433. +
  4434. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base)
  4435. +{
  4436. + dsb();
  4437. +
  4438. + return readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE;
  4439. +}
  4440. +EXPORT_SYMBOL_GPL(bcm_dma_is_busy);
  4441. +
  4442. +/* Complete an ongoing DMA (assuming its results are to be ignored)
  4443. + Does nothing if there is no DMA in progress.
  4444. + This routine waits for the current AXI transfer to complete before
  4445. + terminating the current DMA. If the current transfer is hung on a DREQ used
  4446. + by an uncooperative peripheral the AXI transfer may never complete. In this
  4447. + case the routine times out and return a non-zero error code.
  4448. + Use of this routine doesn't guarantee that the ongoing or aborted DMA
  4449. + does not produce an interrupt.
  4450. +*/
  4451. +extern int
  4452. +bcm_dma_abort(void __iomem *dma_chan_base)
  4453. +{
  4454. + unsigned long int cs;
  4455. + int rc = 0;
  4456. +
  4457. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  4458. +
  4459. + if (BCM2708_DMA_ACTIVE & cs) {
  4460. + long int timeout = 10000;
  4461. +
  4462. + /* write 0 to the active bit - pause the DMA */
  4463. + writel(0, dma_chan_base + BCM2708_DMA_CS);
  4464. +
  4465. + /* wait for any current AXI transfer to complete */
  4466. + while (0 != (cs & BCM2708_DMA_ISPAUSED) && --timeout >= 0)
  4467. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  4468. +
  4469. + if (0 != (cs & BCM2708_DMA_ISPAUSED)) {
  4470. + /* we'll un-pause when we set of our next DMA */
  4471. + rc = -ETIMEDOUT;
  4472. +
  4473. + } else if (BCM2708_DMA_ACTIVE & cs) {
  4474. + /* terminate the control block chain */
  4475. + writel(0, dma_chan_base + BCM2708_DMA_NEXTCB);
  4476. +
  4477. + /* abort the whole DMA */
  4478. + writel(BCM2708_DMA_ABORT | BCM2708_DMA_ACTIVE,
  4479. + dma_chan_base + BCM2708_DMA_CS);
  4480. + }
  4481. + }
  4482. +
  4483. + return rc;
  4484. +}
  4485. +EXPORT_SYMBOL_GPL(bcm_dma_abort);
  4486. +
  4487. +
  4488. +/***************************************************************************** \
  4489. + * *
  4490. + * DMA Manager Device Methods *
  4491. + * *
  4492. +\*****************************************************************************/
  4493. +
  4494. +struct vc_dmaman {
  4495. + void __iomem *dma_base;
  4496. + u32 chan_available; /* bitmap of available channels */
  4497. + u32 has_feature[BCM_DMA_FEATURE_COUNT]; /* bitmap of feature presence */
  4498. +};
  4499. +
  4500. +static void vc_dmaman_init(struct vc_dmaman *dmaman, void __iomem *dma_base,
  4501. + u32 chans_available)
  4502. +{
  4503. + dmaman->dma_base = dma_base;
  4504. + dmaman->chan_available = chans_available;
  4505. + dmaman->has_feature[BCM_DMA_FEATURE_FAST_ORD] = 0x0c; /* chans 2 & 3 */
  4506. + dmaman->has_feature[BCM_DMA_FEATURE_BULK_ORD] = 0x01; /* chan 0 */
  4507. +}
  4508. +
  4509. +static int vc_dmaman_chan_alloc(struct vc_dmaman *dmaman,
  4510. + unsigned preferred_feature_set)
  4511. +{
  4512. + u32 chans;
  4513. + int feature;
  4514. +
  4515. + chans = dmaman->chan_available;
  4516. + for (feature = 0; feature < BCM_DMA_FEATURE_COUNT; feature++)
  4517. + /* select the subset of available channels with the desired
  4518. + feature so long as some of the candidate channels have that
  4519. + feature */
  4520. + if ((preferred_feature_set & (1 << feature)) &&
  4521. + (chans & dmaman->has_feature[feature]))
  4522. + chans &= dmaman->has_feature[feature];
  4523. +
  4524. + if (chans) {
  4525. + int chan = 0;
  4526. + /* return the ordinal of the first channel in the bitmap */
  4527. + while (chans != 0 && (chans & 1) == 0) {
  4528. + chans >>= 1;
  4529. + chan++;
  4530. + }
  4531. + /* claim the channel */
  4532. + dmaman->chan_available &= ~(1 << chan);
  4533. + return chan;
  4534. + } else
  4535. + return -ENOMEM;
  4536. +}
  4537. +
  4538. +static int vc_dmaman_chan_free(struct vc_dmaman *dmaman, int chan)
  4539. +{
  4540. + if (chan < 0)
  4541. + return -EINVAL;
  4542. + else if ((1 << chan) & dmaman->chan_available)
  4543. + return -EIDRM;
  4544. + else {
  4545. + dmaman->chan_available |= (1 << chan);
  4546. + return 0;
  4547. + }
  4548. +}
  4549. +
  4550. +/*****************************************************************************\
  4551. + * *
  4552. + * DMA IRQs *
  4553. + * *
  4554. +\*****************************************************************************/
  4555. +
  4556. +static unsigned char bcm_dma_irqs[] = {
  4557. + IRQ_DMA0,
  4558. + IRQ_DMA1,
  4559. + IRQ_DMA2,
  4560. + IRQ_DMA3,
  4561. + IRQ_DMA4,
  4562. + IRQ_DMA5,
  4563. + IRQ_DMA6,
  4564. + IRQ_DMA7,
  4565. + IRQ_DMA8,
  4566. + IRQ_DMA9,
  4567. + IRQ_DMA10,
  4568. + IRQ_DMA11,
  4569. + IRQ_DMA12
  4570. +};
  4571. +
  4572. +
  4573. +/***************************************************************************** \
  4574. + * *
  4575. + * DMA Manager Monitor *
  4576. + * *
  4577. +\*****************************************************************************/
  4578. +
  4579. +static struct device *dmaman_dev; /* we assume there's only one! */
  4580. +
  4581. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  4582. + void __iomem **out_dma_base, int *out_dma_irq)
  4583. +{
  4584. + if (!dmaman_dev)
  4585. + return -ENODEV;
  4586. + else {
  4587. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  4588. + int rc;
  4589. +
  4590. + device_lock(dmaman_dev);
  4591. + rc = vc_dmaman_chan_alloc(dmaman, preferred_feature_set);
  4592. + if (rc >= 0) {
  4593. + *out_dma_base = BCM2708_DMA_CHANIO(dmaman->dma_base,
  4594. + rc);
  4595. + *out_dma_irq = bcm_dma_irqs[rc];
  4596. + }
  4597. + device_unlock(dmaman_dev);
  4598. +
  4599. + return rc;
  4600. + }
  4601. +}
  4602. +EXPORT_SYMBOL_GPL(bcm_dma_chan_alloc);
  4603. +
  4604. +extern int bcm_dma_chan_free(int channel)
  4605. +{
  4606. + if (dmaman_dev) {
  4607. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  4608. + int rc;
  4609. +
  4610. + device_lock(dmaman_dev);
  4611. + rc = vc_dmaman_chan_free(dmaman, channel);
  4612. + device_unlock(dmaman_dev);
  4613. +
  4614. + return rc;
  4615. + } else
  4616. + return -ENODEV;
  4617. +}
  4618. +EXPORT_SYMBOL_GPL(bcm_dma_chan_free);
  4619. +
  4620. +static int dev_dmaman_register(const char *dev_name, struct device *dev)
  4621. +{
  4622. + int rc = dmaman_dev ? -EINVAL : 0;
  4623. + dmaman_dev = dev;
  4624. + return rc;
  4625. +}
  4626. +
  4627. +static void dev_dmaman_deregister(const char *dev_name, struct device *dev)
  4628. +{
  4629. + dmaman_dev = NULL;
  4630. +}
  4631. +
  4632. +/*****************************************************************************\
  4633. + * *
  4634. + * DMA Device *
  4635. + * *
  4636. +\*****************************************************************************/
  4637. +
  4638. +static int dmachans = -1; /* module parameter */
  4639. +
  4640. +static int bcm_dmaman_probe(struct platform_device *pdev)
  4641. +{
  4642. + int ret = 0;
  4643. + struct vc_dmaman *dmaman;
  4644. + struct resource *dma_res = NULL;
  4645. + void __iomem *dma_base = NULL;
  4646. + int have_dma_region = 0;
  4647. +
  4648. + dmaman = kzalloc(sizeof(*dmaman), GFP_KERNEL);
  4649. + if (NULL == dmaman) {
  4650. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  4651. + "DMA management memory\n");
  4652. + ret = -ENOMEM;
  4653. + } else {
  4654. +
  4655. + dma_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4656. + if (dma_res == NULL) {
  4657. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  4658. + "resource\n");
  4659. + ret = -ENODEV;
  4660. + } else if (!request_mem_region(dma_res->start,
  4661. + resource_size(dma_res),
  4662. + DRIVER_NAME)) {
  4663. + dev_err(&pdev->dev, "cannot obtain DMA region\n");
  4664. + ret = -EBUSY;
  4665. + } else {
  4666. + have_dma_region = 1;
  4667. + dma_base = ioremap(dma_res->start,
  4668. + resource_size(dma_res));
  4669. + if (!dma_base) {
  4670. + dev_err(&pdev->dev, "cannot map DMA region\n");
  4671. + ret = -ENOMEM;
  4672. + } else {
  4673. + /* use module parameter if one was provided */
  4674. + if (dmachans > 0)
  4675. + vc_dmaman_init(dmaman, dma_base,
  4676. + dmachans);
  4677. + else
  4678. + vc_dmaman_init(dmaman, dma_base,
  4679. + DEFAULT_DMACHAN_BITMAP);
  4680. +
  4681. + platform_set_drvdata(pdev, dmaman);
  4682. + dev_dmaman_register(DRIVER_NAME, &pdev->dev);
  4683. +
  4684. + printk(KERN_INFO DRIVER_NAME ": DMA manager "
  4685. + "at %p\n", dma_base);
  4686. + }
  4687. + }
  4688. + }
  4689. + if (ret != 0) {
  4690. + if (dma_base)
  4691. + iounmap(dma_base);
  4692. + if (dma_res && have_dma_region)
  4693. + release_mem_region(dma_res->start,
  4694. + resource_size(dma_res));
  4695. + if (dmaman)
  4696. + kfree(dmaman);
  4697. + }
  4698. + return ret;
  4699. +}
  4700. +
  4701. +static int bcm_dmaman_remove(struct platform_device *pdev)
  4702. +{
  4703. + struct vc_dmaman *dmaman = platform_get_drvdata(pdev);
  4704. +
  4705. + platform_set_drvdata(pdev, NULL);
  4706. + dev_dmaman_deregister(DRIVER_NAME, &pdev->dev);
  4707. + kfree(dmaman);
  4708. +
  4709. + return 0;
  4710. +}
  4711. +
  4712. +static struct platform_driver bcm_dmaman_driver = {
  4713. + .probe = bcm_dmaman_probe,
  4714. + .remove = bcm_dmaman_remove,
  4715. +
  4716. + .driver = {
  4717. + .name = DRIVER_NAME,
  4718. + .owner = THIS_MODULE,
  4719. + },
  4720. +};
  4721. +
  4722. +/*****************************************************************************\
  4723. + * *
  4724. + * Driver init/exit *
  4725. + * *
  4726. +\*****************************************************************************/
  4727. +
  4728. +static int __init bcm_dmaman_drv_init(void)
  4729. +{
  4730. + int ret;
  4731. +
  4732. + ret = platform_driver_register(&bcm_dmaman_driver);
  4733. + if (ret != 0) {
  4734. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  4735. + "on platform\n");
  4736. + }
  4737. +
  4738. + return ret;
  4739. +}
  4740. +
  4741. +static void __exit bcm_dmaman_drv_exit(void)
  4742. +{
  4743. + platform_driver_unregister(&bcm_dmaman_driver);
  4744. +}
  4745. +
  4746. +module_init(bcm_dmaman_drv_init);
  4747. +module_exit(bcm_dmaman_drv_exit);
  4748. +
  4749. +module_param(dmachans, int, 0644);
  4750. +
  4751. +MODULE_AUTHOR("Gray Girling <grayg@broadcom.com>");
  4752. +MODULE_DESCRIPTION("DMA channel manager driver");
  4753. +MODULE_LICENSE("GPL");
  4754. +
  4755. +MODULE_PARM_DESC(dmachans, "Bitmap of DMA channels available to the ARM");
  4756. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/arm_control.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/arm_control.h
  4757. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/arm_control.h 1970-01-01 01:00:00.000000000 +0100
  4758. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/arm_control.h 2014-03-11 16:51:54.000000000 +0100
  4759. @@ -0,0 +1,419 @@
  4760. +/*
  4761. + * linux/arch/arm/mach-bcm2708/arm_control.h
  4762. + *
  4763. + * Copyright (C) 2010 Broadcom
  4764. + *
  4765. + * This program is free software; you can redistribute it and/or modify
  4766. + * it under the terms of the GNU General Public License as published by
  4767. + * the Free Software Foundation; either version 2 of the License, or
  4768. + * (at your option) any later version.
  4769. + *
  4770. + * This program is distributed in the hope that it will be useful,
  4771. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4772. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4773. + * GNU General Public License for more details.
  4774. + *
  4775. + * You should have received a copy of the GNU General Public License
  4776. + * along with this program; if not, write to the Free Software
  4777. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4778. + */
  4779. +
  4780. +#ifndef __BCM2708_ARM_CONTROL_H
  4781. +#define __BCM2708_ARM_CONTROL_H
  4782. +
  4783. +/*
  4784. + * Definitions and addresses for the ARM CONTROL logic
  4785. + * This file is manually generated.
  4786. + */
  4787. +
  4788. +#define ARM_BASE 0x7E00B000
  4789. +
  4790. +/* Basic configuration */
  4791. +#define ARM_CONTROL0 HW_REGISTER_RW(ARM_BASE+0x000)
  4792. +#define ARM_C0_SIZ128M 0x00000000
  4793. +#define ARM_C0_SIZ256M 0x00000001
  4794. +#define ARM_C0_SIZ512M 0x00000002
  4795. +#define ARM_C0_SIZ1G 0x00000003
  4796. +#define ARM_C0_BRESP0 0x00000000
  4797. +#define ARM_C0_BRESP1 0x00000004
  4798. +#define ARM_C0_BRESP2 0x00000008
  4799. +#define ARM_C0_BOOTHI 0x00000010
  4800. +#define ARM_C0_UNUSED05 0x00000020 /* free */
  4801. +#define ARM_C0_FULLPERI 0x00000040
  4802. +#define ARM_C0_UNUSED78 0x00000180 /* free */
  4803. +#define ARM_C0_JTAGMASK 0x00000E00
  4804. +#define ARM_C0_JTAGOFF 0x00000000
  4805. +#define ARM_C0_JTAGBASH 0x00000800 /* Debug on GPIO off */
  4806. +#define ARM_C0_JTAGGPIO 0x00000C00 /* Debug on GPIO on */
  4807. +#define ARM_C0_APROTMSK 0x0000F000
  4808. +#define ARM_C0_DBG0SYNC 0x00010000 /* VPU0 halt sync */
  4809. +#define ARM_C0_DBG1SYNC 0x00020000 /* VPU1 halt sync */
  4810. +#define ARM_C0_SWDBGREQ 0x00040000 /* HW debug request */
  4811. +#define ARM_C0_PASSHALT 0x00080000 /* ARM halt passed to debugger */
  4812. +#define ARM_C0_PRIO_PER 0x00F00000 /* per priority mask */
  4813. +#define ARM_C0_PRIO_L2 0x0F000000
  4814. +#define ARM_C0_PRIO_UC 0xF0000000
  4815. +
  4816. +#define ARM_C0_APROTPASS 0x0000A000 /* Translate 1:1 */
  4817. +#define ARM_C0_APROTUSER 0x00000000 /* Only user mode */
  4818. +#define ARM_C0_APROTSYST 0x0000F000 /* Only system mode */
  4819. +
  4820. +
  4821. +#define ARM_CONTROL1 HW_REGISTER_RW(ARM_BASE+0x440)
  4822. +#define ARM_C1_TIMER 0x00000001 /* re-route timer IRQ to VC */
  4823. +#define ARM_C1_MAIL 0x00000002 /* re-route Mail IRQ to VC */
  4824. +#define ARM_C1_BELL0 0x00000004 /* re-route Doorbell 0 to VC */
  4825. +#define ARM_C1_BELL1 0x00000008 /* re-route Doorbell 1 to VC */
  4826. +#define ARM_C1_PERSON 0x00000100 /* peripherals on */
  4827. +#define ARM_C1_REQSTOP 0x00000200 /* ASYNC bridge request stop */
  4828. +
  4829. +#define ARM_STATUS HW_REGISTER_RW(ARM_BASE+0x444)
  4830. +#define ARM_S_ACKSTOP 0x80000000 /* Bridge stopped */
  4831. +#define ARM_S_READPEND 0x000003FF /* pending reads counter */
  4832. +#define ARM_S_WRITPEND 0x000FFC00 /* pending writes counter */
  4833. +
  4834. +#define ARM_ERRHALT HW_REGISTER_RW(ARM_BASE+0x448)
  4835. +#define ARM_EH_PERIBURST 0x00000001 /* Burst write seen on peri bus */
  4836. +#define ARM_EH_ILLADDRS1 0x00000002 /* Address bits 25-27 error */
  4837. +#define ARM_EH_ILLADDRS2 0x00000004 /* Address bits 31-28 error */
  4838. +#define ARM_EH_VPU0HALT 0x00000008 /* VPU0 halted & in debug mode */
  4839. +#define ARM_EH_VPU1HALT 0x00000010 /* VPU1 halted & in debug mode */
  4840. +#define ARM_EH_ARMHALT 0x00000020 /* ARM in halted debug mode */
  4841. +
  4842. +#define ARM_ID_SECURE HW_REGISTER_RW(ARM_BASE+0x00C)
  4843. +#define ARM_ID HW_REGISTER_RW(ARM_BASE+0x44C)
  4844. +#define ARM_IDVAL 0x364D5241
  4845. +
  4846. +/* Translation memory */
  4847. +#define ARM_TRANSLATE HW_REGISTER_RW(ARM_BASE+0x100)
  4848. +/* 32 locations: 0x100.. 0x17F */
  4849. +/* 32 spare means we CAN go to 64 pages.... */
  4850. +
  4851. +
  4852. +/* Interrupts */
  4853. +#define ARM_IRQ_PEND0 HW_REGISTER_RW(ARM_BASE+0x200) /* Top IRQ bits */
  4854. +#define ARM_I0_TIMER 0x00000001 /* timer IRQ */
  4855. +#define ARM_I0_MAIL 0x00000002 /* Mail IRQ */
  4856. +#define ARM_I0_BELL0 0x00000004 /* Doorbell 0 */
  4857. +#define ARM_I0_BELL1 0x00000008 /* Doorbell 1 */
  4858. +#define ARM_I0_BANK1 0x00000100 /* Bank1 IRQ */
  4859. +#define ARM_I0_BANK2 0x00000200 /* Bank2 IRQ */
  4860. +
  4861. +#define ARM_IRQ_PEND1 HW_REGISTER_RW(ARM_BASE+0x204) /* All bank1 IRQ bits */
  4862. +/* todo: all I1_interrupt sources */
  4863. +#define ARM_IRQ_PEND2 HW_REGISTER_RW(ARM_BASE+0x208) /* All bank2 IRQ bits */
  4864. +/* todo: all I2_interrupt sources */
  4865. +
  4866. +#define ARM_IRQ_FAST HW_REGISTER_RW(ARM_BASE+0x20C) /* FIQ control */
  4867. +#define ARM_IF_INDEX 0x0000007F /* FIQ select */
  4868. +#define ARM_IF_ENABLE 0x00000080 /* FIQ enable */
  4869. +#define ARM_IF_VCMASK 0x0000003F /* FIQ = (index from VC source) */
  4870. +#define ARM_IF_TIMER 0x00000040 /* FIQ = ARM timer */
  4871. +#define ARM_IF_MAIL 0x00000041 /* FIQ = ARM Mail */
  4872. +#define ARM_IF_BELL0 0x00000042 /* FIQ = ARM Doorbell 0 */
  4873. +#define ARM_IF_BELL1 0x00000043 /* FIQ = ARM Doorbell 1 */
  4874. +#define ARM_IF_VP0HALT 0x00000044 /* FIQ = VPU0 Halt seen */
  4875. +#define ARM_IF_VP1HALT 0x00000045 /* FIQ = VPU1 Halt seen */
  4876. +#define ARM_IF_ILLEGAL 0x00000046 /* FIQ = Illegal access seen */
  4877. +
  4878. +#define ARM_IRQ_ENBL1 HW_REGISTER_RW(ARM_BASE+0x210) /* Bank1 enable bits */
  4879. +#define ARM_IRQ_ENBL2 HW_REGISTER_RW(ARM_BASE+0x214) /* Bank2 enable bits */
  4880. +#define ARM_IRQ_ENBL3 HW_REGISTER_RW(ARM_BASE+0x218) /* ARM irqs enable bits */
  4881. +#define ARM_IRQ_DIBL1 HW_REGISTER_RW(ARM_BASE+0x21C) /* Bank1 disable bits */
  4882. +#define ARM_IRQ_DIBL2 HW_REGISTER_RW(ARM_BASE+0x220) /* Bank2 disable bits */
  4883. +#define ARM_IRQ_DIBL3 HW_REGISTER_RW(ARM_BASE+0x224) /* ARM irqs disable bits */
  4884. +#define ARM_IE_TIMER 0x00000001 /* Timer IRQ */
  4885. +#define ARM_IE_MAIL 0x00000002 /* Mail IRQ */
  4886. +#define ARM_IE_BELL0 0x00000004 /* Doorbell 0 */
  4887. +#define ARM_IE_BELL1 0x00000008 /* Doorbell 1 */
  4888. +#define ARM_IE_VP0HALT 0x00000010 /* VPU0 Halt */
  4889. +#define ARM_IE_VP1HALT 0x00000020 /* VPU1 Halt */
  4890. +#define ARM_IE_ILLEGAL 0x00000040 /* Illegal access seen */
  4891. +
  4892. +/* Timer */
  4893. +/* For reg. fields see sp804 spec. */
  4894. +#define ARM_T_LOAD HW_REGISTER_RW(ARM_BASE+0x400)
  4895. +#define ARM_T_VALUE HW_REGISTER_RW(ARM_BASE+0x404)
  4896. +#define ARM_T_CONTROL HW_REGISTER_RW(ARM_BASE+0x408)
  4897. +#define ARM_T_IRQCNTL HW_REGISTER_RW(ARM_BASE+0x40C)
  4898. +#define ARM_T_RAWIRQ HW_REGISTER_RW(ARM_BASE+0x410)
  4899. +#define ARM_T_MSKIRQ HW_REGISTER_RW(ARM_BASE+0x414)
  4900. +#define ARM_T_RELOAD HW_REGISTER_RW(ARM_BASE+0x418)
  4901. +#define ARM_T_PREDIV HW_REGISTER_RW(ARM_BASE+0x41c)
  4902. +#define ARM_T_FREECNT HW_REGISTER_RW(ARM_BASE+0x420)
  4903. +
  4904. +#define TIMER_CTRL_ONESHOT (1 << 0)
  4905. +#define TIMER_CTRL_32BIT (1 << 1)
  4906. +#define TIMER_CTRL_DIV1 (0 << 2)
  4907. +#define TIMER_CTRL_DIV16 (1 << 2)
  4908. +#define TIMER_CTRL_DIV256 (2 << 2)
  4909. +#define TIMER_CTRL_IE (1 << 5)
  4910. +#define TIMER_CTRL_PERIODIC (1 << 6)
  4911. +#define TIMER_CTRL_ENABLE (1 << 7)
  4912. +#define TIMER_CTRL_DBGHALT (1 << 8)
  4913. +#define TIMER_CTRL_ENAFREE (1 << 9)
  4914. +#define TIMER_CTRL_FREEDIV_SHIFT 16)
  4915. +#define TIMER_CTRL_FREEDIV_MASK 0xff
  4916. +
  4917. +/* Semaphores, Doorbells, Mailboxes */
  4918. +#define ARM_SBM_OWN0 (ARM_BASE+0x800)
  4919. +#define ARM_SBM_OWN1 (ARM_BASE+0x900)
  4920. +#define ARM_SBM_OWN2 (ARM_BASE+0xA00)
  4921. +#define ARM_SBM_OWN3 (ARM_BASE+0xB00)
  4922. +
  4923. +/* MAILBOXES
  4924. + * Register flags are common across all
  4925. + * owner registers. See end of this section
  4926. + *
  4927. + * Semaphores, Doorbells, Mailboxes Owner 0
  4928. + *
  4929. + */
  4930. +
  4931. +#define ARM_0_SEMS HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  4932. +#define ARM_0_SEM0 HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  4933. +#define ARM_0_SEM1 HW_REGISTER_RW(ARM_SBM_OWN0+0x04)
  4934. +#define ARM_0_SEM2 HW_REGISTER_RW(ARM_SBM_OWN0+0x08)
  4935. +#define ARM_0_SEM3 HW_REGISTER_RW(ARM_SBM_OWN0+0x0C)
  4936. +#define ARM_0_SEM4 HW_REGISTER_RW(ARM_SBM_OWN0+0x10)
  4937. +#define ARM_0_SEM5 HW_REGISTER_RW(ARM_SBM_OWN0+0x14)
  4938. +#define ARM_0_SEM6 HW_REGISTER_RW(ARM_SBM_OWN0+0x18)
  4939. +#define ARM_0_SEM7 HW_REGISTER_RW(ARM_SBM_OWN0+0x1C)
  4940. +#define ARM_0_BELL0 HW_REGISTER_RW(ARM_SBM_OWN0+0x40)
  4941. +#define ARM_0_BELL1 HW_REGISTER_RW(ARM_SBM_OWN0+0x44)
  4942. +#define ARM_0_BELL2 HW_REGISTER_RW(ARM_SBM_OWN0+0x48)
  4943. +#define ARM_0_BELL3 HW_REGISTER_RW(ARM_SBM_OWN0+0x4C)
  4944. +/* MAILBOX 0 access in Owner 0 area */
  4945. +/* Some addresses should ONLY be used by owner 0 */
  4946. +#define ARM_0_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) */
  4947. +#define ARM_0_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) Normal read */
  4948. +#define ARM_0_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN0+0x90) /* none-pop read */
  4949. +#define ARM_0_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN0+0x94) /* Sender read (only LS 2 bits) */
  4950. +#define ARM_0_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN0+0x98) /* Status read */
  4951. +#define ARM_0_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0x9C) /* Config read/write */
  4952. +/* MAILBOX 1 access in Owner 0 area */
  4953. +/* Owner 0 should only WRITE to this mailbox */
  4954. +#define ARM_0_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) /* .. 0xAC (4 locations) */
  4955. +/*#define ARM_0_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) */ /* DO NOT USE THIS !!!!! */
  4956. +/*#define ARM_0_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN0+0xB0) */ /* DO NOT USE THIS !!!!! */
  4957. +/*#define ARM_0_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN0+0xB4) */ /* DO NOT USE THIS !!!!! */
  4958. +#define ARM_0_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN0+0xB8) /* Status read */
  4959. +/*#define ARM_0_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0xBC) */ /* DO NOT USE THIS !!!!! */
  4960. +/* General SEM, BELL, MAIL config/status */
  4961. +#define ARM_0_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE0) /* semaphore clear/debug register */
  4962. +#define ARM_0_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE4) /* Doorbells clear/debug register */
  4963. +#define ARM_0_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xF8) /* ALL interrupts */
  4964. +#define ARM_0_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xFC) /* IRQS pending for owner 0 */
  4965. +
  4966. +/* Semaphores, Doorbells, Mailboxes Owner 1 */
  4967. +#define ARM_1_SEMS HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  4968. +#define ARM_1_SEM0 HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  4969. +#define ARM_1_SEM1 HW_REGISTER_RW(ARM_SBM_OWN1+0x04)
  4970. +#define ARM_1_SEM2 HW_REGISTER_RW(ARM_SBM_OWN1+0x08)
  4971. +#define ARM_1_SEM3 HW_REGISTER_RW(ARM_SBM_OWN1+0x0C)
  4972. +#define ARM_1_SEM4 HW_REGISTER_RW(ARM_SBM_OWN1+0x10)
  4973. +#define ARM_1_SEM5 HW_REGISTER_RW(ARM_SBM_OWN1+0x14)
  4974. +#define ARM_1_SEM6 HW_REGISTER_RW(ARM_SBM_OWN1+0x18)
  4975. +#define ARM_1_SEM7 HW_REGISTER_RW(ARM_SBM_OWN1+0x1C)
  4976. +#define ARM_1_BELL0 HW_REGISTER_RW(ARM_SBM_OWN1+0x40)
  4977. +#define ARM_1_BELL1 HW_REGISTER_RW(ARM_SBM_OWN1+0x44)
  4978. +#define ARM_1_BELL2 HW_REGISTER_RW(ARM_SBM_OWN1+0x48)
  4979. +#define ARM_1_BELL3 HW_REGISTER_RW(ARM_SBM_OWN1+0x4C)
  4980. +/* MAILBOX 0 access in Owner 0 area */
  4981. +/* Owner 1 should only WRITE to this mailbox */
  4982. +#define ARM_1_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0x80) /* .. 0x8C (4 locations) */
  4983. +/*#define ARM_1_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN1+0x80) */ /* DO NOT USE THIS !!!!! */
  4984. +/*#define ARM_1_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN1+0x90) */ /* DO NOT USE THIS !!!!! */
  4985. +/*#define ARM_1_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN1+0x94) */ /* DO NOT USE THIS !!!!! */
  4986. +#define ARM_1_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN1+0x98) /* Status read */
  4987. +/*#define ARM_1_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0x9C) */ /* DO NOT USE THIS !!!!! */
  4988. +/* MAILBOX 1 access in Owner 0 area */
  4989. +#define ARM_1_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) */
  4990. +#define ARM_1_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) Normal read */
  4991. +#define ARM_1_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN1+0xB0) /* none-pop read */
  4992. +#define ARM_1_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN1+0xB4) /* Sender read (only LS 2 bits) */
  4993. +#define ARM_1_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN1+0xB8) /* Status read */
  4994. +#define ARM_1_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0xBC)
  4995. +/* General SEM, BELL, MAIL config/status */
  4996. +#define ARM_1_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE0) /* semaphore clear/debug register */
  4997. +#define ARM_1_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE4) /* Doorbells clear/debug register */
  4998. +#define ARM_1_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xFC) /* IRQS pending for owner 1 */
  4999. +#define ARM_1_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xF8) /* ALL interrupts */
  5000. +
  5001. +/* Semaphores, Doorbells, Mailboxes Owner 2 */
  5002. +#define ARM_2_SEMS HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5003. +#define ARM_2_SEM0 HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5004. +#define ARM_2_SEM1 HW_REGISTER_RW(ARM_SBM_OWN2+0x04)
  5005. +#define ARM_2_SEM2 HW_REGISTER_RW(ARM_SBM_OWN2+0x08)
  5006. +#define ARM_2_SEM3 HW_REGISTER_RW(ARM_SBM_OWN2+0x0C)
  5007. +#define ARM_2_SEM4 HW_REGISTER_RW(ARM_SBM_OWN2+0x10)
  5008. +#define ARM_2_SEM5 HW_REGISTER_RW(ARM_SBM_OWN2+0x14)
  5009. +#define ARM_2_SEM6 HW_REGISTER_RW(ARM_SBM_OWN2+0x18)
  5010. +#define ARM_2_SEM7 HW_REGISTER_RW(ARM_SBM_OWN2+0x1C)
  5011. +#define ARM_2_BELL0 HW_REGISTER_RW(ARM_SBM_OWN2+0x40)
  5012. +#define ARM_2_BELL1 HW_REGISTER_RW(ARM_SBM_OWN2+0x44)
  5013. +#define ARM_2_BELL2 HW_REGISTER_RW(ARM_SBM_OWN2+0x48)
  5014. +#define ARM_2_BELL3 HW_REGISTER_RW(ARM_SBM_OWN2+0x4C)
  5015. +/* MAILBOX 0 access in Owner 2 area */
  5016. +/* Owner 2 should only WRITE to this mailbox */
  5017. +#define ARM_2_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0x80) /* .. 0x8C (4 locations) */
  5018. +/*#define ARM_2_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN2+0x80) */ /* DO NOT USE THIS !!!!! */
  5019. +/*#define ARM_2_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN2+0x90) */ /* DO NOT USE THIS !!!!! */
  5020. +/*#define ARM_2_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN2+0x94) */ /* DO NOT USE THIS !!!!! */
  5021. +#define ARM_2_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN2+0x98) /* Status read */
  5022. +/*#define ARM_2_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0x9C) */ /* DO NOT USE THIS !!!!! */
  5023. +/* MAILBOX 1 access in Owner 2 area */
  5024. +/* Owner 2 should only WRITE to this mailbox */
  5025. +#define ARM_2_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) /* .. 0xAC (4 locations) */
  5026. +/*#define ARM_2_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) */ /* DO NOT USE THIS !!!!! */
  5027. +/*#define ARM_2_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN2+0xB0) */ /* DO NOT USE THIS !!!!! */
  5028. +/*#define ARM_2_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN2+0xB4) */ /* DO NOT USE THIS !!!!! */
  5029. +#define ARM_2_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN2+0xB8) /* Status read */
  5030. +/*#define ARM_2_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0xBC) */ /* DO NOT USE THIS !!!!! */
  5031. +/* General SEM, BELL, MAIL config/status */
  5032. +#define ARM_2_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE0) /* semaphore clear/debug register */
  5033. +#define ARM_2_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE4) /* Doorbells clear/debug register */
  5034. +#define ARM_2_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xFC) /* IRQS pending for owner 2 */
  5035. +#define ARM_2_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xF8) /* ALL interrupts */
  5036. +
  5037. +/* Semaphores, Doorbells, Mailboxes Owner 3 */
  5038. +#define ARM_3_SEMS HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  5039. +#define ARM_3_SEM0 HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  5040. +#define ARM_3_SEM1 HW_REGISTER_RW(ARM_SBM_OWN3+0x04)
  5041. +#define ARM_3_SEM2 HW_REGISTER_RW(ARM_SBM_OWN3+0x08)
  5042. +#define ARM_3_SEM3 HW_REGISTER_RW(ARM_SBM_OWN3+0x0C)
  5043. +#define ARM_3_SEM4 HW_REGISTER_RW(ARM_SBM_OWN3+0x10)
  5044. +#define ARM_3_SEM5 HW_REGISTER_RW(ARM_SBM_OWN3+0x14)
  5045. +#define ARM_3_SEM6 HW_REGISTER_RW(ARM_SBM_OWN3+0x18)
  5046. +#define ARM_3_SEM7 HW_REGISTER_RW(ARM_SBM_OWN3+0x1C)
  5047. +#define ARM_3_BELL0 HW_REGISTER_RW(ARM_SBM_OWN3+0x40)
  5048. +#define ARM_3_BELL1 HW_REGISTER_RW(ARM_SBM_OWN3+0x44)
  5049. +#define ARM_3_BELL2 HW_REGISTER_RW(ARM_SBM_OWN3+0x48)
  5050. +#define ARM_3_BELL3 HW_REGISTER_RW(ARM_SBM_OWN3+0x4C)
  5051. +/* MAILBOX 0 access in Owner 3 area */
  5052. +/* Owner 3 should only WRITE to this mailbox */
  5053. +#define ARM_3_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0x80) /* .. 0x8C (4 locations) */
  5054. +/*#define ARM_3_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN3+0x80) */ /* DO NOT USE THIS !!!!! */
  5055. +/*#define ARM_3_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN3+0x90) */ /* DO NOT USE THIS !!!!! */
  5056. +/*#define ARM_3_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN3+0x94) */ /* DO NOT USE THIS !!!!! */
  5057. +#define ARM_3_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN3+0x98) /* Status read */
  5058. +/*#define ARM_3_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0x9C) */ /* DO NOT USE THIS !!!!! */
  5059. +/* MAILBOX 1 access in Owner 3 area */
  5060. +/* Owner 3 should only WRITE to this mailbox */
  5061. +#define ARM_3_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) /* .. 0xAC (4 locations) */
  5062. +/*#define ARM_3_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) */ /* DO NOT USE THIS !!!!! */
  5063. +/*#define ARM_3_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN3+0xB0) */ /* DO NOT USE THIS !!!!! */
  5064. +/*#define ARM_3_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN3+0xB4) */ /* DO NOT USE THIS !!!!! */
  5065. +#define ARM_3_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN3+0xB8) /* Status read */
  5066. +/*#define ARM_3_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0xBC) */ /* DO NOT USE THIS !!!!! */
  5067. +/* General SEM, BELL, MAIL config/status */
  5068. +#define ARM_3_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE0) /* semaphore clear/debug register */
  5069. +#define ARM_3_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE4) /* Doorbells clear/debug register */
  5070. +#define ARM_3_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xFC) /* IRQS pending for owner 3 */
  5071. +#define ARM_3_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xF8) /* ALL interrupts */
  5072. +
  5073. +
  5074. +
  5075. +/* Mailbox flags. Valid for all owners */
  5076. +
  5077. +/* Mailbox status register (...0x98) */
  5078. +#define ARM_MS_FULL 0x80000000
  5079. +#define ARM_MS_EMPTY 0x40000000
  5080. +#define ARM_MS_LEVEL 0x400000FF /* Max. value depdnds on mailbox depth parameter */
  5081. +
  5082. +/* MAILBOX config/status register (...0x9C) */
  5083. +/* ANY write to this register clears the error bits! */
  5084. +#define ARM_MC_IHAVEDATAIRQEN 0x00000001 /* mailbox irq enable: has data */
  5085. +#define ARM_MC_IHAVESPACEIRQEN 0x00000002 /* mailbox irq enable: has space */
  5086. +#define ARM_MC_OPPISEMPTYIRQEN 0x00000004 /* mailbox irq enable: Opp. is empty */
  5087. +#define ARM_MC_MAIL_CLEAR 0x00000008 /* mailbox clear write 1, then 0 */
  5088. +#define ARM_MC_IHAVEDATAIRQPEND 0x00000010 /* mailbox irq pending: has space */
  5089. +#define ARM_MC_IHAVESPACEIRQPEND 0x00000020 /* mailbox irq pending: Opp. is empty */
  5090. +#define ARM_MC_OPPISEMPTYIRQPEND 0x00000040 /* mailbox irq pending */
  5091. +/* Bit 7 is unused */
  5092. +#define ARM_MC_ERRNOOWN 0x00000100 /* error : none owner read from mailbox */
  5093. +#define ARM_MC_ERROVERFLW 0x00000200 /* error : write to fill mailbox */
  5094. +#define ARM_MC_ERRUNDRFLW 0x00000400 /* error : read from empty mailbox */
  5095. +
  5096. +/* Semaphore clear/debug register (...0xE0) */
  5097. +#define ARM_SD_OWN0 0x00000003 /* Owner of sem 0 */
  5098. +#define ARM_SD_OWN1 0x0000000C /* Owner of sem 1 */
  5099. +#define ARM_SD_OWN2 0x00000030 /* Owner of sem 2 */
  5100. +#define ARM_SD_OWN3 0x000000C0 /* Owner of sem 3 */
  5101. +#define ARM_SD_OWN4 0x00000300 /* Owner of sem 4 */
  5102. +#define ARM_SD_OWN5 0x00000C00 /* Owner of sem 5 */
  5103. +#define ARM_SD_OWN6 0x00003000 /* Owner of sem 6 */
  5104. +#define ARM_SD_OWN7 0x0000C000 /* Owner of sem 7 */
  5105. +#define ARM_SD_SEM0 0x00010000 /* Status of sem 0 */
  5106. +#define ARM_SD_SEM1 0x00020000 /* Status of sem 1 */
  5107. +#define ARM_SD_SEM2 0x00040000 /* Status of sem 2 */
  5108. +#define ARM_SD_SEM3 0x00080000 /* Status of sem 3 */
  5109. +#define ARM_SD_SEM4 0x00100000 /* Status of sem 4 */
  5110. +#define ARM_SD_SEM5 0x00200000 /* Status of sem 5 */
  5111. +#define ARM_SD_SEM6 0x00400000 /* Status of sem 6 */
  5112. +#define ARM_SD_SEM7 0x00800000 /* Status of sem 7 */
  5113. +
  5114. +/* Doorbells clear/debug register (...0xE4) */
  5115. +#define ARM_BD_OWN0 0x00000003 /* Owner of doorbell 0 */
  5116. +#define ARM_BD_OWN1 0x0000000C /* Owner of doorbell 1 */
  5117. +#define ARM_BD_OWN2 0x00000030 /* Owner of doorbell 2 */
  5118. +#define ARM_BD_OWN3 0x000000C0 /* Owner of doorbell 3 */
  5119. +#define ARM_BD_BELL0 0x00000100 /* Status of doorbell 0 */
  5120. +#define ARM_BD_BELL1 0x00000200 /* Status of doorbell 1 */
  5121. +#define ARM_BD_BELL2 0x00000400 /* Status of doorbell 2 */
  5122. +#define ARM_BD_BELL3 0x00000800 /* Status of doorbell 3 */
  5123. +
  5124. +/* MY IRQS register (...0xF8) */
  5125. +#define ARM_MYIRQ_BELL 0x00000001 /* This owner has a doorbell IRQ */
  5126. +#define ARM_MYIRQ_MAIL 0x00000002 /* This owner has a mailbox IRQ */
  5127. +
  5128. +/* ALL IRQS register (...0xF8) */
  5129. +#define ARM_AIS_BELL0 0x00000001 /* Doorbell 0 IRQ pending */
  5130. +#define ARM_AIS_BELL1 0x00000002 /* Doorbell 1 IRQ pending */
  5131. +#define ARM_AIS_BELL2 0x00000004 /* Doorbell 2 IRQ pending */
  5132. +#define ARM_AIS_BELL3 0x00000008 /* Doorbell 3 IRQ pending */
  5133. +#define ARM_AIS0_HAVEDATA 0x00000010 /* MAIL 0 has data IRQ pending */
  5134. +#define ARM_AIS0_HAVESPAC 0x00000020 /* MAIL 0 has space IRQ pending */
  5135. +#define ARM_AIS0_OPPEMPTY 0x00000040 /* MAIL 0 opposite is empty IRQ */
  5136. +#define ARM_AIS1_HAVEDATA 0x00000080 /* MAIL 1 has data IRQ pending */
  5137. +#define ARM_AIS1_HAVESPAC 0x00000100 /* MAIL 1 has space IRQ pending */
  5138. +#define ARM_AIS1_OPPEMPTY 0x00000200 /* MAIL 1 opposite is empty IRQ */
  5139. +/* Note that bell-0, bell-1 and MAIL0 IRQ go only to the ARM */
  5140. +/* Whilst that bell-2, bell-3 and MAIL1 IRQ go only to the VC */
  5141. +/* */
  5142. +/* ARM JTAG BASH */
  5143. +/* */
  5144. +#define AJB_BASE 0x7e2000c0
  5145. +
  5146. +#define AJBCONF HW_REGISTER_RW(AJB_BASE+0x00)
  5147. +#define AJB_BITS0 0x000000
  5148. +#define AJB_BITS4 0x000004
  5149. +#define AJB_BITS8 0x000008
  5150. +#define AJB_BITS12 0x00000C
  5151. +#define AJB_BITS16 0x000010
  5152. +#define AJB_BITS20 0x000014
  5153. +#define AJB_BITS24 0x000018
  5154. +#define AJB_BITS28 0x00001C
  5155. +#define AJB_BITS32 0x000020
  5156. +#define AJB_BITS34 0x000022
  5157. +#define AJB_OUT_MS 0x000040
  5158. +#define AJB_OUT_LS 0x000000
  5159. +#define AJB_INV_CLK 0x000080
  5160. +#define AJB_D0_RISE 0x000100
  5161. +#define AJB_D0_FALL 0x000000
  5162. +#define AJB_D1_RISE 0x000200
  5163. +#define AJB_D1_FALL 0x000000
  5164. +#define AJB_IN_RISE 0x000400
  5165. +#define AJB_IN_FALL 0x000000
  5166. +#define AJB_ENABLE 0x000800
  5167. +#define AJB_HOLD0 0x000000
  5168. +#define AJB_HOLD1 0x001000
  5169. +#define AJB_HOLD2 0x002000
  5170. +#define AJB_HOLD3 0x003000
  5171. +#define AJB_RESETN 0x004000
  5172. +#define AJB_CLKSHFT 16
  5173. +#define AJB_BUSY 0x80000000
  5174. +#define AJBTMS HW_REGISTER_RW(AJB_BASE+0x04)
  5175. +#define AJBTDI HW_REGISTER_RW(AJB_BASE+0x08)
  5176. +#define AJBTDO HW_REGISTER_RW(AJB_BASE+0x0c)
  5177. +
  5178. +#endif
  5179. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/arm_power.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/arm_power.h
  5180. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/arm_power.h 1970-01-01 01:00:00.000000000 +0100
  5181. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/arm_power.h 2014-03-11 16:51:54.000000000 +0100
  5182. @@ -0,0 +1,60 @@
  5183. +/*
  5184. + * linux/arch/arm/mach-bcm2708/include/mach/arm_power.h
  5185. + *
  5186. + * Copyright (C) 2010 Broadcom
  5187. + *
  5188. + * This program is free software; you can redistribute it and/or modify
  5189. + * it under the terms of the GNU General Public License as published by
  5190. + * the Free Software Foundation; either version 2 of the License, or
  5191. + * (at your option) any later version.
  5192. + *
  5193. + * This program is distributed in the hope that it will be useful,
  5194. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5195. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5196. + * GNU General Public License for more details.
  5197. + *
  5198. + * You should have received a copy of the GNU General Public License
  5199. + * along with this program; if not, write to the Free Software
  5200. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5201. + */
  5202. +
  5203. +#ifndef _ARM_POWER_H
  5204. +#define _ARM_POWER_H
  5205. +
  5206. +/* Use meaningful names on each side */
  5207. +#ifdef __VIDEOCORE__
  5208. +#define PREFIX(x) ARM_##x
  5209. +#else
  5210. +#define PREFIX(x) BCM_##x
  5211. +#endif
  5212. +
  5213. +enum {
  5214. + PREFIX(POWER_SDCARD_BIT),
  5215. + PREFIX(POWER_UART_BIT),
  5216. + PREFIX(POWER_MINIUART_BIT),
  5217. + PREFIX(POWER_USB_BIT),
  5218. + PREFIX(POWER_I2C0_BIT),
  5219. + PREFIX(POWER_I2C1_BIT),
  5220. + PREFIX(POWER_I2C2_BIT),
  5221. + PREFIX(POWER_SPI_BIT),
  5222. + PREFIX(POWER_CCP2TX_BIT),
  5223. +
  5224. + PREFIX(POWER_MAX)
  5225. +};
  5226. +
  5227. +enum {
  5228. + PREFIX(POWER_SDCARD) = (1 << PREFIX(POWER_SDCARD_BIT)),
  5229. + PREFIX(POWER_UART) = (1 << PREFIX(POWER_UART_BIT)),
  5230. + PREFIX(POWER_MINIUART) = (1 << PREFIX(POWER_MINIUART_BIT)),
  5231. + PREFIX(POWER_USB) = (1 << PREFIX(POWER_USB_BIT)),
  5232. + PREFIX(POWER_I2C0) = (1 << PREFIX(POWER_I2C0_BIT)),
  5233. + PREFIX(POWER_I2C1_MASK) = (1 << PREFIX(POWER_I2C1_BIT)),
  5234. + PREFIX(POWER_I2C2_MASK) = (1 << PREFIX(POWER_I2C2_BIT)),
  5235. + PREFIX(POWER_SPI_MASK) = (1 << PREFIX(POWER_SPI_BIT)),
  5236. + PREFIX(POWER_CCP2TX_MASK) = (1 << PREFIX(POWER_CCP2TX_BIT)),
  5237. +
  5238. + PREFIX(POWER_MASK) = (1 << PREFIX(POWER_MAX)) - 1,
  5239. + PREFIX(POWER_NONE) = 0
  5240. +};
  5241. +
  5242. +#endif
  5243. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/clkdev.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/clkdev.h
  5244. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/clkdev.h 1970-01-01 01:00:00.000000000 +0100
  5245. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/clkdev.h 2014-03-11 16:51:54.000000000 +0100
  5246. @@ -0,0 +1,7 @@
  5247. +#ifndef __ASM_MACH_CLKDEV_H
  5248. +#define __ASM_MACH_CLKDEV_H
  5249. +
  5250. +#define __clk_get(clk) ({ 1; })
  5251. +#define __clk_put(clk) do { } while (0)
  5252. +
  5253. +#endif
  5254. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/debug-macro.S linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/debug-macro.S
  5255. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/debug-macro.S 1970-01-01 01:00:00.000000000 +0100
  5256. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/debug-macro.S 2014-03-11 16:54:55.000000000 +0100
  5257. @@ -0,0 +1,22 @@
  5258. +/* arch/arm/mach-bcm2708/include/mach/debug-macro.S
  5259. + *
  5260. + * Debugging macro include header
  5261. + *
  5262. + * Copyright (C) 2010 Broadcom
  5263. + * Copyright (C) 1994-1999 Russell King
  5264. + * Moved from linux/arch/arm/kernel/debug.S by Ben Dooks
  5265. + *
  5266. + * This program is free software; you can redistribute it and/or modify
  5267. + * it under the terms of the GNU General Public License version 2 as
  5268. + * published by the Free Software Foundation.
  5269. + *
  5270. +*/
  5271. +
  5272. +#include <mach/platform.h>
  5273. +
  5274. + .macro addruart, rp, rv, tmp
  5275. + ldr \rp, =UART0_BASE
  5276. + ldr \rv, =IO_ADDRESS(UART0_BASE)
  5277. + .endm
  5278. +
  5279. +#include <debug/pl01x.S>
  5280. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/dma.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/dma.h
  5281. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/dma.h 1970-01-01 01:00:00.000000000 +0100
  5282. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/dma.h 2014-03-11 16:51:54.000000000 +0100
  5283. @@ -0,0 +1,90 @@
  5284. +/*
  5285. + * linux/arch/arm/mach-bcm2708/include/mach/dma.h
  5286. + *
  5287. + * Copyright (C) 2010 Broadcom
  5288. + *
  5289. + * This program is free software; you can redistribute it and/or modify
  5290. + * it under the terms of the GNU General Public License version 2 as
  5291. + * published by the Free Software Foundation.
  5292. + */
  5293. +
  5294. +
  5295. +#ifndef _MACH_BCM2708_DMA_H
  5296. +#define _MACH_BCM2708_DMA_H
  5297. +
  5298. +#define BCM_DMAMAN_DRIVER_NAME "bcm2708_dma"
  5299. +
  5300. +/* DMA CS Control and Status bits */
  5301. +#define BCM2708_DMA_ACTIVE (1 << 0)
  5302. +#define BCM2708_DMA_INT (1 << 2)
  5303. +#define BCM2708_DMA_ISPAUSED (1 << 4) /* Pause requested or not active */
  5304. +#define BCM2708_DMA_ISHELD (1 << 5) /* Is held by DREQ flow control */
  5305. +#define BCM2708_DMA_ERR (1 << 8)
  5306. +#define BCM2708_DMA_ABORT (1 << 30) /* stop current CB, go to next, WO */
  5307. +#define BCM2708_DMA_RESET (1 << 31) /* WO, self clearing */
  5308. +
  5309. +/* DMA control block "info" field bits */
  5310. +#define BCM2708_DMA_INT_EN (1 << 0)
  5311. +#define BCM2708_DMA_TDMODE (1 << 1)
  5312. +#define BCM2708_DMA_WAIT_RESP (1 << 3)
  5313. +#define BCM2708_DMA_D_INC (1 << 4)
  5314. +#define BCM2708_DMA_D_WIDTH (1 << 5)
  5315. +#define BCM2708_DMA_D_DREQ (1 << 6)
  5316. +#define BCM2708_DMA_S_INC (1 << 8)
  5317. +#define BCM2708_DMA_S_WIDTH (1 << 9)
  5318. +#define BCM2708_DMA_S_DREQ (1 << 10)
  5319. +
  5320. +#define BCM2708_DMA_BURST(x) (((x)&0xf) << 12)
  5321. +#define BCM2708_DMA_PER_MAP(x) ((x) << 16)
  5322. +#define BCM2708_DMA_WAITS(x) (((x)&0x1f) << 21)
  5323. +
  5324. +#define BCM2708_DMA_DREQ_EMMC 11
  5325. +#define BCM2708_DMA_DREQ_SDHOST 13
  5326. +
  5327. +#define BCM2708_DMA_CS 0x00 /* Control and Status */
  5328. +#define BCM2708_DMA_ADDR 0x04
  5329. +/* the current control block appears in the following registers - read only */
  5330. +#define BCM2708_DMA_INFO 0x08
  5331. +#define BCM2708_DMA_SOURCE_AD 0x0c
  5332. +#define BCM2708_DMA_DEST_AD 0x10
  5333. +#define BCM2708_DMA_NEXTCB 0x1C
  5334. +#define BCM2708_DMA_DEBUG 0x20
  5335. +
  5336. +#define BCM2708_DMA4_CS (BCM2708_DMA_CHAN(4)+BCM2708_DMA_CS)
  5337. +#define BCM2708_DMA4_ADDR (BCM2708_DMA_CHAN(4)+BCM2708_DMA_ADDR)
  5338. +
  5339. +#define BCM2708_DMA_TDMODE_LEN(w, h) ((h) << 16 | (w))
  5340. +
  5341. +struct bcm2708_dma_cb {
  5342. + unsigned long info;
  5343. + unsigned long src;
  5344. + unsigned long dst;
  5345. + unsigned long length;
  5346. + unsigned long stride;
  5347. + unsigned long next;
  5348. + unsigned long pad[2];
  5349. +};
  5350. +struct scatterlist;
  5351. +
  5352. +extern int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len);
  5353. +extern void bcm_dma_start(void __iomem *dma_chan_base,
  5354. + dma_addr_t control_block);
  5355. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base);
  5356. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base);
  5357. +extern int /*rc*/ bcm_dma_abort(void __iomem *dma_chan_base);
  5358. +
  5359. +/* When listing features we can ask for when allocating DMA channels give
  5360. + those with higher priority smaller ordinal numbers */
  5361. +#define BCM_DMA_FEATURE_FAST_ORD 0
  5362. +#define BCM_DMA_FEATURE_BULK_ORD 1
  5363. +#define BCM_DMA_FEATURE_FAST (1<<BCM_DMA_FEATURE_FAST_ORD)
  5364. +#define BCM_DMA_FEATURE_BULK (1<<BCM_DMA_FEATURE_BULK_ORD)
  5365. +#define BCM_DMA_FEATURE_COUNT 2
  5366. +
  5367. +/* return channel no or -ve error */
  5368. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  5369. + void __iomem **out_dma_base, int *out_dma_irq);
  5370. +extern int bcm_dma_chan_free(int channel);
  5371. +
  5372. +
  5373. +#endif /* _MACH_BCM2708_DMA_H */
  5374. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/entry-macro.S linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/entry-macro.S
  5375. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/entry-macro.S 1970-01-01 01:00:00.000000000 +0100
  5376. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/entry-macro.S 2014-03-11 16:51:54.000000000 +0100
  5377. @@ -0,0 +1,69 @@
  5378. +/*
  5379. + * arch/arm/mach-bcm2708/include/mach/entry-macro.S
  5380. + *
  5381. + * Low-level IRQ helper macros for BCM2708 platforms
  5382. + *
  5383. + * Copyright (C) 2010 Broadcom
  5384. + *
  5385. + * This program is free software; you can redistribute it and/or modify
  5386. + * it under the terms of the GNU General Public License as published by
  5387. + * the Free Software Foundation; either version 2 of the License, or
  5388. + * (at your option) any later version.
  5389. + *
  5390. + * This program is distributed in the hope that it will be useful,
  5391. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5392. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5393. + * GNU General Public License for more details.
  5394. + *
  5395. + * You should have received a copy of the GNU General Public License
  5396. + * along with this program; if not, write to the Free Software
  5397. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5398. + */
  5399. +#include <mach/hardware.h>
  5400. +
  5401. + .macro disable_fiq
  5402. + .endm
  5403. +
  5404. + .macro get_irqnr_preamble, base, tmp
  5405. + ldr \base, =IO_ADDRESS(ARMCTRL_IC_BASE)
  5406. + .endm
  5407. +
  5408. + .macro arch_ret_to_user, tmp1, tmp2
  5409. + .endm
  5410. +
  5411. + .macro get_irqnr_and_base, irqnr, irqstat, base, tmp
  5412. + /* get masked status */
  5413. + ldr \irqstat, [\base, #(ARM_IRQ_PEND0 - ARMCTRL_IC_BASE)]
  5414. + mov \irqnr, #(ARM_IRQ0_BASE + 31)
  5415. + and \tmp, \irqstat, #0x300 @ save bits 8 and 9
  5416. + /* clear bits 8 and 9, and test */
  5417. + bics \irqstat, \irqstat, #0x300
  5418. + bne 1010f
  5419. +
  5420. + tst \tmp, #0x100
  5421. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND1 - ARMCTRL_IC_BASE)]
  5422. + movne \irqnr, #(ARM_IRQ1_BASE + 31)
  5423. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  5424. + bicne \irqstat, #((1<<7) | (1<<9) | (1<<10))
  5425. + bicne \irqstat, #((1<<18) | (1<<19))
  5426. + bne 1010f
  5427. +
  5428. + tst \tmp, #0x200
  5429. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND2 - ARMCTRL_IC_BASE)]
  5430. + movne \irqnr, #(ARM_IRQ2_BASE + 31)
  5431. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  5432. + bicne \irqstat, #((1<<21) | (1<<22) | (1<<23) | (1<<24) | (1<<25))
  5433. + bicne \irqstat, #((1<<30))
  5434. + beq 1020f
  5435. +
  5436. +1010:
  5437. + @ For non-zero x, LSB(x) = 31 - CLZ(x^(x-1))
  5438. + @ N.B. CLZ is an ARM5 instruction.
  5439. + sub \tmp, \irqstat, #1
  5440. + eor \irqstat, \irqstat, \tmp
  5441. + clz \tmp, \irqstat
  5442. + sub \irqnr, \tmp
  5443. +
  5444. +1020: @ EQ will be set if no irqs pending
  5445. +
  5446. + .endm
  5447. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/frc.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/frc.h
  5448. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/frc.h 1970-01-01 01:00:00.000000000 +0100
  5449. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/frc.h 2014-03-11 16:51:54.000000000 +0100
  5450. @@ -0,0 +1,38 @@
  5451. +/*
  5452. + * arch/arm/mach-bcm2708/include/mach/timex.h
  5453. + *
  5454. + * BCM2708 free running counter (timer)
  5455. + *
  5456. + * Copyright (C) 2010 Broadcom
  5457. + *
  5458. + * This program is free software; you can redistribute it and/or modify
  5459. + * it under the terms of the GNU General Public License as published by
  5460. + * the Free Software Foundation; either version 2 of the License, or
  5461. + * (at your option) any later version.
  5462. + *
  5463. + * This program is distributed in the hope that it will be useful,
  5464. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5465. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5466. + * GNU General Public License for more details.
  5467. + *
  5468. + * You should have received a copy of the GNU General Public License
  5469. + * along with this program; if not, write to the Free Software
  5470. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5471. + */
  5472. +
  5473. +#ifndef _MACH_FRC_H
  5474. +#define _MACH_FRC_H
  5475. +
  5476. +#define FRC_TICK_RATE (1000000)
  5477. +
  5478. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  5479. + (slightly faster than frc_clock_ticks63()
  5480. + */
  5481. +extern unsigned long frc_clock_ticks32(void);
  5482. +
  5483. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  5484. + * Note - top bit should be ignored (see cnt32_to_63)
  5485. + */
  5486. +extern unsigned long long frc_clock_ticks63(void);
  5487. +
  5488. +#endif
  5489. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/gpio.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/gpio.h
  5490. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/gpio.h 1970-01-01 01:00:00.000000000 +0100
  5491. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/gpio.h 2014-03-11 16:54:55.000000000 +0100
  5492. @@ -0,0 +1,17 @@
  5493. +/*
  5494. + * arch/arm/mach-bcm2708/include/mach/gpio.h
  5495. + *
  5496. + * This file is licensed under the terms of the GNU General Public
  5497. + * License version 2. This program is licensed "as is" without any
  5498. + * warranty of any kind, whether express or implied.
  5499. + */
  5500. +
  5501. +#ifndef __ASM_ARCH_GPIO_H
  5502. +#define __ASM_ARCH_GPIO_H
  5503. +
  5504. +#define ARCH_NR_GPIOS 54 // number of gpio lines
  5505. +
  5506. +#define gpio_to_irq(x) ((x) + GPIO_IRQ_START)
  5507. +#define irq_to_gpio(x) ((x) - GPIO_IRQ_START)
  5508. +
  5509. +#endif
  5510. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/hardware.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/hardware.h
  5511. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/hardware.h 1970-01-01 01:00:00.000000000 +0100
  5512. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/hardware.h 2014-03-11 16:51:54.000000000 +0100
  5513. @@ -0,0 +1,28 @@
  5514. +/*
  5515. + * arch/arm/mach-bcm2708/include/mach/hardware.h
  5516. + *
  5517. + * This file contains the hardware definitions of the BCM2708 devices.
  5518. + *
  5519. + * Copyright (C) 2010 Broadcom
  5520. + *
  5521. + * This program is free software; you can redistribute it and/or modify
  5522. + * it under the terms of the GNU General Public License as published by
  5523. + * the Free Software Foundation; either version 2 of the License, or
  5524. + * (at your option) any later version.
  5525. + *
  5526. + * This program is distributed in the hope that it will be useful,
  5527. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5528. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5529. + * GNU General Public License for more details.
  5530. + *
  5531. + * You should have received a copy of the GNU General Public License
  5532. + * along with this program; if not, write to the Free Software
  5533. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5534. + */
  5535. +#ifndef __ASM_ARCH_HARDWARE_H
  5536. +#define __ASM_ARCH_HARDWARE_H
  5537. +
  5538. +#include <asm/sizes.h>
  5539. +#include <mach/platform.h>
  5540. +
  5541. +#endif
  5542. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/io.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/io.h
  5543. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/io.h 1970-01-01 01:00:00.000000000 +0100
  5544. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/io.h 2014-03-11 16:51:54.000000000 +0100
  5545. @@ -0,0 +1,27 @@
  5546. +/*
  5547. + * arch/arm/mach-bcm2708/include/mach/io.h
  5548. + *
  5549. + * Copyright (C) 2003 ARM Limited
  5550. + *
  5551. + * This program is free software; you can redistribute it and/or modify
  5552. + * it under the terms of the GNU General Public License as published by
  5553. + * the Free Software Foundation; either version 2 of the License, or
  5554. + * (at your option) any later version.
  5555. + *
  5556. + * This program is distributed in the hope that it will be useful,
  5557. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5558. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5559. + * GNU General Public License for more details.
  5560. + *
  5561. + * You should have received a copy of the GNU General Public License
  5562. + * along with this program; if not, write to the Free Software
  5563. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5564. + */
  5565. +#ifndef __ASM_ARM_ARCH_IO_H
  5566. +#define __ASM_ARM_ARCH_IO_H
  5567. +
  5568. +#define IO_SPACE_LIMIT 0xffffffff
  5569. +
  5570. +#define __io(a) __typesafe_io(a)
  5571. +
  5572. +#endif
  5573. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/irqs.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/irqs.h
  5574. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/irqs.h 1970-01-01 01:00:00.000000000 +0100
  5575. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/irqs.h 2014-03-11 16:51:54.000000000 +0100
  5576. @@ -0,0 +1,199 @@
  5577. +/*
  5578. + * arch/arm/mach-bcm2708/include/mach/irqs.h
  5579. + *
  5580. + * Copyright (C) 2010 Broadcom
  5581. + * Copyright (C) 2003 ARM Limited
  5582. + * Copyright (C) 2000 Deep Blue Solutions Ltd.
  5583. + *
  5584. + * This program is free software; you can redistribute it and/or modify
  5585. + * it under the terms of the GNU General Public License as published by
  5586. + * the Free Software Foundation; either version 2 of the License, or
  5587. + * (at your option) any later version.
  5588. + *
  5589. + * This program is distributed in the hope that it will be useful,
  5590. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5591. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5592. + * GNU General Public License for more details.
  5593. + *
  5594. + * You should have received a copy of the GNU General Public License
  5595. + * along with this program; if not, write to the Free Software
  5596. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5597. + */
  5598. +
  5599. +#ifndef _BCM2708_IRQS_H_
  5600. +#define _BCM2708_IRQS_H_
  5601. +
  5602. +#include <mach/platform.h>
  5603. +
  5604. +/*
  5605. + * IRQ interrupts definitions are the same as the INT definitions
  5606. + * held within platform.h
  5607. + */
  5608. +#define IRQ_ARMCTRL_START 0
  5609. +#define IRQ_TIMER0 (IRQ_ARMCTRL_START + INTERRUPT_TIMER0)
  5610. +#define IRQ_TIMER1 (IRQ_ARMCTRL_START + INTERRUPT_TIMER1)
  5611. +#define IRQ_TIMER2 (IRQ_ARMCTRL_START + INTERRUPT_TIMER2)
  5612. +#define IRQ_TIMER3 (IRQ_ARMCTRL_START + INTERRUPT_TIMER3)
  5613. +#define IRQ_CODEC0 (IRQ_ARMCTRL_START + INTERRUPT_CODEC0)
  5614. +#define IRQ_CODEC1 (IRQ_ARMCTRL_START + INTERRUPT_CODEC1)
  5615. +#define IRQ_CODEC2 (IRQ_ARMCTRL_START + INTERRUPT_CODEC2)
  5616. +#define IRQ_JPEG (IRQ_ARMCTRL_START + INTERRUPT_JPEG)
  5617. +#define IRQ_ISP (IRQ_ARMCTRL_START + INTERRUPT_ISP)
  5618. +#define IRQ_USB (IRQ_ARMCTRL_START + INTERRUPT_USB)
  5619. +#define IRQ_3D (IRQ_ARMCTRL_START + INTERRUPT_3D)
  5620. +#define IRQ_TRANSPOSER (IRQ_ARMCTRL_START + INTERRUPT_TRANSPOSER)
  5621. +#define IRQ_MULTICORESYNC0 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC0)
  5622. +#define IRQ_MULTICORESYNC1 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC1)
  5623. +#define IRQ_MULTICORESYNC2 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC2)
  5624. +#define IRQ_MULTICORESYNC3 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC3)
  5625. +#define IRQ_DMA0 (IRQ_ARMCTRL_START + INTERRUPT_DMA0)
  5626. +#define IRQ_DMA1 (IRQ_ARMCTRL_START + INTERRUPT_DMA1)
  5627. +#define IRQ_DMA2 (IRQ_ARMCTRL_START + INTERRUPT_DMA2)
  5628. +#define IRQ_DMA3 (IRQ_ARMCTRL_START + INTERRUPT_DMA3)
  5629. +#define IRQ_DMA4 (IRQ_ARMCTRL_START + INTERRUPT_DMA4)
  5630. +#define IRQ_DMA5 (IRQ_ARMCTRL_START + INTERRUPT_DMA5)
  5631. +#define IRQ_DMA6 (IRQ_ARMCTRL_START + INTERRUPT_DMA6)
  5632. +#define IRQ_DMA7 (IRQ_ARMCTRL_START + INTERRUPT_DMA7)
  5633. +#define IRQ_DMA8 (IRQ_ARMCTRL_START + INTERRUPT_DMA8)
  5634. +#define IRQ_DMA9 (IRQ_ARMCTRL_START + INTERRUPT_DMA9)
  5635. +#define IRQ_DMA10 (IRQ_ARMCTRL_START + INTERRUPT_DMA10)
  5636. +#define IRQ_DMA11 (IRQ_ARMCTRL_START + INTERRUPT_DMA11)
  5637. +#define IRQ_DMA12 (IRQ_ARMCTRL_START + INTERRUPT_DMA12)
  5638. +#define IRQ_AUX (IRQ_ARMCTRL_START + INTERRUPT_AUX)
  5639. +#define IRQ_ARM (IRQ_ARMCTRL_START + INTERRUPT_ARM)
  5640. +#define IRQ_VPUDMA (IRQ_ARMCTRL_START + INTERRUPT_VPUDMA)
  5641. +#define IRQ_HOSTPORT (IRQ_ARMCTRL_START + INTERRUPT_HOSTPORT)
  5642. +#define IRQ_VIDEOSCALER (IRQ_ARMCTRL_START + INTERRUPT_VIDEOSCALER)
  5643. +#define IRQ_CCP2TX (IRQ_ARMCTRL_START + INTERRUPT_CCP2TX)
  5644. +#define IRQ_SDC (IRQ_ARMCTRL_START + INTERRUPT_SDC)
  5645. +#define IRQ_DSI0 (IRQ_ARMCTRL_START + INTERRUPT_DSI0)
  5646. +#define IRQ_AVE (IRQ_ARMCTRL_START + INTERRUPT_AVE)
  5647. +#define IRQ_CAM0 (IRQ_ARMCTRL_START + INTERRUPT_CAM0)
  5648. +#define IRQ_CAM1 (IRQ_ARMCTRL_START + INTERRUPT_CAM1)
  5649. +#define IRQ_HDMI0 (IRQ_ARMCTRL_START + INTERRUPT_HDMI0)
  5650. +#define IRQ_HDMI1 (IRQ_ARMCTRL_START + INTERRUPT_HDMI1)
  5651. +#define IRQ_PIXELVALVE1 (IRQ_ARMCTRL_START + INTERRUPT_PIXELVALVE1)
  5652. +#define IRQ_I2CSPISLV (IRQ_ARMCTRL_START + INTERRUPT_I2CSPISLV)
  5653. +#define IRQ_DSI1 (IRQ_ARMCTRL_START + INTERRUPT_DSI1)
  5654. +#define IRQ_PWA0 (IRQ_ARMCTRL_START + INTERRUPT_PWA0)
  5655. +#define IRQ_PWA1 (IRQ_ARMCTRL_START + INTERRUPT_PWA1)
  5656. +#define IRQ_CPR (IRQ_ARMCTRL_START + INTERRUPT_CPR)
  5657. +#define IRQ_SMI (IRQ_ARMCTRL_START + INTERRUPT_SMI)
  5658. +#define IRQ_GPIO0 (IRQ_ARMCTRL_START + INTERRUPT_GPIO0)
  5659. +#define IRQ_GPIO1 (IRQ_ARMCTRL_START + INTERRUPT_GPIO1)
  5660. +#define IRQ_GPIO2 (IRQ_ARMCTRL_START + INTERRUPT_GPIO2)
  5661. +#define IRQ_GPIO3 (IRQ_ARMCTRL_START + INTERRUPT_GPIO3)
  5662. +#define IRQ_I2C (IRQ_ARMCTRL_START + INTERRUPT_I2C)
  5663. +#define IRQ_SPI (IRQ_ARMCTRL_START + INTERRUPT_SPI)
  5664. +#define IRQ_I2SPCM (IRQ_ARMCTRL_START + INTERRUPT_I2SPCM)
  5665. +#define IRQ_SDIO (IRQ_ARMCTRL_START + INTERRUPT_SDIO)
  5666. +#define IRQ_UART (IRQ_ARMCTRL_START + INTERRUPT_UART)
  5667. +#define IRQ_SLIMBUS (IRQ_ARMCTRL_START + INTERRUPT_SLIMBUS)
  5668. +#define IRQ_VEC (IRQ_ARMCTRL_START + INTERRUPT_VEC)
  5669. +#define IRQ_CPG (IRQ_ARMCTRL_START + INTERRUPT_CPG)
  5670. +#define IRQ_RNG (IRQ_ARMCTRL_START + INTERRUPT_RNG)
  5671. +#define IRQ_ARASANSDIO (IRQ_ARMCTRL_START + INTERRUPT_ARASANSDIO)
  5672. +#define IRQ_AVSPMON (IRQ_ARMCTRL_START + INTERRUPT_AVSPMON)
  5673. +
  5674. +#define IRQ_ARM_TIMER (IRQ_ARMCTRL_START + INTERRUPT_ARM_TIMER)
  5675. +#define IRQ_ARM_MAILBOX (IRQ_ARMCTRL_START + INTERRUPT_ARM_MAILBOX)
  5676. +#define IRQ_ARM_DOORBELL_0 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_0)
  5677. +#define IRQ_ARM_DOORBELL_1 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_1)
  5678. +#define IRQ_VPU0_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU0_HALTED)
  5679. +#define IRQ_VPU1_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU1_HALTED)
  5680. +#define IRQ_ILLEGAL_TYPE0 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE0)
  5681. +#define IRQ_ILLEGAL_TYPE1 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE1)
  5682. +#define IRQ_PENDING1 (IRQ_ARMCTRL_START + INTERRUPT_PENDING1)
  5683. +#define IRQ_PENDING2 (IRQ_ARMCTRL_START + INTERRUPT_PENDING2)
  5684. +
  5685. +#define FIQ_START HARD_IRQS
  5686. +
  5687. +/*
  5688. + * FIQ interrupts definitions are the same as the INT definitions.
  5689. + */
  5690. +#define FIQ_TIMER0 (FIQ_START+INTERRUPT_TIMER0)
  5691. +#define FIQ_TIMER1 (FIQ_START+INTERRUPT_TIMER1)
  5692. +#define FIQ_TIMER2 (FIQ_START+INTERRUPT_TIMER2)
  5693. +#define FIQ_TIMER3 (FIQ_START+INTERRUPT_TIMER3)
  5694. +#define FIQ_CODEC0 (FIQ_START+INTERRUPT_CODEC0)
  5695. +#define FIQ_CODEC1 (FIQ_START+INTERRUPT_CODEC1)
  5696. +#define FIQ_CODEC2 (FIQ_START+INTERRUPT_CODEC2)
  5697. +#define FIQ_JPEG (FIQ_START+INTERRUPT_JPEG)
  5698. +#define FIQ_ISP (FIQ_START+INTERRUPT_ISP)
  5699. +#define FIQ_USB (FIQ_START+INTERRUPT_USB)
  5700. +#define FIQ_3D (FIQ_START+INTERRUPT_3D)
  5701. +#define FIQ_TRANSPOSER (FIQ_START+INTERRUPT_TRANSPOSER)
  5702. +#define FIQ_MULTICORESYNC0 (FIQ_START+INTERRUPT_MULTICORESYNC0)
  5703. +#define FIQ_MULTICORESYNC1 (FIQ_START+INTERRUPT_MULTICORESYNC1)
  5704. +#define FIQ_MULTICORESYNC2 (FIQ_START+INTERRUPT_MULTICORESYNC2)
  5705. +#define FIQ_MULTICORESYNC3 (FIQ_START+INTERRUPT_MULTICORESYNC3)
  5706. +#define FIQ_DMA0 (FIQ_START+INTERRUPT_DMA0)
  5707. +#define FIQ_DMA1 (FIQ_START+INTERRUPT_DMA1)
  5708. +#define FIQ_DMA2 (FIQ_START+INTERRUPT_DMA2)
  5709. +#define FIQ_DMA3 (FIQ_START+INTERRUPT_DMA3)
  5710. +#define FIQ_DMA4 (FIQ_START+INTERRUPT_DMA4)
  5711. +#define FIQ_DMA5 (FIQ_START+INTERRUPT_DMA5)
  5712. +#define FIQ_DMA6 (FIQ_START+INTERRUPT_DMA6)
  5713. +#define FIQ_DMA7 (FIQ_START+INTERRUPT_DMA7)
  5714. +#define FIQ_DMA8 (FIQ_START+INTERRUPT_DMA8)
  5715. +#define FIQ_DMA9 (FIQ_START+INTERRUPT_DMA9)
  5716. +#define FIQ_DMA10 (FIQ_START+INTERRUPT_DMA10)
  5717. +#define FIQ_DMA11 (FIQ_START+INTERRUPT_DMA11)
  5718. +#define FIQ_DMA12 (FIQ_START+INTERRUPT_DMA12)
  5719. +#define FIQ_AUX (FIQ_START+INTERRUPT_AUX)
  5720. +#define FIQ_ARM (FIQ_START+INTERRUPT_ARM)
  5721. +#define FIQ_VPUDMA (FIQ_START+INTERRUPT_VPUDMA)
  5722. +#define FIQ_HOSTPORT (FIQ_START+INTERRUPT_HOSTPORT)
  5723. +#define FIQ_VIDEOSCALER (FIQ_START+INTERRUPT_VIDEOSCALER)
  5724. +#define FIQ_CCP2TX (FIQ_START+INTERRUPT_CCP2TX)
  5725. +#define FIQ_SDC (FIQ_START+INTERRUPT_SDC)
  5726. +#define FIQ_DSI0 (FIQ_START+INTERRUPT_DSI0)
  5727. +#define FIQ_AVE (FIQ_START+INTERRUPT_AVE)
  5728. +#define FIQ_CAM0 (FIQ_START+INTERRUPT_CAM0)
  5729. +#define FIQ_CAM1 (FIQ_START+INTERRUPT_CAM1)
  5730. +#define FIQ_HDMI0 (FIQ_START+INTERRUPT_HDMI0)
  5731. +#define FIQ_HDMI1 (FIQ_START+INTERRUPT_HDMI1)
  5732. +#define FIQ_PIXELVALVE1 (FIQ_START+INTERRUPT_PIXELVALVE1)
  5733. +#define FIQ_I2CSPISLV (FIQ_START+INTERRUPT_I2CSPISLV)
  5734. +#define FIQ_DSI1 (FIQ_START+INTERRUPT_DSI1)
  5735. +#define FIQ_PWA0 (FIQ_START+INTERRUPT_PWA0)
  5736. +#define FIQ_PWA1 (FIQ_START+INTERRUPT_PWA1)
  5737. +#define FIQ_CPR (FIQ_START+INTERRUPT_CPR)
  5738. +#define FIQ_SMI (FIQ_START+INTERRUPT_SMI)
  5739. +#define FIQ_GPIO0 (FIQ_START+INTERRUPT_GPIO0)
  5740. +#define FIQ_GPIO1 (FIQ_START+INTERRUPT_GPIO1)
  5741. +#define FIQ_GPIO2 (FIQ_START+INTERRUPT_GPIO2)
  5742. +#define FIQ_GPIO3 (FIQ_START+INTERRUPT_GPIO3)
  5743. +#define FIQ_I2C (FIQ_START+INTERRUPT_I2C)
  5744. +#define FIQ_SPI (FIQ_START+INTERRUPT_SPI)
  5745. +#define FIQ_I2SPCM (FIQ_START+INTERRUPT_I2SPCM)
  5746. +#define FIQ_SDIO (FIQ_START+INTERRUPT_SDIO)
  5747. +#define FIQ_UART (FIQ_START+INTERRUPT_UART)
  5748. +#define FIQ_SLIMBUS (FIQ_START+INTERRUPT_SLIMBUS)
  5749. +#define FIQ_VEC (FIQ_START+INTERRUPT_VEC)
  5750. +#define FIQ_CPG (FIQ_START+INTERRUPT_CPG)
  5751. +#define FIQ_RNG (FIQ_START+INTERRUPT_RNG)
  5752. +#define FIQ_ARASANSDIO (FIQ_START+INTERRUPT_ARASANSDIO)
  5753. +#define FIQ_AVSPMON (FIQ_START+INTERRUPT_AVSPMON)
  5754. +
  5755. +#define FIQ_ARM_TIMER (FIQ_START+INTERRUPT_ARM_TIMER)
  5756. +#define FIQ_ARM_MAILBOX (FIQ_START+INTERRUPT_ARM_MAILBOX)
  5757. +#define FIQ_ARM_DOORBELL_0 (FIQ_START+INTERRUPT_ARM_DOORBELL_0)
  5758. +#define FIQ_ARM_DOORBELL_1 (FIQ_START+INTERRUPT_ARM_DOORBELL_1)
  5759. +#define FIQ_VPU0_HALTED (FIQ_START+INTERRUPT_VPU0_HALTED)
  5760. +#define FIQ_VPU1_HALTED (FIQ_START+INTERRUPT_VPU1_HALTED)
  5761. +#define FIQ_ILLEGAL_TYPE0 (FIQ_START+INTERRUPT_ILLEGAL_TYPE0)
  5762. +#define FIQ_ILLEGAL_TYPE1 (FIQ_START+INTERRUPT_ILLEGAL_TYPE1)
  5763. +#define FIQ_PENDING1 (FIQ_START+INTERRUPT_PENDING1)
  5764. +#define FIQ_PENDING2 (FIQ_START+INTERRUPT_PENDING2)
  5765. +
  5766. +#define GPIO_IRQ_START (HARD_IRQS + FIQ_IRQS)
  5767. +
  5768. +#define HARD_IRQS (64 + 21)
  5769. +#define FIQ_IRQS (64 + 21)
  5770. +#define GPIO_IRQS (32*5)
  5771. +
  5772. +#define NR_IRQS HARD_IRQS+FIQ_IRQS+GPIO_IRQS
  5773. +
  5774. +
  5775. +#endif /* _BCM2708_IRQS_H_ */
  5776. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/memory.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/memory.h
  5777. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/memory.h 1970-01-01 01:00:00.000000000 +0100
  5778. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/memory.h 2014-03-11 16:51:54.000000000 +0100
  5779. @@ -0,0 +1,57 @@
  5780. +/*
  5781. + * arch/arm/mach-bcm2708/include/mach/memory.h
  5782. + *
  5783. + * Copyright (C) 2010 Broadcom
  5784. + *
  5785. + * This program is free software; you can redistribute it and/or modify
  5786. + * it under the terms of the GNU General Public License as published by
  5787. + * the Free Software Foundation; either version 2 of the License, or
  5788. + * (at your option) any later version.
  5789. + *
  5790. + * This program is distributed in the hope that it will be useful,
  5791. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5792. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5793. + * GNU General Public License for more details.
  5794. + *
  5795. + * You should have received a copy of the GNU General Public License
  5796. + * along with this program; if not, write to the Free Software
  5797. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5798. + */
  5799. +#ifndef __ASM_ARCH_MEMORY_H
  5800. +#define __ASM_ARCH_MEMORY_H
  5801. +
  5802. +/* Memory overview:
  5803. +
  5804. + [ARMcore] <--virtual addr-->
  5805. + [ARMmmu] <--physical addr-->
  5806. + [GERTmap] <--bus add-->
  5807. + [VCperiph]
  5808. +
  5809. +*/
  5810. +
  5811. +/*
  5812. + * Physical DRAM offset.
  5813. + */
  5814. +#define PLAT_PHYS_OFFSET UL(0x00000000)
  5815. +#define VC_ARMMEM_OFFSET UL(0x00000000) /* offset in VC of ARM memory */
  5816. +
  5817. +#ifdef CONFIG_BCM2708_NOL2CACHE
  5818. + #define _REAL_BUS_OFFSET UL(0xC0000000) /* don't use L1 or L2 caches */
  5819. +#else
  5820. + #define _REAL_BUS_OFFSET UL(0x40000000) /* use L2 cache */
  5821. +#endif
  5822. +
  5823. +/* We're using the memory at 64M in the VideoCore for Linux - this adjustment
  5824. + * will provide the offset into this area as well as setting the bits that
  5825. + * stop the L1 and L2 cache from being used
  5826. + *
  5827. + * WARNING: this only works because the ARM is given memory at a fixed location
  5828. + * (ARMMEM_OFFSET)
  5829. + */
  5830. +#define BUS_OFFSET (VC_ARMMEM_OFFSET + _REAL_BUS_OFFSET)
  5831. +#define __virt_to_bus(x) ((x) + (BUS_OFFSET - PAGE_OFFSET))
  5832. +#define __bus_to_virt(x) ((x) - (BUS_OFFSET - PAGE_OFFSET))
  5833. +#define __pfn_to_bus(x) (__pfn_to_phys(x) + (BUS_OFFSET - PLAT_PHYS_OFFSET))
  5834. +#define __bus_to_pfn(x) __phys_to_pfn((x) - (BUS_OFFSET - PLAT_PHYS_OFFSET))
  5835. +
  5836. +#endif
  5837. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/platform.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/platform.h
  5838. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/platform.h 1970-01-01 01:00:00.000000000 +0100
  5839. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/platform.h 2014-03-11 16:51:54.000000000 +0100
  5840. @@ -0,0 +1,228 @@
  5841. +/*
  5842. + * arch/arm/mach-bcm2708/include/mach/platform.h
  5843. + *
  5844. + * Copyright (C) 2010 Broadcom
  5845. + *
  5846. + * This program is free software; you can redistribute it and/or modify
  5847. + * it under the terms of the GNU General Public License as published by
  5848. + * the Free Software Foundation; either version 2 of the License, or
  5849. + * (at your option) any later version.
  5850. + *
  5851. + * This program is distributed in the hope that it will be useful,
  5852. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5853. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5854. + * GNU General Public License for more details.
  5855. + *
  5856. + * You should have received a copy of the GNU General Public License
  5857. + * along with this program; if not, write to the Free Software
  5858. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5859. + */
  5860. +
  5861. +#ifndef _BCM2708_PLATFORM_H
  5862. +#define _BCM2708_PLATFORM_H
  5863. +
  5864. +
  5865. +/* macros to get at IO space when running virtually */
  5866. +#define IO_ADDRESS(x) (((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
  5867. +
  5868. +#define __io_address(n) IOMEM(IO_ADDRESS(n))
  5869. +
  5870. +
  5871. +/*
  5872. + * SDRAM
  5873. + */
  5874. +#define BCM2708_SDRAM_BASE 0x00000000
  5875. +
  5876. +/*
  5877. + * Logic expansion modules
  5878. + *
  5879. + */
  5880. +
  5881. +
  5882. +/* ------------------------------------------------------------------------
  5883. + * BCM2708 ARMCTRL Registers
  5884. + * ------------------------------------------------------------------------
  5885. + */
  5886. +
  5887. +#define HW_REGISTER_RW(addr) (addr)
  5888. +#define HW_REGISTER_RO(addr) (addr)
  5889. +
  5890. +#include "arm_control.h"
  5891. +#undef ARM_BASE
  5892. +
  5893. +/*
  5894. + * Definitions and addresses for the ARM CONTROL logic
  5895. + * This file is manually generated.
  5896. + */
  5897. +
  5898. +#define BCM2708_PERI_BASE 0x20000000
  5899. +#define IC0_BASE (BCM2708_PERI_BASE + 0x2000)
  5900. +#define ST_BASE (BCM2708_PERI_BASE + 0x3000) /* System Timer */
  5901. +#define MPHI_BASE (BCM2708_PERI_BASE + 0x6000) /* Message -based Parallel Host Interface */
  5902. +#define DMA_BASE (BCM2708_PERI_BASE + 0x7000) /* DMA controller */
  5903. +#define ARM_BASE (BCM2708_PERI_BASE + 0xB000) /* BCM2708 ARM control block */
  5904. +#define PM_BASE (BCM2708_PERI_BASE + 0x100000) /* Power Management, Reset controller and Watchdog registers */
  5905. +#define PCM_CLOCK_BASE (BCM2708_PERI_BASE + 0x101098) /* PCM Clock */
  5906. +#define RNG_BASE (BCM2708_PERI_BASE + 0x104000) /* Hardware RNG */
  5907. +#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO */
  5908. +#define UART0_BASE (BCM2708_PERI_BASE + 0x201000) /* Uart 0 */
  5909. +#define MMCI0_BASE (BCM2708_PERI_BASE + 0x202000) /* MMC interface */
  5910. +#define I2S_BASE (BCM2708_PERI_BASE + 0x203000) /* I2S */
  5911. +#define SPI0_BASE (BCM2708_PERI_BASE + 0x204000) /* SPI0 */
  5912. +#define BSC0_BASE (BCM2708_PERI_BASE + 0x205000) /* BSC0 I2C/TWI */
  5913. +#define UART1_BASE (BCM2708_PERI_BASE + 0x215000) /* Uart 1 */
  5914. +#define EMMC_BASE (BCM2708_PERI_BASE + 0x300000) /* eMMC interface */
  5915. +#define SMI_BASE (BCM2708_PERI_BASE + 0x600000) /* SMI */
  5916. +#define BSC1_BASE (BCM2708_PERI_BASE + 0x804000) /* BSC1 I2C/TWI */
  5917. +#define USB_BASE (BCM2708_PERI_BASE + 0x980000) /* DTC_OTG USB controller */
  5918. +#define MCORE_BASE (BCM2708_PERI_BASE + 0x0000) /* Fake frame buffer device (actually the multicore sync block*/
  5919. +
  5920. +#define ARMCTRL_BASE (ARM_BASE + 0x000)
  5921. +#define ARMCTRL_IC_BASE (ARM_BASE + 0x200) /* ARM interrupt controller */
  5922. +#define ARMCTRL_TIMER0_1_BASE (ARM_BASE + 0x400) /* Timer 0 and 1 */
  5923. +#define ARMCTRL_0_SBM_BASE (ARM_BASE + 0x800) /* User 0 (ARM)'s Semaphores Doorbells and Mailboxes */
  5924. +
  5925. +
  5926. +/*
  5927. + * Interrupt assignments
  5928. + */
  5929. +
  5930. +#define ARM_IRQ1_BASE 0
  5931. +#define INTERRUPT_TIMER0 (ARM_IRQ1_BASE + 0)
  5932. +#define INTERRUPT_TIMER1 (ARM_IRQ1_BASE + 1)
  5933. +#define INTERRUPT_TIMER2 (ARM_IRQ1_BASE + 2)
  5934. +#define INTERRUPT_TIMER3 (ARM_IRQ1_BASE + 3)
  5935. +#define INTERRUPT_CODEC0 (ARM_IRQ1_BASE + 4)
  5936. +#define INTERRUPT_CODEC1 (ARM_IRQ1_BASE + 5)
  5937. +#define INTERRUPT_CODEC2 (ARM_IRQ1_BASE + 6)
  5938. +#define INTERRUPT_VC_JPEG (ARM_IRQ1_BASE + 7)
  5939. +#define INTERRUPT_ISP (ARM_IRQ1_BASE + 8)
  5940. +#define INTERRUPT_VC_USB (ARM_IRQ1_BASE + 9)
  5941. +#define INTERRUPT_VC_3D (ARM_IRQ1_BASE + 10)
  5942. +#define INTERRUPT_TRANSPOSER (ARM_IRQ1_BASE + 11)
  5943. +#define INTERRUPT_MULTICORESYNC0 (ARM_IRQ1_BASE + 12)
  5944. +#define INTERRUPT_MULTICORESYNC1 (ARM_IRQ1_BASE + 13)
  5945. +#define INTERRUPT_MULTICORESYNC2 (ARM_IRQ1_BASE + 14)
  5946. +#define INTERRUPT_MULTICORESYNC3 (ARM_IRQ1_BASE + 15)
  5947. +#define INTERRUPT_DMA0 (ARM_IRQ1_BASE + 16)
  5948. +#define INTERRUPT_DMA1 (ARM_IRQ1_BASE + 17)
  5949. +#define INTERRUPT_VC_DMA2 (ARM_IRQ1_BASE + 18)
  5950. +#define INTERRUPT_VC_DMA3 (ARM_IRQ1_BASE + 19)
  5951. +#define INTERRUPT_DMA4 (ARM_IRQ1_BASE + 20)
  5952. +#define INTERRUPT_DMA5 (ARM_IRQ1_BASE + 21)
  5953. +#define INTERRUPT_DMA6 (ARM_IRQ1_BASE + 22)
  5954. +#define INTERRUPT_DMA7 (ARM_IRQ1_BASE + 23)
  5955. +#define INTERRUPT_DMA8 (ARM_IRQ1_BASE + 24)
  5956. +#define INTERRUPT_DMA9 (ARM_IRQ1_BASE + 25)
  5957. +#define INTERRUPT_DMA10 (ARM_IRQ1_BASE + 26)
  5958. +#define INTERRUPT_DMA11 (ARM_IRQ1_BASE + 27)
  5959. +#define INTERRUPT_DMA12 (ARM_IRQ1_BASE + 28)
  5960. +#define INTERRUPT_AUX (ARM_IRQ1_BASE + 29)
  5961. +#define INTERRUPT_ARM (ARM_IRQ1_BASE + 30)
  5962. +#define INTERRUPT_VPUDMA (ARM_IRQ1_BASE + 31)
  5963. +
  5964. +#define ARM_IRQ2_BASE 32
  5965. +#define INTERRUPT_HOSTPORT (ARM_IRQ2_BASE + 0)
  5966. +#define INTERRUPT_VIDEOSCALER (ARM_IRQ2_BASE + 1)
  5967. +#define INTERRUPT_CCP2TX (ARM_IRQ2_BASE + 2)
  5968. +#define INTERRUPT_SDC (ARM_IRQ2_BASE + 3)
  5969. +#define INTERRUPT_DSI0 (ARM_IRQ2_BASE + 4)
  5970. +#define INTERRUPT_AVE (ARM_IRQ2_BASE + 5)
  5971. +#define INTERRUPT_CAM0 (ARM_IRQ2_BASE + 6)
  5972. +#define INTERRUPT_CAM1 (ARM_IRQ2_BASE + 7)
  5973. +#define INTERRUPT_HDMI0 (ARM_IRQ2_BASE + 8)
  5974. +#define INTERRUPT_HDMI1 (ARM_IRQ2_BASE + 9)
  5975. +#define INTERRUPT_PIXELVALVE1 (ARM_IRQ2_BASE + 10)
  5976. +#define INTERRUPT_I2CSPISLV (ARM_IRQ2_BASE + 11)
  5977. +#define INTERRUPT_DSI1 (ARM_IRQ2_BASE + 12)
  5978. +#define INTERRUPT_PWA0 (ARM_IRQ2_BASE + 13)
  5979. +#define INTERRUPT_PWA1 (ARM_IRQ2_BASE + 14)
  5980. +#define INTERRUPT_CPR (ARM_IRQ2_BASE + 15)
  5981. +#define INTERRUPT_SMI (ARM_IRQ2_BASE + 16)
  5982. +#define INTERRUPT_GPIO0 (ARM_IRQ2_BASE + 17)
  5983. +#define INTERRUPT_GPIO1 (ARM_IRQ2_BASE + 18)
  5984. +#define INTERRUPT_GPIO2 (ARM_IRQ2_BASE + 19)
  5985. +#define INTERRUPT_GPIO3 (ARM_IRQ2_BASE + 20)
  5986. +#define INTERRUPT_VC_I2C (ARM_IRQ2_BASE + 21)
  5987. +#define INTERRUPT_VC_SPI (ARM_IRQ2_BASE + 22)
  5988. +#define INTERRUPT_VC_I2SPCM (ARM_IRQ2_BASE + 23)
  5989. +#define INTERRUPT_VC_SDIO (ARM_IRQ2_BASE + 24)
  5990. +#define INTERRUPT_VC_UART (ARM_IRQ2_BASE + 25)
  5991. +#define INTERRUPT_SLIMBUS (ARM_IRQ2_BASE + 26)
  5992. +#define INTERRUPT_VEC (ARM_IRQ2_BASE + 27)
  5993. +#define INTERRUPT_CPG (ARM_IRQ2_BASE + 28)
  5994. +#define INTERRUPT_RNG (ARM_IRQ2_BASE + 29)
  5995. +#define INTERRUPT_VC_ARASANSDIO (ARM_IRQ2_BASE + 30)
  5996. +#define INTERRUPT_AVSPMON (ARM_IRQ2_BASE + 31)
  5997. +
  5998. +#define ARM_IRQ0_BASE 64
  5999. +#define INTERRUPT_ARM_TIMER (ARM_IRQ0_BASE + 0)
  6000. +#define INTERRUPT_ARM_MAILBOX (ARM_IRQ0_BASE + 1)
  6001. +#define INTERRUPT_ARM_DOORBELL_0 (ARM_IRQ0_BASE + 2)
  6002. +#define INTERRUPT_ARM_DOORBELL_1 (ARM_IRQ0_BASE + 3)
  6003. +#define INTERRUPT_VPU0_HALTED (ARM_IRQ0_BASE + 4)
  6004. +#define INTERRUPT_VPU1_HALTED (ARM_IRQ0_BASE + 5)
  6005. +#define INTERRUPT_ILLEGAL_TYPE0 (ARM_IRQ0_BASE + 6)
  6006. +#define INTERRUPT_ILLEGAL_TYPE1 (ARM_IRQ0_BASE + 7)
  6007. +#define INTERRUPT_PENDING1 (ARM_IRQ0_BASE + 8)
  6008. +#define INTERRUPT_PENDING2 (ARM_IRQ0_BASE + 9)
  6009. +#define INTERRUPT_JPEG (ARM_IRQ0_BASE + 10)
  6010. +#define INTERRUPT_USB (ARM_IRQ0_BASE + 11)
  6011. +#define INTERRUPT_3D (ARM_IRQ0_BASE + 12)
  6012. +#define INTERRUPT_DMA2 (ARM_IRQ0_BASE + 13)
  6013. +#define INTERRUPT_DMA3 (ARM_IRQ0_BASE + 14)
  6014. +#define INTERRUPT_I2C (ARM_IRQ0_BASE + 15)
  6015. +#define INTERRUPT_SPI (ARM_IRQ0_BASE + 16)
  6016. +#define INTERRUPT_I2SPCM (ARM_IRQ0_BASE + 17)
  6017. +#define INTERRUPT_SDIO (ARM_IRQ0_BASE + 18)
  6018. +#define INTERRUPT_UART (ARM_IRQ0_BASE + 19)
  6019. +#define INTERRUPT_ARASANSDIO (ARM_IRQ0_BASE + 20)
  6020. +
  6021. +#define MAXIRQNUM (32 + 32 + 20)
  6022. +#define MAXFIQNUM (32 + 32 + 20)
  6023. +
  6024. +#define MAX_TIMER 2
  6025. +#define MAX_PERIOD 699050
  6026. +#define TICKS_PER_uSEC 1
  6027. +
  6028. +/*
  6029. + * These are useconds NOT ticks.
  6030. + *
  6031. + */
  6032. +#define mSEC_1 1000
  6033. +#define mSEC_5 (mSEC_1 * 5)
  6034. +#define mSEC_10 (mSEC_1 * 10)
  6035. +#define mSEC_25 (mSEC_1 * 25)
  6036. +#define SEC_1 (mSEC_1 * 1000)
  6037. +
  6038. +/*
  6039. + * Watchdog
  6040. + */
  6041. +#define PM_RSTC (PM_BASE+0x1c)
  6042. +#define PM_RSTS (PM_BASE+0x20)
  6043. +#define PM_WDOG (PM_BASE+0x24)
  6044. +
  6045. +#define PM_WDOG_RESET 0000000000
  6046. +#define PM_PASSWORD 0x5a000000
  6047. +#define PM_WDOG_TIME_SET 0x000fffff
  6048. +#define PM_RSTC_WRCFG_CLR 0xffffffcf
  6049. +#define PM_RSTC_WRCFG_SET 0x00000030
  6050. +#define PM_RSTC_WRCFG_FULL_RESET 0x00000020
  6051. +#define PM_RSTC_RESET 0x00000102
  6052. +
  6053. +#define PM_RSTS_HADPOR_SET 0x00001000
  6054. +#define PM_RSTS_HADSRH_SET 0x00000400
  6055. +#define PM_RSTS_HADSRF_SET 0x00000200
  6056. +#define PM_RSTS_HADSRQ_SET 0x00000100
  6057. +#define PM_RSTS_HADWRH_SET 0x00000040
  6058. +#define PM_RSTS_HADWRF_SET 0x00000020
  6059. +#define PM_RSTS_HADWRQ_SET 0x00000010
  6060. +#define PM_RSTS_HADDRH_SET 0x00000004
  6061. +#define PM_RSTS_HADDRF_SET 0x00000002
  6062. +#define PM_RSTS_HADDRQ_SET 0x00000001
  6063. +
  6064. +#define UART0_CLOCK 3000000
  6065. +
  6066. +#endif
  6067. +
  6068. +/* END */
  6069. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/power.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/power.h
  6070. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/power.h 1970-01-01 01:00:00.000000000 +0100
  6071. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/power.h 2014-03-11 16:51:54.000000000 +0100
  6072. @@ -0,0 +1,26 @@
  6073. +/*
  6074. + * linux/arch/arm/mach-bcm2708/power.h
  6075. + *
  6076. + * Copyright (C) 2010 Broadcom
  6077. + *
  6078. + * This program is free software; you can redistribute it and/or modify
  6079. + * it under the terms of the GNU General Public License version 2 as
  6080. + * published by the Free Software Foundation.
  6081. + *
  6082. + * This device provides a shared mechanism for controlling the power to
  6083. + * VideoCore subsystems.
  6084. + */
  6085. +
  6086. +#ifndef _MACH_BCM2708_POWER_H
  6087. +#define _MACH_BCM2708_POWER_H
  6088. +
  6089. +#include <linux/types.h>
  6090. +#include <mach/arm_power.h>
  6091. +
  6092. +typedef unsigned int BCM_POWER_HANDLE_T;
  6093. +
  6094. +extern int bcm_power_open(BCM_POWER_HANDLE_T *handle);
  6095. +extern int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request);
  6096. +extern int bcm_power_close(BCM_POWER_HANDLE_T handle);
  6097. +
  6098. +#endif
  6099. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/system.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/system.h
  6100. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/system.h 1970-01-01 01:00:00.000000000 +0100
  6101. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/system.h 2014-03-11 16:51:54.000000000 +0100
  6102. @@ -0,0 +1,38 @@
  6103. +/*
  6104. + * arch/arm/mach-bcm2708/include/mach/system.h
  6105. + *
  6106. + * Copyright (C) 2010 Broadcom
  6107. + * Copyright (C) 2003 ARM Limited
  6108. + * Copyright (C) 2000 Deep Blue Solutions Ltd
  6109. + *
  6110. + * This program is free software; you can redistribute it and/or modify
  6111. + * it under the terms of the GNU General Public License as published by
  6112. + * the Free Software Foundation; either version 2 of the License, or
  6113. + * (at your option) any later version.
  6114. + *
  6115. + * This program is distributed in the hope that it will be useful,
  6116. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6117. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6118. + * GNU General Public License for more details.
  6119. + *
  6120. + * You should have received a copy of the GNU General Public License
  6121. + * along with this program; if not, write to the Free Software
  6122. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6123. + */
  6124. +#ifndef __ASM_ARCH_SYSTEM_H
  6125. +#define __ASM_ARCH_SYSTEM_H
  6126. +
  6127. +#include <linux/io.h>
  6128. +#include <mach/hardware.h>
  6129. +#include <mach/platform.h>
  6130. +
  6131. +static inline void arch_idle(void)
  6132. +{
  6133. + /*
  6134. + * This should do all the clock switching
  6135. + * and wait for interrupt tricks
  6136. + */
  6137. + cpu_do_idle();
  6138. +}
  6139. +
  6140. +#endif
  6141. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/timex.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/timex.h
  6142. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/timex.h 1970-01-01 01:00:00.000000000 +0100
  6143. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/timex.h 2014-03-11 16:51:54.000000000 +0100
  6144. @@ -0,0 +1,23 @@
  6145. +/*
  6146. + * arch/arm/mach-bcm2708/include/mach/timex.h
  6147. + *
  6148. + * BCM2708 sysem clock frequency
  6149. + *
  6150. + * Copyright (C) 2010 Broadcom
  6151. + *
  6152. + * This program is free software; you can redistribute it and/or modify
  6153. + * it under the terms of the GNU General Public License as published by
  6154. + * the Free Software Foundation; either version 2 of the License, or
  6155. + * (at your option) any later version.
  6156. + *
  6157. + * This program is distributed in the hope that it will be useful,
  6158. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6159. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6160. + * GNU General Public License for more details.
  6161. + *
  6162. + * You should have received a copy of the GNU General Public License
  6163. + * along with this program; if not, write to the Free Software
  6164. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6165. + */
  6166. +
  6167. +#define CLOCK_TICK_RATE (1000000)
  6168. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/uncompress.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/uncompress.h
  6169. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/uncompress.h 1970-01-01 01:00:00.000000000 +0100
  6170. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/uncompress.h 2014-03-11 16:54:55.000000000 +0100
  6171. @@ -0,0 +1,84 @@
  6172. +/*
  6173. + * arch/arm/mach-bcn2708/include/mach/uncompress.h
  6174. + *
  6175. + * Copyright (C) 2010 Broadcom
  6176. + * Copyright (C) 2003 ARM Limited
  6177. + *
  6178. + * This program is free software; you can redistribute it and/or modify
  6179. + * it under the terms of the GNU General Public License as published by
  6180. + * the Free Software Foundation; either version 2 of the License, or
  6181. + * (at your option) any later version.
  6182. + *
  6183. + * This program is distributed in the hope that it will be useful,
  6184. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6185. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6186. + * GNU General Public License for more details.
  6187. + *
  6188. + * You should have received a copy of the GNU General Public License
  6189. + * along with this program; if not, write to the Free Software
  6190. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6191. + */
  6192. +
  6193. +#include <linux/io.h>
  6194. +#include <linux/amba/serial.h>
  6195. +#include <mach/hardware.h>
  6196. +
  6197. +#define UART_BAUD 115200
  6198. +
  6199. +#define BCM2708_UART_DR __io(UART0_BASE + UART01x_DR)
  6200. +#define BCM2708_UART_FR __io(UART0_BASE + UART01x_FR)
  6201. +#define BCM2708_UART_IBRD __io(UART0_BASE + UART011_IBRD)
  6202. +#define BCM2708_UART_FBRD __io(UART0_BASE + UART011_FBRD)
  6203. +#define BCM2708_UART_LCRH __io(UART0_BASE + UART011_LCRH)
  6204. +#define BCM2708_UART_CR __io(UART0_BASE + UART011_CR)
  6205. +
  6206. +/*
  6207. + * This does not append a newline
  6208. + */
  6209. +static inline void putc(int c)
  6210. +{
  6211. + while (__raw_readl(BCM2708_UART_FR) & UART01x_FR_TXFF)
  6212. + barrier();
  6213. +
  6214. + __raw_writel(c, BCM2708_UART_DR);
  6215. +}
  6216. +
  6217. +static inline void flush(void)
  6218. +{
  6219. + int fr;
  6220. +
  6221. + do {
  6222. + fr = __raw_readl(BCM2708_UART_FR);
  6223. + barrier();
  6224. + } while ((fr & (UART011_FR_TXFE | UART01x_FR_BUSY)) != UART011_FR_TXFE);
  6225. +}
  6226. +
  6227. +static inline void arch_decomp_setup(void)
  6228. +{
  6229. + int temp, div, rem, frac;
  6230. +
  6231. + temp = 16 * UART_BAUD;
  6232. + div = UART0_CLOCK / temp;
  6233. + rem = UART0_CLOCK % temp;
  6234. + temp = (8 * rem) / UART_BAUD;
  6235. + frac = (temp >> 1) + (temp & 1);
  6236. +
  6237. + /* Make sure the UART is disabled before we start */
  6238. + __raw_writel(0, BCM2708_UART_CR);
  6239. +
  6240. + /* Set the baud rate */
  6241. + __raw_writel(div, BCM2708_UART_IBRD);
  6242. + __raw_writel(frac, BCM2708_UART_FBRD);
  6243. +
  6244. + /* Set the UART to 8n1, FIFO enabled */
  6245. + __raw_writel(UART01x_LCRH_WLEN_8 | UART01x_LCRH_FEN, BCM2708_UART_LCRH);
  6246. +
  6247. + /* Enable the UART */
  6248. + __raw_writel(UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_RXE,
  6249. + BCM2708_UART_CR);
  6250. +}
  6251. +
  6252. +/*
  6253. + * nothing to do
  6254. + */
  6255. +#define arch_decomp_wdog()
  6256. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/vcio.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vcio.h
  6257. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/vcio.h 1970-01-01 01:00:00.000000000 +0100
  6258. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vcio.h 2014-03-11 16:54:55.000000000 +0100
  6259. @@ -0,0 +1,141 @@
  6260. +/*
  6261. + * arch/arm/mach-bcm2708/include/mach/vcio.h
  6262. + *
  6263. + * Copyright (C) 2010 Broadcom
  6264. + *
  6265. + * This program is free software; you can redistribute it and/or modify
  6266. + * it under the terms of the GNU General Public License as published by
  6267. + * the Free Software Foundation; either version 2 of the License, or
  6268. + * (at your option) any later version.
  6269. + *
  6270. + * This program is distributed in the hope that it will be useful,
  6271. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6272. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6273. + * GNU General Public License for more details.
  6274. + *
  6275. + * You should have received a copy of the GNU General Public License
  6276. + * along with this program; if not, write to the Free Software
  6277. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6278. + */
  6279. +#ifndef _MACH_BCM2708_VCIO_H
  6280. +#define _MACH_BCM2708_VCIO_H
  6281. +
  6282. +/* Routines to handle I/O via the VideoCore "ARM control" registers
  6283. + * (semaphores, doorbells, mailboxes)
  6284. + */
  6285. +
  6286. +#define BCM_VCIO_DRIVER_NAME "bcm2708_vcio"
  6287. +
  6288. +/* Constants shared with the ARM identifying separate mailbox channels */
  6289. +#define MBOX_CHAN_POWER 0 /* for use by the power management interface */
  6290. +#define MBOX_CHAN_FB 1 /* for use by the frame buffer */
  6291. +#define MBOX_CHAN_VCHIQ 3 /* for use by the VCHIQ interface */
  6292. +#define MBOX_CHAN_PROPERTY 8 /* for use by the property channel */
  6293. +#define MBOX_CHAN_COUNT 9
  6294. +
  6295. +/* Mailbox property tags */
  6296. +enum {
  6297. + VCMSG_PROPERTY_END = 0x00000000,
  6298. + VCMSG_GET_FIRMWARE_REVISION = 0x00000001,
  6299. + VCMSG_GET_BOARD_MODEL = 0x00010001,
  6300. + VCMSG_GET_BOARD_REVISION = 0x00020002,
  6301. + VCMSG_GET_BOARD_MAC_ADDRESS = 0x00020003,
  6302. + VCMSG_GET_BOARD_SERIAL = 0x00020004,
  6303. + VCMSG_GET_ARM_MEMORY = 0x00020005,
  6304. + VCMSG_GET_VC_MEMORY = 0x00020006,
  6305. + VCMSG_GET_CLOCKS = 0x00020007,
  6306. + VCMSG_GET_COMMAND_LINE = 0x00050001,
  6307. + VCMSG_GET_DMA_CHANNELS = 0x00060001,
  6308. + VCMSG_GET_POWER_STATE = 0x00020001,
  6309. + VCMSG_GET_TIMING = 0x00020002,
  6310. + VCMSG_SET_POWER_STATE = 0x00028001,
  6311. + VCMSG_GET_CLOCK_STATE = 0x00030001,
  6312. + VCMSG_SET_CLOCK_STATE = 0x00038001,
  6313. + VCMSG_GET_CLOCK_RATE = 0x00030002,
  6314. + VCMSG_SET_CLOCK_RATE = 0x00038002,
  6315. + VCMSG_GET_VOLTAGE = 0x00030003,
  6316. + VCMSG_SET_VOLTAGE = 0x00038003,
  6317. + VCMSG_GET_MAX_CLOCK = 0x00030004,
  6318. + VCMSG_GET_MAX_VOLTAGE = 0x00030005,
  6319. + VCMSG_GET_TEMPERATURE = 0x00030006,
  6320. + VCMSG_GET_MIN_CLOCK = 0x00030007,
  6321. + VCMSG_GET_MIN_VOLTAGE = 0x00030008,
  6322. + VCMSG_GET_TURBO = 0x00030009,
  6323. + VCMSG_SET_TURBO = 0x00038009,
  6324. + VCMSG_SET_ALLOCATE_BUFFER = 0x00040001,
  6325. + VCMSG_SET_RELEASE_BUFFER = 0x00048001,
  6326. + VCMSG_SET_BLANK_SCREEN = 0x00040002,
  6327. + VCMSG_TST_BLANK_SCREEN = 0x00044002,
  6328. + VCMSG_GET_PHYSICAL_WIDTH_HEIGHT = 0x00040003,
  6329. + VCMSG_TST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
  6330. + VCMSG_SET_PHYSICAL_WIDTH_HEIGHT = 0x00048003,
  6331. + VCMSG_GET_VIRTUAL_WIDTH_HEIGHT = 0x00040004,
  6332. + VCMSG_TST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,
  6333. + VCMSG_SET_VIRTUAL_WIDTH_HEIGHT = 0x00048004,
  6334. + VCMSG_GET_DEPTH = 0x00040005,
  6335. + VCMSG_TST_DEPTH = 0x00044005,
  6336. + VCMSG_SET_DEPTH = 0x00048005,
  6337. + VCMSG_GET_PIXEL_ORDER = 0x00040006,
  6338. + VCMSG_TST_PIXEL_ORDER = 0x00044006,
  6339. + VCMSG_SET_PIXEL_ORDER = 0x00048006,
  6340. + VCMSG_GET_ALPHA_MODE = 0x00040007,
  6341. + VCMSG_TST_ALPHA_MODE = 0x00044007,
  6342. + VCMSG_SET_ALPHA_MODE = 0x00048007,
  6343. + VCMSG_GET_PITCH = 0x00040008,
  6344. + VCMSG_TST_PITCH = 0x00044008,
  6345. + VCMSG_SET_PITCH = 0x00048008,
  6346. + VCMSG_GET_VIRTUAL_OFFSET = 0x00040009,
  6347. + VCMSG_TST_VIRTUAL_OFFSET = 0x00044009,
  6348. + VCMSG_SET_VIRTUAL_OFFSET = 0x00048009,
  6349. + VCMSG_GET_OVERSCAN = 0x0004000a,
  6350. + VCMSG_TST_OVERSCAN = 0x0004400a,
  6351. + VCMSG_SET_OVERSCAN = 0x0004800a,
  6352. + VCMSG_GET_PALETTE = 0x0004000b,
  6353. + VCMSG_TST_PALETTE = 0x0004400b,
  6354. + VCMSG_SET_PALETTE = 0x0004800b,
  6355. + VCMSG_GET_LAYER = 0x0004000c,
  6356. + VCMSG_TST_LAYER = 0x0004400c,
  6357. + VCMSG_SET_LAYER = 0x0004800c,
  6358. + VCMSG_GET_TRANSFORM = 0x0004000d,
  6359. + VCMSG_TST_TRANSFORM = 0x0004400d,
  6360. + VCMSG_SET_TRANSFORM = 0x0004800d,
  6361. +};
  6362. +
  6363. +extern int /*rc*/ bcm_mailbox_read(unsigned chan, uint32_t *data28);
  6364. +extern int /*rc*/ bcm_mailbox_write(unsigned chan, uint32_t data28);
  6365. +extern int /*rc*/ bcm_mailbox_property(void *data, int size);
  6366. +
  6367. +#include <linux/ioctl.h>
  6368. +
  6369. +/*
  6370. + * The major device number. We can't rely on dynamic
  6371. + * registration any more, because ioctls need to know
  6372. + * it.
  6373. + */
  6374. +#define MAJOR_NUM 100
  6375. +
  6376. +/*
  6377. + * Set the message of the device driver
  6378. + */
  6379. +#define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *)
  6380. +/*
  6381. + * _IOWR means that we're creating an ioctl command
  6382. + * number for passing information from a user process
  6383. + * to the kernel module and from the kernel module to user process
  6384. + *
  6385. + * The first arguments, MAJOR_NUM, is the major device
  6386. + * number we're using.
  6387. + *
  6388. + * The second argument is the number of the command
  6389. + * (there could be several with different meanings).
  6390. + *
  6391. + * The third argument is the type we want to get from
  6392. + * the process to the kernel.
  6393. + */
  6394. +
  6395. +/*
  6396. + * The name of the device file
  6397. + */
  6398. +#define DEVICE_FILE_NAME "char_dev"
  6399. +
  6400. +#endif
  6401. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/vc_mem.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vc_mem.h
  6402. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/vc_mem.h 1970-01-01 01:00:00.000000000 +0100
  6403. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vc_mem.h 2014-03-11 16:54:55.000000000 +0100
  6404. @@ -0,0 +1,35 @@
  6405. +/*****************************************************************************
  6406. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  6407. +*
  6408. +* Unless you and Broadcom execute a separate written software license
  6409. +* agreement governing use of this software, this software is licensed to you
  6410. +* under the terms of the GNU General Public License version 2, available at
  6411. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  6412. +*
  6413. +* Notwithstanding the above, under no circumstances may you combine this
  6414. +* software in any way with any other Broadcom software provided under a
  6415. +* license other than the GPL, without Broadcom's express prior written
  6416. +* consent.
  6417. +*****************************************************************************/
  6418. +
  6419. +#if !defined( VC_MEM_H )
  6420. +#define VC_MEM_H
  6421. +
  6422. +#include <linux/ioctl.h>
  6423. +
  6424. +#define VC_MEM_IOC_MAGIC 'v'
  6425. +
  6426. +#define VC_MEM_IOC_MEM_PHYS_ADDR _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
  6427. +#define VC_MEM_IOC_MEM_SIZE _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
  6428. +#define VC_MEM_IOC_MEM_BASE _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
  6429. +#define VC_MEM_IOC_MEM_LOAD _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
  6430. +
  6431. +#if defined( __KERNEL__ )
  6432. +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
  6433. +
  6434. +extern unsigned long mm_vc_mem_phys_addr;
  6435. +extern unsigned int mm_vc_mem_size;
  6436. +extern int vc_mem_get_current_size( void );
  6437. +#endif
  6438. +
  6439. +#endif /* VC_MEM_H */
  6440. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/include/mach/vmalloc.h linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vmalloc.h
  6441. --- linux-3.13.6/arch/arm/mach-bcm2708/include/mach/vmalloc.h 1970-01-01 01:00:00.000000000 +0100
  6442. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/include/mach/vmalloc.h 2014-03-11 16:51:54.000000000 +0100
  6443. @@ -0,0 +1,20 @@
  6444. +/*
  6445. + * arch/arm/mach-bcm2708/include/mach/vmalloc.h
  6446. + *
  6447. + * Copyright (C) 2010 Broadcom
  6448. + *
  6449. + * This program is free software; you can redistribute it and/or modify
  6450. + * it under the terms of the GNU General Public License as published by
  6451. + * the Free Software Foundation; either version 2 of the License, or
  6452. + * (at your option) any later version.
  6453. + *
  6454. + * This program is distributed in the hope that it will be useful,
  6455. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6456. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6457. + * GNU General Public License for more details.
  6458. + *
  6459. + * You should have received a copy of the GNU General Public License
  6460. + * along with this program; if not, write to the Free Software
  6461. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6462. + */
  6463. +#define VMALLOC_END (0xe8000000)
  6464. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/Kconfig linux-raspberry-pi/arch/arm/mach-bcm2708/Kconfig
  6465. --- linux-3.13.6/arch/arm/mach-bcm2708/Kconfig 1970-01-01 01:00:00.000000000 +0100
  6466. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/Kconfig 2014-03-11 16:54:55.000000000 +0100
  6467. @@ -0,0 +1,41 @@
  6468. +menu "Broadcom BCM2708 Implementations"
  6469. + depends on ARCH_BCM2708
  6470. +
  6471. +config MACH_BCM2708
  6472. + bool "Broadcom BCM2708 Development Platform"
  6473. + select NEED_MACH_MEMORY_H
  6474. + select NEED_MACH_IO_H
  6475. + select CPU_V6
  6476. + help
  6477. + Include support for the Broadcom(R) BCM2708 platform.
  6478. +
  6479. +config BCM2708_GPIO
  6480. + bool "BCM2708 gpio support"
  6481. + depends on MACH_BCM2708
  6482. + select ARCH_REQUIRE_GPIOLIB
  6483. + default y
  6484. + help
  6485. + Include support for the Broadcom(R) BCM2708 gpio.
  6486. +
  6487. +config BCM2708_VCMEM
  6488. + bool "Videocore Memory"
  6489. + depends on MACH_BCM2708
  6490. + default y
  6491. + help
  6492. + Helper for videocore memory access and total size allocation.
  6493. +
  6494. +config BCM2708_NOL2CACHE
  6495. + bool "Videocore L2 cache disable"
  6496. + depends on MACH_BCM2708
  6497. + default n
  6498. + help
  6499. + Do not allow ARM to use GPU's L2 cache. Requires disable_l2cache in config.txt.
  6500. +
  6501. +config BCM2708_SPIDEV
  6502. + bool "Bind spidev to SPI0 master"
  6503. + depends on MACH_BCM2708
  6504. + depends on SPI
  6505. + default y
  6506. + help
  6507. + Binds spidev driver to the SPI0 master
  6508. +endmenu
  6509. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/Makefile linux-raspberry-pi/arch/arm/mach-bcm2708/Makefile
  6510. --- linux-3.13.6/arch/arm/mach-bcm2708/Makefile 1970-01-01 01:00:00.000000000 +0100
  6511. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/Makefile 2014-03-11 16:54:55.000000000 +0100
  6512. @@ -0,0 +1,7 @@
  6513. +#
  6514. +# Makefile for the linux kernel.
  6515. +#
  6516. +
  6517. +obj-$(CONFIG_MACH_BCM2708) += clock.o bcm2708.o armctrl.o vcio.o power.o dma.o
  6518. +obj-$(CONFIG_BCM2708_GPIO) += bcm2708_gpio.o
  6519. +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
  6520. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/Makefile.boot linux-raspberry-pi/arch/arm/mach-bcm2708/Makefile.boot
  6521. --- linux-3.13.6/arch/arm/mach-bcm2708/Makefile.boot 1970-01-01 01:00:00.000000000 +0100
  6522. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/Makefile.boot 2014-03-11 16:51:54.000000000 +0100
  6523. @@ -0,0 +1,3 @@
  6524. + zreladdr-y := 0x00008000
  6525. +params_phys-y := 0x00000100
  6526. +initrd_phys-y := 0x00800000
  6527. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/power.c linux-raspberry-pi/arch/arm/mach-bcm2708/power.c
  6528. --- linux-3.13.6/arch/arm/mach-bcm2708/power.c 1970-01-01 01:00:00.000000000 +0100
  6529. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/power.c 2014-03-11 16:51:54.000000000 +0100
  6530. @@ -0,0 +1,194 @@
  6531. +/*
  6532. + * linux/arch/arm/mach-bcm2708/power.c
  6533. + *
  6534. + * Copyright (C) 2010 Broadcom
  6535. + *
  6536. + * This program is free software; you can redistribute it and/or modify
  6537. + * it under the terms of the GNU General Public License version 2 as
  6538. + * published by the Free Software Foundation.
  6539. + *
  6540. + * This device provides a shared mechanism for controlling the power to
  6541. + * VideoCore subsystems.
  6542. + */
  6543. +
  6544. +#include <linux/module.h>
  6545. +#include <linux/semaphore.h>
  6546. +#include <linux/bug.h>
  6547. +#include <mach/power.h>
  6548. +#include <mach/vcio.h>
  6549. +#include <mach/arm_power.h>
  6550. +
  6551. +#define DRIVER_NAME "bcm2708_power"
  6552. +
  6553. +#define BCM_POWER_MAXCLIENTS 4
  6554. +#define BCM_POWER_NOCLIENT (1<<31)
  6555. +
  6556. +/* Some drivers expect there devices to be permanently powered */
  6557. +#define BCM_POWER_ALWAYS_ON (BCM_POWER_USB)
  6558. +
  6559. +#if 1
  6560. +#define DPRINTK printk
  6561. +#else
  6562. +#define DPRINTK if (0) printk
  6563. +#endif
  6564. +
  6565. +struct state_struct {
  6566. + uint32_t global_request;
  6567. + uint32_t client_request[BCM_POWER_MAXCLIENTS];
  6568. + struct semaphore client_mutex;
  6569. + struct semaphore mutex;
  6570. +} g_state;
  6571. +
  6572. +int bcm_power_open(BCM_POWER_HANDLE_T *handle)
  6573. +{
  6574. + BCM_POWER_HANDLE_T i;
  6575. + int ret = -EBUSY;
  6576. +
  6577. + down(&g_state.client_mutex);
  6578. +
  6579. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  6580. + if (g_state.client_request[i] == BCM_POWER_NOCLIENT) {
  6581. + g_state.client_request[i] = BCM_POWER_NONE;
  6582. + *handle = i;
  6583. + ret = 0;
  6584. + break;
  6585. + }
  6586. + }
  6587. +
  6588. + up(&g_state.client_mutex);
  6589. +
  6590. + DPRINTK("bcm_power_open() -> %d\n", *handle);
  6591. +
  6592. + return ret;
  6593. +}
  6594. +EXPORT_SYMBOL_GPL(bcm_power_open);
  6595. +
  6596. +int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request)
  6597. +{
  6598. + int rc = 0;
  6599. +
  6600. + DPRINTK("bcm_power_request(%d, %x)\n", handle, request);
  6601. +
  6602. + if ((handle < BCM_POWER_MAXCLIENTS) &&
  6603. + (g_state.client_request[handle] != BCM_POWER_NOCLIENT)) {
  6604. + if (down_interruptible(&g_state.mutex) != 0) {
  6605. + DPRINTK("bcm_power_request -> interrupted\n");
  6606. + return -EINTR;
  6607. + }
  6608. +
  6609. + if (request != g_state.client_request[handle]) {
  6610. + uint32_t others_request = 0;
  6611. + uint32_t global_request;
  6612. + BCM_POWER_HANDLE_T i;
  6613. +
  6614. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  6615. + if (i != handle)
  6616. + others_request |=
  6617. + g_state.client_request[i];
  6618. + }
  6619. + others_request &= ~BCM_POWER_NOCLIENT;
  6620. +
  6621. + global_request = request | others_request;
  6622. + if (global_request != g_state.global_request) {
  6623. + uint32_t actual;
  6624. +
  6625. + /* Send a request to VideoCore */
  6626. + bcm_mailbox_write(MBOX_CHAN_POWER,
  6627. + global_request << 4);
  6628. +
  6629. + /* Wait for a response during power-up */
  6630. + if (global_request & ~g_state.global_request) {
  6631. + rc = bcm_mailbox_read(MBOX_CHAN_POWER,
  6632. + &actual);
  6633. + DPRINTK
  6634. + ("bcm_mailbox_read -> %08x, %d\n",
  6635. + actual, rc);
  6636. + actual >>= 4;
  6637. + } else {
  6638. + rc = 0;
  6639. + actual = global_request;
  6640. + }
  6641. +
  6642. + if (rc == 0) {
  6643. + if (actual != global_request) {
  6644. + printk(KERN_ERR
  6645. + "%s: prev global %x, new global %x, actual %x, request %x, others_request %x\n",
  6646. + __func__,
  6647. + g_state.global_request,
  6648. + global_request, actual, request, others_request);
  6649. + /* A failure */
  6650. + BUG_ON((others_request & actual)
  6651. + != others_request);
  6652. + request &= actual;
  6653. + rc = -EIO;
  6654. + }
  6655. +
  6656. + g_state.global_request = actual;
  6657. + g_state.client_request[handle] =
  6658. + request;
  6659. + }
  6660. + }
  6661. + }
  6662. + up(&g_state.mutex);
  6663. + } else {
  6664. + rc = -EINVAL;
  6665. + }
  6666. + DPRINTK("bcm_power_request -> %d\n", rc);
  6667. + return rc;
  6668. +}
  6669. +EXPORT_SYMBOL_GPL(bcm_power_request);
  6670. +
  6671. +int bcm_power_close(BCM_POWER_HANDLE_T handle)
  6672. +{
  6673. + int rc;
  6674. +
  6675. + DPRINTK("bcm_power_close(%d)\n", handle);
  6676. +
  6677. + rc = bcm_power_request(handle, BCM_POWER_NONE);
  6678. + if (rc == 0)
  6679. + g_state.client_request[handle] = BCM_POWER_NOCLIENT;
  6680. +
  6681. + return rc;
  6682. +}
  6683. +EXPORT_SYMBOL_GPL(bcm_power_close);
  6684. +
  6685. +static int __init bcm_power_init(void)
  6686. +{
  6687. +#if defined(BCM_POWER_ALWAYS_ON)
  6688. + BCM_POWER_HANDLE_T always_on_handle;
  6689. +#endif
  6690. + int rc = 0;
  6691. + int i;
  6692. +
  6693. + printk(KERN_INFO "bcm_power: Broadcom power driver\n");
  6694. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  6695. +
  6696. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++)
  6697. + g_state.client_request[i] = BCM_POWER_NOCLIENT;
  6698. +
  6699. + sema_init(&g_state.client_mutex, 1);
  6700. + sema_init(&g_state.mutex, 1);
  6701. +
  6702. + g_state.global_request = 0;
  6703. +
  6704. +#if defined(BCM_POWER_ALWAYS_ON)
  6705. + if (BCM_POWER_ALWAYS_ON) {
  6706. + bcm_power_open(&always_on_handle);
  6707. + bcm_power_request(always_on_handle, BCM_POWER_ALWAYS_ON);
  6708. + }
  6709. +#endif
  6710. +
  6711. + return rc;
  6712. +}
  6713. +
  6714. +static void __exit bcm_power_exit(void)
  6715. +{
  6716. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  6717. +}
  6718. +
  6719. +arch_initcall(bcm_power_init); /* Initialize early */
  6720. +module_exit(bcm_power_exit);
  6721. +
  6722. +MODULE_AUTHOR("Phil Elwell");
  6723. +MODULE_DESCRIPTION("Interface to BCM2708 power management");
  6724. +MODULE_LICENSE("GPL");
  6725. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/vcio.c linux-raspberry-pi/arch/arm/mach-bcm2708/vcio.c
  6726. --- linux-3.13.6/arch/arm/mach-bcm2708/vcio.c 1970-01-01 01:00:00.000000000 +0100
  6727. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/vcio.c 2014-03-11 16:54:55.000000000 +0100
  6728. @@ -0,0 +1,474 @@
  6729. +/*
  6730. + * linux/arch/arm/mach-bcm2708/vcio.c
  6731. + *
  6732. + * Copyright (C) 2010 Broadcom
  6733. + *
  6734. + * This program is free software; you can redistribute it and/or modify
  6735. + * it under the terms of the GNU General Public License version 2 as
  6736. + * published by the Free Software Foundation.
  6737. + *
  6738. + * This device provides a shared mechanism for writing to the mailboxes,
  6739. + * semaphores, doorbells etc. that are shared between the ARM and the
  6740. + * VideoCore processor
  6741. + */
  6742. +
  6743. +#if defined(CONFIG_SERIAL_BCM_MBOX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  6744. +#define SUPPORT_SYSRQ
  6745. +#endif
  6746. +
  6747. +#include <linux/module.h>
  6748. +#include <linux/console.h>
  6749. +#include <linux/serial_core.h>
  6750. +#include <linux/serial.h>
  6751. +#include <linux/errno.h>
  6752. +#include <linux/device.h>
  6753. +#include <linux/init.h>
  6754. +#include <linux/mm.h>
  6755. +#include <linux/dma-mapping.h>
  6756. +#include <linux/platform_device.h>
  6757. +#include <linux/sysrq.h>
  6758. +#include <linux/delay.h>
  6759. +#include <linux/slab.h>
  6760. +#include <linux/interrupt.h>
  6761. +#include <linux/irq.h>
  6762. +
  6763. +#include <linux/io.h>
  6764. +
  6765. +#include <mach/vcio.h>
  6766. +#include <mach/platform.h>
  6767. +
  6768. +#include <asm/uaccess.h>
  6769. +
  6770. +
  6771. +#define DRIVER_NAME BCM_VCIO_DRIVER_NAME
  6772. +
  6773. +/* ----------------------------------------------------------------------
  6774. + * Mailbox
  6775. + * -------------------------------------------------------------------- */
  6776. +
  6777. +/* offsets from a mail box base address */
  6778. +#define MAIL_WRT 0x00 /* write - and next 4 words */
  6779. +#define MAIL_RD 0x00 /* read - and next 4 words */
  6780. +#define MAIL_POL 0x10 /* read without popping the fifo */
  6781. +#define MAIL_SND 0x14 /* sender ID (bottom two bits) */
  6782. +#define MAIL_STA 0x18 /* status */
  6783. +#define MAIL_CNF 0x1C /* configuration */
  6784. +
  6785. +#define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
  6786. +#define MBOX_MSG_LSB(chan, data28) (((data28) << 4) | ((chan) & 0xf))
  6787. +#define MBOX_CHAN(msg) ((msg) & 0xf)
  6788. +#define MBOX_DATA28(msg) ((msg) & ~0xf)
  6789. +#define MBOX_DATA28_LSB(msg) (((uint32_t)msg) >> 4)
  6790. +
  6791. +#define MBOX_MAGIC 0xd0d0c0de
  6792. +
  6793. +struct vc_mailbox {
  6794. + struct device *dev; /* parent device */
  6795. + void __iomem *status;
  6796. + void __iomem *config;
  6797. + void __iomem *read;
  6798. + void __iomem *write;
  6799. + uint32_t msg[MBOX_CHAN_COUNT];
  6800. + struct semaphore sema[MBOX_CHAN_COUNT];
  6801. + uint32_t magic;
  6802. +};
  6803. +
  6804. +static void mbox_init(struct vc_mailbox *mbox_out, struct device *dev,
  6805. + uint32_t addr_mbox)
  6806. +{
  6807. + int i;
  6808. +
  6809. + mbox_out->dev = dev;
  6810. + mbox_out->status = __io_address(addr_mbox + MAIL_STA);
  6811. + mbox_out->config = __io_address(addr_mbox + MAIL_CNF);
  6812. + mbox_out->read = __io_address(addr_mbox + MAIL_RD);
  6813. + /* Write to the other mailbox */
  6814. + mbox_out->write =
  6815. + __io_address((addr_mbox ^ ARM_0_MAIL0_WRT ^ ARM_0_MAIL1_WRT) +
  6816. + MAIL_WRT);
  6817. +
  6818. + for (i = 0; i < MBOX_CHAN_COUNT; i++) {
  6819. + mbox_out->msg[i] = 0;
  6820. + sema_init(&mbox_out->sema[i], 0);
  6821. + }
  6822. +
  6823. + /* Enable the interrupt on data reception */
  6824. + writel(ARM_MC_IHAVEDATAIRQEN, mbox_out->config);
  6825. +
  6826. + mbox_out->magic = MBOX_MAGIC;
  6827. +}
  6828. +
  6829. +static int mbox_write(struct vc_mailbox *mbox, unsigned chan, uint32_t data28)
  6830. +{
  6831. + int rc;
  6832. +
  6833. + if (mbox->magic != MBOX_MAGIC)
  6834. + rc = -EINVAL;
  6835. + else {
  6836. + /* wait for the mailbox FIFO to have some space in it */
  6837. + while (0 != (readl(mbox->status) & ARM_MS_FULL))
  6838. + cpu_relax();
  6839. +
  6840. + writel(MBOX_MSG(chan, data28), mbox->write);
  6841. + rc = 0;
  6842. + }
  6843. + return rc;
  6844. +}
  6845. +
  6846. +static int mbox_read(struct vc_mailbox *mbox, unsigned chan, uint32_t *data28)
  6847. +{
  6848. + int rc;
  6849. +
  6850. + if (mbox->magic != MBOX_MAGIC)
  6851. + rc = -EINVAL;
  6852. + else {
  6853. + down(&mbox->sema[chan]);
  6854. + *data28 = MBOX_DATA28(mbox->msg[chan]);
  6855. + mbox->msg[chan] = 0;
  6856. + rc = 0;
  6857. + }
  6858. + return rc;
  6859. +}
  6860. +
  6861. +static irqreturn_t mbox_irq(int irq, void *dev_id)
  6862. +{
  6863. + /* wait for the mailbox FIFO to have some data in it */
  6864. + struct vc_mailbox *mbox = (struct vc_mailbox *) dev_id;
  6865. + int status = readl(mbox->status);
  6866. + int ret = IRQ_NONE;
  6867. +
  6868. + while (!(status & ARM_MS_EMPTY)) {
  6869. + uint32_t msg = readl(mbox->read);
  6870. + int chan = MBOX_CHAN(msg);
  6871. + if (chan < MBOX_CHAN_COUNT) {
  6872. + if (mbox->msg[chan]) {
  6873. + /* Overflow */
  6874. + printk(KERN_ERR DRIVER_NAME
  6875. + ": mbox chan %d overflow - drop %08x\n",
  6876. + chan, msg);
  6877. + } else {
  6878. + mbox->msg[chan] = (msg | 0xf);
  6879. + up(&mbox->sema[chan]);
  6880. + }
  6881. + } else {
  6882. + printk(KERN_ERR DRIVER_NAME
  6883. + ": invalid channel selector (msg %08x)\n", msg);
  6884. + }
  6885. + ret = IRQ_HANDLED;
  6886. + status = readl(mbox->status);
  6887. + }
  6888. + return ret;
  6889. +}
  6890. +
  6891. +static struct irqaction mbox_irqaction = {
  6892. + .name = "ARM Mailbox IRQ",
  6893. + .flags = IRQF_DISABLED | IRQF_IRQPOLL,
  6894. + .handler = mbox_irq,
  6895. +};
  6896. +
  6897. +/* ----------------------------------------------------------------------
  6898. + * Mailbox Methods
  6899. + * -------------------------------------------------------------------- */
  6900. +
  6901. +static struct device *mbox_dev; /* we assume there's only one! */
  6902. +
  6903. +static int dev_mbox_write(struct device *dev, unsigned chan, uint32_t data28)
  6904. +{
  6905. + int rc;
  6906. +
  6907. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  6908. + device_lock(dev);
  6909. + rc = mbox_write(mailbox, chan, data28);
  6910. + device_unlock(dev);
  6911. +
  6912. + return rc;
  6913. +}
  6914. +
  6915. +static int dev_mbox_read(struct device *dev, unsigned chan, uint32_t *data28)
  6916. +{
  6917. + int rc;
  6918. +
  6919. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  6920. + device_lock(dev);
  6921. + rc = mbox_read(mailbox, chan, data28);
  6922. + device_unlock(dev);
  6923. +
  6924. + return rc;
  6925. +}
  6926. +
  6927. +extern int bcm_mailbox_write(unsigned chan, uint32_t data28)
  6928. +{
  6929. + if (mbox_dev)
  6930. + return dev_mbox_write(mbox_dev, chan, data28);
  6931. + else
  6932. + return -ENODEV;
  6933. +}
  6934. +EXPORT_SYMBOL_GPL(bcm_mailbox_write);
  6935. +
  6936. +extern int bcm_mailbox_read(unsigned chan, uint32_t *data28)
  6937. +{
  6938. + if (mbox_dev)
  6939. + return dev_mbox_read(mbox_dev, chan, data28);
  6940. + else
  6941. + return -ENODEV;
  6942. +}
  6943. +EXPORT_SYMBOL_GPL(bcm_mailbox_read);
  6944. +
  6945. +static void dev_mbox_register(const char *dev_name, struct device *dev)
  6946. +{
  6947. + mbox_dev = dev;
  6948. +}
  6949. +
  6950. +static int mbox_copy_from_user(void *dst, const void *src, int size)
  6951. +{
  6952. + if ( (uint32_t)src < TASK_SIZE)
  6953. + {
  6954. + return copy_from_user(dst, src, size);
  6955. + }
  6956. + else
  6957. + {
  6958. + memcpy( dst, src, size );
  6959. + return 0;
  6960. + }
  6961. +}
  6962. +
  6963. +static int mbox_copy_to_user(void *dst, const void *src, int size)
  6964. +{
  6965. + if ( (uint32_t)dst < TASK_SIZE)
  6966. + {
  6967. + return copy_to_user(dst, src, size);
  6968. + }
  6969. + else
  6970. + {
  6971. + memcpy( dst, src, size );
  6972. + return 0;
  6973. + }
  6974. +}
  6975. +
  6976. +static DEFINE_MUTEX(mailbox_lock);
  6977. +extern int bcm_mailbox_property(void *data, int size)
  6978. +{
  6979. + uint32_t success;
  6980. + dma_addr_t mem_bus; /* the memory address accessed from videocore */
  6981. + void *mem_kern; /* the memory address accessed from driver */
  6982. + int s = 0;
  6983. +
  6984. + mutex_lock(&mailbox_lock);
  6985. + /* allocate some memory for the messages communicating with GPU */
  6986. + mem_kern = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &mem_bus, GFP_ATOMIC);
  6987. + if (mem_kern) {
  6988. + /* create the message */
  6989. + mbox_copy_from_user(mem_kern, data, size);
  6990. +
  6991. + /* send the message */
  6992. + wmb();
  6993. + s = bcm_mailbox_write(MBOX_CHAN_PROPERTY, (uint32_t)mem_bus);
  6994. + if (s == 0) {
  6995. + s = bcm_mailbox_read(MBOX_CHAN_PROPERTY, &success);
  6996. + }
  6997. + if (s == 0) {
  6998. + /* copy the response */
  6999. + rmb();
  7000. + mbox_copy_to_user(data, mem_kern, size);
  7001. + }
  7002. + dma_free_coherent(NULL, PAGE_ALIGN(size), mem_kern, mem_bus);
  7003. + } else {
  7004. + s = -ENOMEM;
  7005. + }
  7006. + if (s != 0)
  7007. + printk(KERN_ERR DRIVER_NAME ": %s failed (%d)\n", __func__, s);
  7008. +
  7009. + mutex_unlock(&mailbox_lock);
  7010. + return s;
  7011. +}
  7012. +EXPORT_SYMBOL_GPL(bcm_mailbox_property);
  7013. +
  7014. +/* ----------------------------------------------------------------------
  7015. + * Platform Device for Mailbox
  7016. + * -------------------------------------------------------------------- */
  7017. +
  7018. +/*
  7019. + * Is the device open right now? Used to prevent
  7020. + * concurent access into the same device
  7021. + */
  7022. +static int Device_Open = 0;
  7023. +
  7024. +/*
  7025. + * This is called whenever a process attempts to open the device file
  7026. + */
  7027. +static int device_open(struct inode *inode, struct file *file)
  7028. +{
  7029. + /*
  7030. + * We don't want to talk to two processes at the same time
  7031. + */
  7032. + if (Device_Open)
  7033. + return -EBUSY;
  7034. +
  7035. + Device_Open++;
  7036. + /*
  7037. + * Initialize the message
  7038. + */
  7039. + try_module_get(THIS_MODULE);
  7040. + return 0;
  7041. +}
  7042. +
  7043. +static int device_release(struct inode *inode, struct file *file)
  7044. +{
  7045. + /*
  7046. + * We're now ready for our next caller
  7047. + */
  7048. + Device_Open--;
  7049. +
  7050. + module_put(THIS_MODULE);
  7051. + return 0;
  7052. +}
  7053. +
  7054. +/*
  7055. + * This function is called whenever a process tries to do an ioctl on our
  7056. + * device file. We get two extra parameters (additional to the inode and file
  7057. + * structures, which all device functions get): the number of the ioctl called
  7058. + * and the parameter given to the ioctl function.
  7059. + *
  7060. + * If the ioctl is write or read/write (meaning output is returned to the
  7061. + * calling process), the ioctl call returns the output of this function.
  7062. + *
  7063. + */
  7064. +static long device_ioctl(struct file *file, /* see include/linux/fs.h */
  7065. + unsigned int ioctl_num, /* number and param for ioctl */
  7066. + unsigned long ioctl_param)
  7067. +{
  7068. + unsigned size;
  7069. + /*
  7070. + * Switch according to the ioctl called
  7071. + */
  7072. + switch (ioctl_num) {
  7073. + case IOCTL_MBOX_PROPERTY:
  7074. + /*
  7075. + * Receive a pointer to a message (in user space) and set that
  7076. + * to be the device's message. Get the parameter given to
  7077. + * ioctl by the process.
  7078. + */
  7079. + mbox_copy_from_user(&size, (void *)ioctl_param, sizeof size);
  7080. + return bcm_mailbox_property((void *)ioctl_param, size);
  7081. + break;
  7082. + default:
  7083. + printk(KERN_ERR DRIVER_NAME "unknown ioctl: %d\n", ioctl_num);
  7084. + return -EINVAL;
  7085. + }
  7086. +
  7087. + return 0;
  7088. +}
  7089. +
  7090. +/* Module Declarations */
  7091. +
  7092. +/*
  7093. + * This structure will hold the functions to be called
  7094. + * when a process does something to the device we
  7095. + * created. Since a pointer to this structure is kept in
  7096. + * the devices table, it can't be local to
  7097. + * init_module. NULL is for unimplemented functios.
  7098. + */
  7099. +struct file_operations fops = {
  7100. + .unlocked_ioctl = device_ioctl,
  7101. + .open = device_open,
  7102. + .release = device_release, /* a.k.a. close */
  7103. +};
  7104. +
  7105. +static int bcm_vcio_probe(struct platform_device *pdev)
  7106. +{
  7107. + int ret = 0;
  7108. + struct vc_mailbox *mailbox;
  7109. +
  7110. + mailbox = kzalloc(sizeof(*mailbox), GFP_KERNEL);
  7111. + if (NULL == mailbox) {
  7112. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  7113. + "mailbox memory\n");
  7114. + ret = -ENOMEM;
  7115. + } else {
  7116. + struct resource *res;
  7117. +
  7118. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  7119. + if (res == NULL) {
  7120. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  7121. + "resource\n");
  7122. + ret = -ENODEV;
  7123. + kfree(mailbox);
  7124. + } else {
  7125. + /* should be based on the registers from res really */
  7126. + mbox_init(mailbox, &pdev->dev, ARM_0_MAIL0_RD);
  7127. +
  7128. + platform_set_drvdata(pdev, mailbox);
  7129. + dev_mbox_register(DRIVER_NAME, &pdev->dev);
  7130. +
  7131. + mbox_irqaction.dev_id = mailbox;
  7132. + setup_irq(IRQ_ARM_MAILBOX, &mbox_irqaction);
  7133. + printk(KERN_INFO DRIVER_NAME ": mailbox at %p\n",
  7134. + __io_address(ARM_0_MAIL0_RD));
  7135. + }
  7136. + }
  7137. +
  7138. + if (ret == 0) {
  7139. + /*
  7140. + * Register the character device
  7141. + */
  7142. + ret = register_chrdev(MAJOR_NUM, DEVICE_FILE_NAME, &fops);
  7143. +
  7144. + /*
  7145. + * Negative values signify an error
  7146. + */
  7147. + if (ret < 0) {
  7148. + printk(KERN_ERR DRIVER_NAME
  7149. + "Failed registering the character device %d\n", ret);
  7150. + return ret;
  7151. + }
  7152. + }
  7153. + return ret;
  7154. +}
  7155. +
  7156. +static int bcm_vcio_remove(struct platform_device *pdev)
  7157. +{
  7158. + struct vc_mailbox *mailbox = platform_get_drvdata(pdev);
  7159. +
  7160. + platform_set_drvdata(pdev, NULL);
  7161. + kfree(mailbox);
  7162. +
  7163. + return 0;
  7164. +}
  7165. +
  7166. +static struct platform_driver bcm_mbox_driver = {
  7167. + .probe = bcm_vcio_probe,
  7168. + .remove = bcm_vcio_remove,
  7169. +
  7170. + .driver = {
  7171. + .name = DRIVER_NAME,
  7172. + .owner = THIS_MODULE,
  7173. + },
  7174. +};
  7175. +
  7176. +static int __init bcm_mbox_init(void)
  7177. +{
  7178. + int ret;
  7179. +
  7180. + printk(KERN_INFO "mailbox: Broadcom VideoCore Mailbox driver\n");
  7181. +
  7182. + ret = platform_driver_register(&bcm_mbox_driver);
  7183. + if (ret != 0) {
  7184. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  7185. + "on platform\n");
  7186. + }
  7187. +
  7188. + return ret;
  7189. +}
  7190. +
  7191. +static void __exit bcm_mbox_exit(void)
  7192. +{
  7193. + platform_driver_unregister(&bcm_mbox_driver);
  7194. +}
  7195. +
  7196. +arch_initcall(bcm_mbox_init); /* Initialize early */
  7197. +module_exit(bcm_mbox_exit);
  7198. +
  7199. +MODULE_AUTHOR("Gray Girling");
  7200. +MODULE_DESCRIPTION("ARM I/O to VideoCore processor");
  7201. +MODULE_LICENSE("GPL");
  7202. +MODULE_ALIAS("platform:bcm-mbox");
  7203. diff -Nur linux-3.13.6/arch/arm/mach-bcm2708/vc_mem.c linux-raspberry-pi/arch/arm/mach-bcm2708/vc_mem.c
  7204. --- linux-3.13.6/arch/arm/mach-bcm2708/vc_mem.c 1970-01-01 01:00:00.000000000 +0100
  7205. +++ linux-raspberry-pi/arch/arm/mach-bcm2708/vc_mem.c 2014-03-11 16:51:54.000000000 +0100
  7206. @@ -0,0 +1,432 @@
  7207. +/*****************************************************************************
  7208. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  7209. +*
  7210. +* Unless you and Broadcom execute a separate written software license
  7211. +* agreement governing use of this software, this software is licensed to you
  7212. +* under the terms of the GNU General Public License version 2, available at
  7213. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7214. +*
  7215. +* Notwithstanding the above, under no circumstances may you combine this
  7216. +* software in any way with any other Broadcom software provided under a
  7217. +* license other than the GPL, without Broadcom's express prior written
  7218. +* consent.
  7219. +*****************************************************************************/
  7220. +
  7221. +#include <linux/kernel.h>
  7222. +#include <linux/module.h>
  7223. +#include <linux/fs.h>
  7224. +#include <linux/device.h>
  7225. +#include <linux/cdev.h>
  7226. +#include <linux/mm.h>
  7227. +#include <linux/slab.h>
  7228. +#include <linux/debugfs.h>
  7229. +#include <asm/uaccess.h>
  7230. +#include <linux/dma-mapping.h>
  7231. +
  7232. +#ifdef CONFIG_ARCH_KONA
  7233. +#include <chal/chal_ipc.h>
  7234. +#elif CONFIG_ARCH_BCM2708
  7235. +#else
  7236. +#include <csp/chal_ipc.h>
  7237. +#endif
  7238. +
  7239. +#include "mach/vc_mem.h"
  7240. +#include <mach/vcio.h>
  7241. +
  7242. +#define DRIVER_NAME "vc-mem"
  7243. +
  7244. +// Device (/dev) related variables
  7245. +static dev_t vc_mem_devnum = 0;
  7246. +static struct class *vc_mem_class = NULL;
  7247. +static struct cdev vc_mem_cdev;
  7248. +static int vc_mem_inited = 0;
  7249. +
  7250. +#ifdef CONFIG_DEBUG_FS
  7251. +static struct dentry *vc_mem_debugfs_entry;
  7252. +#endif
  7253. +
  7254. +/*
  7255. + * Videocore memory addresses and size
  7256. + *
  7257. + * Drivers that wish to know the videocore memory addresses and sizes should
  7258. + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
  7259. + * headers. This allows the other drivers to not be tied down to a a certain
  7260. + * address/size at compile time.
  7261. + *
  7262. + * In the future, the goal is to have the videocore memory virtual address and
  7263. + * size be calculated at boot time rather than at compile time. The decision of
  7264. + * where the videocore memory resides and its size would be in the hands of the
  7265. + * bootloader (and/or kernel). When that happens, the values of these variables
  7266. + * would be calculated and assigned in the init function.
  7267. + */
  7268. +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
  7269. +unsigned long mm_vc_mem_phys_addr = 0x00000000;
  7270. +unsigned int mm_vc_mem_size = 0;
  7271. +unsigned int mm_vc_mem_base = 0;
  7272. +
  7273. +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
  7274. +EXPORT_SYMBOL(mm_vc_mem_size);
  7275. +EXPORT_SYMBOL(mm_vc_mem_base);
  7276. +
  7277. +static uint phys_addr = 0;
  7278. +static uint mem_size = 0;
  7279. +static uint mem_base = 0;
  7280. +
  7281. +
  7282. +/****************************************************************************
  7283. +*
  7284. +* vc_mem_open
  7285. +*
  7286. +***************************************************************************/
  7287. +
  7288. +static int
  7289. +vc_mem_open(struct inode *inode, struct file *file)
  7290. +{
  7291. + (void) inode;
  7292. + (void) file;
  7293. +
  7294. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  7295. +
  7296. + return 0;
  7297. +}
  7298. +
  7299. +/****************************************************************************
  7300. +*
  7301. +* vc_mem_release
  7302. +*
  7303. +***************************************************************************/
  7304. +
  7305. +static int
  7306. +vc_mem_release(struct inode *inode, struct file *file)
  7307. +{
  7308. + (void) inode;
  7309. + (void) file;
  7310. +
  7311. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  7312. +
  7313. + return 0;
  7314. +}
  7315. +
  7316. +/****************************************************************************
  7317. +*
  7318. +* vc_mem_get_size
  7319. +*
  7320. +***************************************************************************/
  7321. +
  7322. +static void
  7323. +vc_mem_get_size(void)
  7324. +{
  7325. +}
  7326. +
  7327. +/****************************************************************************
  7328. +*
  7329. +* vc_mem_get_base
  7330. +*
  7331. +***************************************************************************/
  7332. +
  7333. +static void
  7334. +vc_mem_get_base(void)
  7335. +{
  7336. +}
  7337. +
  7338. +/****************************************************************************
  7339. +*
  7340. +* vc_mem_get_current_size
  7341. +*
  7342. +***************************************************************************/
  7343. +
  7344. +int
  7345. +vc_mem_get_current_size(void)
  7346. +{
  7347. + return mm_vc_mem_size;
  7348. +}
  7349. +
  7350. +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
  7351. +
  7352. +/****************************************************************************
  7353. +*
  7354. +* vc_mem_ioctl
  7355. +*
  7356. +***************************************************************************/
  7357. +
  7358. +static long
  7359. +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  7360. +{
  7361. + int rc = 0;
  7362. +
  7363. + (void) cmd;
  7364. + (void) arg;
  7365. +
  7366. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  7367. +
  7368. + switch (cmd) {
  7369. + case VC_MEM_IOC_MEM_PHYS_ADDR:
  7370. + {
  7371. + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
  7372. + __func__, (void *) mm_vc_mem_phys_addr);
  7373. +
  7374. + if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
  7375. + sizeof (mm_vc_mem_phys_addr)) != 0) {
  7376. + rc = -EFAULT;
  7377. + }
  7378. + break;
  7379. + }
  7380. + case VC_MEM_IOC_MEM_SIZE:
  7381. + {
  7382. + // Get the videocore memory size first
  7383. + vc_mem_get_size();
  7384. +
  7385. + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
  7386. + mm_vc_mem_size);
  7387. +
  7388. + if (copy_to_user((void *) arg, &mm_vc_mem_size,
  7389. + sizeof (mm_vc_mem_size)) != 0) {
  7390. + rc = -EFAULT;
  7391. + }
  7392. + break;
  7393. + }
  7394. + case VC_MEM_IOC_MEM_BASE:
  7395. + {
  7396. + // Get the videocore memory base
  7397. + vc_mem_get_base();
  7398. +
  7399. + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
  7400. + mm_vc_mem_base);
  7401. +
  7402. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  7403. + sizeof (mm_vc_mem_base)) != 0) {
  7404. + rc = -EFAULT;
  7405. + }
  7406. + break;
  7407. + }
  7408. + case VC_MEM_IOC_MEM_LOAD:
  7409. + {
  7410. + // Get the videocore memory base
  7411. + vc_mem_get_base();
  7412. +
  7413. + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
  7414. + mm_vc_mem_base);
  7415. +
  7416. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  7417. + sizeof (mm_vc_mem_base)) != 0) {
  7418. + rc = -EFAULT;
  7419. + }
  7420. + break;
  7421. + }
  7422. + default:
  7423. + {
  7424. + return -ENOTTY;
  7425. + }
  7426. + }
  7427. + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
  7428. +
  7429. + return rc;
  7430. +}
  7431. +
  7432. +/****************************************************************************
  7433. +*
  7434. +* vc_mem_mmap
  7435. +*
  7436. +***************************************************************************/
  7437. +
  7438. +static int
  7439. +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
  7440. +{
  7441. + int rc = 0;
  7442. + unsigned long length = vma->vm_end - vma->vm_start;
  7443. + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  7444. +
  7445. + pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
  7446. + __func__, (long) vma->vm_start, (long) vma->vm_end,
  7447. + (long) vma->vm_pgoff);
  7448. +
  7449. + if (offset + length > mm_vc_mem_size) {
  7450. + pr_err("%s: length %ld is too big\n", __func__, length);
  7451. + return -EINVAL;
  7452. + }
  7453. + // Do not cache the memory map
  7454. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  7455. +
  7456. + rc = remap_pfn_range(vma, vma->vm_start,
  7457. + (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
  7458. + vma->vm_pgoff, length, vma->vm_page_prot);
  7459. + if (rc != 0) {
  7460. + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
  7461. + }
  7462. +
  7463. + return rc;
  7464. +}
  7465. +
  7466. +/****************************************************************************
  7467. +*
  7468. +* File Operations for the driver.
  7469. +*
  7470. +***************************************************************************/
  7471. +
  7472. +static const struct file_operations vc_mem_fops = {
  7473. + .owner = THIS_MODULE,
  7474. + .open = vc_mem_open,
  7475. + .release = vc_mem_release,
  7476. + .unlocked_ioctl = vc_mem_ioctl,
  7477. + .mmap = vc_mem_mmap,
  7478. +};
  7479. +
  7480. +#ifdef CONFIG_DEBUG_FS
  7481. +static void vc_mem_debugfs_deinit(void)
  7482. +{
  7483. + debugfs_remove_recursive(vc_mem_debugfs_entry);
  7484. + vc_mem_debugfs_entry = NULL;
  7485. +}
  7486. +
  7487. +
  7488. +static int vc_mem_debugfs_init(
  7489. + struct device *dev)
  7490. +{
  7491. + vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
  7492. + if (!vc_mem_debugfs_entry) {
  7493. + dev_warn(dev, "could not create debugfs entry\n");
  7494. + return -EFAULT;
  7495. + }
  7496. +
  7497. + if (!debugfs_create_x32("vc_mem_phys_addr",
  7498. + 0444,
  7499. + vc_mem_debugfs_entry,
  7500. + (u32 *)&mm_vc_mem_phys_addr)) {
  7501. + dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
  7502. + __func__);
  7503. + goto fail;
  7504. + }
  7505. +
  7506. + if (!debugfs_create_x32("vc_mem_size",
  7507. + 0444,
  7508. + vc_mem_debugfs_entry,
  7509. + (u32 *)&mm_vc_mem_size)) {
  7510. + dev_warn(dev, "%s:could not create vc_mem_size entry\n",
  7511. + __func__);
  7512. + goto fail;
  7513. + }
  7514. +
  7515. + if (!debugfs_create_x32("vc_mem_base",
  7516. + 0444,
  7517. + vc_mem_debugfs_entry,
  7518. + (u32 *)&mm_vc_mem_base)) {
  7519. + dev_warn(dev, "%s:could not create vc_mem_base entry\n",
  7520. + __func__);
  7521. + goto fail;
  7522. + }
  7523. +
  7524. + return 0;
  7525. +
  7526. +fail:
  7527. + vc_mem_debugfs_deinit();
  7528. + return -EFAULT;
  7529. +}
  7530. +
  7531. +#endif /* CONFIG_DEBUG_FS */
  7532. +
  7533. +
  7534. +/****************************************************************************
  7535. +*
  7536. +* vc_mem_init
  7537. +*
  7538. +***************************************************************************/
  7539. +
  7540. +static int __init
  7541. +vc_mem_init(void)
  7542. +{
  7543. + int rc = -EFAULT;
  7544. + struct device *dev;
  7545. +
  7546. + pr_debug("%s: called\n", __func__);
  7547. +
  7548. + mm_vc_mem_phys_addr = phys_addr;
  7549. + mm_vc_mem_size = mem_size;
  7550. + mm_vc_mem_base = mem_base;
  7551. +
  7552. + vc_mem_get_size();
  7553. +
  7554. + pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
  7555. + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
  7556. +
  7557. + if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
  7558. + pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
  7559. + __func__, rc);
  7560. + goto out_err;
  7561. + }
  7562. +
  7563. + cdev_init(&vc_mem_cdev, &vc_mem_fops);
  7564. + if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
  7565. + pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
  7566. + goto out_unregister;
  7567. + }
  7568. +
  7569. + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
  7570. + if (IS_ERR(vc_mem_class)) {
  7571. + rc = PTR_ERR(vc_mem_class);
  7572. + pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
  7573. + goto out_cdev_del;
  7574. + }
  7575. +
  7576. + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
  7577. + DRIVER_NAME);
  7578. + if (IS_ERR(dev)) {
  7579. + rc = PTR_ERR(dev);
  7580. + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
  7581. + goto out_class_destroy;
  7582. + }
  7583. +
  7584. +#ifdef CONFIG_DEBUG_FS
  7585. + /* don't fail if the debug entries cannot be created */
  7586. + vc_mem_debugfs_init(dev);
  7587. +#endif
  7588. +
  7589. + vc_mem_inited = 1;
  7590. + return 0;
  7591. +
  7592. + device_destroy(vc_mem_class, vc_mem_devnum);
  7593. +
  7594. + out_class_destroy:
  7595. + class_destroy(vc_mem_class);
  7596. + vc_mem_class = NULL;
  7597. +
  7598. + out_cdev_del:
  7599. + cdev_del(&vc_mem_cdev);
  7600. +
  7601. + out_unregister:
  7602. + unregister_chrdev_region(vc_mem_devnum, 1);
  7603. +
  7604. + out_err:
  7605. + return -1;
  7606. +}
  7607. +
  7608. +/****************************************************************************
  7609. +*
  7610. +* vc_mem_exit
  7611. +*
  7612. +***************************************************************************/
  7613. +
  7614. +static void __exit
  7615. +vc_mem_exit(void)
  7616. +{
  7617. + pr_debug("%s: called\n", __func__);
  7618. +
  7619. + if (vc_mem_inited) {
  7620. +#if CONFIG_DEBUG_FS
  7621. + vc_mem_debugfs_deinit();
  7622. +#endif
  7623. + device_destroy(vc_mem_class, vc_mem_devnum);
  7624. + class_destroy(vc_mem_class);
  7625. + cdev_del(&vc_mem_cdev);
  7626. + unregister_chrdev_region(vc_mem_devnum, 1);
  7627. + }
  7628. +}
  7629. +
  7630. +module_init(vc_mem_init);
  7631. +module_exit(vc_mem_exit);
  7632. +MODULE_LICENSE("GPL");
  7633. +MODULE_AUTHOR("Broadcom Corporation");
  7634. +
  7635. +module_param(phys_addr, uint, 0644);
  7636. +module_param(mem_size, uint, 0644);
  7637. +module_param(mem_base, uint, 0644);
  7638. +
  7639. diff -Nur linux-3.13.6/arch/arm/Makefile linux-raspberry-pi/arch/arm/Makefile
  7640. --- linux-3.13.6/arch/arm/Makefile 2014-03-07 07:07:02.000000000 +0100
  7641. +++ linux-raspberry-pi/arch/arm/Makefile 2014-03-11 16:54:55.000000000 +0100
  7642. @@ -147,6 +147,7 @@
  7643. # by CONFIG_* macro name.
  7644. machine-$(CONFIG_ARCH_AT91) += at91
  7645. machine-$(CONFIG_ARCH_BCM) += bcm
  7646. +machine-$(CONFIG_ARCH_BCM2708) += bcm2708
  7647. machine-$(CONFIG_ARCH_BCM2835) += bcm2835
  7648. machine-$(CONFIG_ARCH_CLPS711X) += clps711x
  7649. machine-$(CONFIG_ARCH_CNS3XXX) += cns3xxx
  7650. diff -Nur linux-3.13.6/arch/arm/mm/Kconfig linux-raspberry-pi/arch/arm/mm/Kconfig
  7651. --- linux-3.13.6/arch/arm/mm/Kconfig 2014-03-07 07:07:02.000000000 +0100
  7652. +++ linux-raspberry-pi/arch/arm/mm/Kconfig 2014-03-11 16:54:55.000000000 +0100
  7653. @@ -358,7 +358,7 @@
  7654. # ARMv6
  7655. config CPU_V6
  7656. - bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX
  7657. + bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX || MACH_BCM2708
  7658. select CPU_32v6
  7659. select CPU_ABRT_EV6
  7660. select CPU_CACHE_V6
  7661. diff -Nur linux-3.13.6/arch/arm/mm/proc-v6.S linux-raspberry-pi/arch/arm/mm/proc-v6.S
  7662. --- linux-3.13.6/arch/arm/mm/proc-v6.S 2014-03-07 07:07:02.000000000 +0100
  7663. +++ linux-raspberry-pi/arch/arm/mm/proc-v6.S 2014-03-11 16:54:55.000000000 +0100
  7664. @@ -73,10 +73,19 @@
  7665. *
  7666. * IRQs are already disabled.
  7667. */
  7668. +
  7669. +/* See jira SW-5991 for details of this workaround */
  7670. ENTRY(cpu_v6_do_idle)
  7671. - mov r1, #0
  7672. - mcr p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  7673. - mcr p15, 0, r1, c7, c0, 4 @ wait for interrupt
  7674. + .align 5
  7675. + mov r1, #2
  7676. +1: subs r1, #1
  7677. + nop
  7678. + mcreq p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  7679. + mcreq p15, 0, r1, c7, c0, 4 @ wait for interrupt
  7680. + nop
  7681. + nop
  7682. + nop
  7683. + bne 1b
  7684. mov pc, lr
  7685. ENTRY(cpu_v6_dcache_clean_area)
  7686. diff -Nur linux-3.13.6/arch/arm/tools/mach-types linux-raspberry-pi/arch/arm/tools/mach-types
  7687. --- linux-3.13.6/arch/arm/tools/mach-types 2014-03-07 07:07:02.000000000 +0100
  7688. +++ linux-raspberry-pi/arch/arm/tools/mach-types 2014-03-11 16:51:56.000000000 +0100
  7689. @@ -522,6 +522,7 @@
  7690. prima2_evb MACH_PRIMA2_EVB PRIMA2_EVB 3103
  7691. paz00 MACH_PAZ00 PAZ00 3128
  7692. acmenetusfoxg20 MACH_ACMENETUSFOXG20 ACMENETUSFOXG20 3129
  7693. +bcm2708 MACH_BCM2708 BCM2708 3138
  7694. ag5evm MACH_AG5EVM AG5EVM 3189
  7695. ics_if_voip MACH_ICS_IF_VOIP ICS_IF_VOIP 3206
  7696. wlf_cragg_6410 MACH_WLF_CRAGG_6410 WLF_CRAGG_6410 3207
  7697. diff -Nur linux-3.13.6/Documentation/video4linux/bcm2835-v4l2.txt linux-raspberry-pi/Documentation/video4linux/bcm2835-v4l2.txt
  7698. --- linux-3.13.6/Documentation/video4linux/bcm2835-v4l2.txt 1970-01-01 01:00:00.000000000 +0100
  7699. +++ linux-raspberry-pi/Documentation/video4linux/bcm2835-v4l2.txt 2014-03-11 16:51:47.000000000 +0100
  7700. @@ -0,0 +1,60 @@
  7701. +
  7702. +BCM2835 (aka Raspberry Pi) V4L2 driver
  7703. +======================================
  7704. +
  7705. +1. Copyright
  7706. +============
  7707. +
  7708. +Copyright © 2013 Raspberry Pi (Trading) Ltd.
  7709. +
  7710. +2. License
  7711. +==========
  7712. +
  7713. +This program is free software; you can redistribute it and/or modify
  7714. +it under the terms of the GNU General Public License as published by
  7715. +the Free Software Foundation; either version 2 of the License, or
  7716. +(at your option) any later version.
  7717. +
  7718. +This program is distributed in the hope that it will be useful,
  7719. +but WITHOUT ANY WARRANTY; without even the implied warranty of
  7720. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7721. +GNU General Public License for more details.
  7722. +
  7723. +You should have received a copy of the GNU General Public License
  7724. +along with this program; if not, write to the Free Software
  7725. +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  7726. +
  7727. +3. Quick Start
  7728. +==============
  7729. +
  7730. +You need a version 1.0 or later of v4l2-ctl, available from:
  7731. + git://git.linuxtv.org/v4l-utils.git
  7732. +
  7733. +$ sudo modprobe bcm2835-v4l2
  7734. +
  7735. +Turn on the overlay:
  7736. +
  7737. +$ v4l2-ctl --overlay=1
  7738. +
  7739. +Turn off the overlay:
  7740. +
  7741. +$ v4l2-ctl --overlay=0
  7742. +
  7743. +Set the capture format for video:
  7744. +
  7745. +$ v4l2-ctl --set-fmt-video=width=1920,height=1088,pixelformat=4
  7746. +
  7747. +(Note: 1088 not 1080).
  7748. +
  7749. +Capture:
  7750. +
  7751. +$ v4l2-ctl --stream-mmap=3 --stream-count=100 --stream-to=somefile.h264
  7752. +
  7753. +Stills capture:
  7754. +
  7755. +$ v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=3
  7756. +$ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
  7757. +
  7758. +List of available formats:
  7759. +
  7760. +$ v4l2-ctl --list-formats
  7761. diff -Nur linux-3.13.6/drivers/char/broadcom/Kconfig linux-raspberry-pi/drivers/char/broadcom/Kconfig
  7762. --- linux-3.13.6/drivers/char/broadcom/Kconfig 1970-01-01 01:00:00.000000000 +0100
  7763. +++ linux-raspberry-pi/drivers/char/broadcom/Kconfig 2014-03-11 16:54:56.000000000 +0100
  7764. @@ -0,0 +1,16 @@
  7765. +#
  7766. +# Broadcom char driver config
  7767. +#
  7768. +
  7769. +menuconfig BRCM_CHAR_DRIVERS
  7770. + bool "Broadcom Char Drivers"
  7771. + help
  7772. + Broadcom's char drivers
  7773. +
  7774. +config BCM_VC_CMA
  7775. + bool "Videocore CMA"
  7776. + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
  7777. + default n
  7778. + help
  7779. + Helper for videocore CMA access.
  7780. +
  7781. diff -Nur linux-3.13.6/drivers/char/broadcom/Makefile linux-raspberry-pi/drivers/char/broadcom/Makefile
  7782. --- linux-3.13.6/drivers/char/broadcom/Makefile 1970-01-01 01:00:00.000000000 +0100
  7783. +++ linux-raspberry-pi/drivers/char/broadcom/Makefile 2014-03-11 16:54:56.000000000 +0100
  7784. @@ -0,0 +1 @@
  7785. +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
  7786. diff -Nur linux-3.13.6/drivers/char/broadcom/vc_cma/Makefile linux-raspberry-pi/drivers/char/broadcom/vc_cma/Makefile
  7787. --- linux-3.13.6/drivers/char/broadcom/vc_cma/Makefile 1970-01-01 01:00:00.000000000 +0100
  7788. +++ linux-raspberry-pi/drivers/char/broadcom/vc_cma/Makefile 2014-03-11 16:54:56.000000000 +0100
  7789. @@ -0,0 +1,14 @@
  7790. +EXTRA_CFLAGS += -Wall -Wstrict-prototypes -Wno-trigraphs
  7791. +EXTRA_CFLAGS += -Werror
  7792. +EXTRA_CFLAGS += -I"include/linux/broadcom"
  7793. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services"
  7794. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchi"
  7795. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchiq_arm"
  7796. +
  7797. +EXTRA_CFLAGS += -D__KERNEL__
  7798. +EXTRA_CFLAGS += -D__linux__
  7799. +EXTRA_CFLAGS += -Werror
  7800. +
  7801. +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
  7802. +
  7803. +vc-cma-objs := vc_cma.o
  7804. diff -Nur linux-3.13.6/drivers/char/broadcom/vc_cma/vc_cma.c linux-raspberry-pi/drivers/char/broadcom/vc_cma/vc_cma.c
  7805. --- linux-3.13.6/drivers/char/broadcom/vc_cma/vc_cma.c 1970-01-01 01:00:00.000000000 +0100
  7806. +++ linux-raspberry-pi/drivers/char/broadcom/vc_cma/vc_cma.c 2014-03-11 16:54:56.000000000 +0100
  7807. @@ -0,0 +1,1143 @@
  7808. +/**
  7809. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  7810. + *
  7811. + * Redistribution and use in source and binary forms, with or without
  7812. + * modification, are permitted provided that the following conditions
  7813. + * are met:
  7814. + * 1. Redistributions of source code must retain the above copyright
  7815. + * notice, this list of conditions, and the following disclaimer,
  7816. + * without modification.
  7817. + * 2. Redistributions in binary form must reproduce the above copyright
  7818. + * notice, this list of conditions and the following disclaimer in the
  7819. + * documentation and/or other materials provided with the distribution.
  7820. + * 3. The names of the above-listed copyright holders may not be used
  7821. + * to endorse or promote products derived from this software without
  7822. + * specific prior written permission.
  7823. + *
  7824. + * ALTERNATIVELY, this software may be distributed under the terms of the
  7825. + * GNU General Public License ("GPL") version 2, as published by the Free
  7826. + * Software Foundation.
  7827. + *
  7828. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  7829. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  7830. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  7831. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  7832. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  7833. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  7834. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  7835. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  7836. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  7837. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  7838. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7839. + */
  7840. +
  7841. +#include <linux/kernel.h>
  7842. +#include <linux/module.h>
  7843. +#include <linux/kthread.h>
  7844. +#include <linux/fs.h>
  7845. +#include <linux/device.h>
  7846. +#include <linux/cdev.h>
  7847. +#include <linux/mm.h>
  7848. +#include <linux/proc_fs.h>
  7849. +#include <linux/seq_file.h>
  7850. +#include <linux/dma-mapping.h>
  7851. +#include <linux/dma-contiguous.h>
  7852. +#include <linux/platform_device.h>
  7853. +#include <linux/uaccess.h>
  7854. +#include <asm/cacheflush.h>
  7855. +
  7856. +#include "vc_cma.h"
  7857. +
  7858. +#include "vchiq_util.h"
  7859. +#include "vchiq_connected.h"
  7860. +//#include "debug_sym.h"
  7861. +//#include "vc_mem.h"
  7862. +
  7863. +#define DRIVER_NAME "vc-cma"
  7864. +
  7865. +#define LOG_DBG(fmt, ...) \
  7866. + if (vc_cma_debug) \
  7867. + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
  7868. +#define LOG_ERR(fmt, ...) \
  7869. + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
  7870. +
  7871. +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
  7872. +#define VC_CMA_VERSION 2
  7873. +
  7874. +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
  7875. +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
  7876. +#define VC_CMA_MAX_PARAMS_PER_MSG \
  7877. + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
  7878. +#define VC_CMA_RESERVE_COUNT_MAX 16
  7879. +
  7880. +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
  7881. +
  7882. +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
  7883. +
  7884. +#define loud_error(...) \
  7885. + LOG_ERR("===== " __VA_ARGS__)
  7886. +
  7887. +enum {
  7888. + VC_CMA_MSG_QUIT,
  7889. + VC_CMA_MSG_OPEN,
  7890. + VC_CMA_MSG_TICK,
  7891. + VC_CMA_MSG_ALLOC, /* chunk count */
  7892. + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
  7893. + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
  7894. + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
  7895. + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
  7896. + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
  7897. + VC_CMA_MSG_UPDATE_RESERVE,
  7898. + VC_CMA_MSG_MAX
  7899. +};
  7900. +
  7901. +struct cma_msg {
  7902. + unsigned short type;
  7903. + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
  7904. +};
  7905. +
  7906. +struct vc_cma_reserve_user {
  7907. + unsigned int pid;
  7908. + unsigned int reserve;
  7909. +};
  7910. +
  7911. +/* Device (/dev) related variables */
  7912. +static dev_t vc_cma_devnum;
  7913. +static struct class *vc_cma_class;
  7914. +static struct cdev vc_cma_cdev;
  7915. +static int vc_cma_inited;
  7916. +static int vc_cma_debug;
  7917. +
  7918. +/* Proc entry */
  7919. +static struct proc_dir_entry *vc_cma_proc_entry;
  7920. +
  7921. +phys_addr_t vc_cma_base;
  7922. +struct page *vc_cma_base_page;
  7923. +unsigned int vc_cma_size;
  7924. +EXPORT_SYMBOL(vc_cma_size);
  7925. +unsigned int vc_cma_initial;
  7926. +unsigned int vc_cma_chunks;
  7927. +unsigned int vc_cma_chunks_used;
  7928. +unsigned int vc_cma_chunks_reserved;
  7929. +
  7930. +static int in_loud_error;
  7931. +
  7932. +unsigned int vc_cma_reserve_total;
  7933. +unsigned int vc_cma_reserve_count;
  7934. +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
  7935. +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
  7936. +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
  7937. +
  7938. +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
  7939. +static struct platform_device vc_cma_device = {
  7940. + .name = "vc-cma",
  7941. + .id = 0,
  7942. + .dev = {
  7943. + .dma_mask = &vc_cma_dma_mask,
  7944. + .coherent_dma_mask = DMA_BIT_MASK(32),
  7945. + },
  7946. +};
  7947. +
  7948. +static VCHIQ_INSTANCE_T cma_instance;
  7949. +static VCHIQ_SERVICE_HANDLE_T cma_service;
  7950. +static VCHIU_QUEUE_T cma_msg_queue;
  7951. +static struct task_struct *cma_worker;
  7952. +
  7953. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
  7954. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
  7955. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  7956. + VCHIQ_HEADER_T * header,
  7957. + VCHIQ_SERVICE_HANDLE_T service,
  7958. + void *bulk_userdata);
  7959. +static void send_vc_msg(unsigned short type,
  7960. + unsigned short param1, unsigned short param2);
  7961. +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
  7962. +
  7963. +static int early_vc_cma_mem(char *p)
  7964. +{
  7965. + unsigned int new_size;
  7966. + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
  7967. + vc_cma_size = memparse(p, &p);
  7968. + vc_cma_initial = vc_cma_size;
  7969. + if (*p == '/')
  7970. + vc_cma_size = memparse(p + 1, &p);
  7971. + if (*p == '@')
  7972. + vc_cma_base = memparse(p + 1, &p);
  7973. +
  7974. + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
  7975. + & ~(VC_CMA_CHUNK_SIZE - 1);
  7976. + if (new_size > vc_cma_size)
  7977. + vc_cma_size = 0;
  7978. + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
  7979. + & ~(VC_CMA_CHUNK_SIZE - 1);
  7980. + if (vc_cma_initial > vc_cma_size)
  7981. + vc_cma_initial = vc_cma_size;
  7982. + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
  7983. + & ~(VC_CMA_CHUNK_SIZE - 1);
  7984. +
  7985. + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
  7986. + vc_cma_size, (unsigned int)vc_cma_base);
  7987. +
  7988. + return 0;
  7989. +}
  7990. +
  7991. +early_param("vc-cma-mem", early_vc_cma_mem);
  7992. +
  7993. +void vc_cma_early_init(void)
  7994. +{
  7995. + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
  7996. + if (vc_cma_size) {
  7997. + int rc = platform_device_register(&vc_cma_device);
  7998. + LOG_DBG("platform_device_register -> %d", rc);
  7999. + }
  8000. +}
  8001. +
  8002. +void vc_cma_reserve(void)
  8003. +{
  8004. + /* if vc_cma_size is set, then declare vc CMA area of the same
  8005. + * size from the end of memory
  8006. + */
  8007. + if (vc_cma_size) {
  8008. + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
  8009. + vc_cma_base, 0) == 0) {
  8010. + } else {
  8011. + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
  8012. + vc_cma_size, (unsigned int)vc_cma_base);
  8013. + vc_cma_size = 0;
  8014. + }
  8015. + }
  8016. + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
  8017. +}
  8018. +
  8019. +/****************************************************************************
  8020. +*
  8021. +* vc_cma_open
  8022. +*
  8023. +***************************************************************************/
  8024. +
  8025. +static int vc_cma_open(struct inode *inode, struct file *file)
  8026. +{
  8027. + (void)inode;
  8028. + (void)file;
  8029. +
  8030. + return 0;
  8031. +}
  8032. +
  8033. +/****************************************************************************
  8034. +*
  8035. +* vc_cma_release
  8036. +*
  8037. +***************************************************************************/
  8038. +
  8039. +static int vc_cma_release(struct inode *inode, struct file *file)
  8040. +{
  8041. + (void)inode;
  8042. + (void)file;
  8043. +
  8044. + vc_cma_set_reserve(0, current->tgid);
  8045. +
  8046. + return 0;
  8047. +}
  8048. +
  8049. +/****************************************************************************
  8050. +*
  8051. +* vc_cma_ioctl
  8052. +*
  8053. +***************************************************************************/
  8054. +
  8055. +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  8056. +{
  8057. + int rc = 0;
  8058. +
  8059. + (void)cmd;
  8060. + (void)arg;
  8061. +
  8062. + switch (cmd) {
  8063. + case VC_CMA_IOC_RESERVE:
  8064. + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
  8065. + if (rc >= 0)
  8066. + rc = 0;
  8067. + break;
  8068. + default:
  8069. + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
  8070. + return -ENOTTY;
  8071. + }
  8072. +
  8073. + return rc;
  8074. +}
  8075. +
  8076. +/****************************************************************************
  8077. +*
  8078. +* File Operations for the driver.
  8079. +*
  8080. +***************************************************************************/
  8081. +
  8082. +static const struct file_operations vc_cma_fops = {
  8083. + .owner = THIS_MODULE,
  8084. + .open = vc_cma_open,
  8085. + .release = vc_cma_release,
  8086. + .unlocked_ioctl = vc_cma_ioctl,
  8087. +};
  8088. +
  8089. +/****************************************************************************
  8090. +*
  8091. +* vc_cma_proc_open
  8092. +*
  8093. +***************************************************************************/
  8094. +
  8095. +static int vc_cma_show_info(struct seq_file *m, void *v)
  8096. +{
  8097. + int i;
  8098. +
  8099. + seq_printf(m, "Videocore CMA:\n");
  8100. + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
  8101. + seq_printf(m, " Length : %08x\n", vc_cma_size);
  8102. + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
  8103. + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
  8104. + seq_printf(m, " Chunks : %4d (%d bytes)\n",
  8105. + (int)vc_cma_chunks,
  8106. + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
  8107. + seq_printf(m, " Used : %4d (%d bytes)\n",
  8108. + (int)vc_cma_chunks_used,
  8109. + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
  8110. + seq_printf(m, " Reserved : %4d (%d bytes)\n",
  8111. + (unsigned int)vc_cma_chunks_reserved,
  8112. + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
  8113. +
  8114. + for (i = 0; i < vc_cma_reserve_count; i++) {
  8115. + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
  8116. + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
  8117. + user->reserve);
  8118. + }
  8119. +
  8120. + seq_printf(m, "\n");
  8121. +
  8122. + return 0;
  8123. +}
  8124. +
  8125. +static int vc_cma_proc_open(struct inode *inode, struct file *file)
  8126. +{
  8127. + return single_open(file, vc_cma_show_info, NULL);
  8128. +}
  8129. +
  8130. +/****************************************************************************
  8131. +*
  8132. +* vc_cma_proc_write
  8133. +*
  8134. +***************************************************************************/
  8135. +
  8136. +static int vc_cma_proc_write(struct file *file,
  8137. + const char __user *buffer,
  8138. + size_t size, loff_t *ppos)
  8139. +{
  8140. + int rc = -EFAULT;
  8141. + char input_str[20];
  8142. +
  8143. + memset(input_str, 0, sizeof(input_str));
  8144. +
  8145. + if (size > sizeof(input_str)) {
  8146. + LOG_ERR("%s: input string length too long", __func__);
  8147. + goto out;
  8148. + }
  8149. +
  8150. + if (copy_from_user(input_str, buffer, size - 1)) {
  8151. + LOG_ERR("%s: failed to get input string", __func__);
  8152. + goto out;
  8153. + }
  8154. +#define ALLOC_STR "alloc"
  8155. +#define FREE_STR "free"
  8156. +#define DEBUG_STR "debug"
  8157. +#define RESERVE_STR "reserve"
  8158. + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
  8159. + int size;
  8160. + char *p = input_str + strlen(ALLOC_STR);
  8161. +
  8162. + while (*p == ' ')
  8163. + p++;
  8164. + size = memparse(p, NULL);
  8165. + LOG_ERR("/proc/vc-cma: alloc %d", size);
  8166. + if (size)
  8167. + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
  8168. + size / VC_CMA_CHUNK_SIZE, 0);
  8169. + else
  8170. + LOG_ERR("invalid size '%s'", p);
  8171. + rc = size;
  8172. + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
  8173. + int size;
  8174. + char *p = input_str + strlen(FREE_STR);
  8175. +
  8176. + while (*p == ' ')
  8177. + p++;
  8178. + size = memparse(p, NULL);
  8179. + LOG_ERR("/proc/vc-cma: free %d", size);
  8180. + if (size)
  8181. + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
  8182. + size / VC_CMA_CHUNK_SIZE, 0);
  8183. + else
  8184. + LOG_ERR("invalid size '%s'", p);
  8185. + rc = size;
  8186. + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
  8187. + char *p = input_str + strlen(DEBUG_STR);
  8188. + while (*p == ' ')
  8189. + p++;
  8190. + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
  8191. + vc_cma_debug = 1;
  8192. + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
  8193. + vc_cma_debug = 0;
  8194. + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
  8195. + rc = size;
  8196. + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
  8197. + int size;
  8198. + int reserved;
  8199. + char *p = input_str + strlen(RESERVE_STR);
  8200. + while (*p == ' ')
  8201. + p++;
  8202. + size = memparse(p, NULL);
  8203. +
  8204. + reserved = vc_cma_set_reserve(size, current->tgid);
  8205. + rc = (reserved >= 0) ? size : reserved;
  8206. + }
  8207. +
  8208. +out:
  8209. + return rc;
  8210. +}
  8211. +
  8212. +/****************************************************************************
  8213. +*
  8214. +* File Operations for /proc interface.
  8215. +*
  8216. +***************************************************************************/
  8217. +
  8218. +static const struct file_operations vc_cma_proc_fops = {
  8219. + .open = vc_cma_proc_open,
  8220. + .read = seq_read,
  8221. + .write = vc_cma_proc_write,
  8222. + .llseek = seq_lseek,
  8223. + .release = single_release
  8224. +};
  8225. +
  8226. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
  8227. +{
  8228. + struct vc_cma_reserve_user *user = NULL;
  8229. + int delta = 0;
  8230. + int i;
  8231. +
  8232. + if (down_interruptible(&vc_cma_reserve_mutex))
  8233. + return -ERESTARTSYS;
  8234. +
  8235. + for (i = 0; i < vc_cma_reserve_count; i++) {
  8236. + if (pid == vc_cma_reserve_users[i].pid) {
  8237. + user = &vc_cma_reserve_users[i];
  8238. + delta = reserve - user->reserve;
  8239. + if (reserve)
  8240. + user->reserve = reserve;
  8241. + else {
  8242. + /* Remove this entry by copying downwards */
  8243. + while ((i + 1) < vc_cma_reserve_count) {
  8244. + user[0].pid = user[1].pid;
  8245. + user[0].reserve = user[1].reserve;
  8246. + user++;
  8247. + i++;
  8248. + }
  8249. + vc_cma_reserve_count--;
  8250. + user = NULL;
  8251. + }
  8252. + break;
  8253. + }
  8254. + }
  8255. +
  8256. + if (reserve && !user) {
  8257. + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
  8258. + LOG_ERR("vc-cma: Too many reservations - "
  8259. + "increase CMA_RESERVE_COUNT_MAX");
  8260. + up(&vc_cma_reserve_mutex);
  8261. + return -EBUSY;
  8262. + }
  8263. + user = &vc_cma_reserve_users[vc_cma_reserve_count];
  8264. + user->pid = pid;
  8265. + user->reserve = reserve;
  8266. + delta = reserve;
  8267. + vc_cma_reserve_count++;
  8268. + }
  8269. +
  8270. + vc_cma_reserve_total += delta;
  8271. +
  8272. + send_vc_msg(VC_CMA_MSG_RESERVE,
  8273. + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
  8274. +
  8275. + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
  8276. +
  8277. + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
  8278. + reserve, pid, vc_cma_reserve_total);
  8279. +
  8280. + up(&vc_cma_reserve_mutex);
  8281. +
  8282. + return vc_cma_reserve_total;
  8283. +}
  8284. +
  8285. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  8286. + VCHIQ_HEADER_T * header,
  8287. + VCHIQ_SERVICE_HANDLE_T service,
  8288. + void *bulk_userdata)
  8289. +{
  8290. + switch (reason) {
  8291. + case VCHIQ_MESSAGE_AVAILABLE:
  8292. + if (!send_worker_msg(header))
  8293. + return VCHIQ_RETRY;
  8294. + break;
  8295. + case VCHIQ_SERVICE_CLOSED:
  8296. + LOG_DBG("CMA service closed");
  8297. + break;
  8298. + default:
  8299. + LOG_ERR("Unexpected CMA callback reason %d", reason);
  8300. + break;
  8301. + }
  8302. + return VCHIQ_SUCCESS;
  8303. +}
  8304. +
  8305. +static void send_vc_msg(unsigned short type,
  8306. + unsigned short param1, unsigned short param2)
  8307. +{
  8308. + unsigned short msg[] = { type, param1, param2 };
  8309. + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
  8310. + VCHIQ_STATUS_T ret;
  8311. + vchiq_use_service(cma_service);
  8312. + ret = vchiq_queue_message(cma_service, &elem, 1);
  8313. + vchiq_release_service(cma_service);
  8314. + if (ret != VCHIQ_SUCCESS)
  8315. + LOG_ERR("vchiq_queue_message returned %x", ret);
  8316. +}
  8317. +
  8318. +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
  8319. +{
  8320. + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
  8321. + return false;
  8322. + vchiu_queue_push(&cma_msg_queue, msg);
  8323. + up(&vc_cma_worker_queue_push_mutex);
  8324. + return true;
  8325. +}
  8326. +
  8327. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
  8328. +{
  8329. + int i;
  8330. + for (i = 0; i < num_chunks; i++) {
  8331. + struct page *chunk;
  8332. + unsigned int chunk_num;
  8333. + uint8_t *chunk_addr;
  8334. + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
  8335. +
  8336. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  8337. + PAGES_PER_CHUNK,
  8338. + VC_CMA_CHUNK_ORDER);
  8339. + if (!chunk)
  8340. + break;
  8341. +
  8342. + chunk_addr = page_address(chunk);
  8343. + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
  8344. + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
  8345. + chunk_size);
  8346. +
  8347. + chunk_num =
  8348. + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
  8349. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  8350. + VC_CMA_CHUNK_SIZE) != 0);
  8351. + if (chunk_num >= vc_cma_chunks) {
  8352. + LOG_ERR("%s: ===============================",
  8353. + __func__);
  8354. + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
  8355. + "bad SPARSEMEM configuration?",
  8356. + __func__, (unsigned int)page_to_phys(chunk),
  8357. + vc_cma_base, vc_cma_base + vc_cma_size - 1);
  8358. + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
  8359. + (void*)0/*vc_cma_device.dev.cma_area*/);
  8360. + LOG_ERR("%s: ===============================",
  8361. + __func__);
  8362. + break;
  8363. + }
  8364. + reply->params[i] = chunk_num;
  8365. + vc_cma_chunks_used++;
  8366. + }
  8367. +
  8368. + if (i < num_chunks) {
  8369. + LOG_ERR("%s: dma_alloc_from_contiguous failed "
  8370. + "for %x bytes (alloc %d of %d, %d free)",
  8371. + __func__, VC_CMA_CHUNK_SIZE, i,
  8372. + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
  8373. + num_chunks = i;
  8374. + }
  8375. +
  8376. + LOG_DBG("CMA allocated %d chunks -> %d used",
  8377. + num_chunks, vc_cma_chunks_used);
  8378. + reply->type = VC_CMA_MSG_ALLOCATED;
  8379. +
  8380. + {
  8381. + VCHIQ_ELEMENT_T elem = {
  8382. + reply,
  8383. + offsetof(struct cma_msg, params[0]) +
  8384. + num_chunks * sizeof(reply->params[0])
  8385. + };
  8386. + VCHIQ_STATUS_T ret;
  8387. + vchiq_use_service(cma_service);
  8388. + ret = vchiq_queue_message(cma_service, &elem, 1);
  8389. + vchiq_release_service(cma_service);
  8390. + if (ret != VCHIQ_SUCCESS)
  8391. + LOG_ERR("vchiq_queue_message return " "%x", ret);
  8392. + }
  8393. +
  8394. + return num_chunks;
  8395. +}
  8396. +
  8397. +static int cma_worker_proc(void *param)
  8398. +{
  8399. + static struct cma_msg reply;
  8400. + (void)param;
  8401. +
  8402. + while (1) {
  8403. + VCHIQ_HEADER_T *msg;
  8404. + static struct cma_msg msg_copy;
  8405. + struct cma_msg *cma_msg = &msg_copy;
  8406. + int type, msg_size;
  8407. +
  8408. + msg = vchiu_queue_pop(&cma_msg_queue);
  8409. + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
  8410. + msg_size = msg->size;
  8411. + memcpy(&msg_copy, msg->data, msg_size);
  8412. + type = cma_msg->type;
  8413. + vchiq_release_message(cma_service, msg);
  8414. + } else {
  8415. + msg_size = 0;
  8416. + type = (int)msg;
  8417. + if (type == VC_CMA_MSG_QUIT)
  8418. + break;
  8419. + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
  8420. + msg = NULL;
  8421. + cma_msg = NULL;
  8422. + } else {
  8423. + BUG();
  8424. + continue;
  8425. + }
  8426. + }
  8427. +
  8428. + switch (type) {
  8429. + case VC_CMA_MSG_ALLOC:{
  8430. + int num_chunks, free_chunks;
  8431. + num_chunks = cma_msg->params[0];
  8432. + free_chunks =
  8433. + vc_cma_chunks - vc_cma_chunks_used;
  8434. + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
  8435. + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
  8436. + LOG_ERR
  8437. + ("CMA_MSG_ALLOC - chunk count (%d) "
  8438. + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
  8439. + num_chunks,
  8440. + VC_CMA_MAX_PARAMS_PER_MSG);
  8441. + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
  8442. + }
  8443. +
  8444. + if (num_chunks > free_chunks) {
  8445. + LOG_ERR
  8446. + ("CMA_MSG_ALLOC - chunk count (%d) "
  8447. + "exceeds free chunks (%d)",
  8448. + num_chunks, free_chunks);
  8449. + num_chunks = free_chunks;
  8450. + }
  8451. +
  8452. + vc_cma_alloc_chunks(num_chunks, &reply);
  8453. + }
  8454. + break;
  8455. +
  8456. + case VC_CMA_MSG_FREE:{
  8457. + int chunk_count =
  8458. + (msg_size -
  8459. + offsetof(struct cma_msg,
  8460. + params)) /
  8461. + sizeof(cma_msg->params[0]);
  8462. + int i;
  8463. + BUG_ON(chunk_count <= 0);
  8464. +
  8465. + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
  8466. + chunk_count, cma_msg->params[0]);
  8467. + for (i = 0; i < chunk_count; i++) {
  8468. + int chunk_num = cma_msg->params[i];
  8469. + struct page *page = vc_cma_base_page +
  8470. + chunk_num * PAGES_PER_CHUNK;
  8471. + if (chunk_num >= vc_cma_chunks) {
  8472. + LOG_ERR
  8473. + ("CMA_MSG_FREE - chunk %d of %d"
  8474. + " (value %x) exceeds maximum "
  8475. + "(%x)", i, chunk_count,
  8476. + chunk_num,
  8477. + vc_cma_chunks - 1);
  8478. + break;
  8479. + }
  8480. +
  8481. + if (!dma_release_from_contiguous
  8482. + (NULL /*&vc_cma_device.dev*/, page,
  8483. + PAGES_PER_CHUNK)) {
  8484. + LOG_ERR
  8485. + ("CMA_MSG_FREE - failed to "
  8486. + "release chunk %d (phys %x, "
  8487. + "page %x)", chunk_num,
  8488. + page_to_phys(page),
  8489. + (unsigned int)page);
  8490. + }
  8491. + vc_cma_chunks_used--;
  8492. + }
  8493. + LOG_DBG("CMA released %d chunks -> %d used",
  8494. + i, vc_cma_chunks_used);
  8495. + }
  8496. + break;
  8497. +
  8498. + case VC_CMA_MSG_UPDATE_RESERVE:{
  8499. + int chunks_needed =
  8500. + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
  8501. + 1)
  8502. + / VC_CMA_CHUNK_SIZE) -
  8503. + vc_cma_chunks_reserved;
  8504. +
  8505. + LOG_DBG
  8506. + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
  8507. + chunks_needed);
  8508. +
  8509. + /* Cap the reservations to what is available */
  8510. + if (chunks_needed > 0) {
  8511. + if (chunks_needed >
  8512. + (vc_cma_chunks -
  8513. + vc_cma_chunks_used))
  8514. + chunks_needed =
  8515. + (vc_cma_chunks -
  8516. + vc_cma_chunks_used);
  8517. +
  8518. + chunks_needed =
  8519. + vc_cma_alloc_chunks(chunks_needed,
  8520. + &reply);
  8521. + }
  8522. +
  8523. + LOG_DBG
  8524. + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
  8525. + chunks_needed);
  8526. + vc_cma_chunks_reserved += chunks_needed;
  8527. + }
  8528. + break;
  8529. +
  8530. + default:
  8531. + LOG_ERR("unexpected msg type %d", type);
  8532. + break;
  8533. + }
  8534. + }
  8535. +
  8536. + LOG_DBG("quitting...");
  8537. + return 0;
  8538. +}
  8539. +
  8540. +/****************************************************************************
  8541. +*
  8542. +* vc_cma_connected_init
  8543. +*
  8544. +* This function is called once the videocore has been connected.
  8545. +*
  8546. +***************************************************************************/
  8547. +
  8548. +static void vc_cma_connected_init(void)
  8549. +{
  8550. + VCHIQ_SERVICE_PARAMS_T service_params;
  8551. +
  8552. + LOG_DBG("vc_cma_connected_init");
  8553. +
  8554. + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
  8555. + LOG_ERR("could not create CMA msg queue");
  8556. + goto fail_queue;
  8557. + }
  8558. +
  8559. + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
  8560. + goto fail_vchiq_init;
  8561. +
  8562. + vchiq_connect(cma_instance);
  8563. +
  8564. + service_params.fourcc = VC_CMA_FOURCC;
  8565. + service_params.callback = cma_service_callback;
  8566. + service_params.userdata = NULL;
  8567. + service_params.version = VC_CMA_VERSION;
  8568. + service_params.version_min = VC_CMA_VERSION;
  8569. +
  8570. + if (vchiq_open_service(cma_instance, &service_params,
  8571. + &cma_service) != VCHIQ_SUCCESS) {
  8572. + LOG_ERR("failed to open service - already in use?");
  8573. + goto fail_vchiq_open;
  8574. + }
  8575. +
  8576. + vchiq_release_service(cma_service);
  8577. +
  8578. + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
  8579. + if (!cma_worker) {
  8580. + LOG_ERR("could not create CMA worker thread");
  8581. + goto fail_worker;
  8582. + }
  8583. + set_user_nice(cma_worker, -20);
  8584. + wake_up_process(cma_worker);
  8585. +
  8586. + return;
  8587. +
  8588. +fail_worker:
  8589. + vchiq_close_service(cma_service);
  8590. +fail_vchiq_open:
  8591. + vchiq_shutdown(cma_instance);
  8592. +fail_vchiq_init:
  8593. + vchiu_queue_delete(&cma_msg_queue);
  8594. +fail_queue:
  8595. + return;
  8596. +}
  8597. +
  8598. +void
  8599. +loud_error_header(void)
  8600. +{
  8601. + if (in_loud_error)
  8602. + return;
  8603. +
  8604. + LOG_ERR("============================================================"
  8605. + "================");
  8606. + LOG_ERR("============================================================"
  8607. + "================");
  8608. + LOG_ERR("=====");
  8609. +
  8610. + in_loud_error = 1;
  8611. +}
  8612. +
  8613. +void
  8614. +loud_error_footer(void)
  8615. +{
  8616. + if (!in_loud_error)
  8617. + return;
  8618. +
  8619. + LOG_ERR("=====");
  8620. + LOG_ERR("============================================================"
  8621. + "================");
  8622. + LOG_ERR("============================================================"
  8623. + "================");
  8624. +
  8625. + in_loud_error = 0;
  8626. +}
  8627. +
  8628. +#if 1
  8629. +static int check_cma_config(void) { return 1; }
  8630. +#else
  8631. +static int
  8632. +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
  8633. + const char *symbol,
  8634. + void *buf, size_t bufsize)
  8635. +{
  8636. + VC_MEM_ADDR_T vcMemAddr;
  8637. + size_t vcMemSize;
  8638. + uint8_t *mapAddr;
  8639. + off_t vcMapAddr;
  8640. +
  8641. + if (!LookupVideoCoreSymbol(handle, symbol,
  8642. + &vcMemAddr,
  8643. + &vcMemSize)) {
  8644. + loud_error_header();
  8645. + loud_error(
  8646. + "failed to find VC symbol \"%s\".",
  8647. + symbol);
  8648. + loud_error_footer();
  8649. + return 0;
  8650. + }
  8651. +
  8652. + if (vcMemSize != bufsize) {
  8653. + loud_error_header();
  8654. + loud_error(
  8655. + "VC symbol \"%s\" is the wrong size.",
  8656. + symbol);
  8657. + loud_error_footer();
  8658. + return 0;
  8659. + }
  8660. +
  8661. + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
  8662. + vcMapAddr += mm_vc_mem_phys_addr;
  8663. + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
  8664. + if (mapAddr == 0) {
  8665. + loud_error_header();
  8666. + loud_error(
  8667. + "failed to ioremap \"%s\" @ 0x%x "
  8668. + "(phys: 0x%x, size: %u).",
  8669. + symbol,
  8670. + (unsigned int)vcMapAddr,
  8671. + (unsigned int)vcMemAddr,
  8672. + (unsigned int)vcMemSize);
  8673. + loud_error_footer();
  8674. + return 0;
  8675. + }
  8676. +
  8677. + memcpy(buf, mapAddr, bufsize);
  8678. + iounmap(mapAddr);
  8679. +
  8680. + return 1;
  8681. +}
  8682. +
  8683. +
  8684. +static int
  8685. +check_cma_config(void)
  8686. +{
  8687. + VC_MEM_ACCESS_HANDLE_T mem_hndl;
  8688. + VC_MEM_ADDR_T mempool_start;
  8689. + VC_MEM_ADDR_T mempool_end;
  8690. + VC_MEM_ADDR_T mempool_offline_start;
  8691. + VC_MEM_ADDR_T mempool_offline_end;
  8692. + VC_MEM_ADDR_T cam_alloc_base;
  8693. + VC_MEM_ADDR_T cam_alloc_size;
  8694. + VC_MEM_ADDR_T cam_alloc_end;
  8695. + int success = 0;
  8696. +
  8697. + if (OpenVideoCoreMemory(&mem_hndl) != 0)
  8698. + goto out;
  8699. +
  8700. + /* Read the relevant VideoCore variables */
  8701. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
  8702. + &mempool_start,
  8703. + sizeof(mempool_start)))
  8704. + goto close;
  8705. +
  8706. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
  8707. + &mempool_end,
  8708. + sizeof(mempool_end)))
  8709. + goto close;
  8710. +
  8711. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
  8712. + &mempool_offline_start,
  8713. + sizeof(mempool_offline_start)))
  8714. + goto close;
  8715. +
  8716. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
  8717. + &mempool_offline_end,
  8718. + sizeof(mempool_offline_end)))
  8719. + goto close;
  8720. +
  8721. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
  8722. + &cam_alloc_base,
  8723. + sizeof(cam_alloc_base)))
  8724. + goto close;
  8725. +
  8726. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
  8727. + &cam_alloc_size,
  8728. + sizeof(cam_alloc_size)))
  8729. + goto close;
  8730. +
  8731. + cam_alloc_end = cam_alloc_base + cam_alloc_size;
  8732. +
  8733. + success = 1;
  8734. +
  8735. + /* Now the sanity checks */
  8736. + if (!mempool_offline_start)
  8737. + mempool_offline_start = mempool_start;
  8738. + if (!mempool_offline_end)
  8739. + mempool_offline_end = mempool_end;
  8740. +
  8741. + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
  8742. + loud_error_header();
  8743. + loud_error(
  8744. + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
  8745. + "vc_cma_base(%x)",
  8746. + mempool_offline_start,
  8747. + VCADDR_TO_PHYSADDR(mempool_offline_start),
  8748. + vc_cma_base);
  8749. + success = 0;
  8750. + }
  8751. +
  8752. + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
  8753. + (vc_cma_base + vc_cma_size)) {
  8754. + loud_error_header();
  8755. + loud_error(
  8756. + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
  8757. + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
  8758. + mempool_offline_start,
  8759. + VCADDR_TO_PHYSADDR(mempool_offline_end),
  8760. + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
  8761. + success = 0;
  8762. + }
  8763. +
  8764. + if (mempool_end < mempool_start) {
  8765. + loud_error_header();
  8766. + loud_error(
  8767. + "__MEMPOOL_END(%x) must not be before "
  8768. + "__MEMPOOL_START(%x)",
  8769. + mempool_end,
  8770. + mempool_start);
  8771. + success = 0;
  8772. + }
  8773. +
  8774. + if (mempool_offline_end < mempool_offline_start) {
  8775. + loud_error_header();
  8776. + loud_error(
  8777. + "__MEMPOOL_OFFLINE_END(%x) must not be before "
  8778. + "__MEMPOOL_OFFLINE_START(%x)",
  8779. + mempool_offline_end,
  8780. + mempool_offline_start);
  8781. + success = 0;
  8782. + }
  8783. +
  8784. + if (mempool_offline_start < mempool_start) {
  8785. + loud_error_header();
  8786. + loud_error(
  8787. + "__MEMPOOL_OFFLINE_START(%x) must not be before "
  8788. + "__MEMPOOL_START(%x)",
  8789. + mempool_offline_start,
  8790. + mempool_start);
  8791. + success = 0;
  8792. + }
  8793. +
  8794. + if (mempool_offline_end > mempool_end) {
  8795. + loud_error_header();
  8796. + loud_error(
  8797. + "__MEMPOOL_OFFLINE_END(%x) must not be after "
  8798. + "__MEMPOOL_END(%x)",
  8799. + mempool_offline_end,
  8800. + mempool_end);
  8801. + success = 0;
  8802. + }
  8803. +
  8804. + if ((cam_alloc_base < mempool_end) &&
  8805. + (cam_alloc_end > mempool_start)) {
  8806. + loud_error_header();
  8807. + loud_error(
  8808. + "cam_alloc pool(%x-%x) overlaps "
  8809. + "mempool(%x-%x)",
  8810. + cam_alloc_base, cam_alloc_end,
  8811. + mempool_start, mempool_end);
  8812. + success = 0;
  8813. + }
  8814. +
  8815. + loud_error_footer();
  8816. +
  8817. +close:
  8818. + CloseVideoCoreMemory(mem_hndl);
  8819. +
  8820. +out:
  8821. + return success;
  8822. +}
  8823. +#endif
  8824. +
  8825. +static int vc_cma_init(void)
  8826. +{
  8827. + int rc = -EFAULT;
  8828. + struct device *dev;
  8829. +
  8830. + if (!check_cma_config())
  8831. + goto out_release;
  8832. +
  8833. + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
  8834. + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
  8835. + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
  8836. + vc_cma_size, vc_cma_size / (1024 * 1024));
  8837. + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
  8838. + vc_cma_initial, vc_cma_initial / (1024 * 1024));
  8839. +
  8840. + vc_cma_base_page = phys_to_page(vc_cma_base);
  8841. +
  8842. + if (vc_cma_chunks) {
  8843. + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
  8844. +
  8845. + for (vc_cma_chunks_used = 0;
  8846. + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
  8847. + struct page *chunk;
  8848. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  8849. + PAGES_PER_CHUNK,
  8850. + VC_CMA_CHUNK_ORDER);
  8851. + if (!chunk)
  8852. + break;
  8853. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  8854. + VC_CMA_CHUNK_SIZE) != 0);
  8855. + }
  8856. + if (vc_cma_chunks_used != chunks_needed) {
  8857. + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
  8858. + "bytes, allocation %d of %d)",
  8859. + __func__, VC_CMA_CHUNK_SIZE,
  8860. + vc_cma_chunks_used, chunks_needed);
  8861. + goto out_release;
  8862. + }
  8863. +
  8864. + vchiq_add_connected_callback(vc_cma_connected_init);
  8865. + }
  8866. +
  8867. + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
  8868. + if (rc < 0) {
  8869. + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
  8870. + goto out_release;
  8871. + }
  8872. +
  8873. + cdev_init(&vc_cma_cdev, &vc_cma_fops);
  8874. + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
  8875. + if (rc != 0) {
  8876. + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
  8877. + goto out_unregister;
  8878. + }
  8879. +
  8880. + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
  8881. + if (IS_ERR(vc_cma_class)) {
  8882. + rc = PTR_ERR(vc_cma_class);
  8883. + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
  8884. + goto out_cdev_del;
  8885. + }
  8886. +
  8887. + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
  8888. + DRIVER_NAME);
  8889. + if (IS_ERR(dev)) {
  8890. + rc = PTR_ERR(dev);
  8891. + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
  8892. + goto out_class_destroy;
  8893. + }
  8894. +
  8895. + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
  8896. + if (vc_cma_proc_entry == NULL) {
  8897. + rc = -EFAULT;
  8898. + LOG_ERR("%s: proc_create failed", __func__);
  8899. + goto out_device_destroy;
  8900. + }
  8901. +
  8902. + vc_cma_inited = 1;
  8903. + return 0;
  8904. +
  8905. +out_device_destroy:
  8906. + device_destroy(vc_cma_class, vc_cma_devnum);
  8907. +
  8908. +out_class_destroy:
  8909. + class_destroy(vc_cma_class);
  8910. + vc_cma_class = NULL;
  8911. +
  8912. +out_cdev_del:
  8913. + cdev_del(&vc_cma_cdev);
  8914. +
  8915. +out_unregister:
  8916. + unregister_chrdev_region(vc_cma_devnum, 1);
  8917. +
  8918. +out_release:
  8919. + /* It is tempting to try to clean up by calling
  8920. + dma_release_from_contiguous for all allocated chunks, but it isn't
  8921. + a very safe thing to do. If vc_cma_initial is non-zero it is because
  8922. + VideoCore is already using that memory, so giving it back to Linux
  8923. + is likely to be fatal.
  8924. + */
  8925. + return -1;
  8926. +}
  8927. +
  8928. +/****************************************************************************
  8929. +*
  8930. +* vc_cma_exit
  8931. +*
  8932. +***************************************************************************/
  8933. +
  8934. +static void __exit vc_cma_exit(void)
  8935. +{
  8936. + LOG_DBG("%s: called", __func__);
  8937. +
  8938. + if (vc_cma_inited) {
  8939. + remove_proc_entry(DRIVER_NAME, NULL);
  8940. + device_destroy(vc_cma_class, vc_cma_devnum);
  8941. + class_destroy(vc_cma_class);
  8942. + cdev_del(&vc_cma_cdev);
  8943. + unregister_chrdev_region(vc_cma_devnum, 1);
  8944. + }
  8945. +}
  8946. +
  8947. +module_init(vc_cma_init);
  8948. +module_exit(vc_cma_exit);
  8949. +MODULE_LICENSE("GPL");
  8950. +MODULE_AUTHOR("Broadcom Corporation");
  8951. diff -Nur linux-3.13.6/drivers/char/hw_random/bcm2708-rng.c linux-raspberry-pi/drivers/char/hw_random/bcm2708-rng.c
  8952. --- linux-3.13.6/drivers/char/hw_random/bcm2708-rng.c 1970-01-01 01:00:00.000000000 +0100
  8953. +++ linux-raspberry-pi/drivers/char/hw_random/bcm2708-rng.c 2014-03-11 16:52:29.000000000 +0100
  8954. @@ -0,0 +1,117 @@
  8955. +/**
  8956. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  8957. + *
  8958. + * Redistribution and use in source and binary forms, with or without
  8959. + * modification, are permitted provided that the following conditions
  8960. + * are met:
  8961. + * 1. Redistributions of source code must retain the above copyright
  8962. + * notice, this list of conditions, and the following disclaimer,
  8963. + * without modification.
  8964. + * 2. Redistributions in binary form must reproduce the above copyright
  8965. + * notice, this list of conditions and the following disclaimer in the
  8966. + * documentation and/or other materials provided with the distribution.
  8967. + * 3. The names of the above-listed copyright holders may not be used
  8968. + * to endorse or promote products derived from this software without
  8969. + * specific prior written permission.
  8970. + *
  8971. + * ALTERNATIVELY, this software may be distributed under the terms of the
  8972. + * GNU General Public License ("GPL") version 2, as published by the Free
  8973. + * Software Foundation.
  8974. + *
  8975. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  8976. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  8977. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  8978. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  8979. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  8980. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  8981. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  8982. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  8983. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  8984. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  8985. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8986. + */
  8987. +
  8988. +#include <linux/kernel.h>
  8989. +#include <linux/module.h>
  8990. +#include <linux/init.h>
  8991. +#include <linux/hw_random.h>
  8992. +#include <linux/printk.h>
  8993. +
  8994. +#include <asm/io.h>
  8995. +#include <mach/hardware.h>
  8996. +#include <mach/platform.h>
  8997. +
  8998. +#define RNG_CTRL (0x0)
  8999. +#define RNG_STATUS (0x4)
  9000. +#define RNG_DATA (0x8)
  9001. +#define RNG_FF_THRESHOLD (0xc)
  9002. +
  9003. +/* enable rng */
  9004. +#define RNG_RBGEN 0x1
  9005. +/* double speed, less random mode */
  9006. +#define RNG_RBG2X 0x2
  9007. +
  9008. +/* the initial numbers generated are "less random" so will be discarded */
  9009. +#define RNG_WARMUP_COUNT 0x40000
  9010. +
  9011. +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
  9012. +{
  9013. + void __iomem *rng_base = (void __iomem *)rng->priv;
  9014. + unsigned words;
  9015. + /* wait for a random number to be in fifo */
  9016. + do {
  9017. + words = __raw_readl(rng_base + RNG_STATUS)>>24;
  9018. + }
  9019. + while (words == 0);
  9020. + /* read the random number */
  9021. + *buffer = __raw_readl(rng_base + RNG_DATA);
  9022. + return 4;
  9023. +}
  9024. +
  9025. +static struct hwrng bcm2708_rng_ops = {
  9026. + .name = "bcm2708",
  9027. + .data_read = bcm2708_rng_data_read,
  9028. +};
  9029. +
  9030. +static int __init bcm2708_rng_init(void)
  9031. +{
  9032. + void __iomem *rng_base;
  9033. + int err;
  9034. +
  9035. + /* map peripheral */
  9036. + rng_base = ioremap(RNG_BASE, 0x10);
  9037. + pr_info("bcm2708_rng_init=%p\n", rng_base);
  9038. + if (!rng_base) {
  9039. + pr_err("bcm2708_rng_init failed to ioremap\n");
  9040. + return -ENOMEM;
  9041. + }
  9042. + bcm2708_rng_ops.priv = (unsigned long)rng_base;
  9043. + /* register driver */
  9044. + err = hwrng_register(&bcm2708_rng_ops);
  9045. + if (err) {
  9046. + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
  9047. + iounmap(rng_base);
  9048. + } else {
  9049. + /* set warm-up count & enable */
  9050. + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  9051. + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  9052. + }
  9053. + return err;
  9054. +}
  9055. +
  9056. +static void __exit bcm2708_rng_exit(void)
  9057. +{
  9058. + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
  9059. + pr_info("bcm2708_rng_exit\n");
  9060. + /* disable rng hardware */
  9061. + __raw_writel(0, rng_base + RNG_CTRL);
  9062. + /* unregister driver */
  9063. + hwrng_unregister(&bcm2708_rng_ops);
  9064. + iounmap(rng_base);
  9065. +}
  9066. +
  9067. +module_init(bcm2708_rng_init);
  9068. +module_exit(bcm2708_rng_exit);
  9069. +
  9070. +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
  9071. +MODULE_LICENSE("GPL and additional rights");
  9072. diff -Nur linux-3.13.6/drivers/char/hw_random/Kconfig linux-raspberry-pi/drivers/char/hw_random/Kconfig
  9073. --- linux-3.13.6/drivers/char/hw_random/Kconfig 2014-03-07 07:07:02.000000000 +0100
  9074. +++ linux-raspberry-pi/drivers/char/hw_random/Kconfig 2014-03-11 16:54:56.000000000 +0100
  9075. @@ -341,6 +341,17 @@
  9076. If unsure, say Y.
  9077. +config HW_RANDOM_BCM2708
  9078. + tristate "BCM2708 generic true random number generator support"
  9079. + depends on HW_RANDOM && ARCH_BCM2708
  9080. + ---help---
  9081. + This driver provides the kernel-side support for the BCM2708 hardware.
  9082. +
  9083. + To compile this driver as a module, choose M here: the
  9084. + module will be called bcm2708-rng.
  9085. +
  9086. + If unsure, say N.
  9087. +
  9088. config HW_RANDOM_MSM
  9089. tristate "Qualcomm MSM Random Number Generator support"
  9090. depends on HW_RANDOM && ARCH_MSM
  9091. diff -Nur linux-3.13.6/drivers/char/hw_random/Makefile linux-raspberry-pi/drivers/char/hw_random/Makefile
  9092. --- linux-3.13.6/drivers/char/hw_random/Makefile 2014-03-07 07:07:02.000000000 +0100
  9093. +++ linux-raspberry-pi/drivers/char/hw_random/Makefile 2014-03-11 16:54:56.000000000 +0100
  9094. @@ -29,4 +29,5 @@
  9095. obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
  9096. obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
  9097. obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
  9098. +obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
  9099. obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
  9100. diff -Nur linux-3.13.6/drivers/char/Kconfig linux-raspberry-pi/drivers/char/Kconfig
  9101. --- linux-3.13.6/drivers/char/Kconfig 2014-03-07 07:07:02.000000000 +0100
  9102. +++ linux-raspberry-pi/drivers/char/Kconfig 2014-03-11 16:54:56.000000000 +0100
  9103. @@ -580,6 +580,8 @@
  9104. source "drivers/s390/char/Kconfig"
  9105. +source "drivers/char/broadcom/Kconfig"
  9106. +
  9107. config MSM_SMD_PKT
  9108. bool "Enable device interface for some SMD packet ports"
  9109. default n
  9110. diff -Nur linux-3.13.6/drivers/char/Makefile linux-raspberry-pi/drivers/char/Makefile
  9111. --- linux-3.13.6/drivers/char/Makefile 2014-03-07 07:07:02.000000000 +0100
  9112. +++ linux-raspberry-pi/drivers/char/Makefile 2014-03-11 16:54:56.000000000 +0100
  9113. @@ -62,3 +62,5 @@
  9114. js-rtc-y = rtc.o
  9115. obj-$(CONFIG_TILE_SROM) += tile-srom.o
  9116. +
  9117. +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
  9118. diff -Nur linux-3.13.6/drivers/cpufreq/bcm2835-cpufreq.c linux-raspberry-pi/drivers/cpufreq/bcm2835-cpufreq.c
  9119. --- linux-3.13.6/drivers/cpufreq/bcm2835-cpufreq.c 1970-01-01 01:00:00.000000000 +0100
  9120. +++ linux-raspberry-pi/drivers/cpufreq/bcm2835-cpufreq.c 2014-03-11 16:54:56.000000000 +0100
  9121. @@ -0,0 +1,239 @@
  9122. +/*****************************************************************************
  9123. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  9124. +*
  9125. +* Unless you and Broadcom execute a separate written software license
  9126. +* agreement governing use of this software, this software is licensed to you
  9127. +* under the terms of the GNU General Public License version 2, available at
  9128. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  9129. +*
  9130. +* Notwithstanding the above, under no circumstances may you combine this
  9131. +* software in any way with any other Broadcom software provided under a
  9132. +* license other than the GPL, without Broadcom's express prior written
  9133. +* consent.
  9134. +*****************************************************************************/
  9135. +
  9136. +/*****************************************************************************
  9137. +* FILENAME: bcm2835-cpufreq.h
  9138. +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
  9139. +* processor. Messages are sent to Videocore either setting or requesting the
  9140. +* frequency of the ARM in order to match an appropiate frequency to the current
  9141. +* usage of the processor. The policy which selects the frequency to use is
  9142. +* defined in the kernel .config file, but can be changed during runtime.
  9143. +*****************************************************************************/
  9144. +
  9145. +/* ---------- INCLUDES ---------- */
  9146. +#include <linux/kernel.h>
  9147. +#include <linux/init.h>
  9148. +#include <linux/module.h>
  9149. +#include <linux/cpufreq.h>
  9150. +#include <mach/vcio.h>
  9151. +
  9152. +/* ---------- DEFINES ---------- */
  9153. +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
  9154. +#define MODULE_NAME "bcm2835-cpufreq"
  9155. +
  9156. +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
  9157. +
  9158. +/* debug printk macros */
  9159. +#ifdef CPUFREQ_DEBUG_ENABLE
  9160. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  9161. +#else
  9162. +#define print_debug(fmt,...)
  9163. +#endif
  9164. +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  9165. +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
  9166. +
  9167. +/* tag part of the message */
  9168. +struct vc_msg_tag {
  9169. + uint32_t tag_id; /* the message id */
  9170. + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
  9171. + uint32_t data_size; /* amount of data being sent or received */
  9172. + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
  9173. + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
  9174. +};
  9175. +
  9176. +/* message structure to be sent to videocore */
  9177. +struct vc_msg {
  9178. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  9179. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  9180. + struct vc_msg_tag tag; /* the tag structure above to make */
  9181. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  9182. +};
  9183. +
  9184. +/* ---------- GLOBALS ---------- */
  9185. +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
  9186. +
  9187. +/*
  9188. + ===============================================
  9189. + clk_rate either gets or sets the clock rates.
  9190. + ===============================================
  9191. +*/
  9192. +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
  9193. +{
  9194. + int s, actual_rate=0;
  9195. + struct vc_msg msg;
  9196. +
  9197. + /* wipe all previous message data */
  9198. + memset(&msg, 0, sizeof msg);
  9199. +
  9200. + msg.msg_size = sizeof msg;
  9201. +
  9202. + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
  9203. + msg.tag.buffer_size = 8;
  9204. + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
  9205. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  9206. + msg.tag.val = arm_rate * 1000;
  9207. +
  9208. + /* send the message */
  9209. + s = bcm_mailbox_property(&msg, sizeof msg);
  9210. +
  9211. + /* check if it was all ok and return the rate in KHz */
  9212. + if (s == 0 && (msg.request_code & 0x80000000))
  9213. + actual_rate = msg.tag.val/1000;
  9214. +
  9215. + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
  9216. + return actual_rate;
  9217. +}
  9218. +
  9219. +static uint32_t bcm2835_cpufreq_get_clock(int tag)
  9220. +{
  9221. + int s;
  9222. + int arm_rate = 0;
  9223. + struct vc_msg msg;
  9224. +
  9225. + /* wipe all previous message data */
  9226. + memset(&msg, 0, sizeof msg);
  9227. +
  9228. + msg.msg_size = sizeof msg;
  9229. + msg.tag.tag_id = tag;
  9230. + msg.tag.buffer_size = 8;
  9231. + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
  9232. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  9233. +
  9234. + /* send the message */
  9235. + s = bcm_mailbox_property(&msg, sizeof msg);
  9236. +
  9237. + /* check if it was all ok and return the rate in KHz */
  9238. + if (s == 0 && (msg.request_code & 0x80000000))
  9239. + arm_rate = msg.tag.val/1000;
  9240. +
  9241. + print_debug("%s frequency = %d\n",
  9242. + tag == VCMSG_GET_CLOCK_RATE ? "Current":
  9243. + tag == VCMSG_GET_MIN_CLOCK ? "Min":
  9244. + tag == VCMSG_GET_MAX_CLOCK ? "Max":
  9245. + "Unexpected", arm_rate);
  9246. +
  9247. + return arm_rate;
  9248. +}
  9249. +
  9250. +/*
  9251. + ====================================================
  9252. + Module Initialisation registers the cpufreq driver
  9253. + ====================================================
  9254. +*/
  9255. +static int __init bcm2835_cpufreq_module_init(void)
  9256. +{
  9257. + print_debug("IN\n");
  9258. + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
  9259. +}
  9260. +
  9261. +/*
  9262. + =============
  9263. + Module exit
  9264. + =============
  9265. +*/
  9266. +static void __exit bcm2835_cpufreq_module_exit(void)
  9267. +{
  9268. + print_debug("IN\n");
  9269. + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
  9270. + return;
  9271. +}
  9272. +
  9273. +/*
  9274. + ==============================================================
  9275. + Initialisation function sets up the CPU policy for first use
  9276. + ==============================================================
  9277. +*/
  9278. +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
  9279. +{
  9280. + /* measured value of how long it takes to change frequency */
  9281. + policy->cpuinfo.transition_latency = 355000; /* ns */
  9282. +
  9283. + /* now find out what the maximum and minimum frequencies are */
  9284. + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
  9285. + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
  9286. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  9287. +
  9288. + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
  9289. + return 0;
  9290. +}
  9291. +
  9292. +/*
  9293. + =================================================================================
  9294. + Target function chooses the most appropriate frequency from the table to enable
  9295. + =================================================================================
  9296. +*/
  9297. +
  9298. +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
  9299. +{
  9300. + unsigned int target = target_freq;
  9301. +#ifdef CPUFREQ_DEBUG_ENABLE
  9302. + unsigned int cur = policy->cur;
  9303. +#endif
  9304. + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
  9305. +
  9306. + /* if we are above min and using ondemand, then just use max */
  9307. + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
  9308. + target = policy->max;
  9309. + /* if the frequency is the same, just quit */
  9310. + if (target == policy->cur)
  9311. + return 0;
  9312. +
  9313. + /* otherwise were good to set the clock frequency */
  9314. + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
  9315. +
  9316. + if (!policy->cur)
  9317. + {
  9318. + print_err("Error occurred setting a new frequency (%d)!\n", target);
  9319. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  9320. + return -EINVAL;
  9321. + }
  9322. + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
  9323. + return 0;
  9324. +}
  9325. +
  9326. +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
  9327. +{
  9328. + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  9329. + print_debug("cpu=%d\n", actual_rate);
  9330. + return actual_rate;
  9331. +}
  9332. +
  9333. +/*
  9334. + =================================================================================
  9335. + Verify ensures that when a policy is changed, it is suitable for the CPU to use
  9336. + =================================================================================
  9337. +*/
  9338. +
  9339. +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
  9340. +{
  9341. + print_info("switching to governor %s\n", policy->governor->name);
  9342. + return 0;
  9343. +}
  9344. +
  9345. +
  9346. +/* the CPUFreq driver */
  9347. +static struct cpufreq_driver bcm2835_cpufreq_driver = {
  9348. + .name = "BCM2835 CPUFreq",
  9349. + .init = bcm2835_cpufreq_driver_init,
  9350. + .verify = bcm2835_cpufreq_driver_verify,
  9351. + .target = bcm2835_cpufreq_driver_target,
  9352. + .get = bcm2835_cpufreq_driver_get
  9353. +};
  9354. +
  9355. +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
  9356. +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
  9357. +MODULE_LICENSE("GPL");
  9358. +
  9359. +module_init(bcm2835_cpufreq_module_init);
  9360. +module_exit(bcm2835_cpufreq_module_exit);
  9361. diff -Nur linux-3.13.6/drivers/cpufreq/Kconfig.arm linux-raspberry-pi/drivers/cpufreq/Kconfig.arm
  9362. --- linux-3.13.6/drivers/cpufreq/Kconfig.arm 2014-03-07 07:07:02.000000000 +0100
  9363. +++ linux-raspberry-pi/drivers/cpufreq/Kconfig.arm 2014-03-11 16:54:56.000000000 +0100
  9364. @@ -218,6 +218,14 @@
  9365. help
  9366. This adds the CPUFreq driver support for SPEAr SOCs.
  9367. +config ARM_BCM2835_CPUFREQ
  9368. + bool "BCM2835 Driver"
  9369. + default y
  9370. + help
  9371. + This adds the CPUFreq driver for BCM2835
  9372. +
  9373. + If in doubt, say N.
  9374. +
  9375. config ARM_TEGRA_CPUFREQ
  9376. bool "TEGRA CPUFreq support"
  9377. depends on ARCH_TEGRA
  9378. diff -Nur linux-3.13.6/drivers/cpufreq/Makefile linux-raspberry-pi/drivers/cpufreq/Makefile
  9379. --- linux-3.13.6/drivers/cpufreq/Makefile 2014-03-07 07:07:02.000000000 +0100
  9380. +++ linux-raspberry-pi/drivers/cpufreq/Makefile 2014-03-11 16:54:56.000000000 +0100
  9381. @@ -73,6 +73,7 @@
  9382. obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
  9383. obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
  9384. obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
  9385. +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
  9386. obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
  9387. obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
  9388. diff -Nur linux-3.13.6/drivers/dma/bcm2708-dmaengine.c linux-raspberry-pi/drivers/dma/bcm2708-dmaengine.c
  9389. --- linux-3.13.6/drivers/dma/bcm2708-dmaengine.c 1970-01-01 01:00:00.000000000 +0100
  9390. +++ linux-raspberry-pi/drivers/dma/bcm2708-dmaengine.c 2014-03-11 16:54:56.000000000 +0100
  9391. @@ -0,0 +1,588 @@
  9392. +/*
  9393. + * BCM2708 DMA engine support
  9394. + *
  9395. + * This driver only supports cyclic DMA transfers
  9396. + * as needed for the I2S module.
  9397. + *
  9398. + * Author: Florian Meier <florian.meier@koalo.de>
  9399. + * Copyright 2013
  9400. + *
  9401. + * Based on
  9402. + * OMAP DMAengine support by Russell King
  9403. + *
  9404. + * BCM2708 DMA Driver
  9405. + * Copyright (C) 2010 Broadcom
  9406. + *
  9407. + * Raspberry Pi PCM I2S ALSA Driver
  9408. + * Copyright (c) by Phil Poole 2013
  9409. + *
  9410. + * MARVELL MMP Peripheral DMA Driver
  9411. + * Copyright 2012 Marvell International Ltd.
  9412. + *
  9413. + * This program is free software; you can redistribute it and/or modify
  9414. + * it under the terms of the GNU General Public License as published by
  9415. + * the Free Software Foundation; either version 2 of the License, or
  9416. + * (at your option) any later version.
  9417. + *
  9418. + * This program is distributed in the hope that it will be useful,
  9419. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9420. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9421. + * GNU General Public License for more details.
  9422. + */
  9423. +#include <linux/dmaengine.h>
  9424. +#include <linux/dma-mapping.h>
  9425. +#include <linux/err.h>
  9426. +#include <linux/init.h>
  9427. +#include <linux/interrupt.h>
  9428. +#include <linux/list.h>
  9429. +#include <linux/module.h>
  9430. +#include <linux/platform_device.h>
  9431. +#include <linux/slab.h>
  9432. +#include <linux/io.h>
  9433. +#include <linux/spinlock.h>
  9434. +#include <linux/irq.h>
  9435. +
  9436. +#include "virt-dma.h"
  9437. +
  9438. +#include <mach/dma.h>
  9439. +#include <mach/irqs.h>
  9440. +
  9441. +struct bcm2708_dmadev {
  9442. + struct dma_device ddev;
  9443. + spinlock_t lock;
  9444. + void __iomem *base;
  9445. + struct device_dma_parameters dma_parms;
  9446. +};
  9447. +
  9448. +struct bcm2708_chan {
  9449. + struct virt_dma_chan vc;
  9450. + struct list_head node;
  9451. +
  9452. + struct dma_slave_config cfg;
  9453. + bool cyclic;
  9454. +
  9455. + int ch;
  9456. + struct bcm2708_desc *desc;
  9457. +
  9458. + void __iomem *chan_base;
  9459. + int irq_number;
  9460. +};
  9461. +
  9462. +struct bcm2708_desc {
  9463. + struct virt_dma_desc vd;
  9464. + enum dma_transfer_direction dir;
  9465. +
  9466. + unsigned int control_block_size;
  9467. + struct bcm2708_dma_cb *control_block_base;
  9468. + dma_addr_t control_block_base_phys;
  9469. +
  9470. + unsigned frames;
  9471. + size_t size;
  9472. +};
  9473. +
  9474. +#define BCM2708_DMA_DATA_TYPE_S8 1
  9475. +#define BCM2708_DMA_DATA_TYPE_S16 2
  9476. +#define BCM2708_DMA_DATA_TYPE_S32 4
  9477. +#define BCM2708_DMA_DATA_TYPE_S128 16
  9478. +
  9479. +static inline struct bcm2708_dmadev *to_bcm2708_dma_dev(struct dma_device *d)
  9480. +{
  9481. + return container_of(d, struct bcm2708_dmadev, ddev);
  9482. +}
  9483. +
  9484. +static inline struct bcm2708_chan *to_bcm2708_dma_chan(struct dma_chan *c)
  9485. +{
  9486. + return container_of(c, struct bcm2708_chan, vc.chan);
  9487. +}
  9488. +
  9489. +static inline struct bcm2708_desc *to_bcm2708_dma_desc(
  9490. + struct dma_async_tx_descriptor *t)
  9491. +{
  9492. + return container_of(t, struct bcm2708_desc, vd.tx);
  9493. +}
  9494. +
  9495. +static void bcm2708_dma_desc_free(struct virt_dma_desc *vd)
  9496. +{
  9497. + struct bcm2708_desc *desc = container_of(vd, struct bcm2708_desc, vd);
  9498. + dma_free_coherent(desc->vd.tx.chan->device->dev,
  9499. + desc->control_block_size,
  9500. + desc->control_block_base,
  9501. + desc->control_block_base_phys);
  9502. + kfree(desc);
  9503. +}
  9504. +
  9505. +static void bcm2708_dma_start_desc(struct bcm2708_chan *c)
  9506. +{
  9507. + struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  9508. + struct bcm2708_desc *d;
  9509. +
  9510. + if (!vd) {
  9511. + c->desc = NULL;
  9512. + return;
  9513. + }
  9514. +
  9515. + list_del(&vd->node);
  9516. +
  9517. + c->desc = d = to_bcm2708_dma_desc(&vd->tx);
  9518. +
  9519. + bcm_dma_start(c->chan_base, d->control_block_base_phys);
  9520. +}
  9521. +
  9522. +static irqreturn_t bcm2708_dma_callback(int irq, void *data)
  9523. +{
  9524. + struct bcm2708_chan *c = data;
  9525. + struct bcm2708_desc *d;
  9526. + unsigned long flags;
  9527. +
  9528. + spin_lock_irqsave(&c->vc.lock, flags);
  9529. +
  9530. + /* Acknowledge interrupt */
  9531. + writel(BCM2708_DMA_INT, c->chan_base + BCM2708_DMA_CS);
  9532. +
  9533. + d = c->desc;
  9534. +
  9535. + if (d) {
  9536. + /* TODO Only works for cyclic DMA */
  9537. + vchan_cyclic_callback(&d->vd);
  9538. + }
  9539. +
  9540. + /* Keep the DMA engine running */
  9541. + dsb(); /* ARM synchronization barrier */
  9542. + writel(BCM2708_DMA_ACTIVE, c->chan_base + BCM2708_DMA_CS);
  9543. +
  9544. + spin_unlock_irqrestore(&c->vc.lock, flags);
  9545. +
  9546. + return IRQ_HANDLED;
  9547. +}
  9548. +
  9549. +static int bcm2708_dma_alloc_chan_resources(struct dma_chan *chan)
  9550. +{
  9551. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  9552. +
  9553. + return request_irq(c->irq_number,
  9554. + bcm2708_dma_callback, 0, "DMA IRQ", c);
  9555. +}
  9556. +
  9557. +static void bcm2708_dma_free_chan_resources(struct dma_chan *chan)
  9558. +{
  9559. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  9560. +
  9561. + vchan_free_chan_resources(&c->vc);
  9562. + free_irq(c->irq_number, c);
  9563. +
  9564. + dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  9565. +}
  9566. +
  9567. +static size_t bcm2708_dma_desc_size(struct bcm2708_desc *d)
  9568. +{
  9569. + return d->size;
  9570. +}
  9571. +
  9572. +static size_t bcm2708_dma_desc_size_pos(struct bcm2708_desc *d, dma_addr_t addr)
  9573. +{
  9574. + unsigned i;
  9575. + size_t size;
  9576. +
  9577. + for (size = i = 0; i < d->frames; i++) {
  9578. + struct bcm2708_dma_cb *control_block =
  9579. + &d->control_block_base[i];
  9580. + size_t this_size = control_block->length;
  9581. + dma_addr_t dma;
  9582. +
  9583. + if (d->dir == DMA_DEV_TO_MEM)
  9584. + dma = control_block->dst;
  9585. + else
  9586. + dma = control_block->src;
  9587. +
  9588. + if (size)
  9589. + size += this_size;
  9590. + else if (addr >= dma && addr < dma + this_size)
  9591. + size += dma + this_size - addr;
  9592. + }
  9593. +
  9594. + return size;
  9595. +}
  9596. +
  9597. +static enum dma_status bcm2708_dma_tx_status(struct dma_chan *chan,
  9598. + dma_cookie_t cookie, struct dma_tx_state *txstate)
  9599. +{
  9600. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  9601. + struct virt_dma_desc *vd;
  9602. + enum dma_status ret;
  9603. + unsigned long flags;
  9604. +
  9605. + ret = dma_cookie_status(chan, cookie, txstate);
  9606. + if (ret == DMA_COMPLETE || !txstate)
  9607. + return ret;
  9608. +
  9609. + spin_lock_irqsave(&c->vc.lock, flags);
  9610. + vd = vchan_find_desc(&c->vc, cookie);
  9611. + if (vd) {
  9612. + txstate->residue =
  9613. + bcm2708_dma_desc_size(to_bcm2708_dma_desc(&vd->tx));
  9614. + } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  9615. + struct bcm2708_desc *d = c->desc;
  9616. + dma_addr_t pos;
  9617. +
  9618. + if (d->dir == DMA_MEM_TO_DEV)
  9619. + pos = readl(c->chan_base + BCM2708_DMA_SOURCE_AD);
  9620. + else if (d->dir == DMA_DEV_TO_MEM)
  9621. + pos = readl(c->chan_base + BCM2708_DMA_DEST_AD);
  9622. + else
  9623. + pos = 0;
  9624. +
  9625. + txstate->residue = bcm2708_dma_desc_size_pos(d, pos);
  9626. + } else {
  9627. + txstate->residue = 0;
  9628. + }
  9629. +
  9630. + spin_unlock_irqrestore(&c->vc.lock, flags);
  9631. +
  9632. + return ret;
  9633. +}
  9634. +
  9635. +static void bcm2708_dma_issue_pending(struct dma_chan *chan)
  9636. +{
  9637. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  9638. + unsigned long flags;
  9639. +
  9640. + c->cyclic = true; /* Nothing else is implemented */
  9641. +
  9642. + spin_lock_irqsave(&c->vc.lock, flags);
  9643. + if (vchan_issue_pending(&c->vc) && !c->desc)
  9644. + bcm2708_dma_start_desc(c);
  9645. +
  9646. + spin_unlock_irqrestore(&c->vc.lock, flags);
  9647. +}
  9648. +
  9649. +static struct dma_async_tx_descriptor *bcm2708_dma_prep_dma_cyclic(
  9650. + struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  9651. + size_t period_len, enum dma_transfer_direction direction,
  9652. + unsigned long flags, void *context)
  9653. +{
  9654. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  9655. + enum dma_slave_buswidth dev_width;
  9656. + struct bcm2708_desc *d;
  9657. + dma_addr_t dev_addr;
  9658. + unsigned es, sync_type;
  9659. + unsigned frame;
  9660. +
  9661. + /* Grab configuration */
  9662. + if (direction == DMA_DEV_TO_MEM) {
  9663. + dev_addr = c->cfg.src_addr;
  9664. + dev_width = c->cfg.src_addr_width;
  9665. + sync_type = BCM2708_DMA_S_DREQ;
  9666. + } else if (direction == DMA_MEM_TO_DEV) {
  9667. + dev_addr = c->cfg.dst_addr;
  9668. + dev_width = c->cfg.dst_addr_width;
  9669. + sync_type = BCM2708_DMA_D_DREQ;
  9670. + } else {
  9671. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  9672. + return NULL;
  9673. + }
  9674. +
  9675. + /* Bus width translates to the element size (ES) */
  9676. + switch (dev_width) {
  9677. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  9678. + es = BCM2708_DMA_DATA_TYPE_S32;
  9679. + break;
  9680. + default:
  9681. + return NULL;
  9682. + }
  9683. +
  9684. + /* Now allocate and setup the descriptor. */
  9685. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  9686. + if (!d)
  9687. + return NULL;
  9688. +
  9689. + d->dir = direction;
  9690. + d->frames = buf_len / period_len;
  9691. +
  9692. + /* Allocate memory for control blocks */
  9693. + d->control_block_size = d->frames * sizeof(struct bcm2708_dma_cb);
  9694. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  9695. + d->control_block_size, &d->control_block_base_phys,
  9696. + GFP_NOWAIT);
  9697. +
  9698. + if (!d->control_block_base) {
  9699. + kfree(d);
  9700. + return NULL;
  9701. + }
  9702. +
  9703. + /*
  9704. + * Iterate over all frames, create a control block
  9705. + * for each frame and link them together.
  9706. + */
  9707. + for (frame = 0; frame < d->frames; frame++) {
  9708. + struct bcm2708_dma_cb *control_block =
  9709. + &d->control_block_base[frame];
  9710. +
  9711. + /* Setup adresses */
  9712. + if (d->dir == DMA_DEV_TO_MEM) {
  9713. + control_block->info = BCM2708_DMA_D_INC;
  9714. + control_block->src = dev_addr;
  9715. + control_block->dst = buf_addr + frame * period_len;
  9716. + } else {
  9717. + control_block->info = BCM2708_DMA_S_INC;
  9718. + control_block->src = buf_addr + frame * period_len;
  9719. + control_block->dst = dev_addr;
  9720. + }
  9721. +
  9722. + /* Enable interrupt */
  9723. + control_block->info |= BCM2708_DMA_INT_EN;
  9724. +
  9725. + /* Setup synchronization */
  9726. + if (sync_type != 0)
  9727. + control_block->info |= sync_type;
  9728. +
  9729. + /* Setup DREQ channel */
  9730. + if (c->cfg.slave_id != 0)
  9731. + control_block->info |=
  9732. + BCM2708_DMA_PER_MAP(c->cfg.slave_id);
  9733. +
  9734. + /* Length of a frame */
  9735. + control_block->length = period_len;
  9736. + d->size += control_block->length;
  9737. +
  9738. + /*
  9739. + * Next block is the next frame.
  9740. + * This DMA engine driver currently only supports cyclic DMA.
  9741. + * Therefore, wrap around at number of frames.
  9742. + */
  9743. + control_block->next = d->control_block_base_phys +
  9744. + sizeof(struct bcm2708_dma_cb)
  9745. + * ((frame + 1) % d->frames);
  9746. + }
  9747. +
  9748. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  9749. +}
  9750. +
  9751. +static int bcm2708_dma_slave_config(struct bcm2708_chan *c,
  9752. + struct dma_slave_config *cfg)
  9753. +{
  9754. + if ((cfg->direction == DMA_DEV_TO_MEM &&
  9755. + cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  9756. + (cfg->direction == DMA_MEM_TO_DEV &&
  9757. + cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  9758. + !is_slave_direction(cfg->direction)) {
  9759. + return -EINVAL;
  9760. + }
  9761. +
  9762. + c->cfg = *cfg;
  9763. +
  9764. + return 0;
  9765. +}
  9766. +
  9767. +static int bcm2708_dma_terminate_all(struct bcm2708_chan *c)
  9768. +{
  9769. + struct bcm2708_dmadev *d = to_bcm2708_dma_dev(c->vc.chan.device);
  9770. + unsigned long flags;
  9771. + int timeout = 10000;
  9772. + LIST_HEAD(head);
  9773. +
  9774. + spin_lock_irqsave(&c->vc.lock, flags);
  9775. +
  9776. + /* Prevent this channel being scheduled */
  9777. + spin_lock(&d->lock);
  9778. + list_del_init(&c->node);
  9779. + spin_unlock(&d->lock);
  9780. +
  9781. + /*
  9782. + * Stop DMA activity: we assume the callback will not be called
  9783. + * after bcm_dma_abort() returns (even if it does, it will see
  9784. + * c->desc is NULL and exit.)
  9785. + */
  9786. + if (c->desc) {
  9787. + c->desc = NULL;
  9788. + bcm_dma_abort(c->chan_base);
  9789. +
  9790. + /* Wait for stopping */
  9791. + while (timeout > 0) {
  9792. + timeout--;
  9793. + if (!(readl(c->chan_base + BCM2708_DMA_CS) &
  9794. + BCM2708_DMA_ACTIVE))
  9795. + break;
  9796. +
  9797. + cpu_relax();
  9798. + }
  9799. +
  9800. + if (timeout <= 0)
  9801. + dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  9802. + }
  9803. +
  9804. + vchan_get_all_descriptors(&c->vc, &head);
  9805. + spin_unlock_irqrestore(&c->vc.lock, flags);
  9806. + vchan_dma_desc_free_list(&c->vc, &head);
  9807. +
  9808. + return 0;
  9809. +}
  9810. +
  9811. +static int bcm2708_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  9812. + unsigned long arg)
  9813. +{
  9814. + struct bcm2708_chan *c = to_bcm2708_dma_chan(chan);
  9815. +
  9816. + switch (cmd) {
  9817. + case DMA_SLAVE_CONFIG:
  9818. + return bcm2708_dma_slave_config(c,
  9819. + (struct dma_slave_config *)arg);
  9820. +
  9821. + case DMA_TERMINATE_ALL:
  9822. + return bcm2708_dma_terminate_all(c);
  9823. +
  9824. + default:
  9825. + return -ENXIO;
  9826. + }
  9827. +}
  9828. +
  9829. +static int bcm2708_dma_chan_init(struct bcm2708_dmadev *d, void __iomem* chan_base,
  9830. + int chan_id, int irq)
  9831. +{
  9832. + struct bcm2708_chan *c;
  9833. +
  9834. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  9835. + if (!c)
  9836. + return -ENOMEM;
  9837. +
  9838. + c->vc.desc_free = bcm2708_dma_desc_free;
  9839. + vchan_init(&c->vc, &d->ddev);
  9840. + INIT_LIST_HEAD(&c->node);
  9841. +
  9842. + d->ddev.chancnt++;
  9843. +
  9844. + c->chan_base = chan_base;
  9845. + c->ch = chan_id;
  9846. + c->irq_number = irq;
  9847. +
  9848. + return 0;
  9849. +}
  9850. +
  9851. +static void bcm2708_dma_free(struct bcm2708_dmadev *od)
  9852. +{
  9853. + while (!list_empty(&od->ddev.channels)) {
  9854. + struct bcm2708_chan *c = list_first_entry(&od->ddev.channels,
  9855. + struct bcm2708_chan, vc.chan.device_node);
  9856. +
  9857. + list_del(&c->vc.chan.device_node);
  9858. + tasklet_kill(&c->vc.task);
  9859. + }
  9860. +}
  9861. +
  9862. +static int bcm2708_dma_probe(struct platform_device *pdev)
  9863. +{
  9864. + struct bcm2708_dmadev *od;
  9865. + int rc, i;
  9866. +
  9867. + if (!pdev->dev.dma_mask)
  9868. + pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  9869. +
  9870. + rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  9871. + if (rc)
  9872. + return rc;
  9873. + dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  9874. +
  9875. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  9876. + if (!od)
  9877. + return -ENOMEM;
  9878. +
  9879. + pdev->dev.dma_parms = &od->dma_parms;
  9880. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  9881. +
  9882. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  9883. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  9884. + od->ddev.device_alloc_chan_resources = bcm2708_dma_alloc_chan_resources;
  9885. + od->ddev.device_free_chan_resources = bcm2708_dma_free_chan_resources;
  9886. + od->ddev.device_tx_status = bcm2708_dma_tx_status;
  9887. + od->ddev.device_issue_pending = bcm2708_dma_issue_pending;
  9888. + od->ddev.device_prep_dma_cyclic = bcm2708_dma_prep_dma_cyclic;
  9889. + od->ddev.device_control = bcm2708_dma_control;
  9890. + od->ddev.dev = &pdev->dev;
  9891. + INIT_LIST_HEAD(&od->ddev.channels);
  9892. + spin_lock_init(&od->lock);
  9893. +
  9894. + platform_set_drvdata(pdev, od);
  9895. +
  9896. + for (i = 0; i < 16; i++) {
  9897. + void __iomem* chan_base;
  9898. + int chan_id, irq;
  9899. +
  9900. + chan_id = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
  9901. + &chan_base,
  9902. + &irq);
  9903. +
  9904. + if (chan_id < 0)
  9905. + break;
  9906. +
  9907. + rc = bcm2708_dma_chan_init(od, chan_base, chan_id, irq);
  9908. + if (rc) {
  9909. + bcm2708_dma_free(od);
  9910. + return rc;
  9911. + }
  9912. + }
  9913. +
  9914. + rc = dma_async_device_register(&od->ddev);
  9915. + if (rc) {
  9916. + dev_err(&pdev->dev,
  9917. + "Failed to register slave DMA engine device: %d\n", rc);
  9918. + bcm2708_dma_free(od);
  9919. + return rc;
  9920. + }
  9921. +
  9922. + dev_dbg(&pdev->dev, "Load BCM2708 DMA engine driver\n");
  9923. +
  9924. + return rc;
  9925. +}
  9926. +
  9927. +static int bcm2708_dma_remove(struct platform_device *pdev)
  9928. +{
  9929. + struct bcm2708_dmadev *od = platform_get_drvdata(pdev);
  9930. +
  9931. + dma_async_device_unregister(&od->ddev);
  9932. + bcm2708_dma_free(od);
  9933. +
  9934. + return 0;
  9935. +}
  9936. +
  9937. +static struct platform_driver bcm2708_dma_driver = {
  9938. + .probe = bcm2708_dma_probe,
  9939. + .remove = bcm2708_dma_remove,
  9940. + .driver = {
  9941. + .name = "bcm2708-dmaengine",
  9942. + .owner = THIS_MODULE,
  9943. + },
  9944. +};
  9945. +
  9946. +static struct platform_device *pdev;
  9947. +
  9948. +static const struct platform_device_info bcm2708_dma_dev_info = {
  9949. + .name = "bcm2708-dmaengine",
  9950. + .id = -1,
  9951. +};
  9952. +
  9953. +static int bcm2708_dma_init(void)
  9954. +{
  9955. + int rc = platform_driver_register(&bcm2708_dma_driver);
  9956. +
  9957. + if (rc == 0) {
  9958. + pdev = platform_device_register_full(&bcm2708_dma_dev_info);
  9959. + if (IS_ERR(pdev)) {
  9960. + platform_driver_unregister(&bcm2708_dma_driver);
  9961. + rc = PTR_ERR(pdev);
  9962. + }
  9963. + }
  9964. +
  9965. + return rc;
  9966. +}
  9967. +subsys_initcall(bcm2708_dma_init);
  9968. +
  9969. +static void __exit bcm2708_dma_exit(void)
  9970. +{
  9971. + platform_device_unregister(pdev);
  9972. + platform_driver_unregister(&bcm2708_dma_driver);
  9973. +}
  9974. +module_exit(bcm2708_dma_exit);
  9975. +
  9976. +MODULE_ALIAS("platform:bcm2708-dma");
  9977. +MODULE_DESCRIPTION("BCM2708 DMA engine driver");
  9978. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  9979. +MODULE_LICENSE("GPL v2");
  9980. diff -Nur linux-3.13.6/drivers/dma/Kconfig linux-raspberry-pi/drivers/dma/Kconfig
  9981. --- linux-3.13.6/drivers/dma/Kconfig 2014-03-07 07:07:02.000000000 +0100
  9982. +++ linux-raspberry-pi/drivers/dma/Kconfig 2014-03-11 16:54:56.000000000 +0100
  9983. @@ -312,6 +312,12 @@
  9984. The Communications Port Programming Interface (CPPI) 4.1 DMA engine
  9985. is currently used by the USB driver on AM335x platforms.
  9986. +config DMA_BCM2708
  9987. + tristate "BCM2708 DMA engine support"
  9988. + depends on MACH_BCM2708
  9989. + select DMA_ENGINE
  9990. + select DMA_VIRTUAL_CHANNELS
  9991. +
  9992. config MMP_PDMA
  9993. bool "MMP PDMA support"
  9994. depends on (ARCH_MMP || ARCH_PXA)
  9995. diff -Nur linux-3.13.6/drivers/dma/Makefile linux-raspberry-pi/drivers/dma/Makefile
  9996. --- linux-3.13.6/drivers/dma/Makefile 2014-03-07 07:07:02.000000000 +0100
  9997. +++ linux-raspberry-pi/drivers/dma/Makefile 2014-03-11 16:54:56.000000000 +0100
  9998. @@ -38,6 +38,7 @@
  9999. obj-$(CONFIG_DMA_SA11X0) += sa11x0-dma.o
  10000. obj-$(CONFIG_MMP_TDMA) += mmp_tdma.o
  10001. obj-$(CONFIG_DMA_OMAP) += omap-dma.o
  10002. +obj-$(CONFIG_DMA_BCM2708) += bcm2708-dmaengine.o
  10003. obj-$(CONFIG_MMP_PDMA) += mmp_pdma.o
  10004. obj-$(CONFIG_DMA_JZ4740) += dma-jz4740.o
  10005. obj-$(CONFIG_TI_CPPI41) += cppi41.o
  10006. diff -Nur linux-3.13.6/drivers/hwmon/bcm2835-hwmon.c linux-raspberry-pi/drivers/hwmon/bcm2835-hwmon.c
  10007. --- linux-3.13.6/drivers/hwmon/bcm2835-hwmon.c 1970-01-01 01:00:00.000000000 +0100
  10008. +++ linux-raspberry-pi/drivers/hwmon/bcm2835-hwmon.c 2014-03-11 16:52:40.000000000 +0100
  10009. @@ -0,0 +1,219 @@
  10010. +/*****************************************************************************
  10011. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  10012. +*
  10013. +* Unless you and Broadcom execute a separate written software license
  10014. +* agreement governing use of this software, this software is licensed to you
  10015. +* under the terms of the GNU General Public License version 2, available at
  10016. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  10017. +*
  10018. +* Notwithstanding the above, under no circumstances may you combine this
  10019. +* software in any way with any other Broadcom software provided under a
  10020. +* license other than the GPL, without Broadcom's express prior written
  10021. +* consent.
  10022. +*****************************************************************************/
  10023. +
  10024. +#include <linux/kernel.h>
  10025. +#include <linux/module.h>
  10026. +#include <linux/init.h>
  10027. +#include <linux/hwmon.h>
  10028. +#include <linux/hwmon-sysfs.h>
  10029. +#include <linux/platform_device.h>
  10030. +#include <linux/sysfs.h>
  10031. +#include <mach/vcio.h>
  10032. +#include <linux/slab.h>
  10033. +#include <linux/err.h>
  10034. +
  10035. +#define MODULE_NAME "bcm2835_hwmon"
  10036. +
  10037. +/*#define HWMON_DEBUG_ENABLE*/
  10038. +
  10039. +#ifdef HWMON_DEBUG_ENABLE
  10040. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  10041. +#else
  10042. +#define print_debug(fmt,...)
  10043. +#endif
  10044. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  10045. +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
  10046. +
  10047. +#define VC_TAG_GET_TEMP 0x00030006
  10048. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  10049. +
  10050. +/* --- STRUCTS --- */
  10051. +struct bcm2835_hwmon_data {
  10052. + struct device *hwmon_dev;
  10053. +};
  10054. +
  10055. +/* tag part of the message */
  10056. +struct vc_msg_tag {
  10057. + uint32_t tag_id; /* the tag ID for the temperature */
  10058. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  10059. + uint32_t request_code; /* identifies message as a request (should be 0) */
  10060. + uint32_t id; /* extra ID field (should be 0) */
  10061. + uint32_t val; /* returned value of the temperature */
  10062. +};
  10063. +
  10064. +/* message structure to be sent to videocore */
  10065. +struct vc_msg {
  10066. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  10067. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  10068. + struct vc_msg_tag tag; /* the tag structure above to make */
  10069. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  10070. +};
  10071. +
  10072. +typedef enum {
  10073. + TEMP,
  10074. + MAX_TEMP,
  10075. +} temp_type;
  10076. +
  10077. +/* --- PROTOTYPES --- */
  10078. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
  10079. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
  10080. +
  10081. +/* --- GLOBALS --- */
  10082. +
  10083. +static struct bcm2835_hwmon_data *bcm2835_data;
  10084. +static struct platform_driver bcm2835_hwmon_driver;
  10085. +
  10086. +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
  10087. +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
  10088. +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
  10089. +
  10090. +static struct attribute* bcm2835_attributes[] = {
  10091. + &sensor_dev_attr_name.dev_attr.attr,
  10092. + &sensor_dev_attr_temp1_input.dev_attr.attr,
  10093. + &sensor_dev_attr_temp1_max.dev_attr.attr,
  10094. + NULL,
  10095. +};
  10096. +
  10097. +static struct attribute_group bcm2835_attr_group = {
  10098. + .attrs = bcm2835_attributes,
  10099. +};
  10100. +
  10101. +/* --- FUNCTIONS --- */
  10102. +
  10103. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
  10104. +{
  10105. + return sprintf(buf,"bcm2835_hwmon\n");
  10106. +}
  10107. +
  10108. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
  10109. +{
  10110. + struct vc_msg msg;
  10111. + int result;
  10112. + uint temp = 0;
  10113. + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
  10114. +
  10115. + print_debug("IN");
  10116. +
  10117. + /* wipe all previous message data */
  10118. + memset(&msg, 0, sizeof msg);
  10119. +
  10120. + /* determine the message type */
  10121. + if(index == TEMP)
  10122. + msg.tag.tag_id = VC_TAG_GET_TEMP;
  10123. + else if (index == MAX_TEMP)
  10124. + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
  10125. + else
  10126. + {
  10127. + print_debug("Unknown temperature message!");
  10128. + return -EINVAL;
  10129. + }
  10130. +
  10131. + msg.msg_size = sizeof msg;
  10132. + msg.tag.buffer_size = 8;
  10133. +
  10134. + /* send the message */
  10135. + result = bcm_mailbox_property(&msg, sizeof msg);
  10136. +
  10137. + /* check if it was all ok and return the rate in milli degrees C */
  10138. + if (result == 0 && (msg.request_code & 0x80000000))
  10139. + temp = (uint)msg.tag.val;
  10140. + #ifdef HWMON_DEBUG_ENABLE
  10141. + else
  10142. + print_debug("Failed to get temperature!");
  10143. + #endif
  10144. + print_debug("Got temperature as %u",temp);
  10145. + print_debug("OUT");
  10146. + return sprintf(buf, "%u\n", temp);
  10147. +}
  10148. +
  10149. +
  10150. +static int bcm2835_hwmon_probe(struct platform_device *pdev)
  10151. +{
  10152. + int err;
  10153. +
  10154. + print_debug("IN");
  10155. + print_debug("HWMON Driver has been probed!");
  10156. +
  10157. + /* check that the device isn't null!*/
  10158. + if(pdev == NULL)
  10159. + {
  10160. + print_debug("Platform device is empty!");
  10161. + return -ENODEV;
  10162. + }
  10163. +
  10164. + /* allocate memory for neccessary data */
  10165. + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
  10166. + if(!bcm2835_data)
  10167. + {
  10168. + print_debug("Unable to allocate memory for hwmon data!");
  10169. + err = -ENOMEM;
  10170. + goto kzalloc_error;
  10171. + }
  10172. +
  10173. + /* create the sysfs files */
  10174. + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
  10175. + {
  10176. + print_debug("Unable to create sysfs files!");
  10177. + err = -EFAULT;
  10178. + goto sysfs_error;
  10179. + }
  10180. +
  10181. + /* register the hwmon device */
  10182. + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
  10183. + if (IS_ERR(bcm2835_data->hwmon_dev))
  10184. + {
  10185. + err = PTR_ERR(bcm2835_data->hwmon_dev);
  10186. + goto hwmon_error;
  10187. + }
  10188. + print_debug("OUT");
  10189. + return 0;
  10190. +
  10191. + /* error goto's */
  10192. + hwmon_error:
  10193. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  10194. +
  10195. + sysfs_error:
  10196. + kfree(bcm2835_data);
  10197. +
  10198. + kzalloc_error:
  10199. +
  10200. + return err;
  10201. +
  10202. +}
  10203. +
  10204. +static int bcm2835_hwmon_remove(struct platform_device *pdev)
  10205. +{
  10206. + print_debug("IN");
  10207. + hwmon_device_unregister(bcm2835_data->hwmon_dev);
  10208. +
  10209. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  10210. + print_debug("OUT");
  10211. + return 0;
  10212. +}
  10213. +
  10214. +/* Hwmon Driver */
  10215. +static struct platform_driver bcm2835_hwmon_driver = {
  10216. + .probe = bcm2835_hwmon_probe,
  10217. + .remove = bcm2835_hwmon_remove,
  10218. + .driver = {
  10219. + .name = "bcm2835_hwmon",
  10220. + .owner = THIS_MODULE,
  10221. + },
  10222. +};
  10223. +
  10224. +MODULE_LICENSE("GPL");
  10225. +MODULE_AUTHOR("Dorian Peake");
  10226. +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
  10227. +
  10228. +module_platform_driver(bcm2835_hwmon_driver);
  10229. diff -Nur linux-3.13.6/drivers/hwmon/Kconfig linux-raspberry-pi/drivers/hwmon/Kconfig
  10230. --- linux-3.13.6/drivers/hwmon/Kconfig 2014-03-07 07:07:02.000000000 +0100
  10231. +++ linux-raspberry-pi/drivers/hwmon/Kconfig 2014-03-11 16:54:57.000000000 +0100
  10232. @@ -1554,6 +1554,16 @@
  10233. help
  10234. Support for the A/D converter on MC13783 and MC13892 PMIC.
  10235. +config SENSORS_BCM2835
  10236. + depends on THERMAL_BCM2835=n
  10237. + tristate "Broadcom BCM2835 HWMON Driver"
  10238. + help
  10239. + If you say yes here you get support for the hardware
  10240. + monitoring features of the BCM2835 Chip
  10241. +
  10242. + This driver can also be built as a module. If so, the module
  10243. + will be called bcm2835-hwmon.
  10244. +
  10245. if ACPI
  10246. comment "ACPI drivers"
  10247. diff -Nur linux-3.13.6/drivers/hwmon/Makefile linux-raspberry-pi/drivers/hwmon/Makefile
  10248. --- linux-3.13.6/drivers/hwmon/Makefile 2014-03-07 07:07:02.000000000 +0100
  10249. +++ linux-raspberry-pi/drivers/hwmon/Makefile 2014-03-11 16:54:57.000000000 +0100
  10250. @@ -142,6 +142,7 @@
  10251. obj-$(CONFIG_SENSORS_W83L786NG) += w83l786ng.o
  10252. obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
  10253. obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
  10254. +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
  10255. obj-$(CONFIG_PMBUS) += pmbus/
  10256. diff -Nur linux-3.13.6/drivers/i2c/busses/i2c-bcm2708.c linux-raspberry-pi/drivers/i2c/busses/i2c-bcm2708.c
  10257. --- linux-3.13.6/drivers/i2c/busses/i2c-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  10258. +++ linux-raspberry-pi/drivers/i2c/busses/i2c-bcm2708.c 2014-03-11 16:54:57.000000000 +0100
  10259. @@ -0,0 +1,408 @@
  10260. +/*
  10261. + * Driver for Broadcom BCM2708 BSC Controllers
  10262. + *
  10263. + * Copyright (C) 2012 Chris Boot & Frank Buss
  10264. + *
  10265. + * This driver is inspired by:
  10266. + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
  10267. + *
  10268. + * This program is free software; you can redistribute it and/or modify
  10269. + * it under the terms of the GNU General Public License as published by
  10270. + * the Free Software Foundation; either version 2 of the License, or
  10271. + * (at your option) any later version.
  10272. + *
  10273. + * This program is distributed in the hope that it will be useful,
  10274. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10275. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10276. + * GNU General Public License for more details.
  10277. + *
  10278. + * You should have received a copy of the GNU General Public License
  10279. + * along with this program; if not, write to the Free Software
  10280. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  10281. + */
  10282. +
  10283. +#include <linux/kernel.h>
  10284. +#include <linux/module.h>
  10285. +#include <linux/spinlock.h>
  10286. +#include <linux/clk.h>
  10287. +#include <linux/err.h>
  10288. +#include <linux/platform_device.h>
  10289. +#include <linux/io.h>
  10290. +#include <linux/slab.h>
  10291. +#include <linux/i2c.h>
  10292. +#include <linux/interrupt.h>
  10293. +#include <linux/sched.h>
  10294. +#include <linux/wait.h>
  10295. +
  10296. +/* BSC register offsets */
  10297. +#define BSC_C 0x00
  10298. +#define BSC_S 0x04
  10299. +#define BSC_DLEN 0x08
  10300. +#define BSC_A 0x0c
  10301. +#define BSC_FIFO 0x10
  10302. +#define BSC_DIV 0x14
  10303. +#define BSC_DEL 0x18
  10304. +#define BSC_CLKT 0x1c
  10305. +
  10306. +/* Bitfields in BSC_C */
  10307. +#define BSC_C_I2CEN 0x00008000
  10308. +#define BSC_C_INTR 0x00000400
  10309. +#define BSC_C_INTT 0x00000200
  10310. +#define BSC_C_INTD 0x00000100
  10311. +#define BSC_C_ST 0x00000080
  10312. +#define BSC_C_CLEAR_1 0x00000020
  10313. +#define BSC_C_CLEAR_2 0x00000010
  10314. +#define BSC_C_READ 0x00000001
  10315. +
  10316. +/* Bitfields in BSC_S */
  10317. +#define BSC_S_CLKT 0x00000200
  10318. +#define BSC_S_ERR 0x00000100
  10319. +#define BSC_S_RXF 0x00000080
  10320. +#define BSC_S_TXE 0x00000040
  10321. +#define BSC_S_RXD 0x00000020
  10322. +#define BSC_S_TXD 0x00000010
  10323. +#define BSC_S_RXR 0x00000008
  10324. +#define BSC_S_TXW 0x00000004
  10325. +#define BSC_S_DONE 0x00000002
  10326. +#define BSC_S_TA 0x00000001
  10327. +
  10328. +#define I2C_TIMEOUT_MS 150
  10329. +
  10330. +#define DRV_NAME "bcm2708_i2c"
  10331. +
  10332. +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
  10333. +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  10334. +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
  10335. +
  10336. +
  10337. +struct bcm2708_i2c {
  10338. + struct i2c_adapter adapter;
  10339. +
  10340. + spinlock_t lock;
  10341. + void __iomem *base;
  10342. + int irq;
  10343. + struct clk *clk;
  10344. +
  10345. + struct completion done;
  10346. +
  10347. + struct i2c_msg *msg;
  10348. + int pos;
  10349. + int nmsgs;
  10350. + bool error;
  10351. +};
  10352. +
  10353. +/*
  10354. + * This function sets the ALT mode on the I2C pins so that we can use them with
  10355. + * the BSC hardware.
  10356. + *
  10357. + * FIXME: This is a hack. Use pinmux / pinctrl.
  10358. + */
  10359. +static void bcm2708_i2c_init_pinmode(int id)
  10360. +{
  10361. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  10362. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  10363. +
  10364. + int pin;
  10365. + u32 *gpio = ioremap(0x20200000, SZ_16K);
  10366. +
  10367. + BUG_ON(id != 0 && id != 1);
  10368. + /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
  10369. + for (pin = id*2+0; pin <= id*2+1; pin++) {
  10370. +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
  10371. + INP_GPIO(pin); /* set mode to GPIO input first */
  10372. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  10373. + }
  10374. +
  10375. + iounmap(gpio);
  10376. +
  10377. +#undef INP_GPIO
  10378. +#undef SET_GPIO_ALT
  10379. +}
  10380. +
  10381. +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
  10382. +{
  10383. + return readl(bi->base + reg);
  10384. +}
  10385. +
  10386. +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
  10387. +{
  10388. + writel(val, bi->base + reg);
  10389. +}
  10390. +
  10391. +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
  10392. +{
  10393. + bcm2708_wr(bi, BSC_C, 0);
  10394. + bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
  10395. +}
  10396. +
  10397. +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
  10398. +{
  10399. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
  10400. + bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
  10401. +}
  10402. +
  10403. +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
  10404. +{
  10405. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
  10406. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  10407. +}
  10408. +
  10409. +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
  10410. +{
  10411. + unsigned long bus_hz;
  10412. + u32 cdiv;
  10413. + u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
  10414. +
  10415. + bus_hz = clk_get_rate(bi->clk);
  10416. + cdiv = bus_hz / baudrate;
  10417. +
  10418. + if (bi->msg->flags & I2C_M_RD)
  10419. + c |= BSC_C_INTR | BSC_C_READ;
  10420. + else
  10421. + c |= BSC_C_INTT;
  10422. +
  10423. + bcm2708_wr(bi, BSC_DIV, cdiv);
  10424. + bcm2708_wr(bi, BSC_A, bi->msg->addr);
  10425. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  10426. + bcm2708_wr(bi, BSC_C, c);
  10427. +}
  10428. +
  10429. +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
  10430. +{
  10431. + struct bcm2708_i2c *bi = dev_id;
  10432. + bool handled = true;
  10433. + u32 s;
  10434. +
  10435. + spin_lock(&bi->lock);
  10436. +
  10437. + /* we may see camera interrupts on the "other" I2C channel
  10438. + Just return if we've not sent anything */
  10439. + if (!bi->nmsgs || !bi->msg )
  10440. + goto early_exit;
  10441. +
  10442. + s = bcm2708_rd(bi, BSC_S);
  10443. +
  10444. + if (s & (BSC_S_CLKT | BSC_S_ERR)) {
  10445. + bcm2708_bsc_reset(bi);
  10446. + bi->error = true;
  10447. +
  10448. + /* wake up our bh */
  10449. + complete(&bi->done);
  10450. + } else if (s & BSC_S_DONE) {
  10451. + bi->nmsgs--;
  10452. +
  10453. + if (bi->msg->flags & I2C_M_RD)
  10454. + bcm2708_bsc_fifo_drain(bi);
  10455. +
  10456. + bcm2708_bsc_reset(bi);
  10457. +
  10458. + if (bi->nmsgs) {
  10459. + /* advance to next message */
  10460. + bi->msg++;
  10461. + bi->pos = 0;
  10462. + bcm2708_bsc_setup(bi);
  10463. + } else {
  10464. + /* wake up our bh */
  10465. + complete(&bi->done);
  10466. + }
  10467. + } else if (s & BSC_S_TXW) {
  10468. + bcm2708_bsc_fifo_fill(bi);
  10469. + } else if (s & BSC_S_RXR) {
  10470. + bcm2708_bsc_fifo_drain(bi);
  10471. + } else {
  10472. + handled = false;
  10473. + }
  10474. +
  10475. +early_exit:
  10476. + spin_unlock(&bi->lock);
  10477. +
  10478. + return handled ? IRQ_HANDLED : IRQ_NONE;
  10479. +}
  10480. +
  10481. +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
  10482. + struct i2c_msg *msgs, int num)
  10483. +{
  10484. + struct bcm2708_i2c *bi = adap->algo_data;
  10485. + unsigned long flags;
  10486. + int ret;
  10487. +
  10488. + spin_lock_irqsave(&bi->lock, flags);
  10489. +
  10490. + reinit_completion(&bi->done);
  10491. + bi->msg = msgs;
  10492. + bi->pos = 0;
  10493. + bi->nmsgs = num;
  10494. + bi->error = false;
  10495. +
  10496. + spin_unlock_irqrestore(&bi->lock, flags);
  10497. +
  10498. + bcm2708_bsc_setup(bi);
  10499. +
  10500. + ret = wait_for_completion_timeout(&bi->done,
  10501. + msecs_to_jiffies(I2C_TIMEOUT_MS));
  10502. + if (ret == 0) {
  10503. + dev_err(&adap->dev, "transfer timed out\n");
  10504. + spin_lock_irqsave(&bi->lock, flags);
  10505. + bcm2708_bsc_reset(bi);
  10506. + spin_unlock_irqrestore(&bi->lock, flags);
  10507. + return -ETIMEDOUT;
  10508. + }
  10509. +
  10510. + return bi->error ? -EIO : num;
  10511. +}
  10512. +
  10513. +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
  10514. +{
  10515. + return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
  10516. +}
  10517. +
  10518. +static struct i2c_algorithm bcm2708_i2c_algorithm = {
  10519. + .master_xfer = bcm2708_i2c_master_xfer,
  10520. + .functionality = bcm2708_i2c_functionality,
  10521. +};
  10522. +
  10523. +static int bcm2708_i2c_probe(struct platform_device *pdev)
  10524. +{
  10525. + struct resource *regs;
  10526. + int irq, err = -ENOMEM;
  10527. + struct clk *clk;
  10528. + struct bcm2708_i2c *bi;
  10529. + struct i2c_adapter *adap;
  10530. +
  10531. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  10532. + if (!regs) {
  10533. + dev_err(&pdev->dev, "could not get IO memory\n");
  10534. + return -ENXIO;
  10535. + }
  10536. +
  10537. + irq = platform_get_irq(pdev, 0);
  10538. + if (irq < 0) {
  10539. + dev_err(&pdev->dev, "could not get IRQ\n");
  10540. + return irq;
  10541. + }
  10542. +
  10543. + clk = clk_get(&pdev->dev, NULL);
  10544. + if (IS_ERR(clk)) {
  10545. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  10546. + return PTR_ERR(clk);
  10547. + }
  10548. +
  10549. + bcm2708_i2c_init_pinmode(pdev->id);
  10550. +
  10551. + bi = kzalloc(sizeof(*bi), GFP_KERNEL);
  10552. + if (!bi)
  10553. + goto out_clk_put;
  10554. +
  10555. + platform_set_drvdata(pdev, bi);
  10556. +
  10557. + adap = &bi->adapter;
  10558. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
  10559. + adap->algo = &bcm2708_i2c_algorithm;
  10560. + adap->algo_data = bi;
  10561. + adap->dev.parent = &pdev->dev;
  10562. + adap->nr = pdev->id;
  10563. + strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  10564. +
  10565. + switch (pdev->id) {
  10566. + case 0:
  10567. + adap->class = I2C_CLASS_HWMON;
  10568. + break;
  10569. + case 1:
  10570. + adap->class = I2C_CLASS_DDC;
  10571. + break;
  10572. + default:
  10573. + dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
  10574. + err = -ENXIO;
  10575. + goto out_free_bi;
  10576. + }
  10577. +
  10578. + spin_lock_init(&bi->lock);
  10579. + init_completion(&bi->done);
  10580. +
  10581. + bi->base = ioremap(regs->start, resource_size(regs));
  10582. + if (!bi->base) {
  10583. + dev_err(&pdev->dev, "could not remap memory\n");
  10584. + goto out_free_bi;
  10585. + }
  10586. +
  10587. + bi->irq = irq;
  10588. + bi->clk = clk;
  10589. +
  10590. + err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
  10591. + dev_name(&pdev->dev), bi);
  10592. + if (err) {
  10593. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  10594. + goto out_iounmap;
  10595. + }
  10596. +
  10597. + bcm2708_bsc_reset(bi);
  10598. +
  10599. + err = i2c_add_numbered_adapter(adap);
  10600. + if (err < 0) {
  10601. + dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
  10602. + goto out_free_irq;
  10603. + }
  10604. +
  10605. + dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %dk)\n",
  10606. + pdev->id, (unsigned long)regs->start, irq, baudrate/1000);
  10607. +
  10608. + return 0;
  10609. +
  10610. +out_free_irq:
  10611. + free_irq(bi->irq, bi);
  10612. +out_iounmap:
  10613. + iounmap(bi->base);
  10614. +out_free_bi:
  10615. + kfree(bi);
  10616. +out_clk_put:
  10617. + clk_put(clk);
  10618. + return err;
  10619. +}
  10620. +
  10621. +static int bcm2708_i2c_remove(struct platform_device *pdev)
  10622. +{
  10623. + struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
  10624. +
  10625. + platform_set_drvdata(pdev, NULL);
  10626. +
  10627. + i2c_del_adapter(&bi->adapter);
  10628. + free_irq(bi->irq, bi);
  10629. + iounmap(bi->base);
  10630. + clk_disable(bi->clk);
  10631. + clk_put(bi->clk);
  10632. + kfree(bi);
  10633. +
  10634. + return 0;
  10635. +}
  10636. +
  10637. +static struct platform_driver bcm2708_i2c_driver = {
  10638. + .driver = {
  10639. + .name = DRV_NAME,
  10640. + .owner = THIS_MODULE,
  10641. + },
  10642. + .probe = bcm2708_i2c_probe,
  10643. + .remove = bcm2708_i2c_remove,
  10644. +};
  10645. +
  10646. +// module_platform_driver(bcm2708_i2c_driver);
  10647. +
  10648. +
  10649. +static int __init bcm2708_i2c_init(void)
  10650. +{
  10651. + return platform_driver_register(&bcm2708_i2c_driver);
  10652. +}
  10653. +
  10654. +static void __exit bcm2708_i2c_exit(void)
  10655. +{
  10656. + platform_driver_unregister(&bcm2708_i2c_driver);
  10657. +}
  10658. +
  10659. +module_init(bcm2708_i2c_init);
  10660. +module_exit(bcm2708_i2c_exit);
  10661. +
  10662. +
  10663. +
  10664. +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
  10665. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  10666. +MODULE_LICENSE("GPL v2");
  10667. +MODULE_ALIAS("platform:" DRV_NAME);
  10668. diff -Nur linux-3.13.6/drivers/i2c/busses/Kconfig linux-raspberry-pi/drivers/i2c/busses/Kconfig
  10669. --- linux-3.13.6/drivers/i2c/busses/Kconfig 2014-03-07 07:07:02.000000000 +0100
  10670. +++ linux-raspberry-pi/drivers/i2c/busses/Kconfig 2014-03-11 16:54:57.000000000 +0100
  10671. @@ -347,6 +347,25 @@
  10672. This support is also available as a module. If so, the module
  10673. will be called i2c-bcm2835.
  10674. +config I2C_BCM2708
  10675. + tristate "BCM2708 BSC"
  10676. + depends on MACH_BCM2708
  10677. + help
  10678. + Enabling this option will add BSC (Broadcom Serial Controller)
  10679. + support for the BCM2708. BSC is a Broadcom proprietary bus compatible
  10680. + with I2C/TWI/SMBus.
  10681. +
  10682. +config I2C_BCM2708_BAUDRATE
  10683. + prompt "BCM2708 I2C baudrate"
  10684. + depends on I2C_BCM2708
  10685. + int
  10686. + default 100000
  10687. + help
  10688. + Set the I2C baudrate. This will alter the default value. A
  10689. + different baudrate can be set by using a module parameter as well. If
  10690. + no parameter is provided when loading, this is the value that will be
  10691. + used.
  10692. +
  10693. config I2C_BCM_KONA
  10694. tristate "BCM Kona I2C adapter"
  10695. depends on ARCH_BCM_MOBILE
  10696. diff -Nur linux-3.13.6/drivers/i2c/busses/Makefile linux-raspberry-pi/drivers/i2c/busses/Makefile
  10697. --- linux-3.13.6/drivers/i2c/busses/Makefile 2014-03-07 07:07:02.000000000 +0100
  10698. +++ linux-raspberry-pi/drivers/i2c/busses/Makefile 2014-03-11 16:54:57.000000000 +0100
  10699. @@ -32,6 +32,7 @@
  10700. obj-$(CONFIG_I2C_AT91) += i2c-at91.o
  10701. obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
  10702. obj-$(CONFIG_I2C_BCM2835) += i2c-bcm2835.o
  10703. +obj-$(CONFIG_I2C_BCM2708) += i2c-bcm2708.o
  10704. obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
  10705. obj-$(CONFIG_I2C_CBUS_GPIO) += i2c-cbus-gpio.o
  10706. obj-$(CONFIG_I2C_CPM) += i2c-cpm.o
  10707. diff -Nur linux-3.13.6/drivers/media/dvb-core/dvb-usb-ids.h linux-raspberry-pi/drivers/media/dvb-core/dvb-usb-ids.h
  10708. --- linux-3.13.6/drivers/media/dvb-core/dvb-usb-ids.h 2014-03-07 07:07:02.000000000 +0100
  10709. +++ linux-raspberry-pi/drivers/media/dvb-core/dvb-usb-ids.h 2014-03-11 16:54:57.000000000 +0100
  10710. @@ -366,6 +366,7 @@
  10711. #define USB_PID_TERRATEC_DVBS2CI_V2 0x10ac
  10712. #define USB_PID_TECHNISAT_USB2_HDCI_V1 0x0001
  10713. #define USB_PID_TECHNISAT_USB2_HDCI_V2 0x0002
  10714. +#define USB_PID_TECHNISAT_USB2_CABLESTAR_HDCI 0x0003
  10715. #define USB_PID_TECHNISAT_AIRSTAR_TELESTICK_2 0x0004
  10716. #define USB_PID_TECHNISAT_USB2_DVB_S2 0x0500
  10717. #define USB_PID_CPYTO_REDI_PC50A 0xa803
  10718. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/bcm2835-camera.c linux-raspberry-pi/drivers/media/platform/bcm2835/bcm2835-camera.c
  10719. --- linux-3.13.6/drivers/media/platform/bcm2835/bcm2835-camera.c 1970-01-01 01:00:00.000000000 +0100
  10720. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/bcm2835-camera.c 2014-03-11 16:52:42.000000000 +0100
  10721. @@ -0,0 +1,1695 @@
  10722. +/*
  10723. + * Broadcom BM2835 V4L2 driver
  10724. + *
  10725. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  10726. + *
  10727. + * This file is subject to the terms and conditions of the GNU General Public
  10728. + * License. See the file COPYING in the main directory of this archive
  10729. + * for more details.
  10730. + *
  10731. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  10732. + * Dave Stevenson <dsteve@broadcom.com>
  10733. + * Simon Mellor <simellor@broadcom.com>
  10734. + * Luke Diamand <luked@broadcom.com>
  10735. + */
  10736. +
  10737. +#include <linux/errno.h>
  10738. +#include <linux/kernel.h>
  10739. +#include <linux/module.h>
  10740. +#include <linux/slab.h>
  10741. +#include <media/videobuf2-vmalloc.h>
  10742. +#include <media/videobuf2-dma-contig.h>
  10743. +#include <media/v4l2-device.h>
  10744. +#include <media/v4l2-ioctl.h>
  10745. +#include <media/v4l2-ctrls.h>
  10746. +#include <media/v4l2-fh.h>
  10747. +#include <media/v4l2-event.h>
  10748. +#include <media/v4l2-common.h>
  10749. +#include <linux/delay.h>
  10750. +
  10751. +#include "mmal-common.h"
  10752. +#include "mmal-encodings.h"
  10753. +#include "mmal-vchiq.h"
  10754. +#include "mmal-msg.h"
  10755. +#include "mmal-parameters.h"
  10756. +#include "bcm2835-camera.h"
  10757. +
  10758. +#define BM2835_MMAL_VERSION "0.0.2"
  10759. +#define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
  10760. +#define MIN_WIDTH 16
  10761. +#define MIN_HEIGHT 16
  10762. +#define MAX_WIDTH 2592
  10763. +#define MAX_HEIGHT 1944
  10764. +#define MIN_BUFFER_SIZE (80*1024)
  10765. +
  10766. +#define MAX_VIDEO_MODE_WIDTH 1280
  10767. +#define MAX_VIDEO_MODE_HEIGHT 720
  10768. +
  10769. +MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
  10770. +MODULE_AUTHOR("Vincent Sanders");
  10771. +MODULE_LICENSE("GPL");
  10772. +MODULE_VERSION(BM2835_MMAL_VERSION);
  10773. +
  10774. +int bcm2835_v4l2_debug;
  10775. +module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
  10776. +MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
  10777. +
  10778. +static struct bm2835_mmal_dev *gdev; /* global device data */
  10779. +
  10780. +#define FPS_MIN 1
  10781. +#define FPS_MAX 90
  10782. +
  10783. +/* timeperframe: min/max and default */
  10784. +static const struct v4l2_fract
  10785. + tpf_min = {.numerator = 1, .denominator = FPS_MAX},
  10786. + tpf_max = {.numerator = 1, .denominator = FPS_MIN},
  10787. + tpf_default = {.numerator = 1000, .denominator = 30000};
  10788. +
  10789. +/* video formats */
  10790. +static struct mmal_fmt formats[] = {
  10791. + {
  10792. + .name = "4:2:0, packed YUV",
  10793. + .fourcc = V4L2_PIX_FMT_YUV420,
  10794. + .flags = 0,
  10795. + .mmal = MMAL_ENCODING_I420,
  10796. + .depth = 12,
  10797. + .mmal_component = MMAL_COMPONENT_CAMERA,
  10798. + },
  10799. + {
  10800. + .name = "4:2:2, packed, YUYV",
  10801. + .fourcc = V4L2_PIX_FMT_YUYV,
  10802. + .flags = 0,
  10803. + .mmal = MMAL_ENCODING_YUYV,
  10804. + .depth = 16,
  10805. + .mmal_component = MMAL_COMPONENT_CAMERA,
  10806. + },
  10807. + {
  10808. + .name = "RGB24 (LE)",
  10809. + .fourcc = V4L2_PIX_FMT_RGB24,
  10810. + .flags = 0,
  10811. + .mmal = MMAL_ENCODING_BGR24,
  10812. + .depth = 24,
  10813. + .mmal_component = MMAL_COMPONENT_CAMERA,
  10814. + },
  10815. + {
  10816. + .name = "JPEG",
  10817. + .fourcc = V4L2_PIX_FMT_JPEG,
  10818. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  10819. + .mmal = MMAL_ENCODING_JPEG,
  10820. + .depth = 8,
  10821. + .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
  10822. + },
  10823. + {
  10824. + .name = "H264",
  10825. + .fourcc = V4L2_PIX_FMT_H264,
  10826. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  10827. + .mmal = MMAL_ENCODING_H264,
  10828. + .depth = 8,
  10829. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  10830. + },
  10831. + {
  10832. + .name = "MJPEG",
  10833. + .fourcc = V4L2_PIX_FMT_MJPEG,
  10834. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  10835. + .mmal = MMAL_ENCODING_MJPEG,
  10836. + .depth = 8,
  10837. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  10838. + },
  10839. + {
  10840. + .name = "4:2:2, packed, YVYU",
  10841. + .fourcc = V4L2_PIX_FMT_YVYU,
  10842. + .flags = 0,
  10843. + .mmal = MMAL_ENCODING_YVYU,
  10844. + .depth = 16,
  10845. + .mmal_component = MMAL_COMPONENT_CAMERA,
  10846. + },
  10847. + {
  10848. + .name = "4:2:2, packed, VYUY",
  10849. + .fourcc = V4L2_PIX_FMT_VYUY,
  10850. + .flags = 0,
  10851. + .mmal = MMAL_ENCODING_VYUY,
  10852. + .depth = 16,
  10853. + .mmal_component = MMAL_COMPONENT_CAMERA,
  10854. + },
  10855. + {
  10856. + .name = "4:2:2, packed, UYVY",
  10857. + .fourcc = V4L2_PIX_FMT_UYVY,
  10858. + .flags = 0,
  10859. + .mmal = MMAL_ENCODING_UYVY,
  10860. + .depth = 16,
  10861. + .mmal_component = MMAL_COMPONENT_CAMERA,
  10862. + },
  10863. + {
  10864. + .name = "4:2:0, packed, NV12",
  10865. + .fourcc = V4L2_PIX_FMT_NV12,
  10866. + .flags = 0,
  10867. + .mmal = MMAL_ENCODING_NV12,
  10868. + .depth = 12,
  10869. + .mmal_component = MMAL_COMPONENT_CAMERA,
  10870. + },
  10871. +};
  10872. +
  10873. +static struct mmal_fmt *get_format(struct v4l2_format *f)
  10874. +{
  10875. + struct mmal_fmt *fmt;
  10876. + unsigned int k;
  10877. +
  10878. + for (k = 0; k < ARRAY_SIZE(formats); k++) {
  10879. + fmt = &formats[k];
  10880. + if (fmt->fourcc == f->fmt.pix.pixelformat)
  10881. + break;
  10882. + }
  10883. +
  10884. + if (k == ARRAY_SIZE(formats))
  10885. + return NULL;
  10886. +
  10887. + return &formats[k];
  10888. +}
  10889. +
  10890. +/* ------------------------------------------------------------------
  10891. + Videobuf queue operations
  10892. + ------------------------------------------------------------------*/
  10893. +
  10894. +static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  10895. + unsigned int *nbuffers, unsigned int *nplanes,
  10896. + unsigned int sizes[], void *alloc_ctxs[])
  10897. +{
  10898. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  10899. + unsigned long size;
  10900. +
  10901. + /* refuse queue setup if port is not configured */
  10902. + if (dev->capture.port == NULL) {
  10903. + v4l2_err(&dev->v4l2_dev,
  10904. + "%s: capture port not configured\n", __func__);
  10905. + return -EINVAL;
  10906. + }
  10907. +
  10908. + size = dev->capture.port->current_buffer.size;
  10909. + if (size == 0) {
  10910. + v4l2_err(&dev->v4l2_dev,
  10911. + "%s: capture port buffer size is zero\n", __func__);
  10912. + return -EINVAL;
  10913. + }
  10914. +
  10915. + if (*nbuffers < (dev->capture.port->current_buffer.num + 2))
  10916. + *nbuffers = (dev->capture.port->current_buffer.num + 2);
  10917. +
  10918. + *nplanes = 1;
  10919. +
  10920. + sizes[0] = size;
  10921. +
  10922. + /*
  10923. + * videobuf2-vmalloc allocator is context-less so no need to set
  10924. + * alloc_ctxs array.
  10925. + */
  10926. +
  10927. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  10928. + __func__, dev);
  10929. +
  10930. + return 0;
  10931. +}
  10932. +
  10933. +static int buffer_prepare(struct vb2_buffer *vb)
  10934. +{
  10935. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  10936. + unsigned long size;
  10937. +
  10938. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  10939. + __func__, dev);
  10940. +
  10941. + BUG_ON(dev->capture.port == NULL);
  10942. + BUG_ON(dev->capture.fmt == NULL);
  10943. +
  10944. + size = dev->capture.stride * dev->capture.height;
  10945. + if (vb2_plane_size(vb, 0) < size) {
  10946. + v4l2_err(&dev->v4l2_dev,
  10947. + "%s data will not fit into plane (%lu < %lu)\n",
  10948. + __func__, vb2_plane_size(vb, 0), size);
  10949. + return -EINVAL;
  10950. + }
  10951. +
  10952. + return 0;
  10953. +}
  10954. +
  10955. +static inline bool is_capturing(struct bm2835_mmal_dev *dev)
  10956. +{
  10957. + return dev->capture.camera_port ==
  10958. + &dev->
  10959. + component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
  10960. +}
  10961. +
  10962. +static void buffer_cb(struct vchiq_mmal_instance *instance,
  10963. + struct vchiq_mmal_port *port,
  10964. + int status,
  10965. + struct mmal_buffer *buf,
  10966. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
  10967. +{
  10968. + struct bm2835_mmal_dev *dev = port->cb_ctx;
  10969. +
  10970. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  10971. + "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
  10972. + __func__, status, buf, length, mmal_flags, pts);
  10973. +
  10974. + if (status != 0) {
  10975. + /* error in transfer */
  10976. + if (buf != NULL) {
  10977. + /* there was a buffer with the error so return it */
  10978. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  10979. + }
  10980. + return;
  10981. + } else if (length == 0) {
  10982. + /* stream ended */
  10983. + if (buf != NULL) {
  10984. + /* this should only ever happen if the port is
  10985. + * disabled and there are buffers still queued
  10986. + */
  10987. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  10988. + pr_debug("Empty buffer");
  10989. + } else if (dev->capture.frame_count) {
  10990. + /* grab another frame */
  10991. + if (is_capturing(dev)) {
  10992. + pr_debug("Grab another frame");
  10993. + vchiq_mmal_port_parameter_set(
  10994. + instance,
  10995. + dev->capture.
  10996. + camera_port,
  10997. + MMAL_PARAMETER_CAPTURE,
  10998. + &dev->capture.
  10999. + frame_count,
  11000. + sizeof(dev->capture.frame_count));
  11001. + }
  11002. + } else {
  11003. + /* signal frame completion */
  11004. + complete(&dev->capture.frame_cmplt);
  11005. + }
  11006. + } else {
  11007. + if (dev->capture.frame_count) {
  11008. + if (dev->capture.vc_start_timestamp != -1 &&
  11009. + pts != 0) {
  11010. + s64 runtime_us = pts -
  11011. + dev->capture.vc_start_timestamp;
  11012. + u32 div = 0;
  11013. + u32 rem = 0;
  11014. +
  11015. + div =
  11016. + div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
  11017. + buf->vb.v4l2_buf.timestamp.tv_sec =
  11018. + dev->capture.kernel_start_ts.tv_sec - 1 +
  11019. + div;
  11020. + buf->vb.v4l2_buf.timestamp.tv_usec =
  11021. + dev->capture.kernel_start_ts.tv_usec + rem;
  11022. +
  11023. + if (buf->vb.v4l2_buf.timestamp.tv_usec >=
  11024. + USEC_PER_SEC) {
  11025. + buf->vb.v4l2_buf.timestamp.tv_sec++;
  11026. + buf->vb.v4l2_buf.timestamp.tv_usec -=
  11027. + USEC_PER_SEC;
  11028. + }
  11029. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11030. + "Convert start time %d.%06d and %llu "
  11031. + "with offset %llu to %d.%06d\n",
  11032. + (int)dev->capture.kernel_start_ts.
  11033. + tv_sec,
  11034. + (int)dev->capture.kernel_start_ts.
  11035. + tv_usec,
  11036. + dev->capture.vc_start_timestamp, pts,
  11037. + (int)buf->vb.v4l2_buf.timestamp.tv_sec,
  11038. + (int)buf->vb.v4l2_buf.timestamp.
  11039. + tv_usec);
  11040. + } else {
  11041. + v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  11042. + }
  11043. +
  11044. + vb2_set_plane_payload(&buf->vb, 0, length);
  11045. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  11046. +
  11047. + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
  11048. + is_capturing(dev)) {
  11049. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11050. + "Grab another frame as buffer has EOS");
  11051. + vchiq_mmal_port_parameter_set(
  11052. + instance,
  11053. + dev->capture.
  11054. + camera_port,
  11055. + MMAL_PARAMETER_CAPTURE,
  11056. + &dev->capture.
  11057. + frame_count,
  11058. + sizeof(dev->capture.frame_count));
  11059. + }
  11060. + } else {
  11061. + /* signal frame completion */
  11062. + complete(&dev->capture.frame_cmplt);
  11063. + }
  11064. + }
  11065. +}
  11066. +
  11067. +static int enable_camera(struct bm2835_mmal_dev *dev)
  11068. +{
  11069. + int ret;
  11070. + if (!dev->camera_use_count) {
  11071. + ret = vchiq_mmal_component_enable(
  11072. + dev->instance,
  11073. + dev->component[MMAL_COMPONENT_CAMERA]);
  11074. + if (ret < 0) {
  11075. + v4l2_err(&dev->v4l2_dev,
  11076. + "Failed enabling camera, ret %d\n", ret);
  11077. + return -EINVAL;
  11078. + }
  11079. + }
  11080. + dev->camera_use_count++;
  11081. + v4l2_dbg(1, bcm2835_v4l2_debug,
  11082. + &dev->v4l2_dev, "enabled camera (refcount %d)\n",
  11083. + dev->camera_use_count);
  11084. + return 0;
  11085. +}
  11086. +
  11087. +static int disable_camera(struct bm2835_mmal_dev *dev)
  11088. +{
  11089. + int ret;
  11090. + if (!dev->camera_use_count) {
  11091. + v4l2_err(&dev->v4l2_dev,
  11092. + "Disabled the camera when already disabled\n");
  11093. + return -EINVAL;
  11094. + }
  11095. + dev->camera_use_count--;
  11096. + if (!dev->camera_use_count) {
  11097. + unsigned int i = 0xFFFFFFFF;
  11098. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11099. + "Disabling camera\n");
  11100. + ret =
  11101. + vchiq_mmal_component_disable(
  11102. + dev->instance,
  11103. + dev->component[MMAL_COMPONENT_CAMERA]);
  11104. + if (ret < 0) {
  11105. + v4l2_err(&dev->v4l2_dev,
  11106. + "Failed disabling camera, ret %d\n", ret);
  11107. + return -EINVAL;
  11108. + }
  11109. + vchiq_mmal_port_parameter_set(
  11110. + dev->instance,
  11111. + &dev->component[MMAL_COMPONENT_CAMERA]->control,
  11112. + MMAL_PARAMETER_CAMERA_NUM, &i,
  11113. + sizeof(i));
  11114. + }
  11115. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11116. + "Camera refcount now %d\n", dev->camera_use_count);
  11117. + return 0;
  11118. +}
  11119. +
  11120. +static void buffer_queue(struct vb2_buffer *vb)
  11121. +{
  11122. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  11123. + struct mmal_buffer *buf = container_of(vb, struct mmal_buffer, vb);
  11124. + int ret;
  11125. +
  11126. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11127. + "%s: dev:%p buf:%p\n", __func__, dev, buf);
  11128. +
  11129. + buf->buffer = vb2_plane_vaddr(&buf->vb, 0);
  11130. + buf->buffer_size = vb2_plane_size(&buf->vb, 0);
  11131. +
  11132. + ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
  11133. + if (ret < 0)
  11134. + v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
  11135. + __func__);
  11136. +}
  11137. +
  11138. +static int start_streaming(struct vb2_queue *vq, unsigned int count)
  11139. +{
  11140. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  11141. + int ret;
  11142. + int parameter_size;
  11143. +
  11144. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  11145. + __func__, dev);
  11146. +
  11147. + /* ensure a format has actually been set */
  11148. + if (dev->capture.port == NULL)
  11149. + return -EINVAL;
  11150. +
  11151. + if (enable_camera(dev) < 0) {
  11152. + v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
  11153. + return -EINVAL;
  11154. + }
  11155. +
  11156. + /*init_completion(&dev->capture.frame_cmplt); */
  11157. +
  11158. + /* enable frame capture */
  11159. + dev->capture.frame_count = 1;
  11160. +
  11161. + /* if the preview is not already running, wait for a few frames for AGC
  11162. + * to settle down.
  11163. + */
  11164. + if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
  11165. + msleep(300);
  11166. +
  11167. + /* enable the connection from camera to encoder (if applicable) */
  11168. + if (dev->capture.camera_port != dev->capture.port
  11169. + && dev->capture.camera_port) {
  11170. + ret = vchiq_mmal_port_enable(dev->instance,
  11171. + dev->capture.camera_port, NULL);
  11172. + if (ret) {
  11173. + v4l2_err(&dev->v4l2_dev,
  11174. + "Failed to enable encode tunnel - error %d\n",
  11175. + ret);
  11176. + return -1;
  11177. + }
  11178. + }
  11179. +
  11180. + /* Get VC timestamp at this point in time */
  11181. + parameter_size = sizeof(dev->capture.vc_start_timestamp);
  11182. + if (vchiq_mmal_port_parameter_get(dev->instance,
  11183. + dev->capture.camera_port,
  11184. + MMAL_PARAMETER_SYSTEM_TIME,
  11185. + &dev->capture.vc_start_timestamp,
  11186. + &parameter_size)) {
  11187. + v4l2_err(&dev->v4l2_dev,
  11188. + "Failed to get VC start time - update your VC f/w\n");
  11189. +
  11190. + /* Flag to indicate just to rely on kernel timestamps */
  11191. + dev->capture.vc_start_timestamp = -1;
  11192. + } else
  11193. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11194. + "Start time %lld size %d\n",
  11195. + dev->capture.vc_start_timestamp, parameter_size);
  11196. +
  11197. + v4l2_get_timestamp(&dev->capture.kernel_start_ts);
  11198. +
  11199. + /* enable the camera port */
  11200. + dev->capture.port->cb_ctx = dev;
  11201. + ret =
  11202. + vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
  11203. + if (ret) {
  11204. + v4l2_err(&dev->v4l2_dev,
  11205. + "Failed to enable capture port - error %d. "
  11206. + "Disabling camera port again\n", ret);
  11207. +
  11208. + vchiq_mmal_port_disable(dev->instance,
  11209. + dev->capture.camera_port);
  11210. + if (disable_camera(dev) < 0) {
  11211. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  11212. + return -EINVAL;
  11213. + }
  11214. + return -1;
  11215. + }
  11216. +
  11217. + /* capture the first frame */
  11218. + vchiq_mmal_port_parameter_set(dev->instance,
  11219. + dev->capture.camera_port,
  11220. + MMAL_PARAMETER_CAPTURE,
  11221. + &dev->capture.frame_count,
  11222. + sizeof(dev->capture.frame_count));
  11223. + return 0;
  11224. +}
  11225. +
  11226. +/* abort streaming and wait for last buffer */
  11227. +static int stop_streaming(struct vb2_queue *vq)
  11228. +{
  11229. + int ret;
  11230. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  11231. +
  11232. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  11233. + __func__, dev);
  11234. +
  11235. + init_completion(&dev->capture.frame_cmplt);
  11236. + dev->capture.frame_count = 0;
  11237. +
  11238. + /* ensure a format has actually been set */
  11239. + if (dev->capture.port == NULL)
  11240. + return -EINVAL;
  11241. +
  11242. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
  11243. +
  11244. + /* stop capturing frames */
  11245. + vchiq_mmal_port_parameter_set(dev->instance,
  11246. + dev->capture.camera_port,
  11247. + MMAL_PARAMETER_CAPTURE,
  11248. + &dev->capture.frame_count,
  11249. + sizeof(dev->capture.frame_count));
  11250. +
  11251. + /* wait for last frame to complete */
  11252. + ret = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
  11253. + if (ret <= 0)
  11254. + v4l2_err(&dev->v4l2_dev,
  11255. + "error %d waiting for frame completion\n", ret);
  11256. +
  11257. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11258. + "disabling connection\n");
  11259. +
  11260. + /* disable the connection from camera to encoder */
  11261. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
  11262. + if (!ret && dev->capture.camera_port != dev->capture.port) {
  11263. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11264. + "disabling port\n");
  11265. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
  11266. + } else if (dev->capture.camera_port != dev->capture.port) {
  11267. + v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
  11268. + ret);
  11269. + }
  11270. +
  11271. + if (disable_camera(dev) < 0) {
  11272. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  11273. + return -EINVAL;
  11274. + }
  11275. +
  11276. + return ret;
  11277. +}
  11278. +
  11279. +static void bm2835_mmal_lock(struct vb2_queue *vq)
  11280. +{
  11281. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  11282. + mutex_lock(&dev->mutex);
  11283. +}
  11284. +
  11285. +static void bm2835_mmal_unlock(struct vb2_queue *vq)
  11286. +{
  11287. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  11288. + mutex_unlock(&dev->mutex);
  11289. +}
  11290. +
  11291. +static struct vb2_ops bm2835_mmal_video_qops = {
  11292. + .queue_setup = queue_setup,
  11293. + .buf_prepare = buffer_prepare,
  11294. + .buf_queue = buffer_queue,
  11295. + .start_streaming = start_streaming,
  11296. + .stop_streaming = stop_streaming,
  11297. + .wait_prepare = bm2835_mmal_unlock,
  11298. + .wait_finish = bm2835_mmal_lock,
  11299. +};
  11300. +
  11301. +/* ------------------------------------------------------------------
  11302. + IOCTL operations
  11303. + ------------------------------------------------------------------*/
  11304. +
  11305. +/* overlay ioctl */
  11306. +static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
  11307. + struct v4l2_fmtdesc *f)
  11308. +{
  11309. + struct mmal_fmt *fmt;
  11310. +
  11311. + if (f->index >= ARRAY_SIZE(formats))
  11312. + return -EINVAL;
  11313. +
  11314. + fmt = &formats[f->index];
  11315. +
  11316. + strlcpy(f->description, fmt->name, sizeof(f->description));
  11317. + f->pixelformat = fmt->fourcc;
  11318. + f->flags = fmt->flags;
  11319. +
  11320. + return 0;
  11321. +}
  11322. +
  11323. +static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
  11324. + struct v4l2_format *f)
  11325. +{
  11326. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11327. +
  11328. + f->fmt.win = dev->overlay;
  11329. +
  11330. + return 0;
  11331. +}
  11332. +
  11333. +static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
  11334. + struct v4l2_format *f)
  11335. +{
  11336. + /* Only support one format so get the current one. */
  11337. + vidioc_g_fmt_vid_overlay(file, priv, f);
  11338. +
  11339. + /* todo: allow the size and/or offset to be changed. */
  11340. + return 0;
  11341. +}
  11342. +
  11343. +static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
  11344. + struct v4l2_format *f)
  11345. +{
  11346. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11347. +
  11348. + vidioc_try_fmt_vid_overlay(file, priv, f);
  11349. +
  11350. + dev->overlay = f->fmt.win;
  11351. +
  11352. + /* todo: program the preview port parameters */
  11353. + return 0;
  11354. +}
  11355. +
  11356. +static int vidioc_overlay(struct file *file, void *f, unsigned int on)
  11357. +{
  11358. + int ret;
  11359. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11360. + struct vchiq_mmal_port *src;
  11361. + struct vchiq_mmal_port *dst;
  11362. + struct mmal_parameter_displayregion prev_config = {
  11363. + .set = MMAL_DISPLAY_SET_LAYER | MMAL_DISPLAY_SET_ALPHA |
  11364. + MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN,
  11365. + .layer = PREVIEW_LAYER,
  11366. + .alpha = 255,
  11367. + .fullscreen = 0,
  11368. + .dest_rect = {
  11369. + .x = dev->overlay.w.left,
  11370. + .y = dev->overlay.w.top,
  11371. + .width = dev->overlay.w.width,
  11372. + .height = dev->overlay.w.height,
  11373. + },
  11374. + };
  11375. +
  11376. + if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
  11377. + (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
  11378. + return 0; /* already in requested state */
  11379. +
  11380. + src =
  11381. + &dev->component[MMAL_COMPONENT_CAMERA]->
  11382. + output[MMAL_CAMERA_PORT_PREVIEW];
  11383. +
  11384. + if (!on) {
  11385. + /* disconnect preview ports and disable component */
  11386. + ret = vchiq_mmal_port_disable(dev->instance, src);
  11387. + if (!ret)
  11388. + ret =
  11389. + vchiq_mmal_port_connect_tunnel(dev->instance, src,
  11390. + NULL);
  11391. + if (ret >= 0)
  11392. + ret = vchiq_mmal_component_disable(
  11393. + dev->instance,
  11394. + dev->component[MMAL_COMPONENT_PREVIEW]);
  11395. +
  11396. + disable_camera(dev);
  11397. + return ret;
  11398. + }
  11399. +
  11400. + /* set preview port format and connect it to output */
  11401. + dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
  11402. +
  11403. + ret = vchiq_mmal_port_set_format(dev->instance, src);
  11404. + if (ret < 0)
  11405. + goto error;
  11406. +
  11407. + ret = vchiq_mmal_port_parameter_set(dev->instance, dst,
  11408. + MMAL_PARAMETER_DISPLAYREGION,
  11409. + &prev_config, sizeof(prev_config));
  11410. + if (ret < 0)
  11411. + goto error;
  11412. +
  11413. + if (enable_camera(dev) < 0)
  11414. + goto error;
  11415. +
  11416. + ret = vchiq_mmal_component_enable(
  11417. + dev->instance,
  11418. + dev->component[MMAL_COMPONENT_PREVIEW]);
  11419. + if (ret < 0)
  11420. + goto error;
  11421. +
  11422. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
  11423. + src, dst);
  11424. + ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
  11425. + if (!ret)
  11426. + ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
  11427. +error:
  11428. + return ret;
  11429. +}
  11430. +
  11431. +static int vidioc_g_fbuf(struct file *file, void *fh,
  11432. + struct v4l2_framebuffer *a)
  11433. +{
  11434. + /* The video overlay must stay within the framebuffer and can't be
  11435. + positioned independently. */
  11436. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11437. + struct vchiq_mmal_port *preview_port =
  11438. + &dev->component[MMAL_COMPONENT_CAMERA]->
  11439. + output[MMAL_CAMERA_PORT_PREVIEW];
  11440. + a->flags = V4L2_FBUF_FLAG_OVERLAY;
  11441. + a->fmt.width = preview_port->es.video.width;
  11442. + a->fmt.height = preview_port->es.video.height;
  11443. + a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
  11444. + a->fmt.bytesperline = (preview_port->es.video.width * 3)>>1;
  11445. + a->fmt.sizeimage = (preview_port->es.video.width *
  11446. + preview_port->es.video.height * 3)>>1;
  11447. + a->fmt.colorspace = V4L2_COLORSPACE_JPEG;
  11448. +
  11449. + return 0;
  11450. +}
  11451. +
  11452. +/* input ioctls */
  11453. +static int vidioc_enum_input(struct file *file, void *priv,
  11454. + struct v4l2_input *inp)
  11455. +{
  11456. + /* only a single camera input */
  11457. + if (inp->index != 0)
  11458. + return -EINVAL;
  11459. +
  11460. + inp->type = V4L2_INPUT_TYPE_CAMERA;
  11461. + sprintf(inp->name, "Camera %u", inp->index);
  11462. + return 0;
  11463. +}
  11464. +
  11465. +static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  11466. +{
  11467. + *i = 0;
  11468. + return 0;
  11469. +}
  11470. +
  11471. +static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  11472. +{
  11473. + if (i != 0)
  11474. + return -EINVAL;
  11475. +
  11476. + return 0;
  11477. +}
  11478. +
  11479. +/* capture ioctls */
  11480. +static int vidioc_querycap(struct file *file, void *priv,
  11481. + struct v4l2_capability *cap)
  11482. +{
  11483. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11484. + u32 major;
  11485. + u32 minor;
  11486. +
  11487. + vchiq_mmal_version(dev->instance, &major, &minor);
  11488. +
  11489. + strcpy(cap->driver, "bm2835 mmal");
  11490. + snprintf(cap->card, sizeof(cap->card), "mmal service %d.%d",
  11491. + major, minor);
  11492. +
  11493. + snprintf(cap->bus_info, sizeof(cap->bus_info),
  11494. + "platform:%s", dev->v4l2_dev.name);
  11495. + cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
  11496. + V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  11497. + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  11498. +
  11499. + return 0;
  11500. +}
  11501. +
  11502. +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  11503. + struct v4l2_fmtdesc *f)
  11504. +{
  11505. + struct mmal_fmt *fmt;
  11506. +
  11507. + if (f->index >= ARRAY_SIZE(formats))
  11508. + return -EINVAL;
  11509. +
  11510. + fmt = &formats[f->index];
  11511. +
  11512. + strlcpy(f->description, fmt->name, sizeof(f->description));
  11513. + f->pixelformat = fmt->fourcc;
  11514. + f->flags = fmt->flags;
  11515. +
  11516. + return 0;
  11517. +}
  11518. +
  11519. +static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  11520. + struct v4l2_format *f)
  11521. +{
  11522. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11523. +
  11524. + f->fmt.pix.width = dev->capture.width;
  11525. + f->fmt.pix.height = dev->capture.height;
  11526. + f->fmt.pix.field = V4L2_FIELD_NONE;
  11527. + f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
  11528. + f->fmt.pix.bytesperline =
  11529. + (f->fmt.pix.width * dev->capture.fmt->depth) >> 3;
  11530. + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  11531. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_JPEG
  11532. + && f->fmt.pix.sizeimage < (100 << 10)) {
  11533. + /* Need a minimum size for JPEG to account for EXIF. */
  11534. + f->fmt.pix.sizeimage = (100 << 10);
  11535. + }
  11536. +
  11537. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_YUYV ||
  11538. + dev->capture.fmt->fourcc == V4L2_PIX_FMT_UYVY)
  11539. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  11540. + else
  11541. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  11542. + f->fmt.pix.priv = 0;
  11543. +
  11544. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  11545. + __func__);
  11546. + return 0;
  11547. +}
  11548. +
  11549. +static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  11550. + struct v4l2_format *f)
  11551. +{
  11552. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11553. + struct mmal_fmt *mfmt;
  11554. +
  11555. + mfmt = get_format(f);
  11556. + if (!mfmt) {
  11557. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11558. + "Fourcc format (0x%08x) unknown.\n",
  11559. + f->fmt.pix.pixelformat);
  11560. + f->fmt.pix.pixelformat = formats[0].fourcc;
  11561. + mfmt = get_format(f);
  11562. + }
  11563. +
  11564. + f->fmt.pix.field = V4L2_FIELD_NONE;
  11565. + /* image must be a multiple of 32 pixels wide and 16 lines high */
  11566. + v4l_bound_align_image(&f->fmt.pix.width, 48, MAX_WIDTH, 5,
  11567. + &f->fmt.pix.height, 32, MAX_HEIGHT, 4, 0);
  11568. + f->fmt.pix.bytesperline = (f->fmt.pix.width * mfmt->depth) >> 3;
  11569. + f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  11570. + if (f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
  11571. + f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
  11572. +
  11573. + if (mfmt->fourcc == V4L2_PIX_FMT_YUYV ||
  11574. + mfmt->fourcc == V4L2_PIX_FMT_UYVY)
  11575. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  11576. + else
  11577. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  11578. + f->fmt.pix.priv = 0;
  11579. +
  11580. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  11581. + __func__);
  11582. + return 0;
  11583. +}
  11584. +
  11585. +static int mmal_setup_components(struct bm2835_mmal_dev *dev,
  11586. + struct v4l2_format *f)
  11587. +{
  11588. + int ret;
  11589. + struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
  11590. + struct vchiq_mmal_component *encode_component = NULL;
  11591. + struct mmal_fmt *mfmt = get_format(f);
  11592. +
  11593. + BUG_ON(!mfmt);
  11594. +
  11595. + if (dev->capture.encode_component) {
  11596. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11597. + "vid_cap - disconnect previous tunnel\n");
  11598. +
  11599. + /* Disconnect any previous connection */
  11600. + vchiq_mmal_port_connect_tunnel(dev->instance,
  11601. + dev->capture.camera_port, NULL);
  11602. + dev->capture.camera_port = NULL;
  11603. + ret = vchiq_mmal_component_disable(dev->instance,
  11604. + dev->capture.
  11605. + encode_component);
  11606. + if (ret)
  11607. + v4l2_err(&dev->v4l2_dev,
  11608. + "Failed to disable encode component %d\n",
  11609. + ret);
  11610. +
  11611. + dev->capture.encode_component = NULL;
  11612. + }
  11613. + /* format dependant port setup */
  11614. + switch (mfmt->mmal_component) {
  11615. + case MMAL_COMPONENT_CAMERA:
  11616. + /* Make a further decision on port based on resolution */
  11617. + if (f->fmt.pix.width <= MAX_VIDEO_MODE_WIDTH
  11618. + && f->fmt.pix.height <= MAX_VIDEO_MODE_HEIGHT)
  11619. + camera_port = port =
  11620. + &dev->component[MMAL_COMPONENT_CAMERA]->
  11621. + output[MMAL_CAMERA_PORT_VIDEO];
  11622. + else
  11623. + camera_port = port =
  11624. + &dev->component[MMAL_COMPONENT_CAMERA]->
  11625. + output[MMAL_CAMERA_PORT_CAPTURE];
  11626. + break;
  11627. + case MMAL_COMPONENT_IMAGE_ENCODE:
  11628. + encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
  11629. + port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  11630. + camera_port =
  11631. + &dev->component[MMAL_COMPONENT_CAMERA]->
  11632. + output[MMAL_CAMERA_PORT_CAPTURE];
  11633. + break;
  11634. + case MMAL_COMPONENT_VIDEO_ENCODE:
  11635. + encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
  11636. + port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  11637. + camera_port =
  11638. + &dev->component[MMAL_COMPONENT_CAMERA]->
  11639. + output[MMAL_CAMERA_PORT_VIDEO];
  11640. + break;
  11641. + default:
  11642. + break;
  11643. + }
  11644. +
  11645. + if (!port)
  11646. + return -EINVAL;
  11647. +
  11648. + if (encode_component)
  11649. + camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
  11650. + else
  11651. + camera_port->format.encoding = mfmt->mmal;
  11652. +
  11653. + camera_port->format.encoding_variant = 0;
  11654. + camera_port->es.video.width = f->fmt.pix.width;
  11655. + camera_port->es.video.height = f->fmt.pix.height;
  11656. + camera_port->es.video.crop.x = 0;
  11657. + camera_port->es.video.crop.y = 0;
  11658. + camera_port->es.video.crop.width = f->fmt.pix.width;
  11659. + camera_port->es.video.crop.height = f->fmt.pix.height;
  11660. + camera_port->es.video.frame_rate.num = 0;
  11661. + camera_port->es.video.frame_rate.den = 1;
  11662. +
  11663. + ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
  11664. +
  11665. + if (!ret
  11666. + && camera_port ==
  11667. + &dev->component[MMAL_COMPONENT_CAMERA]->
  11668. + output[MMAL_CAMERA_PORT_VIDEO]) {
  11669. + bool overlay_enabled =
  11670. + !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
  11671. + struct vchiq_mmal_port *preview_port =
  11672. + &dev->component[MMAL_COMPONENT_CAMERA]->
  11673. + output[MMAL_CAMERA_PORT_PREVIEW];
  11674. + /* Preview and encode ports need to match on resolution */
  11675. + if (overlay_enabled) {
  11676. + /* Need to disable the overlay before we can update
  11677. + * the resolution
  11678. + */
  11679. + ret =
  11680. + vchiq_mmal_port_disable(dev->instance,
  11681. + preview_port);
  11682. + if (!ret)
  11683. + ret =
  11684. + vchiq_mmal_port_connect_tunnel(
  11685. + dev->instance,
  11686. + preview_port,
  11687. + NULL);
  11688. + }
  11689. + preview_port->es.video.width = f->fmt.pix.width;
  11690. + preview_port->es.video.height = f->fmt.pix.height;
  11691. + preview_port->es.video.crop.x = 0;
  11692. + preview_port->es.video.crop.y = 0;
  11693. + preview_port->es.video.crop.width = f->fmt.pix.width;
  11694. + preview_port->es.video.crop.height = f->fmt.pix.height;
  11695. + preview_port->es.video.frame_rate.num =
  11696. + dev->capture.timeperframe.denominator;
  11697. + preview_port->es.video.frame_rate.den =
  11698. + dev->capture.timeperframe.numerator;
  11699. + ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
  11700. + if (overlay_enabled) {
  11701. + ret = vchiq_mmal_port_connect_tunnel(
  11702. + dev->instance,
  11703. + preview_port,
  11704. + &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
  11705. + if (!ret)
  11706. + ret = vchiq_mmal_port_enable(dev->instance,
  11707. + preview_port,
  11708. + NULL);
  11709. + }
  11710. + }
  11711. +
  11712. + if (ret) {
  11713. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11714. + "%s failed to set format\n", __func__);
  11715. + /* ensure capture is not going to be tried */
  11716. + dev->capture.port = NULL;
  11717. + } else {
  11718. + if (encode_component) {
  11719. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11720. + "vid_cap - set up encode comp\n");
  11721. +
  11722. + /* configure buffering */
  11723. + camera_port->current_buffer.size =
  11724. + camera_port->recommended_buffer.size;
  11725. + camera_port->current_buffer.num =
  11726. + camera_port->recommended_buffer.num;
  11727. +
  11728. + ret =
  11729. + vchiq_mmal_port_connect_tunnel(
  11730. + dev->instance,
  11731. + camera_port,
  11732. + &encode_component->input[0]);
  11733. + if (ret) {
  11734. + v4l2_dbg(1, bcm2835_v4l2_debug,
  11735. + &dev->v4l2_dev,
  11736. + "%s failed to create connection\n",
  11737. + __func__);
  11738. + /* ensure capture is not going to be tried */
  11739. + dev->capture.port = NULL;
  11740. + } else {
  11741. + port->es.video.width = f->fmt.pix.width;
  11742. + port->es.video.height = f->fmt.pix.height;
  11743. + port->es.video.crop.x = 0;
  11744. + port->es.video.crop.y = 0;
  11745. + port->es.video.crop.width = f->fmt.pix.width;
  11746. + port->es.video.crop.height = f->fmt.pix.height;
  11747. + port->es.video.frame_rate.num =
  11748. + dev->capture.timeperframe.denominator;
  11749. + port->es.video.frame_rate.den =
  11750. + dev->capture.timeperframe.numerator;
  11751. +
  11752. + port->format.encoding = mfmt->mmal;
  11753. + port->format.encoding_variant = 0;
  11754. + /* Set any encoding specific parameters */
  11755. + switch (mfmt->mmal_component) {
  11756. + case MMAL_COMPONENT_VIDEO_ENCODE:
  11757. + port->format.bitrate =
  11758. + dev->capture.encode_bitrate;
  11759. + break;
  11760. + case MMAL_COMPONENT_IMAGE_ENCODE:
  11761. + /* Could set EXIF parameters here */
  11762. + break;
  11763. + default:
  11764. + break;
  11765. + }
  11766. + ret = vchiq_mmal_port_set_format(dev->instance,
  11767. + port);
  11768. + if (ret)
  11769. + v4l2_dbg(1, bcm2835_v4l2_debug,
  11770. + &dev->v4l2_dev,
  11771. + "%s failed to set format\n",
  11772. + __func__);
  11773. + }
  11774. +
  11775. + if (!ret) {
  11776. + ret = vchiq_mmal_component_enable(
  11777. + dev->instance,
  11778. + encode_component);
  11779. + if (ret) {
  11780. + v4l2_dbg(1, bcm2835_v4l2_debug,
  11781. + &dev->v4l2_dev,
  11782. + "%s Failed to enable encode components\n",
  11783. + __func__);
  11784. + }
  11785. + }
  11786. + if (!ret) {
  11787. + /* configure buffering */
  11788. + port->current_buffer.num = 1;
  11789. + port->current_buffer.size =
  11790. + f->fmt.pix.sizeimage;
  11791. + if (port->format.encoding ==
  11792. + MMAL_ENCODING_JPEG) {
  11793. + v4l2_dbg(1, bcm2835_v4l2_debug,
  11794. + &dev->v4l2_dev,
  11795. + "JPG - buf size now %d was %d\n",
  11796. + f->fmt.pix.sizeimage,
  11797. + port->current_buffer.size);
  11798. + port->current_buffer.size =
  11799. + (f->fmt.pix.sizeimage <
  11800. + (100 << 10))
  11801. + ? (100 << 10) : f->fmt.pix.
  11802. + sizeimage;
  11803. + }
  11804. + v4l2_dbg(1, bcm2835_v4l2_debug,
  11805. + &dev->v4l2_dev,
  11806. + "vid_cap - cur_buf.size set to %d\n",
  11807. + f->fmt.pix.sizeimage);
  11808. + port->current_buffer.alignment = 0;
  11809. + }
  11810. + } else {
  11811. + /* configure buffering */
  11812. + camera_port->current_buffer.num = 1;
  11813. + camera_port->current_buffer.size = f->fmt.pix.sizeimage;
  11814. + camera_port->current_buffer.alignment = 0;
  11815. + }
  11816. +
  11817. + if (!ret) {
  11818. + dev->capture.fmt = mfmt;
  11819. + dev->capture.stride = f->fmt.pix.bytesperline;
  11820. + dev->capture.width = camera_port->es.video.crop.width;
  11821. + dev->capture.height = camera_port->es.video.crop.height;
  11822. +
  11823. + /* select port for capture */
  11824. + dev->capture.port = port;
  11825. + dev->capture.camera_port = camera_port;
  11826. + dev->capture.encode_component = encode_component;
  11827. + v4l2_dbg(1, bcm2835_v4l2_debug,
  11828. + &dev->v4l2_dev,
  11829. + "Set dev->capture.fmt %08X, %dx%d, stride %d",
  11830. + port->format.encoding,
  11831. + dev->capture.width, dev->capture.height,
  11832. + dev->capture.stride);
  11833. + }
  11834. + }
  11835. +
  11836. + /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
  11837. + return ret;
  11838. +}
  11839. +
  11840. +static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  11841. + struct v4l2_format *f)
  11842. +{
  11843. + int ret;
  11844. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11845. + struct mmal_fmt *mfmt;
  11846. +
  11847. + /* try the format to set valid parameters */
  11848. + ret = vidioc_try_fmt_vid_cap(file, priv, f);
  11849. + if (ret) {
  11850. + v4l2_err(&dev->v4l2_dev,
  11851. + "vid_cap - vidioc_try_fmt_vid_cap failed\n");
  11852. + return ret;
  11853. + }
  11854. +
  11855. + /* if a capture is running refuse to set format */
  11856. + if (vb2_is_busy(&dev->capture.vb_vidq)) {
  11857. + v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
  11858. + return -EBUSY;
  11859. + }
  11860. +
  11861. + /* If the format is unsupported v4l2 says we should switch to
  11862. + * a supported one and not return an error. */
  11863. + mfmt = get_format(f);
  11864. + if (!mfmt) {
  11865. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  11866. + "Fourcc format (0x%08x) unknown.\n",
  11867. + f->fmt.pix.pixelformat);
  11868. + f->fmt.pix.pixelformat = formats[0].fourcc;
  11869. + mfmt = get_format(f);
  11870. + }
  11871. +
  11872. + ret = mmal_setup_components(dev, f);
  11873. + if (ret != 0) {
  11874. + v4l2_err(&dev->v4l2_dev,
  11875. + "%s: failed to setup mmal components: %d\n",
  11876. + __func__, ret);
  11877. + ret = -EINVAL;
  11878. + }
  11879. +
  11880. + return ret;
  11881. +}
  11882. +
  11883. +int vidioc_enum_framesizes(struct file *file, void *fh,
  11884. + struct v4l2_frmsizeenum *fsize)
  11885. +{
  11886. + static const struct v4l2_frmsize_stepwise sizes = {
  11887. + MIN_WIDTH, MAX_WIDTH, 2,
  11888. + MIN_HEIGHT, MAX_HEIGHT, 2
  11889. + };
  11890. + int i;
  11891. +
  11892. + if (fsize->index)
  11893. + return -EINVAL;
  11894. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  11895. + if (formats[i].fourcc == fsize->pixel_format)
  11896. + break;
  11897. + if (i == ARRAY_SIZE(formats))
  11898. + return -EINVAL;
  11899. + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  11900. + fsize->stepwise = sizes;
  11901. + return 0;
  11902. +}
  11903. +
  11904. +/* timeperframe is arbitrary and continous */
  11905. +static int vidioc_enum_frameintervals(struct file *file, void *priv,
  11906. + struct v4l2_frmivalenum *fival)
  11907. +{
  11908. + int i;
  11909. +
  11910. + if (fival->index)
  11911. + return -EINVAL;
  11912. +
  11913. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  11914. + if (formats[i].fourcc == fival->pixel_format)
  11915. + break;
  11916. + if (i == ARRAY_SIZE(formats))
  11917. + return -EINVAL;
  11918. +
  11919. + /* regarding width & height - we support any within range */
  11920. + if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH ||
  11921. + fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT)
  11922. + return -EINVAL;
  11923. +
  11924. + fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
  11925. +
  11926. + /* fill in stepwise (step=1.0 is requred by V4L2 spec) */
  11927. + fival->stepwise.min = tpf_min;
  11928. + fival->stepwise.max = tpf_max;
  11929. + fival->stepwise.step = (struct v4l2_fract) {1, 1};
  11930. +
  11931. + return 0;
  11932. +}
  11933. +
  11934. +static int vidioc_g_parm(struct file *file, void *priv,
  11935. + struct v4l2_streamparm *parm)
  11936. +{
  11937. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11938. +
  11939. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  11940. + return -EINVAL;
  11941. +
  11942. + parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  11943. + parm->parm.capture.timeperframe = dev->capture.timeperframe;
  11944. + parm->parm.capture.readbuffers = 1;
  11945. + return 0;
  11946. +}
  11947. +
  11948. +#define FRACT_CMP(a, OP, b) \
  11949. + ((u64)(a).numerator * (b).denominator OP \
  11950. + (u64)(b).numerator * (a).denominator)
  11951. +
  11952. +static int vidioc_s_parm(struct file *file, void *priv,
  11953. + struct v4l2_streamparm *parm)
  11954. +{
  11955. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  11956. + struct v4l2_fract tpf;
  11957. + struct mmal_parameter_rational fps_param;
  11958. +
  11959. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  11960. + return -EINVAL;
  11961. +
  11962. + tpf = parm->parm.capture.timeperframe;
  11963. +
  11964. + /* tpf: {*, 0} resets timing; clip to [min, max]*/
  11965. + tpf = tpf.denominator ? tpf : tpf_default;
  11966. + tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
  11967. + tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
  11968. +
  11969. + dev->capture.timeperframe = tpf;
  11970. + parm->parm.capture.timeperframe = tpf;
  11971. + parm->parm.capture.readbuffers = 1;
  11972. +
  11973. + fps_param.num = 0; /* Select variable fps, and then use
  11974. + * FPS_RANGE to select the actual limits.
  11975. + */
  11976. + fps_param.den = 1;
  11977. + set_framerate_params(dev);
  11978. +
  11979. + return 0;
  11980. +}
  11981. +
  11982. +static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
  11983. + /* overlay */
  11984. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  11985. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  11986. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  11987. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  11988. + .vidioc_overlay = vidioc_overlay,
  11989. + .vidioc_g_fbuf = vidioc_g_fbuf,
  11990. +
  11991. + /* inputs */
  11992. + .vidioc_enum_input = vidioc_enum_input,
  11993. + .vidioc_g_input = vidioc_g_input,
  11994. + .vidioc_s_input = vidioc_s_input,
  11995. +
  11996. + /* capture */
  11997. + .vidioc_querycap = vidioc_querycap,
  11998. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  11999. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  12000. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  12001. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  12002. +
  12003. + /* buffer management */
  12004. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  12005. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  12006. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  12007. + .vidioc_querybuf = vb2_ioctl_querybuf,
  12008. + .vidioc_qbuf = vb2_ioctl_qbuf,
  12009. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  12010. + .vidioc_enum_framesizes = vidioc_enum_framesizes,
  12011. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  12012. + .vidioc_g_parm = vidioc_g_parm,
  12013. + .vidioc_s_parm = vidioc_s_parm,
  12014. + .vidioc_streamon = vb2_ioctl_streamon,
  12015. + .vidioc_streamoff = vb2_ioctl_streamoff,
  12016. +
  12017. + .vidioc_log_status = v4l2_ctrl_log_status,
  12018. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  12019. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  12020. +};
  12021. +
  12022. +/* ------------------------------------------------------------------
  12023. + Driver init/finalise
  12024. + ------------------------------------------------------------------*/
  12025. +
  12026. +static const struct v4l2_file_operations camera0_fops = {
  12027. + .owner = THIS_MODULE,
  12028. + .open = v4l2_fh_open,
  12029. + .release = vb2_fop_release,
  12030. + .read = vb2_fop_read,
  12031. + .poll = vb2_fop_poll,
  12032. + .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  12033. + .mmap = vb2_fop_mmap,
  12034. +};
  12035. +
  12036. +static struct video_device vdev_template = {
  12037. + .name = "camera0",
  12038. + .fops = &camera0_fops,
  12039. + .ioctl_ops = &camera0_ioctl_ops,
  12040. + .release = video_device_release_empty,
  12041. +};
  12042. +
  12043. +static int set_camera_parameters(struct vchiq_mmal_instance *instance,
  12044. + struct vchiq_mmal_component *camera)
  12045. +{
  12046. + int ret;
  12047. + struct mmal_parameter_camera_config cam_config = {
  12048. + .max_stills_w = MAX_WIDTH,
  12049. + .max_stills_h = MAX_HEIGHT,
  12050. + .stills_yuv422 = 1,
  12051. + .one_shot_stills = 1,
  12052. + .max_preview_video_w = 1920,
  12053. + .max_preview_video_h = 1088,
  12054. + .num_preview_video_frames = 3,
  12055. + .stills_capture_circular_buffer_height = 0,
  12056. + .fast_preview_resume = 0,
  12057. + .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
  12058. + };
  12059. +
  12060. + ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
  12061. + MMAL_PARAMETER_CAMERA_CONFIG,
  12062. + &cam_config, sizeof(cam_config));
  12063. + return ret;
  12064. +}
  12065. +
  12066. +/* MMAL instance and component init */
  12067. +static int __init mmal_init(struct bm2835_mmal_dev *dev)
  12068. +{
  12069. + int ret;
  12070. + struct mmal_es_format *format;
  12071. +
  12072. + ret = vchiq_mmal_init(&dev->instance);
  12073. + if (ret < 0)
  12074. + return ret;
  12075. +
  12076. + /* get the camera component ready */
  12077. + ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
  12078. + &dev->component[MMAL_COMPONENT_CAMERA]);
  12079. + if (ret < 0)
  12080. + goto unreg_mmal;
  12081. +
  12082. + if (dev->component[MMAL_COMPONENT_CAMERA]->outputs <
  12083. + MMAL_CAMERA_PORT_COUNT) {
  12084. + ret = -EINVAL;
  12085. + goto unreg_camera;
  12086. + }
  12087. +
  12088. + ret = set_camera_parameters(dev->instance,
  12089. + dev->component[MMAL_COMPONENT_CAMERA]);
  12090. + if (ret < 0)
  12091. + goto unreg_camera;
  12092. +
  12093. + format =
  12094. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12095. + output[MMAL_CAMERA_PORT_PREVIEW].format;
  12096. +
  12097. + format->encoding = MMAL_ENCODING_OPAQUE;
  12098. + format->encoding_variant = MMAL_ENCODING_I420;
  12099. +
  12100. + format->es->video.width = 1024;
  12101. + format->es->video.height = 768;
  12102. + format->es->video.crop.x = 0;
  12103. + format->es->video.crop.y = 0;
  12104. + format->es->video.crop.width = 1024;
  12105. + format->es->video.crop.height = 768;
  12106. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  12107. + format->es->video.frame_rate.den = 1;
  12108. +
  12109. + format =
  12110. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12111. + output[MMAL_CAMERA_PORT_VIDEO].format;
  12112. +
  12113. + format->encoding = MMAL_ENCODING_OPAQUE;
  12114. + format->encoding_variant = MMAL_ENCODING_I420;
  12115. +
  12116. + format->es->video.width = 1024;
  12117. + format->es->video.height = 768;
  12118. + format->es->video.crop.x = 0;
  12119. + format->es->video.crop.y = 0;
  12120. + format->es->video.crop.width = 1024;
  12121. + format->es->video.crop.height = 768;
  12122. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  12123. + format->es->video.frame_rate.den = 1;
  12124. +
  12125. + format =
  12126. + &dev->component[MMAL_COMPONENT_CAMERA]->
  12127. + output[MMAL_CAMERA_PORT_CAPTURE].format;
  12128. +
  12129. + format->encoding = MMAL_ENCODING_OPAQUE;
  12130. +
  12131. + format->es->video.width = 2592;
  12132. + format->es->video.height = 1944;
  12133. + format->es->video.crop.x = 0;
  12134. + format->es->video.crop.y = 0;
  12135. + format->es->video.crop.width = 2592;
  12136. + format->es->video.crop.height = 1944;
  12137. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  12138. + format->es->video.frame_rate.den = 1;
  12139. +
  12140. + dev->capture.width = format->es->video.width;
  12141. + dev->capture.height = format->es->video.height;
  12142. + dev->capture.fmt = &formats[0];
  12143. + dev->capture.encode_component = NULL;
  12144. + dev->capture.timeperframe = tpf_default;
  12145. + dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
  12146. + dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  12147. +
  12148. + /* get the preview component ready */
  12149. + ret = vchiq_mmal_component_init(
  12150. + dev->instance, "ril.video_render",
  12151. + &dev->component[MMAL_COMPONENT_PREVIEW]);
  12152. + if (ret < 0)
  12153. + goto unreg_camera;
  12154. +
  12155. + if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
  12156. + ret = -EINVAL;
  12157. + pr_debug("too few input ports %d needed %d\n",
  12158. + dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
  12159. + goto unreg_preview;
  12160. + }
  12161. +
  12162. + /* get the image encoder component ready */
  12163. + ret = vchiq_mmal_component_init(
  12164. + dev->instance, "ril.image_encode",
  12165. + &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  12166. + if (ret < 0)
  12167. + goto unreg_preview;
  12168. +
  12169. + if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
  12170. + ret = -EINVAL;
  12171. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  12172. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
  12173. + 1);
  12174. + goto unreg_image_encoder;
  12175. + }
  12176. +
  12177. + /* get the video encoder component ready */
  12178. + ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
  12179. + &dev->
  12180. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  12181. + if (ret < 0)
  12182. + goto unreg_image_encoder;
  12183. +
  12184. + if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
  12185. + ret = -EINVAL;
  12186. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  12187. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
  12188. + 1);
  12189. + goto unreg_vid_encoder;
  12190. + }
  12191. +
  12192. + {
  12193. + struct vchiq_mmal_port *encoder_port =
  12194. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  12195. + encoder_port->format.encoding = MMAL_ENCODING_H264;
  12196. + ret = vchiq_mmal_port_set_format(dev->instance,
  12197. + encoder_port);
  12198. + }
  12199. +
  12200. + {
  12201. + unsigned int enable = 1;
  12202. + vchiq_mmal_port_parameter_set(
  12203. + dev->instance,
  12204. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  12205. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  12206. + &enable, sizeof(enable));
  12207. +
  12208. + vchiq_mmal_port_parameter_set(dev->instance,
  12209. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  12210. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  12211. + &enable,
  12212. + sizeof(enable));
  12213. + }
  12214. + ret = bm2835_mmal_set_all_camera_controls(dev);
  12215. + if (ret < 0)
  12216. + goto unreg_vid_encoder;
  12217. +
  12218. + return 0;
  12219. +
  12220. +unreg_vid_encoder:
  12221. + pr_err("Cleanup: Destroy video encoder\n");
  12222. + vchiq_mmal_component_finalise(
  12223. + dev->instance,
  12224. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
  12225. +
  12226. +unreg_image_encoder:
  12227. + pr_err("Cleanup: Destroy image encoder\n");
  12228. + vchiq_mmal_component_finalise(
  12229. + dev->instance,
  12230. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  12231. +
  12232. +unreg_preview:
  12233. + pr_err("Cleanup: Destroy video render\n");
  12234. + vchiq_mmal_component_finalise(dev->instance,
  12235. + dev->component[MMAL_COMPONENT_PREVIEW]);
  12236. +
  12237. +unreg_camera:
  12238. + pr_err("Cleanup: Destroy camera\n");
  12239. + vchiq_mmal_component_finalise(dev->instance,
  12240. + dev->component[MMAL_COMPONENT_CAMERA]);
  12241. +
  12242. +unreg_mmal:
  12243. + vchiq_mmal_finalise(dev->instance);
  12244. + return ret;
  12245. +}
  12246. +
  12247. +static int __init bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
  12248. + struct video_device *vfd)
  12249. +{
  12250. + int ret;
  12251. +
  12252. + *vfd = vdev_template;
  12253. +
  12254. + vfd->v4l2_dev = &dev->v4l2_dev;
  12255. +
  12256. + vfd->lock = &dev->mutex;
  12257. +
  12258. + vfd->queue = &dev->capture.vb_vidq;
  12259. +
  12260. + set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  12261. +
  12262. + /* video device needs to be able to access instance data */
  12263. + video_set_drvdata(vfd, dev);
  12264. +
  12265. + ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  12266. + if (ret < 0)
  12267. + return ret;
  12268. +
  12269. + v4l2_info(vfd->v4l2_dev, "V4L2 device registered as %s\n",
  12270. + video_device_node_name(vfd));
  12271. +
  12272. + return 0;
  12273. +}
  12274. +
  12275. +static struct v4l2_format default_v4l2_format = {
  12276. + .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
  12277. + .fmt.pix.width = 1024,
  12278. + .fmt.pix.bytesperline = 1024 * 3 / 2,
  12279. + .fmt.pix.height = 768,
  12280. + .fmt.pix.sizeimage = 1<<18,
  12281. +};
  12282. +
  12283. +static int __init bm2835_mmal_init(void)
  12284. +{
  12285. + int ret;
  12286. + struct bm2835_mmal_dev *dev;
  12287. + struct vb2_queue *q;
  12288. +
  12289. + dev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  12290. + if (!dev)
  12291. + return -ENOMEM;
  12292. +
  12293. + /* setup device defaults */
  12294. + dev->overlay.w.left = 150;
  12295. + dev->overlay.w.top = 50;
  12296. + dev->overlay.w.width = 1024;
  12297. + dev->overlay.w.height = 768;
  12298. + dev->overlay.clipcount = 0;
  12299. + dev->overlay.field = V4L2_FIELD_NONE;
  12300. +
  12301. + dev->capture.fmt = &formats[3]; /* JPEG */
  12302. +
  12303. + /* v4l device registration */
  12304. + snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  12305. + "%s", BM2835_MMAL_MODULE_NAME);
  12306. + ret = v4l2_device_register(NULL, &dev->v4l2_dev);
  12307. + if (ret)
  12308. + goto free_dev;
  12309. +
  12310. + /* setup v4l controls */
  12311. + ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
  12312. + if (ret < 0)
  12313. + goto unreg_dev;
  12314. + dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
  12315. +
  12316. + /* mmal init */
  12317. + ret = mmal_init(dev);
  12318. + if (ret < 0)
  12319. + goto unreg_dev;
  12320. +
  12321. + /* initialize queue */
  12322. + q = &dev->capture.vb_vidq;
  12323. + memset(q, 0, sizeof(*q));
  12324. + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  12325. + q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  12326. + q->drv_priv = dev;
  12327. + q->buf_struct_size = sizeof(struct mmal_buffer);
  12328. + q->ops = &bm2835_mmal_video_qops;
  12329. + q->mem_ops = &vb2_vmalloc_memops;
  12330. + q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  12331. + ret = vb2_queue_init(q);
  12332. + if (ret < 0)
  12333. + goto unreg_dev;
  12334. +
  12335. + /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
  12336. + mutex_init(&dev->mutex);
  12337. +
  12338. + /* initialise video devices */
  12339. + ret = bm2835_mmal_init_device(dev, &dev->vdev);
  12340. + if (ret < 0)
  12341. + goto unreg_dev;
  12342. +
  12343. + ret = mmal_setup_components(dev, &default_v4l2_format);
  12344. + if (ret < 0) {
  12345. + v4l2_err(&dev->v4l2_dev,
  12346. + "%s: could not setup components\n", __func__);
  12347. + goto unreg_dev;
  12348. + }
  12349. +
  12350. + v4l2_info(&dev->v4l2_dev,
  12351. + "Broadcom 2835 MMAL video capture ver %s loaded.\n",
  12352. + BM2835_MMAL_VERSION);
  12353. +
  12354. + gdev = dev;
  12355. + return 0;
  12356. +
  12357. +unreg_dev:
  12358. + v4l2_ctrl_handler_free(&dev->ctrl_handler);
  12359. + v4l2_device_unregister(&dev->v4l2_dev);
  12360. +
  12361. +free_dev:
  12362. + kfree(dev);
  12363. +
  12364. + v4l2_err(&dev->v4l2_dev,
  12365. + "%s: error %d while loading driver\n",
  12366. + BM2835_MMAL_MODULE_NAME, ret);
  12367. +
  12368. + return ret;
  12369. +}
  12370. +
  12371. +static void __exit bm2835_mmal_exit(void)
  12372. +{
  12373. + if (!gdev)
  12374. + return;
  12375. +
  12376. + v4l2_info(&gdev->v4l2_dev, "unregistering %s\n",
  12377. + video_device_node_name(&gdev->vdev));
  12378. +
  12379. + video_unregister_device(&gdev->vdev);
  12380. +
  12381. + if (gdev->capture.encode_component) {
  12382. + v4l2_dbg(1, bcm2835_v4l2_debug, &gdev->v4l2_dev,
  12383. + "mmal_exit - disconnect tunnel\n");
  12384. + vchiq_mmal_port_connect_tunnel(gdev->instance,
  12385. + gdev->capture.camera_port, NULL);
  12386. + vchiq_mmal_component_disable(gdev->instance,
  12387. + gdev->capture.encode_component);
  12388. + }
  12389. + vchiq_mmal_component_disable(gdev->instance,
  12390. + gdev->component[MMAL_COMPONENT_CAMERA]);
  12391. +
  12392. + vchiq_mmal_component_finalise(gdev->instance,
  12393. + gdev->
  12394. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  12395. +
  12396. + vchiq_mmal_component_finalise(gdev->instance,
  12397. + gdev->
  12398. + component[MMAL_COMPONENT_IMAGE_ENCODE]);
  12399. +
  12400. + vchiq_mmal_component_finalise(gdev->instance,
  12401. + gdev->component[MMAL_COMPONENT_PREVIEW]);
  12402. +
  12403. + vchiq_mmal_component_finalise(gdev->instance,
  12404. + gdev->component[MMAL_COMPONENT_CAMERA]);
  12405. +
  12406. + vchiq_mmal_finalise(gdev->instance);
  12407. +
  12408. + v4l2_ctrl_handler_free(&gdev->ctrl_handler);
  12409. +
  12410. + v4l2_device_unregister(&gdev->v4l2_dev);
  12411. +
  12412. + kfree(gdev);
  12413. +}
  12414. +
  12415. +module_init(bm2835_mmal_init);
  12416. +module_exit(bm2835_mmal_exit);
  12417. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/bcm2835-camera.h linux-raspberry-pi/drivers/media/platform/bcm2835/bcm2835-camera.h
  12418. --- linux-3.13.6/drivers/media/platform/bcm2835/bcm2835-camera.h 1970-01-01 01:00:00.000000000 +0100
  12419. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/bcm2835-camera.h 2014-03-11 16:52:42.000000000 +0100
  12420. @@ -0,0 +1,123 @@
  12421. +/*
  12422. + * Broadcom BM2835 V4L2 driver
  12423. + *
  12424. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  12425. + *
  12426. + * This file is subject to the terms and conditions of the GNU General Public
  12427. + * License. See the file COPYING in the main directory of this archive
  12428. + * for more details.
  12429. + *
  12430. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  12431. + * Dave Stevenson <dsteve@broadcom.com>
  12432. + * Simon Mellor <simellor@broadcom.com>
  12433. + * Luke Diamand <luked@broadcom.com>
  12434. + *
  12435. + * core driver device
  12436. + */
  12437. +
  12438. +#define V4L2_CTRL_COUNT 25 /* number of v4l controls */
  12439. +
  12440. +enum {
  12441. + MMAL_COMPONENT_CAMERA = 0,
  12442. + MMAL_COMPONENT_PREVIEW,
  12443. + MMAL_COMPONENT_IMAGE_ENCODE,
  12444. + MMAL_COMPONENT_VIDEO_ENCODE,
  12445. + MMAL_COMPONENT_COUNT
  12446. +};
  12447. +
  12448. +enum {
  12449. + MMAL_CAMERA_PORT_PREVIEW = 0,
  12450. + MMAL_CAMERA_PORT_VIDEO,
  12451. + MMAL_CAMERA_PORT_CAPTURE,
  12452. + MMAL_CAMERA_PORT_COUNT
  12453. +};
  12454. +
  12455. +#define PREVIEW_LAYER 2
  12456. +
  12457. +extern int bcm2835_v4l2_debug;
  12458. +
  12459. +struct bm2835_mmal_dev {
  12460. + /* v4l2 devices */
  12461. + struct v4l2_device v4l2_dev;
  12462. + struct video_device vdev;
  12463. + struct mutex mutex;
  12464. +
  12465. + /* controls */
  12466. + struct v4l2_ctrl_handler ctrl_handler;
  12467. + struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
  12468. + enum v4l2_scene_mode scene_mode;
  12469. + struct mmal_colourfx colourfx;
  12470. + int hflip;
  12471. + int vflip;
  12472. + enum mmal_parameter_exposuremode exposure_mode_user;
  12473. + enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
  12474. + /* active exposure mode may differ if selected via a scene mode */
  12475. + enum mmal_parameter_exposuremode exposure_mode_active;
  12476. + enum mmal_parameter_exposuremeteringmode metering_mode;
  12477. + unsigned int manual_shutter_speed;
  12478. + bool exp_auto_priority;
  12479. +
  12480. + /* allocated mmal instance and components */
  12481. + struct vchiq_mmal_instance *instance;
  12482. + struct vchiq_mmal_component *component[MMAL_COMPONENT_COUNT];
  12483. + int camera_use_count;
  12484. +
  12485. + struct v4l2_window overlay;
  12486. +
  12487. + struct {
  12488. + unsigned int width; /* width */
  12489. + unsigned int height; /* height */
  12490. + unsigned int stride; /* stride */
  12491. + struct mmal_fmt *fmt;
  12492. + struct v4l2_fract timeperframe;
  12493. +
  12494. + /* H264 encode bitrate */
  12495. + int encode_bitrate;
  12496. + /* H264 bitrate mode. CBR/VBR */
  12497. + int encode_bitrate_mode;
  12498. + /* H264 profile */
  12499. + enum v4l2_mpeg_video_h264_profile enc_profile;
  12500. + /* H264 level */
  12501. + enum v4l2_mpeg_video_h264_level enc_level;
  12502. + /* JPEG Q-factor */
  12503. + int q_factor;
  12504. +
  12505. + struct vb2_queue vb_vidq;
  12506. +
  12507. + /* VC start timestamp for streaming */
  12508. + s64 vc_start_timestamp;
  12509. + /* Kernel start timestamp for streaming */
  12510. + struct timeval kernel_start_ts;
  12511. +
  12512. + struct vchiq_mmal_port *port; /* port being used for capture */
  12513. + /* camera port being used for capture */
  12514. + struct vchiq_mmal_port *camera_port;
  12515. + /* component being used for encode */
  12516. + struct vchiq_mmal_component *encode_component;
  12517. + /* number of frames remaining which driver should capture */
  12518. + unsigned int frame_count;
  12519. + /* last frame completion */
  12520. + struct completion frame_cmplt;
  12521. +
  12522. + } capture;
  12523. +
  12524. +};
  12525. +
  12526. +int bm2835_mmal_init_controls(
  12527. + struct bm2835_mmal_dev *dev,
  12528. + struct v4l2_ctrl_handler *hdl);
  12529. +
  12530. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
  12531. +int set_framerate_params(struct bm2835_mmal_dev *dev);
  12532. +
  12533. +/* Debug helpers */
  12534. +
  12535. +#define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc) \
  12536. +{ \
  12537. + v4l2_dbg(level, debug, dev, \
  12538. +"%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
  12539. + desc == NULL ? "" : desc, \
  12540. + (pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field, \
  12541. + (pix_fmt)->pixelformat, (pix_fmt)->bytesperline, \
  12542. + (pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
  12543. +}
  12544. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/controls.c linux-raspberry-pi/drivers/media/platform/bcm2835/controls.c
  12545. --- linux-3.13.6/drivers/media/platform/bcm2835/controls.c 1970-01-01 01:00:00.000000000 +0100
  12546. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/controls.c 2014-03-11 16:52:42.000000000 +0100
  12547. @@ -0,0 +1,1278 @@
  12548. +/*
  12549. + * Broadcom BM2835 V4L2 driver
  12550. + *
  12551. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  12552. + *
  12553. + * This file is subject to the terms and conditions of the GNU General Public
  12554. + * License. See the file COPYING in the main directory of this archive
  12555. + * for more details.
  12556. + *
  12557. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  12558. + * Dave Stevenson <dsteve@broadcom.com>
  12559. + * Simon Mellor <simellor@broadcom.com>
  12560. + * Luke Diamand <luked@broadcom.com>
  12561. + */
  12562. +
  12563. +#include <linux/errno.h>
  12564. +#include <linux/kernel.h>
  12565. +#include <linux/module.h>
  12566. +#include <linux/slab.h>
  12567. +#include <media/videobuf2-vmalloc.h>
  12568. +#include <media/v4l2-device.h>
  12569. +#include <media/v4l2-ioctl.h>
  12570. +#include <media/v4l2-ctrls.h>
  12571. +#include <media/v4l2-fh.h>
  12572. +#include <media/v4l2-event.h>
  12573. +#include <media/v4l2-common.h>
  12574. +
  12575. +#include "mmal-common.h"
  12576. +#include "mmal-vchiq.h"
  12577. +#include "mmal-parameters.h"
  12578. +#include "bcm2835-camera.h"
  12579. +
  12580. +/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.
  12581. + * MMAL values are in 1/6th increments so the MMAL range is -24 to +24.
  12582. + * V4L2 docs say value "is expressed in terms of EV, drivers should interpret
  12583. + * the values as 0.001 EV units, where the value 1000 stands for +1 EV."
  12584. + * V4L2 is limited to a max of 32 values in a menu, so count in 1/3rds from
  12585. + * -4 to +4
  12586. + */
  12587. +static const s64 ev_bias_qmenu[] = {
  12588. + -4000, -3667, -3333,
  12589. + -3000, -2667, -2333,
  12590. + -2000, -1667, -1333,
  12591. + -1000, -667, -333,
  12592. + 0, 333, 667,
  12593. + 1000, 1333, 1667,
  12594. + 2000, 2333, 2667,
  12595. + 3000, 3333, 3667,
  12596. + 4000
  12597. +};
  12598. +
  12599. +/* Supported ISO values
  12600. + * ISOO = auto ISO
  12601. + */
  12602. +static const s64 iso_qmenu[] = {
  12603. + 0, 100, 200, 400, 800,
  12604. +};
  12605. +
  12606. +static const s64 mains_freq_qmenu[] = {
  12607. + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
  12608. + V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
  12609. + V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  12610. + V4L2_CID_POWER_LINE_FREQUENCY_AUTO
  12611. +};
  12612. +
  12613. +/* Supported video encode modes */
  12614. +static const s64 bitrate_mode_qmenu[] = {
  12615. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  12616. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
  12617. +};
  12618. +
  12619. +enum bm2835_mmal_ctrl_type {
  12620. + MMAL_CONTROL_TYPE_STD,
  12621. + MMAL_CONTROL_TYPE_STD_MENU,
  12622. + MMAL_CONTROL_TYPE_INT_MENU,
  12623. + MMAL_CONTROL_TYPE_CLUSTER, /* special cluster entry */
  12624. +};
  12625. +
  12626. +struct bm2835_mmal_v4l2_ctrl;
  12627. +
  12628. +typedef int(bm2835_mmal_v4l2_ctrl_cb)(
  12629. + struct bm2835_mmal_dev *dev,
  12630. + struct v4l2_ctrl *ctrl,
  12631. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl);
  12632. +
  12633. +struct bm2835_mmal_v4l2_ctrl {
  12634. + u32 id; /* v4l2 control identifier */
  12635. + enum bm2835_mmal_ctrl_type type;
  12636. + /* control minimum value or
  12637. + * mask for MMAL_CONTROL_TYPE_STD_MENU */
  12638. + s32 min;
  12639. + s32 max; /* maximum value of control */
  12640. + s32 def; /* default value of control */
  12641. + s32 step; /* step size of the control */
  12642. + const s64 *imenu; /* integer menu array */
  12643. + u32 mmal_id; /* mmal parameter id */
  12644. + bm2835_mmal_v4l2_ctrl_cb *setter;
  12645. + bool ignore_errors;
  12646. +};
  12647. +
  12648. +struct v4l2_to_mmal_effects_setting {
  12649. + u32 v4l2_effect;
  12650. + u32 mmal_effect;
  12651. + s32 col_fx_enable;
  12652. + s32 col_fx_fixed_cbcr;
  12653. + u32 u;
  12654. + u32 v;
  12655. + u32 num_effect_params;
  12656. + u32 effect_params[MMAL_MAX_IMAGEFX_PARAMETERS];
  12657. +};
  12658. +
  12659. +static const struct v4l2_to_mmal_effects_setting
  12660. + v4l2_to_mmal_effects_values[] = {
  12661. + { V4L2_COLORFX_NONE, MMAL_PARAM_IMAGEFX_NONE,
  12662. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12663. + { V4L2_COLORFX_BW, MMAL_PARAM_IMAGEFX_NONE,
  12664. + 1, 0, 128, 128, 0, {0, 0, 0, 0, 0} },
  12665. + { V4L2_COLORFX_SEPIA, MMAL_PARAM_IMAGEFX_NONE,
  12666. + 1, 0, 87, 151, 0, {0, 0, 0, 0, 0} },
  12667. + { V4L2_COLORFX_NEGATIVE, MMAL_PARAM_IMAGEFX_NEGATIVE,
  12668. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12669. + { V4L2_COLORFX_EMBOSS, MMAL_PARAM_IMAGEFX_EMBOSS,
  12670. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12671. + { V4L2_COLORFX_SKETCH, MMAL_PARAM_IMAGEFX_SKETCH,
  12672. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12673. + { V4L2_COLORFX_SKY_BLUE, MMAL_PARAM_IMAGEFX_PASTEL,
  12674. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12675. + { V4L2_COLORFX_GRASS_GREEN, MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  12676. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12677. + { V4L2_COLORFX_SKIN_WHITEN, MMAL_PARAM_IMAGEFX_WASHEDOUT,
  12678. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12679. + { V4L2_COLORFX_VIVID, MMAL_PARAM_IMAGEFX_SATURATION,
  12680. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12681. + { V4L2_COLORFX_AQUA, MMAL_PARAM_IMAGEFX_NONE,
  12682. + 1, 0, 171, 121, 0, {0, 0, 0, 0, 0} },
  12683. + { V4L2_COLORFX_ART_FREEZE, MMAL_PARAM_IMAGEFX_HATCH,
  12684. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12685. + { V4L2_COLORFX_SILHOUETTE, MMAL_PARAM_IMAGEFX_FILM,
  12686. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  12687. + { V4L2_COLORFX_SOLARIZATION, MMAL_PARAM_IMAGEFX_SOLARIZE,
  12688. + 0, 0, 0, 0, 5, {1, 128, 160, 160, 48} },
  12689. + { V4L2_COLORFX_ANTIQUE, MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  12690. + 0, 0, 0, 0, 3, {108, 274, 238, 0, 0} },
  12691. + { V4L2_COLORFX_SET_CBCR, MMAL_PARAM_IMAGEFX_NONE,
  12692. + 1, 1, 0, 0, 0, {0, 0, 0, 0, 0} }
  12693. +};
  12694. +
  12695. +struct v4l2_mmal_scene_config {
  12696. + enum v4l2_scene_mode v4l2_scene;
  12697. + enum mmal_parameter_exposuremode exposure_mode;
  12698. + enum mmal_parameter_exposuremeteringmode metering_mode;
  12699. +};
  12700. +
  12701. +static const struct v4l2_mmal_scene_config scene_configs[] = {
  12702. + /* V4L2_SCENE_MODE_NONE automatically added */
  12703. + {
  12704. + V4L2_SCENE_MODE_NIGHT,
  12705. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  12706. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  12707. + },
  12708. + {
  12709. + V4L2_SCENE_MODE_SPORTS,
  12710. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  12711. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  12712. + },
  12713. +};
  12714. +
  12715. +/* control handlers*/
  12716. +
  12717. +static int ctrl_set_rational(struct bm2835_mmal_dev *dev,
  12718. + struct v4l2_ctrl *ctrl,
  12719. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12720. +{
  12721. + struct mmal_parameter_rational rational_value;
  12722. + struct vchiq_mmal_port *control;
  12723. +
  12724. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  12725. +
  12726. + rational_value.num = ctrl->val;
  12727. + rational_value.den = 100;
  12728. +
  12729. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  12730. + mmal_ctrl->mmal_id,
  12731. + &rational_value,
  12732. + sizeof(rational_value));
  12733. +}
  12734. +
  12735. +static int ctrl_set_value(struct bm2835_mmal_dev *dev,
  12736. + struct v4l2_ctrl *ctrl,
  12737. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12738. +{
  12739. + u32 u32_value;
  12740. + struct vchiq_mmal_port *control;
  12741. +
  12742. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  12743. +
  12744. + u32_value = ctrl->val;
  12745. +
  12746. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  12747. + mmal_ctrl->mmal_id,
  12748. + &u32_value, sizeof(u32_value));
  12749. +}
  12750. +
  12751. +static int ctrl_set_value_menu(struct bm2835_mmal_dev *dev,
  12752. + struct v4l2_ctrl *ctrl,
  12753. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12754. +{
  12755. + u32 u32_value;
  12756. + struct vchiq_mmal_port *control;
  12757. +
  12758. + if (ctrl->val > mmal_ctrl->max || ctrl->val < mmal_ctrl->min)
  12759. + return 1;
  12760. +
  12761. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  12762. +
  12763. + u32_value = mmal_ctrl->imenu[ctrl->val];
  12764. +
  12765. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  12766. + mmal_ctrl->mmal_id,
  12767. + &u32_value, sizeof(u32_value));
  12768. +}
  12769. +
  12770. +static int ctrl_set_value_ev(struct bm2835_mmal_dev *dev,
  12771. + struct v4l2_ctrl *ctrl,
  12772. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12773. +{
  12774. + s32 s32_value;
  12775. + struct vchiq_mmal_port *control;
  12776. +
  12777. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  12778. +
  12779. + s32_value = (ctrl->val-12)*2; /* Convert from index to 1/6ths */
  12780. +
  12781. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  12782. + mmal_ctrl->mmal_id,
  12783. + &s32_value, sizeof(s32_value));
  12784. +}
  12785. +
  12786. +static int ctrl_set_rotate(struct bm2835_mmal_dev *dev,
  12787. + struct v4l2_ctrl *ctrl,
  12788. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12789. +{
  12790. + int ret;
  12791. + u32 u32_value;
  12792. + struct vchiq_mmal_component *camera;
  12793. +
  12794. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  12795. +
  12796. + u32_value = ((ctrl->val % 360) / 90) * 90;
  12797. +
  12798. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  12799. + mmal_ctrl->mmal_id,
  12800. + &u32_value, sizeof(u32_value));
  12801. + if (ret < 0)
  12802. + return ret;
  12803. +
  12804. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  12805. + mmal_ctrl->mmal_id,
  12806. + &u32_value, sizeof(u32_value));
  12807. + if (ret < 0)
  12808. + return ret;
  12809. +
  12810. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  12811. + mmal_ctrl->mmal_id,
  12812. + &u32_value, sizeof(u32_value));
  12813. +
  12814. + return ret;
  12815. +}
  12816. +
  12817. +static int ctrl_set_flip(struct bm2835_mmal_dev *dev,
  12818. + struct v4l2_ctrl *ctrl,
  12819. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12820. +{
  12821. + int ret;
  12822. + u32 u32_value;
  12823. + struct vchiq_mmal_component *camera;
  12824. +
  12825. + if (ctrl->id == V4L2_CID_HFLIP)
  12826. + dev->hflip = ctrl->val;
  12827. + else
  12828. + dev->vflip = ctrl->val;
  12829. +
  12830. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  12831. +
  12832. + if (dev->hflip && dev->vflip)
  12833. + u32_value = MMAL_PARAM_MIRROR_BOTH;
  12834. + else if (dev->hflip)
  12835. + u32_value = MMAL_PARAM_MIRROR_HORIZONTAL;
  12836. + else if (dev->vflip)
  12837. + u32_value = MMAL_PARAM_MIRROR_VERTICAL;
  12838. + else
  12839. + u32_value = MMAL_PARAM_MIRROR_NONE;
  12840. +
  12841. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  12842. + mmal_ctrl->mmal_id,
  12843. + &u32_value, sizeof(u32_value));
  12844. + if (ret < 0)
  12845. + return ret;
  12846. +
  12847. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  12848. + mmal_ctrl->mmal_id,
  12849. + &u32_value, sizeof(u32_value));
  12850. + if (ret < 0)
  12851. + return ret;
  12852. +
  12853. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  12854. + mmal_ctrl->mmal_id,
  12855. + &u32_value, sizeof(u32_value));
  12856. +
  12857. + return ret;
  12858. +
  12859. +}
  12860. +
  12861. +static int ctrl_set_exposure(struct bm2835_mmal_dev *dev,
  12862. + struct v4l2_ctrl *ctrl,
  12863. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12864. +{
  12865. + enum mmal_parameter_exposuremode exp_mode = dev->exposure_mode_user;
  12866. + u32 shutter_speed = 0;
  12867. + struct vchiq_mmal_port *control;
  12868. + int ret = 0;
  12869. +
  12870. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  12871. +
  12872. + if (mmal_ctrl->mmal_id == MMAL_PARAMETER_SHUTTER_SPEED) {
  12873. + /* V4L2 is in 100usec increments.
  12874. + * MMAL is 1usec.
  12875. + */
  12876. + dev->manual_shutter_speed = ctrl->val * 100;
  12877. + } else if (mmal_ctrl->mmal_id == MMAL_PARAMETER_EXPOSURE_MODE) {
  12878. + switch (ctrl->val) {
  12879. + case V4L2_EXPOSURE_AUTO:
  12880. + exp_mode = MMAL_PARAM_EXPOSUREMODE_AUTO;
  12881. + break;
  12882. +
  12883. + case V4L2_EXPOSURE_MANUAL:
  12884. + exp_mode = MMAL_PARAM_EXPOSUREMODE_OFF;
  12885. + break;
  12886. + }
  12887. + dev->exposure_mode_user = exp_mode;
  12888. + dev->exposure_mode_v4l2_user = ctrl->val;
  12889. + } else if (mmal_ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
  12890. + dev->exp_auto_priority = ctrl->val;
  12891. + }
  12892. +
  12893. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  12894. + if (exp_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  12895. + shutter_speed = dev->manual_shutter_speed;
  12896. +
  12897. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  12898. + control,
  12899. + MMAL_PARAMETER_SHUTTER_SPEED,
  12900. + &shutter_speed,
  12901. + sizeof(shutter_speed));
  12902. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  12903. + control,
  12904. + MMAL_PARAMETER_EXPOSURE_MODE,
  12905. + &exp_mode,
  12906. + sizeof(u32));
  12907. + dev->exposure_mode_active = exp_mode;
  12908. + }
  12909. + /* exposure_dynamic_framerate (V4L2_CID_EXPOSURE_AUTO_PRIORITY) should
  12910. + * always apply irrespective of scene mode.
  12911. + */
  12912. + ret += set_framerate_params(dev);
  12913. +
  12914. + return ret;
  12915. +}
  12916. +
  12917. +static int ctrl_set_metering_mode(struct bm2835_mmal_dev *dev,
  12918. + struct v4l2_ctrl *ctrl,
  12919. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12920. +{
  12921. + switch (ctrl->val) {
  12922. + case V4L2_EXPOSURE_METERING_AVERAGE:
  12923. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
  12924. + break;
  12925. +
  12926. + case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
  12927. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
  12928. + break;
  12929. +
  12930. + case V4L2_EXPOSURE_METERING_SPOT:
  12931. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
  12932. + break;
  12933. +
  12934. + /* todo matrix weighting not added to Linux API till 3.9
  12935. + case V4L2_EXPOSURE_METERING_MATRIX:
  12936. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
  12937. + break;
  12938. + */
  12939. +
  12940. + }
  12941. +
  12942. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  12943. + struct vchiq_mmal_port *control;
  12944. + u32 u32_value = dev->metering_mode;
  12945. +
  12946. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  12947. +
  12948. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  12949. + mmal_ctrl->mmal_id,
  12950. + &u32_value, sizeof(u32_value));
  12951. + } else
  12952. + return 0;
  12953. +}
  12954. +
  12955. +static int ctrl_set_flicker_avoidance(struct bm2835_mmal_dev *dev,
  12956. + struct v4l2_ctrl *ctrl,
  12957. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12958. +{
  12959. + u32 u32_value;
  12960. + struct vchiq_mmal_port *control;
  12961. +
  12962. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  12963. +
  12964. + switch (ctrl->val) {
  12965. + case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  12966. + u32_value = MMAL_PARAM_FLICKERAVOID_OFF;
  12967. + break;
  12968. + case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  12969. + u32_value = MMAL_PARAM_FLICKERAVOID_50HZ;
  12970. + break;
  12971. + case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  12972. + u32_value = MMAL_PARAM_FLICKERAVOID_60HZ;
  12973. + break;
  12974. + case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
  12975. + u32_value = MMAL_PARAM_FLICKERAVOID_AUTO;
  12976. + break;
  12977. + }
  12978. +
  12979. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  12980. + mmal_ctrl->mmal_id,
  12981. + &u32_value, sizeof(u32_value));
  12982. +}
  12983. +
  12984. +static int ctrl_set_awb_mode(struct bm2835_mmal_dev *dev,
  12985. + struct v4l2_ctrl *ctrl,
  12986. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  12987. +{
  12988. + u32 u32_value;
  12989. + struct vchiq_mmal_port *control;
  12990. +
  12991. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  12992. +
  12993. + switch (ctrl->val) {
  12994. + case V4L2_WHITE_BALANCE_MANUAL:
  12995. + u32_value = MMAL_PARAM_AWBMODE_OFF;
  12996. + break;
  12997. +
  12998. + case V4L2_WHITE_BALANCE_AUTO:
  12999. + u32_value = MMAL_PARAM_AWBMODE_AUTO;
  13000. + break;
  13001. +
  13002. + case V4L2_WHITE_BALANCE_INCANDESCENT:
  13003. + u32_value = MMAL_PARAM_AWBMODE_INCANDESCENT;
  13004. + break;
  13005. +
  13006. + case V4L2_WHITE_BALANCE_FLUORESCENT:
  13007. + u32_value = MMAL_PARAM_AWBMODE_FLUORESCENT;
  13008. + break;
  13009. +
  13010. + case V4L2_WHITE_BALANCE_FLUORESCENT_H:
  13011. + u32_value = MMAL_PARAM_AWBMODE_TUNGSTEN;
  13012. + break;
  13013. +
  13014. + case V4L2_WHITE_BALANCE_HORIZON:
  13015. + u32_value = MMAL_PARAM_AWBMODE_HORIZON;
  13016. + break;
  13017. +
  13018. + case V4L2_WHITE_BALANCE_DAYLIGHT:
  13019. + u32_value = MMAL_PARAM_AWBMODE_SUNLIGHT;
  13020. + break;
  13021. +
  13022. + case V4L2_WHITE_BALANCE_FLASH:
  13023. + u32_value = MMAL_PARAM_AWBMODE_FLASH;
  13024. + break;
  13025. +
  13026. + case V4L2_WHITE_BALANCE_CLOUDY:
  13027. + u32_value = MMAL_PARAM_AWBMODE_CLOUDY;
  13028. + break;
  13029. +
  13030. + case V4L2_WHITE_BALANCE_SHADE:
  13031. + u32_value = MMAL_PARAM_AWBMODE_SHADE;
  13032. + break;
  13033. +
  13034. + }
  13035. +
  13036. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  13037. + mmal_ctrl->mmal_id,
  13038. + &u32_value, sizeof(u32_value));
  13039. +}
  13040. +
  13041. +static int ctrl_set_image_effect(struct bm2835_mmal_dev *dev,
  13042. + struct v4l2_ctrl *ctrl,
  13043. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13044. +{
  13045. + int ret = -EINVAL;
  13046. + int i, j;
  13047. + struct vchiq_mmal_port *control;
  13048. + struct mmal_parameter_imagefx_parameters imagefx;
  13049. +
  13050. + for (i = 0; i < ARRAY_SIZE(v4l2_to_mmal_effects_values); i++) {
  13051. + if (ctrl->val == v4l2_to_mmal_effects_values[i].v4l2_effect) {
  13052. +
  13053. + imagefx.effect =
  13054. + v4l2_to_mmal_effects_values[i].mmal_effect;
  13055. + imagefx.num_effect_params =
  13056. + v4l2_to_mmal_effects_values[i].num_effect_params;
  13057. +
  13058. + if (imagefx.num_effect_params > MMAL_MAX_IMAGEFX_PARAMETERS)
  13059. + imagefx.num_effect_params = MMAL_MAX_IMAGEFX_PARAMETERS;
  13060. +
  13061. + for (j = 0; j < imagefx.num_effect_params; j++)
  13062. + imagefx.effect_parameter[j] =
  13063. + v4l2_to_mmal_effects_values[i].effect_params[j];
  13064. +
  13065. + dev->colourfx.enable =
  13066. + v4l2_to_mmal_effects_values[i].col_fx_enable;
  13067. + if (!v4l2_to_mmal_effects_values[i].col_fx_fixed_cbcr) {
  13068. + dev->colourfx.u =
  13069. + v4l2_to_mmal_effects_values[i].u;
  13070. + dev->colourfx.v =
  13071. + v4l2_to_mmal_effects_values[i].v;
  13072. + }
  13073. +
  13074. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13075. +
  13076. + ret = vchiq_mmal_port_parameter_set(
  13077. + dev->instance, control,
  13078. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  13079. + &imagefx, sizeof(imagefx));
  13080. + if (ret)
  13081. + goto exit;
  13082. +
  13083. + ret = vchiq_mmal_port_parameter_set(
  13084. + dev->instance, control,
  13085. + MMAL_PARAMETER_COLOUR_EFFECT,
  13086. + &dev->colourfx, sizeof(dev->colourfx));
  13087. + }
  13088. + }
  13089. +
  13090. +exit:
  13091. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13092. + "mmal_ctrl:%p ctrl id:0x%x ctrl val:%d imagefx:0x%x color_effect:%s u:%d v:%d ret %d(%d)\n",
  13093. + mmal_ctrl, ctrl->id, ctrl->val, imagefx.effect,
  13094. + dev->colourfx.enable ? "true" : "false",
  13095. + dev->colourfx.u, dev->colourfx.v,
  13096. + ret, (ret == 0 ? 0 : -EINVAL));
  13097. + return (ret == 0 ? 0 : EINVAL);
  13098. +}
  13099. +
  13100. +static int ctrl_set_colfx(struct bm2835_mmal_dev *dev,
  13101. + struct v4l2_ctrl *ctrl,
  13102. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13103. +{
  13104. + int ret = -EINVAL;
  13105. + struct vchiq_mmal_port *control;
  13106. +
  13107. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13108. +
  13109. + dev->colourfx.enable = (ctrl->val & 0xff00) >> 8;
  13110. + dev->colourfx.enable = ctrl->val & 0xff;
  13111. +
  13112. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  13113. + MMAL_PARAMETER_COLOUR_EFFECT,
  13114. + &dev->colourfx, sizeof(dev->colourfx));
  13115. +
  13116. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13117. + "%s: After: mmal_ctrl:%p ctrl id:0x%x ctrl val:%d ret %d(%d)\n",
  13118. + __func__, mmal_ctrl, ctrl->id, ctrl->val, ret,
  13119. + (ret == 0 ? 0 : -EINVAL));
  13120. + return (ret == 0 ? 0 : EINVAL);
  13121. +}
  13122. +
  13123. +static int ctrl_set_bitrate(struct bm2835_mmal_dev *dev,
  13124. + struct v4l2_ctrl *ctrl,
  13125. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13126. +{
  13127. + int ret;
  13128. + struct vchiq_mmal_port *encoder_out;
  13129. +
  13130. + dev->capture.encode_bitrate = ctrl->val;
  13131. +
  13132. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  13133. +
  13134. + ret = vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  13135. + mmal_ctrl->mmal_id,
  13136. + &ctrl->val, sizeof(ctrl->val));
  13137. + ret = 0;
  13138. + return ret;
  13139. +}
  13140. +
  13141. +static int ctrl_set_bitrate_mode(struct bm2835_mmal_dev *dev,
  13142. + struct v4l2_ctrl *ctrl,
  13143. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13144. +{
  13145. + u32 bitrate_mode;
  13146. + struct vchiq_mmal_port *encoder_out;
  13147. +
  13148. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  13149. +
  13150. + dev->capture.encode_bitrate_mode = ctrl->val;
  13151. + switch (ctrl->val) {
  13152. + default:
  13153. + case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
  13154. + bitrate_mode = MMAL_VIDEO_RATECONTROL_VARIABLE;
  13155. + break;
  13156. + case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
  13157. + bitrate_mode = MMAL_VIDEO_RATECONTROL_CONSTANT;
  13158. + break;
  13159. + }
  13160. +
  13161. + vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  13162. + mmal_ctrl->mmal_id,
  13163. + &bitrate_mode,
  13164. + sizeof(bitrate_mode));
  13165. + return 0;
  13166. +}
  13167. +
  13168. +static int ctrl_set_image_encode_output(struct bm2835_mmal_dev *dev,
  13169. + struct v4l2_ctrl *ctrl,
  13170. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13171. +{
  13172. + u32 u32_value;
  13173. + struct vchiq_mmal_port *jpeg_out;
  13174. +
  13175. + jpeg_out = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  13176. +
  13177. + u32_value = ctrl->val;
  13178. +
  13179. + return vchiq_mmal_port_parameter_set(dev->instance, jpeg_out,
  13180. + mmal_ctrl->mmal_id,
  13181. + &u32_value, sizeof(u32_value));
  13182. +}
  13183. +
  13184. +static int ctrl_set_video_encode_param_output(struct bm2835_mmal_dev *dev,
  13185. + struct v4l2_ctrl *ctrl,
  13186. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13187. +{
  13188. + u32 u32_value;
  13189. + struct vchiq_mmal_port *vid_enc_ctl;
  13190. +
  13191. + vid_enc_ctl = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  13192. +
  13193. + u32_value = ctrl->val;
  13194. +
  13195. + return vchiq_mmal_port_parameter_set(dev->instance, vid_enc_ctl,
  13196. + mmal_ctrl->mmal_id,
  13197. + &u32_value, sizeof(u32_value));
  13198. +}
  13199. +
  13200. +static int ctrl_set_video_encode_profile_level(struct bm2835_mmal_dev *dev,
  13201. + struct v4l2_ctrl *ctrl,
  13202. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13203. +{
  13204. + struct mmal_parameter_video_profile param;
  13205. + int ret = 0;
  13206. +
  13207. + if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_PROFILE) {
  13208. + switch (ctrl->val) {
  13209. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  13210. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  13211. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  13212. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  13213. + dev->capture.enc_profile = ctrl->val;
  13214. + break;
  13215. + default:
  13216. + ret = -EINVAL;
  13217. + break;
  13218. + }
  13219. + } else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_LEVEL) {
  13220. + switch (ctrl->val) {
  13221. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  13222. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  13223. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  13224. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  13225. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  13226. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  13227. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  13228. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  13229. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  13230. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  13231. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  13232. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  13233. + dev->capture.enc_level = ctrl->val;
  13234. + break;
  13235. + default:
  13236. + ret = -EINVAL;
  13237. + break;
  13238. + }
  13239. + }
  13240. +
  13241. + if (!ret) {
  13242. + switch (dev->capture.enc_profile) {
  13243. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  13244. + param.profile = MMAL_VIDEO_PROFILE_H264_BASELINE;
  13245. + break;
  13246. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  13247. + param.profile =
  13248. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE;
  13249. + break;
  13250. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  13251. + param.profile = MMAL_VIDEO_PROFILE_H264_MAIN;
  13252. + break;
  13253. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  13254. + param.profile = MMAL_VIDEO_PROFILE_H264_HIGH;
  13255. + break;
  13256. + default:
  13257. + /* Should never get here */
  13258. + break;
  13259. + }
  13260. +
  13261. + switch (dev->capture.enc_level) {
  13262. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  13263. + param.level = MMAL_VIDEO_LEVEL_H264_1;
  13264. + break;
  13265. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  13266. + param.level = MMAL_VIDEO_LEVEL_H264_1b;
  13267. + break;
  13268. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  13269. + param.level = MMAL_VIDEO_LEVEL_H264_11;
  13270. + break;
  13271. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  13272. + param.level = MMAL_VIDEO_LEVEL_H264_12;
  13273. + break;
  13274. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  13275. + param.level = MMAL_VIDEO_LEVEL_H264_13;
  13276. + break;
  13277. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  13278. + param.level = MMAL_VIDEO_LEVEL_H264_2;
  13279. + break;
  13280. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  13281. + param.level = MMAL_VIDEO_LEVEL_H264_21;
  13282. + break;
  13283. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  13284. + param.level = MMAL_VIDEO_LEVEL_H264_22;
  13285. + break;
  13286. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  13287. + param.level = MMAL_VIDEO_LEVEL_H264_3;
  13288. + break;
  13289. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  13290. + param.level = MMAL_VIDEO_LEVEL_H264_31;
  13291. + break;
  13292. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  13293. + param.level = MMAL_VIDEO_LEVEL_H264_32;
  13294. + break;
  13295. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  13296. + param.level = MMAL_VIDEO_LEVEL_H264_4;
  13297. + break;
  13298. + default:
  13299. + /* Should never get here */
  13300. + break;
  13301. + }
  13302. +
  13303. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  13304. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0],
  13305. + mmal_ctrl->mmal_id,
  13306. + &param, sizeof(param));
  13307. + }
  13308. + return ret;
  13309. +}
  13310. +
  13311. +static int ctrl_set_scene_mode(struct bm2835_mmal_dev *dev,
  13312. + struct v4l2_ctrl *ctrl,
  13313. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  13314. +{
  13315. + int ret = 0;
  13316. + int shutter_speed;
  13317. + struct vchiq_mmal_port *control;
  13318. +
  13319. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13320. + "scene mode selected %d, was %d\n", ctrl->val,
  13321. + dev->scene_mode);
  13322. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  13323. +
  13324. + if (ctrl->val == dev->scene_mode)
  13325. + return 0;
  13326. +
  13327. + if (ctrl->val == V4L2_SCENE_MODE_NONE) {
  13328. + /* Restore all user selections */
  13329. + dev->scene_mode = V4L2_SCENE_MODE_NONE;
  13330. +
  13331. + if (dev->exposure_mode_user == MMAL_PARAM_EXPOSUREMODE_OFF)
  13332. + shutter_speed = dev->manual_shutter_speed;
  13333. + else
  13334. + shutter_speed = 0;
  13335. +
  13336. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13337. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  13338. + __func__, shutter_speed, dev->exposure_mode_user,
  13339. + dev->metering_mode);
  13340. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  13341. + control,
  13342. + MMAL_PARAMETER_SHUTTER_SPEED,
  13343. + &shutter_speed,
  13344. + sizeof(shutter_speed));
  13345. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  13346. + control,
  13347. + MMAL_PARAMETER_EXPOSURE_MODE,
  13348. + &dev->exposure_mode_user,
  13349. + sizeof(u32));
  13350. + dev->exposure_mode_active = dev->exposure_mode_user;
  13351. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  13352. + control,
  13353. + MMAL_PARAMETER_EXP_METERING_MODE,
  13354. + &dev->metering_mode,
  13355. + sizeof(u32));
  13356. + ret += set_framerate_params(dev);
  13357. + } else {
  13358. + /* Set up scene mode */
  13359. + int i;
  13360. + const struct v4l2_mmal_scene_config *scene = NULL;
  13361. + int shutter_speed;
  13362. + enum mmal_parameter_exposuremode exposure_mode;
  13363. + enum mmal_parameter_exposuremeteringmode metering_mode;
  13364. +
  13365. + for (i = 0; i < ARRAY_SIZE(scene_configs); i++) {
  13366. + if (scene_configs[i].v4l2_scene ==
  13367. + ctrl->val) {
  13368. + scene = &scene_configs[i];
  13369. + break;
  13370. + }
  13371. + }
  13372. + if (i >= ARRAY_SIZE(scene_configs))
  13373. + return -EINVAL;
  13374. +
  13375. + /* Set all the values */
  13376. + dev->scene_mode = ctrl->val;
  13377. +
  13378. + if (scene->exposure_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  13379. + shutter_speed = dev->manual_shutter_speed;
  13380. + else
  13381. + shutter_speed = 0;
  13382. + exposure_mode = scene->exposure_mode;
  13383. + metering_mode = scene->metering_mode;
  13384. +
  13385. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13386. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  13387. + __func__, shutter_speed, exposure_mode, metering_mode);
  13388. +
  13389. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  13390. + MMAL_PARAMETER_SHUTTER_SPEED,
  13391. + &shutter_speed,
  13392. + sizeof(shutter_speed));
  13393. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  13394. + control,
  13395. + MMAL_PARAMETER_EXPOSURE_MODE,
  13396. + &exposure_mode,
  13397. + sizeof(u32));
  13398. + dev->exposure_mode_active = exposure_mode;
  13399. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  13400. + MMAL_PARAMETER_EXPOSURE_MODE,
  13401. + &exposure_mode,
  13402. + sizeof(u32));
  13403. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  13404. + MMAL_PARAMETER_EXP_METERING_MODE,
  13405. + &metering_mode,
  13406. + sizeof(u32));
  13407. + ret += set_framerate_params(dev);
  13408. + }
  13409. + if (ret) {
  13410. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13411. + "%s: Setting scene to %d, ret=%d\n",
  13412. + __func__, ctrl->val, ret);
  13413. + ret = -EINVAL;
  13414. + }
  13415. + return 0;
  13416. +}
  13417. +
  13418. +static int bm2835_mmal_s_ctrl(struct v4l2_ctrl *ctrl)
  13419. +{
  13420. + struct bm2835_mmal_dev *dev =
  13421. + container_of(ctrl->handler, struct bm2835_mmal_dev,
  13422. + ctrl_handler);
  13423. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl = ctrl->priv;
  13424. + int ret;
  13425. +
  13426. + if ((mmal_ctrl == NULL) ||
  13427. + (mmal_ctrl->id != ctrl->id) ||
  13428. + (mmal_ctrl->setter == NULL)) {
  13429. + pr_warn("mmal_ctrl:%p ctrl id:%d\n", mmal_ctrl, ctrl->id);
  13430. + return -EINVAL;
  13431. + }
  13432. +
  13433. + ret = mmal_ctrl->setter(dev, ctrl, mmal_ctrl);
  13434. + if (ret)
  13435. + pr_warn("ctrl id:%d/MMAL param %08X- returned ret %d\n",
  13436. + ctrl->id, mmal_ctrl->mmal_id, ret);
  13437. + if (mmal_ctrl->ignore_errors)
  13438. + ret = 0;
  13439. + return ret;
  13440. +}
  13441. +
  13442. +static const struct v4l2_ctrl_ops bm2835_mmal_ctrl_ops = {
  13443. + .s_ctrl = bm2835_mmal_s_ctrl,
  13444. +};
  13445. +
  13446. +
  13447. +
  13448. +static const struct bm2835_mmal_v4l2_ctrl v4l2_ctrls[V4L2_CTRL_COUNT] = {
  13449. + {
  13450. + V4L2_CID_SATURATION, MMAL_CONTROL_TYPE_STD,
  13451. + -100, 100, 0, 1, NULL,
  13452. + MMAL_PARAMETER_SATURATION,
  13453. + &ctrl_set_rational,
  13454. + false
  13455. + },
  13456. + {
  13457. + V4L2_CID_SHARPNESS, MMAL_CONTROL_TYPE_STD,
  13458. + -100, 100, 0, 1, NULL,
  13459. + MMAL_PARAMETER_SHARPNESS,
  13460. + &ctrl_set_rational,
  13461. + false
  13462. + },
  13463. + {
  13464. + V4L2_CID_CONTRAST, MMAL_CONTROL_TYPE_STD,
  13465. + -100, 100, 0, 1, NULL,
  13466. + MMAL_PARAMETER_CONTRAST,
  13467. + &ctrl_set_rational,
  13468. + false
  13469. + },
  13470. + {
  13471. + V4L2_CID_BRIGHTNESS, MMAL_CONTROL_TYPE_STD,
  13472. + 0, 100, 50, 1, NULL,
  13473. + MMAL_PARAMETER_BRIGHTNESS,
  13474. + &ctrl_set_rational,
  13475. + false
  13476. + },
  13477. + {
  13478. + V4L2_CID_ISO_SENSITIVITY, MMAL_CONTROL_TYPE_INT_MENU,
  13479. + 0, ARRAY_SIZE(iso_qmenu) - 1, 0, 1, iso_qmenu,
  13480. + MMAL_PARAMETER_ISO,
  13481. + &ctrl_set_value_menu,
  13482. + false
  13483. + },
  13484. + {
  13485. + V4L2_CID_IMAGE_STABILIZATION, MMAL_CONTROL_TYPE_STD,
  13486. + 0, 1, 0, 1, NULL,
  13487. + MMAL_PARAMETER_VIDEO_STABILISATION,
  13488. + &ctrl_set_value,
  13489. + false
  13490. + },
  13491. +/* {
  13492. + 0, MMAL_CONTROL_TYPE_CLUSTER, 3, 1, 0, NULL, 0, NULL
  13493. + },
  13494. +*/ {
  13495. + V4L2_CID_EXPOSURE_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
  13496. + ~0x03, 3, V4L2_EXPOSURE_AUTO, 0, NULL,
  13497. + MMAL_PARAMETER_EXPOSURE_MODE,
  13498. + &ctrl_set_exposure,
  13499. + false
  13500. + },
  13501. +/* todo this needs mixing in with set exposure
  13502. + {
  13503. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  13504. + },
  13505. + */
  13506. + {
  13507. + V4L2_CID_EXPOSURE_ABSOLUTE, MMAL_CONTROL_TYPE_STD,
  13508. + /* Units of 100usecs */
  13509. + 1, 1*1000*10, 100*10, 1, NULL,
  13510. + MMAL_PARAMETER_SHUTTER_SPEED,
  13511. + &ctrl_set_exposure,
  13512. + false
  13513. + },
  13514. + {
  13515. + V4L2_CID_AUTO_EXPOSURE_BIAS, MMAL_CONTROL_TYPE_INT_MENU,
  13516. + 0, ARRAY_SIZE(ev_bias_qmenu) - 1,
  13517. + (ARRAY_SIZE(ev_bias_qmenu)+1)/2 - 1, 0, ev_bias_qmenu,
  13518. + MMAL_PARAMETER_EXPOSURE_COMP,
  13519. + &ctrl_set_value_ev,
  13520. + false
  13521. + },
  13522. + {
  13523. + V4L2_CID_EXPOSURE_AUTO_PRIORITY, MMAL_CONTROL_TYPE_STD,
  13524. + 0, 1,
  13525. + 0, 1, NULL,
  13526. + 0, /* Dummy MMAL ID as it gets mapped into FPS range*/
  13527. + &ctrl_set_exposure,
  13528. + false
  13529. + },
  13530. + {
  13531. + V4L2_CID_EXPOSURE_METERING,
  13532. + MMAL_CONTROL_TYPE_STD_MENU,
  13533. + ~0x7, 2, V4L2_EXPOSURE_METERING_AVERAGE, 0, NULL,
  13534. + MMAL_PARAMETER_EXP_METERING_MODE,
  13535. + &ctrl_set_metering_mode,
  13536. + false
  13537. + },
  13538. + {
  13539. + V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
  13540. + MMAL_CONTROL_TYPE_STD_MENU,
  13541. + ~0x3fe, 9, V4L2_WHITE_BALANCE_AUTO, 0, NULL,
  13542. + MMAL_PARAMETER_AWB_MODE,
  13543. + &ctrl_set_awb_mode,
  13544. + false
  13545. + },
  13546. + {
  13547. + V4L2_CID_COLORFX, MMAL_CONTROL_TYPE_STD_MENU,
  13548. + 0, 15, V4L2_COLORFX_NONE, 0, NULL,
  13549. + MMAL_PARAMETER_IMAGE_EFFECT,
  13550. + &ctrl_set_image_effect,
  13551. + false
  13552. + },
  13553. + {
  13554. + V4L2_CID_COLORFX_CBCR, MMAL_CONTROL_TYPE_STD,
  13555. + 0, 0xffff, 0x8080, 1, NULL,
  13556. + MMAL_PARAMETER_COLOUR_EFFECT,
  13557. + &ctrl_set_colfx,
  13558. + false
  13559. + },
  13560. + {
  13561. + V4L2_CID_ROTATE, MMAL_CONTROL_TYPE_STD,
  13562. + 0, 360, 0, 90, NULL,
  13563. + MMAL_PARAMETER_ROTATION,
  13564. + &ctrl_set_rotate,
  13565. + false
  13566. + },
  13567. + {
  13568. + V4L2_CID_HFLIP, MMAL_CONTROL_TYPE_STD,
  13569. + 0, 1, 0, 1, NULL,
  13570. + MMAL_PARAMETER_MIRROR,
  13571. + &ctrl_set_flip,
  13572. + false
  13573. + },
  13574. + {
  13575. + V4L2_CID_VFLIP, MMAL_CONTROL_TYPE_STD,
  13576. + 0, 1, 0, 1, NULL,
  13577. + MMAL_PARAMETER_MIRROR,
  13578. + &ctrl_set_flip,
  13579. + false
  13580. + },
  13581. + {
  13582. + V4L2_CID_MPEG_VIDEO_BITRATE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  13583. + 0, ARRAY_SIZE(bitrate_mode_qmenu) - 1,
  13584. + 0, 0, bitrate_mode_qmenu,
  13585. + MMAL_PARAMETER_RATECONTROL,
  13586. + &ctrl_set_bitrate_mode,
  13587. + false
  13588. + },
  13589. + {
  13590. + V4L2_CID_MPEG_VIDEO_BITRATE, MMAL_CONTROL_TYPE_STD,
  13591. + 25*1000, 25*1000*1000, 10*1000*1000, 25*1000, NULL,
  13592. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  13593. + &ctrl_set_bitrate,
  13594. + false
  13595. + },
  13596. + {
  13597. + V4L2_CID_JPEG_COMPRESSION_QUALITY, MMAL_CONTROL_TYPE_STD,
  13598. + 1, 100,
  13599. + 30, 1, NULL,
  13600. + MMAL_PARAMETER_JPEG_Q_FACTOR,
  13601. + &ctrl_set_image_encode_output,
  13602. + false
  13603. + },
  13604. + {
  13605. + V4L2_CID_POWER_LINE_FREQUENCY, MMAL_CONTROL_TYPE_STD_MENU,
  13606. + 0, ARRAY_SIZE(mains_freq_qmenu) - 1,
  13607. + 1, 1, NULL,
  13608. + MMAL_PARAMETER_FLICKER_AVOID,
  13609. + &ctrl_set_flicker_avoidance,
  13610. + false
  13611. + },
  13612. + {
  13613. + V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, MMAL_CONTROL_TYPE_STD,
  13614. + 0, 1,
  13615. + 0, 1, NULL,
  13616. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
  13617. + &ctrl_set_video_encode_param_output,
  13618. + true /* Errors ignored as requires latest firmware to work */
  13619. + },
  13620. + {
  13621. + V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  13622. + MMAL_CONTROL_TYPE_STD_MENU,
  13623. + ~((1<<V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  13624. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
  13625. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  13626. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
  13627. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  13628. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 1, NULL,
  13629. + MMAL_PARAMETER_PROFILE,
  13630. + &ctrl_set_video_encode_profile_level,
  13631. + false
  13632. + },
  13633. + {
  13634. + V4L2_CID_MPEG_VIDEO_H264_LEVEL, MMAL_CONTROL_TYPE_STD_MENU,
  13635. + ~((1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
  13636. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1B) |
  13637. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_1) |
  13638. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_2) |
  13639. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_3) |
  13640. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  13641. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_1) |
  13642. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_2) |
  13643. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  13644. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  13645. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  13646. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
  13647. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
  13648. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0, 1, NULL,
  13649. + MMAL_PARAMETER_PROFILE,
  13650. + &ctrl_set_video_encode_profile_level,
  13651. + false
  13652. + },
  13653. + {
  13654. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  13655. + -1, /* Min is computed at runtime */
  13656. + V4L2_SCENE_MODE_TEXT,
  13657. + V4L2_SCENE_MODE_NONE, 1, NULL,
  13658. + MMAL_PARAMETER_PROFILE,
  13659. + &ctrl_set_scene_mode,
  13660. + false
  13661. + },
  13662. +};
  13663. +
  13664. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev)
  13665. +{
  13666. + int c;
  13667. + int ret = 0;
  13668. +
  13669. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  13670. + if ((dev->ctrls[c]) && (v4l2_ctrls[c].setter)) {
  13671. + ret = v4l2_ctrls[c].setter(dev, dev->ctrls[c],
  13672. + &v4l2_ctrls[c]);
  13673. + if (!v4l2_ctrls[c].ignore_errors && ret) {
  13674. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13675. + "Failed when setting default values for ctrl %d\n",
  13676. + c);
  13677. + break;
  13678. + }
  13679. + }
  13680. + }
  13681. + return ret;
  13682. +}
  13683. +
  13684. +int set_framerate_params(struct bm2835_mmal_dev *dev)
  13685. +{
  13686. + struct mmal_parameter_fps_range fps_range;
  13687. + int ret;
  13688. +
  13689. + if ((dev->exposure_mode_active != MMAL_PARAM_EXPOSUREMODE_OFF) &&
  13690. + (dev->exp_auto_priority)) {
  13691. + /* Variable FPS. Define min FPS as 1fps.
  13692. + * Max as max defined FPS.
  13693. + */
  13694. + fps_range.fps_low.num = 1;
  13695. + fps_range.fps_low.den = 1;
  13696. + fps_range.fps_high.num = dev->capture.timeperframe.denominator;
  13697. + fps_range.fps_high.den = dev->capture.timeperframe.numerator;
  13698. + } else {
  13699. + /* Fixed FPS - set min and max to be the same */
  13700. + fps_range.fps_low.num = fps_range.fps_high.num =
  13701. + dev->capture.timeperframe.denominator;
  13702. + fps_range.fps_low.den = fps_range.fps_high.den =
  13703. + dev->capture.timeperframe.numerator;
  13704. + }
  13705. +
  13706. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13707. + "Set fps range to %d/%d to %d/%d\n",
  13708. + fps_range.fps_low.num,
  13709. + fps_range.fps_low.den,
  13710. + fps_range.fps_high.num,
  13711. + fps_range.fps_high.den
  13712. + );
  13713. +
  13714. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  13715. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13716. + output[MMAL_CAMERA_PORT_PREVIEW],
  13717. + MMAL_PARAMETER_FPS_RANGE,
  13718. + &fps_range, sizeof(fps_range));
  13719. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  13720. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13721. + output[MMAL_CAMERA_PORT_VIDEO],
  13722. + MMAL_PARAMETER_FPS_RANGE,
  13723. + &fps_range, sizeof(fps_range));
  13724. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  13725. + &dev->component[MMAL_COMPONENT_CAMERA]->
  13726. + output[MMAL_CAMERA_PORT_CAPTURE],
  13727. + MMAL_PARAMETER_FPS_RANGE,
  13728. + &fps_range, sizeof(fps_range));
  13729. + if (ret)
  13730. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  13731. + "Failed to set fps ret %d\n",
  13732. + ret);
  13733. +
  13734. + return ret;
  13735. +
  13736. +}
  13737. +
  13738. +int bm2835_mmal_init_controls(struct bm2835_mmal_dev *dev,
  13739. + struct v4l2_ctrl_handler *hdl)
  13740. +{
  13741. + int c;
  13742. + const struct bm2835_mmal_v4l2_ctrl *ctrl;
  13743. +
  13744. + v4l2_ctrl_handler_init(hdl, V4L2_CTRL_COUNT);
  13745. +
  13746. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  13747. + ctrl = &v4l2_ctrls[c];
  13748. +
  13749. + switch (ctrl->type) {
  13750. + case MMAL_CONTROL_TYPE_STD:
  13751. + dev->ctrls[c] = v4l2_ctrl_new_std(hdl,
  13752. + &bm2835_mmal_ctrl_ops, ctrl->id,
  13753. + ctrl->min, ctrl->max, ctrl->step, ctrl->def);
  13754. + break;
  13755. +
  13756. + case MMAL_CONTROL_TYPE_STD_MENU:
  13757. + {
  13758. + int mask = ctrl->min;
  13759. +
  13760. + if (ctrl->id == V4L2_CID_SCENE_MODE) {
  13761. + /* Special handling to work out the mask
  13762. + * value based on the scene_configs array
  13763. + * at runtime. Reduces the chance of
  13764. + * mismatches.
  13765. + */
  13766. + int i;
  13767. + mask = 1<<V4L2_SCENE_MODE_NONE;
  13768. + for (i = 0;
  13769. + i < ARRAY_SIZE(scene_configs);
  13770. + i++) {
  13771. + mask |= 1<<scene_configs[i].v4l2_scene;
  13772. + }
  13773. + mask = ~mask;
  13774. + }
  13775. +
  13776. + dev->ctrls[c] = v4l2_ctrl_new_std_menu(hdl,
  13777. + &bm2835_mmal_ctrl_ops, ctrl->id,
  13778. + ctrl->max, mask, ctrl->def);
  13779. + break;
  13780. + }
  13781. +
  13782. + case MMAL_CONTROL_TYPE_INT_MENU:
  13783. + dev->ctrls[c] = v4l2_ctrl_new_int_menu(hdl,
  13784. + &bm2835_mmal_ctrl_ops, ctrl->id,
  13785. + ctrl->max, ctrl->def, ctrl->imenu);
  13786. + break;
  13787. +
  13788. + case MMAL_CONTROL_TYPE_CLUSTER:
  13789. + /* skip this entry when constructing controls */
  13790. + continue;
  13791. + }
  13792. +
  13793. + if (hdl->error)
  13794. + break;
  13795. +
  13796. + dev->ctrls[c]->priv = (void *)ctrl;
  13797. + }
  13798. +
  13799. + if (hdl->error) {
  13800. + pr_err("error adding control %d/%d id 0x%x\n", c,
  13801. + V4L2_CTRL_COUNT, ctrl->id);
  13802. + return hdl->error;
  13803. + }
  13804. +
  13805. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  13806. + ctrl = &v4l2_ctrls[c];
  13807. +
  13808. + switch (ctrl->type) {
  13809. + case MMAL_CONTROL_TYPE_CLUSTER:
  13810. + v4l2_ctrl_auto_cluster(ctrl->min,
  13811. + &dev->ctrls[c+1],
  13812. + ctrl->max,
  13813. + ctrl->def);
  13814. + break;
  13815. +
  13816. + case MMAL_CONTROL_TYPE_STD:
  13817. + case MMAL_CONTROL_TYPE_STD_MENU:
  13818. + case MMAL_CONTROL_TYPE_INT_MENU:
  13819. + break;
  13820. + }
  13821. +
  13822. + }
  13823. +
  13824. + return 0;
  13825. +}
  13826. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/Kconfig linux-raspberry-pi/drivers/media/platform/bcm2835/Kconfig
  13827. --- linux-3.13.6/drivers/media/platform/bcm2835/Kconfig 1970-01-01 01:00:00.000000000 +0100
  13828. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/Kconfig 2014-03-11 16:52:42.000000000 +0100
  13829. @@ -0,0 +1,25 @@
  13830. +# Broadcom VideoCore IV v4l2 camera support
  13831. +
  13832. +config VIDEO_BCM2835
  13833. + bool "Broadcom BCM2835 camera interface driver"
  13834. + depends on VIDEO_V4L2 && ARCH_BCM2708
  13835. + ---help---
  13836. + Say Y here to enable camera host interface devices for
  13837. + Broadcom BCM2835 SoC. This operates over the VCHIQ interface
  13838. + to a service running on VideoCore.
  13839. +
  13840. +
  13841. +if VIDEO_BCM2835
  13842. +
  13843. +config VIDEO_BCM2835_MMAL
  13844. + tristate "Broadcom BM2835 MMAL camera interface driver"
  13845. + depends on BCM2708_VCHIQ
  13846. + select VIDEOBUF2_VMALLOC
  13847. + ---help---
  13848. + This is a V4L2 driver for the Broadcom BCM2835 MMAL camera host interface
  13849. +
  13850. + To compile this driver as a module, choose M here: the
  13851. + module will be called bcm2835-v4l2.o
  13852. +
  13853. +
  13854. +endif # VIDEO_BM2835
  13855. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/Makefile linux-raspberry-pi/drivers/media/platform/bcm2835/Makefile
  13856. --- linux-3.13.6/drivers/media/platform/bcm2835/Makefile 1970-01-01 01:00:00.000000000 +0100
  13857. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/Makefile 2014-03-11 16:52:42.000000000 +0100
  13858. @@ -0,0 +1,5 @@
  13859. +bcm2835-v4l2-objs := bcm2835-camera.o controls.o mmal-vchiq.o
  13860. +
  13861. +obj-$(CONFIG_VIDEO_BCM2835_MMAL) += bcm2835-v4l2.o
  13862. +
  13863. +ccflags-$(CONFIG_VIDEO_BCM2835) += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  13864. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-common.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-common.h
  13865. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-common.h 1970-01-01 01:00:00.000000000 +0100
  13866. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-common.h 2014-03-11 16:52:42.000000000 +0100
  13867. @@ -0,0 +1,53 @@
  13868. +/*
  13869. + * Broadcom BM2835 V4L2 driver
  13870. + *
  13871. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  13872. + *
  13873. + * This file is subject to the terms and conditions of the GNU General Public
  13874. + * License. See the file COPYING in the main directory of this archive
  13875. + * for more details.
  13876. + *
  13877. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  13878. + * Dave Stevenson <dsteve@broadcom.com>
  13879. + * Simon Mellor <simellor@broadcom.com>
  13880. + * Luke Diamand <luked@broadcom.com>
  13881. + *
  13882. + * MMAL structures
  13883. + *
  13884. + */
  13885. +
  13886. +#define MMAL_FOURCC(a, b, c, d) ((a) | (b << 8) | (c << 16) | (d << 24))
  13887. +#define MMAL_MAGIC MMAL_FOURCC('m', 'm', 'a', 'l')
  13888. +
  13889. +/** Special value signalling that time is not known */
  13890. +#define MMAL_TIME_UNKNOWN (1LL<<63)
  13891. +
  13892. +/* mapping between v4l and mmal video modes */
  13893. +struct mmal_fmt {
  13894. + char *name;
  13895. + u32 fourcc; /* v4l2 format id */
  13896. + int flags; /* v4l2 flags field */
  13897. + u32 mmal;
  13898. + int depth;
  13899. + u32 mmal_component; /* MMAL component index to be used to encode */
  13900. +};
  13901. +
  13902. +/* buffer for one video frame */
  13903. +struct mmal_buffer {
  13904. + /* v4l buffer data -- must be first */
  13905. + struct vb2_buffer vb;
  13906. +
  13907. + /* list of buffers available */
  13908. + struct list_head list;
  13909. +
  13910. + void *buffer; /* buffer pointer */
  13911. + unsigned long buffer_size; /* size of allocated buffer */
  13912. +};
  13913. +
  13914. +/* */
  13915. +struct mmal_colourfx {
  13916. + s32 enable;
  13917. + u32 u;
  13918. + u32 v;
  13919. +};
  13920. +
  13921. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-encodings.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-encodings.h
  13922. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-encodings.h 1970-01-01 01:00:00.000000000 +0100
  13923. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-encodings.h 2014-03-11 16:52:42.000000000 +0100
  13924. @@ -0,0 +1,94 @@
  13925. +/*
  13926. + * Broadcom BM2835 V4L2 driver
  13927. + *
  13928. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  13929. + *
  13930. + * This file is subject to the terms and conditions of the GNU General Public
  13931. + * License. See the file COPYING in the main directory of this archive
  13932. + * for more details.
  13933. + *
  13934. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  13935. + * Dave Stevenson <dsteve@broadcom.com>
  13936. + * Simon Mellor <simellor@broadcom.com>
  13937. + * Luke Diamand <luked@broadcom.com>
  13938. + */
  13939. +
  13940. +#define MMAL_ENCODING_H264 MMAL_FOURCC('H', '2', '6', '4')
  13941. +#define MMAL_ENCODING_H263 MMAL_FOURCC('H', '2', '6', '3')
  13942. +#define MMAL_ENCODING_MP4V MMAL_FOURCC('M', 'P', '4', 'V')
  13943. +#define MMAL_ENCODING_MP2V MMAL_FOURCC('M', 'P', '2', 'V')
  13944. +#define MMAL_ENCODING_MP1V MMAL_FOURCC('M', 'P', '1', 'V')
  13945. +#define MMAL_ENCODING_WMV3 MMAL_FOURCC('W', 'M', 'V', '3')
  13946. +#define MMAL_ENCODING_WMV2 MMAL_FOURCC('W', 'M', 'V', '2')
  13947. +#define MMAL_ENCODING_WMV1 MMAL_FOURCC('W', 'M', 'V', '1')
  13948. +#define MMAL_ENCODING_WVC1 MMAL_FOURCC('W', 'V', 'C', '1')
  13949. +#define MMAL_ENCODING_VP8 MMAL_FOURCC('V', 'P', '8', ' ')
  13950. +#define MMAL_ENCODING_VP7 MMAL_FOURCC('V', 'P', '7', ' ')
  13951. +#define MMAL_ENCODING_VP6 MMAL_FOURCC('V', 'P', '6', ' ')
  13952. +#define MMAL_ENCODING_THEORA MMAL_FOURCC('T', 'H', 'E', 'O')
  13953. +#define MMAL_ENCODING_SPARK MMAL_FOURCC('S', 'P', 'R', 'K')
  13954. +#define MMAL_ENCODING_MJPEG MMAL_FOURCC('M', 'J', 'P', 'G')
  13955. +
  13956. +#define MMAL_ENCODING_JPEG MMAL_FOURCC('J', 'P', 'E', 'G')
  13957. +#define MMAL_ENCODING_GIF MMAL_FOURCC('G', 'I', 'F', ' ')
  13958. +#define MMAL_ENCODING_PNG MMAL_FOURCC('P', 'N', 'G', ' ')
  13959. +#define MMAL_ENCODING_PPM MMAL_FOURCC('P', 'P', 'M', ' ')
  13960. +#define MMAL_ENCODING_TGA MMAL_FOURCC('T', 'G', 'A', ' ')
  13961. +#define MMAL_ENCODING_BMP MMAL_FOURCC('B', 'M', 'P', ' ')
  13962. +
  13963. +#define MMAL_ENCODING_I420 MMAL_FOURCC('I', '4', '2', '0')
  13964. +#define MMAL_ENCODING_I420_SLICE MMAL_FOURCC('S', '4', '2', '0')
  13965. +#define MMAL_ENCODING_YV12 MMAL_FOURCC('Y', 'V', '1', '2')
  13966. +#define MMAL_ENCODING_I422 MMAL_FOURCC('I', '4', '2', '2')
  13967. +#define MMAL_ENCODING_I422_SLICE MMAL_FOURCC('S', '4', '2', '2')
  13968. +#define MMAL_ENCODING_YUYV MMAL_FOURCC('Y', 'U', 'Y', 'V')
  13969. +#define MMAL_ENCODING_YVYU MMAL_FOURCC('Y', 'V', 'Y', 'U')
  13970. +#define MMAL_ENCODING_UYVY MMAL_FOURCC('U', 'Y', 'V', 'Y')
  13971. +#define MMAL_ENCODING_VYUY MMAL_FOURCC('V', 'Y', 'U', 'Y')
  13972. +#define MMAL_ENCODING_NV12 MMAL_FOURCC('N', 'V', '1', '2')
  13973. +#define MMAL_ENCODING_NV21 MMAL_FOURCC('N', 'V', '2', '1')
  13974. +#define MMAL_ENCODING_ARGB MMAL_FOURCC('A', 'R', 'G', 'B')
  13975. +#define MMAL_ENCODING_RGBA MMAL_FOURCC('R', 'G', 'B', 'A')
  13976. +#define MMAL_ENCODING_ABGR MMAL_FOURCC('A', 'B', 'G', 'R')
  13977. +#define MMAL_ENCODING_BGRA MMAL_FOURCC('B', 'G', 'R', 'A')
  13978. +#define MMAL_ENCODING_RGB16 MMAL_FOURCC('R', 'G', 'B', '2')
  13979. +#define MMAL_ENCODING_RGB24 MMAL_FOURCC('R', 'G', 'B', '3')
  13980. +#define MMAL_ENCODING_RGB32 MMAL_FOURCC('R', 'G', 'B', '4')
  13981. +#define MMAL_ENCODING_BGR16 MMAL_FOURCC('B', 'G', 'R', '2')
  13982. +#define MMAL_ENCODING_BGR24 MMAL_FOURCC('B', 'G', 'R', '3')
  13983. +#define MMAL_ENCODING_BGR32 MMAL_FOURCC('B', 'G', 'R', '4')
  13984. +
  13985. +/** SAND Video (YUVUV128) format, native format understood by VideoCore.
  13986. + * This format is *not* opaque - if requested you will receive full frames
  13987. + * of YUV_UV video.
  13988. + */
  13989. +#define MMAL_ENCODING_YUVUV128 MMAL_FOURCC('S', 'A', 'N', 'D')
  13990. +
  13991. +/** VideoCore opaque image format, image handles are returned to
  13992. + * the host but not the actual image data.
  13993. + */
  13994. +#define MMAL_ENCODING_OPAQUE MMAL_FOURCC('O', 'P', 'Q', 'V')
  13995. +
  13996. +/** An EGL image handle
  13997. + */
  13998. +#define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
  13999. +
  14000. +/* }@ */
  14001. +
  14002. +/** \name Pre-defined audio encodings */
  14003. +/* @{ */
  14004. +#define MMAL_ENCODING_PCM_UNSIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'U')
  14005. +#define MMAL_ENCODING_PCM_UNSIGNED_LE MMAL_FOURCC('p', 'c', 'm', 'u')
  14006. +#define MMAL_ENCODING_PCM_SIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'S')
  14007. +#define MMAL_ENCODING_PCM_SIGNED_LE MMAL_FOURCC('p', 'c', 'm', 's')
  14008. +#define MMAL_ENCODING_PCM_FLOAT_BE MMAL_FOURCC('P', 'C', 'M', 'F')
  14009. +#define MMAL_ENCODING_PCM_FLOAT_LE MMAL_FOURCC('p', 'c', 'm', 'f')
  14010. +
  14011. +/* Pre-defined H264 encoding variants */
  14012. +
  14013. +/** ISO 14496-10 Annex B byte stream format */
  14014. +#define MMAL_ENCODING_VARIANT_H264_DEFAULT 0
  14015. +/** ISO 14496-15 AVC stream format */
  14016. +#define MMAL_ENCODING_VARIANT_H264_AVC1 MMAL_FOURCC('A', 'V', 'C', '1')
  14017. +/** Implicitly delineated NAL units without emulation prevention */
  14018. +#define MMAL_ENCODING_VARIANT_H264_RAW MMAL_FOURCC('R', 'A', 'W', ' ')
  14019. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-msg-common.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-common.h
  14020. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-msg-common.h 1970-01-01 01:00:00.000000000 +0100
  14021. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-common.h 2014-03-11 16:52:42.000000000 +0100
  14022. @@ -0,0 +1,50 @@
  14023. +/*
  14024. + * Broadcom BM2835 V4L2 driver
  14025. + *
  14026. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14027. + *
  14028. + * This file is subject to the terms and conditions of the GNU General Public
  14029. + * License. See the file COPYING in the main directory of this archive
  14030. + * for more details.
  14031. + *
  14032. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14033. + * Dave Stevenson <dsteve@broadcom.com>
  14034. + * Simon Mellor <simellor@broadcom.com>
  14035. + * Luke Diamand <luked@broadcom.com>
  14036. + */
  14037. +
  14038. +#ifndef MMAL_MSG_COMMON_H
  14039. +#define MMAL_MSG_COMMON_H
  14040. +
  14041. +enum mmal_msg_status {
  14042. + MMAL_MSG_STATUS_SUCCESS = 0, /**< Success */
  14043. + MMAL_MSG_STATUS_ENOMEM, /**< Out of memory */
  14044. + MMAL_MSG_STATUS_ENOSPC, /**< Out of resources other than memory */
  14045. + MMAL_MSG_STATUS_EINVAL, /**< Argument is invalid */
  14046. + MMAL_MSG_STATUS_ENOSYS, /**< Function not implemented */
  14047. + MMAL_MSG_STATUS_ENOENT, /**< No such file or directory */
  14048. + MMAL_MSG_STATUS_ENXIO, /**< No such device or address */
  14049. + MMAL_MSG_STATUS_EIO, /**< I/O error */
  14050. + MMAL_MSG_STATUS_ESPIPE, /**< Illegal seek */
  14051. + MMAL_MSG_STATUS_ECORRUPT, /**< Data is corrupt \attention */
  14052. + MMAL_MSG_STATUS_ENOTREADY, /**< Component is not ready */
  14053. + MMAL_MSG_STATUS_ECONFIG, /**< Component is not configured */
  14054. + MMAL_MSG_STATUS_EISCONN, /**< Port is already connected */
  14055. + MMAL_MSG_STATUS_ENOTCONN, /**< Port is disconnected */
  14056. + MMAL_MSG_STATUS_EAGAIN, /**< Resource temporarily unavailable. */
  14057. + MMAL_MSG_STATUS_EFAULT, /**< Bad address */
  14058. +};
  14059. +
  14060. +struct mmal_rect {
  14061. + s32 x; /**< x coordinate (from left) */
  14062. + s32 y; /**< y coordinate (from top) */
  14063. + s32 width; /**< width */
  14064. + s32 height; /**< height */
  14065. +};
  14066. +
  14067. +struct mmal_rational {
  14068. + s32 num; /**< Numerator */
  14069. + s32 den; /**< Denominator */
  14070. +};
  14071. +
  14072. +#endif /* MMAL_MSG_COMMON_H */
  14073. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-msg-format.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-format.h
  14074. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-msg-format.h 1970-01-01 01:00:00.000000000 +0100
  14075. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-format.h 2014-03-11 16:52:42.000000000 +0100
  14076. @@ -0,0 +1,81 @@
  14077. +/*
  14078. + * Broadcom BM2835 V4L2 driver
  14079. + *
  14080. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14081. + *
  14082. + * This file is subject to the terms and conditions of the GNU General Public
  14083. + * License. See the file COPYING in the main directory of this archive
  14084. + * for more details.
  14085. + *
  14086. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14087. + * Dave Stevenson <dsteve@broadcom.com>
  14088. + * Simon Mellor <simellor@broadcom.com>
  14089. + * Luke Diamand <luked@broadcom.com>
  14090. + */
  14091. +
  14092. +#ifndef MMAL_MSG_FORMAT_H
  14093. +#define MMAL_MSG_FORMAT_H
  14094. +
  14095. +#include "mmal-msg-common.h"
  14096. +
  14097. +/* MMAL_ES_FORMAT_T */
  14098. +
  14099. +
  14100. +struct mmal_audio_format {
  14101. + u32 channels; /**< Number of audio channels */
  14102. + u32 sample_rate; /**< Sample rate */
  14103. +
  14104. + u32 bits_per_sample; /**< Bits per sample */
  14105. + u32 block_align; /**< Size of a block of data */
  14106. +};
  14107. +
  14108. +struct mmal_video_format {
  14109. + u32 width; /**< Width of frame in pixels */
  14110. + u32 height; /**< Height of frame in rows of pixels */
  14111. + struct mmal_rect crop; /**< Visible region of the frame */
  14112. + struct mmal_rational frame_rate; /**< Frame rate */
  14113. + struct mmal_rational par; /**< Pixel aspect ratio */
  14114. +
  14115. + /* FourCC specifying the color space of the video stream. See the
  14116. + * \ref MmalColorSpace "pre-defined color spaces" for some examples.
  14117. + */
  14118. + u32 color_space;
  14119. +};
  14120. +
  14121. +struct mmal_subpicture_format {
  14122. + u32 x_offset;
  14123. + u32 y_offset;
  14124. +};
  14125. +
  14126. +union mmal_es_specific_format {
  14127. + struct mmal_audio_format audio;
  14128. + struct mmal_video_format video;
  14129. + struct mmal_subpicture_format subpicture;
  14130. +};
  14131. +
  14132. +/** Definition of an elementary stream format (MMAL_ES_FORMAT_T) */
  14133. +struct mmal_es_format {
  14134. + u32 type; /* enum mmal_es_type */
  14135. +
  14136. + u32 encoding; /* FourCC specifying encoding of the elementary stream.*/
  14137. + u32 encoding_variant; /* FourCC specifying the specific
  14138. + * encoding variant of the elementary
  14139. + * stream.
  14140. + */
  14141. +
  14142. + union mmal_es_specific_format *es; /* TODO: pointers in
  14143. + * message serialisation?!?
  14144. + */
  14145. + /* Type specific
  14146. + * information for the
  14147. + * elementary stream
  14148. + */
  14149. +
  14150. + u32 bitrate; /**< Bitrate in bits per second */
  14151. + u32 flags; /**< Flags describing properties of the elementary stream. */
  14152. +
  14153. + u32 extradata_size; /**< Size of the codec specific data */
  14154. + u8 *extradata; /**< Codec specific data */
  14155. +};
  14156. +
  14157. +#endif /* MMAL_MSG_FORMAT_H */
  14158. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-msg.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg.h
  14159. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-msg.h 1970-01-01 01:00:00.000000000 +0100
  14160. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg.h 2014-03-11 16:52:42.000000000 +0100
  14161. @@ -0,0 +1,404 @@
  14162. +/*
  14163. + * Broadcom BM2835 V4L2 driver
  14164. + *
  14165. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14166. + *
  14167. + * This file is subject to the terms and conditions of the GNU General Public
  14168. + * License. See the file COPYING in the main directory of this archive
  14169. + * for more details.
  14170. + *
  14171. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14172. + * Dave Stevenson <dsteve@broadcom.com>
  14173. + * Simon Mellor <simellor@broadcom.com>
  14174. + * Luke Diamand <luked@broadcom.com>
  14175. + */
  14176. +
  14177. +/* all the data structures which serialise the MMAL protocol. note
  14178. + * these are directly mapped onto the recived message data.
  14179. + *
  14180. + * BEWARE: They seem to *assume* pointers are u32 and that there is no
  14181. + * structure padding!
  14182. + *
  14183. + * NOTE: this implementation uses kernel types to ensure sizes. Rather
  14184. + * than assigning values to enums to force their size the
  14185. + * implementation uses fixed size types and not the enums (though the
  14186. + * comments have the actual enum type
  14187. + */
  14188. +
  14189. +#define VC_MMAL_VER 15
  14190. +#define VC_MMAL_MIN_VER 10
  14191. +#define VC_MMAL_SERVER_NAME MAKE_FOURCC("mmal")
  14192. +
  14193. +/* max total message size is 512 bytes */
  14194. +#define MMAL_MSG_MAX_SIZE 512
  14195. +/* with six 32bit header elements max payload is therefore 488 bytes */
  14196. +#define MMAL_MSG_MAX_PAYLOAD 488
  14197. +
  14198. +#include "mmal-msg-common.h"
  14199. +#include "mmal-msg-format.h"
  14200. +#include "mmal-msg-port.h"
  14201. +
  14202. +enum mmal_msg_type {
  14203. + MMAL_MSG_TYPE_QUIT = 1,
  14204. + MMAL_MSG_TYPE_SERVICE_CLOSED,
  14205. + MMAL_MSG_TYPE_GET_VERSION,
  14206. + MMAL_MSG_TYPE_COMPONENT_CREATE,
  14207. + MMAL_MSG_TYPE_COMPONENT_DESTROY, /* 5 */
  14208. + MMAL_MSG_TYPE_COMPONENT_ENABLE,
  14209. + MMAL_MSG_TYPE_COMPONENT_DISABLE,
  14210. + MMAL_MSG_TYPE_PORT_INFO_GET,
  14211. + MMAL_MSG_TYPE_PORT_INFO_SET,
  14212. + MMAL_MSG_TYPE_PORT_ACTION, /* 10 */
  14213. + MMAL_MSG_TYPE_BUFFER_FROM_HOST,
  14214. + MMAL_MSG_TYPE_BUFFER_TO_HOST,
  14215. + MMAL_MSG_TYPE_GET_STATS,
  14216. + MMAL_MSG_TYPE_PORT_PARAMETER_SET,
  14217. + MMAL_MSG_TYPE_PORT_PARAMETER_GET, /* 15 */
  14218. + MMAL_MSG_TYPE_EVENT_TO_HOST,
  14219. + MMAL_MSG_TYPE_GET_CORE_STATS_FOR_PORT,
  14220. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR,
  14221. + MMAL_MSG_TYPE_CONSUME_MEM,
  14222. + MMAL_MSG_TYPE_LMK, /* 20 */
  14223. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR_DESC,
  14224. + MMAL_MSG_TYPE_DRM_GET_LHS32,
  14225. + MMAL_MSG_TYPE_DRM_GET_TIME,
  14226. + MMAL_MSG_TYPE_BUFFER_FROM_HOST_ZEROLEN,
  14227. + MMAL_MSG_TYPE_PORT_FLUSH, /* 25 */
  14228. + MMAL_MSG_TYPE_HOST_LOG,
  14229. + MMAL_MSG_TYPE_MSG_LAST
  14230. +};
  14231. +
  14232. +/* port action request messages differ depending on the action type */
  14233. +enum mmal_msg_port_action_type {
  14234. + MMAL_MSG_PORT_ACTION_TYPE_UNKNOWN = 0, /* Unkown action */
  14235. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE, /* Enable a port */
  14236. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE, /* Disable a port */
  14237. + MMAL_MSG_PORT_ACTION_TYPE_FLUSH, /* Flush a port */
  14238. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT, /* Connect ports */
  14239. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT, /* Disconnect ports */
  14240. + MMAL_MSG_PORT_ACTION_TYPE_SET_REQUIREMENTS, /* Set buffer requirements*/
  14241. +};
  14242. +
  14243. +struct mmal_msg_header {
  14244. + u32 magic;
  14245. + u32 type; /** enum mmal_msg_type */
  14246. +
  14247. + /* Opaque handle to the control service */
  14248. + struct mmal_control_service *control_service;
  14249. +
  14250. + struct mmal_msg_context *context; /** a u32 per message context */
  14251. + u32 status; /** The status of the vchiq operation */
  14252. + u32 padding;
  14253. +};
  14254. +
  14255. +/* Send from VC to host to report version */
  14256. +struct mmal_msg_version {
  14257. + u32 flags;
  14258. + u32 major;
  14259. + u32 minor;
  14260. + u32 minimum;
  14261. +};
  14262. +
  14263. +/* request to VC to create component */
  14264. +struct mmal_msg_component_create {
  14265. + void *client_component; /* component context */
  14266. + char name[128];
  14267. + u32 pid; /* For debug */
  14268. +};
  14269. +
  14270. +/* reply from VC to component creation request */
  14271. +struct mmal_msg_component_create_reply {
  14272. + u32 status; /** enum mmal_msg_status - how does this differ to
  14273. + * the one in the header?
  14274. + */
  14275. + u32 component_handle; /* VideoCore handle for component */
  14276. + u32 input_num; /* Number of input ports */
  14277. + u32 output_num; /* Number of output ports */
  14278. + u32 clock_num; /* Number of clock ports */
  14279. +};
  14280. +
  14281. +/* request to VC to destroy a component */
  14282. +struct mmal_msg_component_destroy {
  14283. + u32 component_handle;
  14284. +};
  14285. +
  14286. +struct mmal_msg_component_destroy_reply {
  14287. + u32 status; /** The component destruction status */
  14288. +};
  14289. +
  14290. +
  14291. +/* request and reply to VC to enable a component */
  14292. +struct mmal_msg_component_enable {
  14293. + u32 component_handle;
  14294. +};
  14295. +
  14296. +struct mmal_msg_component_enable_reply {
  14297. + u32 status; /** The component enable status */
  14298. +};
  14299. +
  14300. +
  14301. +/* request and reply to VC to disable a component */
  14302. +struct mmal_msg_component_disable {
  14303. + u32 component_handle;
  14304. +};
  14305. +
  14306. +struct mmal_msg_component_disable_reply {
  14307. + u32 status; /** The component disable status */
  14308. +};
  14309. +
  14310. +/* request to VC to get port information */
  14311. +struct mmal_msg_port_info_get {
  14312. + u32 component_handle; /* component handle port is associated with */
  14313. + u32 port_type; /* enum mmal_msg_port_type */
  14314. + u32 index; /* port index to query */
  14315. +};
  14316. +
  14317. +/* reply from VC to get port info request */
  14318. +struct mmal_msg_port_info_get_reply {
  14319. + u32 status; /** enum mmal_msg_status */
  14320. + u32 component_handle; /* component handle port is associated with */
  14321. + u32 port_type; /* enum mmal_msg_port_type */
  14322. + u32 port_index; /* port indexed in query */
  14323. + s32 found; /* unused */
  14324. + u32 port_handle; /**< Handle to use for this port */
  14325. + struct mmal_port port;
  14326. + struct mmal_es_format format; /* elementry stream format */
  14327. + union mmal_es_specific_format es; /* es type specific data */
  14328. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE]; /* es extra data */
  14329. +};
  14330. +
  14331. +/* request to VC to set port information */
  14332. +struct mmal_msg_port_info_set {
  14333. + u32 component_handle;
  14334. + u32 port_type; /* enum mmal_msg_port_type */
  14335. + u32 port_index; /* port indexed in query */
  14336. + struct mmal_port port;
  14337. + struct mmal_es_format format;
  14338. + union mmal_es_specific_format es;
  14339. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  14340. +};
  14341. +
  14342. +/* reply from VC to port info set request */
  14343. +struct mmal_msg_port_info_set_reply {
  14344. + u32 status;
  14345. + u32 component_handle; /* component handle port is associated with */
  14346. + u32 port_type; /* enum mmal_msg_port_type */
  14347. + u32 index; /* port indexed in query */
  14348. + s32 found; /* unused */
  14349. + u32 port_handle; /**< Handle to use for this port */
  14350. + struct mmal_port port;
  14351. + struct mmal_es_format format;
  14352. + union mmal_es_specific_format es;
  14353. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  14354. +};
  14355. +
  14356. +
  14357. +/* port action requests that take a mmal_port as a parameter */
  14358. +struct mmal_msg_port_action_port {
  14359. + u32 component_handle;
  14360. + u32 port_handle;
  14361. + u32 action; /* enum mmal_msg_port_action_type */
  14362. + struct mmal_port port;
  14363. +};
  14364. +
  14365. +/* port action requests that take handles as a parameter */
  14366. +struct mmal_msg_port_action_handle {
  14367. + u32 component_handle;
  14368. + u32 port_handle;
  14369. + u32 action; /* enum mmal_msg_port_action_type */
  14370. + u32 connect_component_handle;
  14371. + u32 connect_port_handle;
  14372. +};
  14373. +
  14374. +struct mmal_msg_port_action_reply {
  14375. + u32 status; /** The port action operation status */
  14376. +};
  14377. +
  14378. +
  14379. +
  14380. +
  14381. +/* MMAL buffer transfer */
  14382. +
  14383. +/** Size of space reserved in a buffer message for short messages. */
  14384. +#define MMAL_VC_SHORT_DATA 128
  14385. +
  14386. +/** Signals that the current payload is the end of the stream of data */
  14387. +#define MMAL_BUFFER_HEADER_FLAG_EOS (1<<0)
  14388. +/** Signals that the start of the current payload starts a frame */
  14389. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_START (1<<1)
  14390. +/** Signals that the end of the current payload ends a frame */
  14391. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_END (1<<2)
  14392. +/** Signals that the current payload contains only complete frames (>1) */
  14393. +#define MMAL_BUFFER_HEADER_FLAG_FRAME \
  14394. + (MMAL_BUFFER_HEADER_FLAG_FRAME_START|MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  14395. +/** Signals that the current payload is a keyframe (i.e. self decodable) */
  14396. +#define MMAL_BUFFER_HEADER_FLAG_KEYFRAME (1<<3)
  14397. +/** Signals a discontinuity in the stream of data (e.g. after a seek).
  14398. + * Can be used for instance by a decoder to reset its state */
  14399. +#define MMAL_BUFFER_HEADER_FLAG_DISCONTINUITY (1<<4)
  14400. +/** Signals a buffer containing some kind of config data for the component
  14401. + * (e.g. codec config data) */
  14402. +#define MMAL_BUFFER_HEADER_FLAG_CONFIG (1<<5)
  14403. +/** Signals an encrypted payload */
  14404. +#define MMAL_BUFFER_HEADER_FLAG_ENCRYPTED (1<<6)
  14405. +/** Signals a buffer containing side information */
  14406. +#define MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO (1<<7)
  14407. +/** Signals a buffer which is the snapshot/postview image from a stills
  14408. + * capture
  14409. + */
  14410. +#define MMAL_BUFFER_HEADER_FLAGS_SNAPSHOT (1<<8)
  14411. +/** Signals a buffer which contains data known to be corrupted */
  14412. +#define MMAL_BUFFER_HEADER_FLAG_CORRUPTED (1<<9)
  14413. +/** Signals that a buffer failed to be transmitted */
  14414. +#define MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED (1<<10)
  14415. +
  14416. +struct mmal_driver_buffer {
  14417. + u32 magic;
  14418. + u32 component_handle;
  14419. + u32 port_handle;
  14420. + void *client_context;
  14421. +};
  14422. +
  14423. +/* buffer header */
  14424. +struct mmal_buffer_header {
  14425. + struct mmal_buffer_header *next; /* next header */
  14426. + void *priv; /* framework private data */
  14427. + u32 cmd;
  14428. + void *data;
  14429. + u32 alloc_size;
  14430. + u32 length;
  14431. + u32 offset;
  14432. + u32 flags;
  14433. + s64 pts;
  14434. + s64 dts;
  14435. + void *type;
  14436. + void *user_data;
  14437. +};
  14438. +
  14439. +struct mmal_buffer_header_type_specific {
  14440. + union {
  14441. + struct {
  14442. + u32 planes;
  14443. + u32 offset[4];
  14444. + u32 pitch[4];
  14445. + u32 flags;
  14446. + } video;
  14447. + } u;
  14448. +};
  14449. +
  14450. +struct mmal_msg_buffer_from_host {
  14451. + /* The front 32 bytes of the buffer header are copied
  14452. + * back to us in the reply to allow for context. This
  14453. + * area is used to store two mmal_driver_buffer structures to
  14454. + * allow for multiple concurrent service users.
  14455. + */
  14456. + /* control data */
  14457. + struct mmal_driver_buffer drvbuf;
  14458. +
  14459. + /* referenced control data for passthrough buffer management */
  14460. + struct mmal_driver_buffer drvbuf_ref;
  14461. + struct mmal_buffer_header buffer_header; /* buffer header itself */
  14462. + struct mmal_buffer_header_type_specific buffer_header_type_specific;
  14463. + s32 is_zero_copy;
  14464. + s32 has_reference;
  14465. +
  14466. + /** allows short data to be xfered in control message */
  14467. + u32 payload_in_message;
  14468. + u8 short_data[MMAL_VC_SHORT_DATA];
  14469. +};
  14470. +
  14471. +
  14472. +/* port parameter setting */
  14473. +
  14474. +#define MMAL_WORKER_PORT_PARAMETER_SPACE 96
  14475. +
  14476. +struct mmal_msg_port_parameter_set {
  14477. + u32 component_handle; /* component */
  14478. + u32 port_handle; /* port */
  14479. + u32 id; /* Parameter ID */
  14480. + u32 size; /* Parameter size */
  14481. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  14482. +};
  14483. +
  14484. +struct mmal_msg_port_parameter_set_reply {
  14485. + u32 status; /** enum mmal_msg_status todo: how does this
  14486. + * differ to the one in the header?
  14487. + */
  14488. +};
  14489. +
  14490. +/* port parameter getting */
  14491. +
  14492. +struct mmal_msg_port_parameter_get {
  14493. + u32 component_handle; /* component */
  14494. + u32 port_handle; /* port */
  14495. + u32 id; /* Parameter ID */
  14496. + u32 size; /* Parameter size */
  14497. +};
  14498. +
  14499. +struct mmal_msg_port_parameter_get_reply {
  14500. + u32 status; /* Status of mmal_port_parameter_get call */
  14501. + u32 id; /* Parameter ID */
  14502. + u32 size; /* Parameter size */
  14503. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  14504. +};
  14505. +
  14506. +/* event messages */
  14507. +#define MMAL_WORKER_EVENT_SPACE 256
  14508. +
  14509. +struct mmal_msg_event_to_host {
  14510. + void *client_component; /* component context */
  14511. +
  14512. + u32 port_type;
  14513. + u32 port_num;
  14514. +
  14515. + u32 cmd;
  14516. + u32 length;
  14517. + u8 data[MMAL_WORKER_EVENT_SPACE];
  14518. + struct mmal_buffer_header *delayed_buffer;
  14519. +};
  14520. +
  14521. +/* all mmal messages are serialised through this structure */
  14522. +struct mmal_msg {
  14523. + /* header */
  14524. + struct mmal_msg_header h;
  14525. + /* payload */
  14526. + union {
  14527. + struct mmal_msg_version version;
  14528. +
  14529. + struct mmal_msg_component_create component_create;
  14530. + struct mmal_msg_component_create_reply component_create_reply;
  14531. +
  14532. + struct mmal_msg_component_destroy component_destroy;
  14533. + struct mmal_msg_component_destroy_reply component_destroy_reply;
  14534. +
  14535. + struct mmal_msg_component_enable component_enable;
  14536. + struct mmal_msg_component_enable_reply component_enable_reply;
  14537. +
  14538. + struct mmal_msg_component_disable component_disable;
  14539. + struct mmal_msg_component_disable_reply component_disable_reply;
  14540. +
  14541. + struct mmal_msg_port_info_get port_info_get;
  14542. + struct mmal_msg_port_info_get_reply port_info_get_reply;
  14543. +
  14544. + struct mmal_msg_port_info_set port_info_set;
  14545. + struct mmal_msg_port_info_set_reply port_info_set_reply;
  14546. +
  14547. + struct mmal_msg_port_action_port port_action_port;
  14548. + struct mmal_msg_port_action_handle port_action_handle;
  14549. + struct mmal_msg_port_action_reply port_action_reply;
  14550. +
  14551. + struct mmal_msg_buffer_from_host buffer_from_host;
  14552. +
  14553. + struct mmal_msg_port_parameter_set port_parameter_set;
  14554. + struct mmal_msg_port_parameter_set_reply
  14555. + port_parameter_set_reply;
  14556. + struct mmal_msg_port_parameter_get
  14557. + port_parameter_get;
  14558. + struct mmal_msg_port_parameter_get_reply
  14559. + port_parameter_get_reply;
  14560. +
  14561. + struct mmal_msg_event_to_host event_to_host;
  14562. +
  14563. + u8 payload[MMAL_MSG_MAX_PAYLOAD];
  14564. + } u;
  14565. +};
  14566. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-msg-port.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-port.h
  14567. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-msg-port.h 1970-01-01 01:00:00.000000000 +0100
  14568. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-msg-port.h 2014-03-11 16:52:42.000000000 +0100
  14569. @@ -0,0 +1,107 @@
  14570. +/*
  14571. + * Broadcom BM2835 V4L2 driver
  14572. + *
  14573. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14574. + *
  14575. + * This file is subject to the terms and conditions of the GNU General Public
  14576. + * License. See the file COPYING in the main directory of this archive
  14577. + * for more details.
  14578. + *
  14579. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14580. + * Dave Stevenson <dsteve@broadcom.com>
  14581. + * Simon Mellor <simellor@broadcom.com>
  14582. + * Luke Diamand <luked@broadcom.com>
  14583. + */
  14584. +
  14585. +/* MMAL_PORT_TYPE_T */
  14586. +enum mmal_port_type {
  14587. + MMAL_PORT_TYPE_UNKNOWN = 0, /**< Unknown port type */
  14588. + MMAL_PORT_TYPE_CONTROL, /**< Control port */
  14589. + MMAL_PORT_TYPE_INPUT, /**< Input port */
  14590. + MMAL_PORT_TYPE_OUTPUT, /**< Output port */
  14591. + MMAL_PORT_TYPE_CLOCK, /**< Clock port */
  14592. +};
  14593. +
  14594. +/** The port is pass-through and doesn't need buffer headers allocated */
  14595. +#define MMAL_PORT_CAPABILITY_PASSTHROUGH 0x01
  14596. +/** The port wants to allocate the buffer payloads.
  14597. + * This signals a preference that payload allocation should be done
  14598. + * on this port for efficiency reasons. */
  14599. +#define MMAL_PORT_CAPABILITY_ALLOCATION 0x02
  14600. +/** The port supports format change events.
  14601. + * This applies to input ports and is used to let the client know
  14602. + * whether the port supports being reconfigured via a format
  14603. + * change event (i.e. without having to disable the port). */
  14604. +#define MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE 0x04
  14605. +
  14606. +/* mmal port structure (MMAL_PORT_T)
  14607. + *
  14608. + * most elements are informational only, the pointer values for
  14609. + * interogation messages are generally provided as additional
  14610. + * strucures within the message. When used to set values only teh
  14611. + * buffer_num, buffer_size and userdata parameters are writable.
  14612. + */
  14613. +struct mmal_port {
  14614. + void *priv; /* Private member used by the framework */
  14615. + const char *name; /* Port name. Used for debugging purposes (RO) */
  14616. +
  14617. + u32 type; /* Type of the port (RO) enum mmal_port_type */
  14618. + u16 index; /* Index of the port in its type list (RO) */
  14619. + u16 index_all; /* Index of the port in the list of all ports (RO) */
  14620. +
  14621. + u32 is_enabled; /* Indicates whether the port is enabled or not (RO) */
  14622. + struct mmal_es_format *format; /* Format of the elementary stream */
  14623. +
  14624. + u32 buffer_num_min; /* Minimum number of buffers the port
  14625. + * requires (RO). This is set by the
  14626. + * component.
  14627. + */
  14628. +
  14629. + u32 buffer_size_min; /* Minimum size of buffers the port
  14630. + * requires (RO). This is set by the
  14631. + * component.
  14632. + */
  14633. +
  14634. + u32 buffer_alignment_min; /* Minimum alignment requirement for
  14635. + * the buffers (RO). A value of
  14636. + * zero means no special alignment
  14637. + * requirements. This is set by the
  14638. + * component.
  14639. + */
  14640. +
  14641. + u32 buffer_num_recommended; /* Number of buffers the port
  14642. + * recommends for optimal
  14643. + * performance (RO). A value of
  14644. + * zero means no special
  14645. + * recommendation. This is set
  14646. + * by the component.
  14647. + */
  14648. +
  14649. + u32 buffer_size_recommended; /* Size of buffers the port
  14650. + * recommends for optimal
  14651. + * performance (RO). A value of
  14652. + * zero means no special
  14653. + * recommendation. This is set
  14654. + * by the component.
  14655. + */
  14656. +
  14657. + u32 buffer_num; /* Actual number of buffers the port will use.
  14658. + * This is set by the client.
  14659. + */
  14660. +
  14661. + u32 buffer_size; /* Actual maximum size of the buffers that
  14662. + * will be sent to the port. This is set by
  14663. + * the client.
  14664. + */
  14665. +
  14666. + void *component; /* Component this port belongs to (Read Only) */
  14667. +
  14668. + void *userdata; /* Field reserved for use by the client */
  14669. +
  14670. + u32 capabilities; /* Flags describing the capabilities of a
  14671. + * port (RO). Bitwise combination of \ref
  14672. + * portcapabilities "Port capabilities"
  14673. + * values.
  14674. + */
  14675. +
  14676. +};
  14677. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-parameters.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-parameters.h
  14678. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-parameters.h 1970-01-01 01:00:00.000000000 +0100
  14679. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-parameters.h 2014-03-11 16:52:42.000000000 +0100
  14680. @@ -0,0 +1,649 @@
  14681. +/*
  14682. + * Broadcom BM2835 V4L2 driver
  14683. + *
  14684. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  14685. + *
  14686. + * This file is subject to the terms and conditions of the GNU General Public
  14687. + * License. See the file COPYING in the main directory of this archive
  14688. + * for more details.
  14689. + *
  14690. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  14691. + * Dave Stevenson <dsteve@broadcom.com>
  14692. + * Simon Mellor <simellor@broadcom.com>
  14693. + * Luke Diamand <luked@broadcom.com>
  14694. + */
  14695. +
  14696. +/* common parameters */
  14697. +
  14698. +/** @name Parameter groups
  14699. + * Parameters are divided into groups, and then allocated sequentially within
  14700. + * a group using an enum.
  14701. + * @{
  14702. + */
  14703. +
  14704. +/** Common parameter ID group, used with many types of component. */
  14705. +#define MMAL_PARAMETER_GROUP_COMMON (0<<16)
  14706. +/** Camera-specific parameter ID group. */
  14707. +#define MMAL_PARAMETER_GROUP_CAMERA (1<<16)
  14708. +/** Video-specific parameter ID group. */
  14709. +#define MMAL_PARAMETER_GROUP_VIDEO (2<<16)
  14710. +/** Audio-specific parameter ID group. */
  14711. +#define MMAL_PARAMETER_GROUP_AUDIO (3<<16)
  14712. +/** Clock-specific parameter ID group. */
  14713. +#define MMAL_PARAMETER_GROUP_CLOCK (4<<16)
  14714. +/** Miracast-specific parameter ID group. */
  14715. +#define MMAL_PARAMETER_GROUP_MIRACAST (5<<16)
  14716. +
  14717. +/* Common parameters */
  14718. +enum mmal_parameter_common_type {
  14719. + MMAL_PARAMETER_UNUSED /**< Never a valid parameter ID */
  14720. + = MMAL_PARAMETER_GROUP_COMMON,
  14721. + MMAL_PARAMETER_SUPPORTED_ENCODINGS, /**< MMAL_PARAMETER_ENCODING_T */
  14722. + MMAL_PARAMETER_URI, /**< MMAL_PARAMETER_URI_T */
  14723. +
  14724. + /** MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T */
  14725. + MMAL_PARAMETER_CHANGE_EVENT_REQUEST,
  14726. +
  14727. + /** MMAL_PARAMETER_BOOLEAN_T */
  14728. + MMAL_PARAMETER_ZERO_COPY,
  14729. +
  14730. + /**< MMAL_PARAMETER_BUFFER_REQUIREMENTS_T */
  14731. + MMAL_PARAMETER_BUFFER_REQUIREMENTS,
  14732. +
  14733. + MMAL_PARAMETER_STATISTICS, /**< MMAL_PARAMETER_STATISTICS_T */
  14734. + MMAL_PARAMETER_CORE_STATISTICS, /**< MMAL_PARAMETER_CORE_STATISTICS_T */
  14735. + MMAL_PARAMETER_MEM_USAGE, /**< MMAL_PARAMETER_MEM_USAGE_T */
  14736. + MMAL_PARAMETER_BUFFER_FLAG_FILTER, /**< MMAL_PARAMETER_UINT32_T */
  14737. + MMAL_PARAMETER_SEEK, /**< MMAL_PARAMETER_SEEK_T */
  14738. + MMAL_PARAMETER_POWERMON_ENABLE, /**< MMAL_PARAMETER_BOOLEAN_T */
  14739. + MMAL_PARAMETER_LOGGING, /**< MMAL_PARAMETER_LOGGING_T */
  14740. + MMAL_PARAMETER_SYSTEM_TIME /**< MMAL_PARAMETER_UINT64_T */
  14741. +};
  14742. +
  14743. +/* camera parameters */
  14744. +
  14745. +enum mmal_parameter_camera_type {
  14746. + /* 0 */
  14747. + /** @ref MMAL_PARAMETER_THUMBNAIL_CONFIG_T */
  14748. + MMAL_PARAMETER_THUMBNAIL_CONFIGURATION
  14749. + = MMAL_PARAMETER_GROUP_CAMERA,
  14750. + MMAL_PARAMETER_CAPTURE_QUALITY, /**< Unused? */
  14751. + MMAL_PARAMETER_ROTATION, /**< @ref MMAL_PARAMETER_INT32_T */
  14752. + MMAL_PARAMETER_EXIF_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14753. + MMAL_PARAMETER_EXIF, /**< @ref MMAL_PARAMETER_EXIF_T */
  14754. + MMAL_PARAMETER_AWB_MODE, /**< @ref MMAL_PARAM_AWBMODE_T */
  14755. + MMAL_PARAMETER_IMAGE_EFFECT, /**< @ref MMAL_PARAMETER_IMAGEFX_T */
  14756. + MMAL_PARAMETER_COLOUR_EFFECT, /**< @ref MMAL_PARAMETER_COLOURFX_T */
  14757. + MMAL_PARAMETER_FLICKER_AVOID, /**< @ref MMAL_PARAMETER_FLICKERAVOID_T */
  14758. + MMAL_PARAMETER_FLASH, /**< @ref MMAL_PARAMETER_FLASH_T */
  14759. + MMAL_PARAMETER_REDEYE, /**< @ref MMAL_PARAMETER_REDEYE_T */
  14760. + MMAL_PARAMETER_FOCUS, /**< @ref MMAL_PARAMETER_FOCUS_T */
  14761. + MMAL_PARAMETER_FOCAL_LENGTHS, /**< Unused? */
  14762. + MMAL_PARAMETER_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  14763. + MMAL_PARAMETER_ZOOM, /**< @ref MMAL_PARAMETER_SCALEFACTOR_T */
  14764. + MMAL_PARAMETER_MIRROR, /**< @ref MMAL_PARAMETER_MIRROR_T */
  14765. +
  14766. + /* 0x10 */
  14767. + MMAL_PARAMETER_CAMERA_NUM, /**< @ref MMAL_PARAMETER_UINT32_T */
  14768. + MMAL_PARAMETER_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14769. + MMAL_PARAMETER_EXPOSURE_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMODE_T */
  14770. + MMAL_PARAMETER_EXP_METERING_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMETERINGMODE_T */
  14771. + MMAL_PARAMETER_FOCUS_STATUS, /**< @ref MMAL_PARAMETER_FOCUS_STATUS_T */
  14772. + MMAL_PARAMETER_CAMERA_CONFIG, /**< @ref MMAL_PARAMETER_CAMERA_CONFIG_T */
  14773. + MMAL_PARAMETER_CAPTURE_STATUS, /**< @ref MMAL_PARAMETER_CAPTURE_STATUS_T */
  14774. + MMAL_PARAMETER_FACE_TRACK, /**< @ref MMAL_PARAMETER_FACE_TRACK_T */
  14775. + MMAL_PARAMETER_DRAW_BOX_FACES_AND_FOCUS, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14776. + MMAL_PARAMETER_JPEG_Q_FACTOR, /**< @ref MMAL_PARAMETER_UINT32_T */
  14777. + MMAL_PARAMETER_FRAME_RATE, /**< @ref MMAL_PARAMETER_FRAME_RATE_T */
  14778. + MMAL_PARAMETER_USE_STC, /**< @ref MMAL_PARAMETER_CAMERA_STC_MODE_T */
  14779. + MMAL_PARAMETER_CAMERA_INFO, /**< @ref MMAL_PARAMETER_CAMERA_INFO_T */
  14780. + MMAL_PARAMETER_VIDEO_STABILISATION, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14781. + MMAL_PARAMETER_FACE_TRACK_RESULTS, /**< @ref MMAL_PARAMETER_FACE_TRACK_RESULTS_T */
  14782. + MMAL_PARAMETER_ENABLE_RAW_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14783. +
  14784. + /* 0x20 */
  14785. + MMAL_PARAMETER_DPF_FILE, /**< @ref MMAL_PARAMETER_URI_T */
  14786. + MMAL_PARAMETER_ENABLE_DPF_FILE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14787. + MMAL_PARAMETER_DPF_FAIL_IS_FATAL, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14788. + MMAL_PARAMETER_CAPTURE_MODE, /**< @ref MMAL_PARAMETER_CAPTUREMODE_T */
  14789. + MMAL_PARAMETER_FOCUS_REGIONS, /**< @ref MMAL_PARAMETER_FOCUS_REGIONS_T */
  14790. + MMAL_PARAMETER_INPUT_CROP, /**< @ref MMAL_PARAMETER_INPUT_CROP_T */
  14791. + MMAL_PARAMETER_SENSOR_INFORMATION, /**< @ref MMAL_PARAMETER_SENSOR_INFORMATION_T */
  14792. + MMAL_PARAMETER_FLASH_SELECT, /**< @ref MMAL_PARAMETER_FLASH_SELECT_T */
  14793. + MMAL_PARAMETER_FIELD_OF_VIEW, /**< @ref MMAL_PARAMETER_FIELD_OF_VIEW_T */
  14794. + MMAL_PARAMETER_HIGH_DYNAMIC_RANGE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14795. + MMAL_PARAMETER_DYNAMIC_RANGE_COMPRESSION, /**< @ref MMAL_PARAMETER_DRC_T */
  14796. + MMAL_PARAMETER_ALGORITHM_CONTROL, /**< @ref MMAL_PARAMETER_ALGORITHM_CONTROL_T */
  14797. + MMAL_PARAMETER_SHARPNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  14798. + MMAL_PARAMETER_CONTRAST, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  14799. + MMAL_PARAMETER_BRIGHTNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  14800. + MMAL_PARAMETER_SATURATION, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  14801. +
  14802. + /* 0x30 */
  14803. + MMAL_PARAMETER_ISO, /**< @ref MMAL_PARAMETER_UINT32_T */
  14804. + MMAL_PARAMETER_ANTISHAKE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14805. +
  14806. + /** @ref MMAL_PARAMETER_IMAGEFX_PARAMETERS_T */
  14807. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  14808. +
  14809. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  14810. + MMAL_PARAMETER_CAMERA_BURST_CAPTURE,
  14811. +
  14812. + /** @ref MMAL_PARAMETER_UINT32_T */
  14813. + MMAL_PARAMETER_CAMERA_MIN_ISO,
  14814. +
  14815. + /** @ref MMAL_PARAMETER_CAMERA_USE_CASE_T */
  14816. + MMAL_PARAMETER_CAMERA_USE_CASE,
  14817. +
  14818. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14819. + MMAL_PARAMETER_CAPTURE_STATS_PASS,
  14820. +
  14821. + /** @ref MMAL_PARAMETER_UINT32_T */
  14822. + MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG,
  14823. +
  14824. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  14825. + MMAL_PARAMETER_ENABLE_REGISTER_FILE,
  14826. +
  14827. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  14828. + MMAL_PARAMETER_REGISTER_FAIL_IS_FATAL,
  14829. +
  14830. + /** @ref MMAL_PARAMETER_CONFIGFILE_T */
  14831. + MMAL_PARAMETER_CONFIGFILE_REGISTERS,
  14832. +
  14833. + /** @ref MMAL_PARAMETER_CONFIGFILE_CHUNK_T */
  14834. + MMAL_PARAMETER_CONFIGFILE_CHUNK_REGISTERS,
  14835. + MMAL_PARAMETER_JPEG_ATTACH_LOG, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14836. + MMAL_PARAMETER_ZERO_SHUTTER_LAG, /**< @ref MMAL_PARAMETER_ZEROSHUTTERLAG_T */
  14837. + MMAL_PARAMETER_FPS_RANGE, /**< @ref MMAL_PARAMETER_FPS_RANGE_T */
  14838. + MMAL_PARAMETER_CAPTURE_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  14839. +
  14840. + /* 0x40 */
  14841. + MMAL_PARAMETER_SW_SHARPEN_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14842. + MMAL_PARAMETER_FLASH_REQUIRED, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14843. + MMAL_PARAMETER_SW_SATURATION_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  14844. + MMAL_PARAMETER_SHUTTER_SPEED /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
  14845. +};
  14846. +
  14847. +struct mmal_parameter_rational {
  14848. + s32 num; /**< Numerator */
  14849. + s32 den; /**< Denominator */
  14850. +};
  14851. +
  14852. +enum mmal_parameter_camera_config_timestamp_mode {
  14853. + MMAL_PARAM_TIMESTAMP_MODE_ZERO = 0, /* Always timestamp frames as 0 */
  14854. + MMAL_PARAM_TIMESTAMP_MODE_RAW_STC, /* Use the raw STC value
  14855. + * for the frame timestamp
  14856. + */
  14857. + MMAL_PARAM_TIMESTAMP_MODE_RESET_STC, /* Use the STC timestamp
  14858. + * but subtract the
  14859. + * timestamp of the first
  14860. + * frame sent to give a
  14861. + * zero based timestamp.
  14862. + */
  14863. +};
  14864. +
  14865. +struct mmal_parameter_fps_range {
  14866. + /**< Low end of the permitted framerate range */
  14867. + struct mmal_parameter_rational fps_low;
  14868. + /**< High end of the permitted framerate range */
  14869. + struct mmal_parameter_rational fps_high;
  14870. +};
  14871. +
  14872. +
  14873. +/* camera configuration parameter */
  14874. +struct mmal_parameter_camera_config {
  14875. + /* Parameters for setting up the image pools */
  14876. + u32 max_stills_w; /* Max size of stills capture */
  14877. + u32 max_stills_h;
  14878. + u32 stills_yuv422; /* Allow YUV422 stills capture */
  14879. + u32 one_shot_stills; /* Continuous or one shot stills captures. */
  14880. +
  14881. + u32 max_preview_video_w; /* Max size of the preview or video
  14882. + * capture frames
  14883. + */
  14884. + u32 max_preview_video_h;
  14885. + u32 num_preview_video_frames;
  14886. +
  14887. + /** Sets the height of the circular buffer for stills capture. */
  14888. + u32 stills_capture_circular_buffer_height;
  14889. +
  14890. + /** Allows preview/encode to resume as fast as possible after the stills
  14891. + * input frame has been received, and then processes the still frame in
  14892. + * the background whilst preview/encode has resumed.
  14893. + * Actual mode is controlled by MMAL_PARAMETER_CAPTURE_MODE.
  14894. + */
  14895. + u32 fast_preview_resume;
  14896. +
  14897. + /** Selects algorithm for timestamping frames if
  14898. + * there is no clock component connected.
  14899. + * enum mmal_parameter_camera_config_timestamp_mode
  14900. + */
  14901. + s32 use_stc_timestamp;
  14902. +};
  14903. +
  14904. +
  14905. +enum mmal_parameter_exposuremode {
  14906. + MMAL_PARAM_EXPOSUREMODE_OFF,
  14907. + MMAL_PARAM_EXPOSUREMODE_AUTO,
  14908. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  14909. + MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
  14910. + MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
  14911. + MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
  14912. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  14913. + MMAL_PARAM_EXPOSUREMODE_SNOW,
  14914. + MMAL_PARAM_EXPOSUREMODE_BEACH,
  14915. + MMAL_PARAM_EXPOSUREMODE_VERYLONG,
  14916. + MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
  14917. + MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
  14918. + MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
  14919. +};
  14920. +
  14921. +enum mmal_parameter_exposuremeteringmode {
  14922. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
  14923. + MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
  14924. + MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
  14925. + MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX,
  14926. +};
  14927. +
  14928. +enum mmal_parameter_awbmode {
  14929. + MMAL_PARAM_AWBMODE_OFF,
  14930. + MMAL_PARAM_AWBMODE_AUTO,
  14931. + MMAL_PARAM_AWBMODE_SUNLIGHT,
  14932. + MMAL_PARAM_AWBMODE_CLOUDY,
  14933. + MMAL_PARAM_AWBMODE_SHADE,
  14934. + MMAL_PARAM_AWBMODE_TUNGSTEN,
  14935. + MMAL_PARAM_AWBMODE_FLUORESCENT,
  14936. + MMAL_PARAM_AWBMODE_INCANDESCENT,
  14937. + MMAL_PARAM_AWBMODE_FLASH,
  14938. + MMAL_PARAM_AWBMODE_HORIZON,
  14939. +};
  14940. +
  14941. +enum mmal_parameter_imagefx {
  14942. + MMAL_PARAM_IMAGEFX_NONE,
  14943. + MMAL_PARAM_IMAGEFX_NEGATIVE,
  14944. + MMAL_PARAM_IMAGEFX_SOLARIZE,
  14945. + MMAL_PARAM_IMAGEFX_POSTERIZE,
  14946. + MMAL_PARAM_IMAGEFX_WHITEBOARD,
  14947. + MMAL_PARAM_IMAGEFX_BLACKBOARD,
  14948. + MMAL_PARAM_IMAGEFX_SKETCH,
  14949. + MMAL_PARAM_IMAGEFX_DENOISE,
  14950. + MMAL_PARAM_IMAGEFX_EMBOSS,
  14951. + MMAL_PARAM_IMAGEFX_OILPAINT,
  14952. + MMAL_PARAM_IMAGEFX_HATCH,
  14953. + MMAL_PARAM_IMAGEFX_GPEN,
  14954. + MMAL_PARAM_IMAGEFX_PASTEL,
  14955. + MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  14956. + MMAL_PARAM_IMAGEFX_FILM,
  14957. + MMAL_PARAM_IMAGEFX_BLUR,
  14958. + MMAL_PARAM_IMAGEFX_SATURATION,
  14959. + MMAL_PARAM_IMAGEFX_COLOURSWAP,
  14960. + MMAL_PARAM_IMAGEFX_WASHEDOUT,
  14961. + MMAL_PARAM_IMAGEFX_POSTERISE,
  14962. + MMAL_PARAM_IMAGEFX_COLOURPOINT,
  14963. + MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  14964. + MMAL_PARAM_IMAGEFX_CARTOON,
  14965. +};
  14966. +
  14967. +enum MMAL_PARAM_FLICKERAVOID_T {
  14968. + MMAL_PARAM_FLICKERAVOID_OFF,
  14969. + MMAL_PARAM_FLICKERAVOID_AUTO,
  14970. + MMAL_PARAM_FLICKERAVOID_50HZ,
  14971. + MMAL_PARAM_FLICKERAVOID_60HZ,
  14972. + MMAL_PARAM_FLICKERAVOID_MAX = 0x7FFFFFFF
  14973. +};
  14974. +
  14975. +/** Manner of video rate control */
  14976. +enum mmal_parameter_rate_control_mode {
  14977. + MMAL_VIDEO_RATECONTROL_DEFAULT,
  14978. + MMAL_VIDEO_RATECONTROL_VARIABLE,
  14979. + MMAL_VIDEO_RATECONTROL_CONSTANT,
  14980. + MMAL_VIDEO_RATECONTROL_VARIABLE_SKIP_FRAMES,
  14981. + MMAL_VIDEO_RATECONTROL_CONSTANT_SKIP_FRAMES
  14982. +};
  14983. +
  14984. +enum mmal_video_profile {
  14985. + MMAL_VIDEO_PROFILE_H263_BASELINE,
  14986. + MMAL_VIDEO_PROFILE_H263_H320CODING,
  14987. + MMAL_VIDEO_PROFILE_H263_BACKWARDCOMPATIBLE,
  14988. + MMAL_VIDEO_PROFILE_H263_ISWV2,
  14989. + MMAL_VIDEO_PROFILE_H263_ISWV3,
  14990. + MMAL_VIDEO_PROFILE_H263_HIGHCOMPRESSION,
  14991. + MMAL_VIDEO_PROFILE_H263_INTERNET,
  14992. + MMAL_VIDEO_PROFILE_H263_INTERLACE,
  14993. + MMAL_VIDEO_PROFILE_H263_HIGHLATENCY,
  14994. + MMAL_VIDEO_PROFILE_MP4V_SIMPLE,
  14995. + MMAL_VIDEO_PROFILE_MP4V_SIMPLESCALABLE,
  14996. + MMAL_VIDEO_PROFILE_MP4V_CORE,
  14997. + MMAL_VIDEO_PROFILE_MP4V_MAIN,
  14998. + MMAL_VIDEO_PROFILE_MP4V_NBIT,
  14999. + MMAL_VIDEO_PROFILE_MP4V_SCALABLETEXTURE,
  15000. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFACE,
  15001. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFBA,
  15002. + MMAL_VIDEO_PROFILE_MP4V_BASICANIMATED,
  15003. + MMAL_VIDEO_PROFILE_MP4V_HYBRID,
  15004. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDREALTIME,
  15005. + MMAL_VIDEO_PROFILE_MP4V_CORESCALABLE,
  15006. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCODING,
  15007. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCORE,
  15008. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSCALABLE,
  15009. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSIMPLE,
  15010. + MMAL_VIDEO_PROFILE_H264_BASELINE,
  15011. + MMAL_VIDEO_PROFILE_H264_MAIN,
  15012. + MMAL_VIDEO_PROFILE_H264_EXTENDED,
  15013. + MMAL_VIDEO_PROFILE_H264_HIGH,
  15014. + MMAL_VIDEO_PROFILE_H264_HIGH10,
  15015. + MMAL_VIDEO_PROFILE_H264_HIGH422,
  15016. + MMAL_VIDEO_PROFILE_H264_HIGH444,
  15017. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
  15018. + MMAL_VIDEO_PROFILE_DUMMY = 0x7FFFFFFF
  15019. +};
  15020. +
  15021. +enum mmal_video_level {
  15022. + MMAL_VIDEO_LEVEL_H263_10,
  15023. + MMAL_VIDEO_LEVEL_H263_20,
  15024. + MMAL_VIDEO_LEVEL_H263_30,
  15025. + MMAL_VIDEO_LEVEL_H263_40,
  15026. + MMAL_VIDEO_LEVEL_H263_45,
  15027. + MMAL_VIDEO_LEVEL_H263_50,
  15028. + MMAL_VIDEO_LEVEL_H263_60,
  15029. + MMAL_VIDEO_LEVEL_H263_70,
  15030. + MMAL_VIDEO_LEVEL_MP4V_0,
  15031. + MMAL_VIDEO_LEVEL_MP4V_0b,
  15032. + MMAL_VIDEO_LEVEL_MP4V_1,
  15033. + MMAL_VIDEO_LEVEL_MP4V_2,
  15034. + MMAL_VIDEO_LEVEL_MP4V_3,
  15035. + MMAL_VIDEO_LEVEL_MP4V_4,
  15036. + MMAL_VIDEO_LEVEL_MP4V_4a,
  15037. + MMAL_VIDEO_LEVEL_MP4V_5,
  15038. + MMAL_VIDEO_LEVEL_MP4V_6,
  15039. + MMAL_VIDEO_LEVEL_H264_1,
  15040. + MMAL_VIDEO_LEVEL_H264_1b,
  15041. + MMAL_VIDEO_LEVEL_H264_11,
  15042. + MMAL_VIDEO_LEVEL_H264_12,
  15043. + MMAL_VIDEO_LEVEL_H264_13,
  15044. + MMAL_VIDEO_LEVEL_H264_2,
  15045. + MMAL_VIDEO_LEVEL_H264_21,
  15046. + MMAL_VIDEO_LEVEL_H264_22,
  15047. + MMAL_VIDEO_LEVEL_H264_3,
  15048. + MMAL_VIDEO_LEVEL_H264_31,
  15049. + MMAL_VIDEO_LEVEL_H264_32,
  15050. + MMAL_VIDEO_LEVEL_H264_4,
  15051. + MMAL_VIDEO_LEVEL_H264_41,
  15052. + MMAL_VIDEO_LEVEL_H264_42,
  15053. + MMAL_VIDEO_LEVEL_H264_5,
  15054. + MMAL_VIDEO_LEVEL_H264_51,
  15055. + MMAL_VIDEO_LEVEL_DUMMY = 0x7FFFFFFF
  15056. +};
  15057. +
  15058. +struct mmal_parameter_video_profile {
  15059. + enum mmal_video_profile profile;
  15060. + enum mmal_video_level level;
  15061. +};
  15062. +
  15063. +/* video parameters */
  15064. +
  15065. +enum mmal_parameter_video_type {
  15066. + /** @ref MMAL_DISPLAYREGION_T */
  15067. + MMAL_PARAMETER_DISPLAYREGION = MMAL_PARAMETER_GROUP_VIDEO,
  15068. +
  15069. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  15070. + MMAL_PARAMETER_SUPPORTED_PROFILES,
  15071. +
  15072. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  15073. + MMAL_PARAMETER_PROFILE,
  15074. +
  15075. + /** @ref MMAL_PARAMETER_UINT32_T */
  15076. + MMAL_PARAMETER_INTRAPERIOD,
  15077. +
  15078. + /** @ref MMAL_PARAMETER_VIDEO_RATECONTROL_T */
  15079. + MMAL_PARAMETER_RATECONTROL,
  15080. +
  15081. + /** @ref MMAL_PARAMETER_VIDEO_NALUNITFORMAT_T */
  15082. + MMAL_PARAMETER_NALUNITFORMAT,
  15083. +
  15084. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15085. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  15086. +
  15087. + /** @ref MMAL_PARAMETER_UINT32_T.
  15088. + * Setting the value to zero resets to the default (one slice per frame).
  15089. + */
  15090. + MMAL_PARAMETER_MB_ROWS_PER_SLICE,
  15091. +
  15092. + /** @ref MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION_T */
  15093. + MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION,
  15094. +
  15095. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_ENABLE_T */
  15096. + MMAL_PARAMETER_VIDEO_EEDE_ENABLE,
  15097. +
  15098. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE_T */
  15099. + MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE,
  15100. +
  15101. + /** @ref MMAL_PARAMETER_BOOLEAN_T. Request an I-frame. */
  15102. + MMAL_PARAMETER_VIDEO_REQUEST_I_FRAME,
  15103. + /** @ref MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T */
  15104. + MMAL_PARAMETER_VIDEO_INTRA_REFRESH,
  15105. +
  15106. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15107. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  15108. +
  15109. + /** @ref MMAL_PARAMETER_UINT32_T. Run-time bit rate control */
  15110. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  15111. +
  15112. + /** @ref MMAL_PARAMETER_FRAME_RATE_T */
  15113. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  15114. +
  15115. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15116. + MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
  15117. +
  15118. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15119. + MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
  15120. +
  15121. + /** @ref MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL_T. */
  15122. + MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL,
  15123. +
  15124. + MMAL_PARAMETER_EXTRA_BUFFERS, /**< @ref MMAL_PARAMETER_UINT32_T. */
  15125. + /** @ref MMAL_PARAMETER_UINT32_T.
  15126. + * Changing this parameter from the default can reduce frame rate
  15127. + * because image buffers need to be re-pitched.
  15128. + */
  15129. + MMAL_PARAMETER_VIDEO_ALIGN_HORIZ,
  15130. +
  15131. + /** @ref MMAL_PARAMETER_UINT32_T.
  15132. + * Changing this parameter from the default can reduce frame rate
  15133. + * because image buffers need to be re-pitched.
  15134. + */
  15135. + MMAL_PARAMETER_VIDEO_ALIGN_VERT,
  15136. +
  15137. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15138. + MMAL_PARAMETER_VIDEO_DROPPABLE_PFRAMES,
  15139. +
  15140. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15141. + MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
  15142. +
  15143. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  15144. + MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
  15145. +
  15146. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  15147. + MMAL_PARAMETER_VIDEO_ENCODE_RC_SLICE_DQUANT,
  15148. +
  15149. + /** @ref MMAL_PARAMETER_UINT32_T */
  15150. + MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS,
  15151. +
  15152. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15153. + MMAL_PARAMETER_VIDEO_ENCODE_PEAK_RATE,
  15154. +
  15155. + /* H264 specific parameters */
  15156. +
  15157. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15158. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DISABLE_CABAC,
  15159. +
  15160. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15161. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_LATENCY,
  15162. +
  15163. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  15164. + MMAL_PARAMETER_VIDEO_ENCODE_H264_AU_DELIMITERS,
  15165. +
  15166. + /** @ref MMAL_PARAMETER_UINT32_T. */
  15167. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DEBLOCK_IDC,
  15168. +
  15169. + /** @ref MMAL_PARAMETER_VIDEO_ENCODER_H264_MB_INTRA_MODES_T. */
  15170. + MMAL_PARAMETER_VIDEO_ENCODE_H264_MB_INTRA_MODE,
  15171. +
  15172. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15173. + MMAL_PARAMETER_VIDEO_ENCODE_HEADER_ON_OPEN,
  15174. +
  15175. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15176. + MMAL_PARAMETER_VIDEO_ENCODE_PRECODE_FOR_QP,
  15177. +
  15178. + /** @ref MMAL_PARAMETER_VIDEO_DRM_INIT_INFO_T. */
  15179. + MMAL_PARAMETER_VIDEO_DRM_INIT_INFO,
  15180. +
  15181. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15182. + MMAL_PARAMETER_VIDEO_TIMESTAMP_FIFO,
  15183. +
  15184. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  15185. + MMAL_PARAMETER_VIDEO_DECODE_ERROR_CONCEALMENT,
  15186. +
  15187. + /** @ref MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER_T. */
  15188. + MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER,
  15189. +
  15190. + /** @ref MMAL_PARAMETER_BYTES_T */
  15191. + MMAL_PARAMETER_VIDEO_DECODE_CONFIG_VD3,
  15192. +
  15193. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15194. + MMAL_PARAMETER_VIDEO_ENCODE_H264_VCL_HRD_PARAMETERS,
  15195. +
  15196. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15197. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_DELAY_HRD_FLAG,
  15198. +
  15199. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  15200. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER
  15201. +};
  15202. +
  15203. +/** Valid mirror modes */
  15204. +enum mmal_parameter_mirror {
  15205. + MMAL_PARAM_MIRROR_NONE,
  15206. + MMAL_PARAM_MIRROR_VERTICAL,
  15207. + MMAL_PARAM_MIRROR_HORIZONTAL,
  15208. + MMAL_PARAM_MIRROR_BOTH,
  15209. +};
  15210. +
  15211. +enum mmal_parameter_displaytransform {
  15212. + MMAL_DISPLAY_ROT0 = 0,
  15213. + MMAL_DISPLAY_MIRROR_ROT0 = 1,
  15214. + MMAL_DISPLAY_MIRROR_ROT180 = 2,
  15215. + MMAL_DISPLAY_ROT180 = 3,
  15216. + MMAL_DISPLAY_MIRROR_ROT90 = 4,
  15217. + MMAL_DISPLAY_ROT270 = 5,
  15218. + MMAL_DISPLAY_ROT90 = 6,
  15219. + MMAL_DISPLAY_MIRROR_ROT270 = 7,
  15220. +};
  15221. +
  15222. +enum mmal_parameter_displaymode {
  15223. + MMAL_DISPLAY_MODE_FILL = 0,
  15224. + MMAL_DISPLAY_MODE_LETTERBOX = 1,
  15225. +};
  15226. +
  15227. +enum mmal_parameter_displayset {
  15228. + MMAL_DISPLAY_SET_NONE = 0,
  15229. + MMAL_DISPLAY_SET_NUM = 1,
  15230. + MMAL_DISPLAY_SET_FULLSCREEN = 2,
  15231. + MMAL_DISPLAY_SET_TRANSFORM = 4,
  15232. + MMAL_DISPLAY_SET_DEST_RECT = 8,
  15233. + MMAL_DISPLAY_SET_SRC_RECT = 0x10,
  15234. + MMAL_DISPLAY_SET_MODE = 0x20,
  15235. + MMAL_DISPLAY_SET_PIXEL = 0x40,
  15236. + MMAL_DISPLAY_SET_NOASPECT = 0x80,
  15237. + MMAL_DISPLAY_SET_LAYER = 0x100,
  15238. + MMAL_DISPLAY_SET_COPYPROTECT = 0x200,
  15239. + MMAL_DISPLAY_SET_ALPHA = 0x400,
  15240. +};
  15241. +
  15242. +struct mmal_parameter_displayregion {
  15243. + /** Bitfield that indicates which fields are set and should be
  15244. + * used. All other fields will maintain their current value.
  15245. + * \ref MMAL_DISPLAYSET_T defines the bits that can be
  15246. + * combined.
  15247. + */
  15248. + u32 set;
  15249. +
  15250. + /** Describes the display output device, with 0 typically
  15251. + * being a directly connected LCD display. The actual values
  15252. + * will depend on the hardware. Code using hard-wired numbers
  15253. + * (e.g. 2) is certain to fail.
  15254. + */
  15255. +
  15256. + u32 display_num;
  15257. + /** Indicates that we are using the full device screen area,
  15258. + * rather than a window of the display. If zero, then
  15259. + * dest_rect is used to specify a region of the display to
  15260. + * use.
  15261. + */
  15262. +
  15263. + s32 fullscreen;
  15264. + /** Indicates any rotation or flipping used to map frames onto
  15265. + * the natural display orientation.
  15266. + */
  15267. + u32 transform; /* enum mmal_parameter_displaytransform */
  15268. +
  15269. + /** Where to display the frame within the screen, if
  15270. + * fullscreen is zero.
  15271. + */
  15272. + struct vchiq_mmal_rect dest_rect;
  15273. +
  15274. + /** Indicates which area of the frame to display. If all
  15275. + * values are zero, the whole frame will be used.
  15276. + */
  15277. + struct vchiq_mmal_rect src_rect;
  15278. +
  15279. + /** If set to non-zero, indicates that any display scaling
  15280. + * should disregard the aspect ratio of the frame region being
  15281. + * displayed.
  15282. + */
  15283. + s32 noaspect;
  15284. +
  15285. + /** Indicates how the image should be scaled to fit the
  15286. + * display. \code MMAL_DISPLAY_MODE_FILL \endcode indicates
  15287. + * that the image should fill the screen by potentially
  15288. + * cropping the frames. Setting \code mode \endcode to \code
  15289. + * MMAL_DISPLAY_MODE_LETTERBOX \endcode indicates that all the
  15290. + * source region should be displayed and black bars added if
  15291. + * necessary.
  15292. + */
  15293. + u32 mode; /* enum mmal_parameter_displaymode */
  15294. +
  15295. + /** If non-zero, defines the width of a source pixel relative
  15296. + * to \code pixel_y \endcode. If zero, then pixels default to
  15297. + * being square.
  15298. + */
  15299. + u32 pixel_x;
  15300. +
  15301. + /** If non-zero, defines the height of a source pixel relative
  15302. + * to \code pixel_x \endcode. If zero, then pixels default to
  15303. + * being square.
  15304. + */
  15305. + u32 pixel_y;
  15306. +
  15307. + /** Sets the relative depth of the images, with greater values
  15308. + * being in front of smaller values.
  15309. + */
  15310. + u32 layer;
  15311. +
  15312. + /** Set to non-zero to ensure copy protection is used on
  15313. + * output.
  15314. + */
  15315. + s32 copyprotect_required;
  15316. +
  15317. + /** Level of opacity of the layer, where zero is fully
  15318. + * transparent and 255 is fully opaque.
  15319. + */
  15320. + u32 alpha;
  15321. +};
  15322. +
  15323. +#define MMAL_MAX_IMAGEFX_PARAMETERS 5
  15324. +
  15325. +struct mmal_parameter_imagefx_parameters {
  15326. + enum mmal_parameter_imagefx effect;
  15327. + u32 num_effect_params;
  15328. + u32 effect_parameter[MMAL_MAX_IMAGEFX_PARAMETERS];
  15329. +};
  15330. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-vchiq.c linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-vchiq.c
  15331. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-vchiq.c 1970-01-01 01:00:00.000000000 +0100
  15332. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-vchiq.c 2014-03-11 16:54:57.000000000 +0100
  15333. @@ -0,0 +1,1916 @@
  15334. +/*
  15335. + * Broadcom BM2835 V4L2 driver
  15336. + *
  15337. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  15338. + *
  15339. + * This file is subject to the terms and conditions of the GNU General Public
  15340. + * License. See the file COPYING in the main directory of this archive
  15341. + * for more details.
  15342. + *
  15343. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  15344. + * Dave Stevenson <dsteve@broadcom.com>
  15345. + * Simon Mellor <simellor@broadcom.com>
  15346. + * Luke Diamand <luked@broadcom.com>
  15347. + *
  15348. + * V4L2 driver MMAL vchiq interface code
  15349. + */
  15350. +
  15351. +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15352. +
  15353. +#include <linux/errno.h>
  15354. +#include <linux/kernel.h>
  15355. +#include <linux/mutex.h>
  15356. +#include <linux/mm.h>
  15357. +#include <linux/slab.h>
  15358. +#include <linux/completion.h>
  15359. +#include <linux/vmalloc.h>
  15360. +#include <asm/cacheflush.h>
  15361. +#include <media/videobuf2-vmalloc.h>
  15362. +
  15363. +#include "mmal-common.h"
  15364. +#include "mmal-vchiq.h"
  15365. +#include "mmal-msg.h"
  15366. +
  15367. +#define USE_VCHIQ_ARM
  15368. +#include "interface/vchi/vchi.h"
  15369. +
  15370. +/* maximum number of components supported */
  15371. +#define VCHIQ_MMAL_MAX_COMPONENTS 4
  15372. +
  15373. +/*#define FULL_MSG_DUMP 1*/
  15374. +
  15375. +#ifdef DEBUG
  15376. +static const char *const msg_type_names[] = {
  15377. + "UNKNOWN",
  15378. + "QUIT",
  15379. + "SERVICE_CLOSED",
  15380. + "GET_VERSION",
  15381. + "COMPONENT_CREATE",
  15382. + "COMPONENT_DESTROY",
  15383. + "COMPONENT_ENABLE",
  15384. + "COMPONENT_DISABLE",
  15385. + "PORT_INFO_GET",
  15386. + "PORT_INFO_SET",
  15387. + "PORT_ACTION",
  15388. + "BUFFER_FROM_HOST",
  15389. + "BUFFER_TO_HOST",
  15390. + "GET_STATS",
  15391. + "PORT_PARAMETER_SET",
  15392. + "PORT_PARAMETER_GET",
  15393. + "EVENT_TO_HOST",
  15394. + "GET_CORE_STATS_FOR_PORT",
  15395. + "OPAQUE_ALLOCATOR",
  15396. + "CONSUME_MEM",
  15397. + "LMK",
  15398. + "OPAQUE_ALLOCATOR_DESC",
  15399. + "DRM_GET_LHS32",
  15400. + "DRM_GET_TIME",
  15401. + "BUFFER_FROM_HOST_ZEROLEN",
  15402. + "PORT_FLUSH",
  15403. + "HOST_LOG",
  15404. +};
  15405. +#endif
  15406. +
  15407. +static const char *const port_action_type_names[] = {
  15408. + "UNKNOWN",
  15409. + "ENABLE",
  15410. + "DISABLE",
  15411. + "FLUSH",
  15412. + "CONNECT",
  15413. + "DISCONNECT",
  15414. + "SET_REQUIREMENTS",
  15415. +};
  15416. +
  15417. +#if defined(DEBUG)
  15418. +#if defined(FULL_MSG_DUMP)
  15419. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  15420. + do { \
  15421. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  15422. + msg_type_names[(MSG)->h.type], \
  15423. + (MSG)->h.type, (MSG_LEN)); \
  15424. + print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
  15425. + 16, 4, (MSG), \
  15426. + sizeof(struct mmal_msg_header), 1); \
  15427. + print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
  15428. + 16, 4, \
  15429. + ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
  15430. + (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
  15431. + } while (0)
  15432. +#else
  15433. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  15434. + { \
  15435. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  15436. + msg_type_names[(MSG)->h.type], \
  15437. + (MSG)->h.type, (MSG_LEN)); \
  15438. + }
  15439. +#endif
  15440. +#else
  15441. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
  15442. +#endif
  15443. +
  15444. +/* normal message context */
  15445. +struct mmal_msg_context {
  15446. + union {
  15447. + struct {
  15448. + /* work struct for defered callback - must come first */
  15449. + struct work_struct work;
  15450. + /* mmal instance */
  15451. + struct vchiq_mmal_instance *instance;
  15452. + /* mmal port */
  15453. + struct vchiq_mmal_port *port;
  15454. + /* actual buffer used to store bulk reply */
  15455. + struct mmal_buffer *buffer;
  15456. + /* amount of buffer used */
  15457. + unsigned long buffer_used;
  15458. + /* MMAL buffer flags */
  15459. + u32 mmal_flags;
  15460. + /* Presentation and Decode timestamps */
  15461. + s64 pts;
  15462. + s64 dts;
  15463. +
  15464. + int status; /* context status */
  15465. +
  15466. + } bulk; /* bulk data */
  15467. +
  15468. + struct {
  15469. + /* message handle to release */
  15470. + VCHI_HELD_MSG_T msg_handle;
  15471. + /* pointer to received message */
  15472. + struct mmal_msg *msg;
  15473. + /* received message length */
  15474. + u32 msg_len;
  15475. + /* completion upon reply */
  15476. + struct completion cmplt;
  15477. + } sync; /* synchronous response */
  15478. + } u;
  15479. +
  15480. +};
  15481. +
  15482. +struct vchiq_mmal_instance {
  15483. + VCHI_SERVICE_HANDLE_T handle;
  15484. +
  15485. + /* ensure serialised access to service */
  15486. + struct mutex vchiq_mutex;
  15487. +
  15488. + /* ensure serialised access to bulk operations */
  15489. + struct mutex bulk_mutex;
  15490. +
  15491. + /* vmalloc page to receive scratch bulk xfers into */
  15492. + void *bulk_scratch;
  15493. +
  15494. + /* component to use next */
  15495. + int component_idx;
  15496. + struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
  15497. +};
  15498. +
  15499. +static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
  15500. + *instance)
  15501. +{
  15502. + struct mmal_msg_context *msg_context;
  15503. +
  15504. + /* todo: should this be allocated from a pool to avoid kmalloc */
  15505. + msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
  15506. + memset(msg_context, 0, sizeof(*msg_context));
  15507. +
  15508. + return msg_context;
  15509. +}
  15510. +
  15511. +static void release_msg_context(struct mmal_msg_context *msg_context)
  15512. +{
  15513. + kfree(msg_context);
  15514. +}
  15515. +
  15516. +/* deals with receipt of event to host message */
  15517. +static void event_to_host_cb(struct vchiq_mmal_instance *instance,
  15518. + struct mmal_msg *msg, u32 msg_len)
  15519. +{
  15520. + pr_debug("unhandled event\n");
  15521. + pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
  15522. + msg->u.event_to_host.client_component,
  15523. + msg->u.event_to_host.port_type,
  15524. + msg->u.event_to_host.port_num,
  15525. + msg->u.event_to_host.cmd, msg->u.event_to_host.length);
  15526. +}
  15527. +
  15528. +/* workqueue scheduled callback
  15529. + *
  15530. + * we do this because it is important we do not call any other vchiq
  15531. + * sync calls from witin the message delivery thread
  15532. + */
  15533. +static void buffer_work_cb(struct work_struct *work)
  15534. +{
  15535. + struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
  15536. +
  15537. + msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
  15538. + msg_context->u.bulk.port,
  15539. + msg_context->u.bulk.status,
  15540. + msg_context->u.bulk.buffer,
  15541. + msg_context->u.bulk.buffer_used,
  15542. + msg_context->u.bulk.mmal_flags,
  15543. + msg_context->u.bulk.dts,
  15544. + msg_context->u.bulk.pts);
  15545. +
  15546. + /* release message context */
  15547. + release_msg_context(msg_context);
  15548. +}
  15549. +
  15550. +/* enqueue a bulk receive for a given message context */
  15551. +static int bulk_receive(struct vchiq_mmal_instance *instance,
  15552. + struct mmal_msg *msg,
  15553. + struct mmal_msg_context *msg_context)
  15554. +{
  15555. + unsigned long rd_len;
  15556. + unsigned long flags = 0;
  15557. + int ret;
  15558. +
  15559. + /* bulk mutex stops other bulk operations while we have a
  15560. + * receive in progress - released in callback
  15561. + */
  15562. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  15563. + if (ret != 0)
  15564. + return ret;
  15565. +
  15566. + rd_len = msg->u.buffer_from_host.buffer_header.length;
  15567. +
  15568. + /* take buffer from queue */
  15569. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  15570. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  15571. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  15572. + pr_err("buffer list empty trying to submit bulk receive\n");
  15573. +
  15574. + /* todo: this is a serious error, we should never have
  15575. + * commited a buffer_to_host operation to the mmal
  15576. + * port without the buffer to back it up (underflow
  15577. + * handling) and there is no obvious way to deal with
  15578. + * this - how is the mmal servie going to react when
  15579. + * we fail to do the xfer and reschedule a buffer when
  15580. + * it arrives? perhaps a starved flag to indicate a
  15581. + * waiting bulk receive?
  15582. + */
  15583. +
  15584. + mutex_unlock(&instance->bulk_mutex);
  15585. +
  15586. + return -EINVAL;
  15587. + }
  15588. +
  15589. + msg_context->u.bulk.buffer =
  15590. + list_entry(msg_context->u.bulk.port->buffers.next,
  15591. + struct mmal_buffer, list);
  15592. + list_del(&msg_context->u.bulk.buffer->list);
  15593. +
  15594. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  15595. +
  15596. + /* ensure we do not overrun the available buffer */
  15597. + if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
  15598. + rd_len = msg_context->u.bulk.buffer->buffer_size;
  15599. + pr_warn("short read as not enough receive buffer space\n");
  15600. + /* todo: is this the correct response, what happens to
  15601. + * the rest of the message data?
  15602. + */
  15603. + }
  15604. +
  15605. + /* store length */
  15606. + msg_context->u.bulk.buffer_used = rd_len;
  15607. + msg_context->u.bulk.mmal_flags =
  15608. + msg->u.buffer_from_host.buffer_header.flags;
  15609. + msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
  15610. + msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
  15611. +
  15612. + // only need to flush L1 cache here, as VCHIQ takes care of the L2
  15613. + // cache.
  15614. + __cpuc_flush_dcache_area(msg_context->u.bulk.buffer->buffer, rd_len);
  15615. +
  15616. + /* queue the bulk submission */
  15617. + vchi_service_use(instance->handle);
  15618. + ret = vchi_bulk_queue_receive(instance->handle,
  15619. + msg_context->u.bulk.buffer->buffer,
  15620. + /* Actual receive needs to be a multiple
  15621. + * of 4 bytes
  15622. + */
  15623. + (rd_len + 3) & ~3,
  15624. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  15625. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  15626. + msg_context);
  15627. +
  15628. + vchi_service_release(instance->handle);
  15629. +
  15630. + if (ret != 0) {
  15631. + /* callback will not be clearing the mutex */
  15632. + mutex_unlock(&instance->bulk_mutex);
  15633. + }
  15634. +
  15635. + return ret;
  15636. +}
  15637. +
  15638. +/* enque a dummy bulk receive for a given message context */
  15639. +static int dummy_bulk_receive(struct vchiq_mmal_instance *instance,
  15640. + struct mmal_msg_context *msg_context)
  15641. +{
  15642. + int ret;
  15643. +
  15644. + /* bulk mutex stops other bulk operations while we have a
  15645. + * receive in progress - released in callback
  15646. + */
  15647. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  15648. + if (ret != 0)
  15649. + return ret;
  15650. +
  15651. + /* zero length indicates this was a dummy transfer */
  15652. + msg_context->u.bulk.buffer_used = 0;
  15653. +
  15654. + /* queue the bulk submission */
  15655. + vchi_service_use(instance->handle);
  15656. +
  15657. + ret = vchi_bulk_queue_receive(instance->handle,
  15658. + instance->bulk_scratch,
  15659. + 8,
  15660. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  15661. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  15662. + msg_context);
  15663. +
  15664. + vchi_service_release(instance->handle);
  15665. +
  15666. + if (ret != 0) {
  15667. + /* callback will not be clearing the mutex */
  15668. + mutex_unlock(&instance->bulk_mutex);
  15669. + }
  15670. +
  15671. + return ret;
  15672. +}
  15673. +
  15674. +/* data in message, memcpy from packet into output buffer */
  15675. +static int inline_receive(struct vchiq_mmal_instance *instance,
  15676. + struct mmal_msg *msg,
  15677. + struct mmal_msg_context *msg_context)
  15678. +{
  15679. + unsigned long flags = 0;
  15680. +
  15681. + /* take buffer from queue */
  15682. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  15683. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  15684. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  15685. + pr_err("buffer list empty trying to receive inline\n");
  15686. +
  15687. + /* todo: this is a serious error, we should never have
  15688. + * commited a buffer_to_host operation to the mmal
  15689. + * port without the buffer to back it up (with
  15690. + * underflow handling) and there is no obvious way to
  15691. + * deal with this. Less bad than the bulk case as we
  15692. + * can just drop this on the floor but...unhelpful
  15693. + */
  15694. + return -EINVAL;
  15695. + }
  15696. +
  15697. + msg_context->u.bulk.buffer =
  15698. + list_entry(msg_context->u.bulk.port->buffers.next,
  15699. + struct mmal_buffer, list);
  15700. + list_del(&msg_context->u.bulk.buffer->list);
  15701. +
  15702. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  15703. +
  15704. + memcpy(msg_context->u.bulk.buffer->buffer,
  15705. + msg->u.buffer_from_host.short_data,
  15706. + msg->u.buffer_from_host.payload_in_message);
  15707. +
  15708. + msg_context->u.bulk.buffer_used =
  15709. + msg->u.buffer_from_host.payload_in_message;
  15710. +
  15711. + return 0;
  15712. +}
  15713. +
  15714. +/* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
  15715. +static int
  15716. +buffer_from_host(struct vchiq_mmal_instance *instance,
  15717. + struct vchiq_mmal_port *port, struct mmal_buffer *buf)
  15718. +{
  15719. + struct mmal_msg_context *msg_context;
  15720. + struct mmal_msg m;
  15721. + int ret;
  15722. +
  15723. + pr_debug("instance:%p buffer:%p\n", instance->handle, buf);
  15724. +
  15725. + /* bulk mutex stops other bulk operations while we
  15726. + * have a receive in progress
  15727. + */
  15728. + if (mutex_lock_interruptible(&instance->bulk_mutex))
  15729. + return -EINTR;
  15730. +
  15731. + /* get context */
  15732. + msg_context = get_msg_context(instance);
  15733. + if (msg_context == NULL)
  15734. + return -ENOMEM;
  15735. +
  15736. + /* store bulk message context for when data arrives */
  15737. + msg_context->u.bulk.instance = instance;
  15738. + msg_context->u.bulk.port = port;
  15739. + msg_context->u.bulk.buffer = NULL; /* not valid until bulk xfer */
  15740. + msg_context->u.bulk.buffer_used = 0;
  15741. +
  15742. + /* initialise work structure ready to schedule callback */
  15743. + INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
  15744. +
  15745. + /* prep the buffer from host message */
  15746. + memset(&m, 0xbc, sizeof(m)); /* just to make debug clearer */
  15747. +
  15748. + m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
  15749. + m.h.magic = MMAL_MAGIC;
  15750. + m.h.context = msg_context;
  15751. + m.h.status = 0;
  15752. +
  15753. + /* drvbuf is our private data passed back */
  15754. + m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
  15755. + m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
  15756. + m.u.buffer_from_host.drvbuf.port_handle = port->handle;
  15757. + m.u.buffer_from_host.drvbuf.client_context = msg_context;
  15758. +
  15759. + /* buffer header */
  15760. + m.u.buffer_from_host.buffer_header.cmd = 0;
  15761. + m.u.buffer_from_host.buffer_header.data = buf->buffer;
  15762. + m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
  15763. + m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
  15764. + m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
  15765. + m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */
  15766. + m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
  15767. + m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
  15768. +
  15769. + /* clear buffer type sepecific data */
  15770. + memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
  15771. + sizeof(m.u.buffer_from_host.buffer_header_type_specific));
  15772. +
  15773. + /* no payload in message */
  15774. + m.u.buffer_from_host.payload_in_message = 0;
  15775. +
  15776. + vchi_service_use(instance->handle);
  15777. +
  15778. + ret = vchi_msg_queue(instance->handle, &m,
  15779. + sizeof(struct mmal_msg_header) +
  15780. + sizeof(m.u.buffer_from_host),
  15781. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  15782. +
  15783. + if (ret != 0) {
  15784. + release_msg_context(msg_context);
  15785. + /* todo: is this correct error value? */
  15786. + }
  15787. +
  15788. + vchi_service_release(instance->handle);
  15789. +
  15790. + mutex_unlock(&instance->bulk_mutex);
  15791. +
  15792. + return ret;
  15793. +}
  15794. +
  15795. +/* submit a buffer to the mmal sevice
  15796. + *
  15797. + * the buffer_from_host uses size data from the ports next available
  15798. + * mmal_buffer and deals with there being no buffer available by
  15799. + * incrementing the underflow for later
  15800. + */
  15801. +static int port_buffer_from_host(struct vchiq_mmal_instance *instance,
  15802. + struct vchiq_mmal_port *port)
  15803. +{
  15804. + int ret;
  15805. + struct mmal_buffer *buf;
  15806. + unsigned long flags = 0;
  15807. +
  15808. + if (!port->enabled)
  15809. + return -EINVAL;
  15810. +
  15811. + /* peek buffer from queue */
  15812. + spin_lock_irqsave(&port->slock, flags);
  15813. + if (list_empty(&port->buffers)) {
  15814. + port->buffer_underflow++;
  15815. + spin_unlock_irqrestore(&port->slock, flags);
  15816. + return -ENOSPC;
  15817. + }
  15818. +
  15819. + buf = list_entry(port->buffers.next, struct mmal_buffer, list);
  15820. +
  15821. + spin_unlock_irqrestore(&port->slock, flags);
  15822. +
  15823. + /* issue buffer to mmal service */
  15824. + ret = buffer_from_host(instance, port, buf);
  15825. + if (ret) {
  15826. + pr_err("adding buffer header failed\n");
  15827. + /* todo: how should this be dealt with */
  15828. + }
  15829. +
  15830. + return ret;
  15831. +}
  15832. +
  15833. +/* deals with receipt of buffer to host message */
  15834. +static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
  15835. + struct mmal_msg *msg, u32 msg_len)
  15836. +{
  15837. + struct mmal_msg_context *msg_context;
  15838. +
  15839. + pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
  15840. + instance, msg, msg_len);
  15841. +
  15842. + if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
  15843. + msg_context = msg->u.buffer_from_host.drvbuf.client_context;
  15844. + } else {
  15845. + pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
  15846. + return;
  15847. + }
  15848. +
  15849. + if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
  15850. + /* message reception had an error */
  15851. + pr_warn("error %d in reply\n", msg->h.status);
  15852. +
  15853. + msg_context->u.bulk.status = msg->h.status;
  15854. +
  15855. + } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
  15856. + /* empty buffer */
  15857. + if (msg->u.buffer_from_host.buffer_header.flags &
  15858. + MMAL_BUFFER_HEADER_FLAG_EOS) {
  15859. + msg_context->u.bulk.status =
  15860. + dummy_bulk_receive(instance, msg_context);
  15861. + if (msg_context->u.bulk.status == 0)
  15862. + return; /* successful bulk submission, bulk
  15863. + * completion will trigger callback
  15864. + */
  15865. + } else {
  15866. + /* do callback with empty buffer - not EOS though */
  15867. + msg_context->u.bulk.status = 0;
  15868. + msg_context->u.bulk.buffer_used = 0;
  15869. + }
  15870. + } else if (msg->u.buffer_from_host.payload_in_message == 0) {
  15871. + /* data is not in message, queue a bulk receive */
  15872. + msg_context->u.bulk.status =
  15873. + bulk_receive(instance, msg, msg_context);
  15874. + if (msg_context->u.bulk.status == 0)
  15875. + return; /* successful bulk submission, bulk
  15876. + * completion will trigger callback
  15877. + */
  15878. +
  15879. + /* failed to submit buffer, this will end badly */
  15880. + pr_err("error %d on bulk submission\n",
  15881. + msg_context->u.bulk.status);
  15882. +
  15883. + } else if (msg->u.buffer_from_host.payload_in_message <=
  15884. + MMAL_VC_SHORT_DATA) {
  15885. + /* data payload within message */
  15886. + msg_context->u.bulk.status = inline_receive(instance, msg,
  15887. + msg_context);
  15888. + } else {
  15889. + pr_err("message with invalid short payload\n");
  15890. +
  15891. + /* signal error */
  15892. + msg_context->u.bulk.status = -EINVAL;
  15893. + msg_context->u.bulk.buffer_used =
  15894. + msg->u.buffer_from_host.payload_in_message;
  15895. + }
  15896. +
  15897. + /* replace the buffer header */
  15898. + port_buffer_from_host(instance, msg_context->u.bulk.port);
  15899. +
  15900. + /* schedule the port callback */
  15901. + schedule_work(&msg_context->u.bulk.work);
  15902. +}
  15903. +
  15904. +static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
  15905. + struct mmal_msg_context *msg_context)
  15906. +{
  15907. + /* bulk receive operation complete */
  15908. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  15909. +
  15910. + /* replace the buffer header */
  15911. + port_buffer_from_host(msg_context->u.bulk.instance,
  15912. + msg_context->u.bulk.port);
  15913. +
  15914. + msg_context->u.bulk.status = 0;
  15915. +
  15916. + /* schedule the port callback */
  15917. + schedule_work(&msg_context->u.bulk.work);
  15918. +}
  15919. +
  15920. +static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
  15921. + struct mmal_msg_context *msg_context)
  15922. +{
  15923. + pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
  15924. +
  15925. + /* bulk receive operation complete */
  15926. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  15927. +
  15928. + /* replace the buffer header */
  15929. + port_buffer_from_host(msg_context->u.bulk.instance,
  15930. + msg_context->u.bulk.port);
  15931. +
  15932. + msg_context->u.bulk.status = -EINTR;
  15933. +
  15934. + schedule_work(&msg_context->u.bulk.work);
  15935. +}
  15936. +
  15937. +/* incoming event service callback */
  15938. +static void service_callback(void *param,
  15939. + const VCHI_CALLBACK_REASON_T reason,
  15940. + void *bulk_ctx)
  15941. +{
  15942. + struct vchiq_mmal_instance *instance = param;
  15943. + int status;
  15944. + u32 msg_len;
  15945. + struct mmal_msg *msg;
  15946. + VCHI_HELD_MSG_T msg_handle;
  15947. +
  15948. + if (!instance) {
  15949. + pr_err("Message callback passed NULL instance\n");
  15950. + return;
  15951. + }
  15952. +
  15953. + switch (reason) {
  15954. + case VCHI_CALLBACK_MSG_AVAILABLE:
  15955. + status = vchi_msg_hold(instance->handle, (void **)&msg,
  15956. + &msg_len, VCHI_FLAGS_NONE, &msg_handle);
  15957. + if (status) {
  15958. + pr_err("Unable to dequeue a message (%d)\n", status);
  15959. + break;
  15960. + }
  15961. +
  15962. + DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
  15963. +
  15964. + /* handling is different for buffer messages */
  15965. + switch (msg->h.type) {
  15966. +
  15967. + case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
  15968. + vchi_held_msg_release(&msg_handle);
  15969. + break;
  15970. +
  15971. + case MMAL_MSG_TYPE_EVENT_TO_HOST:
  15972. + event_to_host_cb(instance, msg, msg_len);
  15973. + vchi_held_msg_release(&msg_handle);
  15974. +
  15975. + break;
  15976. +
  15977. + case MMAL_MSG_TYPE_BUFFER_TO_HOST:
  15978. + buffer_to_host_cb(instance, msg, msg_len);
  15979. + vchi_held_msg_release(&msg_handle);
  15980. + break;
  15981. +
  15982. + default:
  15983. + /* messages dependant on header context to complete */
  15984. +
  15985. + /* todo: the msg.context really ought to be sanity
  15986. + * checked before we just use it, afaict it comes back
  15987. + * and is used raw from the videocore. Perhaps it
  15988. + * should be verified the address lies in the kernel
  15989. + * address space.
  15990. + */
  15991. + if (msg->h.context == NULL) {
  15992. + pr_err("received message context was null!\n");
  15993. + vchi_held_msg_release(&msg_handle);
  15994. + break;
  15995. + }
  15996. +
  15997. + /* fill in context values */
  15998. + msg->h.context->u.sync.msg_handle = msg_handle;
  15999. + msg->h.context->u.sync.msg = msg;
  16000. + msg->h.context->u.sync.msg_len = msg_len;
  16001. +
  16002. + /* todo: should this check (completion_done()
  16003. + * == 1) for no one waiting? or do we need a
  16004. + * flag to tell us the completion has been
  16005. + * interrupted so we can free the message and
  16006. + * its context. This probably also solves the
  16007. + * message arriving after interruption todo
  16008. + * below
  16009. + */
  16010. +
  16011. + /* complete message so caller knows it happened */
  16012. + complete(&msg->h.context->u.sync.cmplt);
  16013. + break;
  16014. + }
  16015. +
  16016. + break;
  16017. +
  16018. + case VCHI_CALLBACK_BULK_RECEIVED:
  16019. + bulk_receive_cb(instance, bulk_ctx);
  16020. + break;
  16021. +
  16022. + case VCHI_CALLBACK_BULK_RECEIVE_ABORTED:
  16023. + bulk_abort_cb(instance, bulk_ctx);
  16024. + break;
  16025. +
  16026. + case VCHI_CALLBACK_SERVICE_CLOSED:
  16027. + /* TODO: consider if this requires action if received when
  16028. + * driver is not explicitly closing the service
  16029. + */
  16030. + break;
  16031. +
  16032. + default:
  16033. + pr_err("Received unhandled message reason %d\n", reason);
  16034. + break;
  16035. + }
  16036. +}
  16037. +
  16038. +static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
  16039. + struct mmal_msg *msg,
  16040. + unsigned int payload_len,
  16041. + struct mmal_msg **msg_out,
  16042. + VCHI_HELD_MSG_T *msg_handle_out)
  16043. +{
  16044. + struct mmal_msg_context msg_context;
  16045. + int ret;
  16046. +
  16047. + /* payload size must not cause message to exceed max size */
  16048. + if (payload_len >
  16049. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
  16050. + pr_err("payload length %d exceeds max:%d\n", payload_len,
  16051. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header)));
  16052. + return -EINVAL;
  16053. + }
  16054. +
  16055. + init_completion(&msg_context.u.sync.cmplt);
  16056. +
  16057. + msg->h.magic = MMAL_MAGIC;
  16058. + msg->h.context = &msg_context;
  16059. + msg->h.status = 0;
  16060. +
  16061. + DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
  16062. + ">>> sync message");
  16063. +
  16064. + vchi_service_use(instance->handle);
  16065. +
  16066. + ret = vchi_msg_queue(instance->handle,
  16067. + msg,
  16068. + sizeof(struct mmal_msg_header) + payload_len,
  16069. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  16070. +
  16071. + vchi_service_release(instance->handle);
  16072. +
  16073. + if (ret) {
  16074. + pr_err("error %d queuing message\n", ret);
  16075. + return ret;
  16076. + }
  16077. +
  16078. + ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, HZ);
  16079. + if (ret <= 0) {
  16080. + pr_err("error %d waiting for sync completion\n", ret);
  16081. + if (ret == 0)
  16082. + ret = -ETIME;
  16083. + /* todo: what happens if the message arrives after aborting */
  16084. + return ret;
  16085. + }
  16086. +
  16087. + *msg_out = msg_context.u.sync.msg;
  16088. + *msg_handle_out = msg_context.u.sync.msg_handle;
  16089. +
  16090. + return 0;
  16091. +}
  16092. +
  16093. +static void dump_port_info(struct vchiq_mmal_port *port)
  16094. +{
  16095. + pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
  16096. +
  16097. + pr_debug("buffer minimum num:%d size:%d align:%d\n",
  16098. + port->minimum_buffer.num,
  16099. + port->minimum_buffer.size, port->minimum_buffer.alignment);
  16100. +
  16101. + pr_debug("buffer recommended num:%d size:%d align:%d\n",
  16102. + port->recommended_buffer.num,
  16103. + port->recommended_buffer.size,
  16104. + port->recommended_buffer.alignment);
  16105. +
  16106. + pr_debug("buffer current values num:%d size:%d align:%d\n",
  16107. + port->current_buffer.num,
  16108. + port->current_buffer.size, port->current_buffer.alignment);
  16109. +
  16110. + pr_debug("elementry stream: type:%d encoding:0x%x varient:0x%x\n",
  16111. + port->format.type,
  16112. + port->format.encoding, port->format.encoding_variant);
  16113. +
  16114. + pr_debug(" bitrate:%d flags:0x%x\n",
  16115. + port->format.bitrate, port->format.flags);
  16116. +
  16117. + if (port->format.type == MMAL_ES_TYPE_VIDEO) {
  16118. + pr_debug
  16119. + ("es video format: width:%d height:%d colourspace:0x%x\n",
  16120. + port->es.video.width, port->es.video.height,
  16121. + port->es.video.color_space);
  16122. +
  16123. + pr_debug(" : crop xywh %d,%d,%d,%d\n",
  16124. + port->es.video.crop.x,
  16125. + port->es.video.crop.y,
  16126. + port->es.video.crop.width, port->es.video.crop.height);
  16127. + pr_debug(" : framerate %d/%d aspect %d/%d\n",
  16128. + port->es.video.frame_rate.num,
  16129. + port->es.video.frame_rate.den,
  16130. + port->es.video.par.num, port->es.video.par.den);
  16131. + }
  16132. +}
  16133. +
  16134. +static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
  16135. +{
  16136. +
  16137. + /* todo do readonly fields need setting at all? */
  16138. + p->type = port->type;
  16139. + p->index = port->index;
  16140. + p->index_all = 0;
  16141. + p->is_enabled = port->enabled;
  16142. + p->buffer_num_min = port->minimum_buffer.num;
  16143. + p->buffer_size_min = port->minimum_buffer.size;
  16144. + p->buffer_alignment_min = port->minimum_buffer.alignment;
  16145. + p->buffer_num_recommended = port->recommended_buffer.num;
  16146. + p->buffer_size_recommended = port->recommended_buffer.size;
  16147. +
  16148. + /* only three writable fields in a port */
  16149. + p->buffer_num = port->current_buffer.num;
  16150. + p->buffer_size = port->current_buffer.size;
  16151. + p->userdata = port;
  16152. +}
  16153. +
  16154. +static int port_info_set(struct vchiq_mmal_instance *instance,
  16155. + struct vchiq_mmal_port *port)
  16156. +{
  16157. + int ret;
  16158. + struct mmal_msg m;
  16159. + struct mmal_msg *rmsg;
  16160. + VCHI_HELD_MSG_T rmsg_handle;
  16161. +
  16162. + pr_debug("setting port info port %p\n", port);
  16163. + if (!port)
  16164. + return -1;
  16165. + dump_port_info(port);
  16166. +
  16167. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
  16168. +
  16169. + m.u.port_info_set.component_handle = port->component->handle;
  16170. + m.u.port_info_set.port_type = port->type;
  16171. + m.u.port_info_set.port_index = port->index;
  16172. +
  16173. + port_to_mmal_msg(port, &m.u.port_info_set.port);
  16174. +
  16175. + /* elementry stream format setup */
  16176. + m.u.port_info_set.format.type = port->format.type;
  16177. + m.u.port_info_set.format.encoding = port->format.encoding;
  16178. + m.u.port_info_set.format.encoding_variant =
  16179. + port->format.encoding_variant;
  16180. + m.u.port_info_set.format.bitrate = port->format.bitrate;
  16181. + m.u.port_info_set.format.flags = port->format.flags;
  16182. +
  16183. + memcpy(&m.u.port_info_set.es, &port->es,
  16184. + sizeof(union mmal_es_specific_format));
  16185. +
  16186. + m.u.port_info_set.format.extradata_size = port->format.extradata_size;
  16187. + memcpy(rmsg->u.port_info_set.extradata, port->format.extradata,
  16188. + port->format.extradata_size);
  16189. +
  16190. + ret = send_synchronous_mmal_msg(instance, &m,
  16191. + sizeof(m.u.port_info_set),
  16192. + &rmsg, &rmsg_handle);
  16193. + if (ret)
  16194. + return ret;
  16195. +
  16196. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
  16197. + /* got an unexpected message type in reply */
  16198. + ret = -EINVAL;
  16199. + goto release_msg;
  16200. + }
  16201. +
  16202. + /* return operation status */
  16203. + ret = -rmsg->u.port_info_get_reply.status;
  16204. +
  16205. + pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
  16206. + port->component->handle, port->handle);
  16207. +
  16208. +release_msg:
  16209. + vchi_held_msg_release(&rmsg_handle);
  16210. +
  16211. + return ret;
  16212. +
  16213. +}
  16214. +
  16215. +/* use port info get message to retrive port information */
  16216. +static int port_info_get(struct vchiq_mmal_instance *instance,
  16217. + struct vchiq_mmal_port *port)
  16218. +{
  16219. + int ret;
  16220. + struct mmal_msg m;
  16221. + struct mmal_msg *rmsg;
  16222. + VCHI_HELD_MSG_T rmsg_handle;
  16223. +
  16224. + /* port info time */
  16225. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
  16226. + m.u.port_info_get.component_handle = port->component->handle;
  16227. + m.u.port_info_get.port_type = port->type;
  16228. + m.u.port_info_get.index = port->index;
  16229. +
  16230. + ret = send_synchronous_mmal_msg(instance, &m,
  16231. + sizeof(m.u.port_info_get),
  16232. + &rmsg, &rmsg_handle);
  16233. + if (ret)
  16234. + return ret;
  16235. +
  16236. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
  16237. + /* got an unexpected message type in reply */
  16238. + ret = -EINVAL;
  16239. + goto release_msg;
  16240. + }
  16241. +
  16242. + /* return operation status */
  16243. + ret = -rmsg->u.port_info_get_reply.status;
  16244. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  16245. + goto release_msg;
  16246. +
  16247. + if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
  16248. + port->enabled = false;
  16249. + else
  16250. + port->enabled = true;
  16251. +
  16252. + /* copy the values out of the message */
  16253. + port->handle = rmsg->u.port_info_get_reply.port_handle;
  16254. +
  16255. + /* port type and index cached to use on port info set becuase
  16256. + * it does not use a port handle
  16257. + */
  16258. + port->type = rmsg->u.port_info_get_reply.port_type;
  16259. + port->index = rmsg->u.port_info_get_reply.port_index;
  16260. +
  16261. + port->minimum_buffer.num =
  16262. + rmsg->u.port_info_get_reply.port.buffer_num_min;
  16263. + port->minimum_buffer.size =
  16264. + rmsg->u.port_info_get_reply.port.buffer_size_min;
  16265. + port->minimum_buffer.alignment =
  16266. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  16267. +
  16268. + port->recommended_buffer.alignment =
  16269. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  16270. + port->recommended_buffer.num =
  16271. + rmsg->u.port_info_get_reply.port.buffer_num_recommended;
  16272. +
  16273. + port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
  16274. + port->current_buffer.size =
  16275. + rmsg->u.port_info_get_reply.port.buffer_size;
  16276. +
  16277. + /* stream format */
  16278. + port->format.type = rmsg->u.port_info_get_reply.format.type;
  16279. + port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
  16280. + port->format.encoding_variant =
  16281. + rmsg->u.port_info_get_reply.format.encoding_variant;
  16282. + port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
  16283. + port->format.flags = rmsg->u.port_info_get_reply.format.flags;
  16284. +
  16285. + /* elementry stream format */
  16286. + memcpy(&port->es,
  16287. + &rmsg->u.port_info_get_reply.es,
  16288. + sizeof(union mmal_es_specific_format));
  16289. + port->format.es = &port->es;
  16290. +
  16291. + port->format.extradata_size =
  16292. + rmsg->u.port_info_get_reply.format.extradata_size;
  16293. + memcpy(port->format.extradata,
  16294. + rmsg->u.port_info_get_reply.extradata,
  16295. + port->format.extradata_size);
  16296. +
  16297. + pr_debug("received port info\n");
  16298. + dump_port_info(port);
  16299. +
  16300. +release_msg:
  16301. +
  16302. + pr_debug("%s:result:%d component:0x%x port:%d\n",
  16303. + __func__, ret, port->component->handle, port->handle);
  16304. +
  16305. + vchi_held_msg_release(&rmsg_handle);
  16306. +
  16307. + return ret;
  16308. +}
  16309. +
  16310. +/* create comonent on vc */
  16311. +static int create_component(struct vchiq_mmal_instance *instance,
  16312. + struct vchiq_mmal_component *component,
  16313. + const char *name)
  16314. +{
  16315. + int ret;
  16316. + struct mmal_msg m;
  16317. + struct mmal_msg *rmsg;
  16318. + VCHI_HELD_MSG_T rmsg_handle;
  16319. +
  16320. + /* build component create message */
  16321. + m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
  16322. + m.u.component_create.client_component = component;
  16323. + strncpy(m.u.component_create.name, name,
  16324. + sizeof(m.u.component_create.name));
  16325. +
  16326. + ret = send_synchronous_mmal_msg(instance, &m,
  16327. + sizeof(m.u.component_create),
  16328. + &rmsg, &rmsg_handle);
  16329. + if (ret)
  16330. + return ret;
  16331. +
  16332. + if (rmsg->h.type != m.h.type) {
  16333. + /* got an unexpected message type in reply */
  16334. + ret = -EINVAL;
  16335. + goto release_msg;
  16336. + }
  16337. +
  16338. + ret = -rmsg->u.component_create_reply.status;
  16339. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  16340. + goto release_msg;
  16341. +
  16342. + /* a valid component response received */
  16343. + component->handle = rmsg->u.component_create_reply.component_handle;
  16344. + component->inputs = rmsg->u.component_create_reply.input_num;
  16345. + component->outputs = rmsg->u.component_create_reply.output_num;
  16346. + component->clocks = rmsg->u.component_create_reply.clock_num;
  16347. +
  16348. + pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
  16349. + component->handle,
  16350. + component->inputs, component->outputs, component->clocks);
  16351. +
  16352. +release_msg:
  16353. + vchi_held_msg_release(&rmsg_handle);
  16354. +
  16355. + return ret;
  16356. +}
  16357. +
  16358. +/* destroys a component on vc */
  16359. +static int destroy_component(struct vchiq_mmal_instance *instance,
  16360. + struct vchiq_mmal_component *component)
  16361. +{
  16362. + int ret;
  16363. + struct mmal_msg m;
  16364. + struct mmal_msg *rmsg;
  16365. + VCHI_HELD_MSG_T rmsg_handle;
  16366. +
  16367. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
  16368. + m.u.component_destroy.component_handle = component->handle;
  16369. +
  16370. + ret = send_synchronous_mmal_msg(instance, &m,
  16371. + sizeof(m.u.component_destroy),
  16372. + &rmsg, &rmsg_handle);
  16373. + if (ret)
  16374. + return ret;
  16375. +
  16376. + if (rmsg->h.type != m.h.type) {
  16377. + /* got an unexpected message type in reply */
  16378. + ret = -EINVAL;
  16379. + goto release_msg;
  16380. + }
  16381. +
  16382. + ret = -rmsg->u.component_destroy_reply.status;
  16383. +
  16384. +release_msg:
  16385. +
  16386. + vchi_held_msg_release(&rmsg_handle);
  16387. +
  16388. + return ret;
  16389. +}
  16390. +
  16391. +/* enable a component on vc */
  16392. +static int enable_component(struct vchiq_mmal_instance *instance,
  16393. + struct vchiq_mmal_component *component)
  16394. +{
  16395. + int ret;
  16396. + struct mmal_msg m;
  16397. + struct mmal_msg *rmsg;
  16398. + VCHI_HELD_MSG_T rmsg_handle;
  16399. +
  16400. + m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
  16401. + m.u.component_enable.component_handle = component->handle;
  16402. +
  16403. + ret = send_synchronous_mmal_msg(instance, &m,
  16404. + sizeof(m.u.component_enable),
  16405. + &rmsg, &rmsg_handle);
  16406. + if (ret)
  16407. + return ret;
  16408. +
  16409. + if (rmsg->h.type != m.h.type) {
  16410. + /* got an unexpected message type in reply */
  16411. + ret = -EINVAL;
  16412. + goto release_msg;
  16413. + }
  16414. +
  16415. + ret = -rmsg->u.component_enable_reply.status;
  16416. +
  16417. +release_msg:
  16418. + vchi_held_msg_release(&rmsg_handle);
  16419. +
  16420. + return ret;
  16421. +}
  16422. +
  16423. +/* disable a component on vc */
  16424. +static int disable_component(struct vchiq_mmal_instance *instance,
  16425. + struct vchiq_mmal_component *component)
  16426. +{
  16427. + int ret;
  16428. + struct mmal_msg m;
  16429. + struct mmal_msg *rmsg;
  16430. + VCHI_HELD_MSG_T rmsg_handle;
  16431. +
  16432. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
  16433. + m.u.component_disable.component_handle = component->handle;
  16434. +
  16435. + ret = send_synchronous_mmal_msg(instance, &m,
  16436. + sizeof(m.u.component_disable),
  16437. + &rmsg, &rmsg_handle);
  16438. + if (ret)
  16439. + return ret;
  16440. +
  16441. + if (rmsg->h.type != m.h.type) {
  16442. + /* got an unexpected message type in reply */
  16443. + ret = -EINVAL;
  16444. + goto release_msg;
  16445. + }
  16446. +
  16447. + ret = -rmsg->u.component_disable_reply.status;
  16448. +
  16449. +release_msg:
  16450. +
  16451. + vchi_held_msg_release(&rmsg_handle);
  16452. +
  16453. + return ret;
  16454. +}
  16455. +
  16456. +/* get version of mmal implementation */
  16457. +static int get_version(struct vchiq_mmal_instance *instance,
  16458. + u32 *major_out, u32 *minor_out)
  16459. +{
  16460. + int ret;
  16461. + struct mmal_msg m;
  16462. + struct mmal_msg *rmsg;
  16463. + VCHI_HELD_MSG_T rmsg_handle;
  16464. +
  16465. + m.h.type = MMAL_MSG_TYPE_GET_VERSION;
  16466. +
  16467. + ret = send_synchronous_mmal_msg(instance, &m,
  16468. + sizeof(m.u.version),
  16469. + &rmsg, &rmsg_handle);
  16470. + if (ret)
  16471. + return ret;
  16472. +
  16473. + if (rmsg->h.type != m.h.type) {
  16474. + /* got an unexpected message type in reply */
  16475. + ret = -EINVAL;
  16476. + goto release_msg;
  16477. + }
  16478. +
  16479. + *major_out = rmsg->u.version.major;
  16480. + *minor_out = rmsg->u.version.minor;
  16481. +
  16482. +release_msg:
  16483. + vchi_held_msg_release(&rmsg_handle);
  16484. +
  16485. + return ret;
  16486. +}
  16487. +
  16488. +/* do a port action with a port as a parameter */
  16489. +static int port_action_port(struct vchiq_mmal_instance *instance,
  16490. + struct vchiq_mmal_port *port,
  16491. + enum mmal_msg_port_action_type action_type)
  16492. +{
  16493. + int ret;
  16494. + struct mmal_msg m;
  16495. + struct mmal_msg *rmsg;
  16496. + VCHI_HELD_MSG_T rmsg_handle;
  16497. +
  16498. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  16499. + m.u.port_action_port.component_handle = port->component->handle;
  16500. + m.u.port_action_port.port_handle = port->handle;
  16501. + m.u.port_action_port.action = action_type;
  16502. +
  16503. + port_to_mmal_msg(port, &m.u.port_action_port.port);
  16504. +
  16505. + ret = send_synchronous_mmal_msg(instance, &m,
  16506. + sizeof(m.u.port_action_port),
  16507. + &rmsg, &rmsg_handle);
  16508. + if (ret)
  16509. + return ret;
  16510. +
  16511. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  16512. + /* got an unexpected message type in reply */
  16513. + ret = -EINVAL;
  16514. + goto release_msg;
  16515. + }
  16516. +
  16517. + ret = -rmsg->u.port_action_reply.status;
  16518. +
  16519. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
  16520. + __func__,
  16521. + ret, port->component->handle, port->handle,
  16522. + port_action_type_names[action_type], action_type);
  16523. +
  16524. +release_msg:
  16525. + vchi_held_msg_release(&rmsg_handle);
  16526. +
  16527. + return ret;
  16528. +}
  16529. +
  16530. +/* do a port action with handles as parameters */
  16531. +static int port_action_handle(struct vchiq_mmal_instance *instance,
  16532. + struct vchiq_mmal_port *port,
  16533. + enum mmal_msg_port_action_type action_type,
  16534. + u32 connect_component_handle,
  16535. + u32 connect_port_handle)
  16536. +{
  16537. + int ret;
  16538. + struct mmal_msg m;
  16539. + struct mmal_msg *rmsg;
  16540. + VCHI_HELD_MSG_T rmsg_handle;
  16541. +
  16542. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  16543. +
  16544. + m.u.port_action_handle.component_handle = port->component->handle;
  16545. + m.u.port_action_handle.port_handle = port->handle;
  16546. + m.u.port_action_handle.action = action_type;
  16547. +
  16548. + m.u.port_action_handle.connect_component_handle =
  16549. + connect_component_handle;
  16550. + m.u.port_action_handle.connect_port_handle = connect_port_handle;
  16551. +
  16552. + ret = send_synchronous_mmal_msg(instance, &m,
  16553. + sizeof(m.u.port_action_handle),
  16554. + &rmsg, &rmsg_handle);
  16555. + if (ret)
  16556. + return ret;
  16557. +
  16558. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  16559. + /* got an unexpected message type in reply */
  16560. + ret = -EINVAL;
  16561. + goto release_msg;
  16562. + }
  16563. +
  16564. + ret = -rmsg->u.port_action_reply.status;
  16565. +
  16566. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)" \
  16567. + " connect component:0x%x connect port:%d\n",
  16568. + __func__,
  16569. + ret, port->component->handle, port->handle,
  16570. + port_action_type_names[action_type],
  16571. + action_type, connect_component_handle, connect_port_handle);
  16572. +
  16573. +release_msg:
  16574. + vchi_held_msg_release(&rmsg_handle);
  16575. +
  16576. + return ret;
  16577. +}
  16578. +
  16579. +static int port_parameter_set(struct vchiq_mmal_instance *instance,
  16580. + struct vchiq_mmal_port *port,
  16581. + u32 parameter_id, void *value, u32 value_size)
  16582. +{
  16583. + int ret;
  16584. + struct mmal_msg m;
  16585. + struct mmal_msg *rmsg;
  16586. + VCHI_HELD_MSG_T rmsg_handle;
  16587. +
  16588. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
  16589. +
  16590. + m.u.port_parameter_set.component_handle = port->component->handle;
  16591. + m.u.port_parameter_set.port_handle = port->handle;
  16592. + m.u.port_parameter_set.id = parameter_id;
  16593. + m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
  16594. + memcpy(&m.u.port_parameter_set.value, value, value_size);
  16595. +
  16596. + ret = send_synchronous_mmal_msg(instance, &m,
  16597. + (4 * sizeof(u32)) + value_size,
  16598. + &rmsg, &rmsg_handle);
  16599. + if (ret)
  16600. + return ret;
  16601. +
  16602. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
  16603. + /* got an unexpected message type in reply */
  16604. + ret = -EINVAL;
  16605. + goto release_msg;
  16606. + }
  16607. +
  16608. + ret = -rmsg->u.port_parameter_set_reply.status;
  16609. +
  16610. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
  16611. + __func__,
  16612. + ret, port->component->handle, port->handle, parameter_id);
  16613. +
  16614. +release_msg:
  16615. + vchi_held_msg_release(&rmsg_handle);
  16616. +
  16617. + return ret;
  16618. +}
  16619. +
  16620. +static int port_parameter_get(struct vchiq_mmal_instance *instance,
  16621. + struct vchiq_mmal_port *port,
  16622. + u32 parameter_id, void *value, u32 *value_size)
  16623. +{
  16624. + int ret;
  16625. + struct mmal_msg m;
  16626. + struct mmal_msg *rmsg;
  16627. + VCHI_HELD_MSG_T rmsg_handle;
  16628. +
  16629. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
  16630. +
  16631. + m.u.port_parameter_get.component_handle = port->component->handle;
  16632. + m.u.port_parameter_get.port_handle = port->handle;
  16633. + m.u.port_parameter_get.id = parameter_id;
  16634. + m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
  16635. +
  16636. + ret = send_synchronous_mmal_msg(instance, &m,
  16637. + sizeof(struct
  16638. + mmal_msg_port_parameter_get),
  16639. + &rmsg, &rmsg_handle);
  16640. + if (ret)
  16641. + return ret;
  16642. +
  16643. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
  16644. + /* got an unexpected message type in reply */
  16645. + pr_err("Incorrect reply type %d\n", rmsg->h.type);
  16646. + ret = -EINVAL;
  16647. + goto release_msg;
  16648. + }
  16649. +
  16650. + ret = -rmsg->u.port_parameter_get_reply.status;
  16651. + if (ret) {
  16652. + /* Copy only as much as we have space for
  16653. + * but report true size of parameter
  16654. + */
  16655. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  16656. + *value_size);
  16657. + *value_size = rmsg->u.port_parameter_get_reply.size;
  16658. + } else
  16659. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  16660. + rmsg->u.port_parameter_get_reply.size);
  16661. +
  16662. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
  16663. + ret, port->component->handle, port->handle, parameter_id);
  16664. +
  16665. +release_msg:
  16666. + vchi_held_msg_release(&rmsg_handle);
  16667. +
  16668. + return ret;
  16669. +}
  16670. +
  16671. +/* disables a port and drains buffers from it */
  16672. +static int port_disable(struct vchiq_mmal_instance *instance,
  16673. + struct vchiq_mmal_port *port)
  16674. +{
  16675. + int ret;
  16676. + struct list_head *q, *buf_head;
  16677. + unsigned long flags = 0;
  16678. +
  16679. + if (!port->enabled)
  16680. + return 0;
  16681. +
  16682. + port->enabled = false;
  16683. +
  16684. + ret = port_action_port(instance, port,
  16685. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
  16686. + if (ret == 0) {
  16687. +
  16688. + /* drain all queued buffers on port */
  16689. + spin_lock_irqsave(&port->slock, flags);
  16690. +
  16691. + list_for_each_safe(buf_head, q, &port->buffers) {
  16692. + struct mmal_buffer *mmalbuf;
  16693. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  16694. + list);
  16695. + list_del(buf_head);
  16696. + if (port->buffer_cb)
  16697. + port->buffer_cb(instance,
  16698. + port, 0, mmalbuf, 0, 0,
  16699. + MMAL_TIME_UNKNOWN,
  16700. + MMAL_TIME_UNKNOWN);
  16701. + }
  16702. +
  16703. + spin_unlock_irqrestore(&port->slock, flags);
  16704. +
  16705. + ret = port_info_get(instance, port);
  16706. + }
  16707. +
  16708. + return ret;
  16709. +}
  16710. +
  16711. +/* enable a port */
  16712. +static int port_enable(struct vchiq_mmal_instance *instance,
  16713. + struct vchiq_mmal_port *port)
  16714. +{
  16715. + unsigned int hdr_count;
  16716. + struct list_head *buf_head;
  16717. + int ret;
  16718. +
  16719. + if (port->enabled)
  16720. + return 0;
  16721. +
  16722. + /* ensure there are enough buffers queued to cover the buffer headers */
  16723. + if (port->buffer_cb != NULL) {
  16724. + hdr_count = 0;
  16725. + list_for_each(buf_head, &port->buffers) {
  16726. + hdr_count++;
  16727. + }
  16728. + if (hdr_count < port->current_buffer.num)
  16729. + return -ENOSPC;
  16730. + }
  16731. +
  16732. + ret = port_action_port(instance, port,
  16733. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
  16734. + if (ret)
  16735. + goto done;
  16736. +
  16737. + port->enabled = true;
  16738. +
  16739. + if (port->buffer_cb) {
  16740. + /* send buffer headers to videocore */
  16741. + hdr_count = 1;
  16742. + list_for_each(buf_head, &port->buffers) {
  16743. + struct mmal_buffer *mmalbuf;
  16744. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  16745. + list);
  16746. + ret = buffer_from_host(instance, port, mmalbuf);
  16747. + if (ret)
  16748. + goto done;
  16749. +
  16750. + hdr_count++;
  16751. + if (hdr_count > port->current_buffer.num)
  16752. + break;
  16753. + }
  16754. + }
  16755. +
  16756. + ret = port_info_get(instance, port);
  16757. +
  16758. +done:
  16759. + return ret;
  16760. +}
  16761. +
  16762. +/* ------------------------------------------------------------------
  16763. + * Exported API
  16764. + *------------------------------------------------------------------*/
  16765. +
  16766. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  16767. + struct vchiq_mmal_port *port)
  16768. +{
  16769. + int ret;
  16770. +
  16771. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  16772. + return -EINTR;
  16773. +
  16774. + ret = port_info_set(instance, port);
  16775. + if (ret)
  16776. + goto release_unlock;
  16777. +
  16778. + /* read what has actually been set */
  16779. + ret = port_info_get(instance, port);
  16780. +
  16781. +release_unlock:
  16782. + mutex_unlock(&instance->vchiq_mutex);
  16783. +
  16784. + return ret;
  16785. +
  16786. +}
  16787. +
  16788. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  16789. + struct vchiq_mmal_port *port,
  16790. + u32 parameter, void *value, u32 value_size)
  16791. +{
  16792. + int ret;
  16793. +
  16794. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  16795. + return -EINTR;
  16796. +
  16797. + ret = port_parameter_set(instance, port, parameter, value, value_size);
  16798. +
  16799. + mutex_unlock(&instance->vchiq_mutex);
  16800. +
  16801. + return ret;
  16802. +}
  16803. +
  16804. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  16805. + struct vchiq_mmal_port *port,
  16806. + u32 parameter, void *value, u32 *value_size)
  16807. +{
  16808. + int ret;
  16809. +
  16810. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  16811. + return -EINTR;
  16812. +
  16813. + ret = port_parameter_get(instance, port, parameter, value, value_size);
  16814. +
  16815. + mutex_unlock(&instance->vchiq_mutex);
  16816. +
  16817. + return ret;
  16818. +}
  16819. +
  16820. +/* enable a port
  16821. + *
  16822. + * enables a port and queues buffers for satisfying callbacks if we
  16823. + * provide a callback handler
  16824. + */
  16825. +int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
  16826. + struct vchiq_mmal_port *port,
  16827. + vchiq_mmal_buffer_cb buffer_cb)
  16828. +{
  16829. + int ret;
  16830. +
  16831. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  16832. + return -EINTR;
  16833. +
  16834. + /* already enabled - noop */
  16835. + if (port->enabled) {
  16836. + ret = 0;
  16837. + goto unlock;
  16838. + }
  16839. +
  16840. + port->buffer_cb = buffer_cb;
  16841. +
  16842. + ret = port_enable(instance, port);
  16843. +
  16844. +unlock:
  16845. + mutex_unlock(&instance->vchiq_mutex);
  16846. +
  16847. + return ret;
  16848. +}
  16849. +
  16850. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  16851. + struct vchiq_mmal_port *port)
  16852. +{
  16853. + int ret;
  16854. +
  16855. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  16856. + return -EINTR;
  16857. +
  16858. + if (!port->enabled) {
  16859. + mutex_unlock(&instance->vchiq_mutex);
  16860. + return 0;
  16861. + }
  16862. +
  16863. + ret = port_disable(instance, port);
  16864. +
  16865. + mutex_unlock(&instance->vchiq_mutex);
  16866. +
  16867. + return ret;
  16868. +}
  16869. +
  16870. +/* ports will be connected in a tunneled manner so data buffers
  16871. + * are not handled by client.
  16872. + */
  16873. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  16874. + struct vchiq_mmal_port *src,
  16875. + struct vchiq_mmal_port *dst)
  16876. +{
  16877. + int ret;
  16878. +
  16879. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  16880. + return -EINTR;
  16881. +
  16882. + /* disconnect ports if connected */
  16883. + if (src->connected != NULL) {
  16884. + ret = port_disable(instance, src);
  16885. + if (ret) {
  16886. + pr_err("failed disabling src port(%d)\n", ret);
  16887. + goto release_unlock;
  16888. + }
  16889. +
  16890. + /* do not need to disable the destination port as they
  16891. + * are connected and it is done automatically
  16892. + */
  16893. +
  16894. + ret = port_action_handle(instance, src,
  16895. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
  16896. + src->connected->component->handle,
  16897. + src->connected->handle);
  16898. + if (ret < 0) {
  16899. + pr_err("failed disconnecting src port\n");
  16900. + goto release_unlock;
  16901. + }
  16902. + src->connected->enabled = false;
  16903. + src->connected = NULL;
  16904. + }
  16905. +
  16906. + if (dst == NULL) {
  16907. + /* do not make new connection */
  16908. + ret = 0;
  16909. + pr_debug("not making new connection\n");
  16910. + goto release_unlock;
  16911. + }
  16912. +
  16913. + /* copy src port format to dst */
  16914. + dst->format.encoding = src->format.encoding;
  16915. + dst->es.video.width = src->es.video.width;
  16916. + dst->es.video.height = src->es.video.height;
  16917. + dst->es.video.crop.x = src->es.video.crop.x;
  16918. + dst->es.video.crop.y = src->es.video.crop.y;
  16919. + dst->es.video.crop.width = src->es.video.crop.width;
  16920. + dst->es.video.crop.height = src->es.video.crop.height;
  16921. + dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
  16922. + dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
  16923. +
  16924. + /* set new format */
  16925. + ret = port_info_set(instance, dst);
  16926. + if (ret) {
  16927. + pr_debug("setting port info failed\n");
  16928. + goto release_unlock;
  16929. + }
  16930. +
  16931. + /* read what has actually been set */
  16932. + ret = port_info_get(instance, dst);
  16933. + if (ret) {
  16934. + pr_debug("read back port info failed\n");
  16935. + goto release_unlock;
  16936. + }
  16937. +
  16938. + /* connect two ports together */
  16939. + ret = port_action_handle(instance, src,
  16940. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
  16941. + dst->component->handle, dst->handle);
  16942. + if (ret < 0) {
  16943. + pr_debug("connecting port %d:%d to %d:%d failed\n",
  16944. + src->component->handle, src->handle,
  16945. + dst->component->handle, dst->handle);
  16946. + goto release_unlock;
  16947. + }
  16948. + src->connected = dst;
  16949. +
  16950. +release_unlock:
  16951. +
  16952. + mutex_unlock(&instance->vchiq_mutex);
  16953. +
  16954. + return ret;
  16955. +}
  16956. +
  16957. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  16958. + struct vchiq_mmal_port *port,
  16959. + struct mmal_buffer *buffer)
  16960. +{
  16961. + unsigned long flags = 0;
  16962. +
  16963. + spin_lock_irqsave(&port->slock, flags);
  16964. + list_add_tail(&buffer->list, &port->buffers);
  16965. + spin_unlock_irqrestore(&port->slock, flags);
  16966. +
  16967. + /* the port previously underflowed because it was missing a
  16968. + * mmal_buffer which has just been added, submit that buffer
  16969. + * to the mmal service.
  16970. + */
  16971. + if (port->buffer_underflow) {
  16972. + port_buffer_from_host(instance, port);
  16973. + port->buffer_underflow--;
  16974. + }
  16975. +
  16976. + return 0;
  16977. +}
  16978. +
  16979. +/* Initialise a mmal component and its ports
  16980. + *
  16981. + */
  16982. +int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
  16983. + const char *name,
  16984. + struct vchiq_mmal_component **component_out)
  16985. +{
  16986. + int ret;
  16987. + int idx; /* port index */
  16988. + struct vchiq_mmal_component *component;
  16989. +
  16990. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  16991. + return -EINTR;
  16992. +
  16993. + if (instance->component_idx == VCHIQ_MMAL_MAX_COMPONENTS) {
  16994. + ret = -EINVAL; /* todo is this correct error? */
  16995. + goto unlock;
  16996. + }
  16997. +
  16998. + component = &instance->component[instance->component_idx];
  16999. +
  17000. + ret = create_component(instance, component, name);
  17001. + if (ret < 0)
  17002. + goto unlock;
  17003. +
  17004. + /* ports info needs gathering */
  17005. + component->control.type = MMAL_PORT_TYPE_CONTROL;
  17006. + component->control.index = 0;
  17007. + component->control.component = component;
  17008. + spin_lock_init(&component->control.slock);
  17009. + INIT_LIST_HEAD(&component->control.buffers);
  17010. + ret = port_info_get(instance, &component->control);
  17011. + if (ret < 0)
  17012. + goto release_component;
  17013. +
  17014. + for (idx = 0; idx < component->inputs; idx++) {
  17015. + component->input[idx].type = MMAL_PORT_TYPE_INPUT;
  17016. + component->input[idx].index = idx;
  17017. + component->input[idx].component = component;
  17018. + spin_lock_init(&component->input[idx].slock);
  17019. + INIT_LIST_HEAD(&component->input[idx].buffers);
  17020. + ret = port_info_get(instance, &component->input[idx]);
  17021. + if (ret < 0)
  17022. + goto release_component;
  17023. + }
  17024. +
  17025. + for (idx = 0; idx < component->outputs; idx++) {
  17026. + component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
  17027. + component->output[idx].index = idx;
  17028. + component->output[idx].component = component;
  17029. + spin_lock_init(&component->output[idx].slock);
  17030. + INIT_LIST_HEAD(&component->output[idx].buffers);
  17031. + ret = port_info_get(instance, &component->output[idx]);
  17032. + if (ret < 0)
  17033. + goto release_component;
  17034. + }
  17035. +
  17036. + for (idx = 0; idx < component->clocks; idx++) {
  17037. + component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
  17038. + component->clock[idx].index = idx;
  17039. + component->clock[idx].component = component;
  17040. + spin_lock_init(&component->clock[idx].slock);
  17041. + INIT_LIST_HEAD(&component->clock[idx].buffers);
  17042. + ret = port_info_get(instance, &component->clock[idx]);
  17043. + if (ret < 0)
  17044. + goto release_component;
  17045. + }
  17046. +
  17047. + instance->component_idx++;
  17048. +
  17049. + *component_out = component;
  17050. +
  17051. + mutex_unlock(&instance->vchiq_mutex);
  17052. +
  17053. + return 0;
  17054. +
  17055. +release_component:
  17056. + destroy_component(instance, component);
  17057. +unlock:
  17058. + mutex_unlock(&instance->vchiq_mutex);
  17059. +
  17060. + return ret;
  17061. +}
  17062. +
  17063. +/*
  17064. + * cause a mmal component to be destroyed
  17065. + */
  17066. +int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
  17067. + struct vchiq_mmal_component *component)
  17068. +{
  17069. + int ret;
  17070. +
  17071. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17072. + return -EINTR;
  17073. +
  17074. + if (component->enabled)
  17075. + ret = disable_component(instance, component);
  17076. +
  17077. + ret = destroy_component(instance, component);
  17078. +
  17079. + mutex_unlock(&instance->vchiq_mutex);
  17080. +
  17081. + return ret;
  17082. +}
  17083. +
  17084. +/*
  17085. + * cause a mmal component to be enabled
  17086. + */
  17087. +int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
  17088. + struct vchiq_mmal_component *component)
  17089. +{
  17090. + int ret;
  17091. +
  17092. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17093. + return -EINTR;
  17094. +
  17095. + if (component->enabled) {
  17096. + mutex_unlock(&instance->vchiq_mutex);
  17097. + return 0;
  17098. + }
  17099. +
  17100. + ret = enable_component(instance, component);
  17101. + if (ret == 0)
  17102. + component->enabled = true;
  17103. +
  17104. + mutex_unlock(&instance->vchiq_mutex);
  17105. +
  17106. + return ret;
  17107. +}
  17108. +
  17109. +/*
  17110. + * cause a mmal component to be enabled
  17111. + */
  17112. +int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
  17113. + struct vchiq_mmal_component *component)
  17114. +{
  17115. + int ret;
  17116. +
  17117. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17118. + return -EINTR;
  17119. +
  17120. + if (!component->enabled) {
  17121. + mutex_unlock(&instance->vchiq_mutex);
  17122. + return 0;
  17123. + }
  17124. +
  17125. + ret = disable_component(instance, component);
  17126. + if (ret == 0)
  17127. + component->enabled = false;
  17128. +
  17129. + mutex_unlock(&instance->vchiq_mutex);
  17130. +
  17131. + return ret;
  17132. +}
  17133. +
  17134. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  17135. + u32 *major_out, u32 *minor_out)
  17136. +{
  17137. + int ret;
  17138. +
  17139. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17140. + return -EINTR;
  17141. +
  17142. + ret = get_version(instance, major_out, minor_out);
  17143. +
  17144. + mutex_unlock(&instance->vchiq_mutex);
  17145. +
  17146. + return ret;
  17147. +}
  17148. +
  17149. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
  17150. +{
  17151. + int status = 0;
  17152. +
  17153. + if (instance == NULL)
  17154. + return -EINVAL;
  17155. +
  17156. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  17157. + return -EINTR;
  17158. +
  17159. + vchi_service_use(instance->handle);
  17160. +
  17161. + status = vchi_service_close(instance->handle);
  17162. + if (status != 0)
  17163. + pr_err("mmal-vchiq: VCHIQ close failed");
  17164. +
  17165. + mutex_unlock(&instance->vchiq_mutex);
  17166. +
  17167. + vfree(instance->bulk_scratch);
  17168. +
  17169. + kfree(instance);
  17170. +
  17171. + return status;
  17172. +}
  17173. +
  17174. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
  17175. +{
  17176. + int status;
  17177. + struct vchiq_mmal_instance *instance;
  17178. + static VCHI_CONNECTION_T *vchi_connection;
  17179. + static VCHI_INSTANCE_T vchi_instance;
  17180. + SERVICE_CREATION_T params = {
  17181. + VCHI_VERSION_EX(VC_MMAL_VER, VC_MMAL_MIN_VER),
  17182. + VC_MMAL_SERVER_NAME,
  17183. + vchi_connection,
  17184. + 0, /* rx fifo size (unused) */
  17185. + 0, /* tx fifo size (unused) */
  17186. + service_callback,
  17187. + NULL, /* service callback parameter */
  17188. + 1, /* unaligned bulk receives */
  17189. + 1, /* unaligned bulk transmits */
  17190. + 0 /* want crc check on bulk transfers */
  17191. + };
  17192. +
  17193. + /* compile time checks to ensure structure size as they are
  17194. + * directly (de)serialised from memory.
  17195. + */
  17196. +
  17197. + /* ensure the header structure has packed to the correct size */
  17198. + BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
  17199. +
  17200. + /* ensure message structure does not exceed maximum length */
  17201. + BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
  17202. +
  17203. + /* mmal port struct is correct size */
  17204. + BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
  17205. +
  17206. + /* create a vchi instance */
  17207. + status = vchi_initialise(&vchi_instance);
  17208. + if (status) {
  17209. + pr_err("Failed to initialise VCHI instance (status=%d)\n",
  17210. + status);
  17211. + return -EIO;
  17212. + }
  17213. +
  17214. + status = vchi_connect(NULL, 0, vchi_instance);
  17215. + if (status) {
  17216. + pr_err("Failed to connect VCHI instance (status=%d)\n", status);
  17217. + return -EIO;
  17218. + }
  17219. +
  17220. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  17221. + memset(instance, 0, sizeof(*instance));
  17222. +
  17223. + mutex_init(&instance->vchiq_mutex);
  17224. + mutex_init(&instance->bulk_mutex);
  17225. +
  17226. + instance->bulk_scratch = vmalloc(PAGE_SIZE);
  17227. +
  17228. + params.callback_param = instance;
  17229. +
  17230. + status = vchi_service_open(vchi_instance, &params, &instance->handle);
  17231. + if (status) {
  17232. + pr_err("Failed to open VCHI service connection (status=%d)\n",
  17233. + status);
  17234. + goto err_close_services;
  17235. + }
  17236. +
  17237. + vchi_service_release(instance->handle);
  17238. +
  17239. + *out_instance = instance;
  17240. +
  17241. + return 0;
  17242. +
  17243. +err_close_services:
  17244. +
  17245. + vchi_service_close(instance->handle);
  17246. + vfree(instance->bulk_scratch);
  17247. + kfree(instance);
  17248. + return -ENODEV;
  17249. +}
  17250. diff -Nur linux-3.13.6/drivers/media/platform/bcm2835/mmal-vchiq.h linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-vchiq.h
  17251. --- linux-3.13.6/drivers/media/platform/bcm2835/mmal-vchiq.h 1970-01-01 01:00:00.000000000 +0100
  17252. +++ linux-raspberry-pi/drivers/media/platform/bcm2835/mmal-vchiq.h 2014-03-11 16:52:42.000000000 +0100
  17253. @@ -0,0 +1,178 @@
  17254. +/*
  17255. + * Broadcom BM2835 V4L2 driver
  17256. + *
  17257. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  17258. + *
  17259. + * This file is subject to the terms and conditions of the GNU General Public
  17260. + * License. See the file COPYING in the main directory of this archive
  17261. + * for more details.
  17262. + *
  17263. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  17264. + * Dave Stevenson <dsteve@broadcom.com>
  17265. + * Simon Mellor <simellor@broadcom.com>
  17266. + * Luke Diamand <luked@broadcom.com>
  17267. + *
  17268. + * MMAL interface to VCHIQ message passing
  17269. + */
  17270. +
  17271. +#ifndef MMAL_VCHIQ_H
  17272. +#define MMAL_VCHIQ_H
  17273. +
  17274. +#include "mmal-msg-format.h"
  17275. +
  17276. +#define MAX_PORT_COUNT 4
  17277. +
  17278. +/* Maximum size of the format extradata. */
  17279. +#define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128
  17280. +
  17281. +struct vchiq_mmal_instance;
  17282. +
  17283. +enum vchiq_mmal_es_type {
  17284. + MMAL_ES_TYPE_UNKNOWN, /**< Unknown elementary stream type */
  17285. + MMAL_ES_TYPE_CONTROL, /**< Elementary stream of control commands */
  17286. + MMAL_ES_TYPE_AUDIO, /**< Audio elementary stream */
  17287. + MMAL_ES_TYPE_VIDEO, /**< Video elementary stream */
  17288. + MMAL_ES_TYPE_SUBPICTURE /**< Sub-picture elementary stream */
  17289. +};
  17290. +
  17291. +/* rectangle, used lots so it gets its own struct */
  17292. +struct vchiq_mmal_rect {
  17293. + s32 x;
  17294. + s32 y;
  17295. + s32 width;
  17296. + s32 height;
  17297. +};
  17298. +
  17299. +struct vchiq_mmal_port_buffer {
  17300. + unsigned int num; /* number of buffers */
  17301. + u32 size; /* size of buffers */
  17302. + u32 alignment; /* alignment of buffers */
  17303. +};
  17304. +
  17305. +struct vchiq_mmal_port;
  17306. +
  17307. +typedef void (*vchiq_mmal_buffer_cb)(
  17308. + struct vchiq_mmal_instance *instance,
  17309. + struct vchiq_mmal_port *port,
  17310. + int status, struct mmal_buffer *buffer,
  17311. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts);
  17312. +
  17313. +struct vchiq_mmal_port {
  17314. + bool enabled;
  17315. + u32 handle;
  17316. + u32 type; /* port type, cached to use on port info set */
  17317. + u32 index; /* port index, cached to use on port info set */
  17318. +
  17319. + /* component port belongs to, allows simple deref */
  17320. + struct vchiq_mmal_component *component;
  17321. +
  17322. + struct vchiq_mmal_port *connected; /* port conencted to */
  17323. +
  17324. + /* buffer info */
  17325. + struct vchiq_mmal_port_buffer minimum_buffer;
  17326. + struct vchiq_mmal_port_buffer recommended_buffer;
  17327. + struct vchiq_mmal_port_buffer current_buffer;
  17328. +
  17329. + /* stream format */
  17330. + struct mmal_es_format format;
  17331. + /* elementry stream format */
  17332. + union mmal_es_specific_format es;
  17333. +
  17334. + /* data buffers to fill */
  17335. + struct list_head buffers;
  17336. + /* lock to serialise adding and removing buffers from list */
  17337. + spinlock_t slock;
  17338. + /* count of how many buffer header refils have failed because
  17339. + * there was no buffer to satisfy them
  17340. + */
  17341. + int buffer_underflow;
  17342. + /* callback on buffer completion */
  17343. + vchiq_mmal_buffer_cb buffer_cb;
  17344. + /* callback context */
  17345. + void *cb_ctx;
  17346. +};
  17347. +
  17348. +struct vchiq_mmal_component {
  17349. + bool enabled;
  17350. + u32 handle; /* VideoCore handle for component */
  17351. + u32 inputs; /* Number of input ports */
  17352. + u32 outputs; /* Number of output ports */
  17353. + u32 clocks; /* Number of clock ports */
  17354. + struct vchiq_mmal_port control; /* control port */
  17355. + struct vchiq_mmal_port input[MAX_PORT_COUNT]; /* input ports */
  17356. + struct vchiq_mmal_port output[MAX_PORT_COUNT]; /* output ports */
  17357. + struct vchiq_mmal_port clock[MAX_PORT_COUNT]; /* clock ports */
  17358. +};
  17359. +
  17360. +
  17361. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance);
  17362. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance);
  17363. +
  17364. +/* Initialise a mmal component and its ports
  17365. +*
  17366. +*/
  17367. +int vchiq_mmal_component_init(
  17368. + struct vchiq_mmal_instance *instance,
  17369. + const char *name,
  17370. + struct vchiq_mmal_component **component_out);
  17371. +
  17372. +int vchiq_mmal_component_finalise(
  17373. + struct vchiq_mmal_instance *instance,
  17374. + struct vchiq_mmal_component *component);
  17375. +
  17376. +int vchiq_mmal_component_enable(
  17377. + struct vchiq_mmal_instance *instance,
  17378. + struct vchiq_mmal_component *component);
  17379. +
  17380. +int vchiq_mmal_component_disable(
  17381. + struct vchiq_mmal_instance *instance,
  17382. + struct vchiq_mmal_component *component);
  17383. +
  17384. +
  17385. +
  17386. +/* enable a mmal port
  17387. + *
  17388. + * enables a port and if a buffer callback provided enque buffer
  17389. + * headers as apropriate for the port.
  17390. + */
  17391. +int vchiq_mmal_port_enable(
  17392. + struct vchiq_mmal_instance *instance,
  17393. + struct vchiq_mmal_port *port,
  17394. + vchiq_mmal_buffer_cb buffer_cb);
  17395. +
  17396. +/* disable a port
  17397. + *
  17398. + * disable a port will dequeue any pending buffers
  17399. + */
  17400. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  17401. + struct vchiq_mmal_port *port);
  17402. +
  17403. +
  17404. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  17405. + struct vchiq_mmal_port *port,
  17406. + u32 parameter,
  17407. + void *value,
  17408. + u32 value_size);
  17409. +
  17410. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  17411. + struct vchiq_mmal_port *port,
  17412. + u32 parameter,
  17413. + void *value,
  17414. + u32 *value_size);
  17415. +
  17416. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  17417. + struct vchiq_mmal_port *port);
  17418. +
  17419. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  17420. + struct vchiq_mmal_port *src,
  17421. + struct vchiq_mmal_port *dst);
  17422. +
  17423. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  17424. + u32 *major_out,
  17425. + u32 *minor_out);
  17426. +
  17427. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  17428. + struct vchiq_mmal_port *port,
  17429. + struct mmal_buffer *buf);
  17430. +
  17431. +#endif /* MMAL_VCHIQ_H */
  17432. diff -Nur linux-3.13.6/drivers/media/platform/Kconfig linux-raspberry-pi/drivers/media/platform/Kconfig
  17433. --- linux-3.13.6/drivers/media/platform/Kconfig 2014-03-07 07:07:02.000000000 +0100
  17434. +++ linux-raspberry-pi/drivers/media/platform/Kconfig 2014-03-11 16:54:57.000000000 +0100
  17435. @@ -124,6 +124,7 @@
  17436. source "drivers/media/platform/soc_camera/Kconfig"
  17437. source "drivers/media/platform/exynos4-is/Kconfig"
  17438. source "drivers/media/platform/s5p-tv/Kconfig"
  17439. +source "drivers/media/platform/bcm2835/Kconfig"
  17440. endif # V4L_PLATFORM_DRIVERS
  17441. diff -Nur linux-3.13.6/drivers/media/platform/Makefile linux-raspberry-pi/drivers/media/platform/Makefile
  17442. --- linux-3.13.6/drivers/media/platform/Makefile 2014-03-07 07:07:02.000000000 +0100
  17443. +++ linux-raspberry-pi/drivers/media/platform/Makefile 2014-03-11 16:54:57.000000000 +0100
  17444. @@ -54,4 +54,6 @@
  17445. obj-$(CONFIG_ARCH_OMAP) += omap/
  17446. +obj-$(CONFIG_VIDEO_BCM2835) += bcm2835/
  17447. +
  17448. ccflags-y += -I$(srctree)/drivers/media/i2c
  17449. diff -Nur linux-3.13.6/drivers/media/usb/dvb-usb-v2/az6007.c linux-raspberry-pi/drivers/media/usb/dvb-usb-v2/az6007.c
  17450. --- linux-3.13.6/drivers/media/usb/dvb-usb-v2/az6007.c 2014-03-07 07:07:02.000000000 +0100
  17451. +++ linux-raspberry-pi/drivers/media/usb/dvb-usb-v2/az6007.c 2014-03-11 16:52:43.000000000 +0100
  17452. @@ -68,6 +68,19 @@
  17453. .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
  17454. };
  17455. +static struct drxk_config cablestar_hdci_drxk = {
  17456. + .adr = 0x29,
  17457. + .parallel_ts = true,
  17458. + .dynamic_clk = true,
  17459. + .single_master = true,
  17460. + .enable_merr_cfg = true,
  17461. + .no_i2c_bridge = false,
  17462. + .chunk_size = 64,
  17463. + .mpeg_out_clk_strength = 0x02,
  17464. + .qam_demod_parameter_count = 2,
  17465. + .microcode_name = "dvb-usb-technisat-cablestar-hdci-drxk.fw",
  17466. +};
  17467. +
  17468. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  17469. {
  17470. struct az6007_device_state *st = fe_to_priv(fe);
  17471. @@ -630,6 +643,27 @@
  17472. return 0;
  17473. }
  17474. +static int az6007_cablestar_hdci_frontend_attach(struct dvb_usb_adapter *adap)
  17475. +{
  17476. + struct az6007_device_state *st = adap_to_priv(adap);
  17477. + struct dvb_usb_device *d = adap_to_d(adap);
  17478. +
  17479. + pr_debug("attaching demod drxk\n");
  17480. +
  17481. + adap->fe[0] = dvb_attach(drxk_attach, &cablestar_hdci_drxk,
  17482. + &d->i2c_adap);
  17483. + if (!adap->fe[0])
  17484. + return -EINVAL;
  17485. +
  17486. + adap->fe[0]->sec_priv = adap;
  17487. + st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
  17488. + adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  17489. +
  17490. + az6007_ci_init(adap);
  17491. +
  17492. + return 0;
  17493. +}
  17494. +
  17495. static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
  17496. {
  17497. struct dvb_usb_device *d = adap_to_d(adap);
  17498. @@ -868,6 +902,29 @@
  17499. }
  17500. };
  17501. +static struct dvb_usb_device_properties az6007_cablestar_hdci_props = {
  17502. + .driver_name = KBUILD_MODNAME,
  17503. + .owner = THIS_MODULE,
  17504. + .firmware = AZ6007_FIRMWARE,
  17505. +
  17506. + .adapter_nr = adapter_nr,
  17507. + .size_of_priv = sizeof(struct az6007_device_state),
  17508. + .i2c_algo = &az6007_i2c_algo,
  17509. + .tuner_attach = az6007_tuner_attach,
  17510. + .frontend_attach = az6007_cablestar_hdci_frontend_attach,
  17511. + .streaming_ctrl = az6007_streaming_ctrl,
  17512. +/* ditch get_rc_config as it can't work (TS35 remote, I believe it's rc5) */
  17513. + .get_rc_config = NULL,
  17514. + .read_mac_address = az6007_read_mac_addr,
  17515. + .download_firmware = az6007_download_firmware,
  17516. + .identify_state = az6007_identify_state,
  17517. + .power_ctrl = az6007_power_ctrl,
  17518. + .num_adapters = 1,
  17519. + .adapter = {
  17520. + { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
  17521. + }
  17522. +};
  17523. +
  17524. static struct usb_device_id az6007_usb_table[] = {
  17525. {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007,
  17526. &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)},
  17527. @@ -875,6 +932,8 @@
  17528. &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
  17529. {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2,
  17530. &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
  17531. + {DVB_USB_DEVICE(USB_VID_TECHNISAT, USB_PID_TECHNISAT_USB2_CABLESTAR_HDCI,
  17532. + &az6007_cablestar_hdci_props, "Technisat CableStar Combo HD CI", RC_MAP_EMPTY)},
  17533. {0},
  17534. };
  17535. diff -Nur linux-3.13.6/drivers/media/usb/dvb-usb-v2/rtl28xxu.c linux-raspberry-pi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
  17536. --- linux-3.13.6/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-03-07 07:07:02.000000000 +0100
  17537. +++ linux-raspberry-pi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-03-11 16:54:57.000000000 +0100
  17538. @@ -1423,6 +1423,10 @@
  17539. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  17540. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  17541. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  17542. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xb803 /*USB_PID_AUGUST_DVBT205*/,
  17543. + &rtl2832u_props, "August DVB-T 205", NULL) },
  17544. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xa803 /*USB_PID_AUGUST_DVBT205*/,
  17545. + &rtl2832u_props, "August DVB-T 205", NULL) },
  17546. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
  17547. &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
  17548. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
  17549. diff -Nur linux-3.13.6/drivers/misc/Kconfig linux-raspberry-pi/drivers/misc/Kconfig
  17550. --- linux-3.13.6/drivers/misc/Kconfig 2014-03-07 07:07:02.000000000 +0100
  17551. +++ linux-raspberry-pi/drivers/misc/Kconfig 2014-03-11 16:54:57.000000000 +0100
  17552. @@ -524,5 +524,6 @@
  17553. source "drivers/misc/altera-stapl/Kconfig"
  17554. source "drivers/misc/mei/Kconfig"
  17555. source "drivers/misc/vmw_vmci/Kconfig"
  17556. +source "drivers/misc/vc04_services/Kconfig"
  17557. source "drivers/misc/mic/Kconfig"
  17558. endmenu
  17559. diff -Nur linux-3.13.6/drivers/misc/Makefile linux-raspberry-pi/drivers/misc/Makefile
  17560. --- linux-3.13.6/drivers/misc/Makefile 2014-03-07 07:07:02.000000000 +0100
  17561. +++ linux-raspberry-pi/drivers/misc/Makefile 2014-03-11 16:54:57.000000000 +0100
  17562. @@ -52,4 +52,5 @@
  17563. obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
  17564. obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
  17565. obj-$(CONFIG_SRAM) += sram.o
  17566. +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
  17567. obj-y += mic/
  17568. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchi/connections/connection.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/connections/connection.h
  17569. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchi/connections/connection.h 1970-01-01 01:00:00.000000000 +0100
  17570. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/connections/connection.h 2014-03-11 16:52:43.000000000 +0100
  17571. @@ -0,0 +1,328 @@
  17572. +/**
  17573. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  17574. + *
  17575. + * Redistribution and use in source and binary forms, with or without
  17576. + * modification, are permitted provided that the following conditions
  17577. + * are met:
  17578. + * 1. Redistributions of source code must retain the above copyright
  17579. + * notice, this list of conditions, and the following disclaimer,
  17580. + * without modification.
  17581. + * 2. Redistributions in binary form must reproduce the above copyright
  17582. + * notice, this list of conditions and the following disclaimer in the
  17583. + * documentation and/or other materials provided with the distribution.
  17584. + * 3. The names of the above-listed copyright holders may not be used
  17585. + * to endorse or promote products derived from this software without
  17586. + * specific prior written permission.
  17587. + *
  17588. + * ALTERNATIVELY, this software may be distributed under the terms of the
  17589. + * GNU General Public License ("GPL") version 2, as published by the Free
  17590. + * Software Foundation.
  17591. + *
  17592. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  17593. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17594. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17595. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  17596. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17597. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17598. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  17599. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  17600. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17601. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  17602. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17603. + */
  17604. +
  17605. +#ifndef CONNECTION_H_
  17606. +#define CONNECTION_H_
  17607. +
  17608. +#include <linux/kernel.h>
  17609. +#include <linux/types.h>
  17610. +#include <linux/semaphore.h>
  17611. +
  17612. +#include "interface/vchi/vchi_cfg_internal.h"
  17613. +#include "interface/vchi/vchi_common.h"
  17614. +#include "interface/vchi/message_drivers/message.h"
  17615. +
  17616. +/******************************************************************************
  17617. + Global defs
  17618. + *****************************************************************************/
  17619. +
  17620. +// Opaque handle for a connection / service pair
  17621. +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
  17622. +
  17623. +// opaque handle to the connection state information
  17624. +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
  17625. +
  17626. +typedef struct vchi_connection_t VCHI_CONNECTION_T;
  17627. +
  17628. +
  17629. +/******************************************************************************
  17630. + API
  17631. + *****************************************************************************/
  17632. +
  17633. +// Routine to init a connection with a particular low level driver
  17634. +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
  17635. + const VCHI_MESSAGE_DRIVER_T * driver );
  17636. +
  17637. +// Routine to control CRC enabling at a connection level
  17638. +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
  17639. + VCHI_CRC_CONTROL_T control );
  17640. +
  17641. +// Routine to create a service
  17642. +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
  17643. + int32_t service_id,
  17644. + uint32_t rx_fifo_size,
  17645. + uint32_t tx_fifo_size,
  17646. + int server,
  17647. + VCHI_CALLBACK_T callback,
  17648. + void *callback_param,
  17649. + int32_t want_crc,
  17650. + int32_t want_unaligned_bulk_rx,
  17651. + int32_t want_unaligned_bulk_tx,
  17652. + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
  17653. +
  17654. +// Routine to close a service
  17655. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
  17656. +
  17657. +// Routine to queue a message
  17658. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17659. + const void *data,
  17660. + uint32_t data_size,
  17661. + VCHI_FLAGS_T flags,
  17662. + void *msg_handle );
  17663. +
  17664. +// scatter-gather (vector) message queueing
  17665. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17666. + VCHI_MSG_VECTOR_T *vector,
  17667. + uint32_t count,
  17668. + VCHI_FLAGS_T flags,
  17669. + void *msg_handle );
  17670. +
  17671. +// Routine to dequeue a message
  17672. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17673. + void *data,
  17674. + uint32_t max_data_size_to_read,
  17675. + uint32_t *actual_msg_size,
  17676. + VCHI_FLAGS_T flags );
  17677. +
  17678. +// Routine to peek at a message
  17679. +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17680. + void **data,
  17681. + uint32_t *msg_size,
  17682. + VCHI_FLAGS_T flags );
  17683. +
  17684. +// Routine to hold a message
  17685. +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17686. + void **data,
  17687. + uint32_t *msg_size,
  17688. + VCHI_FLAGS_T flags,
  17689. + void **message_handle );
  17690. +
  17691. +// Routine to initialise a received message iterator
  17692. +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17693. + VCHI_MSG_ITER_T *iter,
  17694. + VCHI_FLAGS_T flags );
  17695. +
  17696. +// Routine to release a held message
  17697. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17698. + void *message_handle );
  17699. +
  17700. +// Routine to get info on a held message
  17701. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17702. + void *message_handle,
  17703. + void **data,
  17704. + int32_t *msg_size,
  17705. + uint32_t *tx_timestamp,
  17706. + uint32_t *rx_timestamp );
  17707. +
  17708. +// Routine to check whether the iterator has a next message
  17709. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  17710. + const VCHI_MSG_ITER_T *iter );
  17711. +
  17712. +// Routine to advance the iterator
  17713. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  17714. + VCHI_MSG_ITER_T *iter,
  17715. + void **data,
  17716. + uint32_t *msg_size );
  17717. +
  17718. +// Routine to remove the last message returned by the iterator
  17719. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  17720. + VCHI_MSG_ITER_T *iter );
  17721. +
  17722. +// Routine to hold the last message returned by the iterator
  17723. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  17724. + VCHI_MSG_ITER_T *iter,
  17725. + void **msg_handle );
  17726. +
  17727. +// Routine to transmit bulk data
  17728. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17729. + const void *data_src,
  17730. + uint32_t data_size,
  17731. + VCHI_FLAGS_T flags,
  17732. + void *bulk_handle );
  17733. +
  17734. +// Routine to receive data
  17735. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  17736. + void *data_dst,
  17737. + uint32_t data_size,
  17738. + VCHI_FLAGS_T flags,
  17739. + void *bulk_handle );
  17740. +
  17741. +// Routine to report if a server is available
  17742. +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
  17743. +
  17744. +// Routine to report the number of RX slots available
  17745. +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
  17746. +
  17747. +// Routine to report the RX slot size
  17748. +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
  17749. +
  17750. +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  17751. +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
  17752. + int32_t service,
  17753. + uint32_t length,
  17754. + MESSAGE_TX_CHANNEL_T channel,
  17755. + uint32_t channel_params,
  17756. + uint32_t data_length,
  17757. + uint32_t data_offset);
  17758. +
  17759. +// Callback to inform a service that a Xon or Xoff message has been received
  17760. +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
  17761. +
  17762. +// Callback to inform a service that a server available reply message has been received
  17763. +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
  17764. +
  17765. +// Callback to indicate that bulk auxiliary messages have arrived
  17766. +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
  17767. +
  17768. +// Callback to indicate that bulk auxiliary messages have arrived
  17769. +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
  17770. +
  17771. +// Callback with all the connection info you require
  17772. +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);
  17773. +
  17774. +// Callback to inform of a disconnect
  17775. +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
  17776. +
  17777. +// Callback to inform of a power control request
  17778. +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
  17779. +
  17780. +// allocate memory suitably aligned for this connection
  17781. +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
  17782. +
  17783. +// free memory allocated by buffer_allocate
  17784. +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
  17785. +
  17786. +
  17787. +/******************************************************************************
  17788. + System driver struct
  17789. + *****************************************************************************/
  17790. +
  17791. +struct opaque_vchi_connection_api_t
  17792. +{
  17793. + // Routine to init the connection
  17794. + VCHI_CONNECTION_INIT_T init;
  17795. +
  17796. + // Connection-level CRC control
  17797. + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
  17798. +
  17799. + // Routine to connect to or create service
  17800. + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
  17801. +
  17802. + // Routine to disconnect from a service
  17803. + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
  17804. +
  17805. + // Routine to queue a message
  17806. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
  17807. +
  17808. + // scatter-gather (vector) message queue
  17809. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
  17810. +
  17811. + // Routine to dequeue a message
  17812. + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
  17813. +
  17814. + // Routine to peek at a message
  17815. + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
  17816. +
  17817. + // Routine to hold a message
  17818. + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
  17819. +
  17820. + // Routine to initialise a received message iterator
  17821. + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
  17822. +
  17823. + // Routine to release a message
  17824. + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
  17825. +
  17826. + // Routine to get information on a held message
  17827. + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
  17828. +
  17829. + // Routine to check for next message on iterator
  17830. + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
  17831. +
  17832. + // Routine to get next message on iterator
  17833. + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
  17834. +
  17835. + // Routine to remove the last message returned by iterator
  17836. + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
  17837. +
  17838. + // Routine to hold the last message returned by iterator
  17839. + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
  17840. +
  17841. + // Routine to transmit bulk data
  17842. + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
  17843. +
  17844. + // Routine to receive data
  17845. + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
  17846. +
  17847. + // Routine to report the available servers
  17848. + VCHI_CONNECTION_SERVER_PRESENT server_present;
  17849. +
  17850. + // Routine to report the number of RX slots available
  17851. + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
  17852. +
  17853. + // Routine to report the RX slot size
  17854. + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
  17855. +
  17856. + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  17857. + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
  17858. +
  17859. + // Callback to inform a service that a Xon or Xoff message has been received
  17860. + VCHI_CONNECTION_FLOW_CONTROL flow_control;
  17861. +
  17862. + // Callback to inform a service that a server available reply message has been received
  17863. + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
  17864. +
  17865. + // Callback to indicate that bulk auxiliary messages have arrived
  17866. + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
  17867. +
  17868. + // Callback to indicate that a bulk auxiliary message has been transmitted
  17869. + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
  17870. +
  17871. + // Callback to provide information about the connection
  17872. + VCHI_CONNECTION_INFO connection_info;
  17873. +
  17874. + // Callback to notify that peer has requested disconnect
  17875. + VCHI_CONNECTION_DISCONNECT disconnect;
  17876. +
  17877. + // Callback to notify that peer has requested power change
  17878. + VCHI_CONNECTION_POWER_CONTROL power_control;
  17879. +
  17880. + // allocate memory suitably aligned for this connection
  17881. + VCHI_BUFFER_ALLOCATE buffer_allocate;
  17882. +
  17883. + // free memory allocated by buffer_allocate
  17884. + VCHI_BUFFER_FREE buffer_free;
  17885. +
  17886. +};
  17887. +
  17888. +struct vchi_connection_t {
  17889. + const VCHI_CONNECTION_API_T *api;
  17890. + VCHI_CONNECTION_STATE_T *state;
  17891. +#ifdef VCHI_COARSE_LOCKING
  17892. + struct semaphore sem;
  17893. +#endif
  17894. +};
  17895. +
  17896. +
  17897. +#endif /* CONNECTION_H_ */
  17898. +
  17899. +/****************************** End of file **********************************/
  17900. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  17901. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 1970-01-01 01:00:00.000000000 +0100
  17902. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 2014-03-11 16:52:43.000000000 +0100
  17903. @@ -0,0 +1,204 @@
  17904. +/**
  17905. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  17906. + *
  17907. + * Redistribution and use in source and binary forms, with or without
  17908. + * modification, are permitted provided that the following conditions
  17909. + * are met:
  17910. + * 1. Redistributions of source code must retain the above copyright
  17911. + * notice, this list of conditions, and the following disclaimer,
  17912. + * without modification.
  17913. + * 2. Redistributions in binary form must reproduce the above copyright
  17914. + * notice, this list of conditions and the following disclaimer in the
  17915. + * documentation and/or other materials provided with the distribution.
  17916. + * 3. The names of the above-listed copyright holders may not be used
  17917. + * to endorse or promote products derived from this software without
  17918. + * specific prior written permission.
  17919. + *
  17920. + * ALTERNATIVELY, this software may be distributed under the terms of the
  17921. + * GNU General Public License ("GPL") version 2, as published by the Free
  17922. + * Software Foundation.
  17923. + *
  17924. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  17925. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17926. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17927. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  17928. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17929. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  17930. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  17931. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  17932. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  17933. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  17934. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17935. + */
  17936. +
  17937. +#ifndef _VCHI_MESSAGE_H_
  17938. +#define _VCHI_MESSAGE_H_
  17939. +
  17940. +#include <linux/kernel.h>
  17941. +#include <linux/types.h>
  17942. +#include <linux/semaphore.h>
  17943. +
  17944. +#include "interface/vchi/vchi_cfg_internal.h"
  17945. +#include "interface/vchi/vchi_common.h"
  17946. +
  17947. +
  17948. +typedef enum message_event_type {
  17949. + MESSAGE_EVENT_NONE,
  17950. + MESSAGE_EVENT_NOP,
  17951. + MESSAGE_EVENT_MESSAGE,
  17952. + MESSAGE_EVENT_SLOT_COMPLETE,
  17953. + MESSAGE_EVENT_RX_BULK_PAUSED,
  17954. + MESSAGE_EVENT_RX_BULK_COMPLETE,
  17955. + MESSAGE_EVENT_TX_COMPLETE,
  17956. + MESSAGE_EVENT_MSG_DISCARDED
  17957. +} MESSAGE_EVENT_TYPE_T;
  17958. +
  17959. +typedef enum vchi_msg_flags
  17960. +{
  17961. + VCHI_MSG_FLAGS_NONE = 0x0,
  17962. + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
  17963. +} VCHI_MSG_FLAGS_T;
  17964. +
  17965. +typedef enum message_tx_channel
  17966. +{
  17967. + MESSAGE_TX_CHANNEL_MESSAGE = 0,
  17968. + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  17969. +} MESSAGE_TX_CHANNEL_T;
  17970. +
  17971. +// Macros used for cycling through bulk channels
  17972. +#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)
  17973. +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  17974. +
  17975. +typedef enum message_rx_channel
  17976. +{
  17977. + MESSAGE_RX_CHANNEL_MESSAGE = 0,
  17978. + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  17979. +} MESSAGE_RX_CHANNEL_T;
  17980. +
  17981. +// Message receive slot information
  17982. +typedef struct rx_msg_slot_info {
  17983. +
  17984. + struct rx_msg_slot_info *next;
  17985. + //struct slot_info *prev;
  17986. +#if !defined VCHI_COARSE_LOCKING
  17987. + struct semaphore sem;
  17988. +#endif
  17989. +
  17990. + uint8_t *addr; // base address of slot
  17991. + uint32_t len; // length of slot in bytes
  17992. +
  17993. + uint32_t write_ptr; // hardware causes this to advance
  17994. + uint32_t read_ptr; // this module does the reading
  17995. + int active; // is this slot in the hardware dma fifo?
  17996. + uint32_t msgs_parsed; // count how many messages are in this slot
  17997. + uint32_t msgs_released; // how many messages have been released
  17998. + void *state; // connection state information
  17999. + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
  18000. +} RX_MSG_SLOTINFO_T;
  18001. +
  18002. +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
  18003. +// In particular, it mustn't use addr and len - they're the client buffer, but the message
  18004. +// driver will be tasked with sending the aligned core section.
  18005. +typedef struct rx_bulk_slotinfo_t {
  18006. + struct rx_bulk_slotinfo_t *next;
  18007. +
  18008. + struct semaphore *blocking;
  18009. +
  18010. + // needed by DMA
  18011. + void *addr;
  18012. + uint32_t len;
  18013. +
  18014. + // needed for the callback
  18015. + void *service;
  18016. + void *handle;
  18017. + VCHI_FLAGS_T flags;
  18018. +} RX_BULK_SLOTINFO_T;
  18019. +
  18020. +
  18021. +/* ----------------------------------------------------------------------
  18022. + * each connection driver will have a pool of the following struct.
  18023. + *
  18024. + * the pool will be managed by vchi_qman_*
  18025. + * this means there will be multiple queues (single linked lists)
  18026. + * a given struct message_info will be on exactly one of these queues
  18027. + * at any one time
  18028. + * -------------------------------------------------------------------- */
  18029. +typedef struct rx_message_info {
  18030. +
  18031. + struct message_info *next;
  18032. + //struct message_info *prev;
  18033. +
  18034. + uint8_t *addr;
  18035. + uint32_t len;
  18036. + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
  18037. + uint32_t tx_timestamp;
  18038. + uint32_t rx_timestamp;
  18039. +
  18040. +} RX_MESSAGE_INFO_T;
  18041. +
  18042. +typedef struct {
  18043. + MESSAGE_EVENT_TYPE_T type;
  18044. +
  18045. + struct {
  18046. + // for messages
  18047. + void *addr; // address of message
  18048. + uint16_t slot_delta; // whether this message indicated slot delta
  18049. + uint32_t len; // length of message
  18050. + RX_MSG_SLOTINFO_T *slot; // slot this message is in
  18051. + int32_t service; // service id this message is destined for
  18052. + uint32_t tx_timestamp; // timestamp from the header
  18053. + uint32_t rx_timestamp; // timestamp when we parsed it
  18054. + } message;
  18055. +
  18056. + // FIXME: cleanup slot reporting...
  18057. + RX_MSG_SLOTINFO_T *rx_msg;
  18058. + RX_BULK_SLOTINFO_T *rx_bulk;
  18059. + void *tx_handle;
  18060. + MESSAGE_TX_CHANNEL_T tx_channel;
  18061. +
  18062. +} MESSAGE_EVENT_T;
  18063. +
  18064. +
  18065. +// callbacks
  18066. +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
  18067. +
  18068. +typedef struct {
  18069. + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
  18070. +} VCHI_MESSAGE_DRIVER_OPEN_T;
  18071. +
  18072. +
  18073. +// handle to this instance of message driver (as returned by ->open)
  18074. +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
  18075. +
  18076. +struct opaque_vchi_message_driver_t {
  18077. + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
  18078. + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
  18079. + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
  18080. + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
  18081. + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
  18082. + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
  18083. + 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)
  18084. + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
  18085. + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
  18086. + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
  18087. + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
  18088. +
  18089. + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
  18090. + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
  18091. + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
  18092. + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
  18093. + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  18094. + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  18095. +
  18096. + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18097. + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18098. + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  18099. + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
  18100. + 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 );
  18101. + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
  18102. +};
  18103. +
  18104. +
  18105. +#endif // _VCHI_MESSAGE_H_
  18106. +
  18107. +/****************************** End of file ***********************************/
  18108. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  18109. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 1970-01-01 01:00:00.000000000 +0100
  18110. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 2014-03-11 16:52:43.000000000 +0100
  18111. @@ -0,0 +1,224 @@
  18112. +/**
  18113. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18114. + *
  18115. + * Redistribution and use in source and binary forms, with or without
  18116. + * modification, are permitted provided that the following conditions
  18117. + * are met:
  18118. + * 1. Redistributions of source code must retain the above copyright
  18119. + * notice, this list of conditions, and the following disclaimer,
  18120. + * without modification.
  18121. + * 2. Redistributions in binary form must reproduce the above copyright
  18122. + * notice, this list of conditions and the following disclaimer in the
  18123. + * documentation and/or other materials provided with the distribution.
  18124. + * 3. The names of the above-listed copyright holders may not be used
  18125. + * to endorse or promote products derived from this software without
  18126. + * specific prior written permission.
  18127. + *
  18128. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18129. + * GNU General Public License ("GPL") version 2, as published by the Free
  18130. + * Software Foundation.
  18131. + *
  18132. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18133. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18134. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18135. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18136. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18137. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18138. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18139. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18140. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18141. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18142. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18143. + */
  18144. +
  18145. +#ifndef VCHI_CFG_H_
  18146. +#define VCHI_CFG_H_
  18147. +
  18148. +/****************************************************************************************
  18149. + * Defines in this first section are part of the VCHI API and may be examined by VCHI
  18150. + * services.
  18151. + ***************************************************************************************/
  18152. +
  18153. +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
  18154. +/* Really determined by the message driver, and should be available from a run-time call. */
  18155. +#ifndef VCHI_BULK_ALIGN
  18156. +# if __VCCOREVER__ >= 0x04000000
  18157. +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
  18158. +# else
  18159. +# define VCHI_BULK_ALIGN 16
  18160. +# endif
  18161. +#endif
  18162. +
  18163. +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
  18164. +/* May be less than or greater than VCHI_BULK_ALIGN */
  18165. +/* Really determined by the message driver, and should be available from a run-time call. */
  18166. +#ifndef VCHI_BULK_GRANULARITY
  18167. +# if __VCCOREVER__ >= 0x04000000
  18168. +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
  18169. +# else
  18170. +# define VCHI_BULK_GRANULARITY 16
  18171. +# endif
  18172. +#endif
  18173. +
  18174. +/* The largest possible message to be queued with vchi_msg_queue. */
  18175. +#ifndef VCHI_MAX_MSG_SIZE
  18176. +# if defined VCHI_LOCAL_HOST_PORT
  18177. +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
  18178. +# else
  18179. +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
  18180. +# endif
  18181. +#endif
  18182. +
  18183. +/******************************************************************************************
  18184. + * Defines below are system configuration options, and should not be used by VCHI services.
  18185. + *****************************************************************************************/
  18186. +
  18187. +/* How many connections can we support? A localhost implementation uses 2 connections,
  18188. + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
  18189. + * driver. */
  18190. +#ifndef VCHI_MAX_NUM_CONNECTIONS
  18191. +# define VCHI_MAX_NUM_CONNECTIONS 3
  18192. +#endif
  18193. +
  18194. +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
  18195. + * amount of static memory. */
  18196. +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
  18197. +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
  18198. +#endif
  18199. +
  18200. +/* Adjust if using a message driver that supports more logical TX channels */
  18201. +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
  18202. +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
  18203. +#endif
  18204. +
  18205. +/* Adjust if using a message driver that supports more logical RX channels */
  18206. +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
  18207. +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
  18208. +#endif
  18209. +
  18210. +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
  18211. + * receive queue space, less message headers. */
  18212. +#ifndef VCHI_NUM_READ_SLOTS
  18213. +# if defined(VCHI_LOCAL_HOST_PORT)
  18214. +# define VCHI_NUM_READ_SLOTS 4
  18215. +# else
  18216. +# define VCHI_NUM_READ_SLOTS 48
  18217. +# endif
  18218. +#endif
  18219. +
  18220. +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
  18221. + * performance. Only define on VideoCore end, talking to host.
  18222. + */
  18223. +//#define VCHI_MSG_RX_OVERRUN
  18224. +
  18225. +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
  18226. + * underneath VCHI will usually have its own buffering. */
  18227. +#ifndef VCHI_NUM_WRITE_SLOTS
  18228. +# define VCHI_NUM_WRITE_SLOTS 4
  18229. +#endif
  18230. +
  18231. +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
  18232. + * then it's taking up too much buffer space, and the peer service will be told to stop
  18233. + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
  18234. + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
  18235. + * is too high. */
  18236. +#ifndef VCHI_XOFF_THRESHOLD
  18237. +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
  18238. +#endif
  18239. +
  18240. +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
  18241. + * service has dequeued/released enough messages that it's now occupying
  18242. + * VCHI_XON_THRESHOLD slots or fewer. */
  18243. +#ifndef VCHI_XON_THRESHOLD
  18244. +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
  18245. +#endif
  18246. +
  18247. +/* A size below which a bulk transfer omits the handshake completely and always goes
  18248. + * via the message channel, if bulk auxiliary is being sent on that service. (The user
  18249. + * can guarantee this by enabling unaligned transmits).
  18250. + * Not API. */
  18251. +#ifndef VCHI_MIN_BULK_SIZE
  18252. +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
  18253. +#endif
  18254. +
  18255. +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
  18256. + * speed and latency; the smaller the chunk size the better change of messages and other
  18257. + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
  18258. + * break transmissions into chunks.
  18259. + */
  18260. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
  18261. +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
  18262. +#endif
  18263. +
  18264. +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
  18265. + * with multiple-line frames. Only use if the receiver can cope. */
  18266. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
  18267. +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
  18268. +#endif
  18269. +
  18270. +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
  18271. + * vchi_msg_queue will be blocked. */
  18272. +#ifndef VCHI_TX_MSG_QUEUE_SIZE
  18273. +# define VCHI_TX_MSG_QUEUE_SIZE 256
  18274. +#endif
  18275. +
  18276. +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
  18277. + * will be suspended until older messages are dequeued/released. */
  18278. +#ifndef VCHI_RX_MSG_QUEUE_SIZE
  18279. +# define VCHI_RX_MSG_QUEUE_SIZE 256
  18280. +#endif
  18281. +
  18282. +/* Really should be able to cope if we run out of received message descriptors, by
  18283. + * suspending parsing as the comment above says, but we don't. This sweeps the issue
  18284. + * under the carpet. */
  18285. +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  18286. +# undef VCHI_RX_MSG_QUEUE_SIZE
  18287. +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  18288. +#endif
  18289. +
  18290. +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
  18291. + * will be blocked. */
  18292. +#ifndef VCHI_TX_BULK_QUEUE_SIZE
  18293. +# define VCHI_TX_BULK_QUEUE_SIZE 64
  18294. +#endif
  18295. +
  18296. +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
  18297. + * will be blocked. */
  18298. +#ifndef VCHI_RX_BULK_QUEUE_SIZE
  18299. +# define VCHI_RX_BULK_QUEUE_SIZE 64
  18300. +#endif
  18301. +
  18302. +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
  18303. + * the peer asks for more than this, VCHI will fail and assert. The number is determined
  18304. + * by the peer's hardware - it's the number of outstanding requests that can be queued
  18305. + * on all bulk channels. VC3's MPHI peripheral allows 16. */
  18306. +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
  18307. +# define VCHI_MAX_PEER_BULK_REQUESTS 32
  18308. +#endif
  18309. +
  18310. +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
  18311. + * transmitter on and off.
  18312. + */
  18313. +/*#define VCHI_CCP2TX_MANUAL_POWER*/
  18314. +
  18315. +#ifndef VCHI_CCP2TX_MANUAL_POWER
  18316. +
  18317. +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
  18318. + * negative for no IDLE.
  18319. + */
  18320. +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
  18321. +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
  18322. +# endif
  18323. +
  18324. +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
  18325. + * negative for no OFF.
  18326. + */
  18327. +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
  18328. +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
  18329. +# endif
  18330. +
  18331. +#endif /* VCHI_CCP2TX_MANUAL_POWER */
  18332. +
  18333. +#endif /* VCHI_CFG_H_ */
  18334. +
  18335. +/****************************** End of file **********************************/
  18336. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  18337. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 1970-01-01 01:00:00.000000000 +0100
  18338. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 2014-03-11 16:52:43.000000000 +0100
  18339. @@ -0,0 +1,71 @@
  18340. +/**
  18341. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18342. + *
  18343. + * Redistribution and use in source and binary forms, with or without
  18344. + * modification, are permitted provided that the following conditions
  18345. + * are met:
  18346. + * 1. Redistributions of source code must retain the above copyright
  18347. + * notice, this list of conditions, and the following disclaimer,
  18348. + * without modification.
  18349. + * 2. Redistributions in binary form must reproduce the above copyright
  18350. + * notice, this list of conditions and the following disclaimer in the
  18351. + * documentation and/or other materials provided with the distribution.
  18352. + * 3. The names of the above-listed copyright holders may not be used
  18353. + * to endorse or promote products derived from this software without
  18354. + * specific prior written permission.
  18355. + *
  18356. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18357. + * GNU General Public License ("GPL") version 2, as published by the Free
  18358. + * Software Foundation.
  18359. + *
  18360. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18361. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18362. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18363. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18364. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18365. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18366. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18367. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18368. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18369. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18370. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18371. + */
  18372. +
  18373. +#ifndef VCHI_CFG_INTERNAL_H_
  18374. +#define VCHI_CFG_INTERNAL_H_
  18375. +
  18376. +/****************************************************************************************
  18377. + * Control optimisation attempts.
  18378. + ***************************************************************************************/
  18379. +
  18380. +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
  18381. +#define VCHI_COARSE_LOCKING
  18382. +
  18383. +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
  18384. +// (only relevant if VCHI_COARSE_LOCKING)
  18385. +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
  18386. +
  18387. +// Avoid lock on non-blocking peek
  18388. +// (only relevant if VCHI_COARSE_LOCKING)
  18389. +#define VCHI_AVOID_PEEK_LOCK
  18390. +
  18391. +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
  18392. +#define VCHI_MULTIPLE_HANDLER_THREADS
  18393. +
  18394. +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
  18395. +// our way through the pool of descriptors.
  18396. +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
  18397. +
  18398. +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
  18399. +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
  18400. +
  18401. +// Don't use message descriptors for TX messages that don't need them
  18402. +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
  18403. +
  18404. +// Nano-locks for multiqueue
  18405. +//#define VCHI_MQUEUE_NANOLOCKS
  18406. +
  18407. +// Lock-free(er) dequeuing
  18408. +//#define VCHI_RX_NANOLOCKS
  18409. +
  18410. +#endif /*VCHI_CFG_INTERNAL_H_*/
  18411. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi_common.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_common.h
  18412. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi_common.h 1970-01-01 01:00:00.000000000 +0100
  18413. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_common.h 2014-03-11 16:52:43.000000000 +0100
  18414. @@ -0,0 +1,163 @@
  18415. +/**
  18416. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18417. + *
  18418. + * Redistribution and use in source and binary forms, with or without
  18419. + * modification, are permitted provided that the following conditions
  18420. + * are met:
  18421. + * 1. Redistributions of source code must retain the above copyright
  18422. + * notice, this list of conditions, and the following disclaimer,
  18423. + * without modification.
  18424. + * 2. Redistributions in binary form must reproduce the above copyright
  18425. + * notice, this list of conditions and the following disclaimer in the
  18426. + * documentation and/or other materials provided with the distribution.
  18427. + * 3. The names of the above-listed copyright holders may not be used
  18428. + * to endorse or promote products derived from this software without
  18429. + * specific prior written permission.
  18430. + *
  18431. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18432. + * GNU General Public License ("GPL") version 2, as published by the Free
  18433. + * Software Foundation.
  18434. + *
  18435. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18436. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18437. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18438. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18439. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18440. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18441. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18442. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18443. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18444. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18445. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18446. + */
  18447. +
  18448. +#ifndef VCHI_COMMON_H_
  18449. +#define VCHI_COMMON_H_
  18450. +
  18451. +
  18452. +//flags used when sending messages (must be bitmapped)
  18453. +typedef enum
  18454. +{
  18455. + VCHI_FLAGS_NONE = 0x0,
  18456. + 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)
  18457. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
  18458. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
  18459. + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
  18460. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
  18461. + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
  18462. +
  18463. + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
  18464. + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
  18465. + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
  18466. + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
  18467. + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
  18468. + VCHI_FLAGS_INTERNAL = 0xFF0000
  18469. +} VCHI_FLAGS_T;
  18470. +
  18471. +// constants for vchi_crc_control()
  18472. +typedef enum {
  18473. + VCHI_CRC_NOTHING = -1,
  18474. + VCHI_CRC_PER_SERVICE = 0,
  18475. + VCHI_CRC_EVERYTHING = 1,
  18476. +} VCHI_CRC_CONTROL_T;
  18477. +
  18478. +//callback reasons when an event occurs on a service
  18479. +typedef enum
  18480. +{
  18481. + VCHI_CALLBACK_REASON_MIN,
  18482. +
  18483. + //This indicates that there is data available
  18484. + //handle is the msg id that was transmitted with the data
  18485. + // When a message is received and there was no FULL message available previously, send callback
  18486. + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
  18487. + VCHI_CALLBACK_MSG_AVAILABLE,
  18488. + VCHI_CALLBACK_MSG_SENT,
  18489. + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
  18490. +
  18491. + // This indicates that a transfer from the other side has completed
  18492. + VCHI_CALLBACK_BULK_RECEIVED,
  18493. + //This indicates that data queued up to be sent has now gone
  18494. + //handle is the msg id that was used when sending the data
  18495. + VCHI_CALLBACK_BULK_SENT,
  18496. + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
  18497. + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
  18498. +
  18499. + VCHI_CALLBACK_SERVICE_CLOSED,
  18500. +
  18501. + // this side has sent XOFF to peer due to lack of data consumption by service
  18502. + // (suggests the service may need to take some recovery action if it has
  18503. + // been deliberately holding off consuming data)
  18504. + VCHI_CALLBACK_SENT_XOFF,
  18505. + VCHI_CALLBACK_SENT_XON,
  18506. +
  18507. + // indicates that a bulk transfer has finished reading the source buffer
  18508. + VCHI_CALLBACK_BULK_DATA_READ,
  18509. +
  18510. + // power notification events (currently host side only)
  18511. + VCHI_CALLBACK_PEER_OFF,
  18512. + VCHI_CALLBACK_PEER_SUSPENDED,
  18513. + VCHI_CALLBACK_PEER_ON,
  18514. + VCHI_CALLBACK_PEER_RESUMED,
  18515. + VCHI_CALLBACK_FORCED_POWER_OFF,
  18516. +
  18517. +#ifdef USE_VCHIQ_ARM
  18518. + // some extra notifications provided by vchiq_arm
  18519. + VCHI_CALLBACK_SERVICE_OPENED,
  18520. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  18521. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  18522. +#endif
  18523. +
  18524. + VCHI_CALLBACK_REASON_MAX
  18525. +} VCHI_CALLBACK_REASON_T;
  18526. +
  18527. +//Calback used by all services / bulk transfers
  18528. +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
  18529. + VCHI_CALLBACK_REASON_T reason,
  18530. + void *handle ); //for transmitting msg's only
  18531. +
  18532. +
  18533. +
  18534. +/*
  18535. + * Define vector struct for scatter-gather (vector) operations
  18536. + * Vectors can be nested - if a vector element has negative length, then
  18537. + * the data pointer is treated as pointing to another vector array, with
  18538. + * '-vec_len' elements. Thus to append a header onto an existing vector,
  18539. + * you can do this:
  18540. + *
  18541. + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
  18542. + * {
  18543. + * VCHI_MSG_VECTOR_T nv[2];
  18544. + * nv[0].vec_base = my_header;
  18545. + * nv[0].vec_len = sizeof my_header;
  18546. + * nv[1].vec_base = v;
  18547. + * nv[1].vec_len = -n;
  18548. + * ...
  18549. + *
  18550. + */
  18551. +typedef struct vchi_msg_vector {
  18552. + const void *vec_base;
  18553. + int32_t vec_len;
  18554. +} VCHI_MSG_VECTOR_T;
  18555. +
  18556. +// Opaque type for a connection API
  18557. +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
  18558. +
  18559. +// Opaque type for a message driver
  18560. +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
  18561. +
  18562. +
  18563. +// Iterator structure for reading ahead through received message queue. Allocated by client,
  18564. +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
  18565. +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
  18566. +// will not proceed to messages received since. Behaviour is undefined if an iterator
  18567. +// is used again after messages for that service are removed/dequeued by any
  18568. +// means other than vchi_msg_iter_... calls on the iterator itself.
  18569. +typedef struct {
  18570. + struct opaque_vchi_service_t *service;
  18571. + void *last;
  18572. + void *next;
  18573. + void *remove;
  18574. +} VCHI_MSG_ITER_T;
  18575. +
  18576. +
  18577. +#endif // VCHI_COMMON_H_
  18578. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi.h
  18579. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi.h 1970-01-01 01:00:00.000000000 +0100
  18580. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi.h 2014-03-11 16:52:43.000000000 +0100
  18581. @@ -0,0 +1,373 @@
  18582. +/**
  18583. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18584. + *
  18585. + * Redistribution and use in source and binary forms, with or without
  18586. + * modification, are permitted provided that the following conditions
  18587. + * are met:
  18588. + * 1. Redistributions of source code must retain the above copyright
  18589. + * notice, this list of conditions, and the following disclaimer,
  18590. + * without modification.
  18591. + * 2. Redistributions in binary form must reproduce the above copyright
  18592. + * notice, this list of conditions and the following disclaimer in the
  18593. + * documentation and/or other materials provided with the distribution.
  18594. + * 3. The names of the above-listed copyright holders may not be used
  18595. + * to endorse or promote products derived from this software without
  18596. + * specific prior written permission.
  18597. + *
  18598. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18599. + * GNU General Public License ("GPL") version 2, as published by the Free
  18600. + * Software Foundation.
  18601. + *
  18602. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18603. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18604. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18605. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18606. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18607. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18608. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18609. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18610. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18611. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18612. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18613. + */
  18614. +
  18615. +#ifndef VCHI_H_
  18616. +#define VCHI_H_
  18617. +
  18618. +#include "interface/vchi/vchi_cfg.h"
  18619. +#include "interface/vchi/vchi_common.h"
  18620. +#include "interface/vchi/connections/connection.h"
  18621. +#include "vchi_mh.h"
  18622. +
  18623. +
  18624. +/******************************************************************************
  18625. + Global defs
  18626. + *****************************************************************************/
  18627. +
  18628. +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
  18629. +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
  18630. +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
  18631. +
  18632. +#ifdef USE_VCHIQ_ARM
  18633. +#define VCHI_BULK_ALIGNED(x) 1
  18634. +#else
  18635. +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
  18636. +#endif
  18637. +
  18638. +struct vchi_version {
  18639. + uint32_t version;
  18640. + uint32_t version_min;
  18641. +};
  18642. +#define VCHI_VERSION(v_) { v_, v_ }
  18643. +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
  18644. +
  18645. +typedef enum
  18646. +{
  18647. + VCHI_VEC_POINTER,
  18648. + VCHI_VEC_HANDLE,
  18649. + VCHI_VEC_LIST
  18650. +} VCHI_MSG_VECTOR_TYPE_T;
  18651. +
  18652. +typedef struct vchi_msg_vector_ex {
  18653. +
  18654. + VCHI_MSG_VECTOR_TYPE_T type;
  18655. + union
  18656. + {
  18657. + // a memory handle
  18658. + struct
  18659. + {
  18660. + VCHI_MEM_HANDLE_T handle;
  18661. + uint32_t offset;
  18662. + int32_t vec_len;
  18663. + } handle;
  18664. +
  18665. + // an ordinary data pointer
  18666. + struct
  18667. + {
  18668. + const void *vec_base;
  18669. + int32_t vec_len;
  18670. + } ptr;
  18671. +
  18672. + // a nested vector list
  18673. + struct
  18674. + {
  18675. + struct vchi_msg_vector_ex *vec;
  18676. + uint32_t vec_len;
  18677. + } list;
  18678. + } u;
  18679. +} VCHI_MSG_VECTOR_EX_T;
  18680. +
  18681. +
  18682. +// Construct an entry in a msg vector for a pointer (p) of length (l)
  18683. +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
  18684. +
  18685. +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
  18686. +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
  18687. +
  18688. +// Macros to manipulate 'FOURCC' values
  18689. +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
  18690. +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
  18691. +
  18692. +
  18693. +// Opaque service information
  18694. +struct opaque_vchi_service_t;
  18695. +
  18696. +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
  18697. +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
  18698. +typedef struct
  18699. +{
  18700. + struct opaque_vchi_service_t *service;
  18701. + void *message;
  18702. +} VCHI_HELD_MSG_T;
  18703. +
  18704. +
  18705. +
  18706. +// structure used to provide the information needed to open a server or a client
  18707. +typedef struct {
  18708. + struct vchi_version version;
  18709. + int32_t service_id;
  18710. + VCHI_CONNECTION_T *connection;
  18711. + uint32_t rx_fifo_size;
  18712. + uint32_t tx_fifo_size;
  18713. + VCHI_CALLBACK_T callback;
  18714. + void *callback_param;
  18715. + /* client intends to receive bulk transfers of
  18716. + odd lengths or into unaligned buffers */
  18717. + int32_t want_unaligned_bulk_rx;
  18718. + /* client intends to transmit bulk transfers of
  18719. + odd lengths or out of unaligned buffers */
  18720. + int32_t want_unaligned_bulk_tx;
  18721. + /* client wants to check CRCs on (bulk) xfers.
  18722. + Only needs to be set at 1 end - will do both directions. */
  18723. + int32_t want_crc;
  18724. +} SERVICE_CREATION_T;
  18725. +
  18726. +// Opaque handle for a VCHI instance
  18727. +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
  18728. +
  18729. +// Opaque handle for a server or client
  18730. +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
  18731. +
  18732. +// Service registration & startup
  18733. +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
  18734. +
  18735. +typedef struct service_info_tag {
  18736. + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
  18737. + VCHI_SERVICE_INIT init; /* Service initialisation function */
  18738. + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
  18739. +} SERVICE_INFO_T;
  18740. +
  18741. +/******************************************************************************
  18742. + Global funcs - implementation is specific to which side you are on (local / remote)
  18743. + *****************************************************************************/
  18744. +
  18745. +#ifdef __cplusplus
  18746. +extern "C" {
  18747. +#endif
  18748. +
  18749. +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
  18750. + const VCHI_MESSAGE_DRIVER_T * low_level);
  18751. +
  18752. +
  18753. +// Routine used to initialise the vchi on both local + remote connections
  18754. +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
  18755. +
  18756. +extern int32_t vchi_exit( void );
  18757. +
  18758. +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
  18759. + const uint32_t num_connections,
  18760. + VCHI_INSTANCE_T instance_handle );
  18761. +
  18762. +//When this is called, ensure that all services have no data pending.
  18763. +//Bulk transfers can remain 'queued'
  18764. +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
  18765. +
  18766. +// Global control over bulk CRC checking
  18767. +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
  18768. + VCHI_CRC_CONTROL_T control );
  18769. +
  18770. +// helper functions
  18771. +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
  18772. +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
  18773. +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
  18774. +
  18775. +
  18776. +/******************************************************************************
  18777. + Global service API
  18778. + *****************************************************************************/
  18779. +// Routine to create a named service
  18780. +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
  18781. + SERVICE_CREATION_T *setup,
  18782. + VCHI_SERVICE_HANDLE_T *handle );
  18783. +
  18784. +// Routine to destory a service
  18785. +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
  18786. +
  18787. +// Routine to open a named service
  18788. +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
  18789. + SERVICE_CREATION_T *setup,
  18790. + VCHI_SERVICE_HANDLE_T *handle);
  18791. +
  18792. +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
  18793. + short *peer_version );
  18794. +
  18795. +// Routine to close a named service
  18796. +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
  18797. +
  18798. +// Routine to increment ref count on a named service
  18799. +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
  18800. +
  18801. +// Routine to decrement ref count on a named service
  18802. +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
  18803. +
  18804. +// Routine to send a message accross a service
  18805. +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
  18806. + const void *data,
  18807. + uint32_t data_size,
  18808. + VCHI_FLAGS_T flags,
  18809. + void *msg_handle );
  18810. +
  18811. +// scatter-gather (vector) and send message
  18812. +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
  18813. + VCHI_MSG_VECTOR_EX_T *vector,
  18814. + uint32_t count,
  18815. + VCHI_FLAGS_T flags,
  18816. + void *msg_handle );
  18817. +
  18818. +// legacy scatter-gather (vector) and send message, only handles pointers
  18819. +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
  18820. + VCHI_MSG_VECTOR_T *vector,
  18821. + uint32_t count,
  18822. + VCHI_FLAGS_T flags,
  18823. + void *msg_handle );
  18824. +
  18825. +// Routine to receive a msg from a service
  18826. +// Dequeue is equivalent to hold, copy into client buffer, release
  18827. +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
  18828. + void *data,
  18829. + uint32_t max_data_size_to_read,
  18830. + uint32_t *actual_msg_size,
  18831. + VCHI_FLAGS_T flags );
  18832. +
  18833. +// Routine to look at a message in place.
  18834. +// The message is not dequeued, so a subsequent call to peek or dequeue
  18835. +// will return the same message.
  18836. +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
  18837. + void **data,
  18838. + uint32_t *msg_size,
  18839. + VCHI_FLAGS_T flags );
  18840. +
  18841. +// Routine to remove a message after it has been read in place with peek
  18842. +// The first message on the queue is dequeued.
  18843. +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
  18844. +
  18845. +// Routine to look at a message in place.
  18846. +// The message is dequeued, so the caller is left holding it; the descriptor is
  18847. +// filled in and must be released when the user has finished with the message.
  18848. +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
  18849. + void **data, // } may be NULL, as info can be
  18850. + uint32_t *msg_size, // } obtained from HELD_MSG_T
  18851. + VCHI_FLAGS_T flags,
  18852. + VCHI_HELD_MSG_T *message_descriptor );
  18853. +
  18854. +// Initialise an iterator to look through messages in place
  18855. +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
  18856. + VCHI_MSG_ITER_T *iter,
  18857. + VCHI_FLAGS_T flags );
  18858. +
  18859. +/******************************************************************************
  18860. + Global service support API - operations on held messages and message iterators
  18861. + *****************************************************************************/
  18862. +
  18863. +// Routine to get the address of a held message
  18864. +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
  18865. +
  18866. +// Routine to get the size of a held message
  18867. +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
  18868. +
  18869. +// Routine to get the transmit timestamp as written into the header by the peer
  18870. +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
  18871. +
  18872. +// Routine to get the reception timestamp, written as we parsed the header
  18873. +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
  18874. +
  18875. +// Routine to release a held message after it has been processed
  18876. +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
  18877. +
  18878. +// Indicates whether the iterator has a next message.
  18879. +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
  18880. +
  18881. +// Return the pointer and length for the next message and advance the iterator.
  18882. +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
  18883. + void **data,
  18884. + uint32_t *msg_size );
  18885. +
  18886. +// Remove the last message returned by vchi_msg_iter_next.
  18887. +// Can only be called once after each call to vchi_msg_iter_next.
  18888. +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
  18889. +
  18890. +// Hold the last message returned by vchi_msg_iter_next.
  18891. +// Can only be called once after each call to vchi_msg_iter_next.
  18892. +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
  18893. + VCHI_HELD_MSG_T *message );
  18894. +
  18895. +// Return information for the next message, and hold it, advancing the iterator.
  18896. +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
  18897. + void **data, // } may be NULL
  18898. + uint32_t *msg_size, // }
  18899. + VCHI_HELD_MSG_T *message );
  18900. +
  18901. +
  18902. +/******************************************************************************
  18903. + Global bulk API
  18904. + *****************************************************************************/
  18905. +
  18906. +// Routine to prepare interface for a transfer from the other side
  18907. +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
  18908. + void *data_dst,
  18909. + uint32_t data_size,
  18910. + VCHI_FLAGS_T flags,
  18911. + void *transfer_handle );
  18912. +
  18913. +
  18914. +// Prepare interface for a transfer from the other side into relocatable memory.
  18915. +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
  18916. + VCHI_MEM_HANDLE_T h_dst,
  18917. + uint32_t offset,
  18918. + uint32_t data_size,
  18919. + const VCHI_FLAGS_T flags,
  18920. + void * const bulk_handle );
  18921. +
  18922. +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
  18923. +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
  18924. + const void *data_src,
  18925. + uint32_t data_size,
  18926. + VCHI_FLAGS_T flags,
  18927. + void *transfer_handle );
  18928. +
  18929. +
  18930. +/******************************************************************************
  18931. + Configuration plumbing
  18932. + *****************************************************************************/
  18933. +
  18934. +// function prototypes for the different mid layers (the state info gives the different physical connections)
  18935. +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
  18936. +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
  18937. +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
  18938. +
  18939. +// declare all message drivers here
  18940. +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
  18941. +
  18942. +#ifdef __cplusplus
  18943. +}
  18944. +#endif
  18945. +
  18946. +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
  18947. + VCHI_MEM_HANDLE_T h_src,
  18948. + uint32_t offset,
  18949. + uint32_t data_size,
  18950. + VCHI_FLAGS_T flags,
  18951. + void *transfer_handle );
  18952. +#endif /* VCHI_H_ */
  18953. +
  18954. +/****************************** End of file **********************************/
  18955. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi_mh.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  18956. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 1970-01-01 01:00:00.000000000 +0100
  18957. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 2014-03-11 16:52:43.000000000 +0100
  18958. @@ -0,0 +1,42 @@
  18959. +/**
  18960. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  18961. + *
  18962. + * Redistribution and use in source and binary forms, with or without
  18963. + * modification, are permitted provided that the following conditions
  18964. + * are met:
  18965. + * 1. Redistributions of source code must retain the above copyright
  18966. + * notice, this list of conditions, and the following disclaimer,
  18967. + * without modification.
  18968. + * 2. Redistributions in binary form must reproduce the above copyright
  18969. + * notice, this list of conditions and the following disclaimer in the
  18970. + * documentation and/or other materials provided with the distribution.
  18971. + * 3. The names of the above-listed copyright holders may not be used
  18972. + * to endorse or promote products derived from this software without
  18973. + * specific prior written permission.
  18974. + *
  18975. + * ALTERNATIVELY, this software may be distributed under the terms of the
  18976. + * GNU General Public License ("GPL") version 2, as published by the Free
  18977. + * Software Foundation.
  18978. + *
  18979. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  18980. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  18981. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18982. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18983. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18984. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18985. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18986. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  18987. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  18988. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  18989. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18990. + */
  18991. +
  18992. +#ifndef VCHI_MH_H_
  18993. +#define VCHI_MH_H_
  18994. +
  18995. +#include <linux/types.h>
  18996. +
  18997. +typedef int32_t VCHI_MEM_HANDLE_T;
  18998. +#define VCHI_MEM_HANDLE_INVALID 0
  18999. +
  19000. +#endif
  19001. diff -Nur linux-3.13.6/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
  19002. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 1970-01-01 01:00:00.000000000 +0100
  19003. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 2014-03-11 16:52:43.000000000 +0100
  19004. @@ -0,0 +1,561 @@
  19005. +/**
  19006. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19007. + *
  19008. + * Redistribution and use in source and binary forms, with or without
  19009. + * modification, are permitted provided that the following conditions
  19010. + * are met:
  19011. + * 1. Redistributions of source code must retain the above copyright
  19012. + * notice, this list of conditions, and the following disclaimer,
  19013. + * without modification.
  19014. + * 2. Redistributions in binary form must reproduce the above copyright
  19015. + * notice, this list of conditions and the following disclaimer in the
  19016. + * documentation and/or other materials provided with the distribution.
  19017. + * 3. The names of the above-listed copyright holders may not be used
  19018. + * to endorse or promote products derived from this software without
  19019. + * specific prior written permission.
  19020. + *
  19021. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19022. + * GNU General Public License ("GPL") version 2, as published by the Free
  19023. + * Software Foundation.
  19024. + *
  19025. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19026. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19027. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19028. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19029. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19030. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19031. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19032. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19033. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19034. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19035. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19036. + */
  19037. +
  19038. +#include <linux/kernel.h>
  19039. +#include <linux/types.h>
  19040. +#include <linux/errno.h>
  19041. +#include <linux/interrupt.h>
  19042. +#include <linux/irq.h>
  19043. +#include <linux/pagemap.h>
  19044. +#include <linux/dma-mapping.h>
  19045. +#include <linux/version.h>
  19046. +#include <linux/io.h>
  19047. +#include <linux/uaccess.h>
  19048. +#include <asm/pgtable.h>
  19049. +
  19050. +#include <mach/irqs.h>
  19051. +
  19052. +#include <mach/platform.h>
  19053. +#include <mach/vcio.h>
  19054. +
  19055. +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
  19056. +
  19057. +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
  19058. +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
  19059. +
  19060. +#include "vchiq_arm.h"
  19061. +#include "vchiq_2835.h"
  19062. +#include "vchiq_connected.h"
  19063. +
  19064. +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
  19065. +
  19066. +typedef struct vchiq_2835_state_struct {
  19067. + int inited;
  19068. + VCHIQ_ARM_STATE_T arm_state;
  19069. +} VCHIQ_2835_ARM_STATE_T;
  19070. +
  19071. +static char *g_slot_mem;
  19072. +static int g_slot_mem_size;
  19073. +dma_addr_t g_slot_phys;
  19074. +static FRAGMENTS_T *g_fragments_base;
  19075. +static FRAGMENTS_T *g_free_fragments;
  19076. +struct semaphore g_free_fragments_sema;
  19077. +
  19078. +extern int vchiq_arm_log_level;
  19079. +
  19080. +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
  19081. +
  19082. +static irqreturn_t
  19083. +vchiq_doorbell_irq(int irq, void *dev_id);
  19084. +
  19085. +static int
  19086. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  19087. + struct task_struct *task, PAGELIST_T ** ppagelist);
  19088. +
  19089. +static void
  19090. +free_pagelist(PAGELIST_T *pagelist, int actual);
  19091. +
  19092. +int __init
  19093. +vchiq_platform_init(VCHIQ_STATE_T *state)
  19094. +{
  19095. + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
  19096. + int frag_mem_size;
  19097. + int err;
  19098. + int i;
  19099. +
  19100. + /* Allocate space for the channels in coherent memory */
  19101. + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
  19102. + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
  19103. +
  19104. + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
  19105. + &g_slot_phys, GFP_ATOMIC);
  19106. +
  19107. + if (!g_slot_mem) {
  19108. + vchiq_log_error(vchiq_arm_log_level,
  19109. + "Unable to allocate channel memory");
  19110. + err = -ENOMEM;
  19111. + goto failed_alloc;
  19112. + }
  19113. +
  19114. + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
  19115. +
  19116. + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
  19117. + if (!vchiq_slot_zero) {
  19118. + err = -EINVAL;
  19119. + goto failed_init_slots;
  19120. + }
  19121. +
  19122. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
  19123. + (int)g_slot_phys + g_slot_mem_size;
  19124. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
  19125. + MAX_FRAGMENTS;
  19126. +
  19127. + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
  19128. + g_slot_mem_size += frag_mem_size;
  19129. +
  19130. + g_free_fragments = g_fragments_base;
  19131. + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
  19132. + *(FRAGMENTS_T **)&g_fragments_base[i] =
  19133. + &g_fragments_base[i + 1];
  19134. + }
  19135. + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
  19136. + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
  19137. +
  19138. + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
  19139. + VCHIQ_SUCCESS) {
  19140. + err = -EINVAL;
  19141. + goto failed_vchiq_init;
  19142. + }
  19143. +
  19144. + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
  19145. + IRQF_IRQPOLL, "VCHIQ doorbell",
  19146. + state);
  19147. + if (err < 0) {
  19148. + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
  19149. + "irq=%d err=%d", __func__,
  19150. + VCHIQ_DOORBELL_IRQ, err);
  19151. + goto failed_request_irq;
  19152. + }
  19153. +
  19154. + /* Send the base address of the slots to VideoCore */
  19155. +
  19156. + dsb(); /* Ensure all writes have completed */
  19157. +
  19158. + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
  19159. +
  19160. + vchiq_log_info(vchiq_arm_log_level,
  19161. + "vchiq_init - done (slots %x, phys %x)",
  19162. + (unsigned int)vchiq_slot_zero, g_slot_phys);
  19163. +
  19164. + vchiq_call_connected_callbacks();
  19165. +
  19166. + return 0;
  19167. +
  19168. +failed_request_irq:
  19169. +failed_vchiq_init:
  19170. +failed_init_slots:
  19171. + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
  19172. +
  19173. +failed_alloc:
  19174. + return err;
  19175. +}
  19176. +
  19177. +void __exit
  19178. +vchiq_platform_exit(VCHIQ_STATE_T *state)
  19179. +{
  19180. + free_irq(VCHIQ_DOORBELL_IRQ, state);
  19181. + dma_free_coherent(NULL, g_slot_mem_size,
  19182. + g_slot_mem, g_slot_phys);
  19183. +}
  19184. +
  19185. +
  19186. +VCHIQ_STATUS_T
  19187. +vchiq_platform_init_state(VCHIQ_STATE_T *state)
  19188. +{
  19189. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  19190. + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
  19191. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
  19192. + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
  19193. + if(status != VCHIQ_SUCCESS)
  19194. + {
  19195. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
  19196. + }
  19197. + return status;
  19198. +}
  19199. +
  19200. +VCHIQ_ARM_STATE_T*
  19201. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
  19202. +{
  19203. + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
  19204. + {
  19205. + BUG();
  19206. + }
  19207. + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
  19208. +}
  19209. +
  19210. +void
  19211. +remote_event_signal(REMOTE_EVENT_T *event)
  19212. +{
  19213. + wmb();
  19214. +
  19215. + event->fired = 1;
  19216. +
  19217. + dsb(); /* data barrier operation */
  19218. +
  19219. + if (event->armed) {
  19220. + /* trigger vc interrupt */
  19221. +
  19222. + writel(0, __io_address(ARM_0_BELL2));
  19223. + }
  19224. +}
  19225. +
  19226. +int
  19227. +vchiq_copy_from_user(void *dst, const void *src, int size)
  19228. +{
  19229. + if ((uint32_t)src < TASK_SIZE) {
  19230. + return copy_from_user(dst, src, size);
  19231. + } else {
  19232. + memcpy(dst, src, size);
  19233. + return 0;
  19234. + }
  19235. +}
  19236. +
  19237. +VCHIQ_STATUS_T
  19238. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
  19239. + void *offset, int size, int dir)
  19240. +{
  19241. + PAGELIST_T *pagelist;
  19242. + int ret;
  19243. +
  19244. + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
  19245. +
  19246. + ret = create_pagelist((char __user *)offset, size,
  19247. + (dir == VCHIQ_BULK_RECEIVE)
  19248. + ? PAGELIST_READ
  19249. + : PAGELIST_WRITE,
  19250. + current,
  19251. + &pagelist);
  19252. + if (ret != 0)
  19253. + return VCHIQ_ERROR;
  19254. +
  19255. + bulk->handle = memhandle;
  19256. + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
  19257. +
  19258. + /* Store the pagelist address in remote_data, which isn't used by the
  19259. + slave. */
  19260. + bulk->remote_data = pagelist;
  19261. +
  19262. + return VCHIQ_SUCCESS;
  19263. +}
  19264. +
  19265. +void
  19266. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
  19267. +{
  19268. + if (bulk && bulk->remote_data && bulk->actual)
  19269. + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
  19270. +}
  19271. +
  19272. +void
  19273. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
  19274. +{
  19275. + /*
  19276. + * This should only be called on the master (VideoCore) side, but
  19277. + * provide an implementation to avoid the need for ifdefery.
  19278. + */
  19279. + BUG();
  19280. +}
  19281. +
  19282. +void
  19283. +vchiq_dump_platform_state(void *dump_context)
  19284. +{
  19285. + char buf[80];
  19286. + int len;
  19287. + len = snprintf(buf, sizeof(buf),
  19288. + " Platform: 2835 (VC master)");
  19289. + vchiq_dump(dump_context, buf, len + 1);
  19290. +}
  19291. +
  19292. +VCHIQ_STATUS_T
  19293. +vchiq_platform_suspend(VCHIQ_STATE_T *state)
  19294. +{
  19295. + return VCHIQ_ERROR;
  19296. +}
  19297. +
  19298. +VCHIQ_STATUS_T
  19299. +vchiq_platform_resume(VCHIQ_STATE_T *state)
  19300. +{
  19301. + return VCHIQ_SUCCESS;
  19302. +}
  19303. +
  19304. +void
  19305. +vchiq_platform_paused(VCHIQ_STATE_T *state)
  19306. +{
  19307. +}
  19308. +
  19309. +void
  19310. +vchiq_platform_resumed(VCHIQ_STATE_T *state)
  19311. +{
  19312. +}
  19313. +
  19314. +int
  19315. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
  19316. +{
  19317. + return 1; // autosuspend not supported - videocore always wanted
  19318. +}
  19319. +
  19320. +int
  19321. +vchiq_platform_use_suspend_timer(void)
  19322. +{
  19323. + return 0;
  19324. +}
  19325. +void
  19326. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
  19327. +{
  19328. + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
  19329. +}
  19330. +void
  19331. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
  19332. +{
  19333. + (void)state;
  19334. +}
  19335. +/*
  19336. + * Local functions
  19337. + */
  19338. +
  19339. +static irqreturn_t
  19340. +vchiq_doorbell_irq(int irq, void *dev_id)
  19341. +{
  19342. + VCHIQ_STATE_T *state = dev_id;
  19343. + irqreturn_t ret = IRQ_NONE;
  19344. + unsigned int status;
  19345. +
  19346. + /* Read (and clear) the doorbell */
  19347. + status = readl(__io_address(ARM_0_BELL0));
  19348. +
  19349. + if (status & 0x4) { /* Was the doorbell rung? */
  19350. + remote_event_pollall(state);
  19351. + ret = IRQ_HANDLED;
  19352. + }
  19353. +
  19354. + return ret;
  19355. +}
  19356. +
  19357. +/* There is a potential problem with partial cache lines (pages?)
  19358. +** at the ends of the block when reading. If the CPU accessed anything in
  19359. +** the same line (page?) then it may have pulled old data into the cache,
  19360. +** obscuring the new data underneath. We can solve this by transferring the
  19361. +** partial cache lines separately, and allowing the ARM to copy into the
  19362. +** cached area.
  19363. +
  19364. +** N.B. This implementation plays slightly fast and loose with the Linux
  19365. +** driver programming rules, e.g. its use of __virt_to_bus instead of
  19366. +** dma_map_single, but it isn't a multi-platform driver and it benefits
  19367. +** from increased speed as a result.
  19368. +*/
  19369. +
  19370. +static int
  19371. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  19372. + struct task_struct *task, PAGELIST_T ** ppagelist)
  19373. +{
  19374. + PAGELIST_T *pagelist;
  19375. + struct page **pages;
  19376. + struct page *page;
  19377. + unsigned long *addrs;
  19378. + unsigned int num_pages, offset, i;
  19379. + char *addr, *base_addr, *next_addr;
  19380. + int run, addridx, actual_pages;
  19381. + unsigned long *need_release;
  19382. +
  19383. + offset = (unsigned int)buf & (PAGE_SIZE - 1);
  19384. + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
  19385. +
  19386. + *ppagelist = NULL;
  19387. +
  19388. + /* Allocate enough storage to hold the page pointers and the page
  19389. + ** list
  19390. + */
  19391. + pagelist = kmalloc(sizeof(PAGELIST_T) +
  19392. + (num_pages * sizeof(unsigned long)) +
  19393. + sizeof(unsigned long) +
  19394. + (num_pages * sizeof(pages[0])),
  19395. + GFP_KERNEL);
  19396. +
  19397. + vchiq_log_trace(vchiq_arm_log_level,
  19398. + "create_pagelist - %x", (unsigned int)pagelist);
  19399. + if (!pagelist)
  19400. + return -ENOMEM;
  19401. +
  19402. + addrs = pagelist->addrs;
  19403. + need_release = (unsigned long *)(addrs + num_pages);
  19404. + pages = (struct page **)(addrs + num_pages + 1);
  19405. +
  19406. + if (is_vmalloc_addr(buf)) {
  19407. + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
  19408. + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
  19409. + }
  19410. + *need_release = 0; /* do not try and release vmalloc pages */
  19411. + } else {
  19412. + down_read(&task->mm->mmap_sem);
  19413. + actual_pages = get_user_pages(task, task->mm,
  19414. + (unsigned long)buf & ~(PAGE_SIZE - 1),
  19415. + num_pages,
  19416. + (type == PAGELIST_READ) /*Write */ ,
  19417. + 0 /*Force */ ,
  19418. + pages,
  19419. + NULL /*vmas */);
  19420. + up_read(&task->mm->mmap_sem);
  19421. +
  19422. + if (actual_pages != num_pages) {
  19423. + vchiq_log_info(vchiq_arm_log_level,
  19424. + "create_pagelist - only %d/%d pages locked",
  19425. + actual_pages,
  19426. + num_pages);
  19427. +
  19428. + /* This is probably due to the process being killed */
  19429. + while (actual_pages > 0)
  19430. + {
  19431. + actual_pages--;
  19432. + page_cache_release(pages[actual_pages]);
  19433. + }
  19434. + kfree(pagelist);
  19435. + if (actual_pages == 0)
  19436. + actual_pages = -ENOMEM;
  19437. + return actual_pages;
  19438. + }
  19439. + *need_release = 1; /* release user pages */
  19440. + }
  19441. +
  19442. + pagelist->length = count;
  19443. + pagelist->type = type;
  19444. + pagelist->offset = offset;
  19445. +
  19446. + /* Group the pages into runs of contiguous pages */
  19447. +
  19448. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  19449. + next_addr = base_addr + PAGE_SIZE;
  19450. + addridx = 0;
  19451. + run = 0;
  19452. +
  19453. + for (i = 1; i < num_pages; i++) {
  19454. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  19455. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  19456. + next_addr += PAGE_SIZE;
  19457. + run++;
  19458. + } else {
  19459. + addrs[addridx] = (unsigned long)base_addr + run;
  19460. + addridx++;
  19461. + base_addr = addr;
  19462. + next_addr = addr + PAGE_SIZE;
  19463. + run = 0;
  19464. + }
  19465. + }
  19466. +
  19467. + addrs[addridx] = (unsigned long)base_addr + run;
  19468. + addridx++;
  19469. +
  19470. + /* Partial cache lines (fragments) require special measures */
  19471. + if ((type == PAGELIST_READ) &&
  19472. + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
  19473. + ((pagelist->offset + pagelist->length) &
  19474. + (CACHE_LINE_SIZE - 1)))) {
  19475. + FRAGMENTS_T *fragments;
  19476. +
  19477. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  19478. + kfree(pagelist);
  19479. + return -EINTR;
  19480. + }
  19481. +
  19482. + WARN_ON(g_free_fragments == NULL);
  19483. +
  19484. + down(&g_free_fragments_mutex);
  19485. + fragments = (FRAGMENTS_T *) g_free_fragments;
  19486. + WARN_ON(fragments == NULL);
  19487. + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
  19488. + up(&g_free_fragments_mutex);
  19489. + pagelist->type =
  19490. + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
  19491. + g_fragments_base);
  19492. + }
  19493. +
  19494. + for (page = virt_to_page(pagelist);
  19495. + page <= virt_to_page(addrs + num_pages - 1); page++) {
  19496. + flush_dcache_page(page);
  19497. + }
  19498. +
  19499. + *ppagelist = pagelist;
  19500. +
  19501. + return 0;
  19502. +}
  19503. +
  19504. +static void
  19505. +free_pagelist(PAGELIST_T *pagelist, int actual)
  19506. +{
  19507. + unsigned long *need_release;
  19508. + struct page **pages;
  19509. + unsigned int num_pages, i;
  19510. +
  19511. + vchiq_log_trace(vchiq_arm_log_level,
  19512. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  19513. +
  19514. + num_pages =
  19515. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  19516. + PAGE_SIZE;
  19517. +
  19518. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  19519. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  19520. +
  19521. + /* Deal with any partial cache lines (fragments) */
  19522. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  19523. + FRAGMENTS_T *fragments = g_fragments_base +
  19524. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
  19525. + int head_bytes, tail_bytes;
  19526. + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
  19527. + (CACHE_LINE_SIZE - 1);
  19528. + tail_bytes = (pagelist->offset + actual) &
  19529. + (CACHE_LINE_SIZE - 1);
  19530. +
  19531. + if ((actual >= 0) && (head_bytes != 0)) {
  19532. + if (head_bytes > actual)
  19533. + head_bytes = actual;
  19534. +
  19535. + memcpy((char *)page_address(pages[0]) +
  19536. + pagelist->offset,
  19537. + fragments->headbuf,
  19538. + head_bytes);
  19539. + }
  19540. + if ((actual >= 0) && (head_bytes < actual) &&
  19541. + (tail_bytes != 0)) {
  19542. + memcpy((char *)page_address(pages[num_pages - 1]) +
  19543. + ((pagelist->offset + actual) &
  19544. + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
  19545. + fragments->tailbuf, tail_bytes);
  19546. + }
  19547. +
  19548. + down(&g_free_fragments_mutex);
  19549. + *(FRAGMENTS_T **) fragments = g_free_fragments;
  19550. + g_free_fragments = fragments;
  19551. + up(&g_free_fragments_mutex);
  19552. + up(&g_free_fragments_sema);
  19553. + }
  19554. +
  19555. + if (*need_release) {
  19556. + for (i = 0; i < num_pages; i++) {
  19557. + if (pagelist->type != PAGELIST_WRITE)
  19558. + set_page_dirty(pages[i]);
  19559. +
  19560. + page_cache_release(pages[i]);
  19561. + }
  19562. + }
  19563. +
  19564. + kfree(pagelist);
  19565. +}
  19566. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  19567. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 1970-01-01 01:00:00.000000000 +0100
  19568. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 2014-03-11 16:52:43.000000000 +0100
  19569. @@ -0,0 +1,42 @@
  19570. +/**
  19571. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19572. + *
  19573. + * Redistribution and use in source and binary forms, with or without
  19574. + * modification, are permitted provided that the following conditions
  19575. + * are met:
  19576. + * 1. Redistributions of source code must retain the above copyright
  19577. + * notice, this list of conditions, and the following disclaimer,
  19578. + * without modification.
  19579. + * 2. Redistributions in binary form must reproduce the above copyright
  19580. + * notice, this list of conditions and the following disclaimer in the
  19581. + * documentation and/or other materials provided with the distribution.
  19582. + * 3. The names of the above-listed copyright holders may not be used
  19583. + * to endorse or promote products derived from this software without
  19584. + * specific prior written permission.
  19585. + *
  19586. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19587. + * GNU General Public License ("GPL") version 2, as published by the Free
  19588. + * Software Foundation.
  19589. + *
  19590. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19591. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19592. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19593. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19594. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19595. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19596. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19597. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19598. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19599. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19600. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19601. + */
  19602. +
  19603. +#ifndef VCHIQ_2835_H
  19604. +#define VCHIQ_2835_H
  19605. +
  19606. +#include "vchiq_pagelist.h"
  19607. +
  19608. +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
  19609. +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
  19610. +
  19611. +#endif /* VCHIQ_2835_H */
  19612. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  19613. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 1970-01-01 01:00:00.000000000 +0100
  19614. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 2014-03-11 16:54:58.000000000 +0100
  19615. @@ -0,0 +1,2813 @@
  19616. +/**
  19617. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  19618. + *
  19619. + * Redistribution and use in source and binary forms, with or without
  19620. + * modification, are permitted provided that the following conditions
  19621. + * are met:
  19622. + * 1. Redistributions of source code must retain the above copyright
  19623. + * notice, this list of conditions, and the following disclaimer,
  19624. + * without modification.
  19625. + * 2. Redistributions in binary form must reproduce the above copyright
  19626. + * notice, this list of conditions and the following disclaimer in the
  19627. + * documentation and/or other materials provided with the distribution.
  19628. + * 3. The names of the above-listed copyright holders may not be used
  19629. + * to endorse or promote products derived from this software without
  19630. + * specific prior written permission.
  19631. + *
  19632. + * ALTERNATIVELY, this software may be distributed under the terms of the
  19633. + * GNU General Public License ("GPL") version 2, as published by the Free
  19634. + * Software Foundation.
  19635. + *
  19636. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19637. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19638. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19639. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19640. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19641. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19642. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19643. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19644. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  19645. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  19646. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19647. + */
  19648. +
  19649. +#include <linux/kernel.h>
  19650. +#include <linux/module.h>
  19651. +#include <linux/types.h>
  19652. +#include <linux/errno.h>
  19653. +#include <linux/cdev.h>
  19654. +#include <linux/fs.h>
  19655. +#include <linux/device.h>
  19656. +#include <linux/mm.h>
  19657. +#include <linux/highmem.h>
  19658. +#include <linux/pagemap.h>
  19659. +#include <linux/bug.h>
  19660. +#include <linux/semaphore.h>
  19661. +#include <linux/list.h>
  19662. +#include <linux/proc_fs.h>
  19663. +
  19664. +#include "vchiq_core.h"
  19665. +#include "vchiq_ioctl.h"
  19666. +#include "vchiq_arm.h"
  19667. +
  19668. +#define DEVICE_NAME "vchiq"
  19669. +
  19670. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  19671. +#undef MODULE_PARAM_PREFIX
  19672. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  19673. +
  19674. +#define VCHIQ_MINOR 0
  19675. +
  19676. +/* Some per-instance constants */
  19677. +#define MAX_COMPLETIONS 16
  19678. +#define MAX_SERVICES 64
  19679. +#define MAX_ELEMENTS 8
  19680. +#define MSG_QUEUE_SIZE 64
  19681. +
  19682. +#define KEEPALIVE_VER 1
  19683. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  19684. +
  19685. +/* Run time control of log level, based on KERN_XXX level. */
  19686. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  19687. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  19688. +
  19689. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  19690. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  19691. +
  19692. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  19693. +static const char *const suspend_state_names[] = {
  19694. + "VC_SUSPEND_FORCE_CANCELED",
  19695. + "VC_SUSPEND_REJECTED",
  19696. + "VC_SUSPEND_FAILED",
  19697. + "VC_SUSPEND_IDLE",
  19698. + "VC_SUSPEND_REQUESTED",
  19699. + "VC_SUSPEND_IN_PROGRESS",
  19700. + "VC_SUSPEND_SUSPENDED"
  19701. +};
  19702. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  19703. +static const char *const resume_state_names[] = {
  19704. + "VC_RESUME_FAILED",
  19705. + "VC_RESUME_IDLE",
  19706. + "VC_RESUME_REQUESTED",
  19707. + "VC_RESUME_IN_PROGRESS",
  19708. + "VC_RESUME_RESUMED"
  19709. +};
  19710. +/* The number of times we allow force suspend to timeout before actually
  19711. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  19712. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  19713. +*/
  19714. +#define FORCE_SUSPEND_FAIL_MAX 8
  19715. +
  19716. +/* The time in ms allowed for videocore to go idle when force suspend has been
  19717. + * requested */
  19718. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  19719. +
  19720. +
  19721. +static void suspend_timer_callback(unsigned long context);
  19722. +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance);
  19723. +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance);
  19724. +
  19725. +
  19726. +typedef struct user_service_struct {
  19727. + VCHIQ_SERVICE_T *service;
  19728. + void *userdata;
  19729. + VCHIQ_INSTANCE_T instance;
  19730. + int is_vchi;
  19731. + int dequeue_pending;
  19732. + int message_available_pos;
  19733. + int msg_insert;
  19734. + int msg_remove;
  19735. + struct semaphore insert_event;
  19736. + struct semaphore remove_event;
  19737. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  19738. +} USER_SERVICE_T;
  19739. +
  19740. +struct bulk_waiter_node {
  19741. + struct bulk_waiter bulk_waiter;
  19742. + int pid;
  19743. + struct list_head list;
  19744. +};
  19745. +
  19746. +struct vchiq_instance_struct {
  19747. + VCHIQ_STATE_T *state;
  19748. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  19749. + int completion_insert;
  19750. + int completion_remove;
  19751. + struct semaphore insert_event;
  19752. + struct semaphore remove_event;
  19753. + struct mutex completion_mutex;
  19754. +
  19755. + int connected;
  19756. + int closing;
  19757. + int pid;
  19758. + int mark;
  19759. +
  19760. + struct list_head bulk_waiter_list;
  19761. + struct mutex bulk_waiter_list_mutex;
  19762. +
  19763. + struct proc_dir_entry *proc_entry;
  19764. +};
  19765. +
  19766. +typedef struct dump_context_struct {
  19767. + char __user *buf;
  19768. + size_t actual;
  19769. + size_t space;
  19770. + loff_t offset;
  19771. +} DUMP_CONTEXT_T;
  19772. +
  19773. +static struct cdev vchiq_cdev;
  19774. +static dev_t vchiq_devid;
  19775. +static VCHIQ_STATE_T g_state;
  19776. +static struct class *vchiq_class;
  19777. +static struct device *vchiq_dev;
  19778. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  19779. +
  19780. +static const char *const ioctl_names[] = {
  19781. + "CONNECT",
  19782. + "SHUTDOWN",
  19783. + "CREATE_SERVICE",
  19784. + "REMOVE_SERVICE",
  19785. + "QUEUE_MESSAGE",
  19786. + "QUEUE_BULK_TRANSMIT",
  19787. + "QUEUE_BULK_RECEIVE",
  19788. + "AWAIT_COMPLETION",
  19789. + "DEQUEUE_MESSAGE",
  19790. + "GET_CLIENT_ID",
  19791. + "GET_CONFIG",
  19792. + "CLOSE_SERVICE",
  19793. + "USE_SERVICE",
  19794. + "RELEASE_SERVICE",
  19795. + "SET_SERVICE_OPTION",
  19796. + "DUMP_PHYS_MEM"
  19797. +};
  19798. +
  19799. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  19800. + (VCHIQ_IOC_MAX + 1));
  19801. +
  19802. +static void
  19803. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  19804. +
  19805. +/****************************************************************************
  19806. +*
  19807. +* add_completion
  19808. +*
  19809. +***************************************************************************/
  19810. +
  19811. +static VCHIQ_STATUS_T
  19812. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  19813. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  19814. + void *bulk_userdata)
  19815. +{
  19816. + VCHIQ_COMPLETION_DATA_T *completion;
  19817. + DEBUG_INITIALISE(g_state.local)
  19818. +
  19819. + while (instance->completion_insert ==
  19820. + (instance->completion_remove + MAX_COMPLETIONS)) {
  19821. + /* Out of space - wait for the client */
  19822. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19823. + vchiq_log_trace(vchiq_arm_log_level,
  19824. + "add_completion - completion queue full");
  19825. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  19826. + if (down_interruptible(&instance->remove_event) != 0) {
  19827. + vchiq_log_info(vchiq_arm_log_level,
  19828. + "service_callback interrupted");
  19829. + return VCHIQ_RETRY;
  19830. + } else if (instance->closing) {
  19831. + vchiq_log_info(vchiq_arm_log_level,
  19832. + "service_callback closing");
  19833. + return VCHIQ_ERROR;
  19834. + }
  19835. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19836. + }
  19837. +
  19838. + completion =
  19839. + &instance->completions[instance->completion_insert &
  19840. + (MAX_COMPLETIONS - 1)];
  19841. +
  19842. + completion->header = header;
  19843. + completion->reason = reason;
  19844. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  19845. + completion->service_userdata = user_service->service;
  19846. + completion->bulk_userdata = bulk_userdata;
  19847. +
  19848. + if (reason == VCHIQ_SERVICE_CLOSED)
  19849. + /* Take an extra reference, to be held until
  19850. + this CLOSED notification is delivered. */
  19851. + lock_service(user_service->service);
  19852. +
  19853. + /* A write barrier is needed here to ensure that the entire completion
  19854. + record is written out before the insert point. */
  19855. + wmb();
  19856. +
  19857. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  19858. + user_service->message_available_pos =
  19859. + instance->completion_insert;
  19860. + instance->completion_insert++;
  19861. +
  19862. + up(&instance->insert_event);
  19863. +
  19864. + return VCHIQ_SUCCESS;
  19865. +}
  19866. +
  19867. +/****************************************************************************
  19868. +*
  19869. +* service_callback
  19870. +*
  19871. +***************************************************************************/
  19872. +
  19873. +static VCHIQ_STATUS_T
  19874. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  19875. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  19876. +{
  19877. + /* How do we ensure the callback goes to the right client?
  19878. + ** The service_user data points to a USER_SERVICE_T record containing
  19879. + ** the original callback and the user state structure, which contains a
  19880. + ** circular buffer for completion records.
  19881. + */
  19882. + USER_SERVICE_T *user_service;
  19883. + VCHIQ_SERVICE_T *service;
  19884. + VCHIQ_INSTANCE_T instance;
  19885. + DEBUG_INITIALISE(g_state.local)
  19886. +
  19887. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19888. +
  19889. + service = handle_to_service(handle);
  19890. + BUG_ON(!service);
  19891. + user_service = (USER_SERVICE_T *)service->base.userdata;
  19892. + instance = user_service->instance;
  19893. +
  19894. + if (!instance || instance->closing)
  19895. + return VCHIQ_SUCCESS;
  19896. +
  19897. + vchiq_log_trace(vchiq_arm_log_level,
  19898. + "service_callback - service %lx(%d), reason %d, header %lx, "
  19899. + "instance %lx, bulk_userdata %lx",
  19900. + (unsigned long)user_service,
  19901. + service->localport,
  19902. + reason, (unsigned long)header,
  19903. + (unsigned long)instance, (unsigned long)bulk_userdata);
  19904. +
  19905. + if (header && user_service->is_vchi) {
  19906. + spin_lock(&msg_queue_spinlock);
  19907. + while (user_service->msg_insert ==
  19908. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  19909. + spin_unlock(&msg_queue_spinlock);
  19910. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19911. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  19912. + vchiq_log_trace(vchiq_arm_log_level,
  19913. + "service_callback - msg queue full");
  19914. + /* If there is no MESSAGE_AVAILABLE in the completion
  19915. + ** queue, add one
  19916. + */
  19917. + if ((user_service->message_available_pos -
  19918. + instance->completion_remove) < 0) {
  19919. + VCHIQ_STATUS_T status;
  19920. + vchiq_log_info(vchiq_arm_log_level,
  19921. + "Inserting extra MESSAGE_AVAILABLE");
  19922. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19923. + status = add_completion(instance, reason,
  19924. + NULL, user_service, bulk_userdata);
  19925. + if (status != VCHIQ_SUCCESS) {
  19926. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19927. + return status;
  19928. + }
  19929. + }
  19930. +
  19931. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19932. + if (down_interruptible(&user_service->remove_event)
  19933. + != 0) {
  19934. + vchiq_log_info(vchiq_arm_log_level,
  19935. + "service_callback interrupted");
  19936. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19937. + return VCHIQ_RETRY;
  19938. + } else if (instance->closing) {
  19939. + vchiq_log_info(vchiq_arm_log_level,
  19940. + "service_callback closing");
  19941. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19942. + return VCHIQ_ERROR;
  19943. + }
  19944. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19945. + spin_lock(&msg_queue_spinlock);
  19946. + }
  19947. +
  19948. + user_service->msg_queue[user_service->msg_insert &
  19949. + (MSG_QUEUE_SIZE - 1)] = header;
  19950. + user_service->msg_insert++;
  19951. + spin_unlock(&msg_queue_spinlock);
  19952. +
  19953. + up(&user_service->insert_event);
  19954. +
  19955. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  19956. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  19957. + ** bypass the completion queue.
  19958. + */
  19959. + if (((user_service->message_available_pos -
  19960. + instance->completion_remove) >= 0) ||
  19961. + user_service->dequeue_pending) {
  19962. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19963. + user_service->dequeue_pending = 0;
  19964. + return VCHIQ_SUCCESS;
  19965. + }
  19966. +
  19967. + header = NULL;
  19968. + }
  19969. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  19970. +
  19971. + return add_completion(instance, reason, header, user_service,
  19972. + bulk_userdata);
  19973. +}
  19974. +
  19975. +/****************************************************************************
  19976. +*
  19977. +* user_service_free
  19978. +*
  19979. +***************************************************************************/
  19980. +static void
  19981. +user_service_free(void *userdata)
  19982. +{
  19983. + kfree(userdata);
  19984. +}
  19985. +
  19986. +/****************************************************************************
  19987. +*
  19988. +* vchiq_ioctl
  19989. +*
  19990. +***************************************************************************/
  19991. +
  19992. +static long
  19993. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  19994. +{
  19995. + VCHIQ_INSTANCE_T instance = file->private_data;
  19996. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  19997. + VCHIQ_SERVICE_T *service = NULL;
  19998. + long ret = 0;
  19999. + int i, rc;
  20000. + DEBUG_INITIALISE(g_state.local)
  20001. +
  20002. + vchiq_log_trace(vchiq_arm_log_level,
  20003. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  20004. + (unsigned int)instance,
  20005. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  20006. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  20007. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  20008. +
  20009. + switch (cmd) {
  20010. + case VCHIQ_IOC_SHUTDOWN:
  20011. + if (!instance->connected)
  20012. + break;
  20013. +
  20014. + /* Remove all services */
  20015. + i = 0;
  20016. + while ((service = next_service_by_instance(instance->state,
  20017. + instance, &i)) != NULL) {
  20018. + status = vchiq_remove_service(service->handle);
  20019. + unlock_service(service);
  20020. + if (status != VCHIQ_SUCCESS)
  20021. + break;
  20022. + }
  20023. + service = NULL;
  20024. +
  20025. + if (status == VCHIQ_SUCCESS) {
  20026. + /* Wake the completion thread and ask it to exit */
  20027. + instance->closing = 1;
  20028. + up(&instance->insert_event);
  20029. + }
  20030. +
  20031. + break;
  20032. +
  20033. + case VCHIQ_IOC_CONNECT:
  20034. + if (instance->connected) {
  20035. + ret = -EINVAL;
  20036. + break;
  20037. + }
  20038. + rc = mutex_lock_interruptible(&instance->state->mutex);
  20039. + if (rc != 0) {
  20040. + vchiq_log_error(vchiq_arm_log_level,
  20041. + "vchiq: connect: could not lock mutex for "
  20042. + "state %d: %d",
  20043. + instance->state->id, rc);
  20044. + ret = -EINTR;
  20045. + break;
  20046. + }
  20047. + status = vchiq_connect_internal(instance->state, instance);
  20048. + mutex_unlock(&instance->state->mutex);
  20049. +
  20050. + if (status == VCHIQ_SUCCESS)
  20051. + instance->connected = 1;
  20052. + else
  20053. + vchiq_log_error(vchiq_arm_log_level,
  20054. + "vchiq: could not connect: %d", status);
  20055. + break;
  20056. +
  20057. + case VCHIQ_IOC_CREATE_SERVICE: {
  20058. + VCHIQ_CREATE_SERVICE_T args;
  20059. + USER_SERVICE_T *user_service = NULL;
  20060. + void *userdata;
  20061. + int srvstate;
  20062. +
  20063. + if (copy_from_user
  20064. + (&args, (const void __user *)arg,
  20065. + sizeof(args)) != 0) {
  20066. + ret = -EFAULT;
  20067. + break;
  20068. + }
  20069. +
  20070. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  20071. + if (!user_service) {
  20072. + ret = -ENOMEM;
  20073. + break;
  20074. + }
  20075. +
  20076. + if (args.is_open) {
  20077. + if (!instance->connected) {
  20078. + ret = -ENOTCONN;
  20079. + kfree(user_service);
  20080. + break;
  20081. + }
  20082. + srvstate = VCHIQ_SRVSTATE_OPENING;
  20083. + } else {
  20084. + srvstate =
  20085. + instance->connected ?
  20086. + VCHIQ_SRVSTATE_LISTENING :
  20087. + VCHIQ_SRVSTATE_HIDDEN;
  20088. + }
  20089. +
  20090. + userdata = args.params.userdata;
  20091. + args.params.callback = service_callback;
  20092. + args.params.userdata = user_service;
  20093. + service = vchiq_add_service_internal(
  20094. + instance->state,
  20095. + &args.params, srvstate,
  20096. + instance, user_service_free);
  20097. +
  20098. + if (service != NULL) {
  20099. + user_service->service = service;
  20100. + user_service->userdata = userdata;
  20101. + user_service->instance = instance;
  20102. + user_service->is_vchi = args.is_vchi;
  20103. + user_service->dequeue_pending = 0;
  20104. + user_service->message_available_pos =
  20105. + instance->completion_remove - 1;
  20106. + user_service->msg_insert = 0;
  20107. + user_service->msg_remove = 0;
  20108. + sema_init(&user_service->insert_event, 0);
  20109. + sema_init(&user_service->remove_event, 0);
  20110. +
  20111. + if (args.is_open) {
  20112. + status = vchiq_open_service_internal
  20113. + (service, instance->pid);
  20114. + if (status != VCHIQ_SUCCESS) {
  20115. + vchiq_remove_service(service->handle);
  20116. + service = NULL;
  20117. + ret = (status == VCHIQ_RETRY) ?
  20118. + -EINTR : -EIO;
  20119. + break;
  20120. + }
  20121. + }
  20122. +
  20123. + if (copy_to_user((void __user *)
  20124. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  20125. + arg)->handle),
  20126. + (const void *)&service->handle,
  20127. + sizeof(service->handle)) != 0) {
  20128. + ret = -EFAULT;
  20129. + vchiq_remove_service(service->handle);
  20130. + }
  20131. +
  20132. + service = NULL;
  20133. + } else {
  20134. + ret = -EEXIST;
  20135. + kfree(user_service);
  20136. + }
  20137. + } break;
  20138. +
  20139. + case VCHIQ_IOC_CLOSE_SERVICE: {
  20140. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20141. +
  20142. + service = find_service_for_instance(instance, handle);
  20143. + if (service != NULL)
  20144. + status = vchiq_close_service(service->handle);
  20145. + else
  20146. + ret = -EINVAL;
  20147. + } break;
  20148. +
  20149. + case VCHIQ_IOC_REMOVE_SERVICE: {
  20150. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20151. +
  20152. + service = find_service_for_instance(instance, handle);
  20153. + if (service != NULL)
  20154. + status = vchiq_remove_service(service->handle);
  20155. + else
  20156. + ret = -EINVAL;
  20157. + } break;
  20158. +
  20159. + case VCHIQ_IOC_USE_SERVICE:
  20160. + case VCHIQ_IOC_RELEASE_SERVICE: {
  20161. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20162. +
  20163. + service = find_service_for_instance(instance, handle);
  20164. + if (service != NULL) {
  20165. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  20166. + vchiq_use_service_internal(service) :
  20167. + vchiq_release_service_internal(service);
  20168. + if (status != VCHIQ_SUCCESS) {
  20169. + vchiq_log_error(vchiq_susp_log_level,
  20170. + "%s: cmd %s returned error %d for "
  20171. + "service %c%c%c%c:%03d",
  20172. + __func__,
  20173. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  20174. + "VCHIQ_IOC_USE_SERVICE" :
  20175. + "VCHIQ_IOC_RELEASE_SERVICE",
  20176. + status,
  20177. + VCHIQ_FOURCC_AS_4CHARS(
  20178. + service->base.fourcc),
  20179. + service->client_id);
  20180. + ret = -EINVAL;
  20181. + }
  20182. + } else
  20183. + ret = -EINVAL;
  20184. + } break;
  20185. +
  20186. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  20187. + VCHIQ_QUEUE_MESSAGE_T args;
  20188. + if (copy_from_user
  20189. + (&args, (const void __user *)arg,
  20190. + sizeof(args)) != 0) {
  20191. + ret = -EFAULT;
  20192. + break;
  20193. + }
  20194. +
  20195. + service = find_service_for_instance(instance, args.handle);
  20196. +
  20197. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  20198. + /* Copy elements into kernel space */
  20199. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  20200. + if (copy_from_user(elements, args.elements,
  20201. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  20202. + status = vchiq_queue_message
  20203. + (args.handle,
  20204. + elements, args.count);
  20205. + else
  20206. + ret = -EFAULT;
  20207. + } else {
  20208. + ret = -EINVAL;
  20209. + }
  20210. + } break;
  20211. +
  20212. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  20213. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  20214. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  20215. + struct bulk_waiter_node *waiter = NULL;
  20216. + VCHIQ_BULK_DIR_T dir =
  20217. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  20218. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  20219. +
  20220. + if (copy_from_user
  20221. + (&args, (const void __user *)arg,
  20222. + sizeof(args)) != 0) {
  20223. + ret = -EFAULT;
  20224. + break;
  20225. + }
  20226. +
  20227. + service = find_service_for_instance(instance, args.handle);
  20228. + if (!service) {
  20229. + ret = -EINVAL;
  20230. + break;
  20231. + }
  20232. +
  20233. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  20234. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  20235. + GFP_KERNEL);
  20236. + if (!waiter) {
  20237. + ret = -ENOMEM;
  20238. + break;
  20239. + }
  20240. + args.userdata = &waiter->bulk_waiter;
  20241. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  20242. + struct list_head *pos;
  20243. + mutex_lock(&instance->bulk_waiter_list_mutex);
  20244. + list_for_each(pos, &instance->bulk_waiter_list) {
  20245. + if (list_entry(pos, struct bulk_waiter_node,
  20246. + list)->pid == current->pid) {
  20247. + waiter = list_entry(pos,
  20248. + struct bulk_waiter_node,
  20249. + list);
  20250. + list_del(pos);
  20251. + break;
  20252. + }
  20253. +
  20254. + }
  20255. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  20256. + if (!waiter) {
  20257. + vchiq_log_error(vchiq_arm_log_level,
  20258. + "no bulk_waiter found for pid %d",
  20259. + current->pid);
  20260. + ret = -ESRCH;
  20261. + break;
  20262. + }
  20263. + vchiq_log_info(vchiq_arm_log_level,
  20264. + "found bulk_waiter %x for pid %d",
  20265. + (unsigned int)waiter, current->pid);
  20266. + args.userdata = &waiter->bulk_waiter;
  20267. + }
  20268. + status = vchiq_bulk_transfer
  20269. + (args.handle,
  20270. + VCHI_MEM_HANDLE_INVALID,
  20271. + args.data, args.size,
  20272. + args.userdata, args.mode,
  20273. + dir);
  20274. + if (!waiter)
  20275. + break;
  20276. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  20277. + !waiter->bulk_waiter.bulk) {
  20278. + if (waiter->bulk_waiter.bulk) {
  20279. + /* Cancel the signal when the transfer
  20280. + ** completes. */
  20281. + spin_lock(&bulk_waiter_spinlock);
  20282. + waiter->bulk_waiter.bulk->userdata = NULL;
  20283. + spin_unlock(&bulk_waiter_spinlock);
  20284. + }
  20285. + kfree(waiter);
  20286. + } else {
  20287. + const VCHIQ_BULK_MODE_T mode_waiting =
  20288. + VCHIQ_BULK_MODE_WAITING;
  20289. + waiter->pid = current->pid;
  20290. + mutex_lock(&instance->bulk_waiter_list_mutex);
  20291. + list_add(&waiter->list, &instance->bulk_waiter_list);
  20292. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  20293. + vchiq_log_info(vchiq_arm_log_level,
  20294. + "saved bulk_waiter %x for pid %d",
  20295. + (unsigned int)waiter, current->pid);
  20296. +
  20297. + if (copy_to_user((void __user *)
  20298. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  20299. + arg)->mode),
  20300. + (const void *)&mode_waiting,
  20301. + sizeof(mode_waiting)) != 0)
  20302. + ret = -EFAULT;
  20303. + }
  20304. + } break;
  20305. +
  20306. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  20307. + VCHIQ_AWAIT_COMPLETION_T args;
  20308. +
  20309. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20310. + if (!instance->connected) {
  20311. + ret = -ENOTCONN;
  20312. + break;
  20313. + }
  20314. +
  20315. + if (copy_from_user(&args, (const void __user *)arg,
  20316. + sizeof(args)) != 0) {
  20317. + ret = -EFAULT;
  20318. + break;
  20319. + }
  20320. +
  20321. + mutex_lock(&instance->completion_mutex);
  20322. +
  20323. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20324. + while ((instance->completion_remove ==
  20325. + instance->completion_insert)
  20326. + && !instance->closing) {
  20327. + int rc;
  20328. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20329. + mutex_unlock(&instance->completion_mutex);
  20330. + rc = down_interruptible(&instance->insert_event);
  20331. + mutex_lock(&instance->completion_mutex);
  20332. + if (rc != 0) {
  20333. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20334. + vchiq_log_info(vchiq_arm_log_level,
  20335. + "AWAIT_COMPLETION interrupted");
  20336. + ret = -EINTR;
  20337. + break;
  20338. + }
  20339. + }
  20340. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20341. +
  20342. + /* A read memory barrier is needed to stop prefetch of a stale
  20343. + ** completion record
  20344. + */
  20345. + rmb();
  20346. +
  20347. + if (ret == 0) {
  20348. + int msgbufcount = args.msgbufcount;
  20349. + for (ret = 0; ret < args.count; ret++) {
  20350. + VCHIQ_COMPLETION_DATA_T *completion;
  20351. + VCHIQ_SERVICE_T *service;
  20352. + USER_SERVICE_T *user_service;
  20353. + VCHIQ_HEADER_T *header;
  20354. + if (instance->completion_remove ==
  20355. + instance->completion_insert)
  20356. + break;
  20357. + completion = &instance->completions[
  20358. + instance->completion_remove &
  20359. + (MAX_COMPLETIONS - 1)];
  20360. +
  20361. + service = completion->service_userdata;
  20362. + user_service = service->base.userdata;
  20363. + completion->service_userdata =
  20364. + user_service->userdata;
  20365. +
  20366. + header = completion->header;
  20367. + if (header) {
  20368. + void __user *msgbuf;
  20369. + int msglen;
  20370. +
  20371. + msglen = header->size +
  20372. + sizeof(VCHIQ_HEADER_T);
  20373. + /* This must be a VCHIQ-style service */
  20374. + if (args.msgbufsize < msglen) {
  20375. + vchiq_log_error(
  20376. + vchiq_arm_log_level,
  20377. + "header %x: msgbufsize"
  20378. + " %x < msglen %x",
  20379. + (unsigned int)header,
  20380. + args.msgbufsize,
  20381. + msglen);
  20382. + WARN(1, "invalid message "
  20383. + "size\n");
  20384. + if (ret == 0)
  20385. + ret = -EMSGSIZE;
  20386. + break;
  20387. + }
  20388. + if (msgbufcount <= 0)
  20389. + /* Stall here for lack of a
  20390. + ** buffer for the message. */
  20391. + break;
  20392. + /* Get the pointer from user space */
  20393. + msgbufcount--;
  20394. + if (copy_from_user(&msgbuf,
  20395. + (const void __user *)
  20396. + &args.msgbufs[msgbufcount],
  20397. + sizeof(msgbuf)) != 0) {
  20398. + if (ret == 0)
  20399. + ret = -EFAULT;
  20400. + break;
  20401. + }
  20402. +
  20403. + /* Copy the message to user space */
  20404. + if (copy_to_user(msgbuf, header,
  20405. + msglen) != 0) {
  20406. + if (ret == 0)
  20407. + ret = -EFAULT;
  20408. + break;
  20409. + }
  20410. +
  20411. + /* Now it has been copied, the message
  20412. + ** can be released. */
  20413. + vchiq_release_message(service->handle,
  20414. + header);
  20415. +
  20416. + /* The completion must point to the
  20417. + ** msgbuf. */
  20418. + completion->header = msgbuf;
  20419. + }
  20420. +
  20421. + if (completion->reason ==
  20422. + VCHIQ_SERVICE_CLOSED)
  20423. + unlock_service(service);
  20424. +
  20425. + if (copy_to_user((void __user *)(
  20426. + (size_t)args.buf +
  20427. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  20428. + completion,
  20429. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  20430. + if (ret == 0)
  20431. + ret = -EFAULT;
  20432. + break;
  20433. + }
  20434. +
  20435. + instance->completion_remove++;
  20436. + }
  20437. +
  20438. + if (msgbufcount != args.msgbufcount) {
  20439. + if (copy_to_user((void __user *)
  20440. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  20441. + msgbufcount,
  20442. + &msgbufcount,
  20443. + sizeof(msgbufcount)) != 0) {
  20444. + ret = -EFAULT;
  20445. + }
  20446. + }
  20447. + }
  20448. +
  20449. + if (ret != 0)
  20450. + up(&instance->remove_event);
  20451. + mutex_unlock(&instance->completion_mutex);
  20452. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  20453. + } break;
  20454. +
  20455. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  20456. + VCHIQ_DEQUEUE_MESSAGE_T args;
  20457. + USER_SERVICE_T *user_service;
  20458. + VCHIQ_HEADER_T *header;
  20459. +
  20460. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  20461. + if (copy_from_user
  20462. + (&args, (const void __user *)arg,
  20463. + sizeof(args)) != 0) {
  20464. + ret = -EFAULT;
  20465. + break;
  20466. + }
  20467. + service = find_service_for_instance(instance, args.handle);
  20468. + if (!service) {
  20469. + ret = -EINVAL;
  20470. + break;
  20471. + }
  20472. + user_service = (USER_SERVICE_T *)service->base.userdata;
  20473. + if (user_service->is_vchi == 0) {
  20474. + ret = -EINVAL;
  20475. + break;
  20476. + }
  20477. +
  20478. + spin_lock(&msg_queue_spinlock);
  20479. + if (user_service->msg_remove == user_service->msg_insert) {
  20480. + if (!args.blocking) {
  20481. + spin_unlock(&msg_queue_spinlock);
  20482. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  20483. + ret = -EWOULDBLOCK;
  20484. + break;
  20485. + }
  20486. + user_service->dequeue_pending = 1;
  20487. + do {
  20488. + spin_unlock(&msg_queue_spinlock);
  20489. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  20490. + if (down_interruptible(
  20491. + &user_service->insert_event) != 0) {
  20492. + vchiq_log_info(vchiq_arm_log_level,
  20493. + "DEQUEUE_MESSAGE interrupted");
  20494. + ret = -EINTR;
  20495. + break;
  20496. + }
  20497. + spin_lock(&msg_queue_spinlock);
  20498. + } while (user_service->msg_remove ==
  20499. + user_service->msg_insert);
  20500. +
  20501. + if (ret)
  20502. + break;
  20503. + }
  20504. +
  20505. + BUG_ON((int)(user_service->msg_insert -
  20506. + user_service->msg_remove) < 0);
  20507. +
  20508. + header = user_service->msg_queue[user_service->msg_remove &
  20509. + (MSG_QUEUE_SIZE - 1)];
  20510. + user_service->msg_remove++;
  20511. + spin_unlock(&msg_queue_spinlock);
  20512. +
  20513. + up(&user_service->remove_event);
  20514. + if (header == NULL)
  20515. + ret = -ENOTCONN;
  20516. + else if (header->size <= args.bufsize) {
  20517. + /* Copy to user space if msgbuf is not NULL */
  20518. + if ((args.buf == NULL) ||
  20519. + (copy_to_user((void __user *)args.buf,
  20520. + header->data,
  20521. + header->size) == 0)) {
  20522. + ret = header->size;
  20523. + vchiq_release_message(
  20524. + service->handle,
  20525. + header);
  20526. + } else
  20527. + ret = -EFAULT;
  20528. + } else {
  20529. + vchiq_log_error(vchiq_arm_log_level,
  20530. + "header %x: bufsize %x < size %x",
  20531. + (unsigned int)header, args.bufsize,
  20532. + header->size);
  20533. + WARN(1, "invalid size\n");
  20534. + ret = -EMSGSIZE;
  20535. + }
  20536. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  20537. + } break;
  20538. +
  20539. + case VCHIQ_IOC_GET_CLIENT_ID: {
  20540. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  20541. +
  20542. + ret = vchiq_get_client_id(handle);
  20543. + } break;
  20544. +
  20545. + case VCHIQ_IOC_GET_CONFIG: {
  20546. + VCHIQ_GET_CONFIG_T args;
  20547. + VCHIQ_CONFIG_T config;
  20548. +
  20549. + if (copy_from_user(&args, (const void __user *)arg,
  20550. + sizeof(args)) != 0) {
  20551. + ret = -EFAULT;
  20552. + break;
  20553. + }
  20554. + if (args.config_size > sizeof(config)) {
  20555. + ret = -EINVAL;
  20556. + break;
  20557. + }
  20558. + status = vchiq_get_config(instance, args.config_size, &config);
  20559. + if (status == VCHIQ_SUCCESS) {
  20560. + if (copy_to_user((void __user *)args.pconfig,
  20561. + &config, args.config_size) != 0) {
  20562. + ret = -EFAULT;
  20563. + break;
  20564. + }
  20565. + }
  20566. + } break;
  20567. +
  20568. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  20569. + VCHIQ_SET_SERVICE_OPTION_T args;
  20570. +
  20571. + if (copy_from_user(
  20572. + &args, (const void __user *)arg,
  20573. + sizeof(args)) != 0) {
  20574. + ret = -EFAULT;
  20575. + break;
  20576. + }
  20577. +
  20578. + service = find_service_for_instance(instance, args.handle);
  20579. + if (!service) {
  20580. + ret = -EINVAL;
  20581. + break;
  20582. + }
  20583. +
  20584. + status = vchiq_set_service_option(
  20585. + args.handle, args.option, args.value);
  20586. + } break;
  20587. +
  20588. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  20589. + VCHIQ_DUMP_MEM_T args;
  20590. +
  20591. + if (copy_from_user
  20592. + (&args, (const void __user *)arg,
  20593. + sizeof(args)) != 0) {
  20594. + ret = -EFAULT;
  20595. + break;
  20596. + }
  20597. + dump_phys_mem(args.virt_addr, args.num_bytes);
  20598. + } break;
  20599. +
  20600. + default:
  20601. + ret = -ENOTTY;
  20602. + break;
  20603. + }
  20604. +
  20605. + if (service)
  20606. + unlock_service(service);
  20607. +
  20608. + if (ret == 0) {
  20609. + if (status == VCHIQ_ERROR)
  20610. + ret = -EIO;
  20611. + else if (status == VCHIQ_RETRY)
  20612. + ret = -EINTR;
  20613. + }
  20614. +
  20615. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  20616. + (ret != -EWOULDBLOCK))
  20617. + vchiq_log_info(vchiq_arm_log_level,
  20618. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  20619. + (unsigned long)instance,
  20620. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  20621. + ioctl_names[_IOC_NR(cmd)] :
  20622. + "<invalid>",
  20623. + status, ret);
  20624. + else
  20625. + vchiq_log_trace(vchiq_arm_log_level,
  20626. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  20627. + (unsigned long)instance,
  20628. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  20629. + ioctl_names[_IOC_NR(cmd)] :
  20630. + "<invalid>",
  20631. + status, ret);
  20632. +
  20633. + return ret;
  20634. +}
  20635. +
  20636. +/****************************************************************************
  20637. +*
  20638. +* vchiq_open
  20639. +*
  20640. +***************************************************************************/
  20641. +
  20642. +static int
  20643. +vchiq_open(struct inode *inode, struct file *file)
  20644. +{
  20645. + int dev = iminor(inode) & 0x0f;
  20646. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  20647. + switch (dev) {
  20648. + case VCHIQ_MINOR: {
  20649. + int ret;
  20650. + VCHIQ_STATE_T *state = vchiq_get_state();
  20651. + VCHIQ_INSTANCE_T instance;
  20652. +
  20653. + if (!state) {
  20654. + vchiq_log_error(vchiq_arm_log_level,
  20655. + "vchiq has no connection to VideoCore");
  20656. + return -ENOTCONN;
  20657. + }
  20658. +
  20659. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  20660. + if (!instance)
  20661. + return -ENOMEM;
  20662. +
  20663. + instance->state = state;
  20664. + instance->pid = current->tgid;
  20665. +
  20666. + ret = vchiq_proc_add_instance(instance);
  20667. + if (ret != 0) {
  20668. + kfree(instance);
  20669. + return ret;
  20670. + }
  20671. +
  20672. + sema_init(&instance->insert_event, 0);
  20673. + sema_init(&instance->remove_event, 0);
  20674. + mutex_init(&instance->completion_mutex);
  20675. + mutex_init(&instance->bulk_waiter_list_mutex);
  20676. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  20677. +
  20678. + file->private_data = instance;
  20679. + } break;
  20680. +
  20681. + default:
  20682. + vchiq_log_error(vchiq_arm_log_level,
  20683. + "Unknown minor device: %d", dev);
  20684. + return -ENXIO;
  20685. + }
  20686. +
  20687. + return 0;
  20688. +}
  20689. +
  20690. +/****************************************************************************
  20691. +*
  20692. +* vchiq_release
  20693. +*
  20694. +***************************************************************************/
  20695. +
  20696. +static int
  20697. +vchiq_release(struct inode *inode, struct file *file)
  20698. +{
  20699. + int dev = iminor(inode) & 0x0f;
  20700. + int ret = 0;
  20701. + switch (dev) {
  20702. + case VCHIQ_MINOR: {
  20703. + VCHIQ_INSTANCE_T instance = file->private_data;
  20704. + VCHIQ_STATE_T *state = vchiq_get_state();
  20705. + VCHIQ_SERVICE_T *service;
  20706. + int i;
  20707. +
  20708. + vchiq_log_info(vchiq_arm_log_level,
  20709. + "vchiq_release: instance=%lx",
  20710. + (unsigned long)instance);
  20711. +
  20712. + if (!state) {
  20713. + ret = -EPERM;
  20714. + goto out;
  20715. + }
  20716. +
  20717. + /* Ensure videocore is awake to allow termination. */
  20718. + vchiq_use_internal(instance->state, NULL,
  20719. + USE_TYPE_VCHIQ);
  20720. +
  20721. + mutex_lock(&instance->completion_mutex);
  20722. +
  20723. + /* Wake the completion thread and ask it to exit */
  20724. + instance->closing = 1;
  20725. + up(&instance->insert_event);
  20726. +
  20727. + mutex_unlock(&instance->completion_mutex);
  20728. +
  20729. + /* Wake the slot handler if the completion queue is full. */
  20730. + up(&instance->remove_event);
  20731. +
  20732. + /* Mark all services for termination... */
  20733. + i = 0;
  20734. + while ((service = next_service_by_instance(state, instance,
  20735. + &i)) != NULL) {
  20736. + USER_SERVICE_T *user_service = service->base.userdata;
  20737. +
  20738. + /* Wake the slot handler if the msg queue is full. */
  20739. + up(&user_service->remove_event);
  20740. +
  20741. + vchiq_terminate_service_internal(service);
  20742. + unlock_service(service);
  20743. + }
  20744. +
  20745. + /* ...and wait for them to die */
  20746. + i = 0;
  20747. + while ((service = next_service_by_instance(state, instance, &i))
  20748. + != NULL) {
  20749. + USER_SERVICE_T *user_service = service->base.userdata;
  20750. +
  20751. + down(&service->remove_event);
  20752. +
  20753. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  20754. +
  20755. + spin_lock(&msg_queue_spinlock);
  20756. +
  20757. + while (user_service->msg_remove !=
  20758. + user_service->msg_insert) {
  20759. + VCHIQ_HEADER_T *header = user_service->
  20760. + msg_queue[user_service->msg_remove &
  20761. + (MSG_QUEUE_SIZE - 1)];
  20762. + user_service->msg_remove++;
  20763. + spin_unlock(&msg_queue_spinlock);
  20764. +
  20765. + if (header)
  20766. + vchiq_release_message(
  20767. + service->handle,
  20768. + header);
  20769. + spin_lock(&msg_queue_spinlock);
  20770. + }
  20771. +
  20772. + spin_unlock(&msg_queue_spinlock);
  20773. +
  20774. + unlock_service(service);
  20775. + }
  20776. +
  20777. + /* Release any closed services */
  20778. + while (instance->completion_remove !=
  20779. + instance->completion_insert) {
  20780. + VCHIQ_COMPLETION_DATA_T *completion;
  20781. + VCHIQ_SERVICE_T *service;
  20782. + completion = &instance->completions[
  20783. + instance->completion_remove &
  20784. + (MAX_COMPLETIONS - 1)];
  20785. + service = completion->service_userdata;
  20786. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  20787. + unlock_service(service);
  20788. + instance->completion_remove++;
  20789. + }
  20790. +
  20791. + /* Release the PEER service count. */
  20792. + vchiq_release_internal(instance->state, NULL);
  20793. +
  20794. + {
  20795. + struct list_head *pos, *next;
  20796. + list_for_each_safe(pos, next,
  20797. + &instance->bulk_waiter_list) {
  20798. + struct bulk_waiter_node *waiter;
  20799. + waiter = list_entry(pos,
  20800. + struct bulk_waiter_node,
  20801. + list);
  20802. + list_del(pos);
  20803. + vchiq_log_info(vchiq_arm_log_level,
  20804. + "bulk_waiter - cleaned up %x "
  20805. + "for pid %d",
  20806. + (unsigned int)waiter, waiter->pid);
  20807. + kfree(waiter);
  20808. + }
  20809. + }
  20810. +
  20811. + vchiq_proc_remove_instance(instance);
  20812. +
  20813. + kfree(instance);
  20814. + file->private_data = NULL;
  20815. + } break;
  20816. +
  20817. + default:
  20818. + vchiq_log_error(vchiq_arm_log_level,
  20819. + "Unknown minor device: %d", dev);
  20820. + ret = -ENXIO;
  20821. + }
  20822. +
  20823. +out:
  20824. + return ret;
  20825. +}
  20826. +
  20827. +/****************************************************************************
  20828. +*
  20829. +* vchiq_dump
  20830. +*
  20831. +***************************************************************************/
  20832. +
  20833. +void
  20834. +vchiq_dump(void *dump_context, const char *str, int len)
  20835. +{
  20836. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  20837. +
  20838. + if (context->actual < context->space) {
  20839. + int copy_bytes;
  20840. + if (context->offset > 0) {
  20841. + int skip_bytes = min(len, (int)context->offset);
  20842. + str += skip_bytes;
  20843. + len -= skip_bytes;
  20844. + context->offset -= skip_bytes;
  20845. + if (context->offset > 0)
  20846. + return;
  20847. + }
  20848. + copy_bytes = min(len, (int)(context->space - context->actual));
  20849. + if (copy_bytes == 0)
  20850. + return;
  20851. + if (copy_to_user(context->buf + context->actual, str,
  20852. + copy_bytes))
  20853. + context->actual = -EFAULT;
  20854. + context->actual += copy_bytes;
  20855. + len -= copy_bytes;
  20856. +
  20857. + /* If tne terminating NUL is included in the length, then it
  20858. + ** marks the end of a line and should be replaced with a
  20859. + ** carriage return. */
  20860. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  20861. + char cr = '\n';
  20862. + if (copy_to_user(context->buf + context->actual - 1,
  20863. + &cr, 1))
  20864. + context->actual = -EFAULT;
  20865. + }
  20866. + }
  20867. +}
  20868. +
  20869. +/****************************************************************************
  20870. +*
  20871. +* vchiq_dump_platform_instance_state
  20872. +*
  20873. +***************************************************************************/
  20874. +
  20875. +void
  20876. +vchiq_dump_platform_instances(void *dump_context)
  20877. +{
  20878. + VCHIQ_STATE_T *state = vchiq_get_state();
  20879. + char buf[80];
  20880. + int len;
  20881. + int i;
  20882. +
  20883. + /* There is no list of instances, so instead scan all services,
  20884. + marking those that have been dumped. */
  20885. +
  20886. + for (i = 0; i < state->unused_service; i++) {
  20887. + VCHIQ_SERVICE_T *service = state->services[i];
  20888. + VCHIQ_INSTANCE_T instance;
  20889. +
  20890. + if (service && (service->base.callback == service_callback)) {
  20891. + instance = service->instance;
  20892. + if (instance)
  20893. + instance->mark = 0;
  20894. + }
  20895. + }
  20896. +
  20897. + for (i = 0; i < state->unused_service; i++) {
  20898. + VCHIQ_SERVICE_T *service = state->services[i];
  20899. + VCHIQ_INSTANCE_T instance;
  20900. +
  20901. + if (service && (service->base.callback == service_callback)) {
  20902. + instance = service->instance;
  20903. + if (instance && !instance->mark) {
  20904. + len = snprintf(buf, sizeof(buf),
  20905. + "Instance %x: pid %d,%s completions "
  20906. + "%d/%d",
  20907. + (unsigned int)instance, instance->pid,
  20908. + instance->connected ? " connected, " :
  20909. + "",
  20910. + instance->completion_insert -
  20911. + instance->completion_remove,
  20912. + MAX_COMPLETIONS);
  20913. +
  20914. + vchiq_dump(dump_context, buf, len + 1);
  20915. +
  20916. + instance->mark = 1;
  20917. + }
  20918. + }
  20919. + }
  20920. +}
  20921. +
  20922. +/****************************************************************************
  20923. +*
  20924. +* vchiq_dump_platform_service_state
  20925. +*
  20926. +***************************************************************************/
  20927. +
  20928. +void
  20929. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  20930. +{
  20931. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  20932. + char buf[80];
  20933. + int len;
  20934. +
  20935. + len = snprintf(buf, sizeof(buf), " instance %x",
  20936. + (unsigned int)service->instance);
  20937. +
  20938. + if ((service->base.callback == service_callback) &&
  20939. + user_service->is_vchi) {
  20940. + len += snprintf(buf + len, sizeof(buf) - len,
  20941. + ", %d/%d messages",
  20942. + user_service->msg_insert - user_service->msg_remove,
  20943. + MSG_QUEUE_SIZE);
  20944. +
  20945. + if (user_service->dequeue_pending)
  20946. + len += snprintf(buf + len, sizeof(buf) - len,
  20947. + " (dequeue pending)");
  20948. + }
  20949. +
  20950. + vchiq_dump(dump_context, buf, len + 1);
  20951. +}
  20952. +
  20953. +/****************************************************************************
  20954. +*
  20955. +* dump_user_mem
  20956. +*
  20957. +***************************************************************************/
  20958. +
  20959. +static void
  20960. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  20961. +{
  20962. + int rc;
  20963. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  20964. + int num_pages;
  20965. + int offset;
  20966. + int end_offset;
  20967. + int page_idx;
  20968. + int prev_idx;
  20969. + struct page *page;
  20970. + struct page **pages;
  20971. + uint8_t *kmapped_virt_ptr;
  20972. +
  20973. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  20974. +
  20975. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  20976. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  20977. + ~0x0fuL);
  20978. +
  20979. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  20980. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  20981. +
  20982. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  20983. +
  20984. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  20985. + if (pages == NULL) {
  20986. + vchiq_log_error(vchiq_arm_log_level,
  20987. + "Unable to allocation memory for %d pages\n",
  20988. + num_pages);
  20989. + return;
  20990. + }
  20991. +
  20992. + down_read(&current->mm->mmap_sem);
  20993. + rc = get_user_pages(current, /* task */
  20994. + current->mm, /* mm */
  20995. + (unsigned long)virt_addr, /* start */
  20996. + num_pages, /* len */
  20997. + 0, /* write */
  20998. + 0, /* force */
  20999. + pages, /* pages (array of page pointers) */
  21000. + NULL); /* vmas */
  21001. + up_read(&current->mm->mmap_sem);
  21002. +
  21003. + prev_idx = -1;
  21004. + page = NULL;
  21005. +
  21006. + while (offset < end_offset) {
  21007. +
  21008. + int page_offset = offset % PAGE_SIZE;
  21009. + page_idx = offset / PAGE_SIZE;
  21010. +
  21011. + if (page_idx != prev_idx) {
  21012. +
  21013. + if (page != NULL)
  21014. + kunmap(page);
  21015. + page = pages[page_idx];
  21016. + kmapped_virt_ptr = kmap(page);
  21017. +
  21018. + prev_idx = page_idx;
  21019. + }
  21020. +
  21021. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  21022. + vchiq_log_dump_mem("ph",
  21023. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  21024. + page_offset],
  21025. + &kmapped_virt_ptr[page_offset], 16);
  21026. +
  21027. + offset += 16;
  21028. + }
  21029. + if (page != NULL)
  21030. + kunmap(page);
  21031. +
  21032. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  21033. + page_cache_release(pages[page_idx]);
  21034. +
  21035. + kfree(pages);
  21036. +}
  21037. +
  21038. +/****************************************************************************
  21039. +*
  21040. +* vchiq_read
  21041. +*
  21042. +***************************************************************************/
  21043. +
  21044. +static ssize_t
  21045. +vchiq_read(struct file *file, char __user *buf,
  21046. + size_t count, loff_t *ppos)
  21047. +{
  21048. + DUMP_CONTEXT_T context;
  21049. + context.buf = buf;
  21050. + context.actual = 0;
  21051. + context.space = count;
  21052. + context.offset = *ppos;
  21053. +
  21054. + vchiq_dump_state(&context, &g_state);
  21055. +
  21056. + *ppos += context.actual;
  21057. +
  21058. + return context.actual;
  21059. +}
  21060. +
  21061. +VCHIQ_STATE_T *
  21062. +vchiq_get_state(void)
  21063. +{
  21064. +
  21065. + if (g_state.remote == NULL)
  21066. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  21067. + else if (g_state.remote->initialised != 1)
  21068. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  21069. + __func__, g_state.remote->initialised);
  21070. +
  21071. + return ((g_state.remote != NULL) &&
  21072. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  21073. +}
  21074. +
  21075. +static const struct file_operations
  21076. +vchiq_fops = {
  21077. + .owner = THIS_MODULE,
  21078. + .unlocked_ioctl = vchiq_ioctl,
  21079. + .open = vchiq_open,
  21080. + .release = vchiq_release,
  21081. + .read = vchiq_read
  21082. +};
  21083. +
  21084. +/*
  21085. + * Autosuspend related functionality
  21086. + */
  21087. +
  21088. +int
  21089. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  21090. +{
  21091. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21092. + if (!arm_state)
  21093. + /* autosuspend not supported - always return wanted */
  21094. + return 1;
  21095. + else if (arm_state->blocked_count)
  21096. + return 1;
  21097. + else if (!arm_state->videocore_use_count)
  21098. + /* usage count zero - check for override unless we're forcing */
  21099. + if (arm_state->resume_blocked)
  21100. + return 0;
  21101. + else
  21102. + return vchiq_platform_videocore_wanted(state);
  21103. + else
  21104. + /* non-zero usage count - videocore still required */
  21105. + return 1;
  21106. +}
  21107. +
  21108. +static VCHIQ_STATUS_T
  21109. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  21110. + VCHIQ_HEADER_T *header,
  21111. + VCHIQ_SERVICE_HANDLE_T service_user,
  21112. + void *bulk_user)
  21113. +{
  21114. + vchiq_log_error(vchiq_susp_log_level,
  21115. + "%s callback reason %d", __func__, reason);
  21116. + return 0;
  21117. +}
  21118. +
  21119. +static int
  21120. +vchiq_keepalive_thread_func(void *v)
  21121. +{
  21122. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  21123. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21124. +
  21125. + VCHIQ_STATUS_T status;
  21126. + VCHIQ_INSTANCE_T instance;
  21127. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  21128. +
  21129. + VCHIQ_SERVICE_PARAMS_T params = {
  21130. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  21131. + .callback = vchiq_keepalive_vchiq_callback,
  21132. + .version = KEEPALIVE_VER,
  21133. + .version_min = KEEPALIVE_VER_MIN
  21134. + };
  21135. +
  21136. + status = vchiq_initialise(&instance);
  21137. + if (status != VCHIQ_SUCCESS) {
  21138. + vchiq_log_error(vchiq_susp_log_level,
  21139. + "%s vchiq_initialise failed %d", __func__, status);
  21140. + goto exit;
  21141. + }
  21142. +
  21143. + status = vchiq_connect(instance);
  21144. + if (status != VCHIQ_SUCCESS) {
  21145. + vchiq_log_error(vchiq_susp_log_level,
  21146. + "%s vchiq_connect failed %d", __func__, status);
  21147. + goto shutdown;
  21148. + }
  21149. +
  21150. + status = vchiq_add_service(instance, &params, &ka_handle);
  21151. + if (status != VCHIQ_SUCCESS) {
  21152. + vchiq_log_error(vchiq_susp_log_level,
  21153. + "%s vchiq_open_service failed %d", __func__, status);
  21154. + goto shutdown;
  21155. + }
  21156. +
  21157. + while (1) {
  21158. + long rc = 0, uc = 0;
  21159. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  21160. + != 0) {
  21161. + vchiq_log_error(vchiq_susp_log_level,
  21162. + "%s interrupted", __func__);
  21163. + flush_signals(current);
  21164. + continue;
  21165. + }
  21166. +
  21167. + /* read and clear counters. Do release_count then use_count to
  21168. + * prevent getting more releases than uses */
  21169. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  21170. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  21171. +
  21172. + /* Call use/release service the requisite number of times.
  21173. + * Process use before release so use counts don't go negative */
  21174. + while (uc--) {
  21175. + atomic_inc(&arm_state->ka_use_ack_count);
  21176. + status = vchiq_use_service(ka_handle);
  21177. + if (status != VCHIQ_SUCCESS) {
  21178. + vchiq_log_error(vchiq_susp_log_level,
  21179. + "%s vchiq_use_service error %d",
  21180. + __func__, status);
  21181. + }
  21182. + }
  21183. + while (rc--) {
  21184. + status = vchiq_release_service(ka_handle);
  21185. + if (status != VCHIQ_SUCCESS) {
  21186. + vchiq_log_error(vchiq_susp_log_level,
  21187. + "%s vchiq_release_service error %d",
  21188. + __func__, status);
  21189. + }
  21190. + }
  21191. + }
  21192. +
  21193. +shutdown:
  21194. + vchiq_shutdown(instance);
  21195. +exit:
  21196. + return 0;
  21197. +}
  21198. +
  21199. +
  21200. +
  21201. +VCHIQ_STATUS_T
  21202. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  21203. +{
  21204. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  21205. +
  21206. + if (arm_state) {
  21207. + rwlock_init(&arm_state->susp_res_lock);
  21208. +
  21209. + init_completion(&arm_state->ka_evt);
  21210. + atomic_set(&arm_state->ka_use_count, 0);
  21211. + atomic_set(&arm_state->ka_use_ack_count, 0);
  21212. + atomic_set(&arm_state->ka_release_count, 0);
  21213. +
  21214. + init_completion(&arm_state->vc_suspend_complete);
  21215. +
  21216. + init_completion(&arm_state->vc_resume_complete);
  21217. + /* Initialise to 'done' state. We only want to block on resume
  21218. + * completion while videocore is suspended. */
  21219. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  21220. +
  21221. + init_completion(&arm_state->resume_blocker);
  21222. + /* Initialise to 'done' state. We only want to block on this
  21223. + * completion while resume is blocked */
  21224. + complete_all(&arm_state->resume_blocker);
  21225. +
  21226. + init_completion(&arm_state->blocked_blocker);
  21227. + /* Initialise to 'done' state. We only want to block on this
  21228. + * completion while things are waiting on the resume blocker */
  21229. + complete_all(&arm_state->blocked_blocker);
  21230. +
  21231. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  21232. + arm_state->suspend_timer_running = 0;
  21233. + init_timer(&arm_state->suspend_timer);
  21234. + arm_state->suspend_timer.data = (unsigned long)(state);
  21235. + arm_state->suspend_timer.function = suspend_timer_callback;
  21236. +
  21237. + arm_state->first_connect = 0;
  21238. +
  21239. + }
  21240. + return status;
  21241. +}
  21242. +
  21243. +/*
  21244. +** Functions to modify the state variables;
  21245. +** set_suspend_state
  21246. +** set_resume_state
  21247. +**
  21248. +** There are more state variables than we might like, so ensure they remain in
  21249. +** step. Suspend and resume state are maintained separately, since most of
  21250. +** these state machines can operate independently. However, there are a few
  21251. +** states where state transitions in one state machine cause a reset to the
  21252. +** other state machine. In addition, there are some completion events which
  21253. +** need to occur on state machine reset and end-state(s), so these are also
  21254. +** dealt with in these functions.
  21255. +**
  21256. +** In all states we set the state variable according to the input, but in some
  21257. +** cases we perform additional steps outlined below;
  21258. +**
  21259. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  21260. +** The suspend completion is completed after any suspend
  21261. +** attempt. When we reset the state machine we also reset
  21262. +** the completion. This reset occurs when videocore is
  21263. +** resumed, and also if we initiate suspend after a suspend
  21264. +** failure.
  21265. +**
  21266. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  21267. +** suspend - ie from this point on we must try to suspend
  21268. +** before resuming can occur. We therefore also reset the
  21269. +** resume state machine to VC_RESUME_IDLE in this state.
  21270. +**
  21271. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  21272. +** complete_all on the suspend completion to notify
  21273. +** anything waiting for suspend to happen.
  21274. +**
  21275. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  21276. +** initiate resume, so no need to alter resume state.
  21277. +** We call complete_all on the suspend completion to notify
  21278. +** of suspend rejection.
  21279. +**
  21280. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  21281. +** suspend completion and reset the resume state machine.
  21282. +**
  21283. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  21284. +** resume completion is in it's 'done' state whenever
  21285. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  21286. +** implies that videocore is suspended.
  21287. +** Hence, any thread which needs to wait until videocore is
  21288. +** running can wait on this completion - it will only block
  21289. +** if videocore is suspended.
  21290. +**
  21291. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  21292. +** Call complete_all on the resume completion to unblock
  21293. +** any threads waiting for resume. Also reset the suspend
  21294. +** state machine to it's idle state.
  21295. +**
  21296. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  21297. +*/
  21298. +
  21299. +inline void
  21300. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  21301. + enum vc_suspend_status new_state)
  21302. +{
  21303. + /* set the state in all cases */
  21304. + arm_state->vc_suspend_state = new_state;
  21305. +
  21306. + /* state specific additional actions */
  21307. + switch (new_state) {
  21308. + case VC_SUSPEND_FORCE_CANCELED:
  21309. + complete_all(&arm_state->vc_suspend_complete);
  21310. + break;
  21311. + case VC_SUSPEND_REJECTED:
  21312. + complete_all(&arm_state->vc_suspend_complete);
  21313. + break;
  21314. + case VC_SUSPEND_FAILED:
  21315. + complete_all(&arm_state->vc_suspend_complete);
  21316. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  21317. + complete_all(&arm_state->vc_resume_complete);
  21318. + break;
  21319. + case VC_SUSPEND_IDLE:
  21320. + reinit_completion(&arm_state->vc_suspend_complete);
  21321. + break;
  21322. + case VC_SUSPEND_REQUESTED:
  21323. + break;
  21324. + case VC_SUSPEND_IN_PROGRESS:
  21325. + set_resume_state(arm_state, VC_RESUME_IDLE);
  21326. + break;
  21327. + case VC_SUSPEND_SUSPENDED:
  21328. + complete_all(&arm_state->vc_suspend_complete);
  21329. + break;
  21330. + default:
  21331. + BUG();
  21332. + break;
  21333. + }
  21334. +}
  21335. +
  21336. +inline void
  21337. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  21338. + enum vc_resume_status new_state)
  21339. +{
  21340. + /* set the state in all cases */
  21341. + arm_state->vc_resume_state = new_state;
  21342. +
  21343. + /* state specific additional actions */
  21344. + switch (new_state) {
  21345. + case VC_RESUME_FAILED:
  21346. + break;
  21347. + case VC_RESUME_IDLE:
  21348. + reinit_completion(&arm_state->vc_resume_complete);
  21349. + break;
  21350. + case VC_RESUME_REQUESTED:
  21351. + break;
  21352. + case VC_RESUME_IN_PROGRESS:
  21353. + break;
  21354. + case VC_RESUME_RESUMED:
  21355. + complete_all(&arm_state->vc_resume_complete);
  21356. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  21357. + break;
  21358. + default:
  21359. + BUG();
  21360. + break;
  21361. + }
  21362. +}
  21363. +
  21364. +
  21365. +/* should be called with the write lock held */
  21366. +inline void
  21367. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  21368. +{
  21369. + del_timer(&arm_state->suspend_timer);
  21370. + arm_state->suspend_timer.expires = jiffies +
  21371. + msecs_to_jiffies(arm_state->
  21372. + suspend_timer_timeout);
  21373. + add_timer(&arm_state->suspend_timer);
  21374. + arm_state->suspend_timer_running = 1;
  21375. +}
  21376. +
  21377. +/* should be called with the write lock held */
  21378. +static inline void
  21379. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  21380. +{
  21381. + if (arm_state->suspend_timer_running) {
  21382. + del_timer(&arm_state->suspend_timer);
  21383. + arm_state->suspend_timer_running = 0;
  21384. + }
  21385. +}
  21386. +
  21387. +static inline int
  21388. +need_resume(VCHIQ_STATE_T *state)
  21389. +{
  21390. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21391. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  21392. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  21393. + vchiq_videocore_wanted(state);
  21394. +}
  21395. +
  21396. +static int
  21397. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  21398. +{
  21399. + int status = VCHIQ_SUCCESS;
  21400. + const unsigned long timeout_val =
  21401. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  21402. + int resume_count = 0;
  21403. +
  21404. + /* Allow any threads which were blocked by the last force suspend to
  21405. + * complete if they haven't already. Only give this one shot; if
  21406. + * blocked_count is incremented after blocked_blocker is completed
  21407. + * (which only happens when blocked_count hits 0) then those threads
  21408. + * will have to wait until next time around */
  21409. + if (arm_state->blocked_count) {
  21410. + reinit_completion(&arm_state->blocked_blocker);
  21411. + write_unlock_bh(&arm_state->susp_res_lock);
  21412. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  21413. + "blocked clients", __func__);
  21414. + if (wait_for_completion_interruptible_timeout(
  21415. + &arm_state->blocked_blocker, timeout_val)
  21416. + <= 0) {
  21417. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  21418. + "previously blocked clients failed" , __func__);
  21419. + status = VCHIQ_ERROR;
  21420. + write_lock_bh(&arm_state->susp_res_lock);
  21421. + goto out;
  21422. + }
  21423. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  21424. + "clients resumed", __func__);
  21425. + write_lock_bh(&arm_state->susp_res_lock);
  21426. + }
  21427. +
  21428. + /* We need to wait for resume to complete if it's in process */
  21429. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  21430. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  21431. + if (resume_count > 1) {
  21432. + status = VCHIQ_ERROR;
  21433. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  21434. + "many times for resume" , __func__);
  21435. + goto out;
  21436. + }
  21437. + write_unlock_bh(&arm_state->susp_res_lock);
  21438. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  21439. + __func__);
  21440. + if (wait_for_completion_interruptible_timeout(
  21441. + &arm_state->vc_resume_complete, timeout_val)
  21442. + <= 0) {
  21443. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  21444. + "resume failed (%s)", __func__,
  21445. + resume_state_names[arm_state->vc_resume_state +
  21446. + VC_RESUME_NUM_OFFSET]);
  21447. + status = VCHIQ_ERROR;
  21448. + write_lock_bh(&arm_state->susp_res_lock);
  21449. + goto out;
  21450. + }
  21451. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  21452. + write_lock_bh(&arm_state->susp_res_lock);
  21453. + resume_count++;
  21454. + }
  21455. + reinit_completion(&arm_state->resume_blocker);
  21456. + arm_state->resume_blocked = 1;
  21457. +
  21458. +out:
  21459. + return status;
  21460. +}
  21461. +
  21462. +static inline void
  21463. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  21464. +{
  21465. + complete_all(&arm_state->resume_blocker);
  21466. + arm_state->resume_blocked = 0;
  21467. +}
  21468. +
  21469. +/* Initiate suspend via slot handler. Should be called with the write lock
  21470. + * held */
  21471. +VCHIQ_STATUS_T
  21472. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  21473. +{
  21474. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  21475. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21476. +
  21477. + if (!arm_state)
  21478. + goto out;
  21479. +
  21480. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21481. + status = VCHIQ_SUCCESS;
  21482. +
  21483. +
  21484. + switch (arm_state->vc_suspend_state) {
  21485. + case VC_SUSPEND_REQUESTED:
  21486. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  21487. + "requested", __func__);
  21488. + break;
  21489. + case VC_SUSPEND_IN_PROGRESS:
  21490. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  21491. + "progress", __func__);
  21492. + break;
  21493. +
  21494. + default:
  21495. + /* We don't expect to be in other states, so log but continue
  21496. + * anyway */
  21497. + vchiq_log_error(vchiq_susp_log_level,
  21498. + "%s unexpected suspend state %s", __func__,
  21499. + suspend_state_names[arm_state->vc_suspend_state +
  21500. + VC_SUSPEND_NUM_OFFSET]);
  21501. + /* fall through */
  21502. + case VC_SUSPEND_REJECTED:
  21503. + case VC_SUSPEND_FAILED:
  21504. + /* Ensure any idle state actions have been run */
  21505. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  21506. + /* fall through */
  21507. + case VC_SUSPEND_IDLE:
  21508. + vchiq_log_info(vchiq_susp_log_level,
  21509. + "%s: suspending", __func__);
  21510. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  21511. + /* kick the slot handler thread to initiate suspend */
  21512. + request_poll(state, NULL, 0);
  21513. + break;
  21514. + }
  21515. +
  21516. +out:
  21517. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  21518. + return status;
  21519. +}
  21520. +
  21521. +void
  21522. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  21523. +{
  21524. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21525. + int susp = 0;
  21526. +
  21527. + if (!arm_state)
  21528. + goto out;
  21529. +
  21530. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21531. +
  21532. + write_lock_bh(&arm_state->susp_res_lock);
  21533. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  21534. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  21535. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  21536. + susp = 1;
  21537. + }
  21538. + write_unlock_bh(&arm_state->susp_res_lock);
  21539. +
  21540. + if (susp)
  21541. + vchiq_platform_suspend(state);
  21542. +
  21543. +out:
  21544. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  21545. + return;
  21546. +}
  21547. +
  21548. +
  21549. +static void
  21550. +output_timeout_error(VCHIQ_STATE_T *state)
  21551. +{
  21552. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21553. + char service_err[50] = "";
  21554. + int vc_use_count = arm_state->videocore_use_count;
  21555. + int active_services = state->unused_service;
  21556. + int i;
  21557. +
  21558. + if (!arm_state->videocore_use_count) {
  21559. + snprintf(service_err, 50, " Videocore usecount is 0");
  21560. + goto output_msg;
  21561. + }
  21562. + for (i = 0; i < active_services; i++) {
  21563. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  21564. + if (service_ptr && service_ptr->service_use_count &&
  21565. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  21566. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  21567. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  21568. + service_ptr->base.fourcc),
  21569. + service_ptr->client_id,
  21570. + service_ptr->service_use_count,
  21571. + service_ptr->service_use_count ==
  21572. + vc_use_count ? "" : " (+ more)");
  21573. + break;
  21574. + }
  21575. + }
  21576. +
  21577. +output_msg:
  21578. + vchiq_log_error(vchiq_susp_log_level,
  21579. + "timed out waiting for vc suspend (%d).%s",
  21580. + arm_state->autosuspend_override, service_err);
  21581. +
  21582. +}
  21583. +
  21584. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  21585. +** We don't actually force suspend, since videocore may get into a bad state
  21586. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  21587. +** determine a good point to suspend. If this doesn't happen within 100ms we
  21588. +** report failure.
  21589. +**
  21590. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  21591. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  21592. +*/
  21593. +VCHIQ_STATUS_T
  21594. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  21595. +{
  21596. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21597. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  21598. + long rc = 0;
  21599. + int repeat = -1;
  21600. +
  21601. + if (!arm_state)
  21602. + goto out;
  21603. +
  21604. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21605. +
  21606. + write_lock_bh(&arm_state->susp_res_lock);
  21607. +
  21608. + status = block_resume(arm_state);
  21609. + if (status != VCHIQ_SUCCESS)
  21610. + goto unlock;
  21611. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  21612. + /* Already suspended - just block resume and exit */
  21613. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  21614. + __func__);
  21615. + status = VCHIQ_SUCCESS;
  21616. + goto unlock;
  21617. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  21618. + /* initiate suspend immediately in the case that we're waiting
  21619. + * for the timeout */
  21620. + stop_suspend_timer(arm_state);
  21621. + if (!vchiq_videocore_wanted(state)) {
  21622. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  21623. + "idle, initiating suspend", __func__);
  21624. + status = vchiq_arm_vcsuspend(state);
  21625. + } else if (arm_state->autosuspend_override <
  21626. + FORCE_SUSPEND_FAIL_MAX) {
  21627. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  21628. + "videocore go idle", __func__);
  21629. + status = VCHIQ_SUCCESS;
  21630. + } else {
  21631. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  21632. + "many times - attempting suspend", __func__);
  21633. + status = vchiq_arm_vcsuspend(state);
  21634. + }
  21635. + } else {
  21636. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  21637. + "in progress - wait for completion", __func__);
  21638. + status = VCHIQ_SUCCESS;
  21639. + }
  21640. +
  21641. + /* Wait for suspend to happen due to system idle (not forced..) */
  21642. + if (status != VCHIQ_SUCCESS)
  21643. + goto unblock_resume;
  21644. +
  21645. + do {
  21646. + write_unlock_bh(&arm_state->susp_res_lock);
  21647. +
  21648. + rc = wait_for_completion_interruptible_timeout(
  21649. + &arm_state->vc_suspend_complete,
  21650. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  21651. +
  21652. + write_lock_bh(&arm_state->susp_res_lock);
  21653. + if (rc < 0) {
  21654. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  21655. + "interrupted waiting for suspend", __func__);
  21656. + status = VCHIQ_ERROR;
  21657. + goto unblock_resume;
  21658. + } else if (rc == 0) {
  21659. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  21660. + /* Repeat timeout once if in progress */
  21661. + if (repeat < 0) {
  21662. + repeat = 1;
  21663. + continue;
  21664. + }
  21665. + }
  21666. + arm_state->autosuspend_override++;
  21667. + output_timeout_error(state);
  21668. +
  21669. + status = VCHIQ_RETRY;
  21670. + goto unblock_resume;
  21671. + }
  21672. + } while (0 < (repeat--));
  21673. +
  21674. + /* Check and report state in case we need to abort ARM suspend */
  21675. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  21676. + status = VCHIQ_RETRY;
  21677. + vchiq_log_error(vchiq_susp_log_level,
  21678. + "%s videocore suspend failed (state %s)", __func__,
  21679. + suspend_state_names[arm_state->vc_suspend_state +
  21680. + VC_SUSPEND_NUM_OFFSET]);
  21681. + /* Reset the state only if it's still in an error state.
  21682. + * Something could have already initiated another suspend. */
  21683. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  21684. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  21685. +
  21686. + goto unblock_resume;
  21687. + }
  21688. +
  21689. + /* successfully suspended - unlock and exit */
  21690. + goto unlock;
  21691. +
  21692. +unblock_resume:
  21693. + /* all error states need to unblock resume before exit */
  21694. + unblock_resume(arm_state);
  21695. +
  21696. +unlock:
  21697. + write_unlock_bh(&arm_state->susp_res_lock);
  21698. +
  21699. +out:
  21700. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  21701. + return status;
  21702. +}
  21703. +
  21704. +void
  21705. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  21706. +{
  21707. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21708. +
  21709. + if (!arm_state)
  21710. + goto out;
  21711. +
  21712. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21713. +
  21714. + write_lock_bh(&arm_state->susp_res_lock);
  21715. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  21716. + arm_state->first_connect &&
  21717. + !vchiq_videocore_wanted(state)) {
  21718. + vchiq_arm_vcsuspend(state);
  21719. + }
  21720. + write_unlock_bh(&arm_state->susp_res_lock);
  21721. +
  21722. +out:
  21723. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  21724. + return;
  21725. +}
  21726. +
  21727. +
  21728. +int
  21729. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  21730. +{
  21731. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21732. + int resume = 0;
  21733. + int ret = -1;
  21734. +
  21735. + if (!arm_state)
  21736. + goto out;
  21737. +
  21738. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21739. +
  21740. + write_lock_bh(&arm_state->susp_res_lock);
  21741. + unblock_resume(arm_state);
  21742. + resume = vchiq_check_resume(state);
  21743. + write_unlock_bh(&arm_state->susp_res_lock);
  21744. +
  21745. + if (resume) {
  21746. + if (wait_for_completion_interruptible(
  21747. + &arm_state->vc_resume_complete) < 0) {
  21748. + vchiq_log_error(vchiq_susp_log_level,
  21749. + "%s interrupted", __func__);
  21750. + /* failed, cannot accurately derive suspend
  21751. + * state, so exit early. */
  21752. + goto out;
  21753. + }
  21754. + }
  21755. +
  21756. + read_lock_bh(&arm_state->susp_res_lock);
  21757. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  21758. + vchiq_log_info(vchiq_susp_log_level,
  21759. + "%s: Videocore remains suspended", __func__);
  21760. + } else {
  21761. + vchiq_log_info(vchiq_susp_log_level,
  21762. + "%s: Videocore resumed", __func__);
  21763. + ret = 0;
  21764. + }
  21765. + read_unlock_bh(&arm_state->susp_res_lock);
  21766. +out:
  21767. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  21768. + return ret;
  21769. +}
  21770. +
  21771. +/* This function should be called with the write lock held */
  21772. +int
  21773. +vchiq_check_resume(VCHIQ_STATE_T *state)
  21774. +{
  21775. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21776. + int resume = 0;
  21777. +
  21778. + if (!arm_state)
  21779. + goto out;
  21780. +
  21781. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21782. +
  21783. + if (need_resume(state)) {
  21784. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  21785. + request_poll(state, NULL, 0);
  21786. + resume = 1;
  21787. + }
  21788. +
  21789. +out:
  21790. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  21791. + return resume;
  21792. +}
  21793. +
  21794. +void
  21795. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  21796. +{
  21797. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21798. + int res = 0;
  21799. +
  21800. + if (!arm_state)
  21801. + goto out;
  21802. +
  21803. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21804. +
  21805. + write_lock_bh(&arm_state->susp_res_lock);
  21806. + if (arm_state->wake_address == 0) {
  21807. + vchiq_log_info(vchiq_susp_log_level,
  21808. + "%s: already awake", __func__);
  21809. + goto unlock;
  21810. + }
  21811. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  21812. + vchiq_log_info(vchiq_susp_log_level,
  21813. + "%s: already resuming", __func__);
  21814. + goto unlock;
  21815. + }
  21816. +
  21817. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  21818. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  21819. + res = 1;
  21820. + } else
  21821. + vchiq_log_trace(vchiq_susp_log_level,
  21822. + "%s: not resuming (resume state %s)", __func__,
  21823. + resume_state_names[arm_state->vc_resume_state +
  21824. + VC_RESUME_NUM_OFFSET]);
  21825. +
  21826. +unlock:
  21827. + write_unlock_bh(&arm_state->susp_res_lock);
  21828. +
  21829. + if (res)
  21830. + vchiq_platform_resume(state);
  21831. +
  21832. +out:
  21833. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  21834. + return;
  21835. +
  21836. +}
  21837. +
  21838. +
  21839. +
  21840. +VCHIQ_STATUS_T
  21841. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  21842. + enum USE_TYPE_E use_type)
  21843. +{
  21844. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21845. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  21846. + char entity[16];
  21847. + int *entity_uc;
  21848. + int local_uc, local_entity_uc;
  21849. +
  21850. + if (!arm_state)
  21851. + goto out;
  21852. +
  21853. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21854. +
  21855. + if (use_type == USE_TYPE_VCHIQ) {
  21856. + sprintf(entity, "VCHIQ: ");
  21857. + entity_uc = &arm_state->peer_use_count;
  21858. + } else if (service) {
  21859. + sprintf(entity, "%c%c%c%c:%03d",
  21860. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  21861. + service->client_id);
  21862. + entity_uc = &service->service_use_count;
  21863. + } else {
  21864. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  21865. + "ptr", __func__);
  21866. + ret = VCHIQ_ERROR;
  21867. + goto out;
  21868. + }
  21869. +
  21870. + write_lock_bh(&arm_state->susp_res_lock);
  21871. + while (arm_state->resume_blocked) {
  21872. + /* If we call 'use' while force suspend is waiting for suspend,
  21873. + * then we're about to block the thread which the force is
  21874. + * waiting to complete, so we're bound to just time out. In this
  21875. + * case, set the suspend state such that the wait will be
  21876. + * canceled, so we can complete as quickly as possible. */
  21877. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  21878. + VC_SUSPEND_IDLE) {
  21879. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  21880. + break;
  21881. + }
  21882. + /* If suspend is already in progress then we need to block */
  21883. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  21884. + /* Indicate that there are threads waiting on the resume
  21885. + * blocker. These need to be allowed to complete before
  21886. + * a _second_ call to force suspend can complete,
  21887. + * otherwise low priority threads might never actually
  21888. + * continue */
  21889. + arm_state->blocked_count++;
  21890. + write_unlock_bh(&arm_state->susp_res_lock);
  21891. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  21892. + "blocked - waiting...", __func__, entity);
  21893. + if (wait_for_completion_killable(
  21894. + &arm_state->resume_blocker) != 0) {
  21895. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  21896. + "wait for resume blocker interrupted",
  21897. + __func__, entity);
  21898. + ret = VCHIQ_ERROR;
  21899. + write_lock_bh(&arm_state->susp_res_lock);
  21900. + arm_state->blocked_count--;
  21901. + write_unlock_bh(&arm_state->susp_res_lock);
  21902. + goto out;
  21903. + }
  21904. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  21905. + "unblocked", __func__, entity);
  21906. + write_lock_bh(&arm_state->susp_res_lock);
  21907. + if (--arm_state->blocked_count == 0)
  21908. + complete_all(&arm_state->blocked_blocker);
  21909. + }
  21910. + }
  21911. +
  21912. + stop_suspend_timer(arm_state);
  21913. +
  21914. + local_uc = ++arm_state->videocore_use_count;
  21915. + local_entity_uc = ++(*entity_uc);
  21916. +
  21917. + /* If there's a pending request which hasn't yet been serviced then
  21918. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  21919. + * vc_resume_complete will block until we either resume or fail to
  21920. + * suspend */
  21921. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  21922. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  21923. +
  21924. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  21925. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  21926. + vchiq_log_info(vchiq_susp_log_level,
  21927. + "%s %s count %d, state count %d",
  21928. + __func__, entity, local_entity_uc, local_uc);
  21929. + request_poll(state, NULL, 0);
  21930. + } else
  21931. + vchiq_log_trace(vchiq_susp_log_level,
  21932. + "%s %s count %d, state count %d",
  21933. + __func__, entity, *entity_uc, local_uc);
  21934. +
  21935. +
  21936. + write_unlock_bh(&arm_state->susp_res_lock);
  21937. +
  21938. + /* Completion is in a done state when we're not suspended, so this won't
  21939. + * block for the non-suspended case. */
  21940. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  21941. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  21942. + __func__, entity);
  21943. + if (wait_for_completion_killable(
  21944. + &arm_state->vc_resume_complete) != 0) {
  21945. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  21946. + "resume interrupted", __func__, entity);
  21947. + ret = VCHIQ_ERROR;
  21948. + goto out;
  21949. + }
  21950. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  21951. + entity);
  21952. + }
  21953. +
  21954. + if (ret == VCHIQ_SUCCESS) {
  21955. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  21956. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  21957. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  21958. + /* Send the use notify to videocore */
  21959. + status = vchiq_send_remote_use_active(state);
  21960. + if (status == VCHIQ_SUCCESS)
  21961. + ack_cnt--;
  21962. + else
  21963. + atomic_add(ack_cnt,
  21964. + &arm_state->ka_use_ack_count);
  21965. + }
  21966. + }
  21967. +
  21968. +out:
  21969. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  21970. + return ret;
  21971. +}
  21972. +
  21973. +VCHIQ_STATUS_T
  21974. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  21975. +{
  21976. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  21977. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  21978. + char entity[16];
  21979. + int *entity_uc;
  21980. + int local_uc, local_entity_uc;
  21981. +
  21982. + if (!arm_state)
  21983. + goto out;
  21984. +
  21985. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  21986. +
  21987. + if (service) {
  21988. + sprintf(entity, "%c%c%c%c:%03d",
  21989. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  21990. + service->client_id);
  21991. + entity_uc = &service->service_use_count;
  21992. + } else {
  21993. + sprintf(entity, "PEER: ");
  21994. + entity_uc = &arm_state->peer_use_count;
  21995. + }
  21996. +
  21997. + write_lock_bh(&arm_state->susp_res_lock);
  21998. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  21999. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  22000. + WARN_ON(!arm_state->videocore_use_count);
  22001. + WARN_ON(!(*entity_uc));
  22002. + ret = VCHIQ_ERROR;
  22003. + goto unlock;
  22004. + }
  22005. + local_uc = --arm_state->videocore_use_count;
  22006. + local_entity_uc = --(*entity_uc);
  22007. +
  22008. + if (!vchiq_videocore_wanted(state)) {
  22009. + if (vchiq_platform_use_suspend_timer() &&
  22010. + !arm_state->resume_blocked) {
  22011. + /* Only use the timer if we're not trying to force
  22012. + * suspend (=> resume_blocked) */
  22013. + start_suspend_timer(arm_state);
  22014. + } else {
  22015. + vchiq_log_info(vchiq_susp_log_level,
  22016. + "%s %s count %d, state count %d - suspending",
  22017. + __func__, entity, *entity_uc,
  22018. + arm_state->videocore_use_count);
  22019. + vchiq_arm_vcsuspend(state);
  22020. + }
  22021. + } else
  22022. + vchiq_log_trace(vchiq_susp_log_level,
  22023. + "%s %s count %d, state count %d",
  22024. + __func__, entity, *entity_uc,
  22025. + arm_state->videocore_use_count);
  22026. +
  22027. +unlock:
  22028. + write_unlock_bh(&arm_state->susp_res_lock);
  22029. +
  22030. +out:
  22031. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  22032. + return ret;
  22033. +}
  22034. +
  22035. +void
  22036. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  22037. +{
  22038. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22039. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22040. + atomic_inc(&arm_state->ka_use_count);
  22041. + complete(&arm_state->ka_evt);
  22042. +}
  22043. +
  22044. +void
  22045. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  22046. +{
  22047. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22048. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22049. + atomic_inc(&arm_state->ka_release_count);
  22050. + complete(&arm_state->ka_evt);
  22051. +}
  22052. +
  22053. +VCHIQ_STATUS_T
  22054. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  22055. +{
  22056. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  22057. +}
  22058. +
  22059. +VCHIQ_STATUS_T
  22060. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  22061. +{
  22062. + return vchiq_release_internal(service->state, service);
  22063. +}
  22064. +
  22065. +static void suspend_timer_callback(unsigned long context)
  22066. +{
  22067. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  22068. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22069. + if (!arm_state)
  22070. + goto out;
  22071. + vchiq_log_info(vchiq_susp_log_level,
  22072. + "%s - suspend timer expired - check suspend", __func__);
  22073. + vchiq_check_suspend(state);
  22074. +out:
  22075. + return;
  22076. +}
  22077. +
  22078. +VCHIQ_STATUS_T
  22079. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  22080. +{
  22081. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22082. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22083. + if (service) {
  22084. + ret = vchiq_use_internal(service->state, service,
  22085. + USE_TYPE_SERVICE_NO_RESUME);
  22086. + unlock_service(service);
  22087. + }
  22088. + return ret;
  22089. +}
  22090. +
  22091. +VCHIQ_STATUS_T
  22092. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  22093. +{
  22094. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22095. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22096. + if (service) {
  22097. + ret = vchiq_use_internal(service->state, service,
  22098. + USE_TYPE_SERVICE);
  22099. + unlock_service(service);
  22100. + }
  22101. + return ret;
  22102. +}
  22103. +
  22104. +VCHIQ_STATUS_T
  22105. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  22106. +{
  22107. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22108. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  22109. + if (service) {
  22110. + ret = vchiq_release_internal(service->state, service);
  22111. + unlock_service(service);
  22112. + }
  22113. + return ret;
  22114. +}
  22115. +
  22116. +void
  22117. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  22118. +{
  22119. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22120. + int i, j = 0;
  22121. + /* Only dump 64 services */
  22122. + static const int local_max_services = 64;
  22123. + /* If there's more than 64 services, only dump ones with
  22124. + * non-zero counts */
  22125. + int only_nonzero = 0;
  22126. + static const char *nz = "<-- preventing suspend";
  22127. +
  22128. + enum vc_suspend_status vc_suspend_state;
  22129. + enum vc_resume_status vc_resume_state;
  22130. + int peer_count;
  22131. + int vc_use_count;
  22132. + int active_services;
  22133. + struct service_data_struct {
  22134. + int fourcc;
  22135. + int clientid;
  22136. + int use_count;
  22137. + } service_data[local_max_services];
  22138. +
  22139. + if (!arm_state)
  22140. + return;
  22141. +
  22142. + read_lock_bh(&arm_state->susp_res_lock);
  22143. + vc_suspend_state = arm_state->vc_suspend_state;
  22144. + vc_resume_state = arm_state->vc_resume_state;
  22145. + peer_count = arm_state->peer_use_count;
  22146. + vc_use_count = arm_state->videocore_use_count;
  22147. + active_services = state->unused_service;
  22148. + if (active_services > local_max_services)
  22149. + only_nonzero = 1;
  22150. +
  22151. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  22152. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  22153. + if (!service_ptr)
  22154. + continue;
  22155. +
  22156. + if (only_nonzero && !service_ptr->service_use_count)
  22157. + continue;
  22158. +
  22159. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  22160. + service_data[j].fourcc = service_ptr->base.fourcc;
  22161. + service_data[j].clientid = service_ptr->client_id;
  22162. + service_data[j++].use_count = service_ptr->
  22163. + service_use_count;
  22164. + }
  22165. + }
  22166. +
  22167. + read_unlock_bh(&arm_state->susp_res_lock);
  22168. +
  22169. + vchiq_log_warning(vchiq_susp_log_level,
  22170. + "-- Videcore suspend state: %s --",
  22171. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  22172. + vchiq_log_warning(vchiq_susp_log_level,
  22173. + "-- Videcore resume state: %s --",
  22174. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  22175. +
  22176. + if (only_nonzero)
  22177. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  22178. + "services (%d). Only dumping up to first %d services "
  22179. + "with non-zero use-count", active_services,
  22180. + local_max_services);
  22181. +
  22182. + for (i = 0; i < j; i++) {
  22183. + vchiq_log_warning(vchiq_susp_log_level,
  22184. + "----- %c%c%c%c:%d service count %d %s",
  22185. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  22186. + service_data[i].clientid,
  22187. + service_data[i].use_count,
  22188. + service_data[i].use_count ? nz : "");
  22189. + }
  22190. + vchiq_log_warning(vchiq_susp_log_level,
  22191. + "----- VCHIQ use count count %d", peer_count);
  22192. + vchiq_log_warning(vchiq_susp_log_level,
  22193. + "--- Overall vchiq instance use count %d", vc_use_count);
  22194. +
  22195. + vchiq_dump_platform_use_state(state);
  22196. +}
  22197. +
  22198. +VCHIQ_STATUS_T
  22199. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  22200. +{
  22201. + VCHIQ_ARM_STATE_T *arm_state;
  22202. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  22203. +
  22204. + if (!service || !service->state)
  22205. + goto out;
  22206. +
  22207. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  22208. +
  22209. + arm_state = vchiq_platform_get_arm_state(service->state);
  22210. +
  22211. + read_lock_bh(&arm_state->susp_res_lock);
  22212. + if (service->service_use_count)
  22213. + ret = VCHIQ_SUCCESS;
  22214. + read_unlock_bh(&arm_state->susp_res_lock);
  22215. +
  22216. + if (ret == VCHIQ_ERROR) {
  22217. + vchiq_log_error(vchiq_susp_log_level,
  22218. + "%s ERROR - %c%c%c%c:%d service count %d, "
  22219. + "state count %d, videocore suspend state %s", __func__,
  22220. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  22221. + service->client_id, service->service_use_count,
  22222. + arm_state->videocore_use_count,
  22223. + suspend_state_names[arm_state->vc_suspend_state +
  22224. + VC_SUSPEND_NUM_OFFSET]);
  22225. + vchiq_dump_service_use_state(service->state);
  22226. + }
  22227. +out:
  22228. + return ret;
  22229. +}
  22230. +
  22231. +/* stub functions */
  22232. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  22233. +{
  22234. + (void)state;
  22235. +}
  22236. +
  22237. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  22238. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  22239. +{
  22240. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  22241. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  22242. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  22243. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  22244. + write_lock_bh(&arm_state->susp_res_lock);
  22245. + if (!arm_state->first_connect) {
  22246. + char threadname[10];
  22247. + arm_state->first_connect = 1;
  22248. + write_unlock_bh(&arm_state->susp_res_lock);
  22249. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  22250. + state->id);
  22251. + arm_state->ka_thread = kthread_create(
  22252. + &vchiq_keepalive_thread_func,
  22253. + (void *)state,
  22254. + threadname);
  22255. + if (arm_state->ka_thread == NULL) {
  22256. + vchiq_log_error(vchiq_susp_log_level,
  22257. + "vchiq: FATAL: couldn't create thread %s",
  22258. + threadname);
  22259. + } else {
  22260. + wake_up_process(arm_state->ka_thread);
  22261. + }
  22262. + } else
  22263. + write_unlock_bh(&arm_state->susp_res_lock);
  22264. + }
  22265. +}
  22266. +
  22267. +
  22268. +/****************************************************************************
  22269. +*
  22270. +* vchiq_init - called when the module is loaded.
  22271. +*
  22272. +***************************************************************************/
  22273. +
  22274. +static int __init
  22275. +vchiq_init(void)
  22276. +{
  22277. + int err;
  22278. + void *ptr_err;
  22279. +
  22280. + /* create proc entries */
  22281. + err = vchiq_proc_init();
  22282. + if (err != 0)
  22283. + goto failed_proc_init;
  22284. +
  22285. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  22286. + if (err != 0) {
  22287. + vchiq_log_error(vchiq_arm_log_level,
  22288. + "Unable to allocate device number");
  22289. + goto failed_alloc_chrdev;
  22290. + }
  22291. + cdev_init(&vchiq_cdev, &vchiq_fops);
  22292. + vchiq_cdev.owner = THIS_MODULE;
  22293. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  22294. + if (err != 0) {
  22295. + vchiq_log_error(vchiq_arm_log_level,
  22296. + "Unable to register device");
  22297. + goto failed_cdev_add;
  22298. + }
  22299. +
  22300. + /* create sysfs entries */
  22301. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  22302. + ptr_err = vchiq_class;
  22303. + if (IS_ERR(ptr_err))
  22304. + goto failed_class_create;
  22305. +
  22306. + vchiq_dev = device_create(vchiq_class, NULL,
  22307. + vchiq_devid, NULL, "vchiq");
  22308. + ptr_err = vchiq_dev;
  22309. + if (IS_ERR(ptr_err))
  22310. + goto failed_device_create;
  22311. +
  22312. + err = vchiq_platform_init(&g_state);
  22313. + if (err != 0)
  22314. + goto failed_platform_init;
  22315. +
  22316. + vchiq_log_info(vchiq_arm_log_level,
  22317. + "vchiq: initialised - version %d (min %d), device %d.%d",
  22318. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  22319. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  22320. +
  22321. + return 0;
  22322. +
  22323. +failed_platform_init:
  22324. + device_destroy(vchiq_class, vchiq_devid);
  22325. +failed_device_create:
  22326. + class_destroy(vchiq_class);
  22327. +failed_class_create:
  22328. + cdev_del(&vchiq_cdev);
  22329. + err = PTR_ERR(ptr_err);
  22330. +failed_cdev_add:
  22331. + unregister_chrdev_region(vchiq_devid, 1);
  22332. +failed_alloc_chrdev:
  22333. + vchiq_proc_deinit();
  22334. +failed_proc_init:
  22335. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  22336. + return err;
  22337. +}
  22338. +
  22339. +static int vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  22340. +{
  22341. + VCHIQ_SERVICE_T *service;
  22342. + int use_count = 0, i;
  22343. + i = 0;
  22344. + while ((service = next_service_by_instance(instance->state,
  22345. + instance, &i)) != NULL) {
  22346. + use_count += service->service_use_count;
  22347. + unlock_service(service);
  22348. + }
  22349. + return use_count;
  22350. +}
  22351. +
  22352. +/* read the per-process use-count */
  22353. +static int proc_read_use_count(char *page, char **start,
  22354. + off_t off, int count,
  22355. + int *eof, void *data)
  22356. +{
  22357. + VCHIQ_INSTANCE_T instance = data;
  22358. + int len, use_count;
  22359. +
  22360. + use_count = vchiq_instance_get_use_count(instance);
  22361. + len = snprintf(page+off, count, "%d\n", use_count);
  22362. +
  22363. + return len;
  22364. +}
  22365. +
  22366. +/* add an instance (process) to the proc entries */
  22367. +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance)
  22368. +{
  22369. +#if 1
  22370. + return 0;
  22371. +#else
  22372. + char pidstr[32];
  22373. + struct proc_dir_entry *top, *use_count;
  22374. + struct proc_dir_entry *clients = vchiq_clients_top();
  22375. + int pid = instance->pid;
  22376. +
  22377. + snprintf(pidstr, sizeof(pidstr), "%d", pid);
  22378. + top = proc_mkdir(pidstr, clients);
  22379. + if (!top)
  22380. + goto fail_top;
  22381. +
  22382. + use_count = create_proc_read_entry("use_count",
  22383. + 0444, top,
  22384. + proc_read_use_count,
  22385. + instance);
  22386. + if (!use_count)
  22387. + goto fail_use_count;
  22388. +
  22389. + instance->proc_entry = top;
  22390. +
  22391. + return 0;
  22392. +
  22393. +fail_use_count:
  22394. + remove_proc_entry(top->name, clients);
  22395. +fail_top:
  22396. + return -ENOMEM;
  22397. +#endif
  22398. +}
  22399. +
  22400. +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance)
  22401. +{
  22402. +#if 0
  22403. + struct proc_dir_entry *clients = vchiq_clients_top();
  22404. + remove_proc_entry("use_count", instance->proc_entry);
  22405. + remove_proc_entry(instance->proc_entry->name, clients);
  22406. +#endif
  22407. +}
  22408. +
  22409. +/****************************************************************************
  22410. +*
  22411. +* vchiq_exit - called when the module is unloaded.
  22412. +*
  22413. +***************************************************************************/
  22414. +
  22415. +static void __exit
  22416. +vchiq_exit(void)
  22417. +{
  22418. + vchiq_platform_exit(&g_state);
  22419. + device_destroy(vchiq_class, vchiq_devid);
  22420. + class_destroy(vchiq_class);
  22421. + cdev_del(&vchiq_cdev);
  22422. + unregister_chrdev_region(vchiq_devid, 1);
  22423. +}
  22424. +
  22425. +module_init(vchiq_init);
  22426. +module_exit(vchiq_exit);
  22427. +MODULE_LICENSE("GPL");
  22428. +MODULE_AUTHOR("Broadcom Corporation");
  22429. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  22430. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 1970-01-01 01:00:00.000000000 +0100
  22431. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 2014-03-11 16:52:43.000000000 +0100
  22432. @@ -0,0 +1,212 @@
  22433. +/**
  22434. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  22435. + *
  22436. + * Redistribution and use in source and binary forms, with or without
  22437. + * modification, are permitted provided that the following conditions
  22438. + * are met:
  22439. + * 1. Redistributions of source code must retain the above copyright
  22440. + * notice, this list of conditions, and the following disclaimer,
  22441. + * without modification.
  22442. + * 2. Redistributions in binary form must reproduce the above copyright
  22443. + * notice, this list of conditions and the following disclaimer in the
  22444. + * documentation and/or other materials provided with the distribution.
  22445. + * 3. The names of the above-listed copyright holders may not be used
  22446. + * to endorse or promote products derived from this software without
  22447. + * specific prior written permission.
  22448. + *
  22449. + * ALTERNATIVELY, this software may be distributed under the terms of the
  22450. + * GNU General Public License ("GPL") version 2, as published by the Free
  22451. + * Software Foundation.
  22452. + *
  22453. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  22454. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22455. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22456. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22457. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22458. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22459. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22460. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22461. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22462. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22463. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22464. + */
  22465. +
  22466. +#ifndef VCHIQ_ARM_H
  22467. +#define VCHIQ_ARM_H
  22468. +
  22469. +#include <linux/mutex.h>
  22470. +#include <linux/semaphore.h>
  22471. +#include <linux/atomic.h>
  22472. +#include "vchiq_core.h"
  22473. +
  22474. +
  22475. +enum vc_suspend_status {
  22476. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  22477. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  22478. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  22479. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  22480. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  22481. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  22482. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  22483. +};
  22484. +
  22485. +enum vc_resume_status {
  22486. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  22487. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  22488. + VC_RESUME_REQUESTED, /* User has requested resume */
  22489. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  22490. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  22491. +};
  22492. +
  22493. +
  22494. +enum USE_TYPE_E {
  22495. + USE_TYPE_SERVICE,
  22496. + USE_TYPE_SERVICE_NO_RESUME,
  22497. + USE_TYPE_VCHIQ
  22498. +};
  22499. +
  22500. +
  22501. +
  22502. +typedef struct vchiq_arm_state_struct {
  22503. + /* Keepalive-related data */
  22504. + struct task_struct *ka_thread;
  22505. + struct completion ka_evt;
  22506. + atomic_t ka_use_count;
  22507. + atomic_t ka_use_ack_count;
  22508. + atomic_t ka_release_count;
  22509. +
  22510. + struct completion vc_suspend_complete;
  22511. + struct completion vc_resume_complete;
  22512. +
  22513. + rwlock_t susp_res_lock;
  22514. + enum vc_suspend_status vc_suspend_state;
  22515. + enum vc_resume_status vc_resume_state;
  22516. +
  22517. + unsigned int wake_address;
  22518. +
  22519. + struct timer_list suspend_timer;
  22520. + int suspend_timer_timeout;
  22521. + int suspend_timer_running;
  22522. +
  22523. + /* Global use count for videocore.
  22524. + ** This is equal to the sum of the use counts for all services. When
  22525. + ** this hits zero the videocore suspend procedure will be initiated.
  22526. + */
  22527. + int videocore_use_count;
  22528. +
  22529. + /* Use count to track requests from videocore peer.
  22530. + ** This use count is not associated with a service, so needs to be
  22531. + ** tracked separately with the state.
  22532. + */
  22533. + int peer_use_count;
  22534. +
  22535. + /* Flag to indicate whether resume is blocked. This happens when the
  22536. + ** ARM is suspending
  22537. + */
  22538. + struct completion resume_blocker;
  22539. + int resume_blocked;
  22540. + struct completion blocked_blocker;
  22541. + int blocked_count;
  22542. +
  22543. + int autosuspend_override;
  22544. +
  22545. + /* Flag to indicate that the first vchiq connect has made it through.
  22546. + ** This means that both sides should be fully ready, and we should
  22547. + ** be able to suspend after this point.
  22548. + */
  22549. + int first_connect;
  22550. +
  22551. + unsigned long long suspend_start_time;
  22552. + unsigned long long sleep_start_time;
  22553. + unsigned long long resume_start_time;
  22554. + unsigned long long last_wake_time;
  22555. +
  22556. +} VCHIQ_ARM_STATE_T;
  22557. +
  22558. +extern int vchiq_arm_log_level;
  22559. +extern int vchiq_susp_log_level;
  22560. +
  22561. +extern int __init
  22562. +vchiq_platform_init(VCHIQ_STATE_T *state);
  22563. +
  22564. +extern void __exit
  22565. +vchiq_platform_exit(VCHIQ_STATE_T *state);
  22566. +
  22567. +extern VCHIQ_STATE_T *
  22568. +vchiq_get_state(void);
  22569. +
  22570. +extern VCHIQ_STATUS_T
  22571. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  22572. +
  22573. +extern VCHIQ_STATUS_T
  22574. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  22575. +
  22576. +extern int
  22577. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  22578. +
  22579. +extern VCHIQ_STATUS_T
  22580. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  22581. +
  22582. +extern VCHIQ_STATUS_T
  22583. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  22584. +
  22585. +extern int
  22586. +vchiq_check_resume(VCHIQ_STATE_T *state);
  22587. +
  22588. +extern void
  22589. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  22590. +
  22591. +extern VCHIQ_STATUS_T
  22592. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  22593. +
  22594. +extern VCHIQ_STATUS_T
  22595. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  22596. +
  22597. +extern VCHIQ_STATUS_T
  22598. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  22599. +
  22600. +extern VCHIQ_STATUS_T
  22601. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  22602. +
  22603. +extern int
  22604. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  22605. +
  22606. +extern int
  22607. +vchiq_platform_use_suspend_timer(void);
  22608. +
  22609. +extern void
  22610. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  22611. +
  22612. +extern void
  22613. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  22614. +
  22615. +extern VCHIQ_ARM_STATE_T*
  22616. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  22617. +
  22618. +extern int
  22619. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  22620. +
  22621. +extern VCHIQ_STATUS_T
  22622. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  22623. + enum USE_TYPE_E use_type);
  22624. +extern VCHIQ_STATUS_T
  22625. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  22626. +
  22627. +void
  22628. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  22629. + enum vc_suspend_status new_state);
  22630. +
  22631. +void
  22632. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  22633. + enum vc_resume_status new_state);
  22634. +
  22635. +void
  22636. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  22637. +
  22638. +extern int vchiq_proc_init(void);
  22639. +extern void vchiq_proc_deinit(void);
  22640. +extern struct proc_dir_entry *vchiq_proc_top(void);
  22641. +extern struct proc_dir_entry *vchiq_clients_top(void);
  22642. +
  22643. +
  22644. +#endif /* VCHIQ_ARM_H */
  22645. diff -Nur linux-3.13.6/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
  22646. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 1970-01-01 01:00:00.000000000 +0100
  22647. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 2014-03-11 16:52:43.000000000 +0100
  22648. @@ -0,0 +1,37 @@
  22649. +/**
  22650. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  22651. + *
  22652. + * Redistribution and use in source and binary forms, with or without
  22653. + * modification, are permitted provided that the following conditions
  22654. + * are met:
  22655. + * 1. Redistributions of source code must retain the above copyright
  22656. + * notice, this list of conditions, and the following disclaimer,
  22657. + * without modification.
  22658. + * 2. Redistributions in binary form must reproduce the above copyright
  22659. + * notice, this list of conditions and the following disclaimer in the
  22660. + * documentation and/or other materials provided with the distribution.
  22661. + * 3. The names of the above-listed copyright holders may not be used
  22662. + * to endorse or promote products derived from this software without
  22663. + * specific prior written permission.
  22664. + *
  22665. + * ALTERNATIVELY, this software may be distributed under the terms of the
  22666. + * GNU General Public License ("GPL") version 2, as published by the Free
  22667. + * Software Foundation.
  22668. + *
  22669. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  22670. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22671. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22672. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22673. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22674. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22675. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22676. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22677. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22678. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22679. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22680. + */
  22681. +
  22682. +const char *vchiq_get_build_hostname(void);
  22683. +const char *vchiq_get_build_version(void);
  22684. +const char *vchiq_get_build_time(void);
  22685. +const char *vchiq_get_build_date(void);
  22686. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  22687. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 1970-01-01 01:00:00.000000000 +0100
  22688. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 2014-03-11 16:52:43.000000000 +0100
  22689. @@ -0,0 +1,60 @@
  22690. +/**
  22691. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  22692. + *
  22693. + * Redistribution and use in source and binary forms, with or without
  22694. + * modification, are permitted provided that the following conditions
  22695. + * are met:
  22696. + * 1. Redistributions of source code must retain the above copyright
  22697. + * notice, this list of conditions, and the following disclaimer,
  22698. + * without modification.
  22699. + * 2. Redistributions in binary form must reproduce the above copyright
  22700. + * notice, this list of conditions and the following disclaimer in the
  22701. + * documentation and/or other materials provided with the distribution.
  22702. + * 3. The names of the above-listed copyright holders may not be used
  22703. + * to endorse or promote products derived from this software without
  22704. + * specific prior written permission.
  22705. + *
  22706. + * ALTERNATIVELY, this software may be distributed under the terms of the
  22707. + * GNU General Public License ("GPL") version 2, as published by the Free
  22708. + * Software Foundation.
  22709. + *
  22710. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  22711. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22712. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22713. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22714. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22715. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22716. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22717. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22718. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22719. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22720. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22721. + */
  22722. +
  22723. +#ifndef VCHIQ_CFG_H
  22724. +#define VCHIQ_CFG_H
  22725. +
  22726. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  22727. +/* The version of VCHIQ - change with any non-trivial change */
  22728. +#define VCHIQ_VERSION 6
  22729. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  22730. +** incompatible change */
  22731. +#define VCHIQ_VERSION_MIN 3
  22732. +
  22733. +#define VCHIQ_MAX_STATES 1
  22734. +#define VCHIQ_MAX_SERVICES 4096
  22735. +#define VCHIQ_MAX_SLOTS 128
  22736. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  22737. +
  22738. +#define VCHIQ_NUM_CURRENT_BULKS 32
  22739. +#define VCHIQ_NUM_SERVICE_BULKS 4
  22740. +
  22741. +#ifndef VCHIQ_ENABLE_DEBUG
  22742. +#define VCHIQ_ENABLE_DEBUG 1
  22743. +#endif
  22744. +
  22745. +#ifndef VCHIQ_ENABLE_STATS
  22746. +#define VCHIQ_ENABLE_STATS 1
  22747. +#endif
  22748. +
  22749. +#endif /* VCHIQ_CFG_H */
  22750. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  22751. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 1970-01-01 01:00:00.000000000 +0100
  22752. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 2014-03-11 16:52:43.000000000 +0100
  22753. @@ -0,0 +1,119 @@
  22754. +/**
  22755. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  22756. + *
  22757. + * Redistribution and use in source and binary forms, with or without
  22758. + * modification, are permitted provided that the following conditions
  22759. + * are met:
  22760. + * 1. Redistributions of source code must retain the above copyright
  22761. + * notice, this list of conditions, and the following disclaimer,
  22762. + * without modification.
  22763. + * 2. Redistributions in binary form must reproduce the above copyright
  22764. + * notice, this list of conditions and the following disclaimer in the
  22765. + * documentation and/or other materials provided with the distribution.
  22766. + * 3. The names of the above-listed copyright holders may not be used
  22767. + * to endorse or promote products derived from this software without
  22768. + * specific prior written permission.
  22769. + *
  22770. + * ALTERNATIVELY, this software may be distributed under the terms of the
  22771. + * GNU General Public License ("GPL") version 2, as published by the Free
  22772. + * Software Foundation.
  22773. + *
  22774. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  22775. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22776. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22777. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22778. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22779. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22780. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22781. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22782. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22783. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22784. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22785. + */
  22786. +
  22787. +#include "vchiq_connected.h"
  22788. +#include "vchiq_core.h"
  22789. +#include <linux/module.h>
  22790. +#include <linux/mutex.h>
  22791. +
  22792. +#define MAX_CALLBACKS 10
  22793. +
  22794. +static int g_connected;
  22795. +static int g_num_deferred_callbacks;
  22796. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  22797. +static int g_once_init;
  22798. +static struct mutex g_connected_mutex;
  22799. +
  22800. +/****************************************************************************
  22801. +*
  22802. +* Function to initialize our lock.
  22803. +*
  22804. +***************************************************************************/
  22805. +
  22806. +static void connected_init(void)
  22807. +{
  22808. + if (!g_once_init) {
  22809. + mutex_init(&g_connected_mutex);
  22810. + g_once_init = 1;
  22811. + }
  22812. +}
  22813. +
  22814. +/****************************************************************************
  22815. +*
  22816. +* This function is used to defer initialization until the vchiq stack is
  22817. +* initialized. If the stack is already initialized, then the callback will
  22818. +* be made immediately, otherwise it will be deferred until
  22819. +* vchiq_call_connected_callbacks is called.
  22820. +*
  22821. +***************************************************************************/
  22822. +
  22823. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  22824. +{
  22825. + connected_init();
  22826. +
  22827. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  22828. + return;
  22829. +
  22830. + if (g_connected)
  22831. + /* We're already connected. Call the callback immediately. */
  22832. +
  22833. + callback();
  22834. + else {
  22835. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  22836. + vchiq_log_error(vchiq_core_log_level,
  22837. + "There already %d callback registered - "
  22838. + "please increase MAX_CALLBACKS",
  22839. + g_num_deferred_callbacks);
  22840. + else {
  22841. + g_deferred_callback[g_num_deferred_callbacks] =
  22842. + callback;
  22843. + g_num_deferred_callbacks++;
  22844. + }
  22845. + }
  22846. + mutex_unlock(&g_connected_mutex);
  22847. +}
  22848. +
  22849. +/****************************************************************************
  22850. +*
  22851. +* This function is called by the vchiq stack once it has been connected to
  22852. +* the videocore and clients can start to use the stack.
  22853. +*
  22854. +***************************************************************************/
  22855. +
  22856. +void vchiq_call_connected_callbacks(void)
  22857. +{
  22858. + int i;
  22859. +
  22860. + connected_init();
  22861. +
  22862. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  22863. + return;
  22864. +
  22865. + for (i = 0; i < g_num_deferred_callbacks; i++)
  22866. + g_deferred_callback[i]();
  22867. +
  22868. + g_num_deferred_callbacks = 0;
  22869. + g_connected = 1;
  22870. + mutex_unlock(&g_connected_mutex);
  22871. +}
  22872. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  22873. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  22874. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 1970-01-01 01:00:00.000000000 +0100
  22875. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 2014-03-11 16:54:58.000000000 +0100
  22876. @@ -0,0 +1,50 @@
  22877. +/**
  22878. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  22879. + *
  22880. + * Redistribution and use in source and binary forms, with or without
  22881. + * modification, are permitted provided that the following conditions
  22882. + * are met:
  22883. + * 1. Redistributions of source code must retain the above copyright
  22884. + * notice, this list of conditions, and the following disclaimer,
  22885. + * without modification.
  22886. + * 2. Redistributions in binary form must reproduce the above copyright
  22887. + * notice, this list of conditions and the following disclaimer in the
  22888. + * documentation and/or other materials provided with the distribution.
  22889. + * 3. The names of the above-listed copyright holders may not be used
  22890. + * to endorse or promote products derived from this software without
  22891. + * specific prior written permission.
  22892. + *
  22893. + * ALTERNATIVELY, this software may be distributed under the terms of the
  22894. + * GNU General Public License ("GPL") version 2, as published by the Free
  22895. + * Software Foundation.
  22896. + *
  22897. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  22898. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22899. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22900. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22901. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22902. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22903. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22904. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22905. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22906. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22907. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22908. + */
  22909. +
  22910. +#ifndef VCHIQ_CONNECTED_H
  22911. +#define VCHIQ_CONNECTED_H
  22912. +
  22913. +/* ---- Include Files ----------------------------------------------------- */
  22914. +
  22915. +/* ---- Constants and Types ---------------------------------------------- */
  22916. +
  22917. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  22918. +
  22919. +/* ---- Variable Externs ------------------------------------------------- */
  22920. +
  22921. +/* ---- Function Prototypes ---------------------------------------------- */
  22922. +
  22923. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  22924. +void vchiq_call_connected_callbacks(void);
  22925. +
  22926. +#endif /* VCHIQ_CONNECTED_H */
  22927. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  22928. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 1970-01-01 01:00:00.000000000 +0100
  22929. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 2014-03-11 16:52:43.000000000 +0100
  22930. @@ -0,0 +1,3824 @@
  22931. +/**
  22932. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  22933. + *
  22934. + * Redistribution and use in source and binary forms, with or without
  22935. + * modification, are permitted provided that the following conditions
  22936. + * are met:
  22937. + * 1. Redistributions of source code must retain the above copyright
  22938. + * notice, this list of conditions, and the following disclaimer,
  22939. + * without modification.
  22940. + * 2. Redistributions in binary form must reproduce the above copyright
  22941. + * notice, this list of conditions and the following disclaimer in the
  22942. + * documentation and/or other materials provided with the distribution.
  22943. + * 3. The names of the above-listed copyright holders may not be used
  22944. + * to endorse or promote products derived from this software without
  22945. + * specific prior written permission.
  22946. + *
  22947. + * ALTERNATIVELY, this software may be distributed under the terms of the
  22948. + * GNU General Public License ("GPL") version 2, as published by the Free
  22949. + * Software Foundation.
  22950. + *
  22951. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  22952. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22953. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22954. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22955. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22956. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22957. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22958. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22959. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22960. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22961. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22962. + */
  22963. +
  22964. +#include "vchiq_core.h"
  22965. +
  22966. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  22967. +
  22968. +#define HANDLE_STATE_SHIFT 12
  22969. +
  22970. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  22971. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  22972. +#define SLOT_INDEX_FROM_DATA(state, data) \
  22973. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  22974. + VCHIQ_SLOT_SIZE)
  22975. +#define SLOT_INDEX_FROM_INFO(state, info) \
  22976. + ((unsigned int)(info - state->slot_info))
  22977. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  22978. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  22979. +
  22980. +
  22981. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  22982. +
  22983. +
  22984. +struct vchiq_open_payload {
  22985. + int fourcc;
  22986. + int client_id;
  22987. + short version;
  22988. + short version_min;
  22989. +};
  22990. +
  22991. +struct vchiq_openack_payload {
  22992. + short version;
  22993. +};
  22994. +
  22995. +/* we require this for consistency between endpoints */
  22996. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  22997. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  22998. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  22999. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  23000. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  23001. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  23002. +
  23003. +/* Run time control of log level, based on KERN_XXX level. */
  23004. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  23005. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  23006. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  23007. +
  23008. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  23009. +
  23010. +static DEFINE_SPINLOCK(service_spinlock);
  23011. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  23012. +DEFINE_SPINLOCK(quota_spinlock);
  23013. +
  23014. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  23015. +static unsigned int handle_seq;
  23016. +
  23017. +static const char *const srvstate_names[] = {
  23018. + "FREE",
  23019. + "HIDDEN",
  23020. + "LISTENING",
  23021. + "OPENING",
  23022. + "OPEN",
  23023. + "OPENSYNC",
  23024. + "CLOSESENT",
  23025. + "CLOSERECVD",
  23026. + "CLOSEWAIT",
  23027. + "CLOSED"
  23028. +};
  23029. +
  23030. +static const char *const reason_names[] = {
  23031. + "SERVICE_OPENED",
  23032. + "SERVICE_CLOSED",
  23033. + "MESSAGE_AVAILABLE",
  23034. + "BULK_TRANSMIT_DONE",
  23035. + "BULK_RECEIVE_DONE",
  23036. + "BULK_TRANSMIT_ABORTED",
  23037. + "BULK_RECEIVE_ABORTED"
  23038. +};
  23039. +
  23040. +static const char *const conn_state_names[] = {
  23041. + "DISCONNECTED",
  23042. + "CONNECTING",
  23043. + "CONNECTED",
  23044. + "PAUSING",
  23045. + "PAUSE_SENT",
  23046. + "PAUSED",
  23047. + "RESUMING",
  23048. + "PAUSE_TIMEOUT",
  23049. + "RESUME_TIMEOUT"
  23050. +};
  23051. +
  23052. +
  23053. +static void
  23054. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  23055. +
  23056. +static const char *msg_type_str(unsigned int msg_type)
  23057. +{
  23058. + switch (msg_type) {
  23059. + case VCHIQ_MSG_PADDING: return "PADDING";
  23060. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  23061. + case VCHIQ_MSG_OPEN: return "OPEN";
  23062. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  23063. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  23064. + case VCHIQ_MSG_DATA: return "DATA";
  23065. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  23066. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  23067. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  23068. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  23069. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  23070. + case VCHIQ_MSG_RESUME: return "RESUME";
  23071. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  23072. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  23073. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  23074. + }
  23075. + return "???";
  23076. +}
  23077. +
  23078. +static inline void
  23079. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  23080. +{
  23081. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  23082. + service->state->id, service->localport,
  23083. + srvstate_names[service->srvstate],
  23084. + srvstate_names[newstate]);
  23085. + service->srvstate = newstate;
  23086. +}
  23087. +
  23088. +VCHIQ_SERVICE_T *
  23089. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  23090. +{
  23091. + VCHIQ_SERVICE_T *service;
  23092. +
  23093. + spin_lock(&service_spinlock);
  23094. + service = handle_to_service(handle);
  23095. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23096. + (service->handle == handle)) {
  23097. + BUG_ON(service->ref_count == 0);
  23098. + service->ref_count++;
  23099. + } else
  23100. + service = NULL;
  23101. + spin_unlock(&service_spinlock);
  23102. +
  23103. + if (!service)
  23104. + vchiq_log_info(vchiq_core_log_level,
  23105. + "Invalid service handle 0x%x", handle);
  23106. +
  23107. + return service;
  23108. +}
  23109. +
  23110. +VCHIQ_SERVICE_T *
  23111. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  23112. +{
  23113. + VCHIQ_SERVICE_T *service = NULL;
  23114. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  23115. + spin_lock(&service_spinlock);
  23116. + service = state->services[localport];
  23117. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  23118. + BUG_ON(service->ref_count == 0);
  23119. + service->ref_count++;
  23120. + } else
  23121. + service = NULL;
  23122. + spin_unlock(&service_spinlock);
  23123. + }
  23124. +
  23125. + if (!service)
  23126. + vchiq_log_info(vchiq_core_log_level,
  23127. + "Invalid port %d", localport);
  23128. +
  23129. + return service;
  23130. +}
  23131. +
  23132. +VCHIQ_SERVICE_T *
  23133. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  23134. + VCHIQ_SERVICE_HANDLE_T handle) {
  23135. + VCHIQ_SERVICE_T *service;
  23136. +
  23137. + spin_lock(&service_spinlock);
  23138. + service = handle_to_service(handle);
  23139. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23140. + (service->handle == handle) &&
  23141. + (service->instance == instance)) {
  23142. + BUG_ON(service->ref_count == 0);
  23143. + service->ref_count++;
  23144. + } else
  23145. + service = NULL;
  23146. + spin_unlock(&service_spinlock);
  23147. +
  23148. + if (!service)
  23149. + vchiq_log_info(vchiq_core_log_level,
  23150. + "Invalid service handle 0x%x", handle);
  23151. +
  23152. + return service;
  23153. +}
  23154. +
  23155. +VCHIQ_SERVICE_T *
  23156. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  23157. + int *pidx)
  23158. +{
  23159. + VCHIQ_SERVICE_T *service = NULL;
  23160. + int idx = *pidx;
  23161. +
  23162. + spin_lock(&service_spinlock);
  23163. + while (idx < state->unused_service) {
  23164. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  23165. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  23166. + (srv->instance == instance)) {
  23167. + service = srv;
  23168. + BUG_ON(service->ref_count == 0);
  23169. + service->ref_count++;
  23170. + break;
  23171. + }
  23172. + }
  23173. + spin_unlock(&service_spinlock);
  23174. +
  23175. + *pidx = idx;
  23176. +
  23177. + return service;
  23178. +}
  23179. +
  23180. +void
  23181. +lock_service(VCHIQ_SERVICE_T *service)
  23182. +{
  23183. + spin_lock(&service_spinlock);
  23184. + BUG_ON(!service || (service->ref_count == 0));
  23185. + if (service)
  23186. + service->ref_count++;
  23187. + spin_unlock(&service_spinlock);
  23188. +}
  23189. +
  23190. +void
  23191. +unlock_service(VCHIQ_SERVICE_T *service)
  23192. +{
  23193. + VCHIQ_STATE_T *state = service->state;
  23194. + spin_lock(&service_spinlock);
  23195. + BUG_ON(!service || (service->ref_count == 0));
  23196. + if (service && service->ref_count) {
  23197. + service->ref_count--;
  23198. + if (!service->ref_count) {
  23199. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  23200. + state->services[service->localport] = NULL;
  23201. + } else
  23202. + service = NULL;
  23203. + }
  23204. + spin_unlock(&service_spinlock);
  23205. +
  23206. + if (service && service->userdata_term)
  23207. + service->userdata_term(service->base.userdata);
  23208. +
  23209. + kfree(service);
  23210. +}
  23211. +
  23212. +int
  23213. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  23214. +{
  23215. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  23216. + int id;
  23217. +
  23218. + id = service ? service->client_id : 0;
  23219. + if (service)
  23220. + unlock_service(service);
  23221. +
  23222. + return id;
  23223. +}
  23224. +
  23225. +void *
  23226. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  23227. +{
  23228. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  23229. +
  23230. + return service ? service->base.userdata : NULL;
  23231. +}
  23232. +
  23233. +int
  23234. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  23235. +{
  23236. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  23237. +
  23238. + return service ? service->base.fourcc : 0;
  23239. +}
  23240. +
  23241. +static void
  23242. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  23243. +{
  23244. + VCHIQ_STATE_T *state = service->state;
  23245. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  23246. +
  23247. + service->closing = 1;
  23248. +
  23249. + /* Synchronise with other threads. */
  23250. + mutex_lock(&state->recycle_mutex);
  23251. + mutex_unlock(&state->recycle_mutex);
  23252. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  23253. + /* If we're pausing then the slot_mutex is held until resume
  23254. + * by the slot handler. Therefore don't try to acquire this
  23255. + * mutex if we're the slot handler and in the pause sent state.
  23256. + * We don't need to in this case anyway. */
  23257. + mutex_lock(&state->slot_mutex);
  23258. + mutex_unlock(&state->slot_mutex);
  23259. + }
  23260. +
  23261. + /* Unblock any sending thread. */
  23262. + service_quota = &state->service_quotas[service->localport];
  23263. + up(&service_quota->quota_event);
  23264. +}
  23265. +
  23266. +static void
  23267. +mark_service_closing(VCHIQ_SERVICE_T *service)
  23268. +{
  23269. + mark_service_closing_internal(service, 0);
  23270. +}
  23271. +
  23272. +static inline VCHIQ_STATUS_T
  23273. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  23274. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  23275. +{
  23276. + VCHIQ_STATUS_T status;
  23277. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  23278. + service->state->id, service->localport, reason_names[reason],
  23279. + (unsigned int)header, (unsigned int)bulk_userdata);
  23280. + status = service->base.callback(reason, header, service->handle,
  23281. + bulk_userdata);
  23282. + if (status == VCHIQ_ERROR) {
  23283. + vchiq_log_warning(vchiq_core_log_level,
  23284. + "%d: ignoring ERROR from callback to service %x",
  23285. + service->state->id, service->handle);
  23286. + status = VCHIQ_SUCCESS;
  23287. + }
  23288. + return status;
  23289. +}
  23290. +
  23291. +inline void
  23292. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  23293. +{
  23294. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  23295. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  23296. + conn_state_names[oldstate],
  23297. + conn_state_names[newstate]);
  23298. + state->conn_state = newstate;
  23299. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  23300. +}
  23301. +
  23302. +static inline void
  23303. +remote_event_create(REMOTE_EVENT_T *event)
  23304. +{
  23305. + event->armed = 0;
  23306. + /* Don't clear the 'fired' flag because it may already have been set
  23307. + ** by the other side. */
  23308. + sema_init(event->event, 0);
  23309. +}
  23310. +
  23311. +static inline void
  23312. +remote_event_destroy(REMOTE_EVENT_T *event)
  23313. +{
  23314. + (void)event;
  23315. +}
  23316. +
  23317. +static inline int
  23318. +remote_event_wait(REMOTE_EVENT_T *event)
  23319. +{
  23320. + if (!event->fired) {
  23321. + event->armed = 1;
  23322. + dsb();
  23323. + if (!event->fired) {
  23324. + if (down_interruptible(event->event) != 0) {
  23325. + event->armed = 0;
  23326. + return 0;
  23327. + }
  23328. + }
  23329. + event->armed = 0;
  23330. + wmb();
  23331. + }
  23332. +
  23333. + event->fired = 0;
  23334. + return 1;
  23335. +}
  23336. +
  23337. +static inline void
  23338. +remote_event_signal_local(REMOTE_EVENT_T *event)
  23339. +{
  23340. + event->armed = 0;
  23341. + up(event->event);
  23342. +}
  23343. +
  23344. +static inline void
  23345. +remote_event_poll(REMOTE_EVENT_T *event)
  23346. +{
  23347. + if (event->fired && event->armed)
  23348. + remote_event_signal_local(event);
  23349. +}
  23350. +
  23351. +void
  23352. +remote_event_pollall(VCHIQ_STATE_T *state)
  23353. +{
  23354. + remote_event_poll(&state->local->sync_trigger);
  23355. + remote_event_poll(&state->local->sync_release);
  23356. + remote_event_poll(&state->local->trigger);
  23357. + remote_event_poll(&state->local->recycle);
  23358. +}
  23359. +
  23360. +/* Round up message sizes so that any space at the end of a slot is always big
  23361. +** enough for a header. This relies on header size being a power of two, which
  23362. +** has been verified earlier by a static assertion. */
  23363. +
  23364. +static inline unsigned int
  23365. +calc_stride(unsigned int size)
  23366. +{
  23367. + /* Allow room for the header */
  23368. + size += sizeof(VCHIQ_HEADER_T);
  23369. +
  23370. + /* Round up */
  23371. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  23372. + - 1);
  23373. +}
  23374. +
  23375. +/* Called by the slot handler thread */
  23376. +static VCHIQ_SERVICE_T *
  23377. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  23378. +{
  23379. + int i;
  23380. +
  23381. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  23382. +
  23383. + for (i = 0; i < state->unused_service; i++) {
  23384. + VCHIQ_SERVICE_T *service = state->services[i];
  23385. + if (service &&
  23386. + (service->public_fourcc == fourcc) &&
  23387. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  23388. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  23389. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  23390. + lock_service(service);
  23391. + return service;
  23392. + }
  23393. + }
  23394. +
  23395. + return NULL;
  23396. +}
  23397. +
  23398. +/* Called by the slot handler thread */
  23399. +static VCHIQ_SERVICE_T *
  23400. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  23401. +{
  23402. + int i;
  23403. + for (i = 0; i < state->unused_service; i++) {
  23404. + VCHIQ_SERVICE_T *service = state->services[i];
  23405. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  23406. + && (service->remoteport == port)) {
  23407. + lock_service(service);
  23408. + return service;
  23409. + }
  23410. + }
  23411. + return NULL;
  23412. +}
  23413. +
  23414. +inline void
  23415. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  23416. +{
  23417. + uint32_t value;
  23418. +
  23419. + if (service) {
  23420. + do {
  23421. + value = atomic_read(&service->poll_flags);
  23422. + } while (atomic_cmpxchg(&service->poll_flags, value,
  23423. + value | (1 << poll_type)) != value);
  23424. +
  23425. + do {
  23426. + value = atomic_read(&state->poll_services[
  23427. + service->localport>>5]);
  23428. + } while (atomic_cmpxchg(
  23429. + &state->poll_services[service->localport>>5],
  23430. + value, value | (1 << (service->localport & 0x1f)))
  23431. + != value);
  23432. + }
  23433. +
  23434. + state->poll_needed = 1;
  23435. + wmb();
  23436. +
  23437. + /* ... and ensure the slot handler runs. */
  23438. + remote_event_signal_local(&state->local->trigger);
  23439. +}
  23440. +
  23441. +/* Called from queue_message, by the slot handler and application threads,
  23442. +** with slot_mutex held */
  23443. +static VCHIQ_HEADER_T *
  23444. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  23445. +{
  23446. + VCHIQ_SHARED_STATE_T *local = state->local;
  23447. + int tx_pos = state->local_tx_pos;
  23448. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  23449. +
  23450. + if (space > slot_space) {
  23451. + VCHIQ_HEADER_T *header;
  23452. + /* Fill the remaining space with padding */
  23453. + WARN_ON(state->tx_data == NULL);
  23454. + header = (VCHIQ_HEADER_T *)
  23455. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  23456. + header->msgid = VCHIQ_MSGID_PADDING;
  23457. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  23458. +
  23459. + tx_pos += slot_space;
  23460. + }
  23461. +
  23462. + /* If necessary, get the next slot. */
  23463. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  23464. + int slot_index;
  23465. +
  23466. + /* If there is no free slot... */
  23467. +
  23468. + if (down_trylock(&state->slot_available_event) != 0) {
  23469. + /* ...wait for one. */
  23470. +
  23471. + VCHIQ_STATS_INC(state, slot_stalls);
  23472. +
  23473. + /* But first, flush through the last slot. */
  23474. + state->local_tx_pos = tx_pos;
  23475. + local->tx_pos = tx_pos;
  23476. + remote_event_signal(&state->remote->trigger);
  23477. +
  23478. + if (!is_blocking ||
  23479. + (down_interruptible(
  23480. + &state->slot_available_event) != 0))
  23481. + return NULL; /* No space available */
  23482. + }
  23483. +
  23484. + BUG_ON(tx_pos ==
  23485. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  23486. +
  23487. + slot_index = local->slot_queue[
  23488. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  23489. + VCHIQ_SLOT_QUEUE_MASK];
  23490. + state->tx_data =
  23491. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  23492. + }
  23493. +
  23494. + state->local_tx_pos = tx_pos + space;
  23495. +
  23496. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  23497. +}
  23498. +
  23499. +/* Called by the recycle thread. */
  23500. +static void
  23501. +process_free_queue(VCHIQ_STATE_T *state)
  23502. +{
  23503. + VCHIQ_SHARED_STATE_T *local = state->local;
  23504. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  23505. + int slot_queue_available;
  23506. +
  23507. + /* Use a read memory barrier to ensure that any state that may have
  23508. + ** been modified by another thread is not masked by stale prefetched
  23509. + ** values. */
  23510. + rmb();
  23511. +
  23512. + /* Find slots which have been freed by the other side, and return them
  23513. + ** to the available queue. */
  23514. + slot_queue_available = state->slot_queue_available;
  23515. +
  23516. + while (slot_queue_available != local->slot_queue_recycle) {
  23517. + unsigned int pos;
  23518. + int slot_index = local->slot_queue[slot_queue_available++ &
  23519. + VCHIQ_SLOT_QUEUE_MASK];
  23520. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  23521. + int data_found = 0;
  23522. +
  23523. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  23524. + state->id, slot_index, (unsigned int)data,
  23525. + local->slot_queue_recycle, slot_queue_available);
  23526. +
  23527. + /* Initialise the bitmask for services which have used this
  23528. + ** slot */
  23529. + BITSET_ZERO(service_found);
  23530. +
  23531. + pos = 0;
  23532. +
  23533. + while (pos < VCHIQ_SLOT_SIZE) {
  23534. + VCHIQ_HEADER_T *header =
  23535. + (VCHIQ_HEADER_T *)(data + pos);
  23536. + int msgid = header->msgid;
  23537. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  23538. + int port = VCHIQ_MSG_SRCPORT(msgid);
  23539. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  23540. + &state->service_quotas[port];
  23541. + int count;
  23542. + spin_lock(&quota_spinlock);
  23543. + count = service_quota->message_use_count;
  23544. + if (count > 0)
  23545. + service_quota->message_use_count =
  23546. + count - 1;
  23547. + spin_unlock(&quota_spinlock);
  23548. +
  23549. + if (count == service_quota->message_quota)
  23550. + /* Signal the service that it
  23551. + ** has dropped below its quota
  23552. + */
  23553. + up(&service_quota->quota_event);
  23554. + else if (count == 0) {
  23555. + vchiq_log_error(vchiq_core_log_level,
  23556. + "service %d "
  23557. + "message_use_count=%d "
  23558. + "(header %x, msgid %x, "
  23559. + "header->msgid %x, "
  23560. + "header->size %x)",
  23561. + port,
  23562. + service_quota->
  23563. + message_use_count,
  23564. + (unsigned int)header, msgid,
  23565. + header->msgid,
  23566. + header->size);
  23567. + WARN(1, "invalid message use count\n");
  23568. + }
  23569. + if (!BITSET_IS_SET(service_found, port)) {
  23570. + /* Set the found bit for this service */
  23571. + BITSET_SET(service_found, port);
  23572. +
  23573. + spin_lock(&quota_spinlock);
  23574. + count = service_quota->slot_use_count;
  23575. + if (count > 0)
  23576. + service_quota->slot_use_count =
  23577. + count - 1;
  23578. + spin_unlock(&quota_spinlock);
  23579. +
  23580. + if (count > 0) {
  23581. + /* Signal the service in case
  23582. + ** it has dropped below its
  23583. + ** quota */
  23584. + up(&service_quota->quota_event);
  23585. + vchiq_log_trace(
  23586. + vchiq_core_log_level,
  23587. + "%d: pfq:%d %x@%x - "
  23588. + "slot_use->%d",
  23589. + state->id, port,
  23590. + header->size,
  23591. + (unsigned int)header,
  23592. + count - 1);
  23593. + } else {
  23594. + vchiq_log_error(
  23595. + vchiq_core_log_level,
  23596. + "service %d "
  23597. + "slot_use_count"
  23598. + "=%d (header %x"
  23599. + ", msgid %x, "
  23600. + "header->msgid"
  23601. + " %x, header->"
  23602. + "size %x)",
  23603. + port, count,
  23604. + (unsigned int)header,
  23605. + msgid,
  23606. + header->msgid,
  23607. + header->size);
  23608. + WARN(1, "bad slot use count\n");
  23609. + }
  23610. + }
  23611. +
  23612. + data_found = 1;
  23613. + }
  23614. +
  23615. + pos += calc_stride(header->size);
  23616. + if (pos > VCHIQ_SLOT_SIZE) {
  23617. + vchiq_log_error(vchiq_core_log_level,
  23618. + "pfq - pos %x: header %x, msgid %x, "
  23619. + "header->msgid %x, header->size %x",
  23620. + pos, (unsigned int)header, msgid,
  23621. + header->msgid, header->size);
  23622. + WARN(1, "invalid slot position\n");
  23623. + }
  23624. + }
  23625. +
  23626. + if (data_found) {
  23627. + int count;
  23628. + spin_lock(&quota_spinlock);
  23629. + count = state->data_use_count;
  23630. + if (count > 0)
  23631. + state->data_use_count =
  23632. + count - 1;
  23633. + spin_unlock(&quota_spinlock);
  23634. + if (count == state->data_quota)
  23635. + up(&state->data_quota_event);
  23636. + }
  23637. +
  23638. + state->slot_queue_available = slot_queue_available;
  23639. + up(&state->slot_available_event);
  23640. + }
  23641. +}
  23642. +
  23643. +/* Called by the slot handler and application threads */
  23644. +static VCHIQ_STATUS_T
  23645. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  23646. + int msgid, const VCHIQ_ELEMENT_T *elements,
  23647. + int count, int size, int is_blocking)
  23648. +{
  23649. + VCHIQ_SHARED_STATE_T *local;
  23650. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  23651. + VCHIQ_HEADER_T *header;
  23652. + int type = VCHIQ_MSG_TYPE(msgid);
  23653. +
  23654. + unsigned int stride;
  23655. +
  23656. + local = state->local;
  23657. +
  23658. + stride = calc_stride(size);
  23659. +
  23660. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  23661. +
  23662. + if ((type != VCHIQ_MSG_RESUME) &&
  23663. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  23664. + return VCHIQ_RETRY;
  23665. +
  23666. + if (type == VCHIQ_MSG_DATA) {
  23667. + int tx_end_index;
  23668. +
  23669. + BUG_ON(!service);
  23670. +
  23671. + if (service->closing) {
  23672. + /* The service has been closed */
  23673. + mutex_unlock(&state->slot_mutex);
  23674. + return VCHIQ_ERROR;
  23675. + }
  23676. +
  23677. + service_quota = &state->service_quotas[service->localport];
  23678. +
  23679. + spin_lock(&quota_spinlock);
  23680. +
  23681. + /* Ensure this service doesn't use more than its quota of
  23682. + ** messages or slots */
  23683. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  23684. + state->local_tx_pos + stride - 1);
  23685. +
  23686. + /* Ensure data messages don't use more than their quota of
  23687. + ** slots */
  23688. + while ((tx_end_index != state->previous_data_index) &&
  23689. + (state->data_use_count == state->data_quota)) {
  23690. + VCHIQ_STATS_INC(state, data_stalls);
  23691. + spin_unlock(&quota_spinlock);
  23692. + mutex_unlock(&state->slot_mutex);
  23693. +
  23694. + if (down_interruptible(&state->data_quota_event)
  23695. + != 0)
  23696. + return VCHIQ_RETRY;
  23697. +
  23698. + mutex_lock(&state->slot_mutex);
  23699. + spin_lock(&quota_spinlock);
  23700. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  23701. + state->local_tx_pos + stride - 1);
  23702. + if ((tx_end_index == state->previous_data_index) ||
  23703. + (state->data_use_count < state->data_quota)) {
  23704. + /* Pass the signal on to other waiters */
  23705. + up(&state->data_quota_event);
  23706. + break;
  23707. + }
  23708. + }
  23709. +
  23710. + while ((service_quota->message_use_count ==
  23711. + service_quota->message_quota) ||
  23712. + ((tx_end_index != service_quota->previous_tx_index) &&
  23713. + (service_quota->slot_use_count ==
  23714. + service_quota->slot_quota))) {
  23715. + spin_unlock(&quota_spinlock);
  23716. + vchiq_log_trace(vchiq_core_log_level,
  23717. + "%d: qm:%d %s,%x - quota stall "
  23718. + "(msg %d, slot %d)",
  23719. + state->id, service->localport,
  23720. + msg_type_str(type), size,
  23721. + service_quota->message_use_count,
  23722. + service_quota->slot_use_count);
  23723. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  23724. + mutex_unlock(&state->slot_mutex);
  23725. + if (down_interruptible(&service_quota->quota_event)
  23726. + != 0)
  23727. + return VCHIQ_RETRY;
  23728. + if (service->closing)
  23729. + return VCHIQ_ERROR;
  23730. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  23731. + return VCHIQ_RETRY;
  23732. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  23733. + /* The service has been closed */
  23734. + mutex_unlock(&state->slot_mutex);
  23735. + return VCHIQ_ERROR;
  23736. + }
  23737. + spin_lock(&quota_spinlock);
  23738. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  23739. + state->local_tx_pos + stride - 1);
  23740. + }
  23741. +
  23742. + spin_unlock(&quota_spinlock);
  23743. + }
  23744. +
  23745. + header = reserve_space(state, stride, is_blocking);
  23746. +
  23747. + if (!header) {
  23748. + if (service)
  23749. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  23750. + mutex_unlock(&state->slot_mutex);
  23751. + return VCHIQ_RETRY;
  23752. + }
  23753. +
  23754. + if (type == VCHIQ_MSG_DATA) {
  23755. + int i, pos;
  23756. + int tx_end_index;
  23757. + int slot_use_count;
  23758. +
  23759. + vchiq_log_info(vchiq_core_log_level,
  23760. + "%d: qm %s@%x,%x (%d->%d)",
  23761. + state->id,
  23762. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  23763. + (unsigned int)header, size,
  23764. + VCHIQ_MSG_SRCPORT(msgid),
  23765. + VCHIQ_MSG_DSTPORT(msgid));
  23766. +
  23767. + BUG_ON(!service);
  23768. +
  23769. + for (i = 0, pos = 0; i < (unsigned int)count;
  23770. + pos += elements[i++].size)
  23771. + if (elements[i].size) {
  23772. + if (vchiq_copy_from_user
  23773. + (header->data + pos, elements[i].data,
  23774. + (size_t) elements[i].size) !=
  23775. + VCHIQ_SUCCESS) {
  23776. + mutex_unlock(&state->slot_mutex);
  23777. + VCHIQ_SERVICE_STATS_INC(service,
  23778. + error_count);
  23779. + return VCHIQ_ERROR;
  23780. + }
  23781. + if (i == 0) {
  23782. + if (vchiq_core_msg_log_level >=
  23783. + VCHIQ_LOG_INFO)
  23784. + vchiq_log_dump_mem("Sent", 0,
  23785. + header->data + pos,
  23786. + min(64u,
  23787. + elements[0].size));
  23788. + }
  23789. + }
  23790. +
  23791. + spin_lock(&quota_spinlock);
  23792. + service_quota->message_use_count++;
  23793. +
  23794. + tx_end_index =
  23795. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  23796. +
  23797. + /* If this transmission can't fit in the last slot used by any
  23798. + ** service, the data_use_count must be increased. */
  23799. + if (tx_end_index != state->previous_data_index) {
  23800. + state->previous_data_index = tx_end_index;
  23801. + state->data_use_count++;
  23802. + }
  23803. +
  23804. + /* If this isn't the same slot last used by this service,
  23805. + ** the service's slot_use_count must be increased. */
  23806. + if (tx_end_index != service_quota->previous_tx_index) {
  23807. + service_quota->previous_tx_index = tx_end_index;
  23808. + slot_use_count = ++service_quota->slot_use_count;
  23809. + } else {
  23810. + slot_use_count = 0;
  23811. + }
  23812. +
  23813. + spin_unlock(&quota_spinlock);
  23814. +
  23815. + if (slot_use_count)
  23816. + vchiq_log_trace(vchiq_core_log_level,
  23817. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  23818. + state->id, service->localport,
  23819. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  23820. + slot_use_count, header);
  23821. +
  23822. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  23823. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  23824. + } else {
  23825. + vchiq_log_info(vchiq_core_log_level,
  23826. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  23827. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  23828. + (unsigned int)header, size,
  23829. + VCHIQ_MSG_SRCPORT(msgid),
  23830. + VCHIQ_MSG_DSTPORT(msgid));
  23831. + if (size != 0) {
  23832. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  23833. + memcpy(header->data, elements[0].data,
  23834. + elements[0].size);
  23835. + }
  23836. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  23837. + }
  23838. +
  23839. + header->msgid = msgid;
  23840. + header->size = size;
  23841. +
  23842. + {
  23843. + int svc_fourcc;
  23844. +
  23845. + svc_fourcc = service
  23846. + ? service->base.fourcc
  23847. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  23848. +
  23849. + vchiq_log_info(vchiq_core_msg_log_level,
  23850. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  23851. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  23852. + VCHIQ_MSG_TYPE(msgid),
  23853. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  23854. + VCHIQ_MSG_SRCPORT(msgid),
  23855. + VCHIQ_MSG_DSTPORT(msgid),
  23856. + size);
  23857. + }
  23858. +
  23859. + /* Make sure the new header is visible to the peer. */
  23860. + wmb();
  23861. +
  23862. + /* Make the new tx_pos visible to the peer. */
  23863. + local->tx_pos = state->local_tx_pos;
  23864. + wmb();
  23865. +
  23866. + if (service && (type == VCHIQ_MSG_CLOSE))
  23867. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  23868. +
  23869. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  23870. + mutex_unlock(&state->slot_mutex);
  23871. +
  23872. + remote_event_signal(&state->remote->trigger);
  23873. +
  23874. + return VCHIQ_SUCCESS;
  23875. +}
  23876. +
  23877. +/* Called by the slot handler and application threads */
  23878. +static VCHIQ_STATUS_T
  23879. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  23880. + int msgid, const VCHIQ_ELEMENT_T *elements,
  23881. + int count, int size, int is_blocking)
  23882. +{
  23883. + VCHIQ_SHARED_STATE_T *local;
  23884. + VCHIQ_HEADER_T *header;
  23885. +
  23886. + local = state->local;
  23887. +
  23888. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  23889. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  23890. + return VCHIQ_RETRY;
  23891. +
  23892. + remote_event_wait(&local->sync_release);
  23893. +
  23894. + rmb();
  23895. +
  23896. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  23897. + local->slot_sync);
  23898. +
  23899. + {
  23900. + int oldmsgid = header->msgid;
  23901. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  23902. + vchiq_log_error(vchiq_core_log_level,
  23903. + "%d: qms - msgid %x, not PADDING",
  23904. + state->id, oldmsgid);
  23905. + }
  23906. +
  23907. + if (service) {
  23908. + int i, pos;
  23909. +
  23910. + vchiq_log_info(vchiq_sync_log_level,
  23911. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  23912. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  23913. + (unsigned int)header, size,
  23914. + VCHIQ_MSG_SRCPORT(msgid),
  23915. + VCHIQ_MSG_DSTPORT(msgid));
  23916. +
  23917. + for (i = 0, pos = 0; i < (unsigned int)count;
  23918. + pos += elements[i++].size)
  23919. + if (elements[i].size) {
  23920. + if (vchiq_copy_from_user
  23921. + (header->data + pos, elements[i].data,
  23922. + (size_t) elements[i].size) !=
  23923. + VCHIQ_SUCCESS) {
  23924. + mutex_unlock(&state->sync_mutex);
  23925. + VCHIQ_SERVICE_STATS_INC(service,
  23926. + error_count);
  23927. + return VCHIQ_ERROR;
  23928. + }
  23929. + if (i == 0) {
  23930. + if (vchiq_sync_log_level >=
  23931. + VCHIQ_LOG_TRACE)
  23932. + vchiq_log_dump_mem("Sent Sync",
  23933. + 0, header->data + pos,
  23934. + min(64u,
  23935. + elements[0].size));
  23936. + }
  23937. + }
  23938. +
  23939. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  23940. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  23941. + } else {
  23942. + vchiq_log_info(vchiq_sync_log_level,
  23943. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  23944. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  23945. + (unsigned int)header, size,
  23946. + VCHIQ_MSG_SRCPORT(msgid),
  23947. + VCHIQ_MSG_DSTPORT(msgid));
  23948. + if (size != 0) {
  23949. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  23950. + memcpy(header->data, elements[0].data,
  23951. + elements[0].size);
  23952. + }
  23953. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  23954. + }
  23955. +
  23956. + header->size = size;
  23957. + header->msgid = msgid;
  23958. +
  23959. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  23960. + int svc_fourcc;
  23961. +
  23962. + svc_fourcc = service
  23963. + ? service->base.fourcc
  23964. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  23965. +
  23966. + vchiq_log_trace(vchiq_sync_log_level,
  23967. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  23968. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  23969. + VCHIQ_MSG_TYPE(msgid),
  23970. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  23971. + VCHIQ_MSG_SRCPORT(msgid),
  23972. + VCHIQ_MSG_DSTPORT(msgid),
  23973. + size);
  23974. + }
  23975. +
  23976. + /* Make sure the new header is visible to the peer. */
  23977. + wmb();
  23978. +
  23979. + remote_event_signal(&state->remote->sync_trigger);
  23980. +
  23981. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  23982. + mutex_unlock(&state->sync_mutex);
  23983. +
  23984. + return VCHIQ_SUCCESS;
  23985. +}
  23986. +
  23987. +static inline void
  23988. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  23989. +{
  23990. + slot->use_count++;
  23991. +}
  23992. +
  23993. +static void
  23994. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  23995. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  23996. +{
  23997. + int release_count;
  23998. +
  23999. + mutex_lock(&state->recycle_mutex);
  24000. +
  24001. + if (header) {
  24002. + int msgid = header->msgid;
  24003. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  24004. + (service && service->closing)) {
  24005. + mutex_unlock(&state->recycle_mutex);
  24006. + return;
  24007. + }
  24008. +
  24009. + /* Rewrite the message header to prevent a double
  24010. + ** release */
  24011. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  24012. + }
  24013. +
  24014. + release_count = slot_info->release_count;
  24015. + slot_info->release_count = ++release_count;
  24016. +
  24017. + if (release_count == slot_info->use_count) {
  24018. + int slot_queue_recycle;
  24019. + /* Add to the freed queue */
  24020. +
  24021. + /* A read barrier is necessary here to prevent speculative
  24022. + ** fetches of remote->slot_queue_recycle from overtaking the
  24023. + ** mutex. */
  24024. + rmb();
  24025. +
  24026. + slot_queue_recycle = state->remote->slot_queue_recycle;
  24027. + state->remote->slot_queue[slot_queue_recycle &
  24028. + VCHIQ_SLOT_QUEUE_MASK] =
  24029. + SLOT_INDEX_FROM_INFO(state, slot_info);
  24030. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  24031. + vchiq_log_info(vchiq_core_log_level,
  24032. + "%d: release_slot %d - recycle->%x",
  24033. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  24034. + state->remote->slot_queue_recycle);
  24035. +
  24036. + /* A write barrier is necessary, but remote_event_signal
  24037. + ** contains one. */
  24038. + remote_event_signal(&state->remote->recycle);
  24039. + }
  24040. +
  24041. + mutex_unlock(&state->recycle_mutex);
  24042. +}
  24043. +
  24044. +/* Called by the slot handler - don't hold the bulk mutex */
  24045. +static VCHIQ_STATUS_T
  24046. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  24047. + int retry_poll)
  24048. +{
  24049. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  24050. +
  24051. + vchiq_log_trace(vchiq_core_log_level,
  24052. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  24053. + service->state->id, service->localport,
  24054. + (queue == &service->bulk_tx) ? 't' : 'r',
  24055. + queue->process, queue->remote_notify, queue->remove);
  24056. +
  24057. + if (service->state->is_master) {
  24058. + while (queue->remote_notify != queue->process) {
  24059. + VCHIQ_BULK_T *bulk =
  24060. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  24061. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  24062. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  24063. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  24064. + service->remoteport);
  24065. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  24066. + /* Only reply to non-dummy bulk requests */
  24067. + if (bulk->remote_data) {
  24068. + status = queue_message(service->state, NULL,
  24069. + msgid, &element, 1, 4, 0);
  24070. + if (status != VCHIQ_SUCCESS)
  24071. + break;
  24072. + }
  24073. + queue->remote_notify++;
  24074. + }
  24075. + } else {
  24076. + queue->remote_notify = queue->process;
  24077. + }
  24078. +
  24079. + if (status == VCHIQ_SUCCESS) {
  24080. + while (queue->remove != queue->remote_notify) {
  24081. + VCHIQ_BULK_T *bulk =
  24082. + &queue->bulks[BULK_INDEX(queue->remove)];
  24083. +
  24084. + /* Only generate callbacks for non-dummy bulk
  24085. + ** requests, and non-terminated services */
  24086. + if (bulk->data && service->instance) {
  24087. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  24088. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  24089. + VCHIQ_SERVICE_STATS_INC(service,
  24090. + bulk_tx_count);
  24091. + VCHIQ_SERVICE_STATS_ADD(service,
  24092. + bulk_tx_bytes,
  24093. + bulk->actual);
  24094. + } else {
  24095. + VCHIQ_SERVICE_STATS_INC(service,
  24096. + bulk_rx_count);
  24097. + VCHIQ_SERVICE_STATS_ADD(service,
  24098. + bulk_rx_bytes,
  24099. + bulk->actual);
  24100. + }
  24101. + } else {
  24102. + VCHIQ_SERVICE_STATS_INC(service,
  24103. + bulk_aborted_count);
  24104. + }
  24105. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  24106. + struct bulk_waiter *waiter;
  24107. + spin_lock(&bulk_waiter_spinlock);
  24108. + waiter = bulk->userdata;
  24109. + if (waiter) {
  24110. + waiter->actual = bulk->actual;
  24111. + up(&waiter->event);
  24112. + }
  24113. + spin_unlock(&bulk_waiter_spinlock);
  24114. + } else if (bulk->mode ==
  24115. + VCHIQ_BULK_MODE_CALLBACK) {
  24116. + VCHIQ_REASON_T reason = (bulk->dir ==
  24117. + VCHIQ_BULK_TRANSMIT) ?
  24118. + ((bulk->actual ==
  24119. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  24120. + VCHIQ_BULK_TRANSMIT_ABORTED :
  24121. + VCHIQ_BULK_TRANSMIT_DONE) :
  24122. + ((bulk->actual ==
  24123. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  24124. + VCHIQ_BULK_RECEIVE_ABORTED :
  24125. + VCHIQ_BULK_RECEIVE_DONE);
  24126. + status = make_service_callback(service,
  24127. + reason, NULL, bulk->userdata);
  24128. + if (status == VCHIQ_RETRY)
  24129. + break;
  24130. + }
  24131. + }
  24132. +
  24133. + queue->remove++;
  24134. + up(&service->bulk_remove_event);
  24135. + }
  24136. + if (!retry_poll)
  24137. + status = VCHIQ_SUCCESS;
  24138. + }
  24139. +
  24140. + if (status == VCHIQ_RETRY)
  24141. + request_poll(service->state, service,
  24142. + (queue == &service->bulk_tx) ?
  24143. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  24144. +
  24145. + return status;
  24146. +}
  24147. +
  24148. +/* Called by the slot handler thread */
  24149. +static void
  24150. +poll_services(VCHIQ_STATE_T *state)
  24151. +{
  24152. + int group, i;
  24153. +
  24154. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  24155. + uint32_t flags;
  24156. + flags = atomic_xchg(&state->poll_services[group], 0);
  24157. + for (i = 0; flags; i++) {
  24158. + if (flags & (1 << i)) {
  24159. + VCHIQ_SERVICE_T *service =
  24160. + find_service_by_port(state,
  24161. + (group<<5) + i);
  24162. + uint32_t service_flags;
  24163. + flags &= ~(1 << i);
  24164. + if (!service)
  24165. + continue;
  24166. + service_flags =
  24167. + atomic_xchg(&service->poll_flags, 0);
  24168. + if (service_flags &
  24169. + (1 << VCHIQ_POLL_REMOVE)) {
  24170. + vchiq_log_info(vchiq_core_log_level,
  24171. + "%d: ps - remove %d<->%d",
  24172. + state->id, service->localport,
  24173. + service->remoteport);
  24174. +
  24175. + /* Make it look like a client, because
  24176. + it must be removed and not left in
  24177. + the LISTENING state. */
  24178. + service->public_fourcc =
  24179. + VCHIQ_FOURCC_INVALID;
  24180. +
  24181. + if (vchiq_close_service_internal(
  24182. + service, 0/*!close_recvd*/) !=
  24183. + VCHIQ_SUCCESS)
  24184. + request_poll(state, service,
  24185. + VCHIQ_POLL_REMOVE);
  24186. + } else if (service_flags &
  24187. + (1 << VCHIQ_POLL_TERMINATE)) {
  24188. + vchiq_log_info(vchiq_core_log_level,
  24189. + "%d: ps - terminate %d<->%d",
  24190. + state->id, service->localport,
  24191. + service->remoteport);
  24192. + if (vchiq_close_service_internal(
  24193. + service, 0/*!close_recvd*/) !=
  24194. + VCHIQ_SUCCESS)
  24195. + request_poll(state, service,
  24196. + VCHIQ_POLL_TERMINATE);
  24197. + }
  24198. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  24199. + notify_bulks(service,
  24200. + &service->bulk_tx,
  24201. + 1/*retry_poll*/);
  24202. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  24203. + notify_bulks(service,
  24204. + &service->bulk_rx,
  24205. + 1/*retry_poll*/);
  24206. + unlock_service(service);
  24207. + }
  24208. + }
  24209. + }
  24210. +}
  24211. +
  24212. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  24213. +static int
  24214. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  24215. +{
  24216. + VCHIQ_STATE_T *state = service->state;
  24217. + int resolved = 0;
  24218. + int rc;
  24219. +
  24220. + while ((queue->process != queue->local_insert) &&
  24221. + (queue->process != queue->remote_insert)) {
  24222. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  24223. +
  24224. + vchiq_log_trace(vchiq_core_log_level,
  24225. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  24226. + state->id, service->localport,
  24227. + (queue == &service->bulk_tx) ? 't' : 'r',
  24228. + queue->local_insert, queue->remote_insert,
  24229. + queue->process);
  24230. +
  24231. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  24232. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  24233. +
  24234. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  24235. + if (rc != 0)
  24236. + break;
  24237. +
  24238. + vchiq_transfer_bulk(bulk);
  24239. + mutex_unlock(&state->bulk_transfer_mutex);
  24240. +
  24241. + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
  24242. + const char *header = (queue == &service->bulk_tx) ?
  24243. + "Send Bulk to" : "Recv Bulk from";
  24244. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  24245. + vchiq_log_info(vchiq_core_msg_log_level,
  24246. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  24247. + header,
  24248. + VCHIQ_FOURCC_AS_4CHARS(
  24249. + service->base.fourcc),
  24250. + service->remoteport,
  24251. + bulk->size,
  24252. + (unsigned int)bulk->data,
  24253. + (unsigned int)bulk->remote_data);
  24254. + else
  24255. + vchiq_log_info(vchiq_core_msg_log_level,
  24256. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  24257. + " rx len:%d %x<->%x",
  24258. + header,
  24259. + VCHIQ_FOURCC_AS_4CHARS(
  24260. + service->base.fourcc),
  24261. + service->remoteport,
  24262. + bulk->size,
  24263. + bulk->remote_size,
  24264. + (unsigned int)bulk->data,
  24265. + (unsigned int)bulk->remote_data);
  24266. + }
  24267. +
  24268. + vchiq_complete_bulk(bulk);
  24269. + queue->process++;
  24270. + resolved++;
  24271. + }
  24272. + return resolved;
  24273. +}
  24274. +
  24275. +/* Called with the bulk_mutex held */
  24276. +static void
  24277. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  24278. +{
  24279. + int is_tx = (queue == &service->bulk_tx);
  24280. + vchiq_log_trace(vchiq_core_log_level,
  24281. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  24282. + service->state->id, service->localport, is_tx ? 't' : 'r',
  24283. + queue->local_insert, queue->remote_insert, queue->process);
  24284. +
  24285. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  24286. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  24287. +
  24288. + while ((queue->process != queue->local_insert) ||
  24289. + (queue->process != queue->remote_insert)) {
  24290. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  24291. +
  24292. + if (queue->process == queue->remote_insert) {
  24293. + /* fabricate a matching dummy bulk */
  24294. + bulk->remote_data = NULL;
  24295. + bulk->remote_size = 0;
  24296. + queue->remote_insert++;
  24297. + }
  24298. +
  24299. + if (queue->process != queue->local_insert) {
  24300. + vchiq_complete_bulk(bulk);
  24301. +
  24302. + vchiq_log_info(vchiq_core_msg_log_level,
  24303. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  24304. + "rx len:%d",
  24305. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  24306. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  24307. + service->remoteport,
  24308. + bulk->size,
  24309. + bulk->remote_size);
  24310. + } else {
  24311. + /* fabricate a matching dummy bulk */
  24312. + bulk->data = NULL;
  24313. + bulk->size = 0;
  24314. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  24315. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  24316. + VCHIQ_BULK_RECEIVE;
  24317. + queue->local_insert++;
  24318. + }
  24319. +
  24320. + queue->process++;
  24321. + }
  24322. +}
  24323. +
  24324. +/* Called from the slot handler thread */
  24325. +static void
  24326. +pause_bulks(VCHIQ_STATE_T *state)
  24327. +{
  24328. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  24329. + WARN_ON_ONCE(1);
  24330. + atomic_set(&pause_bulks_count, 1);
  24331. + return;
  24332. + }
  24333. +
  24334. + /* Block bulk transfers from all services */
  24335. + mutex_lock(&state->bulk_transfer_mutex);
  24336. +}
  24337. +
  24338. +/* Called from the slot handler thread */
  24339. +static void
  24340. +resume_bulks(VCHIQ_STATE_T *state)
  24341. +{
  24342. + int i;
  24343. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  24344. + WARN_ON_ONCE(1);
  24345. + atomic_set(&pause_bulks_count, 0);
  24346. + return;
  24347. + }
  24348. +
  24349. + /* Allow bulk transfers from all services */
  24350. + mutex_unlock(&state->bulk_transfer_mutex);
  24351. +
  24352. + if (state->deferred_bulks == 0)
  24353. + return;
  24354. +
  24355. + /* Deal with any bulks which had to be deferred due to being in
  24356. + * paused state. Don't try to match up to number of deferred bulks
  24357. + * in case we've had something come and close the service in the
  24358. + * interim - just process all bulk queues for all services */
  24359. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  24360. + __func__, state->deferred_bulks);
  24361. +
  24362. + for (i = 0; i < state->unused_service; i++) {
  24363. + VCHIQ_SERVICE_T *service = state->services[i];
  24364. + int resolved_rx = 0;
  24365. + int resolved_tx = 0;
  24366. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  24367. + continue;
  24368. +
  24369. + mutex_lock(&service->bulk_mutex);
  24370. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  24371. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  24372. + mutex_unlock(&service->bulk_mutex);
  24373. + if (resolved_rx)
  24374. + notify_bulks(service, &service->bulk_rx, 1);
  24375. + if (resolved_tx)
  24376. + notify_bulks(service, &service->bulk_tx, 1);
  24377. + }
  24378. + state->deferred_bulks = 0;
  24379. +}
  24380. +
  24381. +static int
  24382. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  24383. +{
  24384. + VCHIQ_SERVICE_T *service = NULL;
  24385. + int msgid, size;
  24386. + int type;
  24387. + unsigned int localport, remoteport;
  24388. +
  24389. + msgid = header->msgid;
  24390. + size = header->size;
  24391. + type = VCHIQ_MSG_TYPE(msgid);
  24392. + localport = VCHIQ_MSG_DSTPORT(msgid);
  24393. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  24394. + if (size >= sizeof(struct vchiq_open_payload)) {
  24395. + const struct vchiq_open_payload *payload =
  24396. + (struct vchiq_open_payload *)header->data;
  24397. + unsigned int fourcc;
  24398. +
  24399. + fourcc = payload->fourcc;
  24400. + vchiq_log_info(vchiq_core_log_level,
  24401. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  24402. + state->id, (unsigned int)header,
  24403. + localport,
  24404. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  24405. +
  24406. + service = get_listening_service(state, fourcc);
  24407. +
  24408. + if (service) {
  24409. + /* A matching service exists */
  24410. + short version = payload->version;
  24411. + short version_min = payload->version_min;
  24412. + if ((service->version < version_min) ||
  24413. + (version < service->version_min)) {
  24414. + /* Version mismatch */
  24415. + vchiq_loud_error_header();
  24416. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  24417. + "version mismatch - local (%d, min %d)"
  24418. + " vs. remote (%d, min %d)",
  24419. + state->id, service->localport,
  24420. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  24421. + service->version, service->version_min,
  24422. + version, version_min);
  24423. + vchiq_loud_error_footer();
  24424. + unlock_service(service);
  24425. + service = NULL;
  24426. + goto fail_open;
  24427. + }
  24428. + service->peer_version = version;
  24429. +
  24430. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  24431. + struct vchiq_openack_payload ack_payload = {
  24432. + service->version
  24433. + };
  24434. + VCHIQ_ELEMENT_T body = {
  24435. + &ack_payload,
  24436. + sizeof(ack_payload)
  24437. + };
  24438. +
  24439. + /* Acknowledge the OPEN */
  24440. + if (service->sync) {
  24441. + if (queue_message_sync(state, NULL,
  24442. + VCHIQ_MAKE_MSG(
  24443. + VCHIQ_MSG_OPENACK,
  24444. + service->localport,
  24445. + remoteport),
  24446. + &body, 1, sizeof(ack_payload),
  24447. + 0) == VCHIQ_RETRY)
  24448. + goto bail_not_ready;
  24449. + } else {
  24450. + if (queue_message(state, NULL,
  24451. + VCHIQ_MAKE_MSG(
  24452. + VCHIQ_MSG_OPENACK,
  24453. + service->localport,
  24454. + remoteport),
  24455. + &body, 1, sizeof(ack_payload),
  24456. + 0) == VCHIQ_RETRY)
  24457. + goto bail_not_ready;
  24458. + }
  24459. +
  24460. + /* The service is now open */
  24461. + vchiq_set_service_state(service,
  24462. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  24463. + : VCHIQ_SRVSTATE_OPEN);
  24464. + }
  24465. +
  24466. + service->remoteport = remoteport;
  24467. + service->client_id = ((int *)header->data)[1];
  24468. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  24469. + NULL, NULL) == VCHIQ_RETRY) {
  24470. + /* Bail out if not ready */
  24471. + service->remoteport = VCHIQ_PORT_FREE;
  24472. + goto bail_not_ready;
  24473. + }
  24474. +
  24475. + /* Success - the message has been dealt with */
  24476. + unlock_service(service);
  24477. + return 1;
  24478. + }
  24479. + }
  24480. +
  24481. +fail_open:
  24482. + /* No available service, or an invalid request - send a CLOSE */
  24483. + if (queue_message(state, NULL,
  24484. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  24485. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  24486. + goto bail_not_ready;
  24487. +
  24488. + return 1;
  24489. +
  24490. +bail_not_ready:
  24491. + if (service)
  24492. + unlock_service(service);
  24493. +
  24494. + return 0;
  24495. +}
  24496. +
  24497. +/* Called by the slot handler thread */
  24498. +static void
  24499. +parse_rx_slots(VCHIQ_STATE_T *state)
  24500. +{
  24501. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  24502. + VCHIQ_SERVICE_T *service = NULL;
  24503. + int tx_pos;
  24504. + DEBUG_INITIALISE(state->local)
  24505. +
  24506. + tx_pos = remote->tx_pos;
  24507. +
  24508. + while (state->rx_pos != tx_pos) {
  24509. + VCHIQ_HEADER_T *header;
  24510. + int msgid, size;
  24511. + int type;
  24512. + unsigned int localport, remoteport;
  24513. +
  24514. + DEBUG_TRACE(PARSE_LINE);
  24515. + if (!state->rx_data) {
  24516. + int rx_index;
  24517. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  24518. + rx_index = remote->slot_queue[
  24519. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  24520. + VCHIQ_SLOT_QUEUE_MASK];
  24521. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  24522. + rx_index);
  24523. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  24524. +
  24525. + /* Initialise use_count to one, and increment
  24526. + ** release_count at the end of the slot to avoid
  24527. + ** releasing the slot prematurely. */
  24528. + state->rx_info->use_count = 1;
  24529. + state->rx_info->release_count = 0;
  24530. + }
  24531. +
  24532. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  24533. + (state->rx_pos & VCHIQ_SLOT_MASK));
  24534. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  24535. + msgid = header->msgid;
  24536. + DEBUG_VALUE(PARSE_MSGID, msgid);
  24537. + size = header->size;
  24538. + type = VCHIQ_MSG_TYPE(msgid);
  24539. + localport = VCHIQ_MSG_DSTPORT(msgid);
  24540. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  24541. +
  24542. + if (type != VCHIQ_MSG_DATA)
  24543. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  24544. +
  24545. + switch (type) {
  24546. + case VCHIQ_MSG_OPENACK:
  24547. + case VCHIQ_MSG_CLOSE:
  24548. + case VCHIQ_MSG_DATA:
  24549. + case VCHIQ_MSG_BULK_RX:
  24550. + case VCHIQ_MSG_BULK_TX:
  24551. + case VCHIQ_MSG_BULK_RX_DONE:
  24552. + case VCHIQ_MSG_BULK_TX_DONE:
  24553. + service = find_service_by_port(state, localport);
  24554. + if ((!service || service->remoteport != remoteport) &&
  24555. + (localport == 0) &&
  24556. + (type == VCHIQ_MSG_CLOSE)) {
  24557. + /* This could be a CLOSE from a client which
  24558. + hadn't yet received the OPENACK - look for
  24559. + the connected service */
  24560. + if (service)
  24561. + unlock_service(service);
  24562. + service = get_connected_service(state,
  24563. + remoteport);
  24564. + if (service)
  24565. + vchiq_log_warning(vchiq_core_log_level,
  24566. + "%d: prs %s@%x (%d->%d) - "
  24567. + "found connected service %d",
  24568. + state->id, msg_type_str(type),
  24569. + (unsigned int)header,
  24570. + remoteport, localport,
  24571. + service->localport);
  24572. + }
  24573. +
  24574. + if (!service) {
  24575. + vchiq_log_error(vchiq_core_log_level,
  24576. + "%d: prs %s@%x (%d->%d) - "
  24577. + "invalid/closed service %d",
  24578. + state->id, msg_type_str(type),
  24579. + (unsigned int)header,
  24580. + remoteport, localport, localport);
  24581. + goto skip_message;
  24582. + }
  24583. + break;
  24584. + default:
  24585. + break;
  24586. + }
  24587. +
  24588. + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
  24589. + int svc_fourcc;
  24590. +
  24591. + svc_fourcc = service
  24592. + ? service->base.fourcc
  24593. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  24594. + vchiq_log_info(vchiq_core_msg_log_level,
  24595. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  24596. + "len:%d",
  24597. + msg_type_str(type), type,
  24598. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  24599. + remoteport, localport, size);
  24600. + if (size > 0)
  24601. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  24602. + min(64, size));
  24603. + }
  24604. +
  24605. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  24606. + > VCHIQ_SLOT_SIZE) {
  24607. + vchiq_log_error(vchiq_core_log_level,
  24608. + "header %x (msgid %x) - size %x too big for "
  24609. + "slot",
  24610. + (unsigned int)header, (unsigned int)msgid,
  24611. + (unsigned int)size);
  24612. + WARN(1, "oversized for slot\n");
  24613. + }
  24614. +
  24615. + switch (type) {
  24616. + case VCHIQ_MSG_OPEN:
  24617. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  24618. + if (!parse_open(state, header))
  24619. + goto bail_not_ready;
  24620. + break;
  24621. + case VCHIQ_MSG_OPENACK:
  24622. + if (size >= sizeof(struct vchiq_openack_payload)) {
  24623. + const struct vchiq_openack_payload *payload =
  24624. + (struct vchiq_openack_payload *)
  24625. + header->data;
  24626. + service->peer_version = payload->version;
  24627. + }
  24628. + vchiq_log_info(vchiq_core_log_level,
  24629. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  24630. + state->id, (unsigned int)header, size,
  24631. + remoteport, localport, service->peer_version);
  24632. + if (service->srvstate ==
  24633. + VCHIQ_SRVSTATE_OPENING) {
  24634. + service->remoteport = remoteport;
  24635. + vchiq_set_service_state(service,
  24636. + VCHIQ_SRVSTATE_OPEN);
  24637. + up(&service->remove_event);
  24638. + } else
  24639. + vchiq_log_error(vchiq_core_log_level,
  24640. + "OPENACK received in state %s",
  24641. + srvstate_names[service->srvstate]);
  24642. + break;
  24643. + case VCHIQ_MSG_CLOSE:
  24644. + WARN_ON(size != 0); /* There should be no data */
  24645. +
  24646. + vchiq_log_info(vchiq_core_log_level,
  24647. + "%d: prs CLOSE@%x (%d->%d)",
  24648. + state->id, (unsigned int)header,
  24649. + remoteport, localport);
  24650. +
  24651. + mark_service_closing_internal(service, 1);
  24652. +
  24653. + if (vchiq_close_service_internal(service,
  24654. + 1/*close_recvd*/) == VCHIQ_RETRY)
  24655. + goto bail_not_ready;
  24656. +
  24657. + vchiq_log_info(vchiq_core_log_level,
  24658. + "Close Service %c%c%c%c s:%u d:%d",
  24659. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  24660. + service->localport,
  24661. + service->remoteport);
  24662. + break;
  24663. + case VCHIQ_MSG_DATA:
  24664. + vchiq_log_trace(vchiq_core_log_level,
  24665. + "%d: prs DATA@%x,%x (%d->%d)",
  24666. + state->id, (unsigned int)header, size,
  24667. + remoteport, localport);
  24668. +
  24669. + if ((service->remoteport == remoteport)
  24670. + && (service->srvstate ==
  24671. + VCHIQ_SRVSTATE_OPEN)) {
  24672. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  24673. + claim_slot(state->rx_info);
  24674. + DEBUG_TRACE(PARSE_LINE);
  24675. + if (make_service_callback(service,
  24676. + VCHIQ_MESSAGE_AVAILABLE, header,
  24677. + NULL) == VCHIQ_RETRY) {
  24678. + DEBUG_TRACE(PARSE_LINE);
  24679. + goto bail_not_ready;
  24680. + }
  24681. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  24682. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  24683. + size);
  24684. + } else {
  24685. + VCHIQ_STATS_INC(state, error_count);
  24686. + }
  24687. + break;
  24688. + case VCHIQ_MSG_CONNECT:
  24689. + vchiq_log_info(vchiq_core_log_level,
  24690. + "%d: prs CONNECT@%x",
  24691. + state->id, (unsigned int)header);
  24692. + up(&state->connect);
  24693. + break;
  24694. + case VCHIQ_MSG_BULK_RX:
  24695. + case VCHIQ_MSG_BULK_TX: {
  24696. + VCHIQ_BULK_QUEUE_T *queue;
  24697. + WARN_ON(!state->is_master);
  24698. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  24699. + &service->bulk_tx : &service->bulk_rx;
  24700. + if ((service->remoteport == remoteport)
  24701. + && (service->srvstate ==
  24702. + VCHIQ_SRVSTATE_OPEN)) {
  24703. + VCHIQ_BULK_T *bulk;
  24704. + int resolved = 0;
  24705. +
  24706. + DEBUG_TRACE(PARSE_LINE);
  24707. + if (mutex_lock_interruptible(
  24708. + &service->bulk_mutex) != 0) {
  24709. + DEBUG_TRACE(PARSE_LINE);
  24710. + goto bail_not_ready;
  24711. + }
  24712. +
  24713. + WARN_ON(!(queue->remote_insert < queue->remove +
  24714. + VCHIQ_NUM_SERVICE_BULKS));
  24715. + bulk = &queue->bulks[
  24716. + BULK_INDEX(queue->remote_insert)];
  24717. + bulk->remote_data =
  24718. + (void *)((int *)header->data)[0];
  24719. + bulk->remote_size = ((int *)header->data)[1];
  24720. + wmb();
  24721. +
  24722. + vchiq_log_info(vchiq_core_log_level,
  24723. + "%d: prs %s@%x (%d->%d) %x@%x",
  24724. + state->id, msg_type_str(type),
  24725. + (unsigned int)header,
  24726. + remoteport, localport,
  24727. + bulk->remote_size,
  24728. + (unsigned int)bulk->remote_data);
  24729. +
  24730. + queue->remote_insert++;
  24731. +
  24732. + if (atomic_read(&pause_bulks_count)) {
  24733. + state->deferred_bulks++;
  24734. + vchiq_log_info(vchiq_core_log_level,
  24735. + "%s: deferring bulk (%d)",
  24736. + __func__,
  24737. + state->deferred_bulks);
  24738. + if (state->conn_state !=
  24739. + VCHIQ_CONNSTATE_PAUSE_SENT)
  24740. + vchiq_log_error(
  24741. + vchiq_core_log_level,
  24742. + "%s: bulks paused in "
  24743. + "unexpected state %s",
  24744. + __func__,
  24745. + conn_state_names[
  24746. + state->conn_state]);
  24747. + } else if (state->conn_state ==
  24748. + VCHIQ_CONNSTATE_CONNECTED) {
  24749. + DEBUG_TRACE(PARSE_LINE);
  24750. + resolved = resolve_bulks(service,
  24751. + queue);
  24752. + }
  24753. +
  24754. + mutex_unlock(&service->bulk_mutex);
  24755. + if (resolved)
  24756. + notify_bulks(service, queue,
  24757. + 1/*retry_poll*/);
  24758. + }
  24759. + } break;
  24760. + case VCHIQ_MSG_BULK_RX_DONE:
  24761. + case VCHIQ_MSG_BULK_TX_DONE:
  24762. + WARN_ON(state->is_master);
  24763. + if ((service->remoteport == remoteport)
  24764. + && (service->srvstate !=
  24765. + VCHIQ_SRVSTATE_FREE)) {
  24766. + VCHIQ_BULK_QUEUE_T *queue;
  24767. + VCHIQ_BULK_T *bulk;
  24768. +
  24769. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  24770. + &service->bulk_rx : &service->bulk_tx;
  24771. +
  24772. + DEBUG_TRACE(PARSE_LINE);
  24773. + if (mutex_lock_interruptible(
  24774. + &service->bulk_mutex) != 0) {
  24775. + DEBUG_TRACE(PARSE_LINE);
  24776. + goto bail_not_ready;
  24777. + }
  24778. + if ((int)(queue->remote_insert -
  24779. + queue->local_insert) >= 0) {
  24780. + vchiq_log_error(vchiq_core_log_level,
  24781. + "%d: prs %s@%x (%d->%d) "
  24782. + "unexpected (ri=%d,li=%d)",
  24783. + state->id, msg_type_str(type),
  24784. + (unsigned int)header,
  24785. + remoteport, localport,
  24786. + queue->remote_insert,
  24787. + queue->local_insert);
  24788. + mutex_unlock(&service->bulk_mutex);
  24789. + break;
  24790. + }
  24791. +
  24792. + BUG_ON(queue->process == queue->local_insert);
  24793. + BUG_ON(queue->process != queue->remote_insert);
  24794. +
  24795. + bulk = &queue->bulks[
  24796. + BULK_INDEX(queue->remote_insert)];
  24797. + bulk->actual = *(int *)header->data;
  24798. + queue->remote_insert++;
  24799. +
  24800. + vchiq_log_info(vchiq_core_log_level,
  24801. + "%d: prs %s@%x (%d->%d) %x@%x",
  24802. + state->id, msg_type_str(type),
  24803. + (unsigned int)header,
  24804. + remoteport, localport,
  24805. + bulk->actual, (unsigned int)bulk->data);
  24806. +
  24807. + vchiq_log_trace(vchiq_core_log_level,
  24808. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  24809. + state->id, localport,
  24810. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  24811. + 'r' : 't',
  24812. + queue->local_insert,
  24813. + queue->remote_insert, queue->process);
  24814. +
  24815. + DEBUG_TRACE(PARSE_LINE);
  24816. + WARN_ON(queue->process == queue->local_insert);
  24817. + vchiq_complete_bulk(bulk);
  24818. + queue->process++;
  24819. + mutex_unlock(&service->bulk_mutex);
  24820. + DEBUG_TRACE(PARSE_LINE);
  24821. + notify_bulks(service, queue, 1/*retry_poll*/);
  24822. + DEBUG_TRACE(PARSE_LINE);
  24823. + }
  24824. + break;
  24825. + case VCHIQ_MSG_PADDING:
  24826. + vchiq_log_trace(vchiq_core_log_level,
  24827. + "%d: prs PADDING@%x,%x",
  24828. + state->id, (unsigned int)header, size);
  24829. + break;
  24830. + case VCHIQ_MSG_PAUSE:
  24831. + /* If initiated, signal the application thread */
  24832. + vchiq_log_trace(vchiq_core_log_level,
  24833. + "%d: prs PAUSE@%x,%x",
  24834. + state->id, (unsigned int)header, size);
  24835. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  24836. + vchiq_log_error(vchiq_core_log_level,
  24837. + "%d: PAUSE received in state PAUSED",
  24838. + state->id);
  24839. + break;
  24840. + }
  24841. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  24842. + /* Send a PAUSE in response */
  24843. + if (queue_message(state, NULL,
  24844. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  24845. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  24846. + goto bail_not_ready;
  24847. + if (state->is_master)
  24848. + pause_bulks(state);
  24849. + }
  24850. + /* At this point slot_mutex is held */
  24851. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  24852. + vchiq_platform_paused(state);
  24853. + break;
  24854. + case VCHIQ_MSG_RESUME:
  24855. + vchiq_log_trace(vchiq_core_log_level,
  24856. + "%d: prs RESUME@%x,%x",
  24857. + state->id, (unsigned int)header, size);
  24858. + /* Release the slot mutex */
  24859. + mutex_unlock(&state->slot_mutex);
  24860. + if (state->is_master)
  24861. + resume_bulks(state);
  24862. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  24863. + vchiq_platform_resumed(state);
  24864. + break;
  24865. +
  24866. + case VCHIQ_MSG_REMOTE_USE:
  24867. + vchiq_on_remote_use(state);
  24868. + break;
  24869. + case VCHIQ_MSG_REMOTE_RELEASE:
  24870. + vchiq_on_remote_release(state);
  24871. + break;
  24872. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  24873. + vchiq_on_remote_use_active(state);
  24874. + break;
  24875. +
  24876. + default:
  24877. + vchiq_log_error(vchiq_core_log_level,
  24878. + "%d: prs invalid msgid %x@%x,%x",
  24879. + state->id, msgid, (unsigned int)header, size);
  24880. + WARN(1, "invalid message\n");
  24881. + break;
  24882. + }
  24883. +
  24884. +skip_message:
  24885. + if (service) {
  24886. + unlock_service(service);
  24887. + service = NULL;
  24888. + }
  24889. +
  24890. + state->rx_pos += calc_stride(size);
  24891. +
  24892. + DEBUG_TRACE(PARSE_LINE);
  24893. + /* Perform some housekeeping when the end of the slot is
  24894. + ** reached. */
  24895. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  24896. + /* Remove the extra reference count. */
  24897. + release_slot(state, state->rx_info, NULL, NULL);
  24898. + state->rx_data = NULL;
  24899. + }
  24900. + }
  24901. +
  24902. +bail_not_ready:
  24903. + if (service)
  24904. + unlock_service(service);
  24905. +}
  24906. +
  24907. +/* Called by the slot handler thread */
  24908. +static int
  24909. +slot_handler_func(void *v)
  24910. +{
  24911. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  24912. + VCHIQ_SHARED_STATE_T *local = state->local;
  24913. + DEBUG_INITIALISE(local)
  24914. +
  24915. + while (1) {
  24916. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  24917. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  24918. + remote_event_wait(&local->trigger);
  24919. +
  24920. + rmb();
  24921. +
  24922. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  24923. + if (state->poll_needed) {
  24924. + /* Check if we need to suspend - may change our
  24925. + * conn_state */
  24926. + vchiq_platform_check_suspend(state);
  24927. +
  24928. + state->poll_needed = 0;
  24929. +
  24930. + /* Handle service polling and other rare conditions here
  24931. + ** out of the mainline code */
  24932. + switch (state->conn_state) {
  24933. + case VCHIQ_CONNSTATE_CONNECTED:
  24934. + /* Poll the services as requested */
  24935. + poll_services(state);
  24936. + break;
  24937. +
  24938. + case VCHIQ_CONNSTATE_PAUSING:
  24939. + if (state->is_master)
  24940. + pause_bulks(state);
  24941. + if (queue_message(state, NULL,
  24942. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  24943. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  24944. + vchiq_set_conn_state(state,
  24945. + VCHIQ_CONNSTATE_PAUSE_SENT);
  24946. + } else {
  24947. + if (state->is_master)
  24948. + resume_bulks(state);
  24949. + /* Retry later */
  24950. + state->poll_needed = 1;
  24951. + }
  24952. + break;
  24953. +
  24954. + case VCHIQ_CONNSTATE_PAUSED:
  24955. + vchiq_platform_resume(state);
  24956. + break;
  24957. +
  24958. + case VCHIQ_CONNSTATE_RESUMING:
  24959. + if (queue_message(state, NULL,
  24960. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  24961. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  24962. + if (state->is_master)
  24963. + resume_bulks(state);
  24964. + vchiq_set_conn_state(state,
  24965. + VCHIQ_CONNSTATE_CONNECTED);
  24966. + vchiq_platform_resumed(state);
  24967. + } else {
  24968. + /* This should really be impossible,
  24969. + ** since the PAUSE should have flushed
  24970. + ** through outstanding messages. */
  24971. + vchiq_log_error(vchiq_core_log_level,
  24972. + "Failed to send RESUME "
  24973. + "message");
  24974. + BUG();
  24975. + }
  24976. + break;
  24977. +
  24978. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  24979. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  24980. + vchiq_platform_handle_timeout(state);
  24981. + break;
  24982. + default:
  24983. + break;
  24984. + }
  24985. +
  24986. +
  24987. + }
  24988. +
  24989. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  24990. + parse_rx_slots(state);
  24991. + }
  24992. + return 0;
  24993. +}
  24994. +
  24995. +
  24996. +/* Called by the recycle thread */
  24997. +static int
  24998. +recycle_func(void *v)
  24999. +{
  25000. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  25001. + VCHIQ_SHARED_STATE_T *local = state->local;
  25002. +
  25003. + while (1) {
  25004. + remote_event_wait(&local->recycle);
  25005. +
  25006. + process_free_queue(state);
  25007. + }
  25008. + return 0;
  25009. +}
  25010. +
  25011. +
  25012. +/* Called by the sync thread */
  25013. +static int
  25014. +sync_func(void *v)
  25015. +{
  25016. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  25017. + VCHIQ_SHARED_STATE_T *local = state->local;
  25018. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  25019. + state->remote->slot_sync);
  25020. +
  25021. + while (1) {
  25022. + VCHIQ_SERVICE_T *service;
  25023. + int msgid, size;
  25024. + int type;
  25025. + unsigned int localport, remoteport;
  25026. +
  25027. + remote_event_wait(&local->sync_trigger);
  25028. +
  25029. + rmb();
  25030. +
  25031. + msgid = header->msgid;
  25032. + size = header->size;
  25033. + type = VCHIQ_MSG_TYPE(msgid);
  25034. + localport = VCHIQ_MSG_DSTPORT(msgid);
  25035. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  25036. +
  25037. + service = find_service_by_port(state, localport);
  25038. +
  25039. + if (!service) {
  25040. + vchiq_log_error(vchiq_sync_log_level,
  25041. + "%d: sf %s@%x (%d->%d) - "
  25042. + "invalid/closed service %d",
  25043. + state->id, msg_type_str(type),
  25044. + (unsigned int)header,
  25045. + remoteport, localport, localport);
  25046. + release_message_sync(state, header);
  25047. + continue;
  25048. + }
  25049. +
  25050. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  25051. + int svc_fourcc;
  25052. +
  25053. + svc_fourcc = service
  25054. + ? service->base.fourcc
  25055. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  25056. + vchiq_log_trace(vchiq_sync_log_level,
  25057. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  25058. + msg_type_str(type),
  25059. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  25060. + remoteport, localport, size);
  25061. + if (size > 0)
  25062. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  25063. + min(64, size));
  25064. + }
  25065. +
  25066. + switch (type) {
  25067. + case VCHIQ_MSG_OPENACK:
  25068. + if (size >= sizeof(struct vchiq_openack_payload)) {
  25069. + const struct vchiq_openack_payload *payload =
  25070. + (struct vchiq_openack_payload *)
  25071. + header->data;
  25072. + service->peer_version = payload->version;
  25073. + }
  25074. + vchiq_log_info(vchiq_sync_log_level,
  25075. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  25076. + state->id, (unsigned int)header, size,
  25077. + remoteport, localport, service->peer_version);
  25078. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  25079. + service->remoteport = remoteport;
  25080. + vchiq_set_service_state(service,
  25081. + VCHIQ_SRVSTATE_OPENSYNC);
  25082. + up(&service->remove_event);
  25083. + }
  25084. + release_message_sync(state, header);
  25085. + break;
  25086. +
  25087. + case VCHIQ_MSG_DATA:
  25088. + vchiq_log_trace(vchiq_sync_log_level,
  25089. + "%d: sf DATA@%x,%x (%d->%d)",
  25090. + state->id, (unsigned int)header, size,
  25091. + remoteport, localport);
  25092. +
  25093. + if ((service->remoteport == remoteport) &&
  25094. + (service->srvstate ==
  25095. + VCHIQ_SRVSTATE_OPENSYNC)) {
  25096. + if (make_service_callback(service,
  25097. + VCHIQ_MESSAGE_AVAILABLE, header,
  25098. + NULL) == VCHIQ_RETRY)
  25099. + vchiq_log_error(vchiq_sync_log_level,
  25100. + "synchronous callback to "
  25101. + "service %d returns "
  25102. + "VCHIQ_RETRY",
  25103. + localport);
  25104. + }
  25105. + break;
  25106. +
  25107. + default:
  25108. + vchiq_log_error(vchiq_sync_log_level,
  25109. + "%d: sf unexpected msgid %x@%x,%x",
  25110. + state->id, msgid, (unsigned int)header, size);
  25111. + release_message_sync(state, header);
  25112. + break;
  25113. + }
  25114. +
  25115. + unlock_service(service);
  25116. + }
  25117. +
  25118. + return 0;
  25119. +}
  25120. +
  25121. +
  25122. +static void
  25123. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  25124. +{
  25125. + queue->local_insert = 0;
  25126. + queue->remote_insert = 0;
  25127. + queue->process = 0;
  25128. + queue->remote_notify = 0;
  25129. + queue->remove = 0;
  25130. +}
  25131. +
  25132. +
  25133. +inline const char *
  25134. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  25135. +{
  25136. + return conn_state_names[conn_state];
  25137. +}
  25138. +
  25139. +
  25140. +VCHIQ_SLOT_ZERO_T *
  25141. +vchiq_init_slots(void *mem_base, int mem_size)
  25142. +{
  25143. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  25144. + VCHIQ_SLOT_ZERO_T *slot_zero =
  25145. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  25146. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  25147. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  25148. +
  25149. + /* Ensure there is enough memory to run an absolutely minimum system */
  25150. + num_slots -= first_data_slot;
  25151. +
  25152. + if (num_slots < 4) {
  25153. + vchiq_log_error(vchiq_core_log_level,
  25154. + "vchiq_init_slots - insufficient memory %x bytes",
  25155. + mem_size);
  25156. + return NULL;
  25157. + }
  25158. +
  25159. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  25160. +
  25161. + slot_zero->magic = VCHIQ_MAGIC;
  25162. + slot_zero->version = VCHIQ_VERSION;
  25163. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  25164. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  25165. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  25166. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  25167. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  25168. +
  25169. + slot_zero->master.slot_sync = first_data_slot;
  25170. + slot_zero->master.slot_first = first_data_slot + 1;
  25171. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  25172. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  25173. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  25174. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  25175. +
  25176. + return slot_zero;
  25177. +}
  25178. +
  25179. +VCHIQ_STATUS_T
  25180. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  25181. + int is_master)
  25182. +{
  25183. + VCHIQ_SHARED_STATE_T *local;
  25184. + VCHIQ_SHARED_STATE_T *remote;
  25185. + VCHIQ_STATUS_T status;
  25186. + char threadname[10];
  25187. + static int id;
  25188. + int i;
  25189. +
  25190. + vchiq_log_warning(vchiq_core_log_level,
  25191. + "%s: slot_zero = 0x%08lx, is_master = %d",
  25192. + __func__, (unsigned long)slot_zero, is_master);
  25193. +
  25194. + /* Check the input configuration */
  25195. +
  25196. + if (slot_zero->magic != VCHIQ_MAGIC) {
  25197. + vchiq_loud_error_header();
  25198. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  25199. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  25200. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  25201. + vchiq_loud_error_footer();
  25202. + return VCHIQ_ERROR;
  25203. + }
  25204. +
  25205. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  25206. + vchiq_loud_error_header();
  25207. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  25208. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  25209. + "(minimum %d)",
  25210. + (unsigned int)slot_zero, slot_zero->version,
  25211. + VCHIQ_VERSION_MIN);
  25212. + vchiq_loud_error("Restart with a newer VideoCore image.");
  25213. + vchiq_loud_error_footer();
  25214. + return VCHIQ_ERROR;
  25215. + }
  25216. +
  25217. + if (VCHIQ_VERSION < slot_zero->version_min) {
  25218. + vchiq_loud_error_header();
  25219. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  25220. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  25221. + "minimum %d)",
  25222. + (unsigned int)slot_zero, VCHIQ_VERSION,
  25223. + slot_zero->version_min);
  25224. + vchiq_loud_error("Restart with a newer kernel.");
  25225. + vchiq_loud_error_footer();
  25226. + return VCHIQ_ERROR;
  25227. + }
  25228. +
  25229. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  25230. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  25231. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  25232. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  25233. + vchiq_loud_error_header();
  25234. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  25235. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  25236. + "(expected %x)",
  25237. + (unsigned int)slot_zero,
  25238. + slot_zero->slot_zero_size,
  25239. + sizeof(VCHIQ_SLOT_ZERO_T));
  25240. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  25241. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  25242. + "(expected %d",
  25243. + (unsigned int)slot_zero, slot_zero->slot_size,
  25244. + VCHIQ_SLOT_SIZE);
  25245. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  25246. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  25247. + "(expected %d)",
  25248. + (unsigned int)slot_zero, slot_zero->max_slots,
  25249. + VCHIQ_MAX_SLOTS);
  25250. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  25251. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  25252. + "(expected %d)",
  25253. + (unsigned int)slot_zero,
  25254. + slot_zero->max_slots_per_side,
  25255. + VCHIQ_MAX_SLOTS_PER_SIDE);
  25256. + vchiq_loud_error_footer();
  25257. + return VCHIQ_ERROR;
  25258. + }
  25259. +
  25260. + if (is_master) {
  25261. + local = &slot_zero->master;
  25262. + remote = &slot_zero->slave;
  25263. + } else {
  25264. + local = &slot_zero->slave;
  25265. + remote = &slot_zero->master;
  25266. + }
  25267. +
  25268. + if (local->initialised) {
  25269. + vchiq_loud_error_header();
  25270. + if (remote->initialised)
  25271. + vchiq_loud_error("local state has already been "
  25272. + "initialised");
  25273. + else
  25274. + vchiq_loud_error("master/slave mismatch - two %ss",
  25275. + is_master ? "master" : "slave");
  25276. + vchiq_loud_error_footer();
  25277. + return VCHIQ_ERROR;
  25278. + }
  25279. +
  25280. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  25281. +
  25282. + state->id = id++;
  25283. + state->is_master = is_master;
  25284. +
  25285. + /*
  25286. + initialize shared state pointers
  25287. + */
  25288. +
  25289. + state->local = local;
  25290. + state->remote = remote;
  25291. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  25292. +
  25293. + /*
  25294. + initialize events and mutexes
  25295. + */
  25296. +
  25297. + sema_init(&state->connect, 0);
  25298. + mutex_init(&state->mutex);
  25299. + sema_init(&state->trigger_event, 0);
  25300. + sema_init(&state->recycle_event, 0);
  25301. + sema_init(&state->sync_trigger_event, 0);
  25302. + sema_init(&state->sync_release_event, 0);
  25303. +
  25304. + mutex_init(&state->slot_mutex);
  25305. + mutex_init(&state->recycle_mutex);
  25306. + mutex_init(&state->sync_mutex);
  25307. + mutex_init(&state->bulk_transfer_mutex);
  25308. +
  25309. + sema_init(&state->slot_available_event, 0);
  25310. + sema_init(&state->slot_remove_event, 0);
  25311. + sema_init(&state->data_quota_event, 0);
  25312. +
  25313. + state->slot_queue_available = 0;
  25314. +
  25315. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  25316. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  25317. + &state->service_quotas[i];
  25318. + sema_init(&service_quota->quota_event, 0);
  25319. + }
  25320. +
  25321. + for (i = local->slot_first; i <= local->slot_last; i++) {
  25322. + local->slot_queue[state->slot_queue_available++] = i;
  25323. + up(&state->slot_available_event);
  25324. + }
  25325. +
  25326. + state->default_slot_quota = state->slot_queue_available/2;
  25327. + state->default_message_quota =
  25328. + min((unsigned short)(state->default_slot_quota * 256),
  25329. + (unsigned short)~0);
  25330. +
  25331. + state->previous_data_index = -1;
  25332. + state->data_use_count = 0;
  25333. + state->data_quota = state->slot_queue_available - 1;
  25334. +
  25335. + local->trigger.event = &state->trigger_event;
  25336. + remote_event_create(&local->trigger);
  25337. + local->tx_pos = 0;
  25338. +
  25339. + local->recycle.event = &state->recycle_event;
  25340. + remote_event_create(&local->recycle);
  25341. + local->slot_queue_recycle = state->slot_queue_available;
  25342. +
  25343. + local->sync_trigger.event = &state->sync_trigger_event;
  25344. + remote_event_create(&local->sync_trigger);
  25345. +
  25346. + local->sync_release.event = &state->sync_release_event;
  25347. + remote_event_create(&local->sync_release);
  25348. +
  25349. + /* At start-of-day, the slot is empty and available */
  25350. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  25351. + = VCHIQ_MSGID_PADDING;
  25352. + remote_event_signal_local(&local->sync_release);
  25353. +
  25354. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  25355. +
  25356. + status = vchiq_platform_init_state(state);
  25357. +
  25358. + /*
  25359. + bring up slot handler thread
  25360. + */
  25361. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  25362. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  25363. + (void *)state,
  25364. + threadname);
  25365. +
  25366. + if (state->slot_handler_thread == NULL) {
  25367. + vchiq_loud_error_header();
  25368. + vchiq_loud_error("couldn't create thread %s", threadname);
  25369. + vchiq_loud_error_footer();
  25370. + return VCHIQ_ERROR;
  25371. + }
  25372. + set_user_nice(state->slot_handler_thread, -19);
  25373. + wake_up_process(state->slot_handler_thread);
  25374. +
  25375. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  25376. + state->recycle_thread = kthread_create(&recycle_func,
  25377. + (void *)state,
  25378. + threadname);
  25379. + if (state->recycle_thread == NULL) {
  25380. + vchiq_loud_error_header();
  25381. + vchiq_loud_error("couldn't create thread %s", threadname);
  25382. + vchiq_loud_error_footer();
  25383. + return VCHIQ_ERROR;
  25384. + }
  25385. + set_user_nice(state->recycle_thread, -19);
  25386. + wake_up_process(state->recycle_thread);
  25387. +
  25388. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  25389. + state->sync_thread = kthread_create(&sync_func,
  25390. + (void *)state,
  25391. + threadname);
  25392. + if (state->sync_thread == NULL) {
  25393. + vchiq_loud_error_header();
  25394. + vchiq_loud_error("couldn't create thread %s", threadname);
  25395. + vchiq_loud_error_footer();
  25396. + return VCHIQ_ERROR;
  25397. + }
  25398. + set_user_nice(state->sync_thread, -20);
  25399. + wake_up_process(state->sync_thread);
  25400. +
  25401. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  25402. + vchiq_states[state->id] = state;
  25403. +
  25404. + /* Indicate readiness to the other side */
  25405. + local->initialised = 1;
  25406. +
  25407. + return status;
  25408. +}
  25409. +
  25410. +/* Called from application thread when a client or server service is created. */
  25411. +VCHIQ_SERVICE_T *
  25412. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  25413. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  25414. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  25415. +{
  25416. + VCHIQ_SERVICE_T *service;
  25417. +
  25418. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  25419. + if (service) {
  25420. + service->base.fourcc = params->fourcc;
  25421. + service->base.callback = params->callback;
  25422. + service->base.userdata = params->userdata;
  25423. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  25424. + service->ref_count = 1;
  25425. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  25426. + service->userdata_term = userdata_term;
  25427. + service->localport = VCHIQ_PORT_FREE;
  25428. + service->remoteport = VCHIQ_PORT_FREE;
  25429. +
  25430. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  25431. + VCHIQ_FOURCC_INVALID : params->fourcc;
  25432. + service->client_id = 0;
  25433. + service->auto_close = 1;
  25434. + service->sync = 0;
  25435. + service->closing = 0;
  25436. + atomic_set(&service->poll_flags, 0);
  25437. + service->version = params->version;
  25438. + service->version_min = params->version_min;
  25439. + service->state = state;
  25440. + service->instance = instance;
  25441. + service->service_use_count = 0;
  25442. + init_bulk_queue(&service->bulk_tx);
  25443. + init_bulk_queue(&service->bulk_rx);
  25444. + sema_init(&service->remove_event, 0);
  25445. + sema_init(&service->bulk_remove_event, 0);
  25446. + mutex_init(&service->bulk_mutex);
  25447. + memset(&service->stats, 0, sizeof(service->stats));
  25448. + } else {
  25449. + vchiq_log_error(vchiq_core_log_level,
  25450. + "Out of memory");
  25451. + }
  25452. +
  25453. + if (service) {
  25454. + VCHIQ_SERVICE_T **pservice = NULL;
  25455. + int i;
  25456. +
  25457. + /* Although it is perfectly possible to use service_spinlock
  25458. + ** to protect the creation of services, it is overkill as it
  25459. + ** disables interrupts while the array is searched.
  25460. + ** The only danger is of another thread trying to create a
  25461. + ** service - service deletion is safe.
  25462. + ** Therefore it is preferable to use state->mutex which,
  25463. + ** although slower to claim, doesn't block interrupts while
  25464. + ** it is held.
  25465. + */
  25466. +
  25467. + mutex_lock(&state->mutex);
  25468. +
  25469. + /* Prepare to use a previously unused service */
  25470. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  25471. + pservice = &state->services[state->unused_service];
  25472. +
  25473. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  25474. + for (i = 0; i < state->unused_service; i++) {
  25475. + VCHIQ_SERVICE_T *srv = state->services[i];
  25476. + if (!srv) {
  25477. + pservice = &state->services[i];
  25478. + break;
  25479. + }
  25480. + }
  25481. + } else {
  25482. + for (i = (state->unused_service - 1); i >= 0; i--) {
  25483. + VCHIQ_SERVICE_T *srv = state->services[i];
  25484. + if (!srv)
  25485. + pservice = &state->services[i];
  25486. + else if ((srv->public_fourcc == params->fourcc)
  25487. + && ((srv->instance != instance) ||
  25488. + (srv->base.callback !=
  25489. + params->callback))) {
  25490. + /* There is another server using this
  25491. + ** fourcc which doesn't match. */
  25492. + pservice = NULL;
  25493. + break;
  25494. + }
  25495. + }
  25496. + }
  25497. +
  25498. + if (pservice) {
  25499. + service->localport = (pservice - state->services);
  25500. + if (!handle_seq)
  25501. + handle_seq = VCHIQ_MAX_STATES *
  25502. + VCHIQ_MAX_SERVICES;
  25503. + service->handle = handle_seq |
  25504. + (state->id * VCHIQ_MAX_SERVICES) |
  25505. + service->localport;
  25506. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  25507. + *pservice = service;
  25508. + if (pservice == &state->services[state->unused_service])
  25509. + state->unused_service++;
  25510. + }
  25511. +
  25512. + mutex_unlock(&state->mutex);
  25513. +
  25514. + if (!pservice) {
  25515. + kfree(service);
  25516. + service = NULL;
  25517. + }
  25518. + }
  25519. +
  25520. + if (service) {
  25521. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  25522. + &state->service_quotas[service->localport];
  25523. + service_quota->slot_quota = state->default_slot_quota;
  25524. + service_quota->message_quota = state->default_message_quota;
  25525. + if (service_quota->slot_use_count == 0)
  25526. + service_quota->previous_tx_index =
  25527. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  25528. + - 1;
  25529. +
  25530. + /* Bring this service online */
  25531. + vchiq_set_service_state(service, srvstate);
  25532. +
  25533. + vchiq_log_info(vchiq_core_msg_log_level,
  25534. + "%s Service %c%c%c%c SrcPort:%d",
  25535. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  25536. + ? "Open" : "Add",
  25537. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  25538. + service->localport);
  25539. + }
  25540. +
  25541. + /* Don't unlock the service - leave it with a ref_count of 1. */
  25542. +
  25543. + return service;
  25544. +}
  25545. +
  25546. +VCHIQ_STATUS_T
  25547. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  25548. +{
  25549. + struct vchiq_open_payload payload = {
  25550. + service->base.fourcc,
  25551. + client_id,
  25552. + service->version,
  25553. + service->version_min
  25554. + };
  25555. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  25556. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25557. +
  25558. + service->client_id = client_id;
  25559. + vchiq_use_service_internal(service);
  25560. + status = queue_message(service->state, NULL,
  25561. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  25562. + &body, 1, sizeof(payload), 1);
  25563. + if (status == VCHIQ_SUCCESS) {
  25564. + if (down_interruptible(&service->remove_event) != 0) {
  25565. + status = VCHIQ_RETRY;
  25566. + vchiq_release_service_internal(service);
  25567. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  25568. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  25569. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  25570. + vchiq_log_error(vchiq_core_log_level,
  25571. + "%d: osi - srvstate = %s (ref %d)",
  25572. + service->state->id,
  25573. + srvstate_names[service->srvstate],
  25574. + service->ref_count);
  25575. + status = VCHIQ_ERROR;
  25576. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  25577. + vchiq_release_service_internal(service);
  25578. + }
  25579. + }
  25580. + return status;
  25581. +}
  25582. +
  25583. +static void
  25584. +release_service_messages(VCHIQ_SERVICE_T *service)
  25585. +{
  25586. + VCHIQ_STATE_T *state = service->state;
  25587. + int slot_last = state->remote->slot_last;
  25588. + int i;
  25589. +
  25590. + /* Release any claimed messages */
  25591. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  25592. + VCHIQ_SLOT_INFO_T *slot_info =
  25593. + SLOT_INFO_FROM_INDEX(state, i);
  25594. + if (slot_info->release_count != slot_info->use_count) {
  25595. + char *data =
  25596. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  25597. + unsigned int pos, end;
  25598. +
  25599. + end = VCHIQ_SLOT_SIZE;
  25600. + if (data == state->rx_data)
  25601. + /* This buffer is still being read from - stop
  25602. + ** at the current read position */
  25603. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  25604. +
  25605. + pos = 0;
  25606. +
  25607. + while (pos < end) {
  25608. + VCHIQ_HEADER_T *header =
  25609. + (VCHIQ_HEADER_T *)(data + pos);
  25610. + int msgid = header->msgid;
  25611. + int port = VCHIQ_MSG_DSTPORT(msgid);
  25612. + if ((port == service->localport) &&
  25613. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  25614. + vchiq_log_info(vchiq_core_log_level,
  25615. + " fsi - hdr %x",
  25616. + (unsigned int)header);
  25617. + release_slot(state, slot_info, header,
  25618. + NULL);
  25619. + }
  25620. + pos += calc_stride(header->size);
  25621. + if (pos > VCHIQ_SLOT_SIZE) {
  25622. + vchiq_log_error(vchiq_core_log_level,
  25623. + "fsi - pos %x: header %x, "
  25624. + "msgid %x, header->msgid %x, "
  25625. + "header->size %x",
  25626. + pos, (unsigned int)header,
  25627. + msgid, header->msgid,
  25628. + header->size);
  25629. + WARN(1, "invalid slot position\n");
  25630. + }
  25631. + }
  25632. + }
  25633. + }
  25634. +}
  25635. +
  25636. +static int
  25637. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  25638. +{
  25639. + VCHIQ_STATUS_T status;
  25640. +
  25641. + /* Abort any outstanding bulk transfers */
  25642. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  25643. + return 0;
  25644. + abort_outstanding_bulks(service, &service->bulk_tx);
  25645. + abort_outstanding_bulks(service, &service->bulk_rx);
  25646. + mutex_unlock(&service->bulk_mutex);
  25647. +
  25648. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  25649. + if (status == VCHIQ_SUCCESS)
  25650. + status = notify_bulks(service, &service->bulk_rx,
  25651. + 0/*!retry_poll*/);
  25652. + return (status == VCHIQ_SUCCESS);
  25653. +}
  25654. +
  25655. +static VCHIQ_STATUS_T
  25656. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  25657. +{
  25658. + VCHIQ_STATUS_T status;
  25659. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  25660. + int newstate;
  25661. +
  25662. + switch (service->srvstate) {
  25663. + case VCHIQ_SRVSTATE_OPEN:
  25664. + case VCHIQ_SRVSTATE_CLOSESENT:
  25665. + case VCHIQ_SRVSTATE_CLOSERECVD:
  25666. + if (is_server) {
  25667. + if (service->auto_close) {
  25668. + service->client_id = 0;
  25669. + service->remoteport = VCHIQ_PORT_FREE;
  25670. + newstate = VCHIQ_SRVSTATE_LISTENING;
  25671. + } else
  25672. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  25673. + } else
  25674. + newstate = VCHIQ_SRVSTATE_CLOSED;
  25675. + vchiq_set_service_state(service, newstate);
  25676. + break;
  25677. + case VCHIQ_SRVSTATE_LISTENING:
  25678. + break;
  25679. + default:
  25680. + vchiq_log_error(vchiq_core_log_level,
  25681. + "close_service_complete(%x) called in state %s",
  25682. + service->handle, srvstate_names[service->srvstate]);
  25683. + WARN(1, "close_service_complete in unexpected state\n");
  25684. + return VCHIQ_ERROR;
  25685. + }
  25686. +
  25687. + status = make_service_callback(service,
  25688. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  25689. +
  25690. + if (status != VCHIQ_RETRY) {
  25691. + int uc = service->service_use_count;
  25692. + int i;
  25693. + /* Complete the close process */
  25694. + for (i = 0; i < uc; i++)
  25695. + /* cater for cases where close is forced and the
  25696. + ** client may not close all it's handles */
  25697. + vchiq_release_service_internal(service);
  25698. +
  25699. + service->client_id = 0;
  25700. + service->remoteport = VCHIQ_PORT_FREE;
  25701. +
  25702. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  25703. + vchiq_free_service_internal(service);
  25704. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  25705. + if (is_server)
  25706. + service->closing = 0;
  25707. +
  25708. + up(&service->remove_event);
  25709. + }
  25710. + } else
  25711. + vchiq_set_service_state(service, failstate);
  25712. +
  25713. + return status;
  25714. +}
  25715. +
  25716. +/* Called by the slot handler */
  25717. +VCHIQ_STATUS_T
  25718. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  25719. +{
  25720. + VCHIQ_STATE_T *state = service->state;
  25721. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25722. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  25723. +
  25724. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  25725. + service->state->id, service->localport, close_recvd,
  25726. + srvstate_names[service->srvstate]);
  25727. +
  25728. + switch (service->srvstate) {
  25729. + case VCHIQ_SRVSTATE_CLOSED:
  25730. + case VCHIQ_SRVSTATE_HIDDEN:
  25731. + case VCHIQ_SRVSTATE_LISTENING:
  25732. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  25733. + if (close_recvd)
  25734. + vchiq_log_error(vchiq_core_log_level,
  25735. + "vchiq_close_service_internal(1) called "
  25736. + "in state %s",
  25737. + srvstate_names[service->srvstate]);
  25738. + else if (is_server) {
  25739. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  25740. + status = VCHIQ_ERROR;
  25741. + } else {
  25742. + service->client_id = 0;
  25743. + service->remoteport = VCHIQ_PORT_FREE;
  25744. + if (service->srvstate ==
  25745. + VCHIQ_SRVSTATE_CLOSEWAIT)
  25746. + vchiq_set_service_state(service,
  25747. + VCHIQ_SRVSTATE_LISTENING);
  25748. + }
  25749. + up(&service->remove_event);
  25750. + } else
  25751. + vchiq_free_service_internal(service);
  25752. + break;
  25753. + case VCHIQ_SRVSTATE_OPENING:
  25754. + if (close_recvd) {
  25755. + /* The open was rejected - tell the user */
  25756. + vchiq_set_service_state(service,
  25757. + VCHIQ_SRVSTATE_CLOSEWAIT);
  25758. + up(&service->remove_event);
  25759. + } else {
  25760. + /* Shutdown mid-open - let the other side know */
  25761. + status = queue_message(state, service,
  25762. + VCHIQ_MAKE_MSG
  25763. + (VCHIQ_MSG_CLOSE,
  25764. + service->localport,
  25765. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  25766. + NULL, 0, 0, 0);
  25767. + }
  25768. + break;
  25769. +
  25770. + case VCHIQ_SRVSTATE_OPENSYNC:
  25771. + mutex_lock(&state->sync_mutex);
  25772. + /* Drop through */
  25773. +
  25774. + case VCHIQ_SRVSTATE_OPEN:
  25775. + if (state->is_master || close_recvd) {
  25776. + if (!do_abort_bulks(service))
  25777. + status = VCHIQ_RETRY;
  25778. + }
  25779. +
  25780. + release_service_messages(service);
  25781. +
  25782. + if (status == VCHIQ_SUCCESS)
  25783. + status = queue_message(state, service,
  25784. + VCHIQ_MAKE_MSG
  25785. + (VCHIQ_MSG_CLOSE,
  25786. + service->localport,
  25787. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  25788. + NULL, 0, 0, 0);
  25789. +
  25790. + if (status == VCHIQ_SUCCESS) {
  25791. + if (!close_recvd)
  25792. + break;
  25793. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  25794. + mutex_unlock(&state->sync_mutex);
  25795. + break;
  25796. + } else
  25797. + break;
  25798. +
  25799. + status = close_service_complete(service,
  25800. + VCHIQ_SRVSTATE_CLOSERECVD);
  25801. + break;
  25802. +
  25803. + case VCHIQ_SRVSTATE_CLOSESENT:
  25804. + if (!close_recvd)
  25805. + /* This happens when a process is killed mid-close */
  25806. + break;
  25807. +
  25808. + if (!state->is_master) {
  25809. + if (!do_abort_bulks(service)) {
  25810. + status = VCHIQ_RETRY;
  25811. + break;
  25812. + }
  25813. + }
  25814. +
  25815. + if (status == VCHIQ_SUCCESS)
  25816. + status = close_service_complete(service,
  25817. + VCHIQ_SRVSTATE_CLOSERECVD);
  25818. + break;
  25819. +
  25820. + case VCHIQ_SRVSTATE_CLOSERECVD:
  25821. + if (!close_recvd && is_server)
  25822. + /* Force into LISTENING mode */
  25823. + vchiq_set_service_state(service,
  25824. + VCHIQ_SRVSTATE_LISTENING);
  25825. + status = close_service_complete(service,
  25826. + VCHIQ_SRVSTATE_CLOSERECVD);
  25827. + break;
  25828. +
  25829. + default:
  25830. + vchiq_log_error(vchiq_core_log_level,
  25831. + "vchiq_close_service_internal(%d) called in state %s",
  25832. + close_recvd, srvstate_names[service->srvstate]);
  25833. + break;
  25834. + }
  25835. +
  25836. + return status;
  25837. +}
  25838. +
  25839. +/* Called from the application process upon process death */
  25840. +void
  25841. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  25842. +{
  25843. + VCHIQ_STATE_T *state = service->state;
  25844. +
  25845. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  25846. + state->id, service->localport, service->remoteport);
  25847. +
  25848. + mark_service_closing(service);
  25849. +
  25850. + /* Mark the service for removal by the slot handler */
  25851. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  25852. +}
  25853. +
  25854. +/* Called from the slot handler */
  25855. +void
  25856. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  25857. +{
  25858. + VCHIQ_STATE_T *state = service->state;
  25859. +
  25860. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  25861. + state->id, service->localport);
  25862. +
  25863. + switch (service->srvstate) {
  25864. + case VCHIQ_SRVSTATE_OPENING:
  25865. + case VCHIQ_SRVSTATE_CLOSED:
  25866. + case VCHIQ_SRVSTATE_HIDDEN:
  25867. + case VCHIQ_SRVSTATE_LISTENING:
  25868. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  25869. + break;
  25870. + default:
  25871. + vchiq_log_error(vchiq_core_log_level,
  25872. + "%d: fsi - (%d) in state %s",
  25873. + state->id, service->localport,
  25874. + srvstate_names[service->srvstate]);
  25875. + return;
  25876. + }
  25877. +
  25878. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  25879. +
  25880. + up(&service->remove_event);
  25881. +
  25882. + /* Release the initial lock */
  25883. + unlock_service(service);
  25884. +}
  25885. +
  25886. +VCHIQ_STATUS_T
  25887. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  25888. +{
  25889. + VCHIQ_SERVICE_T *service;
  25890. + int i;
  25891. +
  25892. + /* Find all services registered to this client and enable them. */
  25893. + i = 0;
  25894. + while ((service = next_service_by_instance(state, instance,
  25895. + &i)) != NULL) {
  25896. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  25897. + vchiq_set_service_state(service,
  25898. + VCHIQ_SRVSTATE_LISTENING);
  25899. + unlock_service(service);
  25900. + }
  25901. +
  25902. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  25903. + if (queue_message(state, NULL,
  25904. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  25905. + 0, 1) == VCHIQ_RETRY)
  25906. + return VCHIQ_RETRY;
  25907. +
  25908. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  25909. + }
  25910. +
  25911. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  25912. + if (down_interruptible(&state->connect) != 0)
  25913. + return VCHIQ_RETRY;
  25914. +
  25915. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  25916. + up(&state->connect);
  25917. + }
  25918. +
  25919. + return VCHIQ_SUCCESS;
  25920. +}
  25921. +
  25922. +VCHIQ_STATUS_T
  25923. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  25924. +{
  25925. + VCHIQ_SERVICE_T *service;
  25926. + int i;
  25927. +
  25928. + /* Find all services registered to this client and enable them. */
  25929. + i = 0;
  25930. + while ((service = next_service_by_instance(state, instance,
  25931. + &i)) != NULL) {
  25932. + (void)vchiq_remove_service(service->handle);
  25933. + unlock_service(service);
  25934. + }
  25935. +
  25936. + return VCHIQ_SUCCESS;
  25937. +}
  25938. +
  25939. +VCHIQ_STATUS_T
  25940. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  25941. +{
  25942. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25943. +
  25944. + switch (state->conn_state) {
  25945. + case VCHIQ_CONNSTATE_CONNECTED:
  25946. + /* Request a pause */
  25947. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  25948. + request_poll(state, NULL, 0);
  25949. + break;
  25950. + default:
  25951. + vchiq_log_error(vchiq_core_log_level,
  25952. + "vchiq_pause_internal in state %s\n",
  25953. + conn_state_names[state->conn_state]);
  25954. + status = VCHIQ_ERROR;
  25955. + VCHIQ_STATS_INC(state, error_count);
  25956. + break;
  25957. + }
  25958. +
  25959. + return status;
  25960. +}
  25961. +
  25962. +VCHIQ_STATUS_T
  25963. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  25964. +{
  25965. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25966. +
  25967. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  25968. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  25969. + request_poll(state, NULL, 0);
  25970. + } else {
  25971. + status = VCHIQ_ERROR;
  25972. + VCHIQ_STATS_INC(state, error_count);
  25973. + }
  25974. +
  25975. + return status;
  25976. +}
  25977. +
  25978. +VCHIQ_STATUS_T
  25979. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  25980. +{
  25981. + /* Unregister the service */
  25982. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  25983. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25984. +
  25985. + if (!service)
  25986. + return VCHIQ_ERROR;
  25987. +
  25988. + vchiq_log_info(vchiq_core_log_level,
  25989. + "%d: close_service:%d",
  25990. + service->state->id, service->localport);
  25991. +
  25992. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  25993. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  25994. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  25995. + unlock_service(service);
  25996. + return VCHIQ_ERROR;
  25997. + }
  25998. +
  25999. + mark_service_closing(service);
  26000. +
  26001. + if (current == service->state->slot_handler_thread) {
  26002. + status = vchiq_close_service_internal(service,
  26003. + 0/*!close_recvd*/);
  26004. + BUG_ON(status == VCHIQ_RETRY);
  26005. + } else {
  26006. + /* Mark the service for termination by the slot handler */
  26007. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  26008. + }
  26009. +
  26010. + while (1) {
  26011. + if (down_interruptible(&service->remove_event) != 0) {
  26012. + status = VCHIQ_RETRY;
  26013. + break;
  26014. + }
  26015. +
  26016. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  26017. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  26018. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  26019. + break;
  26020. +
  26021. + vchiq_log_warning(vchiq_core_log_level,
  26022. + "%d: close_service:%d - waiting in state %s",
  26023. + service->state->id, service->localport,
  26024. + srvstate_names[service->srvstate]);
  26025. + }
  26026. +
  26027. + if ((status == VCHIQ_SUCCESS) &&
  26028. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  26029. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  26030. + status = VCHIQ_ERROR;
  26031. +
  26032. + unlock_service(service);
  26033. +
  26034. + return status;
  26035. +}
  26036. +
  26037. +VCHIQ_STATUS_T
  26038. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  26039. +{
  26040. + /* Unregister the service */
  26041. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26042. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26043. +
  26044. + if (!service)
  26045. + return VCHIQ_ERROR;
  26046. +
  26047. + vchiq_log_info(vchiq_core_log_level,
  26048. + "%d: remove_service:%d",
  26049. + service->state->id, service->localport);
  26050. +
  26051. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  26052. + unlock_service(service);
  26053. + return VCHIQ_ERROR;
  26054. + }
  26055. +
  26056. + mark_service_closing(service);
  26057. +
  26058. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  26059. + (current == service->state->slot_handler_thread)) {
  26060. + /* Make it look like a client, because it must be removed and
  26061. + not left in the LISTENING state. */
  26062. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  26063. +
  26064. + status = vchiq_close_service_internal(service,
  26065. + 0/*!close_recvd*/);
  26066. + BUG_ON(status == VCHIQ_RETRY);
  26067. + } else {
  26068. + /* Mark the service for removal by the slot handler */
  26069. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  26070. + }
  26071. + while (1) {
  26072. + if (down_interruptible(&service->remove_event) != 0) {
  26073. + status = VCHIQ_RETRY;
  26074. + break;
  26075. + }
  26076. +
  26077. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  26078. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  26079. + break;
  26080. +
  26081. + vchiq_log_warning(vchiq_core_log_level,
  26082. + "%d: remove_service:%d - waiting in state %s",
  26083. + service->state->id, service->localport,
  26084. + srvstate_names[service->srvstate]);
  26085. + }
  26086. +
  26087. + if ((status == VCHIQ_SUCCESS) &&
  26088. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  26089. + status = VCHIQ_ERROR;
  26090. +
  26091. + unlock_service(service);
  26092. +
  26093. + return status;
  26094. +}
  26095. +
  26096. +
  26097. +/* This function may be called by kernel threads or user threads.
  26098. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  26099. + * received and the call should be retried after being returned to user
  26100. + * context.
  26101. + * When called in blocking mode, the userdata field points to a bulk_waiter
  26102. + * structure.
  26103. + */
  26104. +VCHIQ_STATUS_T
  26105. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  26106. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  26107. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  26108. +{
  26109. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26110. + VCHIQ_BULK_QUEUE_T *queue;
  26111. + VCHIQ_BULK_T *bulk;
  26112. + VCHIQ_STATE_T *state;
  26113. + struct bulk_waiter *bulk_waiter = NULL;
  26114. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  26115. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  26116. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  26117. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26118. +
  26119. + if (!service ||
  26120. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  26121. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  26122. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  26123. + goto error_exit;
  26124. +
  26125. + switch (mode) {
  26126. + case VCHIQ_BULK_MODE_NOCALLBACK:
  26127. + case VCHIQ_BULK_MODE_CALLBACK:
  26128. + break;
  26129. + case VCHIQ_BULK_MODE_BLOCKING:
  26130. + bulk_waiter = (struct bulk_waiter *)userdata;
  26131. + sema_init(&bulk_waiter->event, 0);
  26132. + bulk_waiter->actual = 0;
  26133. + bulk_waiter->bulk = NULL;
  26134. + break;
  26135. + case VCHIQ_BULK_MODE_WAITING:
  26136. + bulk_waiter = (struct bulk_waiter *)userdata;
  26137. + bulk = bulk_waiter->bulk;
  26138. + goto waiting;
  26139. + default:
  26140. + goto error_exit;
  26141. + }
  26142. +
  26143. + state = service->state;
  26144. +
  26145. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  26146. + &service->bulk_tx : &service->bulk_rx;
  26147. +
  26148. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  26149. + status = VCHIQ_RETRY;
  26150. + goto error_exit;
  26151. + }
  26152. +
  26153. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  26154. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  26155. + do {
  26156. + mutex_unlock(&service->bulk_mutex);
  26157. + if (down_interruptible(&service->bulk_remove_event)
  26158. + != 0) {
  26159. + status = VCHIQ_RETRY;
  26160. + goto error_exit;
  26161. + }
  26162. + if (mutex_lock_interruptible(&service->bulk_mutex)
  26163. + != 0) {
  26164. + status = VCHIQ_RETRY;
  26165. + goto error_exit;
  26166. + }
  26167. + } while (queue->local_insert == queue->remove +
  26168. + VCHIQ_NUM_SERVICE_BULKS);
  26169. + }
  26170. +
  26171. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  26172. +
  26173. + bulk->mode = mode;
  26174. + bulk->dir = dir;
  26175. + bulk->userdata = userdata;
  26176. + bulk->size = size;
  26177. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  26178. +
  26179. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  26180. + VCHIQ_SUCCESS)
  26181. + goto unlock_error_exit;
  26182. +
  26183. + wmb();
  26184. +
  26185. + vchiq_log_info(vchiq_core_log_level,
  26186. + "%d: bt (%d->%d) %cx %x@%x %x",
  26187. + state->id,
  26188. + service->localport, service->remoteport, dir_char,
  26189. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  26190. +
  26191. + if (state->is_master) {
  26192. + queue->local_insert++;
  26193. + if (resolve_bulks(service, queue))
  26194. + request_poll(state, service,
  26195. + (dir == VCHIQ_BULK_TRANSMIT) ?
  26196. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  26197. + } else {
  26198. + int payload[2] = { (int)bulk->data, bulk->size };
  26199. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  26200. +
  26201. + status = queue_message(state, NULL,
  26202. + VCHIQ_MAKE_MSG(dir_msgtype,
  26203. + service->localport, service->remoteport),
  26204. + &element, 1, sizeof(payload), 1);
  26205. + if (status != VCHIQ_SUCCESS) {
  26206. + vchiq_complete_bulk(bulk);
  26207. + goto unlock_error_exit;
  26208. + }
  26209. + queue->local_insert++;
  26210. + }
  26211. +
  26212. + mutex_unlock(&service->bulk_mutex);
  26213. +
  26214. + vchiq_log_trace(vchiq_core_log_level,
  26215. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  26216. + state->id,
  26217. + service->localport, dir_char,
  26218. + queue->local_insert, queue->remote_insert, queue->process);
  26219. +
  26220. +waiting:
  26221. + unlock_service(service);
  26222. +
  26223. + status = VCHIQ_SUCCESS;
  26224. +
  26225. + if (bulk_waiter) {
  26226. + bulk_waiter->bulk = bulk;
  26227. + if (down_interruptible(&bulk_waiter->event) != 0)
  26228. + status = VCHIQ_RETRY;
  26229. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  26230. + status = VCHIQ_ERROR;
  26231. + }
  26232. +
  26233. + return status;
  26234. +
  26235. +unlock_error_exit:
  26236. + mutex_unlock(&service->bulk_mutex);
  26237. +
  26238. +error_exit:
  26239. + if (service)
  26240. + unlock_service(service);
  26241. + return status;
  26242. +}
  26243. +
  26244. +VCHIQ_STATUS_T
  26245. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  26246. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  26247. +{
  26248. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26249. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26250. +
  26251. + unsigned int size = 0;
  26252. + unsigned int i;
  26253. +
  26254. + if (!service ||
  26255. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  26256. + goto error_exit;
  26257. +
  26258. + for (i = 0; i < (unsigned int)count; i++) {
  26259. + if (elements[i].size) {
  26260. + if (elements[i].data == NULL) {
  26261. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  26262. + goto error_exit;
  26263. + }
  26264. + size += elements[i].size;
  26265. + }
  26266. + }
  26267. +
  26268. + if (size > VCHIQ_MAX_MSG_SIZE) {
  26269. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  26270. + goto error_exit;
  26271. + }
  26272. +
  26273. + switch (service->srvstate) {
  26274. + case VCHIQ_SRVSTATE_OPEN:
  26275. + status = queue_message(service->state, service,
  26276. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  26277. + service->localport,
  26278. + service->remoteport),
  26279. + elements, count, size, 1);
  26280. + break;
  26281. + case VCHIQ_SRVSTATE_OPENSYNC:
  26282. + status = queue_message_sync(service->state, service,
  26283. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  26284. + service->localport,
  26285. + service->remoteport),
  26286. + elements, count, size, 1);
  26287. + break;
  26288. + default:
  26289. + status = VCHIQ_ERROR;
  26290. + break;
  26291. + }
  26292. +
  26293. +error_exit:
  26294. + if (service)
  26295. + unlock_service(service);
  26296. +
  26297. + return status;
  26298. +}
  26299. +
  26300. +void
  26301. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  26302. +{
  26303. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26304. + VCHIQ_SHARED_STATE_T *remote;
  26305. + VCHIQ_STATE_T *state;
  26306. + int slot_index;
  26307. +
  26308. + if (!service)
  26309. + return;
  26310. +
  26311. + state = service->state;
  26312. + remote = state->remote;
  26313. +
  26314. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  26315. +
  26316. + if ((slot_index >= remote->slot_first) &&
  26317. + (slot_index <= remote->slot_last)) {
  26318. + int msgid = header->msgid;
  26319. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  26320. + VCHIQ_SLOT_INFO_T *slot_info =
  26321. + SLOT_INFO_FROM_INDEX(state, slot_index);
  26322. +
  26323. + release_slot(state, slot_info, header, service);
  26324. + }
  26325. + } else if (slot_index == remote->slot_sync)
  26326. + release_message_sync(state, header);
  26327. +
  26328. + unlock_service(service);
  26329. +}
  26330. +
  26331. +static void
  26332. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  26333. +{
  26334. + header->msgid = VCHIQ_MSGID_PADDING;
  26335. + wmb();
  26336. + remote_event_signal(&state->remote->sync_release);
  26337. +}
  26338. +
  26339. +VCHIQ_STATUS_T
  26340. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  26341. +{
  26342. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26343. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26344. +
  26345. + if (!service ||
  26346. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  26347. + !peer_version)
  26348. + goto exit;
  26349. + *peer_version = service->peer_version;
  26350. + status = VCHIQ_SUCCESS;
  26351. +
  26352. +exit:
  26353. + if (service)
  26354. + unlock_service(service);
  26355. + return status;
  26356. +}
  26357. +
  26358. +VCHIQ_STATUS_T
  26359. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  26360. + int config_size, VCHIQ_CONFIG_T *pconfig)
  26361. +{
  26362. + VCHIQ_CONFIG_T config;
  26363. +
  26364. + (void)instance;
  26365. +
  26366. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  26367. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  26368. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  26369. + config.max_services = VCHIQ_MAX_SERVICES;
  26370. + config.version = VCHIQ_VERSION;
  26371. + config.version_min = VCHIQ_VERSION_MIN;
  26372. +
  26373. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  26374. + return VCHIQ_ERROR;
  26375. +
  26376. + memcpy(pconfig, &config,
  26377. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  26378. +
  26379. + return VCHIQ_SUCCESS;
  26380. +}
  26381. +
  26382. +VCHIQ_STATUS_T
  26383. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  26384. + VCHIQ_SERVICE_OPTION_T option, int value)
  26385. +{
  26386. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  26387. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26388. +
  26389. + if (service) {
  26390. + switch (option) {
  26391. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  26392. + service->auto_close = value;
  26393. + status = VCHIQ_SUCCESS;
  26394. + break;
  26395. +
  26396. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  26397. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  26398. + &service->state->service_quotas[
  26399. + service->localport];
  26400. + if (value == 0)
  26401. + value = service->state->default_slot_quota;
  26402. + if ((value >= service_quota->slot_use_count) &&
  26403. + (value < (unsigned short)~0)) {
  26404. + service_quota->slot_quota = value;
  26405. + if ((value >= service_quota->slot_use_count) &&
  26406. + (service_quota->message_quota >=
  26407. + service_quota->message_use_count)) {
  26408. + /* Signal the service that it may have
  26409. + ** dropped below its quota */
  26410. + up(&service_quota->quota_event);
  26411. + }
  26412. + status = VCHIQ_SUCCESS;
  26413. + }
  26414. + } break;
  26415. +
  26416. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  26417. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  26418. + &service->state->service_quotas[
  26419. + service->localport];
  26420. + if (value == 0)
  26421. + value = service->state->default_message_quota;
  26422. + if ((value >= service_quota->message_use_count) &&
  26423. + (value < (unsigned short)~0)) {
  26424. + service_quota->message_quota = value;
  26425. + if ((value >=
  26426. + service_quota->message_use_count) &&
  26427. + (service_quota->slot_quota >=
  26428. + service_quota->slot_use_count))
  26429. + /* Signal the service that it may have
  26430. + ** dropped below its quota */
  26431. + up(&service_quota->quota_event);
  26432. + status = VCHIQ_SUCCESS;
  26433. + }
  26434. + } break;
  26435. +
  26436. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  26437. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  26438. + (service->srvstate ==
  26439. + VCHIQ_SRVSTATE_LISTENING)) {
  26440. + service->sync = value;
  26441. + status = VCHIQ_SUCCESS;
  26442. + }
  26443. + break;
  26444. +
  26445. + default:
  26446. + break;
  26447. + }
  26448. + unlock_service(service);
  26449. + }
  26450. +
  26451. + return status;
  26452. +}
  26453. +
  26454. +void
  26455. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  26456. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  26457. +{
  26458. + static const char *const debug_names[] = {
  26459. + "<entries>",
  26460. + "SLOT_HANDLER_COUNT",
  26461. + "SLOT_HANDLER_LINE",
  26462. + "PARSE_LINE",
  26463. + "PARSE_HEADER",
  26464. + "PARSE_MSGID",
  26465. + "AWAIT_COMPLETION_LINE",
  26466. + "DEQUEUE_MESSAGE_LINE",
  26467. + "SERVICE_CALLBACK_LINE",
  26468. + "MSG_QUEUE_FULL_COUNT",
  26469. + "COMPLETION_QUEUE_FULL_COUNT"
  26470. + };
  26471. + int i;
  26472. +
  26473. + char buf[80];
  26474. + int len;
  26475. + len = snprintf(buf, sizeof(buf),
  26476. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  26477. + label, shared->slot_first, shared->slot_last,
  26478. + shared->tx_pos, shared->slot_queue_recycle);
  26479. + vchiq_dump(dump_context, buf, len + 1);
  26480. +
  26481. + len = snprintf(buf, sizeof(buf),
  26482. + " Slots claimed:");
  26483. + vchiq_dump(dump_context, buf, len + 1);
  26484. +
  26485. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  26486. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  26487. + if (slot_info.use_count != slot_info.release_count) {
  26488. + len = snprintf(buf, sizeof(buf),
  26489. + " %d: %d/%d", i, slot_info.use_count,
  26490. + slot_info.release_count);
  26491. + vchiq_dump(dump_context, buf, len + 1);
  26492. + }
  26493. + }
  26494. +
  26495. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  26496. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  26497. + debug_names[i], shared->debug[i], shared->debug[i]);
  26498. + vchiq_dump(dump_context, buf, len + 1);
  26499. + }
  26500. +}
  26501. +
  26502. +void
  26503. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  26504. +{
  26505. + char buf[80];
  26506. + int len;
  26507. + int i;
  26508. +
  26509. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  26510. + conn_state_names[state->conn_state]);
  26511. + vchiq_dump(dump_context, buf, len + 1);
  26512. +
  26513. + len = snprintf(buf, sizeof(buf),
  26514. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  26515. + state->local->tx_pos,
  26516. + (uint32_t)state->tx_data +
  26517. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  26518. + state->rx_pos,
  26519. + (uint32_t)state->rx_data +
  26520. + (state->rx_pos & VCHIQ_SLOT_MASK));
  26521. + vchiq_dump(dump_context, buf, len + 1);
  26522. +
  26523. + len = snprintf(buf, sizeof(buf),
  26524. + " Version: %d (min %d)",
  26525. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  26526. + vchiq_dump(dump_context, buf, len + 1);
  26527. +
  26528. + if (VCHIQ_ENABLE_STATS) {
  26529. + len = snprintf(buf, sizeof(buf),
  26530. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  26531. + "error_count=%d",
  26532. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  26533. + state->stats.error_count);
  26534. + vchiq_dump(dump_context, buf, len + 1);
  26535. + }
  26536. +
  26537. + len = snprintf(buf, sizeof(buf),
  26538. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  26539. + "(%d data)",
  26540. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  26541. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  26542. + state->data_quota - state->data_use_count,
  26543. + state->local->slot_queue_recycle - state->slot_queue_available,
  26544. + state->stats.slot_stalls, state->stats.data_stalls);
  26545. + vchiq_dump(dump_context, buf, len + 1);
  26546. +
  26547. + vchiq_dump_platform_state(dump_context);
  26548. +
  26549. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  26550. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  26551. +
  26552. + vchiq_dump_platform_instances(dump_context);
  26553. +
  26554. + for (i = 0; i < state->unused_service; i++) {
  26555. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  26556. +
  26557. + if (service) {
  26558. + vchiq_dump_service_state(dump_context, service);
  26559. + unlock_service(service);
  26560. + }
  26561. + }
  26562. +}
  26563. +
  26564. +void
  26565. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  26566. +{
  26567. + char buf[80];
  26568. + int len;
  26569. +
  26570. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  26571. + service->localport, srvstate_names[service->srvstate],
  26572. + service->ref_count - 1); /*Don't include the lock just taken*/
  26573. +
  26574. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  26575. + char remoteport[30];
  26576. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  26577. + &service->state->service_quotas[service->localport];
  26578. + int fourcc = service->base.fourcc;
  26579. + int tx_pending, rx_pending;
  26580. + if (service->remoteport != VCHIQ_PORT_FREE) {
  26581. + int len2 = snprintf(remoteport, sizeof(remoteport),
  26582. + "%d", service->remoteport);
  26583. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  26584. + snprintf(remoteport + len2,
  26585. + sizeof(remoteport) - len2,
  26586. + " (client %x)", service->client_id);
  26587. + } else
  26588. + strcpy(remoteport, "n/a");
  26589. +
  26590. + len += snprintf(buf + len, sizeof(buf) - len,
  26591. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  26592. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  26593. + remoteport,
  26594. + service_quota->message_use_count,
  26595. + service_quota->message_quota,
  26596. + service_quota->slot_use_count,
  26597. + service_quota->slot_quota);
  26598. +
  26599. + vchiq_dump(dump_context, buf, len + 1);
  26600. +
  26601. + tx_pending = service->bulk_tx.local_insert -
  26602. + service->bulk_tx.remote_insert;
  26603. +
  26604. + rx_pending = service->bulk_rx.local_insert -
  26605. + service->bulk_rx.remote_insert;
  26606. +
  26607. + len = snprintf(buf, sizeof(buf),
  26608. + " Bulk: tx_pending=%d (size %d),"
  26609. + " rx_pending=%d (size %d)",
  26610. + tx_pending,
  26611. + tx_pending ? service->bulk_tx.bulks[
  26612. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  26613. + rx_pending,
  26614. + rx_pending ? service->bulk_rx.bulks[
  26615. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  26616. +
  26617. + if (VCHIQ_ENABLE_STATS) {
  26618. + vchiq_dump(dump_context, buf, len + 1);
  26619. +
  26620. + len = snprintf(buf, sizeof(buf),
  26621. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  26622. + "rx_count=%d, rx_bytes=%llu",
  26623. + service->stats.ctrl_tx_count,
  26624. + service->stats.ctrl_tx_bytes,
  26625. + service->stats.ctrl_rx_count,
  26626. + service->stats.ctrl_rx_bytes);
  26627. + vchiq_dump(dump_context, buf, len + 1);
  26628. +
  26629. + len = snprintf(buf, sizeof(buf),
  26630. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  26631. + "rx_count=%d, rx_bytes=%llu",
  26632. + service->stats.bulk_tx_count,
  26633. + service->stats.bulk_tx_bytes,
  26634. + service->stats.bulk_rx_count,
  26635. + service->stats.bulk_rx_bytes);
  26636. + vchiq_dump(dump_context, buf, len + 1);
  26637. +
  26638. + len = snprintf(buf, sizeof(buf),
  26639. + " %d quota stalls, %d slot stalls, "
  26640. + "%d bulk stalls, %d aborted, %d errors",
  26641. + service->stats.quota_stalls,
  26642. + service->stats.slot_stalls,
  26643. + service->stats.bulk_stalls,
  26644. + service->stats.bulk_aborted_count,
  26645. + service->stats.error_count);
  26646. + }
  26647. + }
  26648. +
  26649. + vchiq_dump(dump_context, buf, len + 1);
  26650. +
  26651. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  26652. + vchiq_dump_platform_service_state(dump_context, service);
  26653. +}
  26654. +
  26655. +
  26656. +void
  26657. +vchiq_loud_error_header(void)
  26658. +{
  26659. + vchiq_log_error(vchiq_core_log_level,
  26660. + "============================================================"
  26661. + "================");
  26662. + vchiq_log_error(vchiq_core_log_level,
  26663. + "============================================================"
  26664. + "================");
  26665. + vchiq_log_error(vchiq_core_log_level, "=====");
  26666. +}
  26667. +
  26668. +void
  26669. +vchiq_loud_error_footer(void)
  26670. +{
  26671. + vchiq_log_error(vchiq_core_log_level, "=====");
  26672. + vchiq_log_error(vchiq_core_log_level,
  26673. + "============================================================"
  26674. + "================");
  26675. + vchiq_log_error(vchiq_core_log_level,
  26676. + "============================================================"
  26677. + "================");
  26678. +}
  26679. +
  26680. +
  26681. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  26682. +{
  26683. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  26684. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  26685. + status = queue_message(state, NULL,
  26686. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  26687. + NULL, 0, 0, 0);
  26688. + return status;
  26689. +}
  26690. +
  26691. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  26692. +{
  26693. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  26694. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  26695. + status = queue_message(state, NULL,
  26696. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  26697. + NULL, 0, 0, 0);
  26698. + return status;
  26699. +}
  26700. +
  26701. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  26702. +{
  26703. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  26704. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  26705. + status = queue_message(state, NULL,
  26706. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  26707. + NULL, 0, 0, 0);
  26708. + return status;
  26709. +}
  26710. +
  26711. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  26712. + size_t numBytes)
  26713. +{
  26714. + const uint8_t *mem = (const uint8_t *)voidMem;
  26715. + size_t offset;
  26716. + char lineBuf[100];
  26717. + char *s;
  26718. +
  26719. + while (numBytes > 0) {
  26720. + s = lineBuf;
  26721. +
  26722. + for (offset = 0; offset < 16; offset++) {
  26723. + if (offset < numBytes)
  26724. + s += snprintf(s, 4, "%02x ", mem[offset]);
  26725. + else
  26726. + s += snprintf(s, 4, " ");
  26727. + }
  26728. +
  26729. + for (offset = 0; offset < 16; offset++) {
  26730. + if (offset < numBytes) {
  26731. + uint8_t ch = mem[offset];
  26732. +
  26733. + if ((ch < ' ') || (ch > '~'))
  26734. + ch = '.';
  26735. + *s++ = (char)ch;
  26736. + }
  26737. + }
  26738. + *s++ = '\0';
  26739. +
  26740. + if ((label != NULL) && (*label != '\0'))
  26741. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  26742. + "%s: %08x: %s", label, addr, lineBuf);
  26743. + else
  26744. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  26745. + "%08x: %s", addr, lineBuf);
  26746. +
  26747. + addr += 16;
  26748. + mem += 16;
  26749. + if (numBytes > 16)
  26750. + numBytes -= 16;
  26751. + else
  26752. + numBytes = 0;
  26753. + }
  26754. +}
  26755. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  26756. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 1970-01-01 01:00:00.000000000 +0100
  26757. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 2014-03-11 16:52:43.000000000 +0100
  26758. @@ -0,0 +1,706 @@
  26759. +/**
  26760. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  26761. + *
  26762. + * Redistribution and use in source and binary forms, with or without
  26763. + * modification, are permitted provided that the following conditions
  26764. + * are met:
  26765. + * 1. Redistributions of source code must retain the above copyright
  26766. + * notice, this list of conditions, and the following disclaimer,
  26767. + * without modification.
  26768. + * 2. Redistributions in binary form must reproduce the above copyright
  26769. + * notice, this list of conditions and the following disclaimer in the
  26770. + * documentation and/or other materials provided with the distribution.
  26771. + * 3. The names of the above-listed copyright holders may not be used
  26772. + * to endorse or promote products derived from this software without
  26773. + * specific prior written permission.
  26774. + *
  26775. + * ALTERNATIVELY, this software may be distributed under the terms of the
  26776. + * GNU General Public License ("GPL") version 2, as published by the Free
  26777. + * Software Foundation.
  26778. + *
  26779. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26780. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  26781. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26782. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  26783. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26784. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26785. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26786. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26787. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26788. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26789. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26790. + */
  26791. +
  26792. +#ifndef VCHIQ_CORE_H
  26793. +#define VCHIQ_CORE_H
  26794. +
  26795. +#include <linux/mutex.h>
  26796. +#include <linux/semaphore.h>
  26797. +#include <linux/kthread.h>
  26798. +
  26799. +#include "vchiq_cfg.h"
  26800. +
  26801. +#include "vchiq.h"
  26802. +
  26803. +/* Run time control of log level, based on KERN_XXX level. */
  26804. +#define VCHIQ_LOG_DEFAULT 4
  26805. +#define VCHIQ_LOG_ERROR 3
  26806. +#define VCHIQ_LOG_WARNING 4
  26807. +#define VCHIQ_LOG_INFO 6
  26808. +#define VCHIQ_LOG_TRACE 7
  26809. +
  26810. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  26811. +
  26812. +#ifndef vchiq_log_error
  26813. +#define vchiq_log_error(cat, fmt, ...) \
  26814. + do { if (cat >= VCHIQ_LOG_ERROR) \
  26815. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  26816. +#endif
  26817. +#ifndef vchiq_log_warning
  26818. +#define vchiq_log_warning(cat, fmt, ...) \
  26819. + do { if (cat >= VCHIQ_LOG_WARNING) \
  26820. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  26821. +#endif
  26822. +#ifndef vchiq_log_info
  26823. +#define vchiq_log_info(cat, fmt, ...) \
  26824. + do { if (cat >= VCHIQ_LOG_INFO) \
  26825. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  26826. +#endif
  26827. +#ifndef vchiq_log_trace
  26828. +#define vchiq_log_trace(cat, fmt, ...) \
  26829. + do { if (cat >= VCHIQ_LOG_TRACE) \
  26830. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  26831. +#endif
  26832. +
  26833. +#define vchiq_loud_error(...) \
  26834. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  26835. +
  26836. +#ifndef vchiq_static_assert
  26837. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  26838. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  26839. +#endif
  26840. +
  26841. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  26842. +
  26843. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  26844. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  26845. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  26846. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  26847. +
  26848. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  26849. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  26850. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  26851. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  26852. +
  26853. +#define VCHIQ_MSG_PADDING 0 /* - */
  26854. +#define VCHIQ_MSG_CONNECT 1 /* - */
  26855. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  26856. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  26857. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  26858. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  26859. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  26860. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  26861. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  26862. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  26863. +#define VCHIQ_MSG_PAUSE 10 /* - */
  26864. +#define VCHIQ_MSG_RESUME 11 /* - */
  26865. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  26866. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  26867. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  26868. +
  26869. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  26870. +#define VCHIQ_PORT_FREE 0x1000
  26871. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  26872. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  26873. + ((type<<24) | (srcport<<12) | (dstport<<0))
  26874. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  26875. +#define VCHIQ_MSG_SRCPORT(msgid) \
  26876. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  26877. +#define VCHIQ_MSG_DSTPORT(msgid) \
  26878. + ((unsigned short)msgid & 0xfff)
  26879. +
  26880. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  26881. + ((fourcc) >> 24) & 0xff, \
  26882. + ((fourcc) >> 16) & 0xff, \
  26883. + ((fourcc) >> 8) & 0xff, \
  26884. + (fourcc) & 0xff
  26885. +
  26886. +/* Ensure the fields are wide enough */
  26887. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  26888. + == 0);
  26889. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  26890. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  26891. + (unsigned int)VCHIQ_PORT_FREE);
  26892. +
  26893. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  26894. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  26895. +
  26896. +#define VCHIQ_FOURCC_INVALID 0x00000000
  26897. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  26898. +
  26899. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  26900. +
  26901. +typedef uint32_t BITSET_T;
  26902. +
  26903. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  26904. +
  26905. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  26906. +#define BITSET_WORD(b) (b >> 5)
  26907. +#define BITSET_BIT(b) (1 << (b & 31))
  26908. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  26909. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  26910. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  26911. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  26912. +
  26913. +#if VCHIQ_ENABLE_STATS
  26914. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  26915. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  26916. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  26917. + (service->stats. stat += addend)
  26918. +#else
  26919. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  26920. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  26921. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  26922. +#endif
  26923. +
  26924. +enum {
  26925. + DEBUG_ENTRIES,
  26926. +#if VCHIQ_ENABLE_DEBUG
  26927. + DEBUG_SLOT_HANDLER_COUNT,
  26928. + DEBUG_SLOT_HANDLER_LINE,
  26929. + DEBUG_PARSE_LINE,
  26930. + DEBUG_PARSE_HEADER,
  26931. + DEBUG_PARSE_MSGID,
  26932. + DEBUG_AWAIT_COMPLETION_LINE,
  26933. + DEBUG_DEQUEUE_MESSAGE_LINE,
  26934. + DEBUG_SERVICE_CALLBACK_LINE,
  26935. + DEBUG_MSG_QUEUE_FULL_COUNT,
  26936. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  26937. +#endif
  26938. + DEBUG_MAX
  26939. +};
  26940. +
  26941. +#if VCHIQ_ENABLE_DEBUG
  26942. +
  26943. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  26944. +#define DEBUG_TRACE(d) \
  26945. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  26946. +#define DEBUG_VALUE(d, v) \
  26947. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  26948. +#define DEBUG_COUNT(d) \
  26949. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  26950. +
  26951. +#else /* VCHIQ_ENABLE_DEBUG */
  26952. +
  26953. +#define DEBUG_INITIALISE(local)
  26954. +#define DEBUG_TRACE(d)
  26955. +#define DEBUG_VALUE(d, v)
  26956. +#define DEBUG_COUNT(d)
  26957. +
  26958. +#endif /* VCHIQ_ENABLE_DEBUG */
  26959. +
  26960. +typedef enum {
  26961. + VCHIQ_CONNSTATE_DISCONNECTED,
  26962. + VCHIQ_CONNSTATE_CONNECTING,
  26963. + VCHIQ_CONNSTATE_CONNECTED,
  26964. + VCHIQ_CONNSTATE_PAUSING,
  26965. + VCHIQ_CONNSTATE_PAUSE_SENT,
  26966. + VCHIQ_CONNSTATE_PAUSED,
  26967. + VCHIQ_CONNSTATE_RESUMING,
  26968. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  26969. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  26970. +} VCHIQ_CONNSTATE_T;
  26971. +
  26972. +enum {
  26973. + VCHIQ_SRVSTATE_FREE,
  26974. + VCHIQ_SRVSTATE_HIDDEN,
  26975. + VCHIQ_SRVSTATE_LISTENING,
  26976. + VCHIQ_SRVSTATE_OPENING,
  26977. + VCHIQ_SRVSTATE_OPEN,
  26978. + VCHIQ_SRVSTATE_OPENSYNC,
  26979. + VCHIQ_SRVSTATE_CLOSESENT,
  26980. + VCHIQ_SRVSTATE_CLOSERECVD,
  26981. + VCHIQ_SRVSTATE_CLOSEWAIT,
  26982. + VCHIQ_SRVSTATE_CLOSED
  26983. +};
  26984. +
  26985. +enum {
  26986. + VCHIQ_POLL_TERMINATE,
  26987. + VCHIQ_POLL_REMOVE,
  26988. + VCHIQ_POLL_TXNOTIFY,
  26989. + VCHIQ_POLL_RXNOTIFY,
  26990. + VCHIQ_POLL_COUNT
  26991. +};
  26992. +
  26993. +typedef enum {
  26994. + VCHIQ_BULK_TRANSMIT,
  26995. + VCHIQ_BULK_RECEIVE
  26996. +} VCHIQ_BULK_DIR_T;
  26997. +
  26998. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  26999. +
  27000. +typedef struct vchiq_bulk_struct {
  27001. + short mode;
  27002. + short dir;
  27003. + void *userdata;
  27004. + VCHI_MEM_HANDLE_T handle;
  27005. + void *data;
  27006. + int size;
  27007. + void *remote_data;
  27008. + int remote_size;
  27009. + int actual;
  27010. +} VCHIQ_BULK_T;
  27011. +
  27012. +typedef struct vchiq_bulk_queue_struct {
  27013. + int local_insert; /* Where to insert the next local bulk */
  27014. + int remote_insert; /* Where to insert the next remote bulk (master) */
  27015. + int process; /* Bulk to transfer next */
  27016. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  27017. + int remove; /* Bulk to notify the local client of, and remove,
  27018. + ** next */
  27019. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  27020. +} VCHIQ_BULK_QUEUE_T;
  27021. +
  27022. +typedef struct remote_event_struct {
  27023. + int armed;
  27024. + int fired;
  27025. + struct semaphore *event;
  27026. +} REMOTE_EVENT_T;
  27027. +
  27028. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  27029. +
  27030. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  27031. +
  27032. +typedef struct vchiq_slot_struct {
  27033. + char data[VCHIQ_SLOT_SIZE];
  27034. +} VCHIQ_SLOT_T;
  27035. +
  27036. +typedef struct vchiq_slot_info_struct {
  27037. + /* Use two counters rather than one to avoid the need for a mutex. */
  27038. + short use_count;
  27039. + short release_count;
  27040. +} VCHIQ_SLOT_INFO_T;
  27041. +
  27042. +typedef struct vchiq_service_struct {
  27043. + VCHIQ_SERVICE_BASE_T base;
  27044. + VCHIQ_SERVICE_HANDLE_T handle;
  27045. + unsigned int ref_count;
  27046. + int srvstate;
  27047. + VCHIQ_USERDATA_TERM_T userdata_term;
  27048. + unsigned int localport;
  27049. + unsigned int remoteport;
  27050. + int public_fourcc;
  27051. + int client_id;
  27052. + char auto_close;
  27053. + char sync;
  27054. + char closing;
  27055. + atomic_t poll_flags;
  27056. + short version;
  27057. + short version_min;
  27058. + short peer_version;
  27059. +
  27060. + VCHIQ_STATE_T *state;
  27061. + VCHIQ_INSTANCE_T instance;
  27062. +
  27063. + int service_use_count;
  27064. +
  27065. + VCHIQ_BULK_QUEUE_T bulk_tx;
  27066. + VCHIQ_BULK_QUEUE_T bulk_rx;
  27067. +
  27068. + struct semaphore remove_event;
  27069. + struct semaphore bulk_remove_event;
  27070. + struct mutex bulk_mutex;
  27071. +
  27072. + struct service_stats_struct {
  27073. + int quota_stalls;
  27074. + int slot_stalls;
  27075. + int bulk_stalls;
  27076. + int error_count;
  27077. + int ctrl_tx_count;
  27078. + int ctrl_rx_count;
  27079. + int bulk_tx_count;
  27080. + int bulk_rx_count;
  27081. + int bulk_aborted_count;
  27082. + uint64_t ctrl_tx_bytes;
  27083. + uint64_t ctrl_rx_bytes;
  27084. + uint64_t bulk_tx_bytes;
  27085. + uint64_t bulk_rx_bytes;
  27086. + } stats;
  27087. +} VCHIQ_SERVICE_T;
  27088. +
  27089. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  27090. + statically allocated, since for accounting reasons a service's slot
  27091. + usage is carried over between users of the same port number.
  27092. + */
  27093. +typedef struct vchiq_service_quota_struct {
  27094. + unsigned short slot_quota;
  27095. + unsigned short slot_use_count;
  27096. + unsigned short message_quota;
  27097. + unsigned short message_use_count;
  27098. + struct semaphore quota_event;
  27099. + int previous_tx_index;
  27100. +} VCHIQ_SERVICE_QUOTA_T;
  27101. +
  27102. +typedef struct vchiq_shared_state_struct {
  27103. +
  27104. + /* A non-zero value here indicates that the content is valid. */
  27105. + int initialised;
  27106. +
  27107. + /* The first and last (inclusive) slots allocated to the owner. */
  27108. + int slot_first;
  27109. + int slot_last;
  27110. +
  27111. + /* The slot allocated to synchronous messages from the owner. */
  27112. + int slot_sync;
  27113. +
  27114. + /* Signalling this event indicates that owner's slot handler thread
  27115. + ** should run. */
  27116. + REMOTE_EVENT_T trigger;
  27117. +
  27118. + /* Indicates the byte position within the stream where the next message
  27119. + ** will be written. The least significant bits are an index into the
  27120. + ** slot. The next bits are the index of the slot in slot_queue. */
  27121. + int tx_pos;
  27122. +
  27123. + /* This event should be signalled when a slot is recycled. */
  27124. + REMOTE_EVENT_T recycle;
  27125. +
  27126. + /* The slot_queue index where the next recycled slot will be written. */
  27127. + int slot_queue_recycle;
  27128. +
  27129. + /* This event should be signalled when a synchronous message is sent. */
  27130. + REMOTE_EVENT_T sync_trigger;
  27131. +
  27132. + /* This event should be signalled when a synchronous message has been
  27133. + ** released. */
  27134. + REMOTE_EVENT_T sync_release;
  27135. +
  27136. + /* A circular buffer of slot indexes. */
  27137. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  27138. +
  27139. + /* Debugging state */
  27140. + int debug[DEBUG_MAX];
  27141. +} VCHIQ_SHARED_STATE_T;
  27142. +
  27143. +typedef struct vchiq_slot_zero_struct {
  27144. + int magic;
  27145. + short version;
  27146. + short version_min;
  27147. + int slot_zero_size;
  27148. + int slot_size;
  27149. + int max_slots;
  27150. + int max_slots_per_side;
  27151. + int platform_data[2];
  27152. + VCHIQ_SHARED_STATE_T master;
  27153. + VCHIQ_SHARED_STATE_T slave;
  27154. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  27155. +} VCHIQ_SLOT_ZERO_T;
  27156. +
  27157. +struct vchiq_state_struct {
  27158. + int id;
  27159. + int initialised;
  27160. + VCHIQ_CONNSTATE_T conn_state;
  27161. + int is_master;
  27162. +
  27163. + VCHIQ_SHARED_STATE_T *local;
  27164. + VCHIQ_SHARED_STATE_T *remote;
  27165. + VCHIQ_SLOT_T *slot_data;
  27166. +
  27167. + unsigned short default_slot_quota;
  27168. + unsigned short default_message_quota;
  27169. +
  27170. + /* Event indicating connect message received */
  27171. + struct semaphore connect;
  27172. +
  27173. + /* Mutex protecting services */
  27174. + struct mutex mutex;
  27175. + VCHIQ_INSTANCE_T *instance;
  27176. +
  27177. + /* Processes incoming messages */
  27178. + struct task_struct *slot_handler_thread;
  27179. +
  27180. + /* Processes recycled slots */
  27181. + struct task_struct *recycle_thread;
  27182. +
  27183. + /* Processes synchronous messages */
  27184. + struct task_struct *sync_thread;
  27185. +
  27186. + /* Local implementation of the trigger remote event */
  27187. + struct semaphore trigger_event;
  27188. +
  27189. + /* Local implementation of the recycle remote event */
  27190. + struct semaphore recycle_event;
  27191. +
  27192. + /* Local implementation of the sync trigger remote event */
  27193. + struct semaphore sync_trigger_event;
  27194. +
  27195. + /* Local implementation of the sync release remote event */
  27196. + struct semaphore sync_release_event;
  27197. +
  27198. + char *tx_data;
  27199. + char *rx_data;
  27200. + VCHIQ_SLOT_INFO_T *rx_info;
  27201. +
  27202. + struct mutex slot_mutex;
  27203. +
  27204. + struct mutex recycle_mutex;
  27205. +
  27206. + struct mutex sync_mutex;
  27207. +
  27208. + struct mutex bulk_transfer_mutex;
  27209. +
  27210. + /* Indicates the byte position within the stream from where the next
  27211. + ** message will be read. The least significant bits are an index into
  27212. + ** the slot.The next bits are the index of the slot in
  27213. + ** remote->slot_queue. */
  27214. + int rx_pos;
  27215. +
  27216. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  27217. + from remote->tx_pos. */
  27218. + int local_tx_pos;
  27219. +
  27220. + /* The slot_queue index of the slot to become available next. */
  27221. + int slot_queue_available;
  27222. +
  27223. + /* A flag to indicate if any poll has been requested */
  27224. + int poll_needed;
  27225. +
  27226. + /* Ths index of the previous slot used for data messages. */
  27227. + int previous_data_index;
  27228. +
  27229. + /* The number of slots occupied by data messages. */
  27230. + unsigned short data_use_count;
  27231. +
  27232. + /* The maximum number of slots to be occupied by data messages. */
  27233. + unsigned short data_quota;
  27234. +
  27235. + /* An array of bit sets indicating which services must be polled. */
  27236. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  27237. +
  27238. + /* The number of the first unused service */
  27239. + int unused_service;
  27240. +
  27241. + /* Signalled when a free slot becomes available. */
  27242. + struct semaphore slot_available_event;
  27243. +
  27244. + struct semaphore slot_remove_event;
  27245. +
  27246. + /* Signalled when a free data slot becomes available. */
  27247. + struct semaphore data_quota_event;
  27248. +
  27249. + /* Incremented when there are bulk transfers which cannot be processed
  27250. + * whilst paused and must be processed on resume */
  27251. + int deferred_bulks;
  27252. +
  27253. + struct state_stats_struct {
  27254. + int slot_stalls;
  27255. + int data_stalls;
  27256. + int ctrl_tx_count;
  27257. + int ctrl_rx_count;
  27258. + int error_count;
  27259. + } stats;
  27260. +
  27261. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  27262. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  27263. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  27264. +
  27265. + VCHIQ_PLATFORM_STATE_T platform_state;
  27266. +};
  27267. +
  27268. +struct bulk_waiter {
  27269. + VCHIQ_BULK_T *bulk;
  27270. + struct semaphore event;
  27271. + int actual;
  27272. +};
  27273. +
  27274. +extern spinlock_t bulk_waiter_spinlock;
  27275. +
  27276. +extern int vchiq_core_log_level;
  27277. +extern int vchiq_core_msg_log_level;
  27278. +extern int vchiq_sync_log_level;
  27279. +
  27280. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  27281. +
  27282. +extern const char *
  27283. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  27284. +
  27285. +extern VCHIQ_SLOT_ZERO_T *
  27286. +vchiq_init_slots(void *mem_base, int mem_size);
  27287. +
  27288. +extern VCHIQ_STATUS_T
  27289. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  27290. + int is_master);
  27291. +
  27292. +extern VCHIQ_STATUS_T
  27293. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  27294. +
  27295. +extern VCHIQ_SERVICE_T *
  27296. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  27297. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  27298. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  27299. +
  27300. +extern VCHIQ_STATUS_T
  27301. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  27302. +
  27303. +extern VCHIQ_STATUS_T
  27304. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  27305. +
  27306. +extern void
  27307. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  27308. +
  27309. +extern void
  27310. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  27311. +
  27312. +extern VCHIQ_STATUS_T
  27313. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  27314. +
  27315. +extern VCHIQ_STATUS_T
  27316. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  27317. +
  27318. +extern VCHIQ_STATUS_T
  27319. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  27320. +
  27321. +extern void
  27322. +remote_event_pollall(VCHIQ_STATE_T *state);
  27323. +
  27324. +extern VCHIQ_STATUS_T
  27325. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  27326. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  27327. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  27328. +
  27329. +extern void
  27330. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  27331. +
  27332. +extern void
  27333. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  27334. +
  27335. +extern void
  27336. +vchiq_loud_error_header(void);
  27337. +
  27338. +extern void
  27339. +vchiq_loud_error_footer(void);
  27340. +
  27341. +extern void
  27342. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  27343. +
  27344. +static inline VCHIQ_SERVICE_T *
  27345. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  27346. +{
  27347. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  27348. + (VCHIQ_MAX_STATES - 1)];
  27349. + if (!state)
  27350. + return NULL;
  27351. +
  27352. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  27353. +}
  27354. +
  27355. +extern VCHIQ_SERVICE_T *
  27356. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  27357. +
  27358. +extern VCHIQ_SERVICE_T *
  27359. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  27360. +
  27361. +extern VCHIQ_SERVICE_T *
  27362. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  27363. + VCHIQ_SERVICE_HANDLE_T handle);
  27364. +
  27365. +extern VCHIQ_SERVICE_T *
  27366. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  27367. + int *pidx);
  27368. +
  27369. +extern void
  27370. +lock_service(VCHIQ_SERVICE_T *service);
  27371. +
  27372. +extern void
  27373. +unlock_service(VCHIQ_SERVICE_T *service);
  27374. +
  27375. +/* The following functions are called from vchiq_core, and external
  27376. +** implementations must be provided. */
  27377. +
  27378. +extern VCHIQ_STATUS_T
  27379. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  27380. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  27381. +
  27382. +extern void
  27383. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  27384. +
  27385. +extern void
  27386. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  27387. +
  27388. +extern VCHIQ_STATUS_T
  27389. +vchiq_copy_from_user(void *dst, const void *src, int size);
  27390. +
  27391. +extern void
  27392. +remote_event_signal(REMOTE_EVENT_T *event);
  27393. +
  27394. +void
  27395. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  27396. +
  27397. +extern void
  27398. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  27399. +
  27400. +extern VCHIQ_STATUS_T
  27401. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  27402. +
  27403. +extern void
  27404. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  27405. +
  27406. +extern void
  27407. +vchiq_dump(void *dump_context, const char *str, int len);
  27408. +
  27409. +extern void
  27410. +vchiq_dump_platform_state(void *dump_context);
  27411. +
  27412. +extern void
  27413. +vchiq_dump_platform_instances(void *dump_context);
  27414. +
  27415. +extern void
  27416. +vchiq_dump_platform_service_state(void *dump_context,
  27417. + VCHIQ_SERVICE_T *service);
  27418. +
  27419. +extern VCHIQ_STATUS_T
  27420. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  27421. +
  27422. +extern VCHIQ_STATUS_T
  27423. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  27424. +
  27425. +extern void
  27426. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  27427. +
  27428. +extern void
  27429. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  27430. +
  27431. +extern VCHIQ_STATUS_T
  27432. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  27433. +
  27434. +extern VCHIQ_STATUS_T
  27435. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  27436. +
  27437. +extern void
  27438. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  27439. +
  27440. +extern VCHIQ_STATUS_T
  27441. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  27442. +
  27443. +extern VCHIQ_STATUS_T
  27444. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  27445. +
  27446. +extern VCHIQ_STATUS_T
  27447. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  27448. +
  27449. +extern void
  27450. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  27451. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  27452. +
  27453. +extern void
  27454. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  27455. +
  27456. +extern void
  27457. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  27458. +
  27459. +
  27460. +extern void
  27461. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  27462. + size_t numBytes);
  27463. +
  27464. +#endif
  27465. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  27466. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 1970-01-01 01:00:00.000000000 +0100
  27467. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 2014-03-11 16:54:58.000000000 +0100
  27468. @@ -0,0 +1,87 @@
  27469. +#!/usr/bin/perl -w
  27470. +
  27471. +use strict;
  27472. +
  27473. +#
  27474. +# Generate a version from available information
  27475. +#
  27476. +
  27477. +my $prefix = shift @ARGV;
  27478. +my $root = shift @ARGV;
  27479. +
  27480. +
  27481. +if ( not defined $root ) {
  27482. + die "usage: $0 prefix root-dir\n";
  27483. +}
  27484. +
  27485. +if ( ! -d $root ) {
  27486. + die "root directory $root not found\n";
  27487. +}
  27488. +
  27489. +my $version = "unknown";
  27490. +my $tainted = "";
  27491. +
  27492. +if ( -d "$root/.git" ) {
  27493. + # attempt to work out git version. only do so
  27494. + # on a linux build host, as cygwin builds are
  27495. + # already slow enough
  27496. +
  27497. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  27498. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  27499. + $version = "no git version";
  27500. + }
  27501. + else {
  27502. + $version = <F>;
  27503. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  27504. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  27505. + }
  27506. +
  27507. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  27508. + $tainted = <G>;
  27509. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  27510. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  27511. + if (length $tainted) {
  27512. + $version = join ' ', $version, "(tainted)";
  27513. + }
  27514. + else {
  27515. + $version = join ' ', $version, "(clean)";
  27516. + }
  27517. + }
  27518. + }
  27519. +}
  27520. +
  27521. +my $hostname = `hostname`;
  27522. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  27523. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  27524. +
  27525. +
  27526. +print STDERR "Version $version\n";
  27527. +print <<EOF;
  27528. +#include "${prefix}_build_info.h"
  27529. +#include <linux/broadcom/vc_debug_sym.h>
  27530. +
  27531. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  27532. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  27533. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  27534. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  27535. +
  27536. +const char *vchiq_get_build_hostname( void )
  27537. +{
  27538. + return vchiq_build_hostname;
  27539. +}
  27540. +
  27541. +const char *vchiq_get_build_version( void )
  27542. +{
  27543. + return vchiq_build_version;
  27544. +}
  27545. +
  27546. +const char *vchiq_get_build_date( void )
  27547. +{
  27548. + return vchiq_build_date;
  27549. +}
  27550. +
  27551. +const char *vchiq_get_build_time( void )
  27552. +{
  27553. + return vchiq_build_time;
  27554. +}
  27555. +EOF
  27556. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  27557. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 1970-01-01 01:00:00.000000000 +0100
  27558. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 2014-03-11 16:54:58.000000000 +0100
  27559. @@ -0,0 +1,40 @@
  27560. +/**
  27561. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  27562. + *
  27563. + * Redistribution and use in source and binary forms, with or without
  27564. + * modification, are permitted provided that the following conditions
  27565. + * are met:
  27566. + * 1. Redistributions of source code must retain the above copyright
  27567. + * notice, this list of conditions, and the following disclaimer,
  27568. + * without modification.
  27569. + * 2. Redistributions in binary form must reproduce the above copyright
  27570. + * notice, this list of conditions and the following disclaimer in the
  27571. + * documentation and/or other materials provided with the distribution.
  27572. + * 3. The names of the above-listed copyright holders may not be used
  27573. + * to endorse or promote products derived from this software without
  27574. + * specific prior written permission.
  27575. + *
  27576. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27577. + * GNU General Public License ("GPL") version 2, as published by the Free
  27578. + * Software Foundation.
  27579. + *
  27580. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27581. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27582. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27583. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27584. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27585. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27586. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27587. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27588. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27589. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27590. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27591. + */
  27592. +
  27593. +#ifndef VCHIQ_VCHIQ_H
  27594. +#define VCHIQ_VCHIQ_H
  27595. +
  27596. +#include "vchiq_if.h"
  27597. +#include "vchiq_util.h"
  27598. +
  27599. +#endif
  27600. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  27601. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 1970-01-01 01:00:00.000000000 +0100
  27602. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 2014-03-11 16:52:43.000000000 +0100
  27603. @@ -0,0 +1,188 @@
  27604. +/**
  27605. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  27606. + *
  27607. + * Redistribution and use in source and binary forms, with or without
  27608. + * modification, are permitted provided that the following conditions
  27609. + * are met:
  27610. + * 1. Redistributions of source code must retain the above copyright
  27611. + * notice, this list of conditions, and the following disclaimer,
  27612. + * without modification.
  27613. + * 2. Redistributions in binary form must reproduce the above copyright
  27614. + * notice, this list of conditions and the following disclaimer in the
  27615. + * documentation and/or other materials provided with the distribution.
  27616. + * 3. The names of the above-listed copyright holders may not be used
  27617. + * to endorse or promote products derived from this software without
  27618. + * specific prior written permission.
  27619. + *
  27620. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27621. + * GNU General Public License ("GPL") version 2, as published by the Free
  27622. + * Software Foundation.
  27623. + *
  27624. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27625. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27626. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27627. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27628. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27629. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27630. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27631. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27632. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27633. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27634. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27635. + */
  27636. +
  27637. +#ifndef VCHIQ_IF_H
  27638. +#define VCHIQ_IF_H
  27639. +
  27640. +#include "interface/vchi/vchi_mh.h"
  27641. +
  27642. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  27643. +
  27644. +#define VCHIQ_SLOT_SIZE 4096
  27645. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  27646. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  27647. +
  27648. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  27649. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  27650. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  27651. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  27652. +
  27653. +typedef enum {
  27654. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  27655. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  27656. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  27657. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  27658. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  27659. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  27660. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  27661. +} VCHIQ_REASON_T;
  27662. +
  27663. +typedef enum {
  27664. + VCHIQ_ERROR = -1,
  27665. + VCHIQ_SUCCESS = 0,
  27666. + VCHIQ_RETRY = 1
  27667. +} VCHIQ_STATUS_T;
  27668. +
  27669. +typedef enum {
  27670. + VCHIQ_BULK_MODE_CALLBACK,
  27671. + VCHIQ_BULK_MODE_BLOCKING,
  27672. + VCHIQ_BULK_MODE_NOCALLBACK,
  27673. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  27674. +} VCHIQ_BULK_MODE_T;
  27675. +
  27676. +typedef enum {
  27677. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  27678. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  27679. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  27680. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS
  27681. +} VCHIQ_SERVICE_OPTION_T;
  27682. +
  27683. +typedef struct vchiq_header_struct {
  27684. + /* The message identifier - opaque to applications. */
  27685. + int msgid;
  27686. +
  27687. + /* Size of message data. */
  27688. + unsigned int size;
  27689. +
  27690. + char data[0]; /* message */
  27691. +} VCHIQ_HEADER_T;
  27692. +
  27693. +typedef struct {
  27694. + const void *data;
  27695. + unsigned int size;
  27696. +} VCHIQ_ELEMENT_T;
  27697. +
  27698. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  27699. +
  27700. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  27701. + VCHIQ_SERVICE_HANDLE_T, void *);
  27702. +
  27703. +typedef struct vchiq_service_base_struct {
  27704. + int fourcc;
  27705. + VCHIQ_CALLBACK_T callback;
  27706. + void *userdata;
  27707. +} VCHIQ_SERVICE_BASE_T;
  27708. +
  27709. +typedef struct vchiq_service_params_struct {
  27710. + int fourcc;
  27711. + VCHIQ_CALLBACK_T callback;
  27712. + void *userdata;
  27713. + short version; /* Increment for non-trivial changes */
  27714. + short version_min; /* Update for incompatible changes */
  27715. +} VCHIQ_SERVICE_PARAMS_T;
  27716. +
  27717. +typedef struct vchiq_config_struct {
  27718. + unsigned int max_msg_size;
  27719. + unsigned int bulk_threshold; /* The message size above which it
  27720. + is better to use a bulk transfer
  27721. + (<= max_msg_size) */
  27722. + unsigned int max_outstanding_bulks;
  27723. + unsigned int max_services;
  27724. + short version; /* The version of VCHIQ */
  27725. + short version_min; /* The minimum compatible version of VCHIQ */
  27726. +} VCHIQ_CONFIG_T;
  27727. +
  27728. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  27729. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  27730. +
  27731. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  27732. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  27733. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  27734. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  27735. + const VCHIQ_SERVICE_PARAMS_T *params,
  27736. + VCHIQ_SERVICE_HANDLE_T *pservice);
  27737. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  27738. + const VCHIQ_SERVICE_PARAMS_T *params,
  27739. + VCHIQ_SERVICE_HANDLE_T *pservice);
  27740. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  27741. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  27742. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  27743. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  27744. + VCHIQ_SERVICE_HANDLE_T service);
  27745. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  27746. +
  27747. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  27748. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  27749. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  27750. + VCHIQ_HEADER_T *header);
  27751. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  27752. + const void *data, unsigned int size, void *userdata);
  27753. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  27754. + void *data, unsigned int size, void *userdata);
  27755. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  27756. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  27757. + const void *offset, unsigned int size, void *userdata);
  27758. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  27759. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  27760. + void *offset, unsigned int size, void *userdata);
  27761. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  27762. + const void *data, unsigned int size, void *userdata,
  27763. + VCHIQ_BULK_MODE_T mode);
  27764. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  27765. + void *data, unsigned int size, void *userdata,
  27766. + VCHIQ_BULK_MODE_T mode);
  27767. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  27768. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  27769. + void *userdata, VCHIQ_BULK_MODE_T mode);
  27770. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  27771. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  27772. + void *userdata, VCHIQ_BULK_MODE_T mode);
  27773. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  27774. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  27775. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  27776. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  27777. + int config_size, VCHIQ_CONFIG_T *pconfig);
  27778. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  27779. + VCHIQ_SERVICE_OPTION_T option, int value);
  27780. +
  27781. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  27782. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  27783. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  27784. +
  27785. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  27786. + void *ptr, size_t num_bytes);
  27787. +
  27788. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  27789. + short *peer_version);
  27790. +
  27791. +#endif /* VCHIQ_IF_H */
  27792. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  27793. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 1970-01-01 01:00:00.000000000 +0100
  27794. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 2014-03-11 16:52:43.000000000 +0100
  27795. @@ -0,0 +1,129 @@
  27796. +/**
  27797. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  27798. + *
  27799. + * Redistribution and use in source and binary forms, with or without
  27800. + * modification, are permitted provided that the following conditions
  27801. + * are met:
  27802. + * 1. Redistributions of source code must retain the above copyright
  27803. + * notice, this list of conditions, and the following disclaimer,
  27804. + * without modification.
  27805. + * 2. Redistributions in binary form must reproduce the above copyright
  27806. + * notice, this list of conditions and the following disclaimer in the
  27807. + * documentation and/or other materials provided with the distribution.
  27808. + * 3. The names of the above-listed copyright holders may not be used
  27809. + * to endorse or promote products derived from this software without
  27810. + * specific prior written permission.
  27811. + *
  27812. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27813. + * GNU General Public License ("GPL") version 2, as published by the Free
  27814. + * Software Foundation.
  27815. + *
  27816. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27817. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27818. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27819. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27820. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27821. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27822. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27823. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27824. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27825. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27826. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27827. + */
  27828. +
  27829. +#ifndef VCHIQ_IOCTLS_H
  27830. +#define VCHIQ_IOCTLS_H
  27831. +
  27832. +#include <linux/ioctl.h>
  27833. +#include "vchiq_if.h"
  27834. +
  27835. +#define VCHIQ_IOC_MAGIC 0xc4
  27836. +#define VCHIQ_INVALID_HANDLE (~0)
  27837. +
  27838. +typedef struct {
  27839. + VCHIQ_SERVICE_PARAMS_T params;
  27840. + int is_open;
  27841. + int is_vchi;
  27842. + unsigned int handle; /* OUT */
  27843. +} VCHIQ_CREATE_SERVICE_T;
  27844. +
  27845. +typedef struct {
  27846. + unsigned int handle;
  27847. + unsigned int count;
  27848. + const VCHIQ_ELEMENT_T *elements;
  27849. +} VCHIQ_QUEUE_MESSAGE_T;
  27850. +
  27851. +typedef struct {
  27852. + unsigned int handle;
  27853. + void *data;
  27854. + unsigned int size;
  27855. + void *userdata;
  27856. + VCHIQ_BULK_MODE_T mode;
  27857. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  27858. +
  27859. +typedef struct {
  27860. + VCHIQ_REASON_T reason;
  27861. + VCHIQ_HEADER_T *header;
  27862. + void *service_userdata;
  27863. + void *bulk_userdata;
  27864. +} VCHIQ_COMPLETION_DATA_T;
  27865. +
  27866. +typedef struct {
  27867. + unsigned int count;
  27868. + VCHIQ_COMPLETION_DATA_T *buf;
  27869. + unsigned int msgbufsize;
  27870. + unsigned int msgbufcount; /* IN/OUT */
  27871. + void **msgbufs;
  27872. +} VCHIQ_AWAIT_COMPLETION_T;
  27873. +
  27874. +typedef struct {
  27875. + unsigned int handle;
  27876. + int blocking;
  27877. + unsigned int bufsize;
  27878. + void *buf;
  27879. +} VCHIQ_DEQUEUE_MESSAGE_T;
  27880. +
  27881. +typedef struct {
  27882. + unsigned int config_size;
  27883. + VCHIQ_CONFIG_T *pconfig;
  27884. +} VCHIQ_GET_CONFIG_T;
  27885. +
  27886. +typedef struct {
  27887. + unsigned int handle;
  27888. + VCHIQ_SERVICE_OPTION_T option;
  27889. + int value;
  27890. +} VCHIQ_SET_SERVICE_OPTION_T;
  27891. +
  27892. +typedef struct {
  27893. + void *virt_addr;
  27894. + size_t num_bytes;
  27895. +} VCHIQ_DUMP_MEM_T;
  27896. +
  27897. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  27898. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  27899. +#define VCHIQ_IOC_CREATE_SERVICE \
  27900. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  27901. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  27902. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  27903. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  27904. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  27905. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  27906. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  27907. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  27908. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  27909. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  27910. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  27911. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  27912. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  27913. +#define VCHIQ_IOC_GET_CONFIG \
  27914. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  27915. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  27916. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  27917. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  27918. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  27919. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  27920. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  27921. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  27922. +#define VCHIQ_IOC_MAX 15
  27923. +
  27924. +#endif
  27925. diff -Nur linux-3.13.6/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
  27926. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 1970-01-01 01:00:00.000000000 +0100
  27927. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 2014-03-11 16:52:43.000000000 +0100
  27928. @@ -0,0 +1,456 @@
  27929. +/**
  27930. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  27931. + *
  27932. + * Redistribution and use in source and binary forms, with or without
  27933. + * modification, are permitted provided that the following conditions
  27934. + * are met:
  27935. + * 1. Redistributions of source code must retain the above copyright
  27936. + * notice, this list of conditions, and the following disclaimer,
  27937. + * without modification.
  27938. + * 2. Redistributions in binary form must reproduce the above copyright
  27939. + * notice, this list of conditions and the following disclaimer in the
  27940. + * documentation and/or other materials provided with the distribution.
  27941. + * 3. The names of the above-listed copyright holders may not be used
  27942. + * to endorse or promote products derived from this software without
  27943. + * specific prior written permission.
  27944. + *
  27945. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27946. + * GNU General Public License ("GPL") version 2, as published by the Free
  27947. + * Software Foundation.
  27948. + *
  27949. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27950. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27951. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27952. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27953. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27954. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27955. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27956. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27957. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27958. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27959. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27960. + */
  27961. +
  27962. +/* ---- Include Files ---------------------------------------------------- */
  27963. +
  27964. +#include <linux/kernel.h>
  27965. +#include <linux/module.h>
  27966. +#include <linux/mutex.h>
  27967. +
  27968. +#include "vchiq_core.h"
  27969. +#include "vchiq_arm.h"
  27970. +
  27971. +/* ---- Public Variables ------------------------------------------------- */
  27972. +
  27973. +/* ---- Private Constants and Types -------------------------------------- */
  27974. +
  27975. +struct bulk_waiter_node {
  27976. + struct bulk_waiter bulk_waiter;
  27977. + int pid;
  27978. + struct list_head list;
  27979. +};
  27980. +
  27981. +struct vchiq_instance_struct {
  27982. + VCHIQ_STATE_T *state;
  27983. +
  27984. + int connected;
  27985. +
  27986. + struct list_head bulk_waiter_list;
  27987. + struct mutex bulk_waiter_list_mutex;
  27988. +};
  27989. +
  27990. +static VCHIQ_STATUS_T
  27991. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  27992. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  27993. +
  27994. +/****************************************************************************
  27995. +*
  27996. +* vchiq_initialise
  27997. +*
  27998. +***************************************************************************/
  27999. +#define VCHIQ_INIT_RETRIES 10
  28000. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  28001. +{
  28002. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  28003. + VCHIQ_STATE_T *state;
  28004. + VCHIQ_INSTANCE_T instance = NULL;
  28005. + int i;
  28006. +
  28007. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  28008. +
  28009. + /* VideoCore may not be ready due to boot up timing.
  28010. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  28011. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  28012. + state = vchiq_get_state();
  28013. + if (state)
  28014. + break;
  28015. + udelay(500);
  28016. + }
  28017. + if (i==VCHIQ_INIT_RETRIES) {
  28018. + vchiq_log_error(vchiq_core_log_level,
  28019. + "%s: videocore not initialized\n", __func__);
  28020. + goto failed;
  28021. + } else if (i>0) {
  28022. + vchiq_log_warning(vchiq_core_log_level,
  28023. + "%s: videocore initialized after %d retries\n", __func__, i);
  28024. + }
  28025. +
  28026. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  28027. + if (!instance) {
  28028. + vchiq_log_error(vchiq_core_log_level,
  28029. + "%s: error allocating vchiq instance\n", __func__);
  28030. + goto failed;
  28031. + }
  28032. +
  28033. + instance->connected = 0;
  28034. + instance->state = state;
  28035. + mutex_init(&instance->bulk_waiter_list_mutex);
  28036. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  28037. +
  28038. + *instanceOut = instance;
  28039. +
  28040. + status = VCHIQ_SUCCESS;
  28041. +
  28042. +failed:
  28043. + vchiq_log_trace(vchiq_core_log_level,
  28044. + "%s(%p): returning %d", __func__, instance, status);
  28045. +
  28046. + return status;
  28047. +}
  28048. +EXPORT_SYMBOL(vchiq_initialise);
  28049. +
  28050. +/****************************************************************************
  28051. +*
  28052. +* vchiq_shutdown
  28053. +*
  28054. +***************************************************************************/
  28055. +
  28056. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  28057. +{
  28058. + VCHIQ_STATUS_T status;
  28059. + VCHIQ_STATE_T *state = instance->state;
  28060. +
  28061. + vchiq_log_trace(vchiq_core_log_level,
  28062. + "%s(%p) called", __func__, instance);
  28063. +
  28064. + if (mutex_lock_interruptible(&state->mutex) != 0)
  28065. + return VCHIQ_RETRY;
  28066. +
  28067. + /* Remove all services */
  28068. + status = vchiq_shutdown_internal(state, instance);
  28069. +
  28070. + mutex_unlock(&state->mutex);
  28071. +
  28072. + vchiq_log_trace(vchiq_core_log_level,
  28073. + "%s(%p): returning %d", __func__, instance, status);
  28074. +
  28075. + if (status == VCHIQ_SUCCESS) {
  28076. + struct list_head *pos, *next;
  28077. + list_for_each_safe(pos, next,
  28078. + &instance->bulk_waiter_list) {
  28079. + struct bulk_waiter_node *waiter;
  28080. + waiter = list_entry(pos,
  28081. + struct bulk_waiter_node,
  28082. + list);
  28083. + list_del(pos);
  28084. + vchiq_log_info(vchiq_arm_log_level,
  28085. + "bulk_waiter - cleaned up %x "
  28086. + "for pid %d",
  28087. + (unsigned int)waiter, waiter->pid);
  28088. + kfree(waiter);
  28089. + }
  28090. + kfree(instance);
  28091. + }
  28092. +
  28093. + return status;
  28094. +}
  28095. +EXPORT_SYMBOL(vchiq_shutdown);
  28096. +
  28097. +/****************************************************************************
  28098. +*
  28099. +* vchiq_is_connected
  28100. +*
  28101. +***************************************************************************/
  28102. +
  28103. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  28104. +{
  28105. + return instance->connected;
  28106. +}
  28107. +
  28108. +/****************************************************************************
  28109. +*
  28110. +* vchiq_connect
  28111. +*
  28112. +***************************************************************************/
  28113. +
  28114. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  28115. +{
  28116. + VCHIQ_STATUS_T status;
  28117. + VCHIQ_STATE_T *state = instance->state;
  28118. +
  28119. + vchiq_log_trace(vchiq_core_log_level,
  28120. + "%s(%p) called", __func__, instance);
  28121. +
  28122. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  28123. + vchiq_log_trace(vchiq_core_log_level,
  28124. + "%s: call to mutex_lock failed", __func__);
  28125. + status = VCHIQ_RETRY;
  28126. + goto failed;
  28127. + }
  28128. + status = vchiq_connect_internal(state, instance);
  28129. +
  28130. + if (status == VCHIQ_SUCCESS)
  28131. + instance->connected = 1;
  28132. +
  28133. + mutex_unlock(&state->mutex);
  28134. +
  28135. +failed:
  28136. + vchiq_log_trace(vchiq_core_log_level,
  28137. + "%s(%p): returning %d", __func__, instance, status);
  28138. +
  28139. + return status;
  28140. +}
  28141. +EXPORT_SYMBOL(vchiq_connect);
  28142. +
  28143. +/****************************************************************************
  28144. +*
  28145. +* vchiq_add_service
  28146. +*
  28147. +***************************************************************************/
  28148. +
  28149. +VCHIQ_STATUS_T vchiq_add_service(
  28150. + VCHIQ_INSTANCE_T instance,
  28151. + const VCHIQ_SERVICE_PARAMS_T *params,
  28152. + VCHIQ_SERVICE_HANDLE_T *phandle)
  28153. +{
  28154. + VCHIQ_STATUS_T status;
  28155. + VCHIQ_STATE_T *state = instance->state;
  28156. + VCHIQ_SERVICE_T *service = NULL;
  28157. + int srvstate;
  28158. +
  28159. + vchiq_log_trace(vchiq_core_log_level,
  28160. + "%s(%p) called", __func__, instance);
  28161. +
  28162. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  28163. +
  28164. + srvstate = vchiq_is_connected(instance)
  28165. + ? VCHIQ_SRVSTATE_LISTENING
  28166. + : VCHIQ_SRVSTATE_HIDDEN;
  28167. +
  28168. + service = vchiq_add_service_internal(
  28169. + state,
  28170. + params,
  28171. + srvstate,
  28172. + instance,
  28173. + NULL);
  28174. +
  28175. + if (service) {
  28176. + *phandle = service->handle;
  28177. + status = VCHIQ_SUCCESS;
  28178. + } else
  28179. + status = VCHIQ_ERROR;
  28180. +
  28181. + vchiq_log_trace(vchiq_core_log_level,
  28182. + "%s(%p): returning %d", __func__, instance, status);
  28183. +
  28184. + return status;
  28185. +}
  28186. +EXPORT_SYMBOL(vchiq_add_service);
  28187. +
  28188. +/****************************************************************************
  28189. +*
  28190. +* vchiq_open_service
  28191. +*
  28192. +***************************************************************************/
  28193. +
  28194. +VCHIQ_STATUS_T vchiq_open_service(
  28195. + VCHIQ_INSTANCE_T instance,
  28196. + const VCHIQ_SERVICE_PARAMS_T *params,
  28197. + VCHIQ_SERVICE_HANDLE_T *phandle)
  28198. +{
  28199. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  28200. + VCHIQ_STATE_T *state = instance->state;
  28201. + VCHIQ_SERVICE_T *service = NULL;
  28202. +
  28203. + vchiq_log_trace(vchiq_core_log_level,
  28204. + "%s(%p) called", __func__, instance);
  28205. +
  28206. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  28207. +
  28208. + if (!vchiq_is_connected(instance))
  28209. + goto failed;
  28210. +
  28211. + service = vchiq_add_service_internal(state,
  28212. + params,
  28213. + VCHIQ_SRVSTATE_OPENING,
  28214. + instance,
  28215. + NULL);
  28216. +
  28217. + if (service) {
  28218. + status = vchiq_open_service_internal(service, current->pid);
  28219. + if (status == VCHIQ_SUCCESS)
  28220. + *phandle = service->handle;
  28221. + else
  28222. + vchiq_remove_service(service->handle);
  28223. + }
  28224. +
  28225. +failed:
  28226. + vchiq_log_trace(vchiq_core_log_level,
  28227. + "%s(%p): returning %d", __func__, instance, status);
  28228. +
  28229. + return status;
  28230. +}
  28231. +EXPORT_SYMBOL(vchiq_open_service);
  28232. +
  28233. +VCHIQ_STATUS_T
  28234. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  28235. + const void *data, unsigned int size, void *userdata)
  28236. +{
  28237. + return vchiq_bulk_transfer(handle,
  28238. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  28239. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  28240. +}
  28241. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  28242. +
  28243. +VCHIQ_STATUS_T
  28244. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  28245. + unsigned int size, void *userdata)
  28246. +{
  28247. + return vchiq_bulk_transfer(handle,
  28248. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  28249. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  28250. +}
  28251. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  28252. +
  28253. +VCHIQ_STATUS_T
  28254. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  28255. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  28256. +{
  28257. + VCHIQ_STATUS_T status;
  28258. +
  28259. + switch (mode) {
  28260. + case VCHIQ_BULK_MODE_NOCALLBACK:
  28261. + case VCHIQ_BULK_MODE_CALLBACK:
  28262. + status = vchiq_bulk_transfer(handle,
  28263. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  28264. + mode, VCHIQ_BULK_TRANSMIT);
  28265. + break;
  28266. + case VCHIQ_BULK_MODE_BLOCKING:
  28267. + status = vchiq_blocking_bulk_transfer(handle,
  28268. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  28269. + break;
  28270. + default:
  28271. + return VCHIQ_ERROR;
  28272. + }
  28273. +
  28274. + return status;
  28275. +}
  28276. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  28277. +
  28278. +VCHIQ_STATUS_T
  28279. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  28280. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  28281. +{
  28282. + VCHIQ_STATUS_T status;
  28283. +
  28284. + switch (mode) {
  28285. + case VCHIQ_BULK_MODE_NOCALLBACK:
  28286. + case VCHIQ_BULK_MODE_CALLBACK:
  28287. + status = vchiq_bulk_transfer(handle,
  28288. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  28289. + mode, VCHIQ_BULK_RECEIVE);
  28290. + break;
  28291. + case VCHIQ_BULK_MODE_BLOCKING:
  28292. + status = vchiq_blocking_bulk_transfer(handle,
  28293. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  28294. + break;
  28295. + default:
  28296. + return VCHIQ_ERROR;
  28297. + }
  28298. +
  28299. + return status;
  28300. +}
  28301. +EXPORT_SYMBOL(vchiq_bulk_receive);
  28302. +
  28303. +static VCHIQ_STATUS_T
  28304. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  28305. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  28306. +{
  28307. + VCHIQ_INSTANCE_T instance;
  28308. + VCHIQ_SERVICE_T *service;
  28309. + VCHIQ_STATUS_T status;
  28310. + struct bulk_waiter_node *waiter = NULL;
  28311. + struct list_head *pos;
  28312. +
  28313. + service = find_service_by_handle(handle);
  28314. + if (!service)
  28315. + return VCHIQ_ERROR;
  28316. +
  28317. + instance = service->instance;
  28318. +
  28319. + unlock_service(service);
  28320. +
  28321. + mutex_lock(&instance->bulk_waiter_list_mutex);
  28322. + list_for_each(pos, &instance->bulk_waiter_list) {
  28323. + if (list_entry(pos, struct bulk_waiter_node,
  28324. + list)->pid == current->pid) {
  28325. + waiter = list_entry(pos,
  28326. + struct bulk_waiter_node,
  28327. + list);
  28328. + list_del(pos);
  28329. + break;
  28330. + }
  28331. + }
  28332. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  28333. +
  28334. + if (waiter) {
  28335. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  28336. + if (bulk) {
  28337. + /* This thread has an outstanding bulk transfer. */
  28338. + if ((bulk->data != data) ||
  28339. + (bulk->size != size)) {
  28340. + /* This is not a retry of the previous one.
  28341. + ** Cancel the signal when the transfer
  28342. + ** completes. */
  28343. + spin_lock(&bulk_waiter_spinlock);
  28344. + bulk->userdata = NULL;
  28345. + spin_unlock(&bulk_waiter_spinlock);
  28346. + }
  28347. + }
  28348. + }
  28349. +
  28350. + if (!waiter) {
  28351. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  28352. + if (!waiter) {
  28353. + vchiq_log_error(vchiq_core_log_level,
  28354. + "%s - out of memory", __func__);
  28355. + return VCHIQ_ERROR;
  28356. + }
  28357. + }
  28358. +
  28359. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  28360. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  28361. + dir);
  28362. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  28363. + !waiter->bulk_waiter.bulk) {
  28364. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  28365. + if (bulk) {
  28366. + /* Cancel the signal when the transfer
  28367. + ** completes. */
  28368. + spin_lock(&bulk_waiter_spinlock);
  28369. + bulk->userdata = NULL;
  28370. + spin_unlock(&bulk_waiter_spinlock);
  28371. + }
  28372. + kfree(waiter);
  28373. + } else {
  28374. + waiter->pid = current->pid;
  28375. + mutex_lock(&instance->bulk_waiter_list_mutex);
  28376. + list_add(&waiter->list, &instance->bulk_waiter_list);
  28377. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  28378. + vchiq_log_info(vchiq_arm_log_level,
  28379. + "saved bulk_waiter %x for pid %d",
  28380. + (unsigned int)waiter, current->pid);
  28381. + }
  28382. +
  28383. + return status;
  28384. +}
  28385. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  28386. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 1970-01-01 01:00:00.000000000 +0100
  28387. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 2014-03-11 16:52:43.000000000 +0100
  28388. @@ -0,0 +1,71 @@
  28389. +/**
  28390. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28391. + *
  28392. + * Redistribution and use in source and binary forms, with or without
  28393. + * modification, are permitted provided that the following conditions
  28394. + * are met:
  28395. + * 1. Redistributions of source code must retain the above copyright
  28396. + * notice, this list of conditions, and the following disclaimer,
  28397. + * without modification.
  28398. + * 2. Redistributions in binary form must reproduce the above copyright
  28399. + * notice, this list of conditions and the following disclaimer in the
  28400. + * documentation and/or other materials provided with the distribution.
  28401. + * 3. The names of the above-listed copyright holders may not be used
  28402. + * to endorse or promote products derived from this software without
  28403. + * specific prior written permission.
  28404. + *
  28405. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28406. + * GNU General Public License ("GPL") version 2, as published by the Free
  28407. + * Software Foundation.
  28408. + *
  28409. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28410. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28411. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28412. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28413. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28414. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28415. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28416. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28417. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28418. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28419. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28420. + */
  28421. +
  28422. +#ifndef VCHIQ_MEMDRV_H
  28423. +#define VCHIQ_MEMDRV_H
  28424. +
  28425. +/* ---- Include Files ----------------------------------------------------- */
  28426. +
  28427. +#include <linux/kernel.h>
  28428. +#include "vchiq_if.h"
  28429. +
  28430. +/* ---- Constants and Types ---------------------------------------------- */
  28431. +
  28432. +typedef struct {
  28433. + void *armSharedMemVirt;
  28434. + dma_addr_t armSharedMemPhys;
  28435. + size_t armSharedMemSize;
  28436. +
  28437. + void *vcSharedMemVirt;
  28438. + dma_addr_t vcSharedMemPhys;
  28439. + size_t vcSharedMemSize;
  28440. +} VCHIQ_SHARED_MEM_INFO_T;
  28441. +
  28442. +/* ---- Variable Externs ------------------------------------------------- */
  28443. +
  28444. +/* ---- Function Prototypes ---------------------------------------------- */
  28445. +
  28446. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  28447. +
  28448. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  28449. +
  28450. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  28451. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  28452. +
  28453. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  28454. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  28455. +
  28456. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  28457. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  28458. +
  28459. +#endif
  28460. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  28461. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 1970-01-01 01:00:00.000000000 +0100
  28462. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 2014-03-11 16:52:43.000000000 +0100
  28463. @@ -0,0 +1,58 @@
  28464. +/**
  28465. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28466. + *
  28467. + * Redistribution and use in source and binary forms, with or without
  28468. + * modification, are permitted provided that the following conditions
  28469. + * are met:
  28470. + * 1. Redistributions of source code must retain the above copyright
  28471. + * notice, this list of conditions, and the following disclaimer,
  28472. + * without modification.
  28473. + * 2. Redistributions in binary form must reproduce the above copyright
  28474. + * notice, this list of conditions and the following disclaimer in the
  28475. + * documentation and/or other materials provided with the distribution.
  28476. + * 3. The names of the above-listed copyright holders may not be used
  28477. + * to endorse or promote products derived from this software without
  28478. + * specific prior written permission.
  28479. + *
  28480. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28481. + * GNU General Public License ("GPL") version 2, as published by the Free
  28482. + * Software Foundation.
  28483. + *
  28484. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28485. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28486. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28487. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28488. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28489. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28490. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28491. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28492. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28493. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28494. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28495. + */
  28496. +
  28497. +#ifndef VCHIQ_PAGELIST_H
  28498. +#define VCHIQ_PAGELIST_H
  28499. +
  28500. +#ifndef PAGE_SIZE
  28501. +#define PAGE_SIZE 4096
  28502. +#endif
  28503. +#define CACHE_LINE_SIZE 32
  28504. +#define PAGELIST_WRITE 0
  28505. +#define PAGELIST_READ 1
  28506. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  28507. +
  28508. +typedef struct pagelist_struct {
  28509. + unsigned long length;
  28510. + unsigned short type;
  28511. + unsigned short offset;
  28512. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  28513. + pages at consecutive addresses. */
  28514. +} PAGELIST_T;
  28515. +
  28516. +typedef struct fragments_struct {
  28517. + char headbuf[CACHE_LINE_SIZE];
  28518. + char tailbuf[CACHE_LINE_SIZE];
  28519. +} FRAGMENTS_T;
  28520. +
  28521. +#endif /* VCHIQ_PAGELIST_H */
  28522. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c
  28523. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c 1970-01-01 01:00:00.000000000 +0100
  28524. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c 2014-03-11 16:54:58.000000000 +0100
  28525. @@ -0,0 +1,253 @@
  28526. +/**
  28527. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28528. + *
  28529. + * Redistribution and use in source and binary forms, with or without
  28530. + * modification, are permitted provided that the following conditions
  28531. + * are met:
  28532. + * 1. Redistributions of source code must retain the above copyright
  28533. + * notice, this list of conditions, and the following disclaimer,
  28534. + * without modification.
  28535. + * 2. Redistributions in binary form must reproduce the above copyright
  28536. + * notice, this list of conditions and the following disclaimer in the
  28537. + * documentation and/or other materials provided with the distribution.
  28538. + * 3. The names of the above-listed copyright holders may not be used
  28539. + * to endorse or promote products derived from this software without
  28540. + * specific prior written permission.
  28541. + *
  28542. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28543. + * GNU General Public License ("GPL") version 2, as published by the Free
  28544. + * Software Foundation.
  28545. + *
  28546. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28547. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28548. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28549. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28550. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28551. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28552. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28553. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28554. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28555. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28556. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28557. + */
  28558. +
  28559. +
  28560. +#include <linux/proc_fs.h>
  28561. +#include "vchiq_core.h"
  28562. +#include "vchiq_arm.h"
  28563. +
  28564. +#if 1
  28565. +
  28566. +int vchiq_proc_init(void)
  28567. +{
  28568. + return 0;
  28569. +}
  28570. +
  28571. +void vchiq_proc_deinit(void)
  28572. +{
  28573. +}
  28574. +
  28575. +#else
  28576. +
  28577. +struct vchiq_proc_info {
  28578. + /* Global 'vc' proc entry used by all instances */
  28579. + struct proc_dir_entry *vc_cfg_dir;
  28580. +
  28581. + /* one entry per client process */
  28582. + struct proc_dir_entry *clients;
  28583. +
  28584. + /* log categories */
  28585. + struct proc_dir_entry *log_categories;
  28586. +};
  28587. +
  28588. +static struct vchiq_proc_info proc_info;
  28589. +
  28590. +struct proc_dir_entry *vchiq_proc_top(void)
  28591. +{
  28592. + BUG_ON(proc_info.vc_cfg_dir == NULL);
  28593. + return proc_info.vc_cfg_dir;
  28594. +}
  28595. +
  28596. +/****************************************************************************
  28597. +*
  28598. +* log category entries
  28599. +*
  28600. +***************************************************************************/
  28601. +#define PROC_WRITE_BUF_SIZE 256
  28602. +
  28603. +#define VCHIQ_LOG_ERROR_STR "error"
  28604. +#define VCHIQ_LOG_WARNING_STR "warning"
  28605. +#define VCHIQ_LOG_INFO_STR "info"
  28606. +#define VCHIQ_LOG_TRACE_STR "trace"
  28607. +
  28608. +static int log_cfg_read(char *buffer,
  28609. + char **start,
  28610. + off_t off,
  28611. + int count,
  28612. + int *eof,
  28613. + void *data)
  28614. +{
  28615. + int len = 0;
  28616. + char *log_value = NULL;
  28617. +
  28618. + switch (*((int *)data)) {
  28619. + case VCHIQ_LOG_ERROR:
  28620. + log_value = VCHIQ_LOG_ERROR_STR;
  28621. + break;
  28622. + case VCHIQ_LOG_WARNING:
  28623. + log_value = VCHIQ_LOG_WARNING_STR;
  28624. + break;
  28625. + case VCHIQ_LOG_INFO:
  28626. + log_value = VCHIQ_LOG_INFO_STR;
  28627. + break;
  28628. + case VCHIQ_LOG_TRACE:
  28629. + log_value = VCHIQ_LOG_TRACE_STR;
  28630. + break;
  28631. + default:
  28632. + break;
  28633. + }
  28634. +
  28635. + len += sprintf(buffer + len,
  28636. + "%s\n",
  28637. + log_value ? log_value : "(null)");
  28638. +
  28639. + return len;
  28640. +}
  28641. +
  28642. +
  28643. +static int log_cfg_write(struct file *file,
  28644. + const char __user *buffer,
  28645. + unsigned long count,
  28646. + void *data)
  28647. +{
  28648. + int *log_module = data;
  28649. + char kbuf[PROC_WRITE_BUF_SIZE + 1];
  28650. +
  28651. + (void)file;
  28652. +
  28653. + memset(kbuf, 0, PROC_WRITE_BUF_SIZE + 1);
  28654. + if (count >= PROC_WRITE_BUF_SIZE)
  28655. + count = PROC_WRITE_BUF_SIZE;
  28656. +
  28657. + if (copy_from_user(kbuf,
  28658. + buffer,
  28659. + count) != 0)
  28660. + return -EFAULT;
  28661. + kbuf[count - 1] = 0;
  28662. +
  28663. + if (strncmp("error", kbuf, strlen("error")) == 0)
  28664. + *log_module = VCHIQ_LOG_ERROR;
  28665. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  28666. + *log_module = VCHIQ_LOG_WARNING;
  28667. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  28668. + *log_module = VCHIQ_LOG_INFO;
  28669. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  28670. + *log_module = VCHIQ_LOG_TRACE;
  28671. + else
  28672. + *log_module = VCHIQ_LOG_DEFAULT;
  28673. +
  28674. + return count;
  28675. +}
  28676. +
  28677. +/* Log category proc entries */
  28678. +struct vchiq_proc_log_entry {
  28679. + const char *name;
  28680. + int *plevel;
  28681. + struct proc_dir_entry *dir;
  28682. +};
  28683. +
  28684. +static struct vchiq_proc_log_entry vchiq_proc_log_entries[] = {
  28685. + { "core", &vchiq_core_log_level },
  28686. + { "msg", &vchiq_core_msg_log_level },
  28687. + { "sync", &vchiq_sync_log_level },
  28688. + { "susp", &vchiq_susp_log_level },
  28689. + { "arm", &vchiq_arm_log_level },
  28690. +};
  28691. +static int n_log_entries =
  28692. + sizeof(vchiq_proc_log_entries)/sizeof(vchiq_proc_log_entries[0]);
  28693. +
  28694. +/* create an entry under /proc/vc/log for each log category */
  28695. +static int vchiq_proc_create_log_entries(struct proc_dir_entry *top)
  28696. +{
  28697. + struct proc_dir_entry *dir;
  28698. + size_t i;
  28699. + int ret = 0;
  28700. + dir = proc_mkdir("log", proc_info.vc_cfg_dir);
  28701. + if (!dir)
  28702. + return -ENOMEM;
  28703. + proc_info.log_categories = dir;
  28704. +
  28705. + for (i = 0; i < n_log_entries; i++) {
  28706. + dir = create_proc_entry(vchiq_proc_log_entries[i].name,
  28707. + 0644,
  28708. + proc_info.log_categories);
  28709. + if (!dir) {
  28710. + ret = -ENOMEM;
  28711. + break;
  28712. + }
  28713. +
  28714. + dir->read_proc = &log_cfg_read;
  28715. + dir->write_proc = &log_cfg_write;
  28716. + dir->data = (void *)vchiq_proc_log_entries[i].plevel;
  28717. +
  28718. + vchiq_proc_log_entries[i].dir = dir;
  28719. + }
  28720. + return ret;
  28721. +}
  28722. +
  28723. +
  28724. +int vchiq_proc_init(void)
  28725. +{
  28726. + BUG_ON(proc_info.vc_cfg_dir != NULL);
  28727. +
  28728. + proc_info.vc_cfg_dir = proc_mkdir("vc", NULL);
  28729. + if (proc_info.vc_cfg_dir == NULL)
  28730. + goto fail;
  28731. +
  28732. + proc_info.clients = proc_mkdir("clients",
  28733. + proc_info.vc_cfg_dir);
  28734. + if (!proc_info.clients)
  28735. + goto fail;
  28736. +
  28737. + if (vchiq_proc_create_log_entries(proc_info.vc_cfg_dir) != 0)
  28738. + goto fail;
  28739. +
  28740. + return 0;
  28741. +
  28742. +fail:
  28743. + vchiq_proc_deinit();
  28744. + vchiq_log_error(vchiq_arm_log_level,
  28745. + "%s: failed to create proc directory",
  28746. + __func__);
  28747. +
  28748. + return -ENOMEM;
  28749. +}
  28750. +
  28751. +/* remove all the proc entries */
  28752. +void vchiq_proc_deinit(void)
  28753. +{
  28754. + /* log category entries */
  28755. + if (proc_info.log_categories) {
  28756. + size_t i;
  28757. + for (i = 0; i < n_log_entries; i++)
  28758. + if (vchiq_proc_log_entries[i].dir)
  28759. + remove_proc_entry(
  28760. + vchiq_proc_log_entries[i].name,
  28761. + proc_info.log_categories);
  28762. +
  28763. + remove_proc_entry(proc_info.log_categories->name,
  28764. + proc_info.vc_cfg_dir);
  28765. + }
  28766. + if (proc_info.clients)
  28767. + remove_proc_entry(proc_info.clients->name,
  28768. + proc_info.vc_cfg_dir);
  28769. + if (proc_info.vc_cfg_dir)
  28770. + remove_proc_entry(proc_info.vc_cfg_dir->name, NULL);
  28771. +}
  28772. +
  28773. +struct proc_dir_entry *vchiq_clients_top(void)
  28774. +{
  28775. + return proc_info.clients;
  28776. +}
  28777. +
  28778. +#endif
  28779. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  28780. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 1970-01-01 01:00:00.000000000 +0100
  28781. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 2014-03-11 16:52:43.000000000 +0100
  28782. @@ -0,0 +1,828 @@
  28783. +/**
  28784. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28785. + *
  28786. + * Redistribution and use in source and binary forms, with or without
  28787. + * modification, are permitted provided that the following conditions
  28788. + * are met:
  28789. + * 1. Redistributions of source code must retain the above copyright
  28790. + * notice, this list of conditions, and the following disclaimer,
  28791. + * without modification.
  28792. + * 2. Redistributions in binary form must reproduce the above copyright
  28793. + * notice, this list of conditions and the following disclaimer in the
  28794. + * documentation and/or other materials provided with the distribution.
  28795. + * 3. The names of the above-listed copyright holders may not be used
  28796. + * to endorse or promote products derived from this software without
  28797. + * specific prior written permission.
  28798. + *
  28799. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28800. + * GNU General Public License ("GPL") version 2, as published by the Free
  28801. + * Software Foundation.
  28802. + *
  28803. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28804. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28805. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28806. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28807. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28808. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28809. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28810. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28811. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28812. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28813. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28814. + */
  28815. +#include <linux/module.h>
  28816. +#include <linux/types.h>
  28817. +
  28818. +#include "interface/vchi/vchi.h"
  28819. +#include "vchiq.h"
  28820. +#include "vchiq_core.h"
  28821. +
  28822. +#include "vchiq_util.h"
  28823. +
  28824. +#include <stddef.h>
  28825. +
  28826. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  28827. +
  28828. +typedef struct {
  28829. + VCHIQ_SERVICE_HANDLE_T handle;
  28830. +
  28831. + VCHIU_QUEUE_T queue;
  28832. +
  28833. + VCHI_CALLBACK_T callback;
  28834. + void *callback_param;
  28835. +} SHIM_SERVICE_T;
  28836. +
  28837. +/* ----------------------------------------------------------------------
  28838. + * return pointer to the mphi message driver function table
  28839. + * -------------------------------------------------------------------- */
  28840. +const VCHI_MESSAGE_DRIVER_T *
  28841. +vchi_mphi_message_driver_func_table(void)
  28842. +{
  28843. + return NULL;
  28844. +}
  28845. +
  28846. +/* ----------------------------------------------------------------------
  28847. + * return a pointer to the 'single' connection driver fops
  28848. + * -------------------------------------------------------------------- */
  28849. +const VCHI_CONNECTION_API_T *
  28850. +single_get_func_table(void)
  28851. +{
  28852. + return NULL;
  28853. +}
  28854. +
  28855. +VCHI_CONNECTION_T *vchi_create_connection(
  28856. + const VCHI_CONNECTION_API_T *function_table,
  28857. + const VCHI_MESSAGE_DRIVER_T *low_level)
  28858. +{
  28859. + (void)function_table;
  28860. + (void)low_level;
  28861. + return NULL;
  28862. +}
  28863. +
  28864. +/***********************************************************
  28865. + * Name: vchi_msg_peek
  28866. + *
  28867. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  28868. + * void **data,
  28869. + * uint32_t *msg_size,
  28870. +
  28871. +
  28872. + * VCHI_FLAGS_T flags
  28873. + *
  28874. + * Description: Routine to return a pointer to the current message (to allow in
  28875. + * place processing). The message can be removed using
  28876. + * vchi_msg_remove when you're finished
  28877. + *
  28878. + * Returns: int32_t - success == 0
  28879. + *
  28880. + ***********************************************************/
  28881. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  28882. + void **data,
  28883. + uint32_t *msg_size,
  28884. + VCHI_FLAGS_T flags)
  28885. +{
  28886. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  28887. + VCHIQ_HEADER_T *header;
  28888. +
  28889. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  28890. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  28891. +
  28892. + if (flags == VCHI_FLAGS_NONE)
  28893. + if (vchiu_queue_is_empty(&service->queue))
  28894. + return -1;
  28895. +
  28896. + header = vchiu_queue_peek(&service->queue);
  28897. +
  28898. + *data = header->data;
  28899. + *msg_size = header->size;
  28900. +
  28901. + return 0;
  28902. +}
  28903. +EXPORT_SYMBOL(vchi_msg_peek);
  28904. +
  28905. +/***********************************************************
  28906. + * Name: vchi_msg_remove
  28907. + *
  28908. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  28909. + *
  28910. + * Description: Routine to remove a message (after it has been read with
  28911. + * vchi_msg_peek)
  28912. + *
  28913. + * Returns: int32_t - success == 0
  28914. + *
  28915. + ***********************************************************/
  28916. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  28917. +{
  28918. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  28919. + VCHIQ_HEADER_T *header;
  28920. +
  28921. + header = vchiu_queue_pop(&service->queue);
  28922. +
  28923. + vchiq_release_message(service->handle, header);
  28924. +
  28925. + return 0;
  28926. +}
  28927. +EXPORT_SYMBOL(vchi_msg_remove);
  28928. +
  28929. +/***********************************************************
  28930. + * Name: vchi_msg_queue
  28931. + *
  28932. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  28933. + * const void *data,
  28934. + * uint32_t data_size,
  28935. + * VCHI_FLAGS_T flags,
  28936. + * void *msg_handle,
  28937. + *
  28938. + * Description: Thin wrapper to queue a message onto a connection
  28939. + *
  28940. + * Returns: int32_t - success == 0
  28941. + *
  28942. + ***********************************************************/
  28943. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  28944. + const void *data,
  28945. + uint32_t data_size,
  28946. + VCHI_FLAGS_T flags,
  28947. + void *msg_handle)
  28948. +{
  28949. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  28950. + VCHIQ_ELEMENT_T element = {data, data_size};
  28951. + VCHIQ_STATUS_T status;
  28952. +
  28953. + (void)msg_handle;
  28954. +
  28955. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  28956. +
  28957. + status = vchiq_queue_message(service->handle, &element, 1);
  28958. +
  28959. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  28960. + ** implement a retry mechanism since this function is supposed
  28961. + ** to block until queued
  28962. + */
  28963. + while (status == VCHIQ_RETRY) {
  28964. + msleep(1);
  28965. + status = vchiq_queue_message(service->handle, &element, 1);
  28966. + }
  28967. +
  28968. + return vchiq_status_to_vchi(status);
  28969. +}
  28970. +EXPORT_SYMBOL(vchi_msg_queue);
  28971. +
  28972. +/***********************************************************
  28973. + * Name: vchi_bulk_queue_receive
  28974. + *
  28975. + * Arguments: VCHI_BULK_HANDLE_T handle,
  28976. + * void *data_dst,
  28977. + * const uint32_t data_size,
  28978. + * VCHI_FLAGS_T flags
  28979. + * void *bulk_handle
  28980. + *
  28981. + * Description: Routine to setup a rcv buffer
  28982. + *
  28983. + * Returns: int32_t - success == 0
  28984. + *
  28985. + ***********************************************************/
  28986. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  28987. + void *data_dst,
  28988. + uint32_t data_size,
  28989. + VCHI_FLAGS_T flags,
  28990. + void *bulk_handle)
  28991. +{
  28992. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  28993. + VCHIQ_BULK_MODE_T mode;
  28994. + VCHIQ_STATUS_T status;
  28995. +
  28996. + switch ((int)flags) {
  28997. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  28998. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  28999. + WARN_ON(!service->callback);
  29000. + mode = VCHIQ_BULK_MODE_CALLBACK;
  29001. + break;
  29002. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  29003. + mode = VCHIQ_BULK_MODE_BLOCKING;
  29004. + break;
  29005. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29006. + case VCHI_FLAGS_NONE:
  29007. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  29008. + break;
  29009. + default:
  29010. + WARN(1, "unsupported message\n");
  29011. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  29012. + }
  29013. +
  29014. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  29015. + bulk_handle, mode);
  29016. +
  29017. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  29018. + ** implement a retry mechanism since this function is supposed
  29019. + ** to block until queued
  29020. + */
  29021. + while (status == VCHIQ_RETRY) {
  29022. + msleep(1);
  29023. + status = vchiq_bulk_receive(service->handle, data_dst,
  29024. + data_size, bulk_handle, mode);
  29025. + }
  29026. +
  29027. + return vchiq_status_to_vchi(status);
  29028. +}
  29029. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  29030. +
  29031. +/***********************************************************
  29032. + * Name: vchi_bulk_queue_transmit
  29033. + *
  29034. + * Arguments: VCHI_BULK_HANDLE_T handle,
  29035. + * const void *data_src,
  29036. + * uint32_t data_size,
  29037. + * VCHI_FLAGS_T flags,
  29038. + * void *bulk_handle
  29039. + *
  29040. + * Description: Routine to transmit some data
  29041. + *
  29042. + * Returns: int32_t - success == 0
  29043. + *
  29044. + ***********************************************************/
  29045. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  29046. + const void *data_src,
  29047. + uint32_t data_size,
  29048. + VCHI_FLAGS_T flags,
  29049. + void *bulk_handle)
  29050. +{
  29051. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29052. + VCHIQ_BULK_MODE_T mode;
  29053. + VCHIQ_STATUS_T status;
  29054. +
  29055. + switch ((int)flags) {
  29056. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  29057. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29058. + WARN_ON(!service->callback);
  29059. + mode = VCHIQ_BULK_MODE_CALLBACK;
  29060. + break;
  29061. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  29062. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  29063. + mode = VCHIQ_BULK_MODE_BLOCKING;
  29064. + break;
  29065. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  29066. + case VCHI_FLAGS_NONE:
  29067. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  29068. + break;
  29069. + default:
  29070. + WARN(1, "unsupported message\n");
  29071. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  29072. + }
  29073. +
  29074. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  29075. + bulk_handle, mode);
  29076. +
  29077. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  29078. + ** implement a retry mechanism since this function is supposed
  29079. + ** to block until queued
  29080. + */
  29081. + while (status == VCHIQ_RETRY) {
  29082. + msleep(1);
  29083. + status = vchiq_bulk_transmit(service->handle, data_src,
  29084. + data_size, bulk_handle, mode);
  29085. + }
  29086. +
  29087. + return vchiq_status_to_vchi(status);
  29088. +}
  29089. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  29090. +
  29091. +/***********************************************************
  29092. + * Name: vchi_msg_dequeue
  29093. + *
  29094. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29095. + * void *data,
  29096. + * uint32_t max_data_size_to_read,
  29097. + * uint32_t *actual_msg_size
  29098. + * VCHI_FLAGS_T flags
  29099. + *
  29100. + * Description: Routine to dequeue a message into the supplied buffer
  29101. + *
  29102. + * Returns: int32_t - success == 0
  29103. + *
  29104. + ***********************************************************/
  29105. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  29106. + void *data,
  29107. + uint32_t max_data_size_to_read,
  29108. + uint32_t *actual_msg_size,
  29109. + VCHI_FLAGS_T flags)
  29110. +{
  29111. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29112. + VCHIQ_HEADER_T *header;
  29113. +
  29114. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  29115. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  29116. +
  29117. + if (flags == VCHI_FLAGS_NONE)
  29118. + if (vchiu_queue_is_empty(&service->queue))
  29119. + return -1;
  29120. +
  29121. + header = vchiu_queue_pop(&service->queue);
  29122. +
  29123. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  29124. + header->size : max_data_size_to_read);
  29125. +
  29126. + *actual_msg_size = header->size;
  29127. +
  29128. + vchiq_release_message(service->handle, header);
  29129. +
  29130. + return 0;
  29131. +}
  29132. +EXPORT_SYMBOL(vchi_msg_dequeue);
  29133. +
  29134. +/***********************************************************
  29135. + * Name: vchi_msg_queuev
  29136. + *
  29137. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29138. + * VCHI_MSG_VECTOR_T *vector,
  29139. + * uint32_t count,
  29140. + * VCHI_FLAGS_T flags,
  29141. + * void *msg_handle
  29142. + *
  29143. + * Description: Thin wrapper to queue a message onto a connection
  29144. + *
  29145. + * Returns: int32_t - success == 0
  29146. + *
  29147. + ***********************************************************/
  29148. +
  29149. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  29150. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  29151. + offsetof(VCHIQ_ELEMENT_T, data));
  29152. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  29153. + offsetof(VCHIQ_ELEMENT_T, size));
  29154. +
  29155. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  29156. + VCHI_MSG_VECTOR_T *vector,
  29157. + uint32_t count,
  29158. + VCHI_FLAGS_T flags,
  29159. + void *msg_handle)
  29160. +{
  29161. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29162. +
  29163. + (void)msg_handle;
  29164. +
  29165. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  29166. +
  29167. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  29168. + (const VCHIQ_ELEMENT_T *)vector, count));
  29169. +}
  29170. +EXPORT_SYMBOL(vchi_msg_queuev);
  29171. +
  29172. +/***********************************************************
  29173. + * Name: vchi_held_msg_release
  29174. + *
  29175. + * Arguments: VCHI_HELD_MSG_T *message
  29176. + *
  29177. + * Description: Routine to release a held message (after it has been read with
  29178. + * vchi_msg_hold)
  29179. + *
  29180. + * Returns: int32_t - success == 0
  29181. + *
  29182. + ***********************************************************/
  29183. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  29184. +{
  29185. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  29186. + (VCHIQ_HEADER_T *)message->message);
  29187. +
  29188. + return 0;
  29189. +}
  29190. +EXPORT_SYMBOL(vchi_held_msg_release);
  29191. +
  29192. +/***********************************************************
  29193. + * Name: vchi_msg_hold
  29194. + *
  29195. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  29196. + * void **data,
  29197. + * uint32_t *msg_size,
  29198. + * VCHI_FLAGS_T flags,
  29199. + * VCHI_HELD_MSG_T *message_handle
  29200. + *
  29201. + * Description: Routine to return a pointer to the current message (to allow
  29202. + * in place processing). The message is dequeued - don't forget
  29203. + * to release the message using vchi_held_msg_release when you're
  29204. + * finished.
  29205. + *
  29206. + * Returns: int32_t - success == 0
  29207. + *
  29208. + ***********************************************************/
  29209. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  29210. + void **data,
  29211. + uint32_t *msg_size,
  29212. + VCHI_FLAGS_T flags,
  29213. + VCHI_HELD_MSG_T *message_handle)
  29214. +{
  29215. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29216. + VCHIQ_HEADER_T *header;
  29217. +
  29218. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  29219. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  29220. +
  29221. + if (flags == VCHI_FLAGS_NONE)
  29222. + if (vchiu_queue_is_empty(&service->queue))
  29223. + return -1;
  29224. +
  29225. + header = vchiu_queue_pop(&service->queue);
  29226. +
  29227. + *data = header->data;
  29228. + *msg_size = header->size;
  29229. +
  29230. + message_handle->service =
  29231. + (struct opaque_vchi_service_t *)service->handle;
  29232. + message_handle->message = header;
  29233. +
  29234. + return 0;
  29235. +}
  29236. +EXPORT_SYMBOL(vchi_msg_hold);
  29237. +
  29238. +/***********************************************************
  29239. + * Name: vchi_initialise
  29240. + *
  29241. + * Arguments: VCHI_INSTANCE_T *instance_handle
  29242. + * VCHI_CONNECTION_T **connections
  29243. + * const uint32_t num_connections
  29244. + *
  29245. + * Description: Initialises the hardware but does not transmit anything
  29246. + * When run as a Host App this will be called twice hence the need
  29247. + * to malloc the state information
  29248. + *
  29249. + * Returns: 0 if successful, failure otherwise
  29250. + *
  29251. + ***********************************************************/
  29252. +
  29253. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  29254. +{
  29255. + VCHIQ_INSTANCE_T instance;
  29256. + VCHIQ_STATUS_T status;
  29257. +
  29258. + status = vchiq_initialise(&instance);
  29259. +
  29260. + *instance_handle = (VCHI_INSTANCE_T)instance;
  29261. +
  29262. + return vchiq_status_to_vchi(status);
  29263. +}
  29264. +EXPORT_SYMBOL(vchi_initialise);
  29265. +
  29266. +/***********************************************************
  29267. + * Name: vchi_connect
  29268. + *
  29269. + * Arguments: VCHI_CONNECTION_T **connections
  29270. + * const uint32_t num_connections
  29271. + * VCHI_INSTANCE_T instance_handle)
  29272. + *
  29273. + * Description: Starts the command service on each connection,
  29274. + * causing INIT messages to be pinged back and forth
  29275. + *
  29276. + * Returns: 0 if successful, failure otherwise
  29277. + *
  29278. + ***********************************************************/
  29279. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  29280. + const uint32_t num_connections,
  29281. + VCHI_INSTANCE_T instance_handle)
  29282. +{
  29283. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  29284. +
  29285. + (void)connections;
  29286. + (void)num_connections;
  29287. +
  29288. + return vchiq_connect(instance);
  29289. +}
  29290. +EXPORT_SYMBOL(vchi_connect);
  29291. +
  29292. +
  29293. +/***********************************************************
  29294. + * Name: vchi_disconnect
  29295. + *
  29296. + * Arguments: VCHI_INSTANCE_T instance_handle
  29297. + *
  29298. + * Description: Stops the command service on each connection,
  29299. + * causing DE-INIT messages to be pinged back and forth
  29300. + *
  29301. + * Returns: 0 if successful, failure otherwise
  29302. + *
  29303. + ***********************************************************/
  29304. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  29305. +{
  29306. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  29307. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  29308. +}
  29309. +EXPORT_SYMBOL(vchi_disconnect);
  29310. +
  29311. +
  29312. +/***********************************************************
  29313. + * Name: vchi_service_open
  29314. + * Name: vchi_service_create
  29315. + *
  29316. + * Arguments: VCHI_INSTANCE_T *instance_handle
  29317. + * SERVICE_CREATION_T *setup,
  29318. + * VCHI_SERVICE_HANDLE_T *handle
  29319. + *
  29320. + * Description: Routine to open a service
  29321. + *
  29322. + * Returns: int32_t - success == 0
  29323. + *
  29324. + ***********************************************************/
  29325. +
  29326. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  29327. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  29328. +{
  29329. + SHIM_SERVICE_T *service =
  29330. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  29331. +
  29332. + if (!service->callback)
  29333. + goto release;
  29334. +
  29335. + switch (reason) {
  29336. + case VCHIQ_MESSAGE_AVAILABLE:
  29337. + vchiu_queue_push(&service->queue, header);
  29338. +
  29339. + service->callback(service->callback_param,
  29340. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  29341. +
  29342. + goto done;
  29343. + break;
  29344. +
  29345. + case VCHIQ_BULK_TRANSMIT_DONE:
  29346. + service->callback(service->callback_param,
  29347. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  29348. + break;
  29349. +
  29350. + case VCHIQ_BULK_RECEIVE_DONE:
  29351. + service->callback(service->callback_param,
  29352. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  29353. + break;
  29354. +
  29355. + case VCHIQ_SERVICE_CLOSED:
  29356. + service->callback(service->callback_param,
  29357. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  29358. + break;
  29359. +
  29360. + case VCHIQ_SERVICE_OPENED:
  29361. + /* No equivalent VCHI reason */
  29362. + break;
  29363. +
  29364. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  29365. + service->callback(service->callback_param,
  29366. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  29367. + bulk_user);
  29368. + break;
  29369. +
  29370. + case VCHIQ_BULK_RECEIVE_ABORTED:
  29371. + service->callback(service->callback_param,
  29372. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  29373. + bulk_user);
  29374. + break;
  29375. +
  29376. + default:
  29377. + WARN(1, "not supported\n");
  29378. + break;
  29379. + }
  29380. +
  29381. +release:
  29382. + vchiq_release_message(service->handle, header);
  29383. +done:
  29384. + return VCHIQ_SUCCESS;
  29385. +}
  29386. +
  29387. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  29388. + SERVICE_CREATION_T *setup)
  29389. +{
  29390. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  29391. +
  29392. + (void)instance;
  29393. +
  29394. + if (service) {
  29395. + if (vchiu_queue_init(&service->queue, 64)) {
  29396. + service->callback = setup->callback;
  29397. + service->callback_param = setup->callback_param;
  29398. + } else {
  29399. + kfree(service);
  29400. + service = NULL;
  29401. + }
  29402. + }
  29403. +
  29404. + return service;
  29405. +}
  29406. +
  29407. +static void service_free(SHIM_SERVICE_T *service)
  29408. +{
  29409. + if (service) {
  29410. + vchiu_queue_delete(&service->queue);
  29411. + kfree(service);
  29412. + }
  29413. +}
  29414. +
  29415. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  29416. + SERVICE_CREATION_T *setup,
  29417. + VCHI_SERVICE_HANDLE_T *handle)
  29418. +{
  29419. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  29420. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  29421. + if (service) {
  29422. + VCHIQ_SERVICE_PARAMS_T params;
  29423. + VCHIQ_STATUS_T status;
  29424. +
  29425. + memset(&params, 0, sizeof(params));
  29426. + params.fourcc = setup->service_id;
  29427. + params.callback = shim_callback;
  29428. + params.userdata = service;
  29429. + params.version = setup->version.version;
  29430. + params.version_min = setup->version.version_min;
  29431. +
  29432. + status = vchiq_open_service(instance, &params,
  29433. + &service->handle);
  29434. + if (status != VCHIQ_SUCCESS) {
  29435. + service_free(service);
  29436. + service = NULL;
  29437. + }
  29438. + }
  29439. +
  29440. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  29441. +
  29442. + return (service != NULL) ? 0 : -1;
  29443. +}
  29444. +EXPORT_SYMBOL(vchi_service_open);
  29445. +
  29446. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  29447. + SERVICE_CREATION_T *setup,
  29448. + VCHI_SERVICE_HANDLE_T *handle)
  29449. +{
  29450. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  29451. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  29452. + if (service) {
  29453. + VCHIQ_SERVICE_PARAMS_T params;
  29454. + VCHIQ_STATUS_T status;
  29455. +
  29456. + memset(&params, 0, sizeof(params));
  29457. + params.fourcc = setup->service_id;
  29458. + params.callback = shim_callback;
  29459. + params.userdata = service;
  29460. + params.version = setup->version.version;
  29461. + params.version_min = setup->version.version_min;
  29462. + status = vchiq_add_service(instance, &params, &service->handle);
  29463. +
  29464. + if (status != VCHIQ_SUCCESS) {
  29465. + service_free(service);
  29466. + service = NULL;
  29467. + }
  29468. + }
  29469. +
  29470. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  29471. +
  29472. + return (service != NULL) ? 0 : -1;
  29473. +}
  29474. +EXPORT_SYMBOL(vchi_service_create);
  29475. +
  29476. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  29477. +{
  29478. + int32_t ret = -1;
  29479. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29480. + if (service) {
  29481. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  29482. + if (status == VCHIQ_SUCCESS) {
  29483. + service_free(service);
  29484. + service = NULL;
  29485. + }
  29486. +
  29487. + ret = vchiq_status_to_vchi(status);
  29488. + }
  29489. + return ret;
  29490. +}
  29491. +EXPORT_SYMBOL(vchi_service_close);
  29492. +
  29493. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  29494. +{
  29495. + int32_t ret = -1;
  29496. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29497. + if (service) {
  29498. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  29499. + if (status == VCHIQ_SUCCESS) {
  29500. + service_free(service);
  29501. + service = NULL;
  29502. + }
  29503. +
  29504. + ret = vchiq_status_to_vchi(status);
  29505. + }
  29506. + return ret;
  29507. +}
  29508. +EXPORT_SYMBOL(vchi_service_destroy);
  29509. +
  29510. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  29511. +{
  29512. + int32_t ret = -1;
  29513. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29514. + if(service)
  29515. + {
  29516. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  29517. + ret = vchiq_status_to_vchi( status );
  29518. + }
  29519. + return ret;
  29520. +}
  29521. +EXPORT_SYMBOL(vchi_get_peer_version);
  29522. +
  29523. +/* ----------------------------------------------------------------------
  29524. + * read a uint32_t from buffer.
  29525. + * network format is defined to be little endian
  29526. + * -------------------------------------------------------------------- */
  29527. +uint32_t
  29528. +vchi_readbuf_uint32(const void *_ptr)
  29529. +{
  29530. + const unsigned char *ptr = _ptr;
  29531. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  29532. +}
  29533. +
  29534. +/* ----------------------------------------------------------------------
  29535. + * write a uint32_t to buffer.
  29536. + * network format is defined to be little endian
  29537. + * -------------------------------------------------------------------- */
  29538. +void
  29539. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  29540. +{
  29541. + unsigned char *ptr = _ptr;
  29542. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  29543. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  29544. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  29545. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  29546. +}
  29547. +
  29548. +/* ----------------------------------------------------------------------
  29549. + * read a uint16_t from buffer.
  29550. + * network format is defined to be little endian
  29551. + * -------------------------------------------------------------------- */
  29552. +uint16_t
  29553. +vchi_readbuf_uint16(const void *_ptr)
  29554. +{
  29555. + const unsigned char *ptr = _ptr;
  29556. + return ptr[0] | (ptr[1] << 8);
  29557. +}
  29558. +
  29559. +/* ----------------------------------------------------------------------
  29560. + * write a uint16_t into the buffer.
  29561. + * network format is defined to be little endian
  29562. + * -------------------------------------------------------------------- */
  29563. +void
  29564. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  29565. +{
  29566. + unsigned char *ptr = _ptr;
  29567. + ptr[0] = (value >> 0) & 0xFF;
  29568. + ptr[1] = (value >> 8) & 0xFF;
  29569. +}
  29570. +
  29571. +/***********************************************************
  29572. + * Name: vchi_service_use
  29573. + *
  29574. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  29575. + *
  29576. + * Description: Routine to increment refcount on a service
  29577. + *
  29578. + * Returns: void
  29579. + *
  29580. + ***********************************************************/
  29581. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  29582. +{
  29583. + int32_t ret = -1;
  29584. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29585. + if (service)
  29586. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  29587. + return ret;
  29588. +}
  29589. +EXPORT_SYMBOL(vchi_service_use);
  29590. +
  29591. +/***********************************************************
  29592. + * Name: vchi_service_release
  29593. + *
  29594. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  29595. + *
  29596. + * Description: Routine to decrement refcount on a service
  29597. + *
  29598. + * Returns: void
  29599. + *
  29600. + ***********************************************************/
  29601. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  29602. +{
  29603. + int32_t ret = -1;
  29604. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  29605. + if (service)
  29606. + ret = vchiq_status_to_vchi(
  29607. + vchiq_release_service(service->handle));
  29608. + return ret;
  29609. +}
  29610. +EXPORT_SYMBOL(vchi_service_release);
  29611. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  29612. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 1970-01-01 01:00:00.000000000 +0100
  29613. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 2014-03-11 16:52:43.000000000 +0100
  29614. @@ -0,0 +1,151 @@
  29615. +/**
  29616. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29617. + *
  29618. + * Redistribution and use in source and binary forms, with or without
  29619. + * modification, are permitted provided that the following conditions
  29620. + * are met:
  29621. + * 1. Redistributions of source code must retain the above copyright
  29622. + * notice, this list of conditions, and the following disclaimer,
  29623. + * without modification.
  29624. + * 2. Redistributions in binary form must reproduce the above copyright
  29625. + * notice, this list of conditions and the following disclaimer in the
  29626. + * documentation and/or other materials provided with the distribution.
  29627. + * 3. The names of the above-listed copyright holders may not be used
  29628. + * to endorse or promote products derived from this software without
  29629. + * specific prior written permission.
  29630. + *
  29631. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29632. + * GNU General Public License ("GPL") version 2, as published by the Free
  29633. + * Software Foundation.
  29634. + *
  29635. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29636. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29637. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29638. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29639. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29640. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29641. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29642. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29643. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29644. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29645. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29646. + */
  29647. +
  29648. +#include "vchiq_util.h"
  29649. +
  29650. +static inline int is_pow2(int i)
  29651. +{
  29652. + return i && !(i & (i - 1));
  29653. +}
  29654. +
  29655. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  29656. +{
  29657. + WARN_ON(!is_pow2(size));
  29658. +
  29659. + queue->size = size;
  29660. + queue->read = 0;
  29661. + queue->write = 0;
  29662. +
  29663. + sema_init(&queue->pop, 0);
  29664. + sema_init(&queue->push, 0);
  29665. +
  29666. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  29667. + if (queue->storage == NULL) {
  29668. + vchiu_queue_delete(queue);
  29669. + return 0;
  29670. + }
  29671. + return 1;
  29672. +}
  29673. +
  29674. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  29675. +{
  29676. + if (queue->storage != NULL)
  29677. + kfree(queue->storage);
  29678. +}
  29679. +
  29680. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  29681. +{
  29682. + return queue->read == queue->write;
  29683. +}
  29684. +
  29685. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  29686. +{
  29687. + return queue->write == queue->read + queue->size;
  29688. +}
  29689. +
  29690. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  29691. +{
  29692. + while (queue->write == queue->read + queue->size) {
  29693. + if (down_interruptible(&queue->pop) != 0) {
  29694. + flush_signals(current);
  29695. + }
  29696. + }
  29697. +
  29698. + /*
  29699. + * Write to queue->storage must be visible after read from
  29700. + * queue->read
  29701. + */
  29702. + smp_mb();
  29703. +
  29704. + queue->storage[queue->write & (queue->size - 1)] = header;
  29705. +
  29706. + /*
  29707. + * Write to queue->storage must be visible before write to
  29708. + * queue->write
  29709. + */
  29710. + smp_wmb();
  29711. +
  29712. + queue->write++;
  29713. +
  29714. + up(&queue->push);
  29715. +}
  29716. +
  29717. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  29718. +{
  29719. + while (queue->write == queue->read) {
  29720. + if (down_interruptible(&queue->push) != 0) {
  29721. + flush_signals(current);
  29722. + }
  29723. + }
  29724. +
  29725. + up(&queue->push); // We haven't removed anything from the queue.
  29726. +
  29727. + /*
  29728. + * Read from queue->storage must be visible after read from
  29729. + * queue->write
  29730. + */
  29731. + smp_rmb();
  29732. +
  29733. + return queue->storage[queue->read & (queue->size - 1)];
  29734. +}
  29735. +
  29736. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  29737. +{
  29738. + VCHIQ_HEADER_T *header;
  29739. +
  29740. + while (queue->write == queue->read) {
  29741. + if (down_interruptible(&queue->push) != 0) {
  29742. + flush_signals(current);
  29743. + }
  29744. + }
  29745. +
  29746. + /*
  29747. + * Read from queue->storage must be visible after read from
  29748. + * queue->write
  29749. + */
  29750. + smp_rmb();
  29751. +
  29752. + header = queue->storage[queue->read & (queue->size - 1)];
  29753. +
  29754. + /*
  29755. + * Read from queue->storage must be visible before write to
  29756. + * queue->read
  29757. + */
  29758. + smp_mb();
  29759. +
  29760. + queue->read++;
  29761. +
  29762. + up(&queue->pop);
  29763. +
  29764. + return header;
  29765. +}
  29766. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  29767. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 1970-01-01 01:00:00.000000000 +0100
  29768. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 2014-03-11 16:54:58.000000000 +0100
  29769. @@ -0,0 +1,81 @@
  29770. +/**
  29771. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29772. + *
  29773. + * Redistribution and use in source and binary forms, with or without
  29774. + * modification, are permitted provided that the following conditions
  29775. + * are met:
  29776. + * 1. Redistributions of source code must retain the above copyright
  29777. + * notice, this list of conditions, and the following disclaimer,
  29778. + * without modification.
  29779. + * 2. Redistributions in binary form must reproduce the above copyright
  29780. + * notice, this list of conditions and the following disclaimer in the
  29781. + * documentation and/or other materials provided with the distribution.
  29782. + * 3. The names of the above-listed copyright holders may not be used
  29783. + * to endorse or promote products derived from this software without
  29784. + * specific prior written permission.
  29785. + *
  29786. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29787. + * GNU General Public License ("GPL") version 2, as published by the Free
  29788. + * Software Foundation.
  29789. + *
  29790. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29791. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29792. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29793. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29794. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29795. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29796. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29797. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29798. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29799. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29800. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29801. + */
  29802. +
  29803. +#ifndef VCHIQ_UTIL_H
  29804. +#define VCHIQ_UTIL_H
  29805. +
  29806. +#include <linux/types.h>
  29807. +#include <linux/semaphore.h>
  29808. +#include <linux/mutex.h>
  29809. +#include <linux/bitops.h>
  29810. +#include <linux/kthread.h>
  29811. +#include <linux/wait.h>
  29812. +#include <linux/vmalloc.h>
  29813. +#include <linux/jiffies.h>
  29814. +#include <linux/delay.h>
  29815. +#include <linux/string.h>
  29816. +#include <linux/types.h>
  29817. +#include <linux/interrupt.h>
  29818. +#include <linux/random.h>
  29819. +#include <linux/sched.h>
  29820. +#include <linux/ctype.h>
  29821. +#include <linux/uaccess.h>
  29822. +#include <linux/time.h> /* for time_t */
  29823. +#include <linux/slab.h>
  29824. +#include <linux/vmalloc.h>
  29825. +
  29826. +#include "vchiq_if.h"
  29827. +
  29828. +typedef struct {
  29829. + int size;
  29830. + int read;
  29831. + int write;
  29832. +
  29833. + struct semaphore pop;
  29834. + struct semaphore push;
  29835. +
  29836. + VCHIQ_HEADER_T **storage;
  29837. +} VCHIU_QUEUE_T;
  29838. +
  29839. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  29840. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  29841. +
  29842. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  29843. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  29844. +
  29845. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  29846. +
  29847. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  29848. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  29849. +
  29850. +#endif
  29851. diff -Nur linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  29852. --- linux-3.13.6/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 1970-01-01 01:00:00.000000000 +0100
  29853. +++ linux-raspberry-pi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 2014-03-11 16:52:43.000000000 +0100
  29854. @@ -0,0 +1,59 @@
  29855. +/**
  29856. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  29857. + *
  29858. + * Redistribution and use in source and binary forms, with or without
  29859. + * modification, are permitted provided that the following conditions
  29860. + * are met:
  29861. + * 1. Redistributions of source code must retain the above copyright
  29862. + * notice, this list of conditions, and the following disclaimer,
  29863. + * without modification.
  29864. + * 2. Redistributions in binary form must reproduce the above copyright
  29865. + * notice, this list of conditions and the following disclaimer in the
  29866. + * documentation and/or other materials provided with the distribution.
  29867. + * 3. The names of the above-listed copyright holders may not be used
  29868. + * to endorse or promote products derived from this software without
  29869. + * specific prior written permission.
  29870. + *
  29871. + * ALTERNATIVELY, this software may be distributed under the terms of the
  29872. + * GNU General Public License ("GPL") version 2, as published by the Free
  29873. + * Software Foundation.
  29874. + *
  29875. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29876. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29877. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29878. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29879. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29880. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29881. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29882. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29883. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29884. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29885. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29886. + */
  29887. +#include "vchiq_build_info.h"
  29888. +#include <linux/broadcom/vc_debug_sym.h>
  29889. +
  29890. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  29891. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  29892. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  29893. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  29894. +
  29895. +const char *vchiq_get_build_hostname( void )
  29896. +{
  29897. + return vchiq_build_hostname;
  29898. +}
  29899. +
  29900. +const char *vchiq_get_build_version( void )
  29901. +{
  29902. + return vchiq_build_version;
  29903. +}
  29904. +
  29905. +const char *vchiq_get_build_date( void )
  29906. +{
  29907. + return vchiq_build_date;
  29908. +}
  29909. +
  29910. +const char *vchiq_get_build_time( void )
  29911. +{
  29912. + return vchiq_build_time;
  29913. +}
  29914. diff -Nur linux-3.13.6/drivers/misc/vc04_services/Kconfig linux-raspberry-pi/drivers/misc/vc04_services/Kconfig
  29915. --- linux-3.13.6/drivers/misc/vc04_services/Kconfig 1970-01-01 01:00:00.000000000 +0100
  29916. +++ linux-raspberry-pi/drivers/misc/vc04_services/Kconfig 2014-03-11 16:54:58.000000000 +0100
  29917. @@ -0,0 +1,9 @@
  29918. +config BCM2708_VCHIQ
  29919. + tristate "Videocore VCHIQ"
  29920. + depends on MACH_BCM2708
  29921. + default y
  29922. + help
  29923. + Kernel to VideoCore communication interface for the
  29924. + BCM2708 family of products.
  29925. + Defaults to Y when the Broadcom Videocore services
  29926. + are included in the build, N otherwise.
  29927. diff -Nur linux-3.13.6/drivers/misc/vc04_services/Makefile linux-raspberry-pi/drivers/misc/vc04_services/Makefile
  29928. --- linux-3.13.6/drivers/misc/vc04_services/Makefile 1970-01-01 01:00:00.000000000 +0100
  29929. +++ linux-raspberry-pi/drivers/misc/vc04_services/Makefile 2014-03-11 16:54:58.000000000 +0100
  29930. @@ -0,0 +1,17 @@
  29931. +ifeq ($(CONFIG_MACH_BCM2708),y)
  29932. +
  29933. +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
  29934. +
  29935. +vchiq-objs := \
  29936. + interface/vchiq_arm/vchiq_core.o \
  29937. + interface/vchiq_arm/vchiq_arm.o \
  29938. + interface/vchiq_arm/vchiq_kern_lib.o \
  29939. + interface/vchiq_arm/vchiq_2835_arm.o \
  29940. + interface/vchiq_arm/vchiq_proc.o \
  29941. + interface/vchiq_arm/vchiq_shim.o \
  29942. + interface/vchiq_arm/vchiq_util.o \
  29943. + interface/vchiq_arm/vchiq_connected.o \
  29944. +
  29945. +EXTRA_CFLAGS += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
  29946. +
  29947. +endif
  29948. diff -Nur linux-3.13.6/drivers/mmc/card/block.c linux-raspberry-pi/drivers/mmc/card/block.c
  29949. --- linux-3.13.6/drivers/mmc/card/block.c 2014-03-07 07:07:02.000000000 +0100
  29950. +++ linux-raspberry-pi/drivers/mmc/card/block.c 2014-03-11 16:54:58.000000000 +0100
  29951. @@ -1361,7 +1361,7 @@
  29952. brq->data.blocks = 1;
  29953. }
  29954. - if (brq->data.blocks > 1 || do_rel_wr) {
  29955. + if (brq->data.blocks > 1 || do_rel_wr || card->host->caps2 & MMC_CAP2_FORCE_MULTIBLOCK) {
  29956. /* SPI multiblock writes terminate using a special
  29957. * token, not a STOP_TRANSMISSION request.
  29958. */
  29959. diff -Nur linux-3.13.6/drivers/mmc/core/sd.c linux-raspberry-pi/drivers/mmc/core/sd.c
  29960. --- linux-3.13.6/drivers/mmc/core/sd.c 2014-03-07 07:07:02.000000000 +0100
  29961. +++ linux-raspberry-pi/drivers/mmc/core/sd.c 2014-03-11 16:54:58.000000000 +0100
  29962. @@ -15,6 +15,8 @@
  29963. #include <linux/slab.h>
  29964. #include <linux/stat.h>
  29965. #include <linux/pm_runtime.h>
  29966. +#include <linux/jiffies.h>
  29967. +#include <linux/nmi.h>
  29968. #include <linux/mmc/host.h>
  29969. #include <linux/mmc/card.h>
  29970. @@ -67,6 +69,15 @@
  29971. __res & __mask; \
  29972. })
  29973. +// timeout for tries
  29974. +static const unsigned long retry_timeout_ms= 10*1000;
  29975. +
  29976. +// try at least 10 times, even if timeout is reached
  29977. +static const int retry_min_tries= 10;
  29978. +
  29979. +// delay between tries
  29980. +static const unsigned long retry_delay_ms= 10;
  29981. +
  29982. /*
  29983. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  29984. */
  29985. @@ -219,12 +230,63 @@
  29986. }
  29987. /*
  29988. - * Fetch and process SD Status register.
  29989. + * Fetch and process SD Configuration Register.
  29990. + */
  29991. +static int mmc_read_scr(struct mmc_card *card)
  29992. +{
  29993. + unsigned long timeout_at;
  29994. + int err, tries;
  29995. +
  29996. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  29997. + tries= 0;
  29998. +
  29999. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  30000. + {
  30001. + unsigned long delay_at;
  30002. + tries++;
  30003. +
  30004. + err = mmc_app_send_scr(card, card->raw_scr);
  30005. + if( !err )
  30006. + break; // success!!!
  30007. +
  30008. + touch_nmi_watchdog(); // we are still alive!
  30009. +
  30010. + // delay
  30011. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  30012. + while( time_before( jiffies, delay_at ) )
  30013. + {
  30014. + mdelay( 1 );
  30015. + touch_nmi_watchdog(); // we are still alive!
  30016. + }
  30017. + }
  30018. +
  30019. + if( err)
  30020. + {
  30021. + 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 );
  30022. + return err;
  30023. + }
  30024. +
  30025. + if( tries > 1 )
  30026. + {
  30027. + pr_info("%s: could read SD Configuration register (SCR) at the %dth attempt\n", mmc_hostname(card->host), tries );
  30028. + }
  30029. +
  30030. + err = mmc_decode_scr(card);
  30031. + if (err)
  30032. + return err;
  30033. +
  30034. + return err;
  30035. +}
  30036. +
  30037. +/*
  30038. + * Fetch and process SD Status Register.
  30039. */
  30040. static int mmc_read_ssr(struct mmc_card *card)
  30041. {
  30042. + unsigned long timeout_at;
  30043. unsigned int au, es, et, eo;
  30044. int err, i;
  30045. + int tries;
  30046. u32 *ssr;
  30047. if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
  30048. @@ -237,14 +299,40 @@
  30049. if (!ssr)
  30050. return -ENOMEM;
  30051. - err = mmc_app_sd_status(card, ssr);
  30052. - if (err) {
  30053. - pr_warning("%s: problem reading SD Status "
  30054. - "register.\n", mmc_hostname(card->host));
  30055. - err = 0;
  30056. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  30057. + tries= 0;
  30058. +
  30059. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  30060. + {
  30061. + unsigned long delay_at;
  30062. + tries++;
  30063. +
  30064. + err= mmc_app_sd_status(card, ssr);
  30065. + if( !err )
  30066. + break; // sucess!!!
  30067. +
  30068. + touch_nmi_watchdog(); // we are still alive!
  30069. +
  30070. + // delay
  30071. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  30072. + while( time_before( jiffies, delay_at ) )
  30073. + {
  30074. + mdelay( 1 );
  30075. + touch_nmi_watchdog(); // we are still alive!
  30076. + }
  30077. + }
  30078. +
  30079. + if( err)
  30080. + {
  30081. + 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 );
  30082. goto out;
  30083. }
  30084. + if( tries > 1 )
  30085. + {
  30086. + pr_info("%s: read SD Status register (SSR) after %d attempts\n", mmc_hostname(card->host), tries );
  30087. + }
  30088. +
  30089. for (i = 0; i < 16; i++)
  30090. ssr[i] = be32_to_cpu(ssr[i]);
  30091. @@ -826,14 +914,10 @@
  30092. if (!reinit) {
  30093. /*
  30094. - * Fetch SCR from card.
  30095. + * Fetch and decode SD Configuration register.
  30096. */
  30097. - err = mmc_app_send_scr(card, card->raw_scr);
  30098. - if (err)
  30099. - return err;
  30100. -
  30101. - err = mmc_decode_scr(card);
  30102. - if (err)
  30103. + err = mmc_read_scr(card);
  30104. + if( err )
  30105. return err;
  30106. /*
  30107. diff -Nur linux-3.13.6/drivers/mmc/host/Kconfig linux-raspberry-pi/drivers/mmc/host/Kconfig
  30108. --- linux-3.13.6/drivers/mmc/host/Kconfig 2014-03-07 07:07:02.000000000 +0100
  30109. +++ linux-raspberry-pi/drivers/mmc/host/Kconfig 2014-03-11 16:54:58.000000000 +0100
  30110. @@ -260,6 +260,27 @@
  30111. If you have a controller with this interface, say Y or M here.
  30112. +config MMC_SDHCI_BCM2708
  30113. + tristate "SDHCI support on BCM2708"
  30114. + depends on MMC_SDHCI && MACH_BCM2708
  30115. + select MMC_SDHCI_IO_ACCESSORS
  30116. + help
  30117. + This selects the Secure Digital Host Controller Interface (SDHCI)
  30118. + often referrered to as the eMMC block.
  30119. +
  30120. + If you have a controller with this interface, say Y or M here.
  30121. +
  30122. + If unsure, say N.
  30123. +
  30124. +config MMC_SDHCI_BCM2708_DMA
  30125. + bool "DMA support on BCM2708 Arasan controller"
  30126. + depends on MMC_SDHCI_BCM2708
  30127. + help
  30128. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  30129. + based chips.
  30130. +
  30131. + If unsure, say N.
  30132. +
  30133. config MMC_SDHCI_BCM2835
  30134. tristate "SDHCI platform support for the BCM2835 SD/MMC Controller"
  30135. depends on ARCH_BCM2835
  30136. diff -Nur linux-3.13.6/drivers/mmc/host/Makefile linux-raspberry-pi/drivers/mmc/host/Makefile
  30137. --- linux-3.13.6/drivers/mmc/host/Makefile 2014-03-07 07:07:02.000000000 +0100
  30138. +++ linux-raspberry-pi/drivers/mmc/host/Makefile 2014-03-11 16:54:58.000000000 +0100
  30139. @@ -15,6 +15,7 @@
  30140. obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o
  30141. obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
  30142. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  30143. +obj-$(CONFIG_MMC_SDHCI_BCM2708) += sdhci-bcm2708.o
  30144. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  30145. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  30146. obj-$(CONFIG_MMC_OMAP) += omap.o
  30147. diff -Nur linux-3.13.6/drivers/mmc/host/sdhci-bcm2708.c linux-raspberry-pi/drivers/mmc/host/sdhci-bcm2708.c
  30148. --- linux-3.13.6/drivers/mmc/host/sdhci-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  30149. +++ linux-raspberry-pi/drivers/mmc/host/sdhci-bcm2708.c 2014-03-11 16:54:58.000000000 +0100
  30150. @@ -0,0 +1,1410 @@
  30151. +/*
  30152. + * sdhci-bcm2708.c Support for SDHCI device on BCM2708
  30153. + * Copyright (c) 2010 Broadcom
  30154. + *
  30155. + * This program is free software; you can redistribute it and/or modify
  30156. + * it under the terms of the GNU General Public License version 2 as
  30157. + * published by the Free Software Foundation.
  30158. + *
  30159. + * This program is distributed in the hope that it will be useful,
  30160. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30161. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30162. + * GNU General Public License for more details.
  30163. + *
  30164. + * You should have received a copy of the GNU General Public License
  30165. + * along with this program; if not, write to the Free Software
  30166. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30167. + */
  30168. +
  30169. +/* Supports:
  30170. + * SDHCI platform device - Arasan SD controller in BCM2708
  30171. + *
  30172. + * Inspired by sdhci-pci.c, by Pierre Ossman
  30173. + */
  30174. +
  30175. +#include <linux/delay.h>
  30176. +#include <linux/highmem.h>
  30177. +#include <linux/platform_device.h>
  30178. +#include <linux/module.h>
  30179. +#include <linux/mmc/mmc.h>
  30180. +#include <linux/mmc/host.h>
  30181. +#include <linux/mmc/sd.h>
  30182. +
  30183. +#include <linux/io.h>
  30184. +#include <linux/dma-mapping.h>
  30185. +#include <mach/dma.h>
  30186. +
  30187. +#include "sdhci.h"
  30188. +
  30189. +/*****************************************************************************\
  30190. + * *
  30191. + * Configuration *
  30192. + * *
  30193. +\*****************************************************************************/
  30194. +
  30195. +#define DRIVER_NAME "bcm2708_sdhci"
  30196. +
  30197. +/* for the time being insist on DMA mode - PIO seems not to work */
  30198. +#ifndef CONFIG_MMC_SDHCI_BCM2708_DMA
  30199. +#warning Non-DMA (PIO) version of this driver currently unavailable
  30200. +#endif
  30201. +#undef CONFIG_MMC_SDHCI_BCM2708_DMA
  30202. +#define CONFIG_MMC_SDHCI_BCM2708_DMA y
  30203. +
  30204. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  30205. +/* #define CHECK_DMA_USE */
  30206. +#endif
  30207. +//#define LOG_REGISTERS
  30208. +
  30209. +#define USE_SCHED_TIME
  30210. +#define USE_SPACED_WRITES_2CLK 1 /* space consecutive register writes */
  30211. +#define USE_SOFTWARE_TIMEOUTS 1 /* not hardware timeouts */
  30212. +#define SOFTWARE_ERASE_TIMEOUT_SEC 30
  30213. +
  30214. +#define SDHCI_BCM_DMA_CHAN 4 /* this default is normally overriden */
  30215. +#define SDHCI_BCM_DMA_WAITS 0 /* delays slowing DMA transfers: 0-31 */
  30216. +/* We are worried that SD card DMA use may be blocking the AXI bus for others */
  30217. +
  30218. +/*! TODO: obtain these from the physical address */
  30219. +#define DMA_SDHCI_BASE 0x7e300000 /* EMMC register block on Videocore */
  30220. +#define DMA_SDHCI_BUFFER (DMA_SDHCI_BASE + SDHCI_BUFFER)
  30221. +
  30222. +#define BCM2708_SDHCI_SLEEP_TIMEOUT 1000 /* msecs */
  30223. +
  30224. +/* Mhz clock that the EMMC core is running at. Should match the platform clockman settings */
  30225. +#define BCM2708_EMMC_CLOCK_FREQ 50000000
  30226. +
  30227. +#define REG_EXRDFIFO_EN 0x80
  30228. +#define REG_EXRDFIFO_CFG 0x84
  30229. +
  30230. +int cycle_delay=2;
  30231. +
  30232. +/*****************************************************************************\
  30233. + * *
  30234. + * Debug *
  30235. + * *
  30236. +\*****************************************************************************/
  30237. +
  30238. +
  30239. +
  30240. +#define DBG(f, x...) \
  30241. + pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
  30242. +// printk(KERN_INFO DRIVER_NAME " [%s()]: " f, __func__,## x)//GRAYG
  30243. +
  30244. +
  30245. +/*****************************************************************************\
  30246. + * *
  30247. + * High Precision Time *
  30248. + * *
  30249. +\*****************************************************************************/
  30250. +
  30251. +#ifdef USE_SCHED_TIME
  30252. +
  30253. +#include <mach/frc.h>
  30254. +
  30255. +typedef unsigned long hptime_t;
  30256. +
  30257. +#define FMT_HPT "lu"
  30258. +
  30259. +static inline hptime_t hptime(void)
  30260. +{
  30261. + return frc_clock_ticks32();
  30262. +}
  30263. +
  30264. +#define HPTIME_CLK_NS 1000ul
  30265. +
  30266. +#else
  30267. +
  30268. +typedef unsigned long hptime_t;
  30269. +
  30270. +#define FMT_HPT "lu"
  30271. +
  30272. +static inline hptime_t hptime(void)
  30273. +{
  30274. + return jiffies;
  30275. +}
  30276. +
  30277. +#define HPTIME_CLK_NS (1000000000ul/HZ)
  30278. +
  30279. +#endif
  30280. +
  30281. +static inline unsigned long int since_ns(hptime_t t)
  30282. +{
  30283. + return (unsigned long)((hptime() - t) * HPTIME_CLK_NS);
  30284. +}
  30285. +
  30286. +static bool allow_highspeed = 1;
  30287. +static int emmc_clock_freq = BCM2708_EMMC_CLOCK_FREQ;
  30288. +static bool sync_after_dma = 1;
  30289. +static bool missing_status = 1;
  30290. +static bool spurious_crc_acmd51 = 0;
  30291. +bool enable_llm = 1;
  30292. +bool extra_messages = 0;
  30293. +
  30294. +#if 0
  30295. +static void hptime_test(void)
  30296. +{
  30297. + hptime_t now;
  30298. + hptime_t later;
  30299. +
  30300. + now = hptime();
  30301. + msleep(10);
  30302. + later = hptime();
  30303. +
  30304. + printk(KERN_INFO DRIVER_NAME": 10ms = %"FMT_HPT" clks "
  30305. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  30306. + later-now, now, later,
  30307. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  30308. +
  30309. + now = hptime();
  30310. + msleep(1000);
  30311. + later = hptime();
  30312. +
  30313. + printk(KERN_INFO DRIVER_NAME": 1s = %"FMT_HPT" clks "
  30314. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  30315. + later-now, now, later,
  30316. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  30317. +}
  30318. +#endif
  30319. +
  30320. +/*****************************************************************************\
  30321. + * *
  30322. + * SDHCI core callbacks *
  30323. + * *
  30324. +\*****************************************************************************/
  30325. +
  30326. +
  30327. +#ifdef CHECK_DMA_USE
  30328. +/*#define CHECK_DMA_REG_USE*/
  30329. +#endif
  30330. +
  30331. +#ifdef CHECK_DMA_REG_USE
  30332. +/* we don't expect anything to be using these registers during a
  30333. + DMA (except the IRQ status) - so check */
  30334. +static void check_dma_reg_use(struct sdhci_host *host, int reg);
  30335. +#else
  30336. +#define check_dma_reg_use(host, reg)
  30337. +#endif
  30338. +
  30339. +
  30340. +static inline u32 sdhci_bcm2708_raw_readl(struct sdhci_host *host, int reg)
  30341. +{
  30342. + return readl(host->ioaddr + reg);
  30343. +}
  30344. +
  30345. +u32 sdhci_bcm2708_readl(struct sdhci_host *host, int reg)
  30346. +{
  30347. + u32 l = sdhci_bcm2708_raw_readl(host, reg);
  30348. +
  30349. +#ifdef LOG_REGISTERS
  30350. + printk(KERN_ERR "%s: readl from 0x%02x, value 0x%08x\n",
  30351. + mmc_hostname(host->mmc), reg, l);
  30352. +#endif
  30353. + check_dma_reg_use(host, reg);
  30354. +
  30355. + return l;
  30356. +}
  30357. +
  30358. +u16 sdhci_bcm2708_readw(struct sdhci_host *host, int reg)
  30359. +{
  30360. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  30361. + u32 w = l >> (reg << 3 & 0x18) & 0xffff;
  30362. +
  30363. +#ifdef LOG_REGISTERS
  30364. + printk(KERN_ERR "%s: readw from 0x%02x, value 0x%04x\n",
  30365. + mmc_hostname(host->mmc), reg, w);
  30366. +#endif
  30367. + check_dma_reg_use(host, reg);
  30368. +
  30369. + return (u16)w;
  30370. +}
  30371. +
  30372. +u8 sdhci_bcm2708_readb(struct sdhci_host *host, int reg)
  30373. +{
  30374. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  30375. + u32 b = l >> (reg << 3 & 0x18) & 0xff;
  30376. +
  30377. +#ifdef LOG_REGISTERS
  30378. + printk(KERN_ERR "%s: readb from 0x%02x, value 0x%02x\n",
  30379. + mmc_hostname(host->mmc), reg, b);
  30380. +#endif
  30381. + check_dma_reg_use(host, reg);
  30382. +
  30383. + return (u8)b;
  30384. +}
  30385. +
  30386. +
  30387. +static void sdhci_bcm2708_raw_writel(struct sdhci_host *host, u32 val, int reg)
  30388. +{
  30389. + u32 ier;
  30390. +
  30391. +#if USE_SPACED_WRITES_2CLK
  30392. + static bool timeout_disabled = false;
  30393. + unsigned int ns_2clk = 0;
  30394. +
  30395. + /* The Arasan has a bugette whereby it may lose the content of
  30396. + * successive writes to registers that are within two SD-card clock
  30397. + * cycles of each other (a clock domain crossing problem).
  30398. + * It seems, however, that the data register does not have this problem.
  30399. + * (Which is just as well - otherwise we'd have to nobble the DMA engine
  30400. + * too)
  30401. + */
  30402. + if (reg != SDHCI_BUFFER && host->clock != 0) {
  30403. + /* host->clock is the clock freq in Hz */
  30404. + static hptime_t last_write_hpt;
  30405. + hptime_t now = hptime();
  30406. + ns_2clk = cycle_delay*1000000/(host->clock/1000);
  30407. +
  30408. + if (now == last_write_hpt || now == last_write_hpt+1) {
  30409. + /* we can't guarantee any significant time has
  30410. + * passed - we'll have to wait anyway ! */
  30411. + ndelay(ns_2clk);
  30412. + } else
  30413. + {
  30414. + /* we must have waited at least this many ns: */
  30415. + unsigned int ns_wait = HPTIME_CLK_NS *
  30416. + (last_write_hpt - now - 1);
  30417. + if (ns_wait < ns_2clk)
  30418. + ndelay(ns_2clk - ns_wait);
  30419. + }
  30420. + last_write_hpt = now;
  30421. + }
  30422. +#if USE_SOFTWARE_TIMEOUTS
  30423. + /* The Arasan is clocked for timeouts using the SD clock which is too
  30424. + * fast for ERASE commands and causes issues. So we disable timeouts
  30425. + * for ERASE */
  30426. + if (host->cmd != NULL && host->cmd->opcode == MMC_ERASE &&
  30427. + reg == (SDHCI_COMMAND & ~3)) {
  30428. + mod_timer(&host->timer,
  30429. + jiffies + SOFTWARE_ERASE_TIMEOUT_SEC * HZ);
  30430. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  30431. + ier &= ~SDHCI_INT_DATA_TIMEOUT;
  30432. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  30433. + timeout_disabled = true;
  30434. + ndelay(ns_2clk);
  30435. + } else if (timeout_disabled) {
  30436. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  30437. + ier |= SDHCI_INT_DATA_TIMEOUT;
  30438. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  30439. + timeout_disabled = false;
  30440. + ndelay(ns_2clk);
  30441. + }
  30442. +#endif
  30443. + writel(val, host->ioaddr + reg);
  30444. +#else
  30445. + void __iomem * regaddr = host->ioaddr + reg;
  30446. +
  30447. + writel(val, regaddr);
  30448. +
  30449. + if (reg != SDHCI_BUFFER && reg != SDHCI_INT_STATUS && host->clock != 0)
  30450. + {
  30451. + int timeout = 100000;
  30452. + while (val != readl(regaddr) && --timeout > 0)
  30453. + continue;
  30454. +
  30455. + if (timeout <= 0)
  30456. + printk(KERN_ERR "%s: writing 0x%X to reg 0x%X "
  30457. + "always gives 0x%X\n",
  30458. + mmc_hostname(host->mmc),
  30459. + val, reg, readl(regaddr));
  30460. + BUG_ON(timeout <= 0);
  30461. + }
  30462. +#endif
  30463. +}
  30464. +
  30465. +
  30466. +void sdhci_bcm2708_writel(struct sdhci_host *host, u32 val, int reg)
  30467. +{
  30468. +#ifdef LOG_REGISTERS
  30469. + printk(KERN_ERR "%s: writel to 0x%02x, value 0x%08x\n",
  30470. + mmc_hostname(host->mmc), reg, val);
  30471. +#endif
  30472. + check_dma_reg_use(host, reg);
  30473. +
  30474. + sdhci_bcm2708_raw_writel(host, val, reg);
  30475. +}
  30476. +
  30477. +void sdhci_bcm2708_writew(struct sdhci_host *host, u16 val, int reg)
  30478. +{
  30479. + static u32 shadow = 0;
  30480. +
  30481. + u32 p = reg == SDHCI_COMMAND ? shadow :
  30482. + sdhci_bcm2708_raw_readl(host, reg & ~3);
  30483. + u32 s = reg << 3 & 0x18;
  30484. + u32 l = val << s;
  30485. + u32 m = 0xffff << s;
  30486. +
  30487. +#ifdef LOG_REGISTERS
  30488. + printk(KERN_ERR "%s: writew to 0x%02x, value 0x%04x\n",
  30489. + mmc_hostname(host->mmc), reg, val);
  30490. +#endif
  30491. +
  30492. + if (reg == SDHCI_TRANSFER_MODE)
  30493. + shadow = (p & ~m) | l;
  30494. + else {
  30495. + check_dma_reg_use(host, reg);
  30496. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  30497. + }
  30498. +}
  30499. +
  30500. +void sdhci_bcm2708_writeb(struct sdhci_host *host, u8 val, int reg)
  30501. +{
  30502. + u32 p = sdhci_bcm2708_raw_readl(host, reg & ~3);
  30503. + u32 s = reg << 3 & 0x18;
  30504. + u32 l = val << s;
  30505. + u32 m = 0xff << s;
  30506. +
  30507. +#ifdef LOG_REGISTERS
  30508. + printk(KERN_ERR "%s: writeb to 0x%02x, value 0x%02x\n",
  30509. + mmc_hostname(host->mmc), reg, val);
  30510. +#endif
  30511. +
  30512. + check_dma_reg_use(host, reg);
  30513. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  30514. +}
  30515. +
  30516. +static unsigned int sdhci_bcm2708_get_max_clock(struct sdhci_host *host)
  30517. +{
  30518. + return emmc_clock_freq;
  30519. +}
  30520. +
  30521. +/*****************************************************************************\
  30522. + * *
  30523. + * DMA Operation *
  30524. + * *
  30525. +\*****************************************************************************/
  30526. +
  30527. +struct sdhci_bcm2708_priv {
  30528. + int dma_chan;
  30529. + int dma_irq;
  30530. + void __iomem *dma_chan_base;
  30531. + struct bcm2708_dma_cb *cb_base; /* DMA control blocks */
  30532. + dma_addr_t cb_handle;
  30533. + /* tracking scatter gather progress */
  30534. + unsigned sg_ix; /* scatter gather list index */
  30535. + unsigned sg_done; /* bytes in current sg_ix done */
  30536. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  30537. + unsigned char dma_wanted; /* DMA transfer requested */
  30538. + unsigned char dma_waits; /* wait states in DMAs */
  30539. +#ifdef CHECK_DMA_USE
  30540. + unsigned char dmas_pending; /* no of unfinished DMAs */
  30541. + hptime_t when_started;
  30542. + hptime_t when_reset;
  30543. + hptime_t when_stopped;
  30544. +#endif
  30545. +#endif
  30546. + /* signalling the end of a transfer */
  30547. + void (*complete)(struct sdhci_host *);
  30548. +};
  30549. +
  30550. +#define SDHCI_HOST_PRIV(host) \
  30551. + (struct sdhci_bcm2708_priv *)((struct sdhci_host *)(host)+1)
  30552. +
  30553. +
  30554. +
  30555. +#ifdef CHECK_DMA_REG_USE
  30556. +static void check_dma_reg_use(struct sdhci_host *host, int reg)
  30557. +{
  30558. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  30559. + if (host_priv->dma_wanted && reg != SDHCI_INT_STATUS) {
  30560. + printk(KERN_INFO"%s: accessing register 0x%x during DMA\n",
  30561. + mmc_hostname(host->mmc), reg);
  30562. + }
  30563. +}
  30564. +#endif
  30565. +
  30566. +
  30567. +
  30568. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  30569. +
  30570. +static void sdhci_clear_set_irqgen(struct sdhci_host *host, u32 clear, u32 set)
  30571. +{
  30572. + u32 ier;
  30573. +
  30574. + ier = sdhci_bcm2708_raw_readl(host, SDHCI_SIGNAL_ENABLE);
  30575. + ier &= ~clear;
  30576. + ier |= set;
  30577. + /* change which requests generate IRQs - makes no difference to
  30578. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  30579. + sdhci_bcm2708_raw_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  30580. +}
  30581. +
  30582. +static void sdhci_signal_irqs(struct sdhci_host *host, u32 irqs)
  30583. +{
  30584. + sdhci_clear_set_irqgen(host, 0, irqs);
  30585. +}
  30586. +
  30587. +static void sdhci_unsignal_irqs(struct sdhci_host *host, u32 irqs)
  30588. +{
  30589. + sdhci_clear_set_irqgen(host, irqs, 0);
  30590. +}
  30591. +
  30592. +
  30593. +
  30594. +static void schci_bcm2708_cb_read(struct sdhci_bcm2708_priv *host,
  30595. + int ix,
  30596. + dma_addr_t dma_addr, unsigned len,
  30597. + int /*bool*/ is_last)
  30598. +{
  30599. + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
  30600. + unsigned char dmawaits = host->dma_waits;
  30601. +
  30602. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  30603. + BCM2708_DMA_WAITS(dmawaits) |
  30604. + BCM2708_DMA_S_DREQ |
  30605. + BCM2708_DMA_D_WIDTH |
  30606. + BCM2708_DMA_D_INC;
  30607. + cb->src = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  30608. + cb->dst = dma_addr;
  30609. + cb->length = len;
  30610. + cb->stride = 0;
  30611. +
  30612. + if (is_last) {
  30613. + cb->info |= BCM2708_DMA_INT_EN |
  30614. + BCM2708_DMA_WAIT_RESP;
  30615. + cb->next = 0;
  30616. + } else
  30617. + cb->next = host->cb_handle +
  30618. + (ix+1)*sizeof(struct bcm2708_dma_cb);
  30619. +
  30620. + cb->pad[0] = 0;
  30621. + cb->pad[1] = 0;
  30622. +}
  30623. +
  30624. +static void schci_bcm2708_cb_write(struct sdhci_bcm2708_priv *host,
  30625. + int ix,
  30626. + dma_addr_t dma_addr, unsigned len,
  30627. + int /*bool*/ is_last)
  30628. +{
  30629. + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
  30630. + unsigned char dmawaits = host->dma_waits;
  30631. +
  30632. + /* We can make arbitrarily large writes as long as we specify DREQ to
  30633. + pace the delivery of bytes to the Arasan hardware */
  30634. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  30635. + BCM2708_DMA_WAITS(dmawaits) |
  30636. + BCM2708_DMA_D_DREQ |
  30637. + BCM2708_DMA_S_WIDTH |
  30638. + BCM2708_DMA_S_INC;
  30639. + cb->src = dma_addr;
  30640. + cb->dst = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  30641. + cb->length = len;
  30642. + cb->stride = 0;
  30643. +
  30644. + if (is_last) {
  30645. + cb->info |= BCM2708_DMA_INT_EN |
  30646. + BCM2708_DMA_WAIT_RESP;
  30647. + cb->next = 0;
  30648. + } else
  30649. + cb->next = host->cb_handle +
  30650. + (ix+1)*sizeof(struct bcm2708_dma_cb);
  30651. +
  30652. + cb->pad[0] = 0;
  30653. + cb->pad[1] = 0;
  30654. +}
  30655. +
  30656. +
  30657. +static void schci_bcm2708_dma_go(struct sdhci_host *host)
  30658. +{
  30659. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  30660. + void __iomem *dma_chan_base = host_priv->dma_chan_base;
  30661. +
  30662. + BUG_ON(host_priv->dma_wanted);
  30663. +#ifdef CHECK_DMA_USE
  30664. + if (host_priv->dma_wanted)
  30665. + printk(KERN_ERR "%s: DMA already in progress - "
  30666. + "now %"FMT_HPT", last started %lu "
  30667. + "reset %lu stopped %lu\n",
  30668. + mmc_hostname(host->mmc),
  30669. + hptime(), since_ns(host_priv->when_started),
  30670. + since_ns(host_priv->when_reset),
  30671. + since_ns(host_priv->when_stopped));
  30672. + else if (host_priv->dmas_pending > 0)
  30673. + printk(KERN_INFO "%s: note - new DMA when %d reset DMAs "
  30674. + "already in progress - "
  30675. + "now %"FMT_HPT", started %lu reset %lu stopped %lu\n",
  30676. + mmc_hostname(host->mmc),
  30677. + host_priv->dmas_pending,
  30678. + hptime(), since_ns(host_priv->when_started),
  30679. + since_ns(host_priv->when_reset),
  30680. + since_ns(host_priv->when_stopped));
  30681. + host_priv->dmas_pending += 1;
  30682. + host_priv->when_started = hptime();
  30683. +#endif
  30684. + host_priv->dma_wanted = 1;
  30685. + DBG("PDMA go - base %p handle %08X\n", dma_chan_base,
  30686. + host_priv->cb_handle);
  30687. + bcm_dma_start(dma_chan_base, host_priv->cb_handle);
  30688. +}
  30689. +
  30690. +
  30691. +static void
  30692. +sdhci_platdma_read(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  30693. +{
  30694. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  30695. +
  30696. + DBG("PDMA to read %d bytes\n", len);
  30697. + host_priv->sg_done += len;
  30698. + schci_bcm2708_cb_read(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  30699. + schci_bcm2708_dma_go(host);
  30700. +}
  30701. +
  30702. +
  30703. +static void
  30704. +sdhci_platdma_write(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  30705. +{
  30706. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  30707. +
  30708. + DBG("PDMA to write %d bytes\n", len);
  30709. + //BUG_ON(0 != (len & 0x1ff));
  30710. +
  30711. + host_priv->sg_done += len;
  30712. + schci_bcm2708_cb_write(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  30713. + schci_bcm2708_dma_go(host);
  30714. +}
  30715. +
  30716. +/*! space is avaiable to receive into or data is available to write
  30717. + Platform DMA exported function
  30718. +*/
  30719. +void
  30720. +sdhci_bcm2708_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  30721. + void(*completion_callback)(struct sdhci_host *host))
  30722. +{
  30723. + struct mmc_data *data = host->data;
  30724. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  30725. + int sg_ix;
  30726. + size_t bytes;
  30727. + dma_addr_t addr;
  30728. +
  30729. + BUG_ON(NULL == data);
  30730. + BUG_ON(0 == data->blksz);
  30731. +
  30732. + host_priv->complete = completion_callback;
  30733. +
  30734. + sg_ix = host_priv->sg_ix;
  30735. + BUG_ON(sg_ix >= data->sg_len);
  30736. +
  30737. + /* we can DMA blocks larger than blksz - it may hang the DMA
  30738. + channel but we are its only user */
  30739. + bytes = sg_dma_len(&data->sg[sg_ix]) - host_priv->sg_done;
  30740. + addr = sg_dma_address(&data->sg[sg_ix]) + host_priv->sg_done;
  30741. +
  30742. + if (bytes > 0) {
  30743. + /* We're going to poll for read/write available state until
  30744. + we finish this DMA
  30745. + */
  30746. +
  30747. + if (data->flags & MMC_DATA_READ) {
  30748. + if (*ref_intmask & SDHCI_INT_DATA_AVAIL) {
  30749. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  30750. + SDHCI_INT_SPACE_AVAIL);
  30751. + sdhci_platdma_read(host, addr, bytes);
  30752. + }
  30753. + } else {
  30754. + if (*ref_intmask & SDHCI_INT_SPACE_AVAIL) {
  30755. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  30756. + SDHCI_INT_SPACE_AVAIL);
  30757. + sdhci_platdma_write(host, addr, bytes);
  30758. + }
  30759. + }
  30760. + }
  30761. + /* else:
  30762. + we have run out of bytes that need transferring (e.g. we may be in
  30763. + the middle of the last DMA transfer), or
  30764. + it is also possible that we've been called when another IRQ is
  30765. + signalled, even though we've turned off signalling of our own IRQ */
  30766. +
  30767. + *ref_intmask &= ~SDHCI_INT_DATA_END;
  30768. + /* don't let the main sdhci driver act on this .. we'll deal with it
  30769. + when we respond to the DMA - if one is currently in progress */
  30770. +}
  30771. +
  30772. +/* is it possible to DMA the given mmc_data structure?
  30773. + Platform DMA exported function
  30774. +*/
  30775. +int /*bool*/
  30776. +sdhci_bcm2708_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  30777. +{
  30778. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  30779. + int ok = bcm_sg_suitable_for_dma(data->sg, data->sg_len);
  30780. +
  30781. + if (!ok)
  30782. + DBG("Reverting to PIO - bad cache alignment\n");
  30783. +
  30784. + else {
  30785. + host_priv->sg_ix = 0; /* first SG index */
  30786. + host_priv->sg_done = 0; /* no bytes done */
  30787. + }
  30788. +
  30789. + return ok;
  30790. +}
  30791. +
  30792. +#include <mach/arm_control.h> //GRAYG
  30793. +/*! the current SD transacton has been abandonned
  30794. + We need to tidy up if we were in the middle of a DMA
  30795. + Platform DMA exported function
  30796. +*/
  30797. +void
  30798. +sdhci_bcm2708_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  30799. +{
  30800. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  30801. +// unsigned long flags;
  30802. +
  30803. + BUG_ON(NULL == host);
  30804. +
  30805. +// spin_lock_irqsave(&host->lock, flags);
  30806. +
  30807. + if (host_priv->dma_wanted) {
  30808. + if (NULL == data) {
  30809. + printk(KERN_ERR "%s: ongoing DMA reset - no data!\n",
  30810. + mmc_hostname(host->mmc));
  30811. + BUG_ON(NULL == data);
  30812. + } else {
  30813. + struct scatterlist *sg;
  30814. + int sg_len;
  30815. + int sg_todo;
  30816. + int rc;
  30817. + unsigned long cs;
  30818. +
  30819. + sg = data->sg;
  30820. + sg_len = data->sg_len;
  30821. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  30822. +
  30823. + cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  30824. +
  30825. + if (!(BCM2708_DMA_ACTIVE & cs))
  30826. + {
  30827. + if (extra_messages)
  30828. + printk(KERN_INFO "%s: missed completion of "
  30829. + "cmd %d DMA (%d/%d [%d]/[%d]) - "
  30830. + "ignoring it\n",
  30831. + mmc_hostname(host->mmc),
  30832. + host->last_cmdop,
  30833. + host_priv->sg_done, sg_todo,
  30834. + host_priv->sg_ix+1, sg_len);
  30835. + }
  30836. + else
  30837. + printk(KERN_INFO "%s: resetting ongoing cmd %d"
  30838. + "DMA before %d/%d [%d]/[%d] complete\n",
  30839. + mmc_hostname(host->mmc),
  30840. + host->last_cmdop,
  30841. + host_priv->sg_done, sg_todo,
  30842. + host_priv->sg_ix+1, sg_len);
  30843. +#ifdef CHECK_DMA_USE
  30844. + printk(KERN_INFO "%s: now %"FMT_HPT" started %lu "
  30845. + "last reset %lu last stopped %lu\n",
  30846. + mmc_hostname(host->mmc),
  30847. + hptime(), since_ns(host_priv->when_started),
  30848. + since_ns(host_priv->when_reset),
  30849. + since_ns(host_priv->when_stopped));
  30850. + { unsigned long info, debug;
  30851. + void __iomem *base;
  30852. + unsigned long pend0, pend1, pend2;
  30853. +
  30854. + base = host_priv->dma_chan_base;
  30855. + cs = readl(base + BCM2708_DMA_CS);
  30856. + info = readl(base + BCM2708_DMA_INFO);
  30857. + debug = readl(base + BCM2708_DMA_DEBUG);
  30858. + printk(KERN_INFO "%s: DMA%d CS=%08lX TI=%08lX "
  30859. + "DEBUG=%08lX\n",
  30860. + mmc_hostname(host->mmc),
  30861. + host_priv->dma_chan,
  30862. + cs, info, debug);
  30863. + pend0 = readl(__io_address(ARM_IRQ_PEND0));
  30864. + pend1 = readl(__io_address(ARM_IRQ_PEND1));
  30865. + pend2 = readl(__io_address(ARM_IRQ_PEND2));
  30866. +
  30867. + printk(KERN_INFO "%s: PEND0=%08lX "
  30868. + "PEND1=%08lX PEND2=%08lX\n",
  30869. + mmc_hostname(host->mmc),
  30870. + pend0, pend1, pend2);
  30871. +
  30872. + //gintsts = readl(__io_address(GINTSTS));
  30873. + //gintmsk = readl(__io_address(GINTMSK));
  30874. + //printk(KERN_INFO "%s: USB GINTSTS=%08lX"
  30875. + // "GINTMSK=%08lX\n",
  30876. + // mmc_hostname(host->mmc), gintsts, gintmsk);
  30877. + }
  30878. +#endif
  30879. + rc = bcm_dma_abort(host_priv->dma_chan_base);
  30880. + BUG_ON(rc != 0);
  30881. + }
  30882. + host_priv->dma_wanted = 0;
  30883. +#ifdef CHECK_DMA_USE
  30884. + host_priv->when_reset = hptime();
  30885. +#endif
  30886. + }
  30887. +
  30888. +// spin_unlock_irqrestore(&host->lock, flags);
  30889. +}
  30890. +
  30891. +
  30892. +static void sdhci_bcm2708_dma_complete_irq(struct sdhci_host *host,
  30893. + u32 dma_cs)
  30894. +{
  30895. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  30896. + struct mmc_data *data;
  30897. + struct scatterlist *sg;
  30898. + int sg_len;
  30899. + int sg_ix;
  30900. + int sg_todo;
  30901. +// unsigned long flags;
  30902. +
  30903. + BUG_ON(NULL == host);
  30904. +
  30905. +// spin_lock_irqsave(&host->lock, flags);
  30906. + data = host->data;
  30907. +
  30908. +#ifdef CHECK_DMA_USE
  30909. + if (host_priv->dmas_pending <= 0)
  30910. + DBG("on completion no DMA in progress - "
  30911. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  30912. + hptime(), since_ns(host_priv->when_started),
  30913. + since_ns(host_priv->when_reset),
  30914. + since_ns(host_priv->when_stopped));
  30915. + else if (host_priv->dmas_pending > 1)
  30916. + DBG("still %d DMA in progress after completion - "
  30917. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  30918. + host_priv->dmas_pending - 1,
  30919. + hptime(), since_ns(host_priv->when_started),
  30920. + since_ns(host_priv->when_reset),
  30921. + since_ns(host_priv->when_stopped));
  30922. + BUG_ON(host_priv->dmas_pending <= 0);
  30923. + host_priv->dmas_pending -= 1;
  30924. + host_priv->when_stopped = hptime();
  30925. +#endif
  30926. + host_priv->dma_wanted = 0;
  30927. +
  30928. + if (NULL == data) {
  30929. + DBG("PDMA unused completion - status 0x%X\n", dma_cs);
  30930. +// spin_unlock_irqrestore(&host->lock, flags);
  30931. + return;
  30932. + }
  30933. + sg = data->sg;
  30934. + sg_len = data->sg_len;
  30935. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  30936. +
  30937. + DBG("PDMA complete %d/%d [%d]/[%d]..\n",
  30938. + host_priv->sg_done, sg_todo,
  30939. + host_priv->sg_ix+1, sg_len);
  30940. +
  30941. + BUG_ON(host_priv->sg_done > sg_todo);
  30942. +
  30943. + if (host_priv->sg_done >= sg_todo) {
  30944. + host_priv->sg_ix++;
  30945. + host_priv->sg_done = 0;
  30946. + }
  30947. +
  30948. + sg_ix = host_priv->sg_ix;
  30949. + if (sg_ix < sg_len) {
  30950. + u32 irq_mask;
  30951. + /* Set off next DMA if we've got the capacity */
  30952. +
  30953. + if (data->flags & MMC_DATA_READ)
  30954. + irq_mask = SDHCI_INT_DATA_AVAIL;
  30955. + else
  30956. + irq_mask = SDHCI_INT_SPACE_AVAIL;
  30957. +
  30958. + /* We have to use the interrupt status register on the BCM2708
  30959. + rather than the SDHCI_PRESENT_STATE register because latency
  30960. + in the glue logic means that the information retrieved from
  30961. + the latter is not always up-to-date w.r.t the DMA engine -
  30962. + it may not indicate that a read or a write is ready yet */
  30963. + if (sdhci_bcm2708_raw_readl(host, SDHCI_INT_STATUS) &
  30964. + irq_mask) {
  30965. + size_t bytes = sg_dma_len(&sg[sg_ix]) -
  30966. + host_priv->sg_done;
  30967. + dma_addr_t addr = sg_dma_address(&data->sg[sg_ix]) +
  30968. + host_priv->sg_done;
  30969. +
  30970. + /* acknowledge interrupt */
  30971. + sdhci_bcm2708_raw_writel(host, irq_mask,
  30972. + SDHCI_INT_STATUS);
  30973. +
  30974. + BUG_ON(0 == bytes);
  30975. +
  30976. + if (data->flags & MMC_DATA_READ)
  30977. + sdhci_platdma_read(host, addr, bytes);
  30978. + else
  30979. + sdhci_platdma_write(host, addr, bytes);
  30980. + } else {
  30981. + DBG("PDMA - wait avail\n");
  30982. + /* may generate an IRQ if already present */
  30983. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  30984. + SDHCI_INT_SPACE_AVAIL);
  30985. + }
  30986. + } else {
  30987. + if (sync_after_dma) {
  30988. + /* On the Arasan controller the stop command (which will be
  30989. + scheduled after this completes) does not seem to work
  30990. + properly if we allow it to be issued when we are
  30991. + transferring data to/from the SD card.
  30992. + We get CRC and DEND errors unless we wait for
  30993. + the SD controller to finish reading/writing to the card. */
  30994. + u32 state_mask;
  30995. + int timeout=3*1000*1000;
  30996. +
  30997. + DBG("PDMA over - sync card\n");
  30998. + if (data->flags & MMC_DATA_READ)
  30999. + state_mask = SDHCI_DOING_READ;
  31000. + else
  31001. + state_mask = SDHCI_DOING_WRITE;
  31002. +
  31003. + while (0 != (sdhci_bcm2708_raw_readl(host, SDHCI_PRESENT_STATE)
  31004. + & state_mask) && --timeout > 0)
  31005. + {
  31006. + udelay(1);
  31007. + continue;
  31008. + }
  31009. + if (timeout <= 0)
  31010. + printk(KERN_ERR"%s: final %s to SD card still "
  31011. + "running\n",
  31012. + mmc_hostname(host->mmc),
  31013. + data->flags & MMC_DATA_READ? "read": "write");
  31014. + }
  31015. + if (host_priv->complete) {
  31016. + (*host_priv->complete)(host);
  31017. + DBG("PDMA %s complete\n",
  31018. + data->flags & MMC_DATA_READ?"read":"write");
  31019. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  31020. + SDHCI_INT_SPACE_AVAIL);
  31021. + }
  31022. + }
  31023. +// spin_unlock_irqrestore(&host->lock, flags);
  31024. +}
  31025. +
  31026. +static irqreturn_t sdhci_bcm2708_dma_irq(int irq, void *dev_id)
  31027. +{
  31028. + irqreturn_t result = IRQ_NONE;
  31029. + struct sdhci_host *host = dev_id;
  31030. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31031. + u32 dma_cs; /* control and status register */
  31032. +
  31033. + BUG_ON(NULL == dev_id);
  31034. + BUG_ON(NULL == host_priv->dma_chan_base);
  31035. +
  31036. + sdhci_spin_lock(host);
  31037. +
  31038. + dma_cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  31039. +
  31040. + if (dma_cs & BCM2708_DMA_ERR) {
  31041. + unsigned long debug;
  31042. + debug = readl(host_priv->dma_chan_base +
  31043. + BCM2708_DMA_DEBUG);
  31044. + printk(KERN_ERR "%s: DMA error - CS %lX DEBUG %lX\n",
  31045. + mmc_hostname(host->mmc), (unsigned long)dma_cs,
  31046. + (unsigned long)debug);
  31047. + /* reset error */
  31048. + writel(debug, host_priv->dma_chan_base +
  31049. + BCM2708_DMA_DEBUG);
  31050. + }
  31051. + if (dma_cs & BCM2708_DMA_INT) {
  31052. + /* acknowledge interrupt */
  31053. + writel(BCM2708_DMA_INT,
  31054. + host_priv->dma_chan_base + BCM2708_DMA_CS);
  31055. +
  31056. + dsb(); /* ARM data synchronization (push) operation */
  31057. +
  31058. + if (!host_priv->dma_wanted) {
  31059. + /* ignore this interrupt - it was reset */
  31060. + if (extra_messages)
  31061. + printk(KERN_INFO "%s: DMA IRQ %X ignored - "
  31062. + "results were reset\n",
  31063. + mmc_hostname(host->mmc), dma_cs);
  31064. +#ifdef CHECK_DMA_USE
  31065. + printk(KERN_INFO "%s: now %"FMT_HPT
  31066. + " started %lu reset %lu stopped %lu\n",
  31067. + mmc_hostname(host->mmc), hptime(),
  31068. + since_ns(host_priv->when_started),
  31069. + since_ns(host_priv->when_reset),
  31070. + since_ns(host_priv->when_stopped));
  31071. + host_priv->dmas_pending--;
  31072. +#endif
  31073. + } else
  31074. + sdhci_bcm2708_dma_complete_irq(host, dma_cs);
  31075. +
  31076. + result = IRQ_HANDLED;
  31077. + }
  31078. + sdhci_spin_unlock(host);
  31079. +
  31080. + return result;
  31081. +}
  31082. +#endif /* CONFIG_MMC_SDHCI_BCM2708_DMA */
  31083. +
  31084. +
  31085. +/***************************************************************************** \
  31086. + * *
  31087. + * Device Attributes *
  31088. + * *
  31089. +\*****************************************************************************/
  31090. +
  31091. +
  31092. +/**
  31093. + * Show the DMA-using status
  31094. + */
  31095. +static ssize_t attr_dma_show(struct device *_dev,
  31096. + struct device_attribute *attr, char *buf)
  31097. +{
  31098. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31099. +
  31100. + if (host) {
  31101. + int use_dma = (host->flags & SDHCI_USE_PLATDMA? 1:0);
  31102. + return sprintf(buf, "%d\n", use_dma);
  31103. + } else
  31104. + return -EINVAL;
  31105. +}
  31106. +
  31107. +/**
  31108. + * Set the DMA-using status
  31109. + */
  31110. +static ssize_t attr_dma_store(struct device *_dev,
  31111. + struct device_attribute *attr,
  31112. + const char *buf, size_t count)
  31113. +{
  31114. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31115. +
  31116. + if (host) {
  31117. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31118. + int on = simple_strtol(buf, NULL, 0);
  31119. + if (on) {
  31120. + host->flags |= SDHCI_USE_PLATDMA;
  31121. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  31122. + printk(KERN_INFO "%s: DMA enabled\n",
  31123. + mmc_hostname(host->mmc));
  31124. + } else {
  31125. + host->flags &= ~(SDHCI_USE_PLATDMA | SDHCI_REQ_USE_DMA);
  31126. + sdhci_bcm2708_writel(host, 0, REG_EXRDFIFO_EN);
  31127. + printk(KERN_INFO "%s: DMA disabled\n",
  31128. + mmc_hostname(host->mmc));
  31129. + }
  31130. +#endif
  31131. + return count;
  31132. + } else
  31133. + return -EINVAL;
  31134. +}
  31135. +
  31136. +static DEVICE_ATTR(use_dma, S_IRUGO | S_IWUGO, attr_dma_show, attr_dma_store);
  31137. +
  31138. +
  31139. +/**
  31140. + * Show the DMA wait states used
  31141. + */
  31142. +static ssize_t attr_dmawait_show(struct device *_dev,
  31143. + struct device_attribute *attr, char *buf)
  31144. +{
  31145. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31146. +
  31147. + if (host) {
  31148. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31149. + int dmawait = host_priv->dma_waits;
  31150. + return sprintf(buf, "%d\n", dmawait);
  31151. + } else
  31152. + return -EINVAL;
  31153. +}
  31154. +
  31155. +/**
  31156. + * Set the DMA wait state used
  31157. + */
  31158. +static ssize_t attr_dmawait_store(struct device *_dev,
  31159. + struct device_attribute *attr,
  31160. + const char *buf, size_t count)
  31161. +{
  31162. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31163. +
  31164. + if (host) {
  31165. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31166. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31167. + int dma_waits = simple_strtol(buf, NULL, 0);
  31168. + if (dma_waits >= 0 && dma_waits < 32)
  31169. + host_priv->dma_waits = dma_waits;
  31170. + else
  31171. + printk(KERN_ERR "%s: illegal dma_waits value - %d",
  31172. + mmc_hostname(host->mmc), dma_waits);
  31173. +#endif
  31174. + return count;
  31175. + } else
  31176. + return -EINVAL;
  31177. +}
  31178. +
  31179. +static DEVICE_ATTR(dma_wait, S_IRUGO | S_IWUGO,
  31180. + attr_dmawait_show, attr_dmawait_store);
  31181. +
  31182. +
  31183. +/**
  31184. + * Show the DMA-using status
  31185. + */
  31186. +static ssize_t attr_status_show(struct device *_dev,
  31187. + struct device_attribute *attr, char *buf)
  31188. +{
  31189. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  31190. +
  31191. + if (host) {
  31192. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31193. + return sprintf(buf,
  31194. + "present: yes\n"
  31195. + "power: %s\n"
  31196. + "clock: %u Hz\n"
  31197. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31198. + "dma: %s (%d waits)\n",
  31199. +#else
  31200. + "dma: unconfigured\n",
  31201. +#endif
  31202. + "always on",
  31203. + host->clock
  31204. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31205. + , (host->flags & SDHCI_USE_PLATDMA)? "on": "off"
  31206. + , host_priv->dma_waits
  31207. +#endif
  31208. + );
  31209. + } else
  31210. + return -EINVAL;
  31211. +}
  31212. +
  31213. +static DEVICE_ATTR(status, S_IRUGO, attr_status_show, NULL);
  31214. +
  31215. +/***************************************************************************** \
  31216. + * *
  31217. + * Power Management *
  31218. + * *
  31219. +\*****************************************************************************/
  31220. +
  31221. +
  31222. +#ifdef CONFIG_PM
  31223. +static int sdhci_bcm2708_suspend(struct platform_device *dev, pm_message_t state)
  31224. +{
  31225. + struct sdhci_host *host = (struct sdhci_host *)
  31226. + platform_get_drvdata(dev);
  31227. + int ret = 0;
  31228. +
  31229. + if (host->mmc) {
  31230. + //ret = mmc_suspend_host(host->mmc);
  31231. + }
  31232. +
  31233. + return ret;
  31234. +}
  31235. +
  31236. +static int sdhci_bcm2708_resume(struct platform_device *dev)
  31237. +{
  31238. + struct sdhci_host *host = (struct sdhci_host *)
  31239. + platform_get_drvdata(dev);
  31240. + int ret = 0;
  31241. +
  31242. + if (host->mmc) {
  31243. + //ret = mmc_resume_host(host->mmc);
  31244. + }
  31245. +
  31246. + return ret;
  31247. +}
  31248. +#endif
  31249. +
  31250. +
  31251. +/*****************************************************************************\
  31252. + * *
  31253. + * Device quirk functions. Implemented as local ops because the flags *
  31254. + * field is out of space with newer kernels. This implementation can be *
  31255. + * back ported to older kernels as well. *
  31256. +\****************************************************************************/
  31257. +static unsigned int sdhci_bcm2708_quirk_extra_ints(struct sdhci_host *host)
  31258. +{
  31259. + return 1;
  31260. +}
  31261. +
  31262. +static unsigned int sdhci_bcm2708_quirk_spurious_crc_acmd51(struct sdhci_host *host)
  31263. +{
  31264. + return 1;
  31265. +}
  31266. +
  31267. +static unsigned int sdhci_bcm2708_missing_status(struct sdhci_host *host)
  31268. +{
  31269. + return 1;
  31270. +}
  31271. +
  31272. +/***************************************************************************** \
  31273. + * *
  31274. + * Device ops *
  31275. + * *
  31276. +\*****************************************************************************/
  31277. +
  31278. +static struct sdhci_ops sdhci_bcm2708_ops = {
  31279. +#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
  31280. + .read_l = sdhci_bcm2708_readl,
  31281. + .read_w = sdhci_bcm2708_readw,
  31282. + .read_b = sdhci_bcm2708_readb,
  31283. + .write_l = sdhci_bcm2708_writel,
  31284. + .write_w = sdhci_bcm2708_writew,
  31285. + .write_b = sdhci_bcm2708_writeb,
  31286. +#else
  31287. +#error The BCM2708 SDHCI driver needs CONFIG_MMC_SDHCI_IO_ACCESSORS to be set
  31288. +#endif
  31289. + .get_max_clock = sdhci_bcm2708_get_max_clock,
  31290. +
  31291. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31292. + // Platform DMA operations
  31293. + .pdma_able = sdhci_bcm2708_platdma_dmaable,
  31294. + .pdma_avail = sdhci_bcm2708_platdma_avail,
  31295. + .pdma_reset = sdhci_bcm2708_platdma_reset,
  31296. +#endif
  31297. + .extra_ints = sdhci_bcm2708_quirk_extra_ints,
  31298. +};
  31299. +
  31300. +/*****************************************************************************\
  31301. + * *
  31302. + * Device probing/removal *
  31303. + * *
  31304. +\*****************************************************************************/
  31305. +
  31306. +static int sdhci_bcm2708_probe(struct platform_device *pdev)
  31307. +{
  31308. + struct sdhci_host *host;
  31309. + struct resource *iomem;
  31310. + struct sdhci_bcm2708_priv *host_priv;
  31311. + int ret;
  31312. +
  31313. + BUG_ON(pdev == NULL);
  31314. +
  31315. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  31316. + if (!iomem) {
  31317. + ret = -ENOMEM;
  31318. + goto err;
  31319. + }
  31320. +
  31321. + if (resource_size(iomem) != 0x100)
  31322. + dev_err(&pdev->dev, "Invalid iomem size. You may "
  31323. + "experience problems.\n");
  31324. +
  31325. + if (pdev->dev.parent)
  31326. + host = sdhci_alloc_host(pdev->dev.parent,
  31327. + sizeof(struct sdhci_bcm2708_priv));
  31328. + else
  31329. + host = sdhci_alloc_host(&pdev->dev,
  31330. + sizeof(struct sdhci_bcm2708_priv));
  31331. +
  31332. + if (IS_ERR(host)) {
  31333. + ret = PTR_ERR(host);
  31334. + goto err;
  31335. + }
  31336. + if (missing_status) {
  31337. + sdhci_bcm2708_ops.missing_status = sdhci_bcm2708_missing_status;
  31338. + }
  31339. +
  31340. + if( spurious_crc_acmd51 ) {
  31341. + sdhci_bcm2708_ops.spurious_crc_acmd51 = sdhci_bcm2708_quirk_spurious_crc_acmd51;
  31342. + }
  31343. +
  31344. +
  31345. + printk("sdhci: %s low-latency mode\n",enable_llm?"Enable":"Disable");
  31346. +
  31347. + host->hw_name = "BCM2708_Arasan";
  31348. + host->ops = &sdhci_bcm2708_ops;
  31349. + host->irq = platform_get_irq(pdev, 0);
  31350. + host->second_irq = 0;
  31351. +
  31352. + host->quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  31353. + SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  31354. + SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  31355. + SDHCI_QUIRK_MISSING_CAPS |
  31356. + SDHCI_QUIRK_NO_HISPD_BIT |
  31357. + (sync_after_dma ? 0:SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12);
  31358. +
  31359. +
  31360. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31361. + host->flags = SDHCI_USE_PLATDMA;
  31362. +#endif
  31363. +
  31364. + if (!request_mem_region(iomem->start, resource_size(iomem),
  31365. + mmc_hostname(host->mmc))) {
  31366. + dev_err(&pdev->dev, "cannot request region\n");
  31367. + ret = -EBUSY;
  31368. + goto err_request;
  31369. + }
  31370. +
  31371. + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  31372. + if (!host->ioaddr) {
  31373. + dev_err(&pdev->dev, "failed to remap registers\n");
  31374. + ret = -ENOMEM;
  31375. + goto err_remap;
  31376. + }
  31377. +
  31378. + host_priv = SDHCI_HOST_PRIV(host);
  31379. +
  31380. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31381. + host_priv->dma_wanted = 0;
  31382. +#ifdef CHECK_DMA_USE
  31383. + host_priv->dmas_pending = 0;
  31384. + host_priv->when_started = 0;
  31385. + host_priv->when_reset = 0;
  31386. + host_priv->when_stopped = 0;
  31387. +#endif
  31388. + host_priv->sg_ix = 0;
  31389. + host_priv->sg_done = 0;
  31390. + host_priv->complete = NULL;
  31391. + host_priv->dma_waits = SDHCI_BCM_DMA_WAITS;
  31392. +
  31393. + host_priv->cb_base = dma_alloc_writecombine(&pdev->dev, SZ_4K,
  31394. + &host_priv->cb_handle,
  31395. + GFP_KERNEL);
  31396. + if (!host_priv->cb_base) {
  31397. + dev_err(&pdev->dev, "cannot allocate DMA CBs\n");
  31398. + ret = -ENOMEM;
  31399. + goto err_alloc_cb;
  31400. + }
  31401. +
  31402. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
  31403. + &host_priv->dma_chan_base,
  31404. + &host_priv->dma_irq);
  31405. + if (ret < 0) {
  31406. + dev_err(&pdev->dev, "couldn't allocate a DMA channel\n");
  31407. + goto err_add_dma;
  31408. + }
  31409. + host_priv->dma_chan = ret;
  31410. +
  31411. + ret = request_irq(host_priv->dma_irq, sdhci_bcm2708_dma_irq,
  31412. + 0 /*IRQF_SHARED*/, DRIVER_NAME " (dma)", host);
  31413. + if (ret) {
  31414. + dev_err(&pdev->dev, "cannot set DMA IRQ\n");
  31415. + goto err_add_dma_irq;
  31416. + }
  31417. + host->second_irq = host_priv->dma_irq;
  31418. + DBG("DMA CBs %p handle %08X DMA%d %p DMA IRQ %d\n",
  31419. + host_priv->cb_base, (unsigned)host_priv->cb_handle,
  31420. + host_priv->dma_chan, host_priv->dma_chan_base,
  31421. + host_priv->dma_irq);
  31422. +
  31423. + // we support 3.3V
  31424. + host->caps |= SDHCI_CAN_VDD_330;
  31425. + if (allow_highspeed)
  31426. + host->mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  31427. +
  31428. + /* single block writes cause data loss with some SD cards! */
  31429. + host->mmc->caps2 |= MMC_CAP2_FORCE_MULTIBLOCK;
  31430. +#endif
  31431. +
  31432. + ret = sdhci_add_host(host);
  31433. + if (ret)
  31434. + goto err_add_host;
  31435. +
  31436. + platform_set_drvdata(pdev, host);
  31437. + ret = device_create_file(&pdev->dev, &dev_attr_use_dma);
  31438. + ret = device_create_file(&pdev->dev, &dev_attr_dma_wait);
  31439. + ret = device_create_file(&pdev->dev, &dev_attr_status);
  31440. +
  31441. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31442. + /* enable extension fifo for paced DMA transfers */
  31443. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  31444. + sdhci_bcm2708_writel(host, 4, REG_EXRDFIFO_CFG);
  31445. +#endif
  31446. +
  31447. + printk(KERN_INFO "%s: BCM2708 SDHC host at 0x%08llx DMA %d IRQ %d\n",
  31448. + mmc_hostname(host->mmc), (unsigned long long)iomem->start,
  31449. + host_priv->dma_chan, host_priv->dma_irq);
  31450. +
  31451. + return 0;
  31452. +
  31453. +err_add_host:
  31454. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31455. + free_irq(host_priv->dma_irq, host);
  31456. +err_add_dma_irq:
  31457. + bcm_dma_chan_free(host_priv->dma_chan);
  31458. +err_add_dma:
  31459. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  31460. + host_priv->cb_handle);
  31461. +err_alloc_cb:
  31462. +#endif
  31463. + iounmap(host->ioaddr);
  31464. +err_remap:
  31465. + release_mem_region(iomem->start, resource_size(iomem));
  31466. +err_request:
  31467. + sdhci_free_host(host);
  31468. +err:
  31469. + dev_err(&pdev->dev, "probe failed, err %d\n", ret);
  31470. + return ret;
  31471. +}
  31472. +
  31473. +static int sdhci_bcm2708_remove(struct platform_device *pdev)
  31474. +{
  31475. + struct sdhci_host *host = platform_get_drvdata(pdev);
  31476. + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  31477. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  31478. + int dead;
  31479. + u32 scratch;
  31480. +
  31481. + dead = 0;
  31482. + scratch = sdhci_bcm2708_readl(host, SDHCI_INT_STATUS);
  31483. + if (scratch == (u32)-1)
  31484. + dead = 1;
  31485. +
  31486. + device_remove_file(&pdev->dev, &dev_attr_status);
  31487. + device_remove_file(&pdev->dev, &dev_attr_dma_wait);
  31488. + device_remove_file(&pdev->dev, &dev_attr_use_dma);
  31489. +
  31490. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  31491. + free_irq(host_priv->dma_irq, host);
  31492. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  31493. + host_priv->cb_handle);
  31494. +#endif
  31495. + sdhci_remove_host(host, dead);
  31496. + iounmap(host->ioaddr);
  31497. + release_mem_region(iomem->start, resource_size(iomem));
  31498. + sdhci_free_host(host);
  31499. + platform_set_drvdata(pdev, NULL);
  31500. +
  31501. + return 0;
  31502. +}
  31503. +
  31504. +static struct platform_driver sdhci_bcm2708_driver = {
  31505. + .driver = {
  31506. + .name = DRIVER_NAME,
  31507. + .owner = THIS_MODULE,
  31508. + },
  31509. + .probe = sdhci_bcm2708_probe,
  31510. + .remove = sdhci_bcm2708_remove,
  31511. +
  31512. +#ifdef CONFIG_PM
  31513. + .suspend = sdhci_bcm2708_suspend,
  31514. + .resume = sdhci_bcm2708_resume,
  31515. +#endif
  31516. +
  31517. +};
  31518. +
  31519. +/*****************************************************************************\
  31520. + * *
  31521. + * Driver init/exit *
  31522. + * *
  31523. +\*****************************************************************************/
  31524. +
  31525. +static int __init sdhci_drv_init(void)
  31526. +{
  31527. + return platform_driver_register(&sdhci_bcm2708_driver);
  31528. +}
  31529. +
  31530. +static void __exit sdhci_drv_exit(void)
  31531. +{
  31532. + platform_driver_unregister(&sdhci_bcm2708_driver);
  31533. +}
  31534. +
  31535. +module_init(sdhci_drv_init);
  31536. +module_exit(sdhci_drv_exit);
  31537. +
  31538. +module_param(allow_highspeed, bool, 0444);
  31539. +module_param(emmc_clock_freq, int, 0444);
  31540. +module_param(sync_after_dma, bool, 0444);
  31541. +module_param(missing_status, bool, 0444);
  31542. +module_param(spurious_crc_acmd51, bool, 0444);
  31543. +module_param(enable_llm, bool, 0444);
  31544. +module_param(cycle_delay, int, 0444);
  31545. +module_param(extra_messages, bool, 0444);
  31546. +
  31547. +MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
  31548. +MODULE_AUTHOR("Broadcom <info@broadcom.com>");
  31549. +MODULE_LICENSE("GPL v2");
  31550. +MODULE_ALIAS("platform:"DRIVER_NAME);
  31551. +
  31552. +MODULE_PARM_DESC(allow_highspeed, "Allow high speed transfers modes");
  31553. +MODULE_PARM_DESC(emmc_clock_freq, "Specify the speed of emmc clock");
  31554. +MODULE_PARM_DESC(sync_after_dma, "Block in driver until dma complete");
  31555. +MODULE_PARM_DESC(missing_status, "Use the missing status quirk");
  31556. +MODULE_PARM_DESC(spurious_crc_acmd51, "Use the spurious crc quirk for reading SCR (ACMD51)");
  31557. +MODULE_PARM_DESC(enable_llm, "Enable low-latency mode");
  31558. +MODULE_PARM_DESC(extra_messages, "Enable more sdcard warning messages");
  31559. +
  31560. +
  31561. diff -Nur linux-3.13.6/drivers/mmc/host/sdhci.c linux-raspberry-pi/drivers/mmc/host/sdhci.c
  31562. --- linux-3.13.6/drivers/mmc/host/sdhci.c 2014-03-07 07:07:02.000000000 +0100
  31563. +++ linux-raspberry-pi/drivers/mmc/host/sdhci.c 2014-03-11 16:54:58.000000000 +0100
  31564. @@ -28,6 +28,7 @@
  31565. #include <linux/mmc/mmc.h>
  31566. #include <linux/mmc/host.h>
  31567. #include <linux/mmc/card.h>
  31568. +#include <linux/mmc/sd.h>
  31569. #include <linux/mmc/slot-gpio.h>
  31570. #include "sdhci.h"
  31571. @@ -130,6 +131,99 @@
  31572. * Low level functions *
  31573. * *
  31574. \*****************************************************************************/
  31575. +extern bool enable_llm;
  31576. +static int sdhci_locked=0;
  31577. +void sdhci_spin_lock(struct sdhci_host *host)
  31578. +{
  31579. + spin_lock(&host->lock);
  31580. +#ifdef CONFIG_PREEMPT
  31581. + if(enable_llm)
  31582. + {
  31583. + disable_irq_nosync(host->irq);
  31584. + if(host->second_irq)
  31585. + disable_irq_nosync(host->second_irq);
  31586. + local_irq_enable();
  31587. + }
  31588. +#endif
  31589. +}
  31590. +
  31591. +void sdhci_spin_unlock(struct sdhci_host *host)
  31592. +{
  31593. +#ifdef CONFIG_PREEMPT
  31594. + if(enable_llm)
  31595. + {
  31596. + local_irq_disable();
  31597. + if(host->second_irq)
  31598. + enable_irq(host->second_irq);
  31599. + enable_irq(host->irq);
  31600. + }
  31601. +#endif
  31602. + spin_unlock(&host->lock);
  31603. +}
  31604. +
  31605. +void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags)
  31606. +{
  31607. +#ifdef CONFIG_PREEMPT
  31608. + if(enable_llm)
  31609. + {
  31610. + while(sdhci_locked)
  31611. + {
  31612. + preempt_schedule();
  31613. + }
  31614. + spin_lock_irqsave(&host->lock,*flags);
  31615. + disable_irq(host->irq);
  31616. + if(host->second_irq)
  31617. + disable_irq(host->second_irq);
  31618. + local_irq_enable();
  31619. + }
  31620. + else
  31621. +#endif
  31622. + spin_lock_irqsave(&host->lock,*flags);
  31623. +}
  31624. +
  31625. +void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags)
  31626. +{
  31627. +#ifdef CONFIG_PREEMPT
  31628. + if(enable_llm)
  31629. + {
  31630. + local_irq_disable();
  31631. + if(host->second_irq)
  31632. + enable_irq(host->second_irq);
  31633. + enable_irq(host->irq);
  31634. + }
  31635. +#endif
  31636. + spin_unlock_irqrestore(&host->lock,flags);
  31637. +}
  31638. +
  31639. +static void sdhci_spin_enable_schedule(struct sdhci_host *host)
  31640. +{
  31641. +#ifdef CONFIG_PREEMPT
  31642. + if(enable_llm)
  31643. + {
  31644. + sdhci_locked = 1;
  31645. + preempt_enable();
  31646. + }
  31647. +#endif
  31648. +}
  31649. +
  31650. +static void sdhci_spin_disable_schedule(struct sdhci_host *host)
  31651. +{
  31652. +#ifdef CONFIG_PREEMPT
  31653. + if(enable_llm)
  31654. + {
  31655. + preempt_disable();
  31656. + sdhci_locked = 0;
  31657. + }
  31658. +#endif
  31659. +}
  31660. +
  31661. +
  31662. +#undef spin_lock_irqsave
  31663. +#define spin_lock_irqsave(host_lock, flags) sdhci_spin_lock_irqsave(container_of(host_lock, struct sdhci_host, lock), &flags)
  31664. +#define spin_unlock_irqrestore(host_lock, flags) sdhci_spin_unlock_irqrestore(container_of(host_lock, struct sdhci_host, lock), flags)
  31665. +
  31666. +#define spin_lock(host_lock) sdhci_spin_lock(container_of(host_lock, struct sdhci_host, lock))
  31667. +#define spin_unlock(host_lock) sdhci_spin_unlock(container_of(host_lock, struct sdhci_host, lock))
  31668. static void sdhci_clear_set_irqs(struct sdhci_host *host, u32 clear, u32 set)
  31669. {
  31670. @@ -299,7 +393,7 @@
  31671. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  31672. unsigned long flags;
  31673. - spin_lock_irqsave(&host->lock, flags);
  31674. + sdhci_spin_lock_irqsave(host, &flags);
  31675. if (host->runtime_suspended)
  31676. goto out;
  31677. @@ -309,7 +403,7 @@
  31678. else
  31679. sdhci_activate_led(host);
  31680. out:
  31681. - spin_unlock_irqrestore(&host->lock, flags);
  31682. + sdhci_spin_unlock_irqrestore(host, flags);
  31683. }
  31684. #endif
  31685. @@ -326,7 +420,7 @@
  31686. u32 uninitialized_var(scratch);
  31687. u8 *buf;
  31688. - DBG("PIO reading\n");
  31689. + DBG("PIO reading %db\n", host->data->blksz);
  31690. blksize = host->data->blksz;
  31691. chunk = 0;
  31692. @@ -371,7 +465,7 @@
  31693. u32 scratch;
  31694. u8 *buf;
  31695. - DBG("PIO writing\n");
  31696. + DBG("PIO writing %db\n", host->data->blksz);
  31697. blksize = host->data->blksz;
  31698. chunk = 0;
  31699. @@ -410,19 +504,28 @@
  31700. local_irq_restore(flags);
  31701. }
  31702. -static void sdhci_transfer_pio(struct sdhci_host *host)
  31703. +static void sdhci_transfer_pio(struct sdhci_host *host, u32 intstate)
  31704. {
  31705. u32 mask;
  31706. + u32 state = 0;
  31707. + u32 intmask;
  31708. + int available;
  31709. BUG_ON(!host->data);
  31710. if (host->blocks == 0)
  31711. return;
  31712. - if (host->data->flags & MMC_DATA_READ)
  31713. + if (host->data->flags & MMC_DATA_READ) {
  31714. mask = SDHCI_DATA_AVAILABLE;
  31715. - else
  31716. + intmask = SDHCI_INT_DATA_AVAIL;
  31717. + } else {
  31718. mask = SDHCI_SPACE_AVAILABLE;
  31719. + intmask = SDHCI_INT_SPACE_AVAIL;
  31720. + }
  31721. +
  31722. + /* initially we can see whether we can procede using intstate */
  31723. + available = (intstate & intmask);
  31724. /*
  31725. * Some controllers (JMicron JMB38x) mess up the buffer bits
  31726. @@ -433,7 +536,7 @@
  31727. (host->data->blocks == 1))
  31728. mask = ~0;
  31729. - while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  31730. + while (available) {
  31731. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  31732. udelay(100);
  31733. @@ -445,9 +548,12 @@
  31734. host->blocks--;
  31735. if (host->blocks == 0)
  31736. break;
  31737. + state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  31738. + available = state & mask;
  31739. + break;
  31740. }
  31741. - DBG("PIO transfer complete.\n");
  31742. + DBG("PIO transfer complete - %d blocks left.\n", host->blocks);
  31743. }
  31744. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  31745. @@ -720,7 +826,9 @@
  31746. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  31747. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  31748. - if (host->flags & SDHCI_REQ_USE_DMA)
  31749. + /* platform DMA will begin on receipt of PIO irqs */
  31750. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  31751. + !(host->flags & SDHCI_USE_PLATDMA))
  31752. sdhci_clear_set_irqs(host, pio_irqs, dma_irqs);
  31753. else
  31754. sdhci_clear_set_irqs(host, dma_irqs, pio_irqs);
  31755. @@ -752,44 +860,25 @@
  31756. host->data_early = 0;
  31757. host->data->bytes_xfered = 0;
  31758. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  31759. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA | SDHCI_USE_PLATDMA))
  31760. host->flags |= SDHCI_REQ_USE_DMA;
  31761. /*
  31762. * FIXME: This doesn't account for merging when mapping the
  31763. * scatterlist.
  31764. */
  31765. - if (host->flags & SDHCI_REQ_USE_DMA) {
  31766. - int broken, i;
  31767. - struct scatterlist *sg;
  31768. -
  31769. - broken = 0;
  31770. - if (host->flags & SDHCI_USE_ADMA) {
  31771. - if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  31772. - broken = 1;
  31773. - } else {
  31774. - if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  31775. - broken = 1;
  31776. - }
  31777. -
  31778. - if (unlikely(broken)) {
  31779. - for_each_sg(data->sg, sg, data->sg_len, i) {
  31780. - if (sg->length & 0x3) {
  31781. - DBG("Reverting to PIO because of "
  31782. - "transfer size (%d)\n",
  31783. - sg->length);
  31784. - host->flags &= ~SDHCI_REQ_USE_DMA;
  31785. - break;
  31786. - }
  31787. - }
  31788. - }
  31789. - }
  31790. /*
  31791. * The assumption here being that alignment is the same after
  31792. * translation to device address space.
  31793. */
  31794. - if (host->flags & SDHCI_REQ_USE_DMA) {
  31795. + if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) ==
  31796. + (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) {
  31797. +
  31798. + if (! sdhci_platdma_dmaable(host, data))
  31799. + host->flags &= ~SDHCI_REQ_USE_DMA;
  31800. +
  31801. + } else if (host->flags & SDHCI_REQ_USE_DMA) {
  31802. int broken, i;
  31803. struct scatterlist *sg;
  31804. @@ -848,7 +937,8 @@
  31805. */
  31806. WARN_ON(1);
  31807. host->flags &= ~SDHCI_REQ_USE_DMA;
  31808. - } else {
  31809. + } else
  31810. + if (!(host->flags & SDHCI_USE_PLATDMA)) {
  31811. WARN_ON(sg_cnt != 1);
  31812. sdhci_writel(host, sg_dma_address(data->sg),
  31813. SDHCI_DMA_ADDRESS);
  31814. @@ -864,11 +954,13 @@
  31815. if (host->version >= SDHCI_SPEC_200) {
  31816. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  31817. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  31818. + if (! (host->flags & SDHCI_USE_PLATDMA)) {
  31819. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  31820. (host->flags & SDHCI_USE_ADMA))
  31821. ctrl |= SDHCI_CTRL_ADMA32;
  31822. else
  31823. ctrl |= SDHCI_CTRL_SDMA;
  31824. + }
  31825. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  31826. }
  31827. @@ -920,7 +1012,8 @@
  31828. if (data->flags & MMC_DATA_READ)
  31829. mode |= SDHCI_TRNS_READ;
  31830. - if (host->flags & SDHCI_REQ_USE_DMA)
  31831. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  31832. + !(host->flags & SDHCI_USE_PLATDMA))
  31833. mode |= SDHCI_TRNS_DMA;
  31834. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  31835. @@ -936,13 +1029,16 @@
  31836. host->data = NULL;
  31837. if (host->flags & SDHCI_REQ_USE_DMA) {
  31838. - if (host->flags & SDHCI_USE_ADMA)
  31839. - sdhci_adma_table_post(host, data);
  31840. - else {
  31841. + /* we may have to abandon an ongoing platform DMA */
  31842. + if (host->flags & SDHCI_USE_PLATDMA)
  31843. + sdhci_platdma_reset(host, data);
  31844. +
  31845. + if (host->flags & (SDHCI_USE_PLATDMA | SDHCI_USE_SDMA)) {
  31846. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  31847. data->sg_len, (data->flags & MMC_DATA_READ) ?
  31848. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  31849. - }
  31850. + } else if (host->flags & SDHCI_USE_ADMA)
  31851. + sdhci_adma_table_post(host, data);
  31852. }
  31853. /*
  31854. @@ -995,6 +1091,12 @@
  31855. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  31856. mask |= SDHCI_DATA_INHIBIT;
  31857. + if(host->ops->missing_status && (cmd->opcode == MMC_SEND_STATUS)) {
  31858. + timeout = 5000; // Really obscenely large delay to send the status, due to bug in controller
  31859. + // which might cause the STATUS command to get stuck when a data operation is in flow
  31860. + mask |= SDHCI_DATA_INHIBIT;
  31861. + }
  31862. +
  31863. /* We shouldn't wait for data inihibit for stop commands, even
  31864. though they might use busy signaling */
  31865. if (host->mrq->data && (cmd == host->mrq->data->stop))
  31866. @@ -1010,12 +1112,20 @@
  31867. return;
  31868. }
  31869. timeout--;
  31870. + sdhci_spin_enable_schedule(host);
  31871. mdelay(1);
  31872. + sdhci_spin_disable_schedule(host);
  31873. }
  31874. + DBG("send cmd %d - wait 0x%X irq 0x%x\n", cmd->opcode, mask,
  31875. + sdhci_readl(host, SDHCI_INT_STATUS));
  31876. mod_timer(&host->timer, jiffies + 10 * HZ);
  31877. host->cmd = cmd;
  31878. + if (host->last_cmdop == MMC_APP_CMD)
  31879. + host->last_cmdop = -cmd->opcode;
  31880. + else
  31881. + host->last_cmdop = cmd->opcode;
  31882. sdhci_prepare_data(host, cmd);
  31883. @@ -1232,7 +1342,9 @@
  31884. return;
  31885. }
  31886. timeout--;
  31887. + sdhci_spin_enable_schedule(host);
  31888. mdelay(1);
  31889. + sdhci_spin_disable_schedule(host);
  31890. }
  31891. clk |= SDHCI_CLOCK_CARD_EN;
  31892. @@ -1333,7 +1445,7 @@
  31893. sdhci_runtime_pm_get(host);
  31894. - spin_lock_irqsave(&host->lock, flags);
  31895. + sdhci_spin_lock_irqsave(host, &flags);
  31896. WARN_ON(host->mrq != NULL);
  31897. @@ -1391,9 +1503,9 @@
  31898. mmc->card->type == MMC_TYPE_MMC ?
  31899. MMC_SEND_TUNING_BLOCK_HS200 :
  31900. MMC_SEND_TUNING_BLOCK;
  31901. - spin_unlock_irqrestore(&host->lock, flags);
  31902. + sdhci_spin_unlock_irqrestore(host, flags);
  31903. sdhci_execute_tuning(mmc, tuning_opcode);
  31904. - spin_lock_irqsave(&host->lock, flags);
  31905. + sdhci_spin_lock_irqsave(host, &flags);
  31906. /* Restore original mmc_request structure */
  31907. host->mrq = mrq;
  31908. @@ -1407,7 +1519,7 @@
  31909. }
  31910. mmiowb();
  31911. - spin_unlock_irqrestore(&host->lock, flags);
  31912. + sdhci_spin_unlock_irqrestore(host, flags);
  31913. }
  31914. static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
  31915. @@ -1416,10 +1528,10 @@
  31916. int vdd_bit = -1;
  31917. u8 ctrl;
  31918. - spin_lock_irqsave(&host->lock, flags);
  31919. + sdhci_spin_lock_irqsave(host, &flags);
  31920. if (host->flags & SDHCI_DEVICE_DEAD) {
  31921. - spin_unlock_irqrestore(&host->lock, flags);
  31922. + sdhci_spin_unlock_irqrestore(host, flags);
  31923. if (host->vmmc && ios->power_mode == MMC_POWER_OFF)
  31924. mmc_regulator_set_ocr(host->mmc, host->vmmc, 0);
  31925. return;
  31926. @@ -1447,9 +1559,9 @@
  31927. vdd_bit = sdhci_set_power(host, ios->vdd);
  31928. if (host->vmmc && vdd_bit != -1) {
  31929. - spin_unlock_irqrestore(&host->lock, flags);
  31930. + sdhci_spin_unlock_irqrestore(host, flags);
  31931. mmc_regulator_set_ocr(host->mmc, host->vmmc, vdd_bit);
  31932. - spin_lock_irqsave(&host->lock, flags);
  31933. + sdhci_spin_lock_irqsave(host, &flags);
  31934. }
  31935. if (host->ops->platform_send_init_74_clocks)
  31936. @@ -1586,7 +1698,7 @@
  31937. sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  31938. mmiowb();
  31939. - spin_unlock_irqrestore(&host->lock, flags);
  31940. + sdhci_spin_unlock_irqrestore(host, flags);
  31941. }
  31942. static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  31943. @@ -1634,7 +1746,7 @@
  31944. unsigned long flags;
  31945. int is_readonly;
  31946. - spin_lock_irqsave(&host->lock, flags);
  31947. + sdhci_spin_lock_irqsave(host, &flags);
  31948. if (host->flags & SDHCI_DEVICE_DEAD)
  31949. is_readonly = 0;
  31950. @@ -1644,7 +1756,7 @@
  31951. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  31952. & SDHCI_WRITE_PROTECT);
  31953. - spin_unlock_irqrestore(&host->lock, flags);
  31954. + sdhci_spin_unlock_irqrestore(host, flags);
  31955. /* This quirk needs to be replaced by a callback-function later */
  31956. return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
  31957. @@ -1717,9 +1829,9 @@
  31958. struct sdhci_host *host = mmc_priv(mmc);
  31959. unsigned long flags;
  31960. - spin_lock_irqsave(&host->lock, flags);
  31961. + sdhci_spin_lock_irqsave(host, &flags);
  31962. sdhci_enable_sdio_irq_nolock(host, enable);
  31963. - spin_unlock_irqrestore(&host->lock, flags);
  31964. + sdhci_spin_unlock_irqrestore(host, flags);
  31965. }
  31966. static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
  31967. @@ -2070,7 +2182,7 @@
  31968. if (host->ops->card_event)
  31969. host->ops->card_event(host);
  31970. - spin_lock_irqsave(&host->lock, flags);
  31971. + sdhci_spin_lock_irqsave(host, &flags);
  31972. /* Check host->mrq first in case we are runtime suspended */
  31973. if (host->mrq && !sdhci_do_get_cd(host)) {
  31974. @@ -2086,7 +2198,7 @@
  31975. tasklet_schedule(&host->finish_tasklet);
  31976. }
  31977. - spin_unlock_irqrestore(&host->lock, flags);
  31978. + sdhci_spin_unlock_irqrestore(host, flags);
  31979. }
  31980. static const struct mmc_host_ops sdhci_ops = {
  31981. @@ -2125,14 +2237,14 @@
  31982. host = (struct sdhci_host*)param;
  31983. - spin_lock_irqsave(&host->lock, flags);
  31984. + sdhci_spin_lock_irqsave(host, &flags);
  31985. /*
  31986. * If this tasklet gets rescheduled while running, it will
  31987. * be run again afterwards but without any active request.
  31988. */
  31989. if (!host->mrq) {
  31990. - spin_unlock_irqrestore(&host->lock, flags);
  31991. + sdhci_spin_unlock_irqrestore(host, flags);
  31992. return;
  31993. }
  31994. @@ -2170,7 +2282,7 @@
  31995. #endif
  31996. mmiowb();
  31997. - spin_unlock_irqrestore(&host->lock, flags);
  31998. + sdhci_spin_unlock_irqrestore(host, flags);
  31999. mmc_request_done(host->mmc, mrq);
  32000. sdhci_runtime_pm_put(host);
  32001. @@ -2183,11 +2295,11 @@
  32002. host = (struct sdhci_host*)data;
  32003. - spin_lock_irqsave(&host->lock, flags);
  32004. + sdhci_spin_lock_irqsave(host, &flags);
  32005. if (host->mrq) {
  32006. pr_err("%s: Timeout waiting for hardware "
  32007. - "interrupt.\n", mmc_hostname(host->mmc));
  32008. + "interrupt - cmd%d.\n", mmc_hostname(host->mmc), host->last_cmdop);
  32009. sdhci_dumpregs(host);
  32010. if (host->data) {
  32011. @@ -2204,7 +2316,7 @@
  32012. }
  32013. mmiowb();
  32014. - spin_unlock_irqrestore(&host->lock, flags);
  32015. + sdhci_spin_unlock_irqrestore(host, flags);
  32016. }
  32017. static void sdhci_tuning_timer(unsigned long data)
  32018. @@ -2214,11 +2326,11 @@
  32019. host = (struct sdhci_host *)data;
  32020. - spin_lock_irqsave(&host->lock, flags);
  32021. + sdhci_spin_lock_irqsave(host, &flags);
  32022. host->flags |= SDHCI_NEEDS_RETUNING;
  32023. - spin_unlock_irqrestore(&host->lock, flags);
  32024. + sdhci_spin_unlock_irqrestore(host, flags);
  32025. }
  32026. /*****************************************************************************\
  32027. @@ -2232,10 +2344,13 @@
  32028. BUG_ON(intmask == 0);
  32029. if (!host->cmd) {
  32030. + if (!(host->ops->extra_ints)) {
  32031. pr_err("%s: Got command interrupt 0x%08x even "
  32032. "though no command operation was in progress.\n",
  32033. mmc_hostname(host->mmc), (unsigned)intmask);
  32034. sdhci_dumpregs(host);
  32035. + } else
  32036. + DBG("cmd irq 0x%08x cmd complete\n", (unsigned)intmask);
  32037. return;
  32038. }
  32039. @@ -2305,6 +2420,19 @@
  32040. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  32041. #endif
  32042. +static void sdhci_data_end(struct sdhci_host *host)
  32043. +{
  32044. + if (host->cmd) {
  32045. + /*
  32046. + * Data managed to finish before the
  32047. + * command completed. Make sure we do
  32048. + * things in the proper order.
  32049. + */
  32050. + host->data_early = 1;
  32051. + } else
  32052. + sdhci_finish_data(host);
  32053. +}
  32054. +
  32055. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  32056. {
  32057. u32 command;
  32058. @@ -2334,23 +2462,39 @@
  32059. }
  32060. }
  32061. + if (!(host->ops->extra_ints)) {
  32062. pr_err("%s: Got data interrupt 0x%08x even "
  32063. "though no data operation was in progress.\n",
  32064. mmc_hostname(host->mmc), (unsigned)intmask);
  32065. sdhci_dumpregs(host);
  32066. + } else
  32067. + DBG("data irq 0x%08x but no data\n", (unsigned)intmask);
  32068. return;
  32069. }
  32070. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  32071. host->data->error = -ETIMEDOUT;
  32072. - else if (intmask & SDHCI_INT_DATA_END_BIT)
  32073. + else if (intmask & SDHCI_INT_DATA_END_BIT) {
  32074. + DBG("end error in cmd %d\n", host->last_cmdop);
  32075. + if (host->ops->spurious_crc_acmd51 &&
  32076. + host->last_cmdop == -SD_APP_SEND_SCR) {
  32077. + DBG("ignoring spurious data_end_bit error\n");
  32078. + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
  32079. + } else
  32080. host->data->error = -EILSEQ;
  32081. - else if ((intmask & SDHCI_INT_DATA_CRC) &&
  32082. + } else if ((intmask & SDHCI_INT_DATA_CRC) &&
  32083. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  32084. - != MMC_BUS_TEST_R)
  32085. + != MMC_BUS_TEST_R) {
  32086. + DBG("crc error in cmd %d\n", host->last_cmdop);
  32087. + if (host->ops->spurious_crc_acmd51 &&
  32088. + host->last_cmdop == -SD_APP_SEND_SCR) {
  32089. + DBG("ignoring spurious data_crc_bit error\n");
  32090. + intmask = SDHCI_INT_DATA_AVAIL|SDHCI_INT_DATA_END;
  32091. + } else {
  32092. host->data->error = -EILSEQ;
  32093. - else if (intmask & SDHCI_INT_ADMA_ERROR) {
  32094. + }
  32095. + } else if (intmask & SDHCI_INT_ADMA_ERROR) {
  32096. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  32097. sdhci_show_adma_error(host);
  32098. host->data->error = -EIO;
  32099. @@ -2358,11 +2502,18 @@
  32100. host->ops->adma_workaround(host, intmask);
  32101. }
  32102. - if (host->data->error)
  32103. + if (host->data->error) {
  32104. + DBG("finish request early on error %d\n", host->data->error);
  32105. sdhci_finish_data(host);
  32106. - else {
  32107. - if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  32108. - sdhci_transfer_pio(host);
  32109. + } else {
  32110. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
  32111. + if (host->flags & SDHCI_REQ_USE_DMA) {
  32112. + /* possible only in PLATDMA mode */
  32113. + sdhci_platdma_avail(host, &intmask,
  32114. + &sdhci_data_end);
  32115. + } else
  32116. + sdhci_transfer_pio(host, intmask);
  32117. + }
  32118. /*
  32119. * We currently don't do anything fancy with DMA
  32120. @@ -2391,18 +2542,8 @@
  32121. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  32122. }
  32123. - if (intmask & SDHCI_INT_DATA_END) {
  32124. - if (host->cmd) {
  32125. - /*
  32126. - * Data managed to finish before the
  32127. - * command completed. Make sure we do
  32128. - * things in the proper order.
  32129. - */
  32130. - host->data_early = 1;
  32131. - } else {
  32132. - sdhci_finish_data(host);
  32133. - }
  32134. - }
  32135. + if (intmask & SDHCI_INT_DATA_END)
  32136. + sdhci_data_end(host);
  32137. }
  32138. }
  32139. @@ -2413,10 +2554,10 @@
  32140. u32 intmask, unexpected = 0;
  32141. int cardint = 0, max_loops = 16;
  32142. - spin_lock(&host->lock);
  32143. + sdhci_spin_lock(host);
  32144. if (host->runtime_suspended) {
  32145. - spin_unlock(&host->lock);
  32146. + sdhci_spin_unlock(host);
  32147. pr_warning("%s: got irq while runtime suspended\n",
  32148. mmc_hostname(host->mmc));
  32149. return IRQ_HANDLED;
  32150. @@ -2458,6 +2599,22 @@
  32151. tasklet_schedule(&host->card_tasklet);
  32152. }
  32153. + if (intmask & SDHCI_INT_ERROR_MASK & ~SDHCI_INT_ERROR)
  32154. + DBG("controller reports error 0x%x -"
  32155. + "%s%s%s%s%s%s%s%s%s%s",
  32156. + intmask,
  32157. + intmask & SDHCI_INT_TIMEOUT? " timeout": "",
  32158. + intmask & SDHCI_INT_CRC ? " crc": "",
  32159. + intmask & SDHCI_INT_END_BIT? " endbit": "",
  32160. + intmask & SDHCI_INT_INDEX? " index": "",
  32161. + intmask & SDHCI_INT_DATA_TIMEOUT? " data_timeout": "",
  32162. + intmask & SDHCI_INT_DATA_CRC? " data_crc": "",
  32163. + intmask & SDHCI_INT_DATA_END_BIT? " data_endbit": "",
  32164. + intmask & SDHCI_INT_BUS_POWER? " buspower": "",
  32165. + intmask & SDHCI_INT_ACMD12ERR? " acmd12": "",
  32166. + intmask & SDHCI_INT_ADMA_ERROR? " adma": ""
  32167. + );
  32168. +
  32169. if (intmask & SDHCI_INT_CMD_MASK) {
  32170. sdhci_writel(host, intmask & SDHCI_INT_CMD_MASK,
  32171. SDHCI_INT_STATUS);
  32172. @@ -2472,7 +2629,13 @@
  32173. intmask &= ~(SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK);
  32174. - intmask &= ~SDHCI_INT_ERROR;
  32175. + if (intmask & SDHCI_INT_ERROR_MASK) {
  32176. + /* collect any uncovered errors */
  32177. + sdhci_writel(host, intmask & SDHCI_INT_ERROR_MASK,
  32178. + SDHCI_INT_STATUS);
  32179. + }
  32180. +
  32181. + intmask &= ~SDHCI_INT_ERROR_MASK;
  32182. if (intmask & SDHCI_INT_BUS_POWER) {
  32183. pr_err("%s: Card is consuming too much power!\n",
  32184. @@ -2506,7 +2669,7 @@
  32185. if (intmask && --max_loops)
  32186. goto again;
  32187. out:
  32188. - spin_unlock(&host->lock);
  32189. + sdhci_spin_unlock(host);
  32190. if (unexpected) {
  32191. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  32192. @@ -2585,13 +2748,14 @@
  32193. {
  32194. int ret = 0;
  32195. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  32196. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32197. + SDHCI_USE_PLATDMA)) {
  32198. if (host->ops->enable_dma)
  32199. host->ops->enable_dma(host);
  32200. }
  32201. if (!device_may_wakeup(mmc_dev(host->mmc))) {
  32202. - ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  32203. + ret = request_irq(host->irq, sdhci_irq, 0 /*IRQF_SHARED*/,
  32204. mmc_hostname(host->mmc), host);
  32205. if (ret)
  32206. return ret;
  32207. @@ -2667,15 +2831,15 @@
  32208. host->flags &= ~SDHCI_NEEDS_RETUNING;
  32209. }
  32210. - spin_lock_irqsave(&host->lock, flags);
  32211. + sdhci_spin_lock_irqsave(host, &flags);
  32212. sdhci_mask_irqs(host, SDHCI_INT_ALL_MASK);
  32213. - spin_unlock_irqrestore(&host->lock, flags);
  32214. + sdhci_spin_unlock_irqrestore(host, flags);
  32215. synchronize_irq(host->irq);
  32216. - spin_lock_irqsave(&host->lock, flags);
  32217. + sdhci_spin_lock_irqsave(host, &flags);
  32218. host->runtime_suspended = true;
  32219. - spin_unlock_irqrestore(&host->lock, flags);
  32220. + sdhci_spin_unlock_irqrestore(host, flags);
  32221. return ret;
  32222. }
  32223. @@ -2701,16 +2865,16 @@
  32224. sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
  32225. if ((host_flags & SDHCI_PV_ENABLED) &&
  32226. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
  32227. - spin_lock_irqsave(&host->lock, flags);
  32228. + sdhci_spin_lock_irqsave(host, &flags);
  32229. sdhci_enable_preset_value(host, true);
  32230. - spin_unlock_irqrestore(&host->lock, flags);
  32231. + sdhci_spin_unlock_irqrestore(host, flags);
  32232. }
  32233. /* Set the re-tuning expiration flag */
  32234. if (host->flags & SDHCI_USING_RETUNING_TIMER)
  32235. host->flags |= SDHCI_NEEDS_RETUNING;
  32236. - spin_lock_irqsave(&host->lock, flags);
  32237. + sdhci_spin_lock_irqsave(host, &flags);
  32238. host->runtime_suspended = false;
  32239. @@ -2721,7 +2885,7 @@
  32240. /* Enable Card Detection */
  32241. sdhci_enable_card_detection(host);
  32242. - spin_unlock_irqrestore(&host->lock, flags);
  32243. + sdhci_spin_unlock_irqrestore(host, flags);
  32244. return ret;
  32245. }
  32246. @@ -2816,14 +2980,16 @@
  32247. host->flags &= ~SDHCI_USE_ADMA;
  32248. }
  32249. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  32250. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32251. + SDHCI_USE_PLATDMA)) {
  32252. if (host->ops->enable_dma) {
  32253. if (host->ops->enable_dma(host)) {
  32254. pr_warning("%s: No suitable DMA "
  32255. "available. Falling back to PIO.\n",
  32256. mmc_hostname(mmc));
  32257. host->flags &=
  32258. - ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  32259. + ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  32260. + SDHCI_USE_PLATDMA);
  32261. }
  32262. }
  32263. }
  32264. @@ -3215,8 +3381,8 @@
  32265. sdhci_init(host, 0);
  32266. - ret = request_irq(host->irq, sdhci_irq, IRQF_SHARED,
  32267. - mmc_hostname(mmc), host);
  32268. + ret = request_irq(host->irq, sdhci_irq, 0 /*IRQF_SHARED*/,
  32269. + mmc_hostname(mmc), host);
  32270. if (ret) {
  32271. pr_err("%s: Failed to request IRQ %d: %d\n",
  32272. mmc_hostname(mmc), host->irq, ret);
  32273. @@ -3249,6 +3415,7 @@
  32274. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  32275. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  32276. + (host->flags & SDHCI_USE_PLATDMA) ? "platform's DMA" :
  32277. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  32278. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  32279. @@ -3276,7 +3443,7 @@
  32280. unsigned long flags;
  32281. if (dead) {
  32282. - spin_lock_irqsave(&host->lock, flags);
  32283. + sdhci_spin_lock_irqsave(host, &flags);
  32284. host->flags |= SDHCI_DEVICE_DEAD;
  32285. @@ -3288,7 +3455,7 @@
  32286. tasklet_schedule(&host->finish_tasklet);
  32287. }
  32288. - spin_unlock_irqrestore(&host->lock, flags);
  32289. + sdhci_spin_unlock_irqrestore(host, flags);
  32290. }
  32291. sdhci_disable_card_detection(host);
  32292. diff -Nur linux-3.13.6/drivers/mmc/host/sdhci.h linux-raspberry-pi/drivers/mmc/host/sdhci.h
  32293. --- linux-3.13.6/drivers/mmc/host/sdhci.h 2014-03-07 07:07:02.000000000 +0100
  32294. +++ linux-raspberry-pi/drivers/mmc/host/sdhci.h 2014-03-11 16:54:58.000000000 +0100
  32295. @@ -290,6 +290,18 @@
  32296. void (*platform_reset_exit)(struct sdhci_host *host, u8 mask);
  32297. int (*platform_execute_tuning)(struct sdhci_host *host, u32 opcode);
  32298. int (*set_uhs_signaling)(struct sdhci_host *host, unsigned int uhs);
  32299. +
  32300. + int (*pdma_able)(struct sdhci_host *host,
  32301. + struct mmc_data *data);
  32302. + void (*pdma_avail)(struct sdhci_host *host,
  32303. + unsigned int *ref_intmask,
  32304. + void(*complete)(struct sdhci_host *));
  32305. + void (*pdma_reset)(struct sdhci_host *host,
  32306. + struct mmc_data *data);
  32307. + unsigned int (*extra_ints)(struct sdhci_host *host);
  32308. + unsigned int (*spurious_crc_acmd51)(struct sdhci_host *host);
  32309. + unsigned int (*missing_status)(struct sdhci_host *host);
  32310. +
  32311. void (*hw_reset)(struct sdhci_host *host);
  32312. void (*platform_suspend)(struct sdhci_host *host);
  32313. void (*platform_resume)(struct sdhci_host *host);
  32314. @@ -403,9 +415,38 @@
  32315. extern void sdhci_enable_irq_wakeups(struct sdhci_host *host);
  32316. #endif
  32317. +static inline int /*bool*/
  32318. +sdhci_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  32319. +{
  32320. + if (host->ops->pdma_able)
  32321. + return host->ops->pdma_able(host, data);
  32322. + else
  32323. + return 1;
  32324. +}
  32325. +static inline void
  32326. +sdhci_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  32327. + void(*completion_callback)(struct sdhci_host *))
  32328. +{
  32329. + if (host->ops->pdma_avail)
  32330. + host->ops->pdma_avail(host, ref_intmask, completion_callback);
  32331. +}
  32332. +
  32333. +static inline void
  32334. +sdhci_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  32335. +{
  32336. + if (host->ops->pdma_reset)
  32337. + host->ops->pdma_reset(host, data);
  32338. +}
  32339. +
  32340. #ifdef CONFIG_PM_RUNTIME
  32341. extern int sdhci_runtime_suspend_host(struct sdhci_host *host);
  32342. extern int sdhci_runtime_resume_host(struct sdhci_host *host);
  32343. #endif
  32344. +extern void sdhci_spin_lock_irqsave(struct sdhci_host *host,unsigned long *flags);
  32345. +extern void sdhci_spin_unlock_irqrestore(struct sdhci_host *host,unsigned long flags);
  32346. +extern void sdhci_spin_lock(struct sdhci_host *host);
  32347. +extern void sdhci_spin_unlock(struct sdhci_host *host);
  32348. +
  32349. +
  32350. #endif /* __SDHCI_HW_H */
  32351. diff -Nur linux-3.13.6/drivers/net/usb/smsc95xx.c linux-raspberry-pi/drivers/net/usb/smsc95xx.c
  32352. --- linux-3.13.6/drivers/net/usb/smsc95xx.c 2014-03-07 07:07:02.000000000 +0100
  32353. +++ linux-raspberry-pi/drivers/net/usb/smsc95xx.c 2014-03-11 16:52:52.000000000 +0100
  32354. @@ -61,6 +61,7 @@
  32355. #define SUSPEND_SUSPEND3 (0x08)
  32356. #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
  32357. SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
  32358. +#define MAC_ADDR_LEN (6)
  32359. struct smsc95xx_priv {
  32360. u32 mac_cr;
  32361. @@ -76,6 +77,10 @@
  32362. module_param(turbo_mode, bool, 0644);
  32363. MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
  32364. +static char *macaddr = ":";
  32365. +module_param(macaddr, charp, 0);
  32366. +MODULE_PARM_DESC(macaddr, "MAC address");
  32367. +
  32368. static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
  32369. u32 *data, int in_pm)
  32370. {
  32371. @@ -765,8 +770,59 @@
  32372. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  32373. }
  32374. +/* Check the macaddr module parameter for a MAC address */
  32375. +static int smsc95xx_is_macaddr_param(struct usbnet *dev, u8 *dev_mac)
  32376. +{
  32377. + int i, j, got_num, num;
  32378. + u8 mtbl[MAC_ADDR_LEN];
  32379. +
  32380. + if (macaddr[0] == ':')
  32381. + return 0;
  32382. +
  32383. + i = 0;
  32384. + j = 0;
  32385. + num = 0;
  32386. + got_num = 0;
  32387. + while (j < MAC_ADDR_LEN) {
  32388. + if (macaddr[i] && macaddr[i] != ':') {
  32389. + got_num++;
  32390. + if ('0' <= macaddr[i] && macaddr[i] <= '9')
  32391. + num = num * 16 + macaddr[i] - '0';
  32392. + else if ('A' <= macaddr[i] && macaddr[i] <= 'F')
  32393. + num = num * 16 + 10 + macaddr[i] - 'A';
  32394. + else if ('a' <= macaddr[i] && macaddr[i] <= 'f')
  32395. + num = num * 16 + 10 + macaddr[i] - 'a';
  32396. + else
  32397. + break;
  32398. + i++;
  32399. + } else if (got_num == 2) {
  32400. + mtbl[j++] = (u8) num;
  32401. + num = 0;
  32402. + got_num = 0;
  32403. + i++;
  32404. + } else {
  32405. + break;
  32406. + }
  32407. + }
  32408. +
  32409. + if (j == MAC_ADDR_LEN) {
  32410. + netif_dbg(dev, ifup, dev->net, "Overriding MAC address with: "
  32411. + "%02x:%02x:%02x:%02x:%02x:%02x\n", mtbl[0], mtbl[1], mtbl[2],
  32412. + mtbl[3], mtbl[4], mtbl[5]);
  32413. + for (i = 0; i < MAC_ADDR_LEN; i++)
  32414. + dev_mac[i] = mtbl[i];
  32415. + return 1;
  32416. + } else {
  32417. + return 0;
  32418. + }
  32419. +}
  32420. +
  32421. static void smsc95xx_init_mac_address(struct usbnet *dev)
  32422. {
  32423. + /* Check module parameters */
  32424. + if (smsc95xx_is_macaddr_param(dev, dev->net->dev_addr))
  32425. + return;
  32426. +
  32427. /* try reading mac address from EEPROM */
  32428. if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
  32429. dev->net->dev_addr) == 0) {
  32430. diff -Nur linux-3.13.6/drivers/spi/Kconfig linux-raspberry-pi/drivers/spi/Kconfig
  32431. --- linux-3.13.6/drivers/spi/Kconfig 2014-03-07 07:07:02.000000000 +0100
  32432. +++ linux-raspberry-pi/drivers/spi/Kconfig 2014-03-11 16:55:26.000000000 +0100
  32433. @@ -85,6 +85,14 @@
  32434. is for the regular SPI controller. Slave mode operation is not also
  32435. not supported.
  32436. +config SPI_BCM2708
  32437. + tristate "BCM2708 SPI controller driver (SPI0)"
  32438. + depends on MACH_BCM2708
  32439. + help
  32440. + This selects a driver for the Broadcom BCM2708 SPI master (SPI0). This
  32441. + driver is not compatible with the "Universal SPI Master" or the SPI slave
  32442. + device.
  32443. +
  32444. config SPI_BFIN5XX
  32445. tristate "SPI controller driver for ADI Blackfin5xx"
  32446. depends on BLACKFIN && !BF60x
  32447. diff -Nur linux-3.13.6/drivers/spi/Makefile linux-raspberry-pi/drivers/spi/Makefile
  32448. --- linux-3.13.6/drivers/spi/Makefile 2014-03-07 07:07:02.000000000 +0100
  32449. +++ linux-raspberry-pi/drivers/spi/Makefile 2014-03-11 16:55:26.000000000 +0100
  32450. @@ -18,6 +18,7 @@
  32451. obj-$(CONFIG_SPI_BCM63XX) += spi-bcm63xx.o
  32452. obj-$(CONFIG_SPI_BFIN5XX) += spi-bfin5xx.o
  32453. obj-$(CONFIG_SPI_BFIN_V3) += spi-bfin-v3.o
  32454. +obj-$(CONFIG_SPI_BCM2708) += spi-bcm2708.o
  32455. obj-$(CONFIG_SPI_BFIN_SPORT) += spi-bfin-sport.o
  32456. obj-$(CONFIG_SPI_BITBANG) += spi-bitbang.o
  32457. obj-$(CONFIG_SPI_BUTTERFLY) += spi-butterfly.o
  32458. diff -Nur linux-3.13.6/drivers/spi/spi-bcm2708.c linux-raspberry-pi/drivers/spi/spi-bcm2708.c
  32459. --- linux-3.13.6/drivers/spi/spi-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  32460. +++ linux-raspberry-pi/drivers/spi/spi-bcm2708.c 2014-03-11 16:55:26.000000000 +0100
  32461. @@ -0,0 +1,626 @@
  32462. +/*
  32463. + * Driver for Broadcom BCM2708 SPI Controllers
  32464. + *
  32465. + * Copyright (C) 2012 Chris Boot
  32466. + *
  32467. + * This driver is inspired by:
  32468. + * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  32469. + * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  32470. + *
  32471. + * This program is free software; you can redistribute it and/or modify
  32472. + * it under the terms of the GNU General Public License as published by
  32473. + * the Free Software Foundation; either version 2 of the License, or
  32474. + * (at your option) any later version.
  32475. + *
  32476. + * This program is distributed in the hope that it will be useful,
  32477. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32478. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32479. + * GNU General Public License for more details.
  32480. + *
  32481. + * You should have received a copy of the GNU General Public License
  32482. + * along with this program; if not, write to the Free Software
  32483. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  32484. + */
  32485. +
  32486. +#include <linux/kernel.h>
  32487. +#include <linux/module.h>
  32488. +#include <linux/spinlock.h>
  32489. +#include <linux/clk.h>
  32490. +#include <linux/err.h>
  32491. +#include <linux/platform_device.h>
  32492. +#include <linux/io.h>
  32493. +#include <linux/spi/spi.h>
  32494. +#include <linux/interrupt.h>
  32495. +#include <linux/delay.h>
  32496. +#include <linux/log2.h>
  32497. +#include <linux/sched.h>
  32498. +#include <linux/wait.h>
  32499. +
  32500. +/* SPI register offsets */
  32501. +#define SPI_CS 0x00
  32502. +#define SPI_FIFO 0x04
  32503. +#define SPI_CLK 0x08
  32504. +#define SPI_DLEN 0x0c
  32505. +#define SPI_LTOH 0x10
  32506. +#define SPI_DC 0x14
  32507. +
  32508. +/* Bitfields in CS */
  32509. +#define SPI_CS_LEN_LONG 0x02000000
  32510. +#define SPI_CS_DMA_LEN 0x01000000
  32511. +#define SPI_CS_CSPOL2 0x00800000
  32512. +#define SPI_CS_CSPOL1 0x00400000
  32513. +#define SPI_CS_CSPOL0 0x00200000
  32514. +#define SPI_CS_RXF 0x00100000
  32515. +#define SPI_CS_RXR 0x00080000
  32516. +#define SPI_CS_TXD 0x00040000
  32517. +#define SPI_CS_RXD 0x00020000
  32518. +#define SPI_CS_DONE 0x00010000
  32519. +#define SPI_CS_LEN 0x00002000
  32520. +#define SPI_CS_REN 0x00001000
  32521. +#define SPI_CS_ADCS 0x00000800
  32522. +#define SPI_CS_INTR 0x00000400
  32523. +#define SPI_CS_INTD 0x00000200
  32524. +#define SPI_CS_DMAEN 0x00000100
  32525. +#define SPI_CS_TA 0x00000080
  32526. +#define SPI_CS_CSPOL 0x00000040
  32527. +#define SPI_CS_CLEAR_RX 0x00000020
  32528. +#define SPI_CS_CLEAR_TX 0x00000010
  32529. +#define SPI_CS_CPOL 0x00000008
  32530. +#define SPI_CS_CPHA 0x00000004
  32531. +#define SPI_CS_CS_10 0x00000002
  32532. +#define SPI_CS_CS_01 0x00000001
  32533. +
  32534. +#define SPI_TIMEOUT_MS 150
  32535. +
  32536. +#define DRV_NAME "bcm2708_spi"
  32537. +
  32538. +struct bcm2708_spi {
  32539. + spinlock_t lock;
  32540. + void __iomem *base;
  32541. + int irq;
  32542. + struct clk *clk;
  32543. + bool stopping;
  32544. +
  32545. + struct list_head queue;
  32546. + struct workqueue_struct *workq;
  32547. + struct work_struct work;
  32548. + struct completion done;
  32549. +
  32550. + const u8 *tx_buf;
  32551. + u8 *rx_buf;
  32552. + int len;
  32553. +};
  32554. +
  32555. +struct bcm2708_spi_state {
  32556. + u32 cs;
  32557. + u16 cdiv;
  32558. +};
  32559. +
  32560. +/*
  32561. + * This function sets the ALT mode on the SPI pins so that we can use them with
  32562. + * the SPI hardware.
  32563. + *
  32564. + * FIXME: This is a hack. Use pinmux / pinctrl.
  32565. + */
  32566. +static void bcm2708_init_pinmode(void)
  32567. +{
  32568. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  32569. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  32570. +
  32571. + int pin;
  32572. + u32 *gpio = ioremap(0x20200000, SZ_16K);
  32573. +
  32574. + /* SPI is on GPIO 7..11 */
  32575. + for (pin = 7; pin <= 11; pin++) {
  32576. + INP_GPIO(pin); /* set mode to GPIO input first */
  32577. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  32578. + }
  32579. +
  32580. + iounmap(gpio);
  32581. +
  32582. +#undef INP_GPIO
  32583. +#undef SET_GPIO_ALT
  32584. +}
  32585. +
  32586. +static inline u32 bcm2708_rd(struct bcm2708_spi *bs, unsigned reg)
  32587. +{
  32588. + return readl(bs->base + reg);
  32589. +}
  32590. +
  32591. +static inline void bcm2708_wr(struct bcm2708_spi *bs, unsigned reg, u32 val)
  32592. +{
  32593. + writel(val, bs->base + reg);
  32594. +}
  32595. +
  32596. +static inline void bcm2708_rd_fifo(struct bcm2708_spi *bs, int len)
  32597. +{
  32598. + u8 byte;
  32599. +
  32600. + while (len--) {
  32601. + byte = bcm2708_rd(bs, SPI_FIFO);
  32602. + if (bs->rx_buf)
  32603. + *bs->rx_buf++ = byte;
  32604. + }
  32605. +}
  32606. +
  32607. +static inline void bcm2708_wr_fifo(struct bcm2708_spi *bs, int len)
  32608. +{
  32609. + u8 byte;
  32610. + u16 val;
  32611. +
  32612. + if (len > bs->len)
  32613. + len = bs->len;
  32614. +
  32615. + if (unlikely(bcm2708_rd(bs, SPI_CS) & SPI_CS_LEN)) {
  32616. + /* LoSSI mode */
  32617. + if (unlikely(len % 2)) {
  32618. + printk(KERN_ERR"bcm2708_wr_fifo: length must be even, skipping.\n");
  32619. + bs->len = 0;
  32620. + return;
  32621. + }
  32622. + while (len) {
  32623. + if (bs->tx_buf) {
  32624. + val = *(const u16 *)bs->tx_buf;
  32625. + bs->tx_buf += 2;
  32626. + } else
  32627. + val = 0;
  32628. + bcm2708_wr(bs, SPI_FIFO, val);
  32629. + bs->len -= 2;
  32630. + len -= 2;
  32631. + }
  32632. + return;
  32633. + }
  32634. +
  32635. + while (len--) {
  32636. + byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  32637. + bcm2708_wr(bs, SPI_FIFO, byte);
  32638. + bs->len--;
  32639. + }
  32640. +}
  32641. +
  32642. +static irqreturn_t bcm2708_spi_interrupt(int irq, void *dev_id)
  32643. +{
  32644. + struct spi_master *master = dev_id;
  32645. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  32646. + u32 cs;
  32647. +
  32648. + spin_lock(&bs->lock);
  32649. +
  32650. + cs = bcm2708_rd(bs, SPI_CS);
  32651. +
  32652. + if (cs & SPI_CS_DONE) {
  32653. + if (bs->len) { /* first interrupt in a transfer */
  32654. + /* fill the TX fifo with up to 16 bytes */
  32655. + bcm2708_wr_fifo(bs, 16);
  32656. + } else { /* transfer complete */
  32657. + /* disable interrupts */
  32658. + cs &= ~(SPI_CS_INTR | SPI_CS_INTD);
  32659. + bcm2708_wr(bs, SPI_CS, cs);
  32660. +
  32661. + /* drain RX FIFO */
  32662. + while (cs & SPI_CS_RXD) {
  32663. + bcm2708_rd_fifo(bs, 1);
  32664. + cs = bcm2708_rd(bs, SPI_CS);
  32665. + }
  32666. +
  32667. + /* wake up our bh */
  32668. + complete(&bs->done);
  32669. + }
  32670. + } else if (cs & SPI_CS_RXR) {
  32671. + /* read 12 bytes of data */
  32672. + bcm2708_rd_fifo(bs, 12);
  32673. +
  32674. + /* write up to 12 bytes */
  32675. + bcm2708_wr_fifo(bs, 12);
  32676. + }
  32677. +
  32678. + spin_unlock(&bs->lock);
  32679. +
  32680. + return IRQ_HANDLED;
  32681. +}
  32682. +
  32683. +static int bcm2708_setup_state(struct spi_master *master,
  32684. + struct device *dev, struct bcm2708_spi_state *state,
  32685. + u32 hz, u8 csel, u8 mode, u8 bpw)
  32686. +{
  32687. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  32688. + int cdiv;
  32689. + unsigned long bus_hz;
  32690. + u32 cs = 0;
  32691. +
  32692. + bus_hz = clk_get_rate(bs->clk);
  32693. +
  32694. + if (hz >= bus_hz) {
  32695. + cdiv = 2; /* bus_hz / 2 is as fast as we can go */
  32696. + } else if (hz) {
  32697. + cdiv = DIV_ROUND_UP(bus_hz, hz);
  32698. +
  32699. + /* CDIV must be a power of 2, so round up */
  32700. + cdiv = roundup_pow_of_two(cdiv);
  32701. +
  32702. + if (cdiv > 65536) {
  32703. + dev_dbg(dev,
  32704. + "setup: %d Hz too slow, cdiv %u; min %ld Hz\n",
  32705. + hz, cdiv, bus_hz / 65536);
  32706. + return -EINVAL;
  32707. + } else if (cdiv == 65536) {
  32708. + cdiv = 0;
  32709. + } else if (cdiv == 1) {
  32710. + cdiv = 2; /* 1 gets rounded down to 0; == 65536 */
  32711. + }
  32712. + } else {
  32713. + cdiv = 0;
  32714. + }
  32715. +
  32716. + switch (bpw) {
  32717. + case 8:
  32718. + break;
  32719. + case 9:
  32720. + /* Reading in LoSSI mode is a special case. See 'BCM2835 ARM Peripherals' datasheet */
  32721. + cs |= SPI_CS_LEN;
  32722. + break;
  32723. + default:
  32724. + dev_dbg(dev, "setup: invalid bits_per_word %u (must be 8 or 9)\n",
  32725. + bpw);
  32726. + return -EINVAL;
  32727. + }
  32728. +
  32729. + if (mode & SPI_CPOL)
  32730. + cs |= SPI_CS_CPOL;
  32731. + if (mode & SPI_CPHA)
  32732. + cs |= SPI_CS_CPHA;
  32733. +
  32734. + if (!(mode & SPI_NO_CS)) {
  32735. + if (mode & SPI_CS_HIGH) {
  32736. + cs |= SPI_CS_CSPOL;
  32737. + cs |= SPI_CS_CSPOL0 << csel;
  32738. + }
  32739. +
  32740. + cs |= csel;
  32741. + } else {
  32742. + cs |= SPI_CS_CS_10 | SPI_CS_CS_01;
  32743. + }
  32744. +
  32745. + if (state) {
  32746. + state->cs = cs;
  32747. + state->cdiv = cdiv;
  32748. + dev_dbg(dev, "setup: want %d Hz; "
  32749. + "bus_hz=%lu / cdiv=%u == %lu Hz; "
  32750. + "mode %u: cs 0x%08X\n",
  32751. + hz, bus_hz, cdiv, bus_hz/cdiv, mode, cs);
  32752. + }
  32753. +
  32754. + return 0;
  32755. +}
  32756. +
  32757. +static int bcm2708_process_transfer(struct bcm2708_spi *bs,
  32758. + struct spi_message *msg, struct spi_transfer *xfer)
  32759. +{
  32760. + struct spi_device *spi = msg->spi;
  32761. + struct bcm2708_spi_state state, *stp;
  32762. + int ret;
  32763. + u32 cs;
  32764. +
  32765. + if (bs->stopping)
  32766. + return -ESHUTDOWN;
  32767. +
  32768. + if (xfer->bits_per_word || xfer->speed_hz) {
  32769. + ret = bcm2708_setup_state(spi->master, &spi->dev, &state,
  32770. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  32771. + spi->chip_select, spi->mode,
  32772. + xfer->bits_per_word ? xfer->bits_per_word :
  32773. + spi->bits_per_word);
  32774. + if (ret)
  32775. + return ret;
  32776. +
  32777. + stp = &state;
  32778. + } else {
  32779. + stp = spi->controller_state;
  32780. + }
  32781. +
  32782. + reinit_completion(&bs->done);
  32783. + bs->tx_buf = xfer->tx_buf;
  32784. + bs->rx_buf = xfer->rx_buf;
  32785. + bs->len = xfer->len;
  32786. +
  32787. + cs = stp->cs | SPI_CS_INTR | SPI_CS_INTD | SPI_CS_TA;
  32788. +
  32789. + bcm2708_wr(bs, SPI_CLK, stp->cdiv);
  32790. + bcm2708_wr(bs, SPI_CS, cs);
  32791. +
  32792. + ret = wait_for_completion_timeout(&bs->done,
  32793. + msecs_to_jiffies(SPI_TIMEOUT_MS));
  32794. + if (ret == 0) {
  32795. + dev_err(&spi->dev, "transfer timed out\n");
  32796. + return -ETIMEDOUT;
  32797. + }
  32798. +
  32799. + if (xfer->delay_usecs)
  32800. + udelay(xfer->delay_usecs);
  32801. +
  32802. + if (list_is_last(&xfer->transfer_list, &msg->transfers) ||
  32803. + xfer->cs_change) {
  32804. + /* clear TA and interrupt flags */
  32805. + bcm2708_wr(bs, SPI_CS, stp->cs);
  32806. + }
  32807. +
  32808. + msg->actual_length += (xfer->len - bs->len);
  32809. +
  32810. + return 0;
  32811. +}
  32812. +
  32813. +static void bcm2708_work(struct work_struct *work)
  32814. +{
  32815. + struct bcm2708_spi *bs = container_of(work, struct bcm2708_spi, work);
  32816. + unsigned long flags;
  32817. + struct spi_message *msg;
  32818. + struct spi_transfer *xfer;
  32819. + int status = 0;
  32820. +
  32821. + spin_lock_irqsave(&bs->lock, flags);
  32822. + while (!list_empty(&bs->queue)) {
  32823. + msg = list_first_entry(&bs->queue, struct spi_message, queue);
  32824. + list_del_init(&msg->queue);
  32825. + spin_unlock_irqrestore(&bs->lock, flags);
  32826. +
  32827. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  32828. + status = bcm2708_process_transfer(bs, msg, xfer);
  32829. + if (status)
  32830. + break;
  32831. + }
  32832. +
  32833. + msg->status = status;
  32834. + msg->complete(msg->context);
  32835. +
  32836. + spin_lock_irqsave(&bs->lock, flags);
  32837. + }
  32838. + spin_unlock_irqrestore(&bs->lock, flags);
  32839. +}
  32840. +
  32841. +static int bcm2708_spi_setup(struct spi_device *spi)
  32842. +{
  32843. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  32844. + struct bcm2708_spi_state *state;
  32845. + int ret;
  32846. +
  32847. + if (bs->stopping)
  32848. + return -ESHUTDOWN;
  32849. +
  32850. + if (!(spi->mode & SPI_NO_CS) &&
  32851. + (spi->chip_select > spi->master->num_chipselect)) {
  32852. + dev_dbg(&spi->dev,
  32853. + "setup: invalid chipselect %u (%u defined)\n",
  32854. + spi->chip_select, spi->master->num_chipselect);
  32855. + return -EINVAL;
  32856. + }
  32857. +
  32858. + state = spi->controller_state;
  32859. + if (!state) {
  32860. + state = kzalloc(sizeof(*state), GFP_KERNEL);
  32861. + if (!state)
  32862. + return -ENOMEM;
  32863. +
  32864. + spi->controller_state = state;
  32865. + }
  32866. +
  32867. + ret = bcm2708_setup_state(spi->master, &spi->dev, state,
  32868. + spi->max_speed_hz, spi->chip_select, spi->mode,
  32869. + spi->bits_per_word);
  32870. + if (ret < 0) {
  32871. + kfree(state);
  32872. + spi->controller_state = NULL;
  32873. + return ret;
  32874. + }
  32875. +
  32876. + dev_dbg(&spi->dev,
  32877. + "setup: cd %d: %d Hz, bpw %u, mode 0x%x -> CS=%08x CDIV=%04x\n",
  32878. + spi->chip_select, spi->max_speed_hz, spi->bits_per_word,
  32879. + spi->mode, state->cs, state->cdiv);
  32880. +
  32881. + return 0;
  32882. +}
  32883. +
  32884. +static int bcm2708_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  32885. +{
  32886. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  32887. + struct spi_transfer *xfer;
  32888. + int ret;
  32889. + unsigned long flags;
  32890. +
  32891. + if (unlikely(list_empty(&msg->transfers)))
  32892. + return -EINVAL;
  32893. +
  32894. + if (bs->stopping)
  32895. + return -ESHUTDOWN;
  32896. +
  32897. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  32898. + if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  32899. + dev_dbg(&spi->dev, "missing rx or tx buf\n");
  32900. + return -EINVAL;
  32901. + }
  32902. +
  32903. + if (!xfer->bits_per_word || xfer->speed_hz)
  32904. + continue;
  32905. +
  32906. + ret = bcm2708_setup_state(spi->master, &spi->dev, NULL,
  32907. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  32908. + spi->chip_select, spi->mode,
  32909. + xfer->bits_per_word ? xfer->bits_per_word :
  32910. + spi->bits_per_word);
  32911. + if (ret)
  32912. + return ret;
  32913. + }
  32914. +
  32915. + msg->status = -EINPROGRESS;
  32916. + msg->actual_length = 0;
  32917. +
  32918. + spin_lock_irqsave(&bs->lock, flags);
  32919. + list_add_tail(&msg->queue, &bs->queue);
  32920. + queue_work(bs->workq, &bs->work);
  32921. + spin_unlock_irqrestore(&bs->lock, flags);
  32922. +
  32923. + return 0;
  32924. +}
  32925. +
  32926. +static void bcm2708_spi_cleanup(struct spi_device *spi)
  32927. +{
  32928. + if (spi->controller_state) {
  32929. + kfree(spi->controller_state);
  32930. + spi->controller_state = NULL;
  32931. + }
  32932. +}
  32933. +
  32934. +static int bcm2708_spi_probe(struct platform_device *pdev)
  32935. +{
  32936. + struct resource *regs;
  32937. + int irq, err = -ENOMEM;
  32938. + struct clk *clk;
  32939. + struct spi_master *master;
  32940. + struct bcm2708_spi *bs;
  32941. +
  32942. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  32943. + if (!regs) {
  32944. + dev_err(&pdev->dev, "could not get IO memory\n");
  32945. + return -ENXIO;
  32946. + }
  32947. +
  32948. + irq = platform_get_irq(pdev, 0);
  32949. + if (irq < 0) {
  32950. + dev_err(&pdev->dev, "could not get IRQ\n");
  32951. + return irq;
  32952. + }
  32953. +
  32954. + clk = clk_get(&pdev->dev, NULL);
  32955. + if (IS_ERR(clk)) {
  32956. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  32957. + return PTR_ERR(clk);
  32958. + }
  32959. +
  32960. + bcm2708_init_pinmode();
  32961. +
  32962. + master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  32963. + if (!master) {
  32964. + dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  32965. + goto out_clk_put;
  32966. + }
  32967. +
  32968. + /* the spi->mode bits understood by this driver: */
  32969. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
  32970. +
  32971. + master->bus_num = pdev->id;
  32972. + master->num_chipselect = 3;
  32973. + master->setup = bcm2708_spi_setup;
  32974. + master->transfer = bcm2708_spi_transfer;
  32975. + master->cleanup = bcm2708_spi_cleanup;
  32976. + platform_set_drvdata(pdev, master);
  32977. +
  32978. + bs = spi_master_get_devdata(master);
  32979. +
  32980. + spin_lock_init(&bs->lock);
  32981. + INIT_LIST_HEAD(&bs->queue);
  32982. + init_completion(&bs->done);
  32983. + INIT_WORK(&bs->work, bcm2708_work);
  32984. +
  32985. + bs->base = ioremap(regs->start, resource_size(regs));
  32986. + if (!bs->base) {
  32987. + dev_err(&pdev->dev, "could not remap memory\n");
  32988. + goto out_master_put;
  32989. + }
  32990. +
  32991. + bs->workq = create_singlethread_workqueue(dev_name(&pdev->dev));
  32992. + if (!bs->workq) {
  32993. + dev_err(&pdev->dev, "could not create workqueue\n");
  32994. + goto out_iounmap;
  32995. + }
  32996. +
  32997. + bs->irq = irq;
  32998. + bs->clk = clk;
  32999. + bs->stopping = false;
  33000. +
  33001. + err = request_irq(irq, bcm2708_spi_interrupt, 0, dev_name(&pdev->dev),
  33002. + master);
  33003. + if (err) {
  33004. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  33005. + goto out_workqueue;
  33006. + }
  33007. +
  33008. + /* initialise the hardware */
  33009. + clk_enable(clk);
  33010. + bcm2708_wr(bs, SPI_CS, SPI_CS_REN | SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  33011. +
  33012. + err = spi_register_master(master);
  33013. + if (err) {
  33014. + dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  33015. + goto out_free_irq;
  33016. + }
  33017. +
  33018. + dev_info(&pdev->dev, "SPI Controller at 0x%08lx (irq %d)\n",
  33019. + (unsigned long)regs->start, irq);
  33020. +
  33021. + return 0;
  33022. +
  33023. +out_free_irq:
  33024. + free_irq(bs->irq, master);
  33025. +out_workqueue:
  33026. + destroy_workqueue(bs->workq);
  33027. +out_iounmap:
  33028. + iounmap(bs->base);
  33029. +out_master_put:
  33030. + spi_master_put(master);
  33031. +out_clk_put:
  33032. + clk_put(clk);
  33033. + return err;
  33034. +}
  33035. +
  33036. +static int bcm2708_spi_remove(struct platform_device *pdev)
  33037. +{
  33038. + struct spi_master *master = platform_get_drvdata(pdev);
  33039. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  33040. +
  33041. + /* reset the hardware and block queue progress */
  33042. + spin_lock_irq(&bs->lock);
  33043. + bs->stopping = true;
  33044. + bcm2708_wr(bs, SPI_CS, SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  33045. + spin_unlock_irq(&bs->lock);
  33046. +
  33047. + flush_work_sync(&bs->work);
  33048. +
  33049. + clk_disable(bs->clk);
  33050. + clk_put(bs->clk);
  33051. + free_irq(bs->irq, master);
  33052. + iounmap(bs->base);
  33053. +
  33054. + spi_unregister_master(master);
  33055. +
  33056. + return 0;
  33057. +}
  33058. +
  33059. +static struct platform_driver bcm2708_spi_driver = {
  33060. + .driver = {
  33061. + .name = DRV_NAME,
  33062. + .owner = THIS_MODULE,
  33063. + },
  33064. + .probe = bcm2708_spi_probe,
  33065. + .remove = bcm2708_spi_remove,
  33066. +};
  33067. +
  33068. +
  33069. +static int __init bcm2708_spi_init(void)
  33070. +{
  33071. + return platform_driver_probe(&bcm2708_spi_driver, bcm2708_spi_probe);
  33072. +}
  33073. +module_init(bcm2708_spi_init);
  33074. +
  33075. +static void __exit bcm2708_spi_exit(void)
  33076. +{
  33077. + platform_driver_unregister(&bcm2708_spi_driver);
  33078. +}
  33079. +module_exit(bcm2708_spi_exit);
  33080. +
  33081. +
  33082. +//module_platform_driver(bcm2708_spi_driver);
  33083. +
  33084. +MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2708");
  33085. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  33086. +MODULE_LICENSE("GPL v2");
  33087. +MODULE_ALIAS("platform:" DRV_NAME);
  33088. diff -Nur linux-3.13.6/drivers/staging/media/lirc/Kconfig linux-raspberry-pi/drivers/staging/media/lirc/Kconfig
  33089. --- linux-3.13.6/drivers/staging/media/lirc/Kconfig 2014-03-07 07:07:02.000000000 +0100
  33090. +++ linux-raspberry-pi/drivers/staging/media/lirc/Kconfig 2014-03-11 16:53:00.000000000 +0100
  33091. @@ -38,6 +38,12 @@
  33092. help
  33093. Driver for Homebrew Parallel Port Receivers
  33094. +config LIRC_RPI
  33095. + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
  33096. + depends on LIRC
  33097. + help
  33098. + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
  33099. +
  33100. config LIRC_SASEM
  33101. tristate "Sasem USB IR Remote"
  33102. depends on LIRC && USB
  33103. diff -Nur linux-3.13.6/drivers/staging/media/lirc/lirc_rpi.c linux-raspberry-pi/drivers/staging/media/lirc/lirc_rpi.c
  33104. --- linux-3.13.6/drivers/staging/media/lirc/lirc_rpi.c 1970-01-01 01:00:00.000000000 +0100
  33105. +++ linux-raspberry-pi/drivers/staging/media/lirc/lirc_rpi.c 2014-03-11 16:53:00.000000000 +0100
  33106. @@ -0,0 +1,693 @@
  33107. +/*
  33108. + * lirc_rpi.c
  33109. + *
  33110. + * lirc_rpi - Device driver that records pulse- and pause-lengths
  33111. + * (space-lengths) (just like the lirc_serial driver does)
  33112. + * between GPIO interrupt events on the Raspberry Pi.
  33113. + * Lots of code has been taken from the lirc_serial module,
  33114. + * so I would like say thanks to the authors.
  33115. + *
  33116. + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
  33117. + * Michael Bishop <cleverca22@gmail.com>
  33118. + * This program is free software; you can redistribute it and/or modify
  33119. + * it under the terms of the GNU General Public License as published by
  33120. + * the Free Software Foundation; either version 2 of the License, or
  33121. + * (at your option) any later version.
  33122. + *
  33123. + * This program is distributed in the hope that it will be useful,
  33124. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33125. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33126. + * GNU General Public License for more details.
  33127. + *
  33128. + * You should have received a copy of the GNU General Public License
  33129. + * along with this program; if not, write to the Free Software
  33130. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  33131. + */
  33132. +
  33133. +#include <linux/module.h>
  33134. +#include <linux/errno.h>
  33135. +#include <linux/interrupt.h>
  33136. +#include <linux/sched.h>
  33137. +#include <linux/kernel.h>
  33138. +#include <linux/time.h>
  33139. +#include <linux/string.h>
  33140. +#include <linux/delay.h>
  33141. +#include <linux/platform_device.h>
  33142. +#include <linux/irq.h>
  33143. +#include <linux/spinlock.h>
  33144. +#include <media/lirc.h>
  33145. +#include <media/lirc_dev.h>
  33146. +#include <linux/gpio.h>
  33147. +
  33148. +#define LIRC_DRIVER_NAME "lirc_rpi"
  33149. +#define RBUF_LEN 256
  33150. +#define LIRC_TRANSMITTER_LATENCY 256
  33151. +
  33152. +#ifndef MAX_UDELAY_MS
  33153. +#define MAX_UDELAY_US 5000
  33154. +#else
  33155. +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
  33156. +#endif
  33157. +
  33158. +#define dprintk(fmt, args...) \
  33159. + do { \
  33160. + if (debug) \
  33161. + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
  33162. + fmt, ## args); \
  33163. + } while (0)
  33164. +
  33165. +/* module parameters */
  33166. +
  33167. +/* set the default GPIO input pin */
  33168. +static int gpio_in_pin = 18;
  33169. +/* set the default GPIO output pin */
  33170. +static int gpio_out_pin = 17;
  33171. +/* enable debugging messages */
  33172. +static bool debug;
  33173. +/* -1 = auto, 0 = active high, 1 = active low */
  33174. +static int sense = -1;
  33175. +/* use softcarrier by default */
  33176. +static bool softcarrier = 1;
  33177. +/* 0 = do not invert output, 1 = invert output */
  33178. +static bool invert = 0;
  33179. +
  33180. +struct gpio_chip *gpiochip;
  33181. +struct irq_chip *irqchip;
  33182. +struct irq_data *irqdata;
  33183. +
  33184. +/* forward declarations */
  33185. +static long send_pulse(unsigned long length);
  33186. +static void send_space(long length);
  33187. +static void lirc_rpi_exit(void);
  33188. +
  33189. +int valid_gpio_pins[] = { 0, 1, 4, 8, 7, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23,
  33190. + 24, 25 };
  33191. +
  33192. +static struct platform_device *lirc_rpi_dev;
  33193. +static struct timeval lasttv = { 0, 0 };
  33194. +static struct lirc_buffer rbuf;
  33195. +static spinlock_t lock;
  33196. +
  33197. +/* initialized/set in init_timing_params() */
  33198. +static unsigned int freq = 38000;
  33199. +static unsigned int duty_cycle = 50;
  33200. +static unsigned long period;
  33201. +static unsigned long pulse_width;
  33202. +static unsigned long space_width;
  33203. +
  33204. +static void safe_udelay(unsigned long usecs)
  33205. +{
  33206. + while (usecs > MAX_UDELAY_US) {
  33207. + udelay(MAX_UDELAY_US);
  33208. + usecs -= MAX_UDELAY_US;
  33209. + }
  33210. + udelay(usecs);
  33211. +}
  33212. +
  33213. +static int init_timing_params(unsigned int new_duty_cycle,
  33214. + unsigned int new_freq)
  33215. +{
  33216. + /*
  33217. + * period, pulse/space width are kept with 8 binary places -
  33218. + * IE multiplied by 256.
  33219. + */
  33220. + if (256 * 1000000L / new_freq * new_duty_cycle / 100 <=
  33221. + LIRC_TRANSMITTER_LATENCY)
  33222. + return -EINVAL;
  33223. + if (256 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
  33224. + LIRC_TRANSMITTER_LATENCY)
  33225. + return -EINVAL;
  33226. + duty_cycle = new_duty_cycle;
  33227. + freq = new_freq;
  33228. + period = 256 * 1000000L / freq;
  33229. + pulse_width = period * duty_cycle / 100;
  33230. + space_width = period - pulse_width;
  33231. + dprintk("in init_timing_params, freq=%d pulse=%ld, "
  33232. + "space=%ld\n", freq, pulse_width, space_width);
  33233. + return 0;
  33234. +}
  33235. +
  33236. +static long send_pulse_softcarrier(unsigned long length)
  33237. +{
  33238. + int flag;
  33239. + unsigned long actual, target, d;
  33240. +
  33241. + length <<= 8;
  33242. +
  33243. + actual = 0; target = 0; flag = 0;
  33244. + while (actual < length) {
  33245. + if (flag) {
  33246. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  33247. + target += space_width;
  33248. + } else {
  33249. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  33250. + target += pulse_width;
  33251. + }
  33252. + d = (target - actual -
  33253. + LIRC_TRANSMITTER_LATENCY + 128) >> 8;
  33254. + /*
  33255. + * Note - we've checked in ioctl that the pulse/space
  33256. + * widths are big enough so that d is > 0
  33257. + */
  33258. + udelay(d);
  33259. + actual += (d << 8) + LIRC_TRANSMITTER_LATENCY;
  33260. + flag = !flag;
  33261. + }
  33262. + return (actual-length) >> 8;
  33263. +}
  33264. +
  33265. +static long send_pulse(unsigned long length)
  33266. +{
  33267. + if (length <= 0)
  33268. + return 0;
  33269. +
  33270. + if (softcarrier) {
  33271. + return send_pulse_softcarrier(length);
  33272. + } else {
  33273. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  33274. + safe_udelay(length);
  33275. + return 0;
  33276. + }
  33277. +}
  33278. +
  33279. +static void send_space(long length)
  33280. +{
  33281. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  33282. + if (length <= 0)
  33283. + return;
  33284. + safe_udelay(length);
  33285. +}
  33286. +
  33287. +static void rbwrite(int l)
  33288. +{
  33289. + if (lirc_buffer_full(&rbuf)) {
  33290. + /* no new signals will be accepted */
  33291. + dprintk("Buffer overrun\n");
  33292. + return;
  33293. + }
  33294. + lirc_buffer_write(&rbuf, (void *)&l);
  33295. +}
  33296. +
  33297. +static void frbwrite(int l)
  33298. +{
  33299. + /* simple noise filter */
  33300. + static int pulse, space;
  33301. + static unsigned int ptr;
  33302. +
  33303. + if (ptr > 0 && (l & PULSE_BIT)) {
  33304. + pulse += l & PULSE_MASK;
  33305. + if (pulse > 250) {
  33306. + rbwrite(space);
  33307. + rbwrite(pulse | PULSE_BIT);
  33308. + ptr = 0;
  33309. + pulse = 0;
  33310. + }
  33311. + return;
  33312. + }
  33313. + if (!(l & PULSE_BIT)) {
  33314. + if (ptr == 0) {
  33315. + if (l > 20000) {
  33316. + space = l;
  33317. + ptr++;
  33318. + return;
  33319. + }
  33320. + } else {
  33321. + if (l > 20000) {
  33322. + space += pulse;
  33323. + if (space > PULSE_MASK)
  33324. + space = PULSE_MASK;
  33325. + space += l;
  33326. + if (space > PULSE_MASK)
  33327. + space = PULSE_MASK;
  33328. + pulse = 0;
  33329. + return;
  33330. + }
  33331. + rbwrite(space);
  33332. + rbwrite(pulse | PULSE_BIT);
  33333. + ptr = 0;
  33334. + pulse = 0;
  33335. + }
  33336. + }
  33337. + rbwrite(l);
  33338. +}
  33339. +
  33340. +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
  33341. +{
  33342. + struct timeval tv;
  33343. + long deltv;
  33344. + int data;
  33345. + int signal;
  33346. +
  33347. + /* use the GPIO signal level */
  33348. + signal = gpiochip->get(gpiochip, gpio_in_pin);
  33349. +
  33350. + /* unmask the irq */
  33351. + irqchip->irq_unmask(irqdata);
  33352. +
  33353. + if (sense != -1) {
  33354. + /* get current time */
  33355. + do_gettimeofday(&tv);
  33356. +
  33357. + /* calc time since last interrupt in microseconds */
  33358. + deltv = tv.tv_sec-lasttv.tv_sec;
  33359. + if (tv.tv_sec < lasttv.tv_sec ||
  33360. + (tv.tv_sec == lasttv.tv_sec &&
  33361. + tv.tv_usec < lasttv.tv_usec)) {
  33362. + printk(KERN_WARNING LIRC_DRIVER_NAME
  33363. + ": AIEEEE: your clock just jumped backwards\n");
  33364. + printk(KERN_WARNING LIRC_DRIVER_NAME
  33365. + ": %d %d %lx %lx %lx %lx\n", signal, sense,
  33366. + tv.tv_sec, lasttv.tv_sec,
  33367. + tv.tv_usec, lasttv.tv_usec);
  33368. + data = PULSE_MASK;
  33369. + } else if (deltv > 15) {
  33370. + data = PULSE_MASK; /* really long time */
  33371. + if (!(signal^sense)) {
  33372. + /* sanity check */
  33373. + printk(KERN_WARNING LIRC_DRIVER_NAME
  33374. + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
  33375. + signal, sense, tv.tv_sec, lasttv.tv_sec,
  33376. + tv.tv_usec, lasttv.tv_usec);
  33377. + /*
  33378. + * detecting pulse while this
  33379. + * MUST be a space!
  33380. + */
  33381. + sense = sense ? 0 : 1;
  33382. + }
  33383. + } else {
  33384. + data = (int) (deltv*1000000 +
  33385. + (tv.tv_usec - lasttv.tv_usec));
  33386. + }
  33387. + frbwrite(signal^sense ? data : (data|PULSE_BIT));
  33388. + lasttv = tv;
  33389. + wake_up_interruptible(&rbuf.wait_poll);
  33390. + }
  33391. +
  33392. + return IRQ_HANDLED;
  33393. +}
  33394. +
  33395. +static int is_right_chip(struct gpio_chip *chip, void *data)
  33396. +{
  33397. + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
  33398. +
  33399. + if (strcmp(data, chip->label) == 0)
  33400. + return 1;
  33401. + return 0;
  33402. +}
  33403. +
  33404. +static int init_port(void)
  33405. +{
  33406. + int i, nlow, nhigh, ret, irq;
  33407. +
  33408. + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
  33409. +
  33410. + if (!gpiochip)
  33411. + return -ENODEV;
  33412. +
  33413. + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
  33414. + printk(KERN_ALERT LIRC_DRIVER_NAME
  33415. + ": cant claim gpio pin %d\n", gpio_out_pin);
  33416. + ret = -ENODEV;
  33417. + goto exit_init_port;
  33418. + }
  33419. +
  33420. + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
  33421. + printk(KERN_ALERT LIRC_DRIVER_NAME
  33422. + ": cant claim gpio pin %d\n", gpio_in_pin);
  33423. + ret = -ENODEV;
  33424. + goto exit_gpio_free_out_pin;
  33425. + }
  33426. +
  33427. + gpiochip->direction_input(gpiochip, gpio_in_pin);
  33428. + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
  33429. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  33430. +
  33431. + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
  33432. + dprintk("to_irq %d\n", irq);
  33433. + irqdata = irq_get_irq_data(irq);
  33434. +
  33435. + if (irqdata && irqdata->chip) {
  33436. + irqchip = irqdata->chip;
  33437. + } else {
  33438. + ret = -ENODEV;
  33439. + goto exit_gpio_free_in_pin;
  33440. + }
  33441. +
  33442. + /* if pin is high, then this must be an active low receiver. */
  33443. + if (sense == -1) {
  33444. + /* wait 1/2 sec for the power supply */
  33445. + msleep(500);
  33446. +
  33447. + /*
  33448. + * probe 9 times every 0.04s, collect "votes" for
  33449. + * active high/low
  33450. + */
  33451. + nlow = 0;
  33452. + nhigh = 0;
  33453. + for (i = 0; i < 9; i++) {
  33454. + if (gpiochip->get(gpiochip, gpio_in_pin))
  33455. + nlow++;
  33456. + else
  33457. + nhigh++;
  33458. + msleep(40);
  33459. + }
  33460. + sense = (nlow >= nhigh ? 1 : 0);
  33461. + printk(KERN_INFO LIRC_DRIVER_NAME
  33462. + ": auto-detected active %s receiver on GPIO pin %d\n",
  33463. + sense ? "low" : "high", gpio_in_pin);
  33464. + } else {
  33465. + printk(KERN_INFO LIRC_DRIVER_NAME
  33466. + ": manually using active %s receiver on GPIO pin %d\n",
  33467. + sense ? "low" : "high", gpio_in_pin);
  33468. + }
  33469. +
  33470. + return 0;
  33471. +
  33472. + exit_gpio_free_in_pin:
  33473. + gpio_free(gpio_in_pin);
  33474. +
  33475. + exit_gpio_free_out_pin:
  33476. + gpio_free(gpio_out_pin);
  33477. +
  33478. + exit_init_port:
  33479. + return ret;
  33480. +}
  33481. +
  33482. +// called when the character device is opened
  33483. +static int set_use_inc(void *data)
  33484. +{
  33485. + int result;
  33486. + unsigned long flags;
  33487. +
  33488. + /* initialize timestamp */
  33489. + do_gettimeofday(&lasttv);
  33490. +
  33491. + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
  33492. + (irq_handler_t) irq_handler, 0,
  33493. + LIRC_DRIVER_NAME, (void*) 0);
  33494. +
  33495. + switch (result) {
  33496. + case -EBUSY:
  33497. + printk(KERN_ERR LIRC_DRIVER_NAME
  33498. + ": IRQ %d is busy\n",
  33499. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  33500. + return -EBUSY;
  33501. + case -EINVAL:
  33502. + printk(KERN_ERR LIRC_DRIVER_NAME
  33503. + ": Bad irq number or handler\n");
  33504. + return -EINVAL;
  33505. + default:
  33506. + dprintk("Interrupt %d obtained\n",
  33507. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  33508. + break;
  33509. + };
  33510. +
  33511. + /* initialize pulse/space widths */
  33512. + init_timing_params(duty_cycle, freq);
  33513. +
  33514. + spin_lock_irqsave(&lock, flags);
  33515. +
  33516. + /* GPIO Pin Falling/Rising Edge Detect Enable */
  33517. + irqchip->irq_set_type(irqdata,
  33518. + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
  33519. +
  33520. + /* unmask the irq */
  33521. + irqchip->irq_unmask(irqdata);
  33522. +
  33523. + spin_unlock_irqrestore(&lock, flags);
  33524. +
  33525. + return 0;
  33526. +}
  33527. +
  33528. +static void set_use_dec(void *data)
  33529. +{
  33530. + unsigned long flags;
  33531. +
  33532. + spin_lock_irqsave(&lock, flags);
  33533. +
  33534. + /* GPIO Pin Falling/Rising Edge Detect Disable */
  33535. + irqchip->irq_set_type(irqdata, 0);
  33536. + irqchip->irq_mask(irqdata);
  33537. +
  33538. + spin_unlock_irqrestore(&lock, flags);
  33539. +
  33540. + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
  33541. +
  33542. + dprintk(KERN_INFO LIRC_DRIVER_NAME
  33543. + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
  33544. +}
  33545. +
  33546. +static ssize_t lirc_write(struct file *file, const char *buf,
  33547. + size_t n, loff_t *ppos)
  33548. +{
  33549. + int i, count;
  33550. + unsigned long flags;
  33551. + long delta = 0;
  33552. + int *wbuf;
  33553. +
  33554. + count = n / sizeof(int);
  33555. + if (n % sizeof(int) || count % 2 == 0)
  33556. + return -EINVAL;
  33557. + wbuf = memdup_user(buf, n);
  33558. + if (IS_ERR(wbuf))
  33559. + return PTR_ERR(wbuf);
  33560. + spin_lock_irqsave(&lock, flags);
  33561. +
  33562. + for (i = 0; i < count; i++) {
  33563. + if (i%2)
  33564. + send_space(wbuf[i] - delta);
  33565. + else
  33566. + delta = send_pulse(wbuf[i]);
  33567. + }
  33568. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  33569. +
  33570. + spin_unlock_irqrestore(&lock, flags);
  33571. + kfree(wbuf);
  33572. + return n;
  33573. +}
  33574. +
  33575. +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  33576. +{
  33577. + int result;
  33578. + __u32 value;
  33579. +
  33580. + switch (cmd) {
  33581. + case LIRC_GET_SEND_MODE:
  33582. + return -ENOIOCTLCMD;
  33583. + break;
  33584. +
  33585. + case LIRC_SET_SEND_MODE:
  33586. + result = get_user(value, (__u32 *) arg);
  33587. + if (result)
  33588. + return result;
  33589. + /* only LIRC_MODE_PULSE supported */
  33590. + if (value != LIRC_MODE_PULSE)
  33591. + return -ENOSYS;
  33592. + break;
  33593. +
  33594. + case LIRC_GET_LENGTH:
  33595. + return -ENOSYS;
  33596. + break;
  33597. +
  33598. + case LIRC_SET_SEND_DUTY_CYCLE:
  33599. + dprintk("SET_SEND_DUTY_CYCLE\n");
  33600. + result = get_user(value, (__u32 *) arg);
  33601. + if (result)
  33602. + return result;
  33603. + if (value <= 0 || value > 100)
  33604. + return -EINVAL;
  33605. + return init_timing_params(value, freq);
  33606. + break;
  33607. +
  33608. + case LIRC_SET_SEND_CARRIER:
  33609. + dprintk("SET_SEND_CARRIER\n");
  33610. + result = get_user(value, (__u32 *) arg);
  33611. + if (result)
  33612. + return result;
  33613. + if (value > 500000 || value < 20000)
  33614. + return -EINVAL;
  33615. + return init_timing_params(duty_cycle, value);
  33616. + break;
  33617. +
  33618. + default:
  33619. + return lirc_dev_fop_ioctl(filep, cmd, arg);
  33620. + }
  33621. + return 0;
  33622. +}
  33623. +
  33624. +static const struct file_operations lirc_fops = {
  33625. + .owner = THIS_MODULE,
  33626. + .write = lirc_write,
  33627. + .unlocked_ioctl = lirc_ioctl,
  33628. + .read = lirc_dev_fop_read,
  33629. + .poll = lirc_dev_fop_poll,
  33630. + .open = lirc_dev_fop_open,
  33631. + .release = lirc_dev_fop_close,
  33632. + .llseek = no_llseek,
  33633. +};
  33634. +
  33635. +static struct lirc_driver driver = {
  33636. + .name = LIRC_DRIVER_NAME,
  33637. + .minor = -1,
  33638. + .code_length = 1,
  33639. + .sample_rate = 0,
  33640. + .data = NULL,
  33641. + .add_to_buf = NULL,
  33642. + .rbuf = &rbuf,
  33643. + .set_use_inc = set_use_inc,
  33644. + .set_use_dec = set_use_dec,
  33645. + .fops = &lirc_fops,
  33646. + .dev = NULL,
  33647. + .owner = THIS_MODULE,
  33648. +};
  33649. +
  33650. +static struct platform_driver lirc_rpi_driver = {
  33651. + .driver = {
  33652. + .name = LIRC_DRIVER_NAME,
  33653. + .owner = THIS_MODULE,
  33654. + },
  33655. +};
  33656. +
  33657. +static int __init lirc_rpi_init(void)
  33658. +{
  33659. + int result;
  33660. +
  33661. + /* Init read buffer. */
  33662. + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
  33663. + if (result < 0)
  33664. + return -ENOMEM;
  33665. +
  33666. + result = platform_driver_register(&lirc_rpi_driver);
  33667. + if (result) {
  33668. + printk(KERN_ERR LIRC_DRIVER_NAME
  33669. + ": lirc register returned %d\n", result);
  33670. + goto exit_buffer_free;
  33671. + }
  33672. +
  33673. + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
  33674. + if (!lirc_rpi_dev) {
  33675. + result = -ENOMEM;
  33676. + goto exit_driver_unregister;
  33677. + }
  33678. +
  33679. + result = platform_device_add(lirc_rpi_dev);
  33680. + if (result)
  33681. + goto exit_device_put;
  33682. +
  33683. + return 0;
  33684. +
  33685. + exit_device_put:
  33686. + platform_device_put(lirc_rpi_dev);
  33687. +
  33688. + exit_driver_unregister:
  33689. + platform_driver_unregister(&lirc_rpi_driver);
  33690. +
  33691. + exit_buffer_free:
  33692. + lirc_buffer_free(&rbuf);
  33693. +
  33694. + return result;
  33695. +}
  33696. +
  33697. +static void lirc_rpi_exit(void)
  33698. +{
  33699. + platform_device_unregister(lirc_rpi_dev);
  33700. + platform_driver_unregister(&lirc_rpi_driver);
  33701. + lirc_buffer_free(&rbuf);
  33702. +}
  33703. +
  33704. +static int __init lirc_rpi_init_module(void)
  33705. +{
  33706. + int result, i;
  33707. +
  33708. + result = lirc_rpi_init();
  33709. + if (result)
  33710. + return result;
  33711. +
  33712. + /* check if the module received valid gpio pin numbers */
  33713. + result = 0;
  33714. + if (gpio_in_pin != gpio_out_pin) {
  33715. + for(i = 0; (i < ARRAY_SIZE(valid_gpio_pins)) && (result != 2); i++) {
  33716. + if (gpio_in_pin == valid_gpio_pins[i] ||
  33717. + gpio_out_pin == valid_gpio_pins[i]) {
  33718. + result++;
  33719. + }
  33720. + }
  33721. + }
  33722. +
  33723. + if (result != 2) {
  33724. + result = -EINVAL;
  33725. + printk(KERN_ERR LIRC_DRIVER_NAME
  33726. + ": invalid GPIO pin(s) specified!\n");
  33727. + goto exit_rpi;
  33728. + }
  33729. +
  33730. + result = init_port();
  33731. + if (result < 0)
  33732. + goto exit_rpi;
  33733. +
  33734. + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
  33735. + LIRC_CAN_SET_SEND_CARRIER |
  33736. + LIRC_CAN_SEND_PULSE |
  33737. + LIRC_CAN_REC_MODE2;
  33738. +
  33739. + driver.dev = &lirc_rpi_dev->dev;
  33740. + driver.minor = lirc_register_driver(&driver);
  33741. +
  33742. + if (driver.minor < 0) {
  33743. + printk(KERN_ERR LIRC_DRIVER_NAME
  33744. + ": device registration failed with %d\n", result);
  33745. + result = -EIO;
  33746. + goto exit_rpi;
  33747. + }
  33748. +
  33749. + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
  33750. +
  33751. + return 0;
  33752. +
  33753. + exit_rpi:
  33754. + lirc_rpi_exit();
  33755. +
  33756. + return result;
  33757. +}
  33758. +
  33759. +static void __exit lirc_rpi_exit_module(void)
  33760. +{
  33761. + gpio_free(gpio_out_pin);
  33762. + gpio_free(gpio_in_pin);
  33763. +
  33764. + lirc_rpi_exit();
  33765. +
  33766. + lirc_unregister_driver(driver.minor);
  33767. + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
  33768. +}
  33769. +
  33770. +module_init(lirc_rpi_init_module);
  33771. +module_exit(lirc_rpi_exit_module);
  33772. +
  33773. +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
  33774. +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
  33775. +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
  33776. +MODULE_LICENSE("GPL");
  33777. +
  33778. +module_param(gpio_out_pin, int, S_IRUGO);
  33779. +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
  33780. + " processor. Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11,"
  33781. + " 14, 15, 17, 18, 21, 22, 23, 24, 25, default 17");
  33782. +
  33783. +module_param(gpio_in_pin, int, S_IRUGO);
  33784. +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
  33785. + " Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11, 14, 15,"
  33786. + " 17, 18, 21, 22, 23, 24, 25, default 18");
  33787. +
  33788. +module_param(sense, int, S_IRUGO);
  33789. +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
  33790. + " (0 = active high, 1 = active low )");
  33791. +
  33792. +module_param(softcarrier, bool, S_IRUGO);
  33793. +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
  33794. +
  33795. +module_param(invert, bool, S_IRUGO);
  33796. +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
  33797. +
  33798. +module_param(debug, bool, S_IRUGO | S_IWUSR);
  33799. +MODULE_PARM_DESC(debug, "Enable debugging messages");
  33800. diff -Nur linux-3.13.6/drivers/staging/media/lirc/Makefile linux-raspberry-pi/drivers/staging/media/lirc/Makefile
  33801. --- linux-3.13.6/drivers/staging/media/lirc/Makefile 2014-03-07 07:07:02.000000000 +0100
  33802. +++ linux-raspberry-pi/drivers/staging/media/lirc/Makefile 2014-03-11 16:53:00.000000000 +0100
  33803. @@ -7,6 +7,7 @@
  33804. obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
  33805. obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
  33806. obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
  33807. +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
  33808. obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
  33809. obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
  33810. obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
  33811. diff -Nur linux-3.13.6/drivers/thermal/bcm2835-thermal.c linux-raspberry-pi/drivers/thermal/bcm2835-thermal.c
  33812. --- linux-3.13.6/drivers/thermal/bcm2835-thermal.c 1970-01-01 01:00:00.000000000 +0100
  33813. +++ linux-raspberry-pi/drivers/thermal/bcm2835-thermal.c 2014-03-11 16:53:09.000000000 +0100
  33814. @@ -0,0 +1,184 @@
  33815. +/*****************************************************************************
  33816. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  33817. +*
  33818. +* Unless you and Broadcom execute a separate written software license
  33819. +* agreement governing use of this software, this software is licensed to you
  33820. +* under the terms of the GNU General Public License version 2, available at
  33821. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  33822. +*
  33823. +* Notwithstanding the above, under no circumstances may you combine this
  33824. +* software in any way with any other Broadcom software provided under a
  33825. +* license other than the GPL, without Broadcom's express prior written
  33826. +* consent.
  33827. +*****************************************************************************/
  33828. +
  33829. +#include <linux/kernel.h>
  33830. +#include <linux/module.h>
  33831. +#include <linux/init.h>
  33832. +#include <linux/platform_device.h>
  33833. +#include <linux/slab.h>
  33834. +#include <linux/sysfs.h>
  33835. +#include <mach/vcio.h>
  33836. +#include <linux/thermal.h>
  33837. +
  33838. +
  33839. +/* --- DEFINITIONS --- */
  33840. +#define MODULE_NAME "bcm2835_thermal"
  33841. +
  33842. +/*#define THERMAL_DEBUG_ENABLE*/
  33843. +
  33844. +#ifdef THERMAL_DEBUG_ENABLE
  33845. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  33846. +#else
  33847. +#define print_debug(fmt,...)
  33848. +#endif
  33849. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  33850. +
  33851. +#define VC_TAG_GET_TEMP 0x00030006
  33852. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  33853. +
  33854. +typedef enum {
  33855. + TEMP,
  33856. + MAX_TEMP,
  33857. +} temp_type;
  33858. +
  33859. +/* --- STRUCTS --- */
  33860. +/* tag part of the message */
  33861. +struct vc_msg_tag {
  33862. + uint32_t tag_id; /* the tag ID for the temperature */
  33863. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  33864. + uint32_t request_code; /* identifies message as a request (should be 0) */
  33865. + uint32_t id; /* extra ID field (should be 0) */
  33866. + uint32_t val; /* returned value of the temperature */
  33867. +};
  33868. +
  33869. +/* message structure to be sent to videocore */
  33870. +struct vc_msg {
  33871. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  33872. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  33873. + struct vc_msg_tag tag; /* the tag structure above to make */
  33874. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  33875. +};
  33876. +
  33877. +struct bcm2835_thermal_data {
  33878. + struct thermal_zone_device *thermal_dev;
  33879. + struct vc_msg msg;
  33880. +};
  33881. +
  33882. +/* --- GLOBALS --- */
  33883. +static struct bcm2835_thermal_data bcm2835_data;
  33884. +
  33885. +/* Thermal Device Operations */
  33886. +static struct thermal_zone_device_ops ops;
  33887. +
  33888. +/* --- FUNCTIONS --- */
  33889. +
  33890. +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
  33891. +{
  33892. + int result = -1, retry = 3;
  33893. + print_debug("IN");
  33894. +
  33895. + *temp = 0;
  33896. + while (result != 0 && retry-- > 0) {
  33897. + /* wipe all previous message data */
  33898. + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
  33899. +
  33900. + /* prepare message */
  33901. + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
  33902. + bcm2835_data.msg.tag.buffer_size = 8;
  33903. + bcm2835_data.msg.tag.tag_id = tag_id;
  33904. +
  33905. + /* send the message */
  33906. + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
  33907. + 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);
  33908. + if (!(bcm2835_data.msg.request_code & 0x80000000))
  33909. + result = -1;
  33910. + }
  33911. +
  33912. + /* check if it was all ok and return the rate in milli degrees C */
  33913. + if (result == 0)
  33914. + *temp = (uint)bcm2835_data.msg.tag.val;
  33915. + else
  33916. + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
  33917. + print_debug("OUT");
  33918. + return result;
  33919. +}
  33920. +
  33921. +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
  33922. +{
  33923. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
  33924. +}
  33925. +
  33926. +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
  33927. +{
  33928. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
  33929. +}
  33930. +
  33931. +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
  33932. +{
  33933. + *trip_type = THERMAL_TRIP_HOT;
  33934. + return 0;
  33935. +}
  33936. +
  33937. +
  33938. +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
  33939. +{
  33940. + *dev_mode = THERMAL_DEVICE_ENABLED;
  33941. + return 0;
  33942. +}
  33943. +
  33944. +
  33945. +static int bcm2835_thermal_probe(struct platform_device *pdev)
  33946. +{
  33947. + print_debug("IN");
  33948. + print_debug("THERMAL Driver has been probed!");
  33949. +
  33950. + /* check that the device isn't null!*/
  33951. + if(pdev == NULL)
  33952. + {
  33953. + print_debug("Platform device is empty!");
  33954. + return -ENODEV;
  33955. + }
  33956. +
  33957. + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
  33958. + {
  33959. + print_debug("Unable to register the thermal device!");
  33960. + return -EFAULT;
  33961. + }
  33962. + return 0;
  33963. +}
  33964. +
  33965. +
  33966. +static int bcm2835_thermal_remove(struct platform_device *pdev)
  33967. +{
  33968. + print_debug("IN");
  33969. +
  33970. + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
  33971. +
  33972. + print_debug("OUT");
  33973. +
  33974. + return 0;
  33975. +}
  33976. +
  33977. +static struct thermal_zone_device_ops ops = {
  33978. + .get_temp = bcm2835_get_temp,
  33979. + .get_trip_temp = bcm2835_get_max_temp,
  33980. + .get_trip_type = bcm2835_get_trip_type,
  33981. + .get_mode = bcm2835_get_mode,
  33982. +};
  33983. +
  33984. +/* Thermal Driver */
  33985. +static struct platform_driver bcm2835_thermal_driver = {
  33986. + .probe = bcm2835_thermal_probe,
  33987. + .remove = bcm2835_thermal_remove,
  33988. + .driver = {
  33989. + .name = "bcm2835_thermal",
  33990. + .owner = THIS_MODULE,
  33991. + },
  33992. +};
  33993. +
  33994. +MODULE_LICENSE("GPL");
  33995. +MODULE_AUTHOR("Dorian Peake");
  33996. +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
  33997. +
  33998. +module_platform_driver(bcm2835_thermal_driver);
  33999. diff -Nur linux-3.13.6/drivers/thermal/Kconfig linux-raspberry-pi/drivers/thermal/Kconfig
  34000. --- linux-3.13.6/drivers/thermal/Kconfig 2014-03-07 07:07:02.000000000 +0100
  34001. +++ linux-raspberry-pi/drivers/thermal/Kconfig 2014-03-11 16:55:36.000000000 +0100
  34002. @@ -181,6 +181,12 @@
  34003. enforce idle time which results in more package C-state residency. The
  34004. user interface is exposed via generic thermal framework.
  34005. +config THERMAL_BCM2835
  34006. + tristate "BCM2835 Thermal Driver"
  34007. + help
  34008. + This will enable temperature monitoring for the Broadcom BCM2835
  34009. + chip. If built as a module, it will be called 'bcm2835-thermal'.
  34010. +
  34011. config X86_PKG_TEMP_THERMAL
  34012. tristate "X86 package temperature thermal driver"
  34013. depends on X86_THERMAL_VECTOR
  34014. diff -Nur linux-3.13.6/drivers/thermal/Makefile linux-raspberry-pi/drivers/thermal/Makefile
  34015. --- linux-3.13.6/drivers/thermal/Makefile 2014-03-07 07:07:02.000000000 +0100
  34016. +++ linux-raspberry-pi/drivers/thermal/Makefile 2014-03-11 16:55:36.000000000 +0100
  34017. @@ -27,5 +27,6 @@
  34018. obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
  34019. obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
  34020. obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
  34021. +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
  34022. obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
  34023. obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/
  34024. diff -Nur linux-3.13.6/drivers/tty/serial/amba-pl011.c linux-raspberry-pi/drivers/tty/serial/amba-pl011.c
  34025. --- linux-3.13.6/drivers/tty/serial/amba-pl011.c 2014-03-07 07:07:02.000000000 +0100
  34026. +++ linux-raspberry-pi/drivers/tty/serial/amba-pl011.c 2014-03-11 16:55:36.000000000 +0100
  34027. @@ -84,7 +84,7 @@
  34028. static unsigned int get_fifosize_arm(struct amba_device *dev)
  34029. {
  34030. - return amba_rev(dev) < 3 ? 16 : 32;
  34031. + return 16; //TODO: fix: amba_rev(dev) < 3 ? 16 : 32;
  34032. }
  34033. static struct vendor_data vendor_arm = {
  34034. diff -Nur linux-3.13.6/drivers/usb/core/generic.c linux-raspberry-pi/drivers/usb/core/generic.c
  34035. --- linux-3.13.6/drivers/usb/core/generic.c 2014-03-07 07:07:02.000000000 +0100
  34036. +++ linux-raspberry-pi/drivers/usb/core/generic.c 2014-03-11 16:53:09.000000000 +0100
  34037. @@ -152,6 +152,7 @@
  34038. dev_warn(&udev->dev,
  34039. "no configuration chosen from %d choice%s\n",
  34040. num_configs, plural(num_configs));
  34041. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  34042. }
  34043. return i;
  34044. }
  34045. diff -Nur linux-3.13.6/drivers/usb/core/message.c linux-raspberry-pi/drivers/usb/core/message.c
  34046. --- linux-3.13.6/drivers/usb/core/message.c 2014-03-07 07:07:02.000000000 +0100
  34047. +++ linux-raspberry-pi/drivers/usb/core/message.c 2014-03-11 16:55:37.000000000 +0100
  34048. @@ -1889,6 +1889,85 @@
  34049. if (cp->string == NULL &&
  34050. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  34051. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  34052. +/* Uncomment this define to enable the HS Electrical Test support */
  34053. +#define DWC_HS_ELECT_TST 1
  34054. +#ifdef DWC_HS_ELECT_TST
  34055. + /* Here we implement the HS Electrical Test support. The
  34056. + * tester uses a vendor ID of 0x1A0A to indicate we should
  34057. + * run a special test sequence. The product ID tells us
  34058. + * which sequence to run. We invoke the test sequence by
  34059. + * sending a non-standard SetFeature command to our root
  34060. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  34061. + * recognize the command and perform the desired test
  34062. + * sequence.
  34063. + */
  34064. + if (dev->descriptor.idVendor == 0x1A0A) {
  34065. + /* HSOTG Electrical Test */
  34066. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  34067. +
  34068. + if (dev->bus && dev->bus->root_hub) {
  34069. + struct usb_device *hdev = dev->bus->root_hub;
  34070. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  34071. +
  34072. + switch (dev->descriptor.idProduct) {
  34073. + case 0x0101: /* TEST_SE0_NAK */
  34074. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  34075. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34076. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34077. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  34078. + break;
  34079. +
  34080. + case 0x0102: /* TEST_J */
  34081. + dev_warn(&dev->dev, "TEST_J\n");
  34082. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34083. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34084. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  34085. + break;
  34086. +
  34087. + case 0x0103: /* TEST_K */
  34088. + dev_warn(&dev->dev, "TEST_K\n");
  34089. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34090. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34091. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  34092. + break;
  34093. +
  34094. + case 0x0104: /* TEST_PACKET */
  34095. + dev_warn(&dev->dev, "TEST_PACKET\n");
  34096. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34097. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34098. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  34099. + break;
  34100. +
  34101. + case 0x0105: /* TEST_FORCE_ENABLE */
  34102. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  34103. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34104. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34105. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  34106. + break;
  34107. +
  34108. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  34109. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  34110. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34111. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34112. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  34113. + break;
  34114. +
  34115. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  34116. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  34117. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34118. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34119. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  34120. + break;
  34121. +
  34122. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  34123. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  34124. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  34125. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  34126. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  34127. + }
  34128. + }
  34129. + }
  34130. +#endif /* DWC_HS_ELECT_TST */
  34131. /* Now that the interfaces are installed, re-enable LPM. */
  34132. usb_unlocked_enable_lpm(dev);
  34133. diff -Nur linux-3.13.6/drivers/usb/core/otg_whitelist.h linux-raspberry-pi/drivers/usb/core/otg_whitelist.h
  34134. --- linux-3.13.6/drivers/usb/core/otg_whitelist.h 2014-03-07 07:07:02.000000000 +0100
  34135. +++ linux-raspberry-pi/drivers/usb/core/otg_whitelist.h 2014-03-11 16:55:37.000000000 +0100
  34136. @@ -19,33 +19,82 @@
  34137. static struct usb_device_id whitelist_table [] = {
  34138. /* hubs are optional in OTG, but very handy ... */
  34139. +#define CERT_WITHOUT_HUBS
  34140. +#if defined(CERT_WITHOUT_HUBS)
  34141. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  34142. +#else
  34143. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  34144. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  34145. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  34146. +#endif
  34147. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  34148. /* FIXME actually, printers are NOT supposed to use device classes;
  34149. * they're supposed to use interface classes...
  34150. */
  34151. -{ USB_DEVICE_INFO(7, 1, 1) },
  34152. -{ USB_DEVICE_INFO(7, 1, 2) },
  34153. -{ USB_DEVICE_INFO(7, 1, 3) },
  34154. +//{ USB_DEVICE_INFO(7, 1, 1) },
  34155. +//{ USB_DEVICE_INFO(7, 1, 2) },
  34156. +//{ USB_DEVICE_INFO(7, 1, 3) },
  34157. #endif
  34158. #ifdef CONFIG_USB_NET_CDCETHER
  34159. /* Linux-USB CDC Ethernet gadget */
  34160. -{ USB_DEVICE(0x0525, 0xa4a1), },
  34161. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  34162. /* Linux-USB CDC Ethernet + RNDIS gadget */
  34163. -{ USB_DEVICE(0x0525, 0xa4a2), },
  34164. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  34165. #endif
  34166. #if defined(CONFIG_USB_TEST) || defined(CONFIG_USB_TEST_MODULE)
  34167. /* gadget zero, for testing */
  34168. -{ USB_DEVICE(0x0525, 0xa4a0), },
  34169. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  34170. #endif
  34171. +/* OPT Tester */
  34172. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  34173. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  34174. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  34175. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  34176. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  34177. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  34178. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  34179. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  34180. +
  34181. +/* Sony cameras */
  34182. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  34183. +
  34184. +/* Memory Devices */
  34185. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  34186. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  34187. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  34188. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  34189. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  34190. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  34191. +
  34192. +/* HP Printers */
  34193. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  34194. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  34195. +
  34196. +/* Speakers */
  34197. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  34198. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  34199. +
  34200. { } /* Terminating entry */
  34201. };
  34202. +static inline void report_errors(struct usb_device *dev)
  34203. +{
  34204. + /* OTG MESSAGE: report errors here, customize to match your product */
  34205. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  34206. + le16_to_cpu(dev->descriptor.idVendor),
  34207. + le16_to_cpu(dev->descriptor.idProduct));
  34208. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  34209. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  34210. + } else {
  34211. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  34212. + }
  34213. +}
  34214. +
  34215. +
  34216. static int is_targeted(struct usb_device *dev)
  34217. {
  34218. struct usb_device_id *id = whitelist_table;
  34219. @@ -55,58 +104,83 @@
  34220. return 1;
  34221. /* HNP test device is _never_ targeted (see OTG spec 6.6.6) */
  34222. - if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1a0a &&
  34223. - le16_to_cpu(dev->descriptor.idProduct) == 0xbadd))
  34224. - return 0;
  34225. + if (dev->descriptor.idVendor == 0x1a0a &&
  34226. + dev->descriptor.idProduct == 0xbadd) {
  34227. + return 0;
  34228. + } else if (!enable_whitelist) {
  34229. + return 1;
  34230. + } else {
  34231. - /* NOTE: can't use usb_match_id() since interface caches
  34232. - * aren't set up yet. this is cut/paste from that code.
  34233. - */
  34234. - for (id = whitelist_table; id->match_flags; id++) {
  34235. - if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  34236. - id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  34237. - continue;
  34238. -
  34239. - if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  34240. - id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  34241. - continue;
  34242. -
  34243. - /* No need to test id->bcdDevice_lo != 0, since 0 is never
  34244. - greater than any unsigned number. */
  34245. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  34246. - (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  34247. - continue;
  34248. -
  34249. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  34250. - (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  34251. - continue;
  34252. -
  34253. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  34254. - (id->bDeviceClass != dev->descriptor.bDeviceClass))
  34255. - continue;
  34256. -
  34257. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  34258. - (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  34259. - continue;
  34260. -
  34261. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  34262. - (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  34263. - continue;
  34264. +#ifdef DEBUG
  34265. + dev_dbg(&dev->dev, "device V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  34266. + dev->descriptor.idVendor,
  34267. + dev->descriptor.idProduct,
  34268. + dev->descriptor.bDeviceClass,
  34269. + dev->descriptor.bDeviceSubClass,
  34270. + dev->descriptor.bDeviceProtocol);
  34271. +#endif
  34272. return 1;
  34273. + /* NOTE: can't use usb_match_id() since interface caches
  34274. + * aren't set up yet. this is cut/paste from that code.
  34275. + */
  34276. + for (id = whitelist_table; id->match_flags; id++) {
  34277. +#ifdef DEBUG
  34278. + dev_dbg(&dev->dev,
  34279. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  34280. + id->idVendor,
  34281. + id->idProduct,
  34282. + id->bDeviceClass,
  34283. + id->bDeviceSubClass,
  34284. + id->bDeviceProtocol);
  34285. +#endif
  34286. +
  34287. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  34288. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  34289. + continue;
  34290. +
  34291. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  34292. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  34293. + continue;
  34294. +
  34295. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  34296. + greater than any unsigned number. */
  34297. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  34298. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  34299. + continue;
  34300. +
  34301. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  34302. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  34303. + continue;
  34304. +
  34305. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  34306. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  34307. + continue;
  34308. +
  34309. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  34310. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  34311. + continue;
  34312. +
  34313. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  34314. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  34315. + continue;
  34316. +
  34317. + return 1;
  34318. + }
  34319. }
  34320. /* add other match criteria here ... */
  34321. -
  34322. - /* OTG MESSAGE: report errors here, customize to match your product */
  34323. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  34324. - le16_to_cpu(dev->descriptor.idVendor),
  34325. - le16_to_cpu(dev->descriptor.idProduct));
  34326. #ifdef CONFIG_USB_OTG_WHITELIST
  34327. + report_errors(dev);
  34328. return 0;
  34329. #else
  34330. - return 1;
  34331. + if (enable_whitelist) {
  34332. + report_errors(dev);
  34333. + return 0;
  34334. + } else {
  34335. + return 1;
  34336. + }
  34337. #endif
  34338. }
  34339. diff -Nur linux-3.13.6/drivers/usb/gadget/file_storage.c linux-raspberry-pi/drivers/usb/gadget/file_storage.c
  34340. --- linux-3.13.6/drivers/usb/gadget/file_storage.c 1970-01-01 01:00:00.000000000 +0100
  34341. +++ linux-raspberry-pi/drivers/usb/gadget/file_storage.c 2014-03-11 16:53:10.000000000 +0100
  34342. @@ -0,0 +1,3676 @@
  34343. +/*
  34344. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  34345. + *
  34346. + * Copyright (C) 2003-2008 Alan Stern
  34347. + * All rights reserved.
  34348. + *
  34349. + * Redistribution and use in source and binary forms, with or without
  34350. + * modification, are permitted provided that the following conditions
  34351. + * are met:
  34352. + * 1. Redistributions of source code must retain the above copyright
  34353. + * notice, this list of conditions, and the following disclaimer,
  34354. + * without modification.
  34355. + * 2. Redistributions in binary form must reproduce the above copyright
  34356. + * notice, this list of conditions and the following disclaimer in the
  34357. + * documentation and/or other materials provided with the distribution.
  34358. + * 3. The names of the above-listed copyright holders may not be used
  34359. + * to endorse or promote products derived from this software without
  34360. + * specific prior written permission.
  34361. + *
  34362. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34363. + * GNU General Public License ("GPL") as published by the Free Software
  34364. + * Foundation, either version 2 of that License or (at your option) any
  34365. + * later version.
  34366. + *
  34367. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34368. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34369. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34370. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34371. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34372. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34373. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34374. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34375. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34376. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34377. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34378. + */
  34379. +
  34380. +
  34381. +/*
  34382. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  34383. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  34384. + * to providing an example of a genuinely useful gadget driver for a USB
  34385. + * device, it also illustrates a technique of double-buffering for increased
  34386. + * throughput. Last but not least, it gives an easy way to probe the
  34387. + * behavior of the Mass Storage drivers in a USB host.
  34388. + *
  34389. + * Backing storage is provided by a regular file or a block device, specified
  34390. + * by the "file" module parameter. Access can be limited to read-only by
  34391. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  34392. + * access is always read-only.) The gadget will indicate that it has
  34393. + * removable media if the optional "removable" module parameter is set.
  34394. + *
  34395. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  34396. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  34397. + * by the optional "transport" module parameter. It also supports the
  34398. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  34399. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  34400. + * the optional "protocol" module parameter. In addition, the default
  34401. + * Vendor ID, Product ID, release number and serial number can be overridden.
  34402. + *
  34403. + * There is support for multiple logical units (LUNs), each of which has
  34404. + * its own backing file. The number of LUNs can be set using the optional
  34405. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  34406. + * files are specified using comma-separated lists for "file" and "ro".
  34407. + * The default number of LUNs is taken from the number of "file" elements;
  34408. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  34409. + * file must be specified for each LUN. If it is set, then an unspecified
  34410. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  34411. + * each LUN would be settable independently as a disk drive or a CD-ROM
  34412. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  34413. + * emulation includes a single data track and no audio tracks; hence there
  34414. + * need be only one backing file per LUN.
  34415. + *
  34416. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  34417. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  34418. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  34419. + * Support is included for both full-speed and high-speed operation.
  34420. + *
  34421. + * Note that the driver is slightly non-portable in that it assumes a
  34422. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  34423. + * interrupt-in endpoints. With most device controllers this isn't an
  34424. + * issue, but there may be some with hardware restrictions that prevent
  34425. + * a buffer from being used by more than one endpoint.
  34426. + *
  34427. + * Module options:
  34428. + *
  34429. + * file=filename[,filename...]
  34430. + * Required if "removable" is not set, names of
  34431. + * the files or block devices used for
  34432. + * backing storage
  34433. + * serial=HHHH... Required serial number (string of hex chars)
  34434. + * ro=b[,b...] Default false, booleans for read-only access
  34435. + * removable Default false, boolean for removable media
  34436. + * luns=N Default N = number of filenames, number of
  34437. + * LUNs to support
  34438. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  34439. + * in SCSI WRITE(10,12) commands
  34440. + * stall Default determined according to the type of
  34441. + * USB device controller (usually true),
  34442. + * boolean to permit the driver to halt
  34443. + * bulk endpoints
  34444. + * cdrom Default false, boolean for whether to emulate
  34445. + * a CD-ROM drive
  34446. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  34447. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  34448. + * ATAPI, QIC, UFI, 8070, or SCSI;
  34449. + * also 1 - 6)
  34450. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  34451. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  34452. + * release=0xRRRR Override the USB release number (bcdDevice)
  34453. + * buflen=N Default N=16384, buffer size used (will be
  34454. + * rounded down to a multiple of
  34455. + * PAGE_CACHE_SIZE)
  34456. + *
  34457. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  34458. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  34459. + * default values are used for everything else.
  34460. + *
  34461. + * The pathnames of the backing files and the ro settings are available in
  34462. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  34463. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  34464. + * these files will simulate ejecting/loading the medium (writing an empty
  34465. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  34466. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  34467. + * is being used.
  34468. + *
  34469. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  34470. + * The driver's SCSI command interface was based on the "Information
  34471. + * technology - Small Computer System Interface - 2" document from
  34472. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  34473. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  34474. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  34475. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  34476. + * document, Revision 1.0, December 14, 1998, available at
  34477. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  34478. + */
  34479. +
  34480. +
  34481. +/*
  34482. + * Driver Design
  34483. + *
  34484. + * The FSG driver is fairly straightforward. There is a main kernel
  34485. + * thread that handles most of the work. Interrupt routines field
  34486. + * callbacks from the controller driver: bulk- and interrupt-request
  34487. + * completion notifications, endpoint-0 events, and disconnect events.
  34488. + * Completion events are passed to the main thread by wakeup calls. Many
  34489. + * ep0 requests are handled at interrupt time, but SetInterface,
  34490. + * SetConfiguration, and device reset requests are forwarded to the
  34491. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  34492. + * should interrupt any ongoing file I/O operations).
  34493. + *
  34494. + * The thread's main routine implements the standard command/data/status
  34495. + * parts of a SCSI interaction. It and its subroutines are full of tests
  34496. + * for pending signals/exceptions -- all this polling is necessary since
  34497. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  34498. + * indication that the driver really wants to be running in userspace.)
  34499. + * An important point is that so long as the thread is alive it keeps an
  34500. + * open reference to the backing file. This will prevent unmounting
  34501. + * the backing file's underlying filesystem and could cause problems
  34502. + * during system shutdown, for example. To prevent such problems, the
  34503. + * thread catches INT, TERM, and KILL signals and converts them into
  34504. + * an EXIT exception.
  34505. + *
  34506. + * In normal operation the main thread is started during the gadget's
  34507. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  34508. + * exit when it receives a signal, and there's no point leaving the
  34509. + * gadget running when the thread is dead. So just before the thread
  34510. + * exits, it deregisters the gadget driver. This makes things a little
  34511. + * tricky: The driver is deregistered at two places, and the exiting
  34512. + * thread can indirectly call fsg_unbind() which in turn can tell the
  34513. + * thread to exit. The first problem is resolved through the use of the
  34514. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  34515. + * The second problem is resolved by having fsg_unbind() check
  34516. + * fsg->state; it won't try to stop the thread if the state is already
  34517. + * FSG_STATE_TERMINATED.
  34518. + *
  34519. + * To provide maximum throughput, the driver uses a circular pipeline of
  34520. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  34521. + * arbitrarily long; in practice the benefits don't justify having more
  34522. + * than 2 stages (i.e., double buffering). But it helps to think of the
  34523. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  34524. + * a bulk-out request pointer (since the buffer can be used for both
  34525. + * output and input -- directions always are given from the host's
  34526. + * point of view) as well as a pointer to the buffer and various state
  34527. + * variables.
  34528. + *
  34529. + * Use of the pipeline follows a simple protocol. There is a variable
  34530. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  34531. + * At any time that buffer head may still be in use from an earlier
  34532. + * request, so each buffer head has a state variable indicating whether
  34533. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  34534. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  34535. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  34536. + * head FULL when the I/O is complete. Then the buffer will be emptied
  34537. + * (again possibly by USB I/O, during which it is marked BUSY) and
  34538. + * finally marked EMPTY again (possibly by a completion routine).
  34539. + *
  34540. + * A module parameter tells the driver to avoid stalling the bulk
  34541. + * endpoints wherever the transport specification allows. This is
  34542. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  34543. + * halt on a bulk endpoint. However, under certain circumstances the
  34544. + * Bulk-only specification requires a stall. In such cases the driver
  34545. + * will halt the endpoint and set a flag indicating that it should clear
  34546. + * the halt in software during the next device reset. Hopefully this
  34547. + * will permit everything to work correctly. Furthermore, although the
  34548. + * specification allows the bulk-out endpoint to halt when the host sends
  34549. + * too much data, implementing this would cause an unavoidable race.
  34550. + * The driver will always use the "no-stall" approach for OUT transfers.
  34551. + *
  34552. + * One subtle point concerns sending status-stage responses for ep0
  34553. + * requests. Some of these requests, such as device reset, can involve
  34554. + * interrupting an ongoing file I/O operation, which might take an
  34555. + * arbitrarily long time. During that delay the host might give up on
  34556. + * the original ep0 request and issue a new one. When that happens the
  34557. + * driver should not notify the host about completion of the original
  34558. + * request, as the host will no longer be waiting for it. So the driver
  34559. + * assigns to each ep0 request a unique tag, and it keeps track of the
  34560. + * tag value of the request associated with a long-running exception
  34561. + * (device-reset, interface-change, or configuration-change). When the
  34562. + * exception handler is finished, the status-stage response is submitted
  34563. + * only if the current ep0 request tag is equal to the exception request
  34564. + * tag. Thus only the most recently received ep0 request will get a
  34565. + * status-stage response.
  34566. + *
  34567. + * Warning: This driver source file is too long. It ought to be split up
  34568. + * into a header file plus about 3 separate .c files, to handle the details
  34569. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  34570. + */
  34571. +
  34572. +
  34573. +/* #define VERBOSE_DEBUG */
  34574. +/* #define DUMP_MSGS */
  34575. +
  34576. +
  34577. +#include <linux/blkdev.h>
  34578. +#include <linux/completion.h>
  34579. +#include <linux/dcache.h>
  34580. +#include <linux/delay.h>
  34581. +#include <linux/device.h>
  34582. +#include <linux/fcntl.h>
  34583. +#include <linux/file.h>
  34584. +#include <linux/fs.h>
  34585. +#include <linux/kref.h>
  34586. +#include <linux/kthread.h>
  34587. +#include <linux/limits.h>
  34588. +#include <linux/module.h>
  34589. +#include <linux/rwsem.h>
  34590. +#include <linux/slab.h>
  34591. +#include <linux/spinlock.h>
  34592. +#include <linux/string.h>
  34593. +#include <linux/freezer.h>
  34594. +#include <linux/utsname.h>
  34595. +
  34596. +#include <linux/usb/ch9.h>
  34597. +#include <linux/usb/gadget.h>
  34598. +
  34599. +#include "gadget_chips.h"
  34600. +
  34601. +
  34602. +
  34603. +/*
  34604. + * Kbuild is not very cooperative with respect to linking separately
  34605. + * compiled library objects into one module. So for now we won't use
  34606. + * separate compilation ... ensuring init/exit sections work to shrink
  34607. + * the runtime footprint, and giving us at least some parts of what
  34608. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  34609. + */
  34610. +#include "usbstring.c"
  34611. +#include "config.c"
  34612. +#include "epautoconf.c"
  34613. +
  34614. +/*-------------------------------------------------------------------------*/
  34615. +
  34616. +#define DRIVER_DESC "File-backed Storage Gadget"
  34617. +#define DRIVER_NAME "g_file_storage"
  34618. +#define DRIVER_VERSION "1 September 2010"
  34619. +
  34620. +static char fsg_string_manufacturer[64];
  34621. +static const char fsg_string_product[] = DRIVER_DESC;
  34622. +static const char fsg_string_config[] = "Self-powered";
  34623. +static const char fsg_string_interface[] = "Mass Storage";
  34624. +
  34625. +
  34626. +#include "storage_common.c"
  34627. +
  34628. +
  34629. +MODULE_DESCRIPTION(DRIVER_DESC);
  34630. +MODULE_AUTHOR("Alan Stern");
  34631. +MODULE_LICENSE("Dual BSD/GPL");
  34632. +
  34633. +/*
  34634. + * This driver assumes self-powered hardware and has no way for users to
  34635. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  34636. + * and endpoint addresses.
  34637. + */
  34638. +
  34639. +
  34640. +/*-------------------------------------------------------------------------*/
  34641. +
  34642. +
  34643. +/* Encapsulate the module parameter settings */
  34644. +
  34645. +static struct {
  34646. + char *file[FSG_MAX_LUNS];
  34647. + char *serial;
  34648. + bool ro[FSG_MAX_LUNS];
  34649. + bool nofua[FSG_MAX_LUNS];
  34650. + unsigned int num_filenames;
  34651. + unsigned int num_ros;
  34652. + unsigned int num_nofuas;
  34653. + unsigned int nluns;
  34654. +
  34655. + bool removable;
  34656. + bool can_stall;
  34657. + bool cdrom;
  34658. +
  34659. + char *transport_parm;
  34660. + char *protocol_parm;
  34661. + unsigned short vendor;
  34662. + unsigned short product;
  34663. + unsigned short release;
  34664. + unsigned int buflen;
  34665. +
  34666. + int transport_type;
  34667. + char *transport_name;
  34668. + int protocol_type;
  34669. + char *protocol_name;
  34670. +
  34671. +} mod_data = { // Default values
  34672. + .transport_parm = "BBB",
  34673. + .protocol_parm = "SCSI",
  34674. + .removable = 0,
  34675. + .can_stall = 1,
  34676. + .cdrom = 0,
  34677. + .vendor = FSG_VENDOR_ID,
  34678. + .product = FSG_PRODUCT_ID,
  34679. + .release = 0xffff, // Use controller chip type
  34680. + .buflen = 16384,
  34681. + };
  34682. +
  34683. +
  34684. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  34685. + S_IRUGO);
  34686. +MODULE_PARM_DESC(file, "names of backing files or devices");
  34687. +
  34688. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  34689. +MODULE_PARM_DESC(serial, "USB serial number");
  34690. +
  34691. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  34692. +MODULE_PARM_DESC(ro, "true to force read-only");
  34693. +
  34694. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  34695. + S_IRUGO);
  34696. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  34697. +
  34698. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  34699. +MODULE_PARM_DESC(luns, "number of LUNs");
  34700. +
  34701. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  34702. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  34703. +
  34704. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  34705. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  34706. +
  34707. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  34708. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  34709. +
  34710. +/* In the non-TEST version, only the module parameters listed above
  34711. + * are available. */
  34712. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  34713. +
  34714. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  34715. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  34716. +
  34717. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  34718. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  34719. + "8070, or SCSI)");
  34720. +
  34721. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  34722. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  34723. +
  34724. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  34725. +MODULE_PARM_DESC(product, "USB Product ID");
  34726. +
  34727. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  34728. +MODULE_PARM_DESC(release, "USB release number");
  34729. +
  34730. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  34731. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  34732. +
  34733. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  34734. +
  34735. +
  34736. +/*
  34737. + * These definitions will permit the compiler to avoid generating code for
  34738. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  34739. + * can recognize when a test of a constant expression yields a dead code
  34740. + * path.
  34741. + */
  34742. +
  34743. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  34744. +
  34745. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  34746. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  34747. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  34748. +
  34749. +#else
  34750. +
  34751. +#define transport_is_bbb() 1
  34752. +#define transport_is_cbi() 0
  34753. +#define protocol_is_scsi() 1
  34754. +
  34755. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  34756. +
  34757. +
  34758. +/*-------------------------------------------------------------------------*/
  34759. +
  34760. +
  34761. +struct fsg_dev {
  34762. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  34763. + spinlock_t lock;
  34764. + struct usb_gadget *gadget;
  34765. +
  34766. + /* filesem protects: backing files in use */
  34767. + struct rw_semaphore filesem;
  34768. +
  34769. + /* reference counting: wait until all LUNs are released */
  34770. + struct kref ref;
  34771. +
  34772. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  34773. + struct usb_request *ep0req; // For control responses
  34774. + unsigned int ep0_req_tag;
  34775. + const char *ep0req_name;
  34776. +
  34777. + struct usb_request *intreq; // For interrupt responses
  34778. + int intreq_busy;
  34779. + struct fsg_buffhd *intr_buffhd;
  34780. +
  34781. + unsigned int bulk_out_maxpacket;
  34782. + enum fsg_state state; // For exception handling
  34783. + unsigned int exception_req_tag;
  34784. +
  34785. + u8 config, new_config;
  34786. +
  34787. + unsigned int running : 1;
  34788. + unsigned int bulk_in_enabled : 1;
  34789. + unsigned int bulk_out_enabled : 1;
  34790. + unsigned int intr_in_enabled : 1;
  34791. + unsigned int phase_error : 1;
  34792. + unsigned int short_packet_received : 1;
  34793. + unsigned int bad_lun_okay : 1;
  34794. +
  34795. + unsigned long atomic_bitflags;
  34796. +#define REGISTERED 0
  34797. +#define IGNORE_BULK_OUT 1
  34798. +#define SUSPENDED 2
  34799. +
  34800. + struct usb_ep *bulk_in;
  34801. + struct usb_ep *bulk_out;
  34802. + struct usb_ep *intr_in;
  34803. +
  34804. + struct fsg_buffhd *next_buffhd_to_fill;
  34805. + struct fsg_buffhd *next_buffhd_to_drain;
  34806. +
  34807. + int thread_wakeup_needed;
  34808. + struct completion thread_notifier;
  34809. + struct task_struct *thread_task;
  34810. +
  34811. + int cmnd_size;
  34812. + u8 cmnd[MAX_COMMAND_SIZE];
  34813. + enum data_direction data_dir;
  34814. + u32 data_size;
  34815. + u32 data_size_from_cmnd;
  34816. + u32 tag;
  34817. + unsigned int lun;
  34818. + u32 residue;
  34819. + u32 usb_amount_left;
  34820. +
  34821. + /* The CB protocol offers no way for a host to know when a command
  34822. + * has completed. As a result the next command may arrive early,
  34823. + * and we will still have to handle it. For that reason we need
  34824. + * a buffer to store new commands when using CB (or CBI, which
  34825. + * does not oblige a host to wait for command completion either). */
  34826. + int cbbuf_cmnd_size;
  34827. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  34828. +
  34829. + unsigned int nluns;
  34830. + struct fsg_lun *luns;
  34831. + struct fsg_lun *curlun;
  34832. + /* Must be the last entry */
  34833. + struct fsg_buffhd buffhds[];
  34834. +};
  34835. +
  34836. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  34837. +
  34838. +static int exception_in_progress(struct fsg_dev *fsg)
  34839. +{
  34840. + return (fsg->state > FSG_STATE_IDLE);
  34841. +}
  34842. +
  34843. +/* Make bulk-out requests be divisible by the maxpacket size */
  34844. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  34845. + struct fsg_buffhd *bh, unsigned int length)
  34846. +{
  34847. + unsigned int rem;
  34848. +
  34849. + bh->bulk_out_intended_length = length;
  34850. + rem = length % fsg->bulk_out_maxpacket;
  34851. + if (rem > 0)
  34852. + length += fsg->bulk_out_maxpacket - rem;
  34853. + bh->outreq->length = length;
  34854. +}
  34855. +
  34856. +static struct fsg_dev *the_fsg;
  34857. +static struct usb_gadget_driver fsg_driver;
  34858. +
  34859. +
  34860. +/*-------------------------------------------------------------------------*/
  34861. +
  34862. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  34863. +{
  34864. + const char *name;
  34865. +
  34866. + if (ep == fsg->bulk_in)
  34867. + name = "bulk-in";
  34868. + else if (ep == fsg->bulk_out)
  34869. + name = "bulk-out";
  34870. + else
  34871. + name = ep->name;
  34872. + DBG(fsg, "%s set halt\n", name);
  34873. + return usb_ep_set_halt(ep);
  34874. +}
  34875. +
  34876. +
  34877. +/*-------------------------------------------------------------------------*/
  34878. +
  34879. +/*
  34880. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  34881. + * descriptors are built on demand. Also the (static) config and interface
  34882. + * descriptors are adjusted during fsg_bind().
  34883. + */
  34884. +
  34885. +/* There is only one configuration. */
  34886. +#define CONFIG_VALUE 1
  34887. +
  34888. +static struct usb_device_descriptor
  34889. +device_desc = {
  34890. + .bLength = sizeof device_desc,
  34891. + .bDescriptorType = USB_DT_DEVICE,
  34892. +
  34893. + .bcdUSB = cpu_to_le16(0x0200),
  34894. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  34895. +
  34896. + /* The next three values can be overridden by module parameters */
  34897. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  34898. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  34899. + .bcdDevice = cpu_to_le16(0xffff),
  34900. +
  34901. + .iManufacturer = FSG_STRING_MANUFACTURER,
  34902. + .iProduct = FSG_STRING_PRODUCT,
  34903. + .iSerialNumber = FSG_STRING_SERIAL,
  34904. + .bNumConfigurations = 1,
  34905. +};
  34906. +
  34907. +static struct usb_config_descriptor
  34908. +config_desc = {
  34909. + .bLength = sizeof config_desc,
  34910. + .bDescriptorType = USB_DT_CONFIG,
  34911. +
  34912. + /* wTotalLength computed by usb_gadget_config_buf() */
  34913. + .bNumInterfaces = 1,
  34914. + .bConfigurationValue = CONFIG_VALUE,
  34915. + .iConfiguration = FSG_STRING_CONFIG,
  34916. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  34917. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  34918. +};
  34919. +
  34920. +
  34921. +static struct usb_qualifier_descriptor
  34922. +dev_qualifier = {
  34923. + .bLength = sizeof dev_qualifier,
  34924. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  34925. +
  34926. + .bcdUSB = cpu_to_le16(0x0200),
  34927. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  34928. +
  34929. + .bNumConfigurations = 1,
  34930. +};
  34931. +
  34932. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  34933. +{
  34934. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  34935. + buf += USB_DT_BOS_SIZE;
  34936. +
  34937. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  34938. + buf += USB_DT_USB_EXT_CAP_SIZE;
  34939. +
  34940. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  34941. +
  34942. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  34943. + + USB_DT_USB_EXT_CAP_SIZE;
  34944. +}
  34945. +
  34946. +/*
  34947. + * Config descriptors must agree with the code that sets configurations
  34948. + * and with code managing interfaces and their altsettings. They must
  34949. + * also handle different speeds and other-speed requests.
  34950. + */
  34951. +static int populate_config_buf(struct usb_gadget *gadget,
  34952. + u8 *buf, u8 type, unsigned index)
  34953. +{
  34954. + enum usb_device_speed speed = gadget->speed;
  34955. + int len;
  34956. + const struct usb_descriptor_header **function;
  34957. +
  34958. + if (index > 0)
  34959. + return -EINVAL;
  34960. +
  34961. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  34962. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  34963. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  34964. + ? (const struct usb_descriptor_header **)fsg_hs_function
  34965. + : (const struct usb_descriptor_header **)fsg_fs_function;
  34966. +
  34967. + /* for now, don't advertise srp-only devices */
  34968. + if (!gadget_is_otg(gadget))
  34969. + function++;
  34970. +
  34971. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  34972. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  34973. + return len;
  34974. +}
  34975. +
  34976. +
  34977. +/*-------------------------------------------------------------------------*/
  34978. +
  34979. +/* These routines may be called in process context or in_irq */
  34980. +
  34981. +/* Caller must hold fsg->lock */
  34982. +static void wakeup_thread(struct fsg_dev *fsg)
  34983. +{
  34984. + /* Tell the main thread that something has happened */
  34985. + fsg->thread_wakeup_needed = 1;
  34986. + if (fsg->thread_task)
  34987. + wake_up_process(fsg->thread_task);
  34988. +}
  34989. +
  34990. +
  34991. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  34992. +{
  34993. + unsigned long flags;
  34994. +
  34995. + /* Do nothing if a higher-priority exception is already in progress.
  34996. + * If a lower-or-equal priority exception is in progress, preempt it
  34997. + * and notify the main thread by sending it a signal. */
  34998. + spin_lock_irqsave(&fsg->lock, flags);
  34999. + if (fsg->state <= new_state) {
  35000. + fsg->exception_req_tag = fsg->ep0_req_tag;
  35001. + fsg->state = new_state;
  35002. + if (fsg->thread_task)
  35003. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  35004. + fsg->thread_task);
  35005. + }
  35006. + spin_unlock_irqrestore(&fsg->lock, flags);
  35007. +}
  35008. +
  35009. +
  35010. +/*-------------------------------------------------------------------------*/
  35011. +
  35012. +/* The disconnect callback and ep0 routines. These always run in_irq,
  35013. + * except that ep0_queue() is called in the main thread to acknowledge
  35014. + * completion of various requests: set config, set interface, and
  35015. + * Bulk-only device reset. */
  35016. +
  35017. +static void fsg_disconnect(struct usb_gadget *gadget)
  35018. +{
  35019. + struct fsg_dev *fsg = get_gadget_data(gadget);
  35020. +
  35021. + DBG(fsg, "disconnect or port reset\n");
  35022. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  35023. +}
  35024. +
  35025. +
  35026. +static int ep0_queue(struct fsg_dev *fsg)
  35027. +{
  35028. + int rc;
  35029. +
  35030. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  35031. + if (rc != 0 && rc != -ESHUTDOWN) {
  35032. +
  35033. + /* We can't do much more than wait for a reset */
  35034. + WARNING(fsg, "error in submission: %s --> %d\n",
  35035. + fsg->ep0->name, rc);
  35036. + }
  35037. + return rc;
  35038. +}
  35039. +
  35040. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  35041. +{
  35042. + struct fsg_dev *fsg = ep->driver_data;
  35043. +
  35044. + if (req->actual > 0)
  35045. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  35046. + if (req->status || req->actual != req->length)
  35047. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35048. + req->status, req->actual, req->length);
  35049. + if (req->status == -ECONNRESET) // Request was cancelled
  35050. + usb_ep_fifo_flush(ep);
  35051. +
  35052. + if (req->status == 0 && req->context)
  35053. + ((fsg_routine_t) (req->context))(fsg);
  35054. +}
  35055. +
  35056. +
  35057. +/*-------------------------------------------------------------------------*/
  35058. +
  35059. +/* Bulk and interrupt endpoint completion handlers.
  35060. + * These always run in_irq. */
  35061. +
  35062. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  35063. +{
  35064. + struct fsg_dev *fsg = ep->driver_data;
  35065. + struct fsg_buffhd *bh = req->context;
  35066. +
  35067. + if (req->status || req->actual != req->length)
  35068. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35069. + req->status, req->actual, req->length);
  35070. + if (req->status == -ECONNRESET) // Request was cancelled
  35071. + usb_ep_fifo_flush(ep);
  35072. +
  35073. + /* Hold the lock while we update the request and buffer states */
  35074. + smp_wmb();
  35075. + spin_lock(&fsg->lock);
  35076. + bh->inreq_busy = 0;
  35077. + bh->state = BUF_STATE_EMPTY;
  35078. + wakeup_thread(fsg);
  35079. + spin_unlock(&fsg->lock);
  35080. +}
  35081. +
  35082. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  35083. +{
  35084. + struct fsg_dev *fsg = ep->driver_data;
  35085. + struct fsg_buffhd *bh = req->context;
  35086. +
  35087. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  35088. + if (req->status || req->actual != bh->bulk_out_intended_length)
  35089. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35090. + req->status, req->actual,
  35091. + bh->bulk_out_intended_length);
  35092. + if (req->status == -ECONNRESET) // Request was cancelled
  35093. + usb_ep_fifo_flush(ep);
  35094. +
  35095. + /* Hold the lock while we update the request and buffer states */
  35096. + smp_wmb();
  35097. + spin_lock(&fsg->lock);
  35098. + bh->outreq_busy = 0;
  35099. + bh->state = BUF_STATE_FULL;
  35100. + wakeup_thread(fsg);
  35101. + spin_unlock(&fsg->lock);
  35102. +}
  35103. +
  35104. +
  35105. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35106. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  35107. +{
  35108. + struct fsg_dev *fsg = ep->driver_data;
  35109. + struct fsg_buffhd *bh = req->context;
  35110. +
  35111. + if (req->status || req->actual != req->length)
  35112. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  35113. + req->status, req->actual, req->length);
  35114. + if (req->status == -ECONNRESET) // Request was cancelled
  35115. + usb_ep_fifo_flush(ep);
  35116. +
  35117. + /* Hold the lock while we update the request and buffer states */
  35118. + smp_wmb();
  35119. + spin_lock(&fsg->lock);
  35120. + fsg->intreq_busy = 0;
  35121. + bh->state = BUF_STATE_EMPTY;
  35122. + wakeup_thread(fsg);
  35123. + spin_unlock(&fsg->lock);
  35124. +}
  35125. +
  35126. +#else
  35127. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  35128. +{}
  35129. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35130. +
  35131. +
  35132. +/*-------------------------------------------------------------------------*/
  35133. +
  35134. +/* Ep0 class-specific handlers. These always run in_irq. */
  35135. +
  35136. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  35137. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  35138. +{
  35139. + struct usb_request *req = fsg->ep0req;
  35140. + static u8 cbi_reset_cmnd[6] = {
  35141. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  35142. +
  35143. + /* Error in command transfer? */
  35144. + if (req->status || req->length != req->actual ||
  35145. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  35146. +
  35147. + /* Not all controllers allow a protocol stall after
  35148. + * receiving control-out data, but we'll try anyway. */
  35149. + fsg_set_halt(fsg, fsg->ep0);
  35150. + return; // Wait for reset
  35151. + }
  35152. +
  35153. + /* Is it the special reset command? */
  35154. + if (req->actual >= sizeof cbi_reset_cmnd &&
  35155. + memcmp(req->buf, cbi_reset_cmnd,
  35156. + sizeof cbi_reset_cmnd) == 0) {
  35157. +
  35158. + /* Raise an exception to stop the current operation
  35159. + * and reinitialize our state. */
  35160. + DBG(fsg, "cbi reset request\n");
  35161. + raise_exception(fsg, FSG_STATE_RESET);
  35162. + return;
  35163. + }
  35164. +
  35165. + VDBG(fsg, "CB[I] accept device-specific command\n");
  35166. + spin_lock(&fsg->lock);
  35167. +
  35168. + /* Save the command for later */
  35169. + if (fsg->cbbuf_cmnd_size)
  35170. + WARNING(fsg, "CB[I] overwriting previous command\n");
  35171. + fsg->cbbuf_cmnd_size = req->actual;
  35172. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  35173. +
  35174. + wakeup_thread(fsg);
  35175. + spin_unlock(&fsg->lock);
  35176. +}
  35177. +
  35178. +#else
  35179. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  35180. +{}
  35181. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  35182. +
  35183. +
  35184. +static int class_setup_req(struct fsg_dev *fsg,
  35185. + const struct usb_ctrlrequest *ctrl)
  35186. +{
  35187. + struct usb_request *req = fsg->ep0req;
  35188. + int value = -EOPNOTSUPP;
  35189. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  35190. + u16 w_value = le16_to_cpu(ctrl->wValue);
  35191. + u16 w_length = le16_to_cpu(ctrl->wLength);
  35192. +
  35193. + if (!fsg->config)
  35194. + return value;
  35195. +
  35196. + /* Handle Bulk-only class-specific requests */
  35197. + if (transport_is_bbb()) {
  35198. + switch (ctrl->bRequest) {
  35199. +
  35200. + case US_BULK_RESET_REQUEST:
  35201. + if (ctrl->bRequestType != (USB_DIR_OUT |
  35202. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35203. + break;
  35204. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  35205. + value = -EDOM;
  35206. + break;
  35207. + }
  35208. +
  35209. + /* Raise an exception to stop the current operation
  35210. + * and reinitialize our state. */
  35211. + DBG(fsg, "bulk reset request\n");
  35212. + raise_exception(fsg, FSG_STATE_RESET);
  35213. + value = DELAYED_STATUS;
  35214. + break;
  35215. +
  35216. + case US_BULK_GET_MAX_LUN:
  35217. + if (ctrl->bRequestType != (USB_DIR_IN |
  35218. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35219. + break;
  35220. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  35221. + value = -EDOM;
  35222. + break;
  35223. + }
  35224. + VDBG(fsg, "get max LUN\n");
  35225. + *(u8 *) req->buf = fsg->nluns - 1;
  35226. + value = 1;
  35227. + break;
  35228. + }
  35229. + }
  35230. +
  35231. + /* Handle CBI class-specific requests */
  35232. + else {
  35233. + switch (ctrl->bRequest) {
  35234. +
  35235. + case USB_CBI_ADSC_REQUEST:
  35236. + if (ctrl->bRequestType != (USB_DIR_OUT |
  35237. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  35238. + break;
  35239. + if (w_index != 0 || w_value != 0) {
  35240. + value = -EDOM;
  35241. + break;
  35242. + }
  35243. + if (w_length > MAX_COMMAND_SIZE) {
  35244. + value = -EOVERFLOW;
  35245. + break;
  35246. + }
  35247. + value = w_length;
  35248. + fsg->ep0req->context = received_cbi_adsc;
  35249. + break;
  35250. + }
  35251. + }
  35252. +
  35253. + if (value == -EOPNOTSUPP)
  35254. + VDBG(fsg,
  35255. + "unknown class-specific control req "
  35256. + "%02x.%02x v%04x i%04x l%u\n",
  35257. + ctrl->bRequestType, ctrl->bRequest,
  35258. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  35259. + return value;
  35260. +}
  35261. +
  35262. +
  35263. +/*-------------------------------------------------------------------------*/
  35264. +
  35265. +/* Ep0 standard request handlers. These always run in_irq. */
  35266. +
  35267. +static int standard_setup_req(struct fsg_dev *fsg,
  35268. + const struct usb_ctrlrequest *ctrl)
  35269. +{
  35270. + struct usb_request *req = fsg->ep0req;
  35271. + int value = -EOPNOTSUPP;
  35272. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  35273. + u16 w_value = le16_to_cpu(ctrl->wValue);
  35274. +
  35275. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  35276. + * but config change events will also reconfigure hardware. */
  35277. + switch (ctrl->bRequest) {
  35278. +
  35279. + case USB_REQ_GET_DESCRIPTOR:
  35280. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  35281. + USB_RECIP_DEVICE))
  35282. + break;
  35283. + switch (w_value >> 8) {
  35284. +
  35285. + case USB_DT_DEVICE:
  35286. + VDBG(fsg, "get device descriptor\n");
  35287. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  35288. + value = sizeof device_desc;
  35289. + memcpy(req->buf, &device_desc, value);
  35290. + break;
  35291. + case USB_DT_DEVICE_QUALIFIER:
  35292. + VDBG(fsg, "get device qualifier\n");
  35293. + if (!gadget_is_dualspeed(fsg->gadget) ||
  35294. + fsg->gadget->speed == USB_SPEED_SUPER)
  35295. + break;
  35296. + /*
  35297. + * Assume ep0 uses the same maxpacket value for both
  35298. + * speeds
  35299. + */
  35300. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  35301. + value = sizeof dev_qualifier;
  35302. + memcpy(req->buf, &dev_qualifier, value);
  35303. + break;
  35304. +
  35305. + case USB_DT_OTHER_SPEED_CONFIG:
  35306. + VDBG(fsg, "get other-speed config descriptor\n");
  35307. + if (!gadget_is_dualspeed(fsg->gadget) ||
  35308. + fsg->gadget->speed == USB_SPEED_SUPER)
  35309. + break;
  35310. + goto get_config;
  35311. + case USB_DT_CONFIG:
  35312. + VDBG(fsg, "get configuration descriptor\n");
  35313. +get_config:
  35314. + value = populate_config_buf(fsg->gadget,
  35315. + req->buf,
  35316. + w_value >> 8,
  35317. + w_value & 0xff);
  35318. + break;
  35319. +
  35320. + case USB_DT_STRING:
  35321. + VDBG(fsg, "get string descriptor\n");
  35322. +
  35323. + /* wIndex == language code */
  35324. + value = usb_gadget_get_string(&fsg_stringtab,
  35325. + w_value & 0xff, req->buf);
  35326. + break;
  35327. +
  35328. + case USB_DT_BOS:
  35329. + VDBG(fsg, "get bos descriptor\n");
  35330. +
  35331. + if (gadget_is_superspeed(fsg->gadget))
  35332. + value = populate_bos(fsg, req->buf);
  35333. + break;
  35334. + }
  35335. +
  35336. + break;
  35337. +
  35338. + /* One config, two speeds */
  35339. + case USB_REQ_SET_CONFIGURATION:
  35340. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  35341. + USB_RECIP_DEVICE))
  35342. + break;
  35343. + VDBG(fsg, "set configuration\n");
  35344. + if (w_value == CONFIG_VALUE || w_value == 0) {
  35345. + fsg->new_config = w_value;
  35346. +
  35347. + /* Raise an exception to wipe out previous transaction
  35348. + * state (queued bufs, etc) and set the new config. */
  35349. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  35350. + value = DELAYED_STATUS;
  35351. + }
  35352. + break;
  35353. + case USB_REQ_GET_CONFIGURATION:
  35354. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  35355. + USB_RECIP_DEVICE))
  35356. + break;
  35357. + VDBG(fsg, "get configuration\n");
  35358. + *(u8 *) req->buf = fsg->config;
  35359. + value = 1;
  35360. + break;
  35361. +
  35362. + case USB_REQ_SET_INTERFACE:
  35363. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  35364. + USB_RECIP_INTERFACE))
  35365. + break;
  35366. + if (fsg->config && w_index == 0) {
  35367. +
  35368. + /* Raise an exception to wipe out previous transaction
  35369. + * state (queued bufs, etc) and install the new
  35370. + * interface altsetting. */
  35371. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  35372. + value = DELAYED_STATUS;
  35373. + }
  35374. + break;
  35375. + case USB_REQ_GET_INTERFACE:
  35376. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  35377. + USB_RECIP_INTERFACE))
  35378. + break;
  35379. + if (!fsg->config)
  35380. + break;
  35381. + if (w_index != 0) {
  35382. + value = -EDOM;
  35383. + break;
  35384. + }
  35385. + VDBG(fsg, "get interface\n");
  35386. + *(u8 *) req->buf = 0;
  35387. + value = 1;
  35388. + break;
  35389. +
  35390. + default:
  35391. + VDBG(fsg,
  35392. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  35393. + ctrl->bRequestType, ctrl->bRequest,
  35394. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  35395. + }
  35396. +
  35397. + return value;
  35398. +}
  35399. +
  35400. +
  35401. +static int fsg_setup(struct usb_gadget *gadget,
  35402. + const struct usb_ctrlrequest *ctrl)
  35403. +{
  35404. + struct fsg_dev *fsg = get_gadget_data(gadget);
  35405. + int rc;
  35406. + int w_length = le16_to_cpu(ctrl->wLength);
  35407. +
  35408. + ++fsg->ep0_req_tag; // Record arrival of a new request
  35409. + fsg->ep0req->context = NULL;
  35410. + fsg->ep0req->length = 0;
  35411. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  35412. +
  35413. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  35414. + rc = class_setup_req(fsg, ctrl);
  35415. + else
  35416. + rc = standard_setup_req(fsg, ctrl);
  35417. +
  35418. + /* Respond with data/status or defer until later? */
  35419. + if (rc >= 0 && rc != DELAYED_STATUS) {
  35420. + rc = min(rc, w_length);
  35421. + fsg->ep0req->length = rc;
  35422. + fsg->ep0req->zero = rc < w_length;
  35423. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  35424. + "ep0-in" : "ep0-out");
  35425. + rc = ep0_queue(fsg);
  35426. + }
  35427. +
  35428. + /* Device either stalls (rc < 0) or reports success */
  35429. + return rc;
  35430. +}
  35431. +
  35432. +
  35433. +/*-------------------------------------------------------------------------*/
  35434. +
  35435. +/* All the following routines run in process context */
  35436. +
  35437. +
  35438. +/* Use this for bulk or interrupt transfers, not ep0 */
  35439. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  35440. + struct usb_request *req, int *pbusy,
  35441. + enum fsg_buffer_state *state)
  35442. +{
  35443. + int rc;
  35444. +
  35445. + if (ep == fsg->bulk_in)
  35446. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  35447. + else if (ep == fsg->intr_in)
  35448. + dump_msg(fsg, "intr-in", req->buf, req->length);
  35449. +
  35450. + spin_lock_irq(&fsg->lock);
  35451. + *pbusy = 1;
  35452. + *state = BUF_STATE_BUSY;
  35453. + spin_unlock_irq(&fsg->lock);
  35454. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  35455. + if (rc != 0) {
  35456. + *pbusy = 0;
  35457. + *state = BUF_STATE_EMPTY;
  35458. +
  35459. + /* We can't do much more than wait for a reset */
  35460. +
  35461. + /* Note: currently the net2280 driver fails zero-length
  35462. + * submissions if DMA is enabled. */
  35463. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  35464. + req->length == 0))
  35465. + WARNING(fsg, "error in submission: %s --> %d\n",
  35466. + ep->name, rc);
  35467. + }
  35468. +}
  35469. +
  35470. +
  35471. +static int sleep_thread(struct fsg_dev *fsg)
  35472. +{
  35473. + int rc = 0;
  35474. +
  35475. + /* Wait until a signal arrives or we are woken up */
  35476. + for (;;) {
  35477. + try_to_freeze();
  35478. + set_current_state(TASK_INTERRUPTIBLE);
  35479. + if (signal_pending(current)) {
  35480. + rc = -EINTR;
  35481. + break;
  35482. + }
  35483. + if (fsg->thread_wakeup_needed)
  35484. + break;
  35485. + schedule();
  35486. + }
  35487. + __set_current_state(TASK_RUNNING);
  35488. + fsg->thread_wakeup_needed = 0;
  35489. + return rc;
  35490. +}
  35491. +
  35492. +
  35493. +/*-------------------------------------------------------------------------*/
  35494. +
  35495. +static int do_read(struct fsg_dev *fsg)
  35496. +{
  35497. + struct fsg_lun *curlun = fsg->curlun;
  35498. + u32 lba;
  35499. + struct fsg_buffhd *bh;
  35500. + int rc;
  35501. + u32 amount_left;
  35502. + loff_t file_offset, file_offset_tmp;
  35503. + unsigned int amount;
  35504. + ssize_t nread;
  35505. +
  35506. + /* Get the starting Logical Block Address and check that it's
  35507. + * not too big */
  35508. + if (fsg->cmnd[0] == READ_6)
  35509. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  35510. + else {
  35511. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  35512. +
  35513. + /* We allow DPO (Disable Page Out = don't save data in the
  35514. + * cache) and FUA (Force Unit Access = don't read from the
  35515. + * cache), but we don't implement them. */
  35516. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  35517. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  35518. + return -EINVAL;
  35519. + }
  35520. + }
  35521. + if (lba >= curlun->num_sectors) {
  35522. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  35523. + return -EINVAL;
  35524. + }
  35525. + file_offset = ((loff_t) lba) << curlun->blkbits;
  35526. +
  35527. + /* Carry out the file reads */
  35528. + amount_left = fsg->data_size_from_cmnd;
  35529. + if (unlikely(amount_left == 0))
  35530. + return -EIO; // No default reply
  35531. +
  35532. + for (;;) {
  35533. +
  35534. + /* Figure out how much we need to read:
  35535. + * Try to read the remaining amount.
  35536. + * But don't read more than the buffer size.
  35537. + * And don't try to read past the end of the file.
  35538. + */
  35539. + amount = min((unsigned int) amount_left, mod_data.buflen);
  35540. + amount = min((loff_t) amount,
  35541. + curlun->file_length - file_offset);
  35542. +
  35543. + /* Wait for the next buffer to become available */
  35544. + bh = fsg->next_buffhd_to_fill;
  35545. + while (bh->state != BUF_STATE_EMPTY) {
  35546. + rc = sleep_thread(fsg);
  35547. + if (rc)
  35548. + return rc;
  35549. + }
  35550. +
  35551. + /* If we were asked to read past the end of file,
  35552. + * end with an empty buffer. */
  35553. + if (amount == 0) {
  35554. + curlun->sense_data =
  35555. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  35556. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  35557. + curlun->info_valid = 1;
  35558. + bh->inreq->length = 0;
  35559. + bh->state = BUF_STATE_FULL;
  35560. + break;
  35561. + }
  35562. +
  35563. + /* Perform the read */
  35564. + file_offset_tmp = file_offset;
  35565. + nread = vfs_read(curlun->filp,
  35566. + (char __user *) bh->buf,
  35567. + amount, &file_offset_tmp);
  35568. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  35569. + (unsigned long long) file_offset,
  35570. + (int) nread);
  35571. + if (signal_pending(current))
  35572. + return -EINTR;
  35573. +
  35574. + if (nread < 0) {
  35575. + LDBG(curlun, "error in file read: %d\n",
  35576. + (int) nread);
  35577. + nread = 0;
  35578. + } else if (nread < amount) {
  35579. + LDBG(curlun, "partial file read: %d/%u\n",
  35580. + (int) nread, amount);
  35581. + nread = round_down(nread, curlun->blksize);
  35582. + }
  35583. + file_offset += nread;
  35584. + amount_left -= nread;
  35585. + fsg->residue -= nread;
  35586. +
  35587. + /* Except at the end of the transfer, nread will be
  35588. + * equal to the buffer size, which is divisible by the
  35589. + * bulk-in maxpacket size.
  35590. + */
  35591. + bh->inreq->length = nread;
  35592. + bh->state = BUF_STATE_FULL;
  35593. +
  35594. + /* If an error occurred, report it and its position */
  35595. + if (nread < amount) {
  35596. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  35597. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  35598. + curlun->info_valid = 1;
  35599. + break;
  35600. + }
  35601. +
  35602. + if (amount_left == 0)
  35603. + break; // No more left to read
  35604. +
  35605. + /* Send this buffer and go read some more */
  35606. + bh->inreq->zero = 0;
  35607. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  35608. + &bh->inreq_busy, &bh->state);
  35609. + fsg->next_buffhd_to_fill = bh->next;
  35610. + }
  35611. +
  35612. + return -EIO; // No default reply
  35613. +}
  35614. +
  35615. +
  35616. +/*-------------------------------------------------------------------------*/
  35617. +
  35618. +static int do_write(struct fsg_dev *fsg)
  35619. +{
  35620. + struct fsg_lun *curlun = fsg->curlun;
  35621. + u32 lba;
  35622. + struct fsg_buffhd *bh;
  35623. + int get_some_more;
  35624. + u32 amount_left_to_req, amount_left_to_write;
  35625. + loff_t usb_offset, file_offset, file_offset_tmp;
  35626. + unsigned int amount;
  35627. + ssize_t nwritten;
  35628. + int rc;
  35629. +
  35630. + if (curlun->ro) {
  35631. + curlun->sense_data = SS_WRITE_PROTECTED;
  35632. + return -EINVAL;
  35633. + }
  35634. + spin_lock(&curlun->filp->f_lock);
  35635. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  35636. + spin_unlock(&curlun->filp->f_lock);
  35637. +
  35638. + /* Get the starting Logical Block Address and check that it's
  35639. + * not too big */
  35640. + if (fsg->cmnd[0] == WRITE_6)
  35641. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  35642. + else {
  35643. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  35644. +
  35645. + /* We allow DPO (Disable Page Out = don't save data in the
  35646. + * cache) and FUA (Force Unit Access = write directly to the
  35647. + * medium). We don't implement DPO; we implement FUA by
  35648. + * performing synchronous output. */
  35649. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  35650. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  35651. + return -EINVAL;
  35652. + }
  35653. + /* FUA */
  35654. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  35655. + spin_lock(&curlun->filp->f_lock);
  35656. + curlun->filp->f_flags |= O_DSYNC;
  35657. + spin_unlock(&curlun->filp->f_lock);
  35658. + }
  35659. + }
  35660. + if (lba >= curlun->num_sectors) {
  35661. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  35662. + return -EINVAL;
  35663. + }
  35664. +
  35665. + /* Carry out the file writes */
  35666. + get_some_more = 1;
  35667. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  35668. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  35669. +
  35670. + while (amount_left_to_write > 0) {
  35671. +
  35672. + /* Queue a request for more data from the host */
  35673. + bh = fsg->next_buffhd_to_fill;
  35674. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  35675. +
  35676. + /* Figure out how much we want to get:
  35677. + * Try to get the remaining amount,
  35678. + * but not more than the buffer size.
  35679. + */
  35680. + amount = min(amount_left_to_req, mod_data.buflen);
  35681. +
  35682. + /* Beyond the end of the backing file? */
  35683. + if (usb_offset >= curlun->file_length) {
  35684. + get_some_more = 0;
  35685. + curlun->sense_data =
  35686. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  35687. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  35688. + curlun->info_valid = 1;
  35689. + continue;
  35690. + }
  35691. +
  35692. + /* Get the next buffer */
  35693. + usb_offset += amount;
  35694. + fsg->usb_amount_left -= amount;
  35695. + amount_left_to_req -= amount;
  35696. + if (amount_left_to_req == 0)
  35697. + get_some_more = 0;
  35698. +
  35699. + /* Except at the end of the transfer, amount will be
  35700. + * equal to the buffer size, which is divisible by
  35701. + * the bulk-out maxpacket size.
  35702. + */
  35703. + set_bulk_out_req_length(fsg, bh, amount);
  35704. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  35705. + &bh->outreq_busy, &bh->state);
  35706. + fsg->next_buffhd_to_fill = bh->next;
  35707. + continue;
  35708. + }
  35709. +
  35710. + /* Write the received data to the backing file */
  35711. + bh = fsg->next_buffhd_to_drain;
  35712. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  35713. + break; // We stopped early
  35714. + if (bh->state == BUF_STATE_FULL) {
  35715. + smp_rmb();
  35716. + fsg->next_buffhd_to_drain = bh->next;
  35717. + bh->state = BUF_STATE_EMPTY;
  35718. +
  35719. + /* Did something go wrong with the transfer? */
  35720. + if (bh->outreq->status != 0) {
  35721. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  35722. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  35723. + curlun->info_valid = 1;
  35724. + break;
  35725. + }
  35726. +
  35727. + amount = bh->outreq->actual;
  35728. + if (curlun->file_length - file_offset < amount) {
  35729. + LERROR(curlun,
  35730. + "write %u @ %llu beyond end %llu\n",
  35731. + amount, (unsigned long long) file_offset,
  35732. + (unsigned long long) curlun->file_length);
  35733. + amount = curlun->file_length - file_offset;
  35734. + }
  35735. +
  35736. + /* Don't accept excess data. The spec doesn't say
  35737. + * what to do in this case. We'll ignore the error.
  35738. + */
  35739. + amount = min(amount, bh->bulk_out_intended_length);
  35740. +
  35741. + /* Don't write a partial block */
  35742. + amount = round_down(amount, curlun->blksize);
  35743. + if (amount == 0)
  35744. + goto empty_write;
  35745. +
  35746. + /* Perform the write */
  35747. + file_offset_tmp = file_offset;
  35748. + nwritten = vfs_write(curlun->filp,
  35749. + (char __user *) bh->buf,
  35750. + amount, &file_offset_tmp);
  35751. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  35752. + (unsigned long long) file_offset,
  35753. + (int) nwritten);
  35754. + if (signal_pending(current))
  35755. + return -EINTR; // Interrupted!
  35756. +
  35757. + if (nwritten < 0) {
  35758. + LDBG(curlun, "error in file write: %d\n",
  35759. + (int) nwritten);
  35760. + nwritten = 0;
  35761. + } else if (nwritten < amount) {
  35762. + LDBG(curlun, "partial file write: %d/%u\n",
  35763. + (int) nwritten, amount);
  35764. + nwritten = round_down(nwritten, curlun->blksize);
  35765. + }
  35766. + file_offset += nwritten;
  35767. + amount_left_to_write -= nwritten;
  35768. + fsg->residue -= nwritten;
  35769. +
  35770. + /* If an error occurred, report it and its position */
  35771. + if (nwritten < amount) {
  35772. + curlun->sense_data = SS_WRITE_ERROR;
  35773. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  35774. + curlun->info_valid = 1;
  35775. + break;
  35776. + }
  35777. +
  35778. + empty_write:
  35779. + /* Did the host decide to stop early? */
  35780. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  35781. + fsg->short_packet_received = 1;
  35782. + break;
  35783. + }
  35784. + continue;
  35785. + }
  35786. +
  35787. + /* Wait for something to happen */
  35788. + rc = sleep_thread(fsg);
  35789. + if (rc)
  35790. + return rc;
  35791. + }
  35792. +
  35793. + return -EIO; // No default reply
  35794. +}
  35795. +
  35796. +
  35797. +/*-------------------------------------------------------------------------*/
  35798. +
  35799. +static int do_synchronize_cache(struct fsg_dev *fsg)
  35800. +{
  35801. + struct fsg_lun *curlun = fsg->curlun;
  35802. + int rc;
  35803. +
  35804. + /* We ignore the requested LBA and write out all file's
  35805. + * dirty data buffers. */
  35806. + rc = fsg_lun_fsync_sub(curlun);
  35807. + if (rc)
  35808. + curlun->sense_data = SS_WRITE_ERROR;
  35809. + return 0;
  35810. +}
  35811. +
  35812. +
  35813. +/*-------------------------------------------------------------------------*/
  35814. +
  35815. +static void invalidate_sub(struct fsg_lun *curlun)
  35816. +{
  35817. + struct file *filp = curlun->filp;
  35818. + struct inode *inode = filp->f_path.dentry->d_inode;
  35819. + unsigned long rc;
  35820. +
  35821. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  35822. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  35823. +}
  35824. +
  35825. +static int do_verify(struct fsg_dev *fsg)
  35826. +{
  35827. + struct fsg_lun *curlun = fsg->curlun;
  35828. + u32 lba;
  35829. + u32 verification_length;
  35830. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  35831. + loff_t file_offset, file_offset_tmp;
  35832. + u32 amount_left;
  35833. + unsigned int amount;
  35834. + ssize_t nread;
  35835. +
  35836. + /* Get the starting Logical Block Address and check that it's
  35837. + * not too big */
  35838. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  35839. + if (lba >= curlun->num_sectors) {
  35840. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  35841. + return -EINVAL;
  35842. + }
  35843. +
  35844. + /* We allow DPO (Disable Page Out = don't save data in the
  35845. + * cache) but we don't implement it. */
  35846. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  35847. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  35848. + return -EINVAL;
  35849. + }
  35850. +
  35851. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  35852. + if (unlikely(verification_length == 0))
  35853. + return -EIO; // No default reply
  35854. +
  35855. + /* Prepare to carry out the file verify */
  35856. + amount_left = verification_length << curlun->blkbits;
  35857. + file_offset = ((loff_t) lba) << curlun->blkbits;
  35858. +
  35859. + /* Write out all the dirty buffers before invalidating them */
  35860. + fsg_lun_fsync_sub(curlun);
  35861. + if (signal_pending(current))
  35862. + return -EINTR;
  35863. +
  35864. + invalidate_sub(curlun);
  35865. + if (signal_pending(current))
  35866. + return -EINTR;
  35867. +
  35868. + /* Just try to read the requested blocks */
  35869. + while (amount_left > 0) {
  35870. +
  35871. + /* Figure out how much we need to read:
  35872. + * Try to read the remaining amount, but not more than
  35873. + * the buffer size.
  35874. + * And don't try to read past the end of the file.
  35875. + */
  35876. + amount = min((unsigned int) amount_left, mod_data.buflen);
  35877. + amount = min((loff_t) amount,
  35878. + curlun->file_length - file_offset);
  35879. + if (amount == 0) {
  35880. + curlun->sense_data =
  35881. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  35882. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  35883. + curlun->info_valid = 1;
  35884. + break;
  35885. + }
  35886. +
  35887. + /* Perform the read */
  35888. + file_offset_tmp = file_offset;
  35889. + nread = vfs_read(curlun->filp,
  35890. + (char __user *) bh->buf,
  35891. + amount, &file_offset_tmp);
  35892. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  35893. + (unsigned long long) file_offset,
  35894. + (int) nread);
  35895. + if (signal_pending(current))
  35896. + return -EINTR;
  35897. +
  35898. + if (nread < 0) {
  35899. + LDBG(curlun, "error in file verify: %d\n",
  35900. + (int) nread);
  35901. + nread = 0;
  35902. + } else if (nread < amount) {
  35903. + LDBG(curlun, "partial file verify: %d/%u\n",
  35904. + (int) nread, amount);
  35905. + nread = round_down(nread, curlun->blksize);
  35906. + }
  35907. + if (nread == 0) {
  35908. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  35909. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  35910. + curlun->info_valid = 1;
  35911. + break;
  35912. + }
  35913. + file_offset += nread;
  35914. + amount_left -= nread;
  35915. + }
  35916. + return 0;
  35917. +}
  35918. +
  35919. +
  35920. +/*-------------------------------------------------------------------------*/
  35921. +
  35922. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  35923. +{
  35924. + u8 *buf = (u8 *) bh->buf;
  35925. +
  35926. + static char vendor_id[] = "Linux ";
  35927. + static char product_disk_id[] = "File-Stor Gadget";
  35928. + static char product_cdrom_id[] = "File-CD Gadget ";
  35929. +
  35930. + if (!fsg->curlun) { // Unsupported LUNs are okay
  35931. + fsg->bad_lun_okay = 1;
  35932. + memset(buf, 0, 36);
  35933. + buf[0] = 0x7f; // Unsupported, no device-type
  35934. + buf[4] = 31; // Additional length
  35935. + return 36;
  35936. + }
  35937. +
  35938. + memset(buf, 0, 8);
  35939. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  35940. + if (mod_data.removable)
  35941. + buf[1] = 0x80;
  35942. + buf[2] = 2; // ANSI SCSI level 2
  35943. + buf[3] = 2; // SCSI-2 INQUIRY data format
  35944. + buf[4] = 31; // Additional length
  35945. + // No special options
  35946. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  35947. + (mod_data.cdrom ? product_cdrom_id :
  35948. + product_disk_id),
  35949. + mod_data.release);
  35950. + return 36;
  35951. +}
  35952. +
  35953. +
  35954. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  35955. +{
  35956. + struct fsg_lun *curlun = fsg->curlun;
  35957. + u8 *buf = (u8 *) bh->buf;
  35958. + u32 sd, sdinfo;
  35959. + int valid;
  35960. +
  35961. + /*
  35962. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  35963. + *
  35964. + * If a REQUEST SENSE command is received from an initiator
  35965. + * with a pending unit attention condition (before the target
  35966. + * generates the contingent allegiance condition), then the
  35967. + * target shall either:
  35968. + * a) report any pending sense data and preserve the unit
  35969. + * attention condition on the logical unit, or,
  35970. + * b) report the unit attention condition, may discard any
  35971. + * pending sense data, and clear the unit attention
  35972. + * condition on the logical unit for that initiator.
  35973. + *
  35974. + * FSG normally uses option a); enable this code to use option b).
  35975. + */
  35976. +#if 0
  35977. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  35978. + curlun->sense_data = curlun->unit_attention_data;
  35979. + curlun->unit_attention_data = SS_NO_SENSE;
  35980. + }
  35981. +#endif
  35982. +
  35983. + if (!curlun) { // Unsupported LUNs are okay
  35984. + fsg->bad_lun_okay = 1;
  35985. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  35986. + sdinfo = 0;
  35987. + valid = 0;
  35988. + } else {
  35989. + sd = curlun->sense_data;
  35990. + sdinfo = curlun->sense_data_info;
  35991. + valid = curlun->info_valid << 7;
  35992. + curlun->sense_data = SS_NO_SENSE;
  35993. + curlun->sense_data_info = 0;
  35994. + curlun->info_valid = 0;
  35995. + }
  35996. +
  35997. + memset(buf, 0, 18);
  35998. + buf[0] = valid | 0x70; // Valid, current error
  35999. + buf[2] = SK(sd);
  36000. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  36001. + buf[7] = 18 - 8; // Additional sense length
  36002. + buf[12] = ASC(sd);
  36003. + buf[13] = ASCQ(sd);
  36004. + return 18;
  36005. +}
  36006. +
  36007. +
  36008. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36009. +{
  36010. + struct fsg_lun *curlun = fsg->curlun;
  36011. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  36012. + int pmi = fsg->cmnd[8];
  36013. + u8 *buf = (u8 *) bh->buf;
  36014. +
  36015. + /* Check the PMI and LBA fields */
  36016. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  36017. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36018. + return -EINVAL;
  36019. + }
  36020. +
  36021. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  36022. + /* Max logical block */
  36023. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  36024. + return 8;
  36025. +}
  36026. +
  36027. +
  36028. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36029. +{
  36030. + struct fsg_lun *curlun = fsg->curlun;
  36031. + int msf = fsg->cmnd[1] & 0x02;
  36032. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  36033. + u8 *buf = (u8 *) bh->buf;
  36034. +
  36035. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  36036. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36037. + return -EINVAL;
  36038. + }
  36039. + if (lba >= curlun->num_sectors) {
  36040. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  36041. + return -EINVAL;
  36042. + }
  36043. +
  36044. + memset(buf, 0, 8);
  36045. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  36046. + store_cdrom_address(&buf[4], msf, lba);
  36047. + return 8;
  36048. +}
  36049. +
  36050. +
  36051. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36052. +{
  36053. + struct fsg_lun *curlun = fsg->curlun;
  36054. + int msf = fsg->cmnd[1] & 0x02;
  36055. + int start_track = fsg->cmnd[6];
  36056. + u8 *buf = (u8 *) bh->buf;
  36057. +
  36058. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  36059. + start_track > 1) {
  36060. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36061. + return -EINVAL;
  36062. + }
  36063. +
  36064. + memset(buf, 0, 20);
  36065. + buf[1] = (20-2); /* TOC data length */
  36066. + buf[2] = 1; /* First track number */
  36067. + buf[3] = 1; /* Last track number */
  36068. + buf[5] = 0x16; /* Data track, copying allowed */
  36069. + buf[6] = 0x01; /* Only track is number 1 */
  36070. + store_cdrom_address(&buf[8], msf, 0);
  36071. +
  36072. + buf[13] = 0x16; /* Lead-out track is data */
  36073. + buf[14] = 0xAA; /* Lead-out track number */
  36074. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  36075. + return 20;
  36076. +}
  36077. +
  36078. +
  36079. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36080. +{
  36081. + struct fsg_lun *curlun = fsg->curlun;
  36082. + int mscmnd = fsg->cmnd[0];
  36083. + u8 *buf = (u8 *) bh->buf;
  36084. + u8 *buf0 = buf;
  36085. + int pc, page_code;
  36086. + int changeable_values, all_pages;
  36087. + int valid_page = 0;
  36088. + int len, limit;
  36089. +
  36090. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  36091. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36092. + return -EINVAL;
  36093. + }
  36094. + pc = fsg->cmnd[2] >> 6;
  36095. + page_code = fsg->cmnd[2] & 0x3f;
  36096. + if (pc == 3) {
  36097. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  36098. + return -EINVAL;
  36099. + }
  36100. + changeable_values = (pc == 1);
  36101. + all_pages = (page_code == 0x3f);
  36102. +
  36103. + /* Write the mode parameter header. Fixed values are: default
  36104. + * medium type, no cache control (DPOFUA), and no block descriptors.
  36105. + * The only variable value is the WriteProtect bit. We will fill in
  36106. + * the mode data length later. */
  36107. + memset(buf, 0, 8);
  36108. + if (mscmnd == MODE_SENSE) {
  36109. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  36110. + buf += 4;
  36111. + limit = 255;
  36112. + } else { // MODE_SENSE_10
  36113. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  36114. + buf += 8;
  36115. + limit = 65535; // Should really be mod_data.buflen
  36116. + }
  36117. +
  36118. + /* No block descriptors */
  36119. +
  36120. + /* The mode pages, in numerical order. The only page we support
  36121. + * is the Caching page. */
  36122. + if (page_code == 0x08 || all_pages) {
  36123. + valid_page = 1;
  36124. + buf[0] = 0x08; // Page code
  36125. + buf[1] = 10; // Page length
  36126. + memset(buf+2, 0, 10); // None of the fields are changeable
  36127. +
  36128. + if (!changeable_values) {
  36129. + buf[2] = 0x04; // Write cache enable,
  36130. + // Read cache not disabled
  36131. + // No cache retention priorities
  36132. + put_unaligned_be16(0xffff, &buf[4]);
  36133. + /* Don't disable prefetch */
  36134. + /* Minimum prefetch = 0 */
  36135. + put_unaligned_be16(0xffff, &buf[8]);
  36136. + /* Maximum prefetch */
  36137. + put_unaligned_be16(0xffff, &buf[10]);
  36138. + /* Maximum prefetch ceiling */
  36139. + }
  36140. + buf += 12;
  36141. + }
  36142. +
  36143. + /* Check that a valid page was requested and the mode data length
  36144. + * isn't too long. */
  36145. + len = buf - buf0;
  36146. + if (!valid_page || len > limit) {
  36147. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36148. + return -EINVAL;
  36149. + }
  36150. +
  36151. + /* Store the mode data length */
  36152. + if (mscmnd == MODE_SENSE)
  36153. + buf0[0] = len - 1;
  36154. + else
  36155. + put_unaligned_be16(len - 2, buf0);
  36156. + return len;
  36157. +}
  36158. +
  36159. +
  36160. +static int do_start_stop(struct fsg_dev *fsg)
  36161. +{
  36162. + struct fsg_lun *curlun = fsg->curlun;
  36163. + int loej, start;
  36164. +
  36165. + if (!mod_data.removable) {
  36166. + curlun->sense_data = SS_INVALID_COMMAND;
  36167. + return -EINVAL;
  36168. + }
  36169. +
  36170. + // int immed = fsg->cmnd[1] & 0x01;
  36171. + loej = fsg->cmnd[4] & 0x02;
  36172. + start = fsg->cmnd[4] & 0x01;
  36173. +
  36174. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  36175. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  36176. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  36177. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36178. + return -EINVAL;
  36179. + }
  36180. +
  36181. + if (!start) {
  36182. +
  36183. + /* Are we allowed to unload the media? */
  36184. + if (curlun->prevent_medium_removal) {
  36185. + LDBG(curlun, "unload attempt prevented\n");
  36186. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  36187. + return -EINVAL;
  36188. + }
  36189. + if (loej) { // Simulate an unload/eject
  36190. + up_read(&fsg->filesem);
  36191. + down_write(&fsg->filesem);
  36192. + fsg_lun_close(curlun);
  36193. + up_write(&fsg->filesem);
  36194. + down_read(&fsg->filesem);
  36195. + }
  36196. + } else {
  36197. +
  36198. + /* Our emulation doesn't support mounting; the medium is
  36199. + * available for use as soon as it is loaded. */
  36200. + if (!fsg_lun_is_open(curlun)) {
  36201. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  36202. + return -EINVAL;
  36203. + }
  36204. + }
  36205. +#endif
  36206. + return 0;
  36207. +}
  36208. +
  36209. +
  36210. +static int do_prevent_allow(struct fsg_dev *fsg)
  36211. +{
  36212. + struct fsg_lun *curlun = fsg->curlun;
  36213. + int prevent;
  36214. +
  36215. + if (!mod_data.removable) {
  36216. + curlun->sense_data = SS_INVALID_COMMAND;
  36217. + return -EINVAL;
  36218. + }
  36219. +
  36220. + prevent = fsg->cmnd[4] & 0x01;
  36221. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  36222. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36223. + return -EINVAL;
  36224. + }
  36225. +
  36226. + if (curlun->prevent_medium_removal && !prevent)
  36227. + fsg_lun_fsync_sub(curlun);
  36228. + curlun->prevent_medium_removal = prevent;
  36229. + return 0;
  36230. +}
  36231. +
  36232. +
  36233. +static int do_read_format_capacities(struct fsg_dev *fsg,
  36234. + struct fsg_buffhd *bh)
  36235. +{
  36236. + struct fsg_lun *curlun = fsg->curlun;
  36237. + u8 *buf = (u8 *) bh->buf;
  36238. +
  36239. + buf[0] = buf[1] = buf[2] = 0;
  36240. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  36241. + buf += 4;
  36242. +
  36243. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  36244. + /* Number of blocks */
  36245. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  36246. + buf[4] = 0x02; /* Current capacity */
  36247. + return 12;
  36248. +}
  36249. +
  36250. +
  36251. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36252. +{
  36253. + struct fsg_lun *curlun = fsg->curlun;
  36254. +
  36255. + /* We don't support MODE SELECT */
  36256. + curlun->sense_data = SS_INVALID_COMMAND;
  36257. + return -EINVAL;
  36258. +}
  36259. +
  36260. +
  36261. +/*-------------------------------------------------------------------------*/
  36262. +
  36263. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  36264. +{
  36265. + int rc;
  36266. +
  36267. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  36268. + if (rc == -EAGAIN)
  36269. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  36270. + while (rc != 0) {
  36271. + if (rc != -EAGAIN) {
  36272. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  36273. + rc = 0;
  36274. + break;
  36275. + }
  36276. +
  36277. + /* Wait for a short time and then try again */
  36278. + if (msleep_interruptible(100) != 0)
  36279. + return -EINTR;
  36280. + rc = usb_ep_set_halt(fsg->bulk_in);
  36281. + }
  36282. + return rc;
  36283. +}
  36284. +
  36285. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  36286. +{
  36287. + int rc;
  36288. +
  36289. + DBG(fsg, "bulk-in set wedge\n");
  36290. + rc = usb_ep_set_wedge(fsg->bulk_in);
  36291. + if (rc == -EAGAIN)
  36292. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  36293. + while (rc != 0) {
  36294. + if (rc != -EAGAIN) {
  36295. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  36296. + rc = 0;
  36297. + break;
  36298. + }
  36299. +
  36300. + /* Wait for a short time and then try again */
  36301. + if (msleep_interruptible(100) != 0)
  36302. + return -EINTR;
  36303. + rc = usb_ep_set_wedge(fsg->bulk_in);
  36304. + }
  36305. + return rc;
  36306. +}
  36307. +
  36308. +static int throw_away_data(struct fsg_dev *fsg)
  36309. +{
  36310. + struct fsg_buffhd *bh;
  36311. + u32 amount;
  36312. + int rc;
  36313. +
  36314. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  36315. + fsg->usb_amount_left > 0) {
  36316. +
  36317. + /* Throw away the data in a filled buffer */
  36318. + if (bh->state == BUF_STATE_FULL) {
  36319. + smp_rmb();
  36320. + bh->state = BUF_STATE_EMPTY;
  36321. + fsg->next_buffhd_to_drain = bh->next;
  36322. +
  36323. + /* A short packet or an error ends everything */
  36324. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  36325. + bh->outreq->status != 0) {
  36326. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  36327. + return -EINTR;
  36328. + }
  36329. + continue;
  36330. + }
  36331. +
  36332. + /* Try to submit another request if we need one */
  36333. + bh = fsg->next_buffhd_to_fill;
  36334. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  36335. + amount = min(fsg->usb_amount_left,
  36336. + (u32) mod_data.buflen);
  36337. +
  36338. + /* Except at the end of the transfer, amount will be
  36339. + * equal to the buffer size, which is divisible by
  36340. + * the bulk-out maxpacket size.
  36341. + */
  36342. + set_bulk_out_req_length(fsg, bh, amount);
  36343. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  36344. + &bh->outreq_busy, &bh->state);
  36345. + fsg->next_buffhd_to_fill = bh->next;
  36346. + fsg->usb_amount_left -= amount;
  36347. + continue;
  36348. + }
  36349. +
  36350. + /* Otherwise wait for something to happen */
  36351. + rc = sleep_thread(fsg);
  36352. + if (rc)
  36353. + return rc;
  36354. + }
  36355. + return 0;
  36356. +}
  36357. +
  36358. +
  36359. +static int finish_reply(struct fsg_dev *fsg)
  36360. +{
  36361. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  36362. + int rc = 0;
  36363. +
  36364. + switch (fsg->data_dir) {
  36365. + case DATA_DIR_NONE:
  36366. + break; // Nothing to send
  36367. +
  36368. + /* If we don't know whether the host wants to read or write,
  36369. + * this must be CB or CBI with an unknown command. We mustn't
  36370. + * try to send or receive any data. So stall both bulk pipes
  36371. + * if we can and wait for a reset. */
  36372. + case DATA_DIR_UNKNOWN:
  36373. + if (mod_data.can_stall) {
  36374. + fsg_set_halt(fsg, fsg->bulk_out);
  36375. + rc = halt_bulk_in_endpoint(fsg);
  36376. + }
  36377. + break;
  36378. +
  36379. + /* All but the last buffer of data must have already been sent */
  36380. + case DATA_DIR_TO_HOST:
  36381. + if (fsg->data_size == 0)
  36382. + ; // Nothing to send
  36383. +
  36384. + /* If there's no residue, simply send the last buffer */
  36385. + else if (fsg->residue == 0) {
  36386. + bh->inreq->zero = 0;
  36387. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  36388. + &bh->inreq_busy, &bh->state);
  36389. + fsg->next_buffhd_to_fill = bh->next;
  36390. + }
  36391. +
  36392. + /* There is a residue. For CB and CBI, simply mark the end
  36393. + * of the data with a short packet. However, if we are
  36394. + * allowed to stall, there was no data at all (residue ==
  36395. + * data_size), and the command failed (invalid LUN or
  36396. + * sense data is set), then halt the bulk-in endpoint
  36397. + * instead. */
  36398. + else if (!transport_is_bbb()) {
  36399. + if (mod_data.can_stall &&
  36400. + fsg->residue == fsg->data_size &&
  36401. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  36402. + bh->state = BUF_STATE_EMPTY;
  36403. + rc = halt_bulk_in_endpoint(fsg);
  36404. + } else {
  36405. + bh->inreq->zero = 1;
  36406. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  36407. + &bh->inreq_busy, &bh->state);
  36408. + fsg->next_buffhd_to_fill = bh->next;
  36409. + }
  36410. + }
  36411. +
  36412. + /*
  36413. + * For Bulk-only, mark the end of the data with a short
  36414. + * packet. If we are allowed to stall, halt the bulk-in
  36415. + * endpoint. (Note: This violates the Bulk-Only Transport
  36416. + * specification, which requires us to pad the data if we
  36417. + * don't halt the endpoint. Presumably nobody will mind.)
  36418. + */
  36419. + else {
  36420. + bh->inreq->zero = 1;
  36421. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  36422. + &bh->inreq_busy, &bh->state);
  36423. + fsg->next_buffhd_to_fill = bh->next;
  36424. + if (mod_data.can_stall)
  36425. + rc = halt_bulk_in_endpoint(fsg);
  36426. + }
  36427. + break;
  36428. +
  36429. + /* We have processed all we want from the data the host has sent.
  36430. + * There may still be outstanding bulk-out requests. */
  36431. + case DATA_DIR_FROM_HOST:
  36432. + if (fsg->residue == 0)
  36433. + ; // Nothing to receive
  36434. +
  36435. + /* Did the host stop sending unexpectedly early? */
  36436. + else if (fsg->short_packet_received) {
  36437. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  36438. + rc = -EINTR;
  36439. + }
  36440. +
  36441. + /* We haven't processed all the incoming data. Even though
  36442. + * we may be allowed to stall, doing so would cause a race.
  36443. + * The controller may already have ACK'ed all the remaining
  36444. + * bulk-out packets, in which case the host wouldn't see a
  36445. + * STALL. Not realizing the endpoint was halted, it wouldn't
  36446. + * clear the halt -- leading to problems later on. */
  36447. +#if 0
  36448. + else if (mod_data.can_stall) {
  36449. + fsg_set_halt(fsg, fsg->bulk_out);
  36450. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  36451. + rc = -EINTR;
  36452. + }
  36453. +#endif
  36454. +
  36455. + /* We can't stall. Read in the excess data and throw it
  36456. + * all away. */
  36457. + else
  36458. + rc = throw_away_data(fsg);
  36459. + break;
  36460. + }
  36461. + return rc;
  36462. +}
  36463. +
  36464. +
  36465. +static int send_status(struct fsg_dev *fsg)
  36466. +{
  36467. + struct fsg_lun *curlun = fsg->curlun;
  36468. + struct fsg_buffhd *bh;
  36469. + int rc;
  36470. + u8 status = US_BULK_STAT_OK;
  36471. + u32 sd, sdinfo = 0;
  36472. +
  36473. + /* Wait for the next buffer to become available */
  36474. + bh = fsg->next_buffhd_to_fill;
  36475. + while (bh->state != BUF_STATE_EMPTY) {
  36476. + rc = sleep_thread(fsg);
  36477. + if (rc)
  36478. + return rc;
  36479. + }
  36480. +
  36481. + if (curlun) {
  36482. + sd = curlun->sense_data;
  36483. + sdinfo = curlun->sense_data_info;
  36484. + } else if (fsg->bad_lun_okay)
  36485. + sd = SS_NO_SENSE;
  36486. + else
  36487. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  36488. +
  36489. + if (fsg->phase_error) {
  36490. + DBG(fsg, "sending phase-error status\n");
  36491. + status = US_BULK_STAT_PHASE;
  36492. + sd = SS_INVALID_COMMAND;
  36493. + } else if (sd != SS_NO_SENSE) {
  36494. + DBG(fsg, "sending command-failure status\n");
  36495. + status = US_BULK_STAT_FAIL;
  36496. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  36497. + " info x%x\n",
  36498. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  36499. + }
  36500. +
  36501. + if (transport_is_bbb()) {
  36502. + struct bulk_cs_wrap *csw = bh->buf;
  36503. +
  36504. + /* Store and send the Bulk-only CSW */
  36505. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  36506. + csw->Tag = fsg->tag;
  36507. + csw->Residue = cpu_to_le32(fsg->residue);
  36508. + csw->Status = status;
  36509. +
  36510. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  36511. + bh->inreq->zero = 0;
  36512. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  36513. + &bh->inreq_busy, &bh->state);
  36514. +
  36515. + } else if (mod_data.transport_type == USB_PR_CB) {
  36516. +
  36517. + /* Control-Bulk transport has no status phase! */
  36518. + return 0;
  36519. +
  36520. + } else { // USB_PR_CBI
  36521. + struct interrupt_data *buf = bh->buf;
  36522. +
  36523. + /* Store and send the Interrupt data. UFI sends the ASC
  36524. + * and ASCQ bytes. Everything else sends a Type (which
  36525. + * is always 0) and the status Value. */
  36526. + if (mod_data.protocol_type == USB_SC_UFI) {
  36527. + buf->bType = ASC(sd);
  36528. + buf->bValue = ASCQ(sd);
  36529. + } else {
  36530. + buf->bType = 0;
  36531. + buf->bValue = status;
  36532. + }
  36533. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  36534. +
  36535. + fsg->intr_buffhd = bh; // Point to the right buffhd
  36536. + fsg->intreq->buf = bh->inreq->buf;
  36537. + fsg->intreq->context = bh;
  36538. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  36539. + &fsg->intreq_busy, &bh->state);
  36540. + }
  36541. +
  36542. + fsg->next_buffhd_to_fill = bh->next;
  36543. + return 0;
  36544. +}
  36545. +
  36546. +
  36547. +/*-------------------------------------------------------------------------*/
  36548. +
  36549. +/* Check whether the command is properly formed and whether its data size
  36550. + * and direction agree with the values we already have. */
  36551. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  36552. + enum data_direction data_dir, unsigned int mask,
  36553. + int needs_medium, const char *name)
  36554. +{
  36555. + int i;
  36556. + int lun = fsg->cmnd[1] >> 5;
  36557. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  36558. + char hdlen[20];
  36559. + struct fsg_lun *curlun;
  36560. +
  36561. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  36562. + * Transparent SCSI doesn't pad. */
  36563. + if (protocol_is_scsi())
  36564. + ;
  36565. +
  36566. + /* There's some disagreement as to whether RBC pads commands or not.
  36567. + * We'll play it safe and accept either form. */
  36568. + else if (mod_data.protocol_type == USB_SC_RBC) {
  36569. + if (fsg->cmnd_size == 12)
  36570. + cmnd_size = 12;
  36571. +
  36572. + /* All the other protocols pad to 12 bytes */
  36573. + } else
  36574. + cmnd_size = 12;
  36575. +
  36576. + hdlen[0] = 0;
  36577. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  36578. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  36579. + fsg->data_size);
  36580. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  36581. + name, cmnd_size, dirletter[(int) data_dir],
  36582. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  36583. +
  36584. + /* We can't reply at all until we know the correct data direction
  36585. + * and size. */
  36586. + if (fsg->data_size_from_cmnd == 0)
  36587. + data_dir = DATA_DIR_NONE;
  36588. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  36589. + fsg->data_dir = data_dir;
  36590. + fsg->data_size = fsg->data_size_from_cmnd;
  36591. +
  36592. + } else { // Bulk-only
  36593. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  36594. +
  36595. + /* Host data size < Device data size is a phase error.
  36596. + * Carry out the command, but only transfer as much
  36597. + * as we are allowed. */
  36598. + fsg->data_size_from_cmnd = fsg->data_size;
  36599. + fsg->phase_error = 1;
  36600. + }
  36601. + }
  36602. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  36603. +
  36604. + /* Conflicting data directions is a phase error */
  36605. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  36606. + fsg->phase_error = 1;
  36607. + return -EINVAL;
  36608. + }
  36609. +
  36610. + /* Verify the length of the command itself */
  36611. + if (cmnd_size != fsg->cmnd_size) {
  36612. +
  36613. + /* Special case workaround: There are plenty of buggy SCSI
  36614. + * implementations. Many have issues with cbw->Length
  36615. + * field passing a wrong command size. For those cases we
  36616. + * always try to work around the problem by using the length
  36617. + * sent by the host side provided it is at least as large
  36618. + * as the correct command length.
  36619. + * Examples of such cases would be MS-Windows, which issues
  36620. + * REQUEST SENSE with cbw->Length == 12 where it should
  36621. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  36622. + * REQUEST SENSE with cbw->Length == 10 where it should
  36623. + * be 6 as well.
  36624. + */
  36625. + if (cmnd_size <= fsg->cmnd_size) {
  36626. + DBG(fsg, "%s is buggy! Expected length %d "
  36627. + "but we got %d\n", name,
  36628. + cmnd_size, fsg->cmnd_size);
  36629. + cmnd_size = fsg->cmnd_size;
  36630. + } else {
  36631. + fsg->phase_error = 1;
  36632. + return -EINVAL;
  36633. + }
  36634. + }
  36635. +
  36636. + /* Check that the LUN values are consistent */
  36637. + if (transport_is_bbb()) {
  36638. + if (fsg->lun != lun)
  36639. + DBG(fsg, "using LUN %d from CBW, "
  36640. + "not LUN %d from CDB\n",
  36641. + fsg->lun, lun);
  36642. + }
  36643. +
  36644. + /* Check the LUN */
  36645. + curlun = fsg->curlun;
  36646. + if (curlun) {
  36647. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  36648. + curlun->sense_data = SS_NO_SENSE;
  36649. + curlun->sense_data_info = 0;
  36650. + curlun->info_valid = 0;
  36651. + }
  36652. + } else {
  36653. + fsg->bad_lun_okay = 0;
  36654. +
  36655. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  36656. + * to use unsupported LUNs; all others may not. */
  36657. + if (fsg->cmnd[0] != INQUIRY &&
  36658. + fsg->cmnd[0] != REQUEST_SENSE) {
  36659. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  36660. + return -EINVAL;
  36661. + }
  36662. + }
  36663. +
  36664. + /* If a unit attention condition exists, only INQUIRY and
  36665. + * REQUEST SENSE commands are allowed; anything else must fail. */
  36666. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  36667. + fsg->cmnd[0] != INQUIRY &&
  36668. + fsg->cmnd[0] != REQUEST_SENSE) {
  36669. + curlun->sense_data = curlun->unit_attention_data;
  36670. + curlun->unit_attention_data = SS_NO_SENSE;
  36671. + return -EINVAL;
  36672. + }
  36673. +
  36674. + /* Check that only command bytes listed in the mask are non-zero */
  36675. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  36676. + for (i = 1; i < cmnd_size; ++i) {
  36677. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  36678. + if (curlun)
  36679. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  36680. + return -EINVAL;
  36681. + }
  36682. + }
  36683. +
  36684. + /* If the medium isn't mounted and the command needs to access
  36685. + * it, return an error. */
  36686. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  36687. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  36688. + return -EINVAL;
  36689. + }
  36690. +
  36691. + return 0;
  36692. +}
  36693. +
  36694. +/* wrapper of check_command for data size in blocks handling */
  36695. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  36696. + enum data_direction data_dir, unsigned int mask,
  36697. + int needs_medium, const char *name)
  36698. +{
  36699. + if (fsg->curlun)
  36700. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  36701. + return check_command(fsg, cmnd_size, data_dir,
  36702. + mask, needs_medium, name);
  36703. +}
  36704. +
  36705. +static int do_scsi_command(struct fsg_dev *fsg)
  36706. +{
  36707. + struct fsg_buffhd *bh;
  36708. + int rc;
  36709. + int reply = -EINVAL;
  36710. + int i;
  36711. + static char unknown[16];
  36712. +
  36713. + dump_cdb(fsg);
  36714. +
  36715. + /* Wait for the next buffer to become available for data or status */
  36716. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  36717. + while (bh->state != BUF_STATE_EMPTY) {
  36718. + rc = sleep_thread(fsg);
  36719. + if (rc)
  36720. + return rc;
  36721. + }
  36722. + fsg->phase_error = 0;
  36723. + fsg->short_packet_received = 0;
  36724. +
  36725. + down_read(&fsg->filesem); // We're using the backing file
  36726. + switch (fsg->cmnd[0]) {
  36727. +
  36728. + case INQUIRY:
  36729. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  36730. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  36731. + (1<<4), 0,
  36732. + "INQUIRY")) == 0)
  36733. + reply = do_inquiry(fsg, bh);
  36734. + break;
  36735. +
  36736. + case MODE_SELECT:
  36737. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  36738. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  36739. + (1<<1) | (1<<4), 0,
  36740. + "MODE SELECT(6)")) == 0)
  36741. + reply = do_mode_select(fsg, bh);
  36742. + break;
  36743. +
  36744. + case MODE_SELECT_10:
  36745. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  36746. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  36747. + (1<<1) | (3<<7), 0,
  36748. + "MODE SELECT(10)")) == 0)
  36749. + reply = do_mode_select(fsg, bh);
  36750. + break;
  36751. +
  36752. + case MODE_SENSE:
  36753. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  36754. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  36755. + (1<<1) | (1<<2) | (1<<4), 0,
  36756. + "MODE SENSE(6)")) == 0)
  36757. + reply = do_mode_sense(fsg, bh);
  36758. + break;
  36759. +
  36760. + case MODE_SENSE_10:
  36761. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  36762. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  36763. + (1<<1) | (1<<2) | (3<<7), 0,
  36764. + "MODE SENSE(10)")) == 0)
  36765. + reply = do_mode_sense(fsg, bh);
  36766. + break;
  36767. +
  36768. + case ALLOW_MEDIUM_REMOVAL:
  36769. + fsg->data_size_from_cmnd = 0;
  36770. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  36771. + (1<<4), 0,
  36772. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  36773. + reply = do_prevent_allow(fsg);
  36774. + break;
  36775. +
  36776. + case READ_6:
  36777. + i = fsg->cmnd[4];
  36778. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  36779. + if ((reply = check_command_size_in_blocks(fsg, 6,
  36780. + DATA_DIR_TO_HOST,
  36781. + (7<<1) | (1<<4), 1,
  36782. + "READ(6)")) == 0)
  36783. + reply = do_read(fsg);
  36784. + break;
  36785. +
  36786. + case READ_10:
  36787. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  36788. + if ((reply = check_command_size_in_blocks(fsg, 10,
  36789. + DATA_DIR_TO_HOST,
  36790. + (1<<1) | (0xf<<2) | (3<<7), 1,
  36791. + "READ(10)")) == 0)
  36792. + reply = do_read(fsg);
  36793. + break;
  36794. +
  36795. + case READ_12:
  36796. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  36797. + if ((reply = check_command_size_in_blocks(fsg, 12,
  36798. + DATA_DIR_TO_HOST,
  36799. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  36800. + "READ(12)")) == 0)
  36801. + reply = do_read(fsg);
  36802. + break;
  36803. +
  36804. + case READ_CAPACITY:
  36805. + fsg->data_size_from_cmnd = 8;
  36806. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  36807. + (0xf<<2) | (1<<8), 1,
  36808. + "READ CAPACITY")) == 0)
  36809. + reply = do_read_capacity(fsg, bh);
  36810. + break;
  36811. +
  36812. + case READ_HEADER:
  36813. + if (!mod_data.cdrom)
  36814. + goto unknown_cmnd;
  36815. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  36816. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  36817. + (3<<7) | (0x1f<<1), 1,
  36818. + "READ HEADER")) == 0)
  36819. + reply = do_read_header(fsg, bh);
  36820. + break;
  36821. +
  36822. + case READ_TOC:
  36823. + if (!mod_data.cdrom)
  36824. + goto unknown_cmnd;
  36825. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  36826. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  36827. + (7<<6) | (1<<1), 1,
  36828. + "READ TOC")) == 0)
  36829. + reply = do_read_toc(fsg, bh);
  36830. + break;
  36831. +
  36832. + case READ_FORMAT_CAPACITIES:
  36833. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  36834. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  36835. + (3<<7), 1,
  36836. + "READ FORMAT CAPACITIES")) == 0)
  36837. + reply = do_read_format_capacities(fsg, bh);
  36838. + break;
  36839. +
  36840. + case REQUEST_SENSE:
  36841. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  36842. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  36843. + (1<<4), 0,
  36844. + "REQUEST SENSE")) == 0)
  36845. + reply = do_request_sense(fsg, bh);
  36846. + break;
  36847. +
  36848. + case START_STOP:
  36849. + fsg->data_size_from_cmnd = 0;
  36850. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  36851. + (1<<1) | (1<<4), 0,
  36852. + "START-STOP UNIT")) == 0)
  36853. + reply = do_start_stop(fsg);
  36854. + break;
  36855. +
  36856. + case SYNCHRONIZE_CACHE:
  36857. + fsg->data_size_from_cmnd = 0;
  36858. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  36859. + (0xf<<2) | (3<<7), 1,
  36860. + "SYNCHRONIZE CACHE")) == 0)
  36861. + reply = do_synchronize_cache(fsg);
  36862. + break;
  36863. +
  36864. + case TEST_UNIT_READY:
  36865. + fsg->data_size_from_cmnd = 0;
  36866. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  36867. + 0, 1,
  36868. + "TEST UNIT READY");
  36869. + break;
  36870. +
  36871. + /* Although optional, this command is used by MS-Windows. We
  36872. + * support a minimal version: BytChk must be 0. */
  36873. + case VERIFY:
  36874. + fsg->data_size_from_cmnd = 0;
  36875. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  36876. + (1<<1) | (0xf<<2) | (3<<7), 1,
  36877. + "VERIFY")) == 0)
  36878. + reply = do_verify(fsg);
  36879. + break;
  36880. +
  36881. + case WRITE_6:
  36882. + i = fsg->cmnd[4];
  36883. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  36884. + if ((reply = check_command_size_in_blocks(fsg, 6,
  36885. + DATA_DIR_FROM_HOST,
  36886. + (7<<1) | (1<<4), 1,
  36887. + "WRITE(6)")) == 0)
  36888. + reply = do_write(fsg);
  36889. + break;
  36890. +
  36891. + case WRITE_10:
  36892. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  36893. + if ((reply = check_command_size_in_blocks(fsg, 10,
  36894. + DATA_DIR_FROM_HOST,
  36895. + (1<<1) | (0xf<<2) | (3<<7), 1,
  36896. + "WRITE(10)")) == 0)
  36897. + reply = do_write(fsg);
  36898. + break;
  36899. +
  36900. + case WRITE_12:
  36901. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  36902. + if ((reply = check_command_size_in_blocks(fsg, 12,
  36903. + DATA_DIR_FROM_HOST,
  36904. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  36905. + "WRITE(12)")) == 0)
  36906. + reply = do_write(fsg);
  36907. + break;
  36908. +
  36909. + /* Some mandatory commands that we recognize but don't implement.
  36910. + * They don't mean much in this setting. It's left as an exercise
  36911. + * for anyone interested to implement RESERVE and RELEASE in terms
  36912. + * of Posix locks. */
  36913. + case FORMAT_UNIT:
  36914. + case RELEASE:
  36915. + case RESERVE:
  36916. + case SEND_DIAGNOSTIC:
  36917. + // Fall through
  36918. +
  36919. + default:
  36920. + unknown_cmnd:
  36921. + fsg->data_size_from_cmnd = 0;
  36922. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  36923. + if ((reply = check_command(fsg, fsg->cmnd_size,
  36924. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  36925. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  36926. + reply = -EINVAL;
  36927. + }
  36928. + break;
  36929. + }
  36930. + up_read(&fsg->filesem);
  36931. +
  36932. + if (reply == -EINTR || signal_pending(current))
  36933. + return -EINTR;
  36934. +
  36935. + /* Set up the single reply buffer for finish_reply() */
  36936. + if (reply == -EINVAL)
  36937. + reply = 0; // Error reply length
  36938. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  36939. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  36940. + bh->inreq->length = reply;
  36941. + bh->state = BUF_STATE_FULL;
  36942. + fsg->residue -= reply;
  36943. + } // Otherwise it's already set
  36944. +
  36945. + return 0;
  36946. +}
  36947. +
  36948. +
  36949. +/*-------------------------------------------------------------------------*/
  36950. +
  36951. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  36952. +{
  36953. + struct usb_request *req = bh->outreq;
  36954. + struct bulk_cb_wrap *cbw = req->buf;
  36955. +
  36956. + /* Was this a real packet? Should it be ignored? */
  36957. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  36958. + return -EINVAL;
  36959. +
  36960. + /* Is the CBW valid? */
  36961. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  36962. + cbw->Signature != cpu_to_le32(
  36963. + US_BULK_CB_SIGN)) {
  36964. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  36965. + req->actual,
  36966. + le32_to_cpu(cbw->Signature));
  36967. +
  36968. + /* The Bulk-only spec says we MUST stall the IN endpoint
  36969. + * (6.6.1), so it's unavoidable. It also says we must
  36970. + * retain this state until the next reset, but there's
  36971. + * no way to tell the controller driver it should ignore
  36972. + * Clear-Feature(HALT) requests.
  36973. + *
  36974. + * We aren't required to halt the OUT endpoint; instead
  36975. + * we can simply accept and discard any data received
  36976. + * until the next reset. */
  36977. + wedge_bulk_in_endpoint(fsg);
  36978. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  36979. + return -EINVAL;
  36980. + }
  36981. +
  36982. + /* Is the CBW meaningful? */
  36983. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  36984. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  36985. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  36986. + "cmdlen %u\n",
  36987. + cbw->Lun, cbw->Flags, cbw->Length);
  36988. +
  36989. + /* We can do anything we want here, so let's stall the
  36990. + * bulk pipes if we are allowed to. */
  36991. + if (mod_data.can_stall) {
  36992. + fsg_set_halt(fsg, fsg->bulk_out);
  36993. + halt_bulk_in_endpoint(fsg);
  36994. + }
  36995. + return -EINVAL;
  36996. + }
  36997. +
  36998. + /* Save the command for later */
  36999. + fsg->cmnd_size = cbw->Length;
  37000. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  37001. + if (cbw->Flags & US_BULK_FLAG_IN)
  37002. + fsg->data_dir = DATA_DIR_TO_HOST;
  37003. + else
  37004. + fsg->data_dir = DATA_DIR_FROM_HOST;
  37005. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  37006. + if (fsg->data_size == 0)
  37007. + fsg->data_dir = DATA_DIR_NONE;
  37008. + fsg->lun = cbw->Lun;
  37009. + fsg->tag = cbw->Tag;
  37010. + return 0;
  37011. +}
  37012. +
  37013. +
  37014. +static int get_next_command(struct fsg_dev *fsg)
  37015. +{
  37016. + struct fsg_buffhd *bh;
  37017. + int rc = 0;
  37018. +
  37019. + if (transport_is_bbb()) {
  37020. +
  37021. + /* Wait for the next buffer to become available */
  37022. + bh = fsg->next_buffhd_to_fill;
  37023. + while (bh->state != BUF_STATE_EMPTY) {
  37024. + rc = sleep_thread(fsg);
  37025. + if (rc)
  37026. + return rc;
  37027. + }
  37028. +
  37029. + /* Queue a request to read a Bulk-only CBW */
  37030. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  37031. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  37032. + &bh->outreq_busy, &bh->state);
  37033. +
  37034. + /* We will drain the buffer in software, which means we
  37035. + * can reuse it for the next filling. No need to advance
  37036. + * next_buffhd_to_fill. */
  37037. +
  37038. + /* Wait for the CBW to arrive */
  37039. + while (bh->state != BUF_STATE_FULL) {
  37040. + rc = sleep_thread(fsg);
  37041. + if (rc)
  37042. + return rc;
  37043. + }
  37044. + smp_rmb();
  37045. + rc = received_cbw(fsg, bh);
  37046. + bh->state = BUF_STATE_EMPTY;
  37047. +
  37048. + } else { // USB_PR_CB or USB_PR_CBI
  37049. +
  37050. + /* Wait for the next command to arrive */
  37051. + while (fsg->cbbuf_cmnd_size == 0) {
  37052. + rc = sleep_thread(fsg);
  37053. + if (rc)
  37054. + return rc;
  37055. + }
  37056. +
  37057. + /* Is the previous status interrupt request still busy?
  37058. + * The host is allowed to skip reading the status,
  37059. + * so we must cancel it. */
  37060. + if (fsg->intreq_busy)
  37061. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  37062. +
  37063. + /* Copy the command and mark the buffer empty */
  37064. + fsg->data_dir = DATA_DIR_UNKNOWN;
  37065. + spin_lock_irq(&fsg->lock);
  37066. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  37067. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  37068. + fsg->cbbuf_cmnd_size = 0;
  37069. + spin_unlock_irq(&fsg->lock);
  37070. +
  37071. + /* Use LUN from the command */
  37072. + fsg->lun = fsg->cmnd[1] >> 5;
  37073. + }
  37074. +
  37075. + /* Update current lun */
  37076. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  37077. + fsg->curlun = &fsg->luns[fsg->lun];
  37078. + else
  37079. + fsg->curlun = NULL;
  37080. +
  37081. + return rc;
  37082. +}
  37083. +
  37084. +
  37085. +/*-------------------------------------------------------------------------*/
  37086. +
  37087. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  37088. + const struct usb_endpoint_descriptor *d)
  37089. +{
  37090. + int rc;
  37091. +
  37092. + ep->driver_data = fsg;
  37093. + ep->desc = d;
  37094. + rc = usb_ep_enable(ep);
  37095. + if (rc)
  37096. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  37097. + return rc;
  37098. +}
  37099. +
  37100. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  37101. + struct usb_request **preq)
  37102. +{
  37103. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  37104. + if (*preq)
  37105. + return 0;
  37106. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  37107. + return -ENOMEM;
  37108. +}
  37109. +
  37110. +/*
  37111. + * Reset interface setting and re-init endpoint state (toggle etc).
  37112. + * Call with altsetting < 0 to disable the interface. The only other
  37113. + * available altsetting is 0, which enables the interface.
  37114. + */
  37115. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  37116. +{
  37117. + int rc = 0;
  37118. + int i;
  37119. + const struct usb_endpoint_descriptor *d;
  37120. +
  37121. + if (fsg->running)
  37122. + DBG(fsg, "reset interface\n");
  37123. +
  37124. +reset:
  37125. + /* Deallocate the requests */
  37126. + for (i = 0; i < fsg_num_buffers; ++i) {
  37127. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  37128. +
  37129. + if (bh->inreq) {
  37130. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  37131. + bh->inreq = NULL;
  37132. + }
  37133. + if (bh->outreq) {
  37134. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  37135. + bh->outreq = NULL;
  37136. + }
  37137. + }
  37138. + if (fsg->intreq) {
  37139. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  37140. + fsg->intreq = NULL;
  37141. + }
  37142. +
  37143. + /* Disable the endpoints */
  37144. + if (fsg->bulk_in_enabled) {
  37145. + usb_ep_disable(fsg->bulk_in);
  37146. + fsg->bulk_in_enabled = 0;
  37147. + }
  37148. + if (fsg->bulk_out_enabled) {
  37149. + usb_ep_disable(fsg->bulk_out);
  37150. + fsg->bulk_out_enabled = 0;
  37151. + }
  37152. + if (fsg->intr_in_enabled) {
  37153. + usb_ep_disable(fsg->intr_in);
  37154. + fsg->intr_in_enabled = 0;
  37155. + }
  37156. +
  37157. + fsg->running = 0;
  37158. + if (altsetting < 0 || rc != 0)
  37159. + return rc;
  37160. +
  37161. + DBG(fsg, "set interface %d\n", altsetting);
  37162. +
  37163. + /* Enable the endpoints */
  37164. + d = fsg_ep_desc(fsg->gadget,
  37165. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  37166. + &fsg_ss_bulk_in_desc);
  37167. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  37168. + goto reset;
  37169. + fsg->bulk_in_enabled = 1;
  37170. +
  37171. + d = fsg_ep_desc(fsg->gadget,
  37172. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  37173. + &fsg_ss_bulk_out_desc);
  37174. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  37175. + goto reset;
  37176. + fsg->bulk_out_enabled = 1;
  37177. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  37178. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  37179. +
  37180. + if (transport_is_cbi()) {
  37181. + d = fsg_ep_desc(fsg->gadget,
  37182. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  37183. + &fsg_ss_intr_in_desc);
  37184. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  37185. + goto reset;
  37186. + fsg->intr_in_enabled = 1;
  37187. + }
  37188. +
  37189. + /* Allocate the requests */
  37190. + for (i = 0; i < fsg_num_buffers; ++i) {
  37191. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  37192. +
  37193. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  37194. + goto reset;
  37195. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  37196. + goto reset;
  37197. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  37198. + bh->inreq->context = bh->outreq->context = bh;
  37199. + bh->inreq->complete = bulk_in_complete;
  37200. + bh->outreq->complete = bulk_out_complete;
  37201. + }
  37202. + if (transport_is_cbi()) {
  37203. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  37204. + goto reset;
  37205. + fsg->intreq->complete = intr_in_complete;
  37206. + }
  37207. +
  37208. + fsg->running = 1;
  37209. + for (i = 0; i < fsg->nluns; ++i)
  37210. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  37211. + return rc;
  37212. +}
  37213. +
  37214. +
  37215. +/*
  37216. + * Change our operational configuration. This code must agree with the code
  37217. + * that returns config descriptors, and with interface altsetting code.
  37218. + *
  37219. + * It's also responsible for power management interactions. Some
  37220. + * configurations might not work with our current power sources.
  37221. + * For now we just assume the gadget is always self-powered.
  37222. + */
  37223. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  37224. +{
  37225. + int rc = 0;
  37226. +
  37227. + /* Disable the single interface */
  37228. + if (fsg->config != 0) {
  37229. + DBG(fsg, "reset config\n");
  37230. + fsg->config = 0;
  37231. + rc = do_set_interface(fsg, -1);
  37232. + }
  37233. +
  37234. + /* Enable the interface */
  37235. + if (new_config != 0) {
  37236. + fsg->config = new_config;
  37237. + if ((rc = do_set_interface(fsg, 0)) != 0)
  37238. + fsg->config = 0; // Reset on errors
  37239. + else
  37240. + INFO(fsg, "%s config #%d\n",
  37241. + usb_speed_string(fsg->gadget->speed),
  37242. + fsg->config);
  37243. + }
  37244. + return rc;
  37245. +}
  37246. +
  37247. +
  37248. +/*-------------------------------------------------------------------------*/
  37249. +
  37250. +static void handle_exception(struct fsg_dev *fsg)
  37251. +{
  37252. + siginfo_t info;
  37253. + int sig;
  37254. + int i;
  37255. + int num_active;
  37256. + struct fsg_buffhd *bh;
  37257. + enum fsg_state old_state;
  37258. + u8 new_config;
  37259. + struct fsg_lun *curlun;
  37260. + unsigned int exception_req_tag;
  37261. + int rc;
  37262. +
  37263. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  37264. + * into a high-priority EXIT exception. */
  37265. + for (;;) {
  37266. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  37267. + if (!sig)
  37268. + break;
  37269. + if (sig != SIGUSR1) {
  37270. + if (fsg->state < FSG_STATE_EXIT)
  37271. + DBG(fsg, "Main thread exiting on signal\n");
  37272. + raise_exception(fsg, FSG_STATE_EXIT);
  37273. + }
  37274. + }
  37275. +
  37276. + /* Cancel all the pending transfers */
  37277. + if (fsg->intreq_busy)
  37278. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  37279. + for (i = 0; i < fsg_num_buffers; ++i) {
  37280. + bh = &fsg->buffhds[i];
  37281. + if (bh->inreq_busy)
  37282. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  37283. + if (bh->outreq_busy)
  37284. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  37285. + }
  37286. +
  37287. + /* Wait until everything is idle */
  37288. + for (;;) {
  37289. + num_active = fsg->intreq_busy;
  37290. + for (i = 0; i < fsg_num_buffers; ++i) {
  37291. + bh = &fsg->buffhds[i];
  37292. + num_active += bh->inreq_busy + bh->outreq_busy;
  37293. + }
  37294. + if (num_active == 0)
  37295. + break;
  37296. + if (sleep_thread(fsg))
  37297. + return;
  37298. + }
  37299. +
  37300. + /* Clear out the controller's fifos */
  37301. + if (fsg->bulk_in_enabled)
  37302. + usb_ep_fifo_flush(fsg->bulk_in);
  37303. + if (fsg->bulk_out_enabled)
  37304. + usb_ep_fifo_flush(fsg->bulk_out);
  37305. + if (fsg->intr_in_enabled)
  37306. + usb_ep_fifo_flush(fsg->intr_in);
  37307. +
  37308. + /* Reset the I/O buffer states and pointers, the SCSI
  37309. + * state, and the exception. Then invoke the handler. */
  37310. + spin_lock_irq(&fsg->lock);
  37311. +
  37312. + for (i = 0; i < fsg_num_buffers; ++i) {
  37313. + bh = &fsg->buffhds[i];
  37314. + bh->state = BUF_STATE_EMPTY;
  37315. + }
  37316. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  37317. + &fsg->buffhds[0];
  37318. +
  37319. + exception_req_tag = fsg->exception_req_tag;
  37320. + new_config = fsg->new_config;
  37321. + old_state = fsg->state;
  37322. +
  37323. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  37324. + fsg->state = FSG_STATE_STATUS_PHASE;
  37325. + else {
  37326. + for (i = 0; i < fsg->nluns; ++i) {
  37327. + curlun = &fsg->luns[i];
  37328. + curlun->prevent_medium_removal = 0;
  37329. + curlun->sense_data = curlun->unit_attention_data =
  37330. + SS_NO_SENSE;
  37331. + curlun->sense_data_info = 0;
  37332. + curlun->info_valid = 0;
  37333. + }
  37334. + fsg->state = FSG_STATE_IDLE;
  37335. + }
  37336. + spin_unlock_irq(&fsg->lock);
  37337. +
  37338. + /* Carry out any extra actions required for the exception */
  37339. + switch (old_state) {
  37340. + default:
  37341. + break;
  37342. +
  37343. + case FSG_STATE_ABORT_BULK_OUT:
  37344. + send_status(fsg);
  37345. + spin_lock_irq(&fsg->lock);
  37346. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  37347. + fsg->state = FSG_STATE_IDLE;
  37348. + spin_unlock_irq(&fsg->lock);
  37349. + break;
  37350. +
  37351. + case FSG_STATE_RESET:
  37352. + /* In case we were forced against our will to halt a
  37353. + * bulk endpoint, clear the halt now. (The SuperH UDC
  37354. + * requires this.) */
  37355. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  37356. + usb_ep_clear_halt(fsg->bulk_in);
  37357. +
  37358. + if (transport_is_bbb()) {
  37359. + if (fsg->ep0_req_tag == exception_req_tag)
  37360. + ep0_queue(fsg); // Complete the status stage
  37361. +
  37362. + } else if (transport_is_cbi())
  37363. + send_status(fsg); // Status by interrupt pipe
  37364. +
  37365. + /* Technically this should go here, but it would only be
  37366. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  37367. + * CONFIG_CHANGE cases. */
  37368. + // for (i = 0; i < fsg->nluns; ++i)
  37369. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  37370. + break;
  37371. +
  37372. + case FSG_STATE_INTERFACE_CHANGE:
  37373. + rc = do_set_interface(fsg, 0);
  37374. + if (fsg->ep0_req_tag != exception_req_tag)
  37375. + break;
  37376. + if (rc != 0) // STALL on errors
  37377. + fsg_set_halt(fsg, fsg->ep0);
  37378. + else // Complete the status stage
  37379. + ep0_queue(fsg);
  37380. + break;
  37381. +
  37382. + case FSG_STATE_CONFIG_CHANGE:
  37383. + rc = do_set_config(fsg, new_config);
  37384. + if (fsg->ep0_req_tag != exception_req_tag)
  37385. + break;
  37386. + if (rc != 0) // STALL on errors
  37387. + fsg_set_halt(fsg, fsg->ep0);
  37388. + else // Complete the status stage
  37389. + ep0_queue(fsg);
  37390. + break;
  37391. +
  37392. + case FSG_STATE_DISCONNECT:
  37393. + for (i = 0; i < fsg->nluns; ++i)
  37394. + fsg_lun_fsync_sub(fsg->luns + i);
  37395. + do_set_config(fsg, 0); // Unconfigured state
  37396. + break;
  37397. +
  37398. + case FSG_STATE_EXIT:
  37399. + case FSG_STATE_TERMINATED:
  37400. + do_set_config(fsg, 0); // Free resources
  37401. + spin_lock_irq(&fsg->lock);
  37402. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  37403. + spin_unlock_irq(&fsg->lock);
  37404. + break;
  37405. + }
  37406. +}
  37407. +
  37408. +
  37409. +/*-------------------------------------------------------------------------*/
  37410. +
  37411. +static int fsg_main_thread(void *fsg_)
  37412. +{
  37413. + struct fsg_dev *fsg = fsg_;
  37414. +
  37415. + /* Allow the thread to be killed by a signal, but set the signal mask
  37416. + * to block everything but INT, TERM, KILL, and USR1. */
  37417. + allow_signal(SIGINT);
  37418. + allow_signal(SIGTERM);
  37419. + allow_signal(SIGKILL);
  37420. + allow_signal(SIGUSR1);
  37421. +
  37422. + /* Allow the thread to be frozen */
  37423. + set_freezable();
  37424. +
  37425. + /* Arrange for userspace references to be interpreted as kernel
  37426. + * pointers. That way we can pass a kernel pointer to a routine
  37427. + * that expects a __user pointer and it will work okay. */
  37428. + set_fs(get_ds());
  37429. +
  37430. + /* The main loop */
  37431. + while (fsg->state != FSG_STATE_TERMINATED) {
  37432. + if (exception_in_progress(fsg) || signal_pending(current)) {
  37433. + handle_exception(fsg);
  37434. + continue;
  37435. + }
  37436. +
  37437. + if (!fsg->running) {
  37438. + sleep_thread(fsg);
  37439. + continue;
  37440. + }
  37441. +
  37442. + if (get_next_command(fsg))
  37443. + continue;
  37444. +
  37445. + spin_lock_irq(&fsg->lock);
  37446. + if (!exception_in_progress(fsg))
  37447. + fsg->state = FSG_STATE_DATA_PHASE;
  37448. + spin_unlock_irq(&fsg->lock);
  37449. +
  37450. + if (do_scsi_command(fsg) || finish_reply(fsg))
  37451. + continue;
  37452. +
  37453. + spin_lock_irq(&fsg->lock);
  37454. + if (!exception_in_progress(fsg))
  37455. + fsg->state = FSG_STATE_STATUS_PHASE;
  37456. + spin_unlock_irq(&fsg->lock);
  37457. +
  37458. + if (send_status(fsg))
  37459. + continue;
  37460. +
  37461. + spin_lock_irq(&fsg->lock);
  37462. + if (!exception_in_progress(fsg))
  37463. + fsg->state = FSG_STATE_IDLE;
  37464. + spin_unlock_irq(&fsg->lock);
  37465. + }
  37466. +
  37467. + spin_lock_irq(&fsg->lock);
  37468. + fsg->thread_task = NULL;
  37469. + spin_unlock_irq(&fsg->lock);
  37470. +
  37471. + /* If we are exiting because of a signal, unregister the
  37472. + * gadget driver. */
  37473. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  37474. + usb_gadget_unregister_driver(&fsg_driver);
  37475. +
  37476. + /* Let the unbind and cleanup routines know the thread has exited */
  37477. + complete_and_exit(&fsg->thread_notifier, 0);
  37478. +}
  37479. +
  37480. +
  37481. +/*-------------------------------------------------------------------------*/
  37482. +
  37483. +
  37484. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  37485. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  37486. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  37487. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  37488. +
  37489. +
  37490. +/*-------------------------------------------------------------------------*/
  37491. +
  37492. +static void fsg_release(struct kref *ref)
  37493. +{
  37494. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  37495. +
  37496. + kfree(fsg->luns);
  37497. + kfree(fsg);
  37498. +}
  37499. +
  37500. +static void lun_release(struct device *dev)
  37501. +{
  37502. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  37503. + struct fsg_dev *fsg =
  37504. + container_of(filesem, struct fsg_dev, filesem);
  37505. +
  37506. + kref_put(&fsg->ref, fsg_release);
  37507. +}
  37508. +
  37509. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  37510. +{
  37511. + struct fsg_dev *fsg = get_gadget_data(gadget);
  37512. + int i;
  37513. + struct fsg_lun *curlun;
  37514. + struct usb_request *req = fsg->ep0req;
  37515. +
  37516. + DBG(fsg, "unbind\n");
  37517. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  37518. +
  37519. + /* If the thread isn't already dead, tell it to exit now */
  37520. + if (fsg->state != FSG_STATE_TERMINATED) {
  37521. + raise_exception(fsg, FSG_STATE_EXIT);
  37522. + wait_for_completion(&fsg->thread_notifier);
  37523. +
  37524. + /* The cleanup routine waits for this completion also */
  37525. + complete(&fsg->thread_notifier);
  37526. + }
  37527. +
  37528. + /* Unregister the sysfs attribute files and the LUNs */
  37529. + for (i = 0; i < fsg->nluns; ++i) {
  37530. + curlun = &fsg->luns[i];
  37531. + if (curlun->registered) {
  37532. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  37533. + device_remove_file(&curlun->dev, &dev_attr_ro);
  37534. + device_remove_file(&curlun->dev, &dev_attr_file);
  37535. + fsg_lun_close(curlun);
  37536. + device_unregister(&curlun->dev);
  37537. + curlun->registered = 0;
  37538. + }
  37539. + }
  37540. +
  37541. + /* Free the data buffers */
  37542. + for (i = 0; i < fsg_num_buffers; ++i)
  37543. + kfree(fsg->buffhds[i].buf);
  37544. +
  37545. + /* Free the request and buffer for endpoint 0 */
  37546. + if (req) {
  37547. + kfree(req->buf);
  37548. + usb_ep_free_request(fsg->ep0, req);
  37549. + }
  37550. +
  37551. + set_gadget_data(gadget, NULL);
  37552. +}
  37553. +
  37554. +
  37555. +static int __init check_parameters(struct fsg_dev *fsg)
  37556. +{
  37557. + int prot;
  37558. + int gcnum;
  37559. +
  37560. + /* Store the default values */
  37561. + mod_data.transport_type = USB_PR_BULK;
  37562. + mod_data.transport_name = "Bulk-only";
  37563. + mod_data.protocol_type = USB_SC_SCSI;
  37564. + mod_data.protocol_name = "Transparent SCSI";
  37565. +
  37566. + /* Some peripheral controllers are known not to be able to
  37567. + * halt bulk endpoints correctly. If one of them is present,
  37568. + * disable stalls.
  37569. + */
  37570. + if (gadget_is_at91(fsg->gadget))
  37571. + mod_data.can_stall = 0;
  37572. +
  37573. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  37574. + gcnum = usb_gadget_controller_number(fsg->gadget);
  37575. + if (gcnum >= 0)
  37576. + mod_data.release = 0x0300 + gcnum;
  37577. + else {
  37578. + WARNING(fsg, "controller '%s' not recognized\n",
  37579. + fsg->gadget->name);
  37580. + mod_data.release = 0x0399;
  37581. + }
  37582. + }
  37583. +
  37584. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  37585. +
  37586. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  37587. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  37588. + ; // Use default setting
  37589. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  37590. + mod_data.transport_type = USB_PR_CB;
  37591. + mod_data.transport_name = "Control-Bulk";
  37592. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  37593. + mod_data.transport_type = USB_PR_CBI;
  37594. + mod_data.transport_name = "Control-Bulk-Interrupt";
  37595. + } else {
  37596. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  37597. + return -EINVAL;
  37598. + }
  37599. +
  37600. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  37601. + prot == USB_SC_SCSI) {
  37602. + ; // Use default setting
  37603. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  37604. + prot == USB_SC_RBC) {
  37605. + mod_data.protocol_type = USB_SC_RBC;
  37606. + mod_data.protocol_name = "RBC";
  37607. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  37608. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  37609. + prot == USB_SC_8020) {
  37610. + mod_data.protocol_type = USB_SC_8020;
  37611. + mod_data.protocol_name = "8020i (ATAPI)";
  37612. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  37613. + prot == USB_SC_QIC) {
  37614. + mod_data.protocol_type = USB_SC_QIC;
  37615. + mod_data.protocol_name = "QIC-157";
  37616. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  37617. + prot == USB_SC_UFI) {
  37618. + mod_data.protocol_type = USB_SC_UFI;
  37619. + mod_data.protocol_name = "UFI";
  37620. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  37621. + prot == USB_SC_8070) {
  37622. + mod_data.protocol_type = USB_SC_8070;
  37623. + mod_data.protocol_name = "8070i";
  37624. + } else {
  37625. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  37626. + return -EINVAL;
  37627. + }
  37628. +
  37629. + mod_data.buflen &= PAGE_CACHE_MASK;
  37630. + if (mod_data.buflen <= 0) {
  37631. + ERROR(fsg, "invalid buflen\n");
  37632. + return -ETOOSMALL;
  37633. + }
  37634. +
  37635. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  37636. +
  37637. + /* Serial string handling.
  37638. + * On a real device, the serial string would be loaded
  37639. + * from permanent storage. */
  37640. + if (mod_data.serial) {
  37641. + const char *ch;
  37642. + unsigned len = 0;
  37643. +
  37644. + /* Sanity check :
  37645. + * The CB[I] specification limits the serial string to
  37646. + * 12 uppercase hexadecimal characters.
  37647. + * BBB need at least 12 uppercase hexadecimal characters,
  37648. + * with a maximum of 126. */
  37649. + for (ch = mod_data.serial; *ch; ++ch) {
  37650. + ++len;
  37651. + if ((*ch < '0' || *ch > '9') &&
  37652. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  37653. + WARNING(fsg,
  37654. + "Invalid serial string character: %c\n",
  37655. + *ch);
  37656. + goto no_serial;
  37657. + }
  37658. + }
  37659. + if (len > 126 ||
  37660. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  37661. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  37662. + WARNING(fsg, "Invalid serial string length!\n");
  37663. + goto no_serial;
  37664. + }
  37665. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  37666. + } else {
  37667. + WARNING(fsg, "No serial-number string provided!\n");
  37668. + no_serial:
  37669. + device_desc.iSerialNumber = 0;
  37670. + }
  37671. +
  37672. + return 0;
  37673. +}
  37674. +
  37675. +
  37676. +static int __init fsg_bind(struct usb_gadget *gadget)
  37677. +{
  37678. + struct fsg_dev *fsg = the_fsg;
  37679. + int rc;
  37680. + int i;
  37681. + struct fsg_lun *curlun;
  37682. + struct usb_ep *ep;
  37683. + struct usb_request *req;
  37684. + char *pathbuf, *p;
  37685. +
  37686. + fsg->gadget = gadget;
  37687. + set_gadget_data(gadget, fsg);
  37688. + fsg->ep0 = gadget->ep0;
  37689. + fsg->ep0->driver_data = fsg;
  37690. +
  37691. + if ((rc = check_parameters(fsg)) != 0)
  37692. + goto out;
  37693. +
  37694. + if (mod_data.removable) { // Enable the store_xxx attributes
  37695. + dev_attr_file.attr.mode = 0644;
  37696. + dev_attr_file.store = fsg_store_file;
  37697. + if (!mod_data.cdrom) {
  37698. + dev_attr_ro.attr.mode = 0644;
  37699. + dev_attr_ro.store = fsg_store_ro;
  37700. + }
  37701. + }
  37702. +
  37703. + /* Only for removable media? */
  37704. + dev_attr_nofua.attr.mode = 0644;
  37705. + dev_attr_nofua.store = fsg_store_nofua;
  37706. +
  37707. + /* Find out how many LUNs there should be */
  37708. + i = mod_data.nluns;
  37709. + if (i == 0)
  37710. + i = max(mod_data.num_filenames, 1u);
  37711. + if (i > FSG_MAX_LUNS) {
  37712. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  37713. + rc = -EINVAL;
  37714. + goto out;
  37715. + }
  37716. +
  37717. + /* Create the LUNs, open their backing files, and register the
  37718. + * LUN devices in sysfs. */
  37719. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  37720. + if (!fsg->luns) {
  37721. + rc = -ENOMEM;
  37722. + goto out;
  37723. + }
  37724. + fsg->nluns = i;
  37725. +
  37726. + for (i = 0; i < fsg->nluns; ++i) {
  37727. + curlun = &fsg->luns[i];
  37728. + curlun->cdrom = !!mod_data.cdrom;
  37729. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  37730. + curlun->initially_ro = curlun->ro;
  37731. + curlun->removable = mod_data.removable;
  37732. + curlun->nofua = mod_data.nofua[i];
  37733. + curlun->dev.release = lun_release;
  37734. + curlun->dev.parent = &gadget->dev;
  37735. + curlun->dev.driver = &fsg_driver.driver;
  37736. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  37737. + dev_set_name(&curlun->dev,"%s-lun%d",
  37738. + dev_name(&gadget->dev), i);
  37739. +
  37740. + kref_get(&fsg->ref);
  37741. + rc = device_register(&curlun->dev);
  37742. + if (rc) {
  37743. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  37744. + put_device(&curlun->dev);
  37745. + goto out;
  37746. + }
  37747. + curlun->registered = 1;
  37748. +
  37749. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  37750. + if (rc)
  37751. + goto out;
  37752. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  37753. + if (rc)
  37754. + goto out;
  37755. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  37756. + if (rc)
  37757. + goto out;
  37758. +
  37759. + if (mod_data.file[i] && *mod_data.file[i]) {
  37760. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  37761. + if (rc)
  37762. + goto out;
  37763. + } else if (!mod_data.removable) {
  37764. + ERROR(fsg, "no file given for LUN%d\n", i);
  37765. + rc = -EINVAL;
  37766. + goto out;
  37767. + }
  37768. + }
  37769. +
  37770. + /* Find all the endpoints we will use */
  37771. + usb_ep_autoconfig_reset(gadget);
  37772. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  37773. + if (!ep)
  37774. + goto autoconf_fail;
  37775. + ep->driver_data = fsg; // claim the endpoint
  37776. + fsg->bulk_in = ep;
  37777. +
  37778. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  37779. + if (!ep)
  37780. + goto autoconf_fail;
  37781. + ep->driver_data = fsg; // claim the endpoint
  37782. + fsg->bulk_out = ep;
  37783. +
  37784. + if (transport_is_cbi()) {
  37785. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  37786. + if (!ep)
  37787. + goto autoconf_fail;
  37788. + ep->driver_data = fsg; // claim the endpoint
  37789. + fsg->intr_in = ep;
  37790. + }
  37791. +
  37792. + /* Fix up the descriptors */
  37793. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  37794. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  37795. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  37796. +
  37797. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  37798. + fsg_intf_desc.bNumEndpoints = i;
  37799. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  37800. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  37801. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  37802. +
  37803. + if (gadget_is_dualspeed(gadget)) {
  37804. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  37805. +
  37806. + /* Assume endpoint addresses are the same for both speeds */
  37807. + fsg_hs_bulk_in_desc.bEndpointAddress =
  37808. + fsg_fs_bulk_in_desc.bEndpointAddress;
  37809. + fsg_hs_bulk_out_desc.bEndpointAddress =
  37810. + fsg_fs_bulk_out_desc.bEndpointAddress;
  37811. + fsg_hs_intr_in_desc.bEndpointAddress =
  37812. + fsg_fs_intr_in_desc.bEndpointAddress;
  37813. + }
  37814. +
  37815. + if (gadget_is_superspeed(gadget)) {
  37816. + unsigned max_burst;
  37817. +
  37818. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  37819. +
  37820. + /* Calculate bMaxBurst, we know packet size is 1024 */
  37821. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  37822. +
  37823. + /* Assume endpoint addresses are the same for both speeds */
  37824. + fsg_ss_bulk_in_desc.bEndpointAddress =
  37825. + fsg_fs_bulk_in_desc.bEndpointAddress;
  37826. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  37827. +
  37828. + fsg_ss_bulk_out_desc.bEndpointAddress =
  37829. + fsg_fs_bulk_out_desc.bEndpointAddress;
  37830. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  37831. + }
  37832. +
  37833. + if (gadget_is_otg(gadget))
  37834. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  37835. +
  37836. + rc = -ENOMEM;
  37837. +
  37838. + /* Allocate the request and buffer for endpoint 0 */
  37839. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  37840. + if (!req)
  37841. + goto out;
  37842. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  37843. + if (!req->buf)
  37844. + goto out;
  37845. + req->complete = ep0_complete;
  37846. +
  37847. + /* Allocate the data buffers */
  37848. + for (i = 0; i < fsg_num_buffers; ++i) {
  37849. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  37850. +
  37851. + /* Allocate for the bulk-in endpoint. We assume that
  37852. + * the buffer will also work with the bulk-out (and
  37853. + * interrupt-in) endpoint. */
  37854. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  37855. + if (!bh->buf)
  37856. + goto out;
  37857. + bh->next = bh + 1;
  37858. + }
  37859. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  37860. +
  37861. + /* This should reflect the actual gadget power source */
  37862. + usb_gadget_set_selfpowered(gadget);
  37863. +
  37864. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  37865. + "%s %s with %s",
  37866. + init_utsname()->sysname, init_utsname()->release,
  37867. + gadget->name);
  37868. +
  37869. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  37870. + "file-storage-gadget");
  37871. + if (IS_ERR(fsg->thread_task)) {
  37872. + rc = PTR_ERR(fsg->thread_task);
  37873. + goto out;
  37874. + }
  37875. +
  37876. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  37877. + INFO(fsg, "NOTE: This driver is deprecated. "
  37878. + "Consider using g_mass_storage instead.\n");
  37879. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  37880. +
  37881. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  37882. + for (i = 0; i < fsg->nluns; ++i) {
  37883. + curlun = &fsg->luns[i];
  37884. + if (fsg_lun_is_open(curlun)) {
  37885. + p = NULL;
  37886. + if (pathbuf) {
  37887. + p = d_path(&curlun->filp->f_path,
  37888. + pathbuf, PATH_MAX);
  37889. + if (IS_ERR(p))
  37890. + p = NULL;
  37891. + }
  37892. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  37893. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  37894. + }
  37895. + }
  37896. + kfree(pathbuf);
  37897. +
  37898. + DBG(fsg, "transport=%s (x%02x)\n",
  37899. + mod_data.transport_name, mod_data.transport_type);
  37900. + DBG(fsg, "protocol=%s (x%02x)\n",
  37901. + mod_data.protocol_name, mod_data.protocol_type);
  37902. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  37903. + mod_data.vendor, mod_data.product, mod_data.release);
  37904. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  37905. + mod_data.removable, mod_data.can_stall,
  37906. + mod_data.cdrom, mod_data.buflen);
  37907. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  37908. +
  37909. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  37910. +
  37911. + /* Tell the thread to start working */
  37912. + wake_up_process(fsg->thread_task);
  37913. + return 0;
  37914. +
  37915. +autoconf_fail:
  37916. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  37917. + rc = -ENOTSUPP;
  37918. +
  37919. +out:
  37920. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  37921. + fsg_unbind(gadget);
  37922. + complete(&fsg->thread_notifier);
  37923. + return rc;
  37924. +}
  37925. +
  37926. +
  37927. +/*-------------------------------------------------------------------------*/
  37928. +
  37929. +static void fsg_suspend(struct usb_gadget *gadget)
  37930. +{
  37931. + struct fsg_dev *fsg = get_gadget_data(gadget);
  37932. +
  37933. + DBG(fsg, "suspend\n");
  37934. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  37935. +}
  37936. +
  37937. +static void fsg_resume(struct usb_gadget *gadget)
  37938. +{
  37939. + struct fsg_dev *fsg = get_gadget_data(gadget);
  37940. +
  37941. + DBG(fsg, "resume\n");
  37942. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  37943. +}
  37944. +
  37945. +
  37946. +/*-------------------------------------------------------------------------*/
  37947. +
  37948. +static struct usb_gadget_driver fsg_driver = {
  37949. + .max_speed = USB_SPEED_SUPER,
  37950. + .function = (char *) fsg_string_product,
  37951. + .unbind = fsg_unbind,
  37952. + .disconnect = fsg_disconnect,
  37953. + .setup = fsg_setup,
  37954. + .suspend = fsg_suspend,
  37955. + .resume = fsg_resume,
  37956. +
  37957. + .driver = {
  37958. + .name = DRIVER_NAME,
  37959. + .owner = THIS_MODULE,
  37960. + // .release = ...
  37961. + // .suspend = ...
  37962. + // .resume = ...
  37963. + },
  37964. +};
  37965. +
  37966. +
  37967. +static int __init fsg_alloc(void)
  37968. +{
  37969. + struct fsg_dev *fsg;
  37970. +
  37971. + fsg = kzalloc(sizeof *fsg +
  37972. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  37973. +
  37974. + if (!fsg)
  37975. + return -ENOMEM;
  37976. + spin_lock_init(&fsg->lock);
  37977. + init_rwsem(&fsg->filesem);
  37978. + kref_init(&fsg->ref);
  37979. + init_completion(&fsg->thread_notifier);
  37980. +
  37981. + the_fsg = fsg;
  37982. + return 0;
  37983. +}
  37984. +
  37985. +
  37986. +static int __init fsg_init(void)
  37987. +{
  37988. + int rc;
  37989. + struct fsg_dev *fsg;
  37990. +
  37991. + rc = fsg_num_buffers_validate();
  37992. + if (rc != 0)
  37993. + return rc;
  37994. +
  37995. + if ((rc = fsg_alloc()) != 0)
  37996. + return rc;
  37997. + fsg = the_fsg;
  37998. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  37999. + kref_put(&fsg->ref, fsg_release);
  38000. + return rc;
  38001. +}
  38002. +module_init(fsg_init);
  38003. +
  38004. +
  38005. +static void __exit fsg_cleanup(void)
  38006. +{
  38007. + struct fsg_dev *fsg = the_fsg;
  38008. +
  38009. + /* Unregister the driver iff the thread hasn't already done so */
  38010. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  38011. + usb_gadget_unregister_driver(&fsg_driver);
  38012. +
  38013. + /* Wait for the thread to finish up */
  38014. + wait_for_completion(&fsg->thread_notifier);
  38015. +
  38016. + kref_put(&fsg->ref, fsg_release);
  38017. +}
  38018. +module_exit(fsg_cleanup);
  38019. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/changes.txt linux-raspberry-pi/drivers/usb/host/dwc_common_port/changes.txt
  38020. --- linux-3.13.6/drivers/usb/host/dwc_common_port/changes.txt 1970-01-01 01:00:00.000000000 +0100
  38021. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/changes.txt 2014-03-11 16:53:12.000000000 +0100
  38022. @@ -0,0 +1,174 @@
  38023. +
  38024. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  38025. +IO context struct. The IO context struct should live in an os-dependent struct
  38026. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  38027. +named 'os_dep' embedded in the main device struct. So there these calls look
  38028. +like this:
  38029. +
  38030. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  38031. +
  38032. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  38033. + &pcd->dev_global_regs->dcfg, 0);
  38034. +
  38035. +Note that for the existing Linux driver ports, it is not necessary to actually
  38036. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  38037. +require an IO context, its macros for dwc_read_reg32() and friends do not
  38038. +use the context pointer, so it is optimized away by the compiler. But it is
  38039. +necessary to add the pointer parameter to all of the call sites, to be ready
  38040. +for any future ports (such as FreeBSD) which do require an IO context.
  38041. +
  38042. +
  38043. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  38044. +take an additional parameter, a pointer to a memory context. Examples:
  38045. +
  38046. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  38047. +
  38048. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  38049. +
  38050. +Again, for the Linux ports, it is not necessary to actually define the memctx
  38051. +member, but it is necessary to add the pointer parameter to all of the call
  38052. +sites.
  38053. +
  38054. +
  38055. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  38056. +
  38057. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  38058. +
  38059. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  38060. +
  38061. +
  38062. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  38063. +
  38064. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  38065. +
  38066. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  38067. +
  38068. +
  38069. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  38070. +
  38071. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  38072. +
  38073. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  38074. +
  38075. +
  38076. +Same for dwc_timer_alloc(). Example:
  38077. +
  38078. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  38079. + cb_func, cb_data);
  38080. +
  38081. +
  38082. +Same for dwc_waitq_alloc(). Example:
  38083. +
  38084. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  38085. +
  38086. +
  38087. +Same for dwc_thread_run(). Example:
  38088. +
  38089. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  38090. + "dwc_usb3_thd1", data);
  38091. +
  38092. +
  38093. +Same for dwc_workq_alloc(). Example:
  38094. +
  38095. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  38096. +
  38097. +
  38098. +Same for dwc_task_alloc(). Example:
  38099. +
  38100. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  38101. + cb_func, cb_data);
  38102. +
  38103. +
  38104. +In addition to the context pointer additions, a few core functions have had
  38105. +other changes made to their parameters:
  38106. +
  38107. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  38108. +has been changed from a uint64_t to a dwc_irqflags_t.
  38109. +
  38110. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  38111. +FreeBSD equivalent of that function requires it.
  38112. +
  38113. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  38114. +'char *name' parameter, to be consistent with dwc_thread_run() and
  38115. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  38116. +requires a unique name.
  38117. +
  38118. +
  38119. +Here is a complete list of the core functions that now take a pointer to a
  38120. +context as their first parameter:
  38121. +
  38122. + dwc_read_reg32
  38123. + dwc_read_reg64
  38124. + dwc_write_reg32
  38125. + dwc_write_reg64
  38126. + dwc_modify_reg32
  38127. + dwc_modify_reg64
  38128. + dwc_alloc
  38129. + dwc_alloc_atomic
  38130. + dwc_strdup
  38131. + dwc_free
  38132. + dwc_dma_alloc
  38133. + dwc_dma_free
  38134. + dwc_mutex_alloc
  38135. + dwc_mutex_free
  38136. + dwc_spinlock_alloc
  38137. + dwc_spinlock_free
  38138. + dwc_timer_alloc
  38139. + dwc_waitq_alloc
  38140. + dwc_thread_run
  38141. + dwc_workq_alloc
  38142. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  38143. +
  38144. +And here are the core functions that have other changes to their parameters:
  38145. +
  38146. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  38147. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  38148. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  38149. +
  38150. +
  38151. +
  38152. +The changes to the core functions also require some of the other library
  38153. +functions to change:
  38154. +
  38155. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  38156. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  38157. + (for mutex allocation) as the 2nd param.
  38158. +
  38159. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  38160. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  38161. + 'void *memctx' as the 1st param.
  38162. +
  38163. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  38164. + 'void *memctx' as the 1st param.
  38165. +
  38166. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  38167. +
  38168. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  38169. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  38170. + param, and also now returns an integer value that is non-zero if
  38171. + allocation of its data structures or work queue fails.
  38172. +
  38173. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  38174. +
  38175. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  38176. + param, and also now returns an integer value that is non-zero if
  38177. + allocation of its data structures fails.
  38178. +
  38179. +
  38180. +
  38181. +Other miscellaneous changes:
  38182. +
  38183. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  38184. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  38185. +
  38186. +The following #define's have been added to allow selectively compiling library
  38187. +features:
  38188. +
  38189. + DWC_CCLIB
  38190. + DWC_CRYPTOLIB
  38191. + DWC_NOTIFYLIB
  38192. + DWC_UTFLIB
  38193. +
  38194. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  38195. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  38196. +library code directly into a driver module, instead of as a standalone module.
  38197. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/doc/doxygen.cfg linux-raspberry-pi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  38198. --- linux-3.13.6/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 1970-01-01 01:00:00.000000000 +0100
  38199. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 2014-03-11 16:55:38.000000000 +0100
  38200. @@ -0,0 +1,270 @@
  38201. +# Doxyfile 1.4.5
  38202. +
  38203. +#---------------------------------------------------------------------------
  38204. +# Project related configuration options
  38205. +#---------------------------------------------------------------------------
  38206. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  38207. +PROJECT_NUMBER =
  38208. +OUTPUT_DIRECTORY = doc
  38209. +CREATE_SUBDIRS = NO
  38210. +OUTPUT_LANGUAGE = English
  38211. +BRIEF_MEMBER_DESC = YES
  38212. +REPEAT_BRIEF = YES
  38213. +ABBREVIATE_BRIEF = "The $name class" \
  38214. + "The $name widget" \
  38215. + "The $name file" \
  38216. + is \
  38217. + provides \
  38218. + specifies \
  38219. + contains \
  38220. + represents \
  38221. + a \
  38222. + an \
  38223. + the
  38224. +ALWAYS_DETAILED_SEC = YES
  38225. +INLINE_INHERITED_MEMB = NO
  38226. +FULL_PATH_NAMES = NO
  38227. +STRIP_FROM_PATH = ..
  38228. +STRIP_FROM_INC_PATH =
  38229. +SHORT_NAMES = NO
  38230. +JAVADOC_AUTOBRIEF = YES
  38231. +MULTILINE_CPP_IS_BRIEF = NO
  38232. +DETAILS_AT_TOP = YES
  38233. +INHERIT_DOCS = YES
  38234. +SEPARATE_MEMBER_PAGES = NO
  38235. +TAB_SIZE = 8
  38236. +ALIASES =
  38237. +OPTIMIZE_OUTPUT_FOR_C = YES
  38238. +OPTIMIZE_OUTPUT_JAVA = NO
  38239. +BUILTIN_STL_SUPPORT = NO
  38240. +DISTRIBUTE_GROUP_DOC = NO
  38241. +SUBGROUPING = NO
  38242. +#---------------------------------------------------------------------------
  38243. +# Build related configuration options
  38244. +#---------------------------------------------------------------------------
  38245. +EXTRACT_ALL = NO
  38246. +EXTRACT_PRIVATE = NO
  38247. +EXTRACT_STATIC = YES
  38248. +EXTRACT_LOCAL_CLASSES = NO
  38249. +EXTRACT_LOCAL_METHODS = NO
  38250. +HIDE_UNDOC_MEMBERS = NO
  38251. +HIDE_UNDOC_CLASSES = NO
  38252. +HIDE_FRIEND_COMPOUNDS = NO
  38253. +HIDE_IN_BODY_DOCS = NO
  38254. +INTERNAL_DOCS = NO
  38255. +CASE_SENSE_NAMES = YES
  38256. +HIDE_SCOPE_NAMES = NO
  38257. +SHOW_INCLUDE_FILES = NO
  38258. +INLINE_INFO = YES
  38259. +SORT_MEMBER_DOCS = NO
  38260. +SORT_BRIEF_DOCS = NO
  38261. +SORT_BY_SCOPE_NAME = NO
  38262. +GENERATE_TODOLIST = YES
  38263. +GENERATE_TESTLIST = YES
  38264. +GENERATE_BUGLIST = YES
  38265. +GENERATE_DEPRECATEDLIST= YES
  38266. +ENABLED_SECTIONS =
  38267. +MAX_INITIALIZER_LINES = 30
  38268. +SHOW_USED_FILES = YES
  38269. +SHOW_DIRECTORIES = YES
  38270. +FILE_VERSION_FILTER =
  38271. +#---------------------------------------------------------------------------
  38272. +# configuration options related to warning and progress messages
  38273. +#---------------------------------------------------------------------------
  38274. +QUIET = YES
  38275. +WARNINGS = YES
  38276. +WARN_IF_UNDOCUMENTED = NO
  38277. +WARN_IF_DOC_ERROR = YES
  38278. +WARN_NO_PARAMDOC = YES
  38279. +WARN_FORMAT = "$file:$line: $text"
  38280. +WARN_LOGFILE =
  38281. +#---------------------------------------------------------------------------
  38282. +# configuration options related to the input files
  38283. +#---------------------------------------------------------------------------
  38284. +INPUT = .
  38285. +FILE_PATTERNS = *.c \
  38286. + *.cc \
  38287. + *.cxx \
  38288. + *.cpp \
  38289. + *.c++ \
  38290. + *.d \
  38291. + *.java \
  38292. + *.ii \
  38293. + *.ixx \
  38294. + *.ipp \
  38295. + *.i++ \
  38296. + *.inl \
  38297. + *.h \
  38298. + *.hh \
  38299. + *.hxx \
  38300. + *.hpp \
  38301. + *.h++ \
  38302. + *.idl \
  38303. + *.odl \
  38304. + *.cs \
  38305. + *.php \
  38306. + *.php3 \
  38307. + *.inc \
  38308. + *.m \
  38309. + *.mm \
  38310. + *.dox \
  38311. + *.py \
  38312. + *.C \
  38313. + *.CC \
  38314. + *.C++ \
  38315. + *.II \
  38316. + *.I++ \
  38317. + *.H \
  38318. + *.HH \
  38319. + *.H++ \
  38320. + *.CS \
  38321. + *.PHP \
  38322. + *.PHP3 \
  38323. + *.M \
  38324. + *.MM \
  38325. + *.PY
  38326. +RECURSIVE = NO
  38327. +EXCLUDE =
  38328. +EXCLUDE_SYMLINKS = NO
  38329. +EXCLUDE_PATTERNS =
  38330. +EXAMPLE_PATH =
  38331. +EXAMPLE_PATTERNS = *
  38332. +EXAMPLE_RECURSIVE = NO
  38333. +IMAGE_PATH =
  38334. +INPUT_FILTER =
  38335. +FILTER_PATTERNS =
  38336. +FILTER_SOURCE_FILES = NO
  38337. +#---------------------------------------------------------------------------
  38338. +# configuration options related to source browsing
  38339. +#---------------------------------------------------------------------------
  38340. +SOURCE_BROWSER = NO
  38341. +INLINE_SOURCES = NO
  38342. +STRIP_CODE_COMMENTS = YES
  38343. +REFERENCED_BY_RELATION = YES
  38344. +REFERENCES_RELATION = YES
  38345. +USE_HTAGS = NO
  38346. +VERBATIM_HEADERS = NO
  38347. +#---------------------------------------------------------------------------
  38348. +# configuration options related to the alphabetical class index
  38349. +#---------------------------------------------------------------------------
  38350. +ALPHABETICAL_INDEX = NO
  38351. +COLS_IN_ALPHA_INDEX = 5
  38352. +IGNORE_PREFIX =
  38353. +#---------------------------------------------------------------------------
  38354. +# configuration options related to the HTML output
  38355. +#---------------------------------------------------------------------------
  38356. +GENERATE_HTML = YES
  38357. +HTML_OUTPUT = html
  38358. +HTML_FILE_EXTENSION = .html
  38359. +HTML_HEADER =
  38360. +HTML_FOOTER =
  38361. +HTML_STYLESHEET =
  38362. +HTML_ALIGN_MEMBERS = YES
  38363. +GENERATE_HTMLHELP = NO
  38364. +CHM_FILE =
  38365. +HHC_LOCATION =
  38366. +GENERATE_CHI = NO
  38367. +BINARY_TOC = NO
  38368. +TOC_EXPAND = NO
  38369. +DISABLE_INDEX = NO
  38370. +ENUM_VALUES_PER_LINE = 4
  38371. +GENERATE_TREEVIEW = YES
  38372. +TREEVIEW_WIDTH = 250
  38373. +#---------------------------------------------------------------------------
  38374. +# configuration options related to the LaTeX output
  38375. +#---------------------------------------------------------------------------
  38376. +GENERATE_LATEX = NO
  38377. +LATEX_OUTPUT = latex
  38378. +LATEX_CMD_NAME = latex
  38379. +MAKEINDEX_CMD_NAME = makeindex
  38380. +COMPACT_LATEX = NO
  38381. +PAPER_TYPE = a4wide
  38382. +EXTRA_PACKAGES =
  38383. +LATEX_HEADER =
  38384. +PDF_HYPERLINKS = NO
  38385. +USE_PDFLATEX = NO
  38386. +LATEX_BATCHMODE = NO
  38387. +LATEX_HIDE_INDICES = NO
  38388. +#---------------------------------------------------------------------------
  38389. +# configuration options related to the RTF output
  38390. +#---------------------------------------------------------------------------
  38391. +GENERATE_RTF = NO
  38392. +RTF_OUTPUT = rtf
  38393. +COMPACT_RTF = NO
  38394. +RTF_HYPERLINKS = NO
  38395. +RTF_STYLESHEET_FILE =
  38396. +RTF_EXTENSIONS_FILE =
  38397. +#---------------------------------------------------------------------------
  38398. +# configuration options related to the man page output
  38399. +#---------------------------------------------------------------------------
  38400. +GENERATE_MAN = NO
  38401. +MAN_OUTPUT = man
  38402. +MAN_EXTENSION = .3
  38403. +MAN_LINKS = NO
  38404. +#---------------------------------------------------------------------------
  38405. +# configuration options related to the XML output
  38406. +#---------------------------------------------------------------------------
  38407. +GENERATE_XML = NO
  38408. +XML_OUTPUT = xml
  38409. +XML_SCHEMA =
  38410. +XML_DTD =
  38411. +XML_PROGRAMLISTING = YES
  38412. +#---------------------------------------------------------------------------
  38413. +# configuration options for the AutoGen Definitions output
  38414. +#---------------------------------------------------------------------------
  38415. +GENERATE_AUTOGEN_DEF = NO
  38416. +#---------------------------------------------------------------------------
  38417. +# configuration options related to the Perl module output
  38418. +#---------------------------------------------------------------------------
  38419. +GENERATE_PERLMOD = NO
  38420. +PERLMOD_LATEX = NO
  38421. +PERLMOD_PRETTY = YES
  38422. +PERLMOD_MAKEVAR_PREFIX =
  38423. +#---------------------------------------------------------------------------
  38424. +# Configuration options related to the preprocessor
  38425. +#---------------------------------------------------------------------------
  38426. +ENABLE_PREPROCESSING = YES
  38427. +MACRO_EXPANSION = NO
  38428. +EXPAND_ONLY_PREDEF = NO
  38429. +SEARCH_INCLUDES = YES
  38430. +INCLUDE_PATH =
  38431. +INCLUDE_FILE_PATTERNS =
  38432. +PREDEFINED = DEBUG DEBUG_MEMORY
  38433. +EXPAND_AS_DEFINED =
  38434. +SKIP_FUNCTION_MACROS = YES
  38435. +#---------------------------------------------------------------------------
  38436. +# Configuration::additions related to external references
  38437. +#---------------------------------------------------------------------------
  38438. +TAGFILES =
  38439. +GENERATE_TAGFILE =
  38440. +ALLEXTERNALS = NO
  38441. +EXTERNAL_GROUPS = YES
  38442. +PERL_PATH = /usr/bin/perl
  38443. +#---------------------------------------------------------------------------
  38444. +# Configuration options related to the dot tool
  38445. +#---------------------------------------------------------------------------
  38446. +CLASS_DIAGRAMS = YES
  38447. +HIDE_UNDOC_RELATIONS = YES
  38448. +HAVE_DOT = NO
  38449. +CLASS_GRAPH = YES
  38450. +COLLABORATION_GRAPH = YES
  38451. +GROUP_GRAPHS = YES
  38452. +UML_LOOK = NO
  38453. +TEMPLATE_RELATIONS = NO
  38454. +INCLUDE_GRAPH = NO
  38455. +INCLUDED_BY_GRAPH = YES
  38456. +CALL_GRAPH = NO
  38457. +GRAPHICAL_HIERARCHY = YES
  38458. +DIRECTORY_GRAPH = YES
  38459. +DOT_IMAGE_FORMAT = png
  38460. +DOT_PATH =
  38461. +DOTFILE_DIRS =
  38462. +MAX_DOT_GRAPH_DEPTH = 1000
  38463. +DOT_TRANSPARENT = NO
  38464. +DOT_MULTI_TARGETS = NO
  38465. +GENERATE_LEGEND = YES
  38466. +DOT_CLEANUP = YES
  38467. +#---------------------------------------------------------------------------
  38468. +# Configuration::additions related to the search engine
  38469. +#---------------------------------------------------------------------------
  38470. +SEARCHENGINE = NO
  38471. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_cc.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_cc.c
  38472. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_cc.c 1970-01-01 01:00:00.000000000 +0100
  38473. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_cc.c 2014-03-11 16:55:38.000000000 +0100
  38474. @@ -0,0 +1,532 @@
  38475. +/* =========================================================================
  38476. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  38477. + * $Revision: #4 $
  38478. + * $Date: 2010/11/04 $
  38479. + * $Change: 1621692 $
  38480. + *
  38481. + * Synopsys Portability Library Software and documentation
  38482. + * (hereinafter, "Software") is an Unsupported proprietary work of
  38483. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  38484. + * between Synopsys and you.
  38485. + *
  38486. + * The Software IS NOT an item of Licensed Software or Licensed Product
  38487. + * under any End User Software License Agreement or Agreement for
  38488. + * Licensed Product with Synopsys or any supplement thereto. You are
  38489. + * permitted to use and redistribute this Software in source and binary
  38490. + * forms, with or without modification, provided that redistributions
  38491. + * of source code must retain this notice. You may not view, use,
  38492. + * disclose, copy or distribute this file or any information contained
  38493. + * herein except pursuant to this license grant from Synopsys. If you
  38494. + * do not agree with this notice, including the disclaimer below, then
  38495. + * you are not authorized to use the Software.
  38496. + *
  38497. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  38498. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38499. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  38500. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  38501. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38502. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  38503. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  38504. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  38505. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38506. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  38507. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  38508. + * DAMAGE.
  38509. + * ========================================================================= */
  38510. +#ifdef DWC_CCLIB
  38511. +
  38512. +#include "dwc_cc.h"
  38513. +
  38514. +typedef struct dwc_cc
  38515. +{
  38516. + uint32_t uid;
  38517. + uint8_t chid[16];
  38518. + uint8_t cdid[16];
  38519. + uint8_t ck[16];
  38520. + uint8_t *name;
  38521. + uint8_t length;
  38522. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  38523. +} dwc_cc_t;
  38524. +
  38525. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  38526. +
  38527. +/** The main structure for CC management. */
  38528. +struct dwc_cc_if
  38529. +{
  38530. + dwc_mutex_t *mutex;
  38531. + char *filename;
  38532. +
  38533. + unsigned is_host:1;
  38534. +
  38535. + dwc_notifier_t *notifier;
  38536. +
  38537. + struct context_list list;
  38538. +};
  38539. +
  38540. +#ifdef DEBUG
  38541. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  38542. +{
  38543. + int i;
  38544. + DWC_PRINTF("%s: ", name);
  38545. + for (i=0; i<len; i++) {
  38546. + DWC_PRINTF("%02x ", bytes[i]);
  38547. + }
  38548. + DWC_PRINTF("\n");
  38549. +}
  38550. +#else
  38551. +#define dump_bytes(x...)
  38552. +#endif
  38553. +
  38554. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  38555. +{
  38556. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  38557. + if (!cc) {
  38558. + return NULL;
  38559. + }
  38560. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  38561. +
  38562. + if (name) {
  38563. + cc->length = length;
  38564. + cc->name = dwc_alloc(mem_ctx, length);
  38565. + if (!cc->name) {
  38566. + dwc_free(mem_ctx, cc);
  38567. + return NULL;
  38568. + }
  38569. +
  38570. + DWC_MEMCPY(cc->name, name, length);
  38571. + }
  38572. +
  38573. + return cc;
  38574. +}
  38575. +
  38576. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  38577. +{
  38578. + if (cc->name) {
  38579. + dwc_free(mem_ctx, cc->name);
  38580. + }
  38581. + dwc_free(mem_ctx, cc);
  38582. +}
  38583. +
  38584. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  38585. +{
  38586. + uint32_t uid = 0;
  38587. + dwc_cc_t *cc;
  38588. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  38589. + if (cc->uid > uid) {
  38590. + uid = cc->uid;
  38591. + }
  38592. + }
  38593. +
  38594. + if (uid == 0) {
  38595. + uid = 255;
  38596. + }
  38597. +
  38598. + return uid + 1;
  38599. +}
  38600. +
  38601. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  38602. +{
  38603. + dwc_cc_t *cc;
  38604. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  38605. + if (cc->uid == uid) {
  38606. + return cc;
  38607. + }
  38608. + }
  38609. + return NULL;
  38610. +}
  38611. +
  38612. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  38613. +{
  38614. + unsigned int size = 0;
  38615. + dwc_cc_t *cc;
  38616. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  38617. + size += (48 + 1);
  38618. + if (cc->name) {
  38619. + size += cc->length;
  38620. + }
  38621. + }
  38622. + return size;
  38623. +}
  38624. +
  38625. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  38626. +{
  38627. + uint32_t uid = 0;
  38628. + dwc_cc_t *cc;
  38629. +
  38630. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  38631. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  38632. + uid = cc->uid;
  38633. + break;
  38634. + }
  38635. + }
  38636. + return uid;
  38637. +}
  38638. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  38639. +{
  38640. + uint32_t uid = 0;
  38641. + dwc_cc_t *cc;
  38642. +
  38643. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  38644. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  38645. + uid = cc->uid;
  38646. + break;
  38647. + }
  38648. + }
  38649. + return uid;
  38650. +}
  38651. +
  38652. +/* Internal cc_add */
  38653. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  38654. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  38655. +{
  38656. + dwc_cc_t *cc;
  38657. + uint32_t uid;
  38658. +
  38659. + if (cc_if->is_host) {
  38660. + uid = cc_match_cdid(cc_if, cdid);
  38661. + }
  38662. + else {
  38663. + uid = cc_match_chid(cc_if, chid);
  38664. + }
  38665. +
  38666. + if (uid) {
  38667. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  38668. + cc = cc_find(cc_if, uid);
  38669. + }
  38670. + else {
  38671. + cc = alloc_cc(mem_ctx, name, length);
  38672. + cc->uid = next_uid(cc_if);
  38673. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  38674. + }
  38675. +
  38676. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  38677. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  38678. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  38679. +
  38680. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  38681. + dump_bytes("CHID", cc->chid, 16);
  38682. + dump_bytes("CDID", cc->cdid, 16);
  38683. + dump_bytes("CK", cc->ck, 16);
  38684. + return cc->uid;
  38685. +}
  38686. +
  38687. +/* Internal cc_clear */
  38688. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  38689. +{
  38690. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  38691. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  38692. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  38693. + free_cc(mem_ctx, cc);
  38694. + }
  38695. +}
  38696. +
  38697. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  38698. + dwc_notifier_t *notifier, unsigned is_host)
  38699. +{
  38700. + dwc_cc_if_t *cc_if = NULL;
  38701. +
  38702. + /* Allocate a common_cc_if structure */
  38703. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  38704. +
  38705. + if (!cc_if)
  38706. + return NULL;
  38707. +
  38708. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  38709. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  38710. +#else
  38711. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  38712. +#endif
  38713. + if (!cc_if->mutex) {
  38714. + dwc_free(mem_ctx, cc_if);
  38715. + return NULL;
  38716. + }
  38717. +
  38718. + DWC_CIRCLEQ_INIT(&cc_if->list);
  38719. + cc_if->is_host = is_host;
  38720. + cc_if->notifier = notifier;
  38721. + return cc_if;
  38722. +}
  38723. +
  38724. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  38725. +{
  38726. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  38727. + DWC_MUTEX_FREE(cc_if->mutex);
  38728. +#else
  38729. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  38730. +#endif
  38731. + cc_clear(mem_ctx, cc_if);
  38732. + dwc_free(mem_ctx, cc_if);
  38733. +}
  38734. +
  38735. +static void cc_changed(dwc_cc_if_t *cc_if)
  38736. +{
  38737. + if (cc_if->notifier) {
  38738. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  38739. + }
  38740. +}
  38741. +
  38742. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  38743. +{
  38744. + DWC_MUTEX_LOCK(cc_if->mutex);
  38745. + cc_clear(mem_ctx, cc_if);
  38746. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38747. + cc_changed(cc_if);
  38748. +}
  38749. +
  38750. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  38751. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  38752. +{
  38753. + uint32_t uid;
  38754. +
  38755. + DWC_MUTEX_LOCK(cc_if->mutex);
  38756. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  38757. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38758. + cc_changed(cc_if);
  38759. +
  38760. + return uid;
  38761. +}
  38762. +
  38763. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  38764. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  38765. +{
  38766. + dwc_cc_t* cc;
  38767. +
  38768. + DWC_DEBUGC("Change connection context %d", id);
  38769. +
  38770. + DWC_MUTEX_LOCK(cc_if->mutex);
  38771. + cc = cc_find(cc_if, id);
  38772. + if (!cc) {
  38773. + DWC_ERROR("Uid %d not found in cc list\n", id);
  38774. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38775. + return;
  38776. + }
  38777. +
  38778. + if (chid) {
  38779. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  38780. + }
  38781. + if (cdid) {
  38782. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  38783. + }
  38784. + if (ck) {
  38785. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  38786. + }
  38787. +
  38788. + if (name) {
  38789. + if (cc->name) {
  38790. + dwc_free(mem_ctx, cc->name);
  38791. + }
  38792. + cc->name = dwc_alloc(mem_ctx, length);
  38793. + if (!cc->name) {
  38794. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  38795. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38796. + return;
  38797. + }
  38798. + cc->length = length;
  38799. + DWC_MEMCPY(cc->name, name, length);
  38800. + }
  38801. +
  38802. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38803. +
  38804. + cc_changed(cc_if);
  38805. +
  38806. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  38807. + dump_bytes("New CHID", cc->chid, 16);
  38808. + dump_bytes("New CDID", cc->cdid, 16);
  38809. + dump_bytes("New CK", cc->ck, 16);
  38810. +}
  38811. +
  38812. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  38813. +{
  38814. + dwc_cc_t *cc;
  38815. +
  38816. + DWC_DEBUGC("Removing connection context %d", id);
  38817. +
  38818. + DWC_MUTEX_LOCK(cc_if->mutex);
  38819. + cc = cc_find(cc_if, id);
  38820. + if (!cc) {
  38821. + DWC_ERROR("Uid %d not found in cc list\n", id);
  38822. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38823. + return;
  38824. + }
  38825. +
  38826. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  38827. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38828. + free_cc(mem_ctx, cc);
  38829. +
  38830. + cc_changed(cc_if);
  38831. +}
  38832. +
  38833. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  38834. +{
  38835. + uint8_t *buf, *x;
  38836. + uint8_t zero = 0;
  38837. + dwc_cc_t *cc;
  38838. +
  38839. + DWC_MUTEX_LOCK(cc_if->mutex);
  38840. + *length = cc_data_size(cc_if);
  38841. + if (!(*length)) {
  38842. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38843. + return NULL;
  38844. + }
  38845. +
  38846. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  38847. +
  38848. + buf = dwc_alloc(mem_ctx, *length);
  38849. + if (!buf) {
  38850. + *length = 0;
  38851. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38852. + return NULL;
  38853. + }
  38854. +
  38855. + x = buf;
  38856. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  38857. + DWC_MEMCPY(x, cc->chid, 16);
  38858. + x += 16;
  38859. + DWC_MEMCPY(x, cc->cdid, 16);
  38860. + x += 16;
  38861. + DWC_MEMCPY(x, cc->ck, 16);
  38862. + x += 16;
  38863. + if (cc->name) {
  38864. + DWC_MEMCPY(x, &cc->length, 1);
  38865. + x += 1;
  38866. + DWC_MEMCPY(x, cc->name, cc->length);
  38867. + x += cc->length;
  38868. + }
  38869. + else {
  38870. + DWC_MEMCPY(x, &zero, 1);
  38871. + x += 1;
  38872. + }
  38873. + }
  38874. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38875. +
  38876. + return buf;
  38877. +}
  38878. +
  38879. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  38880. +{
  38881. + uint8_t name_length;
  38882. + uint8_t *name;
  38883. + uint8_t *chid;
  38884. + uint8_t *cdid;
  38885. + uint8_t *ck;
  38886. + uint32_t i = 0;
  38887. +
  38888. + DWC_MUTEX_LOCK(cc_if->mutex);
  38889. + cc_clear(mem_ctx, cc_if);
  38890. +
  38891. + while (i < length) {
  38892. + chid = &data[i];
  38893. + i += 16;
  38894. + cdid = &data[i];
  38895. + i += 16;
  38896. + ck = &data[i];
  38897. + i += 16;
  38898. +
  38899. + name_length = data[i];
  38900. + i ++;
  38901. +
  38902. + if (name_length) {
  38903. + name = &data[i];
  38904. + i += name_length;
  38905. + }
  38906. + else {
  38907. + name = NULL;
  38908. + }
  38909. +
  38910. + /* check to see if we haven't overflown the buffer */
  38911. + if (i > length) {
  38912. + DWC_ERROR("Data format error while attempting to load CCs "
  38913. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  38914. + break;
  38915. + }
  38916. +
  38917. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  38918. + }
  38919. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38920. +
  38921. + cc_changed(cc_if);
  38922. +}
  38923. +
  38924. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  38925. +{
  38926. + uint32_t uid = 0;
  38927. +
  38928. + DWC_MUTEX_LOCK(cc_if->mutex);
  38929. + uid = cc_match_chid(cc_if, chid);
  38930. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38931. + return uid;
  38932. +}
  38933. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  38934. +{
  38935. + uint32_t uid = 0;
  38936. +
  38937. + DWC_MUTEX_LOCK(cc_if->mutex);
  38938. + uid = cc_match_cdid(cc_if, cdid);
  38939. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38940. + return uid;
  38941. +}
  38942. +
  38943. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  38944. +{
  38945. + uint8_t *ck = NULL;
  38946. + dwc_cc_t *cc;
  38947. +
  38948. + DWC_MUTEX_LOCK(cc_if->mutex);
  38949. + cc = cc_find(cc_if, id);
  38950. + if (cc) {
  38951. + ck = cc->ck;
  38952. + }
  38953. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38954. +
  38955. + return ck;
  38956. +
  38957. +}
  38958. +
  38959. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  38960. +{
  38961. + uint8_t *retval = NULL;
  38962. + dwc_cc_t *cc;
  38963. +
  38964. + DWC_MUTEX_LOCK(cc_if->mutex);
  38965. + cc = cc_find(cc_if, id);
  38966. + if (cc) {
  38967. + retval = cc->chid;
  38968. + }
  38969. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38970. +
  38971. + return retval;
  38972. +}
  38973. +
  38974. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  38975. +{
  38976. + uint8_t *retval = NULL;
  38977. + dwc_cc_t *cc;
  38978. +
  38979. + DWC_MUTEX_LOCK(cc_if->mutex);
  38980. + cc = cc_find(cc_if, id);
  38981. + if (cc) {
  38982. + retval = cc->cdid;
  38983. + }
  38984. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  38985. +
  38986. + return retval;
  38987. +}
  38988. +
  38989. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  38990. +{
  38991. + uint8_t *retval = NULL;
  38992. + dwc_cc_t *cc;
  38993. +
  38994. + DWC_MUTEX_LOCK(cc_if->mutex);
  38995. + *length = 0;
  38996. + cc = cc_find(cc_if, id);
  38997. + if (cc) {
  38998. + *length = cc->length;
  38999. + retval = cc->name;
  39000. + }
  39001. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  39002. +
  39003. + return retval;
  39004. +}
  39005. +
  39006. +#endif /* DWC_CCLIB */
  39007. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_cc.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_cc.h
  39008. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_cc.h 1970-01-01 01:00:00.000000000 +0100
  39009. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_cc.h 2014-03-11 16:55:38.000000000 +0100
  39010. @@ -0,0 +1,224 @@
  39011. +/* =========================================================================
  39012. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  39013. + * $Revision: #4 $
  39014. + * $Date: 2010/09/28 $
  39015. + * $Change: 1596182 $
  39016. + *
  39017. + * Synopsys Portability Library Software and documentation
  39018. + * (hereinafter, "Software") is an Unsupported proprietary work of
  39019. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  39020. + * between Synopsys and you.
  39021. + *
  39022. + * The Software IS NOT an item of Licensed Software or Licensed Product
  39023. + * under any End User Software License Agreement or Agreement for
  39024. + * Licensed Product with Synopsys or any supplement thereto. You are
  39025. + * permitted to use and redistribute this Software in source and binary
  39026. + * forms, with or without modification, provided that redistributions
  39027. + * of source code must retain this notice. You may not view, use,
  39028. + * disclose, copy or distribute this file or any information contained
  39029. + * herein except pursuant to this license grant from Synopsys. If you
  39030. + * do not agree with this notice, including the disclaimer below, then
  39031. + * you are not authorized to use the Software.
  39032. + *
  39033. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  39034. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  39035. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39036. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  39037. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  39038. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  39039. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  39040. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  39041. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39042. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39043. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  39044. + * DAMAGE.
  39045. + * ========================================================================= */
  39046. +#ifndef _DWC_CC_H_
  39047. +#define _DWC_CC_H_
  39048. +
  39049. +#ifdef __cplusplus
  39050. +extern "C" {
  39051. +#endif
  39052. +
  39053. +/** @file
  39054. + *
  39055. + * This file defines the Context Context library.
  39056. + *
  39057. + * The main data structure is dwc_cc_if_t which is returned by either the
  39058. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  39059. + * function. The data structure is opaque and should only be manipulated via the
  39060. + * functions provied in this API.
  39061. + *
  39062. + * It manages a list of connection contexts and operations can be performed to
  39063. + * add, remove, query, search, and change, those contexts. Additionally,
  39064. + * a dwc_notifier_t object can be requested from the manager so that
  39065. + * the user can be notified whenever the context list has changed.
  39066. + */
  39067. +
  39068. +#include "dwc_os.h"
  39069. +#include "dwc_list.h"
  39070. +#include "dwc_notifier.h"
  39071. +
  39072. +
  39073. +/* Notifications */
  39074. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  39075. +
  39076. +struct dwc_cc_if;
  39077. +typedef struct dwc_cc_if dwc_cc_if_t;
  39078. +
  39079. +
  39080. +/** @name Connection Context Operations */
  39081. +/** @{ */
  39082. +
  39083. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  39084. + * fields to default values, and returns a pointer to the structure or NULL on
  39085. + * error. */
  39086. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  39087. + dwc_notifier_t *notifier, unsigned is_host);
  39088. +
  39089. +/** Frees the memory for the specified CC structure allocated from
  39090. + * dwc_cc_if_alloc(). */
  39091. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  39092. +
  39093. +/** Removes all contexts from the connection context list */
  39094. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  39095. +
  39096. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  39097. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  39098. + * not overwritten.
  39099. + *
  39100. + * @param cc_if The cc_if structure.
  39101. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  39102. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  39103. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  39104. + * @param name An optional host friendly name as defined in the association model
  39105. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  39106. + * @param length The length othe unicode string.
  39107. + * @return A unique identifier used to refer to this context that is valid for
  39108. + * as long as this context is still in the list. */
  39109. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  39110. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  39111. + uint8_t length);
  39112. +
  39113. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  39114. + * list, preserving any accumulated statistics. This would typically be called
  39115. + * if the host decideds to change the context with a SET_CONNECTION request.
  39116. + *
  39117. + * @param cc_if The cc_if structure.
  39118. + * @param id The identifier of the connection context.
  39119. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  39120. + * indicates no change.
  39121. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  39122. + * indicates no change.
  39123. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  39124. + * indicates no change.
  39125. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  39126. + * @param length Length of name. */
  39127. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  39128. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  39129. + uint8_t *name, uint8_t length);
  39130. +
  39131. +/** Remove the specified connection context.
  39132. + * @param cc_if The cc_if structure.
  39133. + * @param id The identifier of the connection context to remove. */
  39134. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  39135. +
  39136. +/** Get a binary block of data for the connection context list and attributes.
  39137. + * This data can be used by the OS specific driver to save the connection
  39138. + * context list into non-volatile memory.
  39139. + *
  39140. + * @param cc_if The cc_if structure.
  39141. + * @param length Return the length of the data buffer.
  39142. + * @return A pointer to the data buffer. The memory for this buffer should be
  39143. + * freed with DWC_FREE() after use. */
  39144. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  39145. + unsigned int *length);
  39146. +
  39147. +/** Restore the connection context list from the binary data that was previously
  39148. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  39149. + * driver to load a connection context list from non-volatile memory.
  39150. + *
  39151. + * @param cc_if The cc_if structure.
  39152. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  39153. + * @param length The length of the data. */
  39154. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  39155. + uint8_t *data, unsigned int length);
  39156. +
  39157. +/** Find the connection context from the specified CHID.
  39158. + *
  39159. + * @param cc_if The cc_if structure.
  39160. + * @param chid A pointer to the CHID data.
  39161. + * @return A non-zero identifier of the connection context if the CHID matches.
  39162. + * Otherwise returns 0. */
  39163. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  39164. +
  39165. +/** Find the connection context from the specified CDID.
  39166. + *
  39167. + * @param cc_if The cc_if structure.
  39168. + * @param cdid A pointer to the CDID data.
  39169. + * @return A non-zero identifier of the connection context if the CHID matches.
  39170. + * Otherwise returns 0. */
  39171. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  39172. +
  39173. +/** Retrieve the CK from the specified connection context.
  39174. + *
  39175. + * @param cc_if The cc_if structure.
  39176. + * @param id The identifier of the connection context.
  39177. + * @return A pointer to the CK data. The memory does not need to be freed. */
  39178. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  39179. +
  39180. +/** Retrieve the CHID from the specified connection context.
  39181. + *
  39182. + * @param cc_if The cc_if structure.
  39183. + * @param id The identifier of the connection context.
  39184. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  39185. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  39186. +
  39187. +/** Retrieve the CDID from the specified connection context.
  39188. + *
  39189. + * @param cc_if The cc_if structure.
  39190. + * @param id The identifier of the connection context.
  39191. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  39192. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  39193. +
  39194. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  39195. +
  39196. +/** Checks a buffer for non-zero.
  39197. + * @param id A pointer to a 16 byte buffer.
  39198. + * @return true if the 16 byte value is non-zero. */
  39199. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  39200. + int i;
  39201. + for (i=0; i<16; i++) {
  39202. + if (id[i]) return 1;
  39203. + }
  39204. + return 0;
  39205. +}
  39206. +
  39207. +/** Checks a buffer for zero.
  39208. + * @param id A pointer to a 16 byte buffer.
  39209. + * @return true if the 16 byte value is zero. */
  39210. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  39211. + return !dwc_assoc_is_not_zero_id(id);
  39212. +}
  39213. +
  39214. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  39215. + * buffer. */
  39216. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  39217. + char *ptr = buffer;
  39218. + int i;
  39219. + for (i=0; i<16; i++) {
  39220. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  39221. + if (i < 15) {
  39222. + ptr += DWC_SPRINTF(ptr, " ");
  39223. + }
  39224. + }
  39225. + return ptr - buffer;
  39226. +}
  39227. +
  39228. +/** @} */
  39229. +
  39230. +#ifdef __cplusplus
  39231. +}
  39232. +#endif
  39233. +
  39234. +#endif /* _DWC_CC_H_ */
  39235. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  39236. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 1970-01-01 01:00:00.000000000 +0100
  39237. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 2014-03-11 16:53:12.000000000 +0100
  39238. @@ -0,0 +1,1308 @@
  39239. +#include "dwc_os.h"
  39240. +#include "dwc_list.h"
  39241. +
  39242. +#ifdef DWC_CCLIB
  39243. +# include "dwc_cc.h"
  39244. +#endif
  39245. +
  39246. +#ifdef DWC_CRYPTOLIB
  39247. +# include "dwc_modpow.h"
  39248. +# include "dwc_dh.h"
  39249. +# include "dwc_crypto.h"
  39250. +#endif
  39251. +
  39252. +#ifdef DWC_NOTIFYLIB
  39253. +# include "dwc_notifier.h"
  39254. +#endif
  39255. +
  39256. +/* OS-Level Implementations */
  39257. +
  39258. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  39259. +
  39260. +
  39261. +/* MISC */
  39262. +
  39263. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  39264. +{
  39265. + return memset(dest, byte, size);
  39266. +}
  39267. +
  39268. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  39269. +{
  39270. + return memcpy(dest, src, size);
  39271. +}
  39272. +
  39273. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  39274. +{
  39275. + bcopy(src, dest, size);
  39276. + return dest;
  39277. +}
  39278. +
  39279. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  39280. +{
  39281. + return memcmp(m1, m2, size);
  39282. +}
  39283. +
  39284. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  39285. +{
  39286. + return strncmp(s1, s2, size);
  39287. +}
  39288. +
  39289. +int DWC_STRCMP(void *s1, void *s2)
  39290. +{
  39291. + return strcmp(s1, s2);
  39292. +}
  39293. +
  39294. +int DWC_STRLEN(char const *str)
  39295. +{
  39296. + return strlen(str);
  39297. +}
  39298. +
  39299. +char *DWC_STRCPY(char *to, char const *from)
  39300. +{
  39301. + return strcpy(to, from);
  39302. +}
  39303. +
  39304. +char *DWC_STRDUP(char const *str)
  39305. +{
  39306. + int len = DWC_STRLEN(str) + 1;
  39307. + char *new = DWC_ALLOC_ATOMIC(len);
  39308. +
  39309. + if (!new) {
  39310. + return NULL;
  39311. + }
  39312. +
  39313. + DWC_MEMCPY(new, str, len);
  39314. + return new;
  39315. +}
  39316. +
  39317. +int DWC_ATOI(char *str, int32_t *value)
  39318. +{
  39319. + char *end = NULL;
  39320. +
  39321. + *value = strtol(str, &end, 0);
  39322. + if (*end == '\0') {
  39323. + return 0;
  39324. + }
  39325. +
  39326. + return -1;
  39327. +}
  39328. +
  39329. +int DWC_ATOUI(char *str, uint32_t *value)
  39330. +{
  39331. + char *end = NULL;
  39332. +
  39333. + *value = strtoul(str, &end, 0);
  39334. + if (*end == '\0') {
  39335. + return 0;
  39336. + }
  39337. +
  39338. + return -1;
  39339. +}
  39340. +
  39341. +
  39342. +#ifdef DWC_UTFLIB
  39343. +/* From usbstring.c */
  39344. +
  39345. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  39346. +{
  39347. + int count = 0;
  39348. + u8 c;
  39349. + u16 uchar;
  39350. +
  39351. + /* this insists on correct encodings, though not minimal ones.
  39352. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  39353. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  39354. + */
  39355. + while (len != 0 && (c = (u8) *s++) != 0) {
  39356. + if (unlikely(c & 0x80)) {
  39357. + // 2-byte sequence:
  39358. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  39359. + if ((c & 0xe0) == 0xc0) {
  39360. + uchar = (c & 0x1f) << 6;
  39361. +
  39362. + c = (u8) *s++;
  39363. + if ((c & 0xc0) != 0xc0)
  39364. + goto fail;
  39365. + c &= 0x3f;
  39366. + uchar |= c;
  39367. +
  39368. + // 3-byte sequence (most CJKV characters):
  39369. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  39370. + } else if ((c & 0xf0) == 0xe0) {
  39371. + uchar = (c & 0x0f) << 12;
  39372. +
  39373. + c = (u8) *s++;
  39374. + if ((c & 0xc0) != 0xc0)
  39375. + goto fail;
  39376. + c &= 0x3f;
  39377. + uchar |= c << 6;
  39378. +
  39379. + c = (u8) *s++;
  39380. + if ((c & 0xc0) != 0xc0)
  39381. + goto fail;
  39382. + c &= 0x3f;
  39383. + uchar |= c;
  39384. +
  39385. + /* no bogus surrogates */
  39386. + if (0xd800 <= uchar && uchar <= 0xdfff)
  39387. + goto fail;
  39388. +
  39389. + // 4-byte sequence (surrogate pairs, currently rare):
  39390. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  39391. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  39392. + // (uuuuu = wwww + 1)
  39393. + // FIXME accept the surrogate code points (only)
  39394. + } else
  39395. + goto fail;
  39396. + } else
  39397. + uchar = c;
  39398. + put_unaligned (cpu_to_le16 (uchar), cp++);
  39399. + count++;
  39400. + len--;
  39401. + }
  39402. + return count;
  39403. +fail:
  39404. + return -1;
  39405. +}
  39406. +
  39407. +#endif /* DWC_UTFLIB */
  39408. +
  39409. +
  39410. +/* dwc_debug.h */
  39411. +
  39412. +dwc_bool_t DWC_IN_IRQ(void)
  39413. +{
  39414. +// return in_irq();
  39415. + return 0;
  39416. +}
  39417. +
  39418. +dwc_bool_t DWC_IN_BH(void)
  39419. +{
  39420. +// return in_softirq();
  39421. + return 0;
  39422. +}
  39423. +
  39424. +void DWC_VPRINTF(char *format, va_list args)
  39425. +{
  39426. + vprintf(format, args);
  39427. +}
  39428. +
  39429. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  39430. +{
  39431. + return vsnprintf(str, size, format, args);
  39432. +}
  39433. +
  39434. +void DWC_PRINTF(char *format, ...)
  39435. +{
  39436. + va_list args;
  39437. +
  39438. + va_start(args, format);
  39439. + DWC_VPRINTF(format, args);
  39440. + va_end(args);
  39441. +}
  39442. +
  39443. +int DWC_SPRINTF(char *buffer, char *format, ...)
  39444. +{
  39445. + int retval;
  39446. + va_list args;
  39447. +
  39448. + va_start(args, format);
  39449. + retval = vsprintf(buffer, format, args);
  39450. + va_end(args);
  39451. + return retval;
  39452. +}
  39453. +
  39454. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  39455. +{
  39456. + int retval;
  39457. + va_list args;
  39458. +
  39459. + va_start(args, format);
  39460. + retval = vsnprintf(buffer, size, format, args);
  39461. + va_end(args);
  39462. + return retval;
  39463. +}
  39464. +
  39465. +void __DWC_WARN(char *format, ...)
  39466. +{
  39467. + va_list args;
  39468. +
  39469. + va_start(args, format);
  39470. + DWC_VPRINTF(format, args);
  39471. + va_end(args);
  39472. +}
  39473. +
  39474. +void __DWC_ERROR(char *format, ...)
  39475. +{
  39476. + va_list args;
  39477. +
  39478. + va_start(args, format);
  39479. + DWC_VPRINTF(format, args);
  39480. + va_end(args);
  39481. +}
  39482. +
  39483. +void DWC_EXCEPTION(char *format, ...)
  39484. +{
  39485. + va_list args;
  39486. +
  39487. + va_start(args, format);
  39488. + DWC_VPRINTF(format, args);
  39489. + va_end(args);
  39490. +// BUG_ON(1); ???
  39491. +}
  39492. +
  39493. +#ifdef DEBUG
  39494. +void __DWC_DEBUG(char *format, ...)
  39495. +{
  39496. + va_list args;
  39497. +
  39498. + va_start(args, format);
  39499. + DWC_VPRINTF(format, args);
  39500. + va_end(args);
  39501. +}
  39502. +#endif
  39503. +
  39504. +
  39505. +/* dwc_mem.h */
  39506. +
  39507. +#if 0
  39508. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  39509. + uint32_t align,
  39510. + uint32_t alloc)
  39511. +{
  39512. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  39513. + size, align, alloc);
  39514. + return (dwc_pool_t *)pool;
  39515. +}
  39516. +
  39517. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  39518. +{
  39519. + dma_pool_destroy((struct dma_pool *)pool);
  39520. +}
  39521. +
  39522. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  39523. +{
  39524. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  39525. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  39526. +}
  39527. +
  39528. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  39529. +{
  39530. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  39531. + memset(..);
  39532. +}
  39533. +
  39534. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  39535. +{
  39536. + dma_pool_free(pool, vaddr, daddr);
  39537. +}
  39538. +#endif
  39539. +
  39540. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  39541. +{
  39542. + if (error)
  39543. + return;
  39544. + *(bus_addr_t *)arg = segs[0].ds_addr;
  39545. +}
  39546. +
  39547. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  39548. +{
  39549. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  39550. + int error;
  39551. +
  39552. + error = bus_dma_tag_create(
  39553. +#if __FreeBSD_version >= 700000
  39554. + bus_get_dma_tag(dma->dev), /* parent */
  39555. +#else
  39556. + NULL, /* parent */
  39557. +#endif
  39558. + 4, 0, /* alignment, bounds */
  39559. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  39560. + BUS_SPACE_MAXADDR, /* highaddr */
  39561. + NULL, NULL, /* filter, filterarg */
  39562. + size, /* maxsize */
  39563. + 1, /* nsegments */
  39564. + size, /* maxsegsize */
  39565. + 0, /* flags */
  39566. + NULL, /* lockfunc */
  39567. + NULL, /* lockarg */
  39568. + &dma->dma_tag);
  39569. + if (error) {
  39570. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  39571. + __func__, error);
  39572. + goto fail_0;
  39573. + }
  39574. +
  39575. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  39576. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  39577. + if (error) {
  39578. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  39579. + __func__, (uintmax_t)size, error);
  39580. + goto fail_1;
  39581. + }
  39582. +
  39583. + dma->dma_paddr = 0;
  39584. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  39585. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  39586. + if (error || dma->dma_paddr == 0) {
  39587. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  39588. + __func__, error);
  39589. + goto fail_2;
  39590. + }
  39591. +
  39592. + *dma_addr = dma->dma_paddr;
  39593. + return dma->dma_vaddr;
  39594. +
  39595. +fail_2:
  39596. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  39597. +fail_1:
  39598. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  39599. + bus_dma_tag_destroy(dma->dma_tag);
  39600. +fail_0:
  39601. + dma->dma_map = NULL;
  39602. + dma->dma_tag = NULL;
  39603. +
  39604. + return NULL;
  39605. +}
  39606. +
  39607. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  39608. +{
  39609. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  39610. +
  39611. + if (dma->dma_tag == NULL)
  39612. + return;
  39613. + if (dma->dma_map != NULL) {
  39614. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  39615. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  39616. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  39617. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  39618. + dma->dma_map = NULL;
  39619. + }
  39620. +
  39621. + bus_dma_tag_destroy(dma->dma_tag);
  39622. + dma->dma_tag = NULL;
  39623. +}
  39624. +
  39625. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  39626. +{
  39627. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  39628. +}
  39629. +
  39630. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  39631. +{
  39632. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  39633. +}
  39634. +
  39635. +void __DWC_FREE(void *mem_ctx, void *addr)
  39636. +{
  39637. + free(addr, M_DEVBUF);
  39638. +}
  39639. +
  39640. +
  39641. +#ifdef DWC_CRYPTOLIB
  39642. +/* dwc_crypto.h */
  39643. +
  39644. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  39645. +{
  39646. + get_random_bytes(buffer, length);
  39647. +}
  39648. +
  39649. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  39650. +{
  39651. + struct crypto_blkcipher *tfm;
  39652. + struct blkcipher_desc desc;
  39653. + struct scatterlist sgd;
  39654. + struct scatterlist sgs;
  39655. +
  39656. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  39657. + if (tfm == NULL) {
  39658. + printk("failed to load transform for aes CBC\n");
  39659. + return -1;
  39660. + }
  39661. +
  39662. + crypto_blkcipher_setkey(tfm, key, keylen);
  39663. + crypto_blkcipher_set_iv(tfm, iv, 16);
  39664. +
  39665. + sg_init_one(&sgd, out, messagelen);
  39666. + sg_init_one(&sgs, message, messagelen);
  39667. +
  39668. + desc.tfm = tfm;
  39669. + desc.flags = 0;
  39670. +
  39671. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  39672. + crypto_free_blkcipher(tfm);
  39673. + DWC_ERROR("AES CBC encryption failed");
  39674. + return -1;
  39675. + }
  39676. +
  39677. + crypto_free_blkcipher(tfm);
  39678. + return 0;
  39679. +}
  39680. +
  39681. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  39682. +{
  39683. + struct crypto_hash *tfm;
  39684. + struct hash_desc desc;
  39685. + struct scatterlist sg;
  39686. +
  39687. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  39688. + if (IS_ERR(tfm)) {
  39689. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  39690. + return 0;
  39691. + }
  39692. + desc.tfm = tfm;
  39693. + desc.flags = 0;
  39694. +
  39695. + sg_init_one(&sg, message, len);
  39696. + crypto_hash_digest(&desc, &sg, len, out);
  39697. + crypto_free_hash(tfm);
  39698. +
  39699. + return 1;
  39700. +}
  39701. +
  39702. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  39703. + uint8_t *key, uint32_t keylen, uint8_t *out)
  39704. +{
  39705. + struct crypto_hash *tfm;
  39706. + struct hash_desc desc;
  39707. + struct scatterlist sg;
  39708. +
  39709. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  39710. + if (IS_ERR(tfm)) {
  39711. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  39712. + return 0;
  39713. + }
  39714. + desc.tfm = tfm;
  39715. + desc.flags = 0;
  39716. +
  39717. + sg_init_one(&sg, message, messagelen);
  39718. + crypto_hash_setkey(tfm, key, keylen);
  39719. + crypto_hash_digest(&desc, &sg, messagelen, out);
  39720. + crypto_free_hash(tfm);
  39721. +
  39722. + return 1;
  39723. +}
  39724. +
  39725. +#endif /* DWC_CRYPTOLIB */
  39726. +
  39727. +
  39728. +/* Byte Ordering Conversions */
  39729. +
  39730. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  39731. +{
  39732. +#ifdef __LITTLE_ENDIAN
  39733. + return *p;
  39734. +#else
  39735. + uint8_t *u_p = (uint8_t *)p;
  39736. +
  39737. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  39738. +#endif
  39739. +}
  39740. +
  39741. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  39742. +{
  39743. +#ifdef __BIG_ENDIAN
  39744. + return *p;
  39745. +#else
  39746. + uint8_t *u_p = (uint8_t *)p;
  39747. +
  39748. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  39749. +#endif
  39750. +}
  39751. +
  39752. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  39753. +{
  39754. +#ifdef __LITTLE_ENDIAN
  39755. + return *p;
  39756. +#else
  39757. + uint8_t *u_p = (uint8_t *)p;
  39758. +
  39759. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  39760. +#endif
  39761. +}
  39762. +
  39763. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  39764. +{
  39765. +#ifdef __BIG_ENDIAN
  39766. + return *p;
  39767. +#else
  39768. + uint8_t *u_p = (uint8_t *)p;
  39769. +
  39770. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  39771. +#endif
  39772. +}
  39773. +
  39774. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  39775. +{
  39776. +#ifdef __LITTLE_ENDIAN
  39777. + return *p;
  39778. +#else
  39779. + uint8_t *u_p = (uint8_t *)p;
  39780. + return (u_p[1] | (u_p[0] << 8));
  39781. +#endif
  39782. +}
  39783. +
  39784. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  39785. +{
  39786. +#ifdef __BIG_ENDIAN
  39787. + return *p;
  39788. +#else
  39789. + uint8_t *u_p = (uint8_t *)p;
  39790. + return (u_p[1] | (u_p[0] << 8));
  39791. +#endif
  39792. +}
  39793. +
  39794. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  39795. +{
  39796. +#ifdef __LITTLE_ENDIAN
  39797. + return *p;
  39798. +#else
  39799. + uint8_t *u_p = (uint8_t *)p;
  39800. + return (u_p[1] | (u_p[0] << 8));
  39801. +#endif
  39802. +}
  39803. +
  39804. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  39805. +{
  39806. +#ifdef __BIG_ENDIAN
  39807. + return *p;
  39808. +#else
  39809. + uint8_t *u_p = (uint8_t *)p;
  39810. + return (u_p[1] | (u_p[0] << 8));
  39811. +#endif
  39812. +}
  39813. +
  39814. +
  39815. +/* Registers */
  39816. +
  39817. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  39818. +{
  39819. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  39820. + bus_size_t ior = (bus_size_t)reg;
  39821. +
  39822. + return bus_space_read_4(io->iot, io->ioh, ior);
  39823. +}
  39824. +
  39825. +#if 0
  39826. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  39827. +{
  39828. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  39829. + bus_size_t ior = (bus_size_t)reg;
  39830. +
  39831. + return bus_space_read_8(io->iot, io->ioh, ior);
  39832. +}
  39833. +#endif
  39834. +
  39835. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  39836. +{
  39837. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  39838. + bus_size_t ior = (bus_size_t)reg;
  39839. +
  39840. + bus_space_write_4(io->iot, io->ioh, ior, value);
  39841. +}
  39842. +
  39843. +#if 0
  39844. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  39845. +{
  39846. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  39847. + bus_size_t ior = (bus_size_t)reg;
  39848. +
  39849. + bus_space_write_8(io->iot, io->ioh, ior, value);
  39850. +}
  39851. +#endif
  39852. +
  39853. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  39854. + uint32_t set_mask)
  39855. +{
  39856. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  39857. + bus_size_t ior = (bus_size_t)reg;
  39858. +
  39859. + bus_space_write_4(io->iot, io->ioh, ior,
  39860. + (bus_space_read_4(io->iot, io->ioh, ior) &
  39861. + ~clear_mask) | set_mask);
  39862. +}
  39863. +
  39864. +#if 0
  39865. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  39866. + uint64_t set_mask)
  39867. +{
  39868. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  39869. + bus_size_t ior = (bus_size_t)reg;
  39870. +
  39871. + bus_space_write_8(io->iot, io->ioh, ior,
  39872. + (bus_space_read_8(io->iot, io->ioh, ior) &
  39873. + ~clear_mask) | set_mask);
  39874. +}
  39875. +#endif
  39876. +
  39877. +
  39878. +/* Locking */
  39879. +
  39880. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  39881. +{
  39882. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  39883. +
  39884. + if (!sl) {
  39885. + DWC_ERROR("Cannot allocate memory for spinlock");
  39886. + return NULL;
  39887. + }
  39888. +
  39889. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  39890. + return (dwc_spinlock_t *)sl;
  39891. +}
  39892. +
  39893. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  39894. +{
  39895. + struct mtx *sl = (struct mtx *)lock;
  39896. +
  39897. + mtx_destroy(sl);
  39898. + DWC_FREE(sl);
  39899. +}
  39900. +
  39901. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  39902. +{
  39903. + mtx_lock_spin((struct mtx *)lock); // ???
  39904. +}
  39905. +
  39906. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  39907. +{
  39908. + mtx_unlock_spin((struct mtx *)lock); // ???
  39909. +}
  39910. +
  39911. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  39912. +{
  39913. + mtx_lock_spin((struct mtx *)lock);
  39914. +}
  39915. +
  39916. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  39917. +{
  39918. + mtx_unlock_spin((struct mtx *)lock);
  39919. +}
  39920. +
  39921. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  39922. +{
  39923. + struct mtx *m;
  39924. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  39925. +
  39926. + if (!mutex) {
  39927. + DWC_ERROR("Cannot allocate memory for mutex");
  39928. + return NULL;
  39929. + }
  39930. +
  39931. + m = (struct mtx *)mutex;
  39932. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  39933. + return mutex;
  39934. +}
  39935. +
  39936. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  39937. +#else
  39938. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  39939. +{
  39940. + mtx_destroy((struct mtx *)mutex);
  39941. + DWC_FREE(mutex);
  39942. +}
  39943. +#endif
  39944. +
  39945. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  39946. +{
  39947. + struct mtx *m = (struct mtx *)mutex;
  39948. +
  39949. + mtx_lock(m);
  39950. +}
  39951. +
  39952. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  39953. +{
  39954. + struct mtx *m = (struct mtx *)mutex;
  39955. +
  39956. + return mtx_trylock(m);
  39957. +}
  39958. +
  39959. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  39960. +{
  39961. + struct mtx *m = (struct mtx *)mutex;
  39962. +
  39963. + mtx_unlock(m);
  39964. +}
  39965. +
  39966. +
  39967. +/* Timing */
  39968. +
  39969. +void DWC_UDELAY(uint32_t usecs)
  39970. +{
  39971. + DELAY(usecs);
  39972. +}
  39973. +
  39974. +void DWC_MDELAY(uint32_t msecs)
  39975. +{
  39976. + do {
  39977. + DELAY(1000);
  39978. + } while (--msecs);
  39979. +}
  39980. +
  39981. +void DWC_MSLEEP(uint32_t msecs)
  39982. +{
  39983. + struct timeval tv;
  39984. +
  39985. + tv.tv_sec = msecs / 1000;
  39986. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  39987. + pause("dw3slp", tvtohz(&tv));
  39988. +}
  39989. +
  39990. +uint32_t DWC_TIME(void)
  39991. +{
  39992. + struct timeval tv;
  39993. +
  39994. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  39995. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  39996. +}
  39997. +
  39998. +
  39999. +/* Timers */
  40000. +
  40001. +struct dwc_timer {
  40002. + struct callout t;
  40003. + char *name;
  40004. + dwc_spinlock_t *lock;
  40005. + dwc_timer_callback_t cb;
  40006. + void *data;
  40007. +};
  40008. +
  40009. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  40010. +{
  40011. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  40012. +
  40013. + if (!t) {
  40014. + DWC_ERROR("Cannot allocate memory for timer");
  40015. + return NULL;
  40016. + }
  40017. +
  40018. + callout_init(&t->t, 1);
  40019. +
  40020. + t->name = DWC_STRDUP(name);
  40021. + if (!t->name) {
  40022. + DWC_ERROR("Cannot allocate memory for timer->name");
  40023. + goto no_name;
  40024. + }
  40025. +
  40026. + t->lock = DWC_SPINLOCK_ALLOC();
  40027. + if (!t->lock) {
  40028. + DWC_ERROR("Cannot allocate memory for lock");
  40029. + goto no_lock;
  40030. + }
  40031. +
  40032. + t->cb = cb;
  40033. + t->data = data;
  40034. +
  40035. + return t;
  40036. +
  40037. + no_lock:
  40038. + DWC_FREE(t->name);
  40039. + no_name:
  40040. + DWC_FREE(t);
  40041. +
  40042. + return NULL;
  40043. +}
  40044. +
  40045. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  40046. +{
  40047. + callout_stop(&timer->t);
  40048. + DWC_SPINLOCK_FREE(timer->lock);
  40049. + DWC_FREE(timer->name);
  40050. + DWC_FREE(timer);
  40051. +}
  40052. +
  40053. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  40054. +{
  40055. + struct timeval tv;
  40056. +
  40057. + tv.tv_sec = time / 1000;
  40058. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  40059. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  40060. +}
  40061. +
  40062. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  40063. +{
  40064. + callout_stop(&timer->t);
  40065. +}
  40066. +
  40067. +
  40068. +/* Wait Queues */
  40069. +
  40070. +struct dwc_waitq {
  40071. + struct mtx lock;
  40072. + int abort;
  40073. +};
  40074. +
  40075. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  40076. +{
  40077. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  40078. +
  40079. + if (!wq) {
  40080. + DWC_ERROR("Cannot allocate memory for waitqueue");
  40081. + return NULL;
  40082. + }
  40083. +
  40084. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  40085. + wq->abort = 0;
  40086. +
  40087. + return wq;
  40088. +}
  40089. +
  40090. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  40091. +{
  40092. + mtx_destroy(&wq->lock);
  40093. + DWC_FREE(wq);
  40094. +}
  40095. +
  40096. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  40097. +{
  40098. +// intrmask_t ipl;
  40099. + int result = 0;
  40100. +
  40101. + mtx_lock(&wq->lock);
  40102. +// ipl = splbio();
  40103. +
  40104. + /* Skip the sleep if already aborted or triggered */
  40105. + if (!wq->abort && !cond(data)) {
  40106. +// splx(ipl);
  40107. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  40108. +// ipl = splbio();
  40109. + }
  40110. +
  40111. + if (result == ERESTART) { // signaled - restart
  40112. + result = -DWC_E_RESTART;
  40113. +
  40114. + } else if (result == EINTR) { // signaled - interrupt
  40115. + result = -DWC_E_ABORT;
  40116. +
  40117. + } else if (wq->abort) {
  40118. + result = -DWC_E_ABORT;
  40119. +
  40120. + } else {
  40121. + result = 0;
  40122. + }
  40123. +
  40124. + wq->abort = 0;
  40125. +// splx(ipl);
  40126. + mtx_unlock(&wq->lock);
  40127. + return result;
  40128. +}
  40129. +
  40130. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  40131. + void *data, int32_t msecs)
  40132. +{
  40133. + struct timeval tv, tv1, tv2;
  40134. +// intrmask_t ipl;
  40135. + int result = 0;
  40136. +
  40137. + tv.tv_sec = msecs / 1000;
  40138. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  40139. +
  40140. + mtx_lock(&wq->lock);
  40141. +// ipl = splbio();
  40142. +
  40143. + /* Skip the sleep if already aborted or triggered */
  40144. + if (!wq->abort && !cond(data)) {
  40145. +// splx(ipl);
  40146. + getmicrouptime(&tv1);
  40147. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  40148. + getmicrouptime(&tv2);
  40149. +// ipl = splbio();
  40150. + }
  40151. +
  40152. + if (result == 0) { // awoken
  40153. + if (wq->abort) {
  40154. + result = -DWC_E_ABORT;
  40155. + } else {
  40156. + tv2.tv_usec -= tv1.tv_usec;
  40157. + if (tv2.tv_usec < 0) {
  40158. + tv2.tv_usec += 1000000;
  40159. + tv2.tv_sec--;
  40160. + }
  40161. +
  40162. + tv2.tv_sec -= tv1.tv_sec;
  40163. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  40164. + result = msecs - result;
  40165. + if (result <= 0)
  40166. + result = 1;
  40167. + }
  40168. + } else if (result == ERESTART) { // signaled - restart
  40169. + result = -DWC_E_RESTART;
  40170. +
  40171. + } else if (result == EINTR) { // signaled - interrupt
  40172. + result = -DWC_E_ABORT;
  40173. +
  40174. + } else { // timed out
  40175. + result = -DWC_E_TIMEOUT;
  40176. + }
  40177. +
  40178. + wq->abort = 0;
  40179. +// splx(ipl);
  40180. + mtx_unlock(&wq->lock);
  40181. + return result;
  40182. +}
  40183. +
  40184. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  40185. +{
  40186. + wakeup(wq);
  40187. +}
  40188. +
  40189. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  40190. +{
  40191. +// intrmask_t ipl;
  40192. +
  40193. + mtx_lock(&wq->lock);
  40194. +// ipl = splbio();
  40195. + wq->abort = 1;
  40196. + wakeup(wq);
  40197. +// splx(ipl);
  40198. + mtx_unlock(&wq->lock);
  40199. +}
  40200. +
  40201. +
  40202. +/* Threading */
  40203. +
  40204. +struct dwc_thread {
  40205. + struct proc *proc;
  40206. + int abort;
  40207. +};
  40208. +
  40209. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  40210. +{
  40211. + int retval;
  40212. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  40213. +
  40214. + if (!thread) {
  40215. + return NULL;
  40216. + }
  40217. +
  40218. + thread->abort = 0;
  40219. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  40220. + RFPROC | RFNOWAIT, 0, "%s", name);
  40221. + if (retval) {
  40222. + DWC_FREE(thread);
  40223. + return NULL;
  40224. + }
  40225. +
  40226. + return thread;
  40227. +}
  40228. +
  40229. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  40230. +{
  40231. + int retval;
  40232. +
  40233. + thread->abort = 1;
  40234. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  40235. +
  40236. + if (retval == 0) {
  40237. + /* DWC_THREAD_EXIT() will free the thread struct */
  40238. + return 0;
  40239. + }
  40240. +
  40241. + /* NOTE: We leak the thread struct if thread doesn't die */
  40242. +
  40243. + if (retval == EWOULDBLOCK) {
  40244. + return -DWC_E_TIMEOUT;
  40245. + }
  40246. +
  40247. + return -DWC_E_UNKNOWN;
  40248. +}
  40249. +
  40250. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  40251. +{
  40252. + return thread->abort;
  40253. +}
  40254. +
  40255. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  40256. +{
  40257. + wakeup(&thread->abort);
  40258. + DWC_FREE(thread);
  40259. + kthread_exit(0);
  40260. +}
  40261. +
  40262. +
  40263. +/* tasklets
  40264. + - Runs in interrupt context (cannot sleep)
  40265. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  40266. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  40267. + */
  40268. +struct dwc_tasklet {
  40269. + struct task t;
  40270. + dwc_tasklet_callback_t cb;
  40271. + void *data;
  40272. +};
  40273. +
  40274. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  40275. +{
  40276. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  40277. +
  40278. + task->cb(task->data);
  40279. +}
  40280. +
  40281. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  40282. +{
  40283. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  40284. +
  40285. + if (task) {
  40286. + task->cb = cb;
  40287. + task->data = data;
  40288. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  40289. + } else {
  40290. + DWC_ERROR("Cannot allocate memory for tasklet");
  40291. + }
  40292. +
  40293. + return task;
  40294. +}
  40295. +
  40296. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  40297. +{
  40298. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  40299. + DWC_FREE(task);
  40300. +}
  40301. +
  40302. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  40303. +{
  40304. + /* Uses predefined system queue */
  40305. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  40306. +}
  40307. +
  40308. +
  40309. +/* workqueues
  40310. + - Runs in process context (can sleep)
  40311. + */
  40312. +typedef struct work_container {
  40313. + dwc_work_callback_t cb;
  40314. + void *data;
  40315. + dwc_workq_t *wq;
  40316. + char *name;
  40317. + int hz;
  40318. +
  40319. +#ifdef DEBUG
  40320. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  40321. +#endif
  40322. + struct task task;
  40323. +} work_container_t;
  40324. +
  40325. +#ifdef DEBUG
  40326. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  40327. +#endif
  40328. +
  40329. +struct dwc_workq {
  40330. + struct taskqueue *taskq;
  40331. + dwc_spinlock_t *lock;
  40332. + dwc_waitq_t *waitq;
  40333. + int pending;
  40334. +
  40335. +#ifdef DEBUG
  40336. + struct work_container_queue entries;
  40337. +#endif
  40338. +};
  40339. +
  40340. +static void do_work(void *data, int pending) // what to do with pending ???
  40341. +{
  40342. + work_container_t *container = (work_container_t *)data;
  40343. + dwc_workq_t *wq = container->wq;
  40344. + dwc_irqflags_t flags;
  40345. +
  40346. + if (container->hz) {
  40347. + pause("dw3wrk", container->hz);
  40348. + }
  40349. +
  40350. + container->cb(container->data);
  40351. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  40352. +
  40353. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  40354. +
  40355. +#ifdef DEBUG
  40356. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  40357. +#endif
  40358. + if (container->name)
  40359. + DWC_FREE(container->name);
  40360. + DWC_FREE(container);
  40361. + wq->pending--;
  40362. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  40363. + DWC_WAITQ_TRIGGER(wq->waitq);
  40364. +}
  40365. +
  40366. +static int work_done(void *data)
  40367. +{
  40368. + dwc_workq_t *workq = (dwc_workq_t *)data;
  40369. +
  40370. + return workq->pending == 0;
  40371. +}
  40372. +
  40373. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  40374. +{
  40375. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  40376. +}
  40377. +
  40378. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  40379. +{
  40380. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  40381. +
  40382. + if (!wq) {
  40383. + DWC_ERROR("Cannot allocate memory for workqueue");
  40384. + return NULL;
  40385. + }
  40386. +
  40387. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  40388. + if (!wq->taskq) {
  40389. + DWC_ERROR("Cannot allocate memory for taskqueue");
  40390. + goto no_taskq;
  40391. + }
  40392. +
  40393. + wq->pending = 0;
  40394. +
  40395. + wq->lock = DWC_SPINLOCK_ALLOC();
  40396. + if (!wq->lock) {
  40397. + DWC_ERROR("Cannot allocate memory for spinlock");
  40398. + goto no_lock;
  40399. + }
  40400. +
  40401. + wq->waitq = DWC_WAITQ_ALLOC();
  40402. + if (!wq->waitq) {
  40403. + DWC_ERROR("Cannot allocate memory for waitqueue");
  40404. + goto no_waitq;
  40405. + }
  40406. +
  40407. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  40408. +
  40409. +#ifdef DEBUG
  40410. + DWC_CIRCLEQ_INIT(&wq->entries);
  40411. +#endif
  40412. + return wq;
  40413. +
  40414. + no_waitq:
  40415. + DWC_SPINLOCK_FREE(wq->lock);
  40416. + no_lock:
  40417. + taskqueue_free(wq->taskq);
  40418. + no_taskq:
  40419. + DWC_FREE(wq);
  40420. +
  40421. + return NULL;
  40422. +}
  40423. +
  40424. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  40425. +{
  40426. +#ifdef DEBUG
  40427. + dwc_irqflags_t flags;
  40428. +
  40429. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  40430. +
  40431. + if (wq->pending != 0) {
  40432. + struct work_container *container;
  40433. +
  40434. + DWC_ERROR("Destroying work queue with pending work");
  40435. +
  40436. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  40437. + DWC_ERROR("Work %s still pending", container->name);
  40438. + }
  40439. + }
  40440. +
  40441. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  40442. +#endif
  40443. + DWC_WAITQ_FREE(wq->waitq);
  40444. + DWC_SPINLOCK_FREE(wq->lock);
  40445. + taskqueue_free(wq->taskq);
  40446. + DWC_FREE(wq);
  40447. +}
  40448. +
  40449. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  40450. + char *format, ...)
  40451. +{
  40452. + dwc_irqflags_t flags;
  40453. + work_container_t *container;
  40454. + static char name[128];
  40455. + va_list args;
  40456. +
  40457. + va_start(args, format);
  40458. + DWC_VSNPRINTF(name, 128, format, args);
  40459. + va_end(args);
  40460. +
  40461. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  40462. + wq->pending++;
  40463. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  40464. + DWC_WAITQ_TRIGGER(wq->waitq);
  40465. +
  40466. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  40467. + if (!container) {
  40468. + DWC_ERROR("Cannot allocate memory for container");
  40469. + return;
  40470. + }
  40471. +
  40472. + container->name = DWC_STRDUP(name);
  40473. + if (!container->name) {
  40474. + DWC_ERROR("Cannot allocate memory for container->name");
  40475. + DWC_FREE(container);
  40476. + return;
  40477. + }
  40478. +
  40479. + container->cb = cb;
  40480. + container->data = data;
  40481. + container->wq = wq;
  40482. + container->hz = 0;
  40483. +
  40484. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  40485. +
  40486. + TASK_INIT(&container->task, 0, do_work, container);
  40487. +
  40488. +#ifdef DEBUG
  40489. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  40490. +#endif
  40491. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  40492. +}
  40493. +
  40494. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  40495. + void *data, uint32_t time, char *format, ...)
  40496. +{
  40497. + dwc_irqflags_t flags;
  40498. + work_container_t *container;
  40499. + static char name[128];
  40500. + struct timeval tv;
  40501. + va_list args;
  40502. +
  40503. + va_start(args, format);
  40504. + DWC_VSNPRINTF(name, 128, format, args);
  40505. + va_end(args);
  40506. +
  40507. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  40508. + wq->pending++;
  40509. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  40510. + DWC_WAITQ_TRIGGER(wq->waitq);
  40511. +
  40512. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  40513. + if (!container) {
  40514. + DWC_ERROR("Cannot allocate memory for container");
  40515. + return;
  40516. + }
  40517. +
  40518. + container->name = DWC_STRDUP(name);
  40519. + if (!container->name) {
  40520. + DWC_ERROR("Cannot allocate memory for container->name");
  40521. + DWC_FREE(container);
  40522. + return;
  40523. + }
  40524. +
  40525. + container->cb = cb;
  40526. + container->data = data;
  40527. + container->wq = wq;
  40528. +
  40529. + tv.tv_sec = time / 1000;
  40530. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  40531. + container->hz = tvtohz(&tv);
  40532. +
  40533. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  40534. +
  40535. + TASK_INIT(&container->task, 0, do_work, container);
  40536. +
  40537. +#ifdef DEBUG
  40538. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  40539. +#endif
  40540. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  40541. +}
  40542. +
  40543. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  40544. +{
  40545. + return wq->pending;
  40546. +}
  40547. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_common_linux.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  40548. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_common_linux.c 1970-01-01 01:00:00.000000000 +0100
  40549. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_linux.c 2014-03-11 16:53:12.000000000 +0100
  40550. @@ -0,0 +1,1431 @@
  40551. +#include <linux/kernel.h>
  40552. +#include <linux/init.h>
  40553. +#include <linux/module.h>
  40554. +#include <linux/kthread.h>
  40555. +
  40556. +#ifdef DWC_CCLIB
  40557. +# include "dwc_cc.h"
  40558. +#endif
  40559. +
  40560. +#ifdef DWC_CRYPTOLIB
  40561. +# include "dwc_modpow.h"
  40562. +# include "dwc_dh.h"
  40563. +# include "dwc_crypto.h"
  40564. +#endif
  40565. +
  40566. +#ifdef DWC_NOTIFYLIB
  40567. +# include "dwc_notifier.h"
  40568. +#endif
  40569. +
  40570. +/* OS-Level Implementations */
  40571. +
  40572. +/* This is the Linux kernel implementation of the DWC platform library. */
  40573. +#include <linux/moduleparam.h>
  40574. +#include <linux/ctype.h>
  40575. +#include <linux/crypto.h>
  40576. +#include <linux/delay.h>
  40577. +#include <linux/device.h>
  40578. +#include <linux/dma-mapping.h>
  40579. +#include <linux/cdev.h>
  40580. +#include <linux/errno.h>
  40581. +#include <linux/interrupt.h>
  40582. +#include <linux/jiffies.h>
  40583. +#include <linux/list.h>
  40584. +#include <linux/pci.h>
  40585. +#include <linux/random.h>
  40586. +#include <linux/scatterlist.h>
  40587. +#include <linux/slab.h>
  40588. +#include <linux/stat.h>
  40589. +#include <linux/string.h>
  40590. +#include <linux/timer.h>
  40591. +#include <linux/usb.h>
  40592. +
  40593. +#include <linux/version.h>
  40594. +
  40595. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  40596. +# include <linux/usb/gadget.h>
  40597. +#else
  40598. +# include <linux/usb_gadget.h>
  40599. +#endif
  40600. +
  40601. +#include <asm/io.h>
  40602. +#include <asm/page.h>
  40603. +#include <asm/uaccess.h>
  40604. +#include <asm/unaligned.h>
  40605. +
  40606. +#include "dwc_os.h"
  40607. +#include "dwc_list.h"
  40608. +
  40609. +
  40610. +/* MISC */
  40611. +
  40612. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  40613. +{
  40614. + return memset(dest, byte, size);
  40615. +}
  40616. +
  40617. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  40618. +{
  40619. + return memcpy(dest, src, size);
  40620. +}
  40621. +
  40622. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  40623. +{
  40624. + return memmove(dest, src, size);
  40625. +}
  40626. +
  40627. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  40628. +{
  40629. + return memcmp(m1, m2, size);
  40630. +}
  40631. +
  40632. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  40633. +{
  40634. + return strncmp(s1, s2, size);
  40635. +}
  40636. +
  40637. +int DWC_STRCMP(void *s1, void *s2)
  40638. +{
  40639. + return strcmp(s1, s2);
  40640. +}
  40641. +
  40642. +int DWC_STRLEN(char const *str)
  40643. +{
  40644. + return strlen(str);
  40645. +}
  40646. +
  40647. +char *DWC_STRCPY(char *to, char const *from)
  40648. +{
  40649. + return strcpy(to, from);
  40650. +}
  40651. +
  40652. +char *DWC_STRDUP(char const *str)
  40653. +{
  40654. + int len = DWC_STRLEN(str) + 1;
  40655. + char *new = DWC_ALLOC_ATOMIC(len);
  40656. +
  40657. + if (!new) {
  40658. + return NULL;
  40659. + }
  40660. +
  40661. + DWC_MEMCPY(new, str, len);
  40662. + return new;
  40663. +}
  40664. +
  40665. +int DWC_ATOI(const char *str, int32_t *value)
  40666. +{
  40667. + char *end = NULL;
  40668. +
  40669. + *value = simple_strtol(str, &end, 0);
  40670. + if (*end == '\0') {
  40671. + return 0;
  40672. + }
  40673. +
  40674. + return -1;
  40675. +}
  40676. +
  40677. +int DWC_ATOUI(const char *str, uint32_t *value)
  40678. +{
  40679. + char *end = NULL;
  40680. +
  40681. + *value = simple_strtoul(str, &end, 0);
  40682. + if (*end == '\0') {
  40683. + return 0;
  40684. + }
  40685. +
  40686. + return -1;
  40687. +}
  40688. +
  40689. +
  40690. +#ifdef DWC_UTFLIB
  40691. +/* From usbstring.c */
  40692. +
  40693. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  40694. +{
  40695. + int count = 0;
  40696. + u8 c;
  40697. + u16 uchar;
  40698. +
  40699. + /* this insists on correct encodings, though not minimal ones.
  40700. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  40701. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  40702. + */
  40703. + while (len != 0 && (c = (u8) *s++) != 0) {
  40704. + if (unlikely(c & 0x80)) {
  40705. + // 2-byte sequence:
  40706. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  40707. + if ((c & 0xe0) == 0xc0) {
  40708. + uchar = (c & 0x1f) << 6;
  40709. +
  40710. + c = (u8) *s++;
  40711. + if ((c & 0xc0) != 0xc0)
  40712. + goto fail;
  40713. + c &= 0x3f;
  40714. + uchar |= c;
  40715. +
  40716. + // 3-byte sequence (most CJKV characters):
  40717. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  40718. + } else if ((c & 0xf0) == 0xe0) {
  40719. + uchar = (c & 0x0f) << 12;
  40720. +
  40721. + c = (u8) *s++;
  40722. + if ((c & 0xc0) != 0xc0)
  40723. + goto fail;
  40724. + c &= 0x3f;
  40725. + uchar |= c << 6;
  40726. +
  40727. + c = (u8) *s++;
  40728. + if ((c & 0xc0) != 0xc0)
  40729. + goto fail;
  40730. + c &= 0x3f;
  40731. + uchar |= c;
  40732. +
  40733. + /* no bogus surrogates */
  40734. + if (0xd800 <= uchar && uchar <= 0xdfff)
  40735. + goto fail;
  40736. +
  40737. + // 4-byte sequence (surrogate pairs, currently rare):
  40738. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  40739. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  40740. + // (uuuuu = wwww + 1)
  40741. + // FIXME accept the surrogate code points (only)
  40742. + } else
  40743. + goto fail;
  40744. + } else
  40745. + uchar = c;
  40746. + put_unaligned (cpu_to_le16 (uchar), cp++);
  40747. + count++;
  40748. + len--;
  40749. + }
  40750. + return count;
  40751. +fail:
  40752. + return -1;
  40753. +}
  40754. +#endif /* DWC_UTFLIB */
  40755. +
  40756. +
  40757. +/* dwc_debug.h */
  40758. +
  40759. +dwc_bool_t DWC_IN_IRQ(void)
  40760. +{
  40761. + return in_irq();
  40762. +}
  40763. +
  40764. +dwc_bool_t DWC_IN_BH(void)
  40765. +{
  40766. + return in_softirq();
  40767. +}
  40768. +
  40769. +void DWC_VPRINTF(char *format, va_list args)
  40770. +{
  40771. + vprintk(format, args);
  40772. +}
  40773. +
  40774. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  40775. +{
  40776. + return vsnprintf(str, size, format, args);
  40777. +}
  40778. +
  40779. +void DWC_PRINTF(char *format, ...)
  40780. +{
  40781. + va_list args;
  40782. +
  40783. + va_start(args, format);
  40784. + DWC_VPRINTF(format, args);
  40785. + va_end(args);
  40786. +}
  40787. +
  40788. +int DWC_SPRINTF(char *buffer, char *format, ...)
  40789. +{
  40790. + int retval;
  40791. + va_list args;
  40792. +
  40793. + va_start(args, format);
  40794. + retval = vsprintf(buffer, format, args);
  40795. + va_end(args);
  40796. + return retval;
  40797. +}
  40798. +
  40799. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  40800. +{
  40801. + int retval;
  40802. + va_list args;
  40803. +
  40804. + va_start(args, format);
  40805. + retval = vsnprintf(buffer, size, format, args);
  40806. + va_end(args);
  40807. + return retval;
  40808. +}
  40809. +
  40810. +void __DWC_WARN(char *format, ...)
  40811. +{
  40812. + va_list args;
  40813. +
  40814. + va_start(args, format);
  40815. + DWC_PRINTF(KERN_WARNING);
  40816. + DWC_VPRINTF(format, args);
  40817. + va_end(args);
  40818. +}
  40819. +
  40820. +void __DWC_ERROR(char *format, ...)
  40821. +{
  40822. + va_list args;
  40823. +
  40824. + va_start(args, format);
  40825. + DWC_PRINTF(KERN_ERR);
  40826. + DWC_VPRINTF(format, args);
  40827. + va_end(args);
  40828. +}
  40829. +
  40830. +void DWC_EXCEPTION(char *format, ...)
  40831. +{
  40832. + va_list args;
  40833. +
  40834. + va_start(args, format);
  40835. + DWC_PRINTF(KERN_ERR);
  40836. + DWC_VPRINTF(format, args);
  40837. + va_end(args);
  40838. + BUG_ON(1);
  40839. +}
  40840. +
  40841. +#ifdef DEBUG
  40842. +void __DWC_DEBUG(char *format, ...)
  40843. +{
  40844. + va_list args;
  40845. +
  40846. + va_start(args, format);
  40847. + DWC_PRINTF(KERN_DEBUG);
  40848. + DWC_VPRINTF(format, args);
  40849. + va_end(args);
  40850. +}
  40851. +#endif
  40852. +
  40853. +
  40854. +/* dwc_mem.h */
  40855. +
  40856. +#if 0
  40857. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  40858. + uint32_t align,
  40859. + uint32_t alloc)
  40860. +{
  40861. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  40862. + size, align, alloc);
  40863. + return (dwc_pool_t *)pool;
  40864. +}
  40865. +
  40866. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  40867. +{
  40868. + dma_pool_destroy((struct dma_pool *)pool);
  40869. +}
  40870. +
  40871. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  40872. +{
  40873. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  40874. +}
  40875. +
  40876. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  40877. +{
  40878. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  40879. + memset(..);
  40880. +}
  40881. +
  40882. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  40883. +{
  40884. + dma_pool_free(pool, vaddr, daddr);
  40885. +}
  40886. +#endif
  40887. +
  40888. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  40889. +{
  40890. +#ifdef xxCOSIM /* Only works for 32-bit cosim */
  40891. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL);
  40892. +#else
  40893. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
  40894. +#endif
  40895. + if (!buf) {
  40896. + return NULL;
  40897. + }
  40898. +
  40899. + memset(buf, 0, (size_t)size);
  40900. + return buf;
  40901. +}
  40902. +
  40903. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  40904. +{
  40905. + void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
  40906. + if (!buf) {
  40907. + return NULL;
  40908. + }
  40909. + memset(buf, 0, (size_t)size);
  40910. + return buf;
  40911. +}
  40912. +
  40913. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  40914. +{
  40915. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  40916. +}
  40917. +
  40918. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  40919. +{
  40920. + return kzalloc(size, GFP_KERNEL);
  40921. +}
  40922. +
  40923. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  40924. +{
  40925. + return kzalloc(size, GFP_ATOMIC);
  40926. +}
  40927. +
  40928. +void __DWC_FREE(void *mem_ctx, void *addr)
  40929. +{
  40930. + kfree(addr);
  40931. +}
  40932. +
  40933. +
  40934. +#ifdef DWC_CRYPTOLIB
  40935. +/* dwc_crypto.h */
  40936. +
  40937. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  40938. +{
  40939. + get_random_bytes(buffer, length);
  40940. +}
  40941. +
  40942. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  40943. +{
  40944. + struct crypto_blkcipher *tfm;
  40945. + struct blkcipher_desc desc;
  40946. + struct scatterlist sgd;
  40947. + struct scatterlist sgs;
  40948. +
  40949. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  40950. + if (tfm == NULL) {
  40951. + printk("failed to load transform for aes CBC\n");
  40952. + return -1;
  40953. + }
  40954. +
  40955. + crypto_blkcipher_setkey(tfm, key, keylen);
  40956. + crypto_blkcipher_set_iv(tfm, iv, 16);
  40957. +
  40958. + sg_init_one(&sgd, out, messagelen);
  40959. + sg_init_one(&sgs, message, messagelen);
  40960. +
  40961. + desc.tfm = tfm;
  40962. + desc.flags = 0;
  40963. +
  40964. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  40965. + crypto_free_blkcipher(tfm);
  40966. + DWC_ERROR("AES CBC encryption failed");
  40967. + return -1;
  40968. + }
  40969. +
  40970. + crypto_free_blkcipher(tfm);
  40971. + return 0;
  40972. +}
  40973. +
  40974. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  40975. +{
  40976. + struct crypto_hash *tfm;
  40977. + struct hash_desc desc;
  40978. + struct scatterlist sg;
  40979. +
  40980. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  40981. + if (IS_ERR(tfm)) {
  40982. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  40983. + return 0;
  40984. + }
  40985. + desc.tfm = tfm;
  40986. + desc.flags = 0;
  40987. +
  40988. + sg_init_one(&sg, message, len);
  40989. + crypto_hash_digest(&desc, &sg, len, out);
  40990. + crypto_free_hash(tfm);
  40991. +
  40992. + return 1;
  40993. +}
  40994. +
  40995. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  40996. + uint8_t *key, uint32_t keylen, uint8_t *out)
  40997. +{
  40998. + struct crypto_hash *tfm;
  40999. + struct hash_desc desc;
  41000. + struct scatterlist sg;
  41001. +
  41002. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  41003. + if (IS_ERR(tfm)) {
  41004. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  41005. + return 0;
  41006. + }
  41007. + desc.tfm = tfm;
  41008. + desc.flags = 0;
  41009. +
  41010. + sg_init_one(&sg, message, messagelen);
  41011. + crypto_hash_setkey(tfm, key, keylen);
  41012. + crypto_hash_digest(&desc, &sg, messagelen, out);
  41013. + crypto_free_hash(tfm);
  41014. +
  41015. + return 1;
  41016. +}
  41017. +#endif /* DWC_CRYPTOLIB */
  41018. +
  41019. +
  41020. +/* Byte Ordering Conversions */
  41021. +
  41022. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  41023. +{
  41024. +#ifdef __LITTLE_ENDIAN
  41025. + return *p;
  41026. +#else
  41027. + uint8_t *u_p = (uint8_t *)p;
  41028. +
  41029. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41030. +#endif
  41031. +}
  41032. +
  41033. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  41034. +{
  41035. +#ifdef __BIG_ENDIAN
  41036. + return *p;
  41037. +#else
  41038. + uint8_t *u_p = (uint8_t *)p;
  41039. +
  41040. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41041. +#endif
  41042. +}
  41043. +
  41044. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  41045. +{
  41046. +#ifdef __LITTLE_ENDIAN
  41047. + return *p;
  41048. +#else
  41049. + uint8_t *u_p = (uint8_t *)p;
  41050. +
  41051. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41052. +#endif
  41053. +}
  41054. +
  41055. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  41056. +{
  41057. +#ifdef __BIG_ENDIAN
  41058. + return *p;
  41059. +#else
  41060. + uint8_t *u_p = (uint8_t *)p;
  41061. +
  41062. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  41063. +#endif
  41064. +}
  41065. +
  41066. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  41067. +{
  41068. +#ifdef __LITTLE_ENDIAN
  41069. + return *p;
  41070. +#else
  41071. + uint8_t *u_p = (uint8_t *)p;
  41072. + return (u_p[1] | (u_p[0] << 8));
  41073. +#endif
  41074. +}
  41075. +
  41076. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  41077. +{
  41078. +#ifdef __BIG_ENDIAN
  41079. + return *p;
  41080. +#else
  41081. + uint8_t *u_p = (uint8_t *)p;
  41082. + return (u_p[1] | (u_p[0] << 8));
  41083. +#endif
  41084. +}
  41085. +
  41086. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  41087. +{
  41088. +#ifdef __LITTLE_ENDIAN
  41089. + return *p;
  41090. +#else
  41091. + uint8_t *u_p = (uint8_t *)p;
  41092. + return (u_p[1] | (u_p[0] << 8));
  41093. +#endif
  41094. +}
  41095. +
  41096. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  41097. +{
  41098. +#ifdef __BIG_ENDIAN
  41099. + return *p;
  41100. +#else
  41101. + uint8_t *u_p = (uint8_t *)p;
  41102. + return (u_p[1] | (u_p[0] << 8));
  41103. +#endif
  41104. +}
  41105. +
  41106. +
  41107. +/* Registers */
  41108. +
  41109. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  41110. +{
  41111. + return readl(reg);
  41112. +}
  41113. +
  41114. +#if 0
  41115. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  41116. +{
  41117. +}
  41118. +#endif
  41119. +
  41120. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  41121. +{
  41122. + writel(value, reg);
  41123. +}
  41124. +
  41125. +#if 0
  41126. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  41127. +{
  41128. +}
  41129. +#endif
  41130. +
  41131. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  41132. +{
  41133. + unsigned long flags;
  41134. +
  41135. + local_irq_save(flags);
  41136. + local_fiq_disable();
  41137. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  41138. + local_irq_restore(flags);
  41139. +}
  41140. +
  41141. +#if 0
  41142. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  41143. +{
  41144. +}
  41145. +#endif
  41146. +
  41147. +
  41148. +/* Locking */
  41149. +
  41150. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  41151. +{
  41152. + spinlock_t *sl = (spinlock_t *)1;
  41153. +
  41154. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41155. + sl = DWC_ALLOC(sizeof(*sl));
  41156. + if (!sl) {
  41157. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  41158. + return NULL;
  41159. + }
  41160. +
  41161. + spin_lock_init(sl);
  41162. +#endif
  41163. + return (dwc_spinlock_t *)sl;
  41164. +}
  41165. +
  41166. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  41167. +{
  41168. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41169. + DWC_FREE(lock);
  41170. +#endif
  41171. +}
  41172. +
  41173. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  41174. +{
  41175. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41176. + spin_lock((spinlock_t *)lock);
  41177. +#endif
  41178. +}
  41179. +
  41180. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  41181. +{
  41182. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41183. + spin_unlock((spinlock_t *)lock);
  41184. +#endif
  41185. +}
  41186. +
  41187. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  41188. +{
  41189. + dwc_irqflags_t f;
  41190. +
  41191. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41192. + spin_lock_irqsave((spinlock_t *)lock, f);
  41193. +#else
  41194. + local_irq_save(f);
  41195. +#endif
  41196. + *flags = f;
  41197. +}
  41198. +
  41199. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  41200. +{
  41201. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  41202. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  41203. +#else
  41204. + local_irq_restore(flags);
  41205. +#endif
  41206. +}
  41207. +
  41208. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  41209. +{
  41210. + struct mutex *m;
  41211. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  41212. +
  41213. + if (!mutex) {
  41214. + DWC_ERROR("Cannot allocate memory for mutex\n");
  41215. + return NULL;
  41216. + }
  41217. +
  41218. + m = (struct mutex *)mutex;
  41219. + mutex_init(m);
  41220. + return mutex;
  41221. +}
  41222. +
  41223. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  41224. +#else
  41225. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  41226. +{
  41227. + mutex_destroy((struct mutex *)mutex);
  41228. + DWC_FREE(mutex);
  41229. +}
  41230. +#endif
  41231. +
  41232. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  41233. +{
  41234. + struct mutex *m = (struct mutex *)mutex;
  41235. + mutex_lock(m);
  41236. +}
  41237. +
  41238. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  41239. +{
  41240. + struct mutex *m = (struct mutex *)mutex;
  41241. + return mutex_trylock(m);
  41242. +}
  41243. +
  41244. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  41245. +{
  41246. + struct mutex *m = (struct mutex *)mutex;
  41247. + mutex_unlock(m);
  41248. +}
  41249. +
  41250. +
  41251. +/* Timing */
  41252. +
  41253. +void DWC_UDELAY(uint32_t usecs)
  41254. +{
  41255. + udelay(usecs);
  41256. +}
  41257. +
  41258. +void DWC_MDELAY(uint32_t msecs)
  41259. +{
  41260. + mdelay(msecs);
  41261. +}
  41262. +
  41263. +void DWC_MSLEEP(uint32_t msecs)
  41264. +{
  41265. + msleep(msecs);
  41266. +}
  41267. +
  41268. +uint32_t DWC_TIME(void)
  41269. +{
  41270. + return jiffies_to_msecs(jiffies);
  41271. +}
  41272. +
  41273. +
  41274. +/* Timers */
  41275. +
  41276. +struct dwc_timer {
  41277. + struct timer_list *t;
  41278. + char *name;
  41279. + dwc_timer_callback_t cb;
  41280. + void *data;
  41281. + uint8_t scheduled;
  41282. + dwc_spinlock_t *lock;
  41283. +};
  41284. +
  41285. +static void timer_callback(unsigned long data)
  41286. +{
  41287. + dwc_timer_t *timer = (dwc_timer_t *)data;
  41288. + dwc_irqflags_t flags;
  41289. +
  41290. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  41291. + timer->scheduled = 0;
  41292. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  41293. + DWC_DEBUGC("Timer %s callback", timer->name);
  41294. + timer->cb(timer->data);
  41295. +}
  41296. +
  41297. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  41298. +{
  41299. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  41300. +
  41301. + if (!t) {
  41302. + DWC_ERROR("Cannot allocate memory for timer");
  41303. + return NULL;
  41304. + }
  41305. +
  41306. + t->t = DWC_ALLOC(sizeof(*t->t));
  41307. + if (!t->t) {
  41308. + DWC_ERROR("Cannot allocate memory for timer->t");
  41309. + goto no_timer;
  41310. + }
  41311. +
  41312. + t->name = DWC_STRDUP(name);
  41313. + if (!t->name) {
  41314. + DWC_ERROR("Cannot allocate memory for timer->name");
  41315. + goto no_name;
  41316. + }
  41317. +
  41318. + t->lock = DWC_SPINLOCK_ALLOC();
  41319. + if (!t->lock) {
  41320. + DWC_ERROR("Cannot allocate memory for lock");
  41321. + goto no_lock;
  41322. + }
  41323. +
  41324. + t->scheduled = 0;
  41325. + t->t->base = &boot_tvec_bases;
  41326. + t->t->expires = jiffies;
  41327. + setup_timer(t->t, timer_callback, (unsigned long)t);
  41328. +
  41329. + t->cb = cb;
  41330. + t->data = data;
  41331. +
  41332. + return t;
  41333. +
  41334. + no_lock:
  41335. + DWC_FREE(t->name);
  41336. + no_name:
  41337. + DWC_FREE(t->t);
  41338. + no_timer:
  41339. + DWC_FREE(t);
  41340. + return NULL;
  41341. +}
  41342. +
  41343. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  41344. +{
  41345. + dwc_irqflags_t flags;
  41346. +
  41347. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  41348. +
  41349. + if (timer->scheduled) {
  41350. + del_timer(timer->t);
  41351. + timer->scheduled = 0;
  41352. + }
  41353. +
  41354. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  41355. + DWC_SPINLOCK_FREE(timer->lock);
  41356. + DWC_FREE(timer->t);
  41357. + DWC_FREE(timer->name);
  41358. + DWC_FREE(timer);
  41359. +}
  41360. +
  41361. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  41362. +{
  41363. + dwc_irqflags_t flags;
  41364. +
  41365. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  41366. +
  41367. + if (!timer->scheduled) {
  41368. + timer->scheduled = 1;
  41369. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  41370. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  41371. + add_timer(timer->t);
  41372. + } else {
  41373. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  41374. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  41375. + }
  41376. +
  41377. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  41378. +}
  41379. +
  41380. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  41381. +{
  41382. + del_timer(timer->t);
  41383. +}
  41384. +
  41385. +
  41386. +/* Wait Queues */
  41387. +
  41388. +struct dwc_waitq {
  41389. + wait_queue_head_t queue;
  41390. + int abort;
  41391. +};
  41392. +
  41393. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  41394. +{
  41395. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  41396. +
  41397. + if (!wq) {
  41398. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  41399. + return NULL;
  41400. + }
  41401. +
  41402. + init_waitqueue_head(&wq->queue);
  41403. + wq->abort = 0;
  41404. + return wq;
  41405. +}
  41406. +
  41407. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  41408. +{
  41409. + DWC_FREE(wq);
  41410. +}
  41411. +
  41412. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  41413. +{
  41414. + int result = wait_event_interruptible(wq->queue,
  41415. + cond(data) || wq->abort);
  41416. + if (result == -ERESTARTSYS) {
  41417. + wq->abort = 0;
  41418. + return -DWC_E_RESTART;
  41419. + }
  41420. +
  41421. + if (wq->abort == 1) {
  41422. + wq->abort = 0;
  41423. + return -DWC_E_ABORT;
  41424. + }
  41425. +
  41426. + wq->abort = 0;
  41427. +
  41428. + if (result == 0) {
  41429. + return 0;
  41430. + }
  41431. +
  41432. + return -DWC_E_UNKNOWN;
  41433. +}
  41434. +
  41435. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  41436. + void *data, int32_t msecs)
  41437. +{
  41438. + int32_t tmsecs;
  41439. + int result = wait_event_interruptible_timeout(wq->queue,
  41440. + cond(data) || wq->abort,
  41441. + msecs_to_jiffies(msecs));
  41442. + if (result == -ERESTARTSYS) {
  41443. + wq->abort = 0;
  41444. + return -DWC_E_RESTART;
  41445. + }
  41446. +
  41447. + if (wq->abort == 1) {
  41448. + wq->abort = 0;
  41449. + return -DWC_E_ABORT;
  41450. + }
  41451. +
  41452. + wq->abort = 0;
  41453. +
  41454. + if (result > 0) {
  41455. + tmsecs = jiffies_to_msecs(result);
  41456. + if (!tmsecs) {
  41457. + return 1;
  41458. + }
  41459. +
  41460. + return tmsecs;
  41461. + }
  41462. +
  41463. + if (result == 0) {
  41464. + return -DWC_E_TIMEOUT;
  41465. + }
  41466. +
  41467. + return -DWC_E_UNKNOWN;
  41468. +}
  41469. +
  41470. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  41471. +{
  41472. + wq->abort = 0;
  41473. + wake_up_interruptible(&wq->queue);
  41474. +}
  41475. +
  41476. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  41477. +{
  41478. + wq->abort = 1;
  41479. + wake_up_interruptible(&wq->queue);
  41480. +}
  41481. +
  41482. +
  41483. +/* Threading */
  41484. +
  41485. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  41486. +{
  41487. + struct task_struct *thread = kthread_run(func, data, name);
  41488. +
  41489. + if (thread == ERR_PTR(-ENOMEM)) {
  41490. + return NULL;
  41491. + }
  41492. +
  41493. + return (dwc_thread_t *)thread;
  41494. +}
  41495. +
  41496. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  41497. +{
  41498. + return kthread_stop((struct task_struct *)thread);
  41499. +}
  41500. +
  41501. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  41502. +{
  41503. + return kthread_should_stop();
  41504. +}
  41505. +
  41506. +
  41507. +/* tasklets
  41508. + - run in interrupt context (cannot sleep)
  41509. + - each tasklet runs on a single CPU
  41510. + - different tasklets can be running simultaneously on different CPUs
  41511. + */
  41512. +struct dwc_tasklet {
  41513. + struct tasklet_struct t;
  41514. + dwc_tasklet_callback_t cb;
  41515. + void *data;
  41516. +};
  41517. +
  41518. +static void tasklet_callback(unsigned long data)
  41519. +{
  41520. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  41521. + t->cb(t->data);
  41522. +}
  41523. +
  41524. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  41525. +{
  41526. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  41527. +
  41528. + if (t) {
  41529. + t->cb = cb;
  41530. + t->data = data;
  41531. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  41532. + } else {
  41533. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  41534. + }
  41535. +
  41536. + return t;
  41537. +}
  41538. +
  41539. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  41540. +{
  41541. + DWC_FREE(task);
  41542. +}
  41543. +
  41544. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  41545. +{
  41546. + tasklet_schedule(&task->t);
  41547. +}
  41548. +
  41549. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  41550. +{
  41551. + tasklet_hi_schedule(&task->t);
  41552. +}
  41553. +
  41554. +
  41555. +/* workqueues
  41556. + - run in process context (can sleep)
  41557. + */
  41558. +typedef struct work_container {
  41559. + dwc_work_callback_t cb;
  41560. + void *data;
  41561. + dwc_workq_t *wq;
  41562. + char *name;
  41563. +
  41564. +#ifdef DEBUG
  41565. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  41566. +#endif
  41567. + struct delayed_work work;
  41568. +} work_container_t;
  41569. +
  41570. +#ifdef DEBUG
  41571. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  41572. +#endif
  41573. +
  41574. +struct dwc_workq {
  41575. + struct workqueue_struct *wq;
  41576. + dwc_spinlock_t *lock;
  41577. + dwc_waitq_t *waitq;
  41578. + int pending;
  41579. +
  41580. +#ifdef DEBUG
  41581. + struct work_container_queue entries;
  41582. +#endif
  41583. +};
  41584. +
  41585. +static void do_work(struct work_struct *work)
  41586. +{
  41587. + dwc_irqflags_t flags;
  41588. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  41589. + work_container_t *container = container_of(dw, struct work_container, work);
  41590. + dwc_workq_t *wq = container->wq;
  41591. +
  41592. + container->cb(container->data);
  41593. +
  41594. +#ifdef DEBUG
  41595. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  41596. +#endif
  41597. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  41598. + if (container->name) {
  41599. + DWC_FREE(container->name);
  41600. + }
  41601. + DWC_FREE(container);
  41602. +
  41603. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41604. + wq->pending--;
  41605. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41606. + DWC_WAITQ_TRIGGER(wq->waitq);
  41607. +}
  41608. +
  41609. +static int work_done(void *data)
  41610. +{
  41611. + dwc_workq_t *workq = (dwc_workq_t *)data;
  41612. + return workq->pending == 0;
  41613. +}
  41614. +
  41615. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  41616. +{
  41617. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  41618. +}
  41619. +
  41620. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  41621. +{
  41622. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  41623. +
  41624. + if (!wq) {
  41625. + return NULL;
  41626. + }
  41627. +
  41628. + wq->wq = create_singlethread_workqueue(name);
  41629. + if (!wq->wq) {
  41630. + goto no_wq;
  41631. + }
  41632. +
  41633. + wq->pending = 0;
  41634. +
  41635. + wq->lock = DWC_SPINLOCK_ALLOC();
  41636. + if (!wq->lock) {
  41637. + goto no_lock;
  41638. + }
  41639. +
  41640. + wq->waitq = DWC_WAITQ_ALLOC();
  41641. + if (!wq->waitq) {
  41642. + goto no_waitq;
  41643. + }
  41644. +
  41645. +#ifdef DEBUG
  41646. + DWC_CIRCLEQ_INIT(&wq->entries);
  41647. +#endif
  41648. + return wq;
  41649. +
  41650. + no_waitq:
  41651. + DWC_SPINLOCK_FREE(wq->lock);
  41652. + no_lock:
  41653. + destroy_workqueue(wq->wq);
  41654. + no_wq:
  41655. + DWC_FREE(wq);
  41656. +
  41657. + return NULL;
  41658. +}
  41659. +
  41660. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  41661. +{
  41662. +#ifdef DEBUG
  41663. + if (wq->pending != 0) {
  41664. + struct work_container *wc;
  41665. + DWC_ERROR("Destroying work queue with pending work");
  41666. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  41667. + DWC_ERROR("Work %s still pending", wc->name);
  41668. + }
  41669. + }
  41670. +#endif
  41671. + destroy_workqueue(wq->wq);
  41672. + DWC_SPINLOCK_FREE(wq->lock);
  41673. + DWC_WAITQ_FREE(wq->waitq);
  41674. + DWC_FREE(wq);
  41675. +}
  41676. +
  41677. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  41678. + char *format, ...)
  41679. +{
  41680. + dwc_irqflags_t flags;
  41681. + work_container_t *container;
  41682. + static char name[128];
  41683. + va_list args;
  41684. +
  41685. + va_start(args, format);
  41686. + DWC_VSNPRINTF(name, 128, format, args);
  41687. + va_end(args);
  41688. +
  41689. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41690. + wq->pending++;
  41691. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41692. + DWC_WAITQ_TRIGGER(wq->waitq);
  41693. +
  41694. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  41695. + if (!container) {
  41696. + DWC_ERROR("Cannot allocate memory for container\n");
  41697. + return;
  41698. + }
  41699. +
  41700. + container->name = DWC_STRDUP(name);
  41701. + if (!container->name) {
  41702. + DWC_ERROR("Cannot allocate memory for container->name\n");
  41703. + DWC_FREE(container);
  41704. + return;
  41705. + }
  41706. +
  41707. + container->cb = cb;
  41708. + container->data = data;
  41709. + container->wq = wq;
  41710. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  41711. + INIT_WORK(&container->work.work, do_work);
  41712. +
  41713. +#ifdef DEBUG
  41714. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  41715. +#endif
  41716. + queue_work(wq->wq, &container->work.work);
  41717. +}
  41718. +
  41719. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  41720. + void *data, uint32_t time, char *format, ...)
  41721. +{
  41722. + dwc_irqflags_t flags;
  41723. + work_container_t *container;
  41724. + static char name[128];
  41725. + va_list args;
  41726. +
  41727. + va_start(args, format);
  41728. + DWC_VSNPRINTF(name, 128, format, args);
  41729. + va_end(args);
  41730. +
  41731. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  41732. + wq->pending++;
  41733. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  41734. + DWC_WAITQ_TRIGGER(wq->waitq);
  41735. +
  41736. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  41737. + if (!container) {
  41738. + DWC_ERROR("Cannot allocate memory for container\n");
  41739. + return;
  41740. + }
  41741. +
  41742. + container->name = DWC_STRDUP(name);
  41743. + if (!container->name) {
  41744. + DWC_ERROR("Cannot allocate memory for container->name\n");
  41745. + DWC_FREE(container);
  41746. + return;
  41747. + }
  41748. +
  41749. + container->cb = cb;
  41750. + container->data = data;
  41751. + container->wq = wq;
  41752. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  41753. + INIT_DELAYED_WORK(&container->work, do_work);
  41754. +
  41755. +#ifdef DEBUG
  41756. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  41757. +#endif
  41758. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  41759. +}
  41760. +
  41761. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  41762. +{
  41763. + return wq->pending;
  41764. +}
  41765. +
  41766. +
  41767. +#ifdef DWC_LIBMODULE
  41768. +
  41769. +#ifdef DWC_CCLIB
  41770. +/* CC */
  41771. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  41772. +EXPORT_SYMBOL(dwc_cc_if_free);
  41773. +EXPORT_SYMBOL(dwc_cc_clear);
  41774. +EXPORT_SYMBOL(dwc_cc_add);
  41775. +EXPORT_SYMBOL(dwc_cc_remove);
  41776. +EXPORT_SYMBOL(dwc_cc_change);
  41777. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  41778. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  41779. +EXPORT_SYMBOL(dwc_cc_match_chid);
  41780. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  41781. +EXPORT_SYMBOL(dwc_cc_ck);
  41782. +EXPORT_SYMBOL(dwc_cc_chid);
  41783. +EXPORT_SYMBOL(dwc_cc_cdid);
  41784. +EXPORT_SYMBOL(dwc_cc_name);
  41785. +#endif /* DWC_CCLIB */
  41786. +
  41787. +#ifdef DWC_CRYPTOLIB
  41788. +# ifndef CONFIG_MACH_IPMATE
  41789. +/* Modpow */
  41790. +EXPORT_SYMBOL(dwc_modpow);
  41791. +
  41792. +/* DH */
  41793. +EXPORT_SYMBOL(dwc_dh_modpow);
  41794. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  41795. +EXPORT_SYMBOL(dwc_dh_pk);
  41796. +# endif /* CONFIG_MACH_IPMATE */
  41797. +
  41798. +/* Crypto */
  41799. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  41800. +EXPORT_SYMBOL(dwc_wusb_cmf);
  41801. +EXPORT_SYMBOL(dwc_wusb_prf);
  41802. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  41803. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  41804. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  41805. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  41806. +#endif /* DWC_CRYPTOLIB */
  41807. +
  41808. +/* Notification */
  41809. +#ifdef DWC_NOTIFYLIB
  41810. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  41811. +EXPORT_SYMBOL(dwc_free_notification_manager);
  41812. +EXPORT_SYMBOL(dwc_register_notifier);
  41813. +EXPORT_SYMBOL(dwc_unregister_notifier);
  41814. +EXPORT_SYMBOL(dwc_add_observer);
  41815. +EXPORT_SYMBOL(dwc_remove_observer);
  41816. +EXPORT_SYMBOL(dwc_notify);
  41817. +#endif
  41818. +
  41819. +/* Memory Debugging Routines */
  41820. +#ifdef DWC_DEBUG_MEMORY
  41821. +EXPORT_SYMBOL(dwc_alloc_debug);
  41822. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  41823. +EXPORT_SYMBOL(dwc_free_debug);
  41824. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  41825. +EXPORT_SYMBOL(dwc_dma_free_debug);
  41826. +#endif
  41827. +
  41828. +EXPORT_SYMBOL(DWC_MEMSET);
  41829. +EXPORT_SYMBOL(DWC_MEMCPY);
  41830. +EXPORT_SYMBOL(DWC_MEMMOVE);
  41831. +EXPORT_SYMBOL(DWC_MEMCMP);
  41832. +EXPORT_SYMBOL(DWC_STRNCMP);
  41833. +EXPORT_SYMBOL(DWC_STRCMP);
  41834. +EXPORT_SYMBOL(DWC_STRLEN);
  41835. +EXPORT_SYMBOL(DWC_STRCPY);
  41836. +EXPORT_SYMBOL(DWC_STRDUP);
  41837. +EXPORT_SYMBOL(DWC_ATOI);
  41838. +EXPORT_SYMBOL(DWC_ATOUI);
  41839. +
  41840. +#ifdef DWC_UTFLIB
  41841. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  41842. +#endif /* DWC_UTFLIB */
  41843. +
  41844. +EXPORT_SYMBOL(DWC_IN_IRQ);
  41845. +EXPORT_SYMBOL(DWC_IN_BH);
  41846. +EXPORT_SYMBOL(DWC_VPRINTF);
  41847. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  41848. +EXPORT_SYMBOL(DWC_PRINTF);
  41849. +EXPORT_SYMBOL(DWC_SPRINTF);
  41850. +EXPORT_SYMBOL(DWC_SNPRINTF);
  41851. +EXPORT_SYMBOL(__DWC_WARN);
  41852. +EXPORT_SYMBOL(__DWC_ERROR);
  41853. +EXPORT_SYMBOL(DWC_EXCEPTION);
  41854. +
  41855. +#ifdef DEBUG
  41856. +EXPORT_SYMBOL(__DWC_DEBUG);
  41857. +#endif
  41858. +
  41859. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  41860. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  41861. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  41862. +EXPORT_SYMBOL(__DWC_ALLOC);
  41863. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  41864. +EXPORT_SYMBOL(__DWC_FREE);
  41865. +
  41866. +#ifdef DWC_CRYPTOLIB
  41867. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  41868. +EXPORT_SYMBOL(DWC_AES_CBC);
  41869. +EXPORT_SYMBOL(DWC_SHA256);
  41870. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  41871. +#endif
  41872. +
  41873. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  41874. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  41875. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  41876. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  41877. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  41878. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  41879. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  41880. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  41881. +EXPORT_SYMBOL(DWC_READ_REG32);
  41882. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  41883. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  41884. +
  41885. +#if 0
  41886. +EXPORT_SYMBOL(DWC_READ_REG64);
  41887. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  41888. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  41889. +#endif
  41890. +
  41891. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  41892. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  41893. +EXPORT_SYMBOL(DWC_SPINLOCK);
  41894. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  41895. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  41896. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  41897. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  41898. +
  41899. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  41900. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  41901. +#endif
  41902. +
  41903. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  41904. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  41905. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  41906. +EXPORT_SYMBOL(DWC_UDELAY);
  41907. +EXPORT_SYMBOL(DWC_MDELAY);
  41908. +EXPORT_SYMBOL(DWC_MSLEEP);
  41909. +EXPORT_SYMBOL(DWC_TIME);
  41910. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  41911. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  41912. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  41913. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  41914. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  41915. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  41916. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  41917. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  41918. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  41919. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  41920. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  41921. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  41922. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  41923. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  41924. +EXPORT_SYMBOL(DWC_TASK_FREE);
  41925. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  41926. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  41927. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  41928. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  41929. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  41930. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  41931. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  41932. +
  41933. +static int dwc_common_port_init_module(void)
  41934. +{
  41935. + int result = 0;
  41936. +
  41937. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  41938. +
  41939. +#ifdef DWC_DEBUG_MEMORY
  41940. + result = dwc_memory_debug_start(NULL);
  41941. + if (result) {
  41942. + printk(KERN_ERR
  41943. + "dwc_memory_debug_start() failed with error %d\n",
  41944. + result);
  41945. + return result;
  41946. + }
  41947. +#endif
  41948. +
  41949. +#ifdef DWC_NOTIFYLIB
  41950. + result = dwc_alloc_notification_manager(NULL, NULL);
  41951. + if (result) {
  41952. + printk(KERN_ERR
  41953. + "dwc_alloc_notification_manager() failed with error %d\n",
  41954. + result);
  41955. + return result;
  41956. + }
  41957. +#endif
  41958. + return result;
  41959. +}
  41960. +
  41961. +static void dwc_common_port_exit_module(void)
  41962. +{
  41963. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  41964. +
  41965. +#ifdef DWC_NOTIFYLIB
  41966. + dwc_free_notification_manager();
  41967. +#endif
  41968. +
  41969. +#ifdef DWC_DEBUG_MEMORY
  41970. + dwc_memory_debug_stop();
  41971. +#endif
  41972. +}
  41973. +
  41974. +module_init(dwc_common_port_init_module);
  41975. +module_exit(dwc_common_port_exit_module);
  41976. +
  41977. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  41978. +MODULE_AUTHOR("Synopsys Inc.");
  41979. +MODULE_LICENSE ("GPL");
  41980. +
  41981. +#endif /* DWC_LIBMODULE */
  41982. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  41983. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 1970-01-01 01:00:00.000000000 +0100
  41984. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 2014-03-11 16:53:12.000000000 +0100
  41985. @@ -0,0 +1,1275 @@
  41986. +#include "dwc_os.h"
  41987. +#include "dwc_list.h"
  41988. +
  41989. +#ifdef DWC_CCLIB
  41990. +# include "dwc_cc.h"
  41991. +#endif
  41992. +
  41993. +#ifdef DWC_CRYPTOLIB
  41994. +# include "dwc_modpow.h"
  41995. +# include "dwc_dh.h"
  41996. +# include "dwc_crypto.h"
  41997. +#endif
  41998. +
  41999. +#ifdef DWC_NOTIFYLIB
  42000. +# include "dwc_notifier.h"
  42001. +#endif
  42002. +
  42003. +/* OS-Level Implementations */
  42004. +
  42005. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  42006. +
  42007. +
  42008. +/* MISC */
  42009. +
  42010. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  42011. +{
  42012. + return memset(dest, byte, size);
  42013. +}
  42014. +
  42015. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  42016. +{
  42017. + return memcpy(dest, src, size);
  42018. +}
  42019. +
  42020. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  42021. +{
  42022. + bcopy(src, dest, size);
  42023. + return dest;
  42024. +}
  42025. +
  42026. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  42027. +{
  42028. + return memcmp(m1, m2, size);
  42029. +}
  42030. +
  42031. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  42032. +{
  42033. + return strncmp(s1, s2, size);
  42034. +}
  42035. +
  42036. +int DWC_STRCMP(void *s1, void *s2)
  42037. +{
  42038. + return strcmp(s1, s2);
  42039. +}
  42040. +
  42041. +int DWC_STRLEN(char const *str)
  42042. +{
  42043. + return strlen(str);
  42044. +}
  42045. +
  42046. +char *DWC_STRCPY(char *to, char const *from)
  42047. +{
  42048. + return strcpy(to, from);
  42049. +}
  42050. +
  42051. +char *DWC_STRDUP(char const *str)
  42052. +{
  42053. + int len = DWC_STRLEN(str) + 1;
  42054. + char *new = DWC_ALLOC_ATOMIC(len);
  42055. +
  42056. + if (!new) {
  42057. + return NULL;
  42058. + }
  42059. +
  42060. + DWC_MEMCPY(new, str, len);
  42061. + return new;
  42062. +}
  42063. +
  42064. +int DWC_ATOI(char *str, int32_t *value)
  42065. +{
  42066. + char *end = NULL;
  42067. +
  42068. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  42069. + * should be equivalent on 2's complement machines
  42070. + */
  42071. + *value = strtoul(str, &end, 0);
  42072. + if (*end == '\0') {
  42073. + return 0;
  42074. + }
  42075. +
  42076. + return -1;
  42077. +}
  42078. +
  42079. +int DWC_ATOUI(char *str, uint32_t *value)
  42080. +{
  42081. + char *end = NULL;
  42082. +
  42083. + *value = strtoul(str, &end, 0);
  42084. + if (*end == '\0') {
  42085. + return 0;
  42086. + }
  42087. +
  42088. + return -1;
  42089. +}
  42090. +
  42091. +
  42092. +#ifdef DWC_UTFLIB
  42093. +/* From usbstring.c */
  42094. +
  42095. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  42096. +{
  42097. + int count = 0;
  42098. + u8 c;
  42099. + u16 uchar;
  42100. +
  42101. + /* this insists on correct encodings, though not minimal ones.
  42102. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  42103. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  42104. + */
  42105. + while (len != 0 && (c = (u8) *s++) != 0) {
  42106. + if (unlikely(c & 0x80)) {
  42107. + // 2-byte sequence:
  42108. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  42109. + if ((c & 0xe0) == 0xc0) {
  42110. + uchar = (c & 0x1f) << 6;
  42111. +
  42112. + c = (u8) *s++;
  42113. + if ((c & 0xc0) != 0xc0)
  42114. + goto fail;
  42115. + c &= 0x3f;
  42116. + uchar |= c;
  42117. +
  42118. + // 3-byte sequence (most CJKV characters):
  42119. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  42120. + } else if ((c & 0xf0) == 0xe0) {
  42121. + uchar = (c & 0x0f) << 12;
  42122. +
  42123. + c = (u8) *s++;
  42124. + if ((c & 0xc0) != 0xc0)
  42125. + goto fail;
  42126. + c &= 0x3f;
  42127. + uchar |= c << 6;
  42128. +
  42129. + c = (u8) *s++;
  42130. + if ((c & 0xc0) != 0xc0)
  42131. + goto fail;
  42132. + c &= 0x3f;
  42133. + uchar |= c;
  42134. +
  42135. + /* no bogus surrogates */
  42136. + if (0xd800 <= uchar && uchar <= 0xdfff)
  42137. + goto fail;
  42138. +
  42139. + // 4-byte sequence (surrogate pairs, currently rare):
  42140. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  42141. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  42142. + // (uuuuu = wwww + 1)
  42143. + // FIXME accept the surrogate code points (only)
  42144. + } else
  42145. + goto fail;
  42146. + } else
  42147. + uchar = c;
  42148. + put_unaligned (cpu_to_le16 (uchar), cp++);
  42149. + count++;
  42150. + len--;
  42151. + }
  42152. + return count;
  42153. +fail:
  42154. + return -1;
  42155. +}
  42156. +
  42157. +#endif /* DWC_UTFLIB */
  42158. +
  42159. +
  42160. +/* dwc_debug.h */
  42161. +
  42162. +dwc_bool_t DWC_IN_IRQ(void)
  42163. +{
  42164. +// return in_irq();
  42165. + return 0;
  42166. +}
  42167. +
  42168. +dwc_bool_t DWC_IN_BH(void)
  42169. +{
  42170. +// return in_softirq();
  42171. + return 0;
  42172. +}
  42173. +
  42174. +void DWC_VPRINTF(char *format, va_list args)
  42175. +{
  42176. + vprintf(format, args);
  42177. +}
  42178. +
  42179. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  42180. +{
  42181. + return vsnprintf(str, size, format, args);
  42182. +}
  42183. +
  42184. +void DWC_PRINTF(char *format, ...)
  42185. +{
  42186. + va_list args;
  42187. +
  42188. + va_start(args, format);
  42189. + DWC_VPRINTF(format, args);
  42190. + va_end(args);
  42191. +}
  42192. +
  42193. +int DWC_SPRINTF(char *buffer, char *format, ...)
  42194. +{
  42195. + int retval;
  42196. + va_list args;
  42197. +
  42198. + va_start(args, format);
  42199. + retval = vsprintf(buffer, format, args);
  42200. + va_end(args);
  42201. + return retval;
  42202. +}
  42203. +
  42204. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  42205. +{
  42206. + int retval;
  42207. + va_list args;
  42208. +
  42209. + va_start(args, format);
  42210. + retval = vsnprintf(buffer, size, format, args);
  42211. + va_end(args);
  42212. + return retval;
  42213. +}
  42214. +
  42215. +void __DWC_WARN(char *format, ...)
  42216. +{
  42217. + va_list args;
  42218. +
  42219. + va_start(args, format);
  42220. + DWC_VPRINTF(format, args);
  42221. + va_end(args);
  42222. +}
  42223. +
  42224. +void __DWC_ERROR(char *format, ...)
  42225. +{
  42226. + va_list args;
  42227. +
  42228. + va_start(args, format);
  42229. + DWC_VPRINTF(format, args);
  42230. + va_end(args);
  42231. +}
  42232. +
  42233. +void DWC_EXCEPTION(char *format, ...)
  42234. +{
  42235. + va_list args;
  42236. +
  42237. + va_start(args, format);
  42238. + DWC_VPRINTF(format, args);
  42239. + va_end(args);
  42240. +// BUG_ON(1); ???
  42241. +}
  42242. +
  42243. +#ifdef DEBUG
  42244. +void __DWC_DEBUG(char *format, ...)
  42245. +{
  42246. + va_list args;
  42247. +
  42248. + va_start(args, format);
  42249. + DWC_VPRINTF(format, args);
  42250. + va_end(args);
  42251. +}
  42252. +#endif
  42253. +
  42254. +
  42255. +/* dwc_mem.h */
  42256. +
  42257. +#if 0
  42258. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  42259. + uint32_t align,
  42260. + uint32_t alloc)
  42261. +{
  42262. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  42263. + size, align, alloc);
  42264. + return (dwc_pool_t *)pool;
  42265. +}
  42266. +
  42267. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  42268. +{
  42269. + dma_pool_destroy((struct dma_pool *)pool);
  42270. +}
  42271. +
  42272. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  42273. +{
  42274. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  42275. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  42276. +}
  42277. +
  42278. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  42279. +{
  42280. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  42281. + memset(..);
  42282. +}
  42283. +
  42284. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  42285. +{
  42286. + dma_pool_free(pool, vaddr, daddr);
  42287. +}
  42288. +#endif
  42289. +
  42290. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  42291. +{
  42292. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  42293. + int error;
  42294. +
  42295. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  42296. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  42297. + &dma->nsegs, BUS_DMA_NOWAIT);
  42298. + if (error) {
  42299. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  42300. + (uintmax_t)size, error);
  42301. + goto fail_0;
  42302. + }
  42303. +
  42304. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  42305. + (caddr_t *)&dma->dma_vaddr,
  42306. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  42307. + if (error) {
  42308. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  42309. + goto fail_1;
  42310. + }
  42311. +
  42312. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  42313. + BUS_DMA_NOWAIT, &dma->dma_map);
  42314. + if (error) {
  42315. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  42316. + goto fail_2;
  42317. + }
  42318. +
  42319. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  42320. + size, NULL, BUS_DMA_NOWAIT);
  42321. + if (error) {
  42322. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  42323. + goto fail_3;
  42324. + }
  42325. +
  42326. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  42327. + *dma_addr = dma->dma_paddr;
  42328. + return dma->dma_vaddr;
  42329. +
  42330. +fail_3:
  42331. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  42332. +fail_2:
  42333. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  42334. +fail_1:
  42335. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  42336. +fail_0:
  42337. + dma->dma_map = NULL;
  42338. + dma->dma_vaddr = NULL;
  42339. + dma->nsegs = 0;
  42340. +
  42341. + return NULL;
  42342. +}
  42343. +
  42344. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  42345. +{
  42346. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  42347. +
  42348. + if (dma->dma_map != NULL) {
  42349. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  42350. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  42351. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  42352. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  42353. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  42354. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  42355. + dma->dma_paddr = 0;
  42356. + dma->dma_map = NULL;
  42357. + dma->dma_vaddr = NULL;
  42358. + dma->nsegs = 0;
  42359. + }
  42360. +}
  42361. +
  42362. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  42363. +{
  42364. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  42365. +}
  42366. +
  42367. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  42368. +{
  42369. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  42370. +}
  42371. +
  42372. +void __DWC_FREE(void *mem_ctx, void *addr)
  42373. +{
  42374. + free(addr, M_DEVBUF);
  42375. +}
  42376. +
  42377. +
  42378. +#ifdef DWC_CRYPTOLIB
  42379. +/* dwc_crypto.h */
  42380. +
  42381. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  42382. +{
  42383. + get_random_bytes(buffer, length);
  42384. +}
  42385. +
  42386. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  42387. +{
  42388. + struct crypto_blkcipher *tfm;
  42389. + struct blkcipher_desc desc;
  42390. + struct scatterlist sgd;
  42391. + struct scatterlist sgs;
  42392. +
  42393. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  42394. + if (tfm == NULL) {
  42395. + printk("failed to load transform for aes CBC\n");
  42396. + return -1;
  42397. + }
  42398. +
  42399. + crypto_blkcipher_setkey(tfm, key, keylen);
  42400. + crypto_blkcipher_set_iv(tfm, iv, 16);
  42401. +
  42402. + sg_init_one(&sgd, out, messagelen);
  42403. + sg_init_one(&sgs, message, messagelen);
  42404. +
  42405. + desc.tfm = tfm;
  42406. + desc.flags = 0;
  42407. +
  42408. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  42409. + crypto_free_blkcipher(tfm);
  42410. + DWC_ERROR("AES CBC encryption failed");
  42411. + return -1;
  42412. + }
  42413. +
  42414. + crypto_free_blkcipher(tfm);
  42415. + return 0;
  42416. +}
  42417. +
  42418. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  42419. +{
  42420. + struct crypto_hash *tfm;
  42421. + struct hash_desc desc;
  42422. + struct scatterlist sg;
  42423. +
  42424. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  42425. + if (IS_ERR(tfm)) {
  42426. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  42427. + return 0;
  42428. + }
  42429. + desc.tfm = tfm;
  42430. + desc.flags = 0;
  42431. +
  42432. + sg_init_one(&sg, message, len);
  42433. + crypto_hash_digest(&desc, &sg, len, out);
  42434. + crypto_free_hash(tfm);
  42435. +
  42436. + return 1;
  42437. +}
  42438. +
  42439. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  42440. + uint8_t *key, uint32_t keylen, uint8_t *out)
  42441. +{
  42442. + struct crypto_hash *tfm;
  42443. + struct hash_desc desc;
  42444. + struct scatterlist sg;
  42445. +
  42446. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  42447. + if (IS_ERR(tfm)) {
  42448. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  42449. + return 0;
  42450. + }
  42451. + desc.tfm = tfm;
  42452. + desc.flags = 0;
  42453. +
  42454. + sg_init_one(&sg, message, messagelen);
  42455. + crypto_hash_setkey(tfm, key, keylen);
  42456. + crypto_hash_digest(&desc, &sg, messagelen, out);
  42457. + crypto_free_hash(tfm);
  42458. +
  42459. + return 1;
  42460. +}
  42461. +
  42462. +#endif /* DWC_CRYPTOLIB */
  42463. +
  42464. +
  42465. +/* Byte Ordering Conversions */
  42466. +
  42467. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  42468. +{
  42469. +#ifdef __LITTLE_ENDIAN
  42470. + return *p;
  42471. +#else
  42472. + uint8_t *u_p = (uint8_t *)p;
  42473. +
  42474. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  42475. +#endif
  42476. +}
  42477. +
  42478. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  42479. +{
  42480. +#ifdef __BIG_ENDIAN
  42481. + return *p;
  42482. +#else
  42483. + uint8_t *u_p = (uint8_t *)p;
  42484. +
  42485. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  42486. +#endif
  42487. +}
  42488. +
  42489. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  42490. +{
  42491. +#ifdef __LITTLE_ENDIAN
  42492. + return *p;
  42493. +#else
  42494. + uint8_t *u_p = (uint8_t *)p;
  42495. +
  42496. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  42497. +#endif
  42498. +}
  42499. +
  42500. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  42501. +{
  42502. +#ifdef __BIG_ENDIAN
  42503. + return *p;
  42504. +#else
  42505. + uint8_t *u_p = (uint8_t *)p;
  42506. +
  42507. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  42508. +#endif
  42509. +}
  42510. +
  42511. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  42512. +{
  42513. +#ifdef __LITTLE_ENDIAN
  42514. + return *p;
  42515. +#else
  42516. + uint8_t *u_p = (uint8_t *)p;
  42517. + return (u_p[1] | (u_p[0] << 8));
  42518. +#endif
  42519. +}
  42520. +
  42521. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  42522. +{
  42523. +#ifdef __BIG_ENDIAN
  42524. + return *p;
  42525. +#else
  42526. + uint8_t *u_p = (uint8_t *)p;
  42527. + return (u_p[1] | (u_p[0] << 8));
  42528. +#endif
  42529. +}
  42530. +
  42531. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  42532. +{
  42533. +#ifdef __LITTLE_ENDIAN
  42534. + return *p;
  42535. +#else
  42536. + uint8_t *u_p = (uint8_t *)p;
  42537. + return (u_p[1] | (u_p[0] << 8));
  42538. +#endif
  42539. +}
  42540. +
  42541. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  42542. +{
  42543. +#ifdef __BIG_ENDIAN
  42544. + return *p;
  42545. +#else
  42546. + uint8_t *u_p = (uint8_t *)p;
  42547. + return (u_p[1] | (u_p[0] << 8));
  42548. +#endif
  42549. +}
  42550. +
  42551. +
  42552. +/* Registers */
  42553. +
  42554. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  42555. +{
  42556. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  42557. + bus_size_t ior = (bus_size_t)reg;
  42558. +
  42559. + return bus_space_read_4(io->iot, io->ioh, ior);
  42560. +}
  42561. +
  42562. +#if 0
  42563. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  42564. +{
  42565. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  42566. + bus_size_t ior = (bus_size_t)reg;
  42567. +
  42568. + return bus_space_read_8(io->iot, io->ioh, ior);
  42569. +}
  42570. +#endif
  42571. +
  42572. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  42573. +{
  42574. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  42575. + bus_size_t ior = (bus_size_t)reg;
  42576. +
  42577. + bus_space_write_4(io->iot, io->ioh, ior, value);
  42578. +}
  42579. +
  42580. +#if 0
  42581. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  42582. +{
  42583. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  42584. + bus_size_t ior = (bus_size_t)reg;
  42585. +
  42586. + bus_space_write_8(io->iot, io->ioh, ior, value);
  42587. +}
  42588. +#endif
  42589. +
  42590. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  42591. + uint32_t set_mask)
  42592. +{
  42593. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  42594. + bus_size_t ior = (bus_size_t)reg;
  42595. +
  42596. + bus_space_write_4(io->iot, io->ioh, ior,
  42597. + (bus_space_read_4(io->iot, io->ioh, ior) &
  42598. + ~clear_mask) | set_mask);
  42599. +}
  42600. +
  42601. +#if 0
  42602. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  42603. + uint64_t set_mask)
  42604. +{
  42605. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  42606. + bus_size_t ior = (bus_size_t)reg;
  42607. +
  42608. + bus_space_write_8(io->iot, io->ioh, ior,
  42609. + (bus_space_read_8(io->iot, io->ioh, ior) &
  42610. + ~clear_mask) | set_mask);
  42611. +}
  42612. +#endif
  42613. +
  42614. +
  42615. +/* Locking */
  42616. +
  42617. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  42618. +{
  42619. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  42620. +
  42621. + if (!sl) {
  42622. + DWC_ERROR("Cannot allocate memory for spinlock");
  42623. + return NULL;
  42624. + }
  42625. +
  42626. + simple_lock_init(sl);
  42627. + return (dwc_spinlock_t *)sl;
  42628. +}
  42629. +
  42630. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  42631. +{
  42632. + struct simplelock *sl = (struct simplelock *)lock;
  42633. +
  42634. + DWC_FREE(sl);
  42635. +}
  42636. +
  42637. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  42638. +{
  42639. + simple_lock((struct simplelock *)lock);
  42640. +}
  42641. +
  42642. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  42643. +{
  42644. + simple_unlock((struct simplelock *)lock);
  42645. +}
  42646. +
  42647. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  42648. +{
  42649. + simple_lock((struct simplelock *)lock);
  42650. + *flags = splbio();
  42651. +}
  42652. +
  42653. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  42654. +{
  42655. + splx(flags);
  42656. + simple_unlock((struct simplelock *)lock);
  42657. +}
  42658. +
  42659. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  42660. +{
  42661. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  42662. +
  42663. + if (!mutex) {
  42664. + DWC_ERROR("Cannot allocate memory for mutex");
  42665. + return NULL;
  42666. + }
  42667. +
  42668. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  42669. + return mutex;
  42670. +}
  42671. +
  42672. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  42673. +#else
  42674. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  42675. +{
  42676. + DWC_FREE(mutex);
  42677. +}
  42678. +#endif
  42679. +
  42680. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  42681. +{
  42682. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  42683. +}
  42684. +
  42685. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  42686. +{
  42687. + int status;
  42688. +
  42689. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  42690. + return status == 0;
  42691. +}
  42692. +
  42693. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  42694. +{
  42695. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  42696. +}
  42697. +
  42698. +
  42699. +/* Timing */
  42700. +
  42701. +void DWC_UDELAY(uint32_t usecs)
  42702. +{
  42703. + DELAY(usecs);
  42704. +}
  42705. +
  42706. +void DWC_MDELAY(uint32_t msecs)
  42707. +{
  42708. + do {
  42709. + DELAY(1000);
  42710. + } while (--msecs);
  42711. +}
  42712. +
  42713. +void DWC_MSLEEP(uint32_t msecs)
  42714. +{
  42715. + struct timeval tv;
  42716. +
  42717. + tv.tv_sec = msecs / 1000;
  42718. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  42719. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  42720. +}
  42721. +
  42722. +uint32_t DWC_TIME(void)
  42723. +{
  42724. + struct timeval tv;
  42725. +
  42726. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  42727. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  42728. +}
  42729. +
  42730. +
  42731. +/* Timers */
  42732. +
  42733. +struct dwc_timer {
  42734. + struct callout t;
  42735. + char *name;
  42736. + dwc_spinlock_t *lock;
  42737. + dwc_timer_callback_t cb;
  42738. + void *data;
  42739. +};
  42740. +
  42741. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  42742. +{
  42743. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  42744. +
  42745. + if (!t) {
  42746. + DWC_ERROR("Cannot allocate memory for timer");
  42747. + return NULL;
  42748. + }
  42749. +
  42750. + callout_init(&t->t);
  42751. +
  42752. + t->name = DWC_STRDUP(name);
  42753. + if (!t->name) {
  42754. + DWC_ERROR("Cannot allocate memory for timer->name");
  42755. + goto no_name;
  42756. + }
  42757. +
  42758. + t->lock = DWC_SPINLOCK_ALLOC();
  42759. + if (!t->lock) {
  42760. + DWC_ERROR("Cannot allocate memory for timer->lock");
  42761. + goto no_lock;
  42762. + }
  42763. +
  42764. + t->cb = cb;
  42765. + t->data = data;
  42766. +
  42767. + return t;
  42768. +
  42769. + no_lock:
  42770. + DWC_FREE(t->name);
  42771. + no_name:
  42772. + DWC_FREE(t);
  42773. +
  42774. + return NULL;
  42775. +}
  42776. +
  42777. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  42778. +{
  42779. + callout_stop(&timer->t);
  42780. + DWC_SPINLOCK_FREE(timer->lock);
  42781. + DWC_FREE(timer->name);
  42782. + DWC_FREE(timer);
  42783. +}
  42784. +
  42785. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  42786. +{
  42787. + struct timeval tv;
  42788. +
  42789. + tv.tv_sec = time / 1000;
  42790. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  42791. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  42792. +}
  42793. +
  42794. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  42795. +{
  42796. + callout_stop(&timer->t);
  42797. +}
  42798. +
  42799. +
  42800. +/* Wait Queues */
  42801. +
  42802. +struct dwc_waitq {
  42803. + struct simplelock lock;
  42804. + int abort;
  42805. +};
  42806. +
  42807. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  42808. +{
  42809. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  42810. +
  42811. + if (!wq) {
  42812. + DWC_ERROR("Cannot allocate memory for waitqueue");
  42813. + return NULL;
  42814. + }
  42815. +
  42816. + simple_lock_init(&wq->lock);
  42817. + wq->abort = 0;
  42818. +
  42819. + return wq;
  42820. +}
  42821. +
  42822. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  42823. +{
  42824. + DWC_FREE(wq);
  42825. +}
  42826. +
  42827. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  42828. +{
  42829. + int ipl;
  42830. + int result = 0;
  42831. +
  42832. + simple_lock(&wq->lock);
  42833. + ipl = splbio();
  42834. +
  42835. + /* Skip the sleep if already aborted or triggered */
  42836. + if (!wq->abort && !cond(data)) {
  42837. + splx(ipl);
  42838. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  42839. + ipl = splbio();
  42840. + }
  42841. +
  42842. + if (result == 0) { // awoken
  42843. + if (wq->abort) {
  42844. + wq->abort = 0;
  42845. + result = -DWC_E_ABORT;
  42846. + } else {
  42847. + result = 0;
  42848. + }
  42849. +
  42850. + splx(ipl);
  42851. + simple_unlock(&wq->lock);
  42852. + } else {
  42853. + wq->abort = 0;
  42854. + splx(ipl);
  42855. + simple_unlock(&wq->lock);
  42856. +
  42857. + if (result == ERESTART) { // signaled - restart
  42858. + result = -DWC_E_RESTART;
  42859. + } else { // signaled - must be EINTR
  42860. + result = -DWC_E_ABORT;
  42861. + }
  42862. + }
  42863. +
  42864. + return result;
  42865. +}
  42866. +
  42867. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  42868. + void *data, int32_t msecs)
  42869. +{
  42870. + struct timeval tv, tv1, tv2;
  42871. + int ipl;
  42872. + int result = 0;
  42873. +
  42874. + tv.tv_sec = msecs / 1000;
  42875. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  42876. +
  42877. + simple_lock(&wq->lock);
  42878. + ipl = splbio();
  42879. +
  42880. + /* Skip the sleep if already aborted or triggered */
  42881. + if (!wq->abort && !cond(data)) {
  42882. + splx(ipl);
  42883. + getmicrouptime(&tv1);
  42884. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  42885. + getmicrouptime(&tv2);
  42886. + ipl = splbio();
  42887. + }
  42888. +
  42889. + if (result == 0) { // awoken
  42890. + if (wq->abort) {
  42891. + wq->abort = 0;
  42892. + splx(ipl);
  42893. + simple_unlock(&wq->lock);
  42894. + result = -DWC_E_ABORT;
  42895. + } else {
  42896. + splx(ipl);
  42897. + simple_unlock(&wq->lock);
  42898. +
  42899. + tv2.tv_usec -= tv1.tv_usec;
  42900. + if (tv2.tv_usec < 0) {
  42901. + tv2.tv_usec += 1000000;
  42902. + tv2.tv_sec--;
  42903. + }
  42904. +
  42905. + tv2.tv_sec -= tv1.tv_sec;
  42906. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  42907. + result = msecs - result;
  42908. + if (result <= 0)
  42909. + result = 1;
  42910. + }
  42911. + } else {
  42912. + wq->abort = 0;
  42913. + splx(ipl);
  42914. + simple_unlock(&wq->lock);
  42915. +
  42916. + if (result == ERESTART) { // signaled - restart
  42917. + result = -DWC_E_RESTART;
  42918. +
  42919. + } else if (result == EINTR) { // signaled - interrupt
  42920. + result = -DWC_E_ABORT;
  42921. +
  42922. + } else { // timed out
  42923. + result = -DWC_E_TIMEOUT;
  42924. + }
  42925. + }
  42926. +
  42927. + return result;
  42928. +}
  42929. +
  42930. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  42931. +{
  42932. + wakeup(wq);
  42933. +}
  42934. +
  42935. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  42936. +{
  42937. + int ipl;
  42938. +
  42939. + simple_lock(&wq->lock);
  42940. + ipl = splbio();
  42941. + wq->abort = 1;
  42942. + wakeup(wq);
  42943. + splx(ipl);
  42944. + simple_unlock(&wq->lock);
  42945. +}
  42946. +
  42947. +
  42948. +/* Threading */
  42949. +
  42950. +struct dwc_thread {
  42951. + struct proc *proc;
  42952. + int abort;
  42953. +};
  42954. +
  42955. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  42956. +{
  42957. + int retval;
  42958. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  42959. +
  42960. + if (!thread) {
  42961. + return NULL;
  42962. + }
  42963. +
  42964. + thread->abort = 0;
  42965. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  42966. + "%s", name);
  42967. + if (retval) {
  42968. + DWC_FREE(thread);
  42969. + return NULL;
  42970. + }
  42971. +
  42972. + return thread;
  42973. +}
  42974. +
  42975. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  42976. +{
  42977. + int retval;
  42978. +
  42979. + thread->abort = 1;
  42980. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  42981. +
  42982. + if (retval == 0) {
  42983. + /* DWC_THREAD_EXIT() will free the thread struct */
  42984. + return 0;
  42985. + }
  42986. +
  42987. + /* NOTE: We leak the thread struct if thread doesn't die */
  42988. +
  42989. + if (retval == EWOULDBLOCK) {
  42990. + return -DWC_E_TIMEOUT;
  42991. + }
  42992. +
  42993. + return -DWC_E_UNKNOWN;
  42994. +}
  42995. +
  42996. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  42997. +{
  42998. + return thread->abort;
  42999. +}
  43000. +
  43001. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  43002. +{
  43003. + wakeup(&thread->abort);
  43004. + DWC_FREE(thread);
  43005. + kthread_exit(0);
  43006. +}
  43007. +
  43008. +/* tasklets
  43009. + - Runs in interrupt context (cannot sleep)
  43010. + - Each tasklet runs on a single CPU
  43011. + - Different tasklets can be running simultaneously on different CPUs
  43012. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  43013. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  43014. + */
  43015. +struct dwc_tasklet {
  43016. + dwc_tasklet_callback_t cb;
  43017. + void *data;
  43018. +};
  43019. +
  43020. +static void tasklet_callback(void *data)
  43021. +{
  43022. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  43023. +
  43024. + task->cb(task->data);
  43025. +}
  43026. +
  43027. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  43028. +{
  43029. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  43030. +
  43031. + if (task) {
  43032. + task->cb = cb;
  43033. + task->data = data;
  43034. + } else {
  43035. + DWC_ERROR("Cannot allocate memory for tasklet");
  43036. + }
  43037. +
  43038. + return task;
  43039. +}
  43040. +
  43041. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  43042. +{
  43043. + DWC_FREE(task);
  43044. +}
  43045. +
  43046. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  43047. +{
  43048. + tasklet_callback(task);
  43049. +}
  43050. +
  43051. +
  43052. +/* workqueues
  43053. + - Runs in process context (can sleep)
  43054. + */
  43055. +typedef struct work_container {
  43056. + dwc_work_callback_t cb;
  43057. + void *data;
  43058. + dwc_workq_t *wq;
  43059. + char *name;
  43060. + int hz;
  43061. + struct work task;
  43062. +} work_container_t;
  43063. +
  43064. +struct dwc_workq {
  43065. + struct workqueue *taskq;
  43066. + dwc_spinlock_t *lock;
  43067. + dwc_waitq_t *waitq;
  43068. + int pending;
  43069. + struct work_container *container;
  43070. +};
  43071. +
  43072. +static void do_work(struct work *task, void *data)
  43073. +{
  43074. + dwc_workq_t *wq = (dwc_workq_t *)data;
  43075. + work_container_t *container = wq->container;
  43076. + dwc_irqflags_t flags;
  43077. +
  43078. + if (container->hz) {
  43079. + tsleep(container, 0, "dw3wrk", container->hz);
  43080. + }
  43081. +
  43082. + container->cb(container->data);
  43083. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  43084. +
  43085. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43086. + if (container->name)
  43087. + DWC_FREE(container->name);
  43088. + DWC_FREE(container);
  43089. + wq->pending--;
  43090. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43091. + DWC_WAITQ_TRIGGER(wq->waitq);
  43092. +}
  43093. +
  43094. +static int work_done(void *data)
  43095. +{
  43096. + dwc_workq_t *workq = (dwc_workq_t *)data;
  43097. +
  43098. + return workq->pending == 0;
  43099. +}
  43100. +
  43101. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  43102. +{
  43103. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  43104. +}
  43105. +
  43106. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  43107. +{
  43108. + int result;
  43109. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  43110. +
  43111. + if (!wq) {
  43112. + DWC_ERROR("Cannot allocate memory for workqueue");
  43113. + return NULL;
  43114. + }
  43115. +
  43116. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  43117. + IPL_BIO, 0);
  43118. + if (result) {
  43119. + DWC_ERROR("Cannot create workqueue");
  43120. + goto no_taskq;
  43121. + }
  43122. +
  43123. + wq->pending = 0;
  43124. +
  43125. + wq->lock = DWC_SPINLOCK_ALLOC();
  43126. + if (!wq->lock) {
  43127. + DWC_ERROR("Cannot allocate memory for spinlock");
  43128. + goto no_lock;
  43129. + }
  43130. +
  43131. + wq->waitq = DWC_WAITQ_ALLOC();
  43132. + if (!wq->waitq) {
  43133. + DWC_ERROR("Cannot allocate memory for waitqueue");
  43134. + goto no_waitq;
  43135. + }
  43136. +
  43137. + return wq;
  43138. +
  43139. + no_waitq:
  43140. + DWC_SPINLOCK_FREE(wq->lock);
  43141. + no_lock:
  43142. + workqueue_destroy(wq->taskq);
  43143. + no_taskq:
  43144. + DWC_FREE(wq);
  43145. +
  43146. + return NULL;
  43147. +}
  43148. +
  43149. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  43150. +{
  43151. +#ifdef DEBUG
  43152. + dwc_irqflags_t flags;
  43153. +
  43154. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43155. +
  43156. + if (wq->pending != 0) {
  43157. + struct work_container *container = wq->container;
  43158. +
  43159. + DWC_ERROR("Destroying work queue with pending work");
  43160. +
  43161. + if (container && container->name) {
  43162. + DWC_ERROR("Work %s still pending", container->name);
  43163. + }
  43164. + }
  43165. +
  43166. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43167. +#endif
  43168. + DWC_WAITQ_FREE(wq->waitq);
  43169. + DWC_SPINLOCK_FREE(wq->lock);
  43170. + workqueue_destroy(wq->taskq);
  43171. + DWC_FREE(wq);
  43172. +}
  43173. +
  43174. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  43175. + char *format, ...)
  43176. +{
  43177. + dwc_irqflags_t flags;
  43178. + work_container_t *container;
  43179. + static char name[128];
  43180. + va_list args;
  43181. +
  43182. + va_start(args, format);
  43183. + DWC_VSNPRINTF(name, 128, format, args);
  43184. + va_end(args);
  43185. +
  43186. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43187. + wq->pending++;
  43188. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43189. + DWC_WAITQ_TRIGGER(wq->waitq);
  43190. +
  43191. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  43192. + if (!container) {
  43193. + DWC_ERROR("Cannot allocate memory for container");
  43194. + return;
  43195. + }
  43196. +
  43197. + container->name = DWC_STRDUP(name);
  43198. + if (!container->name) {
  43199. + DWC_ERROR("Cannot allocate memory for container->name");
  43200. + DWC_FREE(container);
  43201. + return;
  43202. + }
  43203. +
  43204. + container->cb = cb;
  43205. + container->data = data;
  43206. + container->wq = wq;
  43207. + container->hz = 0;
  43208. + wq->container = container;
  43209. +
  43210. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  43211. + workqueue_enqueue(wq->taskq, &container->task);
  43212. +}
  43213. +
  43214. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  43215. + void *data, uint32_t time, char *format, ...)
  43216. +{
  43217. + dwc_irqflags_t flags;
  43218. + work_container_t *container;
  43219. + static char name[128];
  43220. + struct timeval tv;
  43221. + va_list args;
  43222. +
  43223. + va_start(args, format);
  43224. + DWC_VSNPRINTF(name, 128, format, args);
  43225. + va_end(args);
  43226. +
  43227. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  43228. + wq->pending++;
  43229. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  43230. + DWC_WAITQ_TRIGGER(wq->waitq);
  43231. +
  43232. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  43233. + if (!container) {
  43234. + DWC_ERROR("Cannot allocate memory for container");
  43235. + return;
  43236. + }
  43237. +
  43238. + container->name = DWC_STRDUP(name);
  43239. + if (!container->name) {
  43240. + DWC_ERROR("Cannot allocate memory for container->name");
  43241. + DWC_FREE(container);
  43242. + return;
  43243. + }
  43244. +
  43245. + container->cb = cb;
  43246. + container->data = data;
  43247. + container->wq = wq;
  43248. + tv.tv_sec = time / 1000;
  43249. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  43250. + container->hz = tvtohz(&tv);
  43251. + wq->container = container;
  43252. +
  43253. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  43254. + workqueue_enqueue(wq->taskq, &container->task);
  43255. +}
  43256. +
  43257. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  43258. +{
  43259. + return wq->pending;
  43260. +}
  43261. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_crypto.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_crypto.c
  43262. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_crypto.c 1970-01-01 01:00:00.000000000 +0100
  43263. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_crypto.c 2014-03-11 16:53:12.000000000 +0100
  43264. @@ -0,0 +1,308 @@
  43265. +/* =========================================================================
  43266. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  43267. + * $Revision: #5 $
  43268. + * $Date: 2010/09/28 $
  43269. + * $Change: 1596182 $
  43270. + *
  43271. + * Synopsys Portability Library Software and documentation
  43272. + * (hereinafter, "Software") is an Unsupported proprietary work of
  43273. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  43274. + * between Synopsys and you.
  43275. + *
  43276. + * The Software IS NOT an item of Licensed Software or Licensed Product
  43277. + * under any End User Software License Agreement or Agreement for
  43278. + * Licensed Product with Synopsys or any supplement thereto. You are
  43279. + * permitted to use and redistribute this Software in source and binary
  43280. + * forms, with or without modification, provided that redistributions
  43281. + * of source code must retain this notice. You may not view, use,
  43282. + * disclose, copy or distribute this file or any information contained
  43283. + * herein except pursuant to this license grant from Synopsys. If you
  43284. + * do not agree with this notice, including the disclaimer below, then
  43285. + * you are not authorized to use the Software.
  43286. + *
  43287. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  43288. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  43289. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  43290. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  43291. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43292. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  43293. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  43294. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  43295. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43296. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  43297. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  43298. + * DAMAGE.
  43299. + * ========================================================================= */
  43300. +
  43301. +/** @file
  43302. + * This file contains the WUSB cryptographic routines.
  43303. + */
  43304. +
  43305. +#ifdef DWC_CRYPTOLIB
  43306. +
  43307. +#include "dwc_crypto.h"
  43308. +#include "usb.h"
  43309. +
  43310. +#ifdef DEBUG
  43311. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  43312. +{
  43313. + int i;
  43314. + DWC_PRINTF("%s: ", name);
  43315. + for (i=0; i<len; i++) {
  43316. + DWC_PRINTF("%02x ", bytes[i]);
  43317. + }
  43318. + DWC_PRINTF("\n");
  43319. +}
  43320. +#else
  43321. +#define dump_bytes(x...)
  43322. +#endif
  43323. +
  43324. +/* Display a block */
  43325. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  43326. +{
  43327. +#ifdef DWC_DEBUG_CRYPTO
  43328. + int i, blksize = 16;
  43329. +
  43330. + DWC_DEBUG("%s", prefix);
  43331. +
  43332. + if (suffix == NULL) {
  43333. + suffix = "\n";
  43334. + blksize = a;
  43335. + }
  43336. +
  43337. + for (i = 0; i < blksize; i++)
  43338. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  43339. + DWC_PRINT(suffix);
  43340. +#endif
  43341. +}
  43342. +
  43343. +/**
  43344. + * Encrypts an array of bytes using the AES encryption engine.
  43345. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  43346. + * in-place.
  43347. + *
  43348. + * @return 0 on success, negative error code on error.
  43349. + */
  43350. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  43351. +{
  43352. + u8 block_t[16];
  43353. + DWC_MEMSET(block_t, 0, 16);
  43354. +
  43355. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  43356. +}
  43357. +
  43358. +/**
  43359. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  43360. + * This function takes a data string and returns the encrypted CBC
  43361. + * Counter-mode MIC.
  43362. + *
  43363. + * @param key The 128-bit symmetric key.
  43364. + * @param nonce The CCM nonce.
  43365. + * @param label The unique 14-byte ASCII text label.
  43366. + * @param bytes The byte array to be encrypted.
  43367. + * @param len Length of the byte array.
  43368. + * @param result Byte array to receive the 8-byte encrypted MIC.
  43369. + */
  43370. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  43371. + char *label, u8 *bytes, int len, u8 *result)
  43372. +{
  43373. + u8 block_m[16];
  43374. + u8 block_x[16];
  43375. + u8 block_t[8];
  43376. + int idx, blkNum;
  43377. + u16 la = (u16)(len + 14);
  43378. +
  43379. + /* Set the AES-128 key */
  43380. + //dwc_aes_setkey(tfm, key, 16);
  43381. +
  43382. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  43383. + block_m[0] = 0x59;
  43384. + for (idx = 0; idx < 13; idx++)
  43385. + block_m[idx + 1] = nonce[idx];
  43386. + block_m[14] = 0;
  43387. + block_m[15] = 0;
  43388. +
  43389. + /* Produce the CBC IV */
  43390. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  43391. + show_block(block_m, "CBC IV in: ", "\n", 0);
  43392. + show_block(block_x, "CBC IV out:", "\n", 0);
  43393. +
  43394. + /* Fill block B1 from l(a) = Blen + 14, and A */
  43395. + block_x[0] ^= (u8)(la >> 8);
  43396. + block_x[1] ^= (u8)la;
  43397. + for (idx = 0; idx < 14; idx++)
  43398. + block_x[idx + 2] ^= label[idx];
  43399. + show_block(block_x, "After xor: ", "b1\n", 16);
  43400. +
  43401. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  43402. + show_block(block_x, "After AES: ", "b1\n", 16);
  43403. +
  43404. + idx = 0;
  43405. + blkNum = 0;
  43406. +
  43407. + /* Fill remaining blocks with B */
  43408. + while (len-- > 0) {
  43409. + block_x[idx] ^= *bytes++;
  43410. + if (++idx >= 16) {
  43411. + idx = 0;
  43412. + show_block(block_x, "After xor: ", "\n", blkNum);
  43413. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  43414. + show_block(block_x, "After AES: ", "\n", blkNum);
  43415. + blkNum++;
  43416. + }
  43417. + }
  43418. +
  43419. + /* Handle partial last block */
  43420. + if (idx > 0) {
  43421. + show_block(block_x, "After xor: ", "\n", blkNum);
  43422. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  43423. + show_block(block_x, "After AES: ", "\n", blkNum);
  43424. + }
  43425. +
  43426. + /* Save the MIC tag */
  43427. + DWC_MEMCPY(block_t, block_x, 8);
  43428. + show_block(block_t, "MIC tag : ", NULL, 8);
  43429. +
  43430. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  43431. + block_m[0] = 0x01;
  43432. + block_m[14] = 0;
  43433. + block_m[15] = 0;
  43434. +
  43435. + /* Encrypt the counter */
  43436. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  43437. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  43438. +
  43439. + /* XOR with MIC tag */
  43440. + for (idx = 0; idx < 8; idx++) {
  43441. + block_t[idx] ^= block_x[idx];
  43442. + }
  43443. +
  43444. + /* Return result to caller */
  43445. + DWC_MEMCPY(result, block_t, 8);
  43446. + show_block(result, "CCM-MIC : ", NULL, 8);
  43447. +
  43448. +}
  43449. +
  43450. +/**
  43451. + * The PRF function described in section 6.5 of the WUSB spec. This function
  43452. + * concatenates MIC values returned from dwc_cmf() to create a value of
  43453. + * the requested length.
  43454. + *
  43455. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  43456. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  43457. + * @param result Byte array to receive the result.
  43458. + */
  43459. +void dwc_wusb_prf(int prf_len, u8 *key,
  43460. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  43461. +{
  43462. + int i;
  43463. +
  43464. + nonce[0] = 0;
  43465. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  43466. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  43467. + result += 8;
  43468. + }
  43469. +}
  43470. +
  43471. +/**
  43472. + * Fills in CCM Nonce per the WUSB spec.
  43473. + *
  43474. + * @param[in] haddr Host address.
  43475. + * @param[in] daddr Device address.
  43476. + * @param[in] tkid Session Key(PTK) identifier.
  43477. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  43478. + */
  43479. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  43480. + uint8_t *nonce)
  43481. +{
  43482. +
  43483. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  43484. +
  43485. + DWC_MEMSET(&nonce[0], 0, 16);
  43486. +
  43487. + DWC_MEMCPY(&nonce[6], tkid, 3);
  43488. + nonce[9] = daddr & 0xFF;
  43489. + nonce[10] = (daddr >> 8) & 0xFF;
  43490. + nonce[11] = haddr & 0xFF;
  43491. + nonce[12] = (haddr >> 8) & 0xFF;
  43492. +
  43493. + dump_bytes("CCM nonce", nonce, 16);
  43494. +}
  43495. +
  43496. +/**
  43497. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  43498. + * Nonce.
  43499. + */
  43500. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  43501. +{
  43502. + uint8_t inonce[16];
  43503. + uint32_t temp[4];
  43504. +
  43505. + /* Fill in the Nonce */
  43506. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  43507. + inonce[9] = addr & 0xFF;
  43508. + inonce[10] = (addr >> 8) & 0xFF;
  43509. + inonce[11] = inonce[9];
  43510. + inonce[12] = inonce[10];
  43511. +
  43512. + /* Collect "randomness samples" */
  43513. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  43514. +
  43515. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  43516. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  43517. + nonce);
  43518. +}
  43519. +
  43520. +/**
  43521. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  43522. + * WUSB spec.
  43523. + *
  43524. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  43525. + * @param[in] mk Master Key to derive the session from
  43526. + * @param[in] hnonce Pointer to Host Nonce.
  43527. + * @param[in] dnonce Pointer to Device Nonce.
  43528. + * @param[out] kck Pointer to where the KCK output is to be written.
  43529. + * @param[out] ptk Pointer to where the PTK output is to be written.
  43530. + */
  43531. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  43532. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  43533. +{
  43534. + uint8_t idata[32];
  43535. + uint8_t odata[32];
  43536. +
  43537. + dump_bytes("ck", mk, 16);
  43538. + dump_bytes("hnonce", hnonce, 16);
  43539. + dump_bytes("dnonce", dnonce, 16);
  43540. +
  43541. + /* The data is the HNonce and DNonce concatenated */
  43542. + DWC_MEMCPY(&idata[0], hnonce, 16);
  43543. + DWC_MEMCPY(&idata[16], dnonce, 16);
  43544. +
  43545. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  43546. +
  43547. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  43548. + DWC_MEMCPY(kck, &odata[0], 16);
  43549. + DWC_MEMCPY(ptk, &odata[16], 16);
  43550. +
  43551. + dump_bytes("kck", kck, 16);
  43552. + dump_bytes("ptk", ptk, 16);
  43553. +}
  43554. +
  43555. +/**
  43556. + * Generates the Message Integrity Code over the Handshake data per the
  43557. + * WUSB spec.
  43558. + *
  43559. + * @param ccm_nonce Pointer to CCM Nonce.
  43560. + * @param kck Pointer to Key Confirmation Key.
  43561. + * @param data Pointer to Handshake data to be checked.
  43562. + * @param mic Pointer to where the MIC output is to be written.
  43563. + */
  43564. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  43565. + uint8_t *data, uint8_t *mic)
  43566. +{
  43567. +
  43568. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  43569. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  43570. +}
  43571. +
  43572. +#endif /* DWC_CRYPTOLIB */
  43573. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_crypto.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_crypto.h
  43574. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_crypto.h 1970-01-01 01:00:00.000000000 +0100
  43575. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_crypto.h 2014-03-11 16:53:12.000000000 +0100
  43576. @@ -0,0 +1,111 @@
  43577. +/* =========================================================================
  43578. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  43579. + * $Revision: #3 $
  43580. + * $Date: 2010/09/28 $
  43581. + * $Change: 1596182 $
  43582. + *
  43583. + * Synopsys Portability Library Software and documentation
  43584. + * (hereinafter, "Software") is an Unsupported proprietary work of
  43585. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  43586. + * between Synopsys and you.
  43587. + *
  43588. + * The Software IS NOT an item of Licensed Software or Licensed Product
  43589. + * under any End User Software License Agreement or Agreement for
  43590. + * Licensed Product with Synopsys or any supplement thereto. You are
  43591. + * permitted to use and redistribute this Software in source and binary
  43592. + * forms, with or without modification, provided that redistributions
  43593. + * of source code must retain this notice. You may not view, use,
  43594. + * disclose, copy or distribute this file or any information contained
  43595. + * herein except pursuant to this license grant from Synopsys. If you
  43596. + * do not agree with this notice, including the disclaimer below, then
  43597. + * you are not authorized to use the Software.
  43598. + *
  43599. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  43600. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  43601. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  43602. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  43603. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43604. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  43605. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  43606. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  43607. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43608. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  43609. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  43610. + * DAMAGE.
  43611. + * ========================================================================= */
  43612. +
  43613. +#ifndef _DWC_CRYPTO_H_
  43614. +#define _DWC_CRYPTO_H_
  43615. +
  43616. +#ifdef __cplusplus
  43617. +extern "C" {
  43618. +#endif
  43619. +
  43620. +/** @file
  43621. + *
  43622. + * This file contains declarations for the WUSB Cryptographic routines as
  43623. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  43624. + * modules.
  43625. + */
  43626. +
  43627. +#include "dwc_os.h"
  43628. +
  43629. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  43630. +
  43631. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  43632. + char *label, u8 *bytes, int len, u8 *result);
  43633. +void dwc_wusb_prf(int prf_len, u8 *key,
  43634. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  43635. +
  43636. +/**
  43637. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  43638. + *
  43639. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  43640. + */
  43641. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  43642. + char *label, u8 *bytes, int len, u8 *result)
  43643. +{
  43644. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  43645. +}
  43646. +
  43647. +/**
  43648. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  43649. + *
  43650. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  43651. + */
  43652. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  43653. + char *label, u8 *bytes, int len, u8 *result)
  43654. +{
  43655. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  43656. +}
  43657. +
  43658. +/**
  43659. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  43660. + *
  43661. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  43662. + */
  43663. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  43664. + char *label, u8 *bytes, int len, u8 *result)
  43665. +{
  43666. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  43667. +}
  43668. +
  43669. +
  43670. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  43671. + uint8_t *nonce);
  43672. +void dwc_wusb_gen_nonce(uint16_t addr,
  43673. + uint8_t *nonce);
  43674. +
  43675. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  43676. + uint8_t *hnonce, uint8_t *dnonce,
  43677. + uint8_t *kck, uint8_t *ptk);
  43678. +
  43679. +
  43680. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  43681. + *kck, uint8_t *data, uint8_t *mic);
  43682. +
  43683. +#ifdef __cplusplus
  43684. +}
  43685. +#endif
  43686. +
  43687. +#endif /* _DWC_CRYPTO_H_ */
  43688. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_dh.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_dh.c
  43689. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_dh.c 1970-01-01 01:00:00.000000000 +0100
  43690. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_dh.c 2014-03-11 16:55:38.000000000 +0100
  43691. @@ -0,0 +1,291 @@
  43692. +/* =========================================================================
  43693. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  43694. + * $Revision: #3 $
  43695. + * $Date: 2010/09/28 $
  43696. + * $Change: 1596182 $
  43697. + *
  43698. + * Synopsys Portability Library Software and documentation
  43699. + * (hereinafter, "Software") is an Unsupported proprietary work of
  43700. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  43701. + * between Synopsys and you.
  43702. + *
  43703. + * The Software IS NOT an item of Licensed Software or Licensed Product
  43704. + * under any End User Software License Agreement or Agreement for
  43705. + * Licensed Product with Synopsys or any supplement thereto. You are
  43706. + * permitted to use and redistribute this Software in source and binary
  43707. + * forms, with or without modification, provided that redistributions
  43708. + * of source code must retain this notice. You may not view, use,
  43709. + * disclose, copy or distribute this file or any information contained
  43710. + * herein except pursuant to this license grant from Synopsys. If you
  43711. + * do not agree with this notice, including the disclaimer below, then
  43712. + * you are not authorized to use the Software.
  43713. + *
  43714. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  43715. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  43716. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  43717. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  43718. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43719. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  43720. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  43721. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  43722. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43723. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  43724. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  43725. + * DAMAGE.
  43726. + * ========================================================================= */
  43727. +#ifdef DWC_CRYPTOLIB
  43728. +
  43729. +#ifndef CONFIG_MACH_IPMATE
  43730. +
  43731. +#include "dwc_dh.h"
  43732. +#include "dwc_modpow.h"
  43733. +
  43734. +#ifdef DEBUG
  43735. +/* This function prints out a buffer in the format described in the Association
  43736. + * Model specification. */
  43737. +static void dh_dump(char *str, void *_num, int len)
  43738. +{
  43739. + uint8_t *num = _num;
  43740. + int i;
  43741. + DWC_PRINTF("%s\n", str);
  43742. + for (i = 0; i < len; i ++) {
  43743. + DWC_PRINTF("%02x", num[i]);
  43744. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  43745. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  43746. + }
  43747. +
  43748. + DWC_PRINTF("\n");
  43749. +}
  43750. +#else
  43751. +#define dh_dump(_x...) do {; } while(0)
  43752. +#endif
  43753. +
  43754. +/* Constant g value */
  43755. +static __u32 dh_g[] = {
  43756. + 0x02000000,
  43757. +};
  43758. +
  43759. +/* Constant p value */
  43760. +static __u32 dh_p[] = {
  43761. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  43762. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  43763. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  43764. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  43765. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  43766. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  43767. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  43768. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  43769. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  43770. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  43771. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  43772. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  43773. +};
  43774. +
  43775. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  43776. +{
  43777. + uint8_t *in = _in;
  43778. + uint8_t *out = _out;
  43779. + int i;
  43780. + for (i=0; i<len; i++) {
  43781. + out[i] = in[len-1-i];
  43782. + }
  43783. +}
  43784. +
  43785. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  43786. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  43787. + * of 4. */
  43788. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  43789. + void *exp, uint32_t exp_len,
  43790. + void *mod, uint32_t mod_len,
  43791. + void *out)
  43792. +{
  43793. + /* modpow() takes little endian numbers. AM uses big-endian. This
  43794. + * function swaps bytes of numbers before passing onto modpow. */
  43795. +
  43796. + int retval = 0;
  43797. + uint32_t *result;
  43798. +
  43799. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  43800. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  43801. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  43802. +
  43803. + dh_swap_bytes(num, &bignum_num[1], num_len);
  43804. + bignum_num[0] = num_len / 4;
  43805. +
  43806. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  43807. + bignum_exp[0] = exp_len / 4;
  43808. +
  43809. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  43810. + bignum_mod[0] = mod_len / 4;
  43811. +
  43812. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  43813. + if (!result) {
  43814. + retval = -1;
  43815. + goto dh_modpow_nomem;
  43816. + }
  43817. +
  43818. + dh_swap_bytes(&result[1], out, result[0] * 4);
  43819. + dwc_free(mem_ctx, result);
  43820. +
  43821. + dh_modpow_nomem:
  43822. + dwc_free(mem_ctx, bignum_num);
  43823. + dwc_free(mem_ctx, bignum_exp);
  43824. + dwc_free(mem_ctx, bignum_mod);
  43825. + return retval;
  43826. +}
  43827. +
  43828. +
  43829. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  43830. +{
  43831. + int retval;
  43832. + uint8_t m3[385];
  43833. +
  43834. +#ifndef DH_TEST_VECTORS
  43835. + DWC_RANDOM_BYTES(exp, 32);
  43836. +#endif
  43837. +
  43838. + /* Compute the pkd */
  43839. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  43840. + exp, 32,
  43841. + dh_p, 384, pk))) {
  43842. + return retval;
  43843. + }
  43844. +
  43845. + m3[384] = nd;
  43846. + DWC_MEMCPY(&m3[0], pk, 384);
  43847. + DWC_SHA256(m3, 385, hash);
  43848. +
  43849. + dh_dump("PK", pk, 384);
  43850. + dh_dump("SHA-256(M3)", hash, 32);
  43851. + return 0;
  43852. +}
  43853. +
  43854. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  43855. + uint8_t *exp, int is_host,
  43856. + char *dd, uint8_t *ck, uint8_t *kdk)
  43857. +{
  43858. + int retval;
  43859. + uint8_t mv[784];
  43860. + uint8_t sha_result[32];
  43861. + uint8_t dhkey[384];
  43862. + uint8_t shared_secret[384];
  43863. + char *message;
  43864. + uint32_t vd;
  43865. +
  43866. + uint8_t *pk;
  43867. +
  43868. + if (is_host) {
  43869. + pk = pkd;
  43870. + }
  43871. + else {
  43872. + pk = pkh;
  43873. + }
  43874. +
  43875. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  43876. + exp, 32,
  43877. + dh_p, 384, shared_secret))) {
  43878. + return retval;
  43879. + }
  43880. + dh_dump("Shared Secret", shared_secret, 384);
  43881. +
  43882. + DWC_SHA256(shared_secret, 384, dhkey);
  43883. + dh_dump("DHKEY", dhkey, 384);
  43884. +
  43885. + DWC_MEMCPY(&mv[0], pkd, 384);
  43886. + DWC_MEMCPY(&mv[384], pkh, 384);
  43887. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  43888. + dh_dump("MV", mv, 784);
  43889. +
  43890. + DWC_SHA256(mv, 784, sha_result);
  43891. + dh_dump("SHA-256(MV)", sha_result, 32);
  43892. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  43893. +
  43894. + dh_swap_bytes(sha_result, &vd, 4);
  43895. +#ifdef DEBUG
  43896. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  43897. +#endif
  43898. +
  43899. + switch (nd) {
  43900. + case 2:
  43901. + vd = vd % 100;
  43902. + DWC_SPRINTF(dd, "%02d", vd);
  43903. + break;
  43904. + case 3:
  43905. + vd = vd % 1000;
  43906. + DWC_SPRINTF(dd, "%03d", vd);
  43907. + break;
  43908. + case 4:
  43909. + vd = vd % 10000;
  43910. + DWC_SPRINTF(dd, "%04d", vd);
  43911. + break;
  43912. + }
  43913. +#ifdef DEBUG
  43914. + DWC_PRINTF("Display Digits: %s\n", dd);
  43915. +#endif
  43916. +
  43917. + message = "connection key";
  43918. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  43919. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  43920. + DWC_MEMCPY(ck, sha_result, 16);
  43921. +
  43922. + message = "key derivation key";
  43923. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  43924. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  43925. + DWC_MEMCPY(kdk, sha_result, 32);
  43926. +
  43927. + return 0;
  43928. +}
  43929. +
  43930. +
  43931. +#ifdef DH_TEST_VECTORS
  43932. +
  43933. +static __u8 dh_a[] = {
  43934. + 0x44, 0x00, 0x51, 0xd6,
  43935. + 0xf0, 0xb5, 0x5e, 0xa9,
  43936. + 0x67, 0xab, 0x31, 0xc6,
  43937. + 0x8a, 0x8b, 0x5e, 0x37,
  43938. + 0xd9, 0x10, 0xda, 0xe0,
  43939. + 0xe2, 0xd4, 0x59, 0xa4,
  43940. + 0x86, 0x45, 0x9c, 0xaa,
  43941. + 0xdf, 0x36, 0x75, 0x16,
  43942. +};
  43943. +
  43944. +static __u8 dh_b[] = {
  43945. + 0x5d, 0xae, 0xc7, 0x86,
  43946. + 0x79, 0x80, 0xa3, 0x24,
  43947. + 0x8c, 0xe3, 0x57, 0x8f,
  43948. + 0xc7, 0x5f, 0x1b, 0x0f,
  43949. + 0x2d, 0xf8, 0x9d, 0x30,
  43950. + 0x6f, 0xa4, 0x52, 0xcd,
  43951. + 0xe0, 0x7a, 0x04, 0x8a,
  43952. + 0xde, 0xd9, 0x26, 0x56,
  43953. +};
  43954. +
  43955. +void dwc_run_dh_test_vectors(void *mem_ctx)
  43956. +{
  43957. + uint8_t pkd[384];
  43958. + uint8_t pkh[384];
  43959. + uint8_t hashd[32];
  43960. + uint8_t hashh[32];
  43961. + uint8_t ck[16];
  43962. + uint8_t kdk[32];
  43963. + char dd[5];
  43964. +
  43965. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  43966. +
  43967. + /* compute the PKd and SHA-256(PKd || Nd) */
  43968. + DWC_PRINTF("Computing PKd\n");
  43969. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  43970. +
  43971. + /* compute the PKd and SHA-256(PKh || Nd) */
  43972. + DWC_PRINTF("Computing PKh\n");
  43973. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  43974. +
  43975. + /* compute the dhkey */
  43976. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  43977. +}
  43978. +#endif /* DH_TEST_VECTORS */
  43979. +
  43980. +#endif /* !CONFIG_MACH_IPMATE */
  43981. +
  43982. +#endif /* DWC_CRYPTOLIB */
  43983. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_dh.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_dh.h
  43984. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_dh.h 1970-01-01 01:00:00.000000000 +0100
  43985. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_dh.h 2014-03-11 16:53:12.000000000 +0100
  43986. @@ -0,0 +1,106 @@
  43987. +/* =========================================================================
  43988. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  43989. + * $Revision: #4 $
  43990. + * $Date: 2010/09/28 $
  43991. + * $Change: 1596182 $
  43992. + *
  43993. + * Synopsys Portability Library Software and documentation
  43994. + * (hereinafter, "Software") is an Unsupported proprietary work of
  43995. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  43996. + * between Synopsys and you.
  43997. + *
  43998. + * The Software IS NOT an item of Licensed Software or Licensed Product
  43999. + * under any End User Software License Agreement or Agreement for
  44000. + * Licensed Product with Synopsys or any supplement thereto. You are
  44001. + * permitted to use and redistribute this Software in source and binary
  44002. + * forms, with or without modification, provided that redistributions
  44003. + * of source code must retain this notice. You may not view, use,
  44004. + * disclose, copy or distribute this file or any information contained
  44005. + * herein except pursuant to this license grant from Synopsys. If you
  44006. + * do not agree with this notice, including the disclaimer below, then
  44007. + * you are not authorized to use the Software.
  44008. + *
  44009. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  44010. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44011. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44012. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  44013. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  44014. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44015. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  44016. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  44017. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44018. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  44019. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  44020. + * DAMAGE.
  44021. + * ========================================================================= */
  44022. +#ifndef _DWC_DH_H_
  44023. +#define _DWC_DH_H_
  44024. +
  44025. +#ifdef __cplusplus
  44026. +extern "C" {
  44027. +#endif
  44028. +
  44029. +#include "dwc_os.h"
  44030. +
  44031. +/** @file
  44032. + *
  44033. + * This file defines the common functions on device and host for performing
  44034. + * numeric association as defined in the WUSB spec. They are only to be
  44035. + * used internally by the DWC UWB modules. */
  44036. +
  44037. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  44038. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  44039. + uint8_t *key, uint32_t keylen,
  44040. + uint8_t *out);
  44041. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  44042. + void *exp, uint32_t exp_len,
  44043. + void *mod, uint32_t mod_len,
  44044. + void *out);
  44045. +
  44046. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  44047. + *
  44048. + * PK = g^exp mod p.
  44049. + *
  44050. + * Input:
  44051. + * Nd = Number of digits on the device.
  44052. + *
  44053. + * Output:
  44054. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  44055. + * used as either A or B.
  44056. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  44057. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  44058. + */
  44059. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  44060. +
  44061. +/** Computes the DHKEY, and VD.
  44062. + *
  44063. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  44064. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  44065. + *
  44066. + * Input:
  44067. + * pkd = The PKD value.
  44068. + * pkh = The PKH value.
  44069. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  44070. + * is_host = Set to non zero if a WUSB host is calling this function.
  44071. + *
  44072. + * Output:
  44073. +
  44074. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  44075. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  44076. + * null termination character. This buffer can be used directly for display.
  44077. + * ck = A 16-byte buffer to be filled with the CK.
  44078. + * kdk = A 32-byte buffer to be filled with the KDK.
  44079. + */
  44080. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  44081. + uint8_t *exp, int is_host,
  44082. + char *dd, uint8_t *ck, uint8_t *kdk);
  44083. +
  44084. +#ifdef DH_TEST_VECTORS
  44085. +extern void dwc_run_dh_test_vectors(void);
  44086. +#endif
  44087. +
  44088. +#ifdef __cplusplus
  44089. +}
  44090. +#endif
  44091. +
  44092. +#endif /* _DWC_DH_H_ */
  44093. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_list.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_list.h
  44094. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_list.h 1970-01-01 01:00:00.000000000 +0100
  44095. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_list.h 2014-03-11 16:53:12.000000000 +0100
  44096. @@ -0,0 +1,594 @@
  44097. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  44098. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  44099. +
  44100. +/*
  44101. + * Copyright (c) 1991, 1993
  44102. + * The Regents of the University of California. All rights reserved.
  44103. + *
  44104. + * Redistribution and use in source and binary forms, with or without
  44105. + * modification, are permitted provided that the following conditions
  44106. + * are met:
  44107. + * 1. Redistributions of source code must retain the above copyright
  44108. + * notice, this list of conditions and the following disclaimer.
  44109. + * 2. Redistributions in binary form must reproduce the above copyright
  44110. + * notice, this list of conditions and the following disclaimer in the
  44111. + * documentation and/or other materials provided with the distribution.
  44112. + * 3. Neither the name of the University nor the names of its contributors
  44113. + * may be used to endorse or promote products derived from this software
  44114. + * without specific prior written permission.
  44115. + *
  44116. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  44117. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44118. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44119. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  44120. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44121. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44122. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44123. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44124. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  44125. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  44126. + * SUCH DAMAGE.
  44127. + *
  44128. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  44129. + */
  44130. +
  44131. +#ifndef _DWC_LIST_H_
  44132. +#define _DWC_LIST_H_
  44133. +
  44134. +#ifdef __cplusplus
  44135. +extern "C" {
  44136. +#endif
  44137. +
  44138. +/** @file
  44139. + *
  44140. + * This file defines linked list operations. It is derived from BSD with
  44141. + * only the MACRO names being prefixed with DWC_. This is because a few of
  44142. + * these names conflict with those on Linux. For documentation on use, see the
  44143. + * inline comments in the source code. The original license for this source
  44144. + * code applies and is preserved in the dwc_list.h source file.
  44145. + */
  44146. +
  44147. +/*
  44148. + * This file defines five types of data structures: singly-linked lists,
  44149. + * lists, simple queues, tail queues, and circular queues.
  44150. + *
  44151. + *
  44152. + * A singly-linked list is headed by a single forward pointer. The elements
  44153. + * are singly linked for minimum space and pointer manipulation overhead at
  44154. + * the expense of O(n) removal for arbitrary elements. New elements can be
  44155. + * added to the list after an existing element or at the head of the list.
  44156. + * Elements being removed from the head of the list should use the explicit
  44157. + * macro for this purpose for optimum efficiency. A singly-linked list may
  44158. + * only be traversed in the forward direction. Singly-linked lists are ideal
  44159. + * for applications with large datasets and few or no removals or for
  44160. + * implementing a LIFO queue.
  44161. + *
  44162. + * A list is headed by a single forward pointer (or an array of forward
  44163. + * pointers for a hash table header). The elements are doubly linked
  44164. + * so that an arbitrary element can be removed without a need to
  44165. + * traverse the list. New elements can be added to the list before
  44166. + * or after an existing element or at the head of the list. A list
  44167. + * may only be traversed in the forward direction.
  44168. + *
  44169. + * A simple queue is headed by a pair of pointers, one the head of the
  44170. + * list and the other to the tail of the list. The elements are singly
  44171. + * linked to save space, so elements can only be removed from the
  44172. + * head of the list. New elements can be added to the list before or after
  44173. + * an existing element, at the head of the list, or at the end of the
  44174. + * list. A simple queue may only be traversed in the forward direction.
  44175. + *
  44176. + * A tail queue is headed by a pair of pointers, one to the head of the
  44177. + * list and the other to the tail of the list. The elements are doubly
  44178. + * linked so that an arbitrary element can be removed without a need to
  44179. + * traverse the list. New elements can be added to the list before or
  44180. + * after an existing element, at the head of the list, or at the end of
  44181. + * the list. A tail queue may be traversed in either direction.
  44182. + *
  44183. + * A circle queue is headed by a pair of pointers, one to the head of the
  44184. + * list and the other to the tail of the list. The elements are doubly
  44185. + * linked so that an arbitrary element can be removed without a need to
  44186. + * traverse the list. New elements can be added to the list before or after
  44187. + * an existing element, at the head of the list, or at the end of the list.
  44188. + * A circle queue may be traversed in either direction, but has a more
  44189. + * complex end of list detection.
  44190. + *
  44191. + * For details on the use of these macros, see the queue(3) manual page.
  44192. + */
  44193. +
  44194. +/*
  44195. + * Double-linked List.
  44196. + */
  44197. +
  44198. +typedef struct dwc_list_link {
  44199. + struct dwc_list_link *next;
  44200. + struct dwc_list_link *prev;
  44201. +} dwc_list_link_t;
  44202. +
  44203. +#define DWC_LIST_INIT(link) do { \
  44204. + (link)->next = (link); \
  44205. + (link)->prev = (link); \
  44206. +} while (0)
  44207. +
  44208. +#define DWC_LIST_FIRST(link) ((link)->next)
  44209. +#define DWC_LIST_LAST(link) ((link)->prev)
  44210. +#define DWC_LIST_END(link) (link)
  44211. +#define DWC_LIST_NEXT(link) ((link)->next)
  44212. +#define DWC_LIST_PREV(link) ((link)->prev)
  44213. +#define DWC_LIST_EMPTY(link) \
  44214. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  44215. +#define DWC_LIST_ENTRY(link, type, field) \
  44216. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  44217. +
  44218. +#if 0
  44219. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  44220. + (link)->next = (list)->next; \
  44221. + (link)->prev = (list); \
  44222. + (list)->next->prev = (link); \
  44223. + (list)->next = (link); \
  44224. +} while (0)
  44225. +
  44226. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  44227. + (link)->next = (list); \
  44228. + (link)->prev = (list)->prev; \
  44229. + (list)->prev->next = (link); \
  44230. + (list)->prev = (link); \
  44231. +} while (0)
  44232. +#else
  44233. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  44234. + dwc_list_link_t *__next__ = (list)->next; \
  44235. + __next__->prev = (link); \
  44236. + (link)->next = __next__; \
  44237. + (link)->prev = (list); \
  44238. + (list)->next = (link); \
  44239. +} while (0)
  44240. +
  44241. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  44242. + dwc_list_link_t *__prev__ = (list)->prev; \
  44243. + (list)->prev = (link); \
  44244. + (link)->next = (list); \
  44245. + (link)->prev = __prev__; \
  44246. + __prev__->next = (link); \
  44247. +} while (0)
  44248. +#endif
  44249. +
  44250. +#if 0
  44251. +static inline void __list_add(struct list_head *new,
  44252. + struct list_head *prev,
  44253. + struct list_head *next)
  44254. +{
  44255. + next->prev = new;
  44256. + new->next = next;
  44257. + new->prev = prev;
  44258. + prev->next = new;
  44259. +}
  44260. +
  44261. +static inline void list_add(struct list_head *new, struct list_head *head)
  44262. +{
  44263. + __list_add(new, head, head->next);
  44264. +}
  44265. +
  44266. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  44267. +{
  44268. + __list_add(new, head->prev, head);
  44269. +}
  44270. +
  44271. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  44272. +{
  44273. + next->prev = prev;
  44274. + prev->next = next;
  44275. +}
  44276. +
  44277. +static inline void list_del(struct list_head *entry)
  44278. +{
  44279. + __list_del(entry->prev, entry->next);
  44280. + entry->next = LIST_POISON1;
  44281. + entry->prev = LIST_POISON2;
  44282. +}
  44283. +#endif
  44284. +
  44285. +#define DWC_LIST_REMOVE(link) do { \
  44286. + (link)->next->prev = (link)->prev; \
  44287. + (link)->prev->next = (link)->next; \
  44288. +} while (0)
  44289. +
  44290. +#define DWC_LIST_REMOVE_INIT(link) do { \
  44291. + DWC_LIST_REMOVE(link); \
  44292. + DWC_LIST_INIT(link); \
  44293. +} while (0)
  44294. +
  44295. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  44296. + DWC_LIST_REMOVE(link); \
  44297. + DWC_LIST_INSERT_HEAD(list, link); \
  44298. +} while (0)
  44299. +
  44300. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  44301. + DWC_LIST_REMOVE(link); \
  44302. + DWC_LIST_INSERT_TAIL(list, link); \
  44303. +} while (0)
  44304. +
  44305. +#define DWC_LIST_FOREACH(var, list) \
  44306. + for((var) = DWC_LIST_FIRST(list); \
  44307. + (var) != DWC_LIST_END(list); \
  44308. + (var) = DWC_LIST_NEXT(var))
  44309. +
  44310. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  44311. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  44312. + (var) != DWC_LIST_END(list); \
  44313. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  44314. +
  44315. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  44316. + for((var) = DWC_LIST_LAST(list); \
  44317. + (var) != DWC_LIST_END(list); \
  44318. + (var) = DWC_LIST_PREV(var))
  44319. +
  44320. +/*
  44321. + * Singly-linked List definitions.
  44322. + */
  44323. +#define DWC_SLIST_HEAD(name, type) \
  44324. +struct name { \
  44325. + struct type *slh_first; /* first element */ \
  44326. +}
  44327. +
  44328. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  44329. + { NULL }
  44330. +
  44331. +#define DWC_SLIST_ENTRY(type) \
  44332. +struct { \
  44333. + struct type *sle_next; /* next element */ \
  44334. +}
  44335. +
  44336. +/*
  44337. + * Singly-linked List access methods.
  44338. + */
  44339. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  44340. +#define DWC_SLIST_END(head) NULL
  44341. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  44342. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  44343. +
  44344. +#define DWC_SLIST_FOREACH(var, head, field) \
  44345. + for((var) = SLIST_FIRST(head); \
  44346. + (var) != SLIST_END(head); \
  44347. + (var) = SLIST_NEXT(var, field))
  44348. +
  44349. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  44350. + for((varp) = &SLIST_FIRST((head)); \
  44351. + ((var) = *(varp)) != SLIST_END(head); \
  44352. + (varp) = &SLIST_NEXT((var), field))
  44353. +
  44354. +/*
  44355. + * Singly-linked List functions.
  44356. + */
  44357. +#define DWC_SLIST_INIT(head) { \
  44358. + SLIST_FIRST(head) = SLIST_END(head); \
  44359. +}
  44360. +
  44361. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  44362. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  44363. + (slistelm)->field.sle_next = (elm); \
  44364. +} while (0)
  44365. +
  44366. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  44367. + (elm)->field.sle_next = (head)->slh_first; \
  44368. + (head)->slh_first = (elm); \
  44369. +} while (0)
  44370. +
  44371. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  44372. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  44373. +} while (0)
  44374. +
  44375. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  44376. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  44377. +} while (0)
  44378. +
  44379. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  44380. + if ((head)->slh_first == (elm)) { \
  44381. + SLIST_REMOVE_HEAD((head), field); \
  44382. + } \
  44383. + else { \
  44384. + struct type *curelm = (head)->slh_first; \
  44385. + while( curelm->field.sle_next != (elm) ) \
  44386. + curelm = curelm->field.sle_next; \
  44387. + curelm->field.sle_next = \
  44388. + curelm->field.sle_next->field.sle_next; \
  44389. + } \
  44390. +} while (0)
  44391. +
  44392. +/*
  44393. + * Simple queue definitions.
  44394. + */
  44395. +#define DWC_SIMPLEQ_HEAD(name, type) \
  44396. +struct name { \
  44397. + struct type *sqh_first; /* first element */ \
  44398. + struct type **sqh_last; /* addr of last next element */ \
  44399. +}
  44400. +
  44401. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  44402. + { NULL, &(head).sqh_first }
  44403. +
  44404. +#define DWC_SIMPLEQ_ENTRY(type) \
  44405. +struct { \
  44406. + struct type *sqe_next; /* next element */ \
  44407. +}
  44408. +
  44409. +/*
  44410. + * Simple queue access methods.
  44411. + */
  44412. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  44413. +#define DWC_SIMPLEQ_END(head) NULL
  44414. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  44415. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  44416. +
  44417. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  44418. + for((var) = SIMPLEQ_FIRST(head); \
  44419. + (var) != SIMPLEQ_END(head); \
  44420. + (var) = SIMPLEQ_NEXT(var, field))
  44421. +
  44422. +/*
  44423. + * Simple queue functions.
  44424. + */
  44425. +#define DWC_SIMPLEQ_INIT(head) do { \
  44426. + (head)->sqh_first = NULL; \
  44427. + (head)->sqh_last = &(head)->sqh_first; \
  44428. +} while (0)
  44429. +
  44430. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  44431. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  44432. + (head)->sqh_last = &(elm)->field.sqe_next; \
  44433. + (head)->sqh_first = (elm); \
  44434. +} while (0)
  44435. +
  44436. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  44437. + (elm)->field.sqe_next = NULL; \
  44438. + *(head)->sqh_last = (elm); \
  44439. + (head)->sqh_last = &(elm)->field.sqe_next; \
  44440. +} while (0)
  44441. +
  44442. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  44443. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  44444. + (head)->sqh_last = &(elm)->field.sqe_next; \
  44445. + (listelm)->field.sqe_next = (elm); \
  44446. +} while (0)
  44447. +
  44448. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  44449. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  44450. + (head)->sqh_last = &(head)->sqh_first; \
  44451. +} while (0)
  44452. +
  44453. +/*
  44454. + * Tail queue definitions.
  44455. + */
  44456. +#define DWC_TAILQ_HEAD(name, type) \
  44457. +struct name { \
  44458. + struct type *tqh_first; /* first element */ \
  44459. + struct type **tqh_last; /* addr of last next element */ \
  44460. +}
  44461. +
  44462. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  44463. + { NULL, &(head).tqh_first }
  44464. +
  44465. +#define DWC_TAILQ_ENTRY(type) \
  44466. +struct { \
  44467. + struct type *tqe_next; /* next element */ \
  44468. + struct type **tqe_prev; /* address of previous next element */ \
  44469. +}
  44470. +
  44471. +/*
  44472. + * tail queue access methods
  44473. + */
  44474. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  44475. +#define DWC_TAILQ_END(head) NULL
  44476. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  44477. +#define DWC_TAILQ_LAST(head, headname) \
  44478. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  44479. +/* XXX */
  44480. +#define DWC_TAILQ_PREV(elm, headname, field) \
  44481. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  44482. +#define DWC_TAILQ_EMPTY(head) \
  44483. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  44484. +
  44485. +#define DWC_TAILQ_FOREACH(var, head, field) \
  44486. + for ((var) = DWC_TAILQ_FIRST(head); \
  44487. + (var) != DWC_TAILQ_END(head); \
  44488. + (var) = DWC_TAILQ_NEXT(var, field))
  44489. +
  44490. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  44491. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  44492. + (var) != DWC_TAILQ_END(head); \
  44493. + (var) = DWC_TAILQ_PREV(var, headname, field))
  44494. +
  44495. +/*
  44496. + * Tail queue functions.
  44497. + */
  44498. +#define DWC_TAILQ_INIT(head) do { \
  44499. + (head)->tqh_first = NULL; \
  44500. + (head)->tqh_last = &(head)->tqh_first; \
  44501. +} while (0)
  44502. +
  44503. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  44504. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  44505. + (head)->tqh_first->field.tqe_prev = \
  44506. + &(elm)->field.tqe_next; \
  44507. + else \
  44508. + (head)->tqh_last = &(elm)->field.tqe_next; \
  44509. + (head)->tqh_first = (elm); \
  44510. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  44511. +} while (0)
  44512. +
  44513. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  44514. + (elm)->field.tqe_next = NULL; \
  44515. + (elm)->field.tqe_prev = (head)->tqh_last; \
  44516. + *(head)->tqh_last = (elm); \
  44517. + (head)->tqh_last = &(elm)->field.tqe_next; \
  44518. +} while (0)
  44519. +
  44520. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  44521. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  44522. + (elm)->field.tqe_next->field.tqe_prev = \
  44523. + &(elm)->field.tqe_next; \
  44524. + else \
  44525. + (head)->tqh_last = &(elm)->field.tqe_next; \
  44526. + (listelm)->field.tqe_next = (elm); \
  44527. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  44528. +} while (0)
  44529. +
  44530. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  44531. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  44532. + (elm)->field.tqe_next = (listelm); \
  44533. + *(listelm)->field.tqe_prev = (elm); \
  44534. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  44535. +} while (0)
  44536. +
  44537. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  44538. + if (((elm)->field.tqe_next) != NULL) \
  44539. + (elm)->field.tqe_next->field.tqe_prev = \
  44540. + (elm)->field.tqe_prev; \
  44541. + else \
  44542. + (head)->tqh_last = (elm)->field.tqe_prev; \
  44543. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  44544. +} while (0)
  44545. +
  44546. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  44547. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  44548. + (elm2)->field.tqe_next->field.tqe_prev = \
  44549. + &(elm2)->field.tqe_next; \
  44550. + else \
  44551. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  44552. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  44553. + *(elm2)->field.tqe_prev = (elm2); \
  44554. +} while (0)
  44555. +
  44556. +/*
  44557. + * Circular queue definitions.
  44558. + */
  44559. +#define DWC_CIRCLEQ_HEAD(name, type) \
  44560. +struct name { \
  44561. + struct type *cqh_first; /* first element */ \
  44562. + struct type *cqh_last; /* last element */ \
  44563. +}
  44564. +
  44565. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  44566. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  44567. +
  44568. +#define DWC_CIRCLEQ_ENTRY(type) \
  44569. +struct { \
  44570. + struct type *cqe_next; /* next element */ \
  44571. + struct type *cqe_prev; /* previous element */ \
  44572. +}
  44573. +
  44574. +/*
  44575. + * Circular queue access methods
  44576. + */
  44577. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  44578. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  44579. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  44580. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  44581. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  44582. +#define DWC_CIRCLEQ_EMPTY(head) \
  44583. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  44584. +
  44585. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  44586. +
  44587. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  44588. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  44589. + (var) != DWC_CIRCLEQ_END(head); \
  44590. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  44591. +
  44592. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  44593. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  44594. + (var) != DWC_CIRCLEQ_END(head); \
  44595. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  44596. +
  44597. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  44598. + for((var) = DWC_CIRCLEQ_LAST(head); \
  44599. + (var) != DWC_CIRCLEQ_END(head); \
  44600. + (var) = DWC_CIRCLEQ_PREV(var, field))
  44601. +
  44602. +/*
  44603. + * Circular queue functions.
  44604. + */
  44605. +#define DWC_CIRCLEQ_INIT(head) do { \
  44606. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  44607. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  44608. +} while (0)
  44609. +
  44610. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  44611. + (elm)->field.cqe_next = NULL; \
  44612. + (elm)->field.cqe_prev = NULL; \
  44613. +} while (0)
  44614. +
  44615. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  44616. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  44617. + (elm)->field.cqe_prev = (listelm); \
  44618. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  44619. + (head)->cqh_last = (elm); \
  44620. + else \
  44621. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  44622. + (listelm)->field.cqe_next = (elm); \
  44623. +} while (0)
  44624. +
  44625. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  44626. + (elm)->field.cqe_next = (listelm); \
  44627. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  44628. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  44629. + (head)->cqh_first = (elm); \
  44630. + else \
  44631. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  44632. + (listelm)->field.cqe_prev = (elm); \
  44633. +} while (0)
  44634. +
  44635. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  44636. + (elm)->field.cqe_next = (head)->cqh_first; \
  44637. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  44638. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  44639. + (head)->cqh_last = (elm); \
  44640. + else \
  44641. + (head)->cqh_first->field.cqe_prev = (elm); \
  44642. + (head)->cqh_first = (elm); \
  44643. +} while (0)
  44644. +
  44645. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  44646. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  44647. + (elm)->field.cqe_prev = (head)->cqh_last; \
  44648. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  44649. + (head)->cqh_first = (elm); \
  44650. + else \
  44651. + (head)->cqh_last->field.cqe_next = (elm); \
  44652. + (head)->cqh_last = (elm); \
  44653. +} while (0)
  44654. +
  44655. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  44656. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  44657. + (head)->cqh_last = (elm)->field.cqe_prev; \
  44658. + else \
  44659. + (elm)->field.cqe_next->field.cqe_prev = \
  44660. + (elm)->field.cqe_prev; \
  44661. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  44662. + (head)->cqh_first = (elm)->field.cqe_next; \
  44663. + else \
  44664. + (elm)->field.cqe_prev->field.cqe_next = \
  44665. + (elm)->field.cqe_next; \
  44666. +} while (0)
  44667. +
  44668. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  44669. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  44670. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  44671. +} while (0)
  44672. +
  44673. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  44674. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  44675. + DWC_CIRCLEQ_END(head)) \
  44676. + (head).cqh_last = (elm2); \
  44677. + else \
  44678. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  44679. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  44680. + DWC_CIRCLEQ_END(head)) \
  44681. + (head).cqh_first = (elm2); \
  44682. + else \
  44683. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  44684. +} while (0)
  44685. +
  44686. +#ifdef __cplusplus
  44687. +}
  44688. +#endif
  44689. +
  44690. +#endif /* _DWC_LIST_H_ */
  44691. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_mem.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_mem.c
  44692. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_mem.c 1970-01-01 01:00:00.000000000 +0100
  44693. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_mem.c 2014-03-11 16:53:12.000000000 +0100
  44694. @@ -0,0 +1,245 @@
  44695. +/* Memory Debugging */
  44696. +#ifdef DWC_DEBUG_MEMORY
  44697. +
  44698. +#include "dwc_os.h"
  44699. +#include "dwc_list.h"
  44700. +
  44701. +struct allocation {
  44702. + void *addr;
  44703. + void *ctx;
  44704. + char *func;
  44705. + int line;
  44706. + uint32_t size;
  44707. + int dma;
  44708. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  44709. +};
  44710. +
  44711. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  44712. +
  44713. +struct allocation_manager {
  44714. + void *mem_ctx;
  44715. + struct allocation_queue allocations;
  44716. +
  44717. + /* statistics */
  44718. + int num;
  44719. + int num_freed;
  44720. + int num_active;
  44721. + uint32_t total;
  44722. + uint32_t cur;
  44723. + uint32_t max;
  44724. +};
  44725. +
  44726. +static struct allocation_manager *manager = NULL;
  44727. +
  44728. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  44729. + int dma)
  44730. +{
  44731. + struct allocation *a;
  44732. +
  44733. + DWC_ASSERT(manager != NULL, "manager not allocated");
  44734. +
  44735. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  44736. + if (!a) {
  44737. + return -DWC_E_NO_MEMORY;
  44738. + }
  44739. +
  44740. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  44741. + if (!a->func) {
  44742. + __DWC_FREE(manager->mem_ctx, a);
  44743. + return -DWC_E_NO_MEMORY;
  44744. + }
  44745. +
  44746. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  44747. + a->addr = addr;
  44748. + a->ctx = ctx;
  44749. + a->line = line;
  44750. + a->size = size;
  44751. + a->dma = dma;
  44752. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  44753. +
  44754. + /* Update stats */
  44755. + manager->num++;
  44756. + manager->num_active++;
  44757. + manager->total += size;
  44758. + manager->cur += size;
  44759. +
  44760. + if (manager->max < manager->cur) {
  44761. + manager->max = manager->cur;
  44762. + }
  44763. +
  44764. + return 0;
  44765. +}
  44766. +
  44767. +static struct allocation *find_allocation(void *ctx, void *addr)
  44768. +{
  44769. + struct allocation *a;
  44770. +
  44771. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  44772. + if (a->ctx == ctx && a->addr == addr) {
  44773. + return a;
  44774. + }
  44775. + }
  44776. +
  44777. + return NULL;
  44778. +}
  44779. +
  44780. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  44781. +{
  44782. + struct allocation *a = find_allocation(ctx, addr);
  44783. +
  44784. + if (!a) {
  44785. + DWC_ASSERT(0,
  44786. + "Free of address %p that was never allocated or already freed %s:%d",
  44787. + addr, func, line);
  44788. + return;
  44789. + }
  44790. +
  44791. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  44792. +
  44793. + manager->num_active--;
  44794. + manager->num_freed++;
  44795. + manager->cur -= a->size;
  44796. + __DWC_FREE(manager->mem_ctx, a->func);
  44797. + __DWC_FREE(manager->mem_ctx, a);
  44798. +}
  44799. +
  44800. +int dwc_memory_debug_start(void *mem_ctx)
  44801. +{
  44802. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  44803. +
  44804. + if (manager) {
  44805. + return -DWC_E_BUSY;
  44806. + }
  44807. +
  44808. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  44809. + if (!manager) {
  44810. + return -DWC_E_NO_MEMORY;
  44811. + }
  44812. +
  44813. + DWC_CIRCLEQ_INIT(&manager->allocations);
  44814. + manager->mem_ctx = mem_ctx;
  44815. + manager->num = 0;
  44816. + manager->num_freed = 0;
  44817. + manager->num_active = 0;
  44818. + manager->total = 0;
  44819. + manager->cur = 0;
  44820. + manager->max = 0;
  44821. +
  44822. + return 0;
  44823. +}
  44824. +
  44825. +void dwc_memory_debug_stop(void)
  44826. +{
  44827. + struct allocation *a;
  44828. +
  44829. + dwc_memory_debug_report();
  44830. +
  44831. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  44832. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  44833. + free_allocation(a->ctx, a->addr, NULL, -1);
  44834. + }
  44835. +
  44836. + __DWC_FREE(manager->mem_ctx, manager);
  44837. +}
  44838. +
  44839. +void dwc_memory_debug_report(void)
  44840. +{
  44841. + struct allocation *a;
  44842. +
  44843. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  44844. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  44845. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  44846. + DWC_PRINTF("Active = %d\n", manager->num_active);
  44847. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  44848. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  44849. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  44850. + DWC_PRINTF("Unfreed allocations:\n");
  44851. +
  44852. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  44853. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  44854. + a->addr, a->size, a->func, a->line, a->dma);
  44855. + }
  44856. +}
  44857. +
  44858. +/* The replacement functions */
  44859. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  44860. +{
  44861. + void *addr = __DWC_ALLOC(mem_ctx, size);
  44862. +
  44863. + if (!addr) {
  44864. + return NULL;
  44865. + }
  44866. +
  44867. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  44868. + __DWC_FREE(mem_ctx, addr);
  44869. + return NULL;
  44870. + }
  44871. +
  44872. + return addr;
  44873. +}
  44874. +
  44875. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  44876. + int line)
  44877. +{
  44878. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  44879. +
  44880. + if (!addr) {
  44881. + return NULL;
  44882. + }
  44883. +
  44884. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  44885. + __DWC_FREE(mem_ctx, addr);
  44886. + return NULL;
  44887. + }
  44888. +
  44889. + return addr;
  44890. +}
  44891. +
  44892. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  44893. +{
  44894. + free_allocation(mem_ctx, addr, func, line);
  44895. + __DWC_FREE(mem_ctx, addr);
  44896. +}
  44897. +
  44898. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  44899. + char const *func, int line)
  44900. +{
  44901. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  44902. +
  44903. + if (!addr) {
  44904. + return NULL;
  44905. + }
  44906. +
  44907. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  44908. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  44909. + return NULL;
  44910. + }
  44911. +
  44912. + return addr;
  44913. +}
  44914. +
  44915. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  44916. + dwc_dma_t *dma_addr, char const *func, int line)
  44917. +{
  44918. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  44919. +
  44920. + if (!addr) {
  44921. + return NULL;
  44922. + }
  44923. +
  44924. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  44925. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  44926. + return NULL;
  44927. + }
  44928. +
  44929. + return addr;
  44930. +}
  44931. +
  44932. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  44933. + dwc_dma_t dma_addr, char const *func, int line)
  44934. +{
  44935. + free_allocation(dma_ctx, virt_addr, func, line);
  44936. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  44937. +}
  44938. +
  44939. +#endif /* DWC_DEBUG_MEMORY */
  44940. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_modpow.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_modpow.c
  44941. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_modpow.c 1970-01-01 01:00:00.000000000 +0100
  44942. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_modpow.c 2014-03-11 16:55:38.000000000 +0100
  44943. @@ -0,0 +1,636 @@
  44944. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  44945. + *
  44946. + * PuTTY is copyright 1997-2007 Simon Tatham.
  44947. + *
  44948. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  44949. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  44950. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  44951. + * Kuhn, and CORE SDI S.A.
  44952. + *
  44953. + * Permission is hereby granted, free of charge, to any person
  44954. + * obtaining a copy of this software and associated documentation files
  44955. + * (the "Software"), to deal in the Software without restriction,
  44956. + * including without limitation the rights to use, copy, modify, merge,
  44957. + * publish, distribute, sublicense, and/or sell copies of the Software,
  44958. + * and to permit persons to whom the Software is furnished to do so,
  44959. + * subject to the following conditions:
  44960. + *
  44961. + * The above copyright notice and this permission notice shall be
  44962. + * included in all copies or substantial portions of the Software.
  44963. +
  44964. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  44965. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  44966. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  44967. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  44968. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  44969. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  44970. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  44971. + *
  44972. + */
  44973. +#ifdef DWC_CRYPTOLIB
  44974. +
  44975. +#ifndef CONFIG_MACH_IPMATE
  44976. +
  44977. +#include "dwc_modpow.h"
  44978. +
  44979. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  44980. +#define BIGNUM_TOP_BIT 0x80000000UL
  44981. +#define BIGNUM_INT_BITS 32
  44982. +
  44983. +
  44984. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  44985. +{
  44986. + void *p;
  44987. + size *= n;
  44988. + if (size == 0) size = 1;
  44989. + p = dwc_alloc(mem_ctx, size);
  44990. + return p;
  44991. +}
  44992. +
  44993. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  44994. +#define sfree dwc_free
  44995. +
  44996. +/*
  44997. + * Usage notes:
  44998. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  44999. + * subscripts, as some implementations object to this (see below).
  45000. + * * Note that none of the division methods below will cope if the
  45001. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  45002. + * to avoid this case.
  45003. + * If this condition occurs, in the case of the x86 DIV instruction,
  45004. + * an overflow exception will occur, which (according to a correspondent)
  45005. + * will manifest on Windows as something like
  45006. + * 0xC0000095: Integer overflow
  45007. + * The C variant won't give the right answer, either.
  45008. + */
  45009. +
  45010. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  45011. +
  45012. +#if defined __GNUC__ && defined __i386__
  45013. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  45014. + __asm__("div %2" : \
  45015. + "=d" (r), "=a" (q) : \
  45016. + "r" (w), "d" (hi), "a" (lo))
  45017. +#else
  45018. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  45019. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  45020. + q = n / w; \
  45021. + r = n % w; \
  45022. +} while (0)
  45023. +#endif
  45024. +
  45025. +// q = n / w;
  45026. +// r = n % w;
  45027. +
  45028. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  45029. +
  45030. +#define BIGNUM_INTERNAL
  45031. +
  45032. +static Bignum newbn(void *mem_ctx, int length)
  45033. +{
  45034. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  45035. + //if (!b)
  45036. + //abort(); /* FIXME */
  45037. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  45038. + b[0] = length;
  45039. + return b;
  45040. +}
  45041. +
  45042. +void freebn(void *mem_ctx, Bignum b)
  45043. +{
  45044. + /*
  45045. + * Burn the evidence, just in case.
  45046. + */
  45047. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  45048. + sfree(mem_ctx, b);
  45049. +}
  45050. +
  45051. +/*
  45052. + * Compute c = a * b.
  45053. + * Input is in the first len words of a and b.
  45054. + * Result is returned in the first 2*len words of c.
  45055. + */
  45056. +static void internal_mul(BignumInt *a, BignumInt *b,
  45057. + BignumInt *c, int len)
  45058. +{
  45059. + int i, j;
  45060. + BignumDblInt t;
  45061. +
  45062. + for (j = 0; j < 2 * len; j++)
  45063. + c[j] = 0;
  45064. +
  45065. + for (i = len - 1; i >= 0; i--) {
  45066. + t = 0;
  45067. + for (j = len - 1; j >= 0; j--) {
  45068. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  45069. + t += (BignumDblInt) c[i + j + 1];
  45070. + c[i + j + 1] = (BignumInt) t;
  45071. + t = t >> BIGNUM_INT_BITS;
  45072. + }
  45073. + c[i] = (BignumInt) t;
  45074. + }
  45075. +}
  45076. +
  45077. +static void internal_add_shifted(BignumInt *number,
  45078. + unsigned n, int shift)
  45079. +{
  45080. + int word = 1 + (shift / BIGNUM_INT_BITS);
  45081. + int bshift = shift % BIGNUM_INT_BITS;
  45082. + BignumDblInt addend;
  45083. +
  45084. + addend = (BignumDblInt)n << bshift;
  45085. +
  45086. + while (addend) {
  45087. + addend += number[word];
  45088. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  45089. + addend >>= BIGNUM_INT_BITS;
  45090. + word++;
  45091. + }
  45092. +}
  45093. +
  45094. +/*
  45095. + * Compute a = a % m.
  45096. + * Input in first alen words of a and first mlen words of m.
  45097. + * Output in first alen words of a
  45098. + * (of which first alen-mlen words will be zero).
  45099. + * The MSW of m MUST have its high bit set.
  45100. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  45101. + * rather than the internal bigendian format. Quotient parts are shifted
  45102. + * left by `qshift' before adding into quot.
  45103. + */
  45104. +static void internal_mod(BignumInt *a, int alen,
  45105. + BignumInt *m, int mlen,
  45106. + BignumInt *quot, int qshift)
  45107. +{
  45108. + BignumInt m0, m1;
  45109. + unsigned int h;
  45110. + int i, k;
  45111. +
  45112. + m0 = m[0];
  45113. + if (mlen > 1)
  45114. + m1 = m[1];
  45115. + else
  45116. + m1 = 0;
  45117. +
  45118. + for (i = 0; i <= alen - mlen; i++) {
  45119. + BignumDblInt t;
  45120. + unsigned int q, r, c, ai1;
  45121. +
  45122. + if (i == 0) {
  45123. + h = 0;
  45124. + } else {
  45125. + h = a[i - 1];
  45126. + a[i - 1] = 0;
  45127. + }
  45128. +
  45129. + if (i == alen - 1)
  45130. + ai1 = 0;
  45131. + else
  45132. + ai1 = a[i + 1];
  45133. +
  45134. + /* Find q = h:a[i] / m0 */
  45135. + if (h >= m0) {
  45136. + /*
  45137. + * Special case.
  45138. + *
  45139. + * To illustrate it, suppose a BignumInt is 8 bits, and
  45140. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  45141. + * our initial division will be 0xA123 / 0xA1, which
  45142. + * will give a quotient of 0x100 and a divide overflow.
  45143. + * However, the invariants in this division algorithm
  45144. + * are not violated, since the full number A1:23:... is
  45145. + * _less_ than the quotient prefix A1:B2:... and so the
  45146. + * following correction loop would have sorted it out.
  45147. + *
  45148. + * In this situation we set q to be the largest
  45149. + * quotient we _can_ stomach (0xFF, of course).
  45150. + */
  45151. + q = BIGNUM_INT_MASK;
  45152. + } else {
  45153. + /* Macro doesn't want an array subscript expression passed
  45154. + * into it (see definition), so use a temporary. */
  45155. + BignumInt tmplo = a[i];
  45156. + DIVMOD_WORD(q, r, h, tmplo, m0);
  45157. +
  45158. + /* Refine our estimate of q by looking at
  45159. + h:a[i]:a[i+1] / m0:m1 */
  45160. + t = MUL_WORD(m1, q);
  45161. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  45162. + q--;
  45163. + t -= m1;
  45164. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  45165. + if (r >= (BignumDblInt) m0 &&
  45166. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  45167. + }
  45168. + }
  45169. +
  45170. + /* Subtract q * m from a[i...] */
  45171. + c = 0;
  45172. + for (k = mlen - 1; k >= 0; k--) {
  45173. + t = MUL_WORD(q, m[k]);
  45174. + t += c;
  45175. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  45176. + if ((BignumInt) t > a[i + k])
  45177. + c++;
  45178. + a[i + k] -= (BignumInt) t;
  45179. + }
  45180. +
  45181. + /* Add back m in case of borrow */
  45182. + if (c != h) {
  45183. + t = 0;
  45184. + for (k = mlen - 1; k >= 0; k--) {
  45185. + t += m[k];
  45186. + t += a[i + k];
  45187. + a[i + k] = (BignumInt) t;
  45188. + t = t >> BIGNUM_INT_BITS;
  45189. + }
  45190. + q--;
  45191. + }
  45192. + if (quot)
  45193. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  45194. + }
  45195. +}
  45196. +
  45197. +/*
  45198. + * Compute p % mod.
  45199. + * The most significant word of mod MUST be non-zero.
  45200. + * We assume that the result array is the same size as the mod array.
  45201. + * We optionally write out a quotient if `quotient' is non-NULL.
  45202. + * We can avoid writing out the result if `result' is NULL.
  45203. + */
  45204. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  45205. +{
  45206. + BignumInt *n, *m;
  45207. + int mshift;
  45208. + int plen, mlen, i, j;
  45209. +
  45210. + /* Allocate m of size mlen, copy mod to m */
  45211. + /* We use big endian internally */
  45212. + mlen = mod[0];
  45213. + m = snewn(mem_ctx, mlen, BignumInt);
  45214. + //if (!m)
  45215. + //abort(); /* FIXME */
  45216. + for (j = 0; j < mlen; j++)
  45217. + m[j] = mod[mod[0] - j];
  45218. +
  45219. + /* Shift m left to make msb bit set */
  45220. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  45221. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  45222. + break;
  45223. + if (mshift) {
  45224. + for (i = 0; i < mlen - 1; i++)
  45225. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  45226. + m[mlen - 1] = m[mlen - 1] << mshift;
  45227. + }
  45228. +
  45229. + plen = p[0];
  45230. + /* Ensure plen > mlen */
  45231. + if (plen <= mlen)
  45232. + plen = mlen + 1;
  45233. +
  45234. + /* Allocate n of size plen, copy p to n */
  45235. + n = snewn(mem_ctx, plen, BignumInt);
  45236. + //if (!n)
  45237. + //abort(); /* FIXME */
  45238. + for (j = 0; j < plen; j++)
  45239. + n[j] = 0;
  45240. + for (j = 1; j <= (int)p[0]; j++)
  45241. + n[plen - j] = p[j];
  45242. +
  45243. + /* Main computation */
  45244. + internal_mod(n, plen, m, mlen, quotient, mshift);
  45245. +
  45246. + /* Fixup result in case the modulus was shifted */
  45247. + if (mshift) {
  45248. + for (i = plen - mlen - 1; i < plen - 1; i++)
  45249. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  45250. + n[plen - 1] = n[plen - 1] << mshift;
  45251. + internal_mod(n, plen, m, mlen, quotient, 0);
  45252. + for (i = plen - 1; i >= plen - mlen; i--)
  45253. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  45254. + }
  45255. +
  45256. + /* Copy result to buffer */
  45257. + if (result) {
  45258. + for (i = 1; i <= (int)result[0]; i++) {
  45259. + int j = plen - i;
  45260. + result[i] = j >= 0 ? n[j] : 0;
  45261. + }
  45262. + }
  45263. +
  45264. + /* Free temporary arrays */
  45265. + for (i = 0; i < mlen; i++)
  45266. + m[i] = 0;
  45267. + sfree(mem_ctx, m);
  45268. + for (i = 0; i < plen; i++)
  45269. + n[i] = 0;
  45270. + sfree(mem_ctx, n);
  45271. +}
  45272. +
  45273. +/*
  45274. + * Simple remainder.
  45275. + */
  45276. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  45277. +{
  45278. + Bignum r = newbn(mem_ctx, b[0]);
  45279. + bigdivmod(mem_ctx, a, b, r, NULL);
  45280. + return r;
  45281. +}
  45282. +
  45283. +/*
  45284. + * Compute (base ^ exp) % mod.
  45285. + */
  45286. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  45287. +{
  45288. + BignumInt *a, *b, *n, *m;
  45289. + int mshift;
  45290. + int mlen, i, j;
  45291. + Bignum base, result;
  45292. +
  45293. + /*
  45294. + * The most significant word of mod needs to be non-zero. It
  45295. + * should already be, but let's make sure.
  45296. + */
  45297. + //assert(mod[mod[0]] != 0);
  45298. +
  45299. + /*
  45300. + * Make sure the base is smaller than the modulus, by reducing
  45301. + * it modulo the modulus if not.
  45302. + */
  45303. + base = bigmod(mem_ctx, base_in, mod);
  45304. +
  45305. + /* Allocate m of size mlen, copy mod to m */
  45306. + /* We use big endian internally */
  45307. + mlen = mod[0];
  45308. + m = snewn(mem_ctx, mlen, BignumInt);
  45309. + //if (!m)
  45310. + //abort(); /* FIXME */
  45311. + for (j = 0; j < mlen; j++)
  45312. + m[j] = mod[mod[0] - j];
  45313. +
  45314. + /* Shift m left to make msb bit set */
  45315. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  45316. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  45317. + break;
  45318. + if (mshift) {
  45319. + for (i = 0; i < mlen - 1; i++)
  45320. + m[i] =
  45321. + (m[i] << mshift) | (m[i + 1] >>
  45322. + (BIGNUM_INT_BITS - mshift));
  45323. + m[mlen - 1] = m[mlen - 1] << mshift;
  45324. + }
  45325. +
  45326. + /* Allocate n of size mlen, copy base to n */
  45327. + n = snewn(mem_ctx, mlen, BignumInt);
  45328. + //if (!n)
  45329. + //abort(); /* FIXME */
  45330. + i = mlen - base[0];
  45331. + for (j = 0; j < i; j++)
  45332. + n[j] = 0;
  45333. + for (j = 0; j < base[0]; j++)
  45334. + n[i + j] = base[base[0] - j];
  45335. +
  45336. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  45337. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  45338. + //if (!a)
  45339. + //abort(); /* FIXME */
  45340. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  45341. + //if (!b)
  45342. + //abort(); /* FIXME */
  45343. + for (i = 0; i < 2 * mlen; i++)
  45344. + a[i] = 0;
  45345. + a[2 * mlen - 1] = 1;
  45346. +
  45347. + /* Skip leading zero bits of exp. */
  45348. + i = 0;
  45349. + j = BIGNUM_INT_BITS - 1;
  45350. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  45351. + j--;
  45352. + if (j < 0) {
  45353. + i++;
  45354. + j = BIGNUM_INT_BITS - 1;
  45355. + }
  45356. + }
  45357. +
  45358. + /* Main computation */
  45359. + while (i < exp[0]) {
  45360. + while (j >= 0) {
  45361. + internal_mul(a + mlen, a + mlen, b, mlen);
  45362. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  45363. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  45364. + internal_mul(b + mlen, n, a, mlen);
  45365. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  45366. + } else {
  45367. + BignumInt *t;
  45368. + t = a;
  45369. + a = b;
  45370. + b = t;
  45371. + }
  45372. + j--;
  45373. + }
  45374. + i++;
  45375. + j = BIGNUM_INT_BITS - 1;
  45376. + }
  45377. +
  45378. + /* Fixup result in case the modulus was shifted */
  45379. + if (mshift) {
  45380. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  45381. + a[i] =
  45382. + (a[i] << mshift) | (a[i + 1] >>
  45383. + (BIGNUM_INT_BITS - mshift));
  45384. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  45385. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  45386. + for (i = 2 * mlen - 1; i >= mlen; i--)
  45387. + a[i] =
  45388. + (a[i] >> mshift) | (a[i - 1] <<
  45389. + (BIGNUM_INT_BITS - mshift));
  45390. + }
  45391. +
  45392. + /* Copy result to buffer */
  45393. + result = newbn(mem_ctx, mod[0]);
  45394. + for (i = 0; i < mlen; i++)
  45395. + result[result[0] - i] = a[i + mlen];
  45396. + while (result[0] > 1 && result[result[0]] == 0)
  45397. + result[0]--;
  45398. +
  45399. + /* Free temporary arrays */
  45400. + for (i = 0; i < 2 * mlen; i++)
  45401. + a[i] = 0;
  45402. + sfree(mem_ctx, a);
  45403. + for (i = 0; i < 2 * mlen; i++)
  45404. + b[i] = 0;
  45405. + sfree(mem_ctx, b);
  45406. + for (i = 0; i < mlen; i++)
  45407. + m[i] = 0;
  45408. + sfree(mem_ctx, m);
  45409. + for (i = 0; i < mlen; i++)
  45410. + n[i] = 0;
  45411. + sfree(mem_ctx, n);
  45412. +
  45413. + freebn(mem_ctx, base);
  45414. +
  45415. + return result;
  45416. +}
  45417. +
  45418. +
  45419. +#ifdef UNITTEST
  45420. +
  45421. +static __u32 dh_p[] = {
  45422. + 96,
  45423. + 0xFFFFFFFF,
  45424. + 0xFFFFFFFF,
  45425. + 0xA93AD2CA,
  45426. + 0x4B82D120,
  45427. + 0xE0FD108E,
  45428. + 0x43DB5BFC,
  45429. + 0x74E5AB31,
  45430. + 0x08E24FA0,
  45431. + 0xBAD946E2,
  45432. + 0x770988C0,
  45433. + 0x7A615D6C,
  45434. + 0xBBE11757,
  45435. + 0x177B200C,
  45436. + 0x521F2B18,
  45437. + 0x3EC86A64,
  45438. + 0xD8760273,
  45439. + 0xD98A0864,
  45440. + 0xF12FFA06,
  45441. + 0x1AD2EE6B,
  45442. + 0xCEE3D226,
  45443. + 0x4A25619D,
  45444. + 0x1E8C94E0,
  45445. + 0xDB0933D7,
  45446. + 0xABF5AE8C,
  45447. + 0xA6E1E4C7,
  45448. + 0xB3970F85,
  45449. + 0x5D060C7D,
  45450. + 0x8AEA7157,
  45451. + 0x58DBEF0A,
  45452. + 0xECFB8504,
  45453. + 0xDF1CBA64,
  45454. + 0xA85521AB,
  45455. + 0x04507A33,
  45456. + 0xAD33170D,
  45457. + 0x8AAAC42D,
  45458. + 0x15728E5A,
  45459. + 0x98FA0510,
  45460. + 0x15D22618,
  45461. + 0xEA956AE5,
  45462. + 0x3995497C,
  45463. + 0x95581718,
  45464. + 0xDE2BCBF6,
  45465. + 0x6F4C52C9,
  45466. + 0xB5C55DF0,
  45467. + 0xEC07A28F,
  45468. + 0x9B2783A2,
  45469. + 0x180E8603,
  45470. + 0xE39E772C,
  45471. + 0x2E36CE3B,
  45472. + 0x32905E46,
  45473. + 0xCA18217C,
  45474. + 0xF1746C08,
  45475. + 0x4ABC9804,
  45476. + 0x670C354E,
  45477. + 0x7096966D,
  45478. + 0x9ED52907,
  45479. + 0x208552BB,
  45480. + 0x1C62F356,
  45481. + 0xDCA3AD96,
  45482. + 0x83655D23,
  45483. + 0xFD24CF5F,
  45484. + 0x69163FA8,
  45485. + 0x1C55D39A,
  45486. + 0x98DA4836,
  45487. + 0xA163BF05,
  45488. + 0xC2007CB8,
  45489. + 0xECE45B3D,
  45490. + 0x49286651,
  45491. + 0x7C4B1FE6,
  45492. + 0xAE9F2411,
  45493. + 0x5A899FA5,
  45494. + 0xEE386BFB,
  45495. + 0xF406B7ED,
  45496. + 0x0BFF5CB6,
  45497. + 0xA637ED6B,
  45498. + 0xF44C42E9,
  45499. + 0x625E7EC6,
  45500. + 0xE485B576,
  45501. + 0x6D51C245,
  45502. + 0x4FE1356D,
  45503. + 0xF25F1437,
  45504. + 0x302B0A6D,
  45505. + 0xCD3A431B,
  45506. + 0xEF9519B3,
  45507. + 0x8E3404DD,
  45508. + 0x514A0879,
  45509. + 0x3B139B22,
  45510. + 0x020BBEA6,
  45511. + 0x8A67CC74,
  45512. + 0x29024E08,
  45513. + 0x80DC1CD1,
  45514. + 0xC4C6628B,
  45515. + 0x2168C234,
  45516. + 0xC90FDAA2,
  45517. + 0xFFFFFFFF,
  45518. + 0xFFFFFFFF,
  45519. +};
  45520. +
  45521. +static __u32 dh_a[] = {
  45522. + 8,
  45523. + 0xdf367516,
  45524. + 0x86459caa,
  45525. + 0xe2d459a4,
  45526. + 0xd910dae0,
  45527. + 0x8a8b5e37,
  45528. + 0x67ab31c6,
  45529. + 0xf0b55ea9,
  45530. + 0x440051d6,
  45531. +};
  45532. +
  45533. +static __u32 dh_b[] = {
  45534. + 8,
  45535. + 0xded92656,
  45536. + 0xe07a048a,
  45537. + 0x6fa452cd,
  45538. + 0x2df89d30,
  45539. + 0xc75f1b0f,
  45540. + 0x8ce3578f,
  45541. + 0x7980a324,
  45542. + 0x5daec786,
  45543. +};
  45544. +
  45545. +static __u32 dh_g[] = {
  45546. + 1,
  45547. + 2,
  45548. +};
  45549. +
  45550. +int main(void)
  45551. +{
  45552. + int i;
  45553. + __u32 *k;
  45554. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  45555. +
  45556. + printf("\n\n");
  45557. + for (i=0; i<k[0]; i++) {
  45558. + __u32 word32 = k[k[0] - i];
  45559. + __u16 l = word32 & 0xffff;
  45560. + __u16 m = (word32 & 0xffff0000) >> 16;
  45561. + printf("%04x %04x ", m, l);
  45562. + if (!((i + 1)%13)) printf("\n");
  45563. + }
  45564. + printf("\n\n");
  45565. +
  45566. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  45567. + printf("PASS\n\n");
  45568. + }
  45569. + else {
  45570. + printf("FAIL\n\n");
  45571. + }
  45572. +
  45573. +}
  45574. +
  45575. +#endif /* UNITTEST */
  45576. +
  45577. +#endif /* CONFIG_MACH_IPMATE */
  45578. +
  45579. +#endif /*DWC_CRYPTOLIB */
  45580. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_modpow.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_modpow.h
  45581. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_modpow.h 1970-01-01 01:00:00.000000000 +0100
  45582. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_modpow.h 2014-03-11 16:53:12.000000000 +0100
  45583. @@ -0,0 +1,34 @@
  45584. +/*
  45585. + * dwc_modpow.h
  45586. + * See dwc_modpow.c for license and changes
  45587. + */
  45588. +#ifndef _DWC_MODPOW_H
  45589. +#define _DWC_MODPOW_H
  45590. +
  45591. +#ifdef __cplusplus
  45592. +extern "C" {
  45593. +#endif
  45594. +
  45595. +#include "dwc_os.h"
  45596. +
  45597. +/** @file
  45598. + *
  45599. + * This file defines the module exponentiation function which is only used
  45600. + * internally by the DWC UWB modules for calculation of PKs during numeric
  45601. + * association. The routine is taken from the PUTTY, an open source terminal
  45602. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  45603. + *
  45604. + */
  45605. +
  45606. +typedef uint32_t BignumInt;
  45607. +typedef uint64_t BignumDblInt;
  45608. +typedef BignumInt *Bignum;
  45609. +
  45610. +/* Compute modular exponentiaion */
  45611. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  45612. +
  45613. +#ifdef __cplusplus
  45614. +}
  45615. +#endif
  45616. +
  45617. +#endif /* _LINUX_BIGNUM_H */
  45618. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_notifier.c linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_notifier.c
  45619. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_notifier.c 1970-01-01 01:00:00.000000000 +0100
  45620. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_notifier.c 2014-03-11 16:53:12.000000000 +0100
  45621. @@ -0,0 +1,319 @@
  45622. +#ifdef DWC_NOTIFYLIB
  45623. +
  45624. +#include "dwc_notifier.h"
  45625. +#include "dwc_list.h"
  45626. +
  45627. +typedef struct dwc_observer {
  45628. + void *observer;
  45629. + dwc_notifier_callback_t callback;
  45630. + void *data;
  45631. + char *notification;
  45632. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  45633. +} observer_t;
  45634. +
  45635. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  45636. +
  45637. +typedef struct dwc_notifier {
  45638. + void *mem_ctx;
  45639. + void *object;
  45640. + struct observer_queue observers;
  45641. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  45642. +} notifier_t;
  45643. +
  45644. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  45645. +
  45646. +typedef struct manager {
  45647. + void *mem_ctx;
  45648. + void *wkq_ctx;
  45649. + dwc_workq_t *wq;
  45650. +// dwc_mutex_t *mutex;
  45651. + struct notifier_queue notifiers;
  45652. +} manager_t;
  45653. +
  45654. +static manager_t *manager = NULL;
  45655. +
  45656. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  45657. +{
  45658. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  45659. + if (!manager) {
  45660. + return -DWC_E_NO_MEMORY;
  45661. + }
  45662. +
  45663. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  45664. +
  45665. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  45666. + if (!manager->wq) {
  45667. + return -DWC_E_NO_MEMORY;
  45668. + }
  45669. +
  45670. + return 0;
  45671. +}
  45672. +
  45673. +static void free_manager(void)
  45674. +{
  45675. + dwc_workq_free(manager->wq);
  45676. +
  45677. + /* All notifiers must have unregistered themselves before this module
  45678. + * can be removed. Hitting this assertion indicates a programmer
  45679. + * error. */
  45680. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  45681. + "Notification manager being freed before all notifiers have been removed");
  45682. + dwc_free(manager->mem_ctx, manager);
  45683. +}
  45684. +
  45685. +#ifdef DEBUG
  45686. +static void dump_manager(void)
  45687. +{
  45688. + notifier_t *n;
  45689. + observer_t *o;
  45690. +
  45691. + DWC_ASSERT(manager, "Notification manager not found");
  45692. +
  45693. + DWC_DEBUG("List of all notifiers and observers:\n");
  45694. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  45695. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  45696. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  45697. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  45698. + }
  45699. + }
  45700. +}
  45701. +#else
  45702. +#define dump_manager(...)
  45703. +#endif
  45704. +
  45705. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  45706. + dwc_notifier_callback_t callback, void *data)
  45707. +{
  45708. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  45709. +
  45710. + if (!new_observer) {
  45711. + return NULL;
  45712. + }
  45713. +
  45714. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  45715. + new_observer->observer = observer;
  45716. + new_observer->notification = notification;
  45717. + new_observer->callback = callback;
  45718. + new_observer->data = data;
  45719. + return new_observer;
  45720. +}
  45721. +
  45722. +static void free_observer(void *mem_ctx, observer_t *observer)
  45723. +{
  45724. + dwc_free(mem_ctx, observer);
  45725. +}
  45726. +
  45727. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  45728. +{
  45729. + notifier_t *notifier;
  45730. +
  45731. + if (!object) {
  45732. + return NULL;
  45733. + }
  45734. +
  45735. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  45736. + if (!notifier) {
  45737. + return NULL;
  45738. + }
  45739. +
  45740. + DWC_CIRCLEQ_INIT(&notifier->observers);
  45741. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  45742. +
  45743. + notifier->mem_ctx = mem_ctx;
  45744. + notifier->object = object;
  45745. + return notifier;
  45746. +}
  45747. +
  45748. +static void free_notifier(notifier_t *notifier)
  45749. +{
  45750. + observer_t *observer;
  45751. +
  45752. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  45753. + free_observer(notifier->mem_ctx, observer);
  45754. + }
  45755. +
  45756. + dwc_free(notifier->mem_ctx, notifier);
  45757. +}
  45758. +
  45759. +static notifier_t *find_notifier(void *object)
  45760. +{
  45761. + notifier_t *notifier;
  45762. +
  45763. + DWC_ASSERT(manager, "Notification manager not found");
  45764. +
  45765. + if (!object) {
  45766. + return NULL;
  45767. + }
  45768. +
  45769. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  45770. + if (notifier->object == object) {
  45771. + return notifier;
  45772. + }
  45773. + }
  45774. +
  45775. + return NULL;
  45776. +}
  45777. +
  45778. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  45779. +{
  45780. + return create_manager(mem_ctx, wkq_ctx);
  45781. +}
  45782. +
  45783. +void dwc_free_notification_manager(void)
  45784. +{
  45785. + free_manager();
  45786. +}
  45787. +
  45788. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  45789. +{
  45790. + notifier_t *notifier;
  45791. +
  45792. + DWC_ASSERT(manager, "Notification manager not found");
  45793. +
  45794. + notifier = find_notifier(object);
  45795. + if (notifier) {
  45796. + DWC_ERROR("Notifier %p is already registered\n", object);
  45797. + return NULL;
  45798. + }
  45799. +
  45800. + notifier = alloc_notifier(mem_ctx, object);
  45801. + if (!notifier) {
  45802. + return NULL;
  45803. + }
  45804. +
  45805. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  45806. +
  45807. + DWC_INFO("Notifier %p registered", object);
  45808. + dump_manager();
  45809. +
  45810. + return notifier;
  45811. +}
  45812. +
  45813. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  45814. +{
  45815. + DWC_ASSERT(manager, "Notification manager not found");
  45816. +
  45817. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  45818. + observer_t *o;
  45819. +
  45820. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  45821. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  45822. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  45823. + }
  45824. +
  45825. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  45826. + "Notifier %p has active observers when removing", notifier);
  45827. + }
  45828. +
  45829. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  45830. + free_notifier(notifier);
  45831. +
  45832. + DWC_INFO("Notifier unregistered");
  45833. + dump_manager();
  45834. +}
  45835. +
  45836. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  45837. +int dwc_add_observer(void *observer, void *object, char *notification,
  45838. + dwc_notifier_callback_t callback, void *data)
  45839. +{
  45840. + notifier_t *notifier = find_notifier(object);
  45841. + observer_t *new_observer;
  45842. +
  45843. + if (!notifier) {
  45844. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  45845. + return -DWC_E_INVALID;
  45846. + }
  45847. +
  45848. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  45849. + if (!new_observer) {
  45850. + return -DWC_E_NO_MEMORY;
  45851. + }
  45852. +
  45853. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  45854. +
  45855. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  45856. + observer, object, notification, callback, data);
  45857. +
  45858. + dump_manager();
  45859. + return 0;
  45860. +}
  45861. +
  45862. +int dwc_remove_observer(void *observer)
  45863. +{
  45864. + notifier_t *n;
  45865. +
  45866. + DWC_ASSERT(manager, "Notification manager not found");
  45867. +
  45868. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  45869. + observer_t *o;
  45870. + observer_t *o2;
  45871. +
  45872. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  45873. + if (o->observer == observer) {
  45874. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  45875. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  45876. + o->observer, n->object, o->notification);
  45877. + free_observer(n->mem_ctx, o);
  45878. + }
  45879. + }
  45880. + }
  45881. +
  45882. + dump_manager();
  45883. + return 0;
  45884. +}
  45885. +
  45886. +typedef struct callback_data {
  45887. + void *mem_ctx;
  45888. + dwc_notifier_callback_t cb;
  45889. + void *observer;
  45890. + void *data;
  45891. + void *object;
  45892. + char *notification;
  45893. + void *notification_data;
  45894. +} cb_data_t;
  45895. +
  45896. +static void cb_task(void *data)
  45897. +{
  45898. + cb_data_t *cb = (cb_data_t *)data;
  45899. +
  45900. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  45901. + dwc_free(cb->mem_ctx, cb);
  45902. +}
  45903. +
  45904. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  45905. +{
  45906. + observer_t *o;
  45907. +
  45908. + DWC_ASSERT(manager, "Notification manager not found");
  45909. +
  45910. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  45911. + int len = DWC_STRLEN(notification);
  45912. +
  45913. + if (DWC_STRLEN(o->notification) != len) {
  45914. + continue;
  45915. + }
  45916. +
  45917. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  45918. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  45919. +
  45920. + if (!cb_data) {
  45921. + DWC_ERROR("Failed to allocate callback data\n");
  45922. + return;
  45923. + }
  45924. +
  45925. + cb_data->mem_ctx = notifier->mem_ctx;
  45926. + cb_data->cb = o->callback;
  45927. + cb_data->observer = o->observer;
  45928. + cb_data->data = o->data;
  45929. + cb_data->object = notifier->object;
  45930. + cb_data->notification = notification;
  45931. + cb_data->notification_data = notification_data;
  45932. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  45933. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  45934. + "Notify callback from %p for Notification %s, to observer %p",
  45935. + cb_data->object, notification, cb_data->observer);
  45936. + }
  45937. + }
  45938. +}
  45939. +
  45940. +#endif /* DWC_NOTIFYLIB */
  45941. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_notifier.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_notifier.h
  45942. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_notifier.h 1970-01-01 01:00:00.000000000 +0100
  45943. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_notifier.h 2014-03-11 16:53:12.000000000 +0100
  45944. @@ -0,0 +1,122 @@
  45945. +
  45946. +#ifndef __DWC_NOTIFIER_H__
  45947. +#define __DWC_NOTIFIER_H__
  45948. +
  45949. +#ifdef __cplusplus
  45950. +extern "C" {
  45951. +#endif
  45952. +
  45953. +#include "dwc_os.h"
  45954. +
  45955. +/** @file
  45956. + *
  45957. + * A simple implementation of the Observer pattern. Any "module" can
  45958. + * register as an observer or notifier. The notion of "module" is abstract and
  45959. + * can mean anything used to identify either an observer or notifier. Usually
  45960. + * it will be a pointer to a data structure which contains some state, ie an
  45961. + * object.
  45962. + *
  45963. + * Before any notifiers can be added, the global notification manager must be
  45964. + * brought up with dwc_alloc_notification_manager().
  45965. + * dwc_free_notification_manager() will bring it down and free all resources.
  45966. + * These would typically be called upon module load and unload. The
  45967. + * notification manager is a single global instance that handles all registered
  45968. + * observable modules and observers so this should be done only once.
  45969. + *
  45970. + * A module can be observable by using Notifications to publicize some general
  45971. + * information about it's state or operation. It does not care who listens, or
  45972. + * even if anyone listens, or what they do with the information. The observable
  45973. + * modules do not need to know any information about it's observers or their
  45974. + * interface, or their state or data.
  45975. + *
  45976. + * Any module can register to emit Notifications. It should publish a list of
  45977. + * notifications that it can emit and their behavior, such as when they will get
  45978. + * triggered, and what information will be provided to the observer. Then it
  45979. + * should register itself as an observable module. See dwc_register_notifier().
  45980. + *
  45981. + * Any module can observe any observable, registered module, provided it has a
  45982. + * handle to the other module and knows what notifications to observe. See
  45983. + * dwc_add_observer().
  45984. + *
  45985. + * A function of type dwc_notifier_callback_t is called whenever a notification
  45986. + * is triggered with one or more observers observing it. This function is
  45987. + * called in it's own process so it may sleep or block if needed. It is
  45988. + * guaranteed to be called sometime after the notification has occurred and will
  45989. + * be called once per each time the notification is triggered. It will NOT be
  45990. + * called in the same process context used to trigger the notification.
  45991. + *
  45992. + * @section Limitiations
  45993. + *
  45994. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  45995. + * schedule too many processes too handle. Be aware of this limitation when
  45996. + * designing to use notifications, and only add notifications for appropriate
  45997. + * observable information.
  45998. + *
  45999. + * Also Notification callbacks are not synchronous. If you need to synchronize
  46000. + * the behavior between module/observer you must use other means. And perhaps
  46001. + * that will mean Notifications are not the proper solution.
  46002. + */
  46003. +
  46004. +struct dwc_notifier;
  46005. +typedef struct dwc_notifier dwc_notifier_t;
  46006. +
  46007. +/** The callback function must be of this type.
  46008. + *
  46009. + * @param object This is the object that is being observed.
  46010. + * @param notification This is the notification that was triggered.
  46011. + * @param observer This is the observer
  46012. + * @param notification_data This is notification-specific data that the notifier
  46013. + * has included in this notification. The value of this should be published in
  46014. + * the documentation of the observable module with the notifications.
  46015. + * @param user_data This is any custom data that the observer provided when
  46016. + * adding itself as an observer to the notification. */
  46017. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  46018. + void *notification_data, void *user_data);
  46019. +
  46020. +/** Brings up the notification manager. */
  46021. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  46022. +/** Brings down the notification manager. */
  46023. +extern void dwc_free_notification_manager(void);
  46024. +
  46025. +/** This function registers an observable module. A dwc_notifier_t object is
  46026. + * returned to the observable module. This is an opaque object that is used by
  46027. + * the observable module to trigger notifications. This object should only be
  46028. + * accessible to functions that are authorized to trigger notifications for this
  46029. + * module. Observers do not need this object. */
  46030. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  46031. +
  46032. +/** This function unregisters an observable module. All observers have to be
  46033. + * removed prior to unregistration. */
  46034. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  46035. +
  46036. +/** Add a module as an observer to the observable module. The observable module
  46037. + * needs to have previously registered with the notification manager.
  46038. + *
  46039. + * @param observer The observer module
  46040. + * @param object The module to observe
  46041. + * @param notification The notification to observe
  46042. + * @param callback The callback function to call
  46043. + * @param user_data Any additional user data to pass into the callback function */
  46044. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  46045. + dwc_notifier_callback_t callback, void *user_data);
  46046. +
  46047. +/** Removes the specified observer from all notifications that it is currently
  46048. + * observing. */
  46049. +extern int dwc_remove_observer(void *observer);
  46050. +
  46051. +/** This function triggers a Notification. It should be called by the
  46052. + * observable module, or any module or library which the observable module
  46053. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  46054. + *
  46055. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  46056. + * their own process context for each trigger. Callbacks can be blocking.
  46057. + * dwc_notify can be called from interrupt context if needed.
  46058. + *
  46059. + */
  46060. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  46061. +
  46062. +#ifdef __cplusplus
  46063. +}
  46064. +#endif
  46065. +
  46066. +#endif /* __DWC_NOTIFIER_H__ */
  46067. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_os.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_os.h
  46068. --- linux-3.13.6/drivers/usb/host/dwc_common_port/dwc_os.h 1970-01-01 01:00:00.000000000 +0100
  46069. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/dwc_os.h 2014-03-11 16:55:38.000000000 +0100
  46070. @@ -0,0 +1,1262 @@
  46071. +/* =========================================================================
  46072. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  46073. + * $Revision: #14 $
  46074. + * $Date: 2010/11/04 $
  46075. + * $Change: 1621695 $
  46076. + *
  46077. + * Synopsys Portability Library Software and documentation
  46078. + * (hereinafter, "Software") is an Unsupported proprietary work of
  46079. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  46080. + * between Synopsys and you.
  46081. + *
  46082. + * The Software IS NOT an item of Licensed Software or Licensed Product
  46083. + * under any End User Software License Agreement or Agreement for
  46084. + * Licensed Product with Synopsys or any supplement thereto. You are
  46085. + * permitted to use and redistribute this Software in source and binary
  46086. + * forms, with or without modification, provided that redistributions
  46087. + * of source code must retain this notice. You may not view, use,
  46088. + * disclose, copy or distribute this file or any information contained
  46089. + * herein except pursuant to this license grant from Synopsys. If you
  46090. + * do not agree with this notice, including the disclaimer below, then
  46091. + * you are not authorized to use the Software.
  46092. + *
  46093. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  46094. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  46095. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  46096. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  46097. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  46098. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  46099. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  46100. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  46101. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  46102. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  46103. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46104. + * DAMAGE.
  46105. + * ========================================================================= */
  46106. +#ifndef _DWC_OS_H_
  46107. +#define _DWC_OS_H_
  46108. +
  46109. +#ifdef __cplusplus
  46110. +extern "C" {
  46111. +#endif
  46112. +
  46113. +/** @file
  46114. + *
  46115. + * DWC portability library, low level os-wrapper functions
  46116. + *
  46117. + */
  46118. +
  46119. +/* These basic types need to be defined by some OS header file or custom header
  46120. + * file for your specific target architecture.
  46121. + *
  46122. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  46123. + *
  46124. + * Any custom or alternate header file must be added and enabled here.
  46125. + */
  46126. +
  46127. +#ifdef DWC_LINUX
  46128. +# include <linux/types.h>
  46129. +# ifdef CONFIG_DEBUG_MUTEXES
  46130. +# include <linux/mutex.h>
  46131. +# endif
  46132. +# include <linux/errno.h>
  46133. +# include <stdarg.h>
  46134. +#endif
  46135. +
  46136. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46137. +# include <os_dep.h>
  46138. +#endif
  46139. +
  46140. +
  46141. +/** @name Primitive Types and Values */
  46142. +
  46143. +/** We define a boolean type for consistency. Can be either YES or NO */
  46144. +typedef uint8_t dwc_bool_t;
  46145. +#define YES 1
  46146. +#define NO 0
  46147. +
  46148. +#ifdef DWC_LINUX
  46149. +
  46150. +/** @name Error Codes */
  46151. +#define DWC_E_INVALID EINVAL
  46152. +#define DWC_E_NO_MEMORY ENOMEM
  46153. +#define DWC_E_NO_DEVICE ENODEV
  46154. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  46155. +#define DWC_E_TIMEOUT ETIMEDOUT
  46156. +#define DWC_E_BUSY EBUSY
  46157. +#define DWC_E_AGAIN EAGAIN
  46158. +#define DWC_E_RESTART ERESTART
  46159. +#define DWC_E_ABORT ECONNABORTED
  46160. +#define DWC_E_SHUTDOWN ESHUTDOWN
  46161. +#define DWC_E_NO_DATA ENODATA
  46162. +#define DWC_E_DISCONNECT ECONNRESET
  46163. +#define DWC_E_UNKNOWN EINVAL
  46164. +#define DWC_E_NO_STREAM_RES ENOSR
  46165. +#define DWC_E_COMMUNICATION ECOMM
  46166. +#define DWC_E_OVERFLOW EOVERFLOW
  46167. +#define DWC_E_PROTOCOL EPROTO
  46168. +#define DWC_E_IN_PROGRESS EINPROGRESS
  46169. +#define DWC_E_PIPE EPIPE
  46170. +#define DWC_E_IO EIO
  46171. +#define DWC_E_NO_SPACE ENOSPC
  46172. +
  46173. +#else
  46174. +
  46175. +/** @name Error Codes */
  46176. +#define DWC_E_INVALID 1001
  46177. +#define DWC_E_NO_MEMORY 1002
  46178. +#define DWC_E_NO_DEVICE 1003
  46179. +#define DWC_E_NOT_SUPPORTED 1004
  46180. +#define DWC_E_TIMEOUT 1005
  46181. +#define DWC_E_BUSY 1006
  46182. +#define DWC_E_AGAIN 1007
  46183. +#define DWC_E_RESTART 1008
  46184. +#define DWC_E_ABORT 1009
  46185. +#define DWC_E_SHUTDOWN 1010
  46186. +#define DWC_E_NO_DATA 1011
  46187. +#define DWC_E_DISCONNECT 2000
  46188. +#define DWC_E_UNKNOWN 3000
  46189. +#define DWC_E_NO_STREAM_RES 4001
  46190. +#define DWC_E_COMMUNICATION 4002
  46191. +#define DWC_E_OVERFLOW 4003
  46192. +#define DWC_E_PROTOCOL 4004
  46193. +#define DWC_E_IN_PROGRESS 4005
  46194. +#define DWC_E_PIPE 4006
  46195. +#define DWC_E_IO 4007
  46196. +#define DWC_E_NO_SPACE 4008
  46197. +
  46198. +#endif
  46199. +
  46200. +
  46201. +/** @name Tracing/Logging Functions
  46202. + *
  46203. + * These function provide the capability to add tracing, debugging, and error
  46204. + * messages, as well exceptions as assertions. The WUDEV uses these
  46205. + * extensively. These could be logged to the main console, the serial port, an
  46206. + * internal buffer, etc. These functions could also be no-op if they are too
  46207. + * expensive on your system. By default undefining the DEBUG macro already
  46208. + * no-ops some of these functions. */
  46209. +
  46210. +/** Returns non-zero if in interrupt context. */
  46211. +extern dwc_bool_t DWC_IN_IRQ(void);
  46212. +#define dwc_in_irq DWC_IN_IRQ
  46213. +
  46214. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  46215. +static inline char *dwc_irq(void) {
  46216. + return DWC_IN_IRQ() ? "IRQ" : "";
  46217. +}
  46218. +
  46219. +/** Returns non-zero if in bottom-half context. */
  46220. +extern dwc_bool_t DWC_IN_BH(void);
  46221. +#define dwc_in_bh DWC_IN_BH
  46222. +
  46223. +/** Returns "BH" if DWC_IN_BH is true. */
  46224. +static inline char *dwc_bh(void) {
  46225. + return DWC_IN_BH() ? "BH" : "";
  46226. +}
  46227. +
  46228. +/**
  46229. + * A vprintf() clone. Just call vprintf if you've got it.
  46230. + */
  46231. +extern void DWC_VPRINTF(char *format, va_list args);
  46232. +#define dwc_vprintf DWC_VPRINTF
  46233. +
  46234. +/**
  46235. + * A vsnprintf() clone. Just call vprintf if you've got it.
  46236. + */
  46237. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  46238. +#define dwc_vsnprintf DWC_VSNPRINTF
  46239. +
  46240. +/**
  46241. + * printf() clone. Just call printf if you've go it.
  46242. + */
  46243. +extern void DWC_PRINTF(char *format, ...)
  46244. +/* This provides compiler level static checking of the parameters if you're
  46245. + * using GCC. */
  46246. +#ifdef __GNUC__
  46247. + __attribute__ ((format(printf, 1, 2)));
  46248. +#else
  46249. + ;
  46250. +#endif
  46251. +#define dwc_printf DWC_PRINTF
  46252. +
  46253. +/**
  46254. + * sprintf() clone. Just call sprintf if you've got it.
  46255. + */
  46256. +extern int DWC_SPRINTF(char *string, char *format, ...)
  46257. +#ifdef __GNUC__
  46258. + __attribute__ ((format(printf, 2, 3)));
  46259. +#else
  46260. + ;
  46261. +#endif
  46262. +#define dwc_sprintf DWC_SPRINTF
  46263. +
  46264. +/**
  46265. + * snprintf() clone. Just call snprintf if you've got it.
  46266. + */
  46267. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  46268. +#ifdef __GNUC__
  46269. + __attribute__ ((format(printf, 3, 4)));
  46270. +#else
  46271. + ;
  46272. +#endif
  46273. +#define dwc_snprintf DWC_SNPRINTF
  46274. +
  46275. +/**
  46276. + * Prints a WARNING message. On systems that don't differentiate between
  46277. + * warnings and regular log messages, just print it. Indicates that something
  46278. + * may be wrong with the driver. Works like printf().
  46279. + *
  46280. + * Use the DWC_WARN macro to call this function.
  46281. + */
  46282. +extern void __DWC_WARN(char *format, ...)
  46283. +#ifdef __GNUC__
  46284. + __attribute__ ((format(printf, 1, 2)));
  46285. +#else
  46286. + ;
  46287. +#endif
  46288. +
  46289. +/**
  46290. + * Prints an error message. On systems that don't differentiate between errors
  46291. + * and regular log messages, just print it. Indicates that something went wrong
  46292. + * with the driver. Works like printf().
  46293. + *
  46294. + * Use the DWC_ERROR macro to call this function.
  46295. + */
  46296. +extern void __DWC_ERROR(char *format, ...)
  46297. +#ifdef __GNUC__
  46298. + __attribute__ ((format(printf, 1, 2)));
  46299. +#else
  46300. + ;
  46301. +#endif
  46302. +
  46303. +/**
  46304. + * Prints an exception error message and takes some user-defined action such as
  46305. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  46306. + * abnormally wrong with the driver such as programmer error, or other
  46307. + * exceptional condition. It should not be ignored so even on systems without
  46308. + * printing capability, some action should be taken to notify the developer of
  46309. + * it. Works like printf().
  46310. + */
  46311. +extern void DWC_EXCEPTION(char *format, ...)
  46312. +#ifdef __GNUC__
  46313. + __attribute__ ((format(printf, 1, 2)));
  46314. +#else
  46315. + ;
  46316. +#endif
  46317. +#define dwc_exception DWC_EXCEPTION
  46318. +
  46319. +#ifndef DWC_OTG_DEBUG_LEV
  46320. +#define DWC_OTG_DEBUG_LEV 0
  46321. +#endif
  46322. +
  46323. +#ifdef DEBUG
  46324. +/**
  46325. + * Prints out a debug message. Used for logging/trace messages.
  46326. + *
  46327. + * Use the DWC_DEBUG macro to call this function
  46328. + */
  46329. +extern void __DWC_DEBUG(char *format, ...)
  46330. +#ifdef __GNUC__
  46331. + __attribute__ ((format(printf, 1, 2)));
  46332. +#else
  46333. + ;
  46334. +#endif
  46335. +#else
  46336. +#define __DWC_DEBUG printk
  46337. +#endif
  46338. +
  46339. +/**
  46340. + * Prints out a Debug message.
  46341. + */
  46342. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  46343. + __func__, dwc_irq(), ## _args)
  46344. +#define dwc_debug DWC_DEBUG
  46345. +/**
  46346. + * Prints out a Debug message if enabled at compile time.
  46347. + */
  46348. +#if DWC_OTG_DEBUG_LEV > 0
  46349. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  46350. +#else
  46351. +#define DWC_DEBUGC(_format, _args...)
  46352. +#endif
  46353. +#define dwc_debugc DWC_DEBUGC
  46354. +/**
  46355. + * Prints out an informative message.
  46356. + */
  46357. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  46358. + dwc_irq(), ## _args)
  46359. +#define dwc_info DWC_INFO
  46360. +/**
  46361. + * Prints out an informative message if enabled at compile time.
  46362. + */
  46363. +#if DWC_OTG_DEBUG_LEV > 1
  46364. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  46365. +#else
  46366. +#define DWC_INFOC(_format, _args...)
  46367. +#endif
  46368. +#define dwc_infoc DWC_INFOC
  46369. +/**
  46370. + * Prints out a warning message.
  46371. + */
  46372. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  46373. + dwc_irq(), __func__, __LINE__, ## _args)
  46374. +#define dwc_warn DWC_WARN
  46375. +/**
  46376. + * Prints out an error message.
  46377. + */
  46378. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  46379. + dwc_irq(), __func__, __LINE__, ## _args)
  46380. +#define dwc_error DWC_ERROR
  46381. +
  46382. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  46383. + dwc_irq(), __func__, __LINE__, ## _args)
  46384. +#define dwc_proto_error DWC_PROTO_ERROR
  46385. +
  46386. +#ifdef DEBUG
  46387. +/** Prints out a exception error message if the _expr expression fails. Disabled
  46388. + * if DEBUG is not enabled. */
  46389. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  46390. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  46391. + __FILE__, __LINE__, ## _args); } \
  46392. + } while (0)
  46393. +#else
  46394. +#define DWC_ASSERT(_x...)
  46395. +#endif
  46396. +#define dwc_assert DWC_ASSERT
  46397. +
  46398. +
  46399. +/** @name Byte Ordering
  46400. + * The following functions are for conversions between processor's byte ordering
  46401. + * and specific ordering you want.
  46402. + */
  46403. +
  46404. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  46405. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  46406. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  46407. +
  46408. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  46409. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  46410. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  46411. +
  46412. +/** Converts 32 bit little endian data to CPU byte ordering. */
  46413. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  46414. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  46415. +
  46416. +/** Converts 32 bit big endian data to CPU byte ordering. */
  46417. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  46418. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  46419. +
  46420. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  46421. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  46422. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  46423. +
  46424. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  46425. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  46426. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  46427. +
  46428. +/** Converts 16 bit little endian data to CPU byte ordering. */
  46429. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  46430. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  46431. +
  46432. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  46433. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  46434. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  46435. +
  46436. +
  46437. +/** @name Register Read/Write
  46438. + *
  46439. + * The following six functions should be implemented to read/write registers of
  46440. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  46441. + * The reg value is a pointer to the register calculated from the void *base
  46442. + * variable passed into the driver when it is started. */
  46443. +
  46444. +#ifdef DWC_LINUX
  46445. +/* Linux doesn't need any extra parameters for register read/write, so we
  46446. + * just throw away the IO context parameter.
  46447. + */
  46448. +/** Reads the content of a 32-bit register. */
  46449. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  46450. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  46451. +
  46452. +/** Reads the content of a 64-bit register. */
  46453. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  46454. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  46455. +
  46456. +/** Writes to a 32-bit register. */
  46457. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  46458. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  46459. +
  46460. +/** Writes to a 64-bit register. */
  46461. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  46462. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  46463. +
  46464. +/**
  46465. + * Modify bit values in a register. Using the
  46466. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  46467. + */
  46468. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  46469. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  46470. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  46471. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  46472. +
  46473. +#endif /* DWC_LINUX */
  46474. +
  46475. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46476. +typedef struct dwc_ioctx {
  46477. + struct device *dev;
  46478. + bus_space_tag_t iot;
  46479. + bus_space_handle_t ioh;
  46480. +} dwc_ioctx_t;
  46481. +
  46482. +/** BSD needs two extra parameters for register read/write, so we pass
  46483. + * them in using the IO context parameter.
  46484. + */
  46485. +/** Reads the content of a 32-bit register. */
  46486. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  46487. +#define dwc_read_reg32 DWC_READ_REG32
  46488. +
  46489. +/** Reads the content of a 64-bit register. */
  46490. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  46491. +#define dwc_read_reg64 DWC_READ_REG64
  46492. +
  46493. +/** Writes to a 32-bit register. */
  46494. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  46495. +#define dwc_write_reg32 DWC_WRITE_REG32
  46496. +
  46497. +/** Writes to a 64-bit register. */
  46498. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  46499. +#define dwc_write_reg64 DWC_WRITE_REG64
  46500. +
  46501. +/**
  46502. + * Modify bit values in a register. Using the
  46503. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  46504. + */
  46505. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  46506. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  46507. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  46508. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  46509. +
  46510. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  46511. +
  46512. +/** @cond */
  46513. +
  46514. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  46515. + * register writes. */
  46516. +
  46517. +#ifdef DWC_LINUX
  46518. +
  46519. +# ifdef DWC_DEBUG_REGS
  46520. +
  46521. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  46522. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  46523. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  46524. +} \
  46525. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  46526. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  46527. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  46528. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  46529. +}
  46530. +
  46531. +#define dwc_define_read_write_reg(_reg,_container_type) \
  46532. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  46533. + return DWC_READ_REG32(&container->regs->_reg); \
  46534. +} \
  46535. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  46536. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  46537. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  46538. +}
  46539. +
  46540. +# else /* DWC_DEBUG_REGS */
  46541. +
  46542. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  46543. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  46544. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  46545. +} \
  46546. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  46547. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  46548. +}
  46549. +
  46550. +#define dwc_define_read_write_reg(_reg,_container_type) \
  46551. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  46552. + return DWC_READ_REG32(&container->regs->_reg); \
  46553. +} \
  46554. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  46555. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  46556. +}
  46557. +
  46558. +# endif /* DWC_DEBUG_REGS */
  46559. +
  46560. +#endif /* DWC_LINUX */
  46561. +
  46562. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46563. +
  46564. +# ifdef DWC_DEBUG_REGS
  46565. +
  46566. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  46567. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  46568. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  46569. +} \
  46570. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  46571. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  46572. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  46573. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  46574. +}
  46575. +
  46576. +#define dwc_define_read_write_reg(_reg,_container_type) \
  46577. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  46578. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  46579. +} \
  46580. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  46581. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  46582. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  46583. +}
  46584. +
  46585. +# else /* DWC_DEBUG_REGS */
  46586. +
  46587. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  46588. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  46589. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  46590. +} \
  46591. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  46592. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  46593. +}
  46594. +
  46595. +#define dwc_define_read_write_reg(_reg,_container_type) \
  46596. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  46597. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  46598. +} \
  46599. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  46600. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  46601. +}
  46602. +
  46603. +# endif /* DWC_DEBUG_REGS */
  46604. +
  46605. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  46606. +
  46607. +/** @endcond */
  46608. +
  46609. +
  46610. +#ifdef DWC_CRYPTOLIB
  46611. +/** @name Crypto Functions
  46612. + *
  46613. + * These are the low-level cryptographic functions used by the driver. */
  46614. +
  46615. +/** Perform AES CBC */
  46616. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  46617. +#define dwc_aes_cbc DWC_AES_CBC
  46618. +
  46619. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  46620. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  46621. +#define dwc_random_bytes DWC_RANDOM_BYTES
  46622. +
  46623. +/** Perform the SHA-256 hash function */
  46624. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  46625. +#define dwc_sha256 DWC_SHA256
  46626. +
  46627. +/** Calculated the HMAC-SHA256 */
  46628. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  46629. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  46630. +
  46631. +#endif /* DWC_CRYPTOLIB */
  46632. +
  46633. +
  46634. +/** @name Memory Allocation
  46635. + *
  46636. + * These function provide access to memory allocation. There are only 2 DMA
  46637. + * functions and 3 Regular memory functions that need to be implemented. None
  46638. + * of the memory debugging routines need to be implemented. The allocation
  46639. + * routines all ZERO the contents of the memory.
  46640. + *
  46641. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  46642. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  46643. + * keeps track of how much memory the driver is using at any given time. */
  46644. +
  46645. +#define DWC_PAGE_SIZE 4096
  46646. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  46647. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  46648. +
  46649. +#define DWC_INVALID_DMA_ADDR 0x0
  46650. +
  46651. +#ifdef DWC_LINUX
  46652. +/** Type for a DMA address */
  46653. +typedef dma_addr_t dwc_dma_t;
  46654. +#endif
  46655. +
  46656. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46657. +typedef bus_addr_t dwc_dma_t;
  46658. +#endif
  46659. +
  46660. +#ifdef DWC_FREEBSD
  46661. +typedef struct dwc_dmactx {
  46662. + struct device *dev;
  46663. + bus_dma_tag_t dma_tag;
  46664. + bus_dmamap_t dma_map;
  46665. + bus_addr_t dma_paddr;
  46666. + void *dma_vaddr;
  46667. +} dwc_dmactx_t;
  46668. +#endif
  46669. +
  46670. +#ifdef DWC_NETBSD
  46671. +typedef struct dwc_dmactx {
  46672. + struct device *dev;
  46673. + bus_dma_tag_t dma_tag;
  46674. + bus_dmamap_t dma_map;
  46675. + bus_dma_segment_t segs[1];
  46676. + int nsegs;
  46677. + bus_addr_t dma_paddr;
  46678. + void *dma_vaddr;
  46679. +} dwc_dmactx_t;
  46680. +#endif
  46681. +
  46682. +/* @todo these functions will be added in the future */
  46683. +#if 0
  46684. +/**
  46685. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  46686. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  46687. + * boundary requirements specified.
  46688. + *
  46689. + * @param[in] size Specifies the size of the buffers that will be allocated from
  46690. + * this pool.
  46691. + * @param[in] align Specifies the byte alignment requirements of the buffers
  46692. + * allocated from this pool. Must be a power of 2.
  46693. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  46694. + * this pool must not cross.
  46695. + *
  46696. + * @returns A pointer to an internal opaque structure which is not to be
  46697. + * accessed outside of these library functions. Use this handle to specify
  46698. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  46699. + * when you are done with it.
  46700. + */
  46701. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  46702. +
  46703. +/**
  46704. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  46705. + */
  46706. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  46707. +
  46708. +/**
  46709. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  46710. + */
  46711. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  46712. +
  46713. +/**
  46714. + * Free a previously allocated buffer from the DMA pool.
  46715. + */
  46716. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  46717. +#endif
  46718. +
  46719. +/** Allocates a DMA capable buffer and zeroes its contents. */
  46720. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  46721. +
  46722. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  46723. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  46724. +
  46725. +/** Frees a previously allocated buffer. */
  46726. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  46727. +
  46728. +/** Allocates a block of memory and zeroes its contents. */
  46729. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  46730. +
  46731. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  46732. + * which can be used inside interrupt context. The size should be sufficiently
  46733. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  46734. + * __DWC_ALLOC if it is atomic. */
  46735. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  46736. +
  46737. +/** Frees a previously allocated buffer. */
  46738. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  46739. +
  46740. +#ifndef DWC_DEBUG_MEMORY
  46741. +
  46742. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  46743. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  46744. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  46745. +
  46746. +# ifdef DWC_LINUX
  46747. +#define DWC_DMA_ALLOC(_size_,_dma_) __DWC_DMA_ALLOC(NULL, _size_, _dma_)
  46748. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) __DWC_DMA_ALLOC_ATOMIC(NULL, _size_,_dma_)
  46749. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) __DWC_DMA_FREE(NULL, _size_, _virt_, _dma_)
  46750. +# endif
  46751. +
  46752. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46753. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  46754. +#define DWC_DMA_FREE __DWC_DMA_FREE
  46755. +# endif
  46756. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  46757. +
  46758. +#else /* DWC_DEBUG_MEMORY */
  46759. +
  46760. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  46761. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  46762. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  46763. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  46764. + char const *func, int line);
  46765. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  46766. + char const *func, int line);
  46767. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  46768. + dwc_dma_t dma_addr, char const *func, int line);
  46769. +
  46770. +extern int dwc_memory_debug_start(void *mem_ctx);
  46771. +extern void dwc_memory_debug_stop(void);
  46772. +extern void dwc_memory_debug_report(void);
  46773. +
  46774. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  46775. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  46776. + __func__, __LINE__)
  46777. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  46778. +
  46779. +# ifdef DWC_LINUX
  46780. +#define DWC_DMA_ALLOC(_size_,_dma_) dwc_dma_alloc_debug(NULL, _size_, \
  46781. + _dma_, __func__, __LINE__)
  46782. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) dwc_dma_alloc_atomic_debug(NULL, _size_, \
  46783. + _dma_, __func__, __LINE__)
  46784. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) dwc_dma_free_debug(NULL, _size_, \
  46785. + _virt_, _dma_, __func__, __LINE__)
  46786. +# endif
  46787. +
  46788. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46789. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  46790. + _dma_, __func__, __LINE__)
  46791. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  46792. + _virt_, _dma_, __func__, __LINE__)
  46793. +# endif
  46794. +
  46795. +#endif /* DWC_DEBUG_MEMORY */
  46796. +
  46797. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  46798. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  46799. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  46800. +
  46801. +#ifdef DWC_LINUX
  46802. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  46803. + * just throw away the DMA context parameter.
  46804. + */
  46805. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  46806. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  46807. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  46808. +#endif
  46809. +
  46810. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46811. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  46812. + * them in using the DMA context parameter.
  46813. + */
  46814. +#define dwc_dma_alloc DWC_DMA_ALLOC
  46815. +#define dwc_dma_free DWC_DMA_FREE
  46816. +#endif
  46817. +
  46818. +
  46819. +/** @name Memory and String Processing */
  46820. +
  46821. +/** memset() clone */
  46822. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  46823. +#define dwc_memset DWC_MEMSET
  46824. +
  46825. +/** memcpy() clone */
  46826. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  46827. +#define dwc_memcpy DWC_MEMCPY
  46828. +
  46829. +/** memmove() clone */
  46830. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  46831. +#define dwc_memmove DWC_MEMMOVE
  46832. +
  46833. +/** memcmp() clone */
  46834. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  46835. +#define dwc_memcmp DWC_MEMCMP
  46836. +
  46837. +/** strcmp() clone */
  46838. +extern int DWC_STRCMP(void *s1, void *s2);
  46839. +#define dwc_strcmp DWC_STRCMP
  46840. +
  46841. +/** strncmp() clone */
  46842. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  46843. +#define dwc_strncmp DWC_STRNCMP
  46844. +
  46845. +/** strlen() clone, for NULL terminated ASCII strings */
  46846. +extern int DWC_STRLEN(char const *str);
  46847. +#define dwc_strlen DWC_STRLEN
  46848. +
  46849. +/** strcpy() clone, for NULL terminated ASCII strings */
  46850. +extern char *DWC_STRCPY(char *to, const char *from);
  46851. +#define dwc_strcpy DWC_STRCPY
  46852. +
  46853. +/** strdup() clone. If you wish to use memory allocation debugging, this
  46854. + * implementation of strdup should use the DWC_* memory routines instead of
  46855. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  46856. + * will not be seen by the debugging routines. */
  46857. +extern char *DWC_STRDUP(char const *str);
  46858. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  46859. +
  46860. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  46861. + * converted from the string str in base 10 unless the string begins with a "0x"
  46862. + * in which case it is base 16. String must be a NULL terminated sequence of
  46863. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  46864. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  46865. + * the number and end with a NULL character. If any invalid characters are
  46866. + * encountered or it returns with a negative error code and the results of the
  46867. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  46868. + * undefined. An example implementation using atoi() can be referenced from the
  46869. + * Linux implementation. */
  46870. +extern int DWC_ATOI(const char *str, int32_t *value);
  46871. +#define dwc_atoi DWC_ATOI
  46872. +
  46873. +/** Same as above but for unsigned. */
  46874. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  46875. +#define dwc_atoui DWC_ATOUI
  46876. +
  46877. +#ifdef DWC_UTFLIB
  46878. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  46879. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  46880. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  46881. +#endif
  46882. +
  46883. +
  46884. +/** @name Wait queues
  46885. + *
  46886. + * Wait queues provide a means of synchronizing between threads or processes. A
  46887. + * process can block on a waitq if some condition is not true, waiting for it to
  46888. + * become true. When the waitq is triggered all waiting process will get
  46889. + * unblocked and the condition will be check again. Waitqs should be triggered
  46890. + * every time a condition can potentially change.*/
  46891. +struct dwc_waitq;
  46892. +
  46893. +/** Type for a waitq */
  46894. +typedef struct dwc_waitq dwc_waitq_t;
  46895. +
  46896. +/** The type of waitq condition callback function. This is called every time
  46897. + * condition is evaluated. */
  46898. +typedef int (*dwc_waitq_condition_t)(void *data);
  46899. +
  46900. +/** Allocate a waitq */
  46901. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  46902. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  46903. +
  46904. +/** Free a waitq */
  46905. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  46906. +#define dwc_waitq_free DWC_WAITQ_FREE
  46907. +
  46908. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  46909. + * condition again. The function returns when the condition becomes true. The return value
  46910. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  46911. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  46912. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  46913. +
  46914. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  46915. + * check the condition again. The function returns when the condition become
  46916. + * true or the timeout has passed. The return value is 0 on condition true or
  46917. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  46918. + * error. */
  46919. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  46920. + void *data, int32_t msecs);
  46921. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  46922. +
  46923. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  46924. + * has potentially changed. */
  46925. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  46926. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  46927. +
  46928. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  46929. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  46930. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  46931. +
  46932. +
  46933. +/** @name Threads
  46934. + *
  46935. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  46936. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  46937. + * returns the value from the thread.
  46938. + */
  46939. +
  46940. +struct dwc_thread;
  46941. +
  46942. +/** Type for a thread */
  46943. +typedef struct dwc_thread dwc_thread_t;
  46944. +
  46945. +/** The thread function */
  46946. +typedef int (*dwc_thread_function_t)(void *data);
  46947. +
  46948. +/** Create a thread and start it running the thread_function. Returns a handle
  46949. + * to the thread */
  46950. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  46951. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  46952. +
  46953. +/** Stops a thread. Return the value returned by the thread. Or will return
  46954. + * DWC_ABORT if the thread never started. */
  46955. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  46956. +#define dwc_thread_stop DWC_THREAD_STOP
  46957. +
  46958. +/** Signifies to the thread that it must stop. */
  46959. +#ifdef DWC_LINUX
  46960. +/* Linux doesn't need any parameters for kthread_should_stop() */
  46961. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  46962. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  46963. +
  46964. +/* No thread_exit function in Linux */
  46965. +#define dwc_thread_exit(_thrd_)
  46966. +#endif
  46967. +
  46968. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  46969. +/** BSD needs the thread pointer for kthread_suspend_check() */
  46970. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  46971. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  46972. +
  46973. +/** The thread must call this to exit. */
  46974. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  46975. +#define dwc_thread_exit DWC_THREAD_EXIT
  46976. +#endif
  46977. +
  46978. +
  46979. +/** @name Work queues
  46980. + *
  46981. + * Workqs are used to queue a callback function to be called at some later time,
  46982. + * in another thread. */
  46983. +struct dwc_workq;
  46984. +
  46985. +/** Type for a workq */
  46986. +typedef struct dwc_workq dwc_workq_t;
  46987. +
  46988. +/** The type of the callback function to be called. */
  46989. +typedef void (*dwc_work_callback_t)(void *data);
  46990. +
  46991. +/** Allocate a workq */
  46992. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  46993. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  46994. +
  46995. +/** Free a workq. All work must be completed before being freed. */
  46996. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  46997. +#define dwc_workq_free DWC_WORKQ_FREE
  46998. +
  46999. +/** Schedule a callback on the workq, passing in data. The function will be
  47000. + * scheduled at some later time. */
  47001. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  47002. + void *data, char *format, ...)
  47003. +#ifdef __GNUC__
  47004. + __attribute__ ((format(printf, 4, 5)));
  47005. +#else
  47006. + ;
  47007. +#endif
  47008. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  47009. +
  47010. +/** Schedule a callback on the workq, that will be called until at least
  47011. + * given number miliseconds have passed. */
  47012. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  47013. + void *data, uint32_t time, char *format, ...)
  47014. +#ifdef __GNUC__
  47015. + __attribute__ ((format(printf, 5, 6)));
  47016. +#else
  47017. + ;
  47018. +#endif
  47019. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  47020. +
  47021. +/** The number of processes in the workq */
  47022. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  47023. +#define dwc_workq_pending DWC_WORKQ_PENDING
  47024. +
  47025. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  47026. + * 0 on timeout. */
  47027. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  47028. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  47029. +
  47030. +
  47031. +/** @name Tasklets
  47032. + *
  47033. + */
  47034. +struct dwc_tasklet;
  47035. +
  47036. +/** Type for a tasklet */
  47037. +typedef struct dwc_tasklet dwc_tasklet_t;
  47038. +
  47039. +/** The type of the callback function to be called */
  47040. +typedef void (*dwc_tasklet_callback_t)(void *data);
  47041. +
  47042. +/** Allocates a tasklet */
  47043. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  47044. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  47045. +
  47046. +/** Frees a tasklet */
  47047. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  47048. +#define dwc_task_free DWC_TASK_FREE
  47049. +
  47050. +/** Schedules a tasklet to run */
  47051. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  47052. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  47053. +
  47054. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  47055. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  47056. +
  47057. +/** @name Timer
  47058. + *
  47059. + * Callbacks must be small and atomic.
  47060. + */
  47061. +struct dwc_timer;
  47062. +
  47063. +/** Type for a timer */
  47064. +typedef struct dwc_timer dwc_timer_t;
  47065. +
  47066. +/** The type of the callback function to be called */
  47067. +typedef void (*dwc_timer_callback_t)(void *data);
  47068. +
  47069. +/** Allocates a timer */
  47070. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  47071. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  47072. +
  47073. +/** Frees a timer */
  47074. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  47075. +#define dwc_timer_free DWC_TIMER_FREE
  47076. +
  47077. +/** Schedules the timer to run at time ms from now. And will repeat at every
  47078. + * repeat_interval msec therafter
  47079. + *
  47080. + * Modifies a timer that is still awaiting execution to a new expiration time.
  47081. + * The mod_time is added to the old time. */
  47082. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  47083. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  47084. +
  47085. +/** Disables the timer from execution. */
  47086. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  47087. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  47088. +
  47089. +
  47090. +/** @name Spinlocks
  47091. + *
  47092. + * These locks are used when the work between the lock/unlock is atomic and
  47093. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  47094. + * suitable to lock between interrupt/non-interrupt context. They also lock
  47095. + * between processes if you have multiple CPUs or Preemption. If you don't have
  47096. + * multiple CPUS or Preemption, then the you can simply implement the
  47097. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  47098. + * the work between the lock/unlock is atomic, the process context will never
  47099. + * change, and so you never have to lock between processes. */
  47100. +
  47101. +struct dwc_spinlock;
  47102. +
  47103. +/** Type for a spinlock */
  47104. +typedef struct dwc_spinlock dwc_spinlock_t;
  47105. +
  47106. +/** Type for the 'flags' argument to spinlock funtions */
  47107. +typedef unsigned long dwc_irqflags_t;
  47108. +
  47109. +/** Returns an initialized lock variable. This function should allocate and
  47110. + * initialize the OS-specific data structure used for locking. This data
  47111. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  47112. + * be freed by the DWC_FREE_LOCK when it is no longer used. */
  47113. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  47114. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  47115. +
  47116. +/** Frees an initialized lock variable. */
  47117. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  47118. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  47119. +
  47120. +/** Disables interrupts and blocks until it acquires the lock.
  47121. + *
  47122. + * @param lock Pointer to the spinlock.
  47123. + * @param flags Unsigned long for irq flags storage.
  47124. + */
  47125. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  47126. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  47127. +
  47128. +/** Re-enables the interrupt and releases the lock.
  47129. + *
  47130. + * @param lock Pointer to the spinlock.
  47131. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  47132. + * passed into DWC_LOCK.
  47133. + */
  47134. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  47135. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  47136. +
  47137. +/** Blocks until it acquires the lock.
  47138. + *
  47139. + * @param lock Pointer to the spinlock.
  47140. + */
  47141. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  47142. +#define dwc_spinlock DWC_SPINLOCK
  47143. +
  47144. +/** Releases the lock.
  47145. + *
  47146. + * @param lock Pointer to the spinlock.
  47147. + */
  47148. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  47149. +#define dwc_spinunlock DWC_SPINUNLOCK
  47150. +
  47151. +
  47152. +/** @name Mutexes
  47153. + *
  47154. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  47155. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  47156. + */
  47157. +
  47158. +struct dwc_mutex;
  47159. +
  47160. +/** Type for a mutex */
  47161. +typedef struct dwc_mutex dwc_mutex_t;
  47162. +
  47163. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  47164. + * the symbol to determine recursive locking. This makes it falsely think
  47165. + * recursive locking occurs. */
  47166. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  47167. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  47168. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  47169. + mutex_init((struct mutex *)__mutexp); \
  47170. +})
  47171. +#endif
  47172. +
  47173. +/** Allocate a mutex */
  47174. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  47175. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  47176. +
  47177. +/* For memory leak debugging when using Linux Mutex Debugging */
  47178. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  47179. +#define DWC_MUTEX_FREE(__mutexp) do { \
  47180. + mutex_destroy((struct mutex *)__mutexp); \
  47181. + DWC_FREE(__mutexp); \
  47182. +} while(0)
  47183. +#else
  47184. +/** Free a mutex */
  47185. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  47186. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  47187. +#endif
  47188. +
  47189. +/** Lock a mutex */
  47190. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  47191. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  47192. +
  47193. +/** Non-blocking lock returns 1 on successful lock. */
  47194. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  47195. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  47196. +
  47197. +/** Unlock a mutex */
  47198. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  47199. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  47200. +
  47201. +
  47202. +/** @name Time */
  47203. +
  47204. +/** Microsecond delay.
  47205. + *
  47206. + * @param usecs Microseconds to delay.
  47207. + */
  47208. +extern void DWC_UDELAY(uint32_t usecs);
  47209. +#define dwc_udelay DWC_UDELAY
  47210. +
  47211. +/** Millisecond delay.
  47212. + *
  47213. + * @param msecs Milliseconds to delay.
  47214. + */
  47215. +extern void DWC_MDELAY(uint32_t msecs);
  47216. +#define dwc_mdelay DWC_MDELAY
  47217. +
  47218. +/** Non-busy waiting.
  47219. + * Sleeps for specified number of milliseconds.
  47220. + *
  47221. + * @param msecs Milliseconds to sleep.
  47222. + */
  47223. +extern void DWC_MSLEEP(uint32_t msecs);
  47224. +#define dwc_msleep DWC_MSLEEP
  47225. +
  47226. +/**
  47227. + * Returns number of milliseconds since boot.
  47228. + */
  47229. +extern uint32_t DWC_TIME(void);
  47230. +#define dwc_time DWC_TIME
  47231. +
  47232. +
  47233. +
  47234. +
  47235. +/* @mainpage DWC Portability and Common Library
  47236. + *
  47237. + * This is the documentation for the DWC Portability and Common Library.
  47238. + *
  47239. + * @section intro Introduction
  47240. + *
  47241. + * The DWC Portability library consists of wrapper calls and data structures to
  47242. + * all low-level functions which are typically provided by the OS. The WUDEV
  47243. + * driver uses only these functions. In order to port the WUDEV driver, only
  47244. + * the functions in this library need to be re-implemented, with the same
  47245. + * behavior as documented here.
  47246. + *
  47247. + * The Common library consists of higher level functions, which rely only on
  47248. + * calling the functions from the DWC Portability library. These common
  47249. + * routines are shared across modules. Some of the common libraries need to be
  47250. + * used directly by the driver programmer when porting WUDEV. Such as the
  47251. + * parameter and notification libraries.
  47252. + *
  47253. + * @section low Portability Library OS Wrapper Functions
  47254. + *
  47255. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  47256. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  47257. + * these functions are included in the dwc_os.h file.
  47258. + *
  47259. + * There are many functions here covering a wide array of OS services. Please
  47260. + * see dwc_os.h for details, and implementation notes for each function.
  47261. + *
  47262. + * @section common Common Library Functions
  47263. + *
  47264. + * Any function starting with dwc and in all lowercase is a common library
  47265. + * routine. These functions have a portable implementation and do not need to
  47266. + * be reimplemented when porting. The common routines can be used by any
  47267. + * driver, and some must be used by the end user to control the drivers. For
  47268. + * example, you must use the Parameter common library in order to set the
  47269. + * parameters in the WUDEV module.
  47270. + *
  47271. + * The common libraries consist of the following:
  47272. + *
  47273. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  47274. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  47275. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  47276. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  47277. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  47278. + * - Modpow - Used internally only. See dwc_modpow.h
  47279. + * - DH - Used internally only. See dwc_dh.h
  47280. + * - Crypto - Used internally only. See dwc_crypto.h
  47281. + *
  47282. + *
  47283. + * @section prereq Prerequistes For dwc_os.h
  47284. + * @subsection types Data Types
  47285. + *
  47286. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  47287. + * compilation environment. These data types are:
  47288. + *
  47289. + * - uint8_t - unsigned 8-bit data type
  47290. + * - int8_t - signed 8-bit data type
  47291. + * - uint16_t - unsigned 16-bit data type
  47292. + * - int16_t - signed 16-bit data type
  47293. + * - uint32_t - unsigned 32-bit data type
  47294. + * - int32_t - signed 32-bit data type
  47295. + * - uint64_t - unsigned 64-bit data type
  47296. + * - int64_t - signed 64-bit data type
  47297. + *
  47298. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  47299. + * that is to modify the top of the file to include the appropriate header.
  47300. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  47301. + * defined, the correct header will be added. A standard header <stdint.h> is
  47302. + * also used for environments where standard C headers are available.
  47303. + *
  47304. + * @subsection stdarg Variable Arguments
  47305. + *
  47306. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  47307. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  47308. + * provided in your enviornment in order to use dwc_os.h with the debug and
  47309. + * tracing message functionality.
  47310. + *
  47311. + * @subsection thread Threading
  47312. + *
  47313. + * WUDEV Core must be run on an operating system that provides for multiple
  47314. + * threads/processes. Threading can be implemented in many ways, even in
  47315. + * embedded systems without an operating system. At the bare minimum, the
  47316. + * system should be able to start any number of processes at any time to handle
  47317. + * special work. It need not be a pre-emptive system. Process context can
  47318. + * change upon a call to a blocking function. The hardware interrupt context
  47319. + * that calls the module's ISR() function must be differentiable from process
  47320. + * context, even if your processes are impemented via a hardware interrupt.
  47321. + * Further locking mechanism between process must exist (or be implemented), and
  47322. + * process context must have a way to disable interrupts for a period of time to
  47323. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  47324. + * threading should be able to be implemented with the defined behavior.
  47325. + *
  47326. + */
  47327. +
  47328. +#ifdef __cplusplus
  47329. +}
  47330. +#endif
  47331. +
  47332. +#endif /* _DWC_OS_H_ */
  47333. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/Makefile linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile
  47334. --- linux-3.13.6/drivers/usb/host/dwc_common_port/Makefile 1970-01-01 01:00:00.000000000 +0100
  47335. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile 2014-03-11 16:55:38.000000000 +0100
  47336. @@ -0,0 +1,58 @@
  47337. +#
  47338. +# Makefile for DWC_common library
  47339. +#
  47340. +
  47341. +ifneq ($(KERNELRELEASE),)
  47342. +
  47343. +EXTRA_CFLAGS += -DDWC_LINUX
  47344. +#EXTRA_CFLAGS += -DDEBUG
  47345. +#EXTRA_CFLAGS += -DDWC_DEBUG_REGS
  47346. +#EXTRA_CFLAGS += -DDWC_DEBUG_MEMORY
  47347. +
  47348. +EXTRA_CFLAGS += -DDWC_LIBMODULE
  47349. +EXTRA_CFLAGS += -DDWC_CCLIB
  47350. +#EXTRA_CFLAGS += -DDWC_CRYPTOLIB
  47351. +EXTRA_CFLAGS += -DDWC_NOTIFYLIB
  47352. +EXTRA_CFLAGS += -DDWC_UTFLIB
  47353. +
  47354. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  47355. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  47356. + dwc_crypto.o dwc_notifier.o \
  47357. + dwc_common_linux.o dwc_mem.o
  47358. +
  47359. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  47360. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  47361. +
  47362. +ifneq ($(kernrel3),2.6.20)
  47363. +# grayg - I only know that we use EXTRA_CFLAGS in 2.6.31 actually
  47364. +EXTRA_CFLAGS += $(CPPFLAGS)
  47365. +endif
  47366. +
  47367. +else
  47368. +
  47369. +#ifeq ($(KDIR),)
  47370. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  47371. +#endif
  47372. +
  47373. +ifeq ($(ARCH),)
  47374. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  47375. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  47376. +endif
  47377. +
  47378. +ifeq ($(DOXYGEN),)
  47379. +DOXYGEN := doxygen
  47380. +endif
  47381. +
  47382. +default:
  47383. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  47384. +
  47385. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  47386. + $(DOXYGEN) doc/doxygen.cfg
  47387. +
  47388. +tags: $(wildcard *.[hc])
  47389. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  47390. +
  47391. +endif
  47392. +
  47393. +clean:
  47394. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  47395. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/Makefile.fbsd linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile.fbsd
  47396. --- linux-3.13.6/drivers/usb/host/dwc_common_port/Makefile.fbsd 1970-01-01 01:00:00.000000000 +0100
  47397. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile.fbsd 2014-03-11 16:53:12.000000000 +0100
  47398. @@ -0,0 +1,17 @@
  47399. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  47400. +CFLAGS += -DDWC_FREEBSD
  47401. +CFLAGS += -DDEBUG
  47402. +#CFLAGS += -DDWC_DEBUG_REGS
  47403. +#CFLAGS += -DDWC_DEBUG_MEMORY
  47404. +
  47405. +#CFLAGS += -DDWC_LIBMODULE
  47406. +#CFLAGS += -DDWC_CCLIB
  47407. +#CFLAGS += -DDWC_CRYPTOLIB
  47408. +#CFLAGS += -DDWC_NOTIFYLIB
  47409. +#CFLAGS += -DDWC_UTFLIB
  47410. +
  47411. +KMOD = dwc_common_port_lib
  47412. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  47413. + dwc_common_fbsd.c dwc_mem.c
  47414. +
  47415. +.include <bsd.kmod.mk>
  47416. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/Makefile.linux linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile.linux
  47417. --- linux-3.13.6/drivers/usb/host/dwc_common_port/Makefile.linux 1970-01-01 01:00:00.000000000 +0100
  47418. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/Makefile.linux 2014-03-11 16:55:38.000000000 +0100
  47419. @@ -0,0 +1,49 @@
  47420. +#
  47421. +# Makefile for DWC_common library
  47422. +#
  47423. +ifneq ($(KERNELRELEASE),)
  47424. +
  47425. +EXTRA_CFLAGS += -DDWC_LINUX
  47426. +#EXTRA_CFLAGS += -DDEBUG
  47427. +#EXTRA_CFLAGS += -DDWC_DEBUG_REGS
  47428. +#EXTRA_CFLAGS += -DDWC_DEBUG_MEMORY
  47429. +
  47430. +EXTRA_CFLAGS += -DDWC_LIBMODULE
  47431. +EXTRA_CFLAGS += -DDWC_CCLIB
  47432. +EXTRA_CFLAGS += -DDWC_CRYPTOLIB
  47433. +EXTRA_CFLAGS += -DDWC_NOTIFYLIB
  47434. +EXTRA_CFLAGS += -DDWC_UTFLIB
  47435. +
  47436. +obj-m := dwc_common_port_lib.o
  47437. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  47438. + dwc_crypto.o dwc_notifier.o \
  47439. + dwc_common_linux.o dwc_mem.o
  47440. +
  47441. +else
  47442. +
  47443. +ifeq ($(KDIR),)
  47444. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  47445. +endif
  47446. +
  47447. +ifeq ($(ARCH),)
  47448. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  47449. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  47450. +endif
  47451. +
  47452. +ifeq ($(DOXYGEN),)
  47453. +DOXYGEN := doxygen
  47454. +endif
  47455. +
  47456. +default:
  47457. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  47458. +
  47459. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  47460. + $(DOXYGEN) doc/doxygen.cfg
  47461. +
  47462. +tags: $(wildcard *.[hc])
  47463. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  47464. +
  47465. +endif
  47466. +
  47467. +clean:
  47468. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  47469. diff -Nur linux-3.13.6/drivers/usb/host/dwc_common_port/usb.h linux-raspberry-pi/drivers/usb/host/dwc_common_port/usb.h
  47470. --- linux-3.13.6/drivers/usb/host/dwc_common_port/usb.h 1970-01-01 01:00:00.000000000 +0100
  47471. +++ linux-raspberry-pi/drivers/usb/host/dwc_common_port/usb.h 2014-03-11 16:53:12.000000000 +0100
  47472. @@ -0,0 +1,946 @@
  47473. +/*
  47474. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  47475. + * All rights reserved.
  47476. + *
  47477. + * This code is derived from software contributed to The NetBSD Foundation
  47478. + * by Lennart Augustsson (lennart@augustsson.net) at
  47479. + * Carlstedt Research & Technology.
  47480. + *
  47481. + * Redistribution and use in source and binary forms, with or without
  47482. + * modification, are permitted provided that the following conditions
  47483. + * are met:
  47484. + * 1. Redistributions of source code must retain the above copyright
  47485. + * notice, this list of conditions and the following disclaimer.
  47486. + * 2. Redistributions in binary form must reproduce the above copyright
  47487. + * notice, this list of conditions and the following disclaimer in the
  47488. + * documentation and/or other materials provided with the distribution.
  47489. + * 3. All advertising materials mentioning features or use of this software
  47490. + * must display the following acknowledgement:
  47491. + * This product includes software developed by the NetBSD
  47492. + * Foundation, Inc. and its contributors.
  47493. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  47494. + * contributors may be used to endorse or promote products derived
  47495. + * from this software without specific prior written permission.
  47496. + *
  47497. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  47498. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  47499. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  47500. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  47501. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  47502. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  47503. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  47504. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47505. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47506. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  47507. + * POSSIBILITY OF SUCH DAMAGE.
  47508. + */
  47509. +
  47510. +/* Modified by Synopsys, Inc, 12/12/2007 */
  47511. +
  47512. +
  47513. +#ifndef _USB_H_
  47514. +#define _USB_H_
  47515. +
  47516. +#ifdef __cplusplus
  47517. +extern "C" {
  47518. +#endif
  47519. +
  47520. +/*
  47521. + * The USB records contain some unaligned little-endian word
  47522. + * components. The U[SG]ETW macros take care of both the alignment
  47523. + * and endian problem and should always be used to access non-byte
  47524. + * values.
  47525. + */
  47526. +typedef u_int8_t uByte;
  47527. +typedef u_int8_t uWord[2];
  47528. +typedef u_int8_t uDWord[4];
  47529. +
  47530. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  47531. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  47532. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  47533. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  47534. +
  47535. +#if 1
  47536. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  47537. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  47538. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  47539. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  47540. + (w)[1] = (u_int8_t)((v) >> 8), \
  47541. + (w)[2] = (u_int8_t)((v) >> 16), \
  47542. + (w)[3] = (u_int8_t)((v) >> 24))
  47543. +#else
  47544. +/*
  47545. + * On little-endian machines that can handle unanliged accesses
  47546. + * (e.g. i386) these macros can be replaced by the following.
  47547. + */
  47548. +#define UGETW(w) (*(u_int16_t *)(w))
  47549. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  47550. +#define UGETDW(w) (*(u_int32_t *)(w))
  47551. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  47552. +#endif
  47553. +
  47554. +/*
  47555. + * Macros for accessing UAS IU fields, which are big-endian
  47556. + */
  47557. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  47558. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  47559. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  47560. + ((x) >> 8) & 0xff, (x) & 0xff }
  47561. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  47562. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  47563. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  47564. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  47565. + (w)[1] = (u_int8_t)((v) >> 16), \
  47566. + (w)[2] = (u_int8_t)((v) >> 8), \
  47567. + (w)[3] = (u_int8_t)(v))
  47568. +
  47569. +#define UPACKED __attribute__((__packed__))
  47570. +
  47571. +typedef struct {
  47572. + uByte bmRequestType;
  47573. + uByte bRequest;
  47574. + uWord wValue;
  47575. + uWord wIndex;
  47576. + uWord wLength;
  47577. +} UPACKED usb_device_request_t;
  47578. +
  47579. +#define UT_GET_DIR(a) ((a) & 0x80)
  47580. +#define UT_WRITE 0x00
  47581. +#define UT_READ 0x80
  47582. +
  47583. +#define UT_GET_TYPE(a) ((a) & 0x60)
  47584. +#define UT_STANDARD 0x00
  47585. +#define UT_CLASS 0x20
  47586. +#define UT_VENDOR 0x40
  47587. +
  47588. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  47589. +#define UT_DEVICE 0x00
  47590. +#define UT_INTERFACE 0x01
  47591. +#define UT_ENDPOINT 0x02
  47592. +#define UT_OTHER 0x03
  47593. +
  47594. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  47595. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  47596. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  47597. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  47598. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  47599. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  47600. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  47601. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  47602. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  47603. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  47604. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  47605. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  47606. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  47607. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  47608. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  47609. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  47610. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  47611. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  47612. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  47613. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  47614. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  47615. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  47616. +
  47617. +/* Requests */
  47618. +#define UR_GET_STATUS 0x00
  47619. +#define USTAT_STANDARD_STATUS 0x00
  47620. +#define WUSTAT_WUSB_FEATURE 0x01
  47621. +#define WUSTAT_CHANNEL_INFO 0x02
  47622. +#define WUSTAT_RECEIVED_DATA 0x03
  47623. +#define WUSTAT_MAS_AVAILABILITY 0x04
  47624. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  47625. +#define UR_CLEAR_FEATURE 0x01
  47626. +#define UR_SET_FEATURE 0x03
  47627. +#define UR_SET_AND_TEST_FEATURE 0x0c
  47628. +#define UR_SET_ADDRESS 0x05
  47629. +#define UR_GET_DESCRIPTOR 0x06
  47630. +#define UDESC_DEVICE 0x01
  47631. +#define UDESC_CONFIG 0x02
  47632. +#define UDESC_STRING 0x03
  47633. +#define UDESC_INTERFACE 0x04
  47634. +#define UDESC_ENDPOINT 0x05
  47635. +#define UDESC_SS_USB_COMPANION 0x30
  47636. +#define UDESC_DEVICE_QUALIFIER 0x06
  47637. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  47638. +#define UDESC_INTERFACE_POWER 0x08
  47639. +#define UDESC_OTG 0x09
  47640. +#define WUDESC_SECURITY 0x0c
  47641. +#define WUDESC_KEY 0x0d
  47642. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  47643. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  47644. +#define WUD_KEY_TYPE_ASSOC 0x01
  47645. +#define WUD_KEY_TYPE_GTK 0x02
  47646. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  47647. +#define WUD_KEY_ORIGIN_HOST 0x00
  47648. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  47649. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  47650. +#define WUDESC_BOS 0x0f
  47651. +#define WUDESC_DEVICE_CAPABILITY 0x10
  47652. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  47653. +#define UDESC_BOS 0x0f
  47654. +#define UDESC_DEVICE_CAPABILITY 0x10
  47655. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  47656. +#define UDESC_CS_CONFIG 0x22
  47657. +#define UDESC_CS_STRING 0x23
  47658. +#define UDESC_CS_INTERFACE 0x24
  47659. +#define UDESC_CS_ENDPOINT 0x25
  47660. +#define UDESC_HUB 0x29
  47661. +#define UR_SET_DESCRIPTOR 0x07
  47662. +#define UR_GET_CONFIG 0x08
  47663. +#define UR_SET_CONFIG 0x09
  47664. +#define UR_GET_INTERFACE 0x0a
  47665. +#define UR_SET_INTERFACE 0x0b
  47666. +#define UR_SYNCH_FRAME 0x0c
  47667. +#define WUR_SET_ENCRYPTION 0x0d
  47668. +#define WUR_GET_ENCRYPTION 0x0e
  47669. +#define WUR_SET_HANDSHAKE 0x0f
  47670. +#define WUR_GET_HANDSHAKE 0x10
  47671. +#define WUR_SET_CONNECTION 0x11
  47672. +#define WUR_SET_SECURITY_DATA 0x12
  47673. +#define WUR_GET_SECURITY_DATA 0x13
  47674. +#define WUR_SET_WUSB_DATA 0x14
  47675. +#define WUDATA_DRPIE_INFO 0x01
  47676. +#define WUDATA_TRANSMIT_DATA 0x02
  47677. +#define WUDATA_TRANSMIT_PARAMS 0x03
  47678. +#define WUDATA_RECEIVE_PARAMS 0x04
  47679. +#define WUDATA_TRANSMIT_POWER 0x05
  47680. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  47681. +#define WUR_LOOPBACK_DATA_READ 0x16
  47682. +#define WUR_SET_INTERFACE_DS 0x17
  47683. +
  47684. +/* Feature numbers */
  47685. +#define UF_ENDPOINT_HALT 0
  47686. +#define UF_DEVICE_REMOTE_WAKEUP 1
  47687. +#define UF_TEST_MODE 2
  47688. +#define UF_DEVICE_B_HNP_ENABLE 3
  47689. +#define UF_DEVICE_A_HNP_SUPPORT 4
  47690. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  47691. +#define WUF_WUSB 3
  47692. +#define WUF_TX_DRPIE 0x0
  47693. +#define WUF_DEV_XMIT_PACKET 0x1
  47694. +#define WUF_COUNT_PACKETS 0x2
  47695. +#define WUF_CAPTURE_PACKETS 0x3
  47696. +#define UF_FUNCTION_SUSPEND 0
  47697. +#define UF_U1_ENABLE 48
  47698. +#define UF_U2_ENABLE 49
  47699. +#define UF_LTM_ENABLE 50
  47700. +
  47701. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  47702. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  47703. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  47704. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  47705. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  47706. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  47707. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  47708. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  47709. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  47710. +
  47711. +#ifdef _MSC_VER
  47712. +#include <pshpack1.h>
  47713. +#endif
  47714. +
  47715. +typedef struct {
  47716. + uByte bLength;
  47717. + uByte bDescriptorType;
  47718. + uByte bDescriptorSubtype;
  47719. +} UPACKED usb_descriptor_t;
  47720. +
  47721. +typedef struct {
  47722. + uByte bLength;
  47723. + uByte bDescriptorType;
  47724. +} UPACKED usb_descriptor_header_t;
  47725. +
  47726. +typedef struct {
  47727. + uByte bLength;
  47728. + uByte bDescriptorType;
  47729. + uWord bcdUSB;
  47730. +#define UD_USB_2_0 0x0200
  47731. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  47732. + uByte bDeviceClass;
  47733. + uByte bDeviceSubClass;
  47734. + uByte bDeviceProtocol;
  47735. + uByte bMaxPacketSize;
  47736. + /* The fields below are not part of the initial descriptor. */
  47737. + uWord idVendor;
  47738. + uWord idProduct;
  47739. + uWord bcdDevice;
  47740. + uByte iManufacturer;
  47741. + uByte iProduct;
  47742. + uByte iSerialNumber;
  47743. + uByte bNumConfigurations;
  47744. +} UPACKED usb_device_descriptor_t;
  47745. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  47746. +
  47747. +typedef struct {
  47748. + uByte bLength;
  47749. + uByte bDescriptorType;
  47750. + uWord wTotalLength;
  47751. + uByte bNumInterface;
  47752. + uByte bConfigurationValue;
  47753. + uByte iConfiguration;
  47754. +#define UC_ATT_ONE (1 << 7) /* must be set */
  47755. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  47756. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  47757. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  47758. + uByte bmAttributes;
  47759. +#define UC_BUS_POWERED 0x80
  47760. +#define UC_SELF_POWERED 0x40
  47761. +#define UC_REMOTE_WAKEUP 0x20
  47762. + uByte bMaxPower; /* max current in 2 mA units */
  47763. +#define UC_POWER_FACTOR 2
  47764. +} UPACKED usb_config_descriptor_t;
  47765. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  47766. +
  47767. +typedef struct {
  47768. + uByte bLength;
  47769. + uByte bDescriptorType;
  47770. + uByte bInterfaceNumber;
  47771. + uByte bAlternateSetting;
  47772. + uByte bNumEndpoints;
  47773. + uByte bInterfaceClass;
  47774. + uByte bInterfaceSubClass;
  47775. + uByte bInterfaceProtocol;
  47776. + uByte iInterface;
  47777. +} UPACKED usb_interface_descriptor_t;
  47778. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  47779. +
  47780. +typedef struct {
  47781. + uByte bLength;
  47782. + uByte bDescriptorType;
  47783. + uByte bEndpointAddress;
  47784. +#define UE_GET_DIR(a) ((a) & 0x80)
  47785. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  47786. +#define UE_DIR_IN 0x80
  47787. +#define UE_DIR_OUT 0x00
  47788. +#define UE_ADDR 0x0f
  47789. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  47790. + uByte bmAttributes;
  47791. +#define UE_XFERTYPE 0x03
  47792. +#define UE_CONTROL 0x00
  47793. +#define UE_ISOCHRONOUS 0x01
  47794. +#define UE_BULK 0x02
  47795. +#define UE_INTERRUPT 0x03
  47796. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  47797. +#define UE_ISO_TYPE 0x0c
  47798. +#define UE_ISO_ASYNC 0x04
  47799. +#define UE_ISO_ADAPT 0x08
  47800. +#define UE_ISO_SYNC 0x0c
  47801. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  47802. + uWord wMaxPacketSize;
  47803. + uByte bInterval;
  47804. +} UPACKED usb_endpoint_descriptor_t;
  47805. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  47806. +
  47807. +typedef struct ss_endpoint_companion_descriptor {
  47808. + uByte bLength;
  47809. + uByte bDescriptorType;
  47810. + uByte bMaxBurst;
  47811. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  47812. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  47813. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  47814. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  47815. + uByte bmAttributes;
  47816. + uWord wBytesPerInterval;
  47817. +} UPACKED ss_endpoint_companion_descriptor_t;
  47818. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  47819. +
  47820. +typedef struct {
  47821. + uByte bLength;
  47822. + uByte bDescriptorType;
  47823. + uWord bString[127];
  47824. +} UPACKED usb_string_descriptor_t;
  47825. +#define USB_MAX_STRING_LEN 128
  47826. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  47827. +
  47828. +/* Hub specific request */
  47829. +#define UR_GET_BUS_STATE 0x02
  47830. +#define UR_CLEAR_TT_BUFFER 0x08
  47831. +#define UR_RESET_TT 0x09
  47832. +#define UR_GET_TT_STATE 0x0a
  47833. +#define UR_STOP_TT 0x0b
  47834. +
  47835. +/* Hub features */
  47836. +#define UHF_C_HUB_LOCAL_POWER 0
  47837. +#define UHF_C_HUB_OVER_CURRENT 1
  47838. +#define UHF_PORT_CONNECTION 0
  47839. +#define UHF_PORT_ENABLE 1
  47840. +#define UHF_PORT_SUSPEND 2
  47841. +#define UHF_PORT_OVER_CURRENT 3
  47842. +#define UHF_PORT_RESET 4
  47843. +#define UHF_PORT_L1 5
  47844. +#define UHF_PORT_POWER 8
  47845. +#define UHF_PORT_LOW_SPEED 9
  47846. +#define UHF_PORT_HIGH_SPEED 10
  47847. +#define UHF_C_PORT_CONNECTION 16
  47848. +#define UHF_C_PORT_ENABLE 17
  47849. +#define UHF_C_PORT_SUSPEND 18
  47850. +#define UHF_C_PORT_OVER_CURRENT 19
  47851. +#define UHF_C_PORT_RESET 20
  47852. +#define UHF_C_PORT_L1 23
  47853. +#define UHF_PORT_TEST 21
  47854. +#define UHF_PORT_INDICATOR 22
  47855. +
  47856. +typedef struct {
  47857. + uByte bDescLength;
  47858. + uByte bDescriptorType;
  47859. + uByte bNbrPorts;
  47860. + uWord wHubCharacteristics;
  47861. +#define UHD_PWR 0x0003
  47862. +#define UHD_PWR_GANGED 0x0000
  47863. +#define UHD_PWR_INDIVIDUAL 0x0001
  47864. +#define UHD_PWR_NO_SWITCH 0x0002
  47865. +#define UHD_COMPOUND 0x0004
  47866. +#define UHD_OC 0x0018
  47867. +#define UHD_OC_GLOBAL 0x0000
  47868. +#define UHD_OC_INDIVIDUAL 0x0008
  47869. +#define UHD_OC_NONE 0x0010
  47870. +#define UHD_TT_THINK 0x0060
  47871. +#define UHD_TT_THINK_8 0x0000
  47872. +#define UHD_TT_THINK_16 0x0020
  47873. +#define UHD_TT_THINK_24 0x0040
  47874. +#define UHD_TT_THINK_32 0x0060
  47875. +#define UHD_PORT_IND 0x0080
  47876. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  47877. +#define UHD_PWRON_FACTOR 2
  47878. + uByte bHubContrCurrent;
  47879. + uByte DeviceRemovable[32]; /* max 255 ports */
  47880. +#define UHD_NOT_REMOV(desc, i) \
  47881. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  47882. + /* deprecated */ uByte PortPowerCtrlMask[1];
  47883. +} UPACKED usb_hub_descriptor_t;
  47884. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  47885. +
  47886. +typedef struct {
  47887. + uByte bLength;
  47888. + uByte bDescriptorType;
  47889. + uWord bcdUSB;
  47890. + uByte bDeviceClass;
  47891. + uByte bDeviceSubClass;
  47892. + uByte bDeviceProtocol;
  47893. + uByte bMaxPacketSize0;
  47894. + uByte bNumConfigurations;
  47895. + uByte bReserved;
  47896. +} UPACKED usb_device_qualifier_t;
  47897. +#define USB_DEVICE_QUALIFIER_SIZE 10
  47898. +
  47899. +typedef struct {
  47900. + uByte bLength;
  47901. + uByte bDescriptorType;
  47902. + uByte bmAttributes;
  47903. +#define UOTG_SRP 0x01
  47904. +#define UOTG_HNP 0x02
  47905. +} UPACKED usb_otg_descriptor_t;
  47906. +
  47907. +/* OTG feature selectors */
  47908. +#define UOTG_B_HNP_ENABLE 3
  47909. +#define UOTG_A_HNP_SUPPORT 4
  47910. +#define UOTG_A_ALT_HNP_SUPPORT 5
  47911. +
  47912. +typedef struct {
  47913. + uWord wStatus;
  47914. +/* Device status flags */
  47915. +#define UDS_SELF_POWERED 0x0001
  47916. +#define UDS_REMOTE_WAKEUP 0x0002
  47917. +/* Endpoint status flags */
  47918. +#define UES_HALT 0x0001
  47919. +} UPACKED usb_status_t;
  47920. +
  47921. +typedef struct {
  47922. + uWord wHubStatus;
  47923. +#define UHS_LOCAL_POWER 0x0001
  47924. +#define UHS_OVER_CURRENT 0x0002
  47925. + uWord wHubChange;
  47926. +} UPACKED usb_hub_status_t;
  47927. +
  47928. +typedef struct {
  47929. + uWord wPortStatus;
  47930. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  47931. +#define UPS_PORT_ENABLED 0x0002
  47932. +#define UPS_SUSPEND 0x0004
  47933. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  47934. +#define UPS_RESET 0x0010
  47935. +#define UPS_PORT_POWER 0x0100
  47936. +#define UPS_LOW_SPEED 0x0200
  47937. +#define UPS_HIGH_SPEED 0x0400
  47938. +#define UPS_PORT_TEST 0x0800
  47939. +#define UPS_PORT_INDICATOR 0x1000
  47940. + uWord wPortChange;
  47941. +#define UPS_C_CONNECT_STATUS 0x0001
  47942. +#define UPS_C_PORT_ENABLED 0x0002
  47943. +#define UPS_C_SUSPEND 0x0004
  47944. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  47945. +#define UPS_C_PORT_RESET 0x0010
  47946. +} UPACKED usb_port_status_t;
  47947. +
  47948. +#ifdef _MSC_VER
  47949. +#include <poppack.h>
  47950. +#endif
  47951. +
  47952. +/* Device class codes */
  47953. +#define UDCLASS_IN_INTERFACE 0x00
  47954. +#define UDCLASS_COMM 0x02
  47955. +#define UDCLASS_HUB 0x09
  47956. +#define UDSUBCLASS_HUB 0x00
  47957. +#define UDPROTO_FSHUB 0x00
  47958. +#define UDPROTO_HSHUBSTT 0x01
  47959. +#define UDPROTO_HSHUBMTT 0x02
  47960. +#define UDCLASS_DIAGNOSTIC 0xdc
  47961. +#define UDCLASS_WIRELESS 0xe0
  47962. +#define UDSUBCLASS_RF 0x01
  47963. +#define UDPROTO_BLUETOOTH 0x01
  47964. +#define UDCLASS_VENDOR 0xff
  47965. +
  47966. +/* Interface class codes */
  47967. +#define UICLASS_UNSPEC 0x00
  47968. +
  47969. +#define UICLASS_AUDIO 0x01
  47970. +#define UISUBCLASS_AUDIOCONTROL 1
  47971. +#define UISUBCLASS_AUDIOSTREAM 2
  47972. +#define UISUBCLASS_MIDISTREAM 3
  47973. +
  47974. +#define UICLASS_CDC 0x02 /* communication */
  47975. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  47976. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  47977. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  47978. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  47979. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  47980. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  47981. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  47982. +#define UIPROTO_CDC_AT 1
  47983. +
  47984. +#define UICLASS_HID 0x03
  47985. +#define UISUBCLASS_BOOT 1
  47986. +#define UIPROTO_BOOT_KEYBOARD 1
  47987. +
  47988. +#define UICLASS_PHYSICAL 0x05
  47989. +
  47990. +#define UICLASS_IMAGE 0x06
  47991. +
  47992. +#define UICLASS_PRINTER 0x07
  47993. +#define UISUBCLASS_PRINTER 1
  47994. +#define UIPROTO_PRINTER_UNI 1
  47995. +#define UIPROTO_PRINTER_BI 2
  47996. +#define UIPROTO_PRINTER_1284 3
  47997. +
  47998. +#define UICLASS_MASS 0x08
  47999. +#define UISUBCLASS_RBC 1
  48000. +#define UISUBCLASS_SFF8020I 2
  48001. +#define UISUBCLASS_QIC157 3
  48002. +#define UISUBCLASS_UFI 4
  48003. +#define UISUBCLASS_SFF8070I 5
  48004. +#define UISUBCLASS_SCSI 6
  48005. +#define UIPROTO_MASS_CBI_I 0
  48006. +#define UIPROTO_MASS_CBI 1
  48007. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  48008. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  48009. +
  48010. +#define UICLASS_HUB 0x09
  48011. +#define UISUBCLASS_HUB 0
  48012. +#define UIPROTO_FSHUB 0
  48013. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  48014. +#define UIPROTO_HSHUBMTT 1
  48015. +
  48016. +#define UICLASS_CDC_DATA 0x0a
  48017. +#define UISUBCLASS_DATA 0
  48018. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  48019. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  48020. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  48021. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  48022. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  48023. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  48024. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  48025. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  48026. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  48027. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  48028. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  48029. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  48030. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  48031. +
  48032. +#define UICLASS_SMARTCARD 0x0b
  48033. +
  48034. +/*#define UICLASS_FIRM_UPD 0x0c*/
  48035. +
  48036. +#define UICLASS_SECURITY 0x0d
  48037. +
  48038. +#define UICLASS_DIAGNOSTIC 0xdc
  48039. +
  48040. +#define UICLASS_WIRELESS 0xe0
  48041. +#define UISUBCLASS_RF 0x01
  48042. +#define UIPROTO_BLUETOOTH 0x01
  48043. +
  48044. +#define UICLASS_APPL_SPEC 0xfe
  48045. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  48046. +#define UISUBCLASS_IRDA 2
  48047. +#define UIPROTO_IRDA 0
  48048. +
  48049. +#define UICLASS_VENDOR 0xff
  48050. +
  48051. +#define USB_HUB_MAX_DEPTH 5
  48052. +
  48053. +/*
  48054. + * Minimum time a device needs to be powered down to go through
  48055. + * a power cycle. XXX Are these time in the spec?
  48056. + */
  48057. +#define USB_POWER_DOWN_TIME 200 /* ms */
  48058. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  48059. +
  48060. +#if 0
  48061. +/* These are the values from the spec. */
  48062. +#define USB_PORT_RESET_DELAY 10 /* ms */
  48063. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  48064. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  48065. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  48066. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  48067. +#define USB_RESUME_DELAY (20*5) /* ms */
  48068. +#define USB_RESUME_WAIT 10 /* ms */
  48069. +#define USB_RESUME_RECOVERY 10 /* ms */
  48070. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  48071. +#else
  48072. +/* Allow for marginal (i.e. non-conforming) devices. */
  48073. +#define USB_PORT_RESET_DELAY 50 /* ms */
  48074. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  48075. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  48076. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  48077. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  48078. +#define USB_RESUME_DELAY (50*5) /* ms */
  48079. +#define USB_RESUME_WAIT 50 /* ms */
  48080. +#define USB_RESUME_RECOVERY 50 /* ms */
  48081. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  48082. +#endif
  48083. +
  48084. +#define USB_MIN_POWER 100 /* mA */
  48085. +#define USB_MAX_POWER 500 /* mA */
  48086. +
  48087. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  48088. +
  48089. +#define USB_UNCONFIG_NO 0
  48090. +#define USB_UNCONFIG_INDEX (-1)
  48091. +
  48092. +/*** ioctl() related stuff ***/
  48093. +
  48094. +struct usb_ctl_request {
  48095. + int ucr_addr;
  48096. + usb_device_request_t ucr_request;
  48097. + void *ucr_data;
  48098. + int ucr_flags;
  48099. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  48100. + int ucr_actlen; /* actual length transferred */
  48101. +};
  48102. +
  48103. +struct usb_alt_interface {
  48104. + int uai_config_index;
  48105. + int uai_interface_index;
  48106. + int uai_alt_no;
  48107. +};
  48108. +
  48109. +#define USB_CURRENT_CONFIG_INDEX (-1)
  48110. +#define USB_CURRENT_ALT_INDEX (-1)
  48111. +
  48112. +struct usb_config_desc {
  48113. + int ucd_config_index;
  48114. + usb_config_descriptor_t ucd_desc;
  48115. +};
  48116. +
  48117. +struct usb_interface_desc {
  48118. + int uid_config_index;
  48119. + int uid_interface_index;
  48120. + int uid_alt_index;
  48121. + usb_interface_descriptor_t uid_desc;
  48122. +};
  48123. +
  48124. +struct usb_endpoint_desc {
  48125. + int ued_config_index;
  48126. + int ued_interface_index;
  48127. + int ued_alt_index;
  48128. + int ued_endpoint_index;
  48129. + usb_endpoint_descriptor_t ued_desc;
  48130. +};
  48131. +
  48132. +struct usb_full_desc {
  48133. + int ufd_config_index;
  48134. + u_int ufd_size;
  48135. + u_char *ufd_data;
  48136. +};
  48137. +
  48138. +struct usb_string_desc {
  48139. + int usd_string_index;
  48140. + int usd_language_id;
  48141. + usb_string_descriptor_t usd_desc;
  48142. +};
  48143. +
  48144. +struct usb_ctl_report_desc {
  48145. + int ucrd_size;
  48146. + u_char ucrd_data[1024]; /* filled data size will vary */
  48147. +};
  48148. +
  48149. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  48150. +
  48151. +#define USB_MAX_DEVNAMES 4
  48152. +#define USB_MAX_DEVNAMELEN 16
  48153. +struct usb_device_info {
  48154. + u_int8_t udi_bus;
  48155. + u_int8_t udi_addr; /* device address */
  48156. + usb_event_cookie_t udi_cookie;
  48157. + char udi_product[USB_MAX_STRING_LEN];
  48158. + char udi_vendor[USB_MAX_STRING_LEN];
  48159. + char udi_release[8];
  48160. + u_int16_t udi_productNo;
  48161. + u_int16_t udi_vendorNo;
  48162. + u_int16_t udi_releaseNo;
  48163. + u_int8_t udi_class;
  48164. + u_int8_t udi_subclass;
  48165. + u_int8_t udi_protocol;
  48166. + u_int8_t udi_config;
  48167. + u_int8_t udi_speed;
  48168. +#define USB_SPEED_UNKNOWN 0
  48169. +#define USB_SPEED_LOW 1
  48170. +#define USB_SPEED_FULL 2
  48171. +#define USB_SPEED_HIGH 3
  48172. +#define USB_SPEED_VARIABLE 4
  48173. +#define USB_SPEED_SUPER 5
  48174. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  48175. + int udi_nports;
  48176. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  48177. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  48178. +#define USB_PORT_ENABLED 0xff
  48179. +#define USB_PORT_SUSPENDED 0xfe
  48180. +#define USB_PORT_POWERED 0xfd
  48181. +#define USB_PORT_DISABLED 0xfc
  48182. +};
  48183. +
  48184. +struct usb_ctl_report {
  48185. + int ucr_report;
  48186. + u_char ucr_data[1024]; /* filled data size will vary */
  48187. +};
  48188. +
  48189. +struct usb_device_stats {
  48190. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  48191. +};
  48192. +
  48193. +#define WUSB_MIN_IE 0x80
  48194. +#define WUSB_WCTA_IE 0x80
  48195. +#define WUSB_WCONNECTACK_IE 0x81
  48196. +#define WUSB_WHOSTINFO_IE 0x82
  48197. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  48198. +#define WUHI_CA_RECONN 0x00
  48199. +#define WUHI_CA_LIMITED 0x01
  48200. +#define WUHI_CA_ALL 0x03
  48201. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  48202. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  48203. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  48204. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  48205. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  48206. +#define WUSB_WWORK_IE 0x87
  48207. +#define WUSB_WCHANNEL_STOP_IE 0x88
  48208. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  48209. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  48210. +#define WUSB_WRESETDEVICE_IE 0x8B
  48211. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  48212. +#define WUSB_MAX_IE 0x8C
  48213. +
  48214. +/* Device Notification Types */
  48215. +
  48216. +#define WUSB_DN_MIN 0x01
  48217. +#define WUSB_DN_CONNECT 0x01
  48218. +# define WUSB_DA_OLDCONN 0x00
  48219. +# define WUSB_DA_NEWCONN 0x01
  48220. +# define WUSB_DA_SELF_BEACON 0x02
  48221. +# define WUSB_DA_DIR_BEACON 0x04
  48222. +# define WUSB_DA_NO_BEACON 0x06
  48223. +#define WUSB_DN_DISCONNECT 0x02
  48224. +#define WUSB_DN_EPRDY 0x03
  48225. +#define WUSB_DN_MASAVAILCHANGED 0x04
  48226. +#define WUSB_DN_REMOTEWAKEUP 0x05
  48227. +#define WUSB_DN_SLEEP 0x06
  48228. +#define WUSB_DN_ALIVE 0x07
  48229. +#define WUSB_DN_MAX 0x07
  48230. +
  48231. +#ifdef _MSC_VER
  48232. +#include <pshpack1.h>
  48233. +#endif
  48234. +
  48235. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  48236. +typedef struct wusb_hndshk_data {
  48237. + uByte bMessageNumber;
  48238. + uByte bStatus;
  48239. + uByte tTKID[3];
  48240. + uByte bReserved;
  48241. + uByte CDID[16];
  48242. + uByte Nonce[16];
  48243. + uByte MIC[8];
  48244. +} UPACKED wusb_hndshk_data_t;
  48245. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  48246. +
  48247. +/* WUSB Connection Context */
  48248. +typedef struct wusb_conn_context {
  48249. + uByte CHID [16];
  48250. + uByte CDID [16];
  48251. + uByte CK [16];
  48252. +} UPACKED wusb_conn_context_t;
  48253. +
  48254. +/* WUSB Security Descriptor */
  48255. +typedef struct wusb_security_desc {
  48256. + uByte bLength;
  48257. + uByte bDescriptorType;
  48258. + uWord wTotalLength;
  48259. + uByte bNumEncryptionTypes;
  48260. +} UPACKED wusb_security_desc_t;
  48261. +
  48262. +/* WUSB Encryption Type Descriptor */
  48263. +typedef struct wusb_encrypt_type_desc {
  48264. + uByte bLength;
  48265. + uByte bDescriptorType;
  48266. +
  48267. + uByte bEncryptionType;
  48268. +#define WUETD_UNSECURE 0
  48269. +#define WUETD_WIRED 1
  48270. +#define WUETD_CCM_1 2
  48271. +#define WUETD_RSA_1 3
  48272. +
  48273. + uByte bEncryptionValue;
  48274. + uByte bAuthKeyIndex;
  48275. +} UPACKED wusb_encrypt_type_desc_t;
  48276. +
  48277. +/* WUSB Key Descriptor */
  48278. +typedef struct wusb_key_desc {
  48279. + uByte bLength;
  48280. + uByte bDescriptorType;
  48281. + uByte tTKID[3];
  48282. + uByte bReserved;
  48283. + uByte KeyData[1]; /* variable length */
  48284. +} UPACKED wusb_key_desc_t;
  48285. +
  48286. +/* WUSB BOS Descriptor (Binary device Object Store) */
  48287. +typedef struct wusb_bos_desc {
  48288. + uByte bLength;
  48289. + uByte bDescriptorType;
  48290. + uWord wTotalLength;
  48291. + uByte bNumDeviceCaps;
  48292. +} UPACKED wusb_bos_desc_t;
  48293. +
  48294. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  48295. +typedef struct usb_dev_cap_20_ext_desc {
  48296. + uByte bLength;
  48297. + uByte bDescriptorType;
  48298. + uByte bDevCapabilityType;
  48299. +#define USB_20_EXT_LPM 0x02
  48300. + uDWord bmAttributes;
  48301. +} UPACKED usb_dev_cap_20_ext_desc_t;
  48302. +
  48303. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  48304. +typedef struct usb_dev_cap_ss_usb {
  48305. + uByte bLength;
  48306. + uByte bDescriptorType;
  48307. + uByte bDevCapabilityType;
  48308. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  48309. + uByte bmAttributes;
  48310. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  48311. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  48312. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  48313. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  48314. + uWord wSpeedsSupported;
  48315. + uByte bFunctionalitySupport;
  48316. + uByte bU1DevExitLat;
  48317. + uWord wU2DevExitLat;
  48318. +} UPACKED usb_dev_cap_ss_usb_t;
  48319. +
  48320. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  48321. +typedef struct usb_dev_cap_container_id {
  48322. + uByte bLength;
  48323. + uByte bDescriptorType;
  48324. + uByte bDevCapabilityType;
  48325. + uByte bReserved;
  48326. + uByte containerID[16];
  48327. +} UPACKED usb_dev_cap_container_id_t;
  48328. +
  48329. +/* Device Capability Type Codes */
  48330. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  48331. +
  48332. +/* Device Capability Descriptor */
  48333. +typedef struct wusb_dev_cap_desc {
  48334. + uByte bLength;
  48335. + uByte bDescriptorType;
  48336. + uByte bDevCapabilityType;
  48337. + uByte caps[1]; /* Variable length */
  48338. +} UPACKED wusb_dev_cap_desc_t;
  48339. +
  48340. +/* Device Capability Descriptor */
  48341. +typedef struct wusb_dev_cap_uwb_desc {
  48342. + uByte bLength;
  48343. + uByte bDescriptorType;
  48344. + uByte bDevCapabilityType;
  48345. + uByte bmAttributes;
  48346. + uWord wPHYRates; /* Bitmap */
  48347. + uByte bmTFITXPowerInfo;
  48348. + uByte bmFFITXPowerInfo;
  48349. + uWord bmBandGroup;
  48350. + uByte bReserved;
  48351. +} UPACKED wusb_dev_cap_uwb_desc_t;
  48352. +
  48353. +/* Wireless USB Endpoint Companion Descriptor */
  48354. +typedef struct wusb_endpoint_companion_desc {
  48355. + uByte bLength;
  48356. + uByte bDescriptorType;
  48357. + uByte bMaxBurst;
  48358. + uByte bMaxSequence;
  48359. + uWord wMaxStreamDelay;
  48360. + uWord wOverTheAirPacketSize;
  48361. + uByte bOverTheAirInterval;
  48362. + uByte bmCompAttributes;
  48363. +} UPACKED wusb_endpoint_companion_desc_t;
  48364. +
  48365. +/* Wireless USB Numeric Association M1 Data Structure */
  48366. +typedef struct wusb_m1_data {
  48367. + uByte version;
  48368. + uWord langId;
  48369. + uByte deviceFriendlyNameLength;
  48370. + uByte sha_256_m3[32];
  48371. + uByte deviceFriendlyName[256];
  48372. +} UPACKED wusb_m1_data_t;
  48373. +
  48374. +typedef struct wusb_m2_data {
  48375. + uByte version;
  48376. + uWord langId;
  48377. + uByte hostFriendlyNameLength;
  48378. + uByte pkh[384];
  48379. + uByte hostFriendlyName[256];
  48380. +} UPACKED wusb_m2_data_t;
  48381. +
  48382. +typedef struct wusb_m3_data {
  48383. + uByte pkd[384];
  48384. + uByte nd;
  48385. +} UPACKED wusb_m3_data_t;
  48386. +
  48387. +typedef struct wusb_m4_data {
  48388. + uDWord _attributeTypeIdAndLength_1;
  48389. + uWord associationTypeId;
  48390. +
  48391. + uDWord _attributeTypeIdAndLength_2;
  48392. + uWord associationSubTypeId;
  48393. +
  48394. + uDWord _attributeTypeIdAndLength_3;
  48395. + uDWord length;
  48396. +
  48397. + uDWord _attributeTypeIdAndLength_4;
  48398. + uDWord associationStatus;
  48399. +
  48400. + uDWord _attributeTypeIdAndLength_5;
  48401. + uByte chid[16];
  48402. +
  48403. + uDWord _attributeTypeIdAndLength_6;
  48404. + uByte cdid[16];
  48405. +
  48406. + uDWord _attributeTypeIdAndLength_7;
  48407. + uByte bandGroups[2];
  48408. +} UPACKED wusb_m4_data_t;
  48409. +
  48410. +#ifdef _MSC_VER
  48411. +#include <poppack.h>
  48412. +#endif
  48413. +
  48414. +#ifdef __cplusplus
  48415. +}
  48416. +#endif
  48417. +
  48418. +#endif /* _USB_H_ */
  48419. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/doc/doxygen.cfg linux-raspberry-pi/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  48420. --- linux-3.13.6/drivers/usb/host/dwc_otg/doc/doxygen.cfg 1970-01-01 01:00:00.000000000 +0100
  48421. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/doc/doxygen.cfg 2014-03-11 16:55:38.000000000 +0100
  48422. @@ -0,0 +1,224 @@
  48423. +# Doxyfile 1.3.9.1
  48424. +
  48425. +#---------------------------------------------------------------------------
  48426. +# Project related configuration options
  48427. +#---------------------------------------------------------------------------
  48428. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  48429. +PROJECT_NUMBER = v3.00a
  48430. +OUTPUT_DIRECTORY = ./doc/
  48431. +CREATE_SUBDIRS = NO
  48432. +OUTPUT_LANGUAGE = English
  48433. +BRIEF_MEMBER_DESC = YES
  48434. +REPEAT_BRIEF = YES
  48435. +ABBREVIATE_BRIEF = "The $name class" \
  48436. + "The $name widget" \
  48437. + "The $name file" \
  48438. + is \
  48439. + provides \
  48440. + specifies \
  48441. + contains \
  48442. + represents \
  48443. + a \
  48444. + an \
  48445. + the
  48446. +ALWAYS_DETAILED_SEC = NO
  48447. +INLINE_INHERITED_MEMB = NO
  48448. +FULL_PATH_NAMES = NO
  48449. +STRIP_FROM_PATH =
  48450. +STRIP_FROM_INC_PATH =
  48451. +SHORT_NAMES = NO
  48452. +JAVADOC_AUTOBRIEF = YES
  48453. +MULTILINE_CPP_IS_BRIEF = NO
  48454. +INHERIT_DOCS = YES
  48455. +DISTRIBUTE_GROUP_DOC = NO
  48456. +TAB_SIZE = 8
  48457. +ALIASES =
  48458. +OPTIMIZE_OUTPUT_FOR_C = YES
  48459. +OPTIMIZE_OUTPUT_JAVA = NO
  48460. +SUBGROUPING = YES
  48461. +#---------------------------------------------------------------------------
  48462. +# Build related configuration options
  48463. +#---------------------------------------------------------------------------
  48464. +EXTRACT_ALL = NO
  48465. +EXTRACT_PRIVATE = YES
  48466. +EXTRACT_STATIC = YES
  48467. +EXTRACT_LOCAL_CLASSES = YES
  48468. +EXTRACT_LOCAL_METHODS = NO
  48469. +HIDE_UNDOC_MEMBERS = NO
  48470. +HIDE_UNDOC_CLASSES = NO
  48471. +HIDE_FRIEND_COMPOUNDS = NO
  48472. +HIDE_IN_BODY_DOCS = NO
  48473. +INTERNAL_DOCS = NO
  48474. +CASE_SENSE_NAMES = NO
  48475. +HIDE_SCOPE_NAMES = NO
  48476. +SHOW_INCLUDE_FILES = YES
  48477. +INLINE_INFO = YES
  48478. +SORT_MEMBER_DOCS = NO
  48479. +SORT_BRIEF_DOCS = NO
  48480. +SORT_BY_SCOPE_NAME = NO
  48481. +GENERATE_TODOLIST = YES
  48482. +GENERATE_TESTLIST = YES
  48483. +GENERATE_BUGLIST = YES
  48484. +GENERATE_DEPRECATEDLIST= YES
  48485. +ENABLED_SECTIONS =
  48486. +MAX_INITIALIZER_LINES = 30
  48487. +SHOW_USED_FILES = YES
  48488. +SHOW_DIRECTORIES = YES
  48489. +#---------------------------------------------------------------------------
  48490. +# configuration options related to warning and progress messages
  48491. +#---------------------------------------------------------------------------
  48492. +QUIET = YES
  48493. +WARNINGS = YES
  48494. +WARN_IF_UNDOCUMENTED = NO
  48495. +WARN_IF_DOC_ERROR = YES
  48496. +WARN_FORMAT = "$file:$line: $text"
  48497. +WARN_LOGFILE =
  48498. +#---------------------------------------------------------------------------
  48499. +# configuration options related to the input files
  48500. +#---------------------------------------------------------------------------
  48501. +INPUT = .
  48502. +FILE_PATTERNS = *.c \
  48503. + *.h \
  48504. + ./linux/*.c \
  48505. + ./linux/*.h
  48506. +RECURSIVE = NO
  48507. +EXCLUDE = ./test/ \
  48508. + ./dwc_otg/.AppleDouble/
  48509. +EXCLUDE_SYMLINKS = YES
  48510. +EXCLUDE_PATTERNS = *.mod.*
  48511. +EXAMPLE_PATH =
  48512. +EXAMPLE_PATTERNS = *
  48513. +EXAMPLE_RECURSIVE = NO
  48514. +IMAGE_PATH =
  48515. +INPUT_FILTER =
  48516. +FILTER_PATTERNS =
  48517. +FILTER_SOURCE_FILES = NO
  48518. +#---------------------------------------------------------------------------
  48519. +# configuration options related to source browsing
  48520. +#---------------------------------------------------------------------------
  48521. +SOURCE_BROWSER = YES
  48522. +INLINE_SOURCES = NO
  48523. +STRIP_CODE_COMMENTS = YES
  48524. +REFERENCED_BY_RELATION = NO
  48525. +REFERENCES_RELATION = NO
  48526. +VERBATIM_HEADERS = NO
  48527. +#---------------------------------------------------------------------------
  48528. +# configuration options related to the alphabetical class index
  48529. +#---------------------------------------------------------------------------
  48530. +ALPHABETICAL_INDEX = NO
  48531. +COLS_IN_ALPHA_INDEX = 5
  48532. +IGNORE_PREFIX =
  48533. +#---------------------------------------------------------------------------
  48534. +# configuration options related to the HTML output
  48535. +#---------------------------------------------------------------------------
  48536. +GENERATE_HTML = YES
  48537. +HTML_OUTPUT = html
  48538. +HTML_FILE_EXTENSION = .html
  48539. +HTML_HEADER =
  48540. +HTML_FOOTER =
  48541. +HTML_STYLESHEET =
  48542. +HTML_ALIGN_MEMBERS = YES
  48543. +GENERATE_HTMLHELP = NO
  48544. +CHM_FILE =
  48545. +HHC_LOCATION =
  48546. +GENERATE_CHI = NO
  48547. +BINARY_TOC = NO
  48548. +TOC_EXPAND = NO
  48549. +DISABLE_INDEX = NO
  48550. +ENUM_VALUES_PER_LINE = 4
  48551. +GENERATE_TREEVIEW = YES
  48552. +TREEVIEW_WIDTH = 250
  48553. +#---------------------------------------------------------------------------
  48554. +# configuration options related to the LaTeX output
  48555. +#---------------------------------------------------------------------------
  48556. +GENERATE_LATEX = NO
  48557. +LATEX_OUTPUT = latex
  48558. +LATEX_CMD_NAME = latex
  48559. +MAKEINDEX_CMD_NAME = makeindex
  48560. +COMPACT_LATEX = NO
  48561. +PAPER_TYPE = a4wide
  48562. +EXTRA_PACKAGES =
  48563. +LATEX_HEADER =
  48564. +PDF_HYPERLINKS = NO
  48565. +USE_PDFLATEX = NO
  48566. +LATEX_BATCHMODE = NO
  48567. +LATEX_HIDE_INDICES = NO
  48568. +#---------------------------------------------------------------------------
  48569. +# configuration options related to the RTF output
  48570. +#---------------------------------------------------------------------------
  48571. +GENERATE_RTF = NO
  48572. +RTF_OUTPUT = rtf
  48573. +COMPACT_RTF = NO
  48574. +RTF_HYPERLINKS = NO
  48575. +RTF_STYLESHEET_FILE =
  48576. +RTF_EXTENSIONS_FILE =
  48577. +#---------------------------------------------------------------------------
  48578. +# configuration options related to the man page output
  48579. +#---------------------------------------------------------------------------
  48580. +GENERATE_MAN = NO
  48581. +MAN_OUTPUT = man
  48582. +MAN_EXTENSION = .3
  48583. +MAN_LINKS = NO
  48584. +#---------------------------------------------------------------------------
  48585. +# configuration options related to the XML output
  48586. +#---------------------------------------------------------------------------
  48587. +GENERATE_XML = NO
  48588. +XML_OUTPUT = xml
  48589. +XML_SCHEMA =
  48590. +XML_DTD =
  48591. +XML_PROGRAMLISTING = YES
  48592. +#---------------------------------------------------------------------------
  48593. +# configuration options for the AutoGen Definitions output
  48594. +#---------------------------------------------------------------------------
  48595. +GENERATE_AUTOGEN_DEF = NO
  48596. +#---------------------------------------------------------------------------
  48597. +# configuration options related to the Perl module output
  48598. +#---------------------------------------------------------------------------
  48599. +GENERATE_PERLMOD = NO
  48600. +PERLMOD_LATEX = NO
  48601. +PERLMOD_PRETTY = YES
  48602. +PERLMOD_MAKEVAR_PREFIX =
  48603. +#---------------------------------------------------------------------------
  48604. +# Configuration options related to the preprocessor
  48605. +#---------------------------------------------------------------------------
  48606. +ENABLE_PREPROCESSING = YES
  48607. +MACRO_EXPANSION = YES
  48608. +EXPAND_ONLY_PREDEF = YES
  48609. +SEARCH_INCLUDES = YES
  48610. +INCLUDE_PATH =
  48611. +INCLUDE_FILE_PATTERNS =
  48612. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  48613. +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
  48614. +SKIP_FUNCTION_MACROS = NO
  48615. +#---------------------------------------------------------------------------
  48616. +# Configuration::additions related to external references
  48617. +#---------------------------------------------------------------------------
  48618. +TAGFILES =
  48619. +GENERATE_TAGFILE =
  48620. +ALLEXTERNALS = NO
  48621. +EXTERNAL_GROUPS = YES
  48622. +PERL_PATH = /usr/bin/perl
  48623. +#---------------------------------------------------------------------------
  48624. +# Configuration options related to the dot tool
  48625. +#---------------------------------------------------------------------------
  48626. +CLASS_DIAGRAMS = YES
  48627. +HIDE_UNDOC_RELATIONS = YES
  48628. +HAVE_DOT = NO
  48629. +CLASS_GRAPH = YES
  48630. +COLLABORATION_GRAPH = YES
  48631. +UML_LOOK = NO
  48632. +TEMPLATE_RELATIONS = NO
  48633. +INCLUDE_GRAPH = YES
  48634. +INCLUDED_BY_GRAPH = YES
  48635. +CALL_GRAPH = NO
  48636. +GRAPHICAL_HIERARCHY = YES
  48637. +DOT_IMAGE_FORMAT = png
  48638. +DOT_PATH =
  48639. +DOTFILE_DIRS =
  48640. +MAX_DOT_GRAPH_DEPTH = 1000
  48641. +GENERATE_LEGEND = YES
  48642. +DOT_CLEANUP = YES
  48643. +#---------------------------------------------------------------------------
  48644. +# Configuration::additions related to the search engine
  48645. +#---------------------------------------------------------------------------
  48646. +SEARCHENGINE = NO
  48647. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dummy_audio.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dummy_audio.c
  48648. --- linux-3.13.6/drivers/usb/host/dwc_otg/dummy_audio.c 1970-01-01 01:00:00.000000000 +0100
  48649. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dummy_audio.c 2014-03-11 16:55:38.000000000 +0100
  48650. @@ -0,0 +1,1575 @@
  48651. +/*
  48652. + * zero.c -- Gadget Zero, for USB development
  48653. + *
  48654. + * Copyright (C) 2003-2004 David Brownell
  48655. + * All rights reserved.
  48656. + *
  48657. + * Redistribution and use in source and binary forms, with or without
  48658. + * modification, are permitted provided that the following conditions
  48659. + * are met:
  48660. + * 1. Redistributions of source code must retain the above copyright
  48661. + * notice, this list of conditions, and the following disclaimer,
  48662. + * without modification.
  48663. + * 2. Redistributions in binary form must reproduce the above copyright
  48664. + * notice, this list of conditions and the following disclaimer in the
  48665. + * documentation and/or other materials provided with the distribution.
  48666. + * 3. The names of the above-listed copyright holders may not be used
  48667. + * to endorse or promote products derived from this software without
  48668. + * specific prior written permission.
  48669. + *
  48670. + * ALTERNATIVELY, this software may be distributed under the terms of the
  48671. + * GNU General Public License ("GPL") as published by the Free Software
  48672. + * Foundation, either version 2 of that License or (at your option) any
  48673. + * later version.
  48674. + *
  48675. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  48676. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  48677. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48678. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  48679. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  48680. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  48681. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  48682. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  48683. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  48684. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  48685. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48686. + */
  48687. +
  48688. +
  48689. +/*
  48690. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  48691. + * can write a hardware-agnostic gadget driver running inside a USB device.
  48692. + *
  48693. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  48694. + * affect most of the driver.
  48695. + *
  48696. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  48697. + * functional test of your device-side usb stack, or with "usb-skeleton".
  48698. + *
  48699. + * It supports two similar configurations. One sinks whatever the usb host
  48700. + * writes, and in return sources zeroes. The other loops whatever the host
  48701. + * writes back, so the host can read it. Module options include:
  48702. + *
  48703. + * buflen=N default N=4096, buffer size used
  48704. + * qlen=N default N=32, how many buffers in the loopback queue
  48705. + * loopdefault default false, list loopback config first
  48706. + *
  48707. + * Many drivers will only have one configuration, letting them be much
  48708. + * simpler if they also don't support high speed operation (like this
  48709. + * driver does).
  48710. + */
  48711. +
  48712. +#include <linux/config.h>
  48713. +#include <linux/module.h>
  48714. +#include <linux/kernel.h>
  48715. +#include <linux/delay.h>
  48716. +#include <linux/ioport.h>
  48717. +#include <linux/sched.h>
  48718. +#include <linux/slab.h>
  48719. +#include <linux/smp_lock.h>
  48720. +#include <linux/errno.h>
  48721. +#include <linux/init.h>
  48722. +#include <linux/timer.h>
  48723. +#include <linux/list.h>
  48724. +#include <linux/interrupt.h>
  48725. +#include <linux/uts.h>
  48726. +#include <linux/version.h>
  48727. +#include <linux/device.h>
  48728. +#include <linux/moduleparam.h>
  48729. +#include <linux/proc_fs.h>
  48730. +
  48731. +#include <asm/byteorder.h>
  48732. +#include <asm/io.h>
  48733. +#include <asm/irq.h>
  48734. +#include <asm/system.h>
  48735. +#include <asm/unaligned.h>
  48736. +
  48737. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  48738. +# include <linux/usb/ch9.h>
  48739. +#else
  48740. +# include <linux/usb_ch9.h>
  48741. +#endif
  48742. +
  48743. +#include <linux/usb_gadget.h>
  48744. +
  48745. +
  48746. +/*-------------------------------------------------------------------------*/
  48747. +/*-------------------------------------------------------------------------*/
  48748. +
  48749. +
  48750. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  48751. +{
  48752. + int count = 0;
  48753. + u8 c;
  48754. + u16 uchar;
  48755. +
  48756. + /* this insists on correct encodings, though not minimal ones.
  48757. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  48758. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  48759. + */
  48760. + while (len != 0 && (c = (u8) *s++) != 0) {
  48761. + if (unlikely(c & 0x80)) {
  48762. + // 2-byte sequence:
  48763. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  48764. + if ((c & 0xe0) == 0xc0) {
  48765. + uchar = (c & 0x1f) << 6;
  48766. +
  48767. + c = (u8) *s++;
  48768. + if ((c & 0xc0) != 0xc0)
  48769. + goto fail;
  48770. + c &= 0x3f;
  48771. + uchar |= c;
  48772. +
  48773. + // 3-byte sequence (most CJKV characters):
  48774. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  48775. + } else if ((c & 0xf0) == 0xe0) {
  48776. + uchar = (c & 0x0f) << 12;
  48777. +
  48778. + c = (u8) *s++;
  48779. + if ((c & 0xc0) != 0xc0)
  48780. + goto fail;
  48781. + c &= 0x3f;
  48782. + uchar |= c << 6;
  48783. +
  48784. + c = (u8) *s++;
  48785. + if ((c & 0xc0) != 0xc0)
  48786. + goto fail;
  48787. + c &= 0x3f;
  48788. + uchar |= c;
  48789. +
  48790. + /* no bogus surrogates */
  48791. + if (0xd800 <= uchar && uchar <= 0xdfff)
  48792. + goto fail;
  48793. +
  48794. + // 4-byte sequence (surrogate pairs, currently rare):
  48795. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  48796. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  48797. + // (uuuuu = wwww + 1)
  48798. + // FIXME accept the surrogate code points (only)
  48799. +
  48800. + } else
  48801. + goto fail;
  48802. + } else
  48803. + uchar = c;
  48804. + put_unaligned (cpu_to_le16 (uchar), cp++);
  48805. + count++;
  48806. + len--;
  48807. + }
  48808. + return count;
  48809. +fail:
  48810. + return -1;
  48811. +}
  48812. +
  48813. +
  48814. +/**
  48815. + * usb_gadget_get_string - fill out a string descriptor
  48816. + * @table: of c strings encoded using UTF-8
  48817. + * @id: string id, from low byte of wValue in get string descriptor
  48818. + * @buf: at least 256 bytes
  48819. + *
  48820. + * Finds the UTF-8 string matching the ID, and converts it into a
  48821. + * string descriptor in utf16-le.
  48822. + * Returns length of descriptor (always even) or negative errno
  48823. + *
  48824. + * If your driver needs stings in multiple languages, you'll probably
  48825. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  48826. + * using this routine after choosing which set of UTF-8 strings to use.
  48827. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  48828. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  48829. + * characters (which are also widely used in C strings).
  48830. + */
  48831. +int
  48832. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  48833. +{
  48834. + struct usb_string *s;
  48835. + int len;
  48836. +
  48837. + /* descriptor 0 has the language id */
  48838. + if (id == 0) {
  48839. + buf [0] = 4;
  48840. + buf [1] = USB_DT_STRING;
  48841. + buf [2] = (u8) table->language;
  48842. + buf [3] = (u8) (table->language >> 8);
  48843. + return 4;
  48844. + }
  48845. + for (s = table->strings; s && s->s; s++)
  48846. + if (s->id == id)
  48847. + break;
  48848. +
  48849. + /* unrecognized: stall. */
  48850. + if (!s || !s->s)
  48851. + return -EINVAL;
  48852. +
  48853. + /* string descriptors have length, tag, then UTF16-LE text */
  48854. + len = min ((size_t) 126, strlen (s->s));
  48855. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  48856. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  48857. + if (len < 0)
  48858. + return -EINVAL;
  48859. + buf [0] = (len + 1) * 2;
  48860. + buf [1] = USB_DT_STRING;
  48861. + return buf [0];
  48862. +}
  48863. +
  48864. +
  48865. +/*-------------------------------------------------------------------------*/
  48866. +/*-------------------------------------------------------------------------*/
  48867. +
  48868. +
  48869. +/**
  48870. + * usb_descriptor_fillbuf - fill buffer with descriptors
  48871. + * @buf: Buffer to be filled
  48872. + * @buflen: Size of buf
  48873. + * @src: Array of descriptor pointers, terminated by null pointer.
  48874. + *
  48875. + * Copies descriptors into the buffer, returning the length or a
  48876. + * negative error code if they can't all be copied. Useful when
  48877. + * assembling descriptors for an associated set of interfaces used
  48878. + * as part of configuring a composite device; or in other cases where
  48879. + * sets of descriptors need to be marshaled.
  48880. + */
  48881. +int
  48882. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  48883. + const struct usb_descriptor_header **src)
  48884. +{
  48885. + u8 *dest = buf;
  48886. +
  48887. + if (!src)
  48888. + return -EINVAL;
  48889. +
  48890. + /* fill buffer from src[] until null descriptor ptr */
  48891. + for (; 0 != *src; src++) {
  48892. + unsigned len = (*src)->bLength;
  48893. +
  48894. + if (len > buflen)
  48895. + return -EINVAL;
  48896. + memcpy(dest, *src, len);
  48897. + buflen -= len;
  48898. + dest += len;
  48899. + }
  48900. + return dest - (u8 *)buf;
  48901. +}
  48902. +
  48903. +
  48904. +/**
  48905. + * usb_gadget_config_buf - builts a complete configuration descriptor
  48906. + * @config: Header for the descriptor, including characteristics such
  48907. + * as power requirements and number of interfaces.
  48908. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  48909. + * endpoint, etc) defining all functions in this device configuration.
  48910. + * @buf: Buffer for the resulting configuration descriptor.
  48911. + * @length: Length of buffer. If this is not big enough to hold the
  48912. + * entire configuration descriptor, an error code will be returned.
  48913. + *
  48914. + * This copies descriptors into the response buffer, building a descriptor
  48915. + * for that configuration. It returns the buffer length or a negative
  48916. + * status code. The config.wTotalLength field is set to match the length
  48917. + * of the result, but other descriptor fields (including power usage and
  48918. + * interface count) must be set by the caller.
  48919. + *
  48920. + * Gadget drivers could use this when constructing a config descriptor
  48921. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  48922. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  48923. + */
  48924. +int usb_gadget_config_buf(
  48925. + const struct usb_config_descriptor *config,
  48926. + void *buf,
  48927. + unsigned length,
  48928. + const struct usb_descriptor_header **desc
  48929. +)
  48930. +{
  48931. + struct usb_config_descriptor *cp = buf;
  48932. + int len;
  48933. +
  48934. + /* config descriptor first */
  48935. + if (length < USB_DT_CONFIG_SIZE || !desc)
  48936. + return -EINVAL;
  48937. + *cp = *config;
  48938. +
  48939. + /* then interface/endpoint/class/vendor/... */
  48940. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  48941. + length - USB_DT_CONFIG_SIZE, desc);
  48942. + if (len < 0)
  48943. + return len;
  48944. + len += USB_DT_CONFIG_SIZE;
  48945. + if (len > 0xffff)
  48946. + return -EINVAL;
  48947. +
  48948. + /* patch up the config descriptor */
  48949. + cp->bLength = USB_DT_CONFIG_SIZE;
  48950. + cp->bDescriptorType = USB_DT_CONFIG;
  48951. + cp->wTotalLength = cpu_to_le16(len);
  48952. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  48953. + return len;
  48954. +}
  48955. +
  48956. +/*-------------------------------------------------------------------------*/
  48957. +/*-------------------------------------------------------------------------*/
  48958. +
  48959. +
  48960. +#define RBUF_LEN (1024*1024)
  48961. +static int rbuf_start;
  48962. +static int rbuf_len;
  48963. +static __u8 rbuf[RBUF_LEN];
  48964. +
  48965. +/*-------------------------------------------------------------------------*/
  48966. +
  48967. +#define DRIVER_VERSION "St Patrick's Day 2004"
  48968. +
  48969. +static const char shortname [] = "zero";
  48970. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  48971. +
  48972. +static const char source_sink [] = "source and sink data";
  48973. +static const char loopback [] = "loop input to output";
  48974. +
  48975. +/*-------------------------------------------------------------------------*/
  48976. +
  48977. +/*
  48978. + * driver assumes self-powered hardware, and
  48979. + * has no way for users to trigger remote wakeup.
  48980. + *
  48981. + * this version autoconfigures as much as possible,
  48982. + * which is reasonable for most "bulk-only" drivers.
  48983. + */
  48984. +static const char *EP_IN_NAME; /* source */
  48985. +static const char *EP_OUT_NAME; /* sink */
  48986. +
  48987. +/*-------------------------------------------------------------------------*/
  48988. +
  48989. +/* big enough to hold our biggest descriptor */
  48990. +#define USB_BUFSIZ 512
  48991. +
  48992. +struct zero_dev {
  48993. + spinlock_t lock;
  48994. + struct usb_gadget *gadget;
  48995. + struct usb_request *req; /* for control responses */
  48996. +
  48997. + /* when configured, we have one of two configs:
  48998. + * - source data (in to host) and sink it (out from host)
  48999. + * - or loop it back (out from host back in to host)
  49000. + */
  49001. + u8 config;
  49002. + struct usb_ep *in_ep, *out_ep;
  49003. +
  49004. + /* autoresume timer */
  49005. + struct timer_list resume;
  49006. +};
  49007. +
  49008. +#define xprintk(d,level,fmt,args...) \
  49009. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  49010. +
  49011. +#ifdef DEBUG
  49012. +#define DBG(dev,fmt,args...) \
  49013. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  49014. +#else
  49015. +#define DBG(dev,fmt,args...) \
  49016. + do { } while (0)
  49017. +#endif /* DEBUG */
  49018. +
  49019. +#ifdef VERBOSE
  49020. +#define VDBG DBG
  49021. +#else
  49022. +#define VDBG(dev,fmt,args...) \
  49023. + do { } while (0)
  49024. +#endif /* VERBOSE */
  49025. +
  49026. +#define ERROR(dev,fmt,args...) \
  49027. + xprintk(dev , KERN_ERR , fmt , ## args)
  49028. +#define WARN(dev,fmt,args...) \
  49029. + xprintk(dev , KERN_WARNING , fmt , ## args)
  49030. +#define INFO(dev,fmt,args...) \
  49031. + xprintk(dev , KERN_INFO , fmt , ## args)
  49032. +
  49033. +/*-------------------------------------------------------------------------*/
  49034. +
  49035. +static unsigned buflen = 4096;
  49036. +static unsigned qlen = 32;
  49037. +static unsigned pattern = 0;
  49038. +
  49039. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  49040. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  49041. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  49042. +
  49043. +/*
  49044. + * if it's nonzero, autoresume says how many seconds to wait
  49045. + * before trying to wake up the host after suspend.
  49046. + */
  49047. +static unsigned autoresume = 0;
  49048. +module_param (autoresume, uint, 0);
  49049. +
  49050. +/*
  49051. + * Normally the "loopback" configuration is second (index 1) so
  49052. + * it's not the default. Here's where to change that order, to
  49053. + * work better with hosts where config changes are problematic.
  49054. + * Or controllers (like superh) that only support one config.
  49055. + */
  49056. +static int loopdefault = 0;
  49057. +
  49058. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  49059. +
  49060. +/*-------------------------------------------------------------------------*/
  49061. +
  49062. +/* Thanks to NetChip Technologies for donating this product ID.
  49063. + *
  49064. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  49065. + * Instead: allocate your own, using normal USB-IF procedures.
  49066. + */
  49067. +#ifndef CONFIG_USB_ZERO_HNPTEST
  49068. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  49069. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  49070. +#else
  49071. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  49072. +#define DRIVER_PRODUCT_NUM 0xbadd
  49073. +#endif
  49074. +
  49075. +/*-------------------------------------------------------------------------*/
  49076. +
  49077. +/*
  49078. + * DESCRIPTORS ... most are static, but strings and (full)
  49079. + * configuration descriptors are built on demand.
  49080. + */
  49081. +
  49082. +/*
  49083. +#define STRING_MANUFACTURER 25
  49084. +#define STRING_PRODUCT 42
  49085. +#define STRING_SERIAL 101
  49086. +*/
  49087. +#define STRING_MANUFACTURER 1
  49088. +#define STRING_PRODUCT 2
  49089. +#define STRING_SERIAL 3
  49090. +
  49091. +#define STRING_SOURCE_SINK 250
  49092. +#define STRING_LOOPBACK 251
  49093. +
  49094. +/*
  49095. + * This device advertises two configurations; these numbers work
  49096. + * on a pxa250 as well as more flexible hardware.
  49097. + */
  49098. +#define CONFIG_SOURCE_SINK 3
  49099. +#define CONFIG_LOOPBACK 2
  49100. +
  49101. +/*
  49102. +static struct usb_device_descriptor
  49103. +device_desc = {
  49104. + .bLength = sizeof device_desc,
  49105. + .bDescriptorType = USB_DT_DEVICE,
  49106. +
  49107. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  49108. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  49109. +
  49110. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  49111. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  49112. + .iManufacturer = STRING_MANUFACTURER,
  49113. + .iProduct = STRING_PRODUCT,
  49114. + .iSerialNumber = STRING_SERIAL,
  49115. + .bNumConfigurations = 2,
  49116. +};
  49117. +*/
  49118. +static struct usb_device_descriptor
  49119. +device_desc = {
  49120. + .bLength = sizeof device_desc,
  49121. + .bDescriptorType = USB_DT_DEVICE,
  49122. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  49123. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  49124. + .bDeviceSubClass = 0,
  49125. + .bDeviceProtocol = 0,
  49126. + .bMaxPacketSize0 = 64,
  49127. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  49128. + .idVendor = __constant_cpu_to_le16 (0x0499),
  49129. + .idProduct = __constant_cpu_to_le16 (0x3002),
  49130. + .iManufacturer = STRING_MANUFACTURER,
  49131. + .iProduct = STRING_PRODUCT,
  49132. + .iSerialNumber = STRING_SERIAL,
  49133. + .bNumConfigurations = 1,
  49134. +};
  49135. +
  49136. +static struct usb_config_descriptor
  49137. +z_config = {
  49138. + .bLength = sizeof z_config,
  49139. + .bDescriptorType = USB_DT_CONFIG,
  49140. +
  49141. + /* compute wTotalLength on the fly */
  49142. + .bNumInterfaces = 2,
  49143. + .bConfigurationValue = 1,
  49144. + .iConfiguration = 0,
  49145. + .bmAttributes = 0x40,
  49146. + .bMaxPower = 0, /* self-powered */
  49147. +};
  49148. +
  49149. +
  49150. +static struct usb_otg_descriptor
  49151. +otg_descriptor = {
  49152. + .bLength = sizeof otg_descriptor,
  49153. + .bDescriptorType = USB_DT_OTG,
  49154. +
  49155. + .bmAttributes = USB_OTG_SRP,
  49156. +};
  49157. +
  49158. +/* one interface in each configuration */
  49159. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  49160. +
  49161. +/*
  49162. + * usb 2.0 devices need to expose both high speed and full speed
  49163. + * descriptors, unless they only run at full speed.
  49164. + *
  49165. + * that means alternate endpoint descriptors (bigger packets)
  49166. + * and a "device qualifier" ... plus more construction options
  49167. + * for the config descriptor.
  49168. + */
  49169. +
  49170. +static struct usb_qualifier_descriptor
  49171. +dev_qualifier = {
  49172. + .bLength = sizeof dev_qualifier,
  49173. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  49174. +
  49175. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  49176. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  49177. +
  49178. + .bNumConfigurations = 2,
  49179. +};
  49180. +
  49181. +
  49182. +struct usb_cs_as_general_descriptor {
  49183. + __u8 bLength;
  49184. + __u8 bDescriptorType;
  49185. +
  49186. + __u8 bDescriptorSubType;
  49187. + __u8 bTerminalLink;
  49188. + __u8 bDelay;
  49189. + __u16 wFormatTag;
  49190. +} __attribute__ ((packed));
  49191. +
  49192. +struct usb_cs_as_format_descriptor {
  49193. + __u8 bLength;
  49194. + __u8 bDescriptorType;
  49195. +
  49196. + __u8 bDescriptorSubType;
  49197. + __u8 bFormatType;
  49198. + __u8 bNrChannels;
  49199. + __u8 bSubframeSize;
  49200. + __u8 bBitResolution;
  49201. + __u8 bSamfreqType;
  49202. + __u8 tLowerSamFreq[3];
  49203. + __u8 tUpperSamFreq[3];
  49204. +} __attribute__ ((packed));
  49205. +
  49206. +static const struct usb_interface_descriptor
  49207. +z_audio_control_if_desc = {
  49208. + .bLength = sizeof z_audio_control_if_desc,
  49209. + .bDescriptorType = USB_DT_INTERFACE,
  49210. + .bInterfaceNumber = 0,
  49211. + .bAlternateSetting = 0,
  49212. + .bNumEndpoints = 0,
  49213. + .bInterfaceClass = USB_CLASS_AUDIO,
  49214. + .bInterfaceSubClass = 0x1,
  49215. + .bInterfaceProtocol = 0,
  49216. + .iInterface = 0,
  49217. +};
  49218. +
  49219. +static const struct usb_interface_descriptor
  49220. +z_audio_if_desc = {
  49221. + .bLength = sizeof z_audio_if_desc,
  49222. + .bDescriptorType = USB_DT_INTERFACE,
  49223. + .bInterfaceNumber = 1,
  49224. + .bAlternateSetting = 0,
  49225. + .bNumEndpoints = 0,
  49226. + .bInterfaceClass = USB_CLASS_AUDIO,
  49227. + .bInterfaceSubClass = 0x2,
  49228. + .bInterfaceProtocol = 0,
  49229. + .iInterface = 0,
  49230. +};
  49231. +
  49232. +static const struct usb_interface_descriptor
  49233. +z_audio_if_desc2 = {
  49234. + .bLength = sizeof z_audio_if_desc,
  49235. + .bDescriptorType = USB_DT_INTERFACE,
  49236. + .bInterfaceNumber = 1,
  49237. + .bAlternateSetting = 1,
  49238. + .bNumEndpoints = 1,
  49239. + .bInterfaceClass = USB_CLASS_AUDIO,
  49240. + .bInterfaceSubClass = 0x2,
  49241. + .bInterfaceProtocol = 0,
  49242. + .iInterface = 0,
  49243. +};
  49244. +
  49245. +static const struct usb_cs_as_general_descriptor
  49246. +z_audio_cs_as_if_desc = {
  49247. + .bLength = 7,
  49248. + .bDescriptorType = 0x24,
  49249. +
  49250. + .bDescriptorSubType = 0x01,
  49251. + .bTerminalLink = 0x01,
  49252. + .bDelay = 0x0,
  49253. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  49254. +};
  49255. +
  49256. +
  49257. +static const struct usb_cs_as_format_descriptor
  49258. +z_audio_cs_as_format_desc = {
  49259. + .bLength = 0xe,
  49260. + .bDescriptorType = 0x24,
  49261. +
  49262. + .bDescriptorSubType = 2,
  49263. + .bFormatType = 1,
  49264. + .bNrChannels = 1,
  49265. + .bSubframeSize = 1,
  49266. + .bBitResolution = 8,
  49267. + .bSamfreqType = 0,
  49268. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  49269. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  49270. +};
  49271. +
  49272. +static const struct usb_endpoint_descriptor
  49273. +z_iso_ep = {
  49274. + .bLength = 0x09,
  49275. + .bDescriptorType = 0x05,
  49276. + .bEndpointAddress = 0x04,
  49277. + .bmAttributes = 0x09,
  49278. + .wMaxPacketSize = 0x0038,
  49279. + .bInterval = 0x01,
  49280. + .bRefresh = 0x00,
  49281. + .bSynchAddress = 0x00,
  49282. +};
  49283. +
  49284. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49285. +
  49286. +// 9 bytes
  49287. +static char z_ac_interface_header_desc[] =
  49288. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  49289. +
  49290. +// 12 bytes
  49291. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  49292. + 0x03, 0x00, 0x00, 0x00};
  49293. +// 13 bytes
  49294. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  49295. + 0x02, 0x00, 0x02, 0x00, 0x00};
  49296. +// 9 bytes
  49297. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  49298. + 0x00};
  49299. +
  49300. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  49301. + 0x00};
  49302. +
  49303. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49304. +
  49305. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  49306. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49307. +
  49308. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  49309. + 0x00};
  49310. +
  49311. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49312. +
  49313. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  49314. + 0x00};
  49315. +
  49316. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49317. +
  49318. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  49319. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49320. +
  49321. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  49322. + 0x00};
  49323. +
  49324. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49325. +
  49326. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  49327. + 0x00};
  49328. +
  49329. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49330. +
  49331. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  49332. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49333. +
  49334. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  49335. + 0x00};
  49336. +
  49337. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49338. +
  49339. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  49340. + 0x00};
  49341. +
  49342. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49343. +
  49344. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  49345. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49346. +
  49347. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  49348. + 0x00};
  49349. +
  49350. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49351. +
  49352. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  49353. + 0x00};
  49354. +
  49355. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  49356. +
  49357. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  49358. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  49359. +
  49360. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  49361. + 0x00};
  49362. +
  49363. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  49364. +
  49365. +
  49366. +
  49367. +static const struct usb_descriptor_header *z_function [] = {
  49368. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  49369. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  49370. + (struct usb_descriptor_header *) &z_0,
  49371. + (struct usb_descriptor_header *) &z_1,
  49372. + (struct usb_descriptor_header *) &z_2,
  49373. + (struct usb_descriptor_header *) &z_audio_if_desc,
  49374. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  49375. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  49376. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  49377. + (struct usb_descriptor_header *) &z_iso_ep,
  49378. + (struct usb_descriptor_header *) &z_iso_ep2,
  49379. + (struct usb_descriptor_header *) &za_0,
  49380. + (struct usb_descriptor_header *) &za_1,
  49381. + (struct usb_descriptor_header *) &za_2,
  49382. + (struct usb_descriptor_header *) &za_3,
  49383. + (struct usb_descriptor_header *) &za_4,
  49384. + (struct usb_descriptor_header *) &za_5,
  49385. + (struct usb_descriptor_header *) &za_6,
  49386. + (struct usb_descriptor_header *) &za_7,
  49387. + (struct usb_descriptor_header *) &za_8,
  49388. + (struct usb_descriptor_header *) &za_9,
  49389. + (struct usb_descriptor_header *) &za_10,
  49390. + (struct usb_descriptor_header *) &za_11,
  49391. + (struct usb_descriptor_header *) &za_12,
  49392. + (struct usb_descriptor_header *) &za_13,
  49393. + (struct usb_descriptor_header *) &za_14,
  49394. + (struct usb_descriptor_header *) &za_15,
  49395. + (struct usb_descriptor_header *) &za_16,
  49396. + (struct usb_descriptor_header *) &za_17,
  49397. + (struct usb_descriptor_header *) &za_18,
  49398. + (struct usb_descriptor_header *) &za_19,
  49399. + (struct usb_descriptor_header *) &za_20,
  49400. + (struct usb_descriptor_header *) &za_21,
  49401. + (struct usb_descriptor_header *) &za_22,
  49402. + (struct usb_descriptor_header *) &za_23,
  49403. + (struct usb_descriptor_header *) &za_24,
  49404. + NULL,
  49405. +};
  49406. +
  49407. +/* maxpacket and other transfer characteristics vary by speed. */
  49408. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  49409. +
  49410. +#else
  49411. +
  49412. +/* if there's no high speed support, maxpacket doesn't change. */
  49413. +#define ep_desc(g,hs,fs) fs
  49414. +
  49415. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  49416. +
  49417. +static char manufacturer [40];
  49418. +//static char serial [40];
  49419. +static char serial [] = "Ser 00 em";
  49420. +
  49421. +/* static strings, in UTF-8 */
  49422. +static struct usb_string strings [] = {
  49423. + { STRING_MANUFACTURER, manufacturer, },
  49424. + { STRING_PRODUCT, longname, },
  49425. + { STRING_SERIAL, serial, },
  49426. + { STRING_LOOPBACK, loopback, },
  49427. + { STRING_SOURCE_SINK, source_sink, },
  49428. + { } /* end of list */
  49429. +};
  49430. +
  49431. +static struct usb_gadget_strings stringtab = {
  49432. + .language = 0x0409, /* en-us */
  49433. + .strings = strings,
  49434. +};
  49435. +
  49436. +/*
  49437. + * config descriptors are also handcrafted. these must agree with code
  49438. + * that sets configurations, and with code managing interfaces and their
  49439. + * altsettings. other complexity may come from:
  49440. + *
  49441. + * - high speed support, including "other speed config" rules
  49442. + * - multiple configurations
  49443. + * - interfaces with alternate settings
  49444. + * - embedded class or vendor-specific descriptors
  49445. + *
  49446. + * this handles high speed, and has a second config that could as easily
  49447. + * have been an alternate interface setting (on most hardware).
  49448. + *
  49449. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  49450. + * should include an altsetting to test interrupt transfers, including
  49451. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  49452. + * device?)
  49453. + */
  49454. +static int
  49455. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  49456. +{
  49457. + int len;
  49458. + const struct usb_descriptor_header **function;
  49459. +
  49460. + function = z_function;
  49461. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  49462. + if (len < 0)
  49463. + return len;
  49464. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  49465. + return len;
  49466. +}
  49467. +
  49468. +/*-------------------------------------------------------------------------*/
  49469. +
  49470. +static struct usb_request *
  49471. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  49472. +{
  49473. + struct usb_request *req;
  49474. +
  49475. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  49476. + if (req) {
  49477. + req->length = length;
  49478. + req->buf = usb_ep_alloc_buffer (ep, length,
  49479. + &req->dma, GFP_ATOMIC);
  49480. + if (!req->buf) {
  49481. + usb_ep_free_request (ep, req);
  49482. + req = NULL;
  49483. + }
  49484. + }
  49485. + return req;
  49486. +}
  49487. +
  49488. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  49489. +{
  49490. + if (req->buf)
  49491. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  49492. + usb_ep_free_request (ep, req);
  49493. +}
  49494. +
  49495. +/*-------------------------------------------------------------------------*/
  49496. +
  49497. +/* optionally require specific source/sink data patterns */
  49498. +
  49499. +static int
  49500. +check_read_data (
  49501. + struct zero_dev *dev,
  49502. + struct usb_ep *ep,
  49503. + struct usb_request *req
  49504. +)
  49505. +{
  49506. + unsigned i;
  49507. + u8 *buf = req->buf;
  49508. +
  49509. + for (i = 0; i < req->actual; i++, buf++) {
  49510. + switch (pattern) {
  49511. + /* all-zeroes has no synchronization issues */
  49512. + case 0:
  49513. + if (*buf == 0)
  49514. + continue;
  49515. + break;
  49516. + /* mod63 stays in sync with short-terminated transfers,
  49517. + * or otherwise when host and gadget agree on how large
  49518. + * each usb transfer request should be. resync is done
  49519. + * with set_interface or set_config.
  49520. + */
  49521. + case 1:
  49522. + if (*buf == (u8)(i % 63))
  49523. + continue;
  49524. + break;
  49525. + }
  49526. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  49527. + usb_ep_set_halt (ep);
  49528. + return -EINVAL;
  49529. + }
  49530. + return 0;
  49531. +}
  49532. +
  49533. +/*-------------------------------------------------------------------------*/
  49534. +
  49535. +static void zero_reset_config (struct zero_dev *dev)
  49536. +{
  49537. + if (dev->config == 0)
  49538. + return;
  49539. +
  49540. + DBG (dev, "reset config\n");
  49541. +
  49542. + /* just disable endpoints, forcing completion of pending i/o.
  49543. + * all our completion handlers free their requests in this case.
  49544. + */
  49545. + if (dev->in_ep) {
  49546. + usb_ep_disable (dev->in_ep);
  49547. + dev->in_ep = NULL;
  49548. + }
  49549. + if (dev->out_ep) {
  49550. + usb_ep_disable (dev->out_ep);
  49551. + dev->out_ep = NULL;
  49552. + }
  49553. + dev->config = 0;
  49554. + del_timer (&dev->resume);
  49555. +}
  49556. +
  49557. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  49558. +
  49559. +static void
  49560. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  49561. +{
  49562. + struct zero_dev *dev = ep->driver_data;
  49563. + int status = req->status;
  49564. + int i, j;
  49565. +
  49566. + switch (status) {
  49567. +
  49568. + case 0: /* normal completion? */
  49569. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  49570. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  49571. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  49572. + rbuf[j] = ((__u8*)req->buf)[i];
  49573. + j++;
  49574. + if (j >= RBUF_LEN) j=0;
  49575. + }
  49576. + rbuf_start = j;
  49577. + //printk ("\n\n");
  49578. +
  49579. + if (rbuf_len < RBUF_LEN) {
  49580. + rbuf_len += req->actual;
  49581. + if (rbuf_len > RBUF_LEN) {
  49582. + rbuf_len = RBUF_LEN;
  49583. + }
  49584. + }
  49585. +
  49586. + break;
  49587. +
  49588. + /* this endpoint is normally active while we're configured */
  49589. + case -ECONNABORTED: /* hardware forced ep reset */
  49590. + case -ECONNRESET: /* request dequeued */
  49591. + case -ESHUTDOWN: /* disconnect from host */
  49592. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  49593. + req->actual, req->length);
  49594. + if (ep == dev->out_ep)
  49595. + check_read_data (dev, ep, req);
  49596. + free_ep_req (ep, req);
  49597. + return;
  49598. +
  49599. + case -EOVERFLOW: /* buffer overrun on read means that
  49600. + * we didn't provide a big enough
  49601. + * buffer.
  49602. + */
  49603. + default:
  49604. +#if 1
  49605. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  49606. + status, req->actual, req->length);
  49607. +#endif
  49608. + case -EREMOTEIO: /* short read */
  49609. + break;
  49610. + }
  49611. +
  49612. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  49613. + if (status) {
  49614. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  49615. + ep->name, req->length, status);
  49616. + usb_ep_set_halt (ep);
  49617. + /* FIXME recover later ... somehow */
  49618. + }
  49619. +}
  49620. +
  49621. +static struct usb_request *
  49622. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  49623. +{
  49624. + struct usb_request *req;
  49625. + int status;
  49626. +
  49627. + req = alloc_ep_req (ep, 512);
  49628. + if (!req)
  49629. + return NULL;
  49630. +
  49631. + req->complete = zero_isoc_complete;
  49632. +
  49633. + status = usb_ep_queue (ep, req, gfp_flags);
  49634. + if (status) {
  49635. + struct zero_dev *dev = ep->driver_data;
  49636. +
  49637. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  49638. + free_ep_req (ep, req);
  49639. + req = NULL;
  49640. + }
  49641. +
  49642. + return req;
  49643. +}
  49644. +
  49645. +/* change our operational config. this code must agree with the code
  49646. + * that returns config descriptors, and altsetting code.
  49647. + *
  49648. + * it's also responsible for power management interactions. some
  49649. + * configurations might not work with our current power sources.
  49650. + *
  49651. + * note that some device controller hardware will constrain what this
  49652. + * code can do, perhaps by disallowing more than one configuration or
  49653. + * by limiting configuration choices (like the pxa2xx).
  49654. + */
  49655. +static int
  49656. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  49657. +{
  49658. + int result = 0;
  49659. + struct usb_gadget *gadget = dev->gadget;
  49660. + const struct usb_endpoint_descriptor *d;
  49661. + struct usb_ep *ep;
  49662. +
  49663. + if (number == dev->config)
  49664. + return 0;
  49665. +
  49666. + zero_reset_config (dev);
  49667. +
  49668. + gadget_for_each_ep (ep, gadget) {
  49669. +
  49670. + if (strcmp (ep->name, "ep4") == 0) {
  49671. +
  49672. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  49673. + result = usb_ep_enable (ep, d);
  49674. +
  49675. + if (result == 0) {
  49676. + ep->driver_data = dev;
  49677. + dev->in_ep = ep;
  49678. +
  49679. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  49680. +
  49681. + dev->in_ep = ep;
  49682. + continue;
  49683. + }
  49684. +
  49685. + usb_ep_disable (ep);
  49686. + result = -EIO;
  49687. + }
  49688. + }
  49689. +
  49690. + }
  49691. +
  49692. + dev->config = number;
  49693. + return result;
  49694. +}
  49695. +
  49696. +/*-------------------------------------------------------------------------*/
  49697. +
  49698. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  49699. +{
  49700. + if (req->status || req->actual != req->length)
  49701. + DBG ((struct zero_dev *) ep->driver_data,
  49702. + "setup complete --> %d, %d/%d\n",
  49703. + req->status, req->actual, req->length);
  49704. +}
  49705. +
  49706. +/*
  49707. + * The setup() callback implements all the ep0 functionality that's
  49708. + * not handled lower down, in hardware or the hardware driver (like
  49709. + * device and endpoint feature flags, and their status). It's all
  49710. + * housekeeping for the gadget function we're implementing. Most of
  49711. + * the work is in config-specific setup.
  49712. + */
  49713. +static int
  49714. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  49715. +{
  49716. + struct zero_dev *dev = get_gadget_data (gadget);
  49717. + struct usb_request *req = dev->req;
  49718. + int value = -EOPNOTSUPP;
  49719. +
  49720. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  49721. + * but config change events will reconfigure hardware.
  49722. + */
  49723. + req->zero = 0;
  49724. + switch (ctrl->bRequest) {
  49725. +
  49726. + case USB_REQ_GET_DESCRIPTOR:
  49727. +
  49728. + switch (ctrl->wValue >> 8) {
  49729. +
  49730. + case USB_DT_DEVICE:
  49731. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  49732. + memcpy (req->buf, &device_desc, value);
  49733. + break;
  49734. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  49735. + case USB_DT_DEVICE_QUALIFIER:
  49736. + if (!gadget->is_dualspeed)
  49737. + break;
  49738. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  49739. + memcpy (req->buf, &dev_qualifier, value);
  49740. + break;
  49741. +
  49742. + case USB_DT_OTHER_SPEED_CONFIG:
  49743. + if (!gadget->is_dualspeed)
  49744. + break;
  49745. + // FALLTHROUGH
  49746. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  49747. + case USB_DT_CONFIG:
  49748. + value = config_buf (gadget, req->buf,
  49749. + ctrl->wValue >> 8,
  49750. + ctrl->wValue & 0xff);
  49751. + if (value >= 0)
  49752. + value = min (ctrl->wLength, (u16) value);
  49753. + break;
  49754. +
  49755. + case USB_DT_STRING:
  49756. + /* wIndex == language code.
  49757. + * this driver only handles one language, you can
  49758. + * add string tables for other languages, using
  49759. + * any UTF-8 characters
  49760. + */
  49761. + value = usb_gadget_get_string (&stringtab,
  49762. + ctrl->wValue & 0xff, req->buf);
  49763. + if (value >= 0) {
  49764. + value = min (ctrl->wLength, (u16) value);
  49765. + }
  49766. + break;
  49767. + }
  49768. + break;
  49769. +
  49770. + /* currently two configs, two speeds */
  49771. + case USB_REQ_SET_CONFIGURATION:
  49772. + if (ctrl->bRequestType != 0)
  49773. + goto unknown;
  49774. +
  49775. + spin_lock (&dev->lock);
  49776. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  49777. + spin_unlock (&dev->lock);
  49778. + break;
  49779. + case USB_REQ_GET_CONFIGURATION:
  49780. + if (ctrl->bRequestType != USB_DIR_IN)
  49781. + goto unknown;
  49782. + *(u8 *)req->buf = dev->config;
  49783. + value = min (ctrl->wLength, (u16) 1);
  49784. + break;
  49785. +
  49786. + /* until we add altsetting support, or other interfaces,
  49787. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  49788. + * and already killed pending endpoint I/O.
  49789. + */
  49790. + case USB_REQ_SET_INTERFACE:
  49791. +
  49792. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  49793. + goto unknown;
  49794. + spin_lock (&dev->lock);
  49795. + if (dev->config) {
  49796. + u8 config = dev->config;
  49797. +
  49798. + /* resets interface configuration, forgets about
  49799. + * previous transaction state (queued bufs, etc)
  49800. + * and re-inits endpoint state (toggle etc)
  49801. + * no response queued, just zero status == success.
  49802. + * if we had more than one interface we couldn't
  49803. + * use this "reset the config" shortcut.
  49804. + */
  49805. + zero_reset_config (dev);
  49806. + zero_set_config (dev, config, GFP_ATOMIC);
  49807. + value = 0;
  49808. + }
  49809. + spin_unlock (&dev->lock);
  49810. + break;
  49811. + case USB_REQ_GET_INTERFACE:
  49812. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  49813. + value = ctrl->wLength;
  49814. + break;
  49815. + }
  49816. + else {
  49817. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  49818. + goto unknown;
  49819. + if (!dev->config)
  49820. + break;
  49821. + if (ctrl->wIndex != 0) {
  49822. + value = -EDOM;
  49823. + break;
  49824. + }
  49825. + *(u8 *)req->buf = 0;
  49826. + value = min (ctrl->wLength, (u16) 1);
  49827. + }
  49828. + break;
  49829. +
  49830. + /*
  49831. + * These are the same vendor-specific requests supported by
  49832. + * Intel's USB 2.0 compliance test devices. We exceed that
  49833. + * device spec by allowing multiple-packet requests.
  49834. + */
  49835. + case 0x5b: /* control WRITE test -- fill the buffer */
  49836. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  49837. + goto unknown;
  49838. + if (ctrl->wValue || ctrl->wIndex)
  49839. + break;
  49840. + /* just read that many bytes into the buffer */
  49841. + if (ctrl->wLength > USB_BUFSIZ)
  49842. + break;
  49843. + value = ctrl->wLength;
  49844. + break;
  49845. + case 0x5c: /* control READ test -- return the buffer */
  49846. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  49847. + goto unknown;
  49848. + if (ctrl->wValue || ctrl->wIndex)
  49849. + break;
  49850. + /* expect those bytes are still in the buffer; send back */
  49851. + if (ctrl->wLength > USB_BUFSIZ
  49852. + || ctrl->wLength != req->length)
  49853. + break;
  49854. + value = ctrl->wLength;
  49855. + break;
  49856. +
  49857. + case 0x01: // SET_CUR
  49858. + case 0x02:
  49859. + case 0x03:
  49860. + case 0x04:
  49861. + case 0x05:
  49862. + value = ctrl->wLength;
  49863. + break;
  49864. + case 0x81:
  49865. + switch (ctrl->wValue) {
  49866. + case 0x0201:
  49867. + case 0x0202:
  49868. + ((u8*)req->buf)[0] = 0x00;
  49869. + ((u8*)req->buf)[1] = 0xe3;
  49870. + break;
  49871. + case 0x0300:
  49872. + case 0x0500:
  49873. + ((u8*)req->buf)[0] = 0x00;
  49874. + break;
  49875. + }
  49876. + //((u8*)req->buf)[0] = 0x81;
  49877. + //((u8*)req->buf)[1] = 0x81;
  49878. + value = ctrl->wLength;
  49879. + break;
  49880. + case 0x82:
  49881. + switch (ctrl->wValue) {
  49882. + case 0x0201:
  49883. + case 0x0202:
  49884. + ((u8*)req->buf)[0] = 0x00;
  49885. + ((u8*)req->buf)[1] = 0xc3;
  49886. + break;
  49887. + case 0x0300:
  49888. + case 0x0500:
  49889. + ((u8*)req->buf)[0] = 0x00;
  49890. + break;
  49891. + }
  49892. + //((u8*)req->buf)[0] = 0x82;
  49893. + //((u8*)req->buf)[1] = 0x82;
  49894. + value = ctrl->wLength;
  49895. + break;
  49896. + case 0x83:
  49897. + switch (ctrl->wValue) {
  49898. + case 0x0201:
  49899. + case 0x0202:
  49900. + ((u8*)req->buf)[0] = 0x00;
  49901. + ((u8*)req->buf)[1] = 0x00;
  49902. + break;
  49903. + case 0x0300:
  49904. + ((u8*)req->buf)[0] = 0x60;
  49905. + break;
  49906. + case 0x0500:
  49907. + ((u8*)req->buf)[0] = 0x18;
  49908. + break;
  49909. + }
  49910. + //((u8*)req->buf)[0] = 0x83;
  49911. + //((u8*)req->buf)[1] = 0x83;
  49912. + value = ctrl->wLength;
  49913. + break;
  49914. + case 0x84:
  49915. + switch (ctrl->wValue) {
  49916. + case 0x0201:
  49917. + case 0x0202:
  49918. + ((u8*)req->buf)[0] = 0x00;
  49919. + ((u8*)req->buf)[1] = 0x01;
  49920. + break;
  49921. + case 0x0300:
  49922. + case 0x0500:
  49923. + ((u8*)req->buf)[0] = 0x08;
  49924. + break;
  49925. + }
  49926. + //((u8*)req->buf)[0] = 0x84;
  49927. + //((u8*)req->buf)[1] = 0x84;
  49928. + value = ctrl->wLength;
  49929. + break;
  49930. + case 0x85:
  49931. + ((u8*)req->buf)[0] = 0x85;
  49932. + ((u8*)req->buf)[1] = 0x85;
  49933. + value = ctrl->wLength;
  49934. + break;
  49935. +
  49936. +
  49937. + default:
  49938. +unknown:
  49939. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  49940. + ctrl->bRequestType, ctrl->bRequest,
  49941. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  49942. + }
  49943. +
  49944. + /* respond with data transfer before status phase? */
  49945. + if (value >= 0) {
  49946. + req->length = value;
  49947. + req->zero = value < ctrl->wLength
  49948. + && (value % gadget->ep0->maxpacket) == 0;
  49949. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  49950. + if (value < 0) {
  49951. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  49952. + req->status = 0;
  49953. + zero_setup_complete (gadget->ep0, req);
  49954. + }
  49955. + }
  49956. +
  49957. + /* device either stalls (value < 0) or reports success */
  49958. + return value;
  49959. +}
  49960. +
  49961. +static void
  49962. +zero_disconnect (struct usb_gadget *gadget)
  49963. +{
  49964. + struct zero_dev *dev = get_gadget_data (gadget);
  49965. + unsigned long flags;
  49966. +
  49967. + spin_lock_irqsave (&dev->lock, flags);
  49968. + zero_reset_config (dev);
  49969. +
  49970. + /* a more significant application might have some non-usb
  49971. + * activities to quiesce here, saving resources like power
  49972. + * or pushing the notification up a network stack.
  49973. + */
  49974. + spin_unlock_irqrestore (&dev->lock, flags);
  49975. +
  49976. + /* next we may get setup() calls to enumerate new connections;
  49977. + * or an unbind() during shutdown (including removing module).
  49978. + */
  49979. +}
  49980. +
  49981. +static void
  49982. +zero_autoresume (unsigned long _dev)
  49983. +{
  49984. + struct zero_dev *dev = (struct zero_dev *) _dev;
  49985. + int status;
  49986. +
  49987. + /* normally the host would be woken up for something
  49988. + * more significant than just a timer firing...
  49989. + */
  49990. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  49991. + status = usb_gadget_wakeup (dev->gadget);
  49992. + DBG (dev, "wakeup --> %d\n", status);
  49993. + }
  49994. +}
  49995. +
  49996. +/*-------------------------------------------------------------------------*/
  49997. +
  49998. +static void
  49999. +zero_unbind (struct usb_gadget *gadget)
  50000. +{
  50001. + struct zero_dev *dev = get_gadget_data (gadget);
  50002. +
  50003. + DBG (dev, "unbind\n");
  50004. +
  50005. + /* we've already been disconnected ... no i/o is active */
  50006. + if (dev->req)
  50007. + free_ep_req (gadget->ep0, dev->req);
  50008. + del_timer_sync (&dev->resume);
  50009. + kfree (dev);
  50010. + set_gadget_data (gadget, NULL);
  50011. +}
  50012. +
  50013. +static int
  50014. +zero_bind (struct usb_gadget *gadget)
  50015. +{
  50016. + struct zero_dev *dev;
  50017. + //struct usb_ep *ep;
  50018. +
  50019. + printk("binding\n");
  50020. + /*
  50021. + * DRIVER POLICY CHOICE: you may want to do this differently.
  50022. + * One thing to avoid is reusing a bcdDevice revision code
  50023. + * with different host-visible configurations or behavior
  50024. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  50025. + */
  50026. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  50027. +
  50028. +
  50029. + /* ok, we made sense of the hardware ... */
  50030. + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  50031. + if (!dev)
  50032. + return -ENOMEM;
  50033. + memset (dev, 0, sizeof *dev);
  50034. + spin_lock_init (&dev->lock);
  50035. + dev->gadget = gadget;
  50036. + set_gadget_data (gadget, dev);
  50037. +
  50038. + /* preallocate control response and buffer */
  50039. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  50040. + if (!dev->req)
  50041. + goto enomem;
  50042. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  50043. + &dev->req->dma, GFP_KERNEL);
  50044. + if (!dev->req->buf)
  50045. + goto enomem;
  50046. +
  50047. + dev->req->complete = zero_setup_complete;
  50048. +
  50049. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  50050. +
  50051. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  50052. + /* assume ep0 uses the same value for both speeds ... */
  50053. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  50054. +
  50055. + /* and that all endpoints are dual-speed */
  50056. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  50057. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  50058. +#endif
  50059. +
  50060. + usb_gadget_set_selfpowered (gadget);
  50061. +
  50062. + init_timer (&dev->resume);
  50063. + dev->resume.function = zero_autoresume;
  50064. + dev->resume.data = (unsigned long) dev;
  50065. +
  50066. + gadget->ep0->driver_data = dev;
  50067. +
  50068. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  50069. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  50070. + EP_OUT_NAME, EP_IN_NAME);
  50071. +
  50072. + snprintf (manufacturer, sizeof manufacturer,
  50073. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  50074. + gadget->name);
  50075. +
  50076. + return 0;
  50077. +
  50078. +enomem:
  50079. + zero_unbind (gadget);
  50080. + return -ENOMEM;
  50081. +}
  50082. +
  50083. +/*-------------------------------------------------------------------------*/
  50084. +
  50085. +static void
  50086. +zero_suspend (struct usb_gadget *gadget)
  50087. +{
  50088. + struct zero_dev *dev = get_gadget_data (gadget);
  50089. +
  50090. + if (gadget->speed == USB_SPEED_UNKNOWN)
  50091. + return;
  50092. +
  50093. + if (autoresume) {
  50094. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  50095. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  50096. + } else
  50097. + DBG (dev, "suspend\n");
  50098. +}
  50099. +
  50100. +static void
  50101. +zero_resume (struct usb_gadget *gadget)
  50102. +{
  50103. + struct zero_dev *dev = get_gadget_data (gadget);
  50104. +
  50105. + DBG (dev, "resume\n");
  50106. + del_timer (&dev->resume);
  50107. +}
  50108. +
  50109. +
  50110. +/*-------------------------------------------------------------------------*/
  50111. +
  50112. +static struct usb_gadget_driver zero_driver = {
  50113. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  50114. + .speed = USB_SPEED_HIGH,
  50115. +#else
  50116. + .speed = USB_SPEED_FULL,
  50117. +#endif
  50118. + .function = (char *) longname,
  50119. + .bind = zero_bind,
  50120. + .unbind = zero_unbind,
  50121. +
  50122. + .setup = zero_setup,
  50123. + .disconnect = zero_disconnect,
  50124. +
  50125. + .suspend = zero_suspend,
  50126. + .resume = zero_resume,
  50127. +
  50128. + .driver = {
  50129. + .name = (char *) shortname,
  50130. + // .shutdown = ...
  50131. + // .suspend = ...
  50132. + // .resume = ...
  50133. + },
  50134. +};
  50135. +
  50136. +MODULE_AUTHOR ("David Brownell");
  50137. +MODULE_LICENSE ("Dual BSD/GPL");
  50138. +
  50139. +static struct proc_dir_entry *pdir, *pfile;
  50140. +
  50141. +static int isoc_read_data (char *page, char **start,
  50142. + off_t off, int count,
  50143. + int *eof, void *data)
  50144. +{
  50145. + int i;
  50146. + static int c = 0;
  50147. + static int done = 0;
  50148. + static int s = 0;
  50149. +
  50150. +/*
  50151. + printk ("\ncount: %d\n", count);
  50152. + printk ("rbuf_start: %d\n", rbuf_start);
  50153. + printk ("rbuf_len: %d\n", rbuf_len);
  50154. + printk ("off: %d\n", off);
  50155. + printk ("start: %p\n\n", *start);
  50156. +*/
  50157. + if (done) {
  50158. + c = 0;
  50159. + done = 0;
  50160. + *eof = 1;
  50161. + return 0;
  50162. + }
  50163. +
  50164. + if (c == 0) {
  50165. + if (rbuf_len == RBUF_LEN)
  50166. + s = rbuf_start;
  50167. + else s = 0;
  50168. + }
  50169. +
  50170. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  50171. + page[i] = rbuf[(c+s) % RBUF_LEN];
  50172. + }
  50173. + *start = page;
  50174. +
  50175. + if (c >= rbuf_len) {
  50176. + *eof = 1;
  50177. + done = 1;
  50178. + }
  50179. +
  50180. +
  50181. + return i;
  50182. +}
  50183. +
  50184. +static int __init init (void)
  50185. +{
  50186. +
  50187. + int retval = 0;
  50188. +
  50189. + pdir = proc_mkdir("isoc_test", NULL);
  50190. + if(pdir == NULL) {
  50191. + retval = -ENOMEM;
  50192. + printk("Error creating dir\n");
  50193. + goto done;
  50194. + }
  50195. + pdir->owner = THIS_MODULE;
  50196. +
  50197. + pfile = create_proc_read_entry("isoc_data",
  50198. + 0444, pdir,
  50199. + isoc_read_data,
  50200. + NULL);
  50201. + if (pfile == NULL) {
  50202. + retval = -ENOMEM;
  50203. + printk("Error creating file\n");
  50204. + goto no_file;
  50205. + }
  50206. + pfile->owner = THIS_MODULE;
  50207. +
  50208. + return usb_gadget_register_driver (&zero_driver);
  50209. +
  50210. + no_file:
  50211. + remove_proc_entry("isoc_data", NULL);
  50212. + done:
  50213. + return retval;
  50214. +}
  50215. +module_init (init);
  50216. +
  50217. +static void __exit cleanup (void)
  50218. +{
  50219. +
  50220. + usb_gadget_unregister_driver (&zero_driver);
  50221. +
  50222. + remove_proc_entry("isoc_data", pdir);
  50223. + remove_proc_entry("isoc_test", NULL);
  50224. +}
  50225. +module_exit (cleanup);
  50226. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_cfi_common.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  50227. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_cfi_common.h 1970-01-01 01:00:00.000000000 +0100
  50228. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_cfi_common.h 2014-03-11 16:55:38.000000000 +0100
  50229. @@ -0,0 +1,142 @@
  50230. +/* ==========================================================================
  50231. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50232. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50233. + * otherwise expressly agreed to in writing between Synopsys and you.
  50234. + *
  50235. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50236. + * any End User Software License Agreement or Agreement for Licensed Product
  50237. + * with Synopsys or any supplement thereto. You are permitted to use and
  50238. + * redistribute this Software in source and binary forms, with or without
  50239. + * modification, provided that redistributions of source code must retain this
  50240. + * notice. You may not view, use, disclose, copy or distribute this file or
  50241. + * any information contained herein except pursuant to this license grant from
  50242. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50243. + * below, then you are not authorized to use the Software.
  50244. + *
  50245. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50246. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50247. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50248. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50249. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50250. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50251. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50252. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50253. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50254. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50255. + * DAMAGE.
  50256. + * ========================================================================== */
  50257. +
  50258. +#if !defined(__DWC_CFI_COMMON_H__)
  50259. +#define __DWC_CFI_COMMON_H__
  50260. +
  50261. +//#include <linux/types.h>
  50262. +
  50263. +/**
  50264. + * @file
  50265. + *
  50266. + * This file contains the CFI specific common constants, interfaces
  50267. + * (functions and macros) and structures for Linux. No PCD specific
  50268. + * data structure or definition is to be included in this file.
  50269. + *
  50270. + */
  50271. +
  50272. +/** This is a request for all Core Features */
  50273. +#define VEN_CORE_GET_FEATURES 0xB1
  50274. +
  50275. +/** This is a request to get the value of a specific Core Feature */
  50276. +#define VEN_CORE_GET_FEATURE 0xB2
  50277. +
  50278. +/** This command allows the host to set the value of a specific Core Feature */
  50279. +#define VEN_CORE_SET_FEATURE 0xB3
  50280. +
  50281. +/** This command allows the host to set the default values of
  50282. + * either all or any specific Core Feature
  50283. + */
  50284. +#define VEN_CORE_RESET_FEATURES 0xB4
  50285. +
  50286. +/** This command forces the PCD to write the deferred values of a Core Features */
  50287. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  50288. +
  50289. +/** This request reads a DWORD value from a register at the specified offset */
  50290. +#define VEN_CORE_READ_REGISTER 0xB6
  50291. +
  50292. +/** This request writes a DWORD value into a register at the specified offset */
  50293. +#define VEN_CORE_WRITE_REGISTER 0xB7
  50294. +
  50295. +/** This structure is the header of the Core Features dataset returned to
  50296. + * the Host
  50297. + */
  50298. +struct cfi_all_features_header {
  50299. +/** The features header structure length is */
  50300. +#define CFI_ALL_FEATURES_HDR_LEN 8
  50301. + /**
  50302. + * The total length of the features dataset returned to the Host
  50303. + */
  50304. + uint16_t wTotalLen;
  50305. +
  50306. + /**
  50307. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  50308. + * This field identifies the version of the CFI Specification with which
  50309. + * the device is compliant.
  50310. + */
  50311. + uint16_t wVersion;
  50312. +
  50313. + /** The ID of the Core */
  50314. + uint16_t wCoreID;
  50315. +#define CFI_CORE_ID_UDC 1
  50316. +#define CFI_CORE_ID_OTG 2
  50317. +#define CFI_CORE_ID_WUDEV 3
  50318. +
  50319. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  50320. + uint16_t wNumFeatures;
  50321. +} UPACKED;
  50322. +
  50323. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  50324. +
  50325. +/** This structure is a header of the Core Feature descriptor dataset returned to
  50326. + * the Host after the VEN_CORE_GET_FEATURES request
  50327. + */
  50328. +struct cfi_feature_desc_header {
  50329. +#define CFI_FEATURE_DESC_HDR_LEN 8
  50330. +
  50331. + /** The feature ID */
  50332. + uint16_t wFeatureID;
  50333. +
  50334. + /** Length of this feature descriptor in bytes - including the
  50335. + * length of the feature name string
  50336. + */
  50337. + uint16_t wLength;
  50338. +
  50339. + /** The data length of this feature in bytes */
  50340. + uint16_t wDataLength;
  50341. +
  50342. + /**
  50343. + * Attributes of this features
  50344. + * D0: Access rights
  50345. + * 0 - Read/Write
  50346. + * 1 - Read only
  50347. + */
  50348. + uint8_t bmAttributes;
  50349. +#define CFI_FEATURE_ATTR_RO 1
  50350. +#define CFI_FEATURE_ATTR_RW 0
  50351. +
  50352. + /** Length of the feature name in bytes */
  50353. + uint8_t bNameLen;
  50354. +
  50355. + /** The feature name buffer */
  50356. + //uint8_t *name;
  50357. +} UPACKED;
  50358. +
  50359. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  50360. +
  50361. +/**
  50362. + * This structure describes a NULL terminated string referenced by its id field.
  50363. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  50364. + */
  50365. +struct cfi_string {
  50366. + uint16_t id;
  50367. + const uint8_t *s;
  50368. +};
  50369. +typedef struct cfi_string cfi_string_t;
  50370. +
  50371. +#endif
  50372. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_adp.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  50373. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_adp.c 1970-01-01 01:00:00.000000000 +0100
  50374. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_adp.c 2014-03-11 16:55:38.000000000 +0100
  50375. @@ -0,0 +1,854 @@
  50376. +/* ==========================================================================
  50377. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  50378. + * $Revision: #12 $
  50379. + * $Date: 2011/10/26 $
  50380. + * $Change: 1873028 $
  50381. + *
  50382. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50383. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50384. + * otherwise expressly agreed to in writing between Synopsys and you.
  50385. + *
  50386. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50387. + * any End User Software License Agreement or Agreement for Licensed Product
  50388. + * with Synopsys or any supplement thereto. You are permitted to use and
  50389. + * redistribute this Software in source and binary forms, with or without
  50390. + * modification, provided that redistributions of source code must retain this
  50391. + * notice. You may not view, use, disclose, copy or distribute this file or
  50392. + * any information contained herein except pursuant to this license grant from
  50393. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50394. + * below, then you are not authorized to use the Software.
  50395. + *
  50396. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50397. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50398. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50399. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50400. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50401. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50402. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50403. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50404. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50405. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50406. + * DAMAGE.
  50407. + * ========================================================================== */
  50408. +
  50409. +#include "dwc_os.h"
  50410. +#include "dwc_otg_regs.h"
  50411. +#include "dwc_otg_cil.h"
  50412. +#include "dwc_otg_adp.h"
  50413. +
  50414. +/** @file
  50415. + *
  50416. + * This file contains the most of the Attach Detect Protocol implementation for
  50417. + * the driver to support OTG Rev2.0.
  50418. + *
  50419. + */
  50420. +
  50421. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  50422. +{
  50423. + adpctl_data_t adpctl;
  50424. +
  50425. + adpctl.d32 = value;
  50426. + adpctl.b.ar = 0x2;
  50427. +
  50428. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  50429. +
  50430. + while (adpctl.b.ar) {
  50431. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  50432. + }
  50433. +
  50434. +}
  50435. +
  50436. +/**
  50437. + * Function is called to read ADP registers
  50438. + */
  50439. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  50440. +{
  50441. + adpctl_data_t adpctl;
  50442. +
  50443. + adpctl.d32 = 0;
  50444. + adpctl.b.ar = 0x1;
  50445. +
  50446. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  50447. +
  50448. + while (adpctl.b.ar) {
  50449. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  50450. + }
  50451. +
  50452. + return adpctl.d32;
  50453. +}
  50454. +
  50455. +/**
  50456. + * Function is called to read ADPCTL register and filter Write-clear bits
  50457. + */
  50458. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  50459. +{
  50460. + adpctl_data_t adpctl;
  50461. +
  50462. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  50463. + adpctl.b.adp_tmout_int = 0;
  50464. + adpctl.b.adp_prb_int = 0;
  50465. + adpctl.b.adp_tmout_int = 0;
  50466. +
  50467. + return adpctl.d32;
  50468. +}
  50469. +
  50470. +/**
  50471. + * Function is called to write ADP registers
  50472. + */
  50473. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  50474. + uint32_t set)
  50475. +{
  50476. + dwc_otg_adp_write_reg(core_if,
  50477. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  50478. +}
  50479. +
  50480. +static void adp_sense_timeout(void *ptr)
  50481. +{
  50482. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  50483. + core_if->adp.sense_timer_started = 0;
  50484. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  50485. + if (core_if->adp_enable) {
  50486. + dwc_otg_adp_sense_stop(core_if);
  50487. + dwc_otg_adp_probe_start(core_if);
  50488. + }
  50489. +}
  50490. +
  50491. +/**
  50492. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  50493. + */
  50494. +static void adp_vbuson_timeout(void *ptr)
  50495. +{
  50496. + gpwrdn_data_t gpwrdn;
  50497. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  50498. + hprt0_data_t hprt0 = {.d32 = 0 };
  50499. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  50500. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  50501. + if (core_if) {
  50502. + core_if->adp.vbuson_timer_started = 0;
  50503. + /* Turn off vbus */
  50504. + hprt0.b.prtpwr = 1;
  50505. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  50506. + gpwrdn.d32 = 0;
  50507. +
  50508. + /* Power off the core */
  50509. + if (core_if->power_down == 2) {
  50510. + /* Enable Wakeup Logic */
  50511. +// gpwrdn.b.wkupactiv = 1;
  50512. + gpwrdn.b.pmuactv = 0;
  50513. + gpwrdn.b.pwrdnrstn = 1;
  50514. + gpwrdn.b.pwrdnclmp = 1;
  50515. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  50516. + gpwrdn.d32);
  50517. +
  50518. + /* Suspend the Phy Clock */
  50519. + pcgcctl.b.stoppclk = 1;
  50520. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  50521. +
  50522. + /* Switch on VDD */
  50523. +// gpwrdn.b.wkupactiv = 1;
  50524. + gpwrdn.b.pmuactv = 1;
  50525. + gpwrdn.b.pwrdnrstn = 1;
  50526. + gpwrdn.b.pwrdnclmp = 1;
  50527. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  50528. + gpwrdn.d32);
  50529. + } else {
  50530. + /* Enable Power Down Logic */
  50531. + gpwrdn.b.pmuintsel = 1;
  50532. + gpwrdn.b.pmuactv = 1;
  50533. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  50534. + }
  50535. +
  50536. + /* Power off the core */
  50537. + if (core_if->power_down == 2) {
  50538. + gpwrdn.d32 = 0;
  50539. + gpwrdn.b.pwrdnswtch = 1;
  50540. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  50541. + gpwrdn.d32, 0);
  50542. + }
  50543. +
  50544. + /* Unmask SRP detected interrupt from Power Down Logic */
  50545. + gpwrdn.d32 = 0;
  50546. + gpwrdn.b.srp_det_msk = 1;
  50547. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  50548. +
  50549. + dwc_otg_adp_probe_start(core_if);
  50550. + dwc_otg_dump_global_registers(core_if);
  50551. + dwc_otg_dump_host_registers(core_if);
  50552. + }
  50553. +
  50554. +}
  50555. +
  50556. +/**
  50557. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  50558. + * not asserted within 1.1 seconds.
  50559. + *
  50560. + * @param core_if the pointer to core_if strucure.
  50561. + */
  50562. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  50563. +{
  50564. + core_if->adp.vbuson_timer_started = 1;
  50565. + if (core_if->adp.vbuson_timer)
  50566. + {
  50567. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  50568. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  50569. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  50570. + } else {
  50571. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  50572. + }
  50573. +}
  50574. +
  50575. +#if 0
  50576. +/**
  50577. + * Masks all DWC OTG core interrupts
  50578. + *
  50579. + */
  50580. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  50581. +{
  50582. + int i;
  50583. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  50584. +
  50585. + /* Mask Host Interrupts */
  50586. +
  50587. + /* Clear and disable HCINTs */
  50588. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  50589. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  50590. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  50591. +
  50592. + }
  50593. +
  50594. + /* Clear and disable HAINT */
  50595. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  50596. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  50597. +
  50598. + /* Mask Device Interrupts */
  50599. + if (!core_if->multiproc_int_enable) {
  50600. + /* Clear and disable IN Endpoint interrupts */
  50601. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  50602. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  50603. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  50604. + diepint, 0xFFFFFFFF);
  50605. + }
  50606. +
  50607. + /* Clear and disable OUT Endpoint interrupts */
  50608. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  50609. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  50610. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  50611. + doepint, 0xFFFFFFFF);
  50612. + }
  50613. +
  50614. + /* Clear and disable DAINT */
  50615. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  50616. + 0xFFFFFFFF);
  50617. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  50618. + } else {
  50619. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  50620. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  50621. + diepeachintmsk[i], 0);
  50622. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  50623. + diepint, 0xFFFFFFFF);
  50624. + }
  50625. +
  50626. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  50627. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  50628. + doepeachintmsk[i], 0);
  50629. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  50630. + doepint, 0xFFFFFFFF);
  50631. + }
  50632. +
  50633. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  50634. + 0);
  50635. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  50636. + 0xFFFFFFFF);
  50637. +
  50638. + }
  50639. +
  50640. + /* Disable interrupts */
  50641. + ahbcfg.b.glblintrmsk = 1;
  50642. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  50643. +
  50644. + /* Disable all interrupts. */
  50645. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  50646. +
  50647. + /* Clear any pending interrupts */
  50648. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  50649. +
  50650. + /* Clear any pending OTG Interrupts */
  50651. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  50652. +}
  50653. +
  50654. +/**
  50655. + * Unmask Port Connection Detected interrupt
  50656. + *
  50657. + */
  50658. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  50659. +{
  50660. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  50661. +
  50662. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  50663. +}
  50664. +#endif
  50665. +
  50666. +/**
  50667. + * Starts the ADP Probing
  50668. + *
  50669. + * @param core_if the pointer to core_if structure.
  50670. + */
  50671. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  50672. +{
  50673. +
  50674. + adpctl_data_t adpctl = {.d32 = 0};
  50675. + gpwrdn_data_t gpwrdn;
  50676. +#if 0
  50677. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  50678. + .b.adp_sns_int = 1, b.adp_tmout_int};
  50679. +#endif
  50680. + dwc_otg_disable_global_interrupts(core_if);
  50681. + DWC_PRINTF("ADP Probe Start\n");
  50682. + core_if->adp.probe_enabled = 1;
  50683. +
  50684. + adpctl.b.adpres = 1;
  50685. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  50686. +
  50687. + while (adpctl.b.adpres) {
  50688. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  50689. + }
  50690. +
  50691. + adpctl.d32 = 0;
  50692. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  50693. +
  50694. + /* In Host mode unmask SRP detected interrupt */
  50695. + gpwrdn.d32 = 0;
  50696. + gpwrdn.b.sts_chngint_msk = 1;
  50697. + if (!gpwrdn.b.idsts) {
  50698. + gpwrdn.b.srp_det_msk = 1;
  50699. + }
  50700. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  50701. +
  50702. + adpctl.b.adp_tmout_int_msk = 1;
  50703. + adpctl.b.adp_prb_int_msk = 1;
  50704. + adpctl.b.prb_dschg = 1;
  50705. + adpctl.b.prb_delta = 1;
  50706. + adpctl.b.prb_per = 1;
  50707. + adpctl.b.adpen = 1;
  50708. + adpctl.b.enaprb = 1;
  50709. +
  50710. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  50711. + DWC_PRINTF("ADP Probe Finish\n");
  50712. + return 0;
  50713. +}
  50714. +
  50715. +/**
  50716. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  50717. + * within 3 seconds.
  50718. + *
  50719. + * @param core_if the pointer to core_if strucure.
  50720. + */
  50721. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  50722. +{
  50723. + core_if->adp.sense_timer_started = 1;
  50724. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  50725. +}
  50726. +
  50727. +/**
  50728. + * Starts the ADP Sense
  50729. + *
  50730. + * @param core_if the pointer to core_if strucure.
  50731. + */
  50732. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  50733. +{
  50734. + adpctl_data_t adpctl;
  50735. +
  50736. + DWC_PRINTF("ADP Sense Start\n");
  50737. +
  50738. + /* Unmask ADP sense interrupt and mask all other from the core */
  50739. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  50740. + adpctl.b.adp_sns_int_msk = 1;
  50741. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  50742. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  50743. +
  50744. + /* Set ADP reset bit*/
  50745. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  50746. + adpctl.b.adpres = 1;
  50747. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  50748. +
  50749. + while (adpctl.b.adpres) {
  50750. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  50751. + }
  50752. +
  50753. + adpctl.b.adpres = 0;
  50754. + adpctl.b.adpen = 1;
  50755. + adpctl.b.enasns = 1;
  50756. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  50757. +
  50758. + dwc_otg_adp_sense_timer_start(core_if);
  50759. +
  50760. + return 0;
  50761. +}
  50762. +
  50763. +/**
  50764. + * Stops the ADP Probing
  50765. + *
  50766. + * @param core_if the pointer to core_if strucure.
  50767. + */
  50768. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  50769. +{
  50770. +
  50771. + adpctl_data_t adpctl;
  50772. + DWC_PRINTF("Stop ADP probe\n");
  50773. + core_if->adp.probe_enabled = 0;
  50774. + core_if->adp.probe_counter = 0;
  50775. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  50776. +
  50777. + adpctl.b.adpen = 0;
  50778. + adpctl.b.adp_prb_int = 1;
  50779. + adpctl.b.adp_tmout_int = 1;
  50780. + adpctl.b.adp_sns_int = 1;
  50781. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  50782. +
  50783. + return 0;
  50784. +}
  50785. +
  50786. +/**
  50787. + * Stops the ADP Sensing
  50788. + *
  50789. + * @param core_if the pointer to core_if strucure.
  50790. + */
  50791. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  50792. +{
  50793. + adpctl_data_t adpctl;
  50794. +
  50795. + core_if->adp.sense_enabled = 0;
  50796. +
  50797. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  50798. + adpctl.b.enasns = 0;
  50799. + adpctl.b.adp_sns_int = 1;
  50800. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  50801. +
  50802. + return 0;
  50803. +}
  50804. +
  50805. +/**
  50806. + * Called to turn on the VBUS after initial ADP probe in host mode.
  50807. + * If port power was already enabled in cil_hcd_start function then
  50808. + * only schedule a timer.
  50809. + *
  50810. + * @param core_if the pointer to core_if structure.
  50811. + */
  50812. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  50813. +{
  50814. + hprt0_data_t hprt0 = {.d32 = 0 };
  50815. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  50816. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  50817. +
  50818. + if (hprt0.b.prtpwr == 0) {
  50819. + hprt0.b.prtpwr = 1;
  50820. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  50821. + }
  50822. +
  50823. + dwc_otg_adp_vbuson_timer_start(core_if);
  50824. +}
  50825. +
  50826. +/**
  50827. + * Called right after driver is loaded
  50828. + * to perform initial actions for ADP
  50829. + *
  50830. + * @param core_if the pointer to core_if structure.
  50831. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  50832. + */
  50833. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  50834. +{
  50835. + gpwrdn_data_t gpwrdn;
  50836. +
  50837. + DWC_PRINTF("ADP Initial Start\n");
  50838. + core_if->adp.adp_started = 1;
  50839. +
  50840. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  50841. + dwc_otg_disable_global_interrupts(core_if);
  50842. + if (is_host) {
  50843. + DWC_PRINTF("HOST MODE\n");
  50844. + /* Enable Power Down Logic Interrupt*/
  50845. + gpwrdn.d32 = 0;
  50846. + gpwrdn.b.pmuintsel = 1;
  50847. + gpwrdn.b.pmuactv = 1;
  50848. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  50849. + /* Initialize first ADP probe to obtain Ramp Time value */
  50850. + core_if->adp.initial_probe = 1;
  50851. + dwc_otg_adp_probe_start(core_if);
  50852. + } else {
  50853. + gotgctl_data_t gotgctl;
  50854. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  50855. + DWC_PRINTF("DEVICE MODE\n");
  50856. + if (gotgctl.b.bsesvld == 0) {
  50857. + /* Enable Power Down Logic Interrupt*/
  50858. + gpwrdn.d32 = 0;
  50859. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  50860. + gpwrdn.b.pmuintsel = 1;
  50861. + gpwrdn.b.pmuactv = 1;
  50862. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  50863. + core_if->adp.initial_probe = 1;
  50864. + dwc_otg_adp_probe_start(core_if);
  50865. + } else {
  50866. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  50867. + core_if->op_state = B_PERIPHERAL;
  50868. + dwc_otg_core_init(core_if);
  50869. + dwc_otg_enable_global_interrupts(core_if);
  50870. + cil_pcd_start(core_if);
  50871. + dwc_otg_dump_global_registers(core_if);
  50872. + dwc_otg_dump_dev_registers(core_if);
  50873. + }
  50874. + }
  50875. +}
  50876. +
  50877. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  50878. +{
  50879. + core_if->adp.adp_started = 0;
  50880. + core_if->adp.initial_probe = 0;
  50881. + core_if->adp.probe_timer_values[0] = -1;
  50882. + core_if->adp.probe_timer_values[1] = -1;
  50883. + core_if->adp.probe_enabled = 0;
  50884. + core_if->adp.sense_enabled = 0;
  50885. + core_if->adp.sense_timer_started = 0;
  50886. + core_if->adp.vbuson_timer_started = 0;
  50887. + core_if->adp.probe_counter = 0;
  50888. + core_if->adp.gpwrdn = 0;
  50889. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  50890. + /* Initialize timers */
  50891. + core_if->adp.sense_timer =
  50892. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  50893. + core_if->adp.vbuson_timer =
  50894. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  50895. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  50896. + {
  50897. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  50898. + }
  50899. +}
  50900. +
  50901. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  50902. +{
  50903. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  50904. + gpwrdn.b.pmuintsel = 1;
  50905. + gpwrdn.b.pmuactv = 1;
  50906. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  50907. +
  50908. + if (core_if->adp.probe_enabled)
  50909. + dwc_otg_adp_probe_stop(core_if);
  50910. + if (core_if->adp.sense_enabled)
  50911. + dwc_otg_adp_sense_stop(core_if);
  50912. + if (core_if->adp.sense_timer_started)
  50913. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  50914. + if (core_if->adp.vbuson_timer_started)
  50915. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  50916. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  50917. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  50918. +}
  50919. +
  50920. +/////////////////////////////////////////////////////////////////////
  50921. +////////////// ADP Interrupt Handlers ///////////////////////////////
  50922. +/////////////////////////////////////////////////////////////////////
  50923. +/**
  50924. + * This function sets Ramp Timer values
  50925. + */
  50926. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  50927. +{
  50928. + if (core_if->adp.probe_timer_values[0] == -1) {
  50929. + core_if->adp.probe_timer_values[0] = val;
  50930. + core_if->adp.probe_timer_values[1] = -1;
  50931. + return 1;
  50932. + } else {
  50933. + core_if->adp.probe_timer_values[1] =
  50934. + core_if->adp.probe_timer_values[0];
  50935. + core_if->adp.probe_timer_values[0] = val;
  50936. + return 0;
  50937. + }
  50938. +}
  50939. +
  50940. +/**
  50941. + * This function compares Ramp Timer values
  50942. + */
  50943. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  50944. +{
  50945. + uint32_t diff;
  50946. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  50947. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  50948. + else
  50949. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  50950. + if(diff < 2) {
  50951. + return 0;
  50952. + } else {
  50953. + return 1;
  50954. + }
  50955. +}
  50956. +
  50957. +/**
  50958. + * This function handles ADP Probe Interrupts
  50959. + */
  50960. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  50961. + uint32_t val)
  50962. +{
  50963. + adpctl_data_t adpctl = {.d32 = 0 };
  50964. + gpwrdn_data_t gpwrdn, temp;
  50965. + adpctl.d32 = val;
  50966. +
  50967. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  50968. + core_if->adp.probe_counter++;
  50969. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  50970. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  50971. + DWC_PRINTF("RTIM value is 0\n");
  50972. + goto exit;
  50973. + }
  50974. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  50975. + core_if->adp.initial_probe) {
  50976. + core_if->adp.initial_probe = 0;
  50977. + dwc_otg_adp_probe_stop(core_if);
  50978. + gpwrdn.d32 = 0;
  50979. + gpwrdn.b.pmuactv = 1;
  50980. + gpwrdn.b.pmuintsel = 1;
  50981. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  50982. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  50983. +
  50984. + /* check which value is for device mode and which for Host mode */
  50985. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  50986. + /*
  50987. + * Turn on VBUS after initial ADP probe.
  50988. + */
  50989. + core_if->op_state = A_HOST;
  50990. + dwc_otg_enable_global_interrupts(core_if);
  50991. + DWC_SPINUNLOCK(core_if->lock);
  50992. + cil_hcd_start(core_if);
  50993. + dwc_otg_adp_turnon_vbus(core_if);
  50994. + DWC_SPINLOCK(core_if->lock);
  50995. + } else {
  50996. + /*
  50997. + * Initiate SRP after initial ADP probe.
  50998. + */
  50999. + dwc_otg_enable_global_interrupts(core_if);
  51000. + dwc_otg_initiate_srp(core_if);
  51001. + }
  51002. + } else if (core_if->adp.probe_counter > 2){
  51003. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51004. + if (compare_timer_values(core_if)) {
  51005. + DWC_PRINTF("Difference in timer values !!! \n");
  51006. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  51007. + dwc_otg_adp_probe_stop(core_if);
  51008. +
  51009. + /* Power on the core */
  51010. + if (core_if->power_down == 2) {
  51011. + gpwrdn.b.pwrdnswtch = 1;
  51012. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51013. + gpwrdn, 0, gpwrdn.d32);
  51014. + }
  51015. +
  51016. + /* check which value is for device mode and which for Host mode */
  51017. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  51018. + /* Disable Interrupt from Power Down Logic */
  51019. + gpwrdn.d32 = 0;
  51020. + gpwrdn.b.pmuintsel = 1;
  51021. + gpwrdn.b.pmuactv = 1;
  51022. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51023. + gpwrdn, gpwrdn.d32, 0);
  51024. +
  51025. + /*
  51026. + * Initialize the Core for Host mode.
  51027. + */
  51028. + core_if->op_state = A_HOST;
  51029. + dwc_otg_core_init(core_if);
  51030. + dwc_otg_enable_global_interrupts(core_if);
  51031. + cil_hcd_start(core_if);
  51032. + } else {
  51033. + gotgctl_data_t gotgctl;
  51034. + /* Mask SRP detected interrupt from Power Down Logic */
  51035. + gpwrdn.d32 = 0;
  51036. + gpwrdn.b.srp_det_msk = 1;
  51037. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51038. + gpwrdn, gpwrdn.d32, 0);
  51039. +
  51040. + /* Disable Power Down Logic */
  51041. + gpwrdn.d32 = 0;
  51042. + gpwrdn.b.pmuintsel = 1;
  51043. + gpwrdn.b.pmuactv = 1;
  51044. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51045. + gpwrdn, gpwrdn.d32, 0);
  51046. +
  51047. + /*
  51048. + * Initialize the Core for Device mode.
  51049. + */
  51050. + core_if->op_state = B_PERIPHERAL;
  51051. + dwc_otg_core_init(core_if);
  51052. + dwc_otg_enable_global_interrupts(core_if);
  51053. + cil_pcd_start(core_if);
  51054. +
  51055. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  51056. + if (!gotgctl.b.bsesvld) {
  51057. + dwc_otg_initiate_srp(core_if);
  51058. + }
  51059. + }
  51060. + }
  51061. + if (core_if->power_down == 2) {
  51062. + if (gpwrdn.b.bsessvld) {
  51063. + /* Mask SRP detected interrupt from Power Down Logic */
  51064. + gpwrdn.d32 = 0;
  51065. + gpwrdn.b.srp_det_msk = 1;
  51066. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51067. +
  51068. + /* Disable Power Down Logic */
  51069. + gpwrdn.d32 = 0;
  51070. + gpwrdn.b.pmuactv = 1;
  51071. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  51072. +
  51073. + /*
  51074. + * Initialize the Core for Device mode.
  51075. + */
  51076. + core_if->op_state = B_PERIPHERAL;
  51077. + dwc_otg_core_init(core_if);
  51078. + dwc_otg_enable_global_interrupts(core_if);
  51079. + cil_pcd_start(core_if);
  51080. + }
  51081. + }
  51082. + }
  51083. +exit:
  51084. + /* Clear interrupt */
  51085. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51086. + adpctl.b.adp_prb_int = 1;
  51087. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51088. +
  51089. + return 0;
  51090. +}
  51091. +
  51092. +/**
  51093. + * This function hadles ADP Sense Interrupt
  51094. + */
  51095. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  51096. +{
  51097. + adpctl_data_t adpctl;
  51098. + /* Stop ADP Sense timer */
  51099. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  51100. +
  51101. + /* Restart ADP Sense timer */
  51102. + dwc_otg_adp_sense_timer_start(core_if);
  51103. +
  51104. + /* Clear interrupt */
  51105. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51106. + adpctl.b.adp_sns_int = 1;
  51107. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51108. +
  51109. + return 0;
  51110. +}
  51111. +
  51112. +/**
  51113. + * This function handles ADP Probe Interrupts
  51114. + */
  51115. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  51116. + uint32_t val)
  51117. +{
  51118. + adpctl_data_t adpctl = {.d32 = 0 };
  51119. + adpctl.d32 = val;
  51120. + set_timer_value(core_if, adpctl.b.rtim);
  51121. +
  51122. + /* Clear interrupt */
  51123. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51124. + adpctl.b.adp_tmout_int = 1;
  51125. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51126. +
  51127. + return 0;
  51128. +}
  51129. +
  51130. +/**
  51131. + * ADP Interrupt handler.
  51132. + *
  51133. + */
  51134. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  51135. +{
  51136. + int retval = 0;
  51137. + adpctl_data_t adpctl = {.d32 = 0};
  51138. +
  51139. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  51140. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  51141. +
  51142. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  51143. + DWC_PRINTF("ADP Sense interrupt\n");
  51144. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  51145. + }
  51146. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  51147. + DWC_PRINTF("ADP timeout interrupt\n");
  51148. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  51149. + }
  51150. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  51151. + DWC_PRINTF("ADP Probe interrupt\n");
  51152. + adpctl.b.adp_prb_int = 1;
  51153. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  51154. + }
  51155. +
  51156. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  51157. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  51158. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  51159. +
  51160. + return retval;
  51161. +}
  51162. +
  51163. +/**
  51164. + *
  51165. + * @param core_if Programming view of DWC_otg controller.
  51166. + */
  51167. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  51168. +{
  51169. +
  51170. +#ifndef DWC_HOST_ONLY
  51171. + hprt0_data_t hprt0;
  51172. + gpwrdn_data_t gpwrdn;
  51173. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  51174. +
  51175. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  51176. + /* check which value is for device mode and which for Host mode */
  51177. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  51178. + DWC_PRINTF("SRP: Host mode\n");
  51179. +
  51180. + if (core_if->adp_enable) {
  51181. + dwc_otg_adp_probe_stop(core_if);
  51182. +
  51183. + /* Power on the core */
  51184. + if (core_if->power_down == 2) {
  51185. + gpwrdn.b.pwrdnswtch = 1;
  51186. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51187. + gpwrdn, 0, gpwrdn.d32);
  51188. + }
  51189. +
  51190. + core_if->op_state = A_HOST;
  51191. + dwc_otg_core_init(core_if);
  51192. + dwc_otg_enable_global_interrupts(core_if);
  51193. + cil_hcd_start(core_if);
  51194. + }
  51195. +
  51196. + /* Turn on the port power bit. */
  51197. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  51198. + hprt0.b.prtpwr = 1;
  51199. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  51200. +
  51201. + /* Start the Connection timer. So a message can be displayed
  51202. + * if connect does not occur within 10 seconds. */
  51203. + cil_hcd_session_start(core_if);
  51204. + } else {
  51205. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  51206. + if (core_if->adp_enable) {
  51207. + dwc_otg_adp_probe_stop(core_if);
  51208. +
  51209. + /* Power on the core */
  51210. + if (core_if->power_down == 2) {
  51211. + gpwrdn.b.pwrdnswtch = 1;
  51212. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  51213. + gpwrdn, 0, gpwrdn.d32);
  51214. + }
  51215. +
  51216. + gpwrdn.d32 = 0;
  51217. + gpwrdn.b.pmuactv = 0;
  51218. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  51219. + gpwrdn.d32);
  51220. +
  51221. + core_if->op_state = B_PERIPHERAL;
  51222. + dwc_otg_core_init(core_if);
  51223. + dwc_otg_enable_global_interrupts(core_if);
  51224. + cil_pcd_start(core_if);
  51225. + }
  51226. + }
  51227. +#endif
  51228. + return 1;
  51229. +}
  51230. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_adp.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  51231. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_adp.h 1970-01-01 01:00:00.000000000 +0100
  51232. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_adp.h 2014-03-11 16:55:38.000000000 +0100
  51233. @@ -0,0 +1,80 @@
  51234. +/* ==========================================================================
  51235. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  51236. + * $Revision: #7 $
  51237. + * $Date: 2011/10/24 $
  51238. + * $Change: 1871159 $
  51239. + *
  51240. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51241. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51242. + * otherwise expressly agreed to in writing between Synopsys and you.
  51243. + *
  51244. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51245. + * any End User Software License Agreement or Agreement for Licensed Product
  51246. + * with Synopsys or any supplement thereto. You are permitted to use and
  51247. + * redistribute this Software in source and binary forms, with or without
  51248. + * modification, provided that redistributions of source code must retain this
  51249. + * notice. You may not view, use, disclose, copy or distribute this file or
  51250. + * any information contained herein except pursuant to this license grant from
  51251. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51252. + * below, then you are not authorized to use the Software.
  51253. + *
  51254. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51255. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51256. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51257. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51258. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51259. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51260. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51261. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51262. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51263. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51264. + * DAMAGE.
  51265. + * ========================================================================== */
  51266. +
  51267. +#ifndef __DWC_OTG_ADP_H__
  51268. +#define __DWC_OTG_ADP_H__
  51269. +
  51270. +/**
  51271. + * @file
  51272. + *
  51273. + * This file contains the Attach Detect Protocol interfaces and defines
  51274. + * (functions) and structures for Linux.
  51275. + *
  51276. + */
  51277. +
  51278. +#define DWC_OTG_ADP_UNATTACHED 0
  51279. +#define DWC_OTG_ADP_ATTACHED 1
  51280. +#define DWC_OTG_ADP_UNKOWN 2
  51281. +
  51282. +typedef struct dwc_otg_adp {
  51283. + uint32_t adp_started;
  51284. + uint32_t initial_probe;
  51285. + int32_t probe_timer_values[2];
  51286. + uint32_t probe_enabled;
  51287. + uint32_t sense_enabled;
  51288. + dwc_timer_t *sense_timer;
  51289. + uint32_t sense_timer_started;
  51290. + dwc_timer_t *vbuson_timer;
  51291. + uint32_t vbuson_timer_started;
  51292. + uint32_t attached;
  51293. + uint32_t probe_counter;
  51294. + uint32_t gpwrdn;
  51295. +} dwc_otg_adp_t;
  51296. +
  51297. +/**
  51298. + * Attach Detect Protocol functions
  51299. + */
  51300. +
  51301. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  51302. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  51303. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  51304. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  51305. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  51306. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  51307. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  51308. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  51309. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  51310. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  51311. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  51312. +
  51313. +#endif //__DWC_OTG_ADP_H__
  51314. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_attr.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  51315. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_attr.c 1970-01-01 01:00:00.000000000 +0100
  51316. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_attr.c 2014-03-11 16:55:38.000000000 +0100
  51317. @@ -0,0 +1,1210 @@
  51318. +/* ==========================================================================
  51319. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  51320. + * $Revision: #44 $
  51321. + * $Date: 2010/11/29 $
  51322. + * $Change: 1636033 $
  51323. + *
  51324. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51325. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51326. + * otherwise expressly agreed to in writing between Synopsys and you.
  51327. + *
  51328. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51329. + * any End User Software License Agreement or Agreement for Licensed Product
  51330. + * with Synopsys or any supplement thereto. You are permitted to use and
  51331. + * redistribute this Software in source and binary forms, with or without
  51332. + * modification, provided that redistributions of source code must retain this
  51333. + * notice. You may not view, use, disclose, copy or distribute this file or
  51334. + * any information contained herein except pursuant to this license grant from
  51335. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51336. + * below, then you are not authorized to use the Software.
  51337. + *
  51338. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51339. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51340. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51341. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51342. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51343. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51344. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51345. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51346. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51347. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51348. + * DAMAGE.
  51349. + * ========================================================================== */
  51350. +
  51351. +/** @file
  51352. + *
  51353. + * The diagnostic interface will provide access to the controller for
  51354. + * bringing up the hardware and testing. The Linux driver attributes
  51355. + * feature will be used to provide the Linux Diagnostic
  51356. + * Interface. These attributes are accessed through sysfs.
  51357. + */
  51358. +
  51359. +/** @page "Linux Module Attributes"
  51360. + *
  51361. + * The Linux module attributes feature is used to provide the Linux
  51362. + * Diagnostic Interface. These attributes are accessed through sysfs.
  51363. + * The diagnostic interface will provide access to the controller for
  51364. + * bringing up the hardware and testing.
  51365. +
  51366. + The following table shows the attributes.
  51367. + <table>
  51368. + <tr>
  51369. + <td><b> Name</b></td>
  51370. + <td><b> Description</b></td>
  51371. + <td><b> Access</b></td>
  51372. + </tr>
  51373. +
  51374. + <tr>
  51375. + <td> mode </td>
  51376. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  51377. + <td> Read</td>
  51378. + </tr>
  51379. +
  51380. + <tr>
  51381. + <td> hnpcapable </td>
  51382. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  51383. + Read returns the current value.</td>
  51384. + <td> Read/Write</td>
  51385. + </tr>
  51386. +
  51387. + <tr>
  51388. + <td> srpcapable </td>
  51389. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  51390. + Read returns the current value.</td>
  51391. + <td> Read/Write</td>
  51392. + </tr>
  51393. +
  51394. + <tr>
  51395. + <td> hsic_connect </td>
  51396. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  51397. + Read returns the current value.</td>
  51398. + <td> Read/Write</td>
  51399. + </tr>
  51400. +
  51401. + <tr>
  51402. + <td> inv_sel_hsic </td>
  51403. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  51404. + Read returns the current value.</td>
  51405. + <td> Read/Write</td>
  51406. + </tr>
  51407. +
  51408. + <tr>
  51409. + <td> hnp </td>
  51410. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  51411. + <td> Read/Write</td>
  51412. + </tr>
  51413. +
  51414. + <tr>
  51415. + <td> srp </td>
  51416. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  51417. + <td> Read/Write</td>
  51418. + </tr>
  51419. +
  51420. + <tr>
  51421. + <td> buspower </td>
  51422. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  51423. + <td> Read/Write</td>
  51424. + </tr>
  51425. +
  51426. + <tr>
  51427. + <td> bussuspend </td>
  51428. + <td> Suspends the USB bus.</td>
  51429. + <td> Read/Write</td>
  51430. + </tr>
  51431. +
  51432. + <tr>
  51433. + <td> busconnected </td>
  51434. + <td> Gets the connection status of the bus</td>
  51435. + <td> Read</td>
  51436. + </tr>
  51437. +
  51438. + <tr>
  51439. + <td> gotgctl </td>
  51440. + <td> Gets or sets the Core Control Status Register.</td>
  51441. + <td> Read/Write</td>
  51442. + </tr>
  51443. +
  51444. + <tr>
  51445. + <td> gusbcfg </td>
  51446. + <td> Gets or sets the Core USB Configuration Register</td>
  51447. + <td> Read/Write</td>
  51448. + </tr>
  51449. +
  51450. + <tr>
  51451. + <td> grxfsiz </td>
  51452. + <td> Gets or sets the Receive FIFO Size Register</td>
  51453. + <td> Read/Write</td>
  51454. + </tr>
  51455. +
  51456. + <tr>
  51457. + <td> gnptxfsiz </td>
  51458. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  51459. + <td> Read/Write</td>
  51460. + </tr>
  51461. +
  51462. + <tr>
  51463. + <td> gpvndctl </td>
  51464. + <td> Gets or sets the PHY Vendor Control Register</td>
  51465. + <td> Read/Write</td>
  51466. + </tr>
  51467. +
  51468. + <tr>
  51469. + <td> ggpio </td>
  51470. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  51471. + or sets the upper 16 bits.</td>
  51472. + <td> Read/Write</td>
  51473. + </tr>
  51474. +
  51475. + <tr>
  51476. + <td> guid </td>
  51477. + <td> Gets or sets the value of the User ID Register</td>
  51478. + <td> Read/Write</td>
  51479. + </tr>
  51480. +
  51481. + <tr>
  51482. + <td> gsnpsid </td>
  51483. + <td> Gets the value of the Synopsys ID Regester</td>
  51484. + <td> Read</td>
  51485. + </tr>
  51486. +
  51487. + <tr>
  51488. + <td> devspeed </td>
  51489. + <td> Gets or sets the device speed setting in the DCFG register</td>
  51490. + <td> Read/Write</td>
  51491. + </tr>
  51492. +
  51493. + <tr>
  51494. + <td> enumspeed </td>
  51495. + <td> Gets the device enumeration Speed.</td>
  51496. + <td> Read</td>
  51497. + </tr>
  51498. +
  51499. + <tr>
  51500. + <td> hptxfsiz </td>
  51501. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  51502. + <td> Read</td>
  51503. + </tr>
  51504. +
  51505. + <tr>
  51506. + <td> hprt0 </td>
  51507. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  51508. + <td> Read/Write</td>
  51509. + </tr>
  51510. +
  51511. + <tr>
  51512. + <td> regoffset </td>
  51513. + <td> Sets the register offset for the next Register Access</td>
  51514. + <td> Read/Write</td>
  51515. + </tr>
  51516. +
  51517. + <tr>
  51518. + <td> regvalue </td>
  51519. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  51520. + <td> Read/Write</td>
  51521. + </tr>
  51522. +
  51523. + <tr>
  51524. + <td> remote_wakeup </td>
  51525. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  51526. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  51527. + Wakeup signalling bit in the Device Control Register is set for 1
  51528. + milli-second.</td>
  51529. + <td> Read/Write</td>
  51530. + </tr>
  51531. +
  51532. + <tr>
  51533. + <td> rem_wakeup_pwrdn </td>
  51534. + <td> On read, shows the status core - hibernated or not. On write, initiates
  51535. + a remote wakeup of the device from Hibernation. </td>
  51536. + <td> Read/Write</td>
  51537. + </tr>
  51538. +
  51539. + <tr>
  51540. + <td> mode_ch_tim_en </td>
  51541. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  51542. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  51543. + after Suspend or LPM. </td>
  51544. + <td> Read/Write</td>
  51545. + </tr>
  51546. +
  51547. + <tr>
  51548. + <td> fr_interval </td>
  51549. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  51550. + reload HFIR register during runtime. The application can write a value to this
  51551. + register only after the Port Enable bit of the Host Port Control and Status
  51552. + register (HPRT.PrtEnaPort) has been set </td>
  51553. + <td> Read/Write</td>
  51554. + </tr>
  51555. +
  51556. + <tr>
  51557. + <td> disconnect_us </td>
  51558. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  51559. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  51560. + <td> Read/Write</td>
  51561. + </tr>
  51562. +
  51563. + <tr>
  51564. + <td> regdump </td>
  51565. + <td> Dumps the contents of core registers.</td>
  51566. + <td> Read</td>
  51567. + </tr>
  51568. +
  51569. + <tr>
  51570. + <td> spramdump </td>
  51571. + <td> Dumps the contents of core registers.</td>
  51572. + <td> Read</td>
  51573. + </tr>
  51574. +
  51575. + <tr>
  51576. + <td> hcddump </td>
  51577. + <td> Dumps the current HCD state.</td>
  51578. + <td> Read</td>
  51579. + </tr>
  51580. +
  51581. + <tr>
  51582. + <td> hcd_frrem </td>
  51583. + <td> Shows the average value of the Frame Remaining
  51584. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  51585. + occurs. This can be used to determine the average interrupt latency. Also
  51586. + shows the average Frame Remaining value for start_transfer and the "a" and
  51587. + "b" sample points. The "a" and "b" sample points may be used during debugging
  51588. + bto determine how long it takes to execute a section of the HCD code.</td>
  51589. + <td> Read</td>
  51590. + </tr>
  51591. +
  51592. + <tr>
  51593. + <td> rd_reg_test </td>
  51594. + <td> Displays the time required to read the GNPTXFSIZ register many times
  51595. + (the output shows the number of times the register is read).
  51596. + <td> Read</td>
  51597. + </tr>
  51598. +
  51599. + <tr>
  51600. + <td> wr_reg_test </td>
  51601. + <td> Displays the time required to write the GNPTXFSIZ register many times
  51602. + (the output shows the number of times the register is written).
  51603. + <td> Read</td>
  51604. + </tr>
  51605. +
  51606. + <tr>
  51607. + <td> lpm_response </td>
  51608. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  51609. + <td> Write</td>
  51610. + </tr>
  51611. +
  51612. + <tr>
  51613. + <td> sleep_status </td>
  51614. + <td> Shows sleep status of device.
  51615. + <td> Read</td>
  51616. + </tr>
  51617. +
  51618. + </table>
  51619. +
  51620. + Example usage:
  51621. + To get the current mode:
  51622. + cat /sys/devices/lm0/mode
  51623. +
  51624. + To power down the USB:
  51625. + echo 0 > /sys/devices/lm0/buspower
  51626. + */
  51627. +
  51628. +#include "dwc_otg_os_dep.h"
  51629. +#include "dwc_os.h"
  51630. +#include "dwc_otg_driver.h"
  51631. +#include "dwc_otg_attr.h"
  51632. +#include "dwc_otg_core_if.h"
  51633. +#include "dwc_otg_pcd_if.h"
  51634. +#include "dwc_otg_hcd_if.h"
  51635. +
  51636. +/*
  51637. + * MACROs for defining sysfs attribute
  51638. + */
  51639. +#ifdef LM_INTERFACE
  51640. +
  51641. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  51642. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  51643. +{ \
  51644. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  51645. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  51646. + uint32_t val; \
  51647. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  51648. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  51649. +}
  51650. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  51651. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  51652. + const char *buf, size_t count) \
  51653. +{ \
  51654. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  51655. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  51656. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  51657. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  51658. + return count; \
  51659. +}
  51660. +
  51661. +#elif defined(PCI_INTERFACE)
  51662. +
  51663. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  51664. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  51665. +{ \
  51666. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  51667. + uint32_t val; \
  51668. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  51669. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  51670. +}
  51671. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  51672. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  51673. + const char *buf, size_t count) \
  51674. +{ \
  51675. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  51676. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  51677. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  51678. + return count; \
  51679. +}
  51680. +
  51681. +#elif defined(PLATFORM_INTERFACE)
  51682. +
  51683. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  51684. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  51685. +{ \
  51686. + struct platform_device *platform_dev = \
  51687. + container_of(_dev, struct platform_device, dev); \
  51688. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  51689. + uint32_t val; \
  51690. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  51691. + __func__, _dev, platform_dev, otg_dev); \
  51692. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  51693. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  51694. +}
  51695. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  51696. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  51697. + const char *buf, size_t count) \
  51698. +{ \
  51699. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  51700. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  51701. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  51702. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  51703. + return count; \
  51704. +}
  51705. +#endif
  51706. +
  51707. +/*
  51708. + * MACROs for defining sysfs attribute for 32-bit registers
  51709. + */
  51710. +#ifdef LM_INTERFACE
  51711. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  51712. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  51713. +{ \
  51714. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  51715. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  51716. + uint32_t val; \
  51717. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  51718. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  51719. +}
  51720. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  51721. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  51722. + const char *buf, size_t count) \
  51723. +{ \
  51724. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  51725. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  51726. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  51727. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  51728. + return count; \
  51729. +}
  51730. +#elif defined(PCI_INTERFACE)
  51731. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  51732. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  51733. +{ \
  51734. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  51735. + uint32_t val; \
  51736. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  51737. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  51738. +}
  51739. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  51740. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  51741. + const char *buf, size_t count) \
  51742. +{ \
  51743. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  51744. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  51745. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  51746. + return count; \
  51747. +}
  51748. +
  51749. +#elif defined(PLATFORM_INTERFACE)
  51750. +#include "dwc_otg_dbg.h"
  51751. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  51752. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  51753. +{ \
  51754. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  51755. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  51756. + uint32_t val; \
  51757. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  51758. + __func__, _dev, platform_dev, otg_dev); \
  51759. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  51760. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  51761. +}
  51762. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  51763. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  51764. + const char *buf, size_t count) \
  51765. +{ \
  51766. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  51767. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  51768. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  51769. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  51770. + return count; \
  51771. +}
  51772. +
  51773. +#endif
  51774. +
  51775. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  51776. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  51777. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  51778. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  51779. +
  51780. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  51781. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  51782. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  51783. +
  51784. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  51785. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  51786. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  51787. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  51788. +
  51789. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  51790. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  51791. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  51792. +
  51793. +/** @name Functions for Show/Store of Attributes */
  51794. +/**@{*/
  51795. +
  51796. +/**
  51797. + * Helper function returning the otg_device structure of the given device
  51798. + */
  51799. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  51800. +{
  51801. + dwc_otg_device_t *otg_dev;
  51802. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  51803. + return otg_dev;
  51804. +}
  51805. +
  51806. +/**
  51807. + * Show the register offset of the Register Access.
  51808. + */
  51809. +static ssize_t regoffset_show(struct device *_dev,
  51810. + struct device_attribute *attr, char *buf)
  51811. +{
  51812. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  51813. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  51814. + otg_dev->os_dep.reg_offset);
  51815. +}
  51816. +
  51817. +/**
  51818. + * Set the register offset for the next Register Access Read/Write
  51819. + */
  51820. +static ssize_t regoffset_store(struct device *_dev,
  51821. + struct device_attribute *attr,
  51822. + const char *buf, size_t count)
  51823. +{
  51824. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  51825. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  51826. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  51827. + if (offset < SZ_256K) {
  51828. +#elif defined(PCI_INTERFACE)
  51829. + if (offset < 0x00040000) {
  51830. +#endif
  51831. + otg_dev->os_dep.reg_offset = offset;
  51832. + } else {
  51833. + dev_err(_dev, "invalid offset\n");
  51834. + }
  51835. +
  51836. + return count;
  51837. +}
  51838. +
  51839. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  51840. +
  51841. +/**
  51842. + * Show the value of the register at the offset in the reg_offset
  51843. + * attribute.
  51844. + */
  51845. +static ssize_t regvalue_show(struct device *_dev,
  51846. + struct device_attribute *attr, char *buf)
  51847. +{
  51848. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  51849. + uint32_t val;
  51850. + volatile uint32_t *addr;
  51851. +
  51852. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  51853. + /* Calculate the address */
  51854. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  51855. + (uint8_t *) otg_dev->os_dep.base);
  51856. + val = DWC_READ_REG32(addr);
  51857. + return snprintf(buf,
  51858. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  51859. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  51860. + val);
  51861. + } else {
  51862. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  51863. + return sprintf(buf, "invalid offset\n");
  51864. + }
  51865. +}
  51866. +
  51867. +/**
  51868. + * Store the value in the register at the offset in the reg_offset
  51869. + * attribute.
  51870. + *
  51871. + */
  51872. +static ssize_t regvalue_store(struct device *_dev,
  51873. + struct device_attribute *attr,
  51874. + const char *buf, size_t count)
  51875. +{
  51876. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  51877. + volatile uint32_t *addr;
  51878. + uint32_t val = simple_strtoul(buf, NULL, 16);
  51879. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  51880. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  51881. + /* Calculate the address */
  51882. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  51883. + (uint8_t *) otg_dev->os_dep.base);
  51884. + DWC_WRITE_REG32(addr, val);
  51885. + } else {
  51886. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  51887. + otg_dev->os_dep.reg_offset);
  51888. + }
  51889. + return count;
  51890. +}
  51891. +
  51892. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  51893. +
  51894. +/*
  51895. + * Attributes
  51896. + */
  51897. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  51898. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  51899. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  51900. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  51901. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  51902. +
  51903. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  51904. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  51905. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  51906. +
  51907. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  51908. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  51909. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  51910. + "GUSBCFG");
  51911. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  51912. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  51913. + "GRXFSIZ");
  51914. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  51915. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  51916. + "GNPTXFSIZ");
  51917. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  51918. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  51919. + "GPVNDCTL");
  51920. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  51921. + &(otg_dev->core_if->core_global_regs->ggpio),
  51922. + "GGPIO");
  51923. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  51924. + "GUID");
  51925. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  51926. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  51927. + "GSNPSID");
  51928. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  51929. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  51930. +
  51931. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  51932. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  51933. + "HPTXFSIZ");
  51934. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  51935. +
  51936. +/**
  51937. + * @todo Add code to initiate the HNP.
  51938. + */
  51939. +/**
  51940. + * Show the HNP status bit
  51941. + */
  51942. +static ssize_t hnp_show(struct device *_dev,
  51943. + struct device_attribute *attr, char *buf)
  51944. +{
  51945. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  51946. + return sprintf(buf, "HstNegScs = 0x%x\n",
  51947. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  51948. +}
  51949. +
  51950. +/**
  51951. + * Set the HNP Request bit
  51952. + */
  51953. +static ssize_t hnp_store(struct device *_dev,
  51954. + struct device_attribute *attr,
  51955. + const char *buf, size_t count)
  51956. +{
  51957. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  51958. + uint32_t in = simple_strtoul(buf, NULL, 16);
  51959. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  51960. + return count;
  51961. +}
  51962. +
  51963. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  51964. +
  51965. +/**
  51966. + * @todo Add code to initiate the SRP.
  51967. + */
  51968. +/**
  51969. + * Show the SRP status bit
  51970. + */
  51971. +static ssize_t srp_show(struct device *_dev,
  51972. + struct device_attribute *attr, char *buf)
  51973. +{
  51974. +#ifndef DWC_HOST_ONLY
  51975. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  51976. + return sprintf(buf, "SesReqScs = 0x%x\n",
  51977. + dwc_otg_get_srpstatus(otg_dev->core_if));
  51978. +#else
  51979. + return sprintf(buf, "Host Only Mode!\n");
  51980. +#endif
  51981. +}
  51982. +
  51983. +/**
  51984. + * Set the SRP Request bit
  51985. + */
  51986. +static ssize_t srp_store(struct device *_dev,
  51987. + struct device_attribute *attr,
  51988. + const char *buf, size_t count)
  51989. +{
  51990. +#ifndef DWC_HOST_ONLY
  51991. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  51992. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  51993. +#endif
  51994. + return count;
  51995. +}
  51996. +
  51997. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  51998. +
  51999. +/**
  52000. + * @todo Need to do more for power on/off?
  52001. + */
  52002. +/**
  52003. + * Show the Bus Power status
  52004. + */
  52005. +static ssize_t buspower_show(struct device *_dev,
  52006. + struct device_attribute *attr, char *buf)
  52007. +{
  52008. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52009. + return sprintf(buf, "Bus Power = 0x%x\n",
  52010. + dwc_otg_get_prtpower(otg_dev->core_if));
  52011. +}
  52012. +
  52013. +/**
  52014. + * Set the Bus Power status
  52015. + */
  52016. +static ssize_t buspower_store(struct device *_dev,
  52017. + struct device_attribute *attr,
  52018. + const char *buf, size_t count)
  52019. +{
  52020. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52021. + uint32_t on = simple_strtoul(buf, NULL, 16);
  52022. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  52023. + return count;
  52024. +}
  52025. +
  52026. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  52027. +
  52028. +/**
  52029. + * @todo Need to do more for suspend?
  52030. + */
  52031. +/**
  52032. + * Show the Bus Suspend status
  52033. + */
  52034. +static ssize_t bussuspend_show(struct device *_dev,
  52035. + struct device_attribute *attr, char *buf)
  52036. +{
  52037. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52038. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  52039. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  52040. +}
  52041. +
  52042. +/**
  52043. + * Set the Bus Suspend status
  52044. + */
  52045. +static ssize_t bussuspend_store(struct device *_dev,
  52046. + struct device_attribute *attr,
  52047. + const char *buf, size_t count)
  52048. +{
  52049. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52050. + uint32_t in = simple_strtoul(buf, NULL, 16);
  52051. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  52052. + return count;
  52053. +}
  52054. +
  52055. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  52056. +
  52057. +/**
  52058. + * Show the Mode Change Ready Timer status
  52059. + */
  52060. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  52061. + struct device_attribute *attr, char *buf)
  52062. +{
  52063. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52064. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  52065. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  52066. +}
  52067. +
  52068. +/**
  52069. + * Set the Mode Change Ready Timer status
  52070. + */
  52071. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  52072. + struct device_attribute *attr,
  52073. + const char *buf, size_t count)
  52074. +{
  52075. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52076. + uint32_t in = simple_strtoul(buf, NULL, 16);
  52077. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  52078. + return count;
  52079. +}
  52080. +
  52081. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  52082. +
  52083. +/**
  52084. + * Show the value of HFIR Frame Interval bitfield
  52085. + */
  52086. +static ssize_t fr_interval_show(struct device *_dev,
  52087. + struct device_attribute *attr, char *buf)
  52088. +{
  52089. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52090. + return sprintf(buf, "Frame Interval = 0x%x\n",
  52091. + dwc_otg_get_fr_interval(otg_dev->core_if));
  52092. +}
  52093. +
  52094. +/**
  52095. + * Set the HFIR Frame Interval value
  52096. + */
  52097. +static ssize_t fr_interval_store(struct device *_dev,
  52098. + struct device_attribute *attr,
  52099. + const char *buf, size_t count)
  52100. +{
  52101. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52102. + uint32_t in = simple_strtoul(buf, NULL, 10);
  52103. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  52104. + return count;
  52105. +}
  52106. +
  52107. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  52108. +
  52109. +/**
  52110. + * Show the status of Remote Wakeup.
  52111. + */
  52112. +static ssize_t remote_wakeup_show(struct device *_dev,
  52113. + struct device_attribute *attr, char *buf)
  52114. +{
  52115. +#ifndef DWC_HOST_ONLY
  52116. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52117. +
  52118. + return sprintf(buf,
  52119. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  52120. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  52121. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  52122. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  52123. +#else
  52124. + return sprintf(buf, "Host Only Mode!\n");
  52125. +#endif /* DWC_HOST_ONLY */
  52126. +}
  52127. +
  52128. +/**
  52129. + * Initiate a remote wakeup of the host. The Device control register
  52130. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  52131. + * flag is set.
  52132. + *
  52133. + */
  52134. +static ssize_t remote_wakeup_store(struct device *_dev,
  52135. + struct device_attribute *attr,
  52136. + const char *buf, size_t count)
  52137. +{
  52138. +#ifndef DWC_HOST_ONLY
  52139. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52140. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52141. +
  52142. + if (val & 1) {
  52143. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  52144. + } else {
  52145. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  52146. + }
  52147. +#endif /* DWC_HOST_ONLY */
  52148. + return count;
  52149. +}
  52150. +
  52151. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  52152. + remote_wakeup_store);
  52153. +
  52154. +/**
  52155. + * Show the whether core is hibernated or not.
  52156. + */
  52157. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  52158. + struct device_attribute *attr, char *buf)
  52159. +{
  52160. +#ifndef DWC_HOST_ONLY
  52161. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52162. +
  52163. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  52164. + DWC_PRINTF("Core is in hibernation\n");
  52165. + } else {
  52166. + DWC_PRINTF("Core is not in hibernation\n");
  52167. + }
  52168. +#endif /* DWC_HOST_ONLY */
  52169. + return 0;
  52170. +}
  52171. +
  52172. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  52173. + int rem_wakeup, int reset);
  52174. +
  52175. +/**
  52176. + * Initiate a remote wakeup of the device to exit from hibernation.
  52177. + */
  52178. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  52179. + struct device_attribute *attr,
  52180. + const char *buf, size_t count)
  52181. +{
  52182. +#ifndef DWC_HOST_ONLY
  52183. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52184. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  52185. +#endif
  52186. + return count;
  52187. +}
  52188. +
  52189. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  52190. + rem_wakeup_pwrdn_store);
  52191. +
  52192. +static ssize_t disconnect_us(struct device *_dev,
  52193. + struct device_attribute *attr,
  52194. + const char *buf, size_t count)
  52195. +{
  52196. +
  52197. +#ifndef DWC_HOST_ONLY
  52198. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52199. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52200. + DWC_PRINTF("The Passed value is %04x\n", val);
  52201. +
  52202. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  52203. +
  52204. +#endif /* DWC_HOST_ONLY */
  52205. + return count;
  52206. +}
  52207. +
  52208. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  52209. +
  52210. +/**
  52211. + * Dump global registers and either host or device registers (depending on the
  52212. + * current mode of the core).
  52213. + */
  52214. +static ssize_t regdump_show(struct device *_dev,
  52215. + struct device_attribute *attr, char *buf)
  52216. +{
  52217. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52218. +
  52219. + dwc_otg_dump_global_registers(otg_dev->core_if);
  52220. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  52221. + dwc_otg_dump_host_registers(otg_dev->core_if);
  52222. + } else {
  52223. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  52224. +
  52225. + }
  52226. + return sprintf(buf, "Register Dump\n");
  52227. +}
  52228. +
  52229. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  52230. +
  52231. +/**
  52232. + * Dump global registers and either host or device registers (depending on the
  52233. + * current mode of the core).
  52234. + */
  52235. +static ssize_t spramdump_show(struct device *_dev,
  52236. + struct device_attribute *attr, char *buf)
  52237. +{
  52238. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52239. +
  52240. + //dwc_otg_dump_spram(otg_dev->core_if);
  52241. +
  52242. + return sprintf(buf, "SPRAM Dump\n");
  52243. +}
  52244. +
  52245. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  52246. +
  52247. +/**
  52248. + * Dump the current hcd state.
  52249. + */
  52250. +static ssize_t hcddump_show(struct device *_dev,
  52251. + struct device_attribute *attr, char *buf)
  52252. +{
  52253. +#ifndef DWC_DEVICE_ONLY
  52254. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52255. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  52256. +#endif /* DWC_DEVICE_ONLY */
  52257. + return sprintf(buf, "HCD Dump\n");
  52258. +}
  52259. +
  52260. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  52261. +
  52262. +/**
  52263. + * Dump the average frame remaining at SOF. This can be used to
  52264. + * determine average interrupt latency. Frame remaining is also shown for
  52265. + * start transfer and two additional sample points.
  52266. + */
  52267. +static ssize_t hcd_frrem_show(struct device *_dev,
  52268. + struct device_attribute *attr, char *buf)
  52269. +{
  52270. +#ifndef DWC_DEVICE_ONLY
  52271. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52272. +
  52273. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  52274. +#endif /* DWC_DEVICE_ONLY */
  52275. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  52276. +}
  52277. +
  52278. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  52279. +
  52280. +/**
  52281. + * Displays the time required to read the GNPTXFSIZ register many times (the
  52282. + * output shows the number of times the register is read).
  52283. + */
  52284. +#define RW_REG_COUNT 10000000
  52285. +#define MSEC_PER_JIFFIE 1000/HZ
  52286. +static ssize_t rd_reg_test_show(struct device *_dev,
  52287. + struct device_attribute *attr, char *buf)
  52288. +{
  52289. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52290. + int i;
  52291. + int time;
  52292. + int start_jiffies;
  52293. +
  52294. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  52295. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  52296. + start_jiffies = jiffies;
  52297. + for (i = 0; i < RW_REG_COUNT; i++) {
  52298. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  52299. + }
  52300. + time = jiffies - start_jiffies;
  52301. + return sprintf(buf,
  52302. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  52303. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  52304. +}
  52305. +
  52306. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  52307. +
  52308. +/**
  52309. + * Displays the time required to write the GNPTXFSIZ register many times (the
  52310. + * output shows the number of times the register is written).
  52311. + */
  52312. +static ssize_t wr_reg_test_show(struct device *_dev,
  52313. + struct device_attribute *attr, char *buf)
  52314. +{
  52315. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52316. + uint32_t reg_val;
  52317. + int i;
  52318. + int time;
  52319. + int start_jiffies;
  52320. +
  52321. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  52322. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  52323. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  52324. + start_jiffies = jiffies;
  52325. + for (i = 0; i < RW_REG_COUNT; i++) {
  52326. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  52327. + }
  52328. + time = jiffies - start_jiffies;
  52329. + return sprintf(buf,
  52330. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  52331. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  52332. +}
  52333. +
  52334. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  52335. +
  52336. +#ifdef CONFIG_USB_DWC_OTG_LPM
  52337. +
  52338. +/**
  52339. +* Show the lpm_response attribute.
  52340. +*/
  52341. +static ssize_t lpmresp_show(struct device *_dev,
  52342. + struct device_attribute *attr, char *buf)
  52343. +{
  52344. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52345. +
  52346. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  52347. + return sprintf(buf, "** LPM is DISABLED **\n");
  52348. +
  52349. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  52350. + return sprintf(buf, "** Current mode is not device mode\n");
  52351. + }
  52352. + return sprintf(buf, "lpm_response = %d\n",
  52353. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  52354. +}
  52355. +
  52356. +/**
  52357. +* Store the lpm_response attribute.
  52358. +*/
  52359. +static ssize_t lpmresp_store(struct device *_dev,
  52360. + struct device_attribute *attr,
  52361. + const char *buf, size_t count)
  52362. +{
  52363. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52364. + uint32_t val = simple_strtoul(buf, NULL, 16);
  52365. +
  52366. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  52367. + return 0;
  52368. + }
  52369. +
  52370. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  52371. + return 0;
  52372. + }
  52373. +
  52374. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  52375. + return count;
  52376. +}
  52377. +
  52378. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  52379. +
  52380. +/**
  52381. +* Show the sleep_status attribute.
  52382. +*/
  52383. +static ssize_t sleepstatus_show(struct device *_dev,
  52384. + struct device_attribute *attr, char *buf)
  52385. +{
  52386. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52387. + return sprintf(buf, "Sleep Status = %d\n",
  52388. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  52389. +}
  52390. +
  52391. +/**
  52392. + * Store the sleep_status attribure.
  52393. + */
  52394. +static ssize_t sleepstatus_store(struct device *_dev,
  52395. + struct device_attribute *attr,
  52396. + const char *buf, size_t count)
  52397. +{
  52398. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  52399. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  52400. +
  52401. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  52402. + if (dwc_otg_is_host_mode(core_if)) {
  52403. +
  52404. + DWC_PRINTF("Host initiated resume\n");
  52405. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  52406. + }
  52407. + }
  52408. +
  52409. + return count;
  52410. +}
  52411. +
  52412. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  52413. + sleepstatus_store);
  52414. +
  52415. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  52416. +
  52417. +/**@}*/
  52418. +
  52419. +/**
  52420. + * Create the device files
  52421. + */
  52422. +void dwc_otg_attr_create(
  52423. +#ifdef LM_INTERFACE
  52424. + struct lm_device *dev
  52425. +#elif defined(PCI_INTERFACE)
  52426. + struct pci_dev *dev
  52427. +#elif defined(PLATFORM_INTERFACE)
  52428. + struct platform_device *dev
  52429. +#endif
  52430. + )
  52431. +{
  52432. + int error;
  52433. +
  52434. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  52435. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  52436. + error = device_create_file(&dev->dev, &dev_attr_mode);
  52437. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  52438. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  52439. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  52440. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  52441. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  52442. + error = device_create_file(&dev->dev, &dev_attr_srp);
  52443. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  52444. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  52445. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  52446. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  52447. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  52448. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  52449. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  52450. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  52451. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  52452. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  52453. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  52454. + error = device_create_file(&dev->dev, &dev_attr_guid);
  52455. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  52456. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  52457. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  52458. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  52459. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  52460. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  52461. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  52462. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  52463. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  52464. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  52465. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  52466. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  52467. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  52468. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  52469. +#ifdef CONFIG_USB_DWC_OTG_LPM
  52470. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  52471. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  52472. +#endif
  52473. +}
  52474. +
  52475. +/**
  52476. + * Remove the device files
  52477. + */
  52478. +void dwc_otg_attr_remove(
  52479. +#ifdef LM_INTERFACE
  52480. + struct lm_device *dev
  52481. +#elif defined(PCI_INTERFACE)
  52482. + struct pci_dev *dev
  52483. +#elif defined(PLATFORM_INTERFACE)
  52484. + struct platform_device *dev
  52485. +#endif
  52486. + )
  52487. +{
  52488. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  52489. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  52490. + device_remove_file(&dev->dev, &dev_attr_mode);
  52491. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  52492. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  52493. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  52494. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  52495. + device_remove_file(&dev->dev, &dev_attr_hnp);
  52496. + device_remove_file(&dev->dev, &dev_attr_srp);
  52497. + device_remove_file(&dev->dev, &dev_attr_buspower);
  52498. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  52499. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  52500. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  52501. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  52502. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  52503. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  52504. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  52505. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  52506. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  52507. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  52508. + device_remove_file(&dev->dev, &dev_attr_guid);
  52509. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  52510. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  52511. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  52512. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  52513. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  52514. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  52515. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  52516. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  52517. + device_remove_file(&dev->dev, &dev_attr_regdump);
  52518. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  52519. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  52520. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  52521. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  52522. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  52523. +#ifdef CONFIG_USB_DWC_OTG_LPM
  52524. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  52525. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  52526. +#endif
  52527. +}
  52528. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_attr.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  52529. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_attr.h 1970-01-01 01:00:00.000000000 +0100
  52530. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_attr.h 2014-03-11 16:55:38.000000000 +0100
  52531. @@ -0,0 +1,89 @@
  52532. +/* ==========================================================================
  52533. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  52534. + * $Revision: #13 $
  52535. + * $Date: 2010/06/21 $
  52536. + * $Change: 1532021 $
  52537. + *
  52538. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  52539. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  52540. + * otherwise expressly agreed to in writing between Synopsys and you.
  52541. + *
  52542. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  52543. + * any End User Software License Agreement or Agreement for Licensed Product
  52544. + * with Synopsys or any supplement thereto. You are permitted to use and
  52545. + * redistribute this Software in source and binary forms, with or without
  52546. + * modification, provided that redistributions of source code must retain this
  52547. + * notice. You may not view, use, disclose, copy or distribute this file or
  52548. + * any information contained herein except pursuant to this license grant from
  52549. + * Synopsys. If you do not agree with this notice, including the disclaimer
  52550. + * below, then you are not authorized to use the Software.
  52551. + *
  52552. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  52553. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52554. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52555. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  52556. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  52557. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  52558. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  52559. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52560. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52561. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52562. + * DAMAGE.
  52563. + * ========================================================================== */
  52564. +
  52565. +#if !defined(__DWC_OTG_ATTR_H__)
  52566. +#define __DWC_OTG_ATTR_H__
  52567. +
  52568. +/** @file
  52569. + * This file contains the interface to the Linux device attributes.
  52570. + */
  52571. +extern struct device_attribute dev_attr_regoffset;
  52572. +extern struct device_attribute dev_attr_regvalue;
  52573. +
  52574. +extern struct device_attribute dev_attr_mode;
  52575. +extern struct device_attribute dev_attr_hnpcapable;
  52576. +extern struct device_attribute dev_attr_srpcapable;
  52577. +extern struct device_attribute dev_attr_hnp;
  52578. +extern struct device_attribute dev_attr_srp;
  52579. +extern struct device_attribute dev_attr_buspower;
  52580. +extern struct device_attribute dev_attr_bussuspend;
  52581. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  52582. +extern struct device_attribute dev_attr_fr_interval;
  52583. +extern struct device_attribute dev_attr_busconnected;
  52584. +extern struct device_attribute dev_attr_gotgctl;
  52585. +extern struct device_attribute dev_attr_gusbcfg;
  52586. +extern struct device_attribute dev_attr_grxfsiz;
  52587. +extern struct device_attribute dev_attr_gnptxfsiz;
  52588. +extern struct device_attribute dev_attr_gpvndctl;
  52589. +extern struct device_attribute dev_attr_ggpio;
  52590. +extern struct device_attribute dev_attr_guid;
  52591. +extern struct device_attribute dev_attr_gsnpsid;
  52592. +extern struct device_attribute dev_attr_devspeed;
  52593. +extern struct device_attribute dev_attr_enumspeed;
  52594. +extern struct device_attribute dev_attr_hptxfsiz;
  52595. +extern struct device_attribute dev_attr_hprt0;
  52596. +#ifdef CONFIG_USB_DWC_OTG_LPM
  52597. +extern struct device_attribute dev_attr_lpm_response;
  52598. +extern struct device_attribute devi_attr_sleep_status;
  52599. +#endif
  52600. +
  52601. +void dwc_otg_attr_create(
  52602. +#ifdef LM_INTERFACE
  52603. + struct lm_device *dev
  52604. +#elif defined(PCI_INTERFACE)
  52605. + struct pci_dev *dev
  52606. +#elif defined(PLATFORM_INTERFACE)
  52607. + struct platform_device *dev
  52608. +#endif
  52609. + );
  52610. +
  52611. +void dwc_otg_attr_remove(
  52612. +#ifdef LM_INTERFACE
  52613. + struct lm_device *dev
  52614. +#elif defined(PCI_INTERFACE)
  52615. + struct pci_dev *dev
  52616. +#elif defined(PLATFORM_INTERFACE)
  52617. + struct platform_device *dev
  52618. +#endif
  52619. + );
  52620. +#endif
  52621. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cfi.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  52622. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 1970-01-01 01:00:00.000000000 +0100
  52623. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 2014-03-11 16:55:38.000000000 +0100
  52624. @@ -0,0 +1,1876 @@
  52625. +/* ==========================================================================
  52626. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  52627. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  52628. + * otherwise expressly agreed to in writing between Synopsys and you.
  52629. + *
  52630. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  52631. + * any End User Software License Agreement or Agreement for Licensed Product
  52632. + * with Synopsys or any supplement thereto. You are permitted to use and
  52633. + * redistribute this Software in source and binary forms, with or without
  52634. + * modification, provided that redistributions of source code must retain this
  52635. + * notice. You may not view, use, disclose, copy or distribute this file or
  52636. + * any information contained herein except pursuant to this license grant from
  52637. + * Synopsys. If you do not agree with this notice, including the disclaimer
  52638. + * below, then you are not authorized to use the Software.
  52639. + *
  52640. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  52641. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52642. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52643. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  52644. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  52645. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  52646. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  52647. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  52648. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52649. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  52650. + * DAMAGE.
  52651. + * ========================================================================== */
  52652. +
  52653. +/** @file
  52654. + *
  52655. + * This file contains the most of the CFI(Core Feature Interface)
  52656. + * implementation for the OTG.
  52657. + */
  52658. +
  52659. +#ifdef DWC_UTE_CFI
  52660. +
  52661. +#include "dwc_otg_pcd.h"
  52662. +#include "dwc_otg_cfi.h"
  52663. +
  52664. +/** This definition should actually migrate to the Portability Library */
  52665. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  52666. +
  52667. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  52668. +
  52669. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  52670. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  52671. + struct dwc_otg_pcd *pcd,
  52672. + struct cfi_usb_ctrlrequest *ctrl_req);
  52673. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  52674. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  52675. + struct cfi_usb_ctrlrequest *req);
  52676. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  52677. + struct cfi_usb_ctrlrequest *req);
  52678. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  52679. + struct cfi_usb_ctrlrequest *req);
  52680. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  52681. + struct cfi_usb_ctrlrequest *req);
  52682. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  52683. +
  52684. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  52685. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  52686. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  52687. +
  52688. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  52689. +
  52690. +/** This is the header of the all features descriptor */
  52691. +static cfi_all_features_header_t all_props_desc_header = {
  52692. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  52693. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  52694. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  52695. +};
  52696. +
  52697. +/** This is an array of statically allocated feature descriptors */
  52698. +static cfi_feature_desc_header_t prop_descs[] = {
  52699. +
  52700. + /* FT_ID_DMA_MODE */
  52701. + {
  52702. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  52703. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  52704. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  52705. + },
  52706. +
  52707. + /* FT_ID_DMA_BUFFER_SETUP */
  52708. + {
  52709. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  52710. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  52711. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  52712. + },
  52713. +
  52714. + /* FT_ID_DMA_BUFF_ALIGN */
  52715. + {
  52716. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  52717. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  52718. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  52719. + },
  52720. +
  52721. + /* FT_ID_DMA_CONCAT_SETUP */
  52722. + {
  52723. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  52724. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  52725. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  52726. + },
  52727. +
  52728. + /* FT_ID_DMA_CIRCULAR */
  52729. + {
  52730. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  52731. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  52732. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  52733. + },
  52734. +
  52735. + /* FT_ID_THRESHOLD_SETUP */
  52736. + {
  52737. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  52738. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  52739. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  52740. + },
  52741. +
  52742. + /* FT_ID_DFIFO_DEPTH */
  52743. + {
  52744. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  52745. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  52746. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  52747. + },
  52748. +
  52749. + /* FT_ID_TX_FIFO_DEPTH */
  52750. + {
  52751. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  52752. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  52753. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  52754. + },
  52755. +
  52756. + /* FT_ID_RX_FIFO_DEPTH */
  52757. + {
  52758. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  52759. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  52760. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  52761. + }
  52762. +};
  52763. +
  52764. +/** The table of feature names */
  52765. +cfi_string_t prop_name_table[] = {
  52766. + {FT_ID_DMA_MODE, "dma_mode"},
  52767. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  52768. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  52769. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  52770. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  52771. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  52772. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  52773. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  52774. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  52775. + {}
  52776. +};
  52777. +
  52778. +/************************************************************************/
  52779. +
  52780. +/**
  52781. + * Returns the name of the feature by its ID
  52782. + * or NULL if no featute ID matches.
  52783. + *
  52784. + */
  52785. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  52786. +{
  52787. + cfi_string_t *pstr;
  52788. + *len = 0;
  52789. +
  52790. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  52791. + if (pstr->id == prop_id) {
  52792. + *len = DWC_STRLEN(pstr->s);
  52793. + return pstr->s;
  52794. + }
  52795. + }
  52796. + return NULL;
  52797. +}
  52798. +
  52799. +/**
  52800. + * This function handles all CFI specific control requests.
  52801. + *
  52802. + * Return a negative value to stall the DCE.
  52803. + */
  52804. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  52805. +{
  52806. + int retval = 0;
  52807. + dwc_otg_pcd_ep_t *ep = NULL;
  52808. + cfiobject_t *cfi = pcd->cfi;
  52809. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  52810. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  52811. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  52812. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  52813. + uint32_t regaddr = 0;
  52814. + uint32_t regval = 0;
  52815. +
  52816. + /* Save this Control Request in the CFI object.
  52817. + * The data field will be assigned in the data stage completion CB function.
  52818. + */
  52819. + cfi->ctrl_req = *ctrl;
  52820. + cfi->ctrl_req.data = NULL;
  52821. +
  52822. + cfi->need_gadget_att = 0;
  52823. + cfi->need_status_in_complete = 0;
  52824. +
  52825. + switch (ctrl->bRequest) {
  52826. + case VEN_CORE_GET_FEATURES:
  52827. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  52828. + if (retval >= 0) {
  52829. + //dump_msg(cfi->buf_in.buf, retval);
  52830. + ep = &pcd->ep0;
  52831. +
  52832. + retval = min((uint16_t) retval, wLen);
  52833. + /* Transfer this buffer to the host through the EP0-IN EP */
  52834. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  52835. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  52836. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  52837. + ep->dwc_ep.xfer_len = retval;
  52838. + ep->dwc_ep.xfer_count = 0;
  52839. + ep->dwc_ep.sent_zlp = 0;
  52840. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  52841. +
  52842. + pcd->ep0_pending = 1;
  52843. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  52844. + }
  52845. + retval = 0;
  52846. + break;
  52847. +
  52848. + case VEN_CORE_GET_FEATURE:
  52849. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  52850. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  52851. + pcd, ctrl);
  52852. + if (retval >= 0) {
  52853. + ep = &pcd->ep0;
  52854. +
  52855. + retval = min((uint16_t) retval, wLen);
  52856. + /* Transfer this buffer to the host through the EP0-IN EP */
  52857. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  52858. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  52859. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  52860. + ep->dwc_ep.xfer_len = retval;
  52861. + ep->dwc_ep.xfer_count = 0;
  52862. + ep->dwc_ep.sent_zlp = 0;
  52863. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  52864. +
  52865. + pcd->ep0_pending = 1;
  52866. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  52867. + }
  52868. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  52869. + dump_msg(cfi->buf_in.buf, retval);
  52870. + break;
  52871. +
  52872. + case VEN_CORE_SET_FEATURE:
  52873. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  52874. + /* Set up an XFER to get the data stage of the control request,
  52875. + * which is the new value of the feature to be modified.
  52876. + */
  52877. + ep = &pcd->ep0;
  52878. + ep->dwc_ep.is_in = 0;
  52879. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  52880. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  52881. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  52882. + ep->dwc_ep.xfer_len = wLen;
  52883. + ep->dwc_ep.xfer_count = 0;
  52884. + ep->dwc_ep.sent_zlp = 0;
  52885. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  52886. +
  52887. + pcd->ep0_pending = 1;
  52888. + /* Read the control write's data stage */
  52889. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  52890. + retval = 0;
  52891. + break;
  52892. +
  52893. + case VEN_CORE_RESET_FEATURES:
  52894. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  52895. + cfi->need_gadget_att = 1;
  52896. + cfi->need_status_in_complete = 1;
  52897. + retval = cfi_preproc_reset(pcd, ctrl);
  52898. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  52899. + break;
  52900. +
  52901. + case VEN_CORE_ACTIVATE_FEATURES:
  52902. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  52903. + break;
  52904. +
  52905. + case VEN_CORE_READ_REGISTER:
  52906. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  52907. + /* wValue optionally contains the HI WORD of the register offset and
  52908. + * wIndex contains the LOW WORD of the register offset
  52909. + */
  52910. + if (wValue == 0) {
  52911. + /* @TODO - MAS - fix the access to the base field */
  52912. + regaddr = 0;
  52913. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  52914. + //GET_CORE_IF(pcd)->co
  52915. + regaddr |= wIndex;
  52916. + } else {
  52917. + regaddr = (wValue << 16) | wIndex;
  52918. + }
  52919. +
  52920. + /* Read a 32-bit value of the memory at the regaddr */
  52921. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  52922. +
  52923. + ep = &pcd->ep0;
  52924. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  52925. + ep->dwc_ep.is_in = 1;
  52926. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  52927. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  52928. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  52929. + ep->dwc_ep.xfer_len = wLen;
  52930. + ep->dwc_ep.xfer_count = 0;
  52931. + ep->dwc_ep.sent_zlp = 0;
  52932. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  52933. +
  52934. + pcd->ep0_pending = 1;
  52935. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  52936. + cfi->need_gadget_att = 0;
  52937. + retval = 0;
  52938. + break;
  52939. +
  52940. + case VEN_CORE_WRITE_REGISTER:
  52941. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  52942. + /* Set up an XFER to get the data stage of the control request,
  52943. + * which is the new value of the register to be modified.
  52944. + */
  52945. + ep = &pcd->ep0;
  52946. + ep->dwc_ep.is_in = 0;
  52947. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  52948. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  52949. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  52950. + ep->dwc_ep.xfer_len = wLen;
  52951. + ep->dwc_ep.xfer_count = 0;
  52952. + ep->dwc_ep.sent_zlp = 0;
  52953. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  52954. +
  52955. + pcd->ep0_pending = 1;
  52956. + /* Read the control write's data stage */
  52957. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  52958. + retval = 0;
  52959. + break;
  52960. +
  52961. + default:
  52962. + retval = -DWC_E_NOT_SUPPORTED;
  52963. + break;
  52964. + }
  52965. +
  52966. + return retval;
  52967. +}
  52968. +
  52969. +/**
  52970. + * This function prepares the core features descriptors and copies its
  52971. + * raw representation into the buffer <buf>.
  52972. + *
  52973. + * The buffer structure is as follows:
  52974. + * all_features_header (8 bytes)
  52975. + * features_#1 (8 bytes + feature name string length)
  52976. + * features_#2 (8 bytes + feature name string length)
  52977. + * .....
  52978. + * features_#n - where n=the total count of feature descriptors
  52979. + */
  52980. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  52981. +{
  52982. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  52983. + cfi_feature_desc_header_t *prop;
  52984. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  52985. + cfi_all_features_header_t *tmp;
  52986. + uint8_t *tmpbuf = buf;
  52987. + const uint8_t *pname = NULL;
  52988. + int i, j, namelen = 0, totlen;
  52989. +
  52990. + /* Prepare and copy the core features into the buffer */
  52991. + CFI_INFO("%s:\n", __func__);
  52992. +
  52993. + tmp = (cfi_all_features_header_t *) tmpbuf;
  52994. + *tmp = *all_props_hdr;
  52995. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  52996. +
  52997. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  52998. + for (i = 0; i < j; i++, prop_hdr++) {
  52999. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  53000. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  53001. + *prop = *prop_hdr;
  53002. +
  53003. + prop->bNameLen = namelen;
  53004. + prop->wLength =
  53005. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  53006. + namelen);
  53007. +
  53008. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  53009. + dwc_memcpy(tmpbuf, pname, namelen);
  53010. + tmpbuf += namelen;
  53011. + }
  53012. +
  53013. + totlen = tmpbuf - buf;
  53014. +
  53015. + if (totlen > 0) {
  53016. + tmp = (cfi_all_features_header_t *) buf;
  53017. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  53018. + }
  53019. +
  53020. + return totlen;
  53021. +}
  53022. +
  53023. +/**
  53024. + * This function releases all the dynamic memory in the CFI object.
  53025. + */
  53026. +static void cfi_release(cfiobject_t * cfiobj)
  53027. +{
  53028. + cfi_ep_t *cfiep;
  53029. + dwc_list_link_t *tmp;
  53030. +
  53031. + CFI_INFO("%s\n", __func__);
  53032. +
  53033. + if (cfiobj->buf_in.buf) {
  53034. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  53035. + cfiobj->buf_in.addr);
  53036. + cfiobj->buf_in.buf = NULL;
  53037. + }
  53038. +
  53039. + if (cfiobj->buf_out.buf) {
  53040. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  53041. + cfiobj->buf_out.addr);
  53042. + cfiobj->buf_out.buf = NULL;
  53043. + }
  53044. +
  53045. + /* Free the Buffer Setup values for each EP */
  53046. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  53047. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  53048. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  53049. + cfi_free_ep_bs_dyn_data(cfiep);
  53050. + }
  53051. +}
  53052. +
  53053. +/**
  53054. + * This function frees the dynamically allocated EP buffer setup data.
  53055. + */
  53056. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  53057. +{
  53058. + if (cfiep->bm_sg) {
  53059. + DWC_FREE(cfiep->bm_sg);
  53060. + cfiep->bm_sg = NULL;
  53061. + }
  53062. +
  53063. + if (cfiep->bm_align) {
  53064. + DWC_FREE(cfiep->bm_align);
  53065. + cfiep->bm_align = NULL;
  53066. + }
  53067. +
  53068. + if (cfiep->bm_concat) {
  53069. + if (NULL != cfiep->bm_concat->wTxBytes) {
  53070. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  53071. + cfiep->bm_concat->wTxBytes = NULL;
  53072. + }
  53073. + DWC_FREE(cfiep->bm_concat);
  53074. + cfiep->bm_concat = NULL;
  53075. + }
  53076. +}
  53077. +
  53078. +/**
  53079. + * This function initializes the default values of the features
  53080. + * for a specific endpoint and should be called only once when
  53081. + * the EP is enabled first time.
  53082. + */
  53083. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  53084. +{
  53085. + int retval = 0;
  53086. +
  53087. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  53088. + if (NULL == cfiep->bm_sg) {
  53089. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  53090. + return -DWC_E_NO_MEMORY;
  53091. + }
  53092. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  53093. +
  53094. + /* For the Concatenation feature's default value we do not allocate
  53095. + * memory for the wTxBytes field - it will be done in the set_feature_value
  53096. + * request handler.
  53097. + */
  53098. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  53099. + if (NULL == cfiep->bm_concat) {
  53100. + CFI_INFO
  53101. + ("Failed to allocate memory for CONCATENATION feature value\n");
  53102. + DWC_FREE(cfiep->bm_sg);
  53103. + return -DWC_E_NO_MEMORY;
  53104. + }
  53105. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  53106. +
  53107. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  53108. + if (NULL == cfiep->bm_align) {
  53109. + CFI_INFO
  53110. + ("Failed to allocate memory for Alignment feature value\n");
  53111. + DWC_FREE(cfiep->bm_sg);
  53112. + DWC_FREE(cfiep->bm_concat);
  53113. + return -DWC_E_NO_MEMORY;
  53114. + }
  53115. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  53116. +
  53117. + return retval;
  53118. +}
  53119. +
  53120. +/**
  53121. + * The callback function that notifies the CFI on the activation of
  53122. + * an endpoint in the PCD. The following steps are done in this function:
  53123. + *
  53124. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  53125. + * active endpoint)
  53126. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  53127. + * Set the Buffer Mode to standard
  53128. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  53129. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  53130. + */
  53131. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  53132. + struct dwc_otg_pcd_ep *ep)
  53133. +{
  53134. + cfi_ep_t *cfiep;
  53135. + int retval = -DWC_E_NOT_SUPPORTED;
  53136. +
  53137. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  53138. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  53139. + /* MAS - Check whether this endpoint already is in the list */
  53140. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  53141. +
  53142. + if (NULL == cfiep) {
  53143. + /* Allocate a cfi_ep_t object */
  53144. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  53145. + if (NULL == cfiep) {
  53146. + CFI_INFO
  53147. + ("Unable to allocate memory for <cfiep> in function %s\n",
  53148. + __func__);
  53149. + return -DWC_E_NO_MEMORY;
  53150. + }
  53151. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  53152. +
  53153. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  53154. + cfiep->ep = ep;
  53155. +
  53156. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  53157. + ep->dwc_ep.descs =
  53158. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  53159. + sizeof(dwc_otg_dma_desc_t),
  53160. + &ep->dwc_ep.descs_dma_addr);
  53161. +
  53162. + if (NULL == ep->dwc_ep.descs) {
  53163. + DWC_FREE(cfiep);
  53164. + return -DWC_E_NO_MEMORY;
  53165. + }
  53166. +
  53167. + DWC_LIST_INIT(&cfiep->lh);
  53168. +
  53169. + /* Set the buffer mode to BM_STANDARD. It will be modified
  53170. + * when building descriptors for a specific buffer mode */
  53171. + ep->dwc_ep.buff_mode = BM_STANDARD;
  53172. +
  53173. + /* Create and initialize the default values for this EP's Buffer modes */
  53174. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  53175. + return retval;
  53176. +
  53177. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  53178. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  53179. + retval = 0;
  53180. + } else { /* The sought EP already is in the list */
  53181. + CFI_INFO("%s: The sought EP already is in the list\n",
  53182. + __func__);
  53183. + }
  53184. +
  53185. + return retval;
  53186. +}
  53187. +
  53188. +/**
  53189. + * This function is called when the data stage of a 3-stage Control Write request
  53190. + * is complete.
  53191. + *
  53192. + */
  53193. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  53194. + struct dwc_otg_pcd *pcd)
  53195. +{
  53196. + uint32_t addr, reg_value;
  53197. + uint16_t wIndex, wValue;
  53198. + uint8_t bRequest;
  53199. + uint8_t *buf = cfi->buf_out.buf;
  53200. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  53201. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  53202. + int retval = -DWC_E_NOT_SUPPORTED;
  53203. +
  53204. + CFI_INFO("%s\n", __func__);
  53205. +
  53206. + bRequest = ctrl_req->bRequest;
  53207. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  53208. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  53209. +
  53210. + /*
  53211. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  53212. + * The request should be already saved in the command stage by now.
  53213. + */
  53214. + ctrl_req->data = cfi->buf_out.buf;
  53215. + cfi->need_status_in_complete = 0;
  53216. + cfi->need_gadget_att = 0;
  53217. +
  53218. + switch (bRequest) {
  53219. + case VEN_CORE_WRITE_REGISTER:
  53220. + /* The buffer contains raw data of the new value for the register */
  53221. + reg_value = *((uint32_t *) buf);
  53222. + if (wValue == 0) {
  53223. + addr = 0;
  53224. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  53225. + addr += wIndex;
  53226. + } else {
  53227. + addr = (wValue << 16) | wIndex;
  53228. + }
  53229. +
  53230. + //writel(reg_value, addr);
  53231. +
  53232. + retval = 0;
  53233. + cfi->need_status_in_complete = 1;
  53234. + break;
  53235. +
  53236. + case VEN_CORE_SET_FEATURE:
  53237. + /* The buffer contains raw data of the new value of the feature */
  53238. + retval = cfi_set_feature_value(pcd);
  53239. + if (retval < 0)
  53240. + return retval;
  53241. +
  53242. + cfi->need_status_in_complete = 1;
  53243. + break;
  53244. +
  53245. + default:
  53246. + break;
  53247. + }
  53248. +
  53249. + return retval;
  53250. +}
  53251. +
  53252. +/**
  53253. + * This function builds the DMA descriptors for the SG buffer mode.
  53254. + */
  53255. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53256. + dwc_otg_pcd_request_t * req)
  53257. +{
  53258. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  53259. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  53260. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  53261. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  53262. + dma_addr_t buff_addr = req->dma;
  53263. + int i;
  53264. + uint32_t txsize, off;
  53265. +
  53266. + txsize = sgval->wSize;
  53267. + off = sgval->bOffset;
  53268. +
  53269. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  53270. +// __func__, cfiep->ep->ep.name, txsize, off);
  53271. +
  53272. + for (i = 0; i < sgval->bCount; i++) {
  53273. + desc->status.b.bs = BS_HOST_BUSY;
  53274. + desc->buf = buff_addr;
  53275. + desc->status.b.l = 0;
  53276. + desc->status.b.ioc = 0;
  53277. + desc->status.b.sp = 0;
  53278. + desc->status.b.bytes = txsize;
  53279. + desc->status.b.bs = BS_HOST_READY;
  53280. +
  53281. + /* Set the next address of the buffer */
  53282. + buff_addr += txsize + off;
  53283. + desc_last = desc;
  53284. + desc++;
  53285. + }
  53286. +
  53287. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  53288. + desc_last->status.b.l = 1;
  53289. + desc_last->status.b.ioc = 1;
  53290. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  53291. + /* Save the last DMA descriptor pointer */
  53292. + cfiep->dma_desc_last = desc_last;
  53293. + cfiep->desc_count = sgval->bCount;
  53294. +}
  53295. +
  53296. +/**
  53297. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  53298. + */
  53299. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53300. + dwc_otg_pcd_request_t * req)
  53301. +{
  53302. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  53303. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  53304. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  53305. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  53306. + dma_addr_t buff_addr = req->dma;
  53307. + int i;
  53308. + uint16_t *txsize;
  53309. +
  53310. + txsize = concatval->wTxBytes;
  53311. +
  53312. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  53313. + desc->buf = buff_addr;
  53314. + desc->status.b.bs = BS_HOST_BUSY;
  53315. + desc->status.b.l = 0;
  53316. + desc->status.b.ioc = 0;
  53317. + desc->status.b.sp = 0;
  53318. + desc->status.b.bytes = *txsize;
  53319. + desc->status.b.bs = BS_HOST_READY;
  53320. +
  53321. + txsize++;
  53322. + /* Set the next address of the buffer */
  53323. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  53324. + desc_last = desc;
  53325. + desc++;
  53326. + }
  53327. +
  53328. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  53329. + desc_last->status.b.l = 1;
  53330. + desc_last->status.b.ioc = 1;
  53331. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  53332. + cfiep->dma_desc_last = desc_last;
  53333. + cfiep->desc_count = concatval->hdr.bDescCount;
  53334. +}
  53335. +
  53336. +/**
  53337. + * This function builds the DMA descriptors for the Circular buffer mode
  53338. + */
  53339. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53340. + dwc_otg_pcd_request_t * req)
  53341. +{
  53342. + /* @todo: MAS - add implementation when this feature needs to be tested */
  53343. +}
  53344. +
  53345. +/**
  53346. + * This function builds the DMA descriptors for the Alignment buffer mode
  53347. + */
  53348. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  53349. + dwc_otg_pcd_request_t * req)
  53350. +{
  53351. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  53352. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  53353. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  53354. + dma_addr_t buff_addr = req->dma;
  53355. +
  53356. + desc->status.b.bs = BS_HOST_BUSY;
  53357. + desc->status.b.l = 1;
  53358. + desc->status.b.ioc = 1;
  53359. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  53360. + desc->status.b.bytes = req->length;
  53361. + /* Adjust the buffer alignment */
  53362. + desc->buf = (buff_addr + alignval->bAlign);
  53363. + desc->status.b.bs = BS_HOST_READY;
  53364. + cfiep->dma_desc_last = desc;
  53365. + cfiep->desc_count = 1;
  53366. +}
  53367. +
  53368. +/**
  53369. + * This function builds the DMA descriptors chain for different modes of the
  53370. + * buffer setup of an endpoint.
  53371. + */
  53372. +static void cfi_build_descriptors(struct cfiobject *cfi,
  53373. + struct dwc_otg_pcd *pcd,
  53374. + struct dwc_otg_pcd_ep *ep,
  53375. + dwc_otg_pcd_request_t * req)
  53376. +{
  53377. + cfi_ep_t *cfiep;
  53378. +
  53379. + /* Get the cfiep by the dwc_otg_pcd_ep */
  53380. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  53381. + if (NULL == cfiep) {
  53382. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  53383. + __func__);
  53384. + return;
  53385. + }
  53386. +
  53387. + cfiep->xfer_len = req->length;
  53388. +
  53389. + /* Iterate through all the DMA descriptors */
  53390. + switch (cfiep->ep->dwc_ep.buff_mode) {
  53391. + case BM_SG:
  53392. + cfi_build_sg_descs(cfi, cfiep, req);
  53393. + break;
  53394. +
  53395. + case BM_CONCAT:
  53396. + cfi_build_concat_descs(cfi, cfiep, req);
  53397. + break;
  53398. +
  53399. + case BM_CIRCULAR:
  53400. + cfi_build_circ_descs(cfi, cfiep, req);
  53401. + break;
  53402. +
  53403. + case BM_ALIGN:
  53404. + cfi_build_align_descs(cfi, cfiep, req);
  53405. + break;
  53406. +
  53407. + default:
  53408. + break;
  53409. + }
  53410. +}
  53411. +
  53412. +/**
  53413. + * Allocate DMA buffer for different Buffer modes.
  53414. + */
  53415. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  53416. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  53417. + unsigned size, gfp_t flags)
  53418. +{
  53419. + return DWC_DMA_ALLOC(size, dma);
  53420. +}
  53421. +
  53422. +/**
  53423. + * This function initializes the CFI object.
  53424. + */
  53425. +int init_cfi(cfiobject_t * cfiobj)
  53426. +{
  53427. + CFI_INFO("%s\n", __func__);
  53428. +
  53429. + /* Allocate a buffer for IN XFERs */
  53430. + cfiobj->buf_in.buf =
  53431. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  53432. + if (NULL == cfiobj->buf_in.buf) {
  53433. + CFI_INFO("Unable to allocate buffer for INs\n");
  53434. + return -DWC_E_NO_MEMORY;
  53435. + }
  53436. +
  53437. + /* Allocate a buffer for OUT XFERs */
  53438. + cfiobj->buf_out.buf =
  53439. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  53440. + if (NULL == cfiobj->buf_out.buf) {
  53441. + CFI_INFO("Unable to allocate buffer for OUT\n");
  53442. + return -DWC_E_NO_MEMORY;
  53443. + }
  53444. +
  53445. + /* Initialize the callback function pointers */
  53446. + cfiobj->ops.release = cfi_release;
  53447. + cfiobj->ops.ep_enable = cfi_ep_enable;
  53448. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  53449. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  53450. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  53451. +
  53452. + /* Initialize the list of active endpoints in the CFI object */
  53453. + DWC_LIST_INIT(&cfiobj->active_eps);
  53454. +
  53455. + return 0;
  53456. +}
  53457. +
  53458. +/**
  53459. + * This function reads the required feature's current value into the buffer
  53460. + *
  53461. + * @retval: Returns negative as error, or the data length of the feature
  53462. + */
  53463. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  53464. + struct dwc_otg_pcd *pcd,
  53465. + struct cfi_usb_ctrlrequest *ctrl_req)
  53466. +{
  53467. + int retval = -DWC_E_NOT_SUPPORTED;
  53468. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  53469. + uint16_t dfifo, rxfifo, txfifo;
  53470. +
  53471. + switch (ctrl_req->wIndex) {
  53472. + /* Whether the DDMA is enabled or not */
  53473. + case FT_ID_DMA_MODE:
  53474. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  53475. + retval = 1;
  53476. + break;
  53477. +
  53478. + case FT_ID_DMA_BUFFER_SETUP:
  53479. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  53480. + break;
  53481. +
  53482. + case FT_ID_DMA_BUFF_ALIGN:
  53483. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  53484. + break;
  53485. +
  53486. + case FT_ID_DMA_CONCAT_SETUP:
  53487. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  53488. + break;
  53489. +
  53490. + case FT_ID_DMA_CIRCULAR:
  53491. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  53492. + break;
  53493. +
  53494. + case FT_ID_THRESHOLD_SETUP:
  53495. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  53496. + break;
  53497. +
  53498. + case FT_ID_DFIFO_DEPTH:
  53499. + dfifo = get_dfifo_size(coreif);
  53500. + *((uint16_t *) buf) = dfifo;
  53501. + retval = sizeof(uint16_t);
  53502. + break;
  53503. +
  53504. + case FT_ID_TX_FIFO_DEPTH:
  53505. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  53506. + if (retval >= 0) {
  53507. + txfifo = retval;
  53508. + *((uint16_t *) buf) = txfifo;
  53509. + retval = sizeof(uint16_t);
  53510. + }
  53511. + break;
  53512. +
  53513. + case FT_ID_RX_FIFO_DEPTH:
  53514. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  53515. + if (retval >= 0) {
  53516. + rxfifo = retval;
  53517. + *((uint16_t *) buf) = rxfifo;
  53518. + retval = sizeof(uint16_t);
  53519. + }
  53520. + break;
  53521. + }
  53522. +
  53523. + return retval;
  53524. +}
  53525. +
  53526. +/**
  53527. + * This function resets the SG for the specified EP to its default value
  53528. + */
  53529. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  53530. +{
  53531. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  53532. + return 0;
  53533. +}
  53534. +
  53535. +/**
  53536. + * This function resets the Alignment for the specified EP to its default value
  53537. + */
  53538. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  53539. +{
  53540. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  53541. + return 0;
  53542. +}
  53543. +
  53544. +/**
  53545. + * This function resets the Concatenation for the specified EP to its default value
  53546. + * This function will also set the value of the wTxBytes field to NULL after
  53547. + * freeing the memory previously allocated for this field.
  53548. + */
  53549. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  53550. +{
  53551. + /* First we need to free the wTxBytes field */
  53552. + if (cfiep->bm_concat->wTxBytes) {
  53553. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  53554. + cfiep->bm_concat->wTxBytes = NULL;
  53555. + }
  53556. +
  53557. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  53558. + return 0;
  53559. +}
  53560. +
  53561. +/**
  53562. + * This function resets all the buffer setups of the specified endpoint
  53563. + */
  53564. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  53565. +{
  53566. + cfi_reset_sg_val(cfiep);
  53567. + cfi_reset_align_val(cfiep);
  53568. + cfi_reset_concat_val(cfiep);
  53569. + return 0;
  53570. +}
  53571. +
  53572. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  53573. + uint8_t rx_rst, uint8_t tx_rst)
  53574. +{
  53575. + int retval = -DWC_E_INVALID;
  53576. + uint16_t tx_siz[15];
  53577. + uint16_t rx_siz = 0;
  53578. + dwc_otg_pcd_ep_t *ep = NULL;
  53579. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  53580. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  53581. +
  53582. + if (rx_rst) {
  53583. + rx_siz = params->dev_rx_fifo_size;
  53584. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  53585. + }
  53586. +
  53587. + if (tx_rst) {
  53588. + if (ep_addr == 0) {
  53589. + int i;
  53590. +
  53591. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  53592. + tx_siz[i] =
  53593. + core_if->core_params->dev_tx_fifo_size[i];
  53594. + core_if->core_params->dev_tx_fifo_size[i] =
  53595. + core_if->init_txfsiz[i];
  53596. + }
  53597. + } else {
  53598. +
  53599. + ep = get_ep_by_addr(pcd, ep_addr);
  53600. +
  53601. + if (NULL == ep) {
  53602. + CFI_INFO
  53603. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  53604. + __func__, ep_addr);
  53605. + return -DWC_E_INVALID;
  53606. + }
  53607. +
  53608. + tx_siz[0] =
  53609. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  53610. + 1];
  53611. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  53612. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  53613. + dwc_ep.tx_fifo_num -
  53614. + 1];
  53615. + }
  53616. + }
  53617. +
  53618. + if (resize_fifos(GET_CORE_IF(pcd))) {
  53619. + retval = 0;
  53620. + } else {
  53621. + CFI_INFO
  53622. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  53623. + __func__);
  53624. + if (rx_rst) {
  53625. + params->dev_rx_fifo_size = rx_siz;
  53626. + }
  53627. +
  53628. + if (tx_rst) {
  53629. + if (ep_addr == 0) {
  53630. + int i;
  53631. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  53632. + i++) {
  53633. + core_if->
  53634. + core_params->dev_tx_fifo_size[i] =
  53635. + tx_siz[i];
  53636. + }
  53637. + } else {
  53638. + params->dev_tx_fifo_size[ep->
  53639. + dwc_ep.tx_fifo_num -
  53640. + 1] = tx_siz[0];
  53641. + }
  53642. + }
  53643. + retval = -DWC_E_INVALID;
  53644. + }
  53645. + return retval;
  53646. +}
  53647. +
  53648. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  53649. +{
  53650. + int retval = 0;
  53651. + cfi_ep_t *cfiep;
  53652. + cfiobject_t *cfi = pcd->cfi;
  53653. + dwc_list_link_t *tmp;
  53654. +
  53655. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  53656. + if (retval < 0) {
  53657. + return retval;
  53658. + }
  53659. +
  53660. + /* If the EP address is known then reset the features for only that EP */
  53661. + if (addr) {
  53662. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  53663. + if (NULL == cfiep) {
  53664. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  53665. + __func__, addr);
  53666. + return -DWC_E_INVALID;
  53667. + }
  53668. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  53669. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  53670. + }
  53671. + /* Otherwise (wValue == 0), reset all features of all EP's */
  53672. + else {
  53673. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  53674. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  53675. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  53676. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  53677. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  53678. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  53679. + if (retval < 0) {
  53680. + CFI_INFO
  53681. + ("%s: Error resetting the feature Reset All\n",
  53682. + __func__);
  53683. + return retval;
  53684. + }
  53685. + }
  53686. + }
  53687. + return retval;
  53688. +}
  53689. +
  53690. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  53691. + uint8_t addr)
  53692. +{
  53693. + int retval = 0;
  53694. + cfi_ep_t *cfiep;
  53695. + cfiobject_t *cfi = pcd->cfi;
  53696. + dwc_list_link_t *tmp;
  53697. +
  53698. + /* If the EP address is known then reset the features for only that EP */
  53699. + if (addr) {
  53700. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  53701. + if (NULL == cfiep) {
  53702. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  53703. + __func__, addr);
  53704. + return -DWC_E_INVALID;
  53705. + }
  53706. + retval = cfi_reset_sg_val(cfiep);
  53707. + }
  53708. + /* Otherwise (wValue == 0), reset all features of all EP's */
  53709. + else {
  53710. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  53711. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  53712. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  53713. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  53714. + retval = cfi_reset_sg_val(cfiep);
  53715. + if (retval < 0) {
  53716. + CFI_INFO
  53717. + ("%s: Error resetting the feature Buffer Setup\n",
  53718. + __func__);
  53719. + return retval;
  53720. + }
  53721. + }
  53722. + }
  53723. + return retval;
  53724. +}
  53725. +
  53726. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  53727. +{
  53728. + int retval = 0;
  53729. + cfi_ep_t *cfiep;
  53730. + cfiobject_t *cfi = pcd->cfi;
  53731. + dwc_list_link_t *tmp;
  53732. +
  53733. + /* If the EP address is known then reset the features for only that EP */
  53734. + if (addr) {
  53735. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  53736. + if (NULL == cfiep) {
  53737. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  53738. + __func__, addr);
  53739. + return -DWC_E_INVALID;
  53740. + }
  53741. + retval = cfi_reset_concat_val(cfiep);
  53742. + }
  53743. + /* Otherwise (wValue == 0), reset all features of all EP's */
  53744. + else {
  53745. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  53746. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  53747. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  53748. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  53749. + retval = cfi_reset_concat_val(cfiep);
  53750. + if (retval < 0) {
  53751. + CFI_INFO
  53752. + ("%s: Error resetting the feature Concatenation Value\n",
  53753. + __func__);
  53754. + return retval;
  53755. + }
  53756. + }
  53757. + }
  53758. + return retval;
  53759. +}
  53760. +
  53761. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  53762. +{
  53763. + int retval = 0;
  53764. + cfi_ep_t *cfiep;
  53765. + cfiobject_t *cfi = pcd->cfi;
  53766. + dwc_list_link_t *tmp;
  53767. +
  53768. + /* If the EP address is known then reset the features for only that EP */
  53769. + if (addr) {
  53770. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  53771. + if (NULL == cfiep) {
  53772. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  53773. + __func__, addr);
  53774. + return -DWC_E_INVALID;
  53775. + }
  53776. + retval = cfi_reset_align_val(cfiep);
  53777. + }
  53778. + /* Otherwise (wValue == 0), reset all features of all EP's */
  53779. + else {
  53780. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  53781. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  53782. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  53783. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  53784. + retval = cfi_reset_align_val(cfiep);
  53785. + if (retval < 0) {
  53786. + CFI_INFO
  53787. + ("%s: Error resetting the feature Aliignment Value\n",
  53788. + __func__);
  53789. + return retval;
  53790. + }
  53791. + }
  53792. + }
  53793. + return retval;
  53794. +
  53795. +}
  53796. +
  53797. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  53798. + struct cfi_usb_ctrlrequest *req)
  53799. +{
  53800. + int retval = 0;
  53801. +
  53802. + switch (req->wIndex) {
  53803. + case 0:
  53804. + /* Reset all features */
  53805. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  53806. + break;
  53807. +
  53808. + case FT_ID_DMA_BUFFER_SETUP:
  53809. + /* Reset the SG buffer setup */
  53810. + retval =
  53811. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  53812. + break;
  53813. +
  53814. + case FT_ID_DMA_CONCAT_SETUP:
  53815. + /* Reset the Concatenation buffer setup */
  53816. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  53817. + break;
  53818. +
  53819. + case FT_ID_DMA_BUFF_ALIGN:
  53820. + /* Reset the Alignment buffer setup */
  53821. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  53822. + break;
  53823. +
  53824. + case FT_ID_TX_FIFO_DEPTH:
  53825. + retval =
  53826. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  53827. + pcd->cfi->need_gadget_att = 0;
  53828. + break;
  53829. +
  53830. + case FT_ID_RX_FIFO_DEPTH:
  53831. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  53832. + pcd->cfi->need_gadget_att = 0;
  53833. + break;
  53834. + default:
  53835. + break;
  53836. + }
  53837. + return retval;
  53838. +}
  53839. +
  53840. +/**
  53841. + * This function sets a new value for the SG buffer setup.
  53842. + */
  53843. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  53844. +{
  53845. + uint8_t inaddr, outaddr;
  53846. + cfi_ep_t *epin, *epout;
  53847. + ddma_sg_buffer_setup_t *psgval;
  53848. + uint32_t desccount, size;
  53849. +
  53850. + CFI_INFO("%s\n", __func__);
  53851. +
  53852. + psgval = (ddma_sg_buffer_setup_t *) buf;
  53853. + desccount = (uint32_t) psgval->bCount;
  53854. + size = (uint32_t) psgval->wSize;
  53855. +
  53856. + /* Check the DMA descriptor count */
  53857. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  53858. + CFI_INFO
  53859. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  53860. + __func__, MAX_DMA_DESCS_PER_EP);
  53861. + return -DWC_E_INVALID;
  53862. + }
  53863. +
  53864. + /* Check the DMA descriptor count */
  53865. +
  53866. + if (size == 0) {
  53867. +
  53868. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  53869. + __func__);
  53870. +
  53871. + return -DWC_E_INVALID;
  53872. +
  53873. + }
  53874. +
  53875. + inaddr = psgval->bInEndpointAddress;
  53876. + outaddr = psgval->bOutEndpointAddress;
  53877. +
  53878. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  53879. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  53880. +
  53881. + if (NULL == epin || NULL == epout) {
  53882. + CFI_INFO
  53883. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  53884. + __func__, inaddr, outaddr);
  53885. + return -DWC_E_INVALID;
  53886. + }
  53887. +
  53888. + epin->ep->dwc_ep.buff_mode = BM_SG;
  53889. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  53890. +
  53891. + epout->ep->dwc_ep.buff_mode = BM_SG;
  53892. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  53893. +
  53894. + return 0;
  53895. +}
  53896. +
  53897. +/**
  53898. + * This function sets a new value for the buffer Alignment setup.
  53899. + */
  53900. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  53901. +{
  53902. + cfi_ep_t *ep;
  53903. + uint8_t addr;
  53904. + ddma_align_buffer_setup_t *palignval;
  53905. +
  53906. + palignval = (ddma_align_buffer_setup_t *) buf;
  53907. + addr = palignval->bEndpointAddress;
  53908. +
  53909. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  53910. +
  53911. + if (NULL == ep) {
  53912. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  53913. + __func__, addr);
  53914. + return -DWC_E_INVALID;
  53915. + }
  53916. +
  53917. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  53918. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  53919. +
  53920. + return 0;
  53921. +}
  53922. +
  53923. +/**
  53924. + * This function sets a new value for the Concatenation buffer setup.
  53925. + */
  53926. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  53927. +{
  53928. + uint8_t addr;
  53929. + cfi_ep_t *ep;
  53930. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  53931. + uint16_t *pVals;
  53932. + uint32_t desccount;
  53933. + int i;
  53934. + uint16_t mps;
  53935. +
  53936. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  53937. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  53938. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  53939. +
  53940. + /* Check the DMA descriptor count */
  53941. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  53942. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  53943. + __func__, MAX_DMA_DESCS_PER_EP);
  53944. + return -DWC_E_INVALID;
  53945. + }
  53946. +
  53947. + addr = pConcatValHdr->bEndpointAddress;
  53948. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  53949. + if (NULL == ep) {
  53950. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  53951. + __func__, addr);
  53952. + return -DWC_E_INVALID;
  53953. + }
  53954. +
  53955. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  53956. +
  53957. +#if 0
  53958. + for (i = 0; i < desccount; i++) {
  53959. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  53960. + }
  53961. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  53962. +#endif
  53963. +
  53964. + /* Check the wTxSizes to be less than or equal to the mps */
  53965. + for (i = 0; i < desccount; i++) {
  53966. + if (pVals[i] > mps) {
  53967. + CFI_INFO
  53968. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  53969. + __func__, i, pVals[i]);
  53970. + return -DWC_E_INVALID;
  53971. + }
  53972. + }
  53973. +
  53974. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  53975. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  53976. +
  53977. + /* Free the previously allocated storage for the wTxBytes */
  53978. + if (ep->bm_concat->wTxBytes) {
  53979. + DWC_FREE(ep->bm_concat->wTxBytes);
  53980. + }
  53981. +
  53982. + /* Allocate a new storage for the wTxBytes field */
  53983. + ep->bm_concat->wTxBytes =
  53984. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  53985. + if (NULL == ep->bm_concat->wTxBytes) {
  53986. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  53987. + return -DWC_E_NO_MEMORY;
  53988. + }
  53989. +
  53990. + /* Copy the new values into the wTxBytes filed */
  53991. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  53992. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  53993. +
  53994. + return 0;
  53995. +}
  53996. +
  53997. +/**
  53998. + * This function calculates the total of all FIFO sizes
  53999. + *
  54000. + * @param core_if Programming view of DWC_otg controller
  54001. + *
  54002. + * @return The total of data FIFO sizes.
  54003. + *
  54004. + */
  54005. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  54006. +{
  54007. + dwc_otg_core_params_t *params = core_if->core_params;
  54008. + uint16_t dfifo_total = 0;
  54009. + int i;
  54010. +
  54011. + /* The shared RxFIFO size */
  54012. + dfifo_total =
  54013. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  54014. +
  54015. + /* Add up each TxFIFO size to the total */
  54016. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54017. + dfifo_total += params->dev_tx_fifo_size[i];
  54018. + }
  54019. +
  54020. + return dfifo_total;
  54021. +}
  54022. +
  54023. +/**
  54024. + * This function returns Rx FIFO size
  54025. + *
  54026. + * @param core_if Programming view of DWC_otg controller
  54027. + *
  54028. + * @return The total of data FIFO sizes.
  54029. + *
  54030. + */
  54031. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  54032. +{
  54033. + switch (wValue >> 8) {
  54034. + case 0:
  54035. + return (core_if->pwron_rxfsiz <
  54036. + 32768) ? core_if->pwron_rxfsiz : 32768;
  54037. + break;
  54038. + case 1:
  54039. + return core_if->core_params->dev_rx_fifo_size;
  54040. + break;
  54041. + default:
  54042. + return -DWC_E_INVALID;
  54043. + break;
  54044. + }
  54045. +}
  54046. +
  54047. +/**
  54048. + * This function returns Tx FIFO size for IN EP
  54049. + *
  54050. + * @param core_if Programming view of DWC_otg controller
  54051. + *
  54052. + * @return The total of data FIFO sizes.
  54053. + *
  54054. + */
  54055. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  54056. +{
  54057. + dwc_otg_pcd_ep_t *ep;
  54058. +
  54059. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  54060. +
  54061. + if (NULL == ep) {
  54062. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54063. + __func__, wValue & 0xff);
  54064. + return -DWC_E_INVALID;
  54065. + }
  54066. +
  54067. + if (!ep->dwc_ep.is_in) {
  54068. + CFI_INFO
  54069. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  54070. + __func__, wValue & 0xff);
  54071. + return -DWC_E_INVALID;
  54072. + }
  54073. +
  54074. + switch (wValue >> 8) {
  54075. + case 0:
  54076. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  54077. + [ep->dwc_ep.tx_fifo_num - 1] <
  54078. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  54079. + dwc_ep.tx_fifo_num
  54080. + - 1] : 32768;
  54081. + break;
  54082. + case 1:
  54083. + return GET_CORE_IF(pcd)->core_params->
  54084. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  54085. + break;
  54086. + default:
  54087. + return -DWC_E_INVALID;
  54088. + break;
  54089. + }
  54090. +}
  54091. +
  54092. +/**
  54093. + * This function checks if the submitted combination of
  54094. + * device mode FIFO sizes is possible or not.
  54095. + *
  54096. + * @param core_if Programming view of DWC_otg controller
  54097. + *
  54098. + * @return 1 if possible, 0 otherwise.
  54099. + *
  54100. + */
  54101. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  54102. +{
  54103. + uint16_t dfifo_actual = 0;
  54104. + dwc_otg_core_params_t *params = core_if->core_params;
  54105. + uint16_t start_addr = 0;
  54106. + int i;
  54107. +
  54108. + dfifo_actual =
  54109. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  54110. +
  54111. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54112. + dfifo_actual += params->dev_tx_fifo_size[i];
  54113. + }
  54114. +
  54115. + if (dfifo_actual > core_if->total_fifo_size) {
  54116. + return 0;
  54117. + }
  54118. +
  54119. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  54120. + return 0;
  54121. +
  54122. + if (params->dev_nperio_tx_fifo_size > 32768
  54123. + || params->dev_nperio_tx_fifo_size < 16)
  54124. + return 0;
  54125. +
  54126. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54127. +
  54128. + if (params->dev_tx_fifo_size[i] > 768
  54129. + || params->dev_tx_fifo_size[i] < 4)
  54130. + return 0;
  54131. + }
  54132. +
  54133. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  54134. + return 0;
  54135. + start_addr = params->dev_rx_fifo_size;
  54136. +
  54137. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  54138. + return 0;
  54139. + start_addr += params->dev_nperio_tx_fifo_size;
  54140. +
  54141. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54142. +
  54143. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  54144. + return 0;
  54145. + start_addr += params->dev_tx_fifo_size[i];
  54146. + }
  54147. +
  54148. + return 1;
  54149. +}
  54150. +
  54151. +/**
  54152. + * This function resizes Device mode FIFOs
  54153. + *
  54154. + * @param core_if Programming view of DWC_otg controller
  54155. + *
  54156. + * @return 1 if successful, 0 otherwise
  54157. + *
  54158. + */
  54159. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  54160. +{
  54161. + int i = 0;
  54162. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  54163. + dwc_otg_core_params_t *params = core_if->core_params;
  54164. + uint32_t rx_fifo_size;
  54165. + fifosize_data_t nptxfifosize;
  54166. + fifosize_data_t txfifosize[15];
  54167. +
  54168. + uint32_t rx_fsz_bak;
  54169. + uint32_t nptxfsz_bak;
  54170. + uint32_t txfsz_bak[15];
  54171. +
  54172. + uint16_t start_address;
  54173. + uint8_t retval = 1;
  54174. +
  54175. + if (!check_fifo_sizes(core_if)) {
  54176. + return 0;
  54177. + }
  54178. +
  54179. + /* Configure data FIFO sizes */
  54180. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  54181. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  54182. + rx_fifo_size = params->dev_rx_fifo_size;
  54183. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  54184. +
  54185. + /*
  54186. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  54187. + * Indexes of the FIFO size module parameters in the
  54188. + * dev_tx_fifo_size array and the FIFO size registers in
  54189. + * the dtxfsiz array run from 0 to 14.
  54190. + */
  54191. +
  54192. + /* Non-periodic Tx FIFO */
  54193. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  54194. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  54195. + start_address = params->dev_rx_fifo_size;
  54196. + nptxfifosize.b.startaddr = start_address;
  54197. +
  54198. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  54199. +
  54200. + start_address += nptxfifosize.b.depth;
  54201. +
  54202. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54203. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  54204. +
  54205. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  54206. + txfifosize[i].b.startaddr = start_address;
  54207. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  54208. + txfifosize[i].d32);
  54209. +
  54210. + start_address += txfifosize[i].b.depth;
  54211. + }
  54212. +
  54213. + /** Check if register values are set correctly */
  54214. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  54215. + retval = 0;
  54216. + }
  54217. +
  54218. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  54219. + retval = 0;
  54220. + }
  54221. +
  54222. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54223. + if (txfifosize[i].d32 !=
  54224. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  54225. + retval = 0;
  54226. + }
  54227. + }
  54228. +
  54229. + /** If register values are not set correctly, reset old values */
  54230. + if (retval == 0) {
  54231. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  54232. +
  54233. + /* Non-periodic Tx FIFO */
  54234. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  54235. +
  54236. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  54237. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  54238. + txfsz_bak[i]);
  54239. + }
  54240. + }
  54241. + } else {
  54242. + return 0;
  54243. + }
  54244. +
  54245. + /* Flush the FIFOs */
  54246. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  54247. + dwc_otg_flush_rx_fifo(core_if);
  54248. +
  54249. + return retval;
  54250. +}
  54251. +
  54252. +/**
  54253. + * This function sets a new value for the buffer Alignment setup.
  54254. + */
  54255. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  54256. +{
  54257. + int retval;
  54258. + uint32_t fsiz;
  54259. + uint16_t size;
  54260. + uint16_t ep_addr;
  54261. + dwc_otg_pcd_ep_t *ep;
  54262. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  54263. + tx_fifo_size_setup_t *ptxfifoval;
  54264. +
  54265. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  54266. + ep_addr = ptxfifoval->bEndpointAddress;
  54267. + size = ptxfifoval->wDepth;
  54268. +
  54269. + ep = get_ep_by_addr(pcd, ep_addr);
  54270. +
  54271. + CFI_INFO
  54272. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  54273. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  54274. +
  54275. + if (NULL == ep) {
  54276. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  54277. + __func__, ep_addr);
  54278. + return -DWC_E_INVALID;
  54279. + }
  54280. +
  54281. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  54282. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  54283. +
  54284. + if (resize_fifos(GET_CORE_IF(pcd))) {
  54285. + retval = 0;
  54286. + } else {
  54287. + CFI_INFO
  54288. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  54289. + __func__, ep_addr);
  54290. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  54291. + retval = -DWC_E_INVALID;
  54292. + }
  54293. +
  54294. + return retval;
  54295. +}
  54296. +
  54297. +/**
  54298. + * This function sets a new value for the buffer Alignment setup.
  54299. + */
  54300. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  54301. +{
  54302. + int retval;
  54303. + uint32_t fsiz;
  54304. + uint16_t size;
  54305. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  54306. + rx_fifo_size_setup_t *prxfifoval;
  54307. +
  54308. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  54309. + size = prxfifoval->wDepth;
  54310. +
  54311. + fsiz = params->dev_rx_fifo_size;
  54312. + params->dev_rx_fifo_size = size;
  54313. +
  54314. + if (resize_fifos(GET_CORE_IF(pcd))) {
  54315. + retval = 0;
  54316. + } else {
  54317. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  54318. + __func__);
  54319. + params->dev_rx_fifo_size = fsiz;
  54320. + retval = -DWC_E_INVALID;
  54321. + }
  54322. +
  54323. + return retval;
  54324. +}
  54325. +
  54326. +/**
  54327. + * This function reads the SG of an EP's buffer setup into the buffer buf
  54328. + */
  54329. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  54330. + struct cfi_usb_ctrlrequest *req)
  54331. +{
  54332. + int retval = -DWC_E_INVALID;
  54333. + uint8_t addr;
  54334. + cfi_ep_t *ep;
  54335. +
  54336. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  54337. + addr = req->wValue & 0xFF;
  54338. + if (addr == 0) /* The address should be non-zero */
  54339. + return retval;
  54340. +
  54341. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54342. + if (NULL == ep) {
  54343. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  54344. + __func__, addr);
  54345. + return retval;
  54346. + }
  54347. +
  54348. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  54349. + retval = BS_SG_VAL_DESC_LEN;
  54350. + return retval;
  54351. +}
  54352. +
  54353. +/**
  54354. + * This function reads the Concatenation value of an EP's buffer mode into
  54355. + * the buffer buf
  54356. + */
  54357. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  54358. + struct cfi_usb_ctrlrequest *req)
  54359. +{
  54360. + int retval = -DWC_E_INVALID;
  54361. + uint8_t addr;
  54362. + cfi_ep_t *ep;
  54363. + uint8_t desc_count;
  54364. +
  54365. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  54366. + addr = req->wValue & 0xFF;
  54367. + if (addr == 0) /* The address should be non-zero */
  54368. + return retval;
  54369. +
  54370. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54371. + if (NULL == ep) {
  54372. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  54373. + __func__, addr);
  54374. + return retval;
  54375. + }
  54376. +
  54377. + /* Copy the header to the buffer */
  54378. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  54379. + /* Advance the buffer pointer by the header size */
  54380. + buf += BS_CONCAT_VAL_HDR_LEN;
  54381. +
  54382. + desc_count = ep->bm_concat->hdr.bDescCount;
  54383. + /* Copy alll the wTxBytes to the buffer */
  54384. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  54385. +
  54386. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  54387. + return retval;
  54388. +}
  54389. +
  54390. +/**
  54391. + * This function reads the buffer Alignment value of an EP's buffer mode into
  54392. + * the buffer buf
  54393. + *
  54394. + * @return The total number of bytes copied to the buffer or negative error code.
  54395. + */
  54396. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  54397. + struct cfi_usb_ctrlrequest *req)
  54398. +{
  54399. + int retval = -DWC_E_INVALID;
  54400. + uint8_t addr;
  54401. + cfi_ep_t *ep;
  54402. +
  54403. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  54404. + addr = req->wValue & 0xFF;
  54405. + if (addr == 0) /* The address should be non-zero */
  54406. + return retval;
  54407. +
  54408. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  54409. + if (NULL == ep) {
  54410. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  54411. + __func__, addr);
  54412. + return retval;
  54413. + }
  54414. +
  54415. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  54416. + retval = BS_ALIGN_VAL_HDR_LEN;
  54417. +
  54418. + return retval;
  54419. +}
  54420. +
  54421. +/**
  54422. + * This function sets a new value for the specified feature
  54423. + *
  54424. + * @param pcd A pointer to the PCD object
  54425. + *
  54426. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  54427. + */
  54428. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  54429. +{
  54430. + int retval = -DWC_E_NOT_SUPPORTED;
  54431. + uint16_t wIndex, wValue;
  54432. + uint8_t bRequest;
  54433. + struct dwc_otg_core_if *coreif;
  54434. + cfiobject_t *cfi = pcd->cfi;
  54435. + struct cfi_usb_ctrlrequest *ctrl_req;
  54436. + uint8_t *buf;
  54437. + ctrl_req = &cfi->ctrl_req;
  54438. +
  54439. + buf = pcd->cfi->ctrl_req.data;
  54440. +
  54441. + coreif = GET_CORE_IF(pcd);
  54442. + bRequest = ctrl_req->bRequest;
  54443. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  54444. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  54445. +
  54446. + /* See which feature is to be modified */
  54447. + switch (wIndex) {
  54448. + case FT_ID_DMA_BUFFER_SETUP:
  54449. + /* Modify the feature */
  54450. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  54451. + return retval;
  54452. +
  54453. + /* And send this request to the gadget */
  54454. + cfi->need_gadget_att = 1;
  54455. + break;
  54456. +
  54457. + case FT_ID_DMA_BUFF_ALIGN:
  54458. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  54459. + return retval;
  54460. + cfi->need_gadget_att = 1;
  54461. + break;
  54462. +
  54463. + case FT_ID_DMA_CONCAT_SETUP:
  54464. + /* Modify the feature */
  54465. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  54466. + return retval;
  54467. + cfi->need_gadget_att = 1;
  54468. + break;
  54469. +
  54470. + case FT_ID_DMA_CIRCULAR:
  54471. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  54472. + break;
  54473. +
  54474. + case FT_ID_THRESHOLD_SETUP:
  54475. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  54476. + break;
  54477. +
  54478. + case FT_ID_DFIFO_DEPTH:
  54479. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  54480. + break;
  54481. +
  54482. + case FT_ID_TX_FIFO_DEPTH:
  54483. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  54484. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  54485. + return retval;
  54486. + cfi->need_gadget_att = 0;
  54487. + break;
  54488. +
  54489. + case FT_ID_RX_FIFO_DEPTH:
  54490. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  54491. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  54492. + return retval;
  54493. + cfi->need_gadget_att = 0;
  54494. + break;
  54495. + }
  54496. +
  54497. + return retval;
  54498. +}
  54499. +
  54500. +#endif //DWC_UTE_CFI
  54501. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cfi.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  54502. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 1970-01-01 01:00:00.000000000 +0100
  54503. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 2014-03-11 16:55:38.000000000 +0100
  54504. @@ -0,0 +1,320 @@
  54505. +/* ==========================================================================
  54506. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  54507. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  54508. + * otherwise expressly agreed to in writing between Synopsys and you.
  54509. + *
  54510. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  54511. + * any End User Software License Agreement or Agreement for Licensed Product
  54512. + * with Synopsys or any supplement thereto. You are permitted to use and
  54513. + * redistribute this Software in source and binary forms, with or without
  54514. + * modification, provided that redistributions of source code must retain this
  54515. + * notice. You may not view, use, disclose, copy or distribute this file or
  54516. + * any information contained herein except pursuant to this license grant from
  54517. + * Synopsys. If you do not agree with this notice, including the disclaimer
  54518. + * below, then you are not authorized to use the Software.
  54519. + *
  54520. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  54521. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  54522. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  54523. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  54524. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  54525. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  54526. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  54527. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  54528. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  54529. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  54530. + * DAMAGE.
  54531. + * ========================================================================== */
  54532. +
  54533. +#if !defined(__DWC_OTG_CFI_H__)
  54534. +#define __DWC_OTG_CFI_H__
  54535. +
  54536. +#include "dwc_otg_pcd.h"
  54537. +#include "dwc_cfi_common.h"
  54538. +
  54539. +/**
  54540. + * @file
  54541. + * This file contains the CFI related OTG PCD specific common constants,
  54542. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  54543. + * optional interface for internal testing purposes that a DUT may implement to
  54544. + * support testing of configurable features.
  54545. + *
  54546. + */
  54547. +
  54548. +struct dwc_otg_pcd;
  54549. +struct dwc_otg_pcd_ep;
  54550. +
  54551. +/** OTG CFI Features (properties) ID constants */
  54552. +/** This is a request for all Core Features */
  54553. +#define FT_ID_DMA_MODE 0x0001
  54554. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  54555. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  54556. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  54557. +#define FT_ID_DMA_CIRCULAR 0x0005
  54558. +#define FT_ID_THRESHOLD_SETUP 0x0006
  54559. +#define FT_ID_DFIFO_DEPTH 0x0007
  54560. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  54561. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  54562. +
  54563. +/**********************************************************/
  54564. +#define CFI_INFO_DEF
  54565. +
  54566. +#ifdef CFI_INFO_DEF
  54567. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  54568. +#else
  54569. +#define CFI_INFO(fmt...)
  54570. +#endif
  54571. +
  54572. +#define min(x,y) ({ \
  54573. + x < y ? x : y; })
  54574. +
  54575. +#define max(x,y) ({ \
  54576. + x > y ? x : y; })
  54577. +
  54578. +/**
  54579. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  54580. + * also used for setting up a buffer for Circular DDMA.
  54581. + */
  54582. +struct _ddma_sg_buffer_setup {
  54583. +#define BS_SG_VAL_DESC_LEN 6
  54584. + /* The OUT EP address */
  54585. + uint8_t bOutEndpointAddress;
  54586. + /* The IN EP address */
  54587. + uint8_t bInEndpointAddress;
  54588. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  54589. + uint8_t bOffset;
  54590. + /* The number of transfer segments (a DMA descriptors per each segment) */
  54591. + uint8_t bCount;
  54592. + /* Size (in byte) of each transfer segment */
  54593. + uint16_t wSize;
  54594. +} __attribute__ ((packed));
  54595. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  54596. +
  54597. +/** Descriptor DMA Concatenation Buffer setup structure */
  54598. +struct _ddma_concat_buffer_setup_hdr {
  54599. +#define BS_CONCAT_VAL_HDR_LEN 4
  54600. + /* The endpoint for which the buffer is to be set up */
  54601. + uint8_t bEndpointAddress;
  54602. + /* The count of descriptors to be used */
  54603. + uint8_t bDescCount;
  54604. + /* The total size of the transfer */
  54605. + uint16_t wSize;
  54606. +} __attribute__ ((packed));
  54607. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  54608. +
  54609. +/** Descriptor DMA Concatenation Buffer setup structure */
  54610. +struct _ddma_concat_buffer_setup {
  54611. + /* The SG header */
  54612. + ddma_concat_buffer_setup_hdr_t hdr;
  54613. +
  54614. + /* The XFER sizes pointer (allocated dynamically) */
  54615. + uint16_t *wTxBytes;
  54616. +} __attribute__ ((packed));
  54617. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  54618. +
  54619. +/** Descriptor DMA Alignment Buffer setup structure */
  54620. +struct _ddma_align_buffer_setup {
  54621. +#define BS_ALIGN_VAL_HDR_LEN 2
  54622. + uint8_t bEndpointAddress;
  54623. + uint8_t bAlign;
  54624. +} __attribute__ ((packed));
  54625. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  54626. +
  54627. +/** Transmit FIFO Size setup structure */
  54628. +struct _tx_fifo_size_setup {
  54629. + uint8_t bEndpointAddress;
  54630. + uint16_t wDepth;
  54631. +} __attribute__ ((packed));
  54632. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  54633. +
  54634. +/** Transmit FIFO Size setup structure */
  54635. +struct _rx_fifo_size_setup {
  54636. + uint16_t wDepth;
  54637. +} __attribute__ ((packed));
  54638. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  54639. +
  54640. +/**
  54641. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  54642. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  54643. + * to the data returned in the data stage of a 3-stage Control Write requests.
  54644. + */
  54645. +struct cfi_usb_ctrlrequest {
  54646. + uint8_t bRequestType;
  54647. + uint8_t bRequest;
  54648. + uint16_t wValue;
  54649. + uint16_t wIndex;
  54650. + uint16_t wLength;
  54651. + uint8_t *data;
  54652. +} UPACKED;
  54653. +
  54654. +/*---------------------------------------------------------------------------*/
  54655. +
  54656. +/**
  54657. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  54658. + * This structure is used to store the buffer setup data for any
  54659. + * enabled endpoint in the PCD.
  54660. + */
  54661. +struct cfi_ep {
  54662. + /* Entry for the list container */
  54663. + dwc_list_link_t lh;
  54664. + /* Pointer to the active PCD endpoint structure */
  54665. + struct dwc_otg_pcd_ep *ep;
  54666. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  54667. + struct dwc_otg_dma_desc *dma_desc_last;
  54668. + /* The SG feature value */
  54669. + ddma_sg_buffer_setup_t *bm_sg;
  54670. + /* The Circular feature value */
  54671. + ddma_sg_buffer_setup_t *bm_circ;
  54672. + /* The Concatenation feature value */
  54673. + ddma_concat_buffer_setup_t *bm_concat;
  54674. + /* The Alignment feature value */
  54675. + ddma_align_buffer_setup_t *bm_align;
  54676. + /* XFER length */
  54677. + uint32_t xfer_len;
  54678. + /*
  54679. + * Count of DMA descriptors currently used.
  54680. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  54681. + * defined in the dwc_otg_cil.h
  54682. + */
  54683. + uint32_t desc_count;
  54684. +};
  54685. +typedef struct cfi_ep cfi_ep_t;
  54686. +
  54687. +typedef struct cfi_dma_buff {
  54688. +#define CFI_IN_BUF_LEN 1024
  54689. +#define CFI_OUT_BUF_LEN 1024
  54690. + dma_addr_t addr;
  54691. + uint8_t *buf;
  54692. +} cfi_dma_buff_t;
  54693. +
  54694. +struct cfiobject;
  54695. +
  54696. +/**
  54697. + * This is the interface for the CFI operations.
  54698. + *
  54699. + * @param ep_enable Called when any endpoint is enabled and activated.
  54700. + * @param release Called when the CFI object is released and it needs to correctly
  54701. + * deallocate the dynamic memory
  54702. + * @param ctrl_write_complete Called when the data stage of the request is complete
  54703. + */
  54704. +typedef struct cfi_ops {
  54705. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  54706. + struct dwc_otg_pcd_ep * ep);
  54707. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  54708. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  54709. + unsigned size, gfp_t flags);
  54710. + void (*release) (struct cfiobject * cfi);
  54711. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  54712. + struct dwc_otg_pcd * pcd);
  54713. + void (*build_descriptors) (struct cfiobject * cfi,
  54714. + struct dwc_otg_pcd * pcd,
  54715. + struct dwc_otg_pcd_ep * ep,
  54716. + dwc_otg_pcd_request_t * req);
  54717. +} cfi_ops_t;
  54718. +
  54719. +struct cfiobject {
  54720. + cfi_ops_t ops;
  54721. + struct dwc_otg_pcd *pcd;
  54722. + struct usb_gadget *gadget;
  54723. +
  54724. + /* Buffers used to send/receive CFI-related request data */
  54725. + cfi_dma_buff_t buf_in;
  54726. + cfi_dma_buff_t buf_out;
  54727. +
  54728. + /* CFI specific Control request wrapper */
  54729. + struct cfi_usb_ctrlrequest ctrl_req;
  54730. +
  54731. + /* The list of active EP's in the PCD of type cfi_ep_t */
  54732. + dwc_list_link_t active_eps;
  54733. +
  54734. + /* This flag shall control the propagation of a specific request
  54735. + * to the gadget's processing routines.
  54736. + * 0 - no gadget handling
  54737. + * 1 - the gadget needs to know about this request (w/o completing a status
  54738. + * phase - just return a 0 to the _setup callback)
  54739. + */
  54740. + uint8_t need_gadget_att;
  54741. +
  54742. + /* Flag indicating whether the status IN phase needs to be
  54743. + * completed by the PCD
  54744. + */
  54745. + uint8_t need_status_in_complete;
  54746. +};
  54747. +typedef struct cfiobject cfiobject_t;
  54748. +
  54749. +#define DUMP_MSG
  54750. +
  54751. +#if defined(DUMP_MSG)
  54752. +static inline void dump_msg(const u8 * buf, unsigned int length)
  54753. +{
  54754. + unsigned int start, num, i;
  54755. + char line[52], *p;
  54756. +
  54757. + if (length >= 512)
  54758. + return;
  54759. +
  54760. + start = 0;
  54761. + while (length > 0) {
  54762. + num = min(length, 16u);
  54763. + p = line;
  54764. + for (i = 0; i < num; ++i) {
  54765. + if (i == 8)
  54766. + *p++ = ' ';
  54767. + DWC_SPRINTF(p, " %02x", buf[i]);
  54768. + p += 3;
  54769. + }
  54770. + *p = 0;
  54771. + DWC_DEBUG("%6x: %s\n", start, line);
  54772. + buf += num;
  54773. + start += num;
  54774. + length -= num;
  54775. + }
  54776. +}
  54777. +#else
  54778. +static inline void dump_msg(const u8 * buf, unsigned int length)
  54779. +{
  54780. +}
  54781. +#endif
  54782. +
  54783. +/**
  54784. + * This function returns a pointer to cfi_ep_t object with the addr address.
  54785. + */
  54786. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  54787. + uint8_t addr)
  54788. +{
  54789. + struct cfi_ep *pcfiep;
  54790. + dwc_list_link_t *tmp;
  54791. +
  54792. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54793. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54794. +
  54795. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  54796. + return pcfiep;
  54797. + }
  54798. + }
  54799. +
  54800. + return NULL;
  54801. +}
  54802. +
  54803. +/**
  54804. + * This function returns a pointer to cfi_ep_t object that matches
  54805. + * the dwc_otg_pcd_ep object.
  54806. + */
  54807. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  54808. + struct dwc_otg_pcd_ep *ep)
  54809. +{
  54810. + struct cfi_ep *pcfiep = NULL;
  54811. + dwc_list_link_t *tmp;
  54812. +
  54813. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  54814. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  54815. + if (pcfiep->ep == ep) {
  54816. + return pcfiep;
  54817. + }
  54818. + }
  54819. + return NULL;
  54820. +}
  54821. +
  54822. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  54823. +
  54824. +#endif /* (__DWC_OTG_CFI_H__) */
  54825. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cil.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  54826. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cil.c 1970-01-01 01:00:00.000000000 +0100
  54827. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil.c 2014-03-11 16:55:38.000000000 +0100
  54828. @@ -0,0 +1,7151 @@
  54829. +/* ==========================================================================
  54830. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  54831. + * $Revision: #191 $
  54832. + * $Date: 2012/08/10 $
  54833. + * $Change: 2047372 $
  54834. + *
  54835. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  54836. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  54837. + * otherwise expressly agreed to in writing between Synopsys and you.
  54838. + *
  54839. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  54840. + * any End User Software License Agreement or Agreement for Licensed Product
  54841. + * with Synopsys or any supplement thereto. You are permitted to use and
  54842. + * redistribute this Software in source and binary forms, with or without
  54843. + * modification, provided that redistributions of source code must retain this
  54844. + * notice. You may not view, use, disclose, copy or distribute this file or
  54845. + * any information contained herein except pursuant to this license grant from
  54846. + * Synopsys. If you do not agree with this notice, including the disclaimer
  54847. + * below, then you are not authorized to use the Software.
  54848. + *
  54849. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  54850. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  54851. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  54852. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  54853. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  54854. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  54855. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  54856. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  54857. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  54858. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  54859. + * DAMAGE.
  54860. + * ========================================================================== */
  54861. +
  54862. +/** @file
  54863. + *
  54864. + * The Core Interface Layer provides basic services for accessing and
  54865. + * managing the DWC_otg hardware. These services are used by both the
  54866. + * Host Controller Driver and the Peripheral Controller Driver.
  54867. + *
  54868. + * The CIL manages the memory map for the core so that the HCD and PCD
  54869. + * don't have to do this separately. It also handles basic tasks like
  54870. + * reading/writing the registers and data FIFOs in the controller.
  54871. + * Some of the data access functions provide encapsulation of several
  54872. + * operations required to perform a task, such as writing multiple
  54873. + * registers to start a transfer. Finally, the CIL performs basic
  54874. + * services that are not specific to either the host or device modes
  54875. + * of operation. These services include management of the OTG Host
  54876. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  54877. + * Diagnostic API is also provided to allow testing of the controller
  54878. + * hardware.
  54879. + *
  54880. + * The Core Interface Layer has the following requirements:
  54881. + * - Provides basic controller operations.
  54882. + * - Minimal use of OS services.
  54883. + * - The OS services used will be abstracted by using inline functions
  54884. + * or macros.
  54885. + *
  54886. + */
  54887. +
  54888. +#include "dwc_os.h"
  54889. +#include "dwc_otg_regs.h"
  54890. +#include "dwc_otg_cil.h"
  54891. +
  54892. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  54893. +
  54894. +/**
  54895. + * This function is called to initialize the DWC_otg CSR data
  54896. + * structures. The register addresses in the device and host
  54897. + * structures are initialized from the base address supplied by the
  54898. + * caller. The calling function must make the OS calls to get the
  54899. + * base address of the DWC_otg controller registers. The core_params
  54900. + * argument holds the parameters that specify how the core should be
  54901. + * configured.
  54902. + *
  54903. + * @param reg_base_addr Base address of DWC_otg core registers
  54904. + *
  54905. + */
  54906. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  54907. +{
  54908. + dwc_otg_core_if_t *core_if = 0;
  54909. + dwc_otg_dev_if_t *dev_if = 0;
  54910. + dwc_otg_host_if_t *host_if = 0;
  54911. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  54912. + int i = 0;
  54913. +
  54914. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  54915. +
  54916. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  54917. +
  54918. + if (core_if == NULL) {
  54919. + DWC_DEBUGPL(DBG_CIL,
  54920. + "Allocation of dwc_otg_core_if_t failed\n");
  54921. + return 0;
  54922. + }
  54923. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  54924. +
  54925. + /*
  54926. + * Allocate the Device Mode structures.
  54927. + */
  54928. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  54929. +
  54930. + if (dev_if == NULL) {
  54931. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  54932. + DWC_FREE(core_if);
  54933. + return 0;
  54934. + }
  54935. +
  54936. + dev_if->dev_global_regs =
  54937. + (dwc_otg_device_global_regs_t *) (reg_base +
  54938. + DWC_DEV_GLOBAL_REG_OFFSET);
  54939. +
  54940. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  54941. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  54942. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  54943. + (i * DWC_EP_REG_OFFSET));
  54944. +
  54945. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  54946. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  54947. + (i * DWC_EP_REG_OFFSET));
  54948. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  54949. + i, &dev_if->in_ep_regs[i]->diepctl);
  54950. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  54951. + i, &dev_if->out_ep_regs[i]->doepctl);
  54952. + }
  54953. +
  54954. + dev_if->speed = 0; // unknown
  54955. +
  54956. + core_if->dev_if = dev_if;
  54957. +
  54958. + /*
  54959. + * Allocate the Host Mode structures.
  54960. + */
  54961. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  54962. +
  54963. + if (host_if == NULL) {
  54964. + DWC_DEBUGPL(DBG_CIL,
  54965. + "Allocation of dwc_otg_host_if_t failed\n");
  54966. + DWC_FREE(dev_if);
  54967. + DWC_FREE(core_if);
  54968. + return 0;
  54969. + }
  54970. +
  54971. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  54972. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  54973. +
  54974. + host_if->hprt0 =
  54975. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  54976. +
  54977. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  54978. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  54979. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  54980. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  54981. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  54982. + i, &host_if->hc_regs[i]->hcchar);
  54983. + }
  54984. +
  54985. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  54986. + core_if->host_if = host_if;
  54987. +
  54988. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  54989. + core_if->data_fifo[i] =
  54990. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  54991. + (i * DWC_OTG_DATA_FIFO_SIZE));
  54992. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  54993. + i, (unsigned long)core_if->data_fifo[i]);
  54994. + }
  54995. +
  54996. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  54997. +
  54998. + /* Initiate lx_state to L3 disconnected state */
  54999. + core_if->lx_state = DWC_OTG_L3;
  55000. + /*
  55001. + * Store the contents of the hardware configuration registers here for
  55002. + * easy access later.
  55003. + */
  55004. + core_if->hwcfg1.d32 =
  55005. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  55006. + core_if->hwcfg2.d32 =
  55007. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  55008. + core_if->hwcfg3.d32 =
  55009. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  55010. + core_if->hwcfg4.d32 =
  55011. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  55012. +
  55013. + /* Force host mode to get HPTXFSIZ exact power on value */
  55014. + {
  55015. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  55016. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  55017. + gusbcfg.b.force_host_mode = 1;
  55018. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  55019. + dwc_mdelay(100);
  55020. + core_if->hptxfsiz.d32 =
  55021. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  55022. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  55023. + gusbcfg.b.force_host_mode = 0;
  55024. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  55025. + dwc_mdelay(100);
  55026. + }
  55027. +
  55028. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  55029. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  55030. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  55031. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  55032. +
  55033. + core_if->hcfg.d32 =
  55034. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  55035. + core_if->dcfg.d32 =
  55036. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  55037. +
  55038. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  55039. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  55040. +
  55041. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  55042. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  55043. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  55044. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  55045. + core_if->hwcfg2.b.num_host_chan);
  55046. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  55047. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  55048. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  55049. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  55050. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  55051. + core_if->hwcfg2.b.dev_token_q_depth);
  55052. +
  55053. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  55054. + core_if->hwcfg3.b.dfifo_depth);
  55055. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  55056. + core_if->hwcfg3.b.xfer_size_cntr_width);
  55057. +
  55058. + /*
  55059. + * Set the SRP sucess bit for FS-I2c
  55060. + */
  55061. + core_if->srp_success = 0;
  55062. + core_if->srp_timer_started = 0;
  55063. +
  55064. + /*
  55065. + * Create new workqueue and init works
  55066. + */
  55067. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  55068. + if (core_if->wq_otg == 0) {
  55069. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  55070. + DWC_FREE(host_if);
  55071. + DWC_FREE(dev_if);
  55072. + DWC_FREE(core_if);
  55073. + return 0;
  55074. + }
  55075. +
  55076. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  55077. +
  55078. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  55079. + (core_if->snpsid >> 12 & 0xF),
  55080. + (core_if->snpsid >> 8 & 0xF),
  55081. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  55082. +
  55083. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  55084. + w_wakeup_detected, core_if);
  55085. + if (core_if->wkp_timer == 0) {
  55086. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  55087. + DWC_FREE(host_if);
  55088. + DWC_FREE(dev_if);
  55089. + DWC_WORKQ_FREE(core_if->wq_otg);
  55090. + DWC_FREE(core_if);
  55091. + return 0;
  55092. + }
  55093. +
  55094. + if (dwc_otg_setup_params(core_if)) {
  55095. + DWC_WARN("Error while setting core params\n");
  55096. + }
  55097. +
  55098. + core_if->hibernation_suspend = 0;
  55099. +
  55100. + /** ADP initialization */
  55101. + dwc_otg_adp_init(core_if);
  55102. +
  55103. + return core_if;
  55104. +}
  55105. +
  55106. +/**
  55107. + * This function frees the structures allocated by dwc_otg_cil_init().
  55108. + *
  55109. + * @param core_if The core interface pointer returned from
  55110. + * dwc_otg_cil_init().
  55111. + *
  55112. + */
  55113. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  55114. +{
  55115. + dctl_data_t dctl = {.d32 = 0 };
  55116. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  55117. +
  55118. + /* Disable all interrupts */
  55119. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  55120. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  55121. +
  55122. + dctl.b.sftdiscon = 1;
  55123. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  55124. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  55125. + dctl.d32);
  55126. + }
  55127. +
  55128. + if (core_if->wq_otg) {
  55129. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  55130. + DWC_WORKQ_FREE(core_if->wq_otg);
  55131. + }
  55132. + if (core_if->dev_if) {
  55133. + DWC_FREE(core_if->dev_if);
  55134. + }
  55135. + if (core_if->host_if) {
  55136. + DWC_FREE(core_if->host_if);
  55137. + }
  55138. +
  55139. + /** Remove ADP Stuff */
  55140. + dwc_otg_adp_remove(core_if);
  55141. + if (core_if->core_params) {
  55142. + DWC_FREE(core_if->core_params);
  55143. + }
  55144. + if (core_if->wkp_timer) {
  55145. + DWC_TIMER_FREE(core_if->wkp_timer);
  55146. + }
  55147. + if (core_if->srp_timer) {
  55148. + DWC_TIMER_FREE(core_if->srp_timer);
  55149. + }
  55150. + DWC_FREE(core_if);
  55151. +}
  55152. +
  55153. +/**
  55154. + * This function enables the controller's Global Interrupt in the AHB Config
  55155. + * register.
  55156. + *
  55157. + * @param core_if Programming view of DWC_otg controller.
  55158. + */
  55159. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  55160. +{
  55161. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  55162. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  55163. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  55164. +}
  55165. +
  55166. +/**
  55167. + * This function disables the controller's Global Interrupt in the AHB Config
  55168. + * register.
  55169. + *
  55170. + * @param core_if Programming view of DWC_otg controller.
  55171. + */
  55172. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  55173. +{
  55174. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  55175. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  55176. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  55177. +}
  55178. +
  55179. +/**
  55180. + * This function initializes the commmon interrupts, used in both
  55181. + * device and host modes.
  55182. + *
  55183. + * @param core_if Programming view of the DWC_otg controller
  55184. + *
  55185. + */
  55186. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  55187. +{
  55188. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  55189. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55190. +
  55191. + /* Clear any pending OTG Interrupts */
  55192. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  55193. +
  55194. + /* Clear any pending interrupts */
  55195. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  55196. +
  55197. + /*
  55198. + * Enable the interrupts in the GINTMSK.
  55199. + */
  55200. + intr_mask.b.modemismatch = 1;
  55201. + intr_mask.b.otgintr = 1;
  55202. +
  55203. + if (!core_if->dma_enable) {
  55204. + intr_mask.b.rxstsqlvl = 1;
  55205. + }
  55206. +
  55207. + intr_mask.b.conidstschng = 1;
  55208. + intr_mask.b.wkupintr = 1;
  55209. + intr_mask.b.disconnect = 0;
  55210. + intr_mask.b.usbsuspend = 1;
  55211. + intr_mask.b.sessreqintr = 1;
  55212. +#ifdef CONFIG_USB_DWC_OTG_LPM
  55213. + if (core_if->core_params->lpm_enable) {
  55214. + intr_mask.b.lpmtranrcvd = 1;
  55215. + }
  55216. +#endif
  55217. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  55218. +}
  55219. +
  55220. +/*
  55221. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  55222. + * Hibernation. This function is for exiting from Device mode hibernation by
  55223. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  55224. + * @param core_if Programming view of DWC_otg controller.
  55225. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  55226. + * @param reset - indicates whether resume is initiated by Reset.
  55227. + */
  55228. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  55229. + int rem_wakeup, int reset)
  55230. +{
  55231. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  55232. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  55233. + dctl_data_t dctl = {.d32 = 0 };
  55234. +
  55235. + int timeout = 2000;
  55236. +
  55237. + if (!core_if->hibernation_suspend) {
  55238. + DWC_PRINTF("Already exited from Hibernation\n");
  55239. + return 1;
  55240. + }
  55241. +
  55242. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  55243. + /* Switch-on voltage to the core */
  55244. + gpwrdn.b.pwrdnswtch = 1;
  55245. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55246. + dwc_udelay(10);
  55247. +
  55248. + /* Reset core */
  55249. + gpwrdn.d32 = 0;
  55250. + gpwrdn.b.pwrdnrstn = 1;
  55251. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55252. + dwc_udelay(10);
  55253. +
  55254. + /* Assert Restore signal */
  55255. + gpwrdn.d32 = 0;
  55256. + gpwrdn.b.restore = 1;
  55257. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  55258. + dwc_udelay(10);
  55259. +
  55260. + /* Disable power clamps */
  55261. + gpwrdn.d32 = 0;
  55262. + gpwrdn.b.pwrdnclmp = 1;
  55263. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55264. +
  55265. + if (rem_wakeup) {
  55266. + dwc_udelay(70);
  55267. + }
  55268. +
  55269. + /* Deassert Reset core */
  55270. + gpwrdn.d32 = 0;
  55271. + gpwrdn.b.pwrdnrstn = 1;
  55272. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  55273. + dwc_udelay(10);
  55274. +
  55275. + /* Disable PMU interrupt */
  55276. + gpwrdn.d32 = 0;
  55277. + gpwrdn.b.pmuintsel = 1;
  55278. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55279. +
  55280. + /* Mask interrupts from gpwrdn */
  55281. + gpwrdn.d32 = 0;
  55282. + gpwrdn.b.connect_det_msk = 1;
  55283. + gpwrdn.b.srp_det_msk = 1;
  55284. + gpwrdn.b.disconn_det_msk = 1;
  55285. + gpwrdn.b.rst_det_msk = 1;
  55286. + gpwrdn.b.lnstchng_msk = 1;
  55287. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55288. +
  55289. + /* Indicates that we are going out from hibernation */
  55290. + core_if->hibernation_suspend = 0;
  55291. +
  55292. + /*
  55293. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  55294. + * indicates restore from remote_wakeup
  55295. + */
  55296. + restore_essential_regs(core_if, rem_wakeup, 0);
  55297. +
  55298. + /*
  55299. + * Wait a little for seeing new value of variable hibernation_suspend if
  55300. + * Restore done interrupt received before polling
  55301. + */
  55302. + dwc_udelay(10);
  55303. +
  55304. + if (core_if->hibernation_suspend == 0) {
  55305. + /*
  55306. + * Wait For Restore_done Interrupt. This mechanism of polling the
  55307. + * interrupt is introduced to avoid any possible race conditions
  55308. + */
  55309. + do {
  55310. + gintsts_data_t gintsts;
  55311. + gintsts.d32 =
  55312. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  55313. + if (gintsts.b.restoredone) {
  55314. + gintsts.d32 = 0;
  55315. + gintsts.b.restoredone = 1;
  55316. + DWC_WRITE_REG32(&core_if->core_global_regs->
  55317. + gintsts, gintsts.d32);
  55318. + DWC_PRINTF("Restore Done Interrupt seen\n");
  55319. + break;
  55320. + }
  55321. + dwc_udelay(10);
  55322. + } while (--timeout);
  55323. + if (!timeout) {
  55324. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  55325. + }
  55326. + }
  55327. + /* Clear all pending interupts */
  55328. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  55329. +
  55330. + /* De-assert Restore */
  55331. + gpwrdn.d32 = 0;
  55332. + gpwrdn.b.restore = 1;
  55333. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55334. + dwc_udelay(10);
  55335. +
  55336. + if (!rem_wakeup) {
  55337. + pcgcctl.d32 = 0;
  55338. + pcgcctl.b.rstpdwnmodule = 1;
  55339. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  55340. + }
  55341. +
  55342. + /* Restore GUSBCFG and DCFG */
  55343. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  55344. + core_if->gr_backup->gusbcfg_local);
  55345. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  55346. + core_if->dr_backup->dcfg);
  55347. +
  55348. + /* De-assert Wakeup Logic */
  55349. + gpwrdn.d32 = 0;
  55350. + gpwrdn.b.pmuactv = 1;
  55351. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55352. + dwc_udelay(10);
  55353. +
  55354. + if (!rem_wakeup) {
  55355. + /* Set Device programming done bit */
  55356. + dctl.b.pwronprgdone = 1;
  55357. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  55358. + } else {
  55359. + /* Start Remote Wakeup Signaling */
  55360. + dctl.d32 = core_if->dr_backup->dctl;
  55361. + dctl.b.rmtwkupsig = 1;
  55362. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  55363. + }
  55364. +
  55365. + dwc_mdelay(2);
  55366. + /* Clear all pending interupts */
  55367. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  55368. +
  55369. + /* Restore global registers */
  55370. + dwc_otg_restore_global_regs(core_if);
  55371. + /* Restore device global registers */
  55372. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  55373. +
  55374. + if (rem_wakeup) {
  55375. + dwc_mdelay(7);
  55376. + dctl.d32 = 0;
  55377. + dctl.b.rmtwkupsig = 1;
  55378. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  55379. + }
  55380. +
  55381. + core_if->hibernation_suspend = 0;
  55382. + /* The core will be in ON STATE */
  55383. + core_if->lx_state = DWC_OTG_L0;
  55384. + DWC_PRINTF("Hibernation recovery completes here\n");
  55385. +
  55386. + return 1;
  55387. +}
  55388. +
  55389. +/*
  55390. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  55391. + * Hibernation. This function is for exiting from Host mode hibernation by
  55392. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  55393. + * @param core_if Programming view of DWC_otg controller.
  55394. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  55395. + * @param reset - indicates whether resume is initiated by Reset.
  55396. + */
  55397. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  55398. + int rem_wakeup, int reset)
  55399. +{
  55400. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  55401. + hprt0_data_t hprt0 = {.d32 = 0 };
  55402. +
  55403. + int timeout = 2000;
  55404. +
  55405. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  55406. + /* Switch-on voltage to the core */
  55407. + gpwrdn.b.pwrdnswtch = 1;
  55408. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55409. + dwc_udelay(10);
  55410. +
  55411. + /* Reset core */
  55412. + gpwrdn.d32 = 0;
  55413. + gpwrdn.b.pwrdnrstn = 1;
  55414. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55415. + dwc_udelay(10);
  55416. +
  55417. + /* Assert Restore signal */
  55418. + gpwrdn.d32 = 0;
  55419. + gpwrdn.b.restore = 1;
  55420. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  55421. + dwc_udelay(10);
  55422. +
  55423. + /* Disable power clamps */
  55424. + gpwrdn.d32 = 0;
  55425. + gpwrdn.b.pwrdnclmp = 1;
  55426. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55427. +
  55428. + if (!rem_wakeup) {
  55429. + dwc_udelay(50);
  55430. + }
  55431. +
  55432. + /* Deassert Reset core */
  55433. + gpwrdn.d32 = 0;
  55434. + gpwrdn.b.pwrdnrstn = 1;
  55435. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  55436. + dwc_udelay(10);
  55437. +
  55438. + /* Disable PMU interrupt */
  55439. + gpwrdn.d32 = 0;
  55440. + gpwrdn.b.pmuintsel = 1;
  55441. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55442. +
  55443. + gpwrdn.d32 = 0;
  55444. + gpwrdn.b.connect_det_msk = 1;
  55445. + gpwrdn.b.srp_det_msk = 1;
  55446. + gpwrdn.b.disconn_det_msk = 1;
  55447. + gpwrdn.b.rst_det_msk = 1;
  55448. + gpwrdn.b.lnstchng_msk = 1;
  55449. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55450. +
  55451. + /* Indicates that we are going out from hibernation */
  55452. + core_if->hibernation_suspend = 0;
  55453. +
  55454. + /* Set Restore Essential Regs bit in PCGCCTL register */
  55455. + restore_essential_regs(core_if, rem_wakeup, 1);
  55456. +
  55457. + /* Wait a little for seeing new value of variable hibernation_suspend if
  55458. + * Restore done interrupt received before polling */
  55459. + dwc_udelay(10);
  55460. +
  55461. + if (core_if->hibernation_suspend == 0) {
  55462. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  55463. + * interrupt is introduced to avoid any possible race conditions
  55464. + */
  55465. + do {
  55466. + gintsts_data_t gintsts;
  55467. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  55468. + if (gintsts.b.restoredone) {
  55469. + gintsts.d32 = 0;
  55470. + gintsts.b.restoredone = 1;
  55471. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  55472. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  55473. + break;
  55474. + }
  55475. + dwc_udelay(10);
  55476. + } while (--timeout);
  55477. + if (!timeout) {
  55478. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  55479. + }
  55480. + }
  55481. +
  55482. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  55483. + core_if->hibernation_suspend = 0;
  55484. +
  55485. + /* This step is not described in functional spec but if not wait for this
  55486. + * delay, mismatch interrupts occurred because just after restore core is
  55487. + * in Device mode(gintsts.curmode == 0) */
  55488. + dwc_mdelay(100);
  55489. +
  55490. + /* Clear all pending interrupts */
  55491. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  55492. +
  55493. + /* De-assert Restore */
  55494. + gpwrdn.d32 = 0;
  55495. + gpwrdn.b.restore = 1;
  55496. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55497. + dwc_udelay(10);
  55498. +
  55499. + /* Restore GUSBCFG and HCFG */
  55500. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  55501. + core_if->gr_backup->gusbcfg_local);
  55502. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  55503. + core_if->hr_backup->hcfg_local);
  55504. +
  55505. + /* De-assert Wakeup Logic */
  55506. + gpwrdn.d32 = 0;
  55507. + gpwrdn.b.pmuactv = 1;
  55508. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  55509. + dwc_udelay(10);
  55510. +
  55511. + /* Start the Resume operation by programming HPRT0 */
  55512. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  55513. + hprt0.b.prtpwr = 1;
  55514. + hprt0.b.prtena = 0;
  55515. + hprt0.b.prtsusp = 0;
  55516. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  55517. +
  55518. + DWC_PRINTF("Resume Starts Now\n");
  55519. + if (!reset) { // Indicates it is Resume Operation
  55520. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  55521. + hprt0.b.prtres = 1;
  55522. + hprt0.b.prtpwr = 1;
  55523. + hprt0.b.prtena = 0;
  55524. + hprt0.b.prtsusp = 0;
  55525. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  55526. +
  55527. + if (!rem_wakeup)
  55528. + hprt0.b.prtres = 0;
  55529. + /* Wait for Resume time and then program HPRT again */
  55530. + dwc_mdelay(100);
  55531. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  55532. +
  55533. + } else { // Indicates it is Reset Operation
  55534. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  55535. + hprt0.b.prtrst = 1;
  55536. + hprt0.b.prtpwr = 1;
  55537. + hprt0.b.prtena = 0;
  55538. + hprt0.b.prtsusp = 0;
  55539. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  55540. + /* Wait for Reset time and then program HPRT again */
  55541. + dwc_mdelay(60);
  55542. + hprt0.b.prtrst = 0;
  55543. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  55544. + }
  55545. + /* Clear all interrupt status */
  55546. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  55547. + hprt0.b.prtconndet = 1;
  55548. + hprt0.b.prtenchng = 1;
  55549. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  55550. +
  55551. + /* Clear all pending interupts */
  55552. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  55553. +
  55554. + /* Restore global registers */
  55555. + dwc_otg_restore_global_regs(core_if);
  55556. + /* Restore host global registers */
  55557. + dwc_otg_restore_host_regs(core_if, reset);
  55558. +
  55559. + /* The core will be in ON STATE */
  55560. + core_if->lx_state = DWC_OTG_L0;
  55561. + DWC_PRINTF("Hibernation recovery is complete here\n");
  55562. + return 0;
  55563. +}
  55564. +
  55565. +/** Saves some register values into system memory. */
  55566. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  55567. +{
  55568. + struct dwc_otg_global_regs_backup *gr;
  55569. + int i;
  55570. +
  55571. + gr = core_if->gr_backup;
  55572. + if (!gr) {
  55573. + gr = DWC_ALLOC(sizeof(*gr));
  55574. + if (!gr) {
  55575. + return -DWC_E_NO_MEMORY;
  55576. + }
  55577. + core_if->gr_backup = gr;
  55578. + }
  55579. +
  55580. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  55581. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  55582. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  55583. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  55584. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  55585. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  55586. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  55587. +#ifdef CONFIG_USB_DWC_OTG_LPM
  55588. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  55589. +#endif
  55590. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  55591. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  55592. + gr->gdfifocfg_local =
  55593. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  55594. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  55595. + gr->dtxfsiz_local[i] =
  55596. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  55597. + }
  55598. +
  55599. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  55600. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  55601. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  55602. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  55603. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  55604. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  55605. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  55606. + gr->gnptxfsiz_local);
  55607. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  55608. + gr->hptxfsiz_local);
  55609. +#ifdef CONFIG_USB_DWC_OTG_LPM
  55610. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  55611. +#endif
  55612. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  55613. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  55614. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  55615. +
  55616. + return 0;
  55617. +}
  55618. +
  55619. +/** Saves GINTMSK register before setting the msk bits. */
  55620. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  55621. +{
  55622. + struct dwc_otg_global_regs_backup *gr;
  55623. +
  55624. + gr = core_if->gr_backup;
  55625. + if (!gr) {
  55626. + gr = DWC_ALLOC(sizeof(*gr));
  55627. + if (!gr) {
  55628. + return -DWC_E_NO_MEMORY;
  55629. + }
  55630. + core_if->gr_backup = gr;
  55631. + }
  55632. +
  55633. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  55634. +
  55635. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  55636. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  55637. +
  55638. + return 0;
  55639. +}
  55640. +
  55641. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  55642. +{
  55643. + struct dwc_otg_dev_regs_backup *dr;
  55644. + int i;
  55645. +
  55646. + dr = core_if->dr_backup;
  55647. + if (!dr) {
  55648. + dr = DWC_ALLOC(sizeof(*dr));
  55649. + if (!dr) {
  55650. + return -DWC_E_NO_MEMORY;
  55651. + }
  55652. + core_if->dr_backup = dr;
  55653. + }
  55654. +
  55655. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  55656. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  55657. + dr->daintmsk =
  55658. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  55659. + dr->diepmsk =
  55660. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  55661. + dr->doepmsk =
  55662. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  55663. +
  55664. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  55665. + dr->diepctl[i] =
  55666. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  55667. + dr->dieptsiz[i] =
  55668. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  55669. + dr->diepdma[i] =
  55670. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  55671. + }
  55672. +
  55673. + DWC_DEBUGPL(DBG_ANY,
  55674. + "=============Backing Host registers==============\n");
  55675. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  55676. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  55677. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  55678. + dr->daintmsk);
  55679. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  55680. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  55681. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  55682. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  55683. + dr->diepctl[i]);
  55684. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  55685. + i, dr->dieptsiz[i]);
  55686. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  55687. + dr->diepdma[i]);
  55688. + }
  55689. +
  55690. + return 0;
  55691. +}
  55692. +
  55693. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  55694. +{
  55695. + struct dwc_otg_host_regs_backup *hr;
  55696. + int i;
  55697. +
  55698. + hr = core_if->hr_backup;
  55699. + if (!hr) {
  55700. + hr = DWC_ALLOC(sizeof(*hr));
  55701. + if (!hr) {
  55702. + return -DWC_E_NO_MEMORY;
  55703. + }
  55704. + core_if->hr_backup = hr;
  55705. + }
  55706. +
  55707. + hr->hcfg_local =
  55708. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  55709. + hr->haintmsk_local =
  55710. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  55711. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  55712. + hr->hcintmsk_local[i] =
  55713. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  55714. + }
  55715. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  55716. + hr->hfir_local =
  55717. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  55718. +
  55719. + DWC_DEBUGPL(DBG_ANY,
  55720. + "=============Backing Host registers===============\n");
  55721. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  55722. + hr->hcfg_local);
  55723. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  55724. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  55725. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  55726. + hr->hcintmsk_local[i]);
  55727. + }
  55728. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  55729. + hr->hprt0_local);
  55730. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  55731. + hr->hfir_local);
  55732. +
  55733. + return 0;
  55734. +}
  55735. +
  55736. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  55737. +{
  55738. + struct dwc_otg_global_regs_backup *gr;
  55739. + int i;
  55740. +
  55741. + gr = core_if->gr_backup;
  55742. + if (!gr) {
  55743. + return -DWC_E_INVALID;
  55744. + }
  55745. +
  55746. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  55747. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  55748. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  55749. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  55750. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  55751. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  55752. + gr->gnptxfsiz_local);
  55753. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  55754. + gr->hptxfsiz_local);
  55755. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  55756. + gr->gdfifocfg_local);
  55757. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  55758. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  55759. + gr->dtxfsiz_local[i]);
  55760. + }
  55761. +
  55762. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  55763. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  55764. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  55765. + (gr->gahbcfg_local));
  55766. + return 0;
  55767. +}
  55768. +
  55769. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  55770. +{
  55771. + struct dwc_otg_dev_regs_backup *dr;
  55772. + int i;
  55773. +
  55774. + dr = core_if->dr_backup;
  55775. +
  55776. + if (!dr) {
  55777. + return -DWC_E_INVALID;
  55778. + }
  55779. +
  55780. + if (!rem_wakeup) {
  55781. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55782. + dr->dctl);
  55783. + }
  55784. +
  55785. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  55786. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  55787. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  55788. +
  55789. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  55790. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  55791. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  55792. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  55793. + }
  55794. +
  55795. + return 0;
  55796. +}
  55797. +
  55798. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  55799. +{
  55800. + struct dwc_otg_host_regs_backup *hr;
  55801. + int i;
  55802. + hr = core_if->hr_backup;
  55803. +
  55804. + if (!hr) {
  55805. + return -DWC_E_INVALID;
  55806. + }
  55807. +
  55808. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  55809. + //if (!reset)
  55810. + //{
  55811. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  55812. + //}
  55813. +
  55814. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  55815. + hr->haintmsk_local);
  55816. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  55817. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  55818. + hr->hcintmsk_local[i]);
  55819. + }
  55820. +
  55821. + return 0;
  55822. +}
  55823. +
  55824. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  55825. +{
  55826. + struct dwc_otg_global_regs_backup *gr;
  55827. +
  55828. + gr = core_if->gr_backup;
  55829. +
  55830. + /* Restore values for LPM and I2C */
  55831. +#ifdef CONFIG_USB_DWC_OTG_LPM
  55832. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  55833. +#endif
  55834. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  55835. +
  55836. + return 0;
  55837. +}
  55838. +
  55839. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  55840. +{
  55841. + struct dwc_otg_global_regs_backup *gr;
  55842. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  55843. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  55844. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  55845. + gintmsk_data_t gintmsk = {.d32 = 0 };
  55846. +
  55847. + /* Restore LPM and I2C registers */
  55848. + restore_lpm_i2c_regs(core_if);
  55849. +
  55850. + /* Set PCGCCTL to 0 */
  55851. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  55852. +
  55853. + gr = core_if->gr_backup;
  55854. + /* Load restore values for [31:14] bits */
  55855. + DWC_WRITE_REG32(core_if->pcgcctl,
  55856. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  55857. +
  55858. + /* Umnask global Interrupt in GAHBCFG and restore it */
  55859. + gahbcfg.d32 = gr->gahbcfg_local;
  55860. + gahbcfg.b.glblintrmsk = 1;
  55861. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  55862. +
  55863. + /* Clear all pending interupts */
  55864. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  55865. +
  55866. + /* Unmask restore done interrupt */
  55867. + gintmsk.b.restoredone = 1;
  55868. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  55869. +
  55870. + /* Restore GUSBCFG and HCFG/DCFG */
  55871. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  55872. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  55873. +
  55874. + if (is_host) {
  55875. + hcfg_data_t hcfg = {.d32 = 0 };
  55876. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  55877. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  55878. + hcfg.d32);
  55879. +
  55880. + /* Load restore values for [31:14] bits */
  55881. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  55882. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  55883. +
  55884. + if (rmode)
  55885. + pcgcctl.b.restoremode = 1;
  55886. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  55887. + dwc_udelay(10);
  55888. +
  55889. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  55890. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  55891. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  55892. + pcgcctl.b.ess_reg_restored = 1;
  55893. + if (rmode)
  55894. + pcgcctl.b.restoremode = 1;
  55895. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  55896. + } else {
  55897. + dcfg_data_t dcfg = {.d32 = 0 };
  55898. + dcfg.d32 = core_if->dr_backup->dcfg;
  55899. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  55900. +
  55901. + /* Load restore values for [31:14] bits */
  55902. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  55903. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  55904. + if (!rmode) {
  55905. + pcgcctl.d32 |= 0x208;
  55906. + }
  55907. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  55908. + dwc_udelay(10);
  55909. +
  55910. + /* Load restore values for [31:14] bits */
  55911. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  55912. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  55913. + pcgcctl.b.ess_reg_restored = 1;
  55914. + if (!rmode)
  55915. + pcgcctl.d32 |= 0x208;
  55916. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  55917. + }
  55918. +
  55919. + return 0;
  55920. +}
  55921. +
  55922. +/**
  55923. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  55924. + * type.
  55925. + */
  55926. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  55927. +{
  55928. + uint32_t val;
  55929. + hcfg_data_t hcfg;
  55930. +
  55931. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  55932. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  55933. + (core_if->core_params->ulpi_fs_ls)) ||
  55934. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  55935. + /* Full speed PHY */
  55936. + val = DWC_HCFG_48_MHZ;
  55937. + } else {
  55938. + /* High speed PHY running at full speed or high speed */
  55939. + val = DWC_HCFG_30_60_MHZ;
  55940. + }
  55941. +
  55942. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  55943. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  55944. + hcfg.b.fslspclksel = val;
  55945. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  55946. +}
  55947. +
  55948. +/**
  55949. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  55950. + * and the enumeration speed of the device.
  55951. + */
  55952. +static void init_devspd(dwc_otg_core_if_t * core_if)
  55953. +{
  55954. + uint32_t val;
  55955. + dcfg_data_t dcfg;
  55956. +
  55957. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  55958. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  55959. + (core_if->core_params->ulpi_fs_ls)) ||
  55960. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  55961. + /* Full speed PHY */
  55962. + val = 0x3;
  55963. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  55964. + /* High speed PHY running at full speed */
  55965. + val = 0x1;
  55966. + } else {
  55967. + /* High speed PHY running at high speed */
  55968. + val = 0x0;
  55969. + }
  55970. +
  55971. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  55972. +
  55973. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  55974. + dcfg.b.devspd = val;
  55975. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  55976. +}
  55977. +
  55978. +/**
  55979. + * This function calculates the number of IN EPS
  55980. + * using GHWCFG1 and GHWCFG2 registers values
  55981. + *
  55982. + * @param core_if Programming view of the DWC_otg controller
  55983. + */
  55984. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  55985. +{
  55986. + uint32_t num_in_eps = 0;
  55987. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  55988. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  55989. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  55990. + int i;
  55991. +
  55992. + for (i = 0; i < num_eps; ++i) {
  55993. + if (!(hwcfg1 & 0x1))
  55994. + num_in_eps++;
  55995. +
  55996. + hwcfg1 >>= 2;
  55997. + }
  55998. +
  55999. + if (core_if->hwcfg4.b.ded_fifo_en) {
  56000. + num_in_eps =
  56001. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  56002. + }
  56003. +
  56004. + return num_in_eps;
  56005. +}
  56006. +
  56007. +/**
  56008. + * This function calculates the number of OUT EPS
  56009. + * using GHWCFG1 and GHWCFG2 registers values
  56010. + *
  56011. + * @param core_if Programming view of the DWC_otg controller
  56012. + */
  56013. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  56014. +{
  56015. + uint32_t num_out_eps = 0;
  56016. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  56017. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  56018. + int i;
  56019. +
  56020. + for (i = 0; i < num_eps; ++i) {
  56021. + if (!(hwcfg1 & 0x1))
  56022. + num_out_eps++;
  56023. +
  56024. + hwcfg1 >>= 2;
  56025. + }
  56026. + return num_out_eps;
  56027. +}
  56028. +
  56029. +/**
  56030. + * This function initializes the DWC_otg controller registers and
  56031. + * prepares the core for device mode or host mode operation.
  56032. + *
  56033. + * @param core_if Programming view of the DWC_otg controller
  56034. + *
  56035. + */
  56036. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  56037. +{
  56038. + int i = 0;
  56039. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56040. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  56041. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  56042. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  56043. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  56044. +
  56045. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  56046. + core_if, global_regs);
  56047. +
  56048. + /* Common Initialization */
  56049. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56050. +
  56051. + /* Program the ULPI External VBUS bit if needed */
  56052. + usbcfg.b.ulpi_ext_vbus_drv =
  56053. + (core_if->core_params->phy_ulpi_ext_vbus ==
  56054. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  56055. +
  56056. + /* Set external TS Dline pulsing */
  56057. + usbcfg.b.term_sel_dl_pulse =
  56058. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  56059. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56060. +
  56061. + /* Reset the Controller */
  56062. + dwc_otg_core_reset(core_if);
  56063. +
  56064. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  56065. + core_if->power_down = core_if->core_params->power_down;
  56066. + core_if->otg_sts = 0;
  56067. +
  56068. + /* Initialize parameters from Hardware configuration registers. */
  56069. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  56070. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  56071. +
  56072. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  56073. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  56074. +
  56075. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  56076. + dev_if->perio_tx_fifo_size[i] =
  56077. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  56078. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  56079. + i, dev_if->perio_tx_fifo_size[i]);
  56080. + }
  56081. +
  56082. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  56083. + dev_if->tx_fifo_size[i] =
  56084. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  56085. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  56086. + i, dev_if->tx_fifo_size[i]);
  56087. + }
  56088. +
  56089. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  56090. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  56091. + core_if->nperio_tx_fifo_size =
  56092. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  56093. +
  56094. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  56095. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  56096. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  56097. + core_if->nperio_tx_fifo_size);
  56098. +
  56099. + /* This programming sequence needs to happen in FS mode before any other
  56100. + * programming occurs */
  56101. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  56102. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  56103. + /* If FS mode with FS PHY */
  56104. +
  56105. + /* core_init() is now called on every switch so only call the
  56106. + * following for the first time through. */
  56107. + if (!core_if->phy_init_done) {
  56108. + core_if->phy_init_done = 1;
  56109. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  56110. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56111. + usbcfg.b.physel = 1;
  56112. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56113. +
  56114. + /* Reset after a PHY select */
  56115. + dwc_otg_core_reset(core_if);
  56116. + }
  56117. +
  56118. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  56119. + * do this on HNP Dev/Host mode switches (done in dev_init and
  56120. + * host_init). */
  56121. + if (dwc_otg_is_host_mode(core_if)) {
  56122. + init_fslspclksel(core_if);
  56123. + } else {
  56124. + init_devspd(core_if);
  56125. + }
  56126. +
  56127. + if (core_if->core_params->i2c_enable) {
  56128. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  56129. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  56130. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56131. + usbcfg.b.otgutmifssel = 1;
  56132. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56133. +
  56134. + /* Program GI2CCTL.I2CEn */
  56135. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  56136. + i2cctl.b.i2cdevaddr = 1;
  56137. + i2cctl.b.i2cen = 0;
  56138. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  56139. + i2cctl.b.i2cen = 1;
  56140. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  56141. + }
  56142. +
  56143. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  56144. + else {
  56145. + /* High speed PHY. */
  56146. + if (!core_if->phy_init_done) {
  56147. + core_if->phy_init_done = 1;
  56148. + /* HS PHY parameters. These parameters are preserved
  56149. + * during soft reset so only program the first time. Do
  56150. + * a soft reset immediately after setting phyif. */
  56151. +
  56152. + if (core_if->core_params->phy_type == 2) {
  56153. + /* ULPI interface */
  56154. + usbcfg.b.ulpi_utmi_sel = 1;
  56155. + usbcfg.b.phyif = 0;
  56156. + usbcfg.b.ddrsel =
  56157. + core_if->core_params->phy_ulpi_ddr;
  56158. + } else if (core_if->core_params->phy_type == 1) {
  56159. + /* UTMI+ interface */
  56160. + usbcfg.b.ulpi_utmi_sel = 0;
  56161. + if (core_if->core_params->phy_utmi_width == 16) {
  56162. + usbcfg.b.phyif = 1;
  56163. +
  56164. + } else {
  56165. + usbcfg.b.phyif = 0;
  56166. + }
  56167. + } else {
  56168. + DWC_ERROR("FS PHY TYPE\n");
  56169. + }
  56170. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56171. + /* Reset after setting the PHY parameters */
  56172. + dwc_otg_core_reset(core_if);
  56173. + }
  56174. + }
  56175. +
  56176. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  56177. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  56178. + (core_if->core_params->ulpi_fs_ls)) {
  56179. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  56180. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56181. + usbcfg.b.ulpi_fsls = 1;
  56182. + usbcfg.b.ulpi_clk_sus_m = 1;
  56183. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56184. + } else {
  56185. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56186. + usbcfg.b.ulpi_fsls = 0;
  56187. + usbcfg.b.ulpi_clk_sus_m = 0;
  56188. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56189. + }
  56190. +
  56191. + /* Program the GAHBCFG Register. */
  56192. + switch (core_if->hwcfg2.b.architecture) {
  56193. +
  56194. + case DWC_SLAVE_ONLY_ARCH:
  56195. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  56196. + ahbcfg.b.nptxfemplvl_txfemplvl =
  56197. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  56198. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  56199. + core_if->dma_enable = 0;
  56200. + core_if->dma_desc_enable = 0;
  56201. + break;
  56202. +
  56203. + case DWC_EXT_DMA_ARCH:
  56204. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  56205. + {
  56206. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  56207. + ahbcfg.b.hburstlen = 0;
  56208. + while (brst_sz > 1) {
  56209. + ahbcfg.b.hburstlen++;
  56210. + brst_sz >>= 1;
  56211. + }
  56212. + }
  56213. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  56214. + core_if->dma_desc_enable =
  56215. + (core_if->core_params->dma_desc_enable != 0);
  56216. + break;
  56217. +
  56218. + case DWC_INT_DMA_ARCH:
  56219. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  56220. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  56221. + Host mode ISOC in issue fix - vahrama */
  56222. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  56223. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  56224. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  56225. + core_if->dma_desc_enable =
  56226. + (core_if->core_params->dma_desc_enable != 0);
  56227. + break;
  56228. +
  56229. + }
  56230. + if (core_if->dma_enable) {
  56231. + if (core_if->dma_desc_enable) {
  56232. + DWC_PRINTF("Using Descriptor DMA mode\n");
  56233. + } else {
  56234. + DWC_PRINTF("Using Buffer DMA mode\n");
  56235. +
  56236. + }
  56237. + } else {
  56238. + DWC_PRINTF("Using Slave mode\n");
  56239. + core_if->dma_desc_enable = 0;
  56240. + }
  56241. +
  56242. + if (core_if->core_params->ahb_single) {
  56243. + ahbcfg.b.ahbsingle = 1;
  56244. + }
  56245. +
  56246. + ahbcfg.b.dmaenable = core_if->dma_enable;
  56247. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  56248. +
  56249. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  56250. +
  56251. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  56252. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  56253. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  56254. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  56255. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  56256. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  56257. +
  56258. + /*
  56259. + * Program the GUSBCFG register.
  56260. + */
  56261. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  56262. +
  56263. + switch (core_if->hwcfg2.b.op_mode) {
  56264. + case DWC_MODE_HNP_SRP_CAPABLE:
  56265. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  56266. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  56267. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56268. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56269. + break;
  56270. +
  56271. + case DWC_MODE_SRP_ONLY_CAPABLE:
  56272. + usbcfg.b.hnpcap = 0;
  56273. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56274. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56275. + break;
  56276. +
  56277. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  56278. + usbcfg.b.hnpcap = 0;
  56279. + usbcfg.b.srpcap = 0;
  56280. + break;
  56281. +
  56282. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  56283. + usbcfg.b.hnpcap = 0;
  56284. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56285. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56286. + break;
  56287. +
  56288. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  56289. + usbcfg.b.hnpcap = 0;
  56290. + usbcfg.b.srpcap = 0;
  56291. + break;
  56292. +
  56293. + case DWC_MODE_SRP_CAPABLE_HOST:
  56294. + usbcfg.b.hnpcap = 0;
  56295. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  56296. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  56297. + break;
  56298. +
  56299. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  56300. + usbcfg.b.hnpcap = 0;
  56301. + usbcfg.b.srpcap = 0;
  56302. + break;
  56303. + }
  56304. +
  56305. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  56306. +
  56307. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56308. + if (core_if->core_params->lpm_enable) {
  56309. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  56310. +
  56311. + /* To enable LPM support set lpm_cap_en bit */
  56312. + lpmcfg.b.lpm_cap_en = 1;
  56313. +
  56314. + /* Make AppL1Res ACK */
  56315. + lpmcfg.b.appl_resp = 1;
  56316. +
  56317. + /* Retry 3 times */
  56318. + lpmcfg.b.retry_count = 3;
  56319. +
  56320. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  56321. + 0, lpmcfg.d32);
  56322. +
  56323. + }
  56324. +#endif
  56325. + if (core_if->core_params->ic_usb_cap) {
  56326. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  56327. + gusbcfg.b.ic_usb_cap = 1;
  56328. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  56329. + 0, gusbcfg.d32);
  56330. + }
  56331. + {
  56332. + gotgctl_data_t gotgctl = {.d32 = 0 };
  56333. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  56334. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  56335. + gotgctl.d32);
  56336. + /* Set OTG version supported */
  56337. + core_if->otg_ver = core_if->core_params->otg_ver;
  56338. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  56339. + core_if->core_params->otg_ver, core_if->otg_ver);
  56340. + }
  56341. +
  56342. +
  56343. + /* Enable common interrupts */
  56344. + dwc_otg_enable_common_interrupts(core_if);
  56345. +
  56346. + /* Do device or host intialization based on mode during PCD
  56347. + * and HCD initialization */
  56348. + if (dwc_otg_is_host_mode(core_if)) {
  56349. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  56350. + core_if->op_state = A_HOST;
  56351. + } else {
  56352. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  56353. + core_if->op_state = B_PERIPHERAL;
  56354. +#ifdef DWC_DEVICE_ONLY
  56355. + dwc_otg_core_dev_init(core_if);
  56356. +#endif
  56357. + }
  56358. +}
  56359. +
  56360. +/**
  56361. + * This function enables the Device mode interrupts.
  56362. + *
  56363. + * @param core_if Programming view of DWC_otg controller
  56364. + */
  56365. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  56366. +{
  56367. + gintmsk_data_t intr_mask = {.d32 = 0 };
  56368. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56369. +
  56370. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  56371. +
  56372. + /* Disable all interrupts. */
  56373. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  56374. +
  56375. + /* Clear any pending interrupts */
  56376. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  56377. +
  56378. + /* Enable the common interrupts */
  56379. + dwc_otg_enable_common_interrupts(core_if);
  56380. +
  56381. + /* Enable interrupts */
  56382. + intr_mask.b.usbreset = 1;
  56383. + intr_mask.b.enumdone = 1;
  56384. + /* Disable Disconnect interrupt in Device mode */
  56385. + intr_mask.b.disconnect = 0;
  56386. +
  56387. + if (!core_if->multiproc_int_enable) {
  56388. + intr_mask.b.inepintr = 1;
  56389. + intr_mask.b.outepintr = 1;
  56390. + }
  56391. +
  56392. + intr_mask.b.erlysuspend = 1;
  56393. +
  56394. + if (core_if->en_multiple_tx_fifo == 0) {
  56395. + intr_mask.b.epmismatch = 1;
  56396. + }
  56397. +
  56398. + //intr_mask.b.incomplisoout = 1;
  56399. + intr_mask.b.incomplisoin = 1;
  56400. +
  56401. +/* Enable the ignore frame number for ISOC xfers - MAS */
  56402. +/* Disable to support high bandwith ISOC transfers - manukz */
  56403. +#if 0
  56404. +#ifdef DWC_UTE_PER_IO
  56405. + if (core_if->dma_enable) {
  56406. + if (core_if->dma_desc_enable) {
  56407. + dctl_data_t dctl1 = {.d32 = 0 };
  56408. + dctl1.b.ifrmnum = 1;
  56409. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  56410. + dctl, 0, dctl1.d32);
  56411. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  56412. + DWC_READ_REG32(&core_if->dev_if->
  56413. + dev_global_regs->dctl));
  56414. + }
  56415. + }
  56416. +#endif
  56417. +#endif
  56418. +#ifdef DWC_EN_ISOC
  56419. + if (core_if->dma_enable) {
  56420. + if (core_if->dma_desc_enable == 0) {
  56421. + if (core_if->pti_enh_enable) {
  56422. + dctl_data_t dctl = {.d32 = 0 };
  56423. + dctl.b.ifrmnum = 1;
  56424. + DWC_MODIFY_REG32(&core_if->
  56425. + dev_if->dev_global_regs->dctl,
  56426. + 0, dctl.d32);
  56427. + } else {
  56428. + intr_mask.b.incomplisoin = 1;
  56429. + intr_mask.b.incomplisoout = 1;
  56430. + }
  56431. + }
  56432. + } else {
  56433. + intr_mask.b.incomplisoin = 1;
  56434. + intr_mask.b.incomplisoout = 1;
  56435. + }
  56436. +#endif /* DWC_EN_ISOC */
  56437. +
  56438. + /** @todo NGS: Should this be a module parameter? */
  56439. +#ifdef USE_PERIODIC_EP
  56440. + intr_mask.b.isooutdrop = 1;
  56441. + intr_mask.b.eopframe = 1;
  56442. + intr_mask.b.incomplisoin = 1;
  56443. + intr_mask.b.incomplisoout = 1;
  56444. +#endif
  56445. +
  56446. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  56447. +
  56448. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  56449. + DWC_READ_REG32(&global_regs->gintmsk));
  56450. +}
  56451. +
  56452. +/**
  56453. + * This function initializes the DWC_otg controller registers for
  56454. + * device mode.
  56455. + *
  56456. + * @param core_if Programming view of DWC_otg controller
  56457. + *
  56458. + */
  56459. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  56460. +{
  56461. + int i;
  56462. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56463. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  56464. + dwc_otg_core_params_t *params = core_if->core_params;
  56465. + dcfg_data_t dcfg = {.d32 = 0 };
  56466. + depctl_data_t diepctl = {.d32 = 0 };
  56467. + grstctl_t resetctl = {.d32 = 0 };
  56468. + uint32_t rx_fifo_size;
  56469. + fifosize_data_t nptxfifosize;
  56470. + fifosize_data_t txfifosize;
  56471. + dthrctl_data_t dthrctl;
  56472. + fifosize_data_t ptxfifosize;
  56473. + uint16_t rxfsiz, nptxfsiz;
  56474. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  56475. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  56476. +
  56477. + /* Restart the Phy Clock */
  56478. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  56479. +
  56480. + /* Device configuration register */
  56481. + init_devspd(core_if);
  56482. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  56483. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  56484. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  56485. + /* Enable Device OUT NAK in case of DDMA mode*/
  56486. + if (core_if->core_params->dev_out_nak) {
  56487. + dcfg.b.endevoutnak = 1;
  56488. + }
  56489. +
  56490. + if (core_if->core_params->cont_on_bna) {
  56491. + dctl_data_t dctl = {.d32 = 0 };
  56492. + dctl.b.encontonbna = 1;
  56493. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  56494. + }
  56495. +
  56496. +
  56497. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  56498. +
  56499. + /* Configure data FIFO sizes */
  56500. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  56501. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  56502. + core_if->total_fifo_size);
  56503. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  56504. + params->dev_rx_fifo_size);
  56505. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  56506. + params->dev_nperio_tx_fifo_size);
  56507. +
  56508. + /* Rx FIFO */
  56509. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  56510. + DWC_READ_REG32(&global_regs->grxfsiz));
  56511. +
  56512. +#ifdef DWC_UTE_CFI
  56513. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  56514. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  56515. +#endif
  56516. + rx_fifo_size = params->dev_rx_fifo_size;
  56517. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  56518. +
  56519. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  56520. + DWC_READ_REG32(&global_regs->grxfsiz));
  56521. +
  56522. + /** Set Periodic Tx FIFO Mask all bits 0 */
  56523. + core_if->p_tx_msk = 0;
  56524. +
  56525. + /** Set Tx FIFO Mask all bits 0 */
  56526. + core_if->tx_msk = 0;
  56527. +
  56528. + if (core_if->en_multiple_tx_fifo == 0) {
  56529. + /* Non-periodic Tx FIFO */
  56530. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  56531. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  56532. +
  56533. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  56534. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  56535. +
  56536. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  56537. + nptxfifosize.d32);
  56538. +
  56539. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  56540. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  56541. +
  56542. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  56543. + /*
  56544. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  56545. + * Indexes of the FIFO size module parameters in the
  56546. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  56547. + * the dptxfsiz array run from 0 to 14.
  56548. + */
  56549. + /** @todo Finish debug of this */
  56550. + ptxfifosize.b.startaddr =
  56551. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  56552. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  56553. + ptxfifosize.b.depth =
  56554. + params->dev_perio_tx_fifo_size[i];
  56555. + DWC_DEBUGPL(DBG_CIL,
  56556. + "initial dtxfsiz[%d]=%08x\n", i,
  56557. + DWC_READ_REG32(&global_regs->dtxfsiz
  56558. + [i]));
  56559. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  56560. + ptxfifosize.d32);
  56561. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  56562. + i,
  56563. + DWC_READ_REG32(&global_regs->dtxfsiz
  56564. + [i]));
  56565. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  56566. + }
  56567. + } else {
  56568. + /*
  56569. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  56570. + * Indexes of the FIFO size module parameters in the
  56571. + * dev_tx_fifo_size array and the FIFO size registers in
  56572. + * the dtxfsiz array run from 0 to 14.
  56573. + */
  56574. +
  56575. + /* Non-periodic Tx FIFO */
  56576. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  56577. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  56578. +
  56579. +#ifdef DWC_UTE_CFI
  56580. + core_if->pwron_gnptxfsiz =
  56581. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  56582. + core_if->init_gnptxfsiz =
  56583. + params->dev_nperio_tx_fifo_size;
  56584. +#endif
  56585. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  56586. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  56587. +
  56588. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  56589. + nptxfifosize.d32);
  56590. +
  56591. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  56592. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  56593. +
  56594. + txfifosize.b.startaddr =
  56595. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  56596. +
  56597. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  56598. +
  56599. + txfifosize.b.depth =
  56600. + params->dev_tx_fifo_size[i];
  56601. +
  56602. + DWC_DEBUGPL(DBG_CIL,
  56603. + "initial dtxfsiz[%d]=%08x\n",
  56604. + i,
  56605. + DWC_READ_REG32(&global_regs->dtxfsiz
  56606. + [i]));
  56607. +
  56608. +#ifdef DWC_UTE_CFI
  56609. + core_if->pwron_txfsiz[i] =
  56610. + (DWC_READ_REG32
  56611. + (&global_regs->dtxfsiz[i]) >> 16);
  56612. + core_if->init_txfsiz[i] =
  56613. + params->dev_tx_fifo_size[i];
  56614. +#endif
  56615. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  56616. + txfifosize.d32);
  56617. +
  56618. + DWC_DEBUGPL(DBG_CIL,
  56619. + "new dtxfsiz[%d]=%08x\n",
  56620. + i,
  56621. + DWC_READ_REG32(&global_regs->dtxfsiz
  56622. + [i]));
  56623. +
  56624. + txfifosize.b.startaddr += txfifosize.b.depth;
  56625. + }
  56626. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  56627. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  56628. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  56629. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  56630. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  56631. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  56632. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  56633. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  56634. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  56635. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  56636. + }
  56637. + }
  56638. +
  56639. + /* Flush the FIFOs */
  56640. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  56641. + dwc_otg_flush_rx_fifo(core_if);
  56642. +
  56643. + /* Flush the Learning Queue. */
  56644. + resetctl.b.intknqflsh = 1;
  56645. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  56646. +
  56647. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  56648. + core_if->start_predict = 0;
  56649. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  56650. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  56651. + }
  56652. + core_if->nextep_seq[0] = 0;
  56653. + core_if->first_in_nextep_seq = 0;
  56654. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  56655. + diepctl.b.nextep = 0;
  56656. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  56657. +
  56658. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  56659. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  56660. + dcfg.b.epmscnt = 2;
  56661. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  56662. +
  56663. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  56664. + __func__, core_if->first_in_nextep_seq);
  56665. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  56666. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  56667. + }
  56668. + DWC_DEBUGPL(DBG_CILV,"\n");
  56669. + }
  56670. +
  56671. + /* Clear all pending Device Interrupts */
  56672. + /** @todo - if the condition needed to be checked
  56673. + * or in any case all pending interrutps should be cleared?
  56674. + */
  56675. + if (core_if->multiproc_int_enable) {
  56676. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  56677. + DWC_WRITE_REG32(&dev_if->
  56678. + dev_global_regs->diepeachintmsk[i], 0);
  56679. + }
  56680. + }
  56681. +
  56682. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  56683. + DWC_WRITE_REG32(&dev_if->
  56684. + dev_global_regs->doepeachintmsk[i], 0);
  56685. + }
  56686. +
  56687. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  56688. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  56689. + } else {
  56690. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  56691. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  56692. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  56693. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  56694. + }
  56695. +
  56696. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  56697. + depctl_data_t depctl;
  56698. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  56699. + if (depctl.b.epena) {
  56700. + depctl.d32 = 0;
  56701. + depctl.b.epdis = 1;
  56702. + depctl.b.snak = 1;
  56703. + } else {
  56704. + depctl.d32 = 0;
  56705. + }
  56706. +
  56707. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  56708. +
  56709. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  56710. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  56711. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  56712. + }
  56713. +
  56714. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  56715. + depctl_data_t depctl;
  56716. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  56717. + if (depctl.b.epena) {
  56718. + dctl_data_t dctl = {.d32 = 0 };
  56719. + gintmsk_data_t gintsts = {.d32 = 0 };
  56720. + doepint_data_t doepint = {.d32 = 0 };
  56721. + dctl.b.sgoutnak = 1;
  56722. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  56723. + do {
  56724. + dwc_udelay(10);
  56725. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  56726. + } while (!gintsts.b.goutnakeff);
  56727. + gintsts.d32 = 0;
  56728. + gintsts.b.goutnakeff = 1;
  56729. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  56730. +
  56731. + depctl.d32 = 0;
  56732. + depctl.b.epdis = 1;
  56733. + depctl.b.snak = 1;
  56734. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  56735. + do {
  56736. + dwc_udelay(10);
  56737. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  56738. + out_ep_regs[i]->doepint);
  56739. + } while (!doepint.b.epdisabled);
  56740. +
  56741. + doepint.b.epdisabled = 1;
  56742. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  56743. +
  56744. + dctl.d32 = 0;
  56745. + dctl.b.cgoutnak = 1;
  56746. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  56747. + } else {
  56748. + depctl.d32 = 0;
  56749. + }
  56750. +
  56751. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  56752. +
  56753. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  56754. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  56755. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  56756. + }
  56757. +
  56758. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  56759. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  56760. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  56761. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  56762. +
  56763. + dev_if->rx_thr_length = params->rx_thr_length;
  56764. + dev_if->tx_thr_length = params->tx_thr_length;
  56765. +
  56766. + dev_if->setup_desc_index = 0;
  56767. +
  56768. + dthrctl.d32 = 0;
  56769. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  56770. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  56771. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  56772. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  56773. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  56774. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  56775. +
  56776. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  56777. + dthrctl.d32);
  56778. +
  56779. + DWC_DEBUGPL(DBG_CIL,
  56780. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  56781. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  56782. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  56783. + dthrctl.b.rx_thr_len);
  56784. +
  56785. + }
  56786. +
  56787. + dwc_otg_enable_device_interrupts(core_if);
  56788. +
  56789. + {
  56790. + diepmsk_data_t msk = {.d32 = 0 };
  56791. + msk.b.txfifoundrn = 1;
  56792. + if (core_if->multiproc_int_enable) {
  56793. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  56794. + diepeachintmsk[0], msk.d32, msk.d32);
  56795. + } else {
  56796. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  56797. + msk.d32, msk.d32);
  56798. + }
  56799. + }
  56800. +
  56801. + if (core_if->multiproc_int_enable) {
  56802. + /* Set NAK on Babble */
  56803. + dctl_data_t dctl = {.d32 = 0 };
  56804. + dctl.b.nakonbble = 1;
  56805. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  56806. + }
  56807. +
  56808. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  56809. + dctl_data_t dctl = {.d32 = 0 };
  56810. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  56811. + dctl.b.sftdiscon = 0;
  56812. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  56813. + }
  56814. +}
  56815. +
  56816. +/**
  56817. + * This function enables the Host mode interrupts.
  56818. + *
  56819. + * @param core_if Programming view of DWC_otg controller
  56820. + */
  56821. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  56822. +{
  56823. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56824. + gintmsk_data_t intr_mask = {.d32 = 0 };
  56825. +
  56826. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  56827. +
  56828. + /* Disable all interrupts. */
  56829. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  56830. +
  56831. + /* Clear any pending interrupts. */
  56832. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  56833. +
  56834. + /* Enable the common interrupts */
  56835. + dwc_otg_enable_common_interrupts(core_if);
  56836. +
  56837. + /*
  56838. + * Enable host mode interrupts without disturbing common
  56839. + * interrupts.
  56840. + */
  56841. +
  56842. + intr_mask.b.disconnect = 1;
  56843. + intr_mask.b.portintr = 1;
  56844. + intr_mask.b.hcintr = 1;
  56845. +
  56846. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  56847. +}
  56848. +
  56849. +/**
  56850. + * This function disables the Host Mode interrupts.
  56851. + *
  56852. + * @param core_if Programming view of DWC_otg controller
  56853. + */
  56854. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  56855. +{
  56856. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56857. + gintmsk_data_t intr_mask = {.d32 = 0 };
  56858. +
  56859. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  56860. +
  56861. + /*
  56862. + * Disable host mode interrupts without disturbing common
  56863. + * interrupts.
  56864. + */
  56865. + intr_mask.b.sofintr = 1;
  56866. + intr_mask.b.portintr = 1;
  56867. + intr_mask.b.hcintr = 1;
  56868. + intr_mask.b.ptxfempty = 1;
  56869. + intr_mask.b.nptxfempty = 1;
  56870. +
  56871. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  56872. +}
  56873. +
  56874. +/**
  56875. + * This function initializes the DWC_otg controller registers for
  56876. + * host mode.
  56877. + *
  56878. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  56879. + * request queues. Host channels are reset to ensure that they are ready for
  56880. + * performing transfers.
  56881. + *
  56882. + * @param core_if Programming view of DWC_otg controller
  56883. + *
  56884. + */
  56885. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  56886. +{
  56887. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56888. + dwc_otg_host_if_t *host_if = core_if->host_if;
  56889. + dwc_otg_core_params_t *params = core_if->core_params;
  56890. + hprt0_data_t hprt0 = {.d32 = 0 };
  56891. + fifosize_data_t nptxfifosize;
  56892. + fifosize_data_t ptxfifosize;
  56893. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  56894. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  56895. + int i;
  56896. + hcchar_data_t hcchar;
  56897. + hcfg_data_t hcfg;
  56898. + hfir_data_t hfir;
  56899. + dwc_otg_hc_regs_t *hc_regs;
  56900. + int num_channels;
  56901. + gotgctl_data_t gotgctl = {.d32 = 0 };
  56902. +
  56903. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  56904. +
  56905. + /* Restart the Phy Clock */
  56906. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  56907. +
  56908. + /* Initialize Host Configuration Register */
  56909. + init_fslspclksel(core_if);
  56910. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  56911. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  56912. + hcfg.b.fslssupp = 1;
  56913. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  56914. +
  56915. + }
  56916. +
  56917. + /* This bit allows dynamic reloading of the HFIR register
  56918. + * during runtime. This bit needs to be programmed during
  56919. + * initial configuration and its value must not be changed
  56920. + * during runtime.*/
  56921. + if (core_if->core_params->reload_ctl == 1) {
  56922. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  56923. + hfir.b.hfirrldctrl = 1;
  56924. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  56925. + }
  56926. +
  56927. + if (core_if->core_params->dma_desc_enable) {
  56928. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  56929. + if (!
  56930. + (core_if->hwcfg4.b.desc_dma
  56931. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  56932. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  56933. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  56934. + || (op_mode ==
  56935. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  56936. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  56937. + || (op_mode ==
  56938. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  56939. +
  56940. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  56941. + "Either core version is below 2.90a or "
  56942. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  56943. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  56944. + "module parameter to 0.\n");
  56945. + return;
  56946. + }
  56947. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  56948. + hcfg.b.descdma = 1;
  56949. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  56950. + }
  56951. +
  56952. + /* Configure data FIFO sizes */
  56953. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  56954. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  56955. + core_if->total_fifo_size);
  56956. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  56957. + params->host_rx_fifo_size);
  56958. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  56959. + params->host_nperio_tx_fifo_size);
  56960. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  56961. + params->host_perio_tx_fifo_size);
  56962. +
  56963. + /* Rx FIFO */
  56964. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  56965. + DWC_READ_REG32(&global_regs->grxfsiz));
  56966. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  56967. + params->host_rx_fifo_size);
  56968. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  56969. + DWC_READ_REG32(&global_regs->grxfsiz));
  56970. +
  56971. + /* Non-periodic Tx FIFO */
  56972. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  56973. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  56974. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  56975. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  56976. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  56977. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  56978. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  56979. +
  56980. + /* Periodic Tx FIFO */
  56981. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  56982. + DWC_READ_REG32(&global_regs->hptxfsiz));
  56983. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  56984. + ptxfifosize.b.startaddr =
  56985. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  56986. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  56987. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  56988. + DWC_READ_REG32(&global_regs->hptxfsiz));
  56989. +
  56990. + if (core_if->en_multiple_tx_fifo
  56991. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  56992. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  56993. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  56994. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  56995. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  56996. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  56997. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  56998. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  56999. + }
  57000. + }
  57001. +
  57002. + /* TODO - check this */
  57003. + /* Clear Host Set HNP Enable in the OTG Control Register */
  57004. + gotgctl.b.hstsethnpen = 1;
  57005. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  57006. + /* Make sure the FIFOs are flushed. */
  57007. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  57008. + dwc_otg_flush_rx_fifo(core_if);
  57009. +
  57010. + /* Clear Host Set HNP Enable in the OTG Control Register */
  57011. + gotgctl.b.hstsethnpen = 1;
  57012. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  57013. +
  57014. + if (!core_if->core_params->dma_desc_enable) {
  57015. + /* Flush out any leftover queued requests. */
  57016. + num_channels = core_if->core_params->host_channels;
  57017. +
  57018. + for (i = 0; i < num_channels; i++) {
  57019. + hc_regs = core_if->host_if->hc_regs[i];
  57020. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57021. + hcchar.b.chen = 0;
  57022. + hcchar.b.chdis = 1;
  57023. + hcchar.b.epdir = 0;
  57024. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57025. + }
  57026. +
  57027. + /* Halt all channels to put them into a known state. */
  57028. + for (i = 0; i < num_channels; i++) {
  57029. + int count = 0;
  57030. + hc_regs = core_if->host_if->hc_regs[i];
  57031. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57032. + hcchar.b.chen = 1;
  57033. + hcchar.b.chdis = 1;
  57034. + hcchar.b.epdir = 0;
  57035. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57036. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  57037. + do {
  57038. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57039. + if (++count > 1000) {
  57040. + DWC_ERROR
  57041. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  57042. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  57043. + break;
  57044. + }
  57045. + dwc_udelay(1);
  57046. + } while (hcchar.b.chen);
  57047. + }
  57048. + }
  57049. +
  57050. + /* Turn on the vbus power. */
  57051. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  57052. + if (core_if->op_state == A_HOST) {
  57053. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  57054. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  57055. + if (hprt0.b.prtpwr == 0) {
  57056. + hprt0.b.prtpwr = 1;
  57057. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  57058. + }
  57059. + }
  57060. +
  57061. + dwc_otg_enable_host_interrupts(core_if);
  57062. +}
  57063. +
  57064. +/**
  57065. + * Prepares a host channel for transferring packets to/from a specific
  57066. + * endpoint. The HCCHARn register is set up with the characteristics specified
  57067. + * in _hc. Host channel interrupts that may need to be serviced while this
  57068. + * transfer is in progress are enabled.
  57069. + *
  57070. + * @param core_if Programming view of DWC_otg controller
  57071. + * @param hc Information needed to initialize the host channel
  57072. + */
  57073. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57074. +{
  57075. + uint32_t intr_enable;
  57076. + hcintmsk_data_t hc_intr_mask;
  57077. + gintmsk_data_t gintmsk = {.d32 = 0 };
  57078. + hcchar_data_t hcchar;
  57079. + hcsplt_data_t hcsplt;
  57080. +
  57081. + uint8_t hc_num = hc->hc_num;
  57082. + dwc_otg_host_if_t *host_if = core_if->host_if;
  57083. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  57084. +
  57085. + /* Clear old interrupt conditions for this host channel. */
  57086. + hc_intr_mask.d32 = 0xFFFFFFFF;
  57087. + hc_intr_mask.b.reserved14_31 = 0;
  57088. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  57089. +
  57090. + /* Enable channel interrupts required for this transfer. */
  57091. + hc_intr_mask.d32 = 0;
  57092. + hc_intr_mask.b.chhltd = 1;
  57093. + if (core_if->dma_enable) {
  57094. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  57095. + if (!core_if->dma_desc_enable)
  57096. + hc_intr_mask.b.ahberr = 1;
  57097. + else {
  57098. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  57099. + hc_intr_mask.b.xfercompl = 1;
  57100. + }
  57101. +
  57102. + if (hc->error_state && !hc->do_split &&
  57103. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  57104. + hc_intr_mask.b.ack = 1;
  57105. + if (hc->ep_is_in) {
  57106. + hc_intr_mask.b.datatglerr = 1;
  57107. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  57108. + hc_intr_mask.b.nak = 1;
  57109. + }
  57110. + }
  57111. + }
  57112. + } else {
  57113. + switch (hc->ep_type) {
  57114. + case DWC_OTG_EP_TYPE_CONTROL:
  57115. + case DWC_OTG_EP_TYPE_BULK:
  57116. + hc_intr_mask.b.xfercompl = 1;
  57117. + hc_intr_mask.b.stall = 1;
  57118. + hc_intr_mask.b.xacterr = 1;
  57119. + hc_intr_mask.b.datatglerr = 1;
  57120. + if (hc->ep_is_in) {
  57121. + hc_intr_mask.b.bblerr = 1;
  57122. + } else {
  57123. + hc_intr_mask.b.nak = 1;
  57124. + hc_intr_mask.b.nyet = 1;
  57125. + if (hc->do_ping) {
  57126. + hc_intr_mask.b.ack = 1;
  57127. + }
  57128. + }
  57129. +
  57130. + if (hc->do_split) {
  57131. + hc_intr_mask.b.nak = 1;
  57132. + if (hc->complete_split) {
  57133. + hc_intr_mask.b.nyet = 1;
  57134. + } else {
  57135. + hc_intr_mask.b.ack = 1;
  57136. + }
  57137. + }
  57138. +
  57139. + if (hc->error_state) {
  57140. + hc_intr_mask.b.ack = 1;
  57141. + }
  57142. + break;
  57143. + case DWC_OTG_EP_TYPE_INTR:
  57144. + hc_intr_mask.b.xfercompl = 1;
  57145. + hc_intr_mask.b.nak = 1;
  57146. + hc_intr_mask.b.stall = 1;
  57147. + hc_intr_mask.b.xacterr = 1;
  57148. + hc_intr_mask.b.datatglerr = 1;
  57149. + hc_intr_mask.b.frmovrun = 1;
  57150. +
  57151. + if (hc->ep_is_in) {
  57152. + hc_intr_mask.b.bblerr = 1;
  57153. + }
  57154. + if (hc->error_state) {
  57155. + hc_intr_mask.b.ack = 1;
  57156. + }
  57157. + if (hc->do_split) {
  57158. + if (hc->complete_split) {
  57159. + hc_intr_mask.b.nyet = 1;
  57160. + } else {
  57161. + hc_intr_mask.b.ack = 1;
  57162. + }
  57163. + }
  57164. + break;
  57165. + case DWC_OTG_EP_TYPE_ISOC:
  57166. + hc_intr_mask.b.xfercompl = 1;
  57167. + hc_intr_mask.b.frmovrun = 1;
  57168. + hc_intr_mask.b.ack = 1;
  57169. +
  57170. + if (hc->ep_is_in) {
  57171. + hc_intr_mask.b.xacterr = 1;
  57172. + hc_intr_mask.b.bblerr = 1;
  57173. + }
  57174. + break;
  57175. + }
  57176. + }
  57177. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  57178. +
  57179. + /* Enable the top level host channel interrupt. */
  57180. + intr_enable = (1 << hc_num);
  57181. + DWC_MODIFY_REG32(&host_if->host_global_regs->haintmsk, 0, intr_enable);
  57182. +
  57183. + /* Make sure host channel interrupts are enabled. */
  57184. + gintmsk.b.hcintr = 1;
  57185. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  57186. +
  57187. + /*
  57188. + * Program the HCCHARn register with the endpoint characteristics for
  57189. + * the current transfer.
  57190. + */
  57191. + hcchar.d32 = 0;
  57192. + hcchar.b.devaddr = hc->dev_addr;
  57193. + hcchar.b.epnum = hc->ep_num;
  57194. + hcchar.b.epdir = hc->ep_is_in;
  57195. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  57196. + hcchar.b.eptype = hc->ep_type;
  57197. + hcchar.b.mps = hc->max_packet;
  57198. +
  57199. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  57200. +
  57201. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  57202. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  57203. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  57204. + "Max Pkt %d, Multi Cnt %d\n",
  57205. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  57206. + hcchar.b.mps, hcchar.b.multicnt);
  57207. +
  57208. + /*
  57209. + * Program the HCSPLIT register for SPLITs
  57210. + */
  57211. + hcsplt.d32 = 0;
  57212. + if (hc->do_split) {
  57213. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  57214. + hc->hc_num,
  57215. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  57216. + hcsplt.b.compsplt = hc->complete_split;
  57217. + hcsplt.b.xactpos = hc->xact_pos;
  57218. + hcsplt.b.hubaddr = hc->hub_addr;
  57219. + hcsplt.b.prtaddr = hc->port_addr;
  57220. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  57221. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  57222. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  57223. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  57224. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  57225. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  57226. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  57227. + }
  57228. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  57229. +
  57230. +}
  57231. +
  57232. +/**
  57233. + * Attempts to halt a host channel. This function should only be called in
  57234. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  57235. + * normal circumstances in DMA mode, the controller halts the channel when the
  57236. + * transfer is complete or a condition occurs that requires application
  57237. + * intervention.
  57238. + *
  57239. + * In slave mode, checks for a free request queue entry, then sets the Channel
  57240. + * Enable and Channel Disable bits of the Host Channel Characteristics
  57241. + * register of the specified channel to intiate the halt. If there is no free
  57242. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  57243. + * register to flush requests for this channel. In the latter case, sets a
  57244. + * flag to indicate that the host channel needs to be halted when a request
  57245. + * queue slot is open.
  57246. + *
  57247. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  57248. + * HCCHARn register. The controller ensures there is space in the request
  57249. + * queue before submitting the halt request.
  57250. + *
  57251. + * Some time may elapse before the core flushes any posted requests for this
  57252. + * host channel and halts. The Channel Halted interrupt handler completes the
  57253. + * deactivation of the host channel.
  57254. + *
  57255. + * @param core_if Controller register interface.
  57256. + * @param hc Host channel to halt.
  57257. + * @param halt_status Reason for halting the channel.
  57258. + */
  57259. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  57260. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  57261. +{
  57262. + gnptxsts_data_t nptxsts;
  57263. + hptxsts_data_t hptxsts;
  57264. + hcchar_data_t hcchar;
  57265. + dwc_otg_hc_regs_t *hc_regs;
  57266. + dwc_otg_core_global_regs_t *global_regs;
  57267. + dwc_otg_host_global_regs_t *host_global_regs;
  57268. +
  57269. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  57270. + global_regs = core_if->core_global_regs;
  57271. + host_global_regs = core_if->host_if->host_global_regs;
  57272. +
  57273. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  57274. + "halt_status = %d\n", halt_status);
  57275. +
  57276. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  57277. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  57278. + /*
  57279. + * Disable all channel interrupts except Ch Halted. The QTD
  57280. + * and QH state associated with this transfer has been cleared
  57281. + * (in the case of URB_DEQUEUE), so the channel needs to be
  57282. + * shut down carefully to prevent crashes.
  57283. + */
  57284. + hcintmsk_data_t hcintmsk;
  57285. + hcintmsk.d32 = 0;
  57286. + hcintmsk.b.chhltd = 1;
  57287. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  57288. +
  57289. + /*
  57290. + * Make sure no other interrupts besides halt are currently
  57291. + * pending. Handling another interrupt could cause a crash due
  57292. + * to the QTD and QH state.
  57293. + */
  57294. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  57295. +
  57296. + /*
  57297. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  57298. + * even if the channel was already halted for some other
  57299. + * reason.
  57300. + */
  57301. + hc->halt_status = halt_status;
  57302. +
  57303. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57304. + if (hcchar.b.chen == 0) {
  57305. + /*
  57306. + * The channel is either already halted or it hasn't
  57307. + * started yet. In DMA mode, the transfer may halt if
  57308. + * it finishes normally or a condition occurs that
  57309. + * requires driver intervention. Don't want to halt
  57310. + * the channel again. In either Slave or DMA mode,
  57311. + * it's possible that the transfer has been assigned
  57312. + * to a channel, but not started yet when an URB is
  57313. + * dequeued. Don't want to halt a channel that hasn't
  57314. + * started yet.
  57315. + */
  57316. + return;
  57317. + }
  57318. + }
  57319. + if (hc->halt_pending) {
  57320. + /*
  57321. + * A halt has already been issued for this channel. This might
  57322. + * happen when a transfer is aborted by a higher level in
  57323. + * the stack.
  57324. + */
  57325. +#ifdef DEBUG
  57326. + DWC_PRINTF
  57327. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  57328. + __func__, hc->hc_num);
  57329. +
  57330. +#endif
  57331. + return;
  57332. + }
  57333. +
  57334. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57335. +
  57336. + /* No need to set the bit in DDMA for disabling the channel */
  57337. + //TODO check it everywhere channel is disabled
  57338. + if (!core_if->core_params->dma_desc_enable)
  57339. + hcchar.b.chen = 1;
  57340. + hcchar.b.chdis = 1;
  57341. +
  57342. + if (!core_if->dma_enable) {
  57343. + /* Check for space in the request queue to issue the halt. */
  57344. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  57345. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  57346. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  57347. + if (nptxsts.b.nptxqspcavail == 0) {
  57348. + hcchar.b.chen = 0;
  57349. + }
  57350. + } else {
  57351. + hptxsts.d32 =
  57352. + DWC_READ_REG32(&host_global_regs->hptxsts);
  57353. + if ((hptxsts.b.ptxqspcavail == 0)
  57354. + || (core_if->queuing_high_bandwidth)) {
  57355. + hcchar.b.chen = 0;
  57356. + }
  57357. + }
  57358. + }
  57359. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57360. +
  57361. + hc->halt_status = halt_status;
  57362. +
  57363. + if (hcchar.b.chen) {
  57364. + hc->halt_pending = 1;
  57365. + hc->halt_on_queue = 0;
  57366. + } else {
  57367. + hc->halt_on_queue = 1;
  57368. + }
  57369. +
  57370. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  57371. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  57372. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  57373. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  57374. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  57375. +
  57376. + return;
  57377. +}
  57378. +
  57379. +/**
  57380. + * Clears the transfer state for a host channel. This function is normally
  57381. + * called after a transfer is done and the host channel is being released.
  57382. + *
  57383. + * @param core_if Programming view of DWC_otg controller.
  57384. + * @param hc Identifies the host channel to clean up.
  57385. + */
  57386. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57387. +{
  57388. + dwc_otg_hc_regs_t *hc_regs;
  57389. +
  57390. + hc->xfer_started = 0;
  57391. +
  57392. + /*
  57393. + * Clear channel interrupt enables and any unhandled channel interrupt
  57394. + * conditions.
  57395. + */
  57396. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  57397. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  57398. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  57399. +#ifdef DEBUG
  57400. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  57401. +#endif
  57402. +}
  57403. +
  57404. +/**
  57405. + * Sets the channel property that indicates in which frame a periodic transfer
  57406. + * should occur. This is always set to the _next_ frame. This function has no
  57407. + * effect on non-periodic transfers.
  57408. + *
  57409. + * @param core_if Programming view of DWC_otg controller.
  57410. + * @param hc Identifies the host channel to set up and its properties.
  57411. + * @param hcchar Current value of the HCCHAR register for the specified host
  57412. + * channel.
  57413. + */
  57414. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  57415. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  57416. +{
  57417. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  57418. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  57419. + hfnum_data_t hfnum;
  57420. + hfnum.d32 =
  57421. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  57422. +
  57423. + /* 1 if _next_ frame is odd, 0 if it's even */
  57424. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  57425. +#ifdef DEBUG
  57426. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  57427. + && !hc->complete_split) {
  57428. + switch (hfnum.b.frnum & 0x7) {
  57429. + case 7:
  57430. + core_if->hfnum_7_samples++;
  57431. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  57432. + break;
  57433. + case 0:
  57434. + core_if->hfnum_0_samples++;
  57435. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  57436. + break;
  57437. + default:
  57438. + core_if->hfnum_other_samples++;
  57439. + core_if->hfnum_other_frrem_accum +=
  57440. + hfnum.b.frrem;
  57441. + break;
  57442. + }
  57443. + }
  57444. +#endif
  57445. + }
  57446. +}
  57447. +
  57448. +#ifdef DEBUG
  57449. +void hc_xfer_timeout(void *ptr)
  57450. +{
  57451. + hc_xfer_info_t *xfer_info = NULL;
  57452. + int hc_num = 0;
  57453. +
  57454. + if (ptr)
  57455. + xfer_info = (hc_xfer_info_t *) ptr;
  57456. +
  57457. + if (!xfer_info->hc) {
  57458. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  57459. + return;
  57460. + }
  57461. +
  57462. + hc_num = xfer_info->hc->hc_num;
  57463. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  57464. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  57465. + xfer_info->core_if->start_hcchar_val[hc_num]);
  57466. +}
  57467. +#endif
  57468. +
  57469. +void ep_xfer_timeout(void *ptr)
  57470. +{
  57471. + ep_xfer_info_t *xfer_info = NULL;
  57472. + int ep_num = 0;
  57473. + dctl_data_t dctl = {.d32 = 0 };
  57474. + gintsts_data_t gintsts = {.d32 = 0 };
  57475. + gintmsk_data_t gintmsk = {.d32 = 0 };
  57476. +
  57477. + if (ptr)
  57478. + xfer_info = (ep_xfer_info_t *) ptr;
  57479. +
  57480. + if (!xfer_info->ep) {
  57481. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  57482. + return;
  57483. + }
  57484. +
  57485. + ep_num = xfer_info->ep->num;
  57486. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  57487. + /* Put the sate to 2 as it was time outed */
  57488. + xfer_info->state = 2;
  57489. +
  57490. + dctl.d32 =
  57491. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  57492. + gintsts.d32 =
  57493. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  57494. + gintmsk.d32 =
  57495. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  57496. +
  57497. + if (!gintmsk.b.goutnakeff) {
  57498. + /* Unmask it */
  57499. + gintmsk.b.goutnakeff = 1;
  57500. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  57501. + gintmsk.d32);
  57502. +
  57503. + }
  57504. +
  57505. + if (!gintsts.b.goutnakeff) {
  57506. + dctl.b.sgoutnak = 1;
  57507. + }
  57508. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  57509. + dctl.d32);
  57510. +
  57511. +}
  57512. +
  57513. +void set_pid_isoc(dwc_hc_t * hc)
  57514. +{
  57515. + /* Set up the initial PID for the transfer. */
  57516. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  57517. + if (hc->ep_is_in) {
  57518. + if (hc->multi_count == 1) {
  57519. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  57520. + } else if (hc->multi_count == 2) {
  57521. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  57522. + } else {
  57523. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  57524. + }
  57525. + } else {
  57526. + if (hc->multi_count == 1) {
  57527. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  57528. + } else {
  57529. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  57530. + }
  57531. + }
  57532. + } else {
  57533. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  57534. + }
  57535. +}
  57536. +
  57537. +/**
  57538. + * This function does the setup for a data transfer for a host channel and
  57539. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  57540. + * Slave mode, the caller must ensure that there is sufficient space in the
  57541. + * request queue and Tx Data FIFO.
  57542. + *
  57543. + * For an OUT transfer in Slave mode, it loads a data packet into the
  57544. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  57545. + * the Host ISR.
  57546. + *
  57547. + * For an IN transfer in Slave mode, a data packet is requested. The data
  57548. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  57549. + * additional data packets are requested in the Host ISR.
  57550. + *
  57551. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  57552. + * register along with a packet count of 1 and the channel is enabled. This
  57553. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  57554. + * simply set to 0 since no data transfer occurs in this case.
  57555. + *
  57556. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  57557. + * all the information required to perform the subsequent data transfer. In
  57558. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  57559. + * controller performs the entire PING protocol, then starts the data
  57560. + * transfer.
  57561. + *
  57562. + * @param core_if Programming view of DWC_otg controller.
  57563. + * @param hc Information needed to initialize the host channel. The xfer_len
  57564. + * value may be reduced to accommodate the max widths of the XferSize and
  57565. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  57566. + * to reflect the final xfer_len value.
  57567. + */
  57568. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57569. +{
  57570. + hcchar_data_t hcchar;
  57571. + hctsiz_data_t hctsiz;
  57572. + uint16_t num_packets;
  57573. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  57574. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  57575. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  57576. +
  57577. + hctsiz.d32 = 0;
  57578. +
  57579. + if (hc->do_ping) {
  57580. + if (!core_if->dma_enable) {
  57581. + dwc_otg_hc_do_ping(core_if, hc);
  57582. + hc->xfer_started = 1;
  57583. + return;
  57584. + } else {
  57585. + hctsiz.b.dopng = 1;
  57586. + }
  57587. + }
  57588. +
  57589. + if (hc->do_split) {
  57590. + num_packets = 1;
  57591. +
  57592. + if (hc->complete_split && !hc->ep_is_in) {
  57593. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  57594. + * core doesn't expect any data written to the FIFO */
  57595. + hc->xfer_len = 0;
  57596. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  57597. + hc->xfer_len = hc->max_packet;
  57598. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  57599. + hc->xfer_len = 188;
  57600. + }
  57601. +
  57602. + hctsiz.b.xfersize = hc->xfer_len;
  57603. + } else {
  57604. + /*
  57605. + * Ensure that the transfer length and packet count will fit
  57606. + * in the widths allocated for them in the HCTSIZn register.
  57607. + */
  57608. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  57609. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  57610. + /*
  57611. + * Make sure the transfer size is no larger than one
  57612. + * (micro)frame's worth of data. (A check was done
  57613. + * when the periodic transfer was accepted to ensure
  57614. + * that a (micro)frame's worth of data can be
  57615. + * programmed into a channel.)
  57616. + */
  57617. + uint32_t max_periodic_len =
  57618. + hc->multi_count * hc->max_packet;
  57619. + if (hc->xfer_len > max_periodic_len) {
  57620. + hc->xfer_len = max_periodic_len;
  57621. + } else {
  57622. + }
  57623. + } else if (hc->xfer_len > max_hc_xfer_size) {
  57624. + /* Make sure that xfer_len is a multiple of max packet size. */
  57625. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  57626. + }
  57627. +
  57628. + if (hc->xfer_len > 0) {
  57629. + num_packets =
  57630. + (hc->xfer_len + hc->max_packet -
  57631. + 1) / hc->max_packet;
  57632. + if (num_packets > max_hc_pkt_count) {
  57633. + num_packets = max_hc_pkt_count;
  57634. + hc->xfer_len = num_packets * hc->max_packet;
  57635. + }
  57636. + } else {
  57637. + /* Need 1 packet for transfer length of 0. */
  57638. + num_packets = 1;
  57639. + }
  57640. +
  57641. + if (hc->ep_is_in) {
  57642. + /* Always program an integral # of max packets for IN transfers. */
  57643. + hc->xfer_len = num_packets * hc->max_packet;
  57644. + }
  57645. +
  57646. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  57647. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  57648. + /*
  57649. + * Make sure that the multi_count field matches the
  57650. + * actual transfer length.
  57651. + */
  57652. + hc->multi_count = num_packets;
  57653. + }
  57654. +
  57655. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  57656. + set_pid_isoc(hc);
  57657. +
  57658. + hctsiz.b.xfersize = hc->xfer_len;
  57659. + }
  57660. +
  57661. + hc->start_pkt_count = num_packets;
  57662. + hctsiz.b.pktcnt = num_packets;
  57663. + hctsiz.b.pid = hc->data_pid_start;
  57664. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  57665. +
  57666. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  57667. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  57668. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  57669. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  57670. +
  57671. + if (core_if->dma_enable) {
  57672. + dwc_dma_t dma_addr;
  57673. + if (hc->align_buff) {
  57674. + dma_addr = hc->align_buff;
  57675. + } else {
  57676. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  57677. + }
  57678. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  57679. + }
  57680. +
  57681. + /* Start the split */
  57682. + if (hc->do_split) {
  57683. + hcsplt_data_t hcsplt;
  57684. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  57685. + hcsplt.b.spltena = 1;
  57686. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  57687. + }
  57688. +
  57689. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57690. + hcchar.b.multicnt = hc->multi_count;
  57691. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  57692. +#ifdef DEBUG
  57693. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  57694. + if (hcchar.b.chdis) {
  57695. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  57696. + __func__, hc->hc_num, hcchar.d32);
  57697. + }
  57698. +#endif
  57699. +
  57700. + /* Set host channel enable after all other setup is complete. */
  57701. + hcchar.b.chen = 1;
  57702. + hcchar.b.chdis = 0;
  57703. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57704. +
  57705. + hc->xfer_started = 1;
  57706. + hc->requests++;
  57707. +
  57708. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  57709. + /* Load OUT packet into the appropriate Tx FIFO. */
  57710. + dwc_otg_hc_write_packet(core_if, hc);
  57711. + }
  57712. +#ifdef DEBUG
  57713. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  57714. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  57715. + hc->hc_num, core_if);//GRAYG
  57716. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  57717. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  57718. +
  57719. + /* Start a timer for this transfer. */
  57720. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  57721. + }
  57722. +#endif
  57723. +}
  57724. +
  57725. +/**
  57726. + * This function does the setup for a data transfer for a host channel
  57727. + * and starts the transfer in Descriptor DMA mode.
  57728. + *
  57729. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  57730. + * Sets PID and NTD values. For periodic transfers
  57731. + * initializes SCHED_INFO field with micro-frame bitmap.
  57732. + *
  57733. + * Initializes HCDMA register with descriptor list address and CTD value
  57734. + * then starts the transfer via enabling the channel.
  57735. + *
  57736. + * @param core_if Programming view of DWC_otg controller.
  57737. + * @param hc Information needed to initialize the host channel.
  57738. + */
  57739. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57740. +{
  57741. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  57742. + hcchar_data_t hcchar;
  57743. + hctsiz_data_t hctsiz;
  57744. + hcdma_data_t hcdma;
  57745. +
  57746. + hctsiz.d32 = 0;
  57747. +
  57748. + if (hc->do_ping)
  57749. + hctsiz.b_ddma.dopng = 1;
  57750. +
  57751. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  57752. + set_pid_isoc(hc);
  57753. +
  57754. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  57755. + hctsiz.b_ddma.pid = hc->data_pid_start;
  57756. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  57757. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  57758. +
  57759. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  57760. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  57761. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  57762. +
  57763. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  57764. +
  57765. + hcdma.d32 = 0;
  57766. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  57767. +
  57768. + /* Always start from first descriptor. */
  57769. + hcdma.b.ctd = 0;
  57770. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  57771. +
  57772. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57773. + hcchar.b.multicnt = hc->multi_count;
  57774. +
  57775. +#ifdef DEBUG
  57776. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  57777. + if (hcchar.b.chdis) {
  57778. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  57779. + __func__, hc->hc_num, hcchar.d32);
  57780. + }
  57781. +#endif
  57782. +
  57783. + /* Set host channel enable after all other setup is complete. */
  57784. + hcchar.b.chen = 1;
  57785. + hcchar.b.chdis = 0;
  57786. +
  57787. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57788. +
  57789. + hc->xfer_started = 1;
  57790. + hc->requests++;
  57791. +
  57792. +#ifdef DEBUG
  57793. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  57794. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  57795. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  57796. + hc->hc_num, core_if);//GRAYG
  57797. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  57798. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  57799. + /* Start a timer for this transfer. */
  57800. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  57801. + }
  57802. +#endif
  57803. +
  57804. +}
  57805. +
  57806. +/**
  57807. + * This function continues a data transfer that was started by previous call
  57808. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  57809. + * sufficient space in the request queue and Tx Data FIFO. This function
  57810. + * should only be called in Slave mode. In DMA mode, the controller acts
  57811. + * autonomously to complete transfers programmed to a host channel.
  57812. + *
  57813. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  57814. + * if there is any data remaining to be queued. For an IN transfer, another
  57815. + * data packet is always requested. For the SETUP phase of a control transfer,
  57816. + * this function does nothing.
  57817. + *
  57818. + * @return 1 if a new request is queued, 0 if no more requests are required
  57819. + * for this transfer.
  57820. + */
  57821. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57822. +{
  57823. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  57824. +
  57825. + if (hc->do_split) {
  57826. + /* SPLITs always queue just once per channel */
  57827. + return 0;
  57828. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  57829. + /* SETUPs are queued only once since they can't be NAKed. */
  57830. + return 0;
  57831. + } else if (hc->ep_is_in) {
  57832. + /*
  57833. + * Always queue another request for other IN transfers. If
  57834. + * back-to-back INs are issued and NAKs are received for both,
  57835. + * the driver may still be processing the first NAK when the
  57836. + * second NAK is received. When the interrupt handler clears
  57837. + * the NAK interrupt for the first NAK, the second NAK will
  57838. + * not be seen. So we can't depend on the NAK interrupt
  57839. + * handler to requeue a NAKed request. Instead, IN requests
  57840. + * are issued each time this function is called. When the
  57841. + * transfer completes, the extra requests for the channel will
  57842. + * be flushed.
  57843. + */
  57844. + hcchar_data_t hcchar;
  57845. + dwc_otg_hc_regs_t *hc_regs =
  57846. + core_if->host_if->hc_regs[hc->hc_num];
  57847. +
  57848. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57849. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  57850. + hcchar.b.chen = 1;
  57851. + hcchar.b.chdis = 0;
  57852. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  57853. + hcchar.d32);
  57854. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57855. + hc->requests++;
  57856. + return 1;
  57857. + } else {
  57858. + /* OUT transfers. */
  57859. + if (hc->xfer_count < hc->xfer_len) {
  57860. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  57861. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  57862. + hcchar_data_t hcchar;
  57863. + dwc_otg_hc_regs_t *hc_regs;
  57864. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  57865. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57866. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  57867. + }
  57868. +
  57869. + /* Load OUT packet into the appropriate Tx FIFO. */
  57870. + dwc_otg_hc_write_packet(core_if, hc);
  57871. + hc->requests++;
  57872. + return 1;
  57873. + } else {
  57874. + return 0;
  57875. + }
  57876. + }
  57877. +}
  57878. +
  57879. +/**
  57880. + * Starts a PING transfer. This function should only be called in Slave mode.
  57881. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  57882. + */
  57883. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57884. +{
  57885. + hcchar_data_t hcchar;
  57886. + hctsiz_data_t hctsiz;
  57887. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  57888. +
  57889. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  57890. +
  57891. + hctsiz.d32 = 0;
  57892. + hctsiz.b.dopng = 1;
  57893. + hctsiz.b.pktcnt = 1;
  57894. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  57895. +
  57896. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  57897. + hcchar.b.chen = 1;
  57898. + hcchar.b.chdis = 0;
  57899. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  57900. +}
  57901. +
  57902. +/*
  57903. + * This function writes a packet into the Tx FIFO associated with the Host
  57904. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  57905. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  57906. + * periodic Tx FIFO is written. This function should only be called in Slave
  57907. + * mode.
  57908. + *
  57909. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  57910. + * then number of bytes written to the Tx FIFO.
  57911. + */
  57912. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  57913. +{
  57914. + uint32_t i;
  57915. + uint32_t remaining_count;
  57916. + uint32_t byte_count;
  57917. + uint32_t dword_count;
  57918. +
  57919. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  57920. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  57921. +
  57922. + remaining_count = hc->xfer_len - hc->xfer_count;
  57923. + if (remaining_count > hc->max_packet) {
  57924. + byte_count = hc->max_packet;
  57925. + } else {
  57926. + byte_count = remaining_count;
  57927. + }
  57928. +
  57929. + dword_count = (byte_count + 3) / 4;
  57930. +
  57931. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  57932. + /* xfer_buff is DWORD aligned. */
  57933. + for (i = 0; i < dword_count; i++, data_buff++) {
  57934. + DWC_WRITE_REG32(data_fifo, *data_buff);
  57935. + }
  57936. + } else {
  57937. + /* xfer_buff is not DWORD aligned. */
  57938. + for (i = 0; i < dword_count; i++, data_buff++) {
  57939. + uint32_t data;
  57940. + data =
  57941. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  57942. + 16 | data_buff[3] << 24);
  57943. + DWC_WRITE_REG32(data_fifo, data);
  57944. + }
  57945. + }
  57946. +
  57947. + hc->xfer_count += byte_count;
  57948. + hc->xfer_buff += byte_count;
  57949. +}
  57950. +
  57951. +/**
  57952. + * Gets the current USB frame number. This is the frame number from the last
  57953. + * SOF packet.
  57954. + */
  57955. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  57956. +{
  57957. + dsts_data_t dsts;
  57958. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  57959. +
  57960. + /* read current frame/microframe number from DSTS register */
  57961. + return dsts.b.soffn;
  57962. +}
  57963. +
  57964. +/**
  57965. + * Calculates and gets the frame Interval value of HFIR register according PHY
  57966. + * type and speed.The application can modify a value of HFIR register only after
  57967. + * the Port Enable bit of the Host Port Control and Status register
  57968. + * (HPRT.PrtEnaPort) has been set.
  57969. +*/
  57970. +
  57971. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  57972. +{
  57973. + gusbcfg_data_t usbcfg;
  57974. + hwcfg2_data_t hwcfg2;
  57975. + hprt0_data_t hprt0;
  57976. + int clock = 60; // default value
  57977. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  57978. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  57979. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  57980. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  57981. + clock = 60;
  57982. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  57983. + clock = 48;
  57984. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  57985. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  57986. + clock = 30;
  57987. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  57988. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  57989. + clock = 60;
  57990. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  57991. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  57992. + clock = 48;
  57993. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  57994. + clock = 48;
  57995. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  57996. + clock = 48;
  57997. + if (hprt0.b.prtspd == 0)
  57998. + /* High speed case */
  57999. + return 125 * clock;
  58000. + else
  58001. + /* FS/LS case */
  58002. + return 1000 * clock;
  58003. +}
  58004. +
  58005. +/**
  58006. + * This function reads a setup packet from the Rx FIFO into the destination
  58007. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  58008. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  58009. + *
  58010. + * @param core_if Programming view of DWC_otg controller.
  58011. + * @param dest Destination buffer for packet data.
  58012. + */
  58013. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  58014. +{
  58015. + device_grxsts_data_t status;
  58016. + /* Get the 8 bytes of a setup transaction data */
  58017. +
  58018. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  58019. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  58020. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  58021. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  58022. + status.d32 =
  58023. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  58024. + DWC_DEBUGPL(DBG_ANY,
  58025. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  58026. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  58027. + status.b.fn, status.b.fn);
  58028. + }
  58029. +}
  58030. +
  58031. +/**
  58032. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  58033. + * IN for transmitting packets. It is normally called when the
  58034. + * "Enumeration Done" interrupt occurs.
  58035. + *
  58036. + * @param core_if Programming view of DWC_otg controller.
  58037. + * @param ep The EP0 data.
  58038. + */
  58039. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58040. +{
  58041. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  58042. + dsts_data_t dsts;
  58043. + depctl_data_t diepctl;
  58044. + depctl_data_t doepctl;
  58045. + dctl_data_t dctl = {.d32 = 0 };
  58046. +
  58047. + ep->stp_rollover = 0;
  58048. + /* Read the Device Status and Endpoint 0 Control registers */
  58049. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  58050. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  58051. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  58052. +
  58053. + /* Set the MPS of the IN EP based on the enumeration speed */
  58054. + switch (dsts.b.enumspd) {
  58055. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  58056. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  58057. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  58058. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  58059. + break;
  58060. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  58061. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  58062. + break;
  58063. + }
  58064. +
  58065. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  58066. +
  58067. + /* Enable OUT EP for receive */
  58068. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  58069. + doepctl.b.epena = 1;
  58070. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  58071. + }
  58072. +#ifdef VERBOSE
  58073. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  58074. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  58075. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  58076. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  58077. +#endif
  58078. + dctl.b.cgnpinnak = 1;
  58079. +
  58080. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  58081. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  58082. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  58083. +
  58084. +}
  58085. +
  58086. +/**
  58087. + * This function activates an EP. The Device EP control register for
  58088. + * the EP is configured as defined in the ep structure. Note: This
  58089. + * function is not used for EP0.
  58090. + *
  58091. + * @param core_if Programming view of DWC_otg controller.
  58092. + * @param ep The EP to activate.
  58093. + */
  58094. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58095. +{
  58096. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  58097. + depctl_data_t depctl;
  58098. + volatile uint32_t *addr;
  58099. + daint_data_t daintmsk = {.d32 = 0 };
  58100. + dcfg_data_t dcfg;
  58101. + uint8_t i;
  58102. +
  58103. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  58104. + (ep->is_in ? "IN" : "OUT"));
  58105. +
  58106. +#ifdef DWC_UTE_PER_IO
  58107. + ep->xiso_frame_num = 0xFFFFFFFF;
  58108. + ep->xiso_active_xfers = 0;
  58109. + ep->xiso_queued_xfers = 0;
  58110. +#endif
  58111. + /* Read DEPCTLn register */
  58112. + if (ep->is_in == 1) {
  58113. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  58114. + daintmsk.ep.in = 1 << ep->num;
  58115. + } else {
  58116. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  58117. + daintmsk.ep.out = 1 << ep->num;
  58118. + }
  58119. +
  58120. + /* If the EP is already active don't change the EP Control
  58121. + * register. */
  58122. + depctl.d32 = DWC_READ_REG32(addr);
  58123. + if (!depctl.b.usbactep) {
  58124. + depctl.b.mps = ep->maxpacket;
  58125. + depctl.b.eptype = ep->type;
  58126. + depctl.b.txfnum = ep->tx_fifo_num;
  58127. +
  58128. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58129. + depctl.b.setd0pid = 1; // ???
  58130. + } else {
  58131. + depctl.b.setd0pid = 1;
  58132. + }
  58133. + depctl.b.usbactep = 1;
  58134. +
  58135. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  58136. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  58137. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  58138. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  58139. + break;
  58140. + }
  58141. + core_if->nextep_seq[i] = ep->num;
  58142. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  58143. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  58144. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  58145. + dcfg.b.epmscnt++;
  58146. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  58147. +
  58148. + DWC_DEBUGPL(DBG_PCDV,
  58149. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  58150. + __func__, core_if->first_in_nextep_seq);
  58151. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  58152. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  58153. + core_if->nextep_seq[i]);
  58154. + }
  58155. +
  58156. + }
  58157. +
  58158. +
  58159. + DWC_WRITE_REG32(addr, depctl.d32);
  58160. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  58161. + }
  58162. +
  58163. + /* Enable the Interrupt for this EP */
  58164. + if (core_if->multiproc_int_enable) {
  58165. + if (ep->is_in == 1) {
  58166. + diepmsk_data_t diepmsk = {.d32 = 0 };
  58167. + diepmsk.b.xfercompl = 1;
  58168. + diepmsk.b.timeout = 1;
  58169. + diepmsk.b.epdisabled = 1;
  58170. + diepmsk.b.ahberr = 1;
  58171. + diepmsk.b.intknepmis = 1;
  58172. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  58173. + diepmsk.b.intknepmis = 0;
  58174. + diepmsk.b.txfifoundrn = 1; //?????
  58175. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58176. + diepmsk.b.nak = 1;
  58177. + }
  58178. +
  58179. +
  58180. +
  58181. +/*
  58182. + if (core_if->dma_desc_enable) {
  58183. + diepmsk.b.bna = 1;
  58184. + }
  58185. +*/
  58186. +/*
  58187. + if (core_if->dma_enable) {
  58188. + doepmsk.b.nak = 1;
  58189. + }
  58190. +*/
  58191. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  58192. + diepeachintmsk[ep->num], diepmsk.d32);
  58193. +
  58194. + } else {
  58195. + doepmsk_data_t doepmsk = {.d32 = 0 };
  58196. + doepmsk.b.xfercompl = 1;
  58197. + doepmsk.b.ahberr = 1;
  58198. + doepmsk.b.epdisabled = 1;
  58199. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  58200. + doepmsk.b.outtknepdis = 1;
  58201. +
  58202. +/*
  58203. +
  58204. + if (core_if->dma_desc_enable) {
  58205. + doepmsk.b.bna = 1;
  58206. + }
  58207. +*/
  58208. +/*
  58209. + doepmsk.b.babble = 1;
  58210. + doepmsk.b.nyet = 1;
  58211. + doepmsk.b.nak = 1;
  58212. +*/
  58213. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  58214. + doepeachintmsk[ep->num], doepmsk.d32);
  58215. + }
  58216. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  58217. + 0, daintmsk.d32);
  58218. + } else {
  58219. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58220. + if (ep->is_in) {
  58221. + diepmsk_data_t diepmsk = {.d32 = 0 };
  58222. + diepmsk.b.nak = 1;
  58223. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  58224. + } else {
  58225. + doepmsk_data_t doepmsk = {.d32 = 0 };
  58226. + doepmsk.b.outtknepdis = 1;
  58227. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  58228. + }
  58229. + }
  58230. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  58231. + 0, daintmsk.d32);
  58232. + }
  58233. +
  58234. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  58235. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  58236. +
  58237. + ep->stall_clear_flag = 0;
  58238. +
  58239. + return;
  58240. +}
  58241. +
  58242. +/**
  58243. + * This function deactivates an EP. This is done by clearing the USB Active
  58244. + * EP bit in the Device EP control register. Note: This function is not used
  58245. + * for EP0. EP0 cannot be deactivated.
  58246. + *
  58247. + * @param core_if Programming view of DWC_otg controller.
  58248. + * @param ep The EP to deactivate.
  58249. + */
  58250. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58251. +{
  58252. + depctl_data_t depctl = {.d32 = 0 };
  58253. + volatile uint32_t *addr;
  58254. + daint_data_t daintmsk = {.d32 = 0 };
  58255. + dcfg_data_t dcfg;
  58256. + uint8_t i = 0;
  58257. +
  58258. +#ifdef DWC_UTE_PER_IO
  58259. + ep->xiso_frame_num = 0xFFFFFFFF;
  58260. + ep->xiso_active_xfers = 0;
  58261. + ep->xiso_queued_xfers = 0;
  58262. +#endif
  58263. +
  58264. + /* Read DEPCTLn register */
  58265. + if (ep->is_in == 1) {
  58266. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  58267. + daintmsk.ep.in = 1 << ep->num;
  58268. + } else {
  58269. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  58270. + daintmsk.ep.out = 1 << ep->num;
  58271. + }
  58272. +
  58273. + depctl.d32 = DWC_READ_REG32(addr);
  58274. +
  58275. + depctl.b.usbactep = 0;
  58276. +
  58277. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  58278. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  58279. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  58280. + if (core_if->nextep_seq[i] == ep->num)
  58281. + break;
  58282. + }
  58283. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  58284. + if (core_if->first_in_nextep_seq == ep->num)
  58285. + core_if->first_in_nextep_seq = i;
  58286. + core_if->nextep_seq[ep->num] = 0xff;
  58287. + depctl.b.nextep = 0;
  58288. + dcfg.d32 =
  58289. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  58290. + dcfg.b.epmscnt--;
  58291. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  58292. + dcfg.d32);
  58293. +
  58294. + DWC_DEBUGPL(DBG_PCDV,
  58295. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  58296. + __func__, core_if->first_in_nextep_seq);
  58297. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  58298. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  58299. + }
  58300. + }
  58301. +
  58302. + if (ep->is_in == 1)
  58303. + depctl.b.txfnum = 0;
  58304. +
  58305. + if (core_if->dma_desc_enable)
  58306. + depctl.b.epdis = 1;
  58307. +
  58308. + DWC_WRITE_REG32(addr, depctl.d32);
  58309. + depctl.d32 = DWC_READ_REG32(addr);
  58310. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  58311. + && depctl.b.epena) {
  58312. + depctl_data_t depctl = {.d32 = 0};
  58313. + if (ep->is_in) {
  58314. + diepint_data_t diepint = {.d32 = 0};
  58315. +
  58316. + depctl.b.snak = 1;
  58317. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58318. + diepctl, depctl.d32);
  58319. + do {
  58320. + dwc_udelay(10);
  58321. + diepint.d32 =
  58322. + DWC_READ_REG32(&core_if->
  58323. + dev_if->in_ep_regs[ep->num]->
  58324. + diepint);
  58325. + } while (!diepint.b.inepnakeff);
  58326. + diepint.b.inepnakeff = 1;
  58327. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58328. + diepint, diepint.d32);
  58329. + depctl.d32 = 0;
  58330. + depctl.b.epdis = 1;
  58331. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58332. + diepctl, depctl.d32);
  58333. + do {
  58334. + dwc_udelay(10);
  58335. + diepint.d32 =
  58336. + DWC_READ_REG32(&core_if->
  58337. + dev_if->in_ep_regs[ep->num]->
  58338. + diepint);
  58339. + } while (!diepint.b.epdisabled);
  58340. + diepint.b.epdisabled = 1;
  58341. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  58342. + diepint, diepint.d32);
  58343. + } else {
  58344. + dctl_data_t dctl = {.d32 = 0};
  58345. + gintmsk_data_t gintsts = {.d32 = 0};
  58346. + doepint_data_t doepint = {.d32 = 0};
  58347. + dctl.b.sgoutnak = 1;
  58348. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  58349. + dctl, 0, dctl.d32);
  58350. + do {
  58351. + dwc_udelay(10);
  58352. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  58353. + } while (!gintsts.b.goutnakeff);
  58354. + gintsts.d32 = 0;
  58355. + gintsts.b.goutnakeff = 1;
  58356. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  58357. +
  58358. + depctl.d32 = 0;
  58359. + depctl.b.epdis = 1;
  58360. + depctl.b.snak = 1;
  58361. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  58362. + do
  58363. + {
  58364. + dwc_udelay(10);
  58365. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  58366. + out_ep_regs[ep->num]->doepint);
  58367. + } while (!doepint.b.epdisabled);
  58368. +
  58369. + doepint.b.epdisabled = 1;
  58370. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  58371. +
  58372. + dctl.d32 = 0;
  58373. + dctl.b.cgoutnak = 1;
  58374. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  58375. + }
  58376. + }
  58377. +
  58378. + /* Disable the Interrupt for this EP */
  58379. + if (core_if->multiproc_int_enable) {
  58380. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  58381. + daintmsk.d32, 0);
  58382. +
  58383. + if (ep->is_in == 1) {
  58384. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  58385. + diepeachintmsk[ep->num], 0);
  58386. + } else {
  58387. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  58388. + doepeachintmsk[ep->num], 0);
  58389. + }
  58390. + } else {
  58391. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  58392. + daintmsk.d32, 0);
  58393. + }
  58394. +
  58395. +}
  58396. +
  58397. +/**
  58398. + * This function initializes dma descriptor chain.
  58399. + *
  58400. + * @param core_if Programming view of DWC_otg controller.
  58401. + * @param ep The EP to start the transfer on.
  58402. + */
  58403. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58404. +{
  58405. + dwc_otg_dev_dma_desc_t *dma_desc;
  58406. + uint32_t offset;
  58407. + uint32_t xfer_est;
  58408. + int i;
  58409. + unsigned maxxfer_local, total_len;
  58410. +
  58411. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  58412. + (ep->maxpacket%4)) {
  58413. + maxxfer_local = ep->maxpacket;
  58414. + total_len = ep->xfer_len;
  58415. + } else {
  58416. + maxxfer_local = ep->maxxfer;
  58417. + total_len = ep->total_len;
  58418. + }
  58419. +
  58420. + ep->desc_cnt = (total_len / maxxfer_local) +
  58421. + ((total_len % maxxfer_local) ? 1 : 0);
  58422. +
  58423. + if (!ep->desc_cnt)
  58424. + ep->desc_cnt = 1;
  58425. +
  58426. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  58427. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  58428. +
  58429. + dma_desc = ep->desc_addr;
  58430. + if (maxxfer_local == ep->maxpacket) {
  58431. + if ((total_len % maxxfer_local) &&
  58432. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  58433. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  58434. + (total_len % maxxfer_local);
  58435. + } else
  58436. + xfer_est = ep->desc_cnt * maxxfer_local;
  58437. + } else
  58438. + xfer_est = total_len;
  58439. + offset = 0;
  58440. + for (i = 0; i < ep->desc_cnt; ++i) {
  58441. + /** DMA Descriptor Setup */
  58442. + if (xfer_est > maxxfer_local) {
  58443. + dma_desc->status.b.bs = BS_HOST_BUSY;
  58444. + dma_desc->status.b.l = 0;
  58445. + dma_desc->status.b.ioc = 0;
  58446. + dma_desc->status.b.sp = 0;
  58447. + dma_desc->status.b.bytes = maxxfer_local;
  58448. + dma_desc->buf = ep->dma_addr + offset;
  58449. + dma_desc->status.b.sts = 0;
  58450. + dma_desc->status.b.bs = BS_HOST_READY;
  58451. +
  58452. + xfer_est -= maxxfer_local;
  58453. + offset += maxxfer_local;
  58454. + } else {
  58455. + dma_desc->status.b.bs = BS_HOST_BUSY;
  58456. + dma_desc->status.b.l = 1;
  58457. + dma_desc->status.b.ioc = 1;
  58458. + if (ep->is_in) {
  58459. + dma_desc->status.b.sp =
  58460. + (xfer_est %
  58461. + ep->maxpacket) ? 1 : ((ep->
  58462. + sent_zlp) ? 1 : 0);
  58463. + dma_desc->status.b.bytes = xfer_est;
  58464. + } else {
  58465. + if (maxxfer_local == ep->maxpacket)
  58466. + dma_desc->status.b.bytes = xfer_est;
  58467. + else
  58468. + dma_desc->status.b.bytes =
  58469. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  58470. + }
  58471. +
  58472. + dma_desc->buf = ep->dma_addr + offset;
  58473. + dma_desc->status.b.sts = 0;
  58474. + dma_desc->status.b.bs = BS_HOST_READY;
  58475. + }
  58476. + dma_desc++;
  58477. + }
  58478. +}
  58479. +/**
  58480. + * This function is called when to write ISOC data into appropriate dedicated
  58481. + * periodic FIFO.
  58482. + */
  58483. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  58484. +{
  58485. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  58486. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  58487. + dtxfsts_data_t txstatus = {.d32 = 0 };
  58488. + uint32_t len = 0;
  58489. + int epnum = dwc_ep->num;
  58490. + int dwords;
  58491. +
  58492. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  58493. +
  58494. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  58495. +
  58496. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  58497. +
  58498. + if (len > dwc_ep->maxpacket) {
  58499. + len = dwc_ep->maxpacket;
  58500. + }
  58501. +
  58502. + dwords = (len + 3) / 4;
  58503. +
  58504. + /* While there is space in the queue and space in the FIFO and
  58505. + * More data to tranfer, Write packets to the Tx FIFO */
  58506. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  58507. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  58508. +
  58509. + while (txstatus.b.txfspcavail > dwords &&
  58510. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  58511. + /* Write the FIFO */
  58512. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  58513. +
  58514. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  58515. + if (len > dwc_ep->maxpacket) {
  58516. + len = dwc_ep->maxpacket;
  58517. + }
  58518. +
  58519. + dwords = (len + 3) / 4;
  58520. + txstatus.d32 =
  58521. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  58522. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  58523. + txstatus.d32);
  58524. + }
  58525. +
  58526. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  58527. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  58528. +
  58529. + return 1;
  58530. +}
  58531. +/**
  58532. + * This function does the setup for a data transfer for an EP and
  58533. + * starts the transfer. For an IN transfer, the packets will be
  58534. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  58535. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  58536. + *
  58537. + * @param core_if Programming view of DWC_otg controller.
  58538. + * @param ep The EP to start the transfer on.
  58539. + */
  58540. +
  58541. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58542. +{
  58543. + depctl_data_t depctl;
  58544. + deptsiz_data_t deptsiz;
  58545. + gintmsk_data_t intr_mask = {.d32 = 0 };
  58546. +
  58547. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  58548. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  58549. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  58550. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  58551. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  58552. + ep->total_len);
  58553. + /* IN endpoint */
  58554. + if (ep->is_in == 1) {
  58555. + dwc_otg_dev_in_ep_regs_t *in_regs =
  58556. + core_if->dev_if->in_ep_regs[ep->num];
  58557. +
  58558. + gnptxsts_data_t gtxstatus;
  58559. +
  58560. + gtxstatus.d32 =
  58561. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  58562. +
  58563. + if (core_if->en_multiple_tx_fifo == 0
  58564. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  58565. +#ifdef DEBUG
  58566. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  58567. +#endif
  58568. + return;
  58569. + }
  58570. +
  58571. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  58572. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  58573. +
  58574. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  58575. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  58576. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  58577. + else
  58578. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  58579. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  58580. +
  58581. +
  58582. + /* Zero Length Packet? */
  58583. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  58584. + deptsiz.b.xfersize = 0;
  58585. + deptsiz.b.pktcnt = 1;
  58586. + } else {
  58587. + /* Program the transfer size and packet count
  58588. + * as follows: xfersize = N * maxpacket +
  58589. + * short_packet pktcnt = N + (short_packet
  58590. + * exist ? 1 : 0)
  58591. + */
  58592. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  58593. + deptsiz.b.pktcnt =
  58594. + (ep->xfer_len - ep->xfer_count - 1 +
  58595. + ep->maxpacket) / ep->maxpacket;
  58596. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  58597. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  58598. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  58599. + }
  58600. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  58601. + deptsiz.b.mc = deptsiz.b.pktcnt;
  58602. + }
  58603. +
  58604. + /* Write the DMA register */
  58605. + if (core_if->dma_enable) {
  58606. + if (core_if->dma_desc_enable == 0) {
  58607. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  58608. + deptsiz.b.mc = 1;
  58609. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  58610. + deptsiz.d32);
  58611. + DWC_WRITE_REG32(&(in_regs->diepdma),
  58612. + (uint32_t) ep->dma_addr);
  58613. + } else {
  58614. +#ifdef DWC_UTE_CFI
  58615. + /* The descriptor chain should be already initialized by now */
  58616. + if (ep->buff_mode != BM_STANDARD) {
  58617. + DWC_WRITE_REG32(&in_regs->diepdma,
  58618. + ep->descs_dma_addr);
  58619. + } else {
  58620. +#endif
  58621. + init_dma_desc_chain(core_if, ep);
  58622. + /** DIEPDMAn Register write */
  58623. + DWC_WRITE_REG32(&in_regs->diepdma,
  58624. + ep->dma_desc_addr);
  58625. +#ifdef DWC_UTE_CFI
  58626. + }
  58627. +#endif
  58628. + }
  58629. + } else {
  58630. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  58631. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  58632. + /**
  58633. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  58634. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  58635. + * the data will be written into the fifo by the ISR.
  58636. + */
  58637. + if (core_if->en_multiple_tx_fifo == 0) {
  58638. + intr_mask.b.nptxfempty = 1;
  58639. + DWC_MODIFY_REG32
  58640. + (&core_if->core_global_regs->gintmsk,
  58641. + intr_mask.d32, intr_mask.d32);
  58642. + } else {
  58643. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  58644. + if (ep->xfer_len > 0) {
  58645. + uint32_t fifoemptymsk = 0;
  58646. + fifoemptymsk = 1 << ep->num;
  58647. + DWC_MODIFY_REG32
  58648. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  58649. + 0, fifoemptymsk);
  58650. +
  58651. + }
  58652. + }
  58653. + } else {
  58654. + write_isoc_tx_fifo(core_if, ep);
  58655. + }
  58656. + }
  58657. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  58658. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  58659. +
  58660. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58661. + dsts_data_t dsts = {.d32 = 0};
  58662. + if (ep->bInterval == 1) {
  58663. + dsts.d32 =
  58664. + DWC_READ_REG32(&core_if->dev_if->
  58665. + dev_global_regs->dsts);
  58666. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  58667. + if (ep->frame_num > 0x3FFF) {
  58668. + ep->frm_overrun = 1;
  58669. + ep->frame_num &= 0x3FFF;
  58670. + } else
  58671. + ep->frm_overrun = 0;
  58672. + if (ep->frame_num & 0x1) {
  58673. + depctl.b.setd1pid = 1;
  58674. + } else {
  58675. + depctl.b.setd0pid = 1;
  58676. + }
  58677. + }
  58678. + }
  58679. + /* EP enable, IN data in FIFO */
  58680. + depctl.b.cnak = 1;
  58681. + depctl.b.epena = 1;
  58682. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  58683. +
  58684. + } else {
  58685. + /* OUT endpoint */
  58686. + dwc_otg_dev_out_ep_regs_t *out_regs =
  58687. + core_if->dev_if->out_ep_regs[ep->num];
  58688. +
  58689. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  58690. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  58691. +
  58692. + if (!core_if->dma_desc_enable) {
  58693. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  58694. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  58695. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  58696. + else
  58697. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  58698. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  58699. + }
  58700. +
  58701. + /* Program the transfer size and packet count as follows:
  58702. + *
  58703. + * pktcnt = N
  58704. + * xfersize = N * maxpacket
  58705. + */
  58706. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  58707. + /* Zero Length Packet */
  58708. + deptsiz.b.xfersize = ep->maxpacket;
  58709. + deptsiz.b.pktcnt = 1;
  58710. + } else {
  58711. + deptsiz.b.pktcnt =
  58712. + (ep->xfer_len - ep->xfer_count +
  58713. + (ep->maxpacket - 1)) / ep->maxpacket;
  58714. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  58715. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  58716. + }
  58717. + if (!core_if->dma_desc_enable) {
  58718. + ep->xfer_len =
  58719. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  58720. + }
  58721. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  58722. + }
  58723. +
  58724. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  58725. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  58726. +
  58727. + if (core_if->dma_enable) {
  58728. + if (!core_if->dma_desc_enable) {
  58729. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  58730. + deptsiz.d32);
  58731. +
  58732. + DWC_WRITE_REG32(&(out_regs->doepdma),
  58733. + (uint32_t) ep->dma_addr);
  58734. + } else {
  58735. +#ifdef DWC_UTE_CFI
  58736. + /* The descriptor chain should be already initialized by now */
  58737. + if (ep->buff_mode != BM_STANDARD) {
  58738. + DWC_WRITE_REG32(&out_regs->doepdma,
  58739. + ep->descs_dma_addr);
  58740. + } else {
  58741. +#endif
  58742. + /** This is used for interrupt out transfers*/
  58743. + if (!ep->xfer_len)
  58744. + ep->xfer_len = ep->total_len;
  58745. + init_dma_desc_chain(core_if, ep);
  58746. +
  58747. + if (core_if->core_params->dev_out_nak) {
  58748. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  58749. + deptsiz.b.pktcnt = (ep->total_len +
  58750. + (ep->maxpacket - 1)) / ep->maxpacket;
  58751. + deptsiz.b.xfersize = ep->total_len;
  58752. + /* Remember initial value of doeptsiz */
  58753. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  58754. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  58755. + deptsiz.d32);
  58756. + }
  58757. + }
  58758. + /** DOEPDMAn Register write */
  58759. + DWC_WRITE_REG32(&out_regs->doepdma,
  58760. + ep->dma_desc_addr);
  58761. +#ifdef DWC_UTE_CFI
  58762. + }
  58763. +#endif
  58764. + }
  58765. + } else {
  58766. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  58767. + }
  58768. +
  58769. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  58770. + dsts_data_t dsts = {.d32 = 0};
  58771. + if (ep->bInterval == 1) {
  58772. + dsts.d32 =
  58773. + DWC_READ_REG32(&core_if->dev_if->
  58774. + dev_global_regs->dsts);
  58775. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  58776. + if (ep->frame_num > 0x3FFF) {
  58777. + ep->frm_overrun = 1;
  58778. + ep->frame_num &= 0x3FFF;
  58779. + } else
  58780. + ep->frm_overrun = 0;
  58781. +
  58782. + if (ep->frame_num & 0x1) {
  58783. + depctl.b.setd1pid = 1;
  58784. + } else {
  58785. + depctl.b.setd0pid = 1;
  58786. + }
  58787. + }
  58788. + }
  58789. +
  58790. + /* EP enable */
  58791. + depctl.b.cnak = 1;
  58792. + depctl.b.epena = 1;
  58793. +
  58794. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  58795. +
  58796. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  58797. + DWC_READ_REG32(&out_regs->doepctl),
  58798. + DWC_READ_REG32(&out_regs->doeptsiz));
  58799. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  58800. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  58801. + daintmsk),
  58802. + DWC_READ_REG32(&core_if->core_global_regs->
  58803. + gintmsk));
  58804. +
  58805. + /* Timer is scheduling only for out bulk transfers for
  58806. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  58807. + * about received data payload in case of timeout
  58808. + */
  58809. + if (core_if->core_params->dev_out_nak) {
  58810. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  58811. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  58812. + core_if->ep_xfer_info[ep->num].ep = ep;
  58813. + core_if->ep_xfer_info[ep->num].state = 1;
  58814. +
  58815. + /* Start a timer for this transfer. */
  58816. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  58817. + }
  58818. + }
  58819. + }
  58820. +}
  58821. +
  58822. +/**
  58823. + * This function setup a zero length transfer in Buffer DMA and
  58824. + * Slave modes for usb requests with zero field set
  58825. + *
  58826. + * @param core_if Programming view of DWC_otg controller.
  58827. + * @param ep The EP to start the transfer on.
  58828. + *
  58829. + */
  58830. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58831. +{
  58832. +
  58833. + depctl_data_t depctl;
  58834. + deptsiz_data_t deptsiz;
  58835. + gintmsk_data_t intr_mask = {.d32 = 0 };
  58836. +
  58837. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  58838. + DWC_PRINTF("zero length transfer is called\n");
  58839. +
  58840. + /* IN endpoint */
  58841. + if (ep->is_in == 1) {
  58842. + dwc_otg_dev_in_ep_regs_t *in_regs =
  58843. + core_if->dev_if->in_ep_regs[ep->num];
  58844. +
  58845. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  58846. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  58847. +
  58848. + deptsiz.b.xfersize = 0;
  58849. + deptsiz.b.pktcnt = 1;
  58850. +
  58851. + /* Write the DMA register */
  58852. + if (core_if->dma_enable) {
  58853. + if (core_if->dma_desc_enable == 0) {
  58854. + deptsiz.b.mc = 1;
  58855. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  58856. + deptsiz.d32);
  58857. + DWC_WRITE_REG32(&(in_regs->diepdma),
  58858. + (uint32_t) ep->dma_addr);
  58859. + }
  58860. + } else {
  58861. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  58862. + /**
  58863. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  58864. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  58865. + * the data will be written into the fifo by the ISR.
  58866. + */
  58867. + if (core_if->en_multiple_tx_fifo == 0) {
  58868. + intr_mask.b.nptxfempty = 1;
  58869. + DWC_MODIFY_REG32(&core_if->
  58870. + core_global_regs->gintmsk,
  58871. + intr_mask.d32, intr_mask.d32);
  58872. + } else {
  58873. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  58874. + if (ep->xfer_len > 0) {
  58875. + uint32_t fifoemptymsk = 0;
  58876. + fifoemptymsk = 1 << ep->num;
  58877. + DWC_MODIFY_REG32(&core_if->
  58878. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  58879. + 0, fifoemptymsk);
  58880. + }
  58881. + }
  58882. + }
  58883. +
  58884. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  58885. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  58886. + /* EP enable, IN data in FIFO */
  58887. + depctl.b.cnak = 1;
  58888. + depctl.b.epena = 1;
  58889. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  58890. +
  58891. + } else {
  58892. + /* OUT endpoint */
  58893. + dwc_otg_dev_out_ep_regs_t *out_regs =
  58894. + core_if->dev_if->out_ep_regs[ep->num];
  58895. +
  58896. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  58897. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  58898. +
  58899. + /* Zero Length Packet */
  58900. + deptsiz.b.xfersize = ep->maxpacket;
  58901. + deptsiz.b.pktcnt = 1;
  58902. +
  58903. + if (core_if->dma_enable) {
  58904. + if (!core_if->dma_desc_enable) {
  58905. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  58906. + deptsiz.d32);
  58907. +
  58908. + DWC_WRITE_REG32(&(out_regs->doepdma),
  58909. + (uint32_t) ep->dma_addr);
  58910. + }
  58911. + } else {
  58912. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  58913. + }
  58914. +
  58915. + /* EP enable */
  58916. + depctl.b.cnak = 1;
  58917. + depctl.b.epena = 1;
  58918. +
  58919. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  58920. +
  58921. + }
  58922. +}
  58923. +
  58924. +/**
  58925. + * This function does the setup for a data transfer for EP0 and starts
  58926. + * the transfer. For an IN transfer, the packets will be loaded into
  58927. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  58928. + * unloaded from the Rx FIFO in the ISR.
  58929. + *
  58930. + * @param core_if Programming view of DWC_otg controller.
  58931. + * @param ep The EP0 data.
  58932. + */
  58933. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  58934. +{
  58935. + depctl_data_t depctl;
  58936. + deptsiz0_data_t deptsiz;
  58937. + gintmsk_data_t intr_mask = {.d32 = 0 };
  58938. + dwc_otg_dev_dma_desc_t *dma_desc;
  58939. +
  58940. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  58941. + "xfer_buff=%p start_xfer_buff=%p \n",
  58942. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  58943. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  58944. +
  58945. + ep->total_len = ep->xfer_len;
  58946. +
  58947. + /* IN endpoint */
  58948. + if (ep->is_in == 1) {
  58949. + dwc_otg_dev_in_ep_regs_t *in_regs =
  58950. + core_if->dev_if->in_ep_regs[0];
  58951. +
  58952. + gnptxsts_data_t gtxstatus;
  58953. +
  58954. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  58955. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  58956. + if (depctl.b.epena)
  58957. + return;
  58958. + }
  58959. +
  58960. + gtxstatus.d32 =
  58961. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  58962. +
  58963. + /* If dedicated FIFO every time flush fifo before enable ep*/
  58964. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  58965. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  58966. +
  58967. + if (core_if->en_multiple_tx_fifo == 0
  58968. + && gtxstatus.b.nptxqspcavail == 0
  58969. + && !core_if->dma_enable) {
  58970. +#ifdef DEBUG
  58971. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  58972. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  58973. + DWC_READ_REG32(&in_regs->diepctl));
  58974. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  58975. + deptsiz.d32,
  58976. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  58977. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  58978. + gtxstatus.d32);
  58979. +#endif
  58980. + return;
  58981. + }
  58982. +
  58983. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  58984. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  58985. +
  58986. + /* Zero Length Packet? */
  58987. + if (ep->xfer_len == 0) {
  58988. + deptsiz.b.xfersize = 0;
  58989. + deptsiz.b.pktcnt = 1;
  58990. + } else {
  58991. + /* Program the transfer size and packet count
  58992. + * as follows: xfersize = N * maxpacket +
  58993. + * short_packet pktcnt = N + (short_packet
  58994. + * exist ? 1 : 0)
  58995. + */
  58996. + if (ep->xfer_len > ep->maxpacket) {
  58997. + ep->xfer_len = ep->maxpacket;
  58998. + deptsiz.b.xfersize = ep->maxpacket;
  58999. + } else {
  59000. + deptsiz.b.xfersize = ep->xfer_len;
  59001. + }
  59002. + deptsiz.b.pktcnt = 1;
  59003. +
  59004. + }
  59005. + DWC_DEBUGPL(DBG_PCDV,
  59006. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59007. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59008. + deptsiz.d32);
  59009. +
  59010. + /* Write the DMA register */
  59011. + if (core_if->dma_enable) {
  59012. + if (core_if->dma_desc_enable == 0) {
  59013. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  59014. + deptsiz.d32);
  59015. +
  59016. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59017. + (uint32_t) ep->dma_addr);
  59018. + } else {
  59019. + dma_desc = core_if->dev_if->in_desc_addr;
  59020. +
  59021. + /** DMA Descriptor Setup */
  59022. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59023. + dma_desc->status.b.l = 1;
  59024. + dma_desc->status.b.ioc = 1;
  59025. + dma_desc->status.b.sp =
  59026. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  59027. + dma_desc->status.b.bytes = ep->xfer_len;
  59028. + dma_desc->buf = ep->dma_addr;
  59029. + dma_desc->status.b.sts = 0;
  59030. + dma_desc->status.b.bs = BS_HOST_READY;
  59031. +
  59032. + /** DIEPDMA0 Register write */
  59033. + DWC_WRITE_REG32(&in_regs->diepdma,
  59034. + core_if->
  59035. + dev_if->dma_in_desc_addr);
  59036. + }
  59037. + } else {
  59038. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59039. + }
  59040. +
  59041. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59042. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59043. + /* EP enable, IN data in FIFO */
  59044. + depctl.b.cnak = 1;
  59045. + depctl.b.epena = 1;
  59046. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59047. +
  59048. + /**
  59049. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  59050. + * data will be written into the fifo by the ISR.
  59051. + */
  59052. + if (!core_if->dma_enable) {
  59053. + if (core_if->en_multiple_tx_fifo == 0) {
  59054. + intr_mask.b.nptxfempty = 1;
  59055. + DWC_MODIFY_REG32(&core_if->
  59056. + core_global_regs->gintmsk,
  59057. + intr_mask.d32, intr_mask.d32);
  59058. + } else {
  59059. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59060. + if (ep->xfer_len > 0) {
  59061. + uint32_t fifoemptymsk = 0;
  59062. + fifoemptymsk |= 1 << ep->num;
  59063. + DWC_MODIFY_REG32(&core_if->
  59064. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59065. + 0, fifoemptymsk);
  59066. + }
  59067. + }
  59068. + }
  59069. + } else {
  59070. + /* OUT endpoint */
  59071. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59072. + core_if->dev_if->out_ep_regs[0];
  59073. +
  59074. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  59075. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  59076. +
  59077. + /* Program the transfer size and packet count as follows:
  59078. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  59079. + * pktcnt = N */
  59080. + /* Zero Length Packet */
  59081. + deptsiz.b.xfersize = ep->maxpacket;
  59082. + deptsiz.b.pktcnt = 1;
  59083. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  59084. + deptsiz.b.supcnt = 3;
  59085. +
  59086. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  59087. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  59088. +
  59089. + if (core_if->dma_enable) {
  59090. + if (!core_if->dma_desc_enable) {
  59091. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  59092. + deptsiz.d32);
  59093. +
  59094. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59095. + (uint32_t) ep->dma_addr);
  59096. + } else {
  59097. + dma_desc = core_if->dev_if->out_desc_addr;
  59098. +
  59099. + /** DMA Descriptor Setup */
  59100. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59101. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  59102. + dma_desc->status.b.mtrf = 0;
  59103. + dma_desc->status.b.sr = 0;
  59104. + }
  59105. + dma_desc->status.b.l = 1;
  59106. + dma_desc->status.b.ioc = 1;
  59107. + dma_desc->status.b.bytes = ep->maxpacket;
  59108. + dma_desc->buf = ep->dma_addr;
  59109. + dma_desc->status.b.sts = 0;
  59110. + dma_desc->status.b.bs = BS_HOST_READY;
  59111. +
  59112. + /** DOEPDMA0 Register write */
  59113. + DWC_WRITE_REG32(&out_regs->doepdma,
  59114. + core_if->dev_if->
  59115. + dma_out_desc_addr);
  59116. + }
  59117. + } else {
  59118. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59119. + }
  59120. +
  59121. + /* EP enable */
  59122. + depctl.b.cnak = 1;
  59123. + depctl.b.epena = 1;
  59124. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  59125. + }
  59126. +}
  59127. +
  59128. +/**
  59129. + * This function continues control IN transfers started by
  59130. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  59131. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  59132. + * bit for the packet count.
  59133. + *
  59134. + * @param core_if Programming view of DWC_otg controller.
  59135. + * @param ep The EP0 data.
  59136. + */
  59137. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59138. +{
  59139. + depctl_data_t depctl;
  59140. + deptsiz0_data_t deptsiz;
  59141. + gintmsk_data_t intr_mask = {.d32 = 0 };
  59142. + dwc_otg_dev_dma_desc_t *dma_desc;
  59143. +
  59144. + if (ep->is_in == 1) {
  59145. + dwc_otg_dev_in_ep_regs_t *in_regs =
  59146. + core_if->dev_if->in_ep_regs[0];
  59147. + gnptxsts_data_t tx_status = {.d32 = 0 };
  59148. +
  59149. + tx_status.d32 =
  59150. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  59151. + /** @todo Should there be check for room in the Tx
  59152. + * Status Queue. If not remove the code above this comment. */
  59153. +
  59154. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  59155. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  59156. +
  59157. + /* Program the transfer size and packet count
  59158. + * as follows: xfersize = N * maxpacket +
  59159. + * short_packet pktcnt = N + (short_packet
  59160. + * exist ? 1 : 0)
  59161. + */
  59162. +
  59163. + if (core_if->dma_desc_enable == 0) {
  59164. + deptsiz.b.xfersize =
  59165. + (ep->total_len - ep->xfer_count) >
  59166. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  59167. + ep->xfer_count);
  59168. + deptsiz.b.pktcnt = 1;
  59169. + if (core_if->dma_enable == 0) {
  59170. + ep->xfer_len += deptsiz.b.xfersize;
  59171. + } else {
  59172. + ep->xfer_len = deptsiz.b.xfersize;
  59173. + }
  59174. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  59175. + } else {
  59176. + ep->xfer_len =
  59177. + (ep->total_len - ep->xfer_count) >
  59178. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  59179. + ep->xfer_count);
  59180. +
  59181. + dma_desc = core_if->dev_if->in_desc_addr;
  59182. +
  59183. + /** DMA Descriptor Setup */
  59184. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59185. + dma_desc->status.b.l = 1;
  59186. + dma_desc->status.b.ioc = 1;
  59187. + dma_desc->status.b.sp =
  59188. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  59189. + dma_desc->status.b.bytes = ep->xfer_len;
  59190. + dma_desc->buf = ep->dma_addr;
  59191. + dma_desc->status.b.sts = 0;
  59192. + dma_desc->status.b.bs = BS_HOST_READY;
  59193. +
  59194. + /** DIEPDMA0 Register write */
  59195. + DWC_WRITE_REG32(&in_regs->diepdma,
  59196. + core_if->dev_if->dma_in_desc_addr);
  59197. + }
  59198. +
  59199. + DWC_DEBUGPL(DBG_PCDV,
  59200. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59201. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59202. + deptsiz.d32);
  59203. +
  59204. + /* Write the DMA register */
  59205. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  59206. + if (core_if->dma_desc_enable == 0)
  59207. + DWC_WRITE_REG32(&(in_regs->diepdma),
  59208. + (uint32_t) ep->dma_addr);
  59209. + }
  59210. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  59211. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  59212. + /* EP enable, IN data in FIFO */
  59213. + depctl.b.cnak = 1;
  59214. + depctl.b.epena = 1;
  59215. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  59216. +
  59217. + /**
  59218. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  59219. + * data will be written into the fifo by the ISR.
  59220. + */
  59221. + if (!core_if->dma_enable) {
  59222. + if (core_if->en_multiple_tx_fifo == 0) {
  59223. + /* First clear it from GINTSTS */
  59224. + intr_mask.b.nptxfempty = 1;
  59225. + DWC_MODIFY_REG32(&core_if->
  59226. + core_global_regs->gintmsk,
  59227. + intr_mask.d32, intr_mask.d32);
  59228. +
  59229. + } else {
  59230. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  59231. + if (ep->xfer_len > 0) {
  59232. + uint32_t fifoemptymsk = 0;
  59233. + fifoemptymsk |= 1 << ep->num;
  59234. + DWC_MODIFY_REG32(&core_if->
  59235. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  59236. + 0, fifoemptymsk);
  59237. + }
  59238. + }
  59239. + }
  59240. + } else {
  59241. + dwc_otg_dev_out_ep_regs_t *out_regs =
  59242. + core_if->dev_if->out_ep_regs[0];
  59243. +
  59244. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  59245. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  59246. +
  59247. + /* Program the transfer size and packet count
  59248. + * as follows: xfersize = N * maxpacket +
  59249. + * short_packet pktcnt = N + (short_packet
  59250. + * exist ? 1 : 0)
  59251. + */
  59252. + deptsiz.b.xfersize = ep->maxpacket;
  59253. + deptsiz.b.pktcnt = 1;
  59254. +
  59255. + if (core_if->dma_desc_enable == 0) {
  59256. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  59257. + } else {
  59258. + dma_desc = core_if->dev_if->out_desc_addr;
  59259. +
  59260. + /** DMA Descriptor Setup */
  59261. + dma_desc->status.b.bs = BS_HOST_BUSY;
  59262. + dma_desc->status.b.l = 1;
  59263. + dma_desc->status.b.ioc = 1;
  59264. + dma_desc->status.b.bytes = ep->maxpacket;
  59265. + dma_desc->buf = ep->dma_addr;
  59266. + dma_desc->status.b.sts = 0;
  59267. + dma_desc->status.b.bs = BS_HOST_READY;
  59268. +
  59269. + /** DOEPDMA0 Register write */
  59270. + DWC_WRITE_REG32(&out_regs->doepdma,
  59271. + core_if->dev_if->dma_out_desc_addr);
  59272. + }
  59273. +
  59274. + DWC_DEBUGPL(DBG_PCDV,
  59275. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  59276. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  59277. + deptsiz.d32);
  59278. +
  59279. + /* Write the DMA register */
  59280. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  59281. + if (core_if->dma_desc_enable == 0)
  59282. + DWC_WRITE_REG32(&(out_regs->doepdma),
  59283. + (uint32_t) ep->dma_addr);
  59284. +
  59285. + }
  59286. +
  59287. + /* EP enable, IN data in FIFO */
  59288. + depctl.b.cnak = 1;
  59289. + depctl.b.epena = 1;
  59290. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  59291. +
  59292. + }
  59293. +}
  59294. +
  59295. +#ifdef DEBUG
  59296. +void dump_msg(const u8 * buf, unsigned int length)
  59297. +{
  59298. + unsigned int start, num, i;
  59299. + char line[52], *p;
  59300. +
  59301. + if (length >= 512)
  59302. + return;
  59303. + start = 0;
  59304. + while (length > 0) {
  59305. + num = length < 16u ? length : 16u;
  59306. + p = line;
  59307. + for (i = 0; i < num; ++i) {
  59308. + if (i == 8)
  59309. + *p++ = ' ';
  59310. + DWC_SPRINTF(p, " %02x", buf[i]);
  59311. + p += 3;
  59312. + }
  59313. + *p = 0;
  59314. + DWC_PRINTF("%6x: %s\n", start, line);
  59315. + buf += num;
  59316. + start += num;
  59317. + length -= num;
  59318. + }
  59319. +}
  59320. +#else
  59321. +static inline void dump_msg(const u8 * buf, unsigned int length)
  59322. +{
  59323. +}
  59324. +#endif
  59325. +
  59326. +/**
  59327. + * This function writes a packet into the Tx FIFO associated with the
  59328. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  59329. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  59330. + * with all packets for the next micro-frame.
  59331. + *
  59332. + * @param core_if Programming view of DWC_otg controller.
  59333. + * @param ep The EP to write packet for.
  59334. + * @param dma Indicates if DMA is being used.
  59335. + */
  59336. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  59337. + int dma)
  59338. +{
  59339. + /**
  59340. + * The buffer is padded to DWORD on a per packet basis in
  59341. + * slave/dma mode if the MPS is not DWORD aligned. The last
  59342. + * packet, if short, is also padded to a multiple of DWORD.
  59343. + *
  59344. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  59345. + * multiple of DWORD in length
  59346. + *
  59347. + * ep->xfer_len can be any number of bytes
  59348. + *
  59349. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  59350. + * packet
  59351. + *
  59352. + * FIFO access is DWORD */
  59353. +
  59354. + uint32_t i;
  59355. + uint32_t byte_count;
  59356. + uint32_t dword_count;
  59357. + uint32_t *fifo;
  59358. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  59359. +
  59360. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  59361. + ep);
  59362. + if (ep->xfer_count >= ep->xfer_len) {
  59363. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  59364. + return;
  59365. + }
  59366. +
  59367. + /* Find the byte length of the packet either short packet or MPS */
  59368. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  59369. + byte_count = ep->xfer_len - ep->xfer_count;
  59370. + } else {
  59371. + byte_count = ep->maxpacket;
  59372. + }
  59373. +
  59374. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  59375. + * is not a multiple of DWORD */
  59376. + dword_count = (byte_count + 3) / 4;
  59377. +
  59378. +#ifdef VERBOSE
  59379. + dump_msg(ep->xfer_buff, byte_count);
  59380. +#endif
  59381. +
  59382. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  59383. + * intialized? What should this be? */
  59384. +
  59385. + fifo = core_if->data_fifo[ep->num];
  59386. +
  59387. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  59388. + fifo, data_buff, *data_buff, byte_count);
  59389. +
  59390. + if (!dma) {
  59391. + for (i = 0; i < dword_count; i++, data_buff++) {
  59392. + DWC_WRITE_REG32(fifo, *data_buff);
  59393. + }
  59394. + }
  59395. +
  59396. + ep->xfer_count += byte_count;
  59397. + ep->xfer_buff += byte_count;
  59398. + ep->dma_addr += byte_count;
  59399. +}
  59400. +
  59401. +/**
  59402. + * Set the EP STALL.
  59403. + *
  59404. + * @param core_if Programming view of DWC_otg controller.
  59405. + * @param ep The EP to set the stall on.
  59406. + */
  59407. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59408. +{
  59409. + depctl_data_t depctl;
  59410. + volatile uint32_t *depctl_addr;
  59411. +
  59412. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  59413. + (ep->is_in ? "IN" : "OUT"));
  59414. +
  59415. + if (ep->is_in == 1) {
  59416. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  59417. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  59418. +
  59419. + /* set the disable and stall bits */
  59420. + if (depctl.b.epena) {
  59421. + depctl.b.epdis = 1;
  59422. + }
  59423. + depctl.b.stall = 1;
  59424. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  59425. + } else {
  59426. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  59427. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  59428. +
  59429. + /* set the stall bit */
  59430. + depctl.b.stall = 1;
  59431. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  59432. + }
  59433. +
  59434. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  59435. +
  59436. + return;
  59437. +}
  59438. +
  59439. +/**
  59440. + * Clear the EP STALL.
  59441. + *
  59442. + * @param core_if Programming view of DWC_otg controller.
  59443. + * @param ep The EP to clear stall from.
  59444. + */
  59445. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  59446. +{
  59447. + depctl_data_t depctl;
  59448. + volatile uint32_t *depctl_addr;
  59449. +
  59450. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  59451. + (ep->is_in ? "IN" : "OUT"));
  59452. +
  59453. + if (ep->is_in == 1) {
  59454. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  59455. + } else {
  59456. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  59457. + }
  59458. +
  59459. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  59460. +
  59461. + /* clear the stall bits */
  59462. + depctl.b.stall = 0;
  59463. +
  59464. + /*
  59465. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  59466. + * of whether an endpoint has the Halt feature set, a
  59467. + * ClearFeature(ENDPOINT_HALT) request always results in the
  59468. + * data toggle being reinitialized to DATA0.
  59469. + */
  59470. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  59471. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  59472. + depctl.b.setd0pid = 1; /* DATA0 */
  59473. + }
  59474. +
  59475. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  59476. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  59477. + return;
  59478. +}
  59479. +
  59480. +/**
  59481. + * This function reads a packet from the Rx FIFO into the destination
  59482. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  59483. + *
  59484. + * @param core_if Programming view of DWC_otg controller.
  59485. + * @param dest Destination buffer for the packet.
  59486. + * @param bytes Number of bytes to copy to the destination.
  59487. + */
  59488. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  59489. + uint8_t * dest, uint16_t bytes)
  59490. +{
  59491. + int i;
  59492. + int word_count = (bytes + 3) / 4;
  59493. +
  59494. + volatile uint32_t *fifo = core_if->data_fifo[0];
  59495. + uint32_t *data_buff = (uint32_t *) dest;
  59496. +
  59497. + /**
  59498. + * @todo Account for the case where _dest is not dword aligned. This
  59499. + * requires reading data from the FIFO into a uint32_t temp buffer,
  59500. + * then moving it into the data buffer.
  59501. + */
  59502. +
  59503. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  59504. + core_if, dest, bytes);
  59505. +
  59506. + for (i = 0; i < word_count; i++, data_buff++) {
  59507. + *data_buff = DWC_READ_REG32(fifo);
  59508. + }
  59509. +
  59510. + return;
  59511. +}
  59512. +
  59513. +/**
  59514. + * This functions reads the device registers and prints them
  59515. + *
  59516. + * @param core_if Programming view of DWC_otg controller.
  59517. + */
  59518. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  59519. +{
  59520. + int i;
  59521. + volatile uint32_t *addr;
  59522. +
  59523. + DWC_PRINTF("Device Global Registers\n");
  59524. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  59525. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  59526. + (unsigned long)addr, DWC_READ_REG32(addr));
  59527. + addr = &core_if->dev_if->dev_global_regs->dctl;
  59528. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  59529. + (unsigned long)addr, DWC_READ_REG32(addr));
  59530. + addr = &core_if->dev_if->dev_global_regs->dsts;
  59531. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  59532. + (unsigned long)addr, DWC_READ_REG32(addr));
  59533. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  59534. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59535. + DWC_READ_REG32(addr));
  59536. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  59537. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59538. + DWC_READ_REG32(addr));
  59539. + addr = &core_if->dev_if->dev_global_regs->daint;
  59540. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59541. + DWC_READ_REG32(addr));
  59542. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  59543. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59544. + DWC_READ_REG32(addr));
  59545. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  59546. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59547. + DWC_READ_REG32(addr));
  59548. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  59549. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  59550. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  59551. + (unsigned long)addr, DWC_READ_REG32(addr));
  59552. + }
  59553. +
  59554. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  59555. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59556. + DWC_READ_REG32(addr));
  59557. +
  59558. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  59559. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  59560. + (unsigned long)addr, DWC_READ_REG32(addr));
  59561. +
  59562. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  59563. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  59564. + (unsigned long)addr, DWC_READ_REG32(addr));
  59565. +
  59566. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  59567. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  59568. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  59569. + (unsigned long)addr, DWC_READ_REG32(addr));
  59570. + }
  59571. +
  59572. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  59573. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59574. + DWC_READ_REG32(addr));
  59575. +
  59576. + if (core_if->hwcfg2.b.multi_proc_int) {
  59577. +
  59578. + addr = &core_if->dev_if->dev_global_regs->deachint;
  59579. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  59580. + (unsigned long)addr, DWC_READ_REG32(addr));
  59581. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  59582. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  59583. + (unsigned long)addr, DWC_READ_REG32(addr));
  59584. +
  59585. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  59586. + addr =
  59587. + &core_if->dev_if->
  59588. + dev_global_regs->diepeachintmsk[i];
  59589. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  59590. + i, (unsigned long)addr,
  59591. + DWC_READ_REG32(addr));
  59592. + }
  59593. +
  59594. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  59595. + addr =
  59596. + &core_if->dev_if->
  59597. + dev_global_regs->doepeachintmsk[i];
  59598. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  59599. + i, (unsigned long)addr,
  59600. + DWC_READ_REG32(addr));
  59601. + }
  59602. + }
  59603. +
  59604. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  59605. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  59606. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  59607. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  59608. + (unsigned long)addr, DWC_READ_REG32(addr));
  59609. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  59610. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  59611. + (unsigned long)addr, DWC_READ_REG32(addr));
  59612. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  59613. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  59614. + (unsigned long)addr, DWC_READ_REG32(addr));
  59615. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  59616. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  59617. + (unsigned long)addr, DWC_READ_REG32(addr));
  59618. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  59619. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  59620. + (unsigned long)addr, DWC_READ_REG32(addr));
  59621. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  59622. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  59623. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  59624. + }
  59625. +
  59626. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  59627. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  59628. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  59629. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  59630. + (unsigned long)addr, DWC_READ_REG32(addr));
  59631. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  59632. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  59633. + (unsigned long)addr, DWC_READ_REG32(addr));
  59634. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  59635. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  59636. + (unsigned long)addr, DWC_READ_REG32(addr));
  59637. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  59638. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  59639. + (unsigned long)addr, DWC_READ_REG32(addr));
  59640. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  59641. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  59642. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  59643. + (unsigned long)addr, DWC_READ_REG32(addr));
  59644. + }
  59645. +
  59646. + }
  59647. +}
  59648. +
  59649. +/**
  59650. + * This functions reads the SPRAM and prints its content
  59651. + *
  59652. + * @param core_if Programming view of DWC_otg controller.
  59653. + */
  59654. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  59655. +{
  59656. + volatile uint8_t *addr, *start_addr, *end_addr;
  59657. +
  59658. + DWC_PRINTF("SPRAM Data:\n");
  59659. + start_addr = (void *)core_if->core_global_regs;
  59660. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  59661. + start_addr += 0x00028000;
  59662. + end_addr = (void *)core_if->core_global_regs;
  59663. + end_addr += 0x000280e0;
  59664. +
  59665. + for (addr = start_addr; addr < end_addr; addr += 16) {
  59666. + DWC_PRINTF
  59667. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  59668. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  59669. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  59670. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  59671. + );
  59672. + }
  59673. +
  59674. + return;
  59675. +}
  59676. +
  59677. +/**
  59678. + * This function reads the host registers and prints them
  59679. + *
  59680. + * @param core_if Programming view of DWC_otg controller.
  59681. + */
  59682. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  59683. +{
  59684. + int i;
  59685. + volatile uint32_t *addr;
  59686. +
  59687. + DWC_PRINTF("Host Global Registers\n");
  59688. + addr = &core_if->host_if->host_global_regs->hcfg;
  59689. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  59690. + (unsigned long)addr, DWC_READ_REG32(addr));
  59691. + addr = &core_if->host_if->host_global_regs->hfir;
  59692. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  59693. + (unsigned long)addr, DWC_READ_REG32(addr));
  59694. + addr = &core_if->host_if->host_global_regs->hfnum;
  59695. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59696. + DWC_READ_REG32(addr));
  59697. + addr = &core_if->host_if->host_global_regs->hptxsts;
  59698. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59699. + DWC_READ_REG32(addr));
  59700. + addr = &core_if->host_if->host_global_regs->haint;
  59701. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59702. + DWC_READ_REG32(addr));
  59703. + addr = &core_if->host_if->host_global_regs->haintmsk;
  59704. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59705. + DWC_READ_REG32(addr));
  59706. + if (core_if->dma_desc_enable) {
  59707. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  59708. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  59709. + (unsigned long)addr, DWC_READ_REG32(addr));
  59710. + }
  59711. +
  59712. + addr = core_if->host_if->hprt0;
  59713. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59714. + DWC_READ_REG32(addr));
  59715. +
  59716. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  59717. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  59718. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  59719. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  59720. + (unsigned long)addr, DWC_READ_REG32(addr));
  59721. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  59722. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  59723. + (unsigned long)addr, DWC_READ_REG32(addr));
  59724. + addr = &core_if->host_if->hc_regs[i]->hcint;
  59725. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  59726. + (unsigned long)addr, DWC_READ_REG32(addr));
  59727. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  59728. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  59729. + (unsigned long)addr, DWC_READ_REG32(addr));
  59730. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  59731. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  59732. + (unsigned long)addr, DWC_READ_REG32(addr));
  59733. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  59734. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  59735. + (unsigned long)addr, DWC_READ_REG32(addr));
  59736. + if (core_if->dma_desc_enable) {
  59737. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  59738. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  59739. + (unsigned long)addr, DWC_READ_REG32(addr));
  59740. + }
  59741. +
  59742. + }
  59743. + return;
  59744. +}
  59745. +
  59746. +/**
  59747. + * This function reads the core global registers and prints them
  59748. + *
  59749. + * @param core_if Programming view of DWC_otg controller.
  59750. + */
  59751. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  59752. +{
  59753. + int i, ep_num;
  59754. + volatile uint32_t *addr;
  59755. + char *txfsiz;
  59756. +
  59757. + DWC_PRINTF("Core Global Registers\n");
  59758. + addr = &core_if->core_global_regs->gotgctl;
  59759. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59760. + DWC_READ_REG32(addr));
  59761. + addr = &core_if->core_global_regs->gotgint;
  59762. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59763. + DWC_READ_REG32(addr));
  59764. + addr = &core_if->core_global_regs->gahbcfg;
  59765. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59766. + DWC_READ_REG32(addr));
  59767. + addr = &core_if->core_global_regs->gusbcfg;
  59768. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59769. + DWC_READ_REG32(addr));
  59770. + addr = &core_if->core_global_regs->grstctl;
  59771. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59772. + DWC_READ_REG32(addr));
  59773. + addr = &core_if->core_global_regs->gintsts;
  59774. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59775. + DWC_READ_REG32(addr));
  59776. + addr = &core_if->core_global_regs->gintmsk;
  59777. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59778. + DWC_READ_REG32(addr));
  59779. + addr = &core_if->core_global_regs->grxstsr;
  59780. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59781. + DWC_READ_REG32(addr));
  59782. + addr = &core_if->core_global_regs->grxfsiz;
  59783. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59784. + DWC_READ_REG32(addr));
  59785. + addr = &core_if->core_global_regs->gnptxfsiz;
  59786. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59787. + DWC_READ_REG32(addr));
  59788. + addr = &core_if->core_global_regs->gnptxsts;
  59789. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59790. + DWC_READ_REG32(addr));
  59791. + addr = &core_if->core_global_regs->gi2cctl;
  59792. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59793. + DWC_READ_REG32(addr));
  59794. + addr = &core_if->core_global_regs->gpvndctl;
  59795. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59796. + DWC_READ_REG32(addr));
  59797. + addr = &core_if->core_global_regs->ggpio;
  59798. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59799. + DWC_READ_REG32(addr));
  59800. + addr = &core_if->core_global_regs->guid;
  59801. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  59802. + (unsigned long)addr, DWC_READ_REG32(addr));
  59803. + addr = &core_if->core_global_regs->gsnpsid;
  59804. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59805. + DWC_READ_REG32(addr));
  59806. + addr = &core_if->core_global_regs->ghwcfg1;
  59807. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59808. + DWC_READ_REG32(addr));
  59809. + addr = &core_if->core_global_regs->ghwcfg2;
  59810. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59811. + DWC_READ_REG32(addr));
  59812. + addr = &core_if->core_global_regs->ghwcfg3;
  59813. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59814. + DWC_READ_REG32(addr));
  59815. + addr = &core_if->core_global_regs->ghwcfg4;
  59816. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59817. + DWC_READ_REG32(addr));
  59818. + addr = &core_if->core_global_regs->glpmcfg;
  59819. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59820. + DWC_READ_REG32(addr));
  59821. + addr = &core_if->core_global_regs->gpwrdn;
  59822. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59823. + DWC_READ_REG32(addr));
  59824. + addr = &core_if->core_global_regs->gdfifocfg;
  59825. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59826. + DWC_READ_REG32(addr));
  59827. + addr = &core_if->core_global_regs->adpctl;
  59828. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59829. + dwc_otg_adp_read_reg(core_if));
  59830. + addr = &core_if->core_global_regs->hptxfsiz;
  59831. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59832. + DWC_READ_REG32(addr));
  59833. +
  59834. + if (core_if->en_multiple_tx_fifo == 0) {
  59835. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  59836. + txfsiz = "DPTXFSIZ";
  59837. + } else {
  59838. + ep_num = core_if->hwcfg4.b.num_in_eps;
  59839. + txfsiz = "DIENPTXF";
  59840. + }
  59841. + for (i = 0; i < ep_num; i++) {
  59842. + addr = &core_if->core_global_regs->dtxfsiz[i];
  59843. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  59844. + (unsigned long)addr, DWC_READ_REG32(addr));
  59845. + }
  59846. + addr = core_if->pcgcctl;
  59847. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  59848. + DWC_READ_REG32(addr));
  59849. +}
  59850. +
  59851. +/**
  59852. + * Flush a Tx FIFO.
  59853. + *
  59854. + * @param core_if Programming view of DWC_otg controller.
  59855. + * @param num Tx FIFO to flush.
  59856. + */
  59857. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  59858. +{
  59859. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  59860. + volatile grstctl_t greset = {.d32 = 0 };
  59861. + int count = 0;
  59862. +
  59863. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  59864. +
  59865. + greset.b.txfflsh = 1;
  59866. + greset.b.txfnum = num;
  59867. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  59868. +
  59869. + do {
  59870. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  59871. + if (++count > 10000) {
  59872. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  59873. + __func__, greset.d32,
  59874. + DWC_READ_REG32(&global_regs->gnptxsts));
  59875. + break;
  59876. + }
  59877. + dwc_udelay(1);
  59878. + } while (greset.b.txfflsh == 1);
  59879. +
  59880. + /* Wait for 3 PHY Clocks */
  59881. + dwc_udelay(1);
  59882. +}
  59883. +
  59884. +/**
  59885. + * Flush Rx FIFO.
  59886. + *
  59887. + * @param core_if Programming view of DWC_otg controller.
  59888. + */
  59889. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  59890. +{
  59891. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  59892. + volatile grstctl_t greset = {.d32 = 0 };
  59893. + int count = 0;
  59894. +
  59895. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  59896. + /*
  59897. + *
  59898. + */
  59899. + greset.b.rxfflsh = 1;
  59900. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  59901. +
  59902. + do {
  59903. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  59904. + if (++count > 10000) {
  59905. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  59906. + greset.d32);
  59907. + break;
  59908. + }
  59909. + dwc_udelay(1);
  59910. + } while (greset.b.rxfflsh == 1);
  59911. +
  59912. + /* Wait for 3 PHY Clocks */
  59913. + dwc_udelay(1);
  59914. +}
  59915. +
  59916. +/**
  59917. + * Do core a soft reset of the core. Be careful with this because it
  59918. + * resets all the internal state machines of the core.
  59919. + */
  59920. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  59921. +{
  59922. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  59923. + volatile grstctl_t greset = {.d32 = 0 };
  59924. + int count = 0;
  59925. +
  59926. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  59927. + /* Wait for AHB master IDLE state. */
  59928. + do {
  59929. + dwc_udelay(10);
  59930. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  59931. + if (++count > 100000) {
  59932. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  59933. + greset.d32);
  59934. + return;
  59935. + }
  59936. + }
  59937. + while (greset.b.ahbidle == 0);
  59938. +
  59939. + /* Core Soft Reset */
  59940. + count = 0;
  59941. + greset.b.csftrst = 1;
  59942. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  59943. + do {
  59944. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  59945. + if (++count > 10000) {
  59946. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  59947. + __func__, greset.d32);
  59948. + break;
  59949. + }
  59950. + dwc_udelay(1);
  59951. + }
  59952. + while (greset.b.csftrst == 1);
  59953. +
  59954. + /* Wait for 3 PHY Clocks */
  59955. + dwc_mdelay(100);
  59956. +}
  59957. +
  59958. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  59959. +{
  59960. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  59961. +}
  59962. +
  59963. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  59964. +{
  59965. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  59966. +}
  59967. +
  59968. +/**
  59969. + * Register HCD callbacks. The callbacks are used to start and stop
  59970. + * the HCD for interrupt processing.
  59971. + *
  59972. + * @param core_if Programming view of DWC_otg controller.
  59973. + * @param cb the HCD callback structure.
  59974. + * @param p pointer to be passed to callback function (usb_hcd*).
  59975. + */
  59976. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  59977. + dwc_otg_cil_callbacks_t * cb, void *p)
  59978. +{
  59979. + core_if->hcd_cb = cb;
  59980. + cb->p = p;
  59981. +}
  59982. +
  59983. +/**
  59984. + * Register PCD callbacks. The callbacks are used to start and stop
  59985. + * the PCD for interrupt processing.
  59986. + *
  59987. + * @param core_if Programming view of DWC_otg controller.
  59988. + * @param cb the PCD callback structure.
  59989. + * @param p pointer to be passed to callback function (pcd*).
  59990. + */
  59991. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  59992. + dwc_otg_cil_callbacks_t * cb, void *p)
  59993. +{
  59994. + core_if->pcd_cb = cb;
  59995. + cb->p = p;
  59996. +}
  59997. +
  59998. +#ifdef DWC_EN_ISOC
  59999. +
  60000. +/**
  60001. + * This function writes isoc data per 1 (micro)frame into tx fifo
  60002. + *
  60003. + * @param core_if Programming view of DWC_otg controller.
  60004. + * @param ep The EP to start the transfer on.
  60005. + *
  60006. + */
  60007. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  60008. +{
  60009. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  60010. + dtxfsts_data_t txstatus = {.d32 = 0 };
  60011. + uint32_t len = 0;
  60012. + uint32_t dwords;
  60013. +
  60014. + ep->xfer_len = ep->data_per_frame;
  60015. + ep->xfer_count = 0;
  60016. +
  60017. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  60018. +
  60019. + len = ep->xfer_len - ep->xfer_count;
  60020. +
  60021. + if (len > ep->maxpacket) {
  60022. + len = ep->maxpacket;
  60023. + }
  60024. +
  60025. + dwords = (len + 3) / 4;
  60026. +
  60027. + /* While there is space in the queue and space in the FIFO and
  60028. + * More data to tranfer, Write packets to the Tx FIFO */
  60029. + txstatus.d32 =
  60030. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  60031. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  60032. +
  60033. + while (txstatus.b.txfspcavail > dwords &&
  60034. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  60035. + /* Write the FIFO */
  60036. + dwc_otg_ep_write_packet(core_if, ep, 0);
  60037. +
  60038. + len = ep->xfer_len - ep->xfer_count;
  60039. + if (len > ep->maxpacket) {
  60040. + len = ep->maxpacket;
  60041. + }
  60042. +
  60043. + dwords = (len + 3) / 4;
  60044. + txstatus.d32 =
  60045. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  60046. + dtxfsts);
  60047. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  60048. + txstatus.d32);
  60049. + }
  60050. +}
  60051. +
  60052. +/**
  60053. + * This function initializes a descriptor chain for Isochronous transfer
  60054. + *
  60055. + * @param core_if Programming view of DWC_otg controller.
  60056. + * @param ep The EP to start the transfer on.
  60057. + *
  60058. + */
  60059. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  60060. + dwc_ep_t * ep)
  60061. +{
  60062. + deptsiz_data_t deptsiz = {.d32 = 0 };
  60063. + depctl_data_t depctl = {.d32 = 0 };
  60064. + dsts_data_t dsts = {.d32 = 0 };
  60065. + volatile uint32_t *addr;
  60066. +
  60067. + if (ep->is_in) {
  60068. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  60069. + } else {
  60070. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  60071. + }
  60072. +
  60073. + ep->xfer_len = ep->data_per_frame;
  60074. + ep->xfer_count = 0;
  60075. + ep->xfer_buff = ep->cur_pkt_addr;
  60076. + ep->dma_addr = ep->cur_pkt_dma_addr;
  60077. +
  60078. + if (ep->is_in) {
  60079. + /* Program the transfer size and packet count
  60080. + * as follows: xfersize = N * maxpacket +
  60081. + * short_packet pktcnt = N + (short_packet
  60082. + * exist ? 1 : 0)
  60083. + */
  60084. + deptsiz.b.xfersize = ep->xfer_len;
  60085. + deptsiz.b.pktcnt =
  60086. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  60087. + deptsiz.b.mc = deptsiz.b.pktcnt;
  60088. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  60089. + deptsiz.d32);
  60090. +
  60091. + /* Write the DMA register */
  60092. + if (core_if->dma_enable) {
  60093. + DWC_WRITE_REG32(&
  60094. + (core_if->dev_if->in_ep_regs[ep->num]->
  60095. + diepdma), (uint32_t) ep->dma_addr);
  60096. + }
  60097. + } else {
  60098. + deptsiz.b.pktcnt =
  60099. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  60100. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  60101. +
  60102. + DWC_WRITE_REG32(&core_if->dev_if->
  60103. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  60104. +
  60105. + if (core_if->dma_enable) {
  60106. + DWC_WRITE_REG32(&
  60107. + (core_if->dev_if->
  60108. + out_ep_regs[ep->num]->doepdma),
  60109. + (uint32_t) ep->dma_addr);
  60110. + }
  60111. + }
  60112. +
  60113. + /** Enable endpoint, clear nak */
  60114. +
  60115. + depctl.d32 = 0;
  60116. + if (ep->bInterval == 1) {
  60117. + dsts.d32 =
  60118. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  60119. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  60120. +
  60121. + if (ep->next_frame & 0x1) {
  60122. + depctl.b.setd1pid = 1;
  60123. + } else {
  60124. + depctl.b.setd0pid = 1;
  60125. + }
  60126. + } else {
  60127. + ep->next_frame += ep->bInterval;
  60128. +
  60129. + if (ep->next_frame & 0x1) {
  60130. + depctl.b.setd1pid = 1;
  60131. + } else {
  60132. + depctl.b.setd0pid = 1;
  60133. + }
  60134. + }
  60135. + depctl.b.epena = 1;
  60136. + depctl.b.cnak = 1;
  60137. +
  60138. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  60139. + depctl.d32 = DWC_READ_REG32(addr);
  60140. +
  60141. + if (ep->is_in && core_if->dma_enable == 0) {
  60142. + write_isoc_frame_data(core_if, ep);
  60143. + }
  60144. +
  60145. +}
  60146. +#endif /* DWC_EN_ISOC */
  60147. +
  60148. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  60149. +{
  60150. + int i;
  60151. + for (i = 0; i < size; i++) {
  60152. + p[i] = -1;
  60153. + }
  60154. +}
  60155. +
  60156. +static int dwc_otg_param_initialized(int32_t val)
  60157. +{
  60158. + return val != -1;
  60159. +}
  60160. +
  60161. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  60162. +{
  60163. + int i;
  60164. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  60165. + if (!core_if->core_params) {
  60166. + return -DWC_E_NO_MEMORY;
  60167. + }
  60168. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  60169. + sizeof(*core_if->core_params) /
  60170. + sizeof(int32_t));
  60171. + DWC_PRINTF("Setting default values for core params\n");
  60172. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  60173. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  60174. + dwc_otg_set_param_dma_desc_enable(core_if,
  60175. + dwc_param_dma_desc_enable_default);
  60176. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  60177. + dwc_otg_set_param_dma_burst_size(core_if,
  60178. + dwc_param_dma_burst_size_default);
  60179. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  60180. + dwc_param_host_support_fs_ls_low_power_default);
  60181. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  60182. + dwc_param_enable_dynamic_fifo_default);
  60183. + dwc_otg_set_param_data_fifo_size(core_if,
  60184. + dwc_param_data_fifo_size_default);
  60185. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  60186. + dwc_param_dev_rx_fifo_size_default);
  60187. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  60188. + dwc_param_dev_nperio_tx_fifo_size_default);
  60189. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  60190. + dwc_param_host_rx_fifo_size_default);
  60191. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  60192. + dwc_param_host_nperio_tx_fifo_size_default);
  60193. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  60194. + dwc_param_host_perio_tx_fifo_size_default);
  60195. + dwc_otg_set_param_max_transfer_size(core_if,
  60196. + dwc_param_max_transfer_size_default);
  60197. + dwc_otg_set_param_max_packet_count(core_if,
  60198. + dwc_param_max_packet_count_default);
  60199. + dwc_otg_set_param_host_channels(core_if,
  60200. + dwc_param_host_channels_default);
  60201. + dwc_otg_set_param_dev_endpoints(core_if,
  60202. + dwc_param_dev_endpoints_default);
  60203. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  60204. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  60205. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  60206. + dwc_param_host_ls_low_power_phy_clk_default);
  60207. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  60208. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  60209. + dwc_param_phy_ulpi_ext_vbus_default);
  60210. + dwc_otg_set_param_phy_utmi_width(core_if,
  60211. + dwc_param_phy_utmi_width_default);
  60212. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  60213. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  60214. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  60215. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  60216. + dwc_param_en_multiple_tx_fifo_default);
  60217. + for (i = 0; i < 15; i++) {
  60218. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  60219. + dwc_param_dev_perio_tx_fifo_size_default,
  60220. + i);
  60221. + }
  60222. +
  60223. + for (i = 0; i < 15; i++) {
  60224. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  60225. + dwc_param_dev_tx_fifo_size_default,
  60226. + i);
  60227. + }
  60228. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  60229. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  60230. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  60231. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  60232. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  60233. + dwc_otg_set_param_tx_thr_length(core_if,
  60234. + dwc_param_tx_thr_length_default);
  60235. + dwc_otg_set_param_rx_thr_length(core_if,
  60236. + dwc_param_rx_thr_length_default);
  60237. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  60238. + dwc_param_ahb_thr_ratio_default);
  60239. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  60240. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  60241. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  60242. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  60243. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  60244. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  60245. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  60246. + DWC_PRINTF("Finished setting default values for core params\n");
  60247. +
  60248. + return 0;
  60249. +}
  60250. +
  60251. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  60252. +{
  60253. + return core_if->dma_enable;
  60254. +}
  60255. +
  60256. +/* Checks if the parameter is outside of its valid range of values */
  60257. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  60258. + (((_param_) < (_low_)) || \
  60259. + ((_param_) > (_high_)))
  60260. +
  60261. +/* Parameter access functions */
  60262. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  60263. +{
  60264. + int valid;
  60265. + int retval = 0;
  60266. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  60267. + DWC_WARN("Wrong value for otg_cap parameter\n");
  60268. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  60269. + retval = -DWC_E_INVALID;
  60270. + goto out;
  60271. + }
  60272. +
  60273. + valid = 1;
  60274. + switch (val) {
  60275. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  60276. + if (core_if->hwcfg2.b.op_mode !=
  60277. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60278. + valid = 0;
  60279. + break;
  60280. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  60281. + if ((core_if->hwcfg2.b.op_mode !=
  60282. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60283. + && (core_if->hwcfg2.b.op_mode !=
  60284. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  60285. + && (core_if->hwcfg2.b.op_mode !=
  60286. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  60287. + && (core_if->hwcfg2.b.op_mode !=
  60288. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  60289. + valid = 0;
  60290. + }
  60291. + break;
  60292. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  60293. + /* always valid */
  60294. + break;
  60295. + }
  60296. + if (!valid) {
  60297. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  60298. + DWC_ERROR
  60299. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  60300. + val);
  60301. + }
  60302. + val =
  60303. + (((core_if->hwcfg2.b.op_mode ==
  60304. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  60305. + || (core_if->hwcfg2.b.op_mode ==
  60306. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  60307. + || (core_if->hwcfg2.b.op_mode ==
  60308. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  60309. + || (core_if->hwcfg2.b.op_mode ==
  60310. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  60311. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  60312. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  60313. + retval = -DWC_E_INVALID;
  60314. + }
  60315. +
  60316. + core_if->core_params->otg_cap = val;
  60317. +out:
  60318. + return retval;
  60319. +}
  60320. +
  60321. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  60322. +{
  60323. + return core_if->core_params->otg_cap;
  60324. +}
  60325. +
  60326. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  60327. +{
  60328. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60329. + DWC_WARN("Wrong value for opt parameter\n");
  60330. + return -DWC_E_INVALID;
  60331. + }
  60332. + core_if->core_params->opt = val;
  60333. + return 0;
  60334. +}
  60335. +
  60336. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  60337. +{
  60338. + return core_if->core_params->opt;
  60339. +}
  60340. +
  60341. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  60342. +{
  60343. + int retval = 0;
  60344. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60345. + DWC_WARN("Wrong value for dma enable\n");
  60346. + return -DWC_E_INVALID;
  60347. + }
  60348. +
  60349. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  60350. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  60351. + DWC_ERROR
  60352. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  60353. + val);
  60354. + }
  60355. + val = 0;
  60356. + retval = -DWC_E_INVALID;
  60357. + }
  60358. +
  60359. + core_if->core_params->dma_enable = val;
  60360. + if (val == 0) {
  60361. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  60362. + }
  60363. + return retval;
  60364. +}
  60365. +
  60366. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  60367. +{
  60368. + return core_if->core_params->dma_enable;
  60369. +}
  60370. +
  60371. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  60372. +{
  60373. + int retval = 0;
  60374. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60375. + DWC_WARN("Wrong value for dma_enable\n");
  60376. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  60377. + return -DWC_E_INVALID;
  60378. + }
  60379. +
  60380. + if ((val == 1)
  60381. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  60382. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  60383. + if (dwc_otg_param_initialized
  60384. + (core_if->core_params->dma_desc_enable)) {
  60385. + DWC_ERROR
  60386. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  60387. + val);
  60388. + }
  60389. + val = 0;
  60390. + retval = -DWC_E_INVALID;
  60391. + }
  60392. + core_if->core_params->dma_desc_enable = val;
  60393. + return retval;
  60394. +}
  60395. +
  60396. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  60397. +{
  60398. + return core_if->core_params->dma_desc_enable;
  60399. +}
  60400. +
  60401. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  60402. + int32_t val)
  60403. +{
  60404. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60405. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  60406. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  60407. + return -DWC_E_INVALID;
  60408. + }
  60409. + core_if->core_params->host_support_fs_ls_low_power = val;
  60410. + return 0;
  60411. +}
  60412. +
  60413. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  60414. + core_if)
  60415. +{
  60416. + return core_if->core_params->host_support_fs_ls_low_power;
  60417. +}
  60418. +
  60419. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  60420. + int32_t val)
  60421. +{
  60422. + int retval = 0;
  60423. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60424. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  60425. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  60426. + return -DWC_E_INVALID;
  60427. + }
  60428. +
  60429. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  60430. + if (dwc_otg_param_initialized
  60431. + (core_if->core_params->enable_dynamic_fifo)) {
  60432. + DWC_ERROR
  60433. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  60434. + val);
  60435. + }
  60436. + val = 0;
  60437. + retval = -DWC_E_INVALID;
  60438. + }
  60439. + core_if->core_params->enable_dynamic_fifo = val;
  60440. + return retval;
  60441. +}
  60442. +
  60443. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  60444. +{
  60445. + return core_if->core_params->enable_dynamic_fifo;
  60446. +}
  60447. +
  60448. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  60449. +{
  60450. + int retval = 0;
  60451. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  60452. + DWC_WARN("Wrong value for data_fifo_size\n");
  60453. + DWC_WARN("data_fifo_size must be 32-32768\n");
  60454. + return -DWC_E_INVALID;
  60455. + }
  60456. +
  60457. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  60458. + if (dwc_otg_param_initialized
  60459. + (core_if->core_params->data_fifo_size)) {
  60460. + DWC_ERROR
  60461. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  60462. + val);
  60463. + }
  60464. + val = core_if->hwcfg3.b.dfifo_depth;
  60465. + retval = -DWC_E_INVALID;
  60466. + }
  60467. +
  60468. + core_if->core_params->data_fifo_size = val;
  60469. + return retval;
  60470. +}
  60471. +
  60472. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  60473. +{
  60474. + return core_if->core_params->data_fifo_size;
  60475. +}
  60476. +
  60477. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  60478. +{
  60479. + int retval = 0;
  60480. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  60481. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  60482. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  60483. + return -DWC_E_INVALID;
  60484. + }
  60485. +
  60486. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  60487. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  60488. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  60489. + }
  60490. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  60491. + retval = -DWC_E_INVALID;
  60492. + }
  60493. +
  60494. + core_if->core_params->dev_rx_fifo_size = val;
  60495. + return retval;
  60496. +}
  60497. +
  60498. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  60499. +{
  60500. + return core_if->core_params->dev_rx_fifo_size;
  60501. +}
  60502. +
  60503. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  60504. + int32_t val)
  60505. +{
  60506. + int retval = 0;
  60507. +
  60508. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  60509. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  60510. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  60511. + return -DWC_E_INVALID;
  60512. + }
  60513. +
  60514. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  60515. + if (dwc_otg_param_initialized
  60516. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  60517. + DWC_ERROR
  60518. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  60519. + val);
  60520. + }
  60521. + val =
  60522. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  60523. + 16);
  60524. + retval = -DWC_E_INVALID;
  60525. + }
  60526. +
  60527. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  60528. + return retval;
  60529. +}
  60530. +
  60531. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  60532. +{
  60533. + return core_if->core_params->dev_nperio_tx_fifo_size;
  60534. +}
  60535. +
  60536. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  60537. + int32_t val)
  60538. +{
  60539. + int retval = 0;
  60540. +
  60541. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  60542. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  60543. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  60544. + return -DWC_E_INVALID;
  60545. + }
  60546. +
  60547. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  60548. + if (dwc_otg_param_initialized
  60549. + (core_if->core_params->host_rx_fifo_size)) {
  60550. + DWC_ERROR
  60551. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  60552. + val);
  60553. + }
  60554. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  60555. + retval = -DWC_E_INVALID;
  60556. + }
  60557. +
  60558. + core_if->core_params->host_rx_fifo_size = val;
  60559. + return retval;
  60560. +
  60561. +}
  60562. +
  60563. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  60564. +{
  60565. + return core_if->core_params->host_rx_fifo_size;
  60566. +}
  60567. +
  60568. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  60569. + int32_t val)
  60570. +{
  60571. + int retval = 0;
  60572. +
  60573. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  60574. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  60575. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  60576. + return -DWC_E_INVALID;
  60577. + }
  60578. +
  60579. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  60580. + if (dwc_otg_param_initialized
  60581. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  60582. + DWC_ERROR
  60583. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  60584. + val);
  60585. + }
  60586. + val =
  60587. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  60588. + 16);
  60589. + retval = -DWC_E_INVALID;
  60590. + }
  60591. +
  60592. + core_if->core_params->host_nperio_tx_fifo_size = val;
  60593. + return retval;
  60594. +}
  60595. +
  60596. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  60597. +{
  60598. + return core_if->core_params->host_nperio_tx_fifo_size;
  60599. +}
  60600. +
  60601. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  60602. + int32_t val)
  60603. +{
  60604. + int retval = 0;
  60605. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  60606. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  60607. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  60608. + return -DWC_E_INVALID;
  60609. + }
  60610. +
  60611. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  60612. + if (dwc_otg_param_initialized
  60613. + (core_if->core_params->host_perio_tx_fifo_size)) {
  60614. + DWC_ERROR
  60615. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  60616. + val);
  60617. + }
  60618. + val = (core_if->hptxfsiz.d32) >> 16;
  60619. + retval = -DWC_E_INVALID;
  60620. + }
  60621. +
  60622. + core_if->core_params->host_perio_tx_fifo_size = val;
  60623. + return retval;
  60624. +}
  60625. +
  60626. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  60627. +{
  60628. + return core_if->core_params->host_perio_tx_fifo_size;
  60629. +}
  60630. +
  60631. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  60632. + int32_t val)
  60633. +{
  60634. + int retval = 0;
  60635. +
  60636. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  60637. + DWC_WARN("Wrong value for max_transfer_size\n");
  60638. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  60639. + return -DWC_E_INVALID;
  60640. + }
  60641. +
  60642. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  60643. + if (dwc_otg_param_initialized
  60644. + (core_if->core_params->max_transfer_size)) {
  60645. + DWC_ERROR
  60646. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  60647. + val);
  60648. + }
  60649. + val =
  60650. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  60651. + 1);
  60652. + retval = -DWC_E_INVALID;
  60653. + }
  60654. +
  60655. + core_if->core_params->max_transfer_size = val;
  60656. + return retval;
  60657. +}
  60658. +
  60659. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  60660. +{
  60661. + return core_if->core_params->max_transfer_size;
  60662. +}
  60663. +
  60664. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  60665. +{
  60666. + int retval = 0;
  60667. +
  60668. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  60669. + DWC_WARN("Wrong value for max_packet_count\n");
  60670. + DWC_WARN("max_packet_count must be 15-511\n");
  60671. + return -DWC_E_INVALID;
  60672. + }
  60673. +
  60674. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  60675. + if (dwc_otg_param_initialized
  60676. + (core_if->core_params->max_packet_count)) {
  60677. + DWC_ERROR
  60678. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  60679. + val);
  60680. + }
  60681. + val =
  60682. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  60683. + retval = -DWC_E_INVALID;
  60684. + }
  60685. +
  60686. + core_if->core_params->max_packet_count = val;
  60687. + return retval;
  60688. +}
  60689. +
  60690. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  60691. +{
  60692. + return core_if->core_params->max_packet_count;
  60693. +}
  60694. +
  60695. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  60696. +{
  60697. + int retval = 0;
  60698. +
  60699. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  60700. + DWC_WARN("Wrong value for host_channels\n");
  60701. + DWC_WARN("host_channels must be 1-16\n");
  60702. + return -DWC_E_INVALID;
  60703. + }
  60704. +
  60705. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  60706. + if (dwc_otg_param_initialized
  60707. + (core_if->core_params->host_channels)) {
  60708. + DWC_ERROR
  60709. + ("%d invalid for host_channels. Check HW configurations.\n",
  60710. + val);
  60711. + }
  60712. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  60713. + retval = -DWC_E_INVALID;
  60714. + }
  60715. +
  60716. + core_if->core_params->host_channels = val;
  60717. + return retval;
  60718. +}
  60719. +
  60720. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  60721. +{
  60722. + return core_if->core_params->host_channels;
  60723. +}
  60724. +
  60725. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  60726. +{
  60727. + int retval = 0;
  60728. +
  60729. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  60730. + DWC_WARN("Wrong value for dev_endpoints\n");
  60731. + DWC_WARN("dev_endpoints must be 1-15\n");
  60732. + return -DWC_E_INVALID;
  60733. + }
  60734. +
  60735. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  60736. + if (dwc_otg_param_initialized
  60737. + (core_if->core_params->dev_endpoints)) {
  60738. + DWC_ERROR
  60739. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  60740. + val);
  60741. + }
  60742. + val = core_if->hwcfg2.b.num_dev_ep;
  60743. + retval = -DWC_E_INVALID;
  60744. + }
  60745. +
  60746. + core_if->core_params->dev_endpoints = val;
  60747. + return retval;
  60748. +}
  60749. +
  60750. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  60751. +{
  60752. + return core_if->core_params->dev_endpoints;
  60753. +}
  60754. +
  60755. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  60756. +{
  60757. + int retval = 0;
  60758. + int valid = 0;
  60759. +
  60760. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  60761. + DWC_WARN("Wrong value for phy_type\n");
  60762. + DWC_WARN("phy_type must be 0,1 or 2\n");
  60763. + return -DWC_E_INVALID;
  60764. + }
  60765. +#ifndef NO_FS_PHY_HW_CHECKS
  60766. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  60767. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  60768. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  60769. + valid = 1;
  60770. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  60771. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  60772. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  60773. + valid = 1;
  60774. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  60775. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  60776. + valid = 1;
  60777. + }
  60778. + if (!valid) {
  60779. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  60780. + DWC_ERROR
  60781. + ("%d invalid for phy_type. Check HW configurations.\n",
  60782. + val);
  60783. + }
  60784. + if (core_if->hwcfg2.b.hs_phy_type) {
  60785. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  60786. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  60787. + val = DWC_PHY_TYPE_PARAM_UTMI;
  60788. + } else {
  60789. + val = DWC_PHY_TYPE_PARAM_ULPI;
  60790. + }
  60791. + }
  60792. + retval = -DWC_E_INVALID;
  60793. + }
  60794. +#endif
  60795. + core_if->core_params->phy_type = val;
  60796. + return retval;
  60797. +}
  60798. +
  60799. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  60800. +{
  60801. + return core_if->core_params->phy_type;
  60802. +}
  60803. +
  60804. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  60805. +{
  60806. + int retval = 0;
  60807. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60808. + DWC_WARN("Wrong value for speed parameter\n");
  60809. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  60810. + return -DWC_E_INVALID;
  60811. + }
  60812. + if ((val == 0)
  60813. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  60814. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  60815. + DWC_ERROR
  60816. + ("%d invalid for speed paremter. Check HW configuration.\n",
  60817. + val);
  60818. + }
  60819. + val =
  60820. + (dwc_otg_get_param_phy_type(core_if) ==
  60821. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  60822. + retval = -DWC_E_INVALID;
  60823. + }
  60824. + core_if->core_params->speed = val;
  60825. + return retval;
  60826. +}
  60827. +
  60828. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  60829. +{
  60830. + return core_if->core_params->speed;
  60831. +}
  60832. +
  60833. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  60834. + int32_t val)
  60835. +{
  60836. + int retval = 0;
  60837. +
  60838. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60839. + DWC_WARN
  60840. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  60841. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  60842. + return -DWC_E_INVALID;
  60843. + }
  60844. +
  60845. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  60846. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  60847. + if (dwc_otg_param_initialized
  60848. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  60849. + DWC_ERROR
  60850. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  60851. + val);
  60852. + }
  60853. + val =
  60854. + (dwc_otg_get_param_phy_type(core_if) ==
  60855. + DWC_PHY_TYPE_PARAM_FS) ?
  60856. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  60857. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  60858. + retval = -DWC_E_INVALID;
  60859. + }
  60860. +
  60861. + core_if->core_params->host_ls_low_power_phy_clk = val;
  60862. + return retval;
  60863. +}
  60864. +
  60865. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  60866. +{
  60867. + return core_if->core_params->host_ls_low_power_phy_clk;
  60868. +}
  60869. +
  60870. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  60871. +{
  60872. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60873. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  60874. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  60875. + return -DWC_E_INVALID;
  60876. + }
  60877. +
  60878. + core_if->core_params->phy_ulpi_ddr = val;
  60879. + return 0;
  60880. +}
  60881. +
  60882. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  60883. +{
  60884. + return core_if->core_params->phy_ulpi_ddr;
  60885. +}
  60886. +
  60887. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  60888. + int32_t val)
  60889. +{
  60890. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60891. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  60892. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  60893. + return -DWC_E_INVALID;
  60894. + }
  60895. +
  60896. + core_if->core_params->phy_ulpi_ext_vbus = val;
  60897. + return 0;
  60898. +}
  60899. +
  60900. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  60901. +{
  60902. + return core_if->core_params->phy_ulpi_ext_vbus;
  60903. +}
  60904. +
  60905. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  60906. +{
  60907. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  60908. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  60909. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  60910. + return -DWC_E_INVALID;
  60911. + }
  60912. +
  60913. + core_if->core_params->phy_utmi_width = val;
  60914. + return 0;
  60915. +}
  60916. +
  60917. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  60918. +{
  60919. + return core_if->core_params->phy_utmi_width;
  60920. +}
  60921. +
  60922. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  60923. +{
  60924. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60925. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  60926. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  60927. + return -DWC_E_INVALID;
  60928. + }
  60929. +
  60930. + core_if->core_params->ulpi_fs_ls = val;
  60931. + return 0;
  60932. +}
  60933. +
  60934. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  60935. +{
  60936. + return core_if->core_params->ulpi_fs_ls;
  60937. +}
  60938. +
  60939. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  60940. +{
  60941. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60942. + DWC_WARN("Wrong valaue for ts_dline\n");
  60943. + DWC_WARN("ts_dline must be 0 or 1\n");
  60944. + return -DWC_E_INVALID;
  60945. + }
  60946. +
  60947. + core_if->core_params->ts_dline = val;
  60948. + return 0;
  60949. +}
  60950. +
  60951. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  60952. +{
  60953. + return core_if->core_params->ts_dline;
  60954. +}
  60955. +
  60956. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  60957. +{
  60958. + int retval = 0;
  60959. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  60960. + DWC_WARN("Wrong valaue for i2c_enable\n");
  60961. + DWC_WARN("i2c_enable must be 0 or 1\n");
  60962. + return -DWC_E_INVALID;
  60963. + }
  60964. +#ifndef NO_FS_PHY_HW_CHECK
  60965. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  60966. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  60967. + DWC_ERROR
  60968. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  60969. + val);
  60970. + }
  60971. + val = 0;
  60972. + retval = -DWC_E_INVALID;
  60973. + }
  60974. +#endif
  60975. +
  60976. + core_if->core_params->i2c_enable = val;
  60977. + return retval;
  60978. +}
  60979. +
  60980. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  60981. +{
  60982. + return core_if->core_params->i2c_enable;
  60983. +}
  60984. +
  60985. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  60986. + int32_t val, int fifo_num)
  60987. +{
  60988. + int retval = 0;
  60989. +
  60990. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  60991. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  60992. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  60993. + return -DWC_E_INVALID;
  60994. + }
  60995. +
  60996. + if (val >
  60997. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  60998. + if (dwc_otg_param_initialized
  60999. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  61000. + DWC_ERROR
  61001. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  61002. + val, fifo_num);
  61003. + }
  61004. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  61005. + retval = -DWC_E_INVALID;
  61006. + }
  61007. +
  61008. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  61009. + return retval;
  61010. +}
  61011. +
  61012. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61013. + int fifo_num)
  61014. +{
  61015. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  61016. +}
  61017. +
  61018. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  61019. + int32_t val)
  61020. +{
  61021. + int retval = 0;
  61022. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61023. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  61024. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  61025. + return -DWC_E_INVALID;
  61026. + }
  61027. +
  61028. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  61029. + if (dwc_otg_param_initialized
  61030. + (core_if->core_params->en_multiple_tx_fifo)) {
  61031. + DWC_ERROR
  61032. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  61033. + val);
  61034. + }
  61035. + val = 0;
  61036. + retval = -DWC_E_INVALID;
  61037. + }
  61038. +
  61039. + core_if->core_params->en_multiple_tx_fifo = val;
  61040. + return retval;
  61041. +}
  61042. +
  61043. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  61044. +{
  61045. + return core_if->core_params->en_multiple_tx_fifo;
  61046. +}
  61047. +
  61048. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  61049. + int fifo_num)
  61050. +{
  61051. + int retval = 0;
  61052. +
  61053. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  61054. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  61055. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  61056. + return -DWC_E_INVALID;
  61057. + }
  61058. +
  61059. + if (val >
  61060. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  61061. + if (dwc_otg_param_initialized
  61062. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  61063. + DWC_ERROR
  61064. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  61065. + val, fifo_num);
  61066. + }
  61067. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  61068. + retval = -DWC_E_INVALID;
  61069. + }
  61070. +
  61071. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  61072. + return retval;
  61073. +}
  61074. +
  61075. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  61076. + int fifo_num)
  61077. +{
  61078. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  61079. +}
  61080. +
  61081. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  61082. +{
  61083. + int retval = 0;
  61084. +
  61085. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  61086. + DWC_WARN("Wrong value for thr_ctl\n");
  61087. + DWC_WARN("thr_ctl must be 0-7\n");
  61088. + return -DWC_E_INVALID;
  61089. + }
  61090. +
  61091. + if ((val != 0) &&
  61092. + (!dwc_otg_get_param_dma_enable(core_if) ||
  61093. + !core_if->hwcfg4.b.ded_fifo_en)) {
  61094. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  61095. + DWC_ERROR
  61096. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  61097. + val);
  61098. + }
  61099. + val = 0;
  61100. + retval = -DWC_E_INVALID;
  61101. + }
  61102. +
  61103. + core_if->core_params->thr_ctl = val;
  61104. + return retval;
  61105. +}
  61106. +
  61107. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  61108. +{
  61109. + return core_if->core_params->thr_ctl;
  61110. +}
  61111. +
  61112. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61113. +{
  61114. + int retval = 0;
  61115. +
  61116. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61117. + DWC_WARN("Wrong value for lpm_enable\n");
  61118. + DWC_WARN("lpm_enable must be 0 or 1\n");
  61119. + return -DWC_E_INVALID;
  61120. + }
  61121. +
  61122. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  61123. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  61124. + DWC_ERROR
  61125. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  61126. + val);
  61127. + }
  61128. + val = 0;
  61129. + retval = -DWC_E_INVALID;
  61130. + }
  61131. +
  61132. + core_if->core_params->lpm_enable = val;
  61133. + return retval;
  61134. +}
  61135. +
  61136. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  61137. +{
  61138. + return core_if->core_params->lpm_enable;
  61139. +}
  61140. +
  61141. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  61142. +{
  61143. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  61144. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  61145. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  61146. + return -DWC_E_INVALID;
  61147. + }
  61148. +
  61149. + core_if->core_params->tx_thr_length = val;
  61150. + return 0;
  61151. +}
  61152. +
  61153. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  61154. +{
  61155. + return core_if->core_params->tx_thr_length;
  61156. +}
  61157. +
  61158. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  61159. +{
  61160. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  61161. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  61162. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  61163. + return -DWC_E_INVALID;
  61164. + }
  61165. +
  61166. + core_if->core_params->rx_thr_length = val;
  61167. + return 0;
  61168. +}
  61169. +
  61170. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  61171. +{
  61172. + return core_if->core_params->rx_thr_length;
  61173. +}
  61174. +
  61175. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  61176. +{
  61177. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  61178. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  61179. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  61180. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  61181. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  61182. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  61183. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  61184. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  61185. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  61186. + return -DWC_E_INVALID;
  61187. + }
  61188. + core_if->core_params->dma_burst_size = val;
  61189. + return 0;
  61190. +}
  61191. +
  61192. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  61193. +{
  61194. + return core_if->core_params->dma_burst_size;
  61195. +}
  61196. +
  61197. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61198. +{
  61199. + int retval = 0;
  61200. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61201. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  61202. + return -DWC_E_INVALID;
  61203. + }
  61204. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  61205. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  61206. + DWC_ERROR
  61207. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  61208. + val);
  61209. + }
  61210. + retval = -DWC_E_INVALID;
  61211. + val = 0;
  61212. + }
  61213. + core_if->core_params->pti_enable = val;
  61214. + return retval;
  61215. +}
  61216. +
  61217. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  61218. +{
  61219. + return core_if->core_params->pti_enable;
  61220. +}
  61221. +
  61222. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61223. +{
  61224. + int retval = 0;
  61225. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61226. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  61227. + return -DWC_E_INVALID;
  61228. + }
  61229. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  61230. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  61231. + DWC_ERROR
  61232. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  61233. + val);
  61234. + }
  61235. + retval = -DWC_E_INVALID;
  61236. + val = 0;
  61237. + }
  61238. + core_if->core_params->mpi_enable = val;
  61239. + return retval;
  61240. +}
  61241. +
  61242. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  61243. +{
  61244. + return core_if->core_params->mpi_enable;
  61245. +}
  61246. +
  61247. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  61248. +{
  61249. + int retval = 0;
  61250. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61251. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  61252. + return -DWC_E_INVALID;
  61253. + }
  61254. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  61255. + if (dwc_otg_param_initialized
  61256. + (core_if->core_params->adp_supp_enable)) {
  61257. + DWC_ERROR
  61258. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  61259. + val);
  61260. + }
  61261. + retval = -DWC_E_INVALID;
  61262. + val = 0;
  61263. + }
  61264. + core_if->core_params->adp_supp_enable = val;
  61265. + /*Set OTG version 2.0 in case of enabling ADP*/
  61266. + if (val)
  61267. + dwc_otg_set_param_otg_ver(core_if, 1);
  61268. +
  61269. + return retval;
  61270. +}
  61271. +
  61272. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  61273. +{
  61274. + return core_if->core_params->adp_supp_enable;
  61275. +}
  61276. +
  61277. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  61278. +{
  61279. + int retval = 0;
  61280. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61281. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  61282. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  61283. + return -DWC_E_INVALID;
  61284. + }
  61285. +
  61286. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  61287. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  61288. + DWC_ERROR
  61289. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  61290. + val);
  61291. + }
  61292. + retval = -DWC_E_INVALID;
  61293. + val = 0;
  61294. + }
  61295. + core_if->core_params->ic_usb_cap = val;
  61296. + return retval;
  61297. +}
  61298. +
  61299. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  61300. +{
  61301. + return core_if->core_params->ic_usb_cap;
  61302. +}
  61303. +
  61304. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  61305. +{
  61306. + int retval = 0;
  61307. + int valid = 1;
  61308. +
  61309. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  61310. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  61311. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  61312. + return -DWC_E_INVALID;
  61313. + }
  61314. +
  61315. + if (val
  61316. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  61317. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  61318. + valid = 0;
  61319. + } else if (val
  61320. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  61321. + 4)) {
  61322. + valid = 0;
  61323. + }
  61324. + if (valid == 0) {
  61325. + if (dwc_otg_param_initialized
  61326. + (core_if->core_params->ahb_thr_ratio)) {
  61327. + DWC_ERROR
  61328. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  61329. + val);
  61330. + }
  61331. + retval = -DWC_E_INVALID;
  61332. + val = 0;
  61333. + }
  61334. +
  61335. + core_if->core_params->ahb_thr_ratio = val;
  61336. + return retval;
  61337. +}
  61338. +
  61339. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  61340. +{
  61341. + return core_if->core_params->ahb_thr_ratio;
  61342. +}
  61343. +
  61344. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  61345. +{
  61346. + int retval = 0;
  61347. + int valid = 1;
  61348. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  61349. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  61350. +
  61351. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  61352. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  61353. + DWC_WARN("power_down must be 0 - 2\n");
  61354. + return -DWC_E_INVALID;
  61355. + }
  61356. +
  61357. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  61358. + valid = 0;
  61359. + }
  61360. + if ((val == 3)
  61361. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  61362. + || (hwcfg4.b.xhiber == 0))) {
  61363. + valid = 0;
  61364. + }
  61365. + if (valid == 0) {
  61366. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  61367. + DWC_ERROR
  61368. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  61369. + val);
  61370. + }
  61371. + retval = -DWC_E_INVALID;
  61372. + val = 0;
  61373. + }
  61374. + core_if->core_params->power_down = val;
  61375. + return retval;
  61376. +}
  61377. +
  61378. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  61379. +{
  61380. + return core_if->core_params->power_down;
  61381. +}
  61382. +
  61383. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  61384. +{
  61385. + int retval = 0;
  61386. + int valid = 1;
  61387. +
  61388. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61389. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  61390. + DWC_WARN("reload_ctl must be 0 or 1\n");
  61391. + return -DWC_E_INVALID;
  61392. + }
  61393. +
  61394. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  61395. + valid = 0;
  61396. + }
  61397. + if (valid == 0) {
  61398. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  61399. + DWC_ERROR("%d invalid for parameter reload_ctl."
  61400. + "Check HW configuration.\n", val);
  61401. + }
  61402. + retval = -DWC_E_INVALID;
  61403. + val = 0;
  61404. + }
  61405. + core_if->core_params->reload_ctl = val;
  61406. + return retval;
  61407. +}
  61408. +
  61409. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  61410. +{
  61411. + return core_if->core_params->reload_ctl;
  61412. +}
  61413. +
  61414. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  61415. +{
  61416. + int retval = 0;
  61417. + int valid = 1;
  61418. +
  61419. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61420. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  61421. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  61422. + return -DWC_E_INVALID;
  61423. + }
  61424. +
  61425. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  61426. + !(core_if->core_params->dma_desc_enable))) {
  61427. + valid = 0;
  61428. + }
  61429. + if (valid == 0) {
  61430. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  61431. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  61432. + "Check HW configuration.\n", val);
  61433. + }
  61434. + retval = -DWC_E_INVALID;
  61435. + val = 0;
  61436. + }
  61437. + core_if->core_params->dev_out_nak = val;
  61438. + return retval;
  61439. +}
  61440. +
  61441. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  61442. +{
  61443. + return core_if->core_params->dev_out_nak;
  61444. +}
  61445. +
  61446. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  61447. +{
  61448. + int retval = 0;
  61449. + int valid = 1;
  61450. +
  61451. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61452. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  61453. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  61454. + return -DWC_E_INVALID;
  61455. + }
  61456. +
  61457. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  61458. + !(core_if->core_params->dma_desc_enable))) {
  61459. + valid = 0;
  61460. + }
  61461. + if (valid == 0) {
  61462. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  61463. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  61464. + "Check HW configuration.\n", val);
  61465. + }
  61466. + retval = -DWC_E_INVALID;
  61467. + val = 0;
  61468. + }
  61469. + core_if->core_params->cont_on_bna = val;
  61470. + return retval;
  61471. +}
  61472. +
  61473. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  61474. +{
  61475. + return core_if->core_params->cont_on_bna;
  61476. +}
  61477. +
  61478. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  61479. +{
  61480. + int retval = 0;
  61481. + int valid = 1;
  61482. +
  61483. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61484. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  61485. + DWC_WARN("ahb_single must be 0 or 1\n");
  61486. + return -DWC_E_INVALID;
  61487. + }
  61488. +
  61489. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  61490. + valid = 0;
  61491. + }
  61492. + if (valid == 0) {
  61493. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  61494. + DWC_ERROR("%d invalid for parameter ahb_single."
  61495. + "Check HW configuration.\n", val);
  61496. + }
  61497. + retval = -DWC_E_INVALID;
  61498. + val = 0;
  61499. + }
  61500. + core_if->core_params->ahb_single = val;
  61501. + return retval;
  61502. +}
  61503. +
  61504. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  61505. +{
  61506. + return core_if->core_params->ahb_single;
  61507. +}
  61508. +
  61509. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  61510. +{
  61511. + int retval = 0;
  61512. +
  61513. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  61514. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  61515. + DWC_WARN
  61516. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  61517. + return -DWC_E_INVALID;
  61518. + }
  61519. +
  61520. + core_if->core_params->otg_ver = val;
  61521. + return retval;
  61522. +}
  61523. +
  61524. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  61525. +{
  61526. + return core_if->core_params->otg_ver;
  61527. +}
  61528. +
  61529. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  61530. +{
  61531. + gotgctl_data_t otgctl;
  61532. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  61533. + return otgctl.b.hstnegscs;
  61534. +}
  61535. +
  61536. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  61537. +{
  61538. + gotgctl_data_t otgctl;
  61539. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  61540. + return otgctl.b.sesreqscs;
  61541. +}
  61542. +
  61543. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  61544. +{
  61545. + if(core_if->otg_ver == 0) {
  61546. + gotgctl_data_t otgctl;
  61547. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  61548. + otgctl.b.hnpreq = val;
  61549. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  61550. + } else {
  61551. + core_if->otg_sts = val;
  61552. + }
  61553. +}
  61554. +
  61555. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  61556. +{
  61557. + return core_if->snpsid;
  61558. +}
  61559. +
  61560. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  61561. +{
  61562. + gintsts_data_t gintsts;
  61563. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  61564. + return gintsts.b.curmode;
  61565. +}
  61566. +
  61567. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  61568. +{
  61569. + gusbcfg_data_t usbcfg;
  61570. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  61571. + return usbcfg.b.hnpcap;
  61572. +}
  61573. +
  61574. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  61575. +{
  61576. + gusbcfg_data_t usbcfg;
  61577. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  61578. + usbcfg.b.hnpcap = val;
  61579. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  61580. +}
  61581. +
  61582. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  61583. +{
  61584. + gusbcfg_data_t usbcfg;
  61585. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  61586. + return usbcfg.b.srpcap;
  61587. +}
  61588. +
  61589. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  61590. +{
  61591. + gusbcfg_data_t usbcfg;
  61592. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  61593. + usbcfg.b.srpcap = val;
  61594. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  61595. +}
  61596. +
  61597. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  61598. +{
  61599. + dcfg_data_t dcfg;
  61600. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  61601. +
  61602. + dcfg.d32 = -1; //GRAYG
  61603. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  61604. + if (NULL == core_if)
  61605. + DWC_ERROR("reg request with NULL core_if\n");
  61606. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  61607. + core_if, core_if->dev_if);
  61608. + if (NULL == core_if->dev_if)
  61609. + DWC_ERROR("reg request with NULL dev_if\n");
  61610. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  61611. + "dev_global_regs(%p)\n", __func__,
  61612. + core_if, core_if->dev_if,
  61613. + core_if->dev_if->dev_global_regs);
  61614. + if (NULL == core_if->dev_if->dev_global_regs)
  61615. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  61616. + else {
  61617. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  61618. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  61619. + core_if, core_if->dev_if,
  61620. + core_if->dev_if->dev_global_regs,
  61621. + &core_if->dev_if->dev_global_regs->dcfg);
  61622. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  61623. + }
  61624. + return dcfg.b.devspd;
  61625. +}
  61626. +
  61627. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  61628. +{
  61629. + dcfg_data_t dcfg;
  61630. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  61631. + dcfg.b.devspd = val;
  61632. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  61633. +}
  61634. +
  61635. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  61636. +{
  61637. + hprt0_data_t hprt0;
  61638. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  61639. + return hprt0.b.prtconnsts;
  61640. +}
  61641. +
  61642. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  61643. +{
  61644. + dsts_data_t dsts;
  61645. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  61646. + return dsts.b.enumspd;
  61647. +}
  61648. +
  61649. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  61650. +{
  61651. + hprt0_data_t hprt0;
  61652. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  61653. + return hprt0.b.prtpwr;
  61654. +
  61655. +}
  61656. +
  61657. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  61658. +{
  61659. + return core_if->hibernation_suspend;
  61660. +}
  61661. +
  61662. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  61663. +{
  61664. + hprt0_data_t hprt0;
  61665. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  61666. + hprt0.b.prtpwr = val;
  61667. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  61668. +}
  61669. +
  61670. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  61671. +{
  61672. + hprt0_data_t hprt0;
  61673. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  61674. + return hprt0.b.prtsusp;
  61675. +
  61676. +}
  61677. +
  61678. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  61679. +{
  61680. + hprt0_data_t hprt0;
  61681. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  61682. + hprt0.b.prtsusp = val;
  61683. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  61684. +}
  61685. +
  61686. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  61687. +{
  61688. + hfir_data_t hfir;
  61689. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  61690. + return hfir.b.frint;
  61691. +
  61692. +}
  61693. +
  61694. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  61695. +{
  61696. + hfir_data_t hfir;
  61697. + uint32_t fram_int;
  61698. + fram_int = calc_frame_interval(core_if);
  61699. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  61700. + if (!core_if->core_params->reload_ctl) {
  61701. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  61702. + "not set to 1.\nShould load driver with reload_ctl=1"
  61703. + " module parameter\n");
  61704. + return;
  61705. + }
  61706. + switch (fram_int) {
  61707. + case 3750:
  61708. + if ((val < 3350) || (val > 4150)) {
  61709. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  61710. + "clock freq should be from 3350 to 4150\n");
  61711. + return;
  61712. + }
  61713. + break;
  61714. + case 30000:
  61715. + if ((val < 26820) || (val > 33180)) {
  61716. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  61717. + "clock freq should be from 26820 to 33180\n");
  61718. + return;
  61719. + }
  61720. + break;
  61721. + case 6000:
  61722. + if ((val < 5360) || (val > 6640)) {
  61723. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  61724. + "clock freq should be from 5360 to 6640\n");
  61725. + return;
  61726. + }
  61727. + break;
  61728. + case 48000:
  61729. + if ((val < 42912) || (val > 53088)) {
  61730. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  61731. + "clock freq should be from 42912 to 53088\n");
  61732. + return;
  61733. + }
  61734. + break;
  61735. + case 7500:
  61736. + if ((val < 6700) || (val > 8300)) {
  61737. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  61738. + "clock freq should be from 6700 to 8300\n");
  61739. + return;
  61740. + }
  61741. + break;
  61742. + case 60000:
  61743. + if ((val < 53640) || (val > 65536)) {
  61744. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  61745. + "clock freq should be from 53640 to 65536\n");
  61746. + return;
  61747. + }
  61748. + break;
  61749. + default:
  61750. + DWC_WARN("Unknown frame interval\n");
  61751. + return;
  61752. + break;
  61753. +
  61754. + }
  61755. + hfir.b.frint = val;
  61756. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  61757. +}
  61758. +
  61759. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  61760. +{
  61761. + hcfg_data_t hcfg;
  61762. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  61763. + return hcfg.b.modechtimen;
  61764. +
  61765. +}
  61766. +
  61767. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  61768. +{
  61769. + hcfg_data_t hcfg;
  61770. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  61771. + hcfg.b.modechtimen = val;
  61772. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  61773. +}
  61774. +
  61775. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  61776. +{
  61777. + hprt0_data_t hprt0;
  61778. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  61779. + hprt0.b.prtres = val;
  61780. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  61781. +}
  61782. +
  61783. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  61784. +{
  61785. + dctl_data_t dctl;
  61786. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  61787. + return dctl.b.rmtwkupsig;
  61788. +}
  61789. +
  61790. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  61791. +{
  61792. + glpmcfg_data_t lpmcfg;
  61793. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  61794. +
  61795. + DWC_ASSERT(!
  61796. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  61797. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  61798. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  61799. +
  61800. + return lpmcfg.b.prt_sleep_sts;
  61801. +}
  61802. +
  61803. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  61804. +{
  61805. + glpmcfg_data_t lpmcfg;
  61806. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  61807. + return lpmcfg.b.rem_wkup_en;
  61808. +}
  61809. +
  61810. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  61811. +{
  61812. + glpmcfg_data_t lpmcfg;
  61813. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  61814. + return lpmcfg.b.appl_resp;
  61815. +}
  61816. +
  61817. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  61818. +{
  61819. + glpmcfg_data_t lpmcfg;
  61820. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  61821. + lpmcfg.b.appl_resp = val;
  61822. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  61823. +}
  61824. +
  61825. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  61826. +{
  61827. + glpmcfg_data_t lpmcfg;
  61828. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  61829. + return lpmcfg.b.hsic_connect;
  61830. +}
  61831. +
  61832. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  61833. +{
  61834. + glpmcfg_data_t lpmcfg;
  61835. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  61836. + lpmcfg.b.hsic_connect = val;
  61837. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  61838. +}
  61839. +
  61840. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  61841. +{
  61842. + glpmcfg_data_t lpmcfg;
  61843. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  61844. + return lpmcfg.b.inv_sel_hsic;
  61845. +
  61846. +}
  61847. +
  61848. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  61849. +{
  61850. + glpmcfg_data_t lpmcfg;
  61851. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  61852. + lpmcfg.b.inv_sel_hsic = val;
  61853. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  61854. +}
  61855. +
  61856. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  61857. +{
  61858. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  61859. +}
  61860. +
  61861. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  61862. +{
  61863. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  61864. +}
  61865. +
  61866. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  61867. +{
  61868. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  61869. +}
  61870. +
  61871. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  61872. +{
  61873. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  61874. +}
  61875. +
  61876. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  61877. +{
  61878. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  61879. +}
  61880. +
  61881. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  61882. +{
  61883. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  61884. +}
  61885. +
  61886. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  61887. +{
  61888. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  61889. +}
  61890. +
  61891. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  61892. +{
  61893. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  61894. +}
  61895. +
  61896. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  61897. +{
  61898. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  61899. +}
  61900. +
  61901. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  61902. +{
  61903. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  61904. +}
  61905. +
  61906. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  61907. +{
  61908. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  61909. +}
  61910. +
  61911. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  61912. +{
  61913. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  61914. +}
  61915. +
  61916. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  61917. +{
  61918. + return DWC_READ_REG32(core_if->host_if->hprt0);
  61919. +
  61920. +}
  61921. +
  61922. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  61923. +{
  61924. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  61925. +}
  61926. +
  61927. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  61928. +{
  61929. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  61930. +}
  61931. +
  61932. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  61933. +{
  61934. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  61935. +}
  61936. +
  61937. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  61938. +{
  61939. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  61940. +}
  61941. +
  61942. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  61943. +{
  61944. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  61945. +}
  61946. +
  61947. +/**
  61948. + * Start the SRP timer to detect when the SRP does not complete within
  61949. + * 6 seconds.
  61950. + *
  61951. + * @param core_if the pointer to core_if strucure.
  61952. + */
  61953. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  61954. +{
  61955. + core_if->srp_timer_started = 1;
  61956. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  61957. +}
  61958. +
  61959. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  61960. +{
  61961. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  61962. + gotgctl_data_t mem;
  61963. + gotgctl_data_t val;
  61964. +
  61965. + val.d32 = DWC_READ_REG32(addr);
  61966. + if (val.b.sesreq) {
  61967. + DWC_ERROR("Session Request Already active!\n");
  61968. + return;
  61969. + }
  61970. +
  61971. + DWC_INFO("Session Request Initated\n"); //NOTICE
  61972. + mem.d32 = DWC_READ_REG32(addr);
  61973. + mem.b.sesreq = 1;
  61974. + DWC_WRITE_REG32(addr, mem.d32);
  61975. +
  61976. + /* Start the SRP timer */
  61977. + dwc_otg_pcd_start_srp_timer(core_if);
  61978. + return;
  61979. +}
  61980. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cil.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  61981. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cil.h 1970-01-01 01:00:00.000000000 +0100
  61982. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil.h 2014-03-11 16:55:38.000000000 +0100
  61983. @@ -0,0 +1,1464 @@
  61984. +/* ==========================================================================
  61985. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  61986. + * $Revision: #123 $
  61987. + * $Date: 2012/08/10 $
  61988. + * $Change: 2047372 $
  61989. + *
  61990. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  61991. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  61992. + * otherwise expressly agreed to in writing between Synopsys and you.
  61993. + *
  61994. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  61995. + * any End User Software License Agreement or Agreement for Licensed Product
  61996. + * with Synopsys or any supplement thereto. You are permitted to use and
  61997. + * redistribute this Software in source and binary forms, with or without
  61998. + * modification, provided that redistributions of source code must retain this
  61999. + * notice. You may not view, use, disclose, copy or distribute this file or
  62000. + * any information contained herein except pursuant to this license grant from
  62001. + * Synopsys. If you do not agree with this notice, including the disclaimer
  62002. + * below, then you are not authorized to use the Software.
  62003. + *
  62004. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  62005. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  62006. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  62007. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  62008. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  62009. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  62010. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  62011. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  62012. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  62013. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  62014. + * DAMAGE.
  62015. + * ========================================================================== */
  62016. +
  62017. +#if !defined(__DWC_CIL_H__)
  62018. +#define __DWC_CIL_H__
  62019. +
  62020. +#include "dwc_list.h"
  62021. +#include "dwc_otg_dbg.h"
  62022. +#include "dwc_otg_regs.h"
  62023. +
  62024. +#include "dwc_otg_core_if.h"
  62025. +#include "dwc_otg_adp.h"
  62026. +
  62027. +/**
  62028. + * @file
  62029. + * This file contains the interface to the Core Interface Layer.
  62030. + */
  62031. +
  62032. +#ifdef DWC_UTE_CFI
  62033. +
  62034. +#define MAX_DMA_DESCS_PER_EP 256
  62035. +
  62036. +/**
  62037. + * Enumeration for the data buffer mode
  62038. + */
  62039. +typedef enum _data_buffer_mode {
  62040. + BM_STANDARD = 0, /* data buffer is in normal mode */
  62041. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  62042. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  62043. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  62044. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  62045. +} data_buffer_mode_e;
  62046. +#endif //DWC_UTE_CFI
  62047. +
  62048. +/** Macros defined for DWC OTG HW Release version */
  62049. +
  62050. +#define OTG_CORE_REV_2_60a 0x4F54260A
  62051. +#define OTG_CORE_REV_2_71a 0x4F54271A
  62052. +#define OTG_CORE_REV_2_72a 0x4F54272A
  62053. +#define OTG_CORE_REV_2_80a 0x4F54280A
  62054. +#define OTG_CORE_REV_2_81a 0x4F54281A
  62055. +#define OTG_CORE_REV_2_90a 0x4F54290A
  62056. +#define OTG_CORE_REV_2_91a 0x4F54291A
  62057. +#define OTG_CORE_REV_2_92a 0x4F54292A
  62058. +#define OTG_CORE_REV_2_93a 0x4F54293A
  62059. +#define OTG_CORE_REV_2_94a 0x4F54294A
  62060. +#define OTG_CORE_REV_3_00a 0x4F54300A
  62061. +
  62062. +/**
  62063. + * Information for each ISOC packet.
  62064. + */
  62065. +typedef struct iso_pkt_info {
  62066. + uint32_t offset;
  62067. + uint32_t length;
  62068. + int32_t status;
  62069. +} iso_pkt_info_t;
  62070. +
  62071. +/**
  62072. + * The <code>dwc_ep</code> structure represents the state of a single
  62073. + * endpoint when acting in device mode. It contains the data items
  62074. + * needed for an endpoint to be activated and transfer packets.
  62075. + */
  62076. +typedef struct dwc_ep {
  62077. + /** EP number used for register address lookup */
  62078. + uint8_t num;
  62079. + /** EP direction 0 = OUT */
  62080. + unsigned is_in:1;
  62081. + /** EP active. */
  62082. + unsigned active:1;
  62083. +
  62084. + /**
  62085. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  62086. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  62087. + unsigned tx_fifo_num:4;
  62088. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  62089. + unsigned type:2;
  62090. +#define DWC_OTG_EP_TYPE_CONTROL 0
  62091. +#define DWC_OTG_EP_TYPE_ISOC 1
  62092. +#define DWC_OTG_EP_TYPE_BULK 2
  62093. +#define DWC_OTG_EP_TYPE_INTR 3
  62094. +
  62095. + /** DATA start PID for INTR and BULK EP */
  62096. + unsigned data_pid_start:1;
  62097. + /** Frame (even/odd) for ISOC EP */
  62098. + unsigned even_odd_frame:1;
  62099. + /** Max Packet bytes */
  62100. + unsigned maxpacket:11;
  62101. +
  62102. + /** Max Transfer size */
  62103. + uint32_t maxxfer;
  62104. +
  62105. + /** @name Transfer state */
  62106. + /** @{ */
  62107. +
  62108. + /**
  62109. + * Pointer to the beginning of the transfer buffer -- do not modify
  62110. + * during transfer.
  62111. + */
  62112. +
  62113. + dwc_dma_t dma_addr;
  62114. +
  62115. + dwc_dma_t dma_desc_addr;
  62116. + dwc_otg_dev_dma_desc_t *desc_addr;
  62117. +
  62118. + uint8_t *start_xfer_buff;
  62119. + /** pointer to the transfer buffer */
  62120. + uint8_t *xfer_buff;
  62121. + /** Number of bytes to transfer */
  62122. + unsigned xfer_len:19;
  62123. + /** Number of bytes transferred. */
  62124. + unsigned xfer_count:19;
  62125. + /** Sent ZLP */
  62126. + unsigned sent_zlp:1;
  62127. + /** Total len for control transfer */
  62128. + unsigned total_len:19;
  62129. +
  62130. + /** stall clear flag */
  62131. + unsigned stall_clear_flag:1;
  62132. +
  62133. + /** SETUP pkt cnt rollover flag for EP0 out*/
  62134. + unsigned stp_rollover;
  62135. +
  62136. +#ifdef DWC_UTE_CFI
  62137. + /* The buffer mode */
  62138. + data_buffer_mode_e buff_mode;
  62139. +
  62140. + /* The chain of DMA descriptors.
  62141. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  62142. + */
  62143. + dwc_otg_dma_desc_t *descs;
  62144. +
  62145. + /* The DMA address of the descriptors chain start */
  62146. + dma_addr_t descs_dma_addr;
  62147. + /** This variable stores the length of the last enqueued request */
  62148. + uint32_t cfi_req_len;
  62149. +#endif //DWC_UTE_CFI
  62150. +
  62151. +/** Max DMA Descriptor count for any EP */
  62152. +#define MAX_DMA_DESC_CNT 256
  62153. + /** Allocated DMA Desc count */
  62154. + uint32_t desc_cnt;
  62155. +
  62156. + /** bInterval */
  62157. + uint32_t bInterval;
  62158. + /** Next frame num to setup next ISOC transfer */
  62159. + uint32_t frame_num;
  62160. + /** Indicates SOF number overrun in DSTS */
  62161. + uint8_t frm_overrun;
  62162. +
  62163. +#ifdef DWC_UTE_PER_IO
  62164. + /** Next frame num for which will be setup DMA Desc */
  62165. + uint32_t xiso_frame_num;
  62166. + /** bInterval */
  62167. + uint32_t xiso_bInterval;
  62168. + /** Count of currently active transfers - shall be either 0 or 1 */
  62169. + int xiso_active_xfers;
  62170. + int xiso_queued_xfers;
  62171. +#endif
  62172. +#ifdef DWC_EN_ISOC
  62173. + /**
  62174. + * Variables specific for ISOC EPs
  62175. + *
  62176. + */
  62177. + /** DMA addresses of ISOC buffers */
  62178. + dwc_dma_t dma_addr0;
  62179. + dwc_dma_t dma_addr1;
  62180. +
  62181. + dwc_dma_t iso_dma_desc_addr;
  62182. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  62183. +
  62184. + /** pointer to the transfer buffers */
  62185. + uint8_t *xfer_buff0;
  62186. + uint8_t *xfer_buff1;
  62187. +
  62188. + /** number of ISOC Buffer is processing */
  62189. + uint32_t proc_buf_num;
  62190. + /** Interval of ISOC Buffer processing */
  62191. + uint32_t buf_proc_intrvl;
  62192. + /** Data size for regular frame */
  62193. + uint32_t data_per_frame;
  62194. +
  62195. + /* todo - pattern data support is to be implemented in the future */
  62196. + /** Data size for pattern frame */
  62197. + uint32_t data_pattern_frame;
  62198. + /** Frame number of pattern data */
  62199. + uint32_t sync_frame;
  62200. +
  62201. + /** bInterval */
  62202. + uint32_t bInterval;
  62203. + /** ISO Packet number per frame */
  62204. + uint32_t pkt_per_frm;
  62205. + /** Next frame num for which will be setup DMA Desc */
  62206. + uint32_t next_frame;
  62207. + /** Number of packets per buffer processing */
  62208. + uint32_t pkt_cnt;
  62209. + /** Info for all isoc packets */
  62210. + iso_pkt_info_t *pkt_info;
  62211. + /** current pkt number */
  62212. + uint32_t cur_pkt;
  62213. + /** current pkt number */
  62214. + uint8_t *cur_pkt_addr;
  62215. + /** current pkt number */
  62216. + uint32_t cur_pkt_dma_addr;
  62217. +#endif /* DWC_EN_ISOC */
  62218. +
  62219. +/** @} */
  62220. +} dwc_ep_t;
  62221. +
  62222. +/*
  62223. + * Reasons for halting a host channel.
  62224. + */
  62225. +typedef enum dwc_otg_halt_status {
  62226. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  62227. + DWC_OTG_HC_XFER_COMPLETE,
  62228. + DWC_OTG_HC_XFER_URB_COMPLETE,
  62229. + DWC_OTG_HC_XFER_ACK,
  62230. + DWC_OTG_HC_XFER_NAK,
  62231. + DWC_OTG_HC_XFER_NYET,
  62232. + DWC_OTG_HC_XFER_STALL,
  62233. + DWC_OTG_HC_XFER_XACT_ERR,
  62234. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  62235. + DWC_OTG_HC_XFER_BABBLE_ERR,
  62236. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  62237. + DWC_OTG_HC_XFER_AHB_ERR,
  62238. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  62239. + DWC_OTG_HC_XFER_URB_DEQUEUE
  62240. +} dwc_otg_halt_status_e;
  62241. +
  62242. +/**
  62243. + * Host channel descriptor. This structure represents the state of a single
  62244. + * host channel when acting in host mode. It contains the data items needed to
  62245. + * transfer packets to an endpoint via a host channel.
  62246. + */
  62247. +typedef struct dwc_hc {
  62248. + /** Host channel number used for register address lookup */
  62249. + uint8_t hc_num;
  62250. +
  62251. + /** Device to access */
  62252. + unsigned dev_addr:7;
  62253. +
  62254. + /** EP to access */
  62255. + unsigned ep_num:4;
  62256. +
  62257. + /** EP direction. 0: OUT, 1: IN */
  62258. + unsigned ep_is_in:1;
  62259. +
  62260. + /**
  62261. + * EP speed.
  62262. + * One of the following values:
  62263. + * - DWC_OTG_EP_SPEED_LOW
  62264. + * - DWC_OTG_EP_SPEED_FULL
  62265. + * - DWC_OTG_EP_SPEED_HIGH
  62266. + */
  62267. + unsigned speed:2;
  62268. +#define DWC_OTG_EP_SPEED_LOW 0
  62269. +#define DWC_OTG_EP_SPEED_FULL 1
  62270. +#define DWC_OTG_EP_SPEED_HIGH 2
  62271. +
  62272. + /**
  62273. + * Endpoint type.
  62274. + * One of the following values:
  62275. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  62276. + * - DWC_OTG_EP_TYPE_ISOC: 1
  62277. + * - DWC_OTG_EP_TYPE_BULK: 2
  62278. + * - DWC_OTG_EP_TYPE_INTR: 3
  62279. + */
  62280. + unsigned ep_type:2;
  62281. +
  62282. + /** Max packet size in bytes */
  62283. + unsigned max_packet:11;
  62284. +
  62285. + /**
  62286. + * PID for initial transaction.
  62287. + * 0: DATA0,<br>
  62288. + * 1: DATA2,<br>
  62289. + * 2: DATA1,<br>
  62290. + * 3: MDATA (non-Control EP),
  62291. + * SETUP (Control EP)
  62292. + */
  62293. + unsigned data_pid_start:2;
  62294. +#define DWC_OTG_HC_PID_DATA0 0
  62295. +#define DWC_OTG_HC_PID_DATA2 1
  62296. +#define DWC_OTG_HC_PID_DATA1 2
  62297. +#define DWC_OTG_HC_PID_MDATA 3
  62298. +#define DWC_OTG_HC_PID_SETUP 3
  62299. +
  62300. + /** Number of periodic transactions per (micro)frame */
  62301. + unsigned multi_count:2;
  62302. +
  62303. + /** @name Transfer State */
  62304. + /** @{ */
  62305. +
  62306. + /** Pointer to the current transfer buffer position. */
  62307. + uint8_t *xfer_buff;
  62308. + /**
  62309. + * In Buffer DMA mode this buffer will be used
  62310. + * if xfer_buff is not DWORD aligned.
  62311. + */
  62312. + dwc_dma_t align_buff;
  62313. + /** Total number of bytes to transfer. */
  62314. + uint32_t xfer_len;
  62315. + /** Number of bytes transferred so far. */
  62316. + uint32_t xfer_count;
  62317. + /** Packet count at start of transfer.*/
  62318. + uint16_t start_pkt_count;
  62319. +
  62320. + /**
  62321. + * Flag to indicate whether the transfer has been started. Set to 1 if
  62322. + * it has been started, 0 otherwise.
  62323. + */
  62324. + uint8_t xfer_started;
  62325. +
  62326. + /**
  62327. + * Set to 1 to indicate that a PING request should be issued on this
  62328. + * channel. If 0, process normally.
  62329. + */
  62330. + uint8_t do_ping;
  62331. +
  62332. + /**
  62333. + * Set to 1 to indicate that the error count for this transaction is
  62334. + * non-zero. Set to 0 if the error count is 0.
  62335. + */
  62336. + uint8_t error_state;
  62337. +
  62338. + /**
  62339. + * Set to 1 to indicate that this channel should be halted the next
  62340. + * time a request is queued for the channel. This is necessary in
  62341. + * slave mode if no request queue space is available when an attempt
  62342. + * is made to halt the channel.
  62343. + */
  62344. + uint8_t halt_on_queue;
  62345. +
  62346. + /**
  62347. + * Set to 1 if the host channel has been halted, but the core is not
  62348. + * finished flushing queued requests. Otherwise 0.
  62349. + */
  62350. + uint8_t halt_pending;
  62351. +
  62352. + /**
  62353. + * Reason for halting the host channel.
  62354. + */
  62355. + dwc_otg_halt_status_e halt_status;
  62356. +
  62357. + /*
  62358. + * Split settings for the host channel
  62359. + */
  62360. + uint8_t do_split; /**< Enable split for the channel */
  62361. + uint8_t complete_split; /**< Enable complete split */
  62362. + uint8_t hub_addr; /**< Address of high speed hub */
  62363. +
  62364. + uint8_t port_addr; /**< Port of the low/full speed device */
  62365. + /** Split transaction position
  62366. + * One of the following values:
  62367. + * - DWC_HCSPLIT_XACTPOS_MID
  62368. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  62369. + * - DWC_HCSPLIT_XACTPOS_END
  62370. + * - DWC_HCSPLIT_XACTPOS_ALL */
  62371. + uint8_t xact_pos;
  62372. +
  62373. + /** Set when the host channel does a short read. */
  62374. + uint8_t short_read;
  62375. +
  62376. + /**
  62377. + * Number of requests issued for this channel since it was assigned to
  62378. + * the current transfer (not counting PINGs).
  62379. + */
  62380. + uint8_t requests;
  62381. +
  62382. + /**
  62383. + * Queue Head for the transfer being processed by this channel.
  62384. + */
  62385. + struct dwc_otg_qh *qh;
  62386. +
  62387. + /** @} */
  62388. +
  62389. + /** Entry in list of host channels. */
  62390. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  62391. +
  62392. + /** @name Descriptor DMA support */
  62393. + /** @{ */
  62394. +
  62395. + /** Number of Transfer Descriptors */
  62396. + uint16_t ntd;
  62397. +
  62398. + /** Descriptor List DMA address */
  62399. + dwc_dma_t desc_list_addr;
  62400. +
  62401. + /** Scheduling micro-frame bitmap. */
  62402. + uint8_t schinfo;
  62403. +
  62404. + /** @} */
  62405. +} dwc_hc_t;
  62406. +
  62407. +/**
  62408. + * The following parameters may be specified when starting the module. These
  62409. + * parameters define how the DWC_otg controller should be configured.
  62410. + */
  62411. +typedef struct dwc_otg_core_params {
  62412. + int32_t opt;
  62413. +
  62414. + /**
  62415. + * Specifies the OTG capabilities. The driver will automatically
  62416. + * detect the value for this parameter if none is specified.
  62417. + * 0 - HNP and SRP capable (default)
  62418. + * 1 - SRP Only capable
  62419. + * 2 - No HNP/SRP capable
  62420. + */
  62421. + int32_t otg_cap;
  62422. +
  62423. + /**
  62424. + * Specifies whether to use slave or DMA mode for accessing the data
  62425. + * FIFOs. The driver will automatically detect the value for this
  62426. + * parameter if none is specified.
  62427. + * 0 - Slave
  62428. + * 1 - DMA (default, if available)
  62429. + */
  62430. + int32_t dma_enable;
  62431. +
  62432. + /**
  62433. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  62434. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  62435. + * will automatically detect the value for this if none is specified.
  62436. + * 0 - address DMA
  62437. + * 1 - DMA Descriptor(default, if available)
  62438. + */
  62439. + int32_t dma_desc_enable;
  62440. + /** The DMA Burst size (applicable only for External DMA
  62441. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  62442. + */
  62443. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  62444. +
  62445. + /**
  62446. + * Specifies the maximum speed of operation in host and device mode.
  62447. + * The actual speed depends on the speed of the attached device and
  62448. + * the value of phy_type. The actual speed depends on the speed of the
  62449. + * attached device.
  62450. + * 0 - High Speed (default)
  62451. + * 1 - Full Speed
  62452. + */
  62453. + int32_t speed;
  62454. + /** Specifies whether low power mode is supported when attached
  62455. + * to a Full Speed or Low Speed device in host mode.
  62456. + * 0 - Don't support low power mode (default)
  62457. + * 1 - Support low power mode
  62458. + */
  62459. + int32_t host_support_fs_ls_low_power;
  62460. +
  62461. + /** Specifies the PHY clock rate in low power mode when connected to a
  62462. + * Low Speed device in host mode. This parameter is applicable only if
  62463. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  62464. + * then defaults to 6 MHZ otherwise 48 MHZ.
  62465. + *
  62466. + * 0 - 48 MHz
  62467. + * 1 - 6 MHz
  62468. + */
  62469. + int32_t host_ls_low_power_phy_clk;
  62470. +
  62471. + /**
  62472. + * 0 - Use cC FIFO size parameters
  62473. + * 1 - Allow dynamic FIFO sizing (default)
  62474. + */
  62475. + int32_t enable_dynamic_fifo;
  62476. +
  62477. + /** Total number of 4-byte words in the data FIFO memory. This
  62478. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  62479. + * Tx FIFOs.
  62480. + * 32 to 32768 (default 8192)
  62481. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  62482. + */
  62483. + int32_t data_fifo_size;
  62484. +
  62485. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  62486. + * FIFO sizing is enabled.
  62487. + * 16 to 32768 (default 1064)
  62488. + */
  62489. + int32_t dev_rx_fifo_size;
  62490. +
  62491. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  62492. + * when dynamic FIFO sizing is enabled.
  62493. + * 16 to 32768 (default 1024)
  62494. + */
  62495. + int32_t dev_nperio_tx_fifo_size;
  62496. +
  62497. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  62498. + * mode when dynamic FIFO sizing is enabled.
  62499. + * 4 to 768 (default 256)
  62500. + */
  62501. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  62502. +
  62503. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  62504. + * FIFO sizing is enabled.
  62505. + * 16 to 32768 (default 1024)
  62506. + */
  62507. + int32_t host_rx_fifo_size;
  62508. +
  62509. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  62510. + * when Dynamic FIFO sizing is enabled in the core.
  62511. + * 16 to 32768 (default 1024)
  62512. + */
  62513. + int32_t host_nperio_tx_fifo_size;
  62514. +
  62515. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  62516. + * FIFO sizing is enabled.
  62517. + * 16 to 32768 (default 1024)
  62518. + */
  62519. + int32_t host_perio_tx_fifo_size;
  62520. +
  62521. + /** The maximum transfer size supported in bytes.
  62522. + * 2047 to 65,535 (default 65,535)
  62523. + */
  62524. + int32_t max_transfer_size;
  62525. +
  62526. + /** The maximum number of packets in a transfer.
  62527. + * 15 to 511 (default 511)
  62528. + */
  62529. + int32_t max_packet_count;
  62530. +
  62531. + /** The number of host channel registers to use.
  62532. + * 1 to 16 (default 12)
  62533. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  62534. + */
  62535. + int32_t host_channels;
  62536. +
  62537. + /** The number of endpoints in addition to EP0 available for device
  62538. + * mode operations.
  62539. + * 1 to 15 (default 6 IN and OUT)
  62540. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  62541. + * endpoints in addition to EP0.
  62542. + */
  62543. + int32_t dev_endpoints;
  62544. +
  62545. + /**
  62546. + * Specifies the type of PHY interface to use. By default, the driver
  62547. + * will automatically detect the phy_type.
  62548. + *
  62549. + * 0 - Full Speed PHY
  62550. + * 1 - UTMI+ (default)
  62551. + * 2 - ULPI
  62552. + */
  62553. + int32_t phy_type;
  62554. +
  62555. + /**
  62556. + * Specifies the UTMI+ Data Width. This parameter is
  62557. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  62558. + * PHY_TYPE, this parameter indicates the data width between
  62559. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  62560. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  62561. + * to "8 and 16 bits", meaning that the core has been
  62562. + * configured to work at either data path width.
  62563. + *
  62564. + * 8 or 16 bits (default 16)
  62565. + */
  62566. + int32_t phy_utmi_width;
  62567. +
  62568. + /**
  62569. + * Specifies whether the ULPI operates at double or single
  62570. + * data rate. This parameter is only applicable if PHY_TYPE is
  62571. + * ULPI.
  62572. + *
  62573. + * 0 - single data rate ULPI interface with 8 bit wide data
  62574. + * bus (default)
  62575. + * 1 - double data rate ULPI interface with 4 bit wide data
  62576. + * bus
  62577. + */
  62578. + int32_t phy_ulpi_ddr;
  62579. +
  62580. + /**
  62581. + * Specifies whether to use the internal or external supply to
  62582. + * drive the vbus with a ULPI phy.
  62583. + */
  62584. + int32_t phy_ulpi_ext_vbus;
  62585. +
  62586. + /**
  62587. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  62588. + * parameter is only applicable if PHY_TYPE is FS.
  62589. + * 0 - No (default)
  62590. + * 1 - Yes
  62591. + */
  62592. + int32_t i2c_enable;
  62593. +
  62594. + int32_t ulpi_fs_ls;
  62595. +
  62596. + int32_t ts_dline;
  62597. +
  62598. + /**
  62599. + * Specifies whether dedicated transmit FIFOs are
  62600. + * enabled for non periodic IN endpoints in device mode
  62601. + * 0 - No
  62602. + * 1 - Yes
  62603. + */
  62604. + int32_t en_multiple_tx_fifo;
  62605. +
  62606. + /** Number of 4-byte words in each of the Tx FIFOs in device
  62607. + * mode when dynamic FIFO sizing is enabled.
  62608. + * 4 to 768 (default 256)
  62609. + */
  62610. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  62611. +
  62612. + /** Thresholding enable flag-
  62613. + * bit 0 - enable non-ISO Tx thresholding
  62614. + * bit 1 - enable ISO Tx thresholding
  62615. + * bit 2 - enable Rx thresholding
  62616. + */
  62617. + uint32_t thr_ctl;
  62618. +
  62619. + /** Thresholding length for Tx
  62620. + * FIFOs in 32 bit DWORDs
  62621. + */
  62622. + uint32_t tx_thr_length;
  62623. +
  62624. + /** Thresholding length for Rx
  62625. + * FIFOs in 32 bit DWORDs
  62626. + */
  62627. + uint32_t rx_thr_length;
  62628. +
  62629. + /**
  62630. + * Specifies whether LPM (Link Power Management) support is enabled
  62631. + */
  62632. + int32_t lpm_enable;
  62633. +
  62634. + /** Per Transfer Interrupt
  62635. + * mode enable flag
  62636. + * 1 - Enabled
  62637. + * 0 - Disabled
  62638. + */
  62639. + int32_t pti_enable;
  62640. +
  62641. + /** Multi Processor Interrupt
  62642. + * mode enable flag
  62643. + * 1 - Enabled
  62644. + * 0 - Disabled
  62645. + */
  62646. + int32_t mpi_enable;
  62647. +
  62648. + /** IS_USB Capability
  62649. + * 1 - Enabled
  62650. + * 0 - Disabled
  62651. + */
  62652. + int32_t ic_usb_cap;
  62653. +
  62654. + /** AHB Threshold Ratio
  62655. + * 2'b00 AHB Threshold = MAC Threshold
  62656. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  62657. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  62658. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  62659. + */
  62660. + int32_t ahb_thr_ratio;
  62661. +
  62662. + /** ADP Support
  62663. + * 1 - Enabled
  62664. + * 0 - Disabled
  62665. + */
  62666. + int32_t adp_supp_enable;
  62667. +
  62668. + /** HFIR Reload Control
  62669. + * 0 - The HFIR cannot be reloaded dynamically.
  62670. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  62671. + */
  62672. + int32_t reload_ctl;
  62673. +
  62674. + /** DCFG: Enable device Out NAK
  62675. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  62676. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  62677. + */
  62678. + int32_t dev_out_nak;
  62679. +
  62680. + /** DCFG: Enable Continue on BNA
  62681. + * After receiving BNA interrupt the core disables the endpoint,when the
  62682. + * endpoint is re-enabled by the application the core starts processing
  62683. + * 0 - from the DOEPDMA descriptor
  62684. + * 1 - from the descriptor which received the BNA.
  62685. + */
  62686. + int32_t cont_on_bna;
  62687. +
  62688. + /** GAHBCFG: AHB Single Support
  62689. + * This bit when programmed supports SINGLE transfers for remainder
  62690. + * data in a transfer for DMA mode of operation.
  62691. + * 0 - in this case the remainder data will be sent using INCR burst size.
  62692. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  62693. + */
  62694. + int32_t ahb_single;
  62695. +
  62696. + /** Core Power down mode
  62697. + * 0 - No Power Down is enabled
  62698. + * 1 - Reserved
  62699. + * 2 - Complete Power Down (Hibernation)
  62700. + */
  62701. + int32_t power_down;
  62702. +
  62703. + /** OTG revision supported
  62704. + * 0 - OTG 1.3 revision
  62705. + * 1 - OTG 2.0 revision
  62706. + */
  62707. + int32_t otg_ver;
  62708. +
  62709. +} dwc_otg_core_params_t;
  62710. +
  62711. +#ifdef DEBUG
  62712. +struct dwc_otg_core_if;
  62713. +typedef struct hc_xfer_info {
  62714. + struct dwc_otg_core_if *core_if;
  62715. + dwc_hc_t *hc;
  62716. +} hc_xfer_info_t;
  62717. +#endif
  62718. +
  62719. +typedef struct ep_xfer_info {
  62720. + struct dwc_otg_core_if *core_if;
  62721. + dwc_ep_t *ep;
  62722. + uint8_t state;
  62723. +} ep_xfer_info_t;
  62724. +/*
  62725. + * Device States
  62726. + */
  62727. +typedef enum dwc_otg_lx_state {
  62728. + /** On state */
  62729. + DWC_OTG_L0,
  62730. + /** LPM sleep state*/
  62731. + DWC_OTG_L1,
  62732. + /** USB suspend state*/
  62733. + DWC_OTG_L2,
  62734. + /** Off state*/
  62735. + DWC_OTG_L3
  62736. +} dwc_otg_lx_state_e;
  62737. +
  62738. +struct dwc_otg_global_regs_backup {
  62739. + uint32_t gotgctl_local;
  62740. + uint32_t gintmsk_local;
  62741. + uint32_t gahbcfg_local;
  62742. + uint32_t gusbcfg_local;
  62743. + uint32_t grxfsiz_local;
  62744. + uint32_t gnptxfsiz_local;
  62745. +#ifdef CONFIG_USB_DWC_OTG_LPM
  62746. + uint32_t glpmcfg_local;
  62747. +#endif
  62748. + uint32_t gi2cctl_local;
  62749. + uint32_t hptxfsiz_local;
  62750. + uint32_t pcgcctl_local;
  62751. + uint32_t gdfifocfg_local;
  62752. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  62753. + uint32_t gpwrdn_local;
  62754. + uint32_t xhib_pcgcctl;
  62755. + uint32_t xhib_gpwrdn;
  62756. +};
  62757. +
  62758. +struct dwc_otg_host_regs_backup {
  62759. + uint32_t hcfg_local;
  62760. + uint32_t haintmsk_local;
  62761. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  62762. + uint32_t hprt0_local;
  62763. + uint32_t hfir_local;
  62764. +};
  62765. +
  62766. +struct dwc_otg_dev_regs_backup {
  62767. + uint32_t dcfg;
  62768. + uint32_t dctl;
  62769. + uint32_t daintmsk;
  62770. + uint32_t diepmsk;
  62771. + uint32_t doepmsk;
  62772. + uint32_t diepctl[MAX_EPS_CHANNELS];
  62773. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  62774. + uint32_t diepdma[MAX_EPS_CHANNELS];
  62775. +};
  62776. +/**
  62777. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  62778. + * the DWC_otg controller acting in either host or device mode. It
  62779. + * represents the programming view of the controller as a whole.
  62780. + */
  62781. +struct dwc_otg_core_if {
  62782. + /** Parameters that define how the core should be configured.*/
  62783. + dwc_otg_core_params_t *core_params;
  62784. +
  62785. + /** Core Global registers starting at offset 000h. */
  62786. + dwc_otg_core_global_regs_t *core_global_regs;
  62787. +
  62788. + /** Device-specific information */
  62789. + dwc_otg_dev_if_t *dev_if;
  62790. + /** Host-specific information */
  62791. + dwc_otg_host_if_t *host_if;
  62792. +
  62793. + /** Value from SNPSID register */
  62794. + uint32_t snpsid;
  62795. +
  62796. + /*
  62797. + * Set to 1 if the core PHY interface bits in USBCFG have been
  62798. + * initialized.
  62799. + */
  62800. + uint8_t phy_init_done;
  62801. +
  62802. + /*
  62803. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  62804. + */
  62805. + uint8_t srp_success;
  62806. + uint8_t srp_timer_started;
  62807. + /** Timer for SRP. If it expires before SRP is successful
  62808. + * clear the SRP. */
  62809. + dwc_timer_t *srp_timer;
  62810. +
  62811. +#ifdef DWC_DEV_SRPCAP
  62812. + /* This timer is needed to power on the hibernated host core if SRP is not
  62813. + * initiated on connected SRP capable device for limited period of time
  62814. + */
  62815. + uint8_t pwron_timer_started;
  62816. + dwc_timer_t *pwron_timer;
  62817. +#endif
  62818. + /* Common configuration information */
  62819. + /** Power and Clock Gating Control Register */
  62820. + volatile uint32_t *pcgcctl;
  62821. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  62822. +
  62823. + /** Push/pop addresses for endpoints or host channels.*/
  62824. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  62825. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  62826. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  62827. +
  62828. + /** Total RAM for FIFOs (Bytes) */
  62829. + uint16_t total_fifo_size;
  62830. + /** Size of Rx FIFO (Bytes) */
  62831. + uint16_t rx_fifo_size;
  62832. + /** Size of Non-periodic Tx FIFO (Bytes) */
  62833. + uint16_t nperio_tx_fifo_size;
  62834. +
  62835. + /** 1 if DMA is enabled, 0 otherwise. */
  62836. + uint8_t dma_enable;
  62837. +
  62838. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  62839. + uint8_t dma_desc_enable;
  62840. +
  62841. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  62842. + uint8_t pti_enh_enable;
  62843. +
  62844. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  62845. + uint8_t multiproc_int_enable;
  62846. +
  62847. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  62848. + uint8_t en_multiple_tx_fifo;
  62849. +
  62850. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  62851. + * process of being queued */
  62852. + uint8_t queuing_high_bandwidth;
  62853. +
  62854. + /** Hardware Configuration -- stored here for convenience.*/
  62855. + hwcfg1_data_t hwcfg1;
  62856. + hwcfg2_data_t hwcfg2;
  62857. + hwcfg3_data_t hwcfg3;
  62858. + hwcfg4_data_t hwcfg4;
  62859. + fifosize_data_t hptxfsiz;
  62860. +
  62861. + /** Host and Device Configuration -- stored here for convenience.*/
  62862. + hcfg_data_t hcfg;
  62863. + dcfg_data_t dcfg;
  62864. +
  62865. + /** The operational State, during transations
  62866. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  62867. + * match the core but allows the software to determine
  62868. + * transitions.
  62869. + */
  62870. + uint8_t op_state;
  62871. +
  62872. + /**
  62873. + * Set to 1 if the HCD needs to be restarted on a session request
  62874. + * interrupt. This is required if no connector ID status change has
  62875. + * occurred since the HCD was last disconnected.
  62876. + */
  62877. + uint8_t restart_hcd_on_session_req;
  62878. +
  62879. + /** HCD callbacks */
  62880. + /** A-Device is a_host */
  62881. +#define A_HOST (1)
  62882. + /** A-Device is a_suspend */
  62883. +#define A_SUSPEND (2)
  62884. + /** A-Device is a_peripherial */
  62885. +#define A_PERIPHERAL (3)
  62886. + /** B-Device is operating as a Peripheral. */
  62887. +#define B_PERIPHERAL (4)
  62888. + /** B-Device is operating as a Host. */
  62889. +#define B_HOST (5)
  62890. +
  62891. + /** HCD callbacks */
  62892. + struct dwc_otg_cil_callbacks *hcd_cb;
  62893. + /** PCD callbacks */
  62894. + struct dwc_otg_cil_callbacks *pcd_cb;
  62895. +
  62896. + /** Device mode Periodic Tx FIFO Mask */
  62897. + uint32_t p_tx_msk;
  62898. + /** Device mode Periodic Tx FIFO Mask */
  62899. + uint32_t tx_msk;
  62900. +
  62901. + /** Workqueue object used for handling several interrupts */
  62902. + dwc_workq_t *wq_otg;
  62903. +
  62904. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  62905. + dwc_timer_t *wkp_timer;
  62906. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  62907. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  62908. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  62909. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  62910. +#ifdef DEBUG
  62911. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  62912. +
  62913. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  62914. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  62915. +
  62916. + uint32_t hfnum_7_samples;
  62917. + uint64_t hfnum_7_frrem_accum;
  62918. + uint32_t hfnum_0_samples;
  62919. + uint64_t hfnum_0_frrem_accum;
  62920. + uint32_t hfnum_other_samples;
  62921. + uint64_t hfnum_other_frrem_accum;
  62922. +#endif
  62923. +
  62924. +#ifdef DWC_UTE_CFI
  62925. + uint16_t pwron_rxfsiz;
  62926. + uint16_t pwron_gnptxfsiz;
  62927. + uint16_t pwron_txfsiz[15];
  62928. +
  62929. + uint16_t init_rxfsiz;
  62930. + uint16_t init_gnptxfsiz;
  62931. + uint16_t init_txfsiz[15];
  62932. +#endif
  62933. +
  62934. + /** Lx state of device */
  62935. + dwc_otg_lx_state_e lx_state;
  62936. +
  62937. + /** Saved Core Global registers */
  62938. + struct dwc_otg_global_regs_backup *gr_backup;
  62939. + /** Saved Host registers */
  62940. + struct dwc_otg_host_regs_backup *hr_backup;
  62941. + /** Saved Device registers */
  62942. + struct dwc_otg_dev_regs_backup *dr_backup;
  62943. +
  62944. + /** Power Down Enable */
  62945. + uint32_t power_down;
  62946. +
  62947. + /** ADP support Enable */
  62948. + uint32_t adp_enable;
  62949. +
  62950. + /** ADP structure object */
  62951. + dwc_otg_adp_t adp;
  62952. +
  62953. + /** hibernation/suspend flag */
  62954. + int hibernation_suspend;
  62955. +
  62956. + /** Device mode extended hibernation flag */
  62957. + int xhib;
  62958. +
  62959. + /** OTG revision supported */
  62960. + uint32_t otg_ver;
  62961. +
  62962. + /** OTG status flag used for HNP polling */
  62963. + uint8_t otg_sts;
  62964. +
  62965. + /** Pointer to either hcd->lock or pcd->lock */
  62966. + dwc_spinlock_t *lock;
  62967. +
  62968. + /** Start predict NextEP based on Learning Queue if equal 1,
  62969. + * also used as counter of disabled NP IN EP's */
  62970. + uint8_t start_predict;
  62971. +
  62972. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  62973. + * active, 0xff otherwise */
  62974. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  62975. +
  62976. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  62977. + uint8_t first_in_nextep_seq;
  62978. +
  62979. + /** Frame number while entering to ISR - needed for ISOCs **/
  62980. + uint32_t frame_num;
  62981. +
  62982. +};
  62983. +
  62984. +#ifdef DEBUG
  62985. +/*
  62986. + * This function is called when transfer is timed out.
  62987. + */
  62988. +extern void hc_xfer_timeout(void *ptr);
  62989. +#endif
  62990. +
  62991. +/*
  62992. + * This function is called when transfer is timed out on endpoint.
  62993. + */
  62994. +extern void ep_xfer_timeout(void *ptr);
  62995. +
  62996. +/*
  62997. + * The following functions are functions for works
  62998. + * using during handling some interrupts
  62999. + */
  63000. +extern void w_conn_id_status_change(void *p);
  63001. +
  63002. +extern void w_wakeup_detected(void *p);
  63003. +
  63004. +/** Saves global register values into system memory. */
  63005. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  63006. +/** Saves device register values into system memory. */
  63007. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  63008. +/** Saves host register values into system memory. */
  63009. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  63010. +/** Restore global register values. */
  63011. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  63012. +/** Restore host register values. */
  63013. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  63014. +/** Restore device register values. */
  63015. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  63016. + int rem_wakeup);
  63017. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  63018. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  63019. + int is_host);
  63020. +
  63021. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  63022. + int restore_mode, int reset);
  63023. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  63024. + int rem_wakeup, int reset);
  63025. +
  63026. +/*
  63027. + * The following functions support initialization of the CIL driver component
  63028. + * and the DWC_otg controller.
  63029. + */
  63030. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  63031. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  63032. +
  63033. +/** @name Device CIL Functions
  63034. + * The following functions support managing the DWC_otg controller in device
  63035. + * mode.
  63036. + */
  63037. +/**@{*/
  63038. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  63039. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  63040. + uint32_t * _dest);
  63041. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  63042. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63043. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63044. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63045. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  63046. + dwc_ep_t * _ep);
  63047. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  63048. + dwc_ep_t * _ep);
  63049. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  63050. + dwc_ep_t * _ep);
  63051. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  63052. + dwc_ep_t * _ep);
  63053. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  63054. + dwc_ep_t * _ep, int _dma);
  63055. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  63056. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  63057. + dwc_ep_t * _ep);
  63058. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  63059. +
  63060. +#ifdef DWC_EN_ISOC
  63061. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  63062. + dwc_ep_t * ep);
  63063. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  63064. + dwc_ep_t * ep);
  63065. +#endif /* DWC_EN_ISOC */
  63066. +/**@}*/
  63067. +
  63068. +/** @name Host CIL Functions
  63069. + * The following functions support managing the DWC_otg controller in host
  63070. + * mode.
  63071. + */
  63072. +/**@{*/
  63073. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63074. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  63075. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  63076. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63077. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  63078. + dwc_hc_t * _hc);
  63079. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  63080. + dwc_hc_t * _hc);
  63081. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  63082. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  63083. + dwc_hc_t * _hc);
  63084. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  63085. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  63086. +
  63087. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  63088. + dwc_hc_t * hc);
  63089. +
  63090. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  63091. +
  63092. +/* Macro used to clear one channel interrupt */
  63093. +#define clear_hc_int(_hc_regs_, _intr_) \
  63094. +do { \
  63095. + hcint_data_t hcint_clear = {.d32 = 0}; \
  63096. + hcint_clear.b._intr_ = 1; \
  63097. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  63098. +} while (0)
  63099. +
  63100. +/*
  63101. + * Macro used to disable one channel interrupt. Channel interrupts are
  63102. + * disabled when the channel is halted or released by the interrupt handler.
  63103. + * There is no need to handle further interrupts of that type until the
  63104. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  63105. + * because the channel structures are cleaned up when the channel is released.
  63106. + */
  63107. +#define disable_hc_int(_hc_regs_, _intr_) \
  63108. +do { \
  63109. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  63110. + hcintmsk.b._intr_ = 1; \
  63111. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  63112. +} while (0)
  63113. +
  63114. +/**
  63115. + * This function Reads HPRT0 in preparation to modify. It keeps the
  63116. + * WC bits 0 so that if they are read as 1, they won't clear when you
  63117. + * write it back
  63118. + */
  63119. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  63120. +{
  63121. + hprt0_data_t hprt0;
  63122. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  63123. + hprt0.b.prtena = 0;
  63124. + hprt0.b.prtconndet = 0;
  63125. + hprt0.b.prtenchng = 0;
  63126. + hprt0.b.prtovrcurrchng = 0;
  63127. + return hprt0.d32;
  63128. +}
  63129. +
  63130. +/**@}*/
  63131. +
  63132. +/** @name Common CIL Functions
  63133. + * The following functions support managing the DWC_otg controller in either
  63134. + * device or host mode.
  63135. + */
  63136. +/**@{*/
  63137. +
  63138. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  63139. + uint8_t * dest, uint16_t bytes);
  63140. +
  63141. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  63142. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  63143. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  63144. +
  63145. +/**
  63146. + * This function returns the Core Interrupt register.
  63147. + */
  63148. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  63149. +{
  63150. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  63151. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  63152. +}
  63153. +
  63154. +/**
  63155. + * This function returns the OTG Interrupt register.
  63156. + */
  63157. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  63158. +{
  63159. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  63160. +}
  63161. +
  63162. +/**
  63163. + * This function reads the Device All Endpoints Interrupt register and
  63164. + * returns the IN endpoint interrupt bits.
  63165. + */
  63166. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  63167. + core_if)
  63168. +{
  63169. +
  63170. + uint32_t v;
  63171. +
  63172. + if (core_if->multiproc_int_enable) {
  63173. + v = DWC_READ_REG32(&core_if->dev_if->
  63174. + dev_global_regs->deachint) &
  63175. + DWC_READ_REG32(&core_if->
  63176. + dev_if->dev_global_regs->deachintmsk);
  63177. + } else {
  63178. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  63179. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  63180. + }
  63181. + return (v & 0xffff);
  63182. +}
  63183. +
  63184. +/**
  63185. + * This function reads the Device All Endpoints Interrupt register and
  63186. + * returns the OUT endpoint interrupt bits.
  63187. + */
  63188. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  63189. + core_if)
  63190. +{
  63191. + uint32_t v;
  63192. +
  63193. + if (core_if->multiproc_int_enable) {
  63194. + v = DWC_READ_REG32(&core_if->dev_if->
  63195. + dev_global_regs->deachint) &
  63196. + DWC_READ_REG32(&core_if->
  63197. + dev_if->dev_global_regs->deachintmsk);
  63198. + } else {
  63199. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  63200. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  63201. + }
  63202. +
  63203. + return ((v & 0xffff0000) >> 16);
  63204. +}
  63205. +
  63206. +/**
  63207. + * This function returns the Device IN EP Interrupt register
  63208. + */
  63209. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  63210. + dwc_ep_t * ep)
  63211. +{
  63212. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  63213. + uint32_t v, msk, emp;
  63214. +
  63215. + if (core_if->multiproc_int_enable) {
  63216. + msk =
  63217. + DWC_READ_REG32(&dev_if->
  63218. + dev_global_regs->diepeachintmsk[ep->num]);
  63219. + emp =
  63220. + DWC_READ_REG32(&dev_if->
  63221. + dev_global_regs->dtknqr4_fifoemptymsk);
  63222. + msk |= ((emp >> ep->num) & 0x1) << 7;
  63223. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  63224. + } else {
  63225. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  63226. + emp =
  63227. + DWC_READ_REG32(&dev_if->
  63228. + dev_global_regs->dtknqr4_fifoemptymsk);
  63229. + msk |= ((emp >> ep->num) & 0x1) << 7;
  63230. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  63231. + }
  63232. +
  63233. + return v;
  63234. +}
  63235. +
  63236. +/**
  63237. + * This function returns the Device OUT EP Interrupt register
  63238. + */
  63239. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  63240. + _core_if, dwc_ep_t * _ep)
  63241. +{
  63242. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  63243. + uint32_t v;
  63244. + doepmsk_data_t msk = {.d32 = 0 };
  63245. +
  63246. + if (_core_if->multiproc_int_enable) {
  63247. + msk.d32 =
  63248. + DWC_READ_REG32(&dev_if->
  63249. + dev_global_regs->doepeachintmsk[_ep->num]);
  63250. + if (_core_if->pti_enh_enable) {
  63251. + msk.b.pktdrpsts = 1;
  63252. + }
  63253. + v = DWC_READ_REG32(&dev_if->
  63254. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  63255. + } else {
  63256. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  63257. + if (_core_if->pti_enh_enable) {
  63258. + msk.b.pktdrpsts = 1;
  63259. + }
  63260. + v = DWC_READ_REG32(&dev_if->
  63261. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  63262. + }
  63263. + return v;
  63264. +}
  63265. +
  63266. +/**
  63267. + * This function returns the Host All Channel Interrupt register
  63268. + */
  63269. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  63270. + _core_if)
  63271. +{
  63272. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  63273. +}
  63274. +
  63275. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  63276. + _core_if, dwc_hc_t * _hc)
  63277. +{
  63278. + return (DWC_READ_REG32
  63279. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  63280. +}
  63281. +
  63282. +/**
  63283. + * This function returns the mode of the operation, host or device.
  63284. + *
  63285. + * @return 0 - Device Mode, 1 - Host Mode
  63286. + */
  63287. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  63288. +{
  63289. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  63290. +}
  63291. +
  63292. +/**@}*/
  63293. +
  63294. +/**
  63295. + * DWC_otg CIL callback structure. This structure allows the HCD and
  63296. + * PCD to register functions used for starting and stopping the PCD
  63297. + * and HCD for role change on for a DRD.
  63298. + */
  63299. +typedef struct dwc_otg_cil_callbacks {
  63300. + /** Start function for role change */
  63301. + int (*start) (void *_p);
  63302. + /** Stop Function for role change */
  63303. + int (*stop) (void *_p);
  63304. + /** Disconnect Function for role change */
  63305. + int (*disconnect) (void *_p);
  63306. + /** Resume/Remote wakeup Function */
  63307. + int (*resume_wakeup) (void *_p);
  63308. + /** Suspend function */
  63309. + int (*suspend) (void *_p);
  63310. + /** Session Start (SRP) */
  63311. + int (*session_start) (void *_p);
  63312. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63313. + /** Sleep (switch to L0 state) */
  63314. + int (*sleep) (void *_p);
  63315. +#endif
  63316. + /** Pointer passed to start() and stop() */
  63317. + void *p;
  63318. +} dwc_otg_cil_callbacks_t;
  63319. +
  63320. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  63321. + dwc_otg_cil_callbacks_t * _cb,
  63322. + void *_p);
  63323. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  63324. + dwc_otg_cil_callbacks_t * _cb,
  63325. + void *_p);
  63326. +
  63327. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  63328. +
  63329. +//////////////////////////////////////////////////////////////////////
  63330. +/** Start the HCD. Helper function for using the HCD callbacks.
  63331. + *
  63332. + * @param core_if Programming view of DWC_otg controller.
  63333. + */
  63334. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  63335. +{
  63336. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  63337. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  63338. + }
  63339. +}
  63340. +
  63341. +/** Stop the HCD. Helper function for using the HCD callbacks.
  63342. + *
  63343. + * @param core_if Programming view of DWC_otg controller.
  63344. + */
  63345. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  63346. +{
  63347. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  63348. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  63349. + }
  63350. +}
  63351. +
  63352. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  63353. + *
  63354. + * @param core_if Programming view of DWC_otg controller.
  63355. + */
  63356. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  63357. +{
  63358. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  63359. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  63360. + }
  63361. +}
  63362. +
  63363. +/** Inform the HCD the a New Session has begun. Helper function for
  63364. + * using the HCD callbacks.
  63365. + *
  63366. + * @param core_if Programming view of DWC_otg controller.
  63367. + */
  63368. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  63369. +{
  63370. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  63371. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  63372. + }
  63373. +}
  63374. +
  63375. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63376. +/**
  63377. + * Inform the HCD about LPM sleep.
  63378. + * Helper function for using the HCD callbacks.
  63379. + *
  63380. + * @param core_if Programming view of DWC_otg controller.
  63381. + */
  63382. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  63383. +{
  63384. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  63385. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  63386. + }
  63387. +}
  63388. +#endif
  63389. +
  63390. +/** Resume the HCD. Helper function for using the HCD callbacks.
  63391. + *
  63392. + * @param core_if Programming view of DWC_otg controller.
  63393. + */
  63394. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  63395. +{
  63396. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  63397. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  63398. + }
  63399. +}
  63400. +
  63401. +/** Start the PCD. Helper function for using the PCD callbacks.
  63402. + *
  63403. + * @param core_if Programming view of DWC_otg controller.
  63404. + */
  63405. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  63406. +{
  63407. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  63408. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  63409. + }
  63410. +}
  63411. +
  63412. +/** Stop the PCD. Helper function for using the PCD callbacks.
  63413. + *
  63414. + * @param core_if Programming view of DWC_otg controller.
  63415. + */
  63416. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  63417. +{
  63418. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  63419. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  63420. + }
  63421. +}
  63422. +
  63423. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  63424. + *
  63425. + * @param core_if Programming view of DWC_otg controller.
  63426. + */
  63427. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  63428. +{
  63429. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  63430. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  63431. + }
  63432. +}
  63433. +
  63434. +/** Resume the PCD. Helper function for using the PCD callbacks.
  63435. + *
  63436. + * @param core_if Programming view of DWC_otg controller.
  63437. + */
  63438. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  63439. +{
  63440. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  63441. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  63442. + }
  63443. +}
  63444. +
  63445. +//////////////////////////////////////////////////////////////////////
  63446. +
  63447. +#endif
  63448. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  63449. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 1970-01-01 01:00:00.000000000 +0100
  63450. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 2014-03-11 16:55:38.000000000 +0100
  63451. @@ -0,0 +1,1588 @@
  63452. +/* ==========================================================================
  63453. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  63454. + * $Revision: #32 $
  63455. + * $Date: 2012/08/10 $
  63456. + * $Change: 2047372 $
  63457. + *
  63458. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  63459. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  63460. + * otherwise expressly agreed to in writing between Synopsys and you.
  63461. + *
  63462. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  63463. + * any End User Software License Agreement or Agreement for Licensed Product
  63464. + * with Synopsys or any supplement thereto. You are permitted to use and
  63465. + * redistribute this Software in source and binary forms, with or without
  63466. + * modification, provided that redistributions of source code must retain this
  63467. + * notice. You may not view, use, disclose, copy or distribute this file or
  63468. + * any information contained herein except pursuant to this license grant from
  63469. + * Synopsys. If you do not agree with this notice, including the disclaimer
  63470. + * below, then you are not authorized to use the Software.
  63471. + *
  63472. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  63473. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  63474. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  63475. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  63476. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  63477. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  63478. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  63479. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  63480. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  63481. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  63482. + * DAMAGE.
  63483. + * ========================================================================== */
  63484. +
  63485. +/** @file
  63486. + *
  63487. + * The Core Interface Layer provides basic services for accessing and
  63488. + * managing the DWC_otg hardware. These services are used by both the
  63489. + * Host Controller Driver and the Peripheral Controller Driver.
  63490. + *
  63491. + * This file contains the Common Interrupt handlers.
  63492. + */
  63493. +#include "dwc_os.h"
  63494. +#include "dwc_otg_regs.h"
  63495. +#include "dwc_otg_cil.h"
  63496. +#include "dwc_otg_driver.h"
  63497. +#include "dwc_otg_pcd.h"
  63498. +#include "dwc_otg_hcd.h"
  63499. +#include "dwc_otg_mphi_fix.h"
  63500. +
  63501. +#ifdef DEBUG
  63502. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  63503. +{
  63504. + return (core_if->op_state == A_HOST ? "a_host" :
  63505. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  63506. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  63507. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  63508. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  63509. +}
  63510. +#endif
  63511. +
  63512. +/** This function will log a debug message
  63513. + *
  63514. + * @param core_if Programming view of DWC_otg controller.
  63515. + */
  63516. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  63517. +{
  63518. + gintsts_data_t gintsts;
  63519. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  63520. + dwc_otg_mode(core_if) ? "Host" : "Device");
  63521. +
  63522. + /* Clear interrupt */
  63523. + gintsts.d32 = 0;
  63524. + gintsts.b.modemismatch = 1;
  63525. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  63526. + return 1;
  63527. +}
  63528. +
  63529. +/**
  63530. + * This function handles the OTG Interrupts. It reads the OTG
  63531. + * Interrupt Register (GOTGINT) to determine what interrupt has
  63532. + * occurred.
  63533. + *
  63534. + * @param core_if Programming view of DWC_otg controller.
  63535. + */
  63536. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  63537. +{
  63538. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  63539. + gotgint_data_t gotgint;
  63540. + gotgctl_data_t gotgctl;
  63541. + gintmsk_data_t gintmsk;
  63542. + gpwrdn_data_t gpwrdn;
  63543. +
  63544. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  63545. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  63546. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  63547. + op_state_str(core_if));
  63548. +
  63549. + if (gotgint.b.sesenddet) {
  63550. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  63551. + "Session End Detected++ (%s)\n",
  63552. + op_state_str(core_if));
  63553. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  63554. +
  63555. + if (core_if->op_state == B_HOST) {
  63556. + cil_pcd_start(core_if);
  63557. + core_if->op_state = B_PERIPHERAL;
  63558. + } else {
  63559. + /* If not B_HOST and Device HNP still set. HNP
  63560. + * Did not succeed!*/
  63561. + if (gotgctl.b.devhnpen) {
  63562. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  63563. + __DWC_ERROR("Device Not Connected/Responding!\n");
  63564. + }
  63565. +
  63566. + /* If Session End Detected the B-Cable has
  63567. + * been disconnected. */
  63568. + /* Reset PCD and Gadget driver to a
  63569. + * clean state. */
  63570. + core_if->lx_state = DWC_OTG_L0;
  63571. + DWC_SPINUNLOCK(core_if->lock);
  63572. + cil_pcd_stop(core_if);
  63573. + DWC_SPINLOCK(core_if->lock);
  63574. +
  63575. + if (core_if->adp_enable) {
  63576. + if (core_if->power_down == 2) {
  63577. + gpwrdn.d32 = 0;
  63578. + gpwrdn.b.pwrdnswtch = 1;
  63579. + DWC_MODIFY_REG32(&core_if->
  63580. + core_global_regs->
  63581. + gpwrdn, gpwrdn.d32, 0);
  63582. + }
  63583. +
  63584. + gpwrdn.d32 = 0;
  63585. + gpwrdn.b.pmuintsel = 1;
  63586. + gpwrdn.b.pmuactv = 1;
  63587. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  63588. + gpwrdn, 0, gpwrdn.d32);
  63589. +
  63590. + dwc_otg_adp_sense_start(core_if);
  63591. + }
  63592. + }
  63593. +
  63594. + gotgctl.d32 = 0;
  63595. + gotgctl.b.devhnpen = 1;
  63596. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  63597. + }
  63598. + if (gotgint.b.sesreqsucstschng) {
  63599. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  63600. + "Session Reqeust Success Status Change++\n");
  63601. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  63602. + if (gotgctl.b.sesreqscs) {
  63603. +
  63604. + if ((core_if->core_params->phy_type ==
  63605. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  63606. + core_if->srp_success = 1;
  63607. + } else {
  63608. + DWC_SPINUNLOCK(core_if->lock);
  63609. + cil_pcd_resume(core_if);
  63610. + DWC_SPINLOCK(core_if->lock);
  63611. + /* Clear Session Request */
  63612. + gotgctl.d32 = 0;
  63613. + gotgctl.b.sesreq = 1;
  63614. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  63615. + gotgctl.d32, 0);
  63616. + }
  63617. + }
  63618. + }
  63619. + if (gotgint.b.hstnegsucstschng) {
  63620. + /* Print statements during the HNP interrupt handling
  63621. + * can cause it to fail.*/
  63622. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  63623. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  63624. + * this does not help*/
  63625. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  63626. + dwc_udelay(100);
  63627. + if (gotgctl.b.hstnegscs) {
  63628. + if (dwc_otg_is_host_mode(core_if)) {
  63629. + core_if->op_state = B_HOST;
  63630. + /*
  63631. + * Need to disable SOF interrupt immediately.
  63632. + * When switching from device to host, the PCD
  63633. + * interrupt handler won't handle the
  63634. + * interrupt if host mode is already set. The
  63635. + * HCD interrupt handler won't get called if
  63636. + * the HCD state is HALT. This means that the
  63637. + * interrupt does not get handled and Linux
  63638. + * complains loudly.
  63639. + */
  63640. + gintmsk.d32 = 0;
  63641. + gintmsk.b.sofintr = 1;
  63642. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  63643. + gintmsk.d32, 0);
  63644. + /* Call callback function with spin lock released */
  63645. + DWC_SPINUNLOCK(core_if->lock);
  63646. + cil_pcd_stop(core_if);
  63647. + /*
  63648. + * Initialize the Core for Host mode.
  63649. + */
  63650. + cil_hcd_start(core_if);
  63651. + DWC_SPINLOCK(core_if->lock);
  63652. + core_if->op_state = B_HOST;
  63653. + }
  63654. + } else {
  63655. + gotgctl.d32 = 0;
  63656. + gotgctl.b.hnpreq = 1;
  63657. + gotgctl.b.devhnpen = 1;
  63658. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  63659. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  63660. + __DWC_ERROR("Device Not Connected/Responding\n");
  63661. + }
  63662. + }
  63663. + if (gotgint.b.hstnegdet) {
  63664. + /* The disconnect interrupt is set at the same time as
  63665. + * Host Negotiation Detected. During the mode
  63666. + * switch all interrupts are cleared so the disconnect
  63667. + * interrupt handler will not get executed.
  63668. + */
  63669. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  63670. + "Host Negotiation Detected++ (%s)\n",
  63671. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  63672. + "Device"));
  63673. + if (dwc_otg_is_device_mode(core_if)) {
  63674. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  63675. + core_if->op_state);
  63676. + DWC_SPINUNLOCK(core_if->lock);
  63677. + cil_hcd_disconnect(core_if);
  63678. + cil_pcd_start(core_if);
  63679. + DWC_SPINLOCK(core_if->lock);
  63680. + core_if->op_state = A_PERIPHERAL;
  63681. + } else {
  63682. + /*
  63683. + * Need to disable SOF interrupt immediately. When
  63684. + * switching from device to host, the PCD interrupt
  63685. + * handler won't handle the interrupt if host mode is
  63686. + * already set. The HCD interrupt handler won't get
  63687. + * called if the HCD state is HALT. This means that
  63688. + * the interrupt does not get handled and Linux
  63689. + * complains loudly.
  63690. + */
  63691. + gintmsk.d32 = 0;
  63692. + gintmsk.b.sofintr = 1;
  63693. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  63694. + DWC_SPINUNLOCK(core_if->lock);
  63695. + cil_pcd_stop(core_if);
  63696. + cil_hcd_start(core_if);
  63697. + DWC_SPINLOCK(core_if->lock);
  63698. + core_if->op_state = A_HOST;
  63699. + }
  63700. + }
  63701. + if (gotgint.b.adevtoutchng) {
  63702. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  63703. + "A-Device Timeout Change++\n");
  63704. + }
  63705. + if (gotgint.b.debdone) {
  63706. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  63707. + }
  63708. +
  63709. + /* Clear GOTGINT */
  63710. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  63711. +
  63712. + return 1;
  63713. +}
  63714. +
  63715. +void w_conn_id_status_change(void *p)
  63716. +{
  63717. + dwc_otg_core_if_t *core_if = p;
  63718. + uint32_t count = 0;
  63719. + gotgctl_data_t gotgctl = {.d32 = 0 };
  63720. +
  63721. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  63722. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  63723. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  63724. +
  63725. + /* B-Device connector (Device Mode) */
  63726. + if (gotgctl.b.conidsts) {
  63727. + /* Wait for switch to device mode. */
  63728. + while (!dwc_otg_is_device_mode(core_if)) {
  63729. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  63730. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  63731. + "Peripheral"));
  63732. + dwc_mdelay(100);
  63733. + if (++count > 10000)
  63734. + break;
  63735. + }
  63736. + DWC_ASSERT(++count < 10000,
  63737. + "Connection id status change timed out");
  63738. + core_if->op_state = B_PERIPHERAL;
  63739. + dwc_otg_core_init(core_if);
  63740. + dwc_otg_enable_global_interrupts(core_if);
  63741. + cil_pcd_start(core_if);
  63742. + } else {
  63743. + /* A-Device connector (Host Mode) */
  63744. + while (!dwc_otg_is_host_mode(core_if)) {
  63745. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  63746. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  63747. + "Peripheral"));
  63748. + dwc_mdelay(100);
  63749. + if (++count > 10000)
  63750. + break;
  63751. + }
  63752. + DWC_ASSERT(++count < 10000,
  63753. + "Connection id status change timed out");
  63754. + core_if->op_state = A_HOST;
  63755. + /*
  63756. + * Initialize the Core for Host mode.
  63757. + */
  63758. + dwc_otg_core_init(core_if);
  63759. + dwc_otg_enable_global_interrupts(core_if);
  63760. + cil_hcd_start(core_if);
  63761. + }
  63762. +}
  63763. +
  63764. +/**
  63765. + * This function handles the Connector ID Status Change Interrupt. It
  63766. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  63767. + * is a Device to Host Mode transition or a Host Mode to Device
  63768. + * Transition.
  63769. + *
  63770. + * This only occurs when the cable is connected/removed from the PHY
  63771. + * connector.
  63772. + *
  63773. + * @param core_if Programming view of DWC_otg controller.
  63774. + */
  63775. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  63776. +{
  63777. +
  63778. + /*
  63779. + * Need to disable SOF interrupt immediately. If switching from device
  63780. + * to host, the PCD interrupt handler won't handle the interrupt if
  63781. + * host mode is already set. The HCD interrupt handler won't get
  63782. + * called if the HCD state is HALT. This means that the interrupt does
  63783. + * not get handled and Linux complains loudly.
  63784. + */
  63785. + gintmsk_data_t gintmsk = {.d32 = 0 };
  63786. + gintsts_data_t gintsts = {.d32 = 0 };
  63787. +
  63788. + gintmsk.b.sofintr = 1;
  63789. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  63790. +
  63791. + DWC_DEBUGPL(DBG_CIL,
  63792. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  63793. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  63794. +
  63795. + DWC_SPINUNLOCK(core_if->lock);
  63796. +
  63797. + /*
  63798. + * Need to schedule a work, as there are possible DELAY function calls
  63799. + * Release lock before scheduling workq as it holds spinlock during scheduling
  63800. + */
  63801. +
  63802. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  63803. + core_if, "connection id status change");
  63804. + DWC_SPINLOCK(core_if->lock);
  63805. +
  63806. + /* Set flag and clear interrupt */
  63807. + gintsts.b.conidstschng = 1;
  63808. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  63809. +
  63810. + return 1;
  63811. +}
  63812. +
  63813. +/**
  63814. + * This interrupt indicates that a device is initiating the Session
  63815. + * Request Protocol to request the host to turn on bus power so a new
  63816. + * session can begin. The handler responds by turning on bus power. If
  63817. + * the DWC_otg controller is in low power mode, the handler brings the
  63818. + * controller out of low power mode before turning on bus power.
  63819. + *
  63820. + * @param core_if Programming view of DWC_otg controller.
  63821. + */
  63822. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  63823. +{
  63824. + gintsts_data_t gintsts;
  63825. +
  63826. +#ifndef DWC_HOST_ONLY
  63827. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  63828. +
  63829. + if (dwc_otg_is_device_mode(core_if)) {
  63830. + DWC_PRINTF("SRP: Device mode\n");
  63831. + } else {
  63832. + hprt0_data_t hprt0;
  63833. + DWC_PRINTF("SRP: Host mode\n");
  63834. +
  63835. + /* Turn on the port power bit. */
  63836. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  63837. + hprt0.b.prtpwr = 1;
  63838. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  63839. +
  63840. + /* Start the Connection timer. So a message can be displayed
  63841. + * if connect does not occur within 10 seconds. */
  63842. + cil_hcd_session_start(core_if);
  63843. + }
  63844. +#endif
  63845. +
  63846. + /* Clear interrupt */
  63847. + gintsts.d32 = 0;
  63848. + gintsts.b.sessreqintr = 1;
  63849. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  63850. +
  63851. + return 1;
  63852. +}
  63853. +
  63854. +void w_wakeup_detected(void *p)
  63855. +{
  63856. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  63857. + /*
  63858. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  63859. + * so that OPT tests pass with all PHYs).
  63860. + */
  63861. + hprt0_data_t hprt0 = {.d32 = 0 };
  63862. +#if 0
  63863. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  63864. + /* Restart the Phy Clock */
  63865. + pcgcctl.b.stoppclk = 1;
  63866. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  63867. + dwc_udelay(10);
  63868. +#endif //0
  63869. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  63870. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  63871. +// dwc_mdelay(70);
  63872. + hprt0.b.prtres = 0; /* Resume */
  63873. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  63874. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  63875. + DWC_READ_REG32(core_if->host_if->hprt0));
  63876. +
  63877. + cil_hcd_resume(core_if);
  63878. +
  63879. + /** Change to L0 state*/
  63880. + core_if->lx_state = DWC_OTG_L0;
  63881. +}
  63882. +
  63883. +/**
  63884. + * This interrupt indicates that the DWC_otg controller has detected a
  63885. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  63886. + * low power mode, the handler must brings the controller out of low
  63887. + * power mode. The controller automatically begins resume
  63888. + * signaling. The handler schedules a time to stop resume signaling.
  63889. + */
  63890. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  63891. +{
  63892. + gintsts_data_t gintsts;
  63893. +
  63894. + DWC_DEBUGPL(DBG_ANY,
  63895. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  63896. +
  63897. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  63898. +
  63899. + if (dwc_otg_is_device_mode(core_if)) {
  63900. + dctl_data_t dctl = {.d32 = 0 };
  63901. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  63902. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  63903. + dsts));
  63904. + if (core_if->lx_state == DWC_OTG_L2) {
  63905. +#ifdef PARTIAL_POWER_DOWN
  63906. + if (core_if->hwcfg4.b.power_optimiz) {
  63907. + pcgcctl_data_t power = {.d32 = 0 };
  63908. +
  63909. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  63910. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  63911. + power.d32);
  63912. +
  63913. + power.b.stoppclk = 0;
  63914. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  63915. +
  63916. + power.b.pwrclmp = 0;
  63917. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  63918. +
  63919. + power.b.rstpdwnmodule = 0;
  63920. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  63921. + }
  63922. +#endif
  63923. + /* Clear the Remote Wakeup Signaling */
  63924. + dctl.b.rmtwkupsig = 1;
  63925. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  63926. + dctl, dctl.d32, 0);
  63927. +
  63928. + DWC_SPINUNLOCK(core_if->lock);
  63929. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  63930. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  63931. + }
  63932. + DWC_SPINLOCK(core_if->lock);
  63933. + } else {
  63934. + glpmcfg_data_t lpmcfg;
  63935. + lpmcfg.d32 =
  63936. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  63937. + lpmcfg.b.hird_thres &= (~(1 << 4));
  63938. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  63939. + lpmcfg.d32);
  63940. + }
  63941. + /** Change to L0 state*/
  63942. + core_if->lx_state = DWC_OTG_L0;
  63943. + } else {
  63944. + if (core_if->lx_state != DWC_OTG_L1) {
  63945. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  63946. +
  63947. + /* Restart the Phy Clock */
  63948. + pcgcctl.b.stoppclk = 1;
  63949. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  63950. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  63951. + } else {
  63952. + /** Change to L0 state*/
  63953. + core_if->lx_state = DWC_OTG_L0;
  63954. + }
  63955. + }
  63956. +
  63957. + /* Clear interrupt */
  63958. + gintsts.d32 = 0;
  63959. + gintsts.b.wkupintr = 1;
  63960. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  63961. +
  63962. + return 1;
  63963. +}
  63964. +
  63965. +/**
  63966. + * This interrupt indicates that the Wakeup Logic has detected a
  63967. + * Device disconnect.
  63968. + */
  63969. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  63970. +{
  63971. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  63972. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  63973. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  63974. +
  63975. + DWC_PRINTF("%s called\n", __FUNCTION__);
  63976. +
  63977. + if (!core_if->hibernation_suspend) {
  63978. + DWC_PRINTF("Already exited from Hibernation\n");
  63979. + return 1;
  63980. + }
  63981. +
  63982. + /* Switch on the voltage to the core */
  63983. + gpwrdn.b.pwrdnswtch = 1;
  63984. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  63985. + dwc_udelay(10);
  63986. +
  63987. + /* Reset the core */
  63988. + gpwrdn.d32 = 0;
  63989. + gpwrdn.b.pwrdnrstn = 1;
  63990. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  63991. + dwc_udelay(10);
  63992. +
  63993. + /* Disable power clamps*/
  63994. + gpwrdn.d32 = 0;
  63995. + gpwrdn.b.pwrdnclmp = 1;
  63996. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  63997. +
  63998. + /* Remove reset the core signal */
  63999. + gpwrdn.d32 = 0;
  64000. + gpwrdn.b.pwrdnrstn = 1;
  64001. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64002. + dwc_udelay(10);
  64003. +
  64004. + /* Disable PMU interrupt */
  64005. + gpwrdn.d32 = 0;
  64006. + gpwrdn.b.pmuintsel = 1;
  64007. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64008. +
  64009. + core_if->hibernation_suspend = 0;
  64010. +
  64011. + /* Disable PMU */
  64012. + gpwrdn.d32 = 0;
  64013. + gpwrdn.b.pmuactv = 1;
  64014. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64015. + dwc_udelay(10);
  64016. +
  64017. + if (gpwrdn_temp.b.idsts) {
  64018. + core_if->op_state = B_PERIPHERAL;
  64019. + dwc_otg_core_init(core_if);
  64020. + dwc_otg_enable_global_interrupts(core_if);
  64021. + cil_pcd_start(core_if);
  64022. + } else {
  64023. + core_if->op_state = A_HOST;
  64024. + dwc_otg_core_init(core_if);
  64025. + dwc_otg_enable_global_interrupts(core_if);
  64026. + cil_hcd_start(core_if);
  64027. + }
  64028. +
  64029. + return 1;
  64030. +}
  64031. +
  64032. +/**
  64033. + * This interrupt indicates that the Wakeup Logic has detected a
  64034. + * remote wakeup sequence.
  64035. + */
  64036. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  64037. +{
  64038. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64039. + DWC_DEBUGPL(DBG_ANY,
  64040. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  64041. +
  64042. + if (!core_if->hibernation_suspend) {
  64043. + DWC_PRINTF("Already exited from Hibernation\n");
  64044. + return 1;
  64045. + }
  64046. +
  64047. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64048. + if (gpwrdn.b.idsts) { // Device Mode
  64049. + if ((core_if->power_down == 2)
  64050. + && (core_if->hibernation_suspend == 1)) {
  64051. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  64052. + }
  64053. + } else {
  64054. + if ((core_if->power_down == 2)
  64055. + && (core_if->hibernation_suspend == 1)) {
  64056. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  64057. + }
  64058. + }
  64059. + return 1;
  64060. +}
  64061. +
  64062. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  64063. +{
  64064. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64065. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  64066. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  64067. +
  64068. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  64069. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64070. + if (core_if->power_down == 2) {
  64071. + if (!core_if->hibernation_suspend) {
  64072. + DWC_PRINTF("Already exited from Hibernation\n");
  64073. + return 1;
  64074. + }
  64075. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  64076. + /* Switch on the voltage to the core */
  64077. + gpwrdn.b.pwrdnswtch = 1;
  64078. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64079. + dwc_udelay(10);
  64080. +
  64081. + /* Reset the core */
  64082. + gpwrdn.d32 = 0;
  64083. + gpwrdn.b.pwrdnrstn = 1;
  64084. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64085. + dwc_udelay(10);
  64086. +
  64087. + /* Disable power clamps */
  64088. + gpwrdn.d32 = 0;
  64089. + gpwrdn.b.pwrdnclmp = 1;
  64090. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64091. +
  64092. + /* Remove reset the core signal */
  64093. + gpwrdn.d32 = 0;
  64094. + gpwrdn.b.pwrdnrstn = 1;
  64095. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64096. + dwc_udelay(10);
  64097. +
  64098. + /* Disable PMU interrupt */
  64099. + gpwrdn.d32 = 0;
  64100. + gpwrdn.b.pmuintsel = 1;
  64101. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64102. +
  64103. + /*Indicates that we are exiting from hibernation */
  64104. + core_if->hibernation_suspend = 0;
  64105. +
  64106. + /* Disable PMU */
  64107. + gpwrdn.d32 = 0;
  64108. + gpwrdn.b.pmuactv = 1;
  64109. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64110. + dwc_udelay(10);
  64111. +
  64112. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  64113. + if (gpwrdn.b.dis_vbus == 1) {
  64114. + gpwrdn.d32 = 0;
  64115. + gpwrdn.b.dis_vbus = 1;
  64116. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64117. + }
  64118. +
  64119. + if (gpwrdn_temp.b.idsts) {
  64120. + core_if->op_state = B_PERIPHERAL;
  64121. + dwc_otg_core_init(core_if);
  64122. + dwc_otg_enable_global_interrupts(core_if);
  64123. + cil_pcd_start(core_if);
  64124. + } else {
  64125. + core_if->op_state = A_HOST;
  64126. + dwc_otg_core_init(core_if);
  64127. + dwc_otg_enable_global_interrupts(core_if);
  64128. + cil_hcd_start(core_if);
  64129. + }
  64130. + }
  64131. +
  64132. + if (core_if->adp_enable) {
  64133. + uint8_t is_host = 0;
  64134. + DWC_SPINUNLOCK(core_if->lock);
  64135. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  64136. +#ifndef DWC_HOST_ONLY
  64137. + if (gpwrdn_temp.b.idsts)
  64138. + core_if->lock = otg_dev->pcd->lock;
  64139. +#endif
  64140. +#ifndef DWC_DEVICE_ONLY
  64141. + if (!gpwrdn_temp.b.idsts) {
  64142. + core_if->lock = otg_dev->hcd->lock;
  64143. + is_host = 1;
  64144. + }
  64145. +#endif
  64146. + DWC_PRINTF("RESTART ADP\n");
  64147. + if (core_if->adp.probe_enabled)
  64148. + dwc_otg_adp_probe_stop(core_if);
  64149. + if (core_if->adp.sense_enabled)
  64150. + dwc_otg_adp_sense_stop(core_if);
  64151. + if (core_if->adp.sense_timer_started)
  64152. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  64153. + if (core_if->adp.vbuson_timer_started)
  64154. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  64155. + core_if->adp.probe_timer_values[0] = -1;
  64156. + core_if->adp.probe_timer_values[1] = -1;
  64157. + core_if->adp.sense_timer_started = 0;
  64158. + core_if->adp.vbuson_timer_started = 0;
  64159. + core_if->adp.probe_counter = 0;
  64160. + core_if->adp.gpwrdn = 0;
  64161. +
  64162. + /* Disable PMU and restart ADP */
  64163. + gpwrdn_temp.d32 = 0;
  64164. + gpwrdn_temp.b.pmuactv = 1;
  64165. + gpwrdn_temp.b.pmuintsel = 1;
  64166. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64167. + DWC_PRINTF("Check point 1\n");
  64168. + dwc_mdelay(110);
  64169. + dwc_otg_adp_start(core_if, is_host);
  64170. + DWC_SPINLOCK(core_if->lock);
  64171. + }
  64172. +
  64173. +
  64174. + return 1;
  64175. +}
  64176. +
  64177. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  64178. +{
  64179. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64180. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  64181. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  64182. +
  64183. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64184. + if (core_if->power_down == 2) {
  64185. + if (!core_if->hibernation_suspend) {
  64186. + DWC_PRINTF("Already exited from Hibernation\n");
  64187. + return 1;
  64188. + }
  64189. +
  64190. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  64191. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  64192. + gpwrdn.b.bsessvld == 0) {
  64193. + /* Save gpwrdn register for further usage if stschng interrupt */
  64194. + core_if->gr_backup->gpwrdn_local =
  64195. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64196. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  64197. + return 1;
  64198. + }
  64199. +
  64200. + /* Switch on the voltage to the core */
  64201. + gpwrdn.d32 = 0;
  64202. + gpwrdn.b.pwrdnswtch = 1;
  64203. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64204. + dwc_udelay(10);
  64205. +
  64206. + /* Reset the core */
  64207. + gpwrdn.d32 = 0;
  64208. + gpwrdn.b.pwrdnrstn = 1;
  64209. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64210. + dwc_udelay(10);
  64211. +
  64212. + /* Disable power clamps */
  64213. + gpwrdn.d32 = 0;
  64214. + gpwrdn.b.pwrdnclmp = 1;
  64215. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64216. +
  64217. + /* Remove reset the core signal */
  64218. + gpwrdn.d32 = 0;
  64219. + gpwrdn.b.pwrdnrstn = 1;
  64220. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64221. + dwc_udelay(10);
  64222. +
  64223. + /* Disable PMU interrupt */
  64224. + gpwrdn.d32 = 0;
  64225. + gpwrdn.b.pmuintsel = 1;
  64226. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64227. + dwc_udelay(10);
  64228. +
  64229. + /*Indicates that we are exiting from hibernation */
  64230. + core_if->hibernation_suspend = 0;
  64231. +
  64232. + /* Disable PMU */
  64233. + gpwrdn.d32 = 0;
  64234. + gpwrdn.b.pmuactv = 1;
  64235. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64236. + dwc_udelay(10);
  64237. +
  64238. + core_if->op_state = B_PERIPHERAL;
  64239. + dwc_otg_core_init(core_if);
  64240. + dwc_otg_enable_global_interrupts(core_if);
  64241. + cil_pcd_start(core_if);
  64242. +
  64243. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  64244. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  64245. + /*
  64246. + * Initiate SRP after initial ADP probe.
  64247. + */
  64248. + dwc_otg_initiate_srp(core_if);
  64249. + }
  64250. + }
  64251. +
  64252. + return 1;
  64253. +}
  64254. +/**
  64255. + * This interrupt indicates that the Wakeup Logic has detected a
  64256. + * status change either on IDDIG or BSessVld.
  64257. + */
  64258. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  64259. +{
  64260. + int retval;
  64261. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64262. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  64263. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  64264. +
  64265. + DWC_PRINTF("%s called\n", __FUNCTION__);
  64266. +
  64267. + if (core_if->power_down == 2) {
  64268. + if (core_if->hibernation_suspend <= 0) {
  64269. + DWC_PRINTF("Already exited from Hibernation\n");
  64270. + return 1;
  64271. + } else
  64272. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  64273. +
  64274. + } else {
  64275. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  64276. + }
  64277. +
  64278. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64279. +
  64280. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  64281. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  64282. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  64283. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  64284. + }
  64285. +
  64286. + return retval;
  64287. +}
  64288. +
  64289. +/**
  64290. + * This interrupt indicates that the Wakeup Logic has detected a
  64291. + * SRP.
  64292. + */
  64293. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  64294. +{
  64295. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64296. +
  64297. + DWC_PRINTF("%s called\n", __FUNCTION__);
  64298. +
  64299. + if (!core_if->hibernation_suspend) {
  64300. + DWC_PRINTF("Already exited from Hibernation\n");
  64301. + return 1;
  64302. + }
  64303. +#ifdef DWC_DEV_SRPCAP
  64304. + if (core_if->pwron_timer_started) {
  64305. + core_if->pwron_timer_started = 0;
  64306. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  64307. + }
  64308. +#endif
  64309. +
  64310. + /* Switch on the voltage to the core */
  64311. + gpwrdn.b.pwrdnswtch = 1;
  64312. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64313. + dwc_udelay(10);
  64314. +
  64315. + /* Reset the core */
  64316. + gpwrdn.d32 = 0;
  64317. + gpwrdn.b.pwrdnrstn = 1;
  64318. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64319. + dwc_udelay(10);
  64320. +
  64321. + /* Disable power clamps */
  64322. + gpwrdn.d32 = 0;
  64323. + gpwrdn.b.pwrdnclmp = 1;
  64324. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64325. +
  64326. + /* Remove reset the core signal */
  64327. + gpwrdn.d32 = 0;
  64328. + gpwrdn.b.pwrdnrstn = 1;
  64329. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  64330. + dwc_udelay(10);
  64331. +
  64332. + /* Disable PMU interrupt */
  64333. + gpwrdn.d32 = 0;
  64334. + gpwrdn.b.pmuintsel = 1;
  64335. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64336. +
  64337. + /* Indicates that we are exiting from hibernation */
  64338. + core_if->hibernation_suspend = 0;
  64339. +
  64340. + /* Disable PMU */
  64341. + gpwrdn.d32 = 0;
  64342. + gpwrdn.b.pmuactv = 1;
  64343. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64344. + dwc_udelay(10);
  64345. +
  64346. + /* Programm Disable VBUS to 0 */
  64347. + gpwrdn.d32 = 0;
  64348. + gpwrdn.b.dis_vbus = 1;
  64349. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64350. +
  64351. + /*Initialize the core as Host */
  64352. + core_if->op_state = A_HOST;
  64353. + dwc_otg_core_init(core_if);
  64354. + dwc_otg_enable_global_interrupts(core_if);
  64355. + cil_hcd_start(core_if);
  64356. +
  64357. + return 1;
  64358. +}
  64359. +
  64360. +/** This interrupt indicates that restore command after Hibernation
  64361. + * was completed by the core. */
  64362. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  64363. +{
  64364. + pcgcctl_data_t pcgcctl;
  64365. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  64366. +
  64367. + //TODO De-assert restore signal. 8.a
  64368. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  64369. + if (pcgcctl.b.restoremode == 1) {
  64370. + gintmsk_data_t gintmsk = {.d32 = 0 };
  64371. + /*
  64372. + * If restore mode is Remote Wakeup,
  64373. + * unmask Remote Wakeup interrupt.
  64374. + */
  64375. + gintmsk.b.wkupintr = 1;
  64376. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  64377. + 0, gintmsk.d32);
  64378. + }
  64379. +
  64380. + return 1;
  64381. +}
  64382. +
  64383. +/**
  64384. + * This interrupt indicates that a device has been disconnected from
  64385. + * the root port.
  64386. + */
  64387. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  64388. +{
  64389. + gintsts_data_t gintsts;
  64390. +
  64391. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  64392. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  64393. + op_state_str(core_if));
  64394. +
  64395. +/** @todo Consolidate this if statement. */
  64396. +#ifndef DWC_HOST_ONLY
  64397. + if (core_if->op_state == B_HOST) {
  64398. + /* If in device mode Disconnect and stop the HCD, then
  64399. + * start the PCD. */
  64400. + DWC_SPINUNLOCK(core_if->lock);
  64401. + cil_hcd_disconnect(core_if);
  64402. + cil_pcd_start(core_if);
  64403. + DWC_SPINLOCK(core_if->lock);
  64404. + core_if->op_state = B_PERIPHERAL;
  64405. + } else if (dwc_otg_is_device_mode(core_if)) {
  64406. + gotgctl_data_t gotgctl = {.d32 = 0 };
  64407. + gotgctl.d32 =
  64408. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  64409. + if (gotgctl.b.hstsethnpen == 1) {
  64410. + /* Do nothing, if HNP in process the OTG
  64411. + * interrupt "Host Negotiation Detected"
  64412. + * interrupt will do the mode switch.
  64413. + */
  64414. + } else if (gotgctl.b.devhnpen == 0) {
  64415. + /* If in device mode Disconnect and stop the HCD, then
  64416. + * start the PCD. */
  64417. + DWC_SPINUNLOCK(core_if->lock);
  64418. + cil_hcd_disconnect(core_if);
  64419. + cil_pcd_start(core_if);
  64420. + DWC_SPINLOCK(core_if->lock);
  64421. + core_if->op_state = B_PERIPHERAL;
  64422. + } else {
  64423. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  64424. + }
  64425. + } else {
  64426. + if (core_if->op_state == A_HOST) {
  64427. + /* A-Cable still connected but device disconnected. */
  64428. + cil_hcd_disconnect(core_if);
  64429. + if (core_if->adp_enable) {
  64430. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  64431. + cil_hcd_stop(core_if);
  64432. + /* Enable Power Down Logic */
  64433. + gpwrdn.b.pmuintsel = 1;
  64434. + gpwrdn.b.pmuactv = 1;
  64435. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  64436. + gpwrdn, 0, gpwrdn.d32);
  64437. + dwc_otg_adp_probe_start(core_if);
  64438. +
  64439. + /* Power off the core */
  64440. + if (core_if->power_down == 2) {
  64441. + gpwrdn.d32 = 0;
  64442. + gpwrdn.b.pwrdnswtch = 1;
  64443. + DWC_MODIFY_REG32
  64444. + (&core_if->core_global_regs->gpwrdn,
  64445. + gpwrdn.d32, 0);
  64446. + }
  64447. + }
  64448. + }
  64449. + }
  64450. +#endif
  64451. + /* Change to L3(OFF) state */
  64452. + core_if->lx_state = DWC_OTG_L3;
  64453. +
  64454. + gintsts.d32 = 0;
  64455. + gintsts.b.disconnect = 1;
  64456. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64457. + return 1;
  64458. +}
  64459. +
  64460. +/**
  64461. + * This interrupt indicates that SUSPEND state has been detected on
  64462. + * the USB.
  64463. + *
  64464. + * For HNP the USB Suspend interrupt signals the change from
  64465. + * "a_peripheral" to "a_host".
  64466. + *
  64467. + * When power management is enabled the core will be put in low power
  64468. + * mode.
  64469. + */
  64470. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  64471. +{
  64472. + dsts_data_t dsts;
  64473. + gintsts_data_t gintsts;
  64474. + dcfg_data_t dcfg;
  64475. +
  64476. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  64477. +
  64478. + if (dwc_otg_is_device_mode(core_if)) {
  64479. + /* Check the Device status register to determine if the Suspend
  64480. + * state is active. */
  64481. + dsts.d32 =
  64482. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  64483. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  64484. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  64485. + "HWCFG4.power Optimize=%d\n",
  64486. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  64487. +
  64488. +#ifdef PARTIAL_POWER_DOWN
  64489. +/** @todo Add a module parameter for power management. */
  64490. +
  64491. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  64492. + pcgcctl_data_t power = {.d32 = 0 };
  64493. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  64494. +
  64495. + power.b.pwrclmp = 1;
  64496. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  64497. +
  64498. + power.b.rstpdwnmodule = 1;
  64499. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  64500. +
  64501. + power.b.stoppclk = 1;
  64502. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  64503. +
  64504. + } else {
  64505. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  64506. + }
  64507. +#endif
  64508. + /* PCD callback for suspend. Release the lock inside of callback function */
  64509. + cil_pcd_suspend(core_if);
  64510. + if (core_if->power_down == 2)
  64511. + {
  64512. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  64513. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  64514. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  64515. +
  64516. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  64517. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64518. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64519. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  64520. +
  64521. + /* Change to L2(suspend) state */
  64522. + core_if->lx_state = DWC_OTG_L2;
  64523. +
  64524. + /* Clear interrupt in gintsts */
  64525. + gintsts.d32 = 0;
  64526. + gintsts.b.usbsuspend = 1;
  64527. + DWC_WRITE_REG32(&core_if->core_global_regs->
  64528. + gintsts, gintsts.d32);
  64529. + DWC_PRINTF("Start of hibernation completed\n");
  64530. + dwc_otg_save_global_regs(core_if);
  64531. + dwc_otg_save_dev_regs(core_if);
  64532. +
  64533. + gusbcfg.d32 =
  64534. + DWC_READ_REG32(&core_if->core_global_regs->
  64535. + gusbcfg);
  64536. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  64537. + /* ULPI interface */
  64538. + /* Suspend the Phy Clock */
  64539. + pcgcctl.d32 = 0;
  64540. + pcgcctl.b.stoppclk = 1;
  64541. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  64542. + pcgcctl.d32);
  64543. + dwc_udelay(10);
  64544. + gpwrdn.b.pmuactv = 1;
  64545. + DWC_MODIFY_REG32(&core_if->
  64546. + core_global_regs->
  64547. + gpwrdn, 0, gpwrdn.d32);
  64548. + } else {
  64549. + /* UTMI+ Interface */
  64550. + gpwrdn.b.pmuactv = 1;
  64551. + DWC_MODIFY_REG32(&core_if->
  64552. + core_global_regs->
  64553. + gpwrdn, 0, gpwrdn.d32);
  64554. + dwc_udelay(10);
  64555. + pcgcctl.b.stoppclk = 1;
  64556. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  64557. + pcgcctl.d32);
  64558. + dwc_udelay(10);
  64559. + }
  64560. +
  64561. + /* Set flag to indicate that we are in hibernation */
  64562. + core_if->hibernation_suspend = 1;
  64563. + /* Enable interrupts from wake up logic */
  64564. + gpwrdn.d32 = 0;
  64565. + gpwrdn.b.pmuintsel = 1;
  64566. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  64567. + gpwrdn, 0, gpwrdn.d32);
  64568. + dwc_udelay(10);
  64569. +
  64570. + /* Unmask device mode interrupts in GPWRDN */
  64571. + gpwrdn.d32 = 0;
  64572. + gpwrdn.b.rst_det_msk = 1;
  64573. + gpwrdn.b.lnstchng_msk = 1;
  64574. + gpwrdn.b.sts_chngint_msk = 1;
  64575. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  64576. + gpwrdn, 0, gpwrdn.d32);
  64577. + dwc_udelay(10);
  64578. +
  64579. + /* Enable Power Down Clamp */
  64580. + gpwrdn.d32 = 0;
  64581. + gpwrdn.b.pwrdnclmp = 1;
  64582. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  64583. + gpwrdn, 0, gpwrdn.d32);
  64584. + dwc_udelay(10);
  64585. +
  64586. + /* Switch off VDD */
  64587. + gpwrdn.d32 = 0;
  64588. + gpwrdn.b.pwrdnswtch = 1;
  64589. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  64590. + gpwrdn, 0, gpwrdn.d32);
  64591. +
  64592. + /* Save gpwrdn register for further usage if stschng interrupt */
  64593. + core_if->gr_backup->gpwrdn_local =
  64594. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64595. + DWC_PRINTF("Hibernation completed\n");
  64596. +
  64597. + return 1;
  64598. + }
  64599. + } else if (core_if->power_down == 3) {
  64600. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64601. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  64602. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  64603. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  64604. +
  64605. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  64606. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  64607. + core_if->xhib = 1;
  64608. +
  64609. + /* Clear interrupt in gintsts */
  64610. + gintsts.d32 = 0;
  64611. + gintsts.b.usbsuspend = 1;
  64612. + DWC_WRITE_REG32(&core_if->core_global_regs->
  64613. + gintsts, gintsts.d32);
  64614. +
  64615. + dwc_otg_save_global_regs(core_if);
  64616. + dwc_otg_save_dev_regs(core_if);
  64617. +
  64618. + /* Wait for 10 PHY clocks */
  64619. + dwc_udelay(10);
  64620. +
  64621. + /* Program GPIO register while entering to xHib */
  64622. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  64623. +
  64624. + pcgcctl.b.enbl_extnd_hiber = 1;
  64625. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  64626. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  64627. +
  64628. + pcgcctl.d32 = 0;
  64629. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  64630. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  64631. +
  64632. + pcgcctl.d32 = 0;
  64633. + pcgcctl.b.extnd_hiber_switch = 1;
  64634. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64635. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  64636. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  64637. +
  64638. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  64639. +
  64640. + return 1;
  64641. + }
  64642. + }
  64643. + } else {
  64644. + if (core_if->op_state == A_PERIPHERAL) {
  64645. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  64646. + /* Clear the a_peripheral flag, back to a_host. */
  64647. + DWC_SPINUNLOCK(core_if->lock);
  64648. + cil_pcd_stop(core_if);
  64649. + cil_hcd_start(core_if);
  64650. + DWC_SPINLOCK(core_if->lock);
  64651. + core_if->op_state = A_HOST;
  64652. + }
  64653. + }
  64654. +
  64655. + /* Change to L2(suspend) state */
  64656. + core_if->lx_state = DWC_OTG_L2;
  64657. +
  64658. + /* Clear interrupt */
  64659. + gintsts.d32 = 0;
  64660. + gintsts.b.usbsuspend = 1;
  64661. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64662. +
  64663. + return 1;
  64664. +}
  64665. +
  64666. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  64667. +{
  64668. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64669. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64670. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  64671. +
  64672. + dwc_udelay(10);
  64673. +
  64674. + /* Program GPIO register while entering to xHib */
  64675. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  64676. +
  64677. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  64678. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  64679. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  64680. + dwc_udelay(10);
  64681. +
  64682. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  64683. + gpwrdn.b.restore = 1;
  64684. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  64685. + dwc_udelay(10);
  64686. +
  64687. + restore_lpm_i2c_regs(core_if);
  64688. +
  64689. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  64690. + pcgcctl.b.max_xcvrselect = 1;
  64691. + pcgcctl.b.ess_reg_restored = 0;
  64692. + pcgcctl.b.extnd_hiber_switch = 0;
  64693. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  64694. + pcgcctl.b.enbl_extnd_hiber = 1;
  64695. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  64696. +
  64697. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  64698. + gahbcfg.b.glblintrmsk = 1;
  64699. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  64700. +
  64701. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  64702. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  64703. +
  64704. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  64705. + core_if->gr_backup->gusbcfg_local);
  64706. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  64707. + core_if->dr_backup->dcfg);
  64708. +
  64709. + pcgcctl.d32 = 0;
  64710. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  64711. + pcgcctl.b.max_xcvrselect = 1;
  64712. + pcgcctl.d32 |= 0x608;
  64713. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  64714. + dwc_udelay(10);
  64715. +
  64716. + pcgcctl.d32 = 0;
  64717. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  64718. + pcgcctl.b.max_xcvrselect = 1;
  64719. + pcgcctl.b.ess_reg_restored = 1;
  64720. + pcgcctl.b.enbl_extnd_hiber = 1;
  64721. + pcgcctl.b.rstpdwnmodule = 1;
  64722. + pcgcctl.b.restoremode = 1;
  64723. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  64724. +
  64725. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  64726. +
  64727. + return 1;
  64728. +}
  64729. +
  64730. +#ifdef CONFIG_USB_DWC_OTG_LPM
  64731. +/**
  64732. + * This function hadles LPM transaction received interrupt.
  64733. + */
  64734. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  64735. +{
  64736. + glpmcfg_data_t lpmcfg;
  64737. + gintsts_data_t gintsts;
  64738. +
  64739. + if (!core_if->core_params->lpm_enable) {
  64740. + DWC_PRINTF("Unexpected LPM interrupt\n");
  64741. + }
  64742. +
  64743. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  64744. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  64745. +
  64746. + if (dwc_otg_is_host_mode(core_if)) {
  64747. + cil_hcd_sleep(core_if);
  64748. + } else {
  64749. + lpmcfg.b.hird_thres |= (1 << 4);
  64750. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  64751. + lpmcfg.d32);
  64752. + }
  64753. +
  64754. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  64755. + dwc_udelay(10);
  64756. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  64757. + if (lpmcfg.b.prt_sleep_sts) {
  64758. + /* Save the current state */
  64759. + core_if->lx_state = DWC_OTG_L1;
  64760. + }
  64761. +
  64762. + /* Clear interrupt */
  64763. + gintsts.d32 = 0;
  64764. + gintsts.b.lpmtranrcvd = 1;
  64765. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64766. + return 1;
  64767. +}
  64768. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  64769. +
  64770. +/**
  64771. + * This function returns the Core Interrupt register.
  64772. + */
  64773. +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if, gintmsk_data_t *reenable_gintmsk)
  64774. +{
  64775. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  64776. + gintsts_data_t gintsts;
  64777. + gintmsk_data_t gintmsk;
  64778. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  64779. + gintmsk_common.b.wkupintr = 1;
  64780. + gintmsk_common.b.sessreqintr = 1;
  64781. + gintmsk_common.b.conidstschng = 1;
  64782. + gintmsk_common.b.otgintr = 1;
  64783. + gintmsk_common.b.modemismatch = 1;
  64784. + gintmsk_common.b.disconnect = 1;
  64785. + gintmsk_common.b.usbsuspend = 1;
  64786. +#ifdef CONFIG_USB_DWC_OTG_LPM
  64787. + gintmsk_common.b.lpmtranrcvd = 1;
  64788. +#endif
  64789. + gintmsk_common.b.restoredone = 1;
  64790. + if(dwc_otg_is_device_mode(core_if))
  64791. + {
  64792. + /** @todo: The port interrupt occurs while in device
  64793. + * mode. Added code to CIL to clear the interrupt for now!
  64794. + */
  64795. + gintmsk_common.b.portintr = 1;
  64796. + }
  64797. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  64798. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  64799. + {
  64800. + unsigned long flags;
  64801. +
  64802. + // Re-enable the saved interrupts
  64803. + local_irq_save(flags);
  64804. + local_fiq_disable();
  64805. + gintmsk.d32 |= gintmsk_common.d32;
  64806. + gintsts_saved.d32 &= ~gintmsk_common.d32;
  64807. + reenable_gintmsk->d32 = gintmsk.d32;
  64808. + local_irq_restore(flags);
  64809. + }
  64810. +
  64811. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  64812. +
  64813. +#ifdef DEBUG
  64814. + /* if any common interrupts set */
  64815. + if (gintsts.d32 & gintmsk_common.d32) {
  64816. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  64817. + gintsts.d32, gintmsk.d32);
  64818. + }
  64819. +#endif
  64820. + if (!fiq_fix_enable){
  64821. + if (gahbcfg.b.glblintrmsk)
  64822. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  64823. + else
  64824. + return 0;
  64825. + }
  64826. + else {
  64827. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  64828. + }
  64829. +
  64830. +}
  64831. +
  64832. +/* MACRO for clearing interupt bits in GPWRDN register */
  64833. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  64834. +do { \
  64835. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  64836. + gpwrdn.b.__intr = 1; \
  64837. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  64838. + 0, gpwrdn.d32); \
  64839. +} while (0)
  64840. +
  64841. +/**
  64842. + * Common interrupt handler.
  64843. + *
  64844. + * The common interrupts are those that occur in both Host and Device mode.
  64845. + * This handler handles the following interrupts:
  64846. + * - Mode Mismatch Interrupt
  64847. + * - Disconnect Interrupt
  64848. + * - OTG Interrupt
  64849. + * - Connector ID Status Change Interrupt
  64850. + * - Session Request Interrupt.
  64851. + * - Resume / Remote Wakeup Detected Interrupt.
  64852. + * - LPM Transaction Received Interrupt
  64853. + * - ADP Transaction Received Interrupt
  64854. + *
  64855. + */
  64856. +int32_t dwc_otg_handle_common_intr(void *dev)
  64857. +{
  64858. + int retval = 0;
  64859. + gintsts_data_t gintsts;
  64860. + gintmsk_data_t reenable_gintmsk;
  64861. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64862. + dwc_otg_device_t *otg_dev = dev;
  64863. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  64864. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  64865. + if (dwc_otg_is_device_mode(core_if))
  64866. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  64867. +
  64868. + if (core_if->lock)
  64869. + DWC_SPINLOCK(core_if->lock);
  64870. +
  64871. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  64872. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  64873. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  64874. + core_if->xhib = 2;
  64875. + if (core_if->lock)
  64876. + DWC_SPINUNLOCK(core_if->lock);
  64877. +
  64878. + return retval;
  64879. + }
  64880. +
  64881. + if (core_if->hibernation_suspend <= 0) {
  64882. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &reenable_gintmsk);
  64883. +
  64884. + if (gintsts.b.modemismatch) {
  64885. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  64886. + }
  64887. + if (gintsts.b.otgintr) {
  64888. + retval |= dwc_otg_handle_otg_intr(core_if);
  64889. + }
  64890. + if (gintsts.b.conidstschng) {
  64891. + retval |=
  64892. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  64893. + }
  64894. + if (gintsts.b.disconnect) {
  64895. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  64896. + }
  64897. + if (gintsts.b.sessreqintr) {
  64898. + retval |= dwc_otg_handle_session_req_intr(core_if);
  64899. + }
  64900. + if (gintsts.b.wkupintr) {
  64901. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  64902. + }
  64903. + if (gintsts.b.usbsuspend) {
  64904. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  64905. + }
  64906. +#ifdef CONFIG_USB_DWC_OTG_LPM
  64907. + if (gintsts.b.lpmtranrcvd) {
  64908. + retval |= dwc_otg_handle_lpm_intr(core_if);
  64909. + }
  64910. +#endif
  64911. + if (gintsts.b.restoredone) {
  64912. + gintsts.d32 = 0;
  64913. + if (core_if->power_down == 2)
  64914. + core_if->hibernation_suspend = -1;
  64915. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  64916. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  64917. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  64918. + dctl_data_t dctl = {.d32 = 0 };
  64919. +
  64920. + DWC_WRITE_REG32(&core_if->core_global_regs->
  64921. + gintsts, 0xFFFFFFFF);
  64922. +
  64923. + DWC_DEBUGPL(DBG_ANY,
  64924. + "RESTORE DONE generated\n");
  64925. +
  64926. + gpwrdn.b.restore = 1;
  64927. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  64928. + dwc_udelay(10);
  64929. +
  64930. + pcgcctl.b.rstpdwnmodule = 1;
  64931. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  64932. +
  64933. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  64934. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  64935. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  64936. + dwc_udelay(50);
  64937. +
  64938. + dctl.b.pwronprgdone = 1;
  64939. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  64940. + dwc_udelay(10);
  64941. +
  64942. + dwc_otg_restore_global_regs(core_if);
  64943. + dwc_otg_restore_dev_regs(core_if, 0);
  64944. +
  64945. + dctl.d32 = 0;
  64946. + dctl.b.pwronprgdone = 1;
  64947. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  64948. + dwc_udelay(10);
  64949. +
  64950. + pcgcctl.d32 = 0;
  64951. + pcgcctl.b.enbl_extnd_hiber = 1;
  64952. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  64953. +
  64954. + /* The core will be in ON STATE */
  64955. + core_if->lx_state = DWC_OTG_L0;
  64956. + core_if->xhib = 0;
  64957. +
  64958. + DWC_SPINUNLOCK(core_if->lock);
  64959. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  64960. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  64961. + }
  64962. + DWC_SPINLOCK(core_if->lock);
  64963. +
  64964. + }
  64965. +
  64966. + gintsts.b.restoredone = 1;
  64967. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  64968. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  64969. + retval |= 1;
  64970. + }
  64971. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  64972. + /* The port interrupt occurs while in device mode with HPRT0
  64973. + * Port Enable/Disable.
  64974. + */
  64975. + gintsts.d32 = 0;
  64976. + gintsts.b.portintr = 1;
  64977. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  64978. + retval |= 1;
  64979. + reenable_gintmsk.b.portintr = 1;
  64980. +
  64981. + }
  64982. +
  64983. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, reenable_gintmsk.d32);
  64984. +
  64985. + } else {
  64986. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  64987. +
  64988. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  64989. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  64990. + if (gpwrdn.b.linestate == 0) {
  64991. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  64992. + } else {
  64993. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  64994. + }
  64995. +
  64996. + retval |= 1;
  64997. + }
  64998. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  64999. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  65000. + /* remote wakeup from hibernation */
  65001. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  65002. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  65003. + } else {
  65004. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  65005. + }
  65006. + retval |= 1;
  65007. + }
  65008. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  65009. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  65010. + if (gpwrdn.b.linestate == 0) {
  65011. + DWC_PRINTF("Reset detected\n");
  65012. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  65013. + }
  65014. + }
  65015. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  65016. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  65017. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  65018. + retval |= 1;
  65019. + }
  65020. + }
  65021. + /* Handle ADP interrupt here */
  65022. + if (gpwrdn.b.adp_int) {
  65023. + DWC_PRINTF("ADP interrupt\n");
  65024. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  65025. + dwc_otg_adp_handle_intr(core_if);
  65026. + retval |= 1;
  65027. + }
  65028. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  65029. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  65030. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  65031. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  65032. +
  65033. + retval |= 1;
  65034. + }
  65035. + if (core_if->lock)
  65036. + DWC_SPINUNLOCK(core_if->lock);
  65037. +
  65038. + return retval;
  65039. +}
  65040. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_core_if.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  65041. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 1970-01-01 01:00:00.000000000 +0100
  65042. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 2014-03-11 16:55:38.000000000 +0100
  65043. @@ -0,0 +1,705 @@
  65044. +/* ==========================================================================
  65045. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  65046. + * $Revision: #13 $
  65047. + * $Date: 2012/08/10 $
  65048. + * $Change: 2047372 $
  65049. + *
  65050. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  65051. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  65052. + * otherwise expressly agreed to in writing between Synopsys and you.
  65053. + *
  65054. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  65055. + * any End User Software License Agreement or Agreement for Licensed Product
  65056. + * with Synopsys or any supplement thereto. You are permitted to use and
  65057. + * redistribute this Software in source and binary forms, with or without
  65058. + * modification, provided that redistributions of source code must retain this
  65059. + * notice. You may not view, use, disclose, copy or distribute this file or
  65060. + * any information contained herein except pursuant to this license grant from
  65061. + * Synopsys. If you do not agree with this notice, including the disclaimer
  65062. + * below, then you are not authorized to use the Software.
  65063. + *
  65064. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  65065. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65066. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  65067. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  65068. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  65069. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  65070. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  65071. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  65072. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  65073. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  65074. + * DAMAGE.
  65075. + * ========================================================================== */
  65076. +#if !defined(__DWC_CORE_IF_H__)
  65077. +#define __DWC_CORE_IF_H__
  65078. +
  65079. +#include "dwc_os.h"
  65080. +
  65081. +/** @file
  65082. + * This file defines DWC_OTG Core API
  65083. + */
  65084. +
  65085. +struct dwc_otg_core_if;
  65086. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  65087. +
  65088. +/** Maximum number of Periodic FIFOs */
  65089. +#define MAX_PERIO_FIFOS 15
  65090. +/** Maximum number of Periodic FIFOs */
  65091. +#define MAX_TX_FIFOS 15
  65092. +
  65093. +/** Maximum number of Endpoints/HostChannels */
  65094. +#define MAX_EPS_CHANNELS 16
  65095. +
  65096. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  65097. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  65098. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  65099. +
  65100. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  65101. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  65102. +
  65103. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  65104. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  65105. +
  65106. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  65107. +
  65108. +/** This function should be called on every hardware interrupt. */
  65109. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  65110. +
  65111. +/** @name OTG Core Parameters */
  65112. +/** @{ */
  65113. +
  65114. +/**
  65115. + * Specifies the OTG capabilities. The driver will automatically
  65116. + * detect the value for this parameter if none is specified.
  65117. + * 0 - HNP and SRP capable (default)
  65118. + * 1 - SRP Only capable
  65119. + * 2 - No HNP/SRP capable
  65120. + */
  65121. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  65122. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  65123. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  65124. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  65125. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  65126. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  65127. +
  65128. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  65129. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  65130. +#define dwc_param_opt_default 1
  65131. +
  65132. +/**
  65133. + * Specifies whether to use slave or DMA mode for accessing the data
  65134. + * FIFOs. The driver will automatically detect the value for this
  65135. + * parameter if none is specified.
  65136. + * 0 - Slave
  65137. + * 1 - DMA (default, if available)
  65138. + */
  65139. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  65140. + int32_t val);
  65141. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  65142. +#define dwc_param_dma_enable_default 1
  65143. +
  65144. +/**
  65145. + * When DMA mode is enabled specifies whether to use
  65146. + * address DMA or DMA Descritor mode for accessing the data
  65147. + * FIFOs in device mode. The driver will automatically detect
  65148. + * the value for this parameter if none is specified.
  65149. + * 0 - address DMA
  65150. + * 1 - DMA Descriptor(default, if available)
  65151. + */
  65152. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  65153. + int32_t val);
  65154. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  65155. +//#define dwc_param_dma_desc_enable_default 1
  65156. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  65157. +
  65158. +/** The DMA Burst size (applicable only for External DMA
  65159. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  65160. + */
  65161. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  65162. + int32_t val);
  65163. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  65164. +#define dwc_param_dma_burst_size_default 32
  65165. +
  65166. +/**
  65167. + * Specifies the maximum speed of operation in host and device mode.
  65168. + * The actual speed depends on the speed of the attached device and
  65169. + * the value of phy_type. The actual speed depends on the speed of the
  65170. + * attached device.
  65171. + * 0 - High Speed (default)
  65172. + * 1 - Full Speed
  65173. + */
  65174. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  65175. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  65176. +#define dwc_param_speed_default 0
  65177. +#define DWC_SPEED_PARAM_HIGH 0
  65178. +#define DWC_SPEED_PARAM_FULL 1
  65179. +
  65180. +/** Specifies whether low power mode is supported when attached
  65181. + * to a Full Speed or Low Speed device in host mode.
  65182. + * 0 - Don't support low power mode (default)
  65183. + * 1 - Support low power mode
  65184. + */
  65185. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  65186. + core_if, int32_t val);
  65187. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  65188. + * core_if);
  65189. +#define dwc_param_host_support_fs_ls_low_power_default 0
  65190. +
  65191. +/** Specifies the PHY clock rate in low power mode when connected to a
  65192. + * Low Speed device in host mode. This parameter is applicable only if
  65193. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  65194. + * then defaults to 6 MHZ otherwise 48 MHZ.
  65195. + *
  65196. + * 0 - 48 MHz
  65197. + * 1 - 6 MHz
  65198. + */
  65199. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  65200. + core_if, int32_t val);
  65201. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  65202. + core_if);
  65203. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  65204. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  65205. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  65206. +
  65207. +/**
  65208. + * 0 - Use cC FIFO size parameters
  65209. + * 1 - Allow dynamic FIFO sizing (default)
  65210. + */
  65211. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  65212. + int32_t val);
  65213. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  65214. + core_if);
  65215. +#define dwc_param_enable_dynamic_fifo_default 1
  65216. +
  65217. +/** Total number of 4-byte words in the data FIFO memory. This
  65218. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  65219. + * Tx FIFOs.
  65220. + * 32 to 32768 (default 8192)
  65221. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  65222. + */
  65223. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  65224. + int32_t val);
  65225. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  65226. +//#define dwc_param_data_fifo_size_default 8192
  65227. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  65228. +
  65229. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  65230. + * FIFO sizing is enabled.
  65231. + * 16 to 32768 (default 1064)
  65232. + */
  65233. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  65234. + int32_t val);
  65235. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  65236. +#define dwc_param_dev_rx_fifo_size_default 1064
  65237. +
  65238. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  65239. + * when dynamic FIFO sizing is enabled.
  65240. + * 16 to 32768 (default 1024)
  65241. + */
  65242. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65243. + core_if, int32_t val);
  65244. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65245. + core_if);
  65246. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  65247. +
  65248. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  65249. + * mode when dynamic FIFO sizing is enabled.
  65250. + * 4 to 768 (default 256)
  65251. + */
  65252. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  65253. + int32_t val, int fifo_num);
  65254. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  65255. + core_if, int fifo_num);
  65256. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  65257. +
  65258. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  65259. + * FIFO sizing is enabled.
  65260. + * 16 to 32768 (default 1024)
  65261. + */
  65262. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  65263. + int32_t val);
  65264. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  65265. +//#define dwc_param_host_rx_fifo_size_default 1024
  65266. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  65267. +
  65268. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  65269. + * when Dynamic FIFO sizing is enabled in the core.
  65270. + * 16 to 32768 (default 1024)
  65271. + */
  65272. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65273. + core_if, int32_t val);
  65274. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  65275. + core_if);
  65276. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  65277. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  65278. +
  65279. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  65280. + * FIFO sizing is enabled.
  65281. + * 16 to 32768 (default 1024)
  65282. + */
  65283. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  65284. + core_if, int32_t val);
  65285. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  65286. + core_if);
  65287. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  65288. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  65289. +
  65290. +/** The maximum transfer size supported in bytes.
  65291. + * 2047 to 65,535 (default 65,535)
  65292. + */
  65293. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  65294. + int32_t val);
  65295. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  65296. +#define dwc_param_max_transfer_size_default 65535
  65297. +
  65298. +/** The maximum number of packets in a transfer.
  65299. + * 15 to 511 (default 511)
  65300. + */
  65301. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  65302. + int32_t val);
  65303. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  65304. +#define dwc_param_max_packet_count_default 511
  65305. +
  65306. +/** The number of host channel registers to use.
  65307. + * 1 to 16 (default 12)
  65308. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  65309. + */
  65310. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  65311. + int32_t val);
  65312. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  65313. +//#define dwc_param_host_channels_default 12
  65314. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  65315. +
  65316. +/** The number of endpoints in addition to EP0 available for device
  65317. + * mode operations.
  65318. + * 1 to 15 (default 6 IN and OUT)
  65319. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  65320. + * endpoints in addition to EP0.
  65321. + */
  65322. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  65323. + int32_t val);
  65324. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  65325. +#define dwc_param_dev_endpoints_default 6
  65326. +
  65327. +/**
  65328. + * Specifies the type of PHY interface to use. By default, the driver
  65329. + * will automatically detect the phy_type.
  65330. + *
  65331. + * 0 - Full Speed PHY
  65332. + * 1 - UTMI+ (default)
  65333. + * 2 - ULPI
  65334. + */
  65335. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  65336. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  65337. +#define DWC_PHY_TYPE_PARAM_FS 0
  65338. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  65339. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  65340. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  65341. +
  65342. +/**
  65343. + * Specifies the UTMI+ Data Width. This parameter is
  65344. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  65345. + * PHY_TYPE, this parameter indicates the data width between
  65346. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  65347. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  65348. + * to "8 and 16 bits", meaning that the core has been
  65349. + * configured to work at either data path width.
  65350. + *
  65351. + * 8 or 16 bits (default 16)
  65352. + */
  65353. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  65354. + int32_t val);
  65355. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  65356. +//#define dwc_param_phy_utmi_width_default 16
  65357. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  65358. +
  65359. +/**
  65360. + * Specifies whether the ULPI operates at double or single
  65361. + * data rate. This parameter is only applicable if PHY_TYPE is
  65362. + * ULPI.
  65363. + *
  65364. + * 0 - single data rate ULPI interface with 8 bit wide data
  65365. + * bus (default)
  65366. + * 1 - double data rate ULPI interface with 4 bit wide data
  65367. + * bus
  65368. + */
  65369. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  65370. + int32_t val);
  65371. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  65372. +#define dwc_param_phy_ulpi_ddr_default 0
  65373. +
  65374. +/**
  65375. + * Specifies whether to use the internal or external supply to
  65376. + * drive the vbus with a ULPI phy.
  65377. + */
  65378. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  65379. + int32_t val);
  65380. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  65381. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  65382. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  65383. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  65384. +
  65385. +/**
  65386. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  65387. + * parameter is only applicable if PHY_TYPE is FS.
  65388. + * 0 - No (default)
  65389. + * 1 - Yes
  65390. + */
  65391. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  65392. + int32_t val);
  65393. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  65394. +#define dwc_param_i2c_enable_default 0
  65395. +
  65396. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  65397. + int32_t val);
  65398. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  65399. +#define dwc_param_ulpi_fs_ls_default 0
  65400. +
  65401. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  65402. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  65403. +#define dwc_param_ts_dline_default 0
  65404. +
  65405. +/**
  65406. + * Specifies whether dedicated transmit FIFOs are
  65407. + * enabled for non periodic IN endpoints in device mode
  65408. + * 0 - No
  65409. + * 1 - Yes
  65410. + */
  65411. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  65412. + int32_t val);
  65413. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  65414. + core_if);
  65415. +#define dwc_param_en_multiple_tx_fifo_default 1
  65416. +
  65417. +/** Number of 4-byte words in each of the Tx FIFOs in device
  65418. + * mode when dynamic FIFO sizing is enabled.
  65419. + * 4 to 768 (default 256)
  65420. + */
  65421. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  65422. + int fifo_num, int32_t val);
  65423. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  65424. + int fifo_num);
  65425. +#define dwc_param_dev_tx_fifo_size_default 768
  65426. +
  65427. +/** Thresholding enable flag-
  65428. + * bit 0 - enable non-ISO Tx thresholding
  65429. + * bit 1 - enable ISO Tx thresholding
  65430. + * bit 2 - enable Rx thresholding
  65431. + */
  65432. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  65433. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  65434. +#define dwc_param_thr_ctl_default 0
  65435. +
  65436. +/** Thresholding length for Tx
  65437. + * FIFOs in 32 bit DWORDs
  65438. + */
  65439. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  65440. + int32_t val);
  65441. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  65442. +#define dwc_param_tx_thr_length_default 64
  65443. +
  65444. +/** Thresholding length for Rx
  65445. + * FIFOs in 32 bit DWORDs
  65446. + */
  65447. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  65448. + int32_t val);
  65449. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  65450. +#define dwc_param_rx_thr_length_default 64
  65451. +
  65452. +/**
  65453. + * Specifies whether LPM (Link Power Management) support is enabled
  65454. + */
  65455. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  65456. + int32_t val);
  65457. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  65458. +#define dwc_param_lpm_enable_default 1
  65459. +
  65460. +/**
  65461. + * Specifies whether PTI enhancement is enabled
  65462. + */
  65463. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  65464. + int32_t val);
  65465. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  65466. +#define dwc_param_pti_enable_default 0
  65467. +
  65468. +/**
  65469. + * Specifies whether MPI enhancement is enabled
  65470. + */
  65471. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  65472. + int32_t val);
  65473. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  65474. +#define dwc_param_mpi_enable_default 0
  65475. +
  65476. +/**
  65477. + * Specifies whether ADP capability is enabled
  65478. + */
  65479. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  65480. + int32_t val);
  65481. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  65482. +#define dwc_param_adp_enable_default 0
  65483. +
  65484. +/**
  65485. + * Specifies whether IC_USB capability is enabled
  65486. + */
  65487. +
  65488. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  65489. + int32_t val);
  65490. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  65491. +#define dwc_param_ic_usb_cap_default 0
  65492. +
  65493. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  65494. + int32_t val);
  65495. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  65496. +#define dwc_param_ahb_thr_ratio_default 0
  65497. +
  65498. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  65499. + int32_t val);
  65500. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  65501. +#define dwc_param_power_down_default 0
  65502. +
  65503. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  65504. + int32_t val);
  65505. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  65506. +#define dwc_param_reload_ctl_default 0
  65507. +
  65508. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  65509. + int32_t val);
  65510. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  65511. +#define dwc_param_dev_out_nak_default 0
  65512. +
  65513. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  65514. + int32_t val);
  65515. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  65516. +#define dwc_param_cont_on_bna_default 0
  65517. +
  65518. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  65519. + int32_t val);
  65520. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  65521. +#define dwc_param_ahb_single_default 0
  65522. +
  65523. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  65524. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  65525. +#define dwc_param_otg_ver_default 0
  65526. +
  65527. +/** @} */
  65528. +
  65529. +/** @name Access to registers and bit-fields */
  65530. +
  65531. +/**
  65532. + * Dump core registers and SPRAM
  65533. + */
  65534. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  65535. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  65536. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  65537. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  65538. +
  65539. +/**
  65540. + * Get host negotiation status.
  65541. + */
  65542. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  65543. +
  65544. +/**
  65545. + * Get srp status
  65546. + */
  65547. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  65548. +
  65549. +/**
  65550. + * Set hnpreq bit in the GOTGCTL register.
  65551. + */
  65552. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  65553. +
  65554. +/**
  65555. + * Get Content of SNPSID register.
  65556. + */
  65557. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  65558. +
  65559. +/**
  65560. + * Get current mode.
  65561. + * Returns 0 if in device mode, and 1 if in host mode.
  65562. + */
  65563. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  65564. +
  65565. +/**
  65566. + * Get value of hnpcapable field in the GUSBCFG register
  65567. + */
  65568. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  65569. +/**
  65570. + * Set value of hnpcapable field in the GUSBCFG register
  65571. + */
  65572. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  65573. +
  65574. +/**
  65575. + * Get value of srpcapable field in the GUSBCFG register
  65576. + */
  65577. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  65578. +/**
  65579. + * Set value of srpcapable field in the GUSBCFG register
  65580. + */
  65581. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  65582. +
  65583. +/**
  65584. + * Get value of devspeed field in the DCFG register
  65585. + */
  65586. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  65587. +/**
  65588. + * Set value of devspeed field in the DCFG register
  65589. + */
  65590. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  65591. +
  65592. +/**
  65593. + * Get the value of busconnected field from the HPRT0 register
  65594. + */
  65595. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  65596. +
  65597. +/**
  65598. + * Gets the device enumeration Speed.
  65599. + */
  65600. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  65601. +
  65602. +/**
  65603. + * Get value of prtpwr field from the HPRT0 register
  65604. + */
  65605. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  65606. +
  65607. +/**
  65608. + * Get value of flag indicating core state - hibernated or not
  65609. + */
  65610. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  65611. +
  65612. +/**
  65613. + * Set value of prtpwr field from the HPRT0 register
  65614. + */
  65615. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  65616. +
  65617. +/**
  65618. + * Get value of prtsusp field from the HPRT0 regsiter
  65619. + */
  65620. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  65621. +/**
  65622. + * Set value of prtpwr field from the HPRT0 register
  65623. + */
  65624. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  65625. +
  65626. +/**
  65627. + * Get value of ModeChTimEn field from the HCFG regsiter
  65628. + */
  65629. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  65630. +/**
  65631. + * Set value of ModeChTimEn field from the HCFG regsiter
  65632. + */
  65633. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  65634. +
  65635. +/**
  65636. + * Get value of Fram Interval field from the HFIR regsiter
  65637. + */
  65638. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  65639. +/**
  65640. + * Set value of Frame Interval field from the HFIR regsiter
  65641. + */
  65642. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  65643. +
  65644. +/**
  65645. + * Set value of prtres field from the HPRT0 register
  65646. + *FIXME Remove?
  65647. + */
  65648. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  65649. +
  65650. +/**
  65651. + * Get value of rmtwkupsig bit in DCTL register
  65652. + */
  65653. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  65654. +
  65655. +/**
  65656. + * Get value of prt_sleep_sts field from the GLPMCFG register
  65657. + */
  65658. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  65659. +
  65660. +/**
  65661. + * Get value of rem_wkup_en field from the GLPMCFG register
  65662. + */
  65663. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  65664. +
  65665. +/**
  65666. + * Get value of appl_resp field from the GLPMCFG register
  65667. + */
  65668. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  65669. +/**
  65670. + * Set value of appl_resp field from the GLPMCFG register
  65671. + */
  65672. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  65673. +
  65674. +/**
  65675. + * Get value of hsic_connect field from the GLPMCFG register
  65676. + */
  65677. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  65678. +/**
  65679. + * Set value of hsic_connect field from the GLPMCFG register
  65680. + */
  65681. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  65682. +
  65683. +/**
  65684. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  65685. + */
  65686. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  65687. +/**
  65688. + * Set value of inv_sel_hsic field from the GLPMFG register.
  65689. + */
  65690. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  65691. +
  65692. +/*
  65693. + * Some functions for accessing registers
  65694. + */
  65695. +
  65696. +/**
  65697. + * GOTGCTL register
  65698. + */
  65699. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  65700. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  65701. +
  65702. +/**
  65703. + * GUSBCFG register
  65704. + */
  65705. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  65706. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  65707. +
  65708. +/**
  65709. + * GRXFSIZ register
  65710. + */
  65711. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  65712. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  65713. +
  65714. +/**
  65715. + * GNPTXFSIZ register
  65716. + */
  65717. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  65718. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  65719. +
  65720. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  65721. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  65722. +
  65723. +/**
  65724. + * GGPIO register
  65725. + */
  65726. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  65727. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  65728. +
  65729. +/**
  65730. + * GUID register
  65731. + */
  65732. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  65733. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  65734. +
  65735. +/**
  65736. + * HPRT0 register
  65737. + */
  65738. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  65739. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  65740. +
  65741. +/**
  65742. + * GHPTXFSIZE
  65743. + */
  65744. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  65745. +
  65746. +/** @} */
  65747. +
  65748. +#endif /* __DWC_CORE_IF_H__ */
  65749. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_dbg.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  65750. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 1970-01-01 01:00:00.000000000 +0100
  65751. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 2014-03-11 16:55:38.000000000 +0100
  65752. @@ -0,0 +1,117 @@
  65753. +/* ==========================================================================
  65754. + *
  65755. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  65756. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  65757. + * otherwise expressly agreed to in writing between Synopsys and you.
  65758. + *
  65759. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  65760. + * any End User Software License Agreement or Agreement for Licensed Product
  65761. + * with Synopsys or any supplement thereto. You are permitted to use and
  65762. + * redistribute this Software in source and binary forms, with or without
  65763. + * modification, provided that redistributions of source code must retain this
  65764. + * notice. You may not view, use, disclose, copy or distribute this file or
  65765. + * any information contained herein except pursuant to this license grant from
  65766. + * Synopsys. If you do not agree with this notice, including the disclaimer
  65767. + * below, then you are not authorized to use the Software.
  65768. + *
  65769. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  65770. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65771. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  65772. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  65773. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  65774. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  65775. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  65776. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  65777. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  65778. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  65779. + * DAMAGE.
  65780. + * ========================================================================== */
  65781. +
  65782. +#ifndef __DWC_OTG_DBG_H__
  65783. +#define __DWC_OTG_DBG_H__
  65784. +
  65785. +/** @file
  65786. + * This file defines debug levels.
  65787. + * Debugging support vanishes in non-debug builds.
  65788. + */
  65789. +
  65790. +/**
  65791. + * The Debug Level bit-mask variable.
  65792. + */
  65793. +extern uint32_t g_dbg_lvl;
  65794. +/**
  65795. + * Set the Debug Level variable.
  65796. + */
  65797. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  65798. +{
  65799. + uint32_t old = g_dbg_lvl;
  65800. + g_dbg_lvl = new;
  65801. + return old;
  65802. +}
  65803. +
  65804. +#define DBG_USER (0x1)
  65805. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  65806. +#define DBG_CIL (0x2)
  65807. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  65808. + * messages */
  65809. +#define DBG_CILV (0x20)
  65810. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  65811. + * messages */
  65812. +#define DBG_PCD (0x4)
  65813. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  65814. + * messages */
  65815. +#define DBG_PCDV (0x40)
  65816. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  65817. +#define DBG_HCD (0x8)
  65818. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  65819. + * messages */
  65820. +#define DBG_HCDV (0x80)
  65821. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  65822. + * mode. */
  65823. +#define DBG_HCD_URB (0x800)
  65824. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  65825. + * messages. */
  65826. +#define DBG_HCDI (0x1000)
  65827. +
  65828. +/** When debug level has any bit set, display debug messages */
  65829. +#define DBG_ANY (0xFF)
  65830. +
  65831. +/** All debug messages off */
  65832. +#define DBG_OFF 0
  65833. +
  65834. +/** Prefix string for DWC_DEBUG print macros. */
  65835. +#define USB_DWC "DWC_otg: "
  65836. +
  65837. +/**
  65838. + * Print a debug message when the Global debug level variable contains
  65839. + * the bit defined in <code>lvl</code>.
  65840. + *
  65841. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  65842. + * @param[in] x - like printf
  65843. + *
  65844. + * Example:<p>
  65845. + * <code>
  65846. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  65847. + * </code>
  65848. + * <br>
  65849. + * results in:<br>
  65850. + * <code>
  65851. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  65852. + * </code>
  65853. + */
  65854. +#ifdef DEBUG
  65855. +
  65856. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  65857. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  65858. +
  65859. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  65860. +
  65861. +#else
  65862. +
  65863. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  65864. +# define DWC_DEBUGP(x...)
  65865. +
  65866. +# define CHK_DEBUG_LEVEL(level) (0)
  65867. +
  65868. +#endif /*DEBUG*/
  65869. +#endif
  65870. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_driver.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  65871. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_driver.c 1970-01-01 01:00:00.000000000 +0100
  65872. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_driver.c 2014-03-11 16:55:38.000000000 +0100
  65873. @@ -0,0 +1,1742 @@
  65874. +/* ==========================================================================
  65875. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  65876. + * $Revision: #92 $
  65877. + * $Date: 2012/08/10 $
  65878. + * $Change: 2047372 $
  65879. + *
  65880. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  65881. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  65882. + * otherwise expressly agreed to in writing between Synopsys and you.
  65883. + *
  65884. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  65885. + * any End User Software License Agreement or Agreement for Licensed Product
  65886. + * with Synopsys or any supplement thereto. You are permitted to use and
  65887. + * redistribute this Software in source and binary forms, with or without
  65888. + * modification, provided that redistributions of source code must retain this
  65889. + * notice. You may not view, use, disclose, copy or distribute this file or
  65890. + * any information contained herein except pursuant to this license grant from
  65891. + * Synopsys. If you do not agree with this notice, including the disclaimer
  65892. + * below, then you are not authorized to use the Software.
  65893. + *
  65894. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  65895. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  65896. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  65897. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  65898. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  65899. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  65900. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  65901. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  65902. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  65903. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  65904. + * DAMAGE.
  65905. + * ========================================================================== */
  65906. +
  65907. +/** @file
  65908. + * The dwc_otg_driver module provides the initialization and cleanup entry
  65909. + * points for the DWC_otg driver. This module will be dynamically installed
  65910. + * after Linux is booted using the insmod command. When the module is
  65911. + * installed, the dwc_otg_driver_init function is called. When the module is
  65912. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  65913. + *
  65914. + * This module also defines a data structure for the dwc_otg_driver, which is
  65915. + * used in conjunction with the standard ARM lm_device structure. These
  65916. + * structures allow the OTG driver to comply with the standard Linux driver
  65917. + * model in which devices and drivers are registered with a bus driver. This
  65918. + * has the benefit that Linux can expose attributes of the driver and device
  65919. + * in its special sysfs file system. Users can then read or write files in
  65920. + * this file system to perform diagnostics on the driver components or the
  65921. + * device.
  65922. + */
  65923. +
  65924. +#include "dwc_otg_os_dep.h"
  65925. +#include "dwc_os.h"
  65926. +#include "dwc_otg_dbg.h"
  65927. +#include "dwc_otg_driver.h"
  65928. +#include "dwc_otg_attr.h"
  65929. +#include "dwc_otg_core_if.h"
  65930. +#include "dwc_otg_pcd_if.h"
  65931. +#include "dwc_otg_hcd_if.h"
  65932. +
  65933. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  65934. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  65935. +
  65936. +bool microframe_schedule=true;
  65937. +
  65938. +static const char dwc_driver_name[] = "dwc_otg";
  65939. +
  65940. +extern void* dummy_send;
  65941. +
  65942. +extern int pcd_init(
  65943. +#ifdef LM_INTERFACE
  65944. + struct lm_device *_dev
  65945. +#elif defined(PCI_INTERFACE)
  65946. + struct pci_dev *_dev
  65947. +#elif defined(PLATFORM_INTERFACE)
  65948. + struct platform_device *dev
  65949. +#endif
  65950. + );
  65951. +extern int hcd_init(
  65952. +#ifdef LM_INTERFACE
  65953. + struct lm_device *_dev
  65954. +#elif defined(PCI_INTERFACE)
  65955. + struct pci_dev *_dev
  65956. +#elif defined(PLATFORM_INTERFACE)
  65957. + struct platform_device *dev
  65958. +#endif
  65959. + );
  65960. +
  65961. +extern int pcd_remove(
  65962. +#ifdef LM_INTERFACE
  65963. + struct lm_device *_dev
  65964. +#elif defined(PCI_INTERFACE)
  65965. + struct pci_dev *_dev
  65966. +#elif defined(PLATFORM_INTERFACE)
  65967. + struct platform_device *_dev
  65968. +#endif
  65969. + );
  65970. +
  65971. +extern void hcd_remove(
  65972. +#ifdef LM_INTERFACE
  65973. + struct lm_device *_dev
  65974. +#elif defined(PCI_INTERFACE)
  65975. + struct pci_dev *_dev
  65976. +#elif defined(PLATFORM_INTERFACE)
  65977. + struct platform_device *_dev
  65978. +#endif
  65979. + );
  65980. +
  65981. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  65982. +
  65983. +/*-------------------------------------------------------------------------*/
  65984. +/* Encapsulate the module parameter settings */
  65985. +
  65986. +struct dwc_otg_driver_module_params {
  65987. + int32_t opt;
  65988. + int32_t otg_cap;
  65989. + int32_t dma_enable;
  65990. + int32_t dma_desc_enable;
  65991. + int32_t dma_burst_size;
  65992. + int32_t speed;
  65993. + int32_t host_support_fs_ls_low_power;
  65994. + int32_t host_ls_low_power_phy_clk;
  65995. + int32_t enable_dynamic_fifo;
  65996. + int32_t data_fifo_size;
  65997. + int32_t dev_rx_fifo_size;
  65998. + int32_t dev_nperio_tx_fifo_size;
  65999. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  66000. + int32_t host_rx_fifo_size;
  66001. + int32_t host_nperio_tx_fifo_size;
  66002. + int32_t host_perio_tx_fifo_size;
  66003. + int32_t max_transfer_size;
  66004. + int32_t max_packet_count;
  66005. + int32_t host_channels;
  66006. + int32_t dev_endpoints;
  66007. + int32_t phy_type;
  66008. + int32_t phy_utmi_width;
  66009. + int32_t phy_ulpi_ddr;
  66010. + int32_t phy_ulpi_ext_vbus;
  66011. + int32_t i2c_enable;
  66012. + int32_t ulpi_fs_ls;
  66013. + int32_t ts_dline;
  66014. + int32_t en_multiple_tx_fifo;
  66015. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  66016. + uint32_t thr_ctl;
  66017. + uint32_t tx_thr_length;
  66018. + uint32_t rx_thr_length;
  66019. + int32_t pti_enable;
  66020. + int32_t mpi_enable;
  66021. + int32_t lpm_enable;
  66022. + int32_t ic_usb_cap;
  66023. + int32_t ahb_thr_ratio;
  66024. + int32_t power_down;
  66025. + int32_t reload_ctl;
  66026. + int32_t dev_out_nak;
  66027. + int32_t cont_on_bna;
  66028. + int32_t ahb_single;
  66029. + int32_t otg_ver;
  66030. + int32_t adp_enable;
  66031. +};
  66032. +
  66033. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  66034. + .opt = -1,
  66035. + .otg_cap = -1,
  66036. + .dma_enable = -1,
  66037. + .dma_desc_enable = -1,
  66038. + .dma_burst_size = -1,
  66039. + .speed = -1,
  66040. + .host_support_fs_ls_low_power = -1,
  66041. + .host_ls_low_power_phy_clk = -1,
  66042. + .enable_dynamic_fifo = -1,
  66043. + .data_fifo_size = -1,
  66044. + .dev_rx_fifo_size = -1,
  66045. + .dev_nperio_tx_fifo_size = -1,
  66046. + .dev_perio_tx_fifo_size = {
  66047. + /* dev_perio_tx_fifo_size_1 */
  66048. + -1,
  66049. + -1,
  66050. + -1,
  66051. + -1,
  66052. + -1,
  66053. + -1,
  66054. + -1,
  66055. + -1,
  66056. + -1,
  66057. + -1,
  66058. + -1,
  66059. + -1,
  66060. + -1,
  66061. + -1,
  66062. + -1
  66063. + /* 15 */
  66064. + },
  66065. + .host_rx_fifo_size = -1,
  66066. + .host_nperio_tx_fifo_size = -1,
  66067. + .host_perio_tx_fifo_size = -1,
  66068. + .max_transfer_size = -1,
  66069. + .max_packet_count = -1,
  66070. + .host_channels = -1,
  66071. + .dev_endpoints = -1,
  66072. + .phy_type = -1,
  66073. + .phy_utmi_width = -1,
  66074. + .phy_ulpi_ddr = -1,
  66075. + .phy_ulpi_ext_vbus = -1,
  66076. + .i2c_enable = -1,
  66077. + .ulpi_fs_ls = -1,
  66078. + .ts_dline = -1,
  66079. + .en_multiple_tx_fifo = -1,
  66080. + .dev_tx_fifo_size = {
  66081. + /* dev_tx_fifo_size */
  66082. + -1,
  66083. + -1,
  66084. + -1,
  66085. + -1,
  66086. + -1,
  66087. + -1,
  66088. + -1,
  66089. + -1,
  66090. + -1,
  66091. + -1,
  66092. + -1,
  66093. + -1,
  66094. + -1,
  66095. + -1,
  66096. + -1
  66097. + /* 15 */
  66098. + },
  66099. + .thr_ctl = -1,
  66100. + .tx_thr_length = -1,
  66101. + .rx_thr_length = -1,
  66102. + .pti_enable = -1,
  66103. + .mpi_enable = -1,
  66104. + .lpm_enable = 0,
  66105. + .ic_usb_cap = -1,
  66106. + .ahb_thr_ratio = -1,
  66107. + .power_down = -1,
  66108. + .reload_ctl = -1,
  66109. + .dev_out_nak = -1,
  66110. + .cont_on_bna = -1,
  66111. + .ahb_single = -1,
  66112. + .otg_ver = -1,
  66113. + .adp_enable = -1,
  66114. +};
  66115. +
  66116. +//Global variable to switch the fiq fix on or off (declared in bcm2708.c)
  66117. +extern bool fiq_fix_enable;
  66118. +// Global variable to enable the split transaction fix
  66119. +bool fiq_split_enable = true;
  66120. +//Global variable to switch the nak holdoff on or off
  66121. +bool nak_holdoff_enable = true;
  66122. +
  66123. +
  66124. +/**
  66125. + * This function shows the Driver Version.
  66126. + */
  66127. +static ssize_t version_show(struct device_driver *dev, char *buf)
  66128. +{
  66129. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  66130. + DWC_DRIVER_VERSION);
  66131. +}
  66132. +
  66133. +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
  66134. +
  66135. +/**
  66136. + * Global Debug Level Mask.
  66137. + */
  66138. +uint32_t g_dbg_lvl = 0; /* OFF */
  66139. +
  66140. +/**
  66141. + * This function shows the driver Debug Level.
  66142. + */
  66143. +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
  66144. +{
  66145. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  66146. +}
  66147. +
  66148. +/**
  66149. + * This function stores the driver Debug Level.
  66150. + */
  66151. +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
  66152. + size_t count)
  66153. +{
  66154. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  66155. + return count;
  66156. +}
  66157. +
  66158. +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
  66159. + dbg_level_store);
  66160. +
  66161. +/**
  66162. + * This function is called during module intialization
  66163. + * to pass module parameters to the DWC_OTG CORE.
  66164. + */
  66165. +static int set_parameters(dwc_otg_core_if_t * core_if)
  66166. +{
  66167. + int retval = 0;
  66168. + int i;
  66169. +
  66170. + if (dwc_otg_module_params.otg_cap != -1) {
  66171. + retval +=
  66172. + dwc_otg_set_param_otg_cap(core_if,
  66173. + dwc_otg_module_params.otg_cap);
  66174. + }
  66175. + if (dwc_otg_module_params.dma_enable != -1) {
  66176. + retval +=
  66177. + dwc_otg_set_param_dma_enable(core_if,
  66178. + dwc_otg_module_params.
  66179. + dma_enable);
  66180. + }
  66181. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  66182. + retval +=
  66183. + dwc_otg_set_param_dma_desc_enable(core_if,
  66184. + dwc_otg_module_params.
  66185. + dma_desc_enable);
  66186. + }
  66187. + if (dwc_otg_module_params.opt != -1) {
  66188. + retval +=
  66189. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  66190. + }
  66191. + if (dwc_otg_module_params.dma_burst_size != -1) {
  66192. + retval +=
  66193. + dwc_otg_set_param_dma_burst_size(core_if,
  66194. + dwc_otg_module_params.
  66195. + dma_burst_size);
  66196. + }
  66197. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  66198. + retval +=
  66199. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  66200. + dwc_otg_module_params.
  66201. + host_support_fs_ls_low_power);
  66202. + }
  66203. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  66204. + retval +=
  66205. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  66206. + dwc_otg_module_params.
  66207. + enable_dynamic_fifo);
  66208. + }
  66209. + if (dwc_otg_module_params.data_fifo_size != -1) {
  66210. + retval +=
  66211. + dwc_otg_set_param_data_fifo_size(core_if,
  66212. + dwc_otg_module_params.
  66213. + data_fifo_size);
  66214. + }
  66215. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  66216. + retval +=
  66217. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  66218. + dwc_otg_module_params.
  66219. + dev_rx_fifo_size);
  66220. + }
  66221. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  66222. + retval +=
  66223. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  66224. + dwc_otg_module_params.
  66225. + dev_nperio_tx_fifo_size);
  66226. + }
  66227. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  66228. + retval +=
  66229. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  66230. + dwc_otg_module_params.host_rx_fifo_size);
  66231. + }
  66232. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  66233. + retval +=
  66234. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  66235. + dwc_otg_module_params.
  66236. + host_nperio_tx_fifo_size);
  66237. + }
  66238. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  66239. + retval +=
  66240. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  66241. + dwc_otg_module_params.
  66242. + host_perio_tx_fifo_size);
  66243. + }
  66244. + if (dwc_otg_module_params.max_transfer_size != -1) {
  66245. + retval +=
  66246. + dwc_otg_set_param_max_transfer_size(core_if,
  66247. + dwc_otg_module_params.
  66248. + max_transfer_size);
  66249. + }
  66250. + if (dwc_otg_module_params.max_packet_count != -1) {
  66251. + retval +=
  66252. + dwc_otg_set_param_max_packet_count(core_if,
  66253. + dwc_otg_module_params.
  66254. + max_packet_count);
  66255. + }
  66256. + if (dwc_otg_module_params.host_channels != -1) {
  66257. + retval +=
  66258. + dwc_otg_set_param_host_channels(core_if,
  66259. + dwc_otg_module_params.
  66260. + host_channels);
  66261. + }
  66262. + if (dwc_otg_module_params.dev_endpoints != -1) {
  66263. + retval +=
  66264. + dwc_otg_set_param_dev_endpoints(core_if,
  66265. + dwc_otg_module_params.
  66266. + dev_endpoints);
  66267. + }
  66268. + if (dwc_otg_module_params.phy_type != -1) {
  66269. + retval +=
  66270. + dwc_otg_set_param_phy_type(core_if,
  66271. + dwc_otg_module_params.phy_type);
  66272. + }
  66273. + if (dwc_otg_module_params.speed != -1) {
  66274. + retval +=
  66275. + dwc_otg_set_param_speed(core_if,
  66276. + dwc_otg_module_params.speed);
  66277. + }
  66278. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  66279. + retval +=
  66280. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  66281. + dwc_otg_module_params.
  66282. + host_ls_low_power_phy_clk);
  66283. + }
  66284. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  66285. + retval +=
  66286. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  66287. + dwc_otg_module_params.
  66288. + phy_ulpi_ddr);
  66289. + }
  66290. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  66291. + retval +=
  66292. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  66293. + dwc_otg_module_params.
  66294. + phy_ulpi_ext_vbus);
  66295. + }
  66296. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  66297. + retval +=
  66298. + dwc_otg_set_param_phy_utmi_width(core_if,
  66299. + dwc_otg_module_params.
  66300. + phy_utmi_width);
  66301. + }
  66302. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  66303. + retval +=
  66304. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  66305. + dwc_otg_module_params.ulpi_fs_ls);
  66306. + }
  66307. + if (dwc_otg_module_params.ts_dline != -1) {
  66308. + retval +=
  66309. + dwc_otg_set_param_ts_dline(core_if,
  66310. + dwc_otg_module_params.ts_dline);
  66311. + }
  66312. + if (dwc_otg_module_params.i2c_enable != -1) {
  66313. + retval +=
  66314. + dwc_otg_set_param_i2c_enable(core_if,
  66315. + dwc_otg_module_params.
  66316. + i2c_enable);
  66317. + }
  66318. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  66319. + retval +=
  66320. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  66321. + dwc_otg_module_params.
  66322. + en_multiple_tx_fifo);
  66323. + }
  66324. + for (i = 0; i < 15; i++) {
  66325. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  66326. + retval +=
  66327. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  66328. + dwc_otg_module_params.
  66329. + dev_perio_tx_fifo_size
  66330. + [i], i);
  66331. + }
  66332. + }
  66333. +
  66334. + for (i = 0; i < 15; i++) {
  66335. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  66336. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  66337. + dwc_otg_module_params.
  66338. + dev_tx_fifo_size
  66339. + [i], i);
  66340. + }
  66341. + }
  66342. + if (dwc_otg_module_params.thr_ctl != -1) {
  66343. + retval +=
  66344. + dwc_otg_set_param_thr_ctl(core_if,
  66345. + dwc_otg_module_params.thr_ctl);
  66346. + }
  66347. + if (dwc_otg_module_params.mpi_enable != -1) {
  66348. + retval +=
  66349. + dwc_otg_set_param_mpi_enable(core_if,
  66350. + dwc_otg_module_params.
  66351. + mpi_enable);
  66352. + }
  66353. + if (dwc_otg_module_params.pti_enable != -1) {
  66354. + retval +=
  66355. + dwc_otg_set_param_pti_enable(core_if,
  66356. + dwc_otg_module_params.
  66357. + pti_enable);
  66358. + }
  66359. + if (dwc_otg_module_params.lpm_enable != -1) {
  66360. + retval +=
  66361. + dwc_otg_set_param_lpm_enable(core_if,
  66362. + dwc_otg_module_params.
  66363. + lpm_enable);
  66364. + }
  66365. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  66366. + retval +=
  66367. + dwc_otg_set_param_ic_usb_cap(core_if,
  66368. + dwc_otg_module_params.
  66369. + ic_usb_cap);
  66370. + }
  66371. + if (dwc_otg_module_params.tx_thr_length != -1) {
  66372. + retval +=
  66373. + dwc_otg_set_param_tx_thr_length(core_if,
  66374. + dwc_otg_module_params.tx_thr_length);
  66375. + }
  66376. + if (dwc_otg_module_params.rx_thr_length != -1) {
  66377. + retval +=
  66378. + dwc_otg_set_param_rx_thr_length(core_if,
  66379. + dwc_otg_module_params.
  66380. + rx_thr_length);
  66381. + }
  66382. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  66383. + retval +=
  66384. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  66385. + dwc_otg_module_params.ahb_thr_ratio);
  66386. + }
  66387. + if (dwc_otg_module_params.power_down != -1) {
  66388. + retval +=
  66389. + dwc_otg_set_param_power_down(core_if,
  66390. + dwc_otg_module_params.power_down);
  66391. + }
  66392. + if (dwc_otg_module_params.reload_ctl != -1) {
  66393. + retval +=
  66394. + dwc_otg_set_param_reload_ctl(core_if,
  66395. + dwc_otg_module_params.reload_ctl);
  66396. + }
  66397. +
  66398. + if (dwc_otg_module_params.dev_out_nak != -1) {
  66399. + retval +=
  66400. + dwc_otg_set_param_dev_out_nak(core_if,
  66401. + dwc_otg_module_params.dev_out_nak);
  66402. + }
  66403. +
  66404. + if (dwc_otg_module_params.cont_on_bna != -1) {
  66405. + retval +=
  66406. + dwc_otg_set_param_cont_on_bna(core_if,
  66407. + dwc_otg_module_params.cont_on_bna);
  66408. + }
  66409. +
  66410. + if (dwc_otg_module_params.ahb_single != -1) {
  66411. + retval +=
  66412. + dwc_otg_set_param_ahb_single(core_if,
  66413. + dwc_otg_module_params.ahb_single);
  66414. + }
  66415. +
  66416. + if (dwc_otg_module_params.otg_ver != -1) {
  66417. + retval +=
  66418. + dwc_otg_set_param_otg_ver(core_if,
  66419. + dwc_otg_module_params.otg_ver);
  66420. + }
  66421. + if (dwc_otg_module_params.adp_enable != -1) {
  66422. + retval +=
  66423. + dwc_otg_set_param_adp_enable(core_if,
  66424. + dwc_otg_module_params.
  66425. + adp_enable);
  66426. + }
  66427. + return retval;
  66428. +}
  66429. +
  66430. +/**
  66431. + * This function is the top level interrupt handler for the Common
  66432. + * (Device and host modes) interrupts.
  66433. + */
  66434. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  66435. +{
  66436. + int32_t retval = IRQ_NONE;
  66437. +
  66438. + retval = dwc_otg_handle_common_intr(dev);
  66439. + if (retval != 0) {
  66440. + S3C2410X_CLEAR_EINTPEND();
  66441. + }
  66442. + return IRQ_RETVAL(retval);
  66443. +}
  66444. +
  66445. +/**
  66446. + * This function is called when a lm_device is unregistered with the
  66447. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  66448. + * executed. The device may or may not be electrically present. If it is
  66449. + * present, the driver stops device processing. Any resources used on behalf
  66450. + * of this device are freed.
  66451. + *
  66452. + * @param _dev
  66453. + */
  66454. +#ifdef LM_INTERFACE
  66455. +#define REM_RETVAL(n)
  66456. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  66457. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  66458. +#elif defined(PCI_INTERFACE)
  66459. +#define REM_RETVAL(n)
  66460. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  66461. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  66462. +#elif defined(PLATFORM_INTERFACE)
  66463. +#define REM_RETVAL(n) n
  66464. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  66465. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  66466. +#endif
  66467. +
  66468. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  66469. +
  66470. + if (!otg_dev) {
  66471. + /* Memory allocation for the dwc_otg_device failed. */
  66472. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  66473. + return REM_RETVAL(-ENOMEM);
  66474. + }
  66475. +#ifndef DWC_DEVICE_ONLY
  66476. + if (otg_dev->hcd) {
  66477. + hcd_remove(_dev);
  66478. + } else {
  66479. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  66480. + return REM_RETVAL(-EINVAL);
  66481. + }
  66482. +#endif
  66483. +
  66484. +#ifndef DWC_HOST_ONLY
  66485. + if (otg_dev->pcd) {
  66486. + pcd_remove(_dev);
  66487. + } else {
  66488. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  66489. + return REM_RETVAL(-EINVAL);
  66490. + }
  66491. +#endif
  66492. + /*
  66493. + * Free the IRQ
  66494. + */
  66495. + if (otg_dev->common_irq_installed) {
  66496. +#ifdef PLATFORM_INTERFACE
  66497. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  66498. +#else
  66499. + free_irq(_dev->irq, otg_dev);
  66500. +#endif
  66501. + } else {
  66502. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  66503. + return REM_RETVAL(-ENXIO);
  66504. + }
  66505. +
  66506. + if (otg_dev->core_if) {
  66507. + dwc_otg_cil_remove(otg_dev->core_if);
  66508. + } else {
  66509. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  66510. + return REM_RETVAL(-ENXIO);
  66511. + }
  66512. +
  66513. + /*
  66514. + * Remove the device attributes
  66515. + */
  66516. + dwc_otg_attr_remove(_dev);
  66517. +
  66518. + /*
  66519. + * Return the memory.
  66520. + */
  66521. + if (otg_dev->os_dep.base) {
  66522. + iounmap(otg_dev->os_dep.base);
  66523. + }
  66524. + DWC_FREE(otg_dev);
  66525. +
  66526. + /*
  66527. + * Clear the drvdata pointer.
  66528. + */
  66529. +#ifdef LM_INTERFACE
  66530. + lm_set_drvdata(_dev, 0);
  66531. +#elif defined(PCI_INTERFACE)
  66532. + release_mem_region(otg_dev->os_dep.rsrc_start,
  66533. + otg_dev->os_dep.rsrc_len);
  66534. + pci_set_drvdata(_dev, 0);
  66535. +#elif defined(PLATFORM_INTERFACE)
  66536. + platform_set_drvdata(_dev, 0);
  66537. +#endif
  66538. + return REM_RETVAL(0);
  66539. +}
  66540. +
  66541. +/**
  66542. + * This function is called when an lm_device is bound to a
  66543. + * dwc_otg_driver. It creates the driver components required to
  66544. + * control the device (CIL, HCD, and PCD) and it initializes the
  66545. + * device. The driver components are stored in a dwc_otg_device
  66546. + * structure. A reference to the dwc_otg_device is saved in the
  66547. + * lm_device. This allows the driver to access the dwc_otg_device
  66548. + * structure on subsequent calls to driver methods for this device.
  66549. + *
  66550. + * @param _dev Bus device
  66551. + */
  66552. +static int dwc_otg_driver_probe(
  66553. +#ifdef LM_INTERFACE
  66554. + struct lm_device *_dev
  66555. +#elif defined(PCI_INTERFACE)
  66556. + struct pci_dev *_dev,
  66557. + const struct pci_device_id *id
  66558. +#elif defined(PLATFORM_INTERFACE)
  66559. + struct platform_device *_dev
  66560. +#endif
  66561. + )
  66562. +{
  66563. + int retval = 0;
  66564. + dwc_otg_device_t *dwc_otg_device;
  66565. + int devirq;
  66566. +
  66567. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  66568. +#ifdef LM_INTERFACE
  66569. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  66570. +#elif defined(PCI_INTERFACE)
  66571. + if (!id) {
  66572. + DWC_ERROR("Invalid pci_device_id %p", id);
  66573. + return -EINVAL;
  66574. + }
  66575. +
  66576. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  66577. + DWC_ERROR("Invalid pci_device %p", _dev);
  66578. + return -ENODEV;
  66579. + }
  66580. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  66581. + /* other stuff needed as well? */
  66582. +
  66583. +#elif defined(PLATFORM_INTERFACE)
  66584. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  66585. + (unsigned)_dev->resource->start,
  66586. + (unsigned)(_dev->resource->end - _dev->resource->start));
  66587. +#endif
  66588. +
  66589. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  66590. +
  66591. + if (!dwc_otg_device) {
  66592. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  66593. + return -ENOMEM;
  66594. + }
  66595. +
  66596. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  66597. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  66598. +
  66599. + /*
  66600. + * Map the DWC_otg Core memory into virtual address space.
  66601. + */
  66602. +#ifdef LM_INTERFACE
  66603. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  66604. +
  66605. + if (!dwc_otg_device->os_dep.base) {
  66606. + dev_err(&_dev->dev, "ioremap() failed\n");
  66607. + DWC_FREE(dwc_otg_device);
  66608. + return -ENOMEM;
  66609. + }
  66610. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  66611. + (unsigned)dwc_otg_device->os_dep.base);
  66612. +#elif defined(PCI_INTERFACE)
  66613. + _dev->current_state = PCI_D0;
  66614. + _dev->dev.power.power_state = PMSG_ON;
  66615. +
  66616. + if (!_dev->irq) {
  66617. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  66618. + pci_name(_dev));
  66619. + iounmap(dwc_otg_device->os_dep.base);
  66620. + DWC_FREE(dwc_otg_device);
  66621. + return -ENODEV;
  66622. + }
  66623. +
  66624. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  66625. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  66626. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  66627. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  66628. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  66629. + if (!request_mem_region
  66630. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  66631. + "dwc_otg")) {
  66632. + dev_dbg(&_dev->dev, "error requesting memory\n");
  66633. + iounmap(dwc_otg_device->os_dep.base);
  66634. + DWC_FREE(dwc_otg_device);
  66635. + return -EFAULT;
  66636. + }
  66637. +
  66638. + dwc_otg_device->os_dep.base =
  66639. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  66640. + dwc_otg_device->os_dep.rsrc_len);
  66641. + if (dwc_otg_device->os_dep.base == NULL) {
  66642. + dev_dbg(&_dev->dev, "error mapping memory\n");
  66643. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  66644. + dwc_otg_device->os_dep.rsrc_len);
  66645. + iounmap(dwc_otg_device->os_dep.base);
  66646. + DWC_FREE(dwc_otg_device);
  66647. + return -EFAULT;
  66648. + }
  66649. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  66650. + dwc_otg_device->os_dep.base);
  66651. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  66652. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  66653. + dwc_otg_device->os_dep.base);
  66654. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  66655. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  66656. + dwc_otg_device->os_dep.base);
  66657. +
  66658. + pci_set_master(_dev);
  66659. + pci_set_drvdata(_dev, dwc_otg_device);
  66660. +#elif defined(PLATFORM_INTERFACE)
  66661. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  66662. + _dev->resource->start,
  66663. + _dev->resource->end - _dev->resource->start + 1);
  66664. +#if 1
  66665. + if (!request_mem_region(_dev->resource[0].start,
  66666. + _dev->resource[0].end - _dev->resource[0].start + 1,
  66667. + "dwc_otg")) {
  66668. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  66669. + retval = -EFAULT;
  66670. + goto fail;
  66671. + }
  66672. +
  66673. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  66674. + _dev->resource[0].end -
  66675. + _dev->resource[0].start+1);
  66676. + if (fiq_fix_enable)
  66677. + {
  66678. + if (!request_mem_region(_dev->resource[1].start,
  66679. + _dev->resource[1].end - _dev->resource[1].start + 1,
  66680. + "dwc_otg")) {
  66681. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  66682. + retval = -EFAULT;
  66683. + goto fail;
  66684. + }
  66685. +
  66686. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  66687. + _dev->resource[1].end -
  66688. + _dev->resource[1].start + 1);
  66689. + dummy_send = (void *) kmalloc(16, GFP_ATOMIC);
  66690. + }
  66691. +
  66692. +#else
  66693. + {
  66694. + struct map_desc desc = {
  66695. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  66696. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  66697. + .length = SZ_128K,
  66698. + .type = MT_DEVICE
  66699. + };
  66700. + iotable_init(&desc, 1);
  66701. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  66702. + }
  66703. +#endif
  66704. + if (!dwc_otg_device->os_dep.base) {
  66705. + dev_err(&_dev->dev, "ioremap() failed\n");
  66706. + retval = -ENOMEM;
  66707. + goto fail;
  66708. + }
  66709. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  66710. + (unsigned)dwc_otg_device->os_dep.base);
  66711. +#endif
  66712. +
  66713. + /*
  66714. + * Initialize driver data to point to the global DWC_otg
  66715. + * Device structure.
  66716. + */
  66717. +#ifdef LM_INTERFACE
  66718. + lm_set_drvdata(_dev, dwc_otg_device);
  66719. +#elif defined(PLATFORM_INTERFACE)
  66720. + platform_set_drvdata(_dev, dwc_otg_device);
  66721. +#endif
  66722. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  66723. +
  66724. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  66725. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  66726. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  66727. +
  66728. + if (!dwc_otg_device->core_if) {
  66729. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  66730. + retval = -ENOMEM;
  66731. + goto fail;
  66732. + }
  66733. +
  66734. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  66735. + /*
  66736. + * Attempt to ensure this device is really a DWC_otg Controller.
  66737. + * Read and verify the SNPSID register contents. The value should be
  66738. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  66739. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  66740. + */
  66741. +
  66742. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  66743. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  66744. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  66745. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  66746. + retval = -EINVAL;
  66747. + goto fail;
  66748. + }
  66749. +
  66750. + /*
  66751. + * Validate parameter values.
  66752. + */
  66753. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  66754. + if (set_parameters(dwc_otg_device->core_if)) {
  66755. + retval = -EINVAL;
  66756. + goto fail;
  66757. + }
  66758. +
  66759. + /*
  66760. + * Create Device Attributes in sysfs
  66761. + */
  66762. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  66763. + dwc_otg_attr_create(_dev);
  66764. +
  66765. + /*
  66766. + * Disable the global interrupt until all the interrupt
  66767. + * handlers are installed.
  66768. + */
  66769. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  66770. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  66771. +
  66772. + /*
  66773. + * Install the interrupt handler for the common interrupts before
  66774. + * enabling common interrupts in core_init below.
  66775. + */
  66776. +
  66777. +#if defined(PLATFORM_INTERFACE)
  66778. + devirq = platform_get_irq(_dev, 0);
  66779. +#else
  66780. + devirq = _dev->irq;
  66781. +#endif
  66782. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  66783. + devirq);
  66784. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  66785. + retval = request_irq(devirq, dwc_otg_common_irq,
  66786. + IRQF_SHARED,
  66787. + "dwc_otg", dwc_otg_device);
  66788. + if (retval) {
  66789. + DWC_ERROR("request of irq%d failed\n", devirq);
  66790. + retval = -EBUSY;
  66791. + goto fail;
  66792. + } else {
  66793. + dwc_otg_device->common_irq_installed = 1;
  66794. + }
  66795. +
  66796. +#ifndef IRQF_TRIGGER_LOW
  66797. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  66798. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  66799. + set_irq_type(devirq,
  66800. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  66801. + IRQT_LOW
  66802. +#else
  66803. + IRQ_TYPE_LEVEL_LOW
  66804. +#endif
  66805. + );
  66806. +#endif
  66807. +#endif /*IRQF_TRIGGER_LOW*/
  66808. +
  66809. + /*
  66810. + * Initialize the DWC_otg core.
  66811. + */
  66812. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  66813. + dwc_otg_core_init(dwc_otg_device->core_if);
  66814. +
  66815. +#ifndef DWC_HOST_ONLY
  66816. + /*
  66817. + * Initialize the PCD
  66818. + */
  66819. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  66820. + retval = pcd_init(_dev);
  66821. + if (retval != 0) {
  66822. + DWC_ERROR("pcd_init failed\n");
  66823. + dwc_otg_device->pcd = NULL;
  66824. + goto fail;
  66825. + }
  66826. +#endif
  66827. +#ifndef DWC_DEVICE_ONLY
  66828. + /*
  66829. + * Initialize the HCD
  66830. + */
  66831. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  66832. + retval = hcd_init(_dev);
  66833. + if (retval != 0) {
  66834. + DWC_ERROR("hcd_init failed\n");
  66835. + dwc_otg_device->hcd = NULL;
  66836. + goto fail;
  66837. + }
  66838. +#endif
  66839. + /* Recover from drvdata having been overwritten by hcd_init() */
  66840. +#ifdef LM_INTERFACE
  66841. + lm_set_drvdata(_dev, dwc_otg_device);
  66842. +#elif defined(PLATFORM_INTERFACE)
  66843. + platform_set_drvdata(_dev, dwc_otg_device);
  66844. +#elif defined(PCI_INTERFACE)
  66845. + pci_set_drvdata(_dev, dwc_otg_device);
  66846. + dwc_otg_device->os_dep.pcidev = _dev;
  66847. +#endif
  66848. +
  66849. + /*
  66850. + * Enable the global interrupt after all the interrupt
  66851. + * handlers are installed if there is no ADP support else
  66852. + * perform initial actions required for Internal ADP logic.
  66853. + */
  66854. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  66855. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  66856. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  66857. + dev_dbg(&_dev->dev, "Done\n");
  66858. + } else
  66859. + dwc_otg_adp_start(dwc_otg_device->core_if,
  66860. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  66861. +
  66862. + return 0;
  66863. +
  66864. +fail:
  66865. + dwc_otg_driver_remove(_dev);
  66866. + return retval;
  66867. +}
  66868. +
  66869. +/**
  66870. + * This structure defines the methods to be called by a bus driver
  66871. + * during the lifecycle of a device on that bus. Both drivers and
  66872. + * devices are registered with a bus driver. The bus driver matches
  66873. + * devices to drivers based on information in the device and driver
  66874. + * structures.
  66875. + *
  66876. + * The probe function is called when the bus driver matches a device
  66877. + * to this driver. The remove function is called when a device is
  66878. + * unregistered with the bus driver.
  66879. + */
  66880. +#ifdef LM_INTERFACE
  66881. +static struct lm_driver dwc_otg_driver = {
  66882. + .drv = {.name = (char *)dwc_driver_name,},
  66883. + .probe = dwc_otg_driver_probe,
  66884. + .remove = dwc_otg_driver_remove,
  66885. + // 'suspend' and 'resume' absent
  66886. +};
  66887. +#elif defined(PCI_INTERFACE)
  66888. +static const struct pci_device_id pci_ids[] = { {
  66889. + PCI_DEVICE(0x16c3, 0xabcd),
  66890. + .driver_data =
  66891. + (unsigned long)0xdeadbeef,
  66892. + }, { /* end: all zeroes */ }
  66893. +};
  66894. +
  66895. +MODULE_DEVICE_TABLE(pci, pci_ids);
  66896. +
  66897. +/* pci driver glue; this is a "new style" PCI driver module */
  66898. +static struct pci_driver dwc_otg_driver = {
  66899. + .name = "dwc_otg",
  66900. + .id_table = pci_ids,
  66901. +
  66902. + .probe = dwc_otg_driver_probe,
  66903. + .remove = dwc_otg_driver_remove,
  66904. +
  66905. + .driver = {
  66906. + .name = (char *)dwc_driver_name,
  66907. + },
  66908. +};
  66909. +#elif defined(PLATFORM_INTERFACE)
  66910. +static struct platform_device_id platform_ids[] = {
  66911. + {
  66912. + .name = "bcm2708_usb",
  66913. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  66914. + },
  66915. + { /* end: all zeroes */ }
  66916. +};
  66917. +MODULE_DEVICE_TABLE(platform, platform_ids);
  66918. +
  66919. +static struct platform_driver dwc_otg_driver = {
  66920. + .driver = {
  66921. + .name = (char *)dwc_driver_name,
  66922. + },
  66923. + .id_table = platform_ids,
  66924. +
  66925. + .probe = dwc_otg_driver_probe,
  66926. + .remove = dwc_otg_driver_remove,
  66927. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  66928. +};
  66929. +#endif
  66930. +
  66931. +/**
  66932. + * This function is called when the dwc_otg_driver is installed with the
  66933. + * insmod command. It registers the dwc_otg_driver structure with the
  66934. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  66935. + * to be called. In addition, the bus driver will automatically expose
  66936. + * attributes defined for the device and driver in the special sysfs file
  66937. + * system.
  66938. + *
  66939. + * @return
  66940. + */
  66941. +static int __init dwc_otg_driver_init(void)
  66942. +{
  66943. + int retval = 0;
  66944. + int error;
  66945. + struct device_driver *drv;
  66946. +
  66947. + if(fiq_split_enable && !fiq_fix_enable) {
  66948. + printk(KERN_WARNING "dwc_otg: fiq_split_enable was set without fiq_fix_enable! Correcting.\n");
  66949. + fiq_fix_enable = 1;
  66950. + }
  66951. +
  66952. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  66953. + DWC_DRIVER_VERSION,
  66954. +#ifdef LM_INTERFACE
  66955. + "logicmodule");
  66956. + retval = lm_driver_register(&dwc_otg_driver);
  66957. + drv = &dwc_otg_driver.drv;
  66958. +#elif defined(PCI_INTERFACE)
  66959. + "pci");
  66960. + retval = pci_register_driver(&dwc_otg_driver);
  66961. + drv = &dwc_otg_driver.driver;
  66962. +#elif defined(PLATFORM_INTERFACE)
  66963. + "platform");
  66964. + retval = platform_driver_register(&dwc_otg_driver);
  66965. + drv = &dwc_otg_driver.driver;
  66966. +#endif
  66967. + if (retval < 0) {
  66968. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  66969. + return retval;
  66970. + }
  66971. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_fix_enable ? "enabled":"disabled");
  66972. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff_enable ? "enabled":"disabled");
  66973. + printk(KERN_DEBUG "dwc_otg: FIQ split fix %s\n", fiq_split_enable ? "enabled":"disabled");
  66974. +
  66975. + error = driver_create_file(drv, &driver_attr_version);
  66976. +#ifdef DEBUG
  66977. + error = driver_create_file(drv, &driver_attr_debuglevel);
  66978. +#endif
  66979. + return retval;
  66980. +}
  66981. +
  66982. +module_init(dwc_otg_driver_init);
  66983. +
  66984. +/**
  66985. + * This function is called when the driver is removed from the kernel
  66986. + * with the rmmod command. The driver unregisters itself with its bus
  66987. + * driver.
  66988. + *
  66989. + */
  66990. +static void __exit dwc_otg_driver_cleanup(void)
  66991. +{
  66992. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  66993. +
  66994. +#ifdef LM_INTERFACE
  66995. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  66996. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  66997. + lm_driver_unregister(&dwc_otg_driver);
  66998. +#elif defined(PCI_INTERFACE)
  66999. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  67000. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  67001. + pci_unregister_driver(&dwc_otg_driver);
  67002. +#elif defined(PLATFORM_INTERFACE)
  67003. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  67004. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  67005. + platform_driver_unregister(&dwc_otg_driver);
  67006. +#endif
  67007. +
  67008. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  67009. +}
  67010. +
  67011. +module_exit(dwc_otg_driver_cleanup);
  67012. +
  67013. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  67014. +MODULE_AUTHOR("Synopsys Inc.");
  67015. +MODULE_LICENSE("GPL");
  67016. +
  67017. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  67018. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  67019. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  67020. +MODULE_PARM_DESC(opt, "OPT Mode");
  67021. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  67022. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  67023. +
  67024. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  67025. + 0444);
  67026. +MODULE_PARM_DESC(dma_desc_enable,
  67027. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  67028. +
  67029. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  67030. + 0444);
  67031. +MODULE_PARM_DESC(dma_burst_size,
  67032. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  67033. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  67034. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  67035. +module_param_named(host_support_fs_ls_low_power,
  67036. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  67037. + 0444);
  67038. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  67039. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  67040. +module_param_named(host_ls_low_power_phy_clk,
  67041. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  67042. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  67043. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  67044. +module_param_named(enable_dynamic_fifo,
  67045. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  67046. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  67047. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  67048. + 0444);
  67049. +MODULE_PARM_DESC(data_fifo_size,
  67050. + "Total number of words in the data FIFO memory 32-32768");
  67051. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  67052. + int, 0444);
  67053. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  67054. +module_param_named(dev_nperio_tx_fifo_size,
  67055. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  67056. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  67057. + "Number of words in the non-periodic Tx FIFO 16-32768");
  67058. +module_param_named(dev_perio_tx_fifo_size_1,
  67059. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  67060. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  67061. + "Number of words in the periodic Tx FIFO 4-768");
  67062. +module_param_named(dev_perio_tx_fifo_size_2,
  67063. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  67064. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  67065. + "Number of words in the periodic Tx FIFO 4-768");
  67066. +module_param_named(dev_perio_tx_fifo_size_3,
  67067. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  67068. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  67069. + "Number of words in the periodic Tx FIFO 4-768");
  67070. +module_param_named(dev_perio_tx_fifo_size_4,
  67071. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  67072. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  67073. + "Number of words in the periodic Tx FIFO 4-768");
  67074. +module_param_named(dev_perio_tx_fifo_size_5,
  67075. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  67076. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  67077. + "Number of words in the periodic Tx FIFO 4-768");
  67078. +module_param_named(dev_perio_tx_fifo_size_6,
  67079. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  67080. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  67081. + "Number of words in the periodic Tx FIFO 4-768");
  67082. +module_param_named(dev_perio_tx_fifo_size_7,
  67083. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  67084. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  67085. + "Number of words in the periodic Tx FIFO 4-768");
  67086. +module_param_named(dev_perio_tx_fifo_size_8,
  67087. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  67088. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  67089. + "Number of words in the periodic Tx FIFO 4-768");
  67090. +module_param_named(dev_perio_tx_fifo_size_9,
  67091. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  67092. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  67093. + "Number of words in the periodic Tx FIFO 4-768");
  67094. +module_param_named(dev_perio_tx_fifo_size_10,
  67095. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  67096. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  67097. + "Number of words in the periodic Tx FIFO 4-768");
  67098. +module_param_named(dev_perio_tx_fifo_size_11,
  67099. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  67100. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  67101. + "Number of words in the periodic Tx FIFO 4-768");
  67102. +module_param_named(dev_perio_tx_fifo_size_12,
  67103. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  67104. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  67105. + "Number of words in the periodic Tx FIFO 4-768");
  67106. +module_param_named(dev_perio_tx_fifo_size_13,
  67107. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  67108. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  67109. + "Number of words in the periodic Tx FIFO 4-768");
  67110. +module_param_named(dev_perio_tx_fifo_size_14,
  67111. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  67112. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  67113. + "Number of words in the periodic Tx FIFO 4-768");
  67114. +module_param_named(dev_perio_tx_fifo_size_15,
  67115. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  67116. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  67117. + "Number of words in the periodic Tx FIFO 4-768");
  67118. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  67119. + int, 0444);
  67120. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  67121. +module_param_named(host_nperio_tx_fifo_size,
  67122. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  67123. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  67124. + "Number of words in the non-periodic Tx FIFO 16-32768");
  67125. +module_param_named(host_perio_tx_fifo_size,
  67126. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  67127. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  67128. + "Number of words in the host periodic Tx FIFO 16-32768");
  67129. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  67130. + int, 0444);
  67131. +/** @todo Set the max to 512K, modify checks */
  67132. +MODULE_PARM_DESC(max_transfer_size,
  67133. + "The maximum transfer size supported in bytes 2047-65535");
  67134. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  67135. + int, 0444);
  67136. +MODULE_PARM_DESC(max_packet_count,
  67137. + "The maximum number of packets in a transfer 15-511");
  67138. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  67139. + 0444);
  67140. +MODULE_PARM_DESC(host_channels,
  67141. + "The number of host channel registers to use 1-16");
  67142. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  67143. + 0444);
  67144. +MODULE_PARM_DESC(dev_endpoints,
  67145. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  67146. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  67147. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  67148. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  67149. + 0444);
  67150. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  67151. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  67152. +MODULE_PARM_DESC(phy_ulpi_ddr,
  67153. + "ULPI at double or single data rate 0=Single 1=Double");
  67154. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  67155. + int, 0444);
  67156. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  67157. + "ULPI PHY using internal or external vbus 0=Internal");
  67158. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  67159. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  67160. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  67161. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  67162. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  67163. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  67164. +module_param_named(debug, g_dbg_lvl, int, 0444);
  67165. +MODULE_PARM_DESC(debug, "");
  67166. +
  67167. +module_param_named(en_multiple_tx_fifo,
  67168. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  67169. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  67170. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  67171. +module_param_named(dev_tx_fifo_size_1,
  67172. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  67173. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  67174. +module_param_named(dev_tx_fifo_size_2,
  67175. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  67176. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  67177. +module_param_named(dev_tx_fifo_size_3,
  67178. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  67179. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  67180. +module_param_named(dev_tx_fifo_size_4,
  67181. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  67182. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  67183. +module_param_named(dev_tx_fifo_size_5,
  67184. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  67185. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  67186. +module_param_named(dev_tx_fifo_size_6,
  67187. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  67188. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  67189. +module_param_named(dev_tx_fifo_size_7,
  67190. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  67191. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  67192. +module_param_named(dev_tx_fifo_size_8,
  67193. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  67194. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  67195. +module_param_named(dev_tx_fifo_size_9,
  67196. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  67197. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  67198. +module_param_named(dev_tx_fifo_size_10,
  67199. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  67200. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  67201. +module_param_named(dev_tx_fifo_size_11,
  67202. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  67203. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  67204. +module_param_named(dev_tx_fifo_size_12,
  67205. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  67206. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  67207. +module_param_named(dev_tx_fifo_size_13,
  67208. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  67209. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  67210. +module_param_named(dev_tx_fifo_size_14,
  67211. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  67212. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  67213. +module_param_named(dev_tx_fifo_size_15,
  67214. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  67215. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  67216. +
  67217. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  67218. +MODULE_PARM_DESC(thr_ctl,
  67219. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  67220. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  67221. + 0444);
  67222. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  67223. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  67224. + 0444);
  67225. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  67226. +
  67227. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  67228. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  67229. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  67230. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  67231. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  67232. +MODULE_PARM_DESC(ic_usb_cap,
  67233. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  67234. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  67235. + 0444);
  67236. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  67237. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  67238. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  67239. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  67240. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  67241. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  67242. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  67243. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  67244. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  67245. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  67246. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  67247. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  67248. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  67249. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  67250. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  67251. +module_param(microframe_schedule, bool, 0444);
  67252. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  67253. +
  67254. +module_param(fiq_fix_enable, bool, 0444);
  67255. +MODULE_PARM_DESC(fiq_fix_enable, "Enable the fiq fix");
  67256. +module_param(nak_holdoff_enable, bool, 0444);
  67257. +MODULE_PARM_DESC(nak_holdoff_enable, "Enable the NAK holdoff");
  67258. +module_param(fiq_split_enable, bool, 0444);
  67259. +MODULE_PARM_DESC(fiq_split_enable, "Enable the FIQ fix on split transactions");
  67260. +
  67261. +/** @page "Module Parameters"
  67262. + *
  67263. + * The following parameters may be specified when starting the module.
  67264. + * These parameters define how the DWC_otg controller should be
  67265. + * configured. Parameter values are passed to the CIL initialization
  67266. + * function dwc_otg_cil_init
  67267. + *
  67268. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  67269. + *
  67270. +
  67271. + <table>
  67272. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  67273. +
  67274. + <tr>
  67275. + <td>otg_cap</td>
  67276. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  67277. + value for this parameter if none is specified.
  67278. + - 0: HNP and SRP capable (default, if available)
  67279. + - 1: SRP Only capable
  67280. + - 2: No HNP/SRP capable
  67281. + </td></tr>
  67282. +
  67283. + <tr>
  67284. + <td>dma_enable</td>
  67285. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  67286. + The driver will automatically detect the value for this parameter if none is
  67287. + specified.
  67288. + - 0: Slave
  67289. + - 1: DMA (default, if available)
  67290. + </td></tr>
  67291. +
  67292. + <tr>
  67293. + <td>dma_burst_size</td>
  67294. + <td>The DMA Burst size (applicable only for External DMA Mode).
  67295. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  67296. + </td></tr>
  67297. +
  67298. + <tr>
  67299. + <td>speed</td>
  67300. + <td>Specifies the maximum speed of operation in host and device mode. The
  67301. + actual speed depends on the speed of the attached device and the value of
  67302. + phy_type.
  67303. + - 0: High Speed (default)
  67304. + - 1: Full Speed
  67305. + </td></tr>
  67306. +
  67307. + <tr>
  67308. + <td>host_support_fs_ls_low_power</td>
  67309. + <td>Specifies whether low power mode is supported when attached to a Full
  67310. + Speed or Low Speed device in host mode.
  67311. + - 0: Don't support low power mode (default)
  67312. + - 1: Support low power mode
  67313. + </td></tr>
  67314. +
  67315. + <tr>
  67316. + <td>host_ls_low_power_phy_clk</td>
  67317. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  67318. + Speed device in host mode. This parameter is applicable only if
  67319. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  67320. + - 0: 48 MHz (default)
  67321. + - 1: 6 MHz
  67322. + </td></tr>
  67323. +
  67324. + <tr>
  67325. + <td>enable_dynamic_fifo</td>
  67326. + <td> Specifies whether FIFOs may be resized by the driver software.
  67327. + - 0: Use cC FIFO size parameters
  67328. + - 1: Allow dynamic FIFO sizing (default)
  67329. + </td></tr>
  67330. +
  67331. + <tr>
  67332. + <td>data_fifo_size</td>
  67333. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  67334. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  67335. + - Values: 32 to 32768 (default 8192)
  67336. +
  67337. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  67338. + </td></tr>
  67339. +
  67340. + <tr>
  67341. + <td>dev_rx_fifo_size</td>
  67342. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  67343. + FIFO sizing is enabled.
  67344. + - Values: 16 to 32768 (default 1064)
  67345. + </td></tr>
  67346. +
  67347. + <tr>
  67348. + <td>dev_nperio_tx_fifo_size</td>
  67349. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  67350. + dynamic FIFO sizing is enabled.
  67351. + - Values: 16 to 32768 (default 1024)
  67352. + </td></tr>
  67353. +
  67354. + <tr>
  67355. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  67356. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  67357. + when dynamic FIFO sizing is enabled.
  67358. + - Values: 4 to 768 (default 256)
  67359. + </td></tr>
  67360. +
  67361. + <tr>
  67362. + <td>host_rx_fifo_size</td>
  67363. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  67364. + sizing is enabled.
  67365. + - Values: 16 to 32768 (default 1024)
  67366. + </td></tr>
  67367. +
  67368. + <tr>
  67369. + <td>host_nperio_tx_fifo_size</td>
  67370. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  67371. + dynamic FIFO sizing is enabled in the core.
  67372. + - Values: 16 to 32768 (default 1024)
  67373. + </td></tr>
  67374. +
  67375. + <tr>
  67376. + <td>host_perio_tx_fifo_size</td>
  67377. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  67378. + sizing is enabled.
  67379. + - Values: 16 to 32768 (default 1024)
  67380. + </td></tr>
  67381. +
  67382. + <tr>
  67383. + <td>max_transfer_size</td>
  67384. + <td>The maximum transfer size supported in bytes.
  67385. + - Values: 2047 to 65,535 (default 65,535)
  67386. + </td></tr>
  67387. +
  67388. + <tr>
  67389. + <td>max_packet_count</td>
  67390. + <td>The maximum number of packets in a transfer.
  67391. + - Values: 15 to 511 (default 511)
  67392. + </td></tr>
  67393. +
  67394. + <tr>
  67395. + <td>host_channels</td>
  67396. + <td>The number of host channel registers to use.
  67397. + - Values: 1 to 16 (default 12)
  67398. +
  67399. + Note: The FPGA configuration supports a maximum of 12 host channels.
  67400. + </td></tr>
  67401. +
  67402. + <tr>
  67403. + <td>dev_endpoints</td>
  67404. + <td>The number of endpoints in addition to EP0 available for device mode
  67405. + operations.
  67406. + - Values: 1 to 15 (default 6 IN and OUT)
  67407. +
  67408. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  67409. + addition to EP0.
  67410. + </td></tr>
  67411. +
  67412. + <tr>
  67413. + <td>phy_type</td>
  67414. + <td>Specifies the type of PHY interface to use. By default, the driver will
  67415. + automatically detect the phy_type.
  67416. + - 0: Full Speed
  67417. + - 1: UTMI+ (default, if available)
  67418. + - 2: ULPI
  67419. + </td></tr>
  67420. +
  67421. + <tr>
  67422. + <td>phy_utmi_width</td>
  67423. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  67424. + phy_type of UTMI+. Also, this parameter is applicable only if the
  67425. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  67426. + core has been configured to work at either data path width.
  67427. + - Values: 8 or 16 bits (default 16)
  67428. + </td></tr>
  67429. +
  67430. + <tr>
  67431. + <td>phy_ulpi_ddr</td>
  67432. + <td>Specifies whether the ULPI operates at double or single data rate. This
  67433. + parameter is only applicable if phy_type is ULPI.
  67434. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  67435. + - 1: double data rate ULPI interface with 4 bit wide data bus
  67436. + </td></tr>
  67437. +
  67438. + <tr>
  67439. + <td>i2c_enable</td>
  67440. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  67441. + parameter is only applicable if PHY_TYPE is FS.
  67442. + - 0: Disabled (default)
  67443. + - 1: Enabled
  67444. + </td></tr>
  67445. +
  67446. + <tr>
  67447. + <td>ulpi_fs_ls</td>
  67448. + <td>Specifies whether to use ULPI FS/LS mode only.
  67449. + - 0: Disabled (default)
  67450. + - 1: Enabled
  67451. + </td></tr>
  67452. +
  67453. + <tr>
  67454. + <td>ts_dline</td>
  67455. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  67456. + - 0: Disabled (default)
  67457. + - 1: Enabled
  67458. + </td></tr>
  67459. +
  67460. + <tr>
  67461. + <td>en_multiple_tx_fifo</td>
  67462. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  67463. + The driver will automatically detect the value for this parameter if none is
  67464. + specified.
  67465. + - 0: Disabled
  67466. + - 1: Enabled (default, if available)
  67467. + </td></tr>
  67468. +
  67469. + <tr>
  67470. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  67471. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  67472. + when dynamic FIFO sizing is enabled.
  67473. + - Values: 4 to 768 (default 256)
  67474. + </td></tr>
  67475. +
  67476. + <tr>
  67477. + <td>tx_thr_length</td>
  67478. + <td>Transmit Threshold length in 32 bit double words
  67479. + - Values: 8 to 128 (default 64)
  67480. + </td></tr>
  67481. +
  67482. + <tr>
  67483. + <td>rx_thr_length</td>
  67484. + <td>Receive Threshold length in 32 bit double words
  67485. + - Values: 8 to 128 (default 64)
  67486. + </td></tr>
  67487. +
  67488. +<tr>
  67489. + <td>thr_ctl</td>
  67490. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  67491. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  67492. + Rx transfers accordingly.
  67493. + The driver will automatically detect the value for this parameter if none is
  67494. + specified.
  67495. + - Values: 0 to 7 (default 0)
  67496. + Bit values indicate:
  67497. + - 0: Thresholding disabled
  67498. + - 1: Thresholding enabled
  67499. + </td></tr>
  67500. +
  67501. +<tr>
  67502. + <td>dma_desc_enable</td>
  67503. + <td>Specifies whether to enable Descriptor DMA mode.
  67504. + The driver will automatically detect the value for this parameter if none is
  67505. + specified.
  67506. + - 0: Descriptor DMA disabled
  67507. + - 1: Descriptor DMA (default, if available)
  67508. + </td></tr>
  67509. +
  67510. +<tr>
  67511. + <td>mpi_enable</td>
  67512. + <td>Specifies whether to enable MPI enhancement mode.
  67513. + The driver will automatically detect the value for this parameter if none is
  67514. + specified.
  67515. + - 0: MPI disabled (default)
  67516. + - 1: MPI enable
  67517. + </td></tr>
  67518. +
  67519. +<tr>
  67520. + <td>pti_enable</td>
  67521. + <td>Specifies whether to enable PTI enhancement support.
  67522. + The driver will automatically detect the value for this parameter if none is
  67523. + specified.
  67524. + - 0: PTI disabled (default)
  67525. + - 1: PTI enable
  67526. + </td></tr>
  67527. +
  67528. +<tr>
  67529. + <td>lpm_enable</td>
  67530. + <td>Specifies whether to enable LPM support.
  67531. + The driver will automatically detect the value for this parameter if none is
  67532. + specified.
  67533. + - 0: LPM disabled
  67534. + - 1: LPM enable (default, if available)
  67535. + </td></tr>
  67536. +
  67537. +<tr>
  67538. + <td>ic_usb_cap</td>
  67539. + <td>Specifies whether to enable IC_USB capability.
  67540. + The driver will automatically detect the value for this parameter if none is
  67541. + specified.
  67542. + - 0: IC_USB disabled (default, if available)
  67543. + - 1: IC_USB enable
  67544. + </td></tr>
  67545. +
  67546. +<tr>
  67547. + <td>ahb_thr_ratio</td>
  67548. + <td>Specifies AHB Threshold ratio.
  67549. + - Values: 0 to 3 (default 0)
  67550. + </td></tr>
  67551. +
  67552. +<tr>
  67553. + <td>power_down</td>
  67554. + <td>Specifies Power Down(Hibernation) Mode.
  67555. + The driver will automatically detect the value for this parameter if none is
  67556. + specified.
  67557. + - 0: Power Down disabled (default)
  67558. + - 2: Power Down enabled
  67559. + </td></tr>
  67560. +
  67561. + <tr>
  67562. + <td>reload_ctl</td>
  67563. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  67564. + run time. The driver will automatically detect the value for this parameter if
  67565. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  67566. + the core might misbehave.
  67567. + - 0: Reload Control disabled (default)
  67568. + - 1: Reload Control enabled
  67569. + </td></tr>
  67570. +
  67571. + <tr>
  67572. + <td>dev_out_nak</td>
  67573. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  67574. + The driver will automatically detect the value for this parameter if
  67575. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  67576. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  67577. + - 1: The core sets NAK after Bulk OUT transfer complete
  67578. + </td></tr>
  67579. +
  67580. + <tr>
  67581. + <td>cont_on_bna</td>
  67582. + <td>Specifies whether Enable Continue on BNA enabled or no.
  67583. + After receiving BNA interrupt the core disables the endpoint,when the
  67584. + endpoint is re-enabled by the application the
  67585. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  67586. + - 1: Core starts processing from the descriptor which received the BNA.
  67587. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  67588. + </td></tr>
  67589. +
  67590. + <tr>
  67591. + <td>ahb_single</td>
  67592. + <td>This bit when programmed supports SINGLE transfers for remainder data
  67593. + in a transfer for DMA mode of operation.
  67594. + - 0: The remainder data will be sent using INCR burst size (default)
  67595. + - 1: The remainder data will be sent using SINGLE burst size.
  67596. + </td></tr>
  67597. +
  67598. +<tr>
  67599. + <td>adp_enable</td>
  67600. + <td>Specifies whether ADP feature is enabled.
  67601. + The driver will automatically detect the value for this parameter if none is
  67602. + specified.
  67603. + - 0: ADP feature disabled (default)
  67604. + - 1: ADP feature enabled
  67605. + </td></tr>
  67606. +
  67607. + <tr>
  67608. + <td>otg_ver</td>
  67609. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  67610. + USB OTG device.
  67611. + - 0: OTG 2.0 support disabled (default)
  67612. + - 1: OTG 2.0 support enabled
  67613. + </td></tr>
  67614. +
  67615. +*/
  67616. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_driver.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  67617. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_driver.h 1970-01-01 01:00:00.000000000 +0100
  67618. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_driver.h 2014-03-11 16:55:38.000000000 +0100
  67619. @@ -0,0 +1,86 @@
  67620. +/* ==========================================================================
  67621. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  67622. + * $Revision: #19 $
  67623. + * $Date: 2010/11/15 $
  67624. + * $Change: 1627671 $
  67625. + *
  67626. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  67627. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  67628. + * otherwise expressly agreed to in writing between Synopsys and you.
  67629. + *
  67630. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  67631. + * any End User Software License Agreement or Agreement for Licensed Product
  67632. + * with Synopsys or any supplement thereto. You are permitted to use and
  67633. + * redistribute this Software in source and binary forms, with or without
  67634. + * modification, provided that redistributions of source code must retain this
  67635. + * notice. You may not view, use, disclose, copy or distribute this file or
  67636. + * any information contained herein except pursuant to this license grant from
  67637. + * Synopsys. If you do not agree with this notice, including the disclaimer
  67638. + * below, then you are not authorized to use the Software.
  67639. + *
  67640. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  67641. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  67642. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  67643. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  67644. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  67645. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  67646. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  67647. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  67648. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  67649. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  67650. + * DAMAGE.
  67651. + * ========================================================================== */
  67652. +
  67653. +#ifndef __DWC_OTG_DRIVER_H__
  67654. +#define __DWC_OTG_DRIVER_H__
  67655. +
  67656. +/** @file
  67657. + * This file contains the interface to the Linux driver.
  67658. + */
  67659. +#include "dwc_otg_os_dep.h"
  67660. +#include "dwc_otg_core_if.h"
  67661. +
  67662. +/* Type declarations */
  67663. +struct dwc_otg_pcd;
  67664. +struct dwc_otg_hcd;
  67665. +
  67666. +/**
  67667. + * This structure is a wrapper that encapsulates the driver components used to
  67668. + * manage a single DWC_otg controller.
  67669. + */
  67670. +typedef struct dwc_otg_device {
  67671. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  67672. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  67673. + * require this. */
  67674. + struct os_dependent os_dep;
  67675. +
  67676. + /** Pointer to the core interface structure. */
  67677. + dwc_otg_core_if_t *core_if;
  67678. +
  67679. + /** Pointer to the PCD structure. */
  67680. + struct dwc_otg_pcd *pcd;
  67681. +
  67682. + /** Pointer to the HCD structure. */
  67683. + struct dwc_otg_hcd *hcd;
  67684. +
  67685. + /** Flag to indicate whether the common IRQ handler is installed. */
  67686. + uint8_t common_irq_installed;
  67687. +
  67688. +} dwc_otg_device_t;
  67689. +
  67690. +/*We must clear S3C24XX_EINTPEND external interrupt register
  67691. + * because after clearing in this register trigerred IRQ from
  67692. + * H/W core in kernel interrupt can be occured again before OTG
  67693. + * handlers clear all IRQ sources of Core registers because of
  67694. + * timing latencies and Low Level IRQ Type.
  67695. + */
  67696. +#ifdef CONFIG_MACH_IPMATE
  67697. +#define S3C2410X_CLEAR_EINTPEND() \
  67698. +do { \
  67699. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  67700. +} while (0)
  67701. +#else
  67702. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  67703. +#endif
  67704. +
  67705. +#endif
  67706. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  67707. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 1970-01-01 01:00:00.000000000 +0100
  67708. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 2014-03-11 16:55:38.000000000 +0100
  67709. @@ -0,0 +1,3685 @@
  67710. +
  67711. +/* ==========================================================================
  67712. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  67713. + * $Revision: #104 $
  67714. + * $Date: 2011/10/24 $
  67715. + * $Change: 1871159 $
  67716. + *
  67717. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  67718. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  67719. + * otherwise expressly agreed to in writing between Synopsys and you.
  67720. + *
  67721. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  67722. + * any End User Software License Agreement or Agreement for Licensed Product
  67723. + * with Synopsys or any supplement thereto. You are permitted to use and
  67724. + * redistribute this Software in source and binary forms, with or without
  67725. + * modification, provided that redistributions of source code must retain this
  67726. + * notice. You may not view, use, disclose, copy or distribute this file or
  67727. + * any information contained herein except pursuant to this license grant from
  67728. + * Synopsys. If you do not agree with this notice, including the disclaimer
  67729. + * below, then you are not authorized to use the Software.
  67730. + *
  67731. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  67732. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  67733. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  67734. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  67735. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  67736. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  67737. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  67738. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  67739. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  67740. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  67741. + * DAMAGE.
  67742. + * ========================================================================== */
  67743. +#ifndef DWC_DEVICE_ONLY
  67744. +
  67745. +/** @file
  67746. + * This file implements HCD Core. All code in this file is portable and doesn't
  67747. + * use any OS specific functions.
  67748. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  67749. + * header file.
  67750. + */
  67751. +
  67752. +#include <linux/usb.h>
  67753. +#include <linux/usb/hcd.h>
  67754. +
  67755. +#include "dwc_otg_hcd.h"
  67756. +#include "dwc_otg_regs.h"
  67757. +#include "dwc_otg_mphi_fix.h"
  67758. +
  67759. +extern bool microframe_schedule, nak_holdoff_enable;
  67760. +
  67761. +//#define DEBUG_HOST_CHANNELS
  67762. +#ifdef DEBUG_HOST_CHANNELS
  67763. +static int last_sel_trans_num_per_scheduled = 0;
  67764. +static int last_sel_trans_num_nonper_scheduled = 0;
  67765. +static int last_sel_trans_num_avail_hc_at_start = 0;
  67766. +static int last_sel_trans_num_avail_hc_at_end = 0;
  67767. +#endif /* DEBUG_HOST_CHANNELS */
  67768. +
  67769. +extern int g_next_sched_frame, g_np_count, g_np_sent;
  67770. +
  67771. +extern haint_data_t haint_saved;
  67772. +extern hcintmsk_data_t hcintmsk_saved[MAX_EPS_CHANNELS];
  67773. +extern hcint_data_t hcint_saved[MAX_EPS_CHANNELS];
  67774. +extern gintsts_data_t ginsts_saved;
  67775. +
  67776. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  67777. +{
  67778. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  67779. +}
  67780. +
  67781. +/**
  67782. + * Connection timeout function. An OTG host is required to display a
  67783. + * message if the device does not connect within 10 seconds.
  67784. + */
  67785. +void dwc_otg_hcd_connect_timeout(void *ptr)
  67786. +{
  67787. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  67788. + DWC_PRINTF("Connect Timeout\n");
  67789. + __DWC_ERROR("Device Not Connected/Responding\n");
  67790. +}
  67791. +
  67792. +#if defined(DEBUG)
  67793. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  67794. +{
  67795. + if (qh->channel != NULL) {
  67796. + dwc_hc_t *hc = qh->channel;
  67797. + dwc_list_link_t *item;
  67798. + dwc_otg_qh_t *qh_item;
  67799. + int num_channels = hcd->core_if->core_params->host_channels;
  67800. + int i;
  67801. +
  67802. + dwc_otg_hc_regs_t *hc_regs;
  67803. + hcchar_data_t hcchar;
  67804. + hcsplt_data_t hcsplt;
  67805. + hctsiz_data_t hctsiz;
  67806. + uint32_t hcdma;
  67807. +
  67808. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  67809. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  67810. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  67811. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  67812. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  67813. +
  67814. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  67815. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  67816. + hcsplt.d32);
  67817. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  67818. + hcdma);
  67819. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  67820. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  67821. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  67822. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  67823. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  67824. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  67825. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  67826. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  67827. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  67828. + DWC_PRINTF(" qh: %p\n", hc->qh);
  67829. + DWC_PRINTF(" NP inactive sched:\n");
  67830. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  67831. + qh_item =
  67832. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  67833. + DWC_PRINTF(" %p\n", qh_item);
  67834. + }
  67835. + DWC_PRINTF(" NP active sched:\n");
  67836. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  67837. + qh_item =
  67838. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  67839. + DWC_PRINTF(" %p\n", qh_item);
  67840. + }
  67841. + DWC_PRINTF(" Channels: \n");
  67842. + for (i = 0; i < num_channels; i++) {
  67843. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  67844. + DWC_PRINTF(" %2d: %p\n", i, hc);
  67845. + }
  67846. + }
  67847. +}
  67848. +#else
  67849. +#define dump_channel_info(hcd, qh)
  67850. +#endif /* DEBUG */
  67851. +
  67852. +/**
  67853. + * Work queue function for starting the HCD when A-Cable is connected.
  67854. + * The hcd_start() must be called in a process context.
  67855. + */
  67856. +static void hcd_start_func(void *_vp)
  67857. +{
  67858. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  67859. +
  67860. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  67861. + if (hcd) {
  67862. + hcd->fops->start(hcd);
  67863. + }
  67864. +}
  67865. +
  67866. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  67867. +{
  67868. +#ifdef DEBUG
  67869. + int i;
  67870. + int num_channels = hcd->core_if->core_params->host_channels;
  67871. + for (i = 0; i < num_channels; i++) {
  67872. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  67873. + }
  67874. +#endif
  67875. +}
  67876. +
  67877. +static void del_timers(dwc_otg_hcd_t * hcd)
  67878. +{
  67879. + del_xfer_timers(hcd);
  67880. + DWC_TIMER_CANCEL(hcd->conn_timer);
  67881. +}
  67882. +
  67883. +/**
  67884. + * Processes all the URBs in a single list of QHs. Completes them with
  67885. + * -ESHUTDOWN and frees the QTD.
  67886. + */
  67887. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  67888. +{
  67889. + dwc_list_link_t *qh_item, *qh_tmp;
  67890. + dwc_otg_qh_t *qh;
  67891. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  67892. +
  67893. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  67894. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  67895. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  67896. + &qh->qtd_list, qtd_list_entry) {
  67897. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  67898. + if (qtd->urb != NULL) {
  67899. + hcd->fops->complete(hcd, qtd->urb->priv,
  67900. + qtd->urb, -DWC_E_SHUTDOWN);
  67901. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  67902. + }
  67903. +
  67904. + }
  67905. + if(qh->channel) {
  67906. + /* Using hcchar.chen == 1 is not a reliable test.
  67907. + * It is possible that the channel has already halted
  67908. + * but not yet been through the IRQ handler.
  67909. + */
  67910. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  67911. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  67912. + if(microframe_schedule)
  67913. + hcd->available_host_channels++;
  67914. + qh->channel = NULL;
  67915. + }
  67916. + dwc_otg_hcd_qh_remove(hcd, qh);
  67917. + }
  67918. +}
  67919. +
  67920. +/**
  67921. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  67922. + * and periodic schedules. The QTD associated with each URB is removed from
  67923. + * the schedule and freed. This function may be called when a disconnect is
  67924. + * detected or when the HCD is being stopped.
  67925. + */
  67926. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  67927. +{
  67928. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  67929. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  67930. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  67931. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  67932. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  67933. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  67934. +}
  67935. +
  67936. +/**
  67937. + * Start the connection timer. An OTG host is required to display a
  67938. + * message if the device does not connect within 10 seconds. The
  67939. + * timer is deleted if a port connect interrupt occurs before the
  67940. + * timer expires.
  67941. + */
  67942. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  67943. +{
  67944. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  67945. +}
  67946. +
  67947. +/**
  67948. + * HCD Callback function for disconnect of the HCD.
  67949. + *
  67950. + * @param p void pointer to the <code>struct usb_hcd</code>
  67951. + */
  67952. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  67953. +{
  67954. + dwc_otg_hcd_t *dwc_otg_hcd;
  67955. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  67956. + dwc_otg_hcd = p;
  67957. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  67958. + return 1;
  67959. +}
  67960. +
  67961. +/**
  67962. + * HCD Callback function for starting the HCD when A-Cable is
  67963. + * connected.
  67964. + *
  67965. + * @param p void pointer to the <code>struct usb_hcd</code>
  67966. + */
  67967. +static int32_t dwc_otg_hcd_start_cb(void *p)
  67968. +{
  67969. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  67970. + dwc_otg_core_if_t *core_if;
  67971. + hprt0_data_t hprt0;
  67972. +
  67973. + core_if = dwc_otg_hcd->core_if;
  67974. +
  67975. + if (core_if->op_state == B_HOST) {
  67976. + /*
  67977. + * Reset the port. During a HNP mode switch the reset
  67978. + * needs to occur within 1ms and have a duration of at
  67979. + * least 50ms.
  67980. + */
  67981. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  67982. + hprt0.b.prtrst = 1;
  67983. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  67984. + }
  67985. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  67986. + hcd_start_func, dwc_otg_hcd, 50,
  67987. + "start hcd");
  67988. +
  67989. + return 1;
  67990. +}
  67991. +
  67992. +/**
  67993. + * HCD Callback function for disconnect of the HCD.
  67994. + *
  67995. + * @param p void pointer to the <code>struct usb_hcd</code>
  67996. + */
  67997. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  67998. +{
  67999. + gintsts_data_t intr;
  68000. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  68001. +
  68002. + /*
  68003. + * Set status flags for the hub driver.
  68004. + */
  68005. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  68006. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  68007. + if(fiq_fix_enable)
  68008. + local_fiq_disable();
  68009. + /*
  68010. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  68011. + * interrupt mask and status bits and disabling subsequent host
  68012. + * channel interrupts.
  68013. + */
  68014. + intr.d32 = 0;
  68015. + intr.b.nptxfempty = 1;
  68016. + intr.b.ptxfempty = 1;
  68017. + intr.b.hcintr = 1;
  68018. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  68019. + intr.d32, 0);
  68020. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  68021. + intr.d32, 0);
  68022. +
  68023. + del_timers(dwc_otg_hcd);
  68024. +
  68025. + /*
  68026. + * Turn off the vbus power only if the core has transitioned to device
  68027. + * mode. If still in host mode, need to keep power on to detect a
  68028. + * reconnection.
  68029. + */
  68030. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  68031. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  68032. + hprt0_data_t hprt0 = {.d32 = 0 };
  68033. + DWC_PRINTF("Disconnect: PortPower off\n");
  68034. + hprt0.b.prtpwr = 0;
  68035. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  68036. + hprt0.d32);
  68037. + }
  68038. +
  68039. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  68040. + }
  68041. +
  68042. + /* Respond with an error status to all URBs in the schedule. */
  68043. + kill_all_urbs(dwc_otg_hcd);
  68044. +
  68045. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  68046. + /* Clean up any host channels that were in use. */
  68047. + int num_channels;
  68048. + int i;
  68049. + dwc_hc_t *channel;
  68050. + dwc_otg_hc_regs_t *hc_regs;
  68051. + hcchar_data_t hcchar;
  68052. +
  68053. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  68054. +
  68055. + if (!dwc_otg_hcd->core_if->dma_enable) {
  68056. + /* Flush out any channel requests in slave mode. */
  68057. + for (i = 0; i < num_channels; i++) {
  68058. + channel = dwc_otg_hcd->hc_ptr_array[i];
  68059. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  68060. + (channel, hc_list_entry)) {
  68061. + hc_regs =
  68062. + dwc_otg_hcd->core_if->
  68063. + host_if->hc_regs[i];
  68064. + hcchar.d32 =
  68065. + DWC_READ_REG32(&hc_regs->hcchar);
  68066. + if (hcchar.b.chen) {
  68067. + hcchar.b.chen = 0;
  68068. + hcchar.b.chdis = 1;
  68069. + hcchar.b.epdir = 0;
  68070. + DWC_WRITE_REG32
  68071. + (&hc_regs->hcchar,
  68072. + hcchar.d32);
  68073. + }
  68074. + }
  68075. + }
  68076. + }
  68077. +
  68078. + for (i = 0; i < num_channels; i++) {
  68079. + channel = dwc_otg_hcd->hc_ptr_array[i];
  68080. + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
  68081. + hc_regs =
  68082. + dwc_otg_hcd->core_if->host_if->hc_regs[i];
  68083. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  68084. + if (hcchar.b.chen) {
  68085. + /* Halt the channel. */
  68086. + hcchar.b.chdis = 1;
  68087. + DWC_WRITE_REG32(&hc_regs->hcchar,
  68088. + hcchar.d32);
  68089. + }
  68090. +
  68091. + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
  68092. + channel);
  68093. + DWC_CIRCLEQ_INSERT_TAIL
  68094. + (&dwc_otg_hcd->free_hc_list, channel,
  68095. + hc_list_entry);
  68096. + /*
  68097. + * Added for Descriptor DMA to prevent channel double cleanup
  68098. + * in release_channel_ddma(). Which called from ep_disable
  68099. + * when device disconnect.
  68100. + */
  68101. + channel->qh = NULL;
  68102. + }
  68103. + }
  68104. + if(fiq_split_enable) {
  68105. + for(i=0; i < 128; i++) {
  68106. + dwc_otg_hcd->hub_port[i] = 0;
  68107. + }
  68108. + haint_saved.d32 = 0;
  68109. + for(i=0; i < MAX_EPS_CHANNELS; i++) {
  68110. + hcint_saved[i].d32 = 0;
  68111. + hcintmsk_saved[i].d32 = 0;
  68112. + }
  68113. + }
  68114. +
  68115. + }
  68116. +
  68117. + if(fiq_fix_enable)
  68118. + local_fiq_enable();
  68119. +
  68120. + if (dwc_otg_hcd->fops->disconnect) {
  68121. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  68122. + }
  68123. +
  68124. + return 1;
  68125. +}
  68126. +
  68127. +/**
  68128. + * HCD Callback function for stopping the HCD.
  68129. + *
  68130. + * @param p void pointer to the <code>struct usb_hcd</code>
  68131. + */
  68132. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  68133. +{
  68134. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  68135. +
  68136. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  68137. + dwc_otg_hcd_stop(dwc_otg_hcd);
  68138. + return 1;
  68139. +}
  68140. +
  68141. +#ifdef CONFIG_USB_DWC_OTG_LPM
  68142. +/**
  68143. + * HCD Callback function for sleep of HCD.
  68144. + *
  68145. + * @param p void pointer to the <code>struct usb_hcd</code>
  68146. + */
  68147. +static int dwc_otg_hcd_sleep_cb(void *p)
  68148. +{
  68149. + dwc_otg_hcd_t *hcd = p;
  68150. +
  68151. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  68152. +
  68153. + return 0;
  68154. +}
  68155. +#endif
  68156. +
  68157. +
  68158. +/**
  68159. + * HCD Callback function for Remote Wakeup.
  68160. + *
  68161. + * @param p void pointer to the <code>struct usb_hcd</code>
  68162. + */
  68163. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  68164. +{
  68165. + dwc_otg_hcd_t *hcd = p;
  68166. +
  68167. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  68168. + hcd->flags.b.port_suspend_change = 1;
  68169. + }
  68170. +#ifdef CONFIG_USB_DWC_OTG_LPM
  68171. + else {
  68172. + hcd->flags.b.port_l1_change = 1;
  68173. + }
  68174. +#endif
  68175. + return 0;
  68176. +}
  68177. +
  68178. +/**
  68179. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  68180. + * stopped.
  68181. + */
  68182. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  68183. +{
  68184. + hprt0_data_t hprt0 = {.d32 = 0 };
  68185. +
  68186. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  68187. +
  68188. + /*
  68189. + * The root hub should be disconnected before this function is called.
  68190. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  68191. + * and the QH lists (via ..._hcd_endpoint_disable).
  68192. + */
  68193. +
  68194. + /* Turn off all host-specific interrupts. */
  68195. + dwc_otg_disable_host_interrupts(hcd->core_if);
  68196. +
  68197. + /* Turn off the vbus power */
  68198. + DWC_PRINTF("PortPower off\n");
  68199. + hprt0.b.prtpwr = 0;
  68200. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  68201. + dwc_mdelay(1);
  68202. +}
  68203. +
  68204. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  68205. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  68206. + int atomic_alloc)
  68207. +{
  68208. + int retval = 0;
  68209. + uint8_t needs_scheduling = 0;
  68210. + dwc_otg_transaction_type_e tr_type;
  68211. + dwc_otg_qtd_t *qtd;
  68212. + gintmsk_data_t intr_mask = {.d32 = 0 };
  68213. + hprt0_data_t hprt0 = { .d32 = 0 };
  68214. +
  68215. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68216. + if (NULL == hcd->core_if) {
  68217. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  68218. + /* No longer connected. */
  68219. + return -DWC_E_INVALID;
  68220. + }
  68221. +#endif
  68222. + if (!hcd->flags.b.port_connect_status) {
  68223. + /* No longer connected. */
  68224. + DWC_ERROR("Not connected\n");
  68225. + return -DWC_E_NO_DEVICE;
  68226. + }
  68227. +
  68228. + /* Some core configurations cannot support LS traffic on a FS root port */
  68229. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  68230. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  68231. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  68232. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  68233. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  68234. + return -DWC_E_NO_DEVICE;
  68235. + }
  68236. + }
  68237. +
  68238. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  68239. + if (qtd == NULL) {
  68240. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  68241. + return -DWC_E_NO_MEMORY;
  68242. + }
  68243. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68244. + if (qtd->urb == NULL) {
  68245. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  68246. + return -DWC_E_NO_MEMORY;
  68247. + }
  68248. + if (qtd->urb->priv == NULL) {
  68249. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  68250. + return -DWC_E_NO_MEMORY;
  68251. + }
  68252. +#endif
  68253. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  68254. + if(!intr_mask.b.sofintr) needs_scheduling = 1;
  68255. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  68256. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  68257. + needs_scheduling = 0;
  68258. +
  68259. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  68260. + // creates a new queue in ep_handle if it doesn't exist already
  68261. + if (retval < 0) {
  68262. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  68263. + "Error status %d\n", retval);
  68264. + dwc_otg_hcd_qtd_free(qtd);
  68265. + return retval;
  68266. + }
  68267. +
  68268. + if(needs_scheduling) {
  68269. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  68270. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  68271. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  68272. + }
  68273. + }
  68274. + return retval;
  68275. +}
  68276. +
  68277. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  68278. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  68279. +{
  68280. + dwc_otg_qh_t *qh;
  68281. + dwc_otg_qtd_t *urb_qtd;
  68282. + BUG_ON(!hcd);
  68283. + BUG_ON(!dwc_otg_urb);
  68284. +
  68285. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68286. +
  68287. + if (hcd == NULL) {
  68288. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  68289. + return -DWC_E_INVALID;
  68290. + }
  68291. + if (dwc_otg_urb == NULL) {
  68292. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  68293. + return -DWC_E_INVALID;
  68294. + }
  68295. + if (dwc_otg_urb->qtd == NULL) {
  68296. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  68297. + return -DWC_E_INVALID;
  68298. + }
  68299. + urb_qtd = dwc_otg_urb->qtd;
  68300. + BUG_ON(!urb_qtd);
  68301. + if (urb_qtd->qh == NULL) {
  68302. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  68303. + return -DWC_E_INVALID;
  68304. + }
  68305. +#else
  68306. + urb_qtd = dwc_otg_urb->qtd;
  68307. + BUG_ON(!urb_qtd);
  68308. +#endif
  68309. + qh = urb_qtd->qh;
  68310. + BUG_ON(!qh);
  68311. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  68312. + if (urb_qtd->in_process) {
  68313. + dump_channel_info(hcd, qh);
  68314. + }
  68315. + }
  68316. +#ifdef DEBUG /* integrity checks (Broadcom) */
  68317. + if (hcd->core_if == NULL) {
  68318. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  68319. + return -DWC_E_INVALID;
  68320. + }
  68321. +#endif
  68322. + if (urb_qtd->in_process && qh->channel) {
  68323. + /* The QTD is in process (it has been assigned to a channel). */
  68324. + if (hcd->flags.b.port_connect_status) {
  68325. + /*
  68326. + * If still connected (i.e. in host mode), halt the
  68327. + * channel so it can be used for other transfers. If
  68328. + * no longer connected, the host registers can't be
  68329. + * written to halt the channel since the core is in
  68330. + * device mode.
  68331. + */
  68332. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  68333. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  68334. +
  68335. + dwc_otg_hcd_release_port(hcd, qh);
  68336. + }
  68337. + }
  68338. +
  68339. + /*
  68340. + * Free the QTD and clean up the associated QH. Leave the QH in the
  68341. + * schedule if it has any remaining QTDs.
  68342. + */
  68343. +
  68344. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  68345. + "delete %sQueue handler\n",
  68346. + hcd->core_if->dma_desc_enable?"DMA ":"");
  68347. + if (!hcd->core_if->dma_desc_enable) {
  68348. + uint8_t b = urb_qtd->in_process;
  68349. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  68350. + if (b) {
  68351. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  68352. + qh->channel = NULL;
  68353. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  68354. + dwc_otg_hcd_qh_remove(hcd, qh);
  68355. + }
  68356. + } else {
  68357. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  68358. + }
  68359. + return 0;
  68360. +}
  68361. +
  68362. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  68363. + int retry)
  68364. +{
  68365. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  68366. + int retval = 0;
  68367. + dwc_irqflags_t flags;
  68368. +
  68369. + if (retry < 0) {
  68370. + retval = -DWC_E_INVALID;
  68371. + goto done;
  68372. + }
  68373. +
  68374. + if (!qh) {
  68375. + retval = -DWC_E_INVALID;
  68376. + goto done;
  68377. + }
  68378. +
  68379. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  68380. +
  68381. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  68382. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  68383. + retry--;
  68384. + dwc_msleep(5);
  68385. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  68386. + }
  68387. +
  68388. + dwc_otg_hcd_qh_remove(hcd, qh);
  68389. +
  68390. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  68391. + /*
  68392. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  68393. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  68394. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  68395. + * and dwc_otg_hcd_frame_list_alloc().
  68396. + */
  68397. + dwc_otg_hcd_qh_free(hcd, qh);
  68398. +
  68399. +done:
  68400. + return retval;
  68401. +}
  68402. +
  68403. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  68404. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  68405. +{
  68406. + int retval = 0;
  68407. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  68408. + if (!qh)
  68409. + return -DWC_E_INVALID;
  68410. +
  68411. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  68412. + return retval;
  68413. +}
  68414. +#endif
  68415. +
  68416. +/**
  68417. + * HCD Callback structure for handling mode switching.
  68418. + */
  68419. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  68420. + .start = dwc_otg_hcd_start_cb,
  68421. + .stop = dwc_otg_hcd_stop_cb,
  68422. + .disconnect = dwc_otg_hcd_disconnect_cb,
  68423. + .session_start = dwc_otg_hcd_session_start_cb,
  68424. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  68425. +#ifdef CONFIG_USB_DWC_OTG_LPM
  68426. + .sleep = dwc_otg_hcd_sleep_cb,
  68427. +#endif
  68428. + .p = 0,
  68429. +};
  68430. +
  68431. +/**
  68432. + * Reset tasklet function
  68433. + */
  68434. +static void reset_tasklet_func(void *data)
  68435. +{
  68436. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  68437. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  68438. + hprt0_data_t hprt0;
  68439. +
  68440. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  68441. +
  68442. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  68443. + hprt0.b.prtrst = 1;
  68444. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  68445. + dwc_mdelay(60);
  68446. +
  68447. + hprt0.b.prtrst = 0;
  68448. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  68449. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  68450. +}
  68451. +
  68452. +static void completion_tasklet_func(void *ptr)
  68453. +{
  68454. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  68455. + struct urb *urb;
  68456. + urb_tq_entry_t *item;
  68457. + dwc_irqflags_t flags;
  68458. +
  68459. + /* This could just be spin_lock_irq */
  68460. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  68461. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  68462. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  68463. + urb = item->urb;
  68464. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  68465. + urb_tq_entries);
  68466. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  68467. + DWC_FREE(item);
  68468. +
  68469. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  68470. +
  68471. + fiq_print(FIQDBG_PORTHUB, "COMPLETE");
  68472. +
  68473. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  68474. + }
  68475. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  68476. + return;
  68477. +}
  68478. +
  68479. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  68480. +{
  68481. + dwc_list_link_t *item;
  68482. + dwc_otg_qh_t *qh;
  68483. + dwc_irqflags_t flags;
  68484. +
  68485. + if (!qh_list->next) {
  68486. + /* The list hasn't been initialized yet. */
  68487. + return;
  68488. + }
  68489. + /*
  68490. + * Hold spinlock here. Not needed in that case if bellow
  68491. + * function is being called from ISR
  68492. + */
  68493. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  68494. + /* Ensure there are no QTDs or URBs left. */
  68495. + kill_urbs_in_qh_list(hcd, qh_list);
  68496. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  68497. +
  68498. + DWC_LIST_FOREACH(item, qh_list) {
  68499. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  68500. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  68501. + }
  68502. +}
  68503. +
  68504. +/**
  68505. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  68506. + * Device during SRP time by host power up.
  68507. + */
  68508. +void dwc_otg_hcd_power_up(void *ptr)
  68509. +{
  68510. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  68511. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  68512. +
  68513. + DWC_PRINTF("%s called\n", __FUNCTION__);
  68514. +
  68515. + if (!core_if->hibernation_suspend) {
  68516. + DWC_PRINTF("Already exited from Hibernation\n");
  68517. + return;
  68518. + }
  68519. +
  68520. + /* Switch on the voltage to the core */
  68521. + gpwrdn.b.pwrdnswtch = 1;
  68522. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  68523. + dwc_udelay(10);
  68524. +
  68525. + /* Reset the core */
  68526. + gpwrdn.d32 = 0;
  68527. + gpwrdn.b.pwrdnrstn = 1;
  68528. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  68529. + dwc_udelay(10);
  68530. +
  68531. + /* Disable power clamps */
  68532. + gpwrdn.d32 = 0;
  68533. + gpwrdn.b.pwrdnclmp = 1;
  68534. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  68535. +
  68536. + /* Remove reset the core signal */
  68537. + gpwrdn.d32 = 0;
  68538. + gpwrdn.b.pwrdnrstn = 1;
  68539. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  68540. + dwc_udelay(10);
  68541. +
  68542. + /* Disable PMU interrupt */
  68543. + gpwrdn.d32 = 0;
  68544. + gpwrdn.b.pmuintsel = 1;
  68545. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  68546. +
  68547. + core_if->hibernation_suspend = 0;
  68548. +
  68549. + /* Disable PMU */
  68550. + gpwrdn.d32 = 0;
  68551. + gpwrdn.b.pmuactv = 1;
  68552. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  68553. + dwc_udelay(10);
  68554. +
  68555. + /* Enable VBUS */
  68556. + gpwrdn.d32 = 0;
  68557. + gpwrdn.b.dis_vbus = 1;
  68558. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  68559. +
  68560. + core_if->op_state = A_HOST;
  68561. + dwc_otg_core_init(core_if);
  68562. + dwc_otg_enable_global_interrupts(core_if);
  68563. + cil_hcd_start(core_if);
  68564. +}
  68565. +
  68566. +/**
  68567. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  68568. + * in the struct usb_hcd field.
  68569. + */
  68570. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  68571. +{
  68572. + int i;
  68573. +
  68574. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  68575. +
  68576. + del_timers(dwc_otg_hcd);
  68577. +
  68578. + /* Free memory for QH/QTD lists */
  68579. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  68580. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  68581. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  68582. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  68583. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  68584. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  68585. +
  68586. + /* Free memory for the host channels. */
  68587. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  68588. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  68589. +
  68590. +#ifdef DEBUG
  68591. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  68592. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  68593. + }
  68594. +#endif
  68595. + if (hc != NULL) {
  68596. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  68597. + i, hc);
  68598. + DWC_FREE(hc);
  68599. + }
  68600. + }
  68601. +
  68602. + if (dwc_otg_hcd->core_if->dma_enable) {
  68603. + if (dwc_otg_hcd->status_buf_dma) {
  68604. + DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
  68605. + dwc_otg_hcd->status_buf,
  68606. + dwc_otg_hcd->status_buf_dma);
  68607. + }
  68608. + } else if (dwc_otg_hcd->status_buf != NULL) {
  68609. + DWC_FREE(dwc_otg_hcd->status_buf);
  68610. + }
  68611. + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
  68612. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  68613. + /* Set core_if's lock pointer to NULL */
  68614. + dwc_otg_hcd->core_if->lock = NULL;
  68615. +
  68616. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  68617. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  68618. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  68619. +
  68620. +#ifdef DWC_DEV_SRPCAP
  68621. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  68622. + dwc_otg_hcd->core_if->pwron_timer) {
  68623. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  68624. + }
  68625. +#endif
  68626. + DWC_FREE(dwc_otg_hcd);
  68627. +}
  68628. +
  68629. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  68630. +
  68631. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  68632. +{
  68633. + int retval = 0;
  68634. + int num_channels;
  68635. + int i;
  68636. + dwc_hc_t *channel;
  68637. +
  68638. + hcd->lock = DWC_SPINLOCK_ALLOC();
  68639. + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
  68640. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  68641. + hcd, core_if);
  68642. + if (!hcd->lock) {
  68643. + DWC_ERROR("Could not allocate lock for pcd");
  68644. + DWC_FREE(hcd);
  68645. + retval = -DWC_E_NO_MEMORY;
  68646. + goto out;
  68647. + }
  68648. + hcd->core_if = core_if;
  68649. +
  68650. + /* Register the HCD CIL Callbacks */
  68651. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  68652. + &hcd_cil_callbacks, hcd);
  68653. +
  68654. + /* Initialize the non-periodic schedule. */
  68655. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  68656. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  68657. +
  68658. + /* Initialize the periodic schedule. */
  68659. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  68660. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  68661. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  68662. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  68663. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  68664. + /*
  68665. + * Create a host channel descriptor for each host channel implemented
  68666. + * in the controller. Initialize the channel descriptor array.
  68667. + */
  68668. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  68669. + num_channels = hcd->core_if->core_params->host_channels;
  68670. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  68671. + for (i = 0; i < num_channels; i++) {
  68672. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  68673. + if (channel == NULL) {
  68674. + retval = -DWC_E_NO_MEMORY;
  68675. + DWC_ERROR("%s: host channel allocation failed\n",
  68676. + __func__);
  68677. + dwc_otg_hcd_free(hcd);
  68678. + goto out;
  68679. + }
  68680. + channel->hc_num = i;
  68681. + hcd->hc_ptr_array[i] = channel;
  68682. +#ifdef DEBUG
  68683. + hcd->core_if->hc_xfer_timer[i] =
  68684. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  68685. + &hcd->core_if->hc_xfer_info[i]);
  68686. +#endif
  68687. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  68688. + channel);
  68689. + }
  68690. +
  68691. + /* Initialize the Connection timeout timer. */
  68692. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  68693. + dwc_otg_hcd_connect_timeout, 0);
  68694. +
  68695. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  68696. + if (microframe_schedule)
  68697. + init_hcd_usecs(hcd);
  68698. +
  68699. + /* Initialize reset tasklet. */
  68700. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  68701. +
  68702. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  68703. + completion_tasklet_func, hcd);
  68704. +#ifdef DWC_DEV_SRPCAP
  68705. + if (hcd->core_if->power_down == 2) {
  68706. + /* Initialize Power on timer for Host power up in case hibernation */
  68707. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  68708. + dwc_otg_hcd_power_up, core_if);
  68709. + }
  68710. +#endif
  68711. +
  68712. + /*
  68713. + * Allocate space for storing data on status transactions. Normally no
  68714. + * data is sent, but this space acts as a bit bucket. This must be
  68715. + * done after usb_add_hcd since that function allocates the DMA buffer
  68716. + * pool.
  68717. + */
  68718. + if (hcd->core_if->dma_enable) {
  68719. + hcd->status_buf =
  68720. + DWC_DMA_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE,
  68721. + &hcd->status_buf_dma);
  68722. + } else {
  68723. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  68724. + }
  68725. + if (!hcd->status_buf) {
  68726. + retval = -DWC_E_NO_MEMORY;
  68727. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  68728. + dwc_otg_hcd_free(hcd);
  68729. + goto out;
  68730. + }
  68731. +
  68732. + hcd->otg_port = 1;
  68733. + hcd->frame_list = NULL;
  68734. + hcd->frame_list_dma = 0;
  68735. + hcd->periodic_qh_count = 0;
  68736. +
  68737. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  68738. +#ifdef FIQ_DEBUG
  68739. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  68740. +#endif
  68741. +
  68742. +out:
  68743. + return retval;
  68744. +}
  68745. +
  68746. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  68747. +{
  68748. + /* Turn off all host-specific interrupts. */
  68749. + dwc_otg_disable_host_interrupts(hcd->core_if);
  68750. +
  68751. + dwc_otg_hcd_free(hcd);
  68752. +}
  68753. +
  68754. +/**
  68755. + * Initializes dynamic portions of the DWC_otg HCD state.
  68756. + */
  68757. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  68758. +{
  68759. + int num_channels;
  68760. + int i;
  68761. + dwc_hc_t *channel;
  68762. + dwc_hc_t *channel_tmp;
  68763. +
  68764. + hcd->flags.d32 = 0;
  68765. +
  68766. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  68767. + if (!microframe_schedule) {
  68768. + hcd->non_periodic_channels = 0;
  68769. + hcd->periodic_channels = 0;
  68770. + } else {
  68771. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  68772. + }
  68773. + /*
  68774. + * Put all channels in the free channel list and clean up channel
  68775. + * states.
  68776. + */
  68777. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  68778. + &hcd->free_hc_list, hc_list_entry) {
  68779. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  68780. + }
  68781. +
  68782. + num_channels = hcd->core_if->core_params->host_channels;
  68783. + for (i = 0; i < num_channels; i++) {
  68784. + channel = hcd->hc_ptr_array[i];
  68785. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  68786. + hc_list_entry);
  68787. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  68788. + }
  68789. +
  68790. + /* Initialize the DWC core for host mode operation. */
  68791. + dwc_otg_core_host_init(hcd->core_if);
  68792. +
  68793. + /* Set core_if's lock pointer to the hcd->lock */
  68794. + hcd->core_if->lock = hcd->lock;
  68795. +}
  68796. +
  68797. +/**
  68798. + * Assigns transactions from a QTD to a free host channel and initializes the
  68799. + * host channel to perform the transactions. The host channel is removed from
  68800. + * the free list.
  68801. + *
  68802. + * @param hcd The HCD state structure.
  68803. + * @param qh Transactions from the first QTD for this QH are selected and
  68804. + * assigned to a free host channel.
  68805. + */
  68806. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  68807. +{
  68808. + dwc_hc_t *hc;
  68809. + dwc_otg_qtd_t *qtd;
  68810. + dwc_otg_hcd_urb_t *urb;
  68811. + void* ptr = NULL;
  68812. +
  68813. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  68814. +
  68815. + urb = qtd->urb;
  68816. +
  68817. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  68818. +
  68819. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  68820. + urb->actual_length = urb->length;
  68821. +
  68822. +
  68823. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  68824. +
  68825. + /* Remove the host channel from the free list. */
  68826. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  68827. +
  68828. + qh->channel = hc;
  68829. +
  68830. + qtd->in_process = 1;
  68831. +
  68832. + /*
  68833. + * Use usb_pipedevice to determine device address. This address is
  68834. + * 0 before the SET_ADDRESS command and the correct address afterward.
  68835. + */
  68836. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  68837. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  68838. + hc->speed = qh->dev_speed;
  68839. + hc->max_packet = dwc_max_packet(qh->maxp);
  68840. +
  68841. + hc->xfer_started = 0;
  68842. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  68843. + hc->error_state = (qtd->error_count > 0);
  68844. + hc->halt_on_queue = 0;
  68845. + hc->halt_pending = 0;
  68846. + hc->requests = 0;
  68847. +
  68848. + /*
  68849. + * The following values may be modified in the transfer type section
  68850. + * below. The xfer_len value may be reduced when the transfer is
  68851. + * started to accommodate the max widths of the XferSize and PktCnt
  68852. + * fields in the HCTSIZn register.
  68853. + */
  68854. +
  68855. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  68856. + if (hc->ep_is_in) {
  68857. + hc->do_ping = 0;
  68858. + } else {
  68859. + hc->do_ping = qh->ping_state;
  68860. + }
  68861. +
  68862. + hc->data_pid_start = qh->data_toggle;
  68863. + hc->multi_count = 1;
  68864. +
  68865. + if (hcd->core_if->dma_enable) {
  68866. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  68867. +
  68868. + /* For non-dword aligned case */
  68869. + if (((unsigned long)hc->xfer_buff & 0x3)
  68870. + && !hcd->core_if->dma_desc_enable) {
  68871. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  68872. + }
  68873. + } else {
  68874. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  68875. + }
  68876. + hc->xfer_len = urb->length - urb->actual_length;
  68877. + hc->xfer_count = 0;
  68878. +
  68879. + /*
  68880. + * Set the split attributes
  68881. + */
  68882. + hc->do_split = 0;
  68883. + if (qh->do_split) {
  68884. + uint32_t hub_addr, port_addr;
  68885. + hc->do_split = 1;
  68886. + hc->xact_pos = qtd->isoc_split_pos;
  68887. + /* We don't need to do complete splits anymore */
  68888. + if(fiq_split_enable)
  68889. + hc->complete_split = qtd->complete_split = 0;
  68890. + else
  68891. + hc->complete_split = qtd->complete_split;
  68892. +
  68893. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  68894. + hc->hub_addr = (uint8_t) hub_addr;
  68895. + hc->port_addr = (uint8_t) port_addr;
  68896. + }
  68897. +
  68898. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  68899. + case UE_CONTROL:
  68900. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  68901. + switch (qtd->control_phase) {
  68902. + case DWC_OTG_CONTROL_SETUP:
  68903. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  68904. + hc->do_ping = 0;
  68905. + hc->ep_is_in = 0;
  68906. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  68907. + if (hcd->core_if->dma_enable) {
  68908. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  68909. + } else {
  68910. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  68911. + }
  68912. + hc->xfer_len = 8;
  68913. + ptr = NULL;
  68914. + break;
  68915. + case DWC_OTG_CONTROL_DATA:
  68916. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  68917. + hc->data_pid_start = qtd->data_toggle;
  68918. + break;
  68919. + case DWC_OTG_CONTROL_STATUS:
  68920. + /*
  68921. + * Direction is opposite of data direction or IN if no
  68922. + * data.
  68923. + */
  68924. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  68925. + if (urb->length == 0) {
  68926. + hc->ep_is_in = 1;
  68927. + } else {
  68928. + hc->ep_is_in =
  68929. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  68930. + }
  68931. + if (hc->ep_is_in) {
  68932. + hc->do_ping = 0;
  68933. + }
  68934. +
  68935. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  68936. +
  68937. + hc->xfer_len = 0;
  68938. + if (hcd->core_if->dma_enable) {
  68939. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  68940. + } else {
  68941. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  68942. + }
  68943. + ptr = NULL;
  68944. + break;
  68945. + }
  68946. + break;
  68947. + case UE_BULK:
  68948. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  68949. + break;
  68950. + case UE_INTERRUPT:
  68951. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  68952. + break;
  68953. + case UE_ISOCHRONOUS:
  68954. + {
  68955. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  68956. +
  68957. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  68958. +
  68959. + if (hcd->core_if->dma_desc_enable)
  68960. + break;
  68961. +
  68962. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  68963. +
  68964. + frame_desc->status = 0;
  68965. +
  68966. + if (hcd->core_if->dma_enable) {
  68967. + hc->xfer_buff = (uint8_t *) urb->dma;
  68968. + } else {
  68969. + hc->xfer_buff = (uint8_t *) urb->buf;
  68970. + }
  68971. + hc->xfer_buff +=
  68972. + frame_desc->offset + qtd->isoc_split_offset;
  68973. + hc->xfer_len =
  68974. + frame_desc->length - qtd->isoc_split_offset;
  68975. +
  68976. + /* For non-dword aligned buffers */
  68977. + if (((unsigned long)hc->xfer_buff & 0x3)
  68978. + && hcd->core_if->dma_enable) {
  68979. + ptr =
  68980. + (uint8_t *) urb->buf + frame_desc->offset +
  68981. + qtd->isoc_split_offset;
  68982. + } else
  68983. + ptr = NULL;
  68984. +
  68985. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  68986. + if (hc->xfer_len <= 188) {
  68987. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  68988. + } else {
  68989. + hc->xact_pos =
  68990. + DWC_HCSPLIT_XACTPOS_BEGIN;
  68991. + }
  68992. + }
  68993. + }
  68994. + break;
  68995. + }
  68996. + /* non DWORD-aligned buffer case */
  68997. + if (ptr) {
  68998. + uint32_t buf_size;
  68999. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  69000. + buf_size = hcd->core_if->core_params->max_transfer_size;
  69001. + } else {
  69002. + buf_size = 4096;
  69003. + }
  69004. + if (!qh->dw_align_buf) {
  69005. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
  69006. + &qh->dw_align_buf_dma);
  69007. + if (!qh->dw_align_buf) {
  69008. + DWC_ERROR
  69009. + ("%s: Failed to allocate memory to handle "
  69010. + "non-dword aligned buffer case\n",
  69011. + __func__);
  69012. + return;
  69013. + }
  69014. + }
  69015. + if (!hc->ep_is_in) {
  69016. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  69017. + }
  69018. + hc->align_buff = qh->dw_align_buf_dma;
  69019. + } else {
  69020. + hc->align_buff = 0;
  69021. + }
  69022. +
  69023. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  69024. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  69025. + /*
  69026. + * This value may be modified when the transfer is started to
  69027. + * reflect the actual transfer length.
  69028. + */
  69029. + hc->multi_count = dwc_hb_mult(qh->maxp);
  69030. + }
  69031. +
  69032. + if (hcd->core_if->dma_desc_enable)
  69033. + hc->desc_list_addr = qh->desc_list_dma;
  69034. +
  69035. + dwc_otg_hc_init(hcd->core_if, hc);
  69036. + hc->qh = qh;
  69037. +}
  69038. +
  69039. +/*
  69040. +** Check the transaction to see if the port / hub has already been assigned for
  69041. +** a split transaction
  69042. +**
  69043. +** Return 0 - Port is already in use
  69044. +*/
  69045. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh)
  69046. +{
  69047. + uint32_t hub_addr, port_addr;
  69048. +
  69049. + if(!fiq_split_enable)
  69050. + return 0;
  69051. +
  69052. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  69053. +
  69054. + if(hcd->hub_port[hub_addr] & (1 << port_addr))
  69055. + {
  69056. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:S%02d", hub_addr, port_addr, qh->skip_count);
  69057. +
  69058. + qh->skip_count++;
  69059. +
  69060. + if(qh->skip_count > 40000)
  69061. + {
  69062. + printk_once(KERN_ERR "Error: Having to skip port allocation");
  69063. + local_fiq_disable();
  69064. + BUG();
  69065. + return 0;
  69066. + }
  69067. + return 1;
  69068. + }
  69069. + else
  69070. + {
  69071. + qh->skip_count = 0;
  69072. + hcd->hub_port[hub_addr] |= 1 << port_addr;
  69073. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:A %d", hub_addr, port_addr, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->pipe_info.ep_num);
  69074. +#ifdef FIQ_DEBUG
  69075. + hcd->hub_port_alloc[hub_addr * 16 + port_addr] = dwc_otg_hcd_get_frame_number(hcd);
  69076. +#endif
  69077. + return 0;
  69078. + }
  69079. +}
  69080. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh)
  69081. +{
  69082. + uint32_t hub_addr, port_addr;
  69083. +
  69084. + if(!fiq_split_enable)
  69085. + return;
  69086. +
  69087. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  69088. +
  69089. + hcd->hub_port[hub_addr] &= ~(1 << port_addr);
  69090. +#ifdef FIQ_DEBUG
  69091. + hcd->hub_port_alloc[hub_addr * 16 + port_addr] = -1;
  69092. +#endif
  69093. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:RO%d", hub_addr, port_addr, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->pipe_info.ep_num);
  69094. +
  69095. +}
  69096. +
  69097. +
  69098. +/**
  69099. + * This function selects transactions from the HCD transfer schedule and
  69100. + * assigns them to available host channels. It is called from HCD interrupt
  69101. + * handler functions.
  69102. + *
  69103. + * @param hcd The HCD state structure.
  69104. + *
  69105. + * @return The types of new transactions that were assigned to host channels.
  69106. + */
  69107. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  69108. +{
  69109. + dwc_list_link_t *qh_ptr;
  69110. + dwc_otg_qh_t *qh;
  69111. + dwc_otg_qtd_t *qtd;
  69112. + int num_channels;
  69113. + dwc_irqflags_t flags;
  69114. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  69115. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  69116. +
  69117. +#ifdef DEBUG_SOF
  69118. + DWC_DEBUGPL(DBG_HCD, " Select Transactions\n");
  69119. +#endif
  69120. +
  69121. +#ifdef DEBUG_HOST_CHANNELS
  69122. + last_sel_trans_num_per_scheduled = 0;
  69123. + last_sel_trans_num_nonper_scheduled = 0;
  69124. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  69125. +#endif /* DEBUG_HOST_CHANNELS */
  69126. +
  69127. + /* Process entries in the periodic ready list. */
  69128. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  69129. +
  69130. + while (qh_ptr != &hcd->periodic_sched_ready &&
  69131. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  69132. +
  69133. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69134. +
  69135. + if(qh->do_split) {
  69136. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  69137. + if(!(qh->ep_type == UE_ISOCHRONOUS &&
  69138. + (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  69139. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END))) {
  69140. + if(dwc_otg_hcd_allocate_port(hcd, qh))
  69141. + {
  69142. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69143. + g_next_sched_frame = dwc_frame_num_inc(dwc_otg_hcd_get_frame_number(hcd), 1);
  69144. + continue;
  69145. + }
  69146. + }
  69147. + }
  69148. +
  69149. + if (microframe_schedule) {
  69150. + // Make sure we leave one channel for non periodic transactions.
  69151. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69152. + if (hcd->available_host_channels <= 1) {
  69153. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69154. + if(qh->do_split) dwc_otg_hcd_release_port(hcd, qh);
  69155. + break;
  69156. + }
  69157. + hcd->available_host_channels--;
  69158. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69159. +#ifdef DEBUG_HOST_CHANNELS
  69160. + last_sel_trans_num_per_scheduled++;
  69161. +#endif /* DEBUG_HOST_CHANNELS */
  69162. + }
  69163. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69164. + assign_and_init_hc(hcd, qh);
  69165. +
  69166. + /*
  69167. + * Move the QH from the periodic ready schedule to the
  69168. + * periodic assigned schedule.
  69169. + */
  69170. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69171. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69172. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  69173. + &qh->qh_list_entry);
  69174. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69175. + }
  69176. +
  69177. + /*
  69178. + * Process entries in the inactive portion of the non-periodic
  69179. + * schedule. Some free host channels may not be used if they are
  69180. + * reserved for periodic transfers.
  69181. + */
  69182. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  69183. + num_channels = hcd->core_if->core_params->host_channels;
  69184. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  69185. + (microframe_schedule || hcd->non_periodic_channels <
  69186. + num_channels - hcd->periodic_channels) &&
  69187. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  69188. +
  69189. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69190. +
  69191. + /*
  69192. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  69193. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  69194. + * cheeky devices that just hold off using NAKs
  69195. + */
  69196. + if (nak_holdoff_enable && qh->do_split) {
  69197. + if (qh->nak_frame != 0xffff &&
  69198. + dwc_full_frame_num(qh->nak_frame) ==
  69199. + dwc_full_frame_num(dwc_otg_hcd_get_frame_number(hcd))) {
  69200. + /*
  69201. + * Revisit: Need to avoid trampling on periodic scheduling.
  69202. + * Currently we are safe because g_np_count != g_np_sent whenever we hit this,
  69203. + * but if this behaviour is changed then periodic endpoints will get a slower
  69204. + * polling rate.
  69205. + */
  69206. + g_next_sched_frame = ((qh->nak_frame + 8) & ~7) & DWC_HFNUM_MAX_FRNUM;
  69207. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69208. + continue;
  69209. + } else {
  69210. + qh->nak_frame = 0xffff;
  69211. + }
  69212. + }
  69213. +
  69214. + if (microframe_schedule) {
  69215. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69216. + if (hcd->available_host_channels < 1) {
  69217. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69218. + break;
  69219. + }
  69220. + hcd->available_host_channels--;
  69221. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69222. +#ifdef DEBUG_HOST_CHANNELS
  69223. + last_sel_trans_num_nonper_scheduled++;
  69224. +#endif /* DEBUG_HOST_CHANNELS */
  69225. + }
  69226. +
  69227. + assign_and_init_hc(hcd, qh);
  69228. +
  69229. + /*
  69230. + * Move the QH from the non-periodic inactive schedule to the
  69231. + * non-periodic active schedule.
  69232. + */
  69233. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  69234. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  69235. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  69236. + &qh->qh_list_entry);
  69237. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  69238. +
  69239. + g_np_sent++;
  69240. +
  69241. + if (!microframe_schedule)
  69242. + hcd->non_periodic_channels++;
  69243. + }
  69244. +
  69245. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  69246. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  69247. +
  69248. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  69249. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  69250. +
  69251. +
  69252. +#ifdef DEBUG_HOST_CHANNELS
  69253. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  69254. +#endif /* DEBUG_HOST_CHANNELS */
  69255. + return ret_val;
  69256. +}
  69257. +
  69258. +/**
  69259. + * Attempts to queue a single transaction request for a host channel
  69260. + * associated with either a periodic or non-periodic transfer. This function
  69261. + * assumes that there is space available in the appropriate request queue. For
  69262. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  69263. + * is available in the appropriate Tx FIFO.
  69264. + *
  69265. + * @param hcd The HCD state structure.
  69266. + * @param hc Host channel descriptor associated with either a periodic or
  69267. + * non-periodic transfer.
  69268. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  69269. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  69270. + * transfers.
  69271. + *
  69272. + * @return 1 if a request is queued and more requests may be needed to
  69273. + * complete the transfer, 0 if no more requests are required for this
  69274. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  69275. + */
  69276. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  69277. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  69278. +{
  69279. + int retval;
  69280. +
  69281. + if (hcd->core_if->dma_enable) {
  69282. + if (hcd->core_if->dma_desc_enable) {
  69283. + if (!hc->xfer_started
  69284. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  69285. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  69286. + hc->qh->ping_state = 0;
  69287. + }
  69288. + } else if (!hc->xfer_started) {
  69289. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69290. + hc->qh->ping_state = 0;
  69291. + }
  69292. + retval = 0;
  69293. + } else if (hc->halt_pending) {
  69294. + /* Don't queue a request if the channel has been halted. */
  69295. + retval = 0;
  69296. + } else if (hc->halt_on_queue) {
  69297. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  69298. + retval = 0;
  69299. + } else if (hc->do_ping) {
  69300. + if (!hc->xfer_started) {
  69301. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69302. + }
  69303. + retval = 0;
  69304. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  69305. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  69306. + if (!hc->xfer_started) {
  69307. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69308. + retval = 1;
  69309. + } else {
  69310. + retval =
  69311. + dwc_otg_hc_continue_transfer(hcd->core_if,
  69312. + hc);
  69313. + }
  69314. + } else {
  69315. + retval = -1;
  69316. + }
  69317. + } else {
  69318. + if (!hc->xfer_started) {
  69319. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  69320. + retval = 1;
  69321. + } else {
  69322. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  69323. + }
  69324. + }
  69325. +
  69326. + return retval;
  69327. +}
  69328. +
  69329. +/**
  69330. + * Processes periodic channels for the next frame and queues transactions for
  69331. + * these channels to the DWC_otg controller. After queueing transactions, the
  69332. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  69333. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  69334. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  69335. + */
  69336. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  69337. +{
  69338. + hptxsts_data_t tx_status;
  69339. + dwc_list_link_t *qh_ptr;
  69340. + dwc_otg_qh_t *qh;
  69341. + int status;
  69342. + int no_queue_space = 0;
  69343. + int no_fifo_space = 0;
  69344. +
  69345. + dwc_otg_host_global_regs_t *host_regs;
  69346. + host_regs = hcd->core_if->host_if->host_global_regs;
  69347. +
  69348. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  69349. +#ifdef DEBUG
  69350. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  69351. + DWC_DEBUGPL(DBG_HCDV,
  69352. + " P Tx Req Queue Space Avail (before queue): %d\n",
  69353. + tx_status.b.ptxqspcavail);
  69354. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  69355. + tx_status.b.ptxfspcavail);
  69356. +#endif
  69357. +
  69358. + qh_ptr = hcd->periodic_sched_assigned.next;
  69359. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  69360. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  69361. + if (tx_status.b.ptxqspcavail == 0) {
  69362. + no_queue_space = 1;
  69363. + break;
  69364. + }
  69365. +
  69366. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  69367. +
  69368. + // Do not send a split start transaction any later than frame .6
  69369. + // Note, we have to schedule a periodic in .5 to make it go in .6
  69370. + if(fiq_split_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  69371. + {
  69372. + qh_ptr = qh_ptr->next;
  69373. + g_next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  69374. + continue;
  69375. + }
  69376. +
  69377. + /*
  69378. + * Set a flag if we're queuing high-bandwidth in slave mode.
  69379. + * The flag prevents any halts to get into the request queue in
  69380. + * the middle of multiple high-bandwidth packets getting queued.
  69381. + */
  69382. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  69383. + hcd->core_if->queuing_high_bandwidth = 1;
  69384. + }
  69385. + status =
  69386. + queue_transaction(hcd, qh->channel,
  69387. + tx_status.b.ptxfspcavail);
  69388. + if (status < 0) {
  69389. + no_fifo_space = 1;
  69390. + break;
  69391. + }
  69392. +
  69393. + /*
  69394. + * In Slave mode, stay on the current transfer until there is
  69395. + * nothing more to do or the high-bandwidth request count is
  69396. + * reached. In DMA mode, only need to queue one request. The
  69397. + * controller automatically handles multiple packets for
  69398. + * high-bandwidth transfers.
  69399. + */
  69400. + if (hcd->core_if->dma_enable || status == 0 ||
  69401. + qh->channel->requests == qh->channel->multi_count) {
  69402. + qh_ptr = qh_ptr->next;
  69403. + /*
  69404. + * Move the QH from the periodic assigned schedule to
  69405. + * the periodic queued schedule.
  69406. + */
  69407. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  69408. + &qh->qh_list_entry);
  69409. +
  69410. + /* done queuing high bandwidth */
  69411. + hcd->core_if->queuing_high_bandwidth = 0;
  69412. + }
  69413. + }
  69414. +
  69415. + if (!hcd->core_if->dma_enable) {
  69416. + dwc_otg_core_global_regs_t *global_regs;
  69417. + gintmsk_data_t intr_mask = {.d32 = 0 };
  69418. +
  69419. + global_regs = hcd->core_if->core_global_regs;
  69420. + intr_mask.b.ptxfempty = 1;
  69421. +#ifdef DEBUG
  69422. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  69423. + DWC_DEBUGPL(DBG_HCDV,
  69424. + " P Tx Req Queue Space Avail (after queue): %d\n",
  69425. + tx_status.b.ptxqspcavail);
  69426. + DWC_DEBUGPL(DBG_HCDV,
  69427. + " P Tx FIFO Space Avail (after queue): %d\n",
  69428. + tx_status.b.ptxfspcavail);
  69429. +#endif
  69430. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  69431. + no_queue_space || no_fifo_space) {
  69432. + /*
  69433. + * May need to queue more transactions as the request
  69434. + * queue or Tx FIFO empties. Enable the periodic Tx
  69435. + * FIFO empty interrupt. (Always use the half-empty
  69436. + * level to ensure that new requests are loaded as
  69437. + * soon as possible.)
  69438. + */
  69439. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  69440. + intr_mask.d32);
  69441. + } else {
  69442. + /*
  69443. + * Disable the Tx FIFO empty interrupt since there are
  69444. + * no more transactions that need to be queued right
  69445. + * now. This function is called from interrupt
  69446. + * handlers to queue more transactions as transfer
  69447. + * states change.
  69448. + */
  69449. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  69450. + 0);
  69451. + }
  69452. + }
  69453. +}
  69454. +
  69455. +/**
  69456. + * Processes active non-periodic channels and queues transactions for these
  69457. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  69458. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  69459. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  69460. + * FIFO Empty interrupt is disabled.
  69461. + */
  69462. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  69463. +{
  69464. + gnptxsts_data_t tx_status;
  69465. + dwc_list_link_t *orig_qh_ptr;
  69466. + dwc_otg_qh_t *qh;
  69467. + int status;
  69468. + int no_queue_space = 0;
  69469. + int no_fifo_space = 0;
  69470. + int more_to_do = 0;
  69471. +
  69472. + dwc_otg_core_global_regs_t *global_regs =
  69473. + hcd->core_if->core_global_regs;
  69474. +
  69475. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  69476. +#ifdef DEBUG
  69477. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  69478. + DWC_DEBUGPL(DBG_HCDV,
  69479. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  69480. + tx_status.b.nptxqspcavail);
  69481. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  69482. + tx_status.b.nptxfspcavail);
  69483. +#endif
  69484. + /*
  69485. + * Keep track of the starting point. Skip over the start-of-list
  69486. + * entry.
  69487. + */
  69488. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  69489. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  69490. + }
  69491. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  69492. +
  69493. + /*
  69494. + * Process once through the active list or until no more space is
  69495. + * available in the request queue or the Tx FIFO.
  69496. + */
  69497. + do {
  69498. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  69499. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  69500. + no_queue_space = 1;
  69501. + break;
  69502. + }
  69503. +
  69504. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  69505. + qh_list_entry);
  69506. +
  69507. + // Do not send a split start transaction any later than frame .5
  69508. + // non periodic transactions will start immediately in this uframe
  69509. + if(fiq_split_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  69510. + {
  69511. + g_next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  69512. + break;
  69513. + }
  69514. +
  69515. + status =
  69516. + queue_transaction(hcd, qh->channel,
  69517. + tx_status.b.nptxfspcavail);
  69518. +
  69519. + if (status > 0) {
  69520. + more_to_do = 1;
  69521. + } else if (status < 0) {
  69522. + no_fifo_space = 1;
  69523. + break;
  69524. + }
  69525. +
  69526. + /* Advance to next QH, skipping start-of-list entry. */
  69527. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  69528. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  69529. + hcd->non_periodic_qh_ptr =
  69530. + hcd->non_periodic_qh_ptr->next;
  69531. + }
  69532. +
  69533. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  69534. +
  69535. + if (!hcd->core_if->dma_enable) {
  69536. + gintmsk_data_t intr_mask = {.d32 = 0 };
  69537. + intr_mask.b.nptxfempty = 1;
  69538. +
  69539. +#ifdef DEBUG
  69540. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  69541. + DWC_DEBUGPL(DBG_HCDV,
  69542. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  69543. + tx_status.b.nptxqspcavail);
  69544. + DWC_DEBUGPL(DBG_HCDV,
  69545. + " NP Tx FIFO Space Avail (after queue): %d\n",
  69546. + tx_status.b.nptxfspcavail);
  69547. +#endif
  69548. + if (more_to_do || no_queue_space || no_fifo_space) {
  69549. + /*
  69550. + * May need to queue more transactions as the request
  69551. + * queue or Tx FIFO empties. Enable the non-periodic
  69552. + * Tx FIFO empty interrupt. (Always use the half-empty
  69553. + * level to ensure that new requests are loaded as
  69554. + * soon as possible.)
  69555. + */
  69556. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  69557. + intr_mask.d32);
  69558. + } else {
  69559. + /*
  69560. + * Disable the Tx FIFO empty interrupt since there are
  69561. + * no more transactions that need to be queued right
  69562. + * now. This function is called from interrupt
  69563. + * handlers to queue more transactions as transfer
  69564. + * states change.
  69565. + */
  69566. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  69567. + 0);
  69568. + }
  69569. + }
  69570. +}
  69571. +
  69572. +/**
  69573. + * This function processes the currently active host channels and queues
  69574. + * transactions for these channels to the DWC_otg controller. It is called
  69575. + * from HCD interrupt handler functions.
  69576. + *
  69577. + * @param hcd The HCD state structure.
  69578. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  69579. + * periodic, or both).
  69580. + */
  69581. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  69582. + dwc_otg_transaction_type_e tr_type)
  69583. +{
  69584. +#ifdef DEBUG_SOF
  69585. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  69586. +#endif
  69587. + /* Process host channels associated with periodic transfers. */
  69588. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  69589. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  69590. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  69591. +
  69592. + process_periodic_channels(hcd);
  69593. + }
  69594. +
  69595. + /* Process host channels associated with non-periodic transfers. */
  69596. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  69597. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  69598. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  69599. + process_non_periodic_channels(hcd);
  69600. + } else {
  69601. + /*
  69602. + * Ensure NP Tx FIFO empty interrupt is disabled when
  69603. + * there are no non-periodic transfers to process.
  69604. + */
  69605. + gintmsk_data_t gintmsk = {.d32 = 0 };
  69606. + gintmsk.b.nptxfempty = 1;
  69607. + DWC_MODIFY_REG32(&hcd->core_if->
  69608. + core_global_regs->gintmsk, gintmsk.d32,
  69609. + 0);
  69610. + }
  69611. + }
  69612. +}
  69613. +
  69614. +#ifdef DWC_HS_ELECT_TST
  69615. +/*
  69616. + * Quick and dirty hack to implement the HS Electrical Test
  69617. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  69618. + *
  69619. + * This code was copied from our userspace app "hset". It sends a
  69620. + * Get Device Descriptor control sequence in two parts, first the
  69621. + * Setup packet by itself, followed some time later by the In and
  69622. + * Ack packets. Rather than trying to figure out how to add this
  69623. + * functionality to the normal driver code, we just hijack the
  69624. + * hardware, using these two function to drive the hardware
  69625. + * directly.
  69626. + */
  69627. +
  69628. +static dwc_otg_core_global_regs_t *global_regs;
  69629. +static dwc_otg_host_global_regs_t *hc_global_regs;
  69630. +static dwc_otg_hc_regs_t *hc_regs;
  69631. +static uint32_t *data_fifo;
  69632. +
  69633. +static void do_setup(void)
  69634. +{
  69635. + gintsts_data_t gintsts;
  69636. + hctsiz_data_t hctsiz;
  69637. + hcchar_data_t hcchar;
  69638. + haint_data_t haint;
  69639. + hcint_data_t hcint;
  69640. +
  69641. + /* Enable HAINTs */
  69642. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  69643. +
  69644. + /* Enable HCINTs */
  69645. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  69646. +
  69647. + /* Read GINTSTS */
  69648. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69649. +
  69650. + /* Read HAINT */
  69651. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  69652. +
  69653. + /* Read HCINT */
  69654. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  69655. +
  69656. + /* Read HCCHAR */
  69657. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69658. +
  69659. + /* Clear HCINT */
  69660. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  69661. +
  69662. + /* Clear HAINT */
  69663. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  69664. +
  69665. + /* Clear GINTSTS */
  69666. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69667. +
  69668. + /* Read GINTSTS */
  69669. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69670. +
  69671. + /*
  69672. + * Send Setup packet (Get Device Descriptor)
  69673. + */
  69674. +
  69675. + /* Make sure channel is disabled */
  69676. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69677. + if (hcchar.b.chen) {
  69678. + hcchar.b.chdis = 1;
  69679. +// hcchar.b.chen = 1;
  69680. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  69681. + //sleep(1);
  69682. + dwc_mdelay(1000);
  69683. +
  69684. + /* Read GINTSTS */
  69685. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69686. +
  69687. + /* Read HAINT */
  69688. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  69689. +
  69690. + /* Read HCINT */
  69691. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  69692. +
  69693. + /* Read HCCHAR */
  69694. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69695. +
  69696. + /* Clear HCINT */
  69697. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  69698. +
  69699. + /* Clear HAINT */
  69700. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  69701. +
  69702. + /* Clear GINTSTS */
  69703. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69704. +
  69705. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69706. + }
  69707. +
  69708. + /* Set HCTSIZ */
  69709. + hctsiz.d32 = 0;
  69710. + hctsiz.b.xfersize = 8;
  69711. + hctsiz.b.pktcnt = 1;
  69712. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  69713. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  69714. +
  69715. + /* Set HCCHAR */
  69716. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69717. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  69718. + hcchar.b.epdir = 0;
  69719. + hcchar.b.epnum = 0;
  69720. + hcchar.b.mps = 8;
  69721. + hcchar.b.chen = 1;
  69722. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  69723. +
  69724. + /* Fill FIFO with Setup data for Get Device Descriptor */
  69725. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  69726. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  69727. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  69728. +
  69729. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69730. +
  69731. + /* Wait for host channel interrupt */
  69732. + do {
  69733. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69734. + } while (gintsts.b.hcintr == 0);
  69735. +
  69736. + /* Disable HCINTs */
  69737. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  69738. +
  69739. + /* Disable HAINTs */
  69740. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  69741. +
  69742. + /* Read HAINT */
  69743. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  69744. +
  69745. + /* Read HCINT */
  69746. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  69747. +
  69748. + /* Read HCCHAR */
  69749. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69750. +
  69751. + /* Clear HCINT */
  69752. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  69753. +
  69754. + /* Clear HAINT */
  69755. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  69756. +
  69757. + /* Clear GINTSTS */
  69758. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69759. +
  69760. + /* Read GINTSTS */
  69761. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69762. +}
  69763. +
  69764. +static void do_in_ack(void)
  69765. +{
  69766. + gintsts_data_t gintsts;
  69767. + hctsiz_data_t hctsiz;
  69768. + hcchar_data_t hcchar;
  69769. + haint_data_t haint;
  69770. + hcint_data_t hcint;
  69771. + host_grxsts_data_t grxsts;
  69772. +
  69773. + /* Enable HAINTs */
  69774. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  69775. +
  69776. + /* Enable HCINTs */
  69777. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  69778. +
  69779. + /* Read GINTSTS */
  69780. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69781. +
  69782. + /* Read HAINT */
  69783. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  69784. +
  69785. + /* Read HCINT */
  69786. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  69787. +
  69788. + /* Read HCCHAR */
  69789. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69790. +
  69791. + /* Clear HCINT */
  69792. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  69793. +
  69794. + /* Clear HAINT */
  69795. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  69796. +
  69797. + /* Clear GINTSTS */
  69798. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69799. +
  69800. + /* Read GINTSTS */
  69801. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69802. +
  69803. + /*
  69804. + * Receive Control In packet
  69805. + */
  69806. +
  69807. + /* Make sure channel is disabled */
  69808. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69809. + if (hcchar.b.chen) {
  69810. + hcchar.b.chdis = 1;
  69811. + hcchar.b.chen = 1;
  69812. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  69813. + //sleep(1);
  69814. + dwc_mdelay(1000);
  69815. +
  69816. + /* Read GINTSTS */
  69817. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69818. +
  69819. + /* Read HAINT */
  69820. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  69821. +
  69822. + /* Read HCINT */
  69823. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  69824. +
  69825. + /* Read HCCHAR */
  69826. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69827. +
  69828. + /* Clear HCINT */
  69829. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  69830. +
  69831. + /* Clear HAINT */
  69832. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  69833. +
  69834. + /* Clear GINTSTS */
  69835. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69836. +
  69837. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69838. + }
  69839. +
  69840. + /* Set HCTSIZ */
  69841. + hctsiz.d32 = 0;
  69842. + hctsiz.b.xfersize = 8;
  69843. + hctsiz.b.pktcnt = 1;
  69844. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  69845. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  69846. +
  69847. + /* Set HCCHAR */
  69848. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69849. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  69850. + hcchar.b.epdir = 1;
  69851. + hcchar.b.epnum = 0;
  69852. + hcchar.b.mps = 8;
  69853. + hcchar.b.chen = 1;
  69854. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  69855. +
  69856. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69857. +
  69858. + /* Wait for receive status queue interrupt */
  69859. + do {
  69860. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69861. + } while (gintsts.b.rxstsqlvl == 0);
  69862. +
  69863. + /* Read RXSTS */
  69864. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  69865. +
  69866. + /* Clear RXSTSQLVL in GINTSTS */
  69867. + gintsts.d32 = 0;
  69868. + gintsts.b.rxstsqlvl = 1;
  69869. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69870. +
  69871. + switch (grxsts.b.pktsts) {
  69872. + case DWC_GRXSTS_PKTSTS_IN:
  69873. + /* Read the data into the host buffer */
  69874. + if (grxsts.b.bcnt > 0) {
  69875. + int i;
  69876. + int word_count = (grxsts.b.bcnt + 3) / 4;
  69877. +
  69878. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  69879. +
  69880. + for (i = 0; i < word_count; i++) {
  69881. + (void)DWC_READ_REG32(data_fifo++);
  69882. + }
  69883. + }
  69884. + break;
  69885. +
  69886. + default:
  69887. + break;
  69888. + }
  69889. +
  69890. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69891. +
  69892. + /* Wait for receive status queue interrupt */
  69893. + do {
  69894. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69895. + } while (gintsts.b.rxstsqlvl == 0);
  69896. +
  69897. + /* Read RXSTS */
  69898. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  69899. +
  69900. + /* Clear RXSTSQLVL in GINTSTS */
  69901. + gintsts.d32 = 0;
  69902. + gintsts.b.rxstsqlvl = 1;
  69903. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69904. +
  69905. + switch (grxsts.b.pktsts) {
  69906. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  69907. + break;
  69908. +
  69909. + default:
  69910. + break;
  69911. + }
  69912. +
  69913. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69914. +
  69915. + /* Wait for host channel interrupt */
  69916. + do {
  69917. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69918. + } while (gintsts.b.hcintr == 0);
  69919. +
  69920. + /* Read HAINT */
  69921. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  69922. +
  69923. + /* Read HCINT */
  69924. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  69925. +
  69926. + /* Read HCCHAR */
  69927. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69928. +
  69929. + /* Clear HCINT */
  69930. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  69931. +
  69932. + /* Clear HAINT */
  69933. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  69934. +
  69935. + /* Clear GINTSTS */
  69936. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69937. +
  69938. + /* Read GINTSTS */
  69939. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69940. +
  69941. +// usleep(100000);
  69942. +// mdelay(100);
  69943. + dwc_mdelay(1);
  69944. +
  69945. + /*
  69946. + * Send handshake packet
  69947. + */
  69948. +
  69949. + /* Read HAINT */
  69950. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  69951. +
  69952. + /* Read HCINT */
  69953. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  69954. +
  69955. + /* Read HCCHAR */
  69956. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69957. +
  69958. + /* Clear HCINT */
  69959. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  69960. +
  69961. + /* Clear HAINT */
  69962. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  69963. +
  69964. + /* Clear GINTSTS */
  69965. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69966. +
  69967. + /* Read GINTSTS */
  69968. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69969. +
  69970. + /* Make sure channel is disabled */
  69971. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69972. + if (hcchar.b.chen) {
  69973. + hcchar.b.chdis = 1;
  69974. + hcchar.b.chen = 1;
  69975. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  69976. + //sleep(1);
  69977. + dwc_mdelay(1000);
  69978. +
  69979. + /* Read GINTSTS */
  69980. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  69981. +
  69982. + /* Read HAINT */
  69983. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  69984. +
  69985. + /* Read HCINT */
  69986. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  69987. +
  69988. + /* Read HCCHAR */
  69989. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  69990. +
  69991. + /* Clear HCINT */
  69992. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  69993. +
  69994. + /* Clear HAINT */
  69995. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  69996. +
  69997. + /* Clear GINTSTS */
  69998. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  69999. +
  70000. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70001. + }
  70002. +
  70003. + /* Set HCTSIZ */
  70004. + hctsiz.d32 = 0;
  70005. + hctsiz.b.xfersize = 0;
  70006. + hctsiz.b.pktcnt = 1;
  70007. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  70008. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  70009. +
  70010. + /* Set HCCHAR */
  70011. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70012. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  70013. + hcchar.b.epdir = 0;
  70014. + hcchar.b.epnum = 0;
  70015. + hcchar.b.mps = 8;
  70016. + hcchar.b.chen = 1;
  70017. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  70018. +
  70019. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70020. +
  70021. + /* Wait for host channel interrupt */
  70022. + do {
  70023. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70024. + } while (gintsts.b.hcintr == 0);
  70025. +
  70026. + /* Disable HCINTs */
  70027. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  70028. +
  70029. + /* Disable HAINTs */
  70030. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  70031. +
  70032. + /* Read HAINT */
  70033. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  70034. +
  70035. + /* Read HCINT */
  70036. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  70037. +
  70038. + /* Read HCCHAR */
  70039. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  70040. +
  70041. + /* Clear HCINT */
  70042. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  70043. +
  70044. + /* Clear HAINT */
  70045. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  70046. +
  70047. + /* Clear GINTSTS */
  70048. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  70049. +
  70050. + /* Read GINTSTS */
  70051. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  70052. +}
  70053. +#endif
  70054. +
  70055. +/** Handles hub class-specific requests. */
  70056. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  70057. + uint16_t typeReq,
  70058. + uint16_t wValue,
  70059. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  70060. +{
  70061. + int retval = 0;
  70062. +
  70063. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  70064. + usb_hub_descriptor_t *hub_desc;
  70065. + hprt0_data_t hprt0 = {.d32 = 0 };
  70066. +
  70067. + uint32_t port_status;
  70068. +
  70069. + switch (typeReq) {
  70070. + case UCR_CLEAR_HUB_FEATURE:
  70071. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70072. + "ClearHubFeature 0x%x\n", wValue);
  70073. + switch (wValue) {
  70074. + case UHF_C_HUB_LOCAL_POWER:
  70075. + case UHF_C_HUB_OVER_CURRENT:
  70076. + /* Nothing required here */
  70077. + break;
  70078. + default:
  70079. + retval = -DWC_E_INVALID;
  70080. + DWC_ERROR("DWC OTG HCD - "
  70081. + "ClearHubFeature request %xh unknown\n",
  70082. + wValue);
  70083. + }
  70084. + break;
  70085. + case UCR_CLEAR_PORT_FEATURE:
  70086. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70087. + if (wValue != UHF_PORT_L1)
  70088. +#endif
  70089. + if (!wIndex || wIndex > 1)
  70090. + goto error;
  70091. +
  70092. + switch (wValue) {
  70093. + case UHF_PORT_ENABLE:
  70094. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  70095. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  70096. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70097. + hprt0.b.prtena = 1;
  70098. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70099. + break;
  70100. + case UHF_PORT_SUSPEND:
  70101. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70102. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  70103. +
  70104. + if (core_if->power_down == 2) {
  70105. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  70106. + } else {
  70107. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  70108. + dwc_mdelay(5);
  70109. +
  70110. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70111. + hprt0.b.prtres = 1;
  70112. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70113. + hprt0.b.prtsusp = 0;
  70114. + /* Clear Resume bit */
  70115. + dwc_mdelay(100);
  70116. + hprt0.b.prtres = 0;
  70117. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70118. + }
  70119. + break;
  70120. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70121. + case UHF_PORT_L1:
  70122. + {
  70123. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70124. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  70125. +
  70126. + lpmcfg.d32 =
  70127. + DWC_READ_REG32(&core_if->
  70128. + core_global_regs->glpmcfg);
  70129. + lpmcfg.b.en_utmi_sleep = 0;
  70130. + lpmcfg.b.hird_thres &= (~(1 << 4));
  70131. + lpmcfg.b.prt_sleep_sts = 1;
  70132. + DWC_WRITE_REG32(&core_if->
  70133. + core_global_regs->glpmcfg,
  70134. + lpmcfg.d32);
  70135. +
  70136. + /* Clear Enbl_L1Gating bit. */
  70137. + pcgcctl.b.enbl_sleep_gating = 1;
  70138. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  70139. + 0);
  70140. +
  70141. + dwc_mdelay(5);
  70142. +
  70143. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70144. + hprt0.b.prtres = 1;
  70145. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  70146. + hprt0.d32);
  70147. + /* This bit will be cleared in wakeup interrupt handle */
  70148. + break;
  70149. + }
  70150. +#endif
  70151. + case UHF_PORT_POWER:
  70152. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70153. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  70154. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70155. + hprt0.b.prtpwr = 0;
  70156. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70157. + break;
  70158. + case UHF_PORT_INDICATOR:
  70159. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70160. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  70161. + /* Port inidicator not supported */
  70162. + break;
  70163. + case UHF_C_PORT_CONNECTION:
  70164. + /* Clears drivers internal connect status change
  70165. + * flag */
  70166. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70167. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  70168. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  70169. + break;
  70170. + case UHF_C_PORT_RESET:
  70171. + /* Clears the driver's internal Port Reset Change
  70172. + * flag */
  70173. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70174. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  70175. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  70176. + break;
  70177. + case UHF_C_PORT_ENABLE:
  70178. + /* Clears the driver's internal Port
  70179. + * Enable/Disable Change flag */
  70180. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70181. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  70182. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  70183. + break;
  70184. + case UHF_C_PORT_SUSPEND:
  70185. + /* Clears the driver's internal Port Suspend
  70186. + * Change flag, which is set when resume signaling on
  70187. + * the host port is complete */
  70188. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70189. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  70190. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  70191. + break;
  70192. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70193. + case UHF_C_PORT_L1:
  70194. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  70195. + break;
  70196. +#endif
  70197. + case UHF_C_PORT_OVER_CURRENT:
  70198. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70199. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  70200. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  70201. + break;
  70202. + default:
  70203. + retval = -DWC_E_INVALID;
  70204. + DWC_ERROR("DWC OTG HCD - "
  70205. + "ClearPortFeature request %xh "
  70206. + "unknown or unsupported\n", wValue);
  70207. + }
  70208. + break;
  70209. + case UCR_GET_HUB_DESCRIPTOR:
  70210. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70211. + "GetHubDescriptor\n");
  70212. + hub_desc = (usb_hub_descriptor_t *) buf;
  70213. + hub_desc->bDescLength = 9;
  70214. + hub_desc->bDescriptorType = 0x29;
  70215. + hub_desc->bNbrPorts = 1;
  70216. + USETW(hub_desc->wHubCharacteristics, 0x08);
  70217. + hub_desc->bPwrOn2PwrGood = 1;
  70218. + hub_desc->bHubContrCurrent = 0;
  70219. + hub_desc->DeviceRemovable[0] = 0;
  70220. + hub_desc->DeviceRemovable[1] = 0xff;
  70221. + break;
  70222. + case UCR_GET_HUB_STATUS:
  70223. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70224. + "GetHubStatus\n");
  70225. + DWC_MEMSET(buf, 0, 4);
  70226. + break;
  70227. + case UCR_GET_PORT_STATUS:
  70228. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70229. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  70230. + wIndex, dwc_otg_hcd->flags.d32);
  70231. + if (!wIndex || wIndex > 1)
  70232. + goto error;
  70233. +
  70234. + port_status = 0;
  70235. +
  70236. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  70237. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  70238. +
  70239. + if (dwc_otg_hcd->flags.b.port_enable_change)
  70240. + port_status |= (1 << UHF_C_PORT_ENABLE);
  70241. +
  70242. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  70243. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  70244. +
  70245. + if (dwc_otg_hcd->flags.b.port_l1_change)
  70246. + port_status |= (1 << UHF_C_PORT_L1);
  70247. +
  70248. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  70249. + port_status |= (1 << UHF_C_PORT_RESET);
  70250. + }
  70251. +
  70252. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  70253. + DWC_WARN("Overcurrent change detected\n");
  70254. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  70255. + }
  70256. +
  70257. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  70258. + /*
  70259. + * The port is disconnected, which means the core is
  70260. + * either in device mode or it soon will be. Just
  70261. + * return 0's for the remainder of the port status
  70262. + * since the port register can't be read if the core
  70263. + * is in device mode.
  70264. + */
  70265. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  70266. + break;
  70267. + }
  70268. +
  70269. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  70270. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  70271. +
  70272. + if (hprt0.b.prtconnsts)
  70273. + port_status |= (1 << UHF_PORT_CONNECTION);
  70274. +
  70275. + if (hprt0.b.prtena)
  70276. + port_status |= (1 << UHF_PORT_ENABLE);
  70277. +
  70278. + if (hprt0.b.prtsusp)
  70279. + port_status |= (1 << UHF_PORT_SUSPEND);
  70280. +
  70281. + if (hprt0.b.prtovrcurract)
  70282. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  70283. +
  70284. + if (hprt0.b.prtrst)
  70285. + port_status |= (1 << UHF_PORT_RESET);
  70286. +
  70287. + if (hprt0.b.prtpwr)
  70288. + port_status |= (1 << UHF_PORT_POWER);
  70289. +
  70290. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  70291. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  70292. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  70293. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  70294. +
  70295. + if (hprt0.b.prttstctl)
  70296. + port_status |= (1 << UHF_PORT_TEST);
  70297. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  70298. + port_status |= (1 << UHF_PORT_L1);
  70299. + }
  70300. + /*
  70301. + For Synopsys HW emulation of Power down wkup_control asserts the
  70302. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  70303. + We intentionally tell the software that port is in L2Suspend state.
  70304. + Only for STE.
  70305. + */
  70306. + if ((core_if->power_down == 2)
  70307. + && (core_if->hibernation_suspend == 1)) {
  70308. + port_status |= (1 << UHF_PORT_SUSPEND);
  70309. + }
  70310. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  70311. +
  70312. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  70313. +
  70314. + break;
  70315. + case UCR_SET_HUB_FEATURE:
  70316. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70317. + "SetHubFeature\n");
  70318. + /* No HUB features supported */
  70319. + break;
  70320. + case UCR_SET_PORT_FEATURE:
  70321. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  70322. + goto error;
  70323. +
  70324. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  70325. + /*
  70326. + * The port is disconnected, which means the core is
  70327. + * either in device mode or it soon will be. Just
  70328. + * return without doing anything since the port
  70329. + * register can't be written if the core is in device
  70330. + * mode.
  70331. + */
  70332. + break;
  70333. + }
  70334. +
  70335. + switch (wValue) {
  70336. + case UHF_PORT_SUSPEND:
  70337. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70338. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  70339. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  70340. + goto error;
  70341. + }
  70342. + if (core_if->power_down == 2) {
  70343. + int timeout = 300;
  70344. + dwc_irqflags_t flags;
  70345. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70346. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  70347. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  70348. +#ifdef DWC_DEV_SRPCAP
  70349. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  70350. +#endif
  70351. + DWC_PRINTF("Preparing for complete power-off\n");
  70352. +
  70353. + /* Save registers before hibernation */
  70354. + dwc_otg_save_global_regs(core_if);
  70355. + dwc_otg_save_host_regs(core_if);
  70356. +
  70357. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70358. + hprt0.b.prtsusp = 1;
  70359. + hprt0.b.prtena = 0;
  70360. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70361. + /* Spin hprt0.b.prtsusp to became 1 */
  70362. + do {
  70363. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70364. + if (hprt0.b.prtsusp) {
  70365. + break;
  70366. + }
  70367. + dwc_mdelay(1);
  70368. + } while (--timeout);
  70369. + if (!timeout) {
  70370. + DWC_WARN("Suspend wasn't genereted\n");
  70371. + }
  70372. + dwc_udelay(10);
  70373. +
  70374. + /*
  70375. + * We need to disable interrupts to prevent servicing of any IRQ
  70376. + * during going to hibernation
  70377. + */
  70378. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  70379. + core_if->lx_state = DWC_OTG_L2;
  70380. +#ifdef DWC_DEV_SRPCAP
  70381. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70382. + hprt0.b.prtpwr = 0;
  70383. + hprt0.b.prtena = 0;
  70384. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  70385. + hprt0.d32);
  70386. +#endif
  70387. + gusbcfg.d32 =
  70388. + DWC_READ_REG32(&core_if->core_global_regs->
  70389. + gusbcfg);
  70390. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  70391. + /* ULPI interface */
  70392. + /* Suspend the Phy Clock */
  70393. + pcgcctl.d32 = 0;
  70394. + pcgcctl.b.stoppclk = 1;
  70395. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  70396. + pcgcctl.d32);
  70397. + dwc_udelay(10);
  70398. + gpwrdn.b.pmuactv = 1;
  70399. + DWC_MODIFY_REG32(&core_if->
  70400. + core_global_regs->
  70401. + gpwrdn, 0, gpwrdn.d32);
  70402. + } else {
  70403. + /* UTMI+ Interface */
  70404. + gpwrdn.b.pmuactv = 1;
  70405. + DWC_MODIFY_REG32(&core_if->
  70406. + core_global_regs->
  70407. + gpwrdn, 0, gpwrdn.d32);
  70408. + dwc_udelay(10);
  70409. + pcgcctl.b.stoppclk = 1;
  70410. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  70411. + dwc_udelay(10);
  70412. + }
  70413. +#ifdef DWC_DEV_SRPCAP
  70414. + gpwrdn.d32 = 0;
  70415. + gpwrdn.b.dis_vbus = 1;
  70416. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70417. + gpwrdn, 0, gpwrdn.d32);
  70418. +#endif
  70419. + gpwrdn.d32 = 0;
  70420. + gpwrdn.b.pmuintsel = 1;
  70421. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70422. + gpwrdn, 0, gpwrdn.d32);
  70423. + dwc_udelay(10);
  70424. +
  70425. + gpwrdn.d32 = 0;
  70426. +#ifdef DWC_DEV_SRPCAP
  70427. + gpwrdn.b.srp_det_msk = 1;
  70428. +#endif
  70429. + gpwrdn.b.disconn_det_msk = 1;
  70430. + gpwrdn.b.lnstchng_msk = 1;
  70431. + gpwrdn.b.sts_chngint_msk = 1;
  70432. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70433. + gpwrdn, 0, gpwrdn.d32);
  70434. + dwc_udelay(10);
  70435. +
  70436. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  70437. + gpwrdn.d32 = 0;
  70438. + gpwrdn.b.pwrdnclmp = 1;
  70439. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70440. + gpwrdn, 0, gpwrdn.d32);
  70441. + dwc_udelay(10);
  70442. +
  70443. + /* Switch off VDD */
  70444. + gpwrdn.d32 = 0;
  70445. + gpwrdn.b.pwrdnswtch = 1;
  70446. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70447. + gpwrdn, 0, gpwrdn.d32);
  70448. +
  70449. +#ifdef DWC_DEV_SRPCAP
  70450. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  70451. + {
  70452. + core_if->pwron_timer_started = 1;
  70453. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  70454. + }
  70455. +#endif
  70456. + /* Save gpwrdn register for further usage if stschng interrupt */
  70457. + core_if->gr_backup->gpwrdn_local =
  70458. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  70459. +
  70460. + /* Set flag to indicate that we are in hibernation */
  70461. + core_if->hibernation_suspend = 1;
  70462. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  70463. +
  70464. + DWC_PRINTF("Host hibernation completed\n");
  70465. + // Exit from case statement
  70466. + break;
  70467. +
  70468. + }
  70469. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  70470. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  70471. + gotgctl_data_t gotgctl = {.d32 = 0 };
  70472. + gotgctl.b.hstsethnpen = 1;
  70473. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70474. + gotgctl, 0, gotgctl.d32);
  70475. + core_if->op_state = A_SUSPEND;
  70476. + }
  70477. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70478. + hprt0.b.prtsusp = 1;
  70479. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70480. + {
  70481. + dwc_irqflags_t flags;
  70482. + /* Update lx_state */
  70483. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  70484. + core_if->lx_state = DWC_OTG_L2;
  70485. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  70486. + }
  70487. + /* Suspend the Phy Clock */
  70488. + {
  70489. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70490. + pcgcctl.b.stoppclk = 1;
  70491. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  70492. + pcgcctl.d32);
  70493. + dwc_udelay(10);
  70494. + }
  70495. +
  70496. + /* For HNP the bus must be suspended for at least 200ms. */
  70497. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  70498. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70499. + pcgcctl.b.stoppclk = 1;
  70500. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  70501. + dwc_mdelay(200);
  70502. + }
  70503. +
  70504. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  70505. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  70506. + if (core_if->adp_enable) {
  70507. + gotgctl_data_t gotgctl = {.d32 = 0 };
  70508. + gpwrdn_data_t gpwrdn;
  70509. +
  70510. + while (gotgctl.b.asesvld == 1) {
  70511. + gotgctl.d32 =
  70512. + DWC_READ_REG32(&core_if->
  70513. + core_global_regs->
  70514. + gotgctl);
  70515. + dwc_mdelay(100);
  70516. + }
  70517. +
  70518. + /* Enable Power Down Logic */
  70519. + gpwrdn.d32 = 0;
  70520. + gpwrdn.b.pmuactv = 1;
  70521. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70522. + gpwrdn, 0, gpwrdn.d32);
  70523. +
  70524. + /* Unmask SRP detected interrupt from Power Down Logic */
  70525. + gpwrdn.d32 = 0;
  70526. + gpwrdn.b.srp_det_msk = 1;
  70527. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  70528. + gpwrdn, 0, gpwrdn.d32);
  70529. +
  70530. + dwc_otg_adp_probe_start(core_if);
  70531. + }
  70532. +#endif
  70533. + break;
  70534. + case UHF_PORT_POWER:
  70535. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70536. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  70537. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70538. + hprt0.b.prtpwr = 1;
  70539. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70540. + break;
  70541. + case UHF_PORT_RESET:
  70542. + if ((core_if->power_down == 2)
  70543. + && (core_if->hibernation_suspend == 1)) {
  70544. + /* If we are going to exit from Hibernated
  70545. + * state via USB RESET.
  70546. + */
  70547. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  70548. + } else {
  70549. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70550. +
  70551. + DWC_DEBUGPL(DBG_HCD,
  70552. + "DWC OTG HCD HUB CONTROL - "
  70553. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  70554. + {
  70555. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70556. + pcgcctl.b.enbl_sleep_gating = 1;
  70557. + pcgcctl.b.stoppclk = 1;
  70558. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  70559. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  70560. + }
  70561. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70562. + {
  70563. + glpmcfg_data_t lpmcfg;
  70564. + lpmcfg.d32 =
  70565. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70566. + if (lpmcfg.b.prt_sleep_sts) {
  70567. + lpmcfg.b.en_utmi_sleep = 0;
  70568. + lpmcfg.b.hird_thres &= (~(1 << 4));
  70569. + DWC_WRITE_REG32
  70570. + (&core_if->core_global_regs->glpmcfg,
  70571. + lpmcfg.d32);
  70572. + dwc_mdelay(1);
  70573. + }
  70574. + }
  70575. +#endif
  70576. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70577. + /* Clear suspend bit if resetting from suspended state. */
  70578. + hprt0.b.prtsusp = 0;
  70579. + /* When B-Host the Port reset bit is set in
  70580. + * the Start HCD Callback function, so that
  70581. + * the reset is started within 1ms of the HNP
  70582. + * success interrupt. */
  70583. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  70584. + hprt0.b.prtpwr = 1;
  70585. + hprt0.b.prtrst = 1;
  70586. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  70587. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  70588. + hprt0.d32);
  70589. + }
  70590. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  70591. + dwc_mdelay(60);
  70592. + hprt0.b.prtrst = 0;
  70593. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70594. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  70595. + }
  70596. + break;
  70597. +#ifdef DWC_HS_ELECT_TST
  70598. + case UHF_PORT_TEST:
  70599. + {
  70600. + uint32_t t;
  70601. + gintmsk_data_t gintmsk;
  70602. +
  70603. + t = (wIndex >> 8); /* MSB wIndex USB */
  70604. + DWC_DEBUGPL(DBG_HCD,
  70605. + "DWC OTG HCD HUB CONTROL - "
  70606. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  70607. + t);
  70608. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  70609. + if (t < 6) {
  70610. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  70611. + hprt0.b.prttstctl = t;
  70612. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  70613. + hprt0.d32);
  70614. + } else {
  70615. + /* Setup global vars with reg addresses (quick and
  70616. + * dirty hack, should be cleaned up)
  70617. + */
  70618. + global_regs = core_if->core_global_regs;
  70619. + hc_global_regs =
  70620. + core_if->host_if->host_global_regs;
  70621. + hc_regs =
  70622. + (dwc_otg_hc_regs_t *) ((char *)
  70623. + global_regs +
  70624. + 0x500);
  70625. + data_fifo =
  70626. + (uint32_t *) ((char *)global_regs +
  70627. + 0x1000);
  70628. +
  70629. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  70630. + /* Save current interrupt mask */
  70631. + gintmsk.d32 =
  70632. + DWC_READ_REG32
  70633. + (&global_regs->gintmsk);
  70634. +
  70635. + /* Disable all interrupts while we muck with
  70636. + * the hardware directly
  70637. + */
  70638. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  70639. +
  70640. + /* 15 second delay per the test spec */
  70641. + dwc_mdelay(15000);
  70642. +
  70643. + /* Drive suspend on the root port */
  70644. + hprt0.d32 =
  70645. + dwc_otg_read_hprt0(core_if);
  70646. + hprt0.b.prtsusp = 1;
  70647. + hprt0.b.prtres = 0;
  70648. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70649. +
  70650. + /* 15 second delay per the test spec */
  70651. + dwc_mdelay(15000);
  70652. +
  70653. + /* Drive resume on the root port */
  70654. + hprt0.d32 =
  70655. + dwc_otg_read_hprt0(core_if);
  70656. + hprt0.b.prtsusp = 0;
  70657. + hprt0.b.prtres = 1;
  70658. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70659. + dwc_mdelay(100);
  70660. +
  70661. + /* Clear the resume bit */
  70662. + hprt0.b.prtres = 0;
  70663. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  70664. +
  70665. + /* Restore interrupts */
  70666. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  70667. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  70668. + /* Save current interrupt mask */
  70669. + gintmsk.d32 =
  70670. + DWC_READ_REG32
  70671. + (&global_regs->gintmsk);
  70672. +
  70673. + /* Disable all interrupts while we muck with
  70674. + * the hardware directly
  70675. + */
  70676. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  70677. +
  70678. + /* 15 second delay per the test spec */
  70679. + dwc_mdelay(15000);
  70680. +
  70681. + /* Send the Setup packet */
  70682. + do_setup();
  70683. +
  70684. + /* 15 second delay so nothing else happens for awhile */
  70685. + dwc_mdelay(15000);
  70686. +
  70687. + /* Restore interrupts */
  70688. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  70689. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  70690. + /* Save current interrupt mask */
  70691. + gintmsk.d32 =
  70692. + DWC_READ_REG32
  70693. + (&global_regs->gintmsk);
  70694. +
  70695. + /* Disable all interrupts while we muck with
  70696. + * the hardware directly
  70697. + */
  70698. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  70699. +
  70700. + /* Send the Setup packet */
  70701. + do_setup();
  70702. +
  70703. + /* 15 second delay so nothing else happens for awhile */
  70704. + dwc_mdelay(15000);
  70705. +
  70706. + /* Send the In and Ack packets */
  70707. + do_in_ack();
  70708. +
  70709. + /* 15 second delay so nothing else happens for awhile */
  70710. + dwc_mdelay(15000);
  70711. +
  70712. + /* Restore interrupts */
  70713. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  70714. + }
  70715. + }
  70716. + break;
  70717. + }
  70718. +#endif /* DWC_HS_ELECT_TST */
  70719. +
  70720. + case UHF_PORT_INDICATOR:
  70721. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  70722. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  70723. + /* Not supported */
  70724. + break;
  70725. + default:
  70726. + retval = -DWC_E_INVALID;
  70727. + DWC_ERROR("DWC OTG HCD - "
  70728. + "SetPortFeature request %xh "
  70729. + "unknown or unsupported\n", wValue);
  70730. + break;
  70731. + }
  70732. + break;
  70733. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70734. + case UCR_SET_AND_TEST_PORT_FEATURE:
  70735. + if (wValue != UHF_PORT_L1) {
  70736. + goto error;
  70737. + }
  70738. + {
  70739. + int portnum, hird, devaddr, remwake;
  70740. + glpmcfg_data_t lpmcfg;
  70741. + uint32_t time_usecs;
  70742. + gintsts_data_t gintsts;
  70743. + gintmsk_data_t gintmsk;
  70744. +
  70745. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  70746. + goto error;
  70747. + }
  70748. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  70749. + goto error;
  70750. + }
  70751. + /* Check if the port currently is in SLEEP state */
  70752. + lpmcfg.d32 =
  70753. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70754. + if (lpmcfg.b.prt_sleep_sts) {
  70755. + DWC_INFO("Port is already in sleep mode\n");
  70756. + buf[0] = 0; /* Return success */
  70757. + break;
  70758. + }
  70759. +
  70760. + portnum = wIndex & 0xf;
  70761. + hird = (wIndex >> 4) & 0xf;
  70762. + devaddr = (wIndex >> 8) & 0x7f;
  70763. + remwake = (wIndex >> 15);
  70764. +
  70765. + if (portnum != 1) {
  70766. + retval = -DWC_E_INVALID;
  70767. + DWC_WARN
  70768. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  70769. + portnum);
  70770. + break;
  70771. + }
  70772. +
  70773. + DWC_PRINTF
  70774. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  70775. + portnum, hird, devaddr, remwake);
  70776. + /* Disable LPM interrupt */
  70777. + gintmsk.d32 = 0;
  70778. + gintmsk.b.lpmtranrcvd = 1;
  70779. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  70780. + gintmsk.d32, 0);
  70781. +
  70782. + if (dwc_otg_hcd_send_lpm
  70783. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  70784. + retval = -DWC_E_INVALID;
  70785. + break;
  70786. + }
  70787. +
  70788. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  70789. + /* We will consider timeout if time_usecs microseconds pass,
  70790. + * and we don't receive LPM transaction status.
  70791. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  70792. + * core will set lpmtranrcvd bit.
  70793. + */
  70794. + do {
  70795. + gintsts.d32 =
  70796. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  70797. + if (gintsts.b.lpmtranrcvd) {
  70798. + break;
  70799. + }
  70800. + dwc_udelay(1);
  70801. + } while (--time_usecs);
  70802. + /* lpm_int bit will be cleared in LPM interrupt handler */
  70803. +
  70804. + /* Now fill status
  70805. + * 0x00 - Success
  70806. + * 0x10 - NYET
  70807. + * 0x11 - Timeout
  70808. + */
  70809. + if (!gintsts.b.lpmtranrcvd) {
  70810. + buf[0] = 0x3; /* Completion code is Timeout */
  70811. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  70812. + } else {
  70813. + lpmcfg.d32 =
  70814. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  70815. + if (lpmcfg.b.lpm_resp == 0x3) {
  70816. + /* ACK responce from the device */
  70817. + buf[0] = 0x00; /* Success */
  70818. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  70819. + /* NYET responce from the device */
  70820. + buf[0] = 0x2;
  70821. + } else {
  70822. + /* Otherwise responce with Timeout */
  70823. + buf[0] = 0x3;
  70824. + }
  70825. + }
  70826. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  70827. + lpmcfg.b.lpm_resp);
  70828. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  70829. + gintmsk.d32);
  70830. +
  70831. + break;
  70832. + }
  70833. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  70834. + default:
  70835. +error:
  70836. + retval = -DWC_E_INVALID;
  70837. + DWC_WARN("DWC OTG HCD - "
  70838. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  70839. + typeReq, wIndex, wValue);
  70840. + break;
  70841. + }
  70842. +
  70843. + return retval;
  70844. +}
  70845. +
  70846. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70847. +/** Returns index of host channel to perform LPM transaction. */
  70848. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  70849. +{
  70850. + dwc_otg_core_if_t *core_if = hcd->core_if;
  70851. + dwc_hc_t *hc;
  70852. + hcchar_data_t hcchar;
  70853. + gintmsk_data_t gintmsk = {.d32 = 0 };
  70854. +
  70855. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  70856. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  70857. + return -1;
  70858. + }
  70859. +
  70860. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  70861. +
  70862. + /* Mask host channel interrupts. */
  70863. + gintmsk.b.hcintr = 1;
  70864. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  70865. +
  70866. + /* Fill fields that core needs for LPM transaction */
  70867. + hcchar.b.devaddr = devaddr;
  70868. + hcchar.b.epnum = 0;
  70869. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  70870. + hcchar.b.mps = 64;
  70871. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  70872. + hcchar.b.epdir = 0; /* OUT */
  70873. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  70874. + hcchar.d32);
  70875. +
  70876. + /* Remove the host channel from the free list. */
  70877. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  70878. +
  70879. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  70880. +
  70881. + return hc->hc_num;
  70882. +}
  70883. +
  70884. +/** Release hc after performing LPM transaction */
  70885. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  70886. +{
  70887. + dwc_hc_t *hc;
  70888. + glpmcfg_data_t lpmcfg;
  70889. + uint8_t hc_num;
  70890. +
  70891. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  70892. + hc_num = lpmcfg.b.lpm_chan_index;
  70893. +
  70894. + hc = hcd->hc_ptr_array[hc_num];
  70895. +
  70896. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  70897. + /* Return host channel to free list */
  70898. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  70899. +}
  70900. +
  70901. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  70902. + uint8_t bRemoteWake)
  70903. +{
  70904. + glpmcfg_data_t lpmcfg;
  70905. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  70906. + int channel;
  70907. +
  70908. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  70909. + if (channel < 0) {
  70910. + return channel;
  70911. + }
  70912. +
  70913. + pcgcctl.b.enbl_sleep_gating = 1;
  70914. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  70915. +
  70916. + /* Read LPM config register */
  70917. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  70918. +
  70919. + /* Program LPM transaction fields */
  70920. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  70921. + lpmcfg.b.hird = hird;
  70922. + lpmcfg.b.hird_thres = 0x1c;
  70923. + lpmcfg.b.lpm_chan_index = channel;
  70924. + lpmcfg.b.en_utmi_sleep = 1;
  70925. + /* Program LPM config register */
  70926. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  70927. +
  70928. + /* Send LPM transaction */
  70929. + lpmcfg.b.send_lpm = 1;
  70930. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  70931. +
  70932. + return 0;
  70933. +}
  70934. +
  70935. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  70936. +
  70937. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  70938. +{
  70939. + int retval;
  70940. +
  70941. + if (port != 1) {
  70942. + return -DWC_E_INVALID;
  70943. + }
  70944. +
  70945. + retval = (hcd->flags.b.port_connect_status_change ||
  70946. + hcd->flags.b.port_reset_change ||
  70947. + hcd->flags.b.port_enable_change ||
  70948. + hcd->flags.b.port_suspend_change ||
  70949. + hcd->flags.b.port_over_current_change);
  70950. +#ifdef DEBUG
  70951. + if (retval) {
  70952. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  70953. + " Root port status changed\n");
  70954. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  70955. + hcd->flags.b.port_connect_status_change);
  70956. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  70957. + hcd->flags.b.port_reset_change);
  70958. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  70959. + hcd->flags.b.port_enable_change);
  70960. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  70961. + hcd->flags.b.port_suspend_change);
  70962. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  70963. + hcd->flags.b.port_over_current_change);
  70964. + }
  70965. +#endif
  70966. + return retval;
  70967. +}
  70968. +
  70969. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  70970. +{
  70971. + hfnum_data_t hfnum;
  70972. + hfnum.d32 =
  70973. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  70974. + hfnum);
  70975. +
  70976. +#ifdef DEBUG_SOF
  70977. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  70978. + hfnum.b.frnum);
  70979. +#endif
  70980. + return hfnum.b.frnum;
  70981. +}
  70982. +
  70983. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  70984. + struct dwc_otg_hcd_function_ops *fops)
  70985. +{
  70986. + int retval = 0;
  70987. +
  70988. + hcd->fops = fops;
  70989. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  70990. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  70991. + dwc_otg_hcd_reinit(hcd);
  70992. + } else {
  70993. + retval = -DWC_E_NO_DEVICE;
  70994. + }
  70995. +
  70996. + return retval;
  70997. +}
  70998. +
  70999. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  71000. +{
  71001. + return hcd->priv;
  71002. +}
  71003. +
  71004. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  71005. +{
  71006. + hcd->priv = priv_data;
  71007. +}
  71008. +
  71009. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  71010. +{
  71011. + return hcd->otg_port;
  71012. +}
  71013. +
  71014. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  71015. +{
  71016. + uint32_t is_b_host;
  71017. + if (hcd->core_if->op_state == B_HOST) {
  71018. + is_b_host = 1;
  71019. + } else {
  71020. + is_b_host = 0;
  71021. + }
  71022. +
  71023. + return is_b_host;
  71024. +}
  71025. +
  71026. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  71027. + int iso_desc_count, int atomic_alloc)
  71028. +{
  71029. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  71030. + uint32_t size;
  71031. +
  71032. + size =
  71033. + sizeof(*dwc_otg_urb) +
  71034. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  71035. + if (atomic_alloc)
  71036. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  71037. + else
  71038. + dwc_otg_urb = DWC_ALLOC(size);
  71039. +
  71040. + if (dwc_otg_urb)
  71041. + dwc_otg_urb->packet_count = iso_desc_count;
  71042. + else {
  71043. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  71044. + "%salloc of %db failed\n",
  71045. + atomic_alloc?"atomic ":"", size);
  71046. + }
  71047. + return dwc_otg_urb;
  71048. +}
  71049. +
  71050. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71051. + uint8_t dev_addr, uint8_t ep_num,
  71052. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  71053. +{
  71054. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  71055. + ep_type, ep_dir, mps);
  71056. +#if 0
  71057. + DWC_PRINTF
  71058. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  71059. + dev_addr, ep_num, ep_dir, ep_type, mps);
  71060. +#endif
  71061. +}
  71062. +
  71063. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71064. + void *urb_handle, void *buf, dwc_dma_t dma,
  71065. + uint32_t buflen, void *setup_packet,
  71066. + dwc_dma_t setup_dma, uint32_t flags,
  71067. + uint16_t interval)
  71068. +{
  71069. + dwc_otg_urb->priv = urb_handle;
  71070. + dwc_otg_urb->buf = buf;
  71071. + dwc_otg_urb->dma = dma;
  71072. + dwc_otg_urb->length = buflen;
  71073. + dwc_otg_urb->setup_packet = setup_packet;
  71074. + dwc_otg_urb->setup_dma = setup_dma;
  71075. + dwc_otg_urb->flags = flags;
  71076. + dwc_otg_urb->interval = interval;
  71077. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  71078. +}
  71079. +
  71080. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71081. +{
  71082. + return dwc_otg_urb->status;
  71083. +}
  71084. +
  71085. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71086. +{
  71087. + return dwc_otg_urb->actual_length;
  71088. +}
  71089. +
  71090. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  71091. +{
  71092. + return dwc_otg_urb->error_count;
  71093. +}
  71094. +
  71095. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71096. + int desc_num, uint32_t offset,
  71097. + uint32_t length)
  71098. +{
  71099. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  71100. + dwc_otg_urb->iso_descs[desc_num].length = length;
  71101. +}
  71102. +
  71103. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  71104. + int desc_num)
  71105. +{
  71106. + return dwc_otg_urb->iso_descs[desc_num].status;
  71107. +}
  71108. +
  71109. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  71110. + dwc_otg_urb, int desc_num)
  71111. +{
  71112. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  71113. +}
  71114. +
  71115. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  71116. +{
  71117. + int allocated = 0;
  71118. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71119. +
  71120. + if (qh) {
  71121. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  71122. + allocated = 1;
  71123. + }
  71124. + }
  71125. + return allocated;
  71126. +}
  71127. +
  71128. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  71129. +{
  71130. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71131. + int freed = 0;
  71132. + DWC_ASSERT(qh, "qh is not allocated\n");
  71133. +
  71134. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  71135. + freed = 1;
  71136. + }
  71137. +
  71138. + return freed;
  71139. +}
  71140. +
  71141. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  71142. +{
  71143. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  71144. + DWC_ASSERT(qh, "qh is not allocated\n");
  71145. + return qh->usecs;
  71146. +}
  71147. +
  71148. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  71149. +{
  71150. +#ifdef DEBUG
  71151. + int num_channels;
  71152. + int i;
  71153. + gnptxsts_data_t np_tx_status;
  71154. + hptxsts_data_t p_tx_status;
  71155. +
  71156. + num_channels = hcd->core_if->core_params->host_channels;
  71157. + DWC_PRINTF("\n");
  71158. + DWC_PRINTF
  71159. + ("************************************************************\n");
  71160. + DWC_PRINTF("HCD State:\n");
  71161. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  71162. + for (i = 0; i < num_channels; i++) {
  71163. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  71164. + DWC_PRINTF(" Channel %d:\n", i);
  71165. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  71166. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  71167. + DWC_PRINTF(" speed: %d\n", hc->speed);
  71168. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  71169. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  71170. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  71171. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  71172. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  71173. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  71174. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  71175. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  71176. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  71177. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  71178. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  71179. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  71180. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  71181. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  71182. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  71183. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  71184. + DWC_PRINTF(" requests: %d\n", hc->requests);
  71185. + DWC_PRINTF(" qh: %p\n", hc->qh);
  71186. + if (hc->xfer_started) {
  71187. + hfnum_data_t hfnum;
  71188. + hcchar_data_t hcchar;
  71189. + hctsiz_data_t hctsiz;
  71190. + hcint_data_t hcint;
  71191. + hcintmsk_data_t hcintmsk;
  71192. + hfnum.d32 =
  71193. + DWC_READ_REG32(&hcd->core_if->
  71194. + host_if->host_global_regs->hfnum);
  71195. + hcchar.d32 =
  71196. + DWC_READ_REG32(&hcd->core_if->host_if->
  71197. + hc_regs[i]->hcchar);
  71198. + hctsiz.d32 =
  71199. + DWC_READ_REG32(&hcd->core_if->host_if->
  71200. + hc_regs[i]->hctsiz);
  71201. + hcint.d32 =
  71202. + DWC_READ_REG32(&hcd->core_if->host_if->
  71203. + hc_regs[i]->hcint);
  71204. + hcintmsk.d32 =
  71205. + DWC_READ_REG32(&hcd->core_if->host_if->
  71206. + hc_regs[i]->hcintmsk);
  71207. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  71208. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  71209. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  71210. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  71211. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  71212. + }
  71213. + if (hc->xfer_started && hc->qh) {
  71214. + dwc_otg_qtd_t *qtd;
  71215. + dwc_otg_hcd_urb_t *urb;
  71216. +
  71217. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  71218. + if (!qtd->in_process)
  71219. + break;
  71220. +
  71221. + urb = qtd->urb;
  71222. + DWC_PRINTF(" URB Info:\n");
  71223. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  71224. + if (urb) {
  71225. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  71226. + dwc_otg_hcd_get_dev_addr(&urb->
  71227. + pipe_info),
  71228. + dwc_otg_hcd_get_ep_num(&urb->
  71229. + pipe_info),
  71230. + dwc_otg_hcd_is_pipe_in(&urb->
  71231. + pipe_info) ?
  71232. + "IN" : "OUT");
  71233. + DWC_PRINTF(" Max packet size: %d\n",
  71234. + dwc_otg_hcd_get_mps(&urb->
  71235. + pipe_info));
  71236. + DWC_PRINTF(" transfer_buffer: %p\n",
  71237. + urb->buf);
  71238. + DWC_PRINTF(" transfer_dma: %p\n",
  71239. + (void *)urb->dma);
  71240. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  71241. + urb->length);
  71242. + DWC_PRINTF(" actual_length: %d\n",
  71243. + urb->actual_length);
  71244. + }
  71245. + }
  71246. + }
  71247. + }
  71248. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  71249. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  71250. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  71251. + np_tx_status.d32 =
  71252. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  71253. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  71254. + np_tx_status.b.nptxqspcavail);
  71255. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  71256. + np_tx_status.b.nptxfspcavail);
  71257. + p_tx_status.d32 =
  71258. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  71259. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  71260. + p_tx_status.b.ptxqspcavail);
  71261. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  71262. + dwc_otg_hcd_dump_frrem(hcd);
  71263. + dwc_otg_dump_global_registers(hcd->core_if);
  71264. + dwc_otg_dump_host_registers(hcd->core_if);
  71265. + DWC_PRINTF
  71266. + ("************************************************************\n");
  71267. + DWC_PRINTF("\n");
  71268. +#endif
  71269. +}
  71270. +
  71271. +#ifdef DEBUG
  71272. +void dwc_print_setup_data(uint8_t * setup)
  71273. +{
  71274. + int i;
  71275. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  71276. + DWC_PRINTF("Setup Data = MSB ");
  71277. + for (i = 7; i >= 0; i--)
  71278. + DWC_PRINTF("%02x ", setup[i]);
  71279. + DWC_PRINTF("\n");
  71280. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  71281. + (setup[0] & 0x80) ? "Device-to-Host" :
  71282. + "Host-to-Device");
  71283. + DWC_PRINTF(" bmRequestType Type = ");
  71284. + switch ((setup[0] & 0x60) >> 5) {
  71285. + case 0:
  71286. + DWC_PRINTF("Standard\n");
  71287. + break;
  71288. + case 1:
  71289. + DWC_PRINTF("Class\n");
  71290. + break;
  71291. + case 2:
  71292. + DWC_PRINTF("Vendor\n");
  71293. + break;
  71294. + case 3:
  71295. + DWC_PRINTF("Reserved\n");
  71296. + break;
  71297. + }
  71298. + DWC_PRINTF(" bmRequestType Recipient = ");
  71299. + switch (setup[0] & 0x1f) {
  71300. + case 0:
  71301. + DWC_PRINTF("Device\n");
  71302. + break;
  71303. + case 1:
  71304. + DWC_PRINTF("Interface\n");
  71305. + break;
  71306. + case 2:
  71307. + DWC_PRINTF("Endpoint\n");
  71308. + break;
  71309. + case 3:
  71310. + DWC_PRINTF("Other\n");
  71311. + break;
  71312. + default:
  71313. + DWC_PRINTF("Reserved\n");
  71314. + break;
  71315. + }
  71316. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  71317. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  71318. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  71319. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  71320. + }
  71321. +}
  71322. +#endif
  71323. +
  71324. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  71325. +{
  71326. +#if 0
  71327. + DWC_PRINTF("Frame remaining at SOF:\n");
  71328. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71329. + hcd->frrem_samples, hcd->frrem_accum,
  71330. + (hcd->frrem_samples > 0) ?
  71331. + hcd->frrem_accum / hcd->frrem_samples : 0);
  71332. +
  71333. + DWC_PRINTF("\n");
  71334. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  71335. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71336. + hcd->core_if->hfnum_7_samples,
  71337. + hcd->core_if->hfnum_7_frrem_accum,
  71338. + (hcd->core_if->hfnum_7_samples >
  71339. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  71340. + hcd->core_if->hfnum_7_samples : 0);
  71341. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  71342. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71343. + hcd->core_if->hfnum_0_samples,
  71344. + hcd->core_if->hfnum_0_frrem_accum,
  71345. + (hcd->core_if->hfnum_0_samples >
  71346. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  71347. + hcd->core_if->hfnum_0_samples : 0);
  71348. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  71349. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71350. + hcd->core_if->hfnum_other_samples,
  71351. + hcd->core_if->hfnum_other_frrem_accum,
  71352. + (hcd->core_if->hfnum_other_samples >
  71353. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  71354. + hcd->core_if->hfnum_other_samples : 0);
  71355. +
  71356. + DWC_PRINTF("\n");
  71357. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  71358. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71359. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  71360. + (hcd->hfnum_7_samples_a > 0) ?
  71361. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  71362. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  71363. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71364. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  71365. + (hcd->hfnum_0_samples_a > 0) ?
  71366. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  71367. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  71368. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71369. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  71370. + (hcd->hfnum_other_samples_a > 0) ?
  71371. + hcd->hfnum_other_frrem_accum_a /
  71372. + hcd->hfnum_other_samples_a : 0);
  71373. +
  71374. + DWC_PRINTF("\n");
  71375. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  71376. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71377. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  71378. + (hcd->hfnum_7_samples_b > 0) ?
  71379. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  71380. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  71381. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71382. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  71383. + (hcd->hfnum_0_samples_b > 0) ?
  71384. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  71385. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  71386. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  71387. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  71388. + (hcd->hfnum_other_samples_b > 0) ?
  71389. + hcd->hfnum_other_frrem_accum_b /
  71390. + hcd->hfnum_other_samples_b : 0);
  71391. +#endif
  71392. +}
  71393. +
  71394. +#endif /* DWC_DEVICE_ONLY */
  71395. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  71396. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 1970-01-01 01:00:00.000000000 +0100
  71397. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 2014-03-11 16:55:38.000000000 +0100
  71398. @@ -0,0 +1,1132 @@
  71399. +/*==========================================================================
  71400. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  71401. + * $Revision: #10 $
  71402. + * $Date: 2011/10/20 $
  71403. + * $Change: 1869464 $
  71404. + *
  71405. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  71406. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  71407. + * otherwise expressly agreed to in writing between Synopsys and you.
  71408. + *
  71409. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  71410. + * any End User Software License Agreement or Agreement for Licensed Product
  71411. + * with Synopsys or any supplement thereto. You are permitted to use and
  71412. + * redistribute this Software in source and binary forms, with or without
  71413. + * modification, provided that redistributions of source code must retain this
  71414. + * notice. You may not view, use, disclose, copy or distribute this file or
  71415. + * any information contained herein except pursuant to this license grant from
  71416. + * Synopsys. If you do not agree with this notice, including the disclaimer
  71417. + * below, then you are not authorized to use the Software.
  71418. + *
  71419. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  71420. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  71421. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  71422. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  71423. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  71424. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  71425. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  71426. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  71427. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  71428. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  71429. + * DAMAGE.
  71430. + * ========================================================================== */
  71431. +#ifndef DWC_DEVICE_ONLY
  71432. +
  71433. +/** @file
  71434. + * This file contains Descriptor DMA support implementation for host mode.
  71435. + */
  71436. +
  71437. +#include "dwc_otg_hcd.h"
  71438. +#include "dwc_otg_regs.h"
  71439. +
  71440. +extern bool microframe_schedule;
  71441. +
  71442. +static inline uint8_t frame_list_idx(uint16_t frame)
  71443. +{
  71444. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  71445. +}
  71446. +
  71447. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  71448. +{
  71449. + return (idx + inc) &
  71450. + (((speed ==
  71451. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  71452. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  71453. +}
  71454. +
  71455. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  71456. +{
  71457. + return (idx - inc) &
  71458. + (((speed ==
  71459. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  71460. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  71461. +}
  71462. +
  71463. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  71464. +{
  71465. + return (((qh->ep_type == UE_ISOCHRONOUS)
  71466. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  71467. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  71468. +}
  71469. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  71470. +{
  71471. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  71472. + ? ((qh->interval + 8 - 1) / 8)
  71473. + : qh->interval);
  71474. +}
  71475. +
  71476. +static int desc_list_alloc(dwc_otg_qh_t * qh)
  71477. +{
  71478. + int retval = 0;
  71479. +
  71480. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  71481. + DWC_DMA_ALLOC(sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  71482. + &qh->desc_list_dma);
  71483. +
  71484. + if (!qh->desc_list) {
  71485. + retval = -DWC_E_NO_MEMORY;
  71486. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  71487. +
  71488. + }
  71489. +
  71490. + dwc_memset(qh->desc_list, 0x00,
  71491. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  71492. +
  71493. + qh->n_bytes =
  71494. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  71495. +
  71496. + if (!qh->n_bytes) {
  71497. + retval = -DWC_E_NO_MEMORY;
  71498. + DWC_ERROR
  71499. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  71500. + __func__);
  71501. +
  71502. + }
  71503. + return retval;
  71504. +
  71505. +}
  71506. +
  71507. +static void desc_list_free(dwc_otg_qh_t * qh)
  71508. +{
  71509. + if (qh->desc_list) {
  71510. + DWC_DMA_FREE(max_desc_num(qh), qh->desc_list,
  71511. + qh->desc_list_dma);
  71512. + qh->desc_list = NULL;
  71513. + }
  71514. +
  71515. + if (qh->n_bytes) {
  71516. + DWC_FREE(qh->n_bytes);
  71517. + qh->n_bytes = NULL;
  71518. + }
  71519. +}
  71520. +
  71521. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  71522. +{
  71523. + int retval = 0;
  71524. + if (hcd->frame_list)
  71525. + return 0;
  71526. +
  71527. + hcd->frame_list = DWC_DMA_ALLOC(4 * MAX_FRLIST_EN_NUM,
  71528. + &hcd->frame_list_dma);
  71529. + if (!hcd->frame_list) {
  71530. + retval = -DWC_E_NO_MEMORY;
  71531. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  71532. + }
  71533. +
  71534. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  71535. +
  71536. + return retval;
  71537. +}
  71538. +
  71539. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  71540. +{
  71541. + if (!hcd->frame_list)
  71542. + return;
  71543. +
  71544. + DWC_DMA_FREE(4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  71545. + hcd->frame_list = NULL;
  71546. +}
  71547. +
  71548. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  71549. +{
  71550. +
  71551. + hcfg_data_t hcfg;
  71552. +
  71553. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  71554. +
  71555. + if (hcfg.b.perschedena) {
  71556. + /* already enabled */
  71557. + return;
  71558. + }
  71559. +
  71560. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  71561. + hcd->frame_list_dma);
  71562. +
  71563. + switch (fr_list_en) {
  71564. + case 64:
  71565. + hcfg.b.frlisten = 3;
  71566. + break;
  71567. + case 32:
  71568. + hcfg.b.frlisten = 2;
  71569. + break;
  71570. + case 16:
  71571. + hcfg.b.frlisten = 1;
  71572. + break;
  71573. + case 8:
  71574. + hcfg.b.frlisten = 0;
  71575. + break;
  71576. + default:
  71577. + break;
  71578. + }
  71579. +
  71580. + hcfg.b.perschedena = 1;
  71581. +
  71582. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  71583. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  71584. +
  71585. +}
  71586. +
  71587. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  71588. +{
  71589. + hcfg_data_t hcfg;
  71590. +
  71591. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  71592. +
  71593. + if (!hcfg.b.perschedena) {
  71594. + /* already disabled */
  71595. + return;
  71596. + }
  71597. + hcfg.b.perschedena = 0;
  71598. +
  71599. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  71600. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  71601. +}
  71602. +
  71603. +/*
  71604. + * Activates/Deactivates FrameList entries for the channel
  71605. + * based on endpoint servicing period.
  71606. + */
  71607. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  71608. +{
  71609. + uint16_t i, j, inc;
  71610. + dwc_hc_t *hc = NULL;
  71611. +
  71612. + if (!qh->channel) {
  71613. + DWC_ERROR("qh->channel = %p", qh->channel);
  71614. + return;
  71615. + }
  71616. +
  71617. + if (!hcd) {
  71618. + DWC_ERROR("------hcd = %p", hcd);
  71619. + return;
  71620. + }
  71621. +
  71622. + if (!hcd->frame_list) {
  71623. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  71624. + return;
  71625. + }
  71626. +
  71627. + hc = qh->channel;
  71628. + inc = frame_incr_val(qh);
  71629. + if (qh->ep_type == UE_ISOCHRONOUS)
  71630. + i = frame_list_idx(qh->sched_frame);
  71631. + else
  71632. + i = 0;
  71633. +
  71634. + j = i;
  71635. + do {
  71636. + if (enable)
  71637. + hcd->frame_list[j] |= (1 << hc->hc_num);
  71638. + else
  71639. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  71640. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  71641. + }
  71642. + while (j != i);
  71643. + if (!enable)
  71644. + return;
  71645. + hc->schinfo = 0;
  71646. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  71647. + j = 1;
  71648. + /* TODO - check this */
  71649. + inc = (8 + qh->interval - 1) / qh->interval;
  71650. + for (i = 0; i < inc; i++) {
  71651. + hc->schinfo |= j;
  71652. + j = j << qh->interval;
  71653. + }
  71654. + } else {
  71655. + hc->schinfo = 0xff;
  71656. + }
  71657. +}
  71658. +
  71659. +#if 1
  71660. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  71661. +{
  71662. + int i = 0;
  71663. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  71664. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  71665. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  71666. + if (!(i % 8) && i)
  71667. + DWC_PRINTF("\n");
  71668. + }
  71669. + DWC_PRINTF("\n----\n");
  71670. +
  71671. +}
  71672. +#endif
  71673. +
  71674. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  71675. +{
  71676. + dwc_irqflags_t flags;
  71677. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  71678. +
  71679. + dwc_hc_t *hc = qh->channel;
  71680. + if (dwc_qh_is_non_per(qh)) {
  71681. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  71682. + if (!microframe_schedule)
  71683. + hcd->non_periodic_channels--;
  71684. + else
  71685. + hcd->available_host_channels++;
  71686. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  71687. + } else
  71688. + update_frame_list(hcd, qh, 0);
  71689. +
  71690. + /*
  71691. + * The condition is added to prevent double cleanup try in case of device
  71692. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  71693. + */
  71694. + if (hc->qh) {
  71695. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  71696. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  71697. + hc->qh = NULL;
  71698. + }
  71699. +
  71700. + qh->channel = NULL;
  71701. + qh->ntd = 0;
  71702. +
  71703. + if (qh->desc_list) {
  71704. + dwc_memset(qh->desc_list, 0x00,
  71705. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  71706. + }
  71707. +}
  71708. +
  71709. +/**
  71710. + * Initializes a QH structure's Descriptor DMA related members.
  71711. + * Allocates memory for descriptor list.
  71712. + * On first periodic QH, allocates memory for FrameList
  71713. + * and enables periodic scheduling.
  71714. + *
  71715. + * @param hcd The HCD state structure for the DWC OTG controller.
  71716. + * @param qh The QH to init.
  71717. + *
  71718. + * @return 0 if successful, negative error code otherwise.
  71719. + */
  71720. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  71721. +{
  71722. + int retval = 0;
  71723. +
  71724. + if (qh->do_split) {
  71725. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  71726. + return -1;
  71727. + }
  71728. +
  71729. + retval = desc_list_alloc(qh);
  71730. +
  71731. + if ((retval == 0)
  71732. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  71733. + if (!hcd->frame_list) {
  71734. + retval = frame_list_alloc(hcd);
  71735. + /* Enable periodic schedule on first periodic QH */
  71736. + if (retval == 0)
  71737. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  71738. + }
  71739. + }
  71740. +
  71741. + qh->ntd = 0;
  71742. +
  71743. + return retval;
  71744. +}
  71745. +
  71746. +/**
  71747. + * Frees descriptor list memory associated with the QH.
  71748. + * If QH is periodic and the last, frees FrameList memory
  71749. + * and disables periodic scheduling.
  71750. + *
  71751. + * @param hcd The HCD state structure for the DWC OTG controller.
  71752. + * @param qh The QH to init.
  71753. + */
  71754. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  71755. +{
  71756. + desc_list_free(qh);
  71757. +
  71758. + /*
  71759. + * Channel still assigned due to some reasons.
  71760. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  71761. + * ChHalted interrupt to release the channel. Afterwards
  71762. + * when it comes here from endpoint disable routine
  71763. + * channel remains assigned.
  71764. + */
  71765. + if (qh->channel)
  71766. + release_channel_ddma(hcd, qh);
  71767. +
  71768. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  71769. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  71770. +
  71771. + per_sched_disable(hcd);
  71772. + frame_list_free(hcd);
  71773. + }
  71774. +}
  71775. +
  71776. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  71777. +{
  71778. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  71779. + /*
  71780. + * Descriptor set(8 descriptors) index
  71781. + * which is 8-aligned.
  71782. + */
  71783. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  71784. + } else {
  71785. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  71786. + }
  71787. +}
  71788. +
  71789. +/*
  71790. + * Determine starting frame for Isochronous transfer.
  71791. + * Few frames skipped to prevent race condition with HC.
  71792. + */
  71793. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  71794. + uint8_t * skip_frames)
  71795. +{
  71796. + uint16_t frame = 0;
  71797. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  71798. +
  71799. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  71800. +
  71801. + /*
  71802. + * skip_frames is used to limit activated descriptors number
  71803. + * to avoid the situation when HC services the last activated
  71804. + * descriptor firstly.
  71805. + * Example for FS:
  71806. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  71807. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  71808. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  71809. + * list will be fully programmed with Active descriptors and it is possible
  71810. + * case(rare) that the latest descriptor(considering rollback) corresponding
  71811. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  71812. + * up to 11 uframes(16 in the code) may be skipped.
  71813. + */
  71814. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  71815. + /*
  71816. + * Consider uframe counter also, to start xfer asap.
  71817. + * If half of the frame elapsed skip 2 frames otherwise
  71818. + * just 1 frame.
  71819. + * Starting descriptor index must be 8-aligned, so
  71820. + * if the current frame is near to complete the next one
  71821. + * is skipped as well.
  71822. + */
  71823. +
  71824. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  71825. + *skip_frames = 2 * 8;
  71826. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  71827. + } else {
  71828. + *skip_frames = 1 * 8;
  71829. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  71830. + }
  71831. +
  71832. + frame = dwc_full_frame_num(frame);
  71833. + } else {
  71834. + /*
  71835. + * Two frames are skipped for FS - the current and the next.
  71836. + * But for descriptor programming, 1 frame(descriptor) is enough,
  71837. + * see example above.
  71838. + */
  71839. + *skip_frames = 1;
  71840. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  71841. + }
  71842. +
  71843. + return frame;
  71844. +}
  71845. +
  71846. +/*
  71847. + * Calculate initial descriptor index for isochronous transfer
  71848. + * based on scheduled frame.
  71849. + */
  71850. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  71851. +{
  71852. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  71853. + uint8_t skip_frames = 0;
  71854. + /*
  71855. + * With current ISOC processing algorithm the channel is being
  71856. + * released when no more QTDs in the list(qh->ntd == 0).
  71857. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  71858. + *
  71859. + * So qh->channel != NULL branch is not used and just not removed from the
  71860. + * source file. It is required for another possible approach which is,
  71861. + * do not disable and release the channel when ISOC session completed,
  71862. + * just move QH to inactive schedule until new QTD arrives.
  71863. + * On new QTD, the QH moved back to 'ready' schedule,
  71864. + * starting frame and therefore starting desc_index are recalculated.
  71865. + * In this case channel is released only on ep_disable.
  71866. + */
  71867. +
  71868. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  71869. + if (qh->channel) {
  71870. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  71871. + /*
  71872. + * Calculate initial descriptor index based on FrameList current bitmap
  71873. + * and servicing period.
  71874. + */
  71875. + fr_idx_tmp = frame_list_idx(frame);
  71876. + fr_idx =
  71877. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  71878. + fr_idx_tmp)
  71879. + % frame_incr_val(qh);
  71880. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  71881. + } else {
  71882. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  71883. + fr_idx = frame_list_idx(qh->sched_frame);
  71884. + }
  71885. +
  71886. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  71887. +
  71888. + return skip_frames;
  71889. +}
  71890. +
  71891. +#define ISOC_URB_GIVEBACK_ASAP
  71892. +
  71893. +#define MAX_ISOC_XFER_SIZE_FS 1023
  71894. +#define MAX_ISOC_XFER_SIZE_HS 3072
  71895. +#define DESCNUM_THRESHOLD 4
  71896. +
  71897. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  71898. + uint8_t skip_frames)
  71899. +{
  71900. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  71901. + dwc_otg_qtd_t *qtd;
  71902. + dwc_otg_host_dma_desc_t *dma_desc;
  71903. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  71904. +
  71905. + idx = qh->td_last;
  71906. + inc = qh->interval;
  71907. + n_desc = 0;
  71908. +
  71909. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  71910. + if (skip_frames && !qh->channel)
  71911. + ntd_max = ntd_max - skip_frames / qh->interval;
  71912. +
  71913. + max_xfer_size =
  71914. + (qh->dev_speed ==
  71915. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  71916. + MAX_ISOC_XFER_SIZE_FS;
  71917. +
  71918. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  71919. + while ((qh->ntd < ntd_max)
  71920. + && (qtd->isoc_frame_index_last <
  71921. + qtd->urb->packet_count)) {
  71922. +
  71923. + dma_desc = &qh->desc_list[idx];
  71924. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  71925. +
  71926. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  71927. +
  71928. + if (frame_desc->length > max_xfer_size)
  71929. + qh->n_bytes[idx] = max_xfer_size;
  71930. + else
  71931. + qh->n_bytes[idx] = frame_desc->length;
  71932. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  71933. + dma_desc->status.b_isoc.a = 1;
  71934. + dma_desc->status.b_isoc.sts = 0;
  71935. +
  71936. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  71937. +
  71938. + qh->ntd++;
  71939. +
  71940. + qtd->isoc_frame_index_last++;
  71941. +
  71942. +#ifdef ISOC_URB_GIVEBACK_ASAP
  71943. + /*
  71944. + * Set IOC for each descriptor corresponding to the
  71945. + * last frame of the URB.
  71946. + */
  71947. + if (qtd->isoc_frame_index_last ==
  71948. + qtd->urb->packet_count)
  71949. + dma_desc->status.b_isoc.ioc = 1;
  71950. +
  71951. +#endif
  71952. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  71953. + n_desc++;
  71954. +
  71955. + }
  71956. + qtd->in_process = 1;
  71957. + }
  71958. +
  71959. + qh->td_last = idx;
  71960. +
  71961. +#ifdef ISOC_URB_GIVEBACK_ASAP
  71962. + /* Set IOC for the last descriptor if descriptor list is full */
  71963. + if (qh->ntd == ntd_max) {
  71964. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  71965. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  71966. + }
  71967. +#else
  71968. + /*
  71969. + * Set IOC bit only for one descriptor.
  71970. + * Always try to be ahead of HW processing,
  71971. + * i.e. on IOC generation driver activates next descriptors but
  71972. + * core continues to process descriptors followed the one with IOC set.
  71973. + */
  71974. +
  71975. + if (n_desc > DESCNUM_THRESHOLD) {
  71976. + /*
  71977. + * Move IOC "up". Required even if there is only one QTD
  71978. + * in the list, cause QTDs migth continue to be queued,
  71979. + * but during the activation it was only one queued.
  71980. + * Actually more than one QTD might be in the list if this function called
  71981. + * from XferCompletion - QTDs was queued during HW processing of the previous
  71982. + * descriptor chunk.
  71983. + */
  71984. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  71985. + } else {
  71986. + /*
  71987. + * Set the IOC for the latest descriptor
  71988. + * if either number of descriptor is not greather than threshold
  71989. + * or no more new descriptors activated.
  71990. + */
  71991. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  71992. + }
  71993. +
  71994. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  71995. +#endif
  71996. +}
  71997. +
  71998. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  71999. +{
  72000. +
  72001. + dwc_hc_t *hc;
  72002. + dwc_otg_host_dma_desc_t *dma_desc;
  72003. + dwc_otg_qtd_t *qtd;
  72004. + int num_packets, len, n_desc = 0;
  72005. +
  72006. + hc = qh->channel;
  72007. +
  72008. + /*
  72009. + * Start with hc->xfer_buff initialized in
  72010. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  72011. + * this pointer re-assigned to the buffer of the currently processed QTD.
  72012. + * For non-SG request there is always one QTD active.
  72013. + */
  72014. +
  72015. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  72016. +
  72017. + if (n_desc) {
  72018. + /* SG request - more than 1 QTDs */
  72019. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  72020. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  72021. + }
  72022. +
  72023. + qtd->n_desc = 0;
  72024. +
  72025. + do {
  72026. + dma_desc = &qh->desc_list[n_desc];
  72027. + len = hc->xfer_len;
  72028. +
  72029. + if (len > MAX_DMA_DESC_SIZE)
  72030. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  72031. +
  72032. + if (hc->ep_is_in) {
  72033. + if (len > 0) {
  72034. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  72035. + } else {
  72036. + /* Need 1 packet for transfer length of 0. */
  72037. + num_packets = 1;
  72038. + }
  72039. + /* Always program an integral # of max packets for IN transfers. */
  72040. + len = num_packets * hc->max_packet;
  72041. + }
  72042. +
  72043. + dma_desc->status.b.n_bytes = len;
  72044. +
  72045. + qh->n_bytes[n_desc] = len;
  72046. +
  72047. + if ((qh->ep_type == UE_CONTROL)
  72048. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  72049. + dma_desc->status.b.sup = 1; /* Setup Packet */
  72050. +
  72051. + dma_desc->status.b.a = 1; /* Active descriptor */
  72052. + dma_desc->status.b.sts = 0;
  72053. +
  72054. + dma_desc->buf =
  72055. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  72056. +
  72057. + /*
  72058. + * Last descriptor(or single) of IN transfer
  72059. + * with actual size less than MaxPacket.
  72060. + */
  72061. + if (len > hc->xfer_len) {
  72062. + hc->xfer_len = 0;
  72063. + } else {
  72064. + hc->xfer_buff += len;
  72065. + hc->xfer_len -= len;
  72066. + }
  72067. +
  72068. + qtd->n_desc++;
  72069. + n_desc++;
  72070. + }
  72071. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  72072. +
  72073. +
  72074. + qtd->in_process = 1;
  72075. +
  72076. + if (qh->ep_type == UE_CONTROL)
  72077. + break;
  72078. +
  72079. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  72080. + break;
  72081. + }
  72082. +
  72083. + if (n_desc) {
  72084. + /* Request Transfer Complete interrupt for the last descriptor */
  72085. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  72086. + /* End of List indicator */
  72087. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  72088. +
  72089. + hc->ntd = n_desc;
  72090. + }
  72091. +}
  72092. +
  72093. +/**
  72094. + * For Control and Bulk endpoints initializes descriptor list
  72095. + * and starts the transfer.
  72096. + *
  72097. + * For Interrupt and Isochronous endpoints initializes descriptor list
  72098. + * then updates FrameList, marking appropriate entries as active.
  72099. + * In case of Isochronous, the starting descriptor index is calculated based
  72100. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  72101. + * Then starts the transfer via enabling the channel.
  72102. + * For Isochronous endpoint the channel is not halted on XferComplete
  72103. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  72104. + *
  72105. + * @param hcd The HCD state structure for the DWC OTG controller.
  72106. + * @param qh The QH to init.
  72107. + *
  72108. + * @return 0 if successful, negative error code otherwise.
  72109. + */
  72110. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  72111. +{
  72112. + /* Channel is already assigned */
  72113. + dwc_hc_t *hc = qh->channel;
  72114. + uint8_t skip_frames = 0;
  72115. +
  72116. + switch (hc->ep_type) {
  72117. + case DWC_OTG_EP_TYPE_CONTROL:
  72118. + case DWC_OTG_EP_TYPE_BULK:
  72119. + init_non_isoc_dma_desc(hcd, qh);
  72120. +
  72121. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72122. + break;
  72123. + case DWC_OTG_EP_TYPE_INTR:
  72124. + init_non_isoc_dma_desc(hcd, qh);
  72125. +
  72126. + update_frame_list(hcd, qh, 1);
  72127. +
  72128. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72129. + break;
  72130. + case DWC_OTG_EP_TYPE_ISOC:
  72131. +
  72132. + if (!qh->ntd)
  72133. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  72134. +
  72135. + init_isoc_dma_desc(hcd, qh, skip_frames);
  72136. +
  72137. + if (!hc->xfer_started) {
  72138. +
  72139. + update_frame_list(hcd, qh, 1);
  72140. +
  72141. + /*
  72142. + * Always set to max, instead of actual size.
  72143. + * Otherwise ntd will be changed with
  72144. + * channel being enabled. Not recommended.
  72145. + *
  72146. + */
  72147. + hc->ntd = max_desc_num(qh);
  72148. + /* Enable channel only once for ISOC */
  72149. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  72150. + }
  72151. +
  72152. + break;
  72153. + default:
  72154. +
  72155. + break;
  72156. + }
  72157. +}
  72158. +
  72159. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  72160. + dwc_hc_t * hc,
  72161. + dwc_otg_hc_regs_t * hc_regs,
  72162. + dwc_otg_halt_status_e halt_status)
  72163. +{
  72164. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  72165. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  72166. + dwc_otg_qh_t *qh;
  72167. + dwc_otg_host_dma_desc_t *dma_desc;
  72168. + uint16_t idx, remain;
  72169. + uint8_t urb_compl;
  72170. +
  72171. + qh = hc->qh;
  72172. + idx = qh->td_first;
  72173. +
  72174. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  72175. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  72176. + qtd->in_process = 0;
  72177. + return;
  72178. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  72179. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  72180. + /*
  72181. + * Channel is halted in these error cases.
  72182. + * Considered as serious issues.
  72183. + * Complete all URBs marking all frames as failed,
  72184. + * irrespective whether some of the descriptors(frames) succeeded or no.
  72185. + * Pass error code to completion routine as well, to
  72186. + * update urb->status, some of class drivers might use it to stop
  72187. + * queing transfer requests.
  72188. + */
  72189. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  72190. + ? (-DWC_E_IO)
  72191. + : (-DWC_E_OVERFLOW);
  72192. +
  72193. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  72194. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  72195. + frame_desc = &qtd->urb->iso_descs[idx];
  72196. + frame_desc->status = err;
  72197. + }
  72198. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  72199. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  72200. + }
  72201. + return;
  72202. + }
  72203. +
  72204. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  72205. +
  72206. + if (!qtd->in_process)
  72207. + break;
  72208. +
  72209. + urb_compl = 0;
  72210. +
  72211. + do {
  72212. +
  72213. + dma_desc = &qh->desc_list[idx];
  72214. +
  72215. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  72216. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  72217. +
  72218. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  72219. + /*
  72220. + * XactError or, unable to complete all the transactions
  72221. + * in the scheduled micro-frame/frame,
  72222. + * both indicated by DMA_DESC_STS_PKTERR.
  72223. + */
  72224. + qtd->urb->error_count++;
  72225. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  72226. + frame_desc->status = -DWC_E_PROTOCOL;
  72227. + } else {
  72228. + /* Success */
  72229. +
  72230. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  72231. + frame_desc->status = 0;
  72232. + }
  72233. +
  72234. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  72235. + /*
  72236. + * urb->status is not used for isoc transfers here.
  72237. + * The individual frame_desc status are used instead.
  72238. + */
  72239. +
  72240. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  72241. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  72242. +
  72243. + /*
  72244. + * This check is necessary because urb_dequeue can be called
  72245. + * from urb complete callback(sound driver example).
  72246. + * All pending URBs are dequeued there, so no need for
  72247. + * further processing.
  72248. + */
  72249. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  72250. + return;
  72251. + }
  72252. +
  72253. + urb_compl = 1;
  72254. +
  72255. + }
  72256. +
  72257. + qh->ntd--;
  72258. +
  72259. + /* Stop if IOC requested descriptor reached */
  72260. + if (dma_desc->status.b_isoc.ioc) {
  72261. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  72262. + goto stop_scan;
  72263. + }
  72264. +
  72265. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  72266. +
  72267. + if (urb_compl)
  72268. + break;
  72269. + }
  72270. + while (idx != qh->td_first);
  72271. + }
  72272. +stop_scan:
  72273. + qh->td_first = idx;
  72274. +}
  72275. +
  72276. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  72277. + dwc_hc_t * hc,
  72278. + dwc_otg_qtd_t * qtd,
  72279. + dwc_otg_host_dma_desc_t * dma_desc,
  72280. + dwc_otg_halt_status_e halt_status,
  72281. + uint32_t n_bytes, uint8_t * xfer_done)
  72282. +{
  72283. +
  72284. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  72285. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  72286. +
  72287. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  72288. + urb->status = -DWC_E_IO;
  72289. + return 1;
  72290. + }
  72291. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  72292. + switch (halt_status) {
  72293. + case DWC_OTG_HC_XFER_STALL:
  72294. + urb->status = -DWC_E_PIPE;
  72295. + break;
  72296. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  72297. + urb->status = -DWC_E_OVERFLOW;
  72298. + break;
  72299. + case DWC_OTG_HC_XFER_XACT_ERR:
  72300. + urb->status = -DWC_E_PROTOCOL;
  72301. + break;
  72302. + default:
  72303. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  72304. + halt_status);
  72305. + break;
  72306. + }
  72307. + return 1;
  72308. + }
  72309. +
  72310. + if (dma_desc->status.b.a == 1) {
  72311. + DWC_DEBUGPL(DBG_HCDV,
  72312. + "Active descriptor encountered on channel %d\n",
  72313. + hc->hc_num);
  72314. + return 0;
  72315. + }
  72316. +
  72317. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  72318. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  72319. + urb->actual_length += n_bytes - remain;
  72320. + if (remain || urb->actual_length == urb->length) {
  72321. + /*
  72322. + * For Control Data stage do not set urb->status=0 to prevent
  72323. + * URB callback. Set it when Status phase done. See below.
  72324. + */
  72325. + *xfer_done = 1;
  72326. + }
  72327. +
  72328. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  72329. + urb->status = 0;
  72330. + *xfer_done = 1;
  72331. + }
  72332. + /* No handling for SETUP stage */
  72333. + } else {
  72334. + /* BULK and INTR */
  72335. + urb->actual_length += n_bytes - remain;
  72336. + if (remain || urb->actual_length == urb->length) {
  72337. + urb->status = 0;
  72338. + *xfer_done = 1;
  72339. + }
  72340. + }
  72341. +
  72342. + return 0;
  72343. +}
  72344. +
  72345. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  72346. + dwc_hc_t * hc,
  72347. + dwc_otg_hc_regs_t * hc_regs,
  72348. + dwc_otg_halt_status_e halt_status)
  72349. +{
  72350. + dwc_otg_hcd_urb_t *urb = NULL;
  72351. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  72352. + dwc_otg_qh_t *qh;
  72353. + dwc_otg_host_dma_desc_t *dma_desc;
  72354. + uint32_t n_bytes, n_desc, i;
  72355. + uint8_t failed = 0, xfer_done;
  72356. +
  72357. + n_desc = 0;
  72358. +
  72359. + qh = hc->qh;
  72360. +
  72361. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  72362. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  72363. + qtd->in_process = 0;
  72364. + }
  72365. + return;
  72366. + }
  72367. +
  72368. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  72369. +
  72370. + urb = qtd->urb;
  72371. +
  72372. + n_bytes = 0;
  72373. + xfer_done = 0;
  72374. +
  72375. + for (i = 0; i < qtd->n_desc; i++) {
  72376. + dma_desc = &qh->desc_list[n_desc];
  72377. +
  72378. + n_bytes = qh->n_bytes[n_desc];
  72379. +
  72380. + failed =
  72381. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  72382. + dma_desc,
  72383. + halt_status, n_bytes,
  72384. + &xfer_done);
  72385. +
  72386. + if (failed
  72387. + || (xfer_done
  72388. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  72389. +
  72390. + hcd->fops->complete(hcd, urb->priv, urb,
  72391. + urb->status);
  72392. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  72393. +
  72394. + if (failed)
  72395. + goto stop_scan;
  72396. + } else if (qh->ep_type == UE_CONTROL) {
  72397. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  72398. + if (urb->length > 0) {
  72399. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  72400. + } else {
  72401. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  72402. + }
  72403. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  72404. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  72405. + if (xfer_done) {
  72406. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  72407. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  72408. + } else if (i + 1 == qtd->n_desc) {
  72409. + /*
  72410. + * Last descriptor for Control data stage which is
  72411. + * not completed yet.
  72412. + */
  72413. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  72414. + }
  72415. + }
  72416. + }
  72417. +
  72418. + n_desc++;
  72419. + }
  72420. +
  72421. + }
  72422. +
  72423. +stop_scan:
  72424. +
  72425. + if (qh->ep_type != UE_CONTROL) {
  72426. + /*
  72427. + * Resetting the data toggle for bulk
  72428. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  72429. + */
  72430. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  72431. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  72432. + else
  72433. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  72434. + }
  72435. +
  72436. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  72437. + hcint_data_t hcint;
  72438. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  72439. + if (hcint.b.nyet) {
  72440. + /*
  72441. + * Got a NYET on the last transaction of the transfer. It
  72442. + * means that the endpoint should be in the PING state at the
  72443. + * beginning of the next transfer.
  72444. + */
  72445. + qh->ping_state = 1;
  72446. + clear_hc_int(hc_regs, nyet);
  72447. + }
  72448. +
  72449. + }
  72450. +
  72451. +}
  72452. +
  72453. +/**
  72454. + * This function is called from interrupt handlers.
  72455. + * Scans the descriptor list, updates URB's status and
  72456. + * calls completion routine for the URB if it's done.
  72457. + * Releases the channel to be used by other transfers.
  72458. + * In case of Isochronous endpoint the channel is not halted until
  72459. + * the end of the session, i.e. QTD list is empty.
  72460. + * If periodic channel released the FrameList is updated accordingly.
  72461. + *
  72462. + * Calls transaction selection routines to activate pending transfers.
  72463. + *
  72464. + * @param hcd The HCD state structure for the DWC OTG controller.
  72465. + * @param hc Host channel, the transfer is completed on.
  72466. + * @param hc_regs Host channel registers.
  72467. + * @param halt_status Reason the channel is being halted,
  72468. + * or just XferComplete for isochronous transfer
  72469. + */
  72470. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  72471. + dwc_hc_t * hc,
  72472. + dwc_otg_hc_regs_t * hc_regs,
  72473. + dwc_otg_halt_status_e halt_status)
  72474. +{
  72475. + uint8_t continue_isoc_xfer = 0;
  72476. + dwc_otg_transaction_type_e tr_type;
  72477. + dwc_otg_qh_t *qh = hc->qh;
  72478. +
  72479. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  72480. +
  72481. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  72482. +
  72483. + /* Release the channel if halted or session completed */
  72484. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  72485. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  72486. +
  72487. + /* Halt the channel if session completed */
  72488. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  72489. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  72490. + }
  72491. +
  72492. + release_channel_ddma(hcd, qh);
  72493. + dwc_otg_hcd_qh_remove(hcd, qh);
  72494. + } else {
  72495. + /* Keep in assigned schedule to continue transfer */
  72496. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  72497. + &qh->qh_list_entry);
  72498. + continue_isoc_xfer = 1;
  72499. +
  72500. + }
  72501. + /** @todo Consider the case when period exceeds FrameList size.
  72502. + * Frame Rollover interrupt should be used.
  72503. + */
  72504. + } else {
  72505. + /* Scan descriptor list to complete the URB(s), then release the channel */
  72506. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  72507. +
  72508. + release_channel_ddma(hcd, qh);
  72509. + dwc_otg_hcd_qh_remove(hcd, qh);
  72510. +
  72511. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  72512. + /* Add back to inactive non-periodic schedule on normal completion */
  72513. + dwc_otg_hcd_qh_add(hcd, qh);
  72514. + }
  72515. +
  72516. + }
  72517. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  72518. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  72519. + if (continue_isoc_xfer) {
  72520. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  72521. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  72522. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  72523. + tr_type = DWC_OTG_TRANSACTION_ALL;
  72524. + }
  72525. + }
  72526. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  72527. + }
  72528. +}
  72529. +
  72530. +#endif /* DWC_DEVICE_ONLY */
  72531. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  72532. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 1970-01-01 01:00:00.000000000 +0100
  72533. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 2014-03-11 16:55:38.000000000 +0100
  72534. @@ -0,0 +1,851 @@
  72535. +/* ==========================================================================
  72536. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  72537. + * $Revision: #58 $
  72538. + * $Date: 2011/09/15 $
  72539. + * $Change: 1846647 $
  72540. + *
  72541. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  72542. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  72543. + * otherwise expressly agreed to in writing between Synopsys and you.
  72544. + *
  72545. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  72546. + * any End User Software License Agreement or Agreement for Licensed Product
  72547. + * with Synopsys or any supplement thereto. You are permitted to use and
  72548. + * redistribute this Software in source and binary forms, with or without
  72549. + * modification, provided that redistributions of source code must retain this
  72550. + * notice. You may not view, use, disclose, copy or distribute this file or
  72551. + * any information contained herein except pursuant to this license grant from
  72552. + * Synopsys. If you do not agree with this notice, including the disclaimer
  72553. + * below, then you are not authorized to use the Software.
  72554. + *
  72555. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  72556. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72557. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  72558. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  72559. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  72560. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72561. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  72562. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  72563. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72564. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  72565. + * DAMAGE.
  72566. + * ========================================================================== */
  72567. +#ifndef DWC_DEVICE_ONLY
  72568. +#ifndef __DWC_HCD_H__
  72569. +#define __DWC_HCD_H__
  72570. +
  72571. +#include "dwc_otg_os_dep.h"
  72572. +#include "usb.h"
  72573. +#include "dwc_otg_hcd_if.h"
  72574. +#include "dwc_otg_core_if.h"
  72575. +#include "dwc_list.h"
  72576. +#include "dwc_otg_cil.h"
  72577. +
  72578. +/**
  72579. + * @file
  72580. + *
  72581. + * This file contains the structures, constants, and interfaces for
  72582. + * the Host Contoller Driver (HCD).
  72583. + *
  72584. + * The Host Controller Driver (HCD) is responsible for translating requests
  72585. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  72586. + * It isolates the USBD from the specifics of the controller by providing an
  72587. + * API to the USBD.
  72588. + */
  72589. +
  72590. +struct dwc_otg_hcd_pipe_info {
  72591. + uint8_t dev_addr;
  72592. + uint8_t ep_num;
  72593. + uint8_t pipe_type;
  72594. + uint8_t pipe_dir;
  72595. + uint16_t mps;
  72596. +};
  72597. +
  72598. +struct dwc_otg_hcd_iso_packet_desc {
  72599. + uint32_t offset;
  72600. + uint32_t length;
  72601. + uint32_t actual_length;
  72602. + uint32_t status;
  72603. +};
  72604. +
  72605. +struct dwc_otg_qtd;
  72606. +
  72607. +struct dwc_otg_hcd_urb {
  72608. + void *priv;
  72609. + struct dwc_otg_qtd *qtd;
  72610. + void *buf;
  72611. + dwc_dma_t dma;
  72612. + void *setup_packet;
  72613. + dwc_dma_t setup_dma;
  72614. + uint32_t length;
  72615. + uint32_t actual_length;
  72616. + uint32_t status;
  72617. + uint32_t error_count;
  72618. + uint32_t packet_count;
  72619. + uint32_t flags;
  72620. + uint16_t interval;
  72621. + struct dwc_otg_hcd_pipe_info pipe_info;
  72622. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  72623. +};
  72624. +
  72625. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  72626. +{
  72627. + return pipe->ep_num;
  72628. +}
  72629. +
  72630. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  72631. + *pipe)
  72632. +{
  72633. + return pipe->pipe_type;
  72634. +}
  72635. +
  72636. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  72637. +{
  72638. + return pipe->mps;
  72639. +}
  72640. +
  72641. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  72642. + *pipe)
  72643. +{
  72644. + return pipe->dev_addr;
  72645. +}
  72646. +
  72647. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  72648. + *pipe)
  72649. +{
  72650. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  72651. +}
  72652. +
  72653. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  72654. + *pipe)
  72655. +{
  72656. + return (pipe->pipe_type == UE_INTERRUPT);
  72657. +}
  72658. +
  72659. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  72660. + *pipe)
  72661. +{
  72662. + return (pipe->pipe_type == UE_BULK);
  72663. +}
  72664. +
  72665. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  72666. + *pipe)
  72667. +{
  72668. + return (pipe->pipe_type == UE_CONTROL);
  72669. +}
  72670. +
  72671. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  72672. +{
  72673. + return (pipe->pipe_dir == UE_DIR_IN);
  72674. +}
  72675. +
  72676. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  72677. + *pipe)
  72678. +{
  72679. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  72680. +}
  72681. +
  72682. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  72683. + uint8_t devaddr, uint8_t ep_num,
  72684. + uint8_t pipe_type, uint8_t pipe_dir,
  72685. + uint16_t mps)
  72686. +{
  72687. + pipe->dev_addr = devaddr;
  72688. + pipe->ep_num = ep_num;
  72689. + pipe->pipe_type = pipe_type;
  72690. + pipe->pipe_dir = pipe_dir;
  72691. + pipe->mps = mps;
  72692. +}
  72693. +
  72694. +/**
  72695. + * Phases for control transfers.
  72696. + */
  72697. +typedef enum dwc_otg_control_phase {
  72698. + DWC_OTG_CONTROL_SETUP,
  72699. + DWC_OTG_CONTROL_DATA,
  72700. + DWC_OTG_CONTROL_STATUS
  72701. +} dwc_otg_control_phase_e;
  72702. +
  72703. +/** Transaction types. */
  72704. +typedef enum dwc_otg_transaction_type {
  72705. + DWC_OTG_TRANSACTION_NONE = 0,
  72706. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  72707. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  72708. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  72709. +} dwc_otg_transaction_type_e;
  72710. +
  72711. +struct dwc_otg_qh;
  72712. +
  72713. +/**
  72714. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  72715. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  72716. + * (of one of these types) submitted to the HCD. The transfer associated with
  72717. + * a QTD may require one or multiple transactions.
  72718. + *
  72719. + * A QTD is linked to a Queue Head, which is entered in either the
  72720. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  72721. + * execution, some or all of its transactions may be executed. After
  72722. + * execution, the state of the QTD is updated. The QTD may be retired if all
  72723. + * its transactions are complete or if an error occurred. Otherwise, it
  72724. + * remains in the schedule so more transactions can be executed later.
  72725. + */
  72726. +typedef struct dwc_otg_qtd {
  72727. + /**
  72728. + * Determines the PID of the next data packet for the data phase of
  72729. + * control transfers. Ignored for other transfer types.<br>
  72730. + * One of the following values:
  72731. + * - DWC_OTG_HC_PID_DATA0
  72732. + * - DWC_OTG_HC_PID_DATA1
  72733. + */
  72734. + uint8_t data_toggle;
  72735. +
  72736. + /** Current phase for control transfers (Setup, Data, or Status). */
  72737. + dwc_otg_control_phase_e control_phase;
  72738. +
  72739. + /** Keep track of the current split type
  72740. + * for FS/LS endpoints on a HS Hub */
  72741. + uint8_t complete_split;
  72742. +
  72743. + /** How many bytes transferred during SSPLIT OUT */
  72744. + uint32_t ssplit_out_xfer_count;
  72745. +
  72746. + /**
  72747. + * Holds the number of bus errors that have occurred for a transaction
  72748. + * within this transfer.
  72749. + */
  72750. + uint8_t error_count;
  72751. +
  72752. + /**
  72753. + * Index of the next frame descriptor for an isochronous transfer. A
  72754. + * frame descriptor describes the buffer position and length of the
  72755. + * data to be transferred in the next scheduled (micro)frame of an
  72756. + * isochronous transfer. It also holds status for that transaction.
  72757. + * The frame index starts at 0.
  72758. + */
  72759. + uint16_t isoc_frame_index;
  72760. +
  72761. + /** Position of the ISOC split on full/low speed */
  72762. + uint8_t isoc_split_pos;
  72763. +
  72764. + /** Position of the ISOC split in the buffer for the current frame */
  72765. + uint16_t isoc_split_offset;
  72766. +
  72767. + /** URB for this transfer */
  72768. + struct dwc_otg_hcd_urb *urb;
  72769. +
  72770. + struct dwc_otg_qh *qh;
  72771. +
  72772. + /** This list of QTDs */
  72773. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  72774. +
  72775. + /** Indicates if this QTD is currently processed by HW. */
  72776. + uint8_t in_process;
  72777. +
  72778. + /** Number of DMA descriptors for this QTD */
  72779. + uint8_t n_desc;
  72780. +
  72781. + /**
  72782. + * Last activated frame(packet) index.
  72783. + * Used in Descriptor DMA mode only.
  72784. + */
  72785. + uint16_t isoc_frame_index_last;
  72786. +
  72787. +} dwc_otg_qtd_t;
  72788. +
  72789. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  72790. +
  72791. +/**
  72792. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  72793. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  72794. + * be entered in either the non-periodic or periodic schedule.
  72795. + */
  72796. +typedef struct dwc_otg_qh {
  72797. + /**
  72798. + * Endpoint type.
  72799. + * One of the following values:
  72800. + * - UE_CONTROL
  72801. + * - UE_BULK
  72802. + * - UE_INTERRUPT
  72803. + * - UE_ISOCHRONOUS
  72804. + */
  72805. + uint8_t ep_type;
  72806. + uint8_t ep_is_in;
  72807. +
  72808. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  72809. + uint16_t maxp;
  72810. +
  72811. + /**
  72812. + * Device speed.
  72813. + * One of the following values:
  72814. + * - DWC_OTG_EP_SPEED_LOW
  72815. + * - DWC_OTG_EP_SPEED_FULL
  72816. + * - DWC_OTG_EP_SPEED_HIGH
  72817. + */
  72818. + uint8_t dev_speed;
  72819. +
  72820. + /**
  72821. + * Determines the PID of the next data packet for non-control
  72822. + * transfers. Ignored for control transfers.<br>
  72823. + * One of the following values:
  72824. + * - DWC_OTG_HC_PID_DATA0
  72825. + * - DWC_OTG_HC_PID_DATA1
  72826. + */
  72827. + uint8_t data_toggle;
  72828. +
  72829. + /** Ping state if 1. */
  72830. + uint8_t ping_state;
  72831. +
  72832. + /**
  72833. + * List of QTDs for this QH.
  72834. + */
  72835. + struct dwc_otg_qtd_list qtd_list;
  72836. +
  72837. + /** Host channel currently processing transfers for this QH. */
  72838. + struct dwc_hc *channel;
  72839. +
  72840. + /** Full/low speed endpoint on high-speed hub requires split. */
  72841. + uint8_t do_split;
  72842. +
  72843. + /** @name Periodic schedule information */
  72844. + /** @{ */
  72845. +
  72846. + /** Bandwidth in microseconds per (micro)frame. */
  72847. + uint16_t usecs;
  72848. +
  72849. + /** Interval between transfers in (micro)frames. */
  72850. + uint16_t interval;
  72851. +
  72852. + /**
  72853. + * (micro)frame to initialize a periodic transfer. The transfer
  72854. + * executes in the following (micro)frame.
  72855. + */
  72856. + uint16_t sched_frame;
  72857. +
  72858. + /*
  72859. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  72860. + */
  72861. + uint16_t nak_frame;
  72862. +
  72863. + /** (micro)frame at which last start split was initialized. */
  72864. + uint16_t start_split_frame;
  72865. +
  72866. + /** @} */
  72867. +
  72868. + /**
  72869. + * Used instead of original buffer if
  72870. + * it(physical address) is not dword-aligned.
  72871. + */
  72872. + uint8_t *dw_align_buf;
  72873. + dwc_dma_t dw_align_buf_dma;
  72874. +
  72875. + /** Entry for QH in either the periodic or non-periodic schedule. */
  72876. + dwc_list_link_t qh_list_entry;
  72877. +
  72878. + /** @name Descriptor DMA support */
  72879. + /** @{ */
  72880. +
  72881. + /** Descriptor List. */
  72882. + dwc_otg_host_dma_desc_t *desc_list;
  72883. +
  72884. + /** Descriptor List physical address. */
  72885. + dwc_dma_t desc_list_dma;
  72886. +
  72887. + /**
  72888. + * Xfer Bytes array.
  72889. + * Each element corresponds to a descriptor and indicates
  72890. + * original XferSize size value for the descriptor.
  72891. + */
  72892. + uint32_t *n_bytes;
  72893. +
  72894. + /** Actual number of transfer descriptors in a list. */
  72895. + uint16_t ntd;
  72896. +
  72897. + /** First activated isochronous transfer descriptor index. */
  72898. + uint8_t td_first;
  72899. + /** Last activated isochronous transfer descriptor index. */
  72900. + uint8_t td_last;
  72901. +
  72902. + /** @} */
  72903. +
  72904. +
  72905. + uint16_t speed;
  72906. + uint16_t frame_usecs[8];
  72907. +
  72908. + uint32_t skip_count;
  72909. +} dwc_otg_qh_t;
  72910. +
  72911. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  72912. +
  72913. +typedef struct urb_tq_entry {
  72914. + struct urb *urb;
  72915. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  72916. +} urb_tq_entry_t;
  72917. +
  72918. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  72919. +
  72920. +/**
  72921. + * This structure holds the state of the HCD, including the non-periodic and
  72922. + * periodic schedules.
  72923. + */
  72924. +struct dwc_otg_hcd {
  72925. + /** The DWC otg device pointer */
  72926. + struct dwc_otg_device *otg_dev;
  72927. + /** DWC OTG Core Interface Layer */
  72928. + dwc_otg_core_if_t *core_if;
  72929. +
  72930. + /** Function HCD driver callbacks */
  72931. + struct dwc_otg_hcd_function_ops *fops;
  72932. +
  72933. + /** Internal DWC HCD Flags */
  72934. + volatile union dwc_otg_hcd_internal_flags {
  72935. + uint32_t d32;
  72936. + struct {
  72937. + unsigned port_connect_status_change:1;
  72938. + unsigned port_connect_status:1;
  72939. + unsigned port_reset_change:1;
  72940. + unsigned port_enable_change:1;
  72941. + unsigned port_suspend_change:1;
  72942. + unsigned port_over_current_change:1;
  72943. + unsigned port_l1_change:1;
  72944. + unsigned reserved:26;
  72945. + } b;
  72946. + } flags;
  72947. +
  72948. + /**
  72949. + * Inactive items in the non-periodic schedule. This is a list of
  72950. + * Queue Heads. Transfers associated with these Queue Heads are not
  72951. + * currently assigned to a host channel.
  72952. + */
  72953. + dwc_list_link_t non_periodic_sched_inactive;
  72954. +
  72955. + /**
  72956. + * Active items in the non-periodic schedule. This is a list of
  72957. + * Queue Heads. Transfers associated with these Queue Heads are
  72958. + * currently assigned to a host channel.
  72959. + */
  72960. + dwc_list_link_t non_periodic_sched_active;
  72961. +
  72962. + /**
  72963. + * Pointer to the next Queue Head to process in the active
  72964. + * non-periodic schedule.
  72965. + */
  72966. + dwc_list_link_t *non_periodic_qh_ptr;
  72967. +
  72968. + /**
  72969. + * Inactive items in the periodic schedule. This is a list of QHs for
  72970. + * periodic transfers that are _not_ scheduled for the next frame.
  72971. + * Each QH in the list has an interval counter that determines when it
  72972. + * needs to be scheduled for execution. This scheduling mechanism
  72973. + * allows only a simple calculation for periodic bandwidth used (i.e.
  72974. + * must assume that all periodic transfers may need to execute in the
  72975. + * same frame). However, it greatly simplifies scheduling and should
  72976. + * be sufficient for the vast majority of OTG hosts, which need to
  72977. + * connect to a small number of peripherals at one time.
  72978. + *
  72979. + * Items move from this list to periodic_sched_ready when the QH
  72980. + * interval counter is 0 at SOF.
  72981. + */
  72982. + dwc_list_link_t periodic_sched_inactive;
  72983. +
  72984. + /**
  72985. + * List of periodic QHs that are ready for execution in the next
  72986. + * frame, but have not yet been assigned to host channels.
  72987. + *
  72988. + * Items move from this list to periodic_sched_assigned as host
  72989. + * channels become available during the current frame.
  72990. + */
  72991. + dwc_list_link_t periodic_sched_ready;
  72992. +
  72993. + /**
  72994. + * List of periodic QHs to be executed in the next frame that are
  72995. + * assigned to host channels.
  72996. + *
  72997. + * Items move from this list to periodic_sched_queued as the
  72998. + * transactions for the QH are queued to the DWC_otg controller.
  72999. + */
  73000. + dwc_list_link_t periodic_sched_assigned;
  73001. +
  73002. + /**
  73003. + * List of periodic QHs that have been queued for execution.
  73004. + *
  73005. + * Items move from this list to either periodic_sched_inactive or
  73006. + * periodic_sched_ready when the channel associated with the transfer
  73007. + * is released. If the interval for the QH is 1, the item moves to
  73008. + * periodic_sched_ready because it must be rescheduled for the next
  73009. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  73010. + */
  73011. + dwc_list_link_t periodic_sched_queued;
  73012. +
  73013. + /**
  73014. + * Total bandwidth claimed so far for periodic transfers. This value
  73015. + * is in microseconds per (micro)frame. The assumption is that all
  73016. + * periodic transfers may occur in the same (micro)frame.
  73017. + */
  73018. + uint16_t periodic_usecs;
  73019. +
  73020. + /**
  73021. + * Total bandwidth claimed so far for all periodic transfers
  73022. + * in a frame.
  73023. + * This will include a mixture of HS and FS transfers.
  73024. + * Units are microseconds per (micro)frame.
  73025. + * We have a budget per frame and have to schedule
  73026. + * transactions accordingly.
  73027. + * Watch out for the fact that things are actually scheduled for the
  73028. + * "next frame".
  73029. + */
  73030. + uint16_t frame_usecs[8];
  73031. +
  73032. +
  73033. + /**
  73034. + * Frame number read from the core at SOF. The value ranges from 0 to
  73035. + * DWC_HFNUM_MAX_FRNUM.
  73036. + */
  73037. + uint16_t frame_number;
  73038. +
  73039. + /**
  73040. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  73041. + */
  73042. + uint16_t periodic_qh_count;
  73043. +
  73044. + /**
  73045. + * Free host channels in the controller. This is a list of
  73046. + * dwc_hc_t items.
  73047. + */
  73048. + struct hc_list free_hc_list;
  73049. + /**
  73050. + * Number of host channels assigned to periodic transfers. Currently
  73051. + * assuming that there is a dedicated host channel for each periodic
  73052. + * transaction and at least one host channel available for
  73053. + * non-periodic transactions.
  73054. + */
  73055. + int periodic_channels; /* microframe_schedule==0 */
  73056. +
  73057. + /**
  73058. + * Number of host channels assigned to non-periodic transfers.
  73059. + */
  73060. + int non_periodic_channels; /* microframe_schedule==0 */
  73061. +
  73062. + /**
  73063. + * Number of host channels assigned to non-periodic transfers.
  73064. + */
  73065. + int available_host_channels;
  73066. +
  73067. + /**
  73068. + * Array of pointers to the host channel descriptors. Allows accessing
  73069. + * a host channel descriptor given the host channel number. This is
  73070. + * useful in interrupt handlers.
  73071. + */
  73072. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  73073. +
  73074. + /**
  73075. + * Buffer to use for any data received during the status phase of a
  73076. + * control transfer. Normally no data is transferred during the status
  73077. + * phase. This buffer is used as a bit bucket.
  73078. + */
  73079. + uint8_t *status_buf;
  73080. +
  73081. + /**
  73082. + * DMA address for status_buf.
  73083. + */
  73084. + dma_addr_t status_buf_dma;
  73085. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  73086. +
  73087. + /**
  73088. + * Connection timer. An OTG host must display a message if the device
  73089. + * does not connect. Started when the VBus power is turned on via
  73090. + * sysfs attribute "buspower".
  73091. + */
  73092. + dwc_timer_t *conn_timer;
  73093. +
  73094. + /* Tasket to do a reset */
  73095. + dwc_tasklet_t *reset_tasklet;
  73096. +
  73097. + dwc_tasklet_t *completion_tasklet;
  73098. + struct urb_list completed_urb_list;
  73099. +
  73100. + /* */
  73101. + dwc_spinlock_t *lock;
  73102. + dwc_spinlock_t *channel_lock;
  73103. + /**
  73104. + * Private data that could be used by OS wrapper.
  73105. + */
  73106. + void *priv;
  73107. +
  73108. + uint8_t otg_port;
  73109. +
  73110. + /** Frame List */
  73111. + uint32_t *frame_list;
  73112. +
  73113. + /** Hub - Port assignment */
  73114. + int hub_port[128];
  73115. +#ifdef FIQ_DEBUG
  73116. + int hub_port_alloc[2048];
  73117. +#endif
  73118. +
  73119. + /** Frame List DMA address */
  73120. + dma_addr_t frame_list_dma;
  73121. +
  73122. +#ifdef DEBUG
  73123. + uint32_t frrem_samples;
  73124. + uint64_t frrem_accum;
  73125. +
  73126. + uint32_t hfnum_7_samples_a;
  73127. + uint64_t hfnum_7_frrem_accum_a;
  73128. + uint32_t hfnum_0_samples_a;
  73129. + uint64_t hfnum_0_frrem_accum_a;
  73130. + uint32_t hfnum_other_samples_a;
  73131. + uint64_t hfnum_other_frrem_accum_a;
  73132. +
  73133. + uint32_t hfnum_7_samples_b;
  73134. + uint64_t hfnum_7_frrem_accum_b;
  73135. + uint32_t hfnum_0_samples_b;
  73136. + uint64_t hfnum_0_frrem_accum_b;
  73137. + uint32_t hfnum_other_samples_b;
  73138. + uint64_t hfnum_other_frrem_accum_b;
  73139. +#endif
  73140. +};
  73141. +
  73142. +/** @name Transaction Execution Functions */
  73143. +/** @{ */
  73144. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  73145. + * hcd);
  73146. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  73147. + dwc_otg_transaction_type_e tr_type);
  73148. +
  73149. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  73150. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  73151. +
  73152. +
  73153. +/** @} */
  73154. +
  73155. +/** @name Interrupt Handler Functions */
  73156. +/** @{ */
  73157. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73158. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73159. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  73160. + dwc_otg_hcd);
  73161. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  73162. + dwc_otg_hcd);
  73163. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  73164. + dwc_otg_hcd);
  73165. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  73166. + dwc_otg_hcd);
  73167. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73168. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  73169. + dwc_otg_hcd);
  73170. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73171. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73172. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  73173. + uint32_t num);
  73174. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73175. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  73176. + dwc_otg_hcd);
  73177. +/** @} */
  73178. +
  73179. +/** @name Schedule Queue Functions */
  73180. +/** @{ */
  73181. +
  73182. +/* Implemented in dwc_otg_hcd_queue.c */
  73183. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  73184. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  73185. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73186. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73187. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73188. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  73189. + int sched_csplit);
  73190. +
  73191. +/** Remove and free a QH */
  73192. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  73193. + dwc_otg_qh_t * qh)
  73194. +{
  73195. + dwc_irqflags_t flags;
  73196. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  73197. + dwc_otg_hcd_qh_remove(hcd, qh);
  73198. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  73199. + dwc_otg_hcd_qh_free(hcd, qh);
  73200. +}
  73201. +
  73202. +/** Allocates memory for a QH structure.
  73203. + * @return Returns the memory allocate or NULL on error. */
  73204. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  73205. +{
  73206. + if (atomic_alloc)
  73207. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  73208. + else
  73209. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  73210. +}
  73211. +
  73212. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  73213. + int atomic_alloc);
  73214. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  73215. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  73216. + dwc_otg_qh_t ** qh, int atomic_alloc);
  73217. +
  73218. +/** Allocates memory for a QTD structure.
  73219. + * @return Returns the memory allocate or NULL on error. */
  73220. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  73221. +{
  73222. + if (atomic_alloc)
  73223. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  73224. + else
  73225. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  73226. +}
  73227. +
  73228. +/** Frees the memory for a QTD structure. QTD should already be removed from
  73229. + * list.
  73230. + * @param qtd QTD to free.*/
  73231. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  73232. +{
  73233. + DWC_FREE(qtd);
  73234. +}
  73235. +
  73236. +/** Removes a QTD from list.
  73237. + * @param hcd HCD instance.
  73238. + * @param qtd QTD to remove from list.
  73239. + * @param qh QTD belongs to.
  73240. + */
  73241. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  73242. + dwc_otg_qtd_t * qtd,
  73243. + dwc_otg_qh_t * qh)
  73244. +{
  73245. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  73246. +}
  73247. +
  73248. +/** Remove and free a QTD
  73249. + * Need to disable IRQ and hold hcd lock while calling this function out of
  73250. + * interrupt servicing chain */
  73251. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  73252. + dwc_otg_qtd_t * qtd,
  73253. + dwc_otg_qh_t * qh)
  73254. +{
  73255. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  73256. + dwc_otg_hcd_qtd_free(qtd);
  73257. +}
  73258. +
  73259. +/** @} */
  73260. +
  73261. +/** @name Descriptor DMA Supporting Functions */
  73262. +/** @{ */
  73263. +
  73264. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73265. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  73266. + dwc_hc_t * hc,
  73267. + dwc_otg_hc_regs_t * hc_regs,
  73268. + dwc_otg_halt_status_e halt_status);
  73269. +
  73270. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73271. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  73272. +
  73273. +/** @} */
  73274. +
  73275. +/** @name Internal Functions */
  73276. +/** @{ */
  73277. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  73278. +/** @} */
  73279. +
  73280. +#ifdef CONFIG_USB_DWC_OTG_LPM
  73281. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  73282. + uint8_t devaddr);
  73283. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  73284. +#endif
  73285. +
  73286. +/** Gets the QH that contains the list_head */
  73287. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  73288. +
  73289. +/** Gets the QTD that contains the list_head */
  73290. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  73291. +
  73292. +/** Check if QH is non-periodic */
  73293. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  73294. + (_qh_ptr_->ep_type == UE_CONTROL))
  73295. +
  73296. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  73297. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  73298. +
  73299. +/** Packet size for any kind of endpoint descriptor */
  73300. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  73301. +
  73302. +/**
  73303. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  73304. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  73305. + * frame number when the max frame number is reached.
  73306. + */
  73307. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  73308. +{
  73309. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  73310. + (DWC_HFNUM_MAX_FRNUM >> 1);
  73311. +}
  73312. +
  73313. +/**
  73314. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  73315. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  73316. + * number when the max frame number is reached.
  73317. + */
  73318. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  73319. +{
  73320. + return (frame1 != frame2) &&
  73321. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  73322. + (DWC_HFNUM_MAX_FRNUM >> 1));
  73323. +}
  73324. +
  73325. +/**
  73326. + * Increments _frame by the amount specified by _inc. The addition is done
  73327. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  73328. + */
  73329. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  73330. +{
  73331. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  73332. +}
  73333. +
  73334. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  73335. +{
  73336. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  73337. +}
  73338. +
  73339. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  73340. +{
  73341. + return frame & 0x7;
  73342. +}
  73343. +
  73344. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  73345. + dwc_otg_hc_regs_t * hc_regs,
  73346. + dwc_otg_qtd_t * qtd);
  73347. +
  73348. +#ifdef DEBUG
  73349. +/**
  73350. + * Macro to sample the remaining PHY clocks left in the current frame. This
  73351. + * may be used during debugging to determine the average time it takes to
  73352. + * execute sections of code. There are two possible sample points, "a" and
  73353. + * "b", so the _letter argument must be one of these values.
  73354. + *
  73355. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  73356. + * example, "cat /sys/devices/lm0/hcd_frrem".
  73357. + */
  73358. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  73359. +{ \
  73360. + hfnum_data_t hfnum; \
  73361. + dwc_otg_qtd_t *qtd; \
  73362. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  73363. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  73364. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  73365. + switch (hfnum.b.frnum & 0x7) { \
  73366. + case 7: \
  73367. + _hcd->hfnum_7_samples_##_letter++; \
  73368. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  73369. + break; \
  73370. + case 0: \
  73371. + _hcd->hfnum_0_samples_##_letter++; \
  73372. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  73373. + break; \
  73374. + default: \
  73375. + _hcd->hfnum_other_samples_##_letter++; \
  73376. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  73377. + break; \
  73378. + } \
  73379. + } \
  73380. +}
  73381. +#else
  73382. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  73383. +#endif
  73384. +#endif
  73385. +#endif /* DWC_DEVICE_ONLY */
  73386. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  73387. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 1970-01-01 01:00:00.000000000 +0100
  73388. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 2014-03-11 16:55:38.000000000 +0100
  73389. @@ -0,0 +1,417 @@
  73390. +/* ==========================================================================
  73391. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  73392. + * $Revision: #12 $
  73393. + * $Date: 2011/10/26 $
  73394. + * $Change: 1873028 $
  73395. + *
  73396. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  73397. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  73398. + * otherwise expressly agreed to in writing between Synopsys and you.
  73399. + *
  73400. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  73401. + * any End User Software License Agreement or Agreement for Licensed Product
  73402. + * with Synopsys or any supplement thereto. You are permitted to use and
  73403. + * redistribute this Software in source and binary forms, with or without
  73404. + * modification, provided that redistributions of source code must retain this
  73405. + * notice. You may not view, use, disclose, copy or distribute this file or
  73406. + * any information contained herein except pursuant to this license grant from
  73407. + * Synopsys. If you do not agree with this notice, including the disclaimer
  73408. + * below, then you are not authorized to use the Software.
  73409. + *
  73410. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  73411. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  73412. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  73413. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  73414. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  73415. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73416. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  73417. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73418. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  73419. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  73420. + * DAMAGE.
  73421. + * ========================================================================== */
  73422. +#ifndef DWC_DEVICE_ONLY
  73423. +#ifndef __DWC_HCD_IF_H__
  73424. +#define __DWC_HCD_IF_H__
  73425. +
  73426. +#include "dwc_otg_core_if.h"
  73427. +
  73428. +/** @file
  73429. + * This file defines DWC_OTG HCD Core API.
  73430. + */
  73431. +
  73432. +struct dwc_otg_hcd;
  73433. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  73434. +
  73435. +struct dwc_otg_hcd_urb;
  73436. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  73437. +
  73438. +/** @name HCD Function Driver Callbacks */
  73439. +/** @{ */
  73440. +
  73441. +/** This function is called whenever core switches to host mode. */
  73442. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  73443. +
  73444. +/** This function is called when device has been disconnected */
  73445. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  73446. +
  73447. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  73448. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  73449. + void *urb_handle,
  73450. + uint32_t * hub_addr,
  73451. + uint32_t * port_addr);
  73452. +/** Via this function HCD core gets device speed */
  73453. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  73454. + void *urb_handle);
  73455. +
  73456. +/** This function is called when urb is completed */
  73457. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  73458. + void *urb_handle,
  73459. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  73460. + int32_t status);
  73461. +
  73462. +/** Via this function HCD core gets b_hnp_enable parameter */
  73463. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  73464. +
  73465. +struct dwc_otg_hcd_function_ops {
  73466. + dwc_otg_hcd_start_cb_t start;
  73467. + dwc_otg_hcd_disconnect_cb_t disconnect;
  73468. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  73469. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  73470. + dwc_otg_hcd_complete_urb_cb_t complete;
  73471. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  73472. +};
  73473. +/** @} */
  73474. +
  73475. +/** @name HCD Core API */
  73476. +/** @{ */
  73477. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  73478. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  73479. +
  73480. +/** This function should be called to initiate HCD Core.
  73481. + *
  73482. + * @param hcd The HCD
  73483. + * @param core_if The DWC_OTG Core
  73484. + *
  73485. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  73486. + * Returns 0 on success
  73487. + */
  73488. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  73489. +
  73490. +/** Frees HCD
  73491. + *
  73492. + * @param hcd The HCD
  73493. + */
  73494. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  73495. +
  73496. +/** This function should be called on every hardware interrupt.
  73497. + *
  73498. + * @param dwc_otg_hcd The HCD
  73499. + *
  73500. + * Returns non zero if interrupt is handled
  73501. + * Return 0 if interrupt is not handled
  73502. + */
  73503. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  73504. +
  73505. +/** This function is used to handle the fast interrupt
  73506. + *
  73507. + */
  73508. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  73509. +
  73510. +/**
  73511. + * Returns private data set by
  73512. + * dwc_otg_hcd_set_priv_data function.
  73513. + *
  73514. + * @param hcd The HCD
  73515. + */
  73516. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  73517. +
  73518. +/**
  73519. + * Set private data.
  73520. + *
  73521. + * @param hcd The HCD
  73522. + * @param priv_data pointer to be stored in private data
  73523. + */
  73524. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  73525. +
  73526. +/**
  73527. + * This function initializes the HCD Core.
  73528. + *
  73529. + * @param hcd The HCD
  73530. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  73531. + *
  73532. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  73533. + * Returns 0 on success
  73534. + */
  73535. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  73536. + struct dwc_otg_hcd_function_ops *fops);
  73537. +
  73538. +/**
  73539. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  73540. + * stopped.
  73541. + *
  73542. + * @param hcd The HCD
  73543. + */
  73544. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  73545. +
  73546. +/**
  73547. + * Handles hub class-specific requests.
  73548. + *
  73549. + * @param dwc_otg_hcd The HCD
  73550. + * @param typeReq Request Type
  73551. + * @param wValue wValue from control request
  73552. + * @param wIndex wIndex from control request
  73553. + * @param buf data buffer
  73554. + * @param wLength data buffer length
  73555. + *
  73556. + * Returns -DWC_E_INVALID if invalid argument is passed
  73557. + * Returns 0 on success
  73558. + */
  73559. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  73560. + uint16_t typeReq, uint16_t wValue,
  73561. + uint16_t wIndex, uint8_t * buf,
  73562. + uint16_t wLength);
  73563. +
  73564. +/**
  73565. + * Returns otg port number.
  73566. + *
  73567. + * @param hcd The HCD
  73568. + */
  73569. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  73570. +
  73571. +/**
  73572. + * Returns OTG version - either 1.3 or 2.0.
  73573. + *
  73574. + * @param core_if The core_if structure pointer
  73575. + */
  73576. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  73577. +
  73578. +/**
  73579. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  73580. + *
  73581. + * @param hcd The HCD
  73582. + */
  73583. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  73584. +
  73585. +/**
  73586. + * Returns current frame number.
  73587. + *
  73588. + * @param hcd The HCD
  73589. + */
  73590. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  73591. +
  73592. +/**
  73593. + * Dumps hcd state.
  73594. + *
  73595. + * @param hcd The HCD
  73596. + */
  73597. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  73598. +
  73599. +/**
  73600. + * Dump the average frame remaining at SOF. This can be used to
  73601. + * determine average interrupt latency. Frame remaining is also shown for
  73602. + * start transfer and two additional sample points.
  73603. + * Currently this function is not implemented.
  73604. + *
  73605. + * @param hcd The HCD
  73606. + */
  73607. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  73608. +
  73609. +/**
  73610. + * Sends LPM transaction to the local device.
  73611. + *
  73612. + * @param hcd The HCD
  73613. + * @param devaddr Device Address
  73614. + * @param hird Host initiated resume duration
  73615. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  73616. + *
  73617. + * Returns negative value if sending LPM transaction was not succeeded.
  73618. + * Returns 0 on success.
  73619. + */
  73620. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  73621. + uint8_t hird, uint8_t bRemoteWake);
  73622. +
  73623. +/* URB interface */
  73624. +
  73625. +/**
  73626. + * Allocates memory for dwc_otg_hcd_urb structure.
  73627. + * Allocated memory should be freed by call of DWC_FREE.
  73628. + *
  73629. + * @param hcd The HCD
  73630. + * @param iso_desc_count Count of ISOC descriptors
  73631. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  73632. + */
  73633. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  73634. + int iso_desc_count,
  73635. + int atomic_alloc);
  73636. +
  73637. +/**
  73638. + * Set pipe information in URB.
  73639. + *
  73640. + * @param hcd_urb DWC_OTG URB
  73641. + * @param devaddr Device Address
  73642. + * @param ep_num Endpoint Number
  73643. + * @param ep_type Endpoint Type
  73644. + * @param ep_dir Endpoint Direction
  73645. + * @param mps Max Packet Size
  73646. + */
  73647. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  73648. + uint8_t devaddr, uint8_t ep_num,
  73649. + uint8_t ep_type, uint8_t ep_dir,
  73650. + uint16_t mps);
  73651. +
  73652. +/* Transfer flags */
  73653. +#define URB_GIVEBACK_ASAP 0x1
  73654. +#define URB_SEND_ZERO_PACKET 0x2
  73655. +
  73656. +/**
  73657. + * Sets dwc_otg_hcd_urb parameters.
  73658. + *
  73659. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  73660. + * @param urb_handle Unique handle for request, this will be passed back
  73661. + * to function driver in completion callback.
  73662. + * @param buf The buffer for the data
  73663. + * @param dma The DMA buffer for the data
  73664. + * @param buflen Transfer length
  73665. + * @param sp Buffer for setup data
  73666. + * @param sp_dma DMA address of setup data buffer
  73667. + * @param flags Transfer flags
  73668. + * @param interval Polling interval for interrupt or isochronous transfers.
  73669. + */
  73670. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  73671. + void *urb_handle, void *buf,
  73672. + dwc_dma_t dma, uint32_t buflen, void *sp,
  73673. + dwc_dma_t sp_dma, uint32_t flags,
  73674. + uint16_t interval);
  73675. +
  73676. +/** Gets status from dwc_otg_hcd_urb
  73677. + *
  73678. + * @param dwc_otg_urb DWC_OTG URB
  73679. + */
  73680. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  73681. +
  73682. +/** Gets actual length from dwc_otg_hcd_urb
  73683. + *
  73684. + * @param dwc_otg_urb DWC_OTG URB
  73685. + */
  73686. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  73687. + dwc_otg_urb);
  73688. +
  73689. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  73690. + *
  73691. + * @param dwc_otg_urb DWC_OTG URB
  73692. + */
  73693. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  73694. + dwc_otg_urb);
  73695. +
  73696. +/** Set ISOC descriptor offset and length
  73697. + *
  73698. + * @param dwc_otg_urb DWC_OTG URB
  73699. + * @param desc_num ISOC descriptor number
  73700. + * @param offset Offset from beginig of buffer.
  73701. + * @param length Transaction length
  73702. + */
  73703. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  73704. + int desc_num, uint32_t offset,
  73705. + uint32_t length);
  73706. +
  73707. +/** Get status of ISOC descriptor, specified by desc_num
  73708. + *
  73709. + * @param dwc_otg_urb DWC_OTG URB
  73710. + * @param desc_num ISOC descriptor number
  73711. + */
  73712. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  73713. + dwc_otg_urb, int desc_num);
  73714. +
  73715. +/** Get actual length of ISOC descriptor, specified by desc_num
  73716. + *
  73717. + * @param dwc_otg_urb DWC_OTG URB
  73718. + * @param desc_num ISOC descriptor number
  73719. + */
  73720. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  73721. + dwc_otg_urb,
  73722. + int desc_num);
  73723. +
  73724. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  73725. + *
  73726. + * @param dwc_otg_hcd The HCD
  73727. + * @param dwc_otg_urb DWC_OTG URB
  73728. + * @param ep_handle Out parameter for returning endpoint handle
  73729. + * @param atomic_alloc Flag to do atomic allocation if needed
  73730. + *
  73731. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  73732. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  73733. + * Returns 0 on success.
  73734. + */
  73735. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  73736. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  73737. + void **ep_handle, int atomic_alloc);
  73738. +
  73739. +/** De-queue the specified URB
  73740. + *
  73741. + * @param dwc_otg_hcd The HCD
  73742. + * @param dwc_otg_urb DWC_OTG URB
  73743. + */
  73744. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  73745. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  73746. +
  73747. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  73748. + * Any URBs for the endpoint must already be dequeued.
  73749. + *
  73750. + * @param hcd The HCD
  73751. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  73752. + * @param retry Number of retries if there are queued transfers.
  73753. + *
  73754. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  73755. + * Returns 0 on success
  73756. + */
  73757. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  73758. + int retry);
  73759. +
  73760. +/* Resets the data toggle in qh structure. This function can be called from
  73761. + * usb_clear_halt routine.
  73762. + *
  73763. + * @param hcd The HCD
  73764. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  73765. + *
  73766. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  73767. + * Returns 0 on success
  73768. + */
  73769. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  73770. +
  73771. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  73772. + *
  73773. + * @param hcd The HCD
  73774. + * @param port Port number
  73775. + */
  73776. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  73777. +
  73778. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  73779. + * Only for ISOC and INTERRUPT endpoints.
  73780. + *
  73781. + * @param hcd The HCD
  73782. + * @param ep_handle Endpoint handle
  73783. + */
  73784. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  73785. + void *ep_handle);
  73786. +
  73787. +/** Call this function to check if bandwidth was freed for specified endpoint.
  73788. + *
  73789. + * @param hcd The HCD
  73790. + * @param ep_handle Endpoint handle
  73791. + */
  73792. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  73793. +
  73794. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  73795. + * Only for ISOC and INTERRUPT endpoints.
  73796. + *
  73797. + * @param hcd The HCD
  73798. + * @param ep_handle Endpoint handle
  73799. + */
  73800. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  73801. + void *ep_handle);
  73802. +
  73803. +/** @} */
  73804. +
  73805. +#endif /* __DWC_HCD_IF_H__ */
  73806. +#endif /* DWC_DEVICE_ONLY */
  73807. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  73808. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 1970-01-01 01:00:00.000000000 +0100
  73809. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 2014-03-11 16:55:38.000000000 +0100
  73810. @@ -0,0 +1,2741 @@
  73811. +/* ==========================================================================
  73812. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  73813. + * $Revision: #89 $
  73814. + * $Date: 2011/10/20 $
  73815. + * $Change: 1869487 $
  73816. + *
  73817. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  73818. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  73819. + * otherwise expressly agreed to in writing between Synopsys and you.
  73820. + *
  73821. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  73822. + * any End User Software License Agreement or Agreement for Licensed Product
  73823. + * with Synopsys or any supplement thereto. You are permitted to use and
  73824. + * redistribute this Software in source and binary forms, with or without
  73825. + * modification, provided that redistributions of source code must retain this
  73826. + * notice. You may not view, use, disclose, copy or distribute this file or
  73827. + * any information contained herein except pursuant to this license grant from
  73828. + * Synopsys. If you do not agree with this notice, including the disclaimer
  73829. + * below, then you are not authorized to use the Software.
  73830. + *
  73831. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  73832. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  73833. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  73834. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  73835. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  73836. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73837. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  73838. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73839. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  73840. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  73841. + * DAMAGE.
  73842. + * ========================================================================== */
  73843. +#ifndef DWC_DEVICE_ONLY
  73844. +
  73845. +#include "dwc_otg_hcd.h"
  73846. +#include "dwc_otg_regs.h"
  73847. +#include "dwc_otg_mphi_fix.h"
  73848. +
  73849. +#include <linux/jiffies.h>
  73850. +#include <mach/hardware.h>
  73851. +#include <asm/fiq.h>
  73852. +
  73853. +
  73854. +extern bool microframe_schedule;
  73855. +
  73856. +/** @file
  73857. + * This file contains the implementation of the HCD Interrupt handlers.
  73858. + */
  73859. +
  73860. +/*
  73861. + * Some globals to communicate between the FIQ and INTERRUPT
  73862. + */
  73863. +
  73864. +void * dummy_send;
  73865. +mphi_regs_t c_mphi_regs;
  73866. +volatile void *dwc_regs_base;
  73867. +int fiq_done, int_done;
  73868. +
  73869. +gintsts_data_t gintsts_saved = {.d32 = 0};
  73870. +hcint_data_t hcint_saved[MAX_EPS_CHANNELS];
  73871. +hcintmsk_data_t hcintmsk_saved[MAX_EPS_CHANNELS];
  73872. +int split_out_xfersize[MAX_EPS_CHANNELS];
  73873. +haint_data_t haint_saved;
  73874. +
  73875. +int g_next_sched_frame, g_np_count, g_np_sent;
  73876. +static int mphi_int_count = 0 ;
  73877. +
  73878. +hcchar_data_t nak_hcchar;
  73879. +hctsiz_data_t nak_hctsiz;
  73880. +hcsplt_data_t nak_hcsplt;
  73881. +int nak_count;
  73882. +
  73883. +int complete_sched[MAX_EPS_CHANNELS] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  73884. +int split_start_frame[MAX_EPS_CHANNELS];
  73885. +int queued_port[MAX_EPS_CHANNELS];
  73886. +
  73887. +#ifdef FIQ_DEBUG
  73888. +char buffer[1000*16];
  73889. +int wptr;
  73890. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  73891. +{
  73892. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  73893. + va_list args;
  73894. + char text[17];
  73895. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  73896. + unsigned long flags;
  73897. +
  73898. + local_irq_save(flags);
  73899. + local_fiq_disable();
  73900. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  73901. + {
  73902. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  73903. + va_start(args, fmt);
  73904. + vsnprintf(text+8, 9, fmt, args);
  73905. + va_end(args);
  73906. +
  73907. + memcpy(buffer + wptr, text, 16);
  73908. + wptr = (wptr + 16) % sizeof(buffer);
  73909. + }
  73910. + local_irq_restore(flags);
  73911. +}
  73912. +#endif
  73913. +
  73914. +void notrace fiq_queue_request(int channel, int odd_frame)
  73915. +{
  73916. + hcchar_data_t hcchar = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x0) };
  73917. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x4) };
  73918. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x10) };
  73919. +
  73920. + if(hcsplt.b.spltena == 0)
  73921. + {
  73922. + fiq_print(FIQDBG_ERR, "SPLTENA ");
  73923. + BUG();
  73924. + }
  73925. +
  73926. + if(hcchar.b.epdir == 1)
  73927. + {
  73928. + fiq_print(FIQDBG_SCHED, "IN Ch %d", channel);
  73929. + }
  73930. + else
  73931. + {
  73932. + hctsiz.b.xfersize = 0;
  73933. + fiq_print(FIQDBG_SCHED, "OUT Ch %d", channel);
  73934. + }
  73935. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x10), hctsiz.d32);
  73936. +
  73937. + hcsplt.b.compsplt = 1;
  73938. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x4), hcsplt.d32);
  73939. +
  73940. + // Send the Split complete
  73941. + hcchar.b.chen = 1;
  73942. + hcchar.b.oddfrm = odd_frame ? 1 : 0;
  73943. +
  73944. + // Post this for transmit on the next frame for periodic or this frame for non-periodic
  73945. + fiq_print(FIQDBG_SCHED, "SND_%s", odd_frame ? "ODD " : "EVEN");
  73946. +
  73947. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x0), hcchar.d32);
  73948. +}
  73949. +
  73950. +static int last_sof = -1;
  73951. +
  73952. +/*
  73953. +** Function to handle the start of frame interrupt, choose whether we need to do anything and
  73954. +** therefore trigger the main interrupt
  73955. +**
  73956. +** returns int != 0 - interrupt has been handled
  73957. +*/
  73958. +int diff;
  73959. +
  73960. +int notrace fiq_sof_handle(hfnum_data_t hfnum)
  73961. +{
  73962. + int handled = 0;
  73963. + int i;
  73964. +
  73965. + // Just check that once we're running we don't miss a SOF
  73966. + /*if(last_sof != -1 && (hfnum.b.frnum != ((last_sof + 1) & 0x3fff)))
  73967. + {
  73968. + fiq_print(FIQDBG_ERR, "LASTSOF ");
  73969. + fiq_print(FIQDBG_ERR, "%4d%d ", last_sof / 8, last_sof & 7);
  73970. + fiq_print(FIQDBG_ERR, "%4d%d ", hfnum.b.frnum / 8, hfnum.b.frnum & 7);
  73971. + BUG();
  73972. + }*/
  73973. +
  73974. + // Only start remembering the last sof when the interrupt has been
  73975. + // enabled (we don't check the mask to come in here...)
  73976. + if(last_sof != -1 || FIQ_READ(dwc_regs_base + 0x18) & (1<<3))
  73977. + last_sof = hfnum.b.frnum;
  73978. +
  73979. + for(i = 0; i < MAX_EPS_CHANNELS; i++)
  73980. + {
  73981. + if(complete_sched[i] != -1)
  73982. + {
  73983. + if(complete_sched[i] <= hfnum.b.frnum || (complete_sched[i] > 0x3f00 && hfnum.b.frnum < 0xf0))
  73984. + {
  73985. + fiq_queue_request(i, hfnum.b.frnum & 1);
  73986. + complete_sched[i] = -1;
  73987. + }
  73988. + }
  73989. +
  73990. + if(complete_sched[i] != -1)
  73991. + {
  73992. + // This is because we've seen a split complete occur with no start...
  73993. + // most likely because missed the complete 0x3fff frames ago!
  73994. +
  73995. + diff = (hfnum.b.frnum + 0x3fff - complete_sched[i]) & 0x3fff ;
  73996. + if(diff > 32 && diff < 0x3f00)
  73997. + {
  73998. + fiq_print(FIQDBG_ERR, "SPLTMISS");
  73999. + BUG();
  74000. + }
  74001. + }
  74002. + }
  74003. +
  74004. + if(g_np_count == g_np_sent && dwc_frame_num_gt(g_next_sched_frame, hfnum.b.frnum))
  74005. + {
  74006. + /*
  74007. + * If np_count != np_sent that means we need to queue non-periodic (bulk) packets this packet
  74008. + * g_next_sched_frame is the next frame we have periodic packets for
  74009. + *
  74010. + * if neither of these are required for this frame then just clear the interrupt
  74011. + */
  74012. + handled = 1;
  74013. +
  74014. + }
  74015. +
  74016. + return handled;
  74017. +}
  74018. +
  74019. +int notrace port_id(hcsplt_data_t hcsplt)
  74020. +{
  74021. + return hcsplt.b.prtaddr + (hcsplt.b.hubaddr << 8);
  74022. +}
  74023. +
  74024. +int notrace fiq_hcintr_handle(int channel, hfnum_data_t hfnum)
  74025. +{
  74026. + hcchar_data_t hcchar = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x0) };
  74027. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x4) };
  74028. + hcint_data_t hcint = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x8) };
  74029. + hcintmsk_data_t hcintmsk = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0xc) };
  74030. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(dwc_regs_base + 0x500 + (channel * 0x20) + 0x10)};
  74031. +
  74032. + hcint_saved[channel].d32 |= hcint.d32;
  74033. + hcintmsk_saved[channel].d32 = hcintmsk.d32;
  74034. +
  74035. + if(hcsplt.b.spltena)
  74036. + {
  74037. + fiq_print(FIQDBG_PORTHUB, "ph: %4x", port_id(hcsplt));
  74038. + if(hcint.b.chhltd)
  74039. + {
  74040. + fiq_print(FIQDBG_SCHED, "CH HLT %d", channel);
  74041. + fiq_print(FIQDBG_SCHED, "%08x", hcint_saved[channel]);
  74042. + }
  74043. + if(hcint.b.stall || hcint.b.xacterr || hcint.b.bblerr || hcint.b.frmovrun || hcint.b.datatglerr)
  74044. + {
  74045. + queued_port[channel] = 0;
  74046. + fiq_print(FIQDBG_ERR, "CHAN ERR");
  74047. + }
  74048. + if(hcint.b.xfercomp)
  74049. + {
  74050. + // Clear the port allocation and transmit anything also on this port
  74051. + queued_port[channel] = 0;
  74052. + fiq_print(FIQDBG_SCHED, "XFERCOMP");
  74053. + }
  74054. + if(hcint.b.nak)
  74055. + {
  74056. + queued_port[channel] = 0;
  74057. + fiq_print(FIQDBG_SCHED, "NAK");
  74058. + }
  74059. + if(hcint.b.ack && !hcsplt.b.compsplt)
  74060. + {
  74061. + int i;
  74062. +
  74063. + // Do not complete isochronous out transactions
  74064. + if(hcchar.b.eptype == 1 && hcchar.b.epdir == 0)
  74065. + {
  74066. + queued_port[channel] = 0;
  74067. + fiq_print(FIQDBG_SCHED, "ISOC_OUT");
  74068. + }
  74069. + else
  74070. + {
  74071. + // Make sure we check the port / hub combination that we sent this split on.
  74072. + // Do not queue a second request to the same port
  74073. + for(i = 0; i < MAX_EPS_CHANNELS; i++)
  74074. + {
  74075. + if(port_id(hcsplt) == queued_port[i])
  74076. + {
  74077. + fiq_print(FIQDBG_ERR, "PORTERR ");
  74078. + //BUG();
  74079. + }
  74080. + }
  74081. +
  74082. + split_start_frame[channel] = (hfnum.b.frnum + 1) & ~7;
  74083. +
  74084. + // Note, the size of an OUT is in the start split phase, not
  74085. + // the complete split
  74086. + split_out_xfersize[channel] = hctsiz.b.xfersize;
  74087. +
  74088. + hcint_saved[channel].b.chhltd = 0;
  74089. + hcint_saved[channel].b.ack = 0;
  74090. +
  74091. + queued_port[channel] = port_id(hcsplt);
  74092. +
  74093. + if(hcchar.b.eptype & 1)
  74094. + {
  74095. + // Send the periodic complete in the same oddness frame as the ACK went...
  74096. + fiq_queue_request(channel, !(hfnum.b.frnum & 1));
  74097. + // complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 1);
  74098. + }
  74099. + else
  74100. + {
  74101. + // Schedule the split complete to occur later
  74102. + complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 2);
  74103. + fiq_print(FIQDBG_SCHED, "ACK%04d%d", complete_sched[channel]/8, complete_sched[channel]%8);
  74104. + }
  74105. + }
  74106. + }
  74107. + if(hcint.b.nyet)
  74108. + {
  74109. + fiq_print(FIQDBG_ERR, "NYETERR1");
  74110. + //BUG();
  74111. + // Can transmit a split complete up to uframe .0 of the next frame
  74112. + if(hfnum.b.frnum <= dwc_frame_num_inc(split_start_frame[channel], 8))
  74113. + {
  74114. + // Send it next frame
  74115. + if(hcchar.b.eptype & 1) // type 1 & 3 are interrupt & isoc
  74116. + {
  74117. + fiq_print(FIQDBG_SCHED, "NYT:SEND");
  74118. + fiq_queue_request(channel, !(hfnum.b.frnum & 1));
  74119. + }
  74120. + else
  74121. + {
  74122. + // Schedule non-periodic access for next frame (the odd-even bit doesn't effect NP)
  74123. + complete_sched[channel] = dwc_frame_num_inc(hfnum.b.frnum, 1);
  74124. + fiq_print(FIQDBG_SCHED, "NYT%04d%d", complete_sched[channel]/8, complete_sched[channel]%8);
  74125. + }
  74126. + hcint_saved[channel].b.chhltd = 0;
  74127. + hcint_saved[channel].b.nyet = 0;
  74128. + }
  74129. + else
  74130. + {
  74131. + queued_port[channel] = 0;
  74132. + fiq_print(FIQDBG_ERR, "NYETERR2");
  74133. + //BUG();
  74134. + }
  74135. + }
  74136. + }
  74137. + else
  74138. + {
  74139. + /*
  74140. + * If we have any of NAK, ACK, Datatlgerr active on a
  74141. + * non-split channel, the sole reason is to reset error
  74142. + * counts for a previously broken transaction. The FIQ
  74143. + * will thrash on NAK IN and ACK OUT in particular so
  74144. + * handle it "once" and allow the IRQ to do the rest.
  74145. + */
  74146. + hcint.d32 &= hcintmsk.d32;
  74147. + if(hcint.b.nak)
  74148. + {
  74149. + hcintmsk.b.nak = 0;
  74150. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0xc), hcintmsk.d32);
  74151. + }
  74152. + if (hcint.b.ack)
  74153. + {
  74154. + hcintmsk.b.ack = 0;
  74155. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0xc), hcintmsk.d32);
  74156. + }
  74157. + }
  74158. +
  74159. + // Clear the interrupt, this will also clear the HAINT bit
  74160. + FIQ_WRITE((dwc_regs_base + 0x500 + (channel * 0x20) + 0x8), hcint.d32);
  74161. + return hcint_saved[channel].d32 == 0;
  74162. +}
  74163. +
  74164. +gintsts_data_t gintsts;
  74165. +gintmsk_data_t gintmsk;
  74166. +// triggered: The set of interrupts that were triggered
  74167. +// handled: The set of interrupts that have been handled (no IRQ is
  74168. +// required)
  74169. +// keep: The set of interrupts we want to keep unmasked even though we
  74170. +// want to trigger an IRQ to handle it (SOF and HCINTR)
  74171. +gintsts_data_t triggered, handled, keep;
  74172. +hfnum_data_t hfnum;
  74173. +
  74174. +void __attribute__ ((naked)) notrace dwc_otg_hcd_handle_fiq(void)
  74175. +{
  74176. +
  74177. + /* entry takes care to store registers we will be treading on here */
  74178. + asm __volatile__ (
  74179. + "mov ip, sp ;"
  74180. + /* stash FIQ and normal regs */
  74181. + "stmdb sp!, {r0-r12, lr};"
  74182. + /* !! THIS SETS THE FRAME, adjust to > sizeof locals */
  74183. + "sub fp, ip, #512 ;"
  74184. + );
  74185. +
  74186. + // Cannot put local variables at the beginning of the function
  74187. + // because otherwise 'C' will play with the stack pointer. any locals
  74188. + // need to be inside the following block
  74189. + do
  74190. + {
  74191. + fiq_done++;
  74192. + gintsts.d32 = FIQ_READ(dwc_regs_base + 0x14);
  74193. + gintmsk.d32 = FIQ_READ(dwc_regs_base + 0x18);
  74194. + hfnum.d32 = FIQ_READ(dwc_regs_base + 0x408);
  74195. + triggered.d32 = gintsts.d32 & gintmsk.d32;
  74196. + handled.d32 = 0;
  74197. + keep.d32 = 0;
  74198. + fiq_print(FIQDBG_INT, "FIQ ");
  74199. + fiq_print(FIQDBG_INT, "%08x", gintsts.d32);
  74200. + fiq_print(FIQDBG_INT, "%08x", gintmsk.d32);
  74201. + if(gintsts.d32)
  74202. + {
  74203. + // If port enabled
  74204. + if((FIQ_READ(dwc_regs_base + 0x440) & 0xf) == 0x5)
  74205. + {
  74206. + if(gintsts.b.sofintr)
  74207. + {
  74208. + if(fiq_sof_handle(hfnum))
  74209. + {
  74210. + handled.b.sofintr = 1; /* Handled in FIQ */
  74211. + }
  74212. + else
  74213. + {
  74214. + /* Keer interrupt unmasked */
  74215. + keep.b.sofintr = 1;
  74216. + }
  74217. + {
  74218. + // Need to make sure the read and clearing of the SOF interrupt is as close as possible to avoid the possibility of missing
  74219. + // a start of frame interrupt
  74220. + gintsts_data_t gintsts = { .b.sofintr = 1 };
  74221. + FIQ_WRITE((dwc_regs_base + 0x14), gintsts.d32);
  74222. + }
  74223. + }
  74224. +
  74225. + if(fiq_split_enable && gintsts.b.hcintr)
  74226. + {
  74227. + int i;
  74228. + haint_data_t haint;
  74229. + haintmsk_data_t haintmsk;
  74230. +
  74231. + haint.d32 = FIQ_READ(dwc_regs_base + 0x414);
  74232. + haintmsk.d32 = FIQ_READ(dwc_regs_base + 0x418);
  74233. + haint.d32 &= haintmsk.d32;
  74234. + haint_saved.d32 |= haint.d32;
  74235. +
  74236. + fiq_print(FIQDBG_INT, "hcintr");
  74237. + fiq_print(FIQDBG_INT, "%08x", FIQ_READ(dwc_regs_base + 0x414));
  74238. +
  74239. + // Go through each channel that has an enabled interrupt
  74240. + for(i = 0; i < 16; i++)
  74241. + if((haint.d32 >> i) & 1)
  74242. + if(fiq_hcintr_handle(i, hfnum))
  74243. + haint_saved.d32 &= ~(1 << i); /* this was handled */
  74244. +
  74245. + /* If we've handled all host channel interrupts then don't trigger the interrupt */
  74246. + if(haint_saved.d32 == 0)
  74247. + {
  74248. + handled.b.hcintr = 1;
  74249. + }
  74250. + else
  74251. + {
  74252. + /* Make sure we keep the channel interrupt unmasked when triggering the IRQ */
  74253. + keep.b.hcintr = 1;
  74254. + }
  74255. +
  74256. + {
  74257. + gintsts_data_t gintsts = { .b.hcintr = 1 };
  74258. +
  74259. + // Always clear the channel interrupt
  74260. + FIQ_WRITE((dwc_regs_base + 0x14), gintsts.d32);
  74261. + }
  74262. + }
  74263. + }
  74264. + else
  74265. + {
  74266. + last_sof = -1;
  74267. + }
  74268. + }
  74269. +
  74270. + // Mask out the interrupts triggered - those handled - don't mask out the ones we want to keep
  74271. + gintmsk.d32 = keep.d32 | (gintmsk.d32 & ~(triggered.d32 & ~handled.d32));
  74272. + // Save those that were triggered but not handled
  74273. + gintsts_saved.d32 |= triggered.d32 & ~handled.d32;
  74274. + FIQ_WRITE(dwc_regs_base + 0x18, gintmsk.d32);
  74275. +
  74276. + // Clear and save any unhandled interrupts and trigger the interrupt
  74277. + if(gintsts_saved.d32)
  74278. + {
  74279. + /* To enable the MPHI interrupt (INT 32)
  74280. + */
  74281. + FIQ_WRITE( c_mphi_regs.outdda, (int) dummy_send);
  74282. + FIQ_WRITE( c_mphi_regs.outddb, (1 << 29));
  74283. +
  74284. + mphi_int_count++;
  74285. + }
  74286. + }
  74287. + while(0);
  74288. +
  74289. + mb();
  74290. +
  74291. + /* exit back to normal mode restoring everything */
  74292. + asm __volatile__ (
  74293. + /* return FIQ regs back to pristine state
  74294. + * and get normal regs back
  74295. + */
  74296. + "ldmia sp!, {r0-r12, lr};"
  74297. +
  74298. + /* return */
  74299. + "subs pc, lr, #4;"
  74300. + );
  74301. +}
  74302. +
  74303. +/** This function handles interrupts for the HCD. */
  74304. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  74305. +{
  74306. + int retval = 0;
  74307. + static int last_time;
  74308. +
  74309. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  74310. + gintsts_data_t gintsts;
  74311. + gintmsk_data_t gintmsk;
  74312. + hfnum_data_t hfnum;
  74313. +
  74314. +#ifdef DEBUG
  74315. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  74316. +
  74317. +#endif
  74318. +
  74319. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  74320. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  74321. +
  74322. + /* Exit from ISR if core is hibernated */
  74323. + if (core_if->hibernation_suspend == 1) {
  74324. + goto exit_handler_routine;
  74325. + }
  74326. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  74327. + /* Check if HOST Mode */
  74328. + if (dwc_otg_is_host_mode(core_if)) {
  74329. + local_fiq_disable();
  74330. + gintmsk.d32 |= gintsts_saved.d32;
  74331. + gintsts.d32 |= gintsts_saved.d32;
  74332. + gintsts_saved.d32 = 0;
  74333. + local_fiq_enable();
  74334. + if (!gintsts.d32) {
  74335. + goto exit_handler_routine;
  74336. + }
  74337. + gintsts.d32 &= gintmsk.d32;
  74338. +
  74339. +#ifdef DEBUG
  74340. + // We should be OK doing this because the common interrupts should already have been serviced
  74341. + /* Don't print debug message in the interrupt handler on SOF */
  74342. +#ifndef DEBUG_SOF
  74343. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  74344. +#endif
  74345. + DWC_DEBUGPL(DBG_HCDI, "\n");
  74346. +#endif
  74347. +
  74348. +#ifdef DEBUG
  74349. +#ifndef DEBUG_SOF
  74350. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  74351. +#endif
  74352. + DWC_DEBUGPL(DBG_HCDI,
  74353. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  74354. + gintsts.d32, core_if);
  74355. +#endif
  74356. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  74357. + if (gintsts.b.sofintr && g_np_count == g_np_sent && dwc_frame_num_gt(g_next_sched_frame, hfnum.b.frnum))
  74358. + {
  74359. + /* Note, we should never get here if the FIQ is doing it's job properly*/
  74360. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  74361. + }
  74362. + else if (gintsts.b.sofintr) {
  74363. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  74364. + }
  74365. +
  74366. + if (gintsts.b.rxstsqlvl) {
  74367. + retval |=
  74368. + dwc_otg_hcd_handle_rx_status_q_level_intr
  74369. + (dwc_otg_hcd);
  74370. + }
  74371. + if (gintsts.b.nptxfempty) {
  74372. + retval |=
  74373. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  74374. + (dwc_otg_hcd);
  74375. + }
  74376. + if (gintsts.b.i2cintr) {
  74377. + /** @todo Implement i2cintr handler. */
  74378. + }
  74379. + if (gintsts.b.portintr) {
  74380. +
  74381. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  74382. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  74383. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  74384. + }
  74385. + if (gintsts.b.hcintr) {
  74386. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  74387. + }
  74388. + if (gintsts.b.ptxfempty) {
  74389. + retval |=
  74390. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  74391. + (dwc_otg_hcd);
  74392. + }
  74393. +#ifdef DEBUG
  74394. +#ifndef DEBUG_SOF
  74395. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  74396. +#endif
  74397. + {
  74398. + DWC_DEBUGPL(DBG_HCDI,
  74399. + "DWC OTG HCD Finished Servicing Interrupts\n");
  74400. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  74401. + DWC_READ_REG32(&global_regs->gintsts));
  74402. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  74403. + DWC_READ_REG32(&global_regs->gintmsk));
  74404. + }
  74405. +#endif
  74406. +
  74407. +#ifdef DEBUG
  74408. +#ifndef DEBUG_SOF
  74409. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  74410. +#endif
  74411. + DWC_DEBUGPL(DBG_HCDI, "\n");
  74412. +#endif
  74413. +
  74414. + }
  74415. +
  74416. +exit_handler_routine:
  74417. +
  74418. + if (fiq_fix_enable)
  74419. + {
  74420. + local_fiq_disable();
  74421. + // Make sure that we don't clear the interrupt if we've still got pending work to do
  74422. + if(gintsts_saved.d32 == 0)
  74423. + {
  74424. + /* Clear the MPHI interrupt */
  74425. + DWC_WRITE_REG32(c_mphi_regs.intstat, (1<<16));
  74426. + if (mphi_int_count >= 60)
  74427. + {
  74428. + DWC_WRITE_REG32(c_mphi_regs.ctrl, ((1<<31) + (1<<16)));
  74429. + while(!(DWC_READ_REG32(c_mphi_regs.ctrl) & (1 << 17)))
  74430. + ;
  74431. + DWC_WRITE_REG32(c_mphi_regs.ctrl, (1<<31));
  74432. + mphi_int_count = 0;
  74433. + }
  74434. + int_done++;
  74435. + }
  74436. +
  74437. + // Unmask handled interrupts
  74438. + FIQ_WRITE(dwc_regs_base + 0x18, gintmsk.d32);
  74439. + //DWC_MODIFY_REG32((uint32_t *)IO_ADDRESS(USB_BASE + 0x8), 0 , 1);
  74440. +
  74441. + local_fiq_enable();
  74442. +
  74443. + if((jiffies / HZ) > last_time)
  74444. + {
  74445. + /* Once a second output the fiq and irq numbers, useful for debug */
  74446. + last_time = jiffies / HZ;
  74447. + DWC_DEBUGPL(DBG_USER, "int_done = %d fiq_done = %d\n", int_done, fiq_done);
  74448. + }
  74449. + }
  74450. +
  74451. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  74452. + return retval;
  74453. +}
  74454. +
  74455. +#ifdef DWC_TRACK_MISSED_SOFS
  74456. +
  74457. +#warning Compiling code to track missed SOFs
  74458. +#define FRAME_NUM_ARRAY_SIZE 1000
  74459. +/**
  74460. + * This function is for debug only.
  74461. + */
  74462. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  74463. +{
  74464. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  74465. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  74466. + static int frame_num_idx = 0;
  74467. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  74468. + static int dumped_frame_num_array = 0;
  74469. +
  74470. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  74471. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  74472. + curr_frame_number) {
  74473. + frame_num_array[frame_num_idx] = curr_frame_number;
  74474. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  74475. + }
  74476. + } else if (!dumped_frame_num_array) {
  74477. + int i;
  74478. + DWC_PRINTF("Frame Last Frame\n");
  74479. + DWC_PRINTF("----- ----------\n");
  74480. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  74481. + DWC_PRINTF("0x%04x 0x%04x\n",
  74482. + frame_num_array[i], last_frame_num_array[i]);
  74483. + }
  74484. + dumped_frame_num_array = 1;
  74485. + }
  74486. + last_frame_num = curr_frame_number;
  74487. +}
  74488. +#endif
  74489. +
  74490. +/**
  74491. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  74492. + * transactions may be queued to the DWC_otg controller for the current
  74493. + * (micro)frame. Periodic transactions may be queued to the controller for the
  74494. + * next (micro)frame.
  74495. + */
  74496. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  74497. +{
  74498. + hfnum_data_t hfnum;
  74499. + dwc_list_link_t *qh_entry;
  74500. + dwc_otg_qh_t *qh;
  74501. + dwc_otg_transaction_type_e tr_type;
  74502. + int did_something = 0;
  74503. + int32_t next_sched_frame = -1;
  74504. +
  74505. + hfnum.d32 =
  74506. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  74507. +
  74508. +#ifdef DEBUG_SOF
  74509. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  74510. +#endif
  74511. + hcd->frame_number = hfnum.b.frnum;
  74512. +
  74513. +#ifdef DEBUG
  74514. + hcd->frrem_accum += hfnum.b.frrem;
  74515. + hcd->frrem_samples++;
  74516. +#endif
  74517. +
  74518. +#ifdef DWC_TRACK_MISSED_SOFS
  74519. + track_missed_sofs(hcd->frame_number);
  74520. +#endif
  74521. + /* Determine whether any periodic QHs should be executed. */
  74522. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  74523. + while (qh_entry != &hcd->periodic_sched_inactive) {
  74524. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  74525. + qh_entry = qh_entry->next;
  74526. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  74527. +
  74528. + /*
  74529. + * Move QH to the ready list to be executed next
  74530. + * (micro)frame.
  74531. + */
  74532. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  74533. + &qh->qh_list_entry);
  74534. +
  74535. + did_something = 1;
  74536. + }
  74537. + else
  74538. + {
  74539. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  74540. + {
  74541. + next_sched_frame = qh->sched_frame;
  74542. + }
  74543. + }
  74544. + }
  74545. +
  74546. + g_next_sched_frame = next_sched_frame;
  74547. +
  74548. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  74549. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  74550. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  74551. + did_something = 1;
  74552. + }
  74553. +
  74554. + /* Clear interrupt */
  74555. + gintsts.b.sofintr = 1;
  74556. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  74557. +
  74558. + return 1;
  74559. +}
  74560. +
  74561. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  74562. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  74563. + * memory if the DWC_otg controller is operating in Slave mode. */
  74564. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  74565. +{
  74566. + host_grxsts_data_t grxsts;
  74567. + dwc_hc_t *hc = NULL;
  74568. +
  74569. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  74570. +
  74571. + grxsts.d32 =
  74572. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  74573. +
  74574. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  74575. + if (!hc) {
  74576. + DWC_ERROR("Unable to get corresponding channel\n");
  74577. + return 0;
  74578. + }
  74579. +
  74580. + /* Packet Status */
  74581. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  74582. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  74583. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  74584. + hc->data_pid_start);
  74585. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  74586. +
  74587. + switch (grxsts.b.pktsts) {
  74588. + case DWC_GRXSTS_PKTSTS_IN:
  74589. + /* Read the data into the host buffer. */
  74590. + if (grxsts.b.bcnt > 0) {
  74591. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  74592. + hc->xfer_buff, grxsts.b.bcnt);
  74593. +
  74594. + /* Update the HC fields for the next packet received. */
  74595. + hc->xfer_count += grxsts.b.bcnt;
  74596. + hc->xfer_buff += grxsts.b.bcnt;
  74597. + }
  74598. +
  74599. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  74600. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  74601. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  74602. + /* Handled in interrupt, just ignore data */
  74603. + break;
  74604. + default:
  74605. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  74606. + grxsts.b.pktsts);
  74607. + break;
  74608. + }
  74609. +
  74610. + return 1;
  74611. +}
  74612. +
  74613. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  74614. + * data packets may be written to the FIFO for OUT transfers. More requests
  74615. + * may be written to the non-periodic request queue for IN transfers. This
  74616. + * interrupt is enabled only in Slave mode. */
  74617. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  74618. +{
  74619. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  74620. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  74621. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  74622. + return 1;
  74623. +}
  74624. +
  74625. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  74626. + * packets may be written to the FIFO for OUT transfers. More requests may be
  74627. + * written to the periodic request queue for IN transfers. This interrupt is
  74628. + * enabled only in Slave mode. */
  74629. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  74630. +{
  74631. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  74632. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  74633. + DWC_OTG_TRANSACTION_PERIODIC);
  74634. + return 1;
  74635. +}
  74636. +
  74637. +/** There are multiple conditions that can cause a port interrupt. This function
  74638. + * determines which interrupt conditions have occurred and handles them
  74639. + * appropriately. */
  74640. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  74641. +{
  74642. + int retval = 0;
  74643. + hprt0_data_t hprt0;
  74644. + hprt0_data_t hprt0_modify;
  74645. +
  74646. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  74647. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  74648. +
  74649. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  74650. + * GINTSTS */
  74651. +
  74652. + hprt0_modify.b.prtena = 0;
  74653. + hprt0_modify.b.prtconndet = 0;
  74654. + hprt0_modify.b.prtenchng = 0;
  74655. + hprt0_modify.b.prtovrcurrchng = 0;
  74656. +
  74657. + /* Port Connect Detected
  74658. + * Set flag and clear if detected */
  74659. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  74660. + // Dont modify port status if we are in hibernation state
  74661. + hprt0_modify.b.prtconndet = 1;
  74662. + hprt0_modify.b.prtenchng = 1;
  74663. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  74664. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  74665. + return retval;
  74666. + }
  74667. +
  74668. + if (hprt0.b.prtconndet) {
  74669. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  74670. + if (dwc_otg_hcd->core_if->adp_enable &&
  74671. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  74672. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  74673. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  74674. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  74675. + /* TODO - check if this is required, as
  74676. + * host initialization was already performed
  74677. + * after initial ADP probing
  74678. + */
  74679. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  74680. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  74681. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  74682. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  74683. + } else {
  74684. +
  74685. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  74686. + "Port Connect Detected--\n", hprt0.d32);
  74687. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  74688. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  74689. + hprt0_modify.b.prtconndet = 1;
  74690. +
  74691. + /* B-Device has connected, Delete the connection timer. */
  74692. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  74693. + }
  74694. + /* The Hub driver asserts a reset when it sees port connect
  74695. + * status change flag */
  74696. + retval |= 1;
  74697. + }
  74698. +
  74699. + /* Port Enable Changed
  74700. + * Clear if detected - Set internal flag if disabled */
  74701. + if (hprt0.b.prtenchng) {
  74702. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  74703. + "Port Enable Changed--\n", hprt0.d32);
  74704. + hprt0_modify.b.prtenchng = 1;
  74705. + if (hprt0.b.prtena == 1) {
  74706. + hfir_data_t hfir;
  74707. + int do_reset = 0;
  74708. + dwc_otg_core_params_t *params =
  74709. + dwc_otg_hcd->core_if->core_params;
  74710. + dwc_otg_core_global_regs_t *global_regs =
  74711. + dwc_otg_hcd->core_if->core_global_regs;
  74712. + dwc_otg_host_if_t *host_if =
  74713. + dwc_otg_hcd->core_if->host_if;
  74714. +
  74715. + /* Every time when port enables calculate
  74716. + * HFIR.FrInterval
  74717. + */
  74718. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  74719. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  74720. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  74721. +
  74722. + /* Check if we need to adjust the PHY clock speed for
  74723. + * low power and adjust it */
  74724. + if (params->host_support_fs_ls_low_power) {
  74725. + gusbcfg_data_t usbcfg;
  74726. +
  74727. + usbcfg.d32 =
  74728. + DWC_READ_REG32(&global_regs->gusbcfg);
  74729. +
  74730. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  74731. + || hprt0.b.prtspd ==
  74732. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  74733. + /*
  74734. + * Low power
  74735. + */
  74736. + hcfg_data_t hcfg;
  74737. + if (usbcfg.b.phylpwrclksel == 0) {
  74738. + /* Set PHY low power clock select for FS/LS devices */
  74739. + usbcfg.b.phylpwrclksel = 1;
  74740. + DWC_WRITE_REG32
  74741. + (&global_regs->gusbcfg,
  74742. + usbcfg.d32);
  74743. + do_reset = 1;
  74744. + }
  74745. +
  74746. + hcfg.d32 =
  74747. + DWC_READ_REG32
  74748. + (&host_if->host_global_regs->hcfg);
  74749. +
  74750. + if (hprt0.b.prtspd ==
  74751. + DWC_HPRT0_PRTSPD_LOW_SPEED
  74752. + && params->host_ls_low_power_phy_clk
  74753. + ==
  74754. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  74755. + {
  74756. + /* 6 MHZ */
  74757. + DWC_DEBUGPL(DBG_CIL,
  74758. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  74759. + if (hcfg.b.fslspclksel !=
  74760. + DWC_HCFG_6_MHZ) {
  74761. + hcfg.b.fslspclksel =
  74762. + DWC_HCFG_6_MHZ;
  74763. + DWC_WRITE_REG32
  74764. + (&host_if->host_global_regs->hcfg,
  74765. + hcfg.d32);
  74766. + do_reset = 1;
  74767. + }
  74768. + } else {
  74769. + /* 48 MHZ */
  74770. + DWC_DEBUGPL(DBG_CIL,
  74771. + "FS_PHY programming HCFG to 48 MHz ()\n");
  74772. + if (hcfg.b.fslspclksel !=
  74773. + DWC_HCFG_48_MHZ) {
  74774. + hcfg.b.fslspclksel =
  74775. + DWC_HCFG_48_MHZ;
  74776. + DWC_WRITE_REG32
  74777. + (&host_if->host_global_regs->hcfg,
  74778. + hcfg.d32);
  74779. + do_reset = 1;
  74780. + }
  74781. + }
  74782. + } else {
  74783. + /*
  74784. + * Not low power
  74785. + */
  74786. + if (usbcfg.b.phylpwrclksel == 1) {
  74787. + usbcfg.b.phylpwrclksel = 0;
  74788. + DWC_WRITE_REG32
  74789. + (&global_regs->gusbcfg,
  74790. + usbcfg.d32);
  74791. + do_reset = 1;
  74792. + }
  74793. + }
  74794. +
  74795. + if (do_reset) {
  74796. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  74797. + }
  74798. + }
  74799. +
  74800. + if (!do_reset) {
  74801. + /* Port has been enabled set the reset change flag */
  74802. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  74803. + }
  74804. + } else {
  74805. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  74806. + }
  74807. + retval |= 1;
  74808. + }
  74809. +
  74810. + /** Overcurrent Change Interrupt */
  74811. + if (hprt0.b.prtovrcurrchng) {
  74812. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  74813. + "Port Overcurrent Changed--\n", hprt0.d32);
  74814. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  74815. + hprt0_modify.b.prtovrcurrchng = 1;
  74816. + retval |= 1;
  74817. + }
  74818. +
  74819. + /* Clear Port Interrupts */
  74820. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  74821. +
  74822. + return retval;
  74823. +}
  74824. +
  74825. +/** This interrupt indicates that one or more host channels has a pending
  74826. + * interrupt. There are multiple conditions that can cause each host channel
  74827. + * interrupt. This function determines which conditions have occurred for each
  74828. + * host channel interrupt and handles them appropriately. */
  74829. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  74830. +{
  74831. + int i;
  74832. + int retval = 0;
  74833. + haint_data_t haint;
  74834. +
  74835. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  74836. + * GINTSTS */
  74837. +
  74838. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  74839. +
  74840. + // Overwrite with saved interrupts from fiq handler
  74841. + if(fiq_split_enable)
  74842. + {
  74843. + local_fiq_disable();
  74844. + haint.d32 = haint_saved.d32;
  74845. + haint_saved.d32 = 0;
  74846. + local_fiq_enable();
  74847. + }
  74848. +
  74849. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  74850. + if (haint.b2.chint & (1 << i)) {
  74851. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  74852. + }
  74853. + }
  74854. +
  74855. + return retval;
  74856. +}
  74857. +
  74858. +/**
  74859. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  74860. + * holds the reason for the halt.
  74861. + *
  74862. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  74863. + * *short_read is set to 1 upon return if less than the requested
  74864. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  74865. + * return. short_read may also be NULL on entry, in which case it remains
  74866. + * unchanged.
  74867. + */
  74868. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  74869. + dwc_otg_hc_regs_t * hc_regs,
  74870. + dwc_otg_qtd_t * qtd,
  74871. + dwc_otg_halt_status_e halt_status,
  74872. + int *short_read)
  74873. +{
  74874. + hctsiz_data_t hctsiz;
  74875. + uint32_t length;
  74876. +
  74877. + if (short_read != NULL) {
  74878. + *short_read = 0;
  74879. + }
  74880. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  74881. +
  74882. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  74883. + if (hc->ep_is_in) {
  74884. + length = hc->xfer_len - hctsiz.b.xfersize;
  74885. + if (short_read != NULL) {
  74886. + *short_read = (hctsiz.b.xfersize != 0);
  74887. + }
  74888. + } else if (hc->qh->do_split) {
  74889. + if(fiq_split_enable)
  74890. + length = split_out_xfersize[hc->hc_num];
  74891. + else
  74892. + length = qtd->ssplit_out_xfer_count;
  74893. + } else {
  74894. + length = hc->xfer_len;
  74895. + }
  74896. + } else {
  74897. + /*
  74898. + * Must use the hctsiz.pktcnt field to determine how much data
  74899. + * has been transferred. This field reflects the number of
  74900. + * packets that have been transferred via the USB. This is
  74901. + * always an integral number of packets if the transfer was
  74902. + * halted before its normal completion. (Can't use the
  74903. + * hctsiz.xfersize field because that reflects the number of
  74904. + * bytes transferred via the AHB, not the USB).
  74905. + */
  74906. + length =
  74907. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  74908. + }
  74909. +
  74910. + return length;
  74911. +}
  74912. +
  74913. +/**
  74914. + * Updates the state of the URB after a Transfer Complete interrupt on the
  74915. + * host channel. Updates the actual_length field of the URB based on the
  74916. + * number of bytes transferred via the host channel. Sets the URB status
  74917. + * if the data transfer is finished.
  74918. + *
  74919. + * @return 1 if the data transfer specified by the URB is completely finished,
  74920. + * 0 otherwise.
  74921. + */
  74922. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  74923. + dwc_otg_hc_regs_t * hc_regs,
  74924. + dwc_otg_hcd_urb_t * urb,
  74925. + dwc_otg_qtd_t * qtd)
  74926. +{
  74927. + int xfer_done = 0;
  74928. + int short_read = 0;
  74929. +
  74930. + int xfer_length;
  74931. +
  74932. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  74933. + DWC_OTG_HC_XFER_COMPLETE,
  74934. + &short_read);
  74935. +
  74936. + /* non DWORD-aligned buffer case handling. */
  74937. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  74938. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  74939. + xfer_length);
  74940. + }
  74941. +
  74942. + urb->actual_length += xfer_length;
  74943. +
  74944. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  74945. + (urb->flags & URB_SEND_ZERO_PACKET)
  74946. + && (urb->actual_length == urb->length)
  74947. + && !(urb->length % hc->max_packet)) {
  74948. + xfer_done = 0;
  74949. + } else if (short_read || urb->actual_length >= urb->length) {
  74950. + xfer_done = 1;
  74951. + urb->status = 0;
  74952. + }
  74953. +
  74954. +#ifdef DEBUG
  74955. + {
  74956. + hctsiz_data_t hctsiz;
  74957. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  74958. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  74959. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  74960. + hc->hc_num);
  74961. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  74962. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  74963. + hctsiz.b.xfersize);
  74964. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  74965. + urb->length);
  74966. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  74967. + urb->actual_length);
  74968. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  74969. + short_read, xfer_done);
  74970. + }
  74971. +#endif
  74972. +
  74973. + return xfer_done;
  74974. +}
  74975. +
  74976. +/*
  74977. + * Save the starting data toggle for the next transfer. The data toggle is
  74978. + * saved in the QH for non-control transfers and it's saved in the QTD for
  74979. + * control transfers.
  74980. + */
  74981. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  74982. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  74983. +{
  74984. + hctsiz_data_t hctsiz;
  74985. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  74986. +
  74987. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  74988. + dwc_otg_qh_t *qh = hc->qh;
  74989. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  74990. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  74991. + } else {
  74992. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  74993. + }
  74994. + } else {
  74995. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  74996. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  74997. + } else {
  74998. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  74999. + }
  75000. + }
  75001. +}
  75002. +
  75003. +/**
  75004. + * Updates the state of an Isochronous URB when the transfer is stopped for
  75005. + * any reason. The fields of the current entry in the frame descriptor array
  75006. + * are set based on the transfer state and the input _halt_status. Completes
  75007. + * the Isochronous URB if all the URB frames have been completed.
  75008. + *
  75009. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  75010. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  75011. + */
  75012. +static dwc_otg_halt_status_e
  75013. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  75014. + dwc_hc_t * hc,
  75015. + dwc_otg_hc_regs_t * hc_regs,
  75016. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  75017. +{
  75018. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  75019. + dwc_otg_halt_status_e ret_val = halt_status;
  75020. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  75021. +
  75022. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  75023. + switch (halt_status) {
  75024. + case DWC_OTG_HC_XFER_COMPLETE:
  75025. + frame_desc->status = 0;
  75026. + frame_desc->actual_length =
  75027. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  75028. +
  75029. + /* non DWORD-aligned buffer case handling. */
  75030. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  75031. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  75032. + hc->qh->dw_align_buf, frame_desc->actual_length);
  75033. + }
  75034. +
  75035. + break;
  75036. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  75037. + urb->error_count++;
  75038. + if (hc->ep_is_in) {
  75039. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  75040. + } else {
  75041. + frame_desc->status = -DWC_E_COMMUNICATION;
  75042. + }
  75043. + frame_desc->actual_length = 0;
  75044. + break;
  75045. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  75046. + urb->error_count++;
  75047. + frame_desc->status = -DWC_E_OVERFLOW;
  75048. + /* Don't need to update actual_length in this case. */
  75049. + break;
  75050. + case DWC_OTG_HC_XFER_XACT_ERR:
  75051. + urb->error_count++;
  75052. + frame_desc->status = -DWC_E_PROTOCOL;
  75053. + frame_desc->actual_length =
  75054. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  75055. +
  75056. + /* non DWORD-aligned buffer case handling. */
  75057. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  75058. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  75059. + hc->qh->dw_align_buf, frame_desc->actual_length);
  75060. + }
  75061. + /* Skip whole frame */
  75062. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  75063. + hc->ep_is_in && hcd->core_if->dma_enable) {
  75064. + qtd->complete_split = 0;
  75065. + qtd->isoc_split_offset = 0;
  75066. + }
  75067. +
  75068. + break;
  75069. + default:
  75070. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  75071. + break;
  75072. + }
  75073. + if (++qtd->isoc_frame_index == urb->packet_count) {
  75074. + /*
  75075. + * urb->status is not used for isoc transfers.
  75076. + * The individual frame_desc statuses are used instead.
  75077. + */
  75078. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  75079. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  75080. + } else {
  75081. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  75082. + }
  75083. + return ret_val;
  75084. +}
  75085. +
  75086. +/**
  75087. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  75088. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  75089. + * still linked to the QH, the QH is added to the end of the inactive
  75090. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  75091. + * schedule if no more QTDs are linked to the QH.
  75092. + */
  75093. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  75094. +{
  75095. + int continue_split = 0;
  75096. + dwc_otg_qtd_t *qtd;
  75097. +
  75098. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  75099. +
  75100. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  75101. +
  75102. + if (qtd->complete_split) {
  75103. + continue_split = 1;
  75104. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  75105. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  75106. + continue_split = 1;
  75107. + }
  75108. +
  75109. + if (free_qtd) {
  75110. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  75111. + continue_split = 0;
  75112. + }
  75113. +
  75114. + qh->channel = NULL;
  75115. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  75116. +}
  75117. +
  75118. +/**
  75119. + * Releases a host channel for use by other transfers. Attempts to select and
  75120. + * queue more transactions since at least one host channel is available.
  75121. + *
  75122. + * @param hcd The HCD state structure.
  75123. + * @param hc The host channel to release.
  75124. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  75125. + * if the transfer is complete or an error has occurred.
  75126. + * @param halt_status Reason the channel is being released. This status
  75127. + * determines the actions taken by this function.
  75128. + */
  75129. +static void release_channel(dwc_otg_hcd_t * hcd,
  75130. + dwc_hc_t * hc,
  75131. + dwc_otg_qtd_t * qtd,
  75132. + dwc_otg_halt_status_e halt_status)
  75133. +{
  75134. + dwc_otg_transaction_type_e tr_type;
  75135. + int free_qtd;
  75136. + dwc_irqflags_t flags;
  75137. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  75138. +#ifdef FIQ_DEBUG
  75139. + int endp = qtd->urb ? qtd->urb->pipe_info.ep_num : 0;
  75140. +#endif
  75141. + int hog_port = 0;
  75142. +
  75143. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  75144. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  75145. +
  75146. + if(fiq_split_enable && hc->do_split) {
  75147. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  75148. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  75149. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  75150. + hog_port = 1;
  75151. + }
  75152. + }
  75153. + }
  75154. +
  75155. + switch (halt_status) {
  75156. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  75157. + free_qtd = 1;
  75158. + break;
  75159. + case DWC_OTG_HC_XFER_AHB_ERR:
  75160. + case DWC_OTG_HC_XFER_STALL:
  75161. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  75162. + free_qtd = 1;
  75163. + break;
  75164. + case DWC_OTG_HC_XFER_XACT_ERR:
  75165. + if (qtd->error_count >= 3) {
  75166. + DWC_DEBUGPL(DBG_HCDV,
  75167. + " Complete URB with transaction error\n");
  75168. + free_qtd = 1;
  75169. + qtd->urb->status = -DWC_E_PROTOCOL;
  75170. + hcd->fops->complete(hcd, qtd->urb->priv,
  75171. + qtd->urb, -DWC_E_PROTOCOL);
  75172. + } else {
  75173. + free_qtd = 0;
  75174. + }
  75175. + break;
  75176. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  75177. + /*
  75178. + * The QTD has already been removed and the QH has been
  75179. + * deactivated. Don't want to do anything except release the
  75180. + * host channel and try to queue more transfers.
  75181. + */
  75182. + goto cleanup;
  75183. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  75184. + free_qtd = 0;
  75185. + break;
  75186. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  75187. + DWC_DEBUGPL(DBG_HCDV,
  75188. + " Complete URB with I/O error\n");
  75189. + free_qtd = 1;
  75190. + qtd->urb->status = -DWC_E_IO;
  75191. + hcd->fops->complete(hcd, qtd->urb->priv,
  75192. + qtd->urb, -DWC_E_IO);
  75193. + break;
  75194. + default:
  75195. + free_qtd = 0;
  75196. + break;
  75197. + }
  75198. +
  75199. + deactivate_qh(hcd, hc->qh, free_qtd);
  75200. +
  75201. +cleanup:
  75202. + /*
  75203. + * Release the host channel for use by other transfers. The cleanup
  75204. + * function clears the channel interrupt enables and conditions, so
  75205. + * there's no need to clear the Channel Halted interrupt separately.
  75206. + */
  75207. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  75208. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  75209. +
  75210. + if (!microframe_schedule) {
  75211. + switch (hc->ep_type) {
  75212. + case DWC_OTG_EP_TYPE_CONTROL:
  75213. + case DWC_OTG_EP_TYPE_BULK:
  75214. + hcd->non_periodic_channels--;
  75215. + break;
  75216. +
  75217. + default:
  75218. + /*
  75219. + * Don't release reservations for periodic channels here.
  75220. + * That's done when a periodic transfer is descheduled (i.e.
  75221. + * when the QH is removed from the periodic schedule).
  75222. + */
  75223. + break;
  75224. + }
  75225. + } else {
  75226. +
  75227. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  75228. + hcd->available_host_channels++;
  75229. + fiq_print(FIQDBG_PORTHUB, "AHC = %d ", hcd->available_host_channels);
  75230. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  75231. + }
  75232. +
  75233. + if(fiq_split_enable && hc->do_split)
  75234. + {
  75235. + if(!(hcd->hub_port[hc->hub_addr] & (1 << hc->port_addr)))
  75236. + {
  75237. + fiq_print(FIQDBG_ERR, "PRTNOTAL");
  75238. + //BUG();
  75239. + }
  75240. + if(!hog_port && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC ||
  75241. + hc->ep_type == DWC_OTG_EP_TYPE_INTR)) {
  75242. + hcd->hub_port[hc->hub_addr] &= ~(1 << hc->port_addr);
  75243. +#ifdef FIQ_DEBUG
  75244. + hcd->hub_port_alloc[hc->hub_addr * 16 + hc->port_addr] = -1;
  75245. +#endif
  75246. + fiq_print(FIQDBG_PORTHUB, "H%dP%d:RR%d", hc->hub_addr, hc->port_addr, endp);
  75247. + }
  75248. + }
  75249. +
  75250. + /* Try to queue more transfers now that there's a free channel. */
  75251. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  75252. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  75253. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  75254. + }
  75255. +}
  75256. +
  75257. +/**
  75258. + * Halts a host channel. If the channel cannot be halted immediately because
  75259. + * the request queue is full, this function ensures that the FIFO empty
  75260. + * interrupt for the appropriate queue is enabled so that the halt request can
  75261. + * be queued when there is space in the request queue.
  75262. + *
  75263. + * This function may also be called in DMA mode. In that case, the channel is
  75264. + * simply released since the core always halts the channel automatically in
  75265. + * DMA mode.
  75266. + */
  75267. +static void halt_channel(dwc_otg_hcd_t * hcd,
  75268. + dwc_hc_t * hc,
  75269. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  75270. +{
  75271. + if (hcd->core_if->dma_enable) {
  75272. + release_channel(hcd, hc, qtd, halt_status);
  75273. + return;
  75274. + }
  75275. +
  75276. + /* Slave mode processing... */
  75277. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  75278. +
  75279. + if (hc->halt_on_queue) {
  75280. + gintmsk_data_t gintmsk = {.d32 = 0 };
  75281. + dwc_otg_core_global_regs_t *global_regs;
  75282. + global_regs = hcd->core_if->core_global_regs;
  75283. +
  75284. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  75285. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  75286. + /*
  75287. + * Make sure the Non-periodic Tx FIFO empty interrupt
  75288. + * is enabled so that the non-periodic schedule will
  75289. + * be processed.
  75290. + */
  75291. + gintmsk.b.nptxfempty = 1;
  75292. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  75293. + } else {
  75294. + /*
  75295. + * Move the QH from the periodic queued schedule to
  75296. + * the periodic assigned schedule. This allows the
  75297. + * halt to be queued when the periodic schedule is
  75298. + * processed.
  75299. + */
  75300. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  75301. + &hc->qh->qh_list_entry);
  75302. +
  75303. + /*
  75304. + * Make sure the Periodic Tx FIFO Empty interrupt is
  75305. + * enabled so that the periodic schedule will be
  75306. + * processed.
  75307. + */
  75308. + gintmsk.b.ptxfempty = 1;
  75309. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  75310. + }
  75311. + }
  75312. +}
  75313. +
  75314. +/**
  75315. + * Performs common cleanup for non-periodic transfers after a Transfer
  75316. + * Complete interrupt. This function should be called after any endpoint type
  75317. + * specific handling is finished to release the host channel.
  75318. + */
  75319. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  75320. + dwc_hc_t * hc,
  75321. + dwc_otg_hc_regs_t * hc_regs,
  75322. + dwc_otg_qtd_t * qtd,
  75323. + dwc_otg_halt_status_e halt_status)
  75324. +{
  75325. + hcint_data_t hcint;
  75326. +
  75327. + qtd->error_count = 0;
  75328. +
  75329. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  75330. + if (hcint.b.nyet) {
  75331. + /*
  75332. + * Got a NYET on the last transaction of the transfer. This
  75333. + * means that the endpoint should be in the PING state at the
  75334. + * beginning of the next transfer.
  75335. + */
  75336. + hc->qh->ping_state = 1;
  75337. + clear_hc_int(hc_regs, nyet);
  75338. + }
  75339. +
  75340. + /*
  75341. + * Always halt and release the host channel to make it available for
  75342. + * more transfers. There may still be more phases for a control
  75343. + * transfer or more data packets for a bulk transfer at this point,
  75344. + * but the host channel is still halted. A channel will be reassigned
  75345. + * to the transfer when the non-periodic schedule is processed after
  75346. + * the channel is released. This allows transactions to be queued
  75347. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  75348. + * Tx FIFO Empty interrupt if necessary.
  75349. + */
  75350. + if (hc->ep_is_in) {
  75351. + /*
  75352. + * IN transfers in Slave mode require an explicit disable to
  75353. + * halt the channel. (In DMA mode, this call simply releases
  75354. + * the channel.)
  75355. + */
  75356. + halt_channel(hcd, hc, qtd, halt_status);
  75357. + } else {
  75358. + /*
  75359. + * The channel is automatically disabled by the core for OUT
  75360. + * transfers in Slave mode.
  75361. + */
  75362. + release_channel(hcd, hc, qtd, halt_status);
  75363. + }
  75364. +}
  75365. +
  75366. +/**
  75367. + * Performs common cleanup for periodic transfers after a Transfer Complete
  75368. + * interrupt. This function should be called after any endpoint type specific
  75369. + * handling is finished to release the host channel.
  75370. + */
  75371. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  75372. + dwc_hc_t * hc,
  75373. + dwc_otg_hc_regs_t * hc_regs,
  75374. + dwc_otg_qtd_t * qtd,
  75375. + dwc_otg_halt_status_e halt_status)
  75376. +{
  75377. + hctsiz_data_t hctsiz;
  75378. + qtd->error_count = 0;
  75379. +
  75380. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75381. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  75382. + /* Core halts channel in these cases. */
  75383. + release_channel(hcd, hc, qtd, halt_status);
  75384. + } else {
  75385. + /* Flush any outstanding requests from the Tx queue. */
  75386. + halt_channel(hcd, hc, qtd, halt_status);
  75387. + }
  75388. +}
  75389. +
  75390. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  75391. + dwc_hc_t * hc,
  75392. + dwc_otg_hc_regs_t * hc_regs,
  75393. + dwc_otg_qtd_t * qtd)
  75394. +{
  75395. + uint32_t len;
  75396. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  75397. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  75398. +
  75399. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  75400. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  75401. +
  75402. + if (!len) {
  75403. + qtd->complete_split = 0;
  75404. + qtd->isoc_split_offset = 0;
  75405. + return 0;
  75406. + }
  75407. + frame_desc->actual_length += len;
  75408. +
  75409. + if (hc->align_buff && len)
  75410. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  75411. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  75412. + qtd->isoc_split_offset += len;
  75413. +
  75414. + if (frame_desc->length == frame_desc->actual_length) {
  75415. + frame_desc->status = 0;
  75416. + qtd->isoc_frame_index++;
  75417. + qtd->complete_split = 0;
  75418. + qtd->isoc_split_offset = 0;
  75419. + }
  75420. +
  75421. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  75422. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  75423. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  75424. + } else {
  75425. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  75426. + }
  75427. +
  75428. + return 1; /* Indicates that channel released */
  75429. +}
  75430. +
  75431. +/**
  75432. + * Handles a host channel Transfer Complete interrupt. This handler may be
  75433. + * called in either DMA mode or Slave mode.
  75434. + */
  75435. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  75436. + dwc_hc_t * hc,
  75437. + dwc_otg_hc_regs_t * hc_regs,
  75438. + dwc_otg_qtd_t * qtd)
  75439. +{
  75440. + int urb_xfer_done;
  75441. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  75442. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  75443. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  75444. +
  75445. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  75446. + "Transfer Complete--\n", hc->hc_num);
  75447. +
  75448. + if (hcd->core_if->dma_desc_enable) {
  75449. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  75450. + if (pipe_type == UE_ISOCHRONOUS) {
  75451. + /* Do not disable the interrupt, just clear it */
  75452. + clear_hc_int(hc_regs, xfercomp);
  75453. + return 1;
  75454. + }
  75455. + goto handle_xfercomp_done;
  75456. + }
  75457. +
  75458. + /*
  75459. + * Handle xfer complete on CSPLIT.
  75460. + */
  75461. +
  75462. + if (hc->qh->do_split) {
  75463. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  75464. + && hcd->core_if->dma_enable) {
  75465. + if (qtd->complete_split
  75466. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  75467. + qtd))
  75468. + goto handle_xfercomp_done;
  75469. + } else {
  75470. + qtd->complete_split = 0;
  75471. + }
  75472. + }
  75473. +
  75474. + /* Update the QTD and URB states. */
  75475. + switch (pipe_type) {
  75476. + case UE_CONTROL:
  75477. + switch (qtd->control_phase) {
  75478. + case DWC_OTG_CONTROL_SETUP:
  75479. + if (urb->length > 0) {
  75480. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  75481. + } else {
  75482. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  75483. + }
  75484. + DWC_DEBUGPL(DBG_HCDV,
  75485. + " Control setup transaction done\n");
  75486. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  75487. + break;
  75488. + case DWC_OTG_CONTROL_DATA:{
  75489. + urb_xfer_done =
  75490. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  75491. + qtd);
  75492. + if (urb_xfer_done) {
  75493. + qtd->control_phase =
  75494. + DWC_OTG_CONTROL_STATUS;
  75495. + DWC_DEBUGPL(DBG_HCDV,
  75496. + " Control data transfer done\n");
  75497. + } else {
  75498. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  75499. + }
  75500. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  75501. + break;
  75502. + }
  75503. + case DWC_OTG_CONTROL_STATUS:
  75504. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  75505. + if (urb->status == -DWC_E_IN_PROGRESS) {
  75506. + urb->status = 0;
  75507. + }
  75508. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  75509. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  75510. + break;
  75511. + }
  75512. +
  75513. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  75514. + break;
  75515. + case UE_BULK:
  75516. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  75517. + urb_xfer_done =
  75518. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  75519. + if (urb_xfer_done) {
  75520. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  75521. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  75522. + } else {
  75523. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  75524. + }
  75525. +
  75526. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  75527. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  75528. + break;
  75529. + case UE_INTERRUPT:
  75530. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  75531. + urb_xfer_done =
  75532. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  75533. +
  75534. + /*
  75535. + * Interrupt URB is done on the first transfer complete
  75536. + * interrupt.
  75537. + */
  75538. + if (urb_xfer_done) {
  75539. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  75540. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  75541. + } else {
  75542. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  75543. + }
  75544. +
  75545. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  75546. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  75547. + break;
  75548. + case UE_ISOCHRONOUS:
  75549. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  75550. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  75551. + halt_status =
  75552. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  75553. + DWC_OTG_HC_XFER_COMPLETE);
  75554. + }
  75555. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  75556. + break;
  75557. + }
  75558. +
  75559. +handle_xfercomp_done:
  75560. + disable_hc_int(hc_regs, xfercompl);
  75561. +
  75562. + return 1;
  75563. +}
  75564. +
  75565. +/**
  75566. + * Handles a host channel STALL interrupt. This handler may be called in
  75567. + * either DMA mode or Slave mode.
  75568. + */
  75569. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  75570. + dwc_hc_t * hc,
  75571. + dwc_otg_hc_regs_t * hc_regs,
  75572. + dwc_otg_qtd_t * qtd)
  75573. +{
  75574. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  75575. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  75576. +
  75577. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  75578. + "STALL Received--\n", hc->hc_num);
  75579. +
  75580. + if (hcd->core_if->dma_desc_enable) {
  75581. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  75582. + goto handle_stall_done;
  75583. + }
  75584. +
  75585. + if (pipe_type == UE_CONTROL) {
  75586. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  75587. + }
  75588. +
  75589. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  75590. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  75591. + /*
  75592. + * USB protocol requires resetting the data toggle for bulk
  75593. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  75594. + * setup command is issued to the endpoint. Anticipate the
  75595. + * CLEAR_FEATURE command since a STALL has occurred and reset
  75596. + * the data toggle now.
  75597. + */
  75598. + hc->qh->data_toggle = 0;
  75599. + }
  75600. +
  75601. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  75602. +
  75603. +handle_stall_done:
  75604. + disable_hc_int(hc_regs, stall);
  75605. +
  75606. + return 1;
  75607. +}
  75608. +
  75609. +/*
  75610. + * Updates the state of the URB when a transfer has been stopped due to an
  75611. + * abnormal condition before the transfer completes. Modifies the
  75612. + * actual_length field of the URB to reflect the number of bytes that have
  75613. + * actually been transferred via the host channel.
  75614. + */
  75615. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  75616. + dwc_otg_hc_regs_t * hc_regs,
  75617. + dwc_otg_hcd_urb_t * urb,
  75618. + dwc_otg_qtd_t * qtd,
  75619. + dwc_otg_halt_status_e halt_status)
  75620. +{
  75621. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  75622. + halt_status, NULL);
  75623. + /* non DWORD-aligned buffer case handling. */
  75624. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  75625. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  75626. + bytes_transferred);
  75627. + }
  75628. +
  75629. + urb->actual_length += bytes_transferred;
  75630. +
  75631. +#ifdef DEBUG
  75632. + {
  75633. + hctsiz_data_t hctsiz;
  75634. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75635. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  75636. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  75637. + hc->hc_num);
  75638. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  75639. + hc->start_pkt_count);
  75640. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  75641. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  75642. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  75643. + bytes_transferred);
  75644. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  75645. + urb->actual_length);
  75646. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  75647. + urb->length);
  75648. + }
  75649. +#endif
  75650. +}
  75651. +
  75652. +/**
  75653. + * Handles a host channel NAK interrupt. This handler may be called in either
  75654. + * DMA mode or Slave mode.
  75655. + */
  75656. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  75657. + dwc_hc_t * hc,
  75658. + dwc_otg_hc_regs_t * hc_regs,
  75659. + dwc_otg_qtd_t * qtd)
  75660. +{
  75661. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  75662. + "NAK Received--\n", hc->hc_num);
  75663. +
  75664. + /*
  75665. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  75666. + * the beginning of the next frame
  75667. + */
  75668. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  75669. + case UE_BULK:
  75670. + case UE_CONTROL:
  75671. + if (nak_holdoff_enable)
  75672. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  75673. + }
  75674. +
  75675. + /*
  75676. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  75677. + * interrupt. Re-start the SSPLIT transfer.
  75678. + */
  75679. + if (hc->do_split) {
  75680. + if (hc->complete_split) {
  75681. + qtd->error_count = 0;
  75682. + }
  75683. + qtd->complete_split = 0;
  75684. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  75685. + goto handle_nak_done;
  75686. + }
  75687. +
  75688. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  75689. + case UE_CONTROL:
  75690. + case UE_BULK:
  75691. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  75692. + /*
  75693. + * NAK interrupts are enabled on bulk/control IN
  75694. + * transfers in DMA mode for the sole purpose of
  75695. + * resetting the error count after a transaction error
  75696. + * occurs. The core will continue transferring data.
  75697. + * Disable other interrupts unmasked for the same
  75698. + * reason.
  75699. + */
  75700. + disable_hc_int(hc_regs, datatglerr);
  75701. + disable_hc_int(hc_regs, ack);
  75702. + qtd->error_count = 0;
  75703. + goto handle_nak_done;
  75704. + }
  75705. +
  75706. + /*
  75707. + * NAK interrupts normally occur during OUT transfers in DMA
  75708. + * or Slave mode. For IN transfers, more requests will be
  75709. + * queued as request queue space is available.
  75710. + */
  75711. + qtd->error_count = 0;
  75712. +
  75713. + if (!hc->qh->ping_state) {
  75714. + update_urb_state_xfer_intr(hc, hc_regs,
  75715. + qtd->urb, qtd,
  75716. + DWC_OTG_HC_XFER_NAK);
  75717. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  75718. +
  75719. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  75720. + hc->qh->ping_state = 1;
  75721. + }
  75722. +
  75723. + /*
  75724. + * Halt the channel so the transfer can be re-started from
  75725. + * the appropriate point or the PING protocol will
  75726. + * start/continue.
  75727. + */
  75728. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  75729. + break;
  75730. + case UE_INTERRUPT:
  75731. + qtd->error_count = 0;
  75732. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  75733. + break;
  75734. + case UE_ISOCHRONOUS:
  75735. + /* Should never get called for isochronous transfers. */
  75736. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  75737. + break;
  75738. + }
  75739. +
  75740. +handle_nak_done:
  75741. + disable_hc_int(hc_regs, nak);
  75742. +
  75743. + return 1;
  75744. +}
  75745. +
  75746. +/**
  75747. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  75748. + * performing the PING protocol in Slave mode, when errors occur during
  75749. + * either Slave mode or DMA mode, and during Start Split transactions.
  75750. + */
  75751. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  75752. + dwc_hc_t * hc,
  75753. + dwc_otg_hc_regs_t * hc_regs,
  75754. + dwc_otg_qtd_t * qtd)
  75755. +{
  75756. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  75757. + "ACK Received--\n", hc->hc_num);
  75758. +
  75759. + if (hc->do_split) {
  75760. + /*
  75761. + * Handle ACK on SSPLIT.
  75762. + * ACK should not occur in CSPLIT.
  75763. + */
  75764. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  75765. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  75766. + }
  75767. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  75768. + /* Don't need complete for isochronous out transfers. */
  75769. + qtd->complete_split = 1;
  75770. + }
  75771. +
  75772. + /* ISOC OUT */
  75773. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  75774. + switch (hc->xact_pos) {
  75775. + case DWC_HCSPLIT_XACTPOS_ALL:
  75776. + break;
  75777. + case DWC_HCSPLIT_XACTPOS_END:
  75778. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  75779. + qtd->isoc_split_offset = 0;
  75780. + break;
  75781. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  75782. + case DWC_HCSPLIT_XACTPOS_MID:
  75783. + /*
  75784. + * For BEGIN or MID, calculate the length for
  75785. + * the next microframe to determine the correct
  75786. + * SSPLIT token, either MID or END.
  75787. + */
  75788. + {
  75789. + struct dwc_otg_hcd_iso_packet_desc
  75790. + *frame_desc;
  75791. +
  75792. + frame_desc =
  75793. + &qtd->urb->
  75794. + iso_descs[qtd->isoc_frame_index];
  75795. + qtd->isoc_split_offset += 188;
  75796. +
  75797. + if ((frame_desc->length -
  75798. + qtd->isoc_split_offset) <= 188) {
  75799. + qtd->isoc_split_pos =
  75800. + DWC_HCSPLIT_XACTPOS_END;
  75801. + } else {
  75802. + qtd->isoc_split_pos =
  75803. + DWC_HCSPLIT_XACTPOS_MID;
  75804. + }
  75805. +
  75806. + }
  75807. + break;
  75808. + }
  75809. + } else {
  75810. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  75811. + }
  75812. + } else {
  75813. + /*
  75814. + * An unmasked ACK on a non-split DMA transaction is
  75815. + * for the sole purpose of resetting error counts. Disable other
  75816. + * interrupts unmasked for the same reason.
  75817. + */
  75818. + if(hcd->core_if->dma_enable) {
  75819. + disable_hc_int(hc_regs, datatglerr);
  75820. + disable_hc_int(hc_regs, nak);
  75821. + }
  75822. + qtd->error_count = 0;
  75823. +
  75824. + if (hc->qh->ping_state) {
  75825. + hc->qh->ping_state = 0;
  75826. + /*
  75827. + * Halt the channel so the transfer can be re-started
  75828. + * from the appropriate point. This only happens in
  75829. + * Slave mode. In DMA mode, the ping_state is cleared
  75830. + * when the transfer is started because the core
  75831. + * automatically executes the PING, then the transfer.
  75832. + */
  75833. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  75834. + }
  75835. + }
  75836. +
  75837. + /*
  75838. + * If the ACK occurred when _not_ in the PING state, let the channel
  75839. + * continue transferring data after clearing the error count.
  75840. + */
  75841. +
  75842. + disable_hc_int(hc_regs, ack);
  75843. +
  75844. + return 1;
  75845. +}
  75846. +
  75847. +/**
  75848. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  75849. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  75850. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  75851. + * handled in the xfercomp interrupt handler, not here. This handler may be
  75852. + * called in either DMA mode or Slave mode.
  75853. + */
  75854. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  75855. + dwc_hc_t * hc,
  75856. + dwc_otg_hc_regs_t * hc_regs,
  75857. + dwc_otg_qtd_t * qtd)
  75858. +{
  75859. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  75860. + "NYET Received--\n", hc->hc_num);
  75861. +
  75862. + /*
  75863. + * NYET on CSPLIT
  75864. + * re-do the CSPLIT immediately on non-periodic
  75865. + */
  75866. + if (hc->do_split && hc->complete_split) {
  75867. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  75868. + && hcd->core_if->dma_enable) {
  75869. + qtd->complete_split = 0;
  75870. + qtd->isoc_split_offset = 0;
  75871. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  75872. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  75873. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  75874. + }
  75875. + else
  75876. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  75877. + goto handle_nyet_done;
  75878. + }
  75879. +
  75880. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  75881. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  75882. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  75883. +
  75884. + // With the FIQ running we only ever see the failed NYET
  75885. + if (dwc_full_frame_num(frnum) !=
  75886. + dwc_full_frame_num(hc->qh->sched_frame) ||
  75887. + fiq_split_enable) {
  75888. + /*
  75889. + * No longer in the same full speed frame.
  75890. + * Treat this as a transaction error.
  75891. + */
  75892. +#if 0
  75893. + /** @todo Fix system performance so this can
  75894. + * be treated as an error. Right now complete
  75895. + * splits cannot be scheduled precisely enough
  75896. + * due to other system activity, so this error
  75897. + * occurs regularly in Slave mode.
  75898. + */
  75899. + qtd->error_count++;
  75900. +#endif
  75901. + qtd->complete_split = 0;
  75902. + halt_channel(hcd, hc, qtd,
  75903. + DWC_OTG_HC_XFER_XACT_ERR);
  75904. + /** @todo add support for isoc release */
  75905. + goto handle_nyet_done;
  75906. + }
  75907. + }
  75908. +
  75909. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  75910. + goto handle_nyet_done;
  75911. + }
  75912. +
  75913. + hc->qh->ping_state = 1;
  75914. + qtd->error_count = 0;
  75915. +
  75916. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  75917. + DWC_OTG_HC_XFER_NYET);
  75918. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  75919. +
  75920. + /*
  75921. + * Halt the channel and re-start the transfer so the PING
  75922. + * protocol will start.
  75923. + */
  75924. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  75925. +
  75926. +handle_nyet_done:
  75927. + disable_hc_int(hc_regs, nyet);
  75928. + return 1;
  75929. +}
  75930. +
  75931. +/**
  75932. + * Handles a host channel babble interrupt. This handler may be called in
  75933. + * either DMA mode or Slave mode.
  75934. + */
  75935. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  75936. + dwc_hc_t * hc,
  75937. + dwc_otg_hc_regs_t * hc_regs,
  75938. + dwc_otg_qtd_t * qtd)
  75939. +{
  75940. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  75941. + "Babble Error--\n", hc->hc_num);
  75942. +
  75943. + if (hcd->core_if->dma_desc_enable) {
  75944. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  75945. + DWC_OTG_HC_XFER_BABBLE_ERR);
  75946. + goto handle_babble_done;
  75947. + }
  75948. +
  75949. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  75950. + hcd->fops->complete(hcd, qtd->urb->priv,
  75951. + qtd->urb, -DWC_E_OVERFLOW);
  75952. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  75953. + } else {
  75954. + dwc_otg_halt_status_e halt_status;
  75955. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  75956. + DWC_OTG_HC_XFER_BABBLE_ERR);
  75957. + halt_channel(hcd, hc, qtd, halt_status);
  75958. + }
  75959. +
  75960. +handle_babble_done:
  75961. + disable_hc_int(hc_regs, bblerr);
  75962. + return 1;
  75963. +}
  75964. +
  75965. +/**
  75966. + * Handles a host channel AHB error interrupt. This handler is only called in
  75967. + * DMA mode.
  75968. + */
  75969. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  75970. + dwc_hc_t * hc,
  75971. + dwc_otg_hc_regs_t * hc_regs,
  75972. + dwc_otg_qtd_t * qtd)
  75973. +{
  75974. + hcchar_data_t hcchar;
  75975. + hcsplt_data_t hcsplt;
  75976. + hctsiz_data_t hctsiz;
  75977. + uint32_t hcdma;
  75978. + char *pipetype, *speed;
  75979. +
  75980. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  75981. +
  75982. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  75983. + "AHB Error--\n", hc->hc_num);
  75984. +
  75985. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  75986. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  75987. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  75988. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  75989. +
  75990. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  75991. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  75992. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  75993. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  75994. + DWC_ERROR(" Device address: %d\n",
  75995. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  75996. + DWC_ERROR(" Endpoint: %d, %s\n",
  75997. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  75998. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  75999. +
  76000. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  76001. + case UE_CONTROL:
  76002. + pipetype = "CONTROL";
  76003. + break;
  76004. + case UE_BULK:
  76005. + pipetype = "BULK";
  76006. + break;
  76007. + case UE_INTERRUPT:
  76008. + pipetype = "INTERRUPT";
  76009. + break;
  76010. + case UE_ISOCHRONOUS:
  76011. + pipetype = "ISOCHRONOUS";
  76012. + break;
  76013. + default:
  76014. + pipetype = "UNKNOWN";
  76015. + break;
  76016. + }
  76017. +
  76018. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  76019. +
  76020. + switch (hc->speed) {
  76021. + case DWC_OTG_EP_SPEED_HIGH:
  76022. + speed = "HIGH";
  76023. + break;
  76024. + case DWC_OTG_EP_SPEED_FULL:
  76025. + speed = "FULL";
  76026. + break;
  76027. + case DWC_OTG_EP_SPEED_LOW:
  76028. + speed = "LOW";
  76029. + break;
  76030. + default:
  76031. + speed = "UNKNOWN";
  76032. + break;
  76033. + };
  76034. +
  76035. + DWC_ERROR(" Speed: %s\n", speed);
  76036. +
  76037. + DWC_ERROR(" Max packet size: %d\n",
  76038. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  76039. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  76040. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  76041. + urb->buf, (void *)urb->dma);
  76042. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  76043. + urb->setup_packet, (void *)urb->setup_dma);
  76044. + DWC_ERROR(" Interval: %d\n", urb->interval);
  76045. +
  76046. + /* Core haltes the channel for Descriptor DMA mode */
  76047. + if (hcd->core_if->dma_desc_enable) {
  76048. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76049. + DWC_OTG_HC_XFER_AHB_ERR);
  76050. + goto handle_ahberr_done;
  76051. + }
  76052. +
  76053. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  76054. +
  76055. + /*
  76056. + * Force a channel halt. Don't call halt_channel because that won't
  76057. + * write to the HCCHARn register in DMA mode to force the halt.
  76058. + */
  76059. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  76060. +handle_ahberr_done:
  76061. + disable_hc_int(hc_regs, ahberr);
  76062. + return 1;
  76063. +}
  76064. +
  76065. +/**
  76066. + * Handles a host channel transaction error interrupt. This handler may be
  76067. + * called in either DMA mode or Slave mode.
  76068. + */
  76069. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  76070. + dwc_hc_t * hc,
  76071. + dwc_otg_hc_regs_t * hc_regs,
  76072. + dwc_otg_qtd_t * qtd)
  76073. +{
  76074. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76075. + "Transaction Error--\n", hc->hc_num);
  76076. +
  76077. + if (hcd->core_if->dma_desc_enable) {
  76078. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76079. + DWC_OTG_HC_XFER_XACT_ERR);
  76080. + goto handle_xacterr_done;
  76081. + }
  76082. +
  76083. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76084. + case UE_CONTROL:
  76085. + case UE_BULK:
  76086. + qtd->error_count++;
  76087. + if (!hc->qh->ping_state) {
  76088. +
  76089. + update_urb_state_xfer_intr(hc, hc_regs,
  76090. + qtd->urb, qtd,
  76091. + DWC_OTG_HC_XFER_XACT_ERR);
  76092. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76093. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  76094. + hc->qh->ping_state = 1;
  76095. + }
  76096. + }
  76097. +
  76098. + /*
  76099. + * Halt the channel so the transfer can be re-started from
  76100. + * the appropriate point or the PING protocol will start.
  76101. + */
  76102. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76103. + break;
  76104. + case UE_INTERRUPT:
  76105. + qtd->error_count++;
  76106. + if (hc->do_split && hc->complete_split) {
  76107. + qtd->complete_split = 0;
  76108. + }
  76109. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76110. + break;
  76111. + case UE_ISOCHRONOUS:
  76112. + {
  76113. + dwc_otg_halt_status_e halt_status;
  76114. + halt_status =
  76115. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76116. + DWC_OTG_HC_XFER_XACT_ERR);
  76117. +
  76118. + halt_channel(hcd, hc, qtd, halt_status);
  76119. + }
  76120. + break;
  76121. + }
  76122. +handle_xacterr_done:
  76123. + disable_hc_int(hc_regs, xacterr);
  76124. +
  76125. + return 1;
  76126. +}
  76127. +
  76128. +/**
  76129. + * Handles a host channel frame overrun interrupt. This handler may be called
  76130. + * in either DMA mode or Slave mode.
  76131. + */
  76132. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  76133. + dwc_hc_t * hc,
  76134. + dwc_otg_hc_regs_t * hc_regs,
  76135. + dwc_otg_qtd_t * qtd)
  76136. +{
  76137. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76138. + "Frame Overrun--\n", hc->hc_num);
  76139. +
  76140. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  76141. + case UE_CONTROL:
  76142. + case UE_BULK:
  76143. + break;
  76144. + case UE_INTERRUPT:
  76145. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  76146. + break;
  76147. + case UE_ISOCHRONOUS:
  76148. + {
  76149. + dwc_otg_halt_status_e halt_status;
  76150. + halt_status =
  76151. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  76152. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  76153. +
  76154. + halt_channel(hcd, hc, qtd, halt_status);
  76155. + }
  76156. + break;
  76157. + }
  76158. +
  76159. + disable_hc_int(hc_regs, frmovrun);
  76160. +
  76161. + return 1;
  76162. +}
  76163. +
  76164. +/**
  76165. + * Handles a host channel data toggle error interrupt. This handler may be
  76166. + * called in either DMA mode or Slave mode.
  76167. + */
  76168. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  76169. + dwc_hc_t * hc,
  76170. + dwc_otg_hc_regs_t * hc_regs,
  76171. + dwc_otg_qtd_t * qtd)
  76172. +{
  76173. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76174. + "Data Toggle Error on %s transfer--\n",
  76175. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  76176. +
  76177. + /* Data toggles on split transactions cause the hc to halt.
  76178. + * restart transfer */
  76179. + if(hc->qh->do_split)
  76180. + {
  76181. + qtd->error_count++;
  76182. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76183. + update_urb_state_xfer_intr(hc, hc_regs,
  76184. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76185. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76186. + } else if (hc->ep_is_in) {
  76187. + /* An unmasked data toggle error on a non-split DMA transaction is
  76188. + * for the sole purpose of resetting error counts. Disable other
  76189. + * interrupts unmasked for the same reason.
  76190. + */
  76191. + if(hcd->core_if->dma_enable) {
  76192. + disable_hc_int(hc_regs, ack);
  76193. + disable_hc_int(hc_regs, nak);
  76194. + }
  76195. + qtd->error_count = 0;
  76196. + }
  76197. +
  76198. + disable_hc_int(hc_regs, datatglerr);
  76199. +
  76200. + return 1;
  76201. +}
  76202. +
  76203. +#ifdef DEBUG
  76204. +/**
  76205. + * This function is for debug only. It checks that a valid halt status is set
  76206. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  76207. + * taken and a warning is issued.
  76208. + * @return 1 if halt status is ok, 0 otherwise.
  76209. + */
  76210. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  76211. + dwc_hc_t * hc,
  76212. + dwc_otg_hc_regs_t * hc_regs,
  76213. + dwc_otg_qtd_t * qtd)
  76214. +{
  76215. + hcchar_data_t hcchar;
  76216. + hctsiz_data_t hctsiz;
  76217. + hcint_data_t hcint;
  76218. + hcintmsk_data_t hcintmsk;
  76219. + hcsplt_data_t hcsplt;
  76220. +
  76221. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  76222. + /*
  76223. + * This code is here only as a check. This condition should
  76224. + * never happen. Ignore the halt if it does occur.
  76225. + */
  76226. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76227. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76228. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  76229. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  76230. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  76231. + DWC_WARN
  76232. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  76233. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  76234. + "hcint 0x%08x, hcintmsk 0x%08x, "
  76235. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  76236. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  76237. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  76238. +
  76239. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  76240. + __func__, hc->hc_num);
  76241. + DWC_WARN("\n");
  76242. + clear_hc_int(hc_regs, chhltd);
  76243. + return 0;
  76244. + }
  76245. +
  76246. + /*
  76247. + * This code is here only as a check. hcchar.chdis should
  76248. + * never be set when the halt interrupt occurs. Halt the
  76249. + * channel again if it does occur.
  76250. + */
  76251. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76252. + if (hcchar.b.chdis) {
  76253. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  76254. + "hcchar 0x%08x, trying to halt again\n",
  76255. + __func__, hcchar.d32);
  76256. + clear_hc_int(hc_regs, chhltd);
  76257. + hc->halt_pending = 0;
  76258. + halt_channel(hcd, hc, qtd, hc->halt_status);
  76259. + return 0;
  76260. + }
  76261. +
  76262. + return 1;
  76263. +}
  76264. +#endif
  76265. +
  76266. +/**
  76267. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  76268. + * determines the reason the channel halted and proceeds accordingly.
  76269. + */
  76270. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  76271. + dwc_hc_t * hc,
  76272. + dwc_otg_hc_regs_t * hc_regs,
  76273. + dwc_otg_qtd_t * qtd,
  76274. + hcint_data_t hcint,
  76275. + hcintmsk_data_t hcintmsk)
  76276. +{
  76277. + int out_nak_enh = 0;
  76278. +
  76279. + /* For core with OUT NAK enhancement, the flow for high-
  76280. + * speed CONTROL/BULK OUT is handled a little differently.
  76281. + */
  76282. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  76283. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  76284. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  76285. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  76286. + out_nak_enh = 1;
  76287. + }
  76288. + }
  76289. +
  76290. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  76291. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  76292. + && !hcd->core_if->dma_desc_enable)) {
  76293. + /*
  76294. + * Just release the channel. A dequeue can happen on a
  76295. + * transfer timeout. In the case of an AHB Error, the channel
  76296. + * was forced to halt because there's no way to gracefully
  76297. + * recover.
  76298. + */
  76299. + if (hcd->core_if->dma_desc_enable)
  76300. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  76301. + hc->halt_status);
  76302. + else
  76303. + release_channel(hcd, hc, qtd, hc->halt_status);
  76304. + return;
  76305. + }
  76306. +
  76307. + /* Read the HCINTn register to determine the cause for the halt. */
  76308. + if(!fiq_split_enable)
  76309. + {
  76310. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  76311. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  76312. + }
  76313. +
  76314. + if (hcint.b.xfercomp) {
  76315. + /** @todo This is here because of a possible hardware bug. Spec
  76316. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  76317. + * interrupt w/ACK bit set should occur, but I only see the
  76318. + * XFERCOMP bit, even with it masked out. This is a workaround
  76319. + * for that behavior. Should fix this when hardware is fixed.
  76320. + */
  76321. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  76322. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  76323. + }
  76324. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  76325. + } else if (hcint.b.stall) {
  76326. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  76327. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  76328. + if (out_nak_enh) {
  76329. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  76330. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  76331. + qtd->error_count = 0;
  76332. + } else {
  76333. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  76334. + }
  76335. + }
  76336. +
  76337. + /*
  76338. + * Must handle xacterr before nak or ack. Could get a xacterr
  76339. + * at the same time as either of these on a BULK/CONTROL OUT
  76340. + * that started with a PING. The xacterr takes precedence.
  76341. + */
  76342. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  76343. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  76344. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  76345. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  76346. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  76347. + } else if (hcint.b.bblerr) {
  76348. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  76349. + } else if (hcint.b.frmovrun) {
  76350. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  76351. + } else if (hcint.b.datatglerr) {
  76352. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  76353. + } else if (!out_nak_enh) {
  76354. + if (hcint.b.nyet) {
  76355. + /*
  76356. + * Must handle nyet before nak or ack. Could get a nyet at the
  76357. + * same time as either of those on a BULK/CONTROL OUT that
  76358. + * started with a PING. The nyet takes precedence.
  76359. + */
  76360. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  76361. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  76362. + /*
  76363. + * If nak is not masked, it's because a non-split IN transfer
  76364. + * is in an error state. In that case, the nak is handled by
  76365. + * the nak interrupt handler, not here. Handle nak here for
  76366. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  76367. + * rewinding the buffer pointer.
  76368. + */
  76369. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  76370. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  76371. + /*
  76372. + * If ack is not masked, it's because a non-split IN transfer
  76373. + * is in an error state. In that case, the ack is handled by
  76374. + * the ack interrupt handler, not here. Handle ack here for
  76375. + * split transfers. Start splits halt on ACK.
  76376. + */
  76377. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  76378. + } else {
  76379. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  76380. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  76381. + /*
  76382. + * A periodic transfer halted with no other channel
  76383. + * interrupts set. Assume it was halted by the core
  76384. + * because it could not be completed in its scheduled
  76385. + * (micro)frame.
  76386. + */
  76387. +#ifdef DEBUG
  76388. + DWC_PRINTF
  76389. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  76390. + __func__, hc->hc_num);
  76391. +#endif
  76392. + halt_channel(hcd, hc, qtd,
  76393. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  76394. + } else {
  76395. + DWC_ERROR
  76396. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  76397. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  76398. + __func__, hc->hc_num, hcint.d32,
  76399. + DWC_READ_REG32(&hcd->
  76400. + core_if->core_global_regs->
  76401. + gintsts));
  76402. + /* Failthrough: use 3-strikes rule */
  76403. + qtd->error_count++;
  76404. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76405. + update_urb_state_xfer_intr(hc, hc_regs,
  76406. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76407. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76408. + }
  76409. +
  76410. + }
  76411. + } else {
  76412. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  76413. + hcint.d32);
  76414. + /* Failthrough: use 3-strikes rule */
  76415. + qtd->error_count++;
  76416. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  76417. + update_urb_state_xfer_intr(hc, hc_regs,
  76418. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76419. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  76420. + }
  76421. +}
  76422. +
  76423. +/**
  76424. + * Handles a host channel Channel Halted interrupt.
  76425. + *
  76426. + * In slave mode, this handler is called only when the driver specifically
  76427. + * requests a halt. This occurs during handling other host channel interrupts
  76428. + * (e.g. nak, xacterr, stall, nyet, etc.).
  76429. + *
  76430. + * In DMA mode, this is the interrupt that occurs when the core has finished
  76431. + * processing a transfer on a channel. Other host channel interrupts (except
  76432. + * ahberr) are disabled in DMA mode.
  76433. + */
  76434. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  76435. + dwc_hc_t * hc,
  76436. + dwc_otg_hc_regs_t * hc_regs,
  76437. + dwc_otg_qtd_t * qtd,
  76438. + hcint_data_t hcint,
  76439. + hcintmsk_data_t hcintmsk)
  76440. +{
  76441. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  76442. + "Channel Halted--\n", hc->hc_num);
  76443. +
  76444. + if (hcd->core_if->dma_enable) {
  76445. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd, hcint, hcintmsk);
  76446. + } else {
  76447. +#ifdef DEBUG
  76448. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  76449. + return 1;
  76450. + }
  76451. +#endif
  76452. + release_channel(hcd, hc, qtd, hc->halt_status);
  76453. + }
  76454. +
  76455. + return 1;
  76456. +}
  76457. +
  76458. +/** Handles interrupt for a specific Host Channel */
  76459. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  76460. +{
  76461. + int retval = 0;
  76462. + hcint_data_t hcint, hcint_orig;
  76463. + hcintmsk_data_t hcintmsk;
  76464. + dwc_hc_t *hc;
  76465. + dwc_otg_hc_regs_t *hc_regs;
  76466. + dwc_otg_qtd_t *qtd;
  76467. +
  76468. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  76469. +
  76470. + hc = dwc_otg_hcd->hc_ptr_array[num];
  76471. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  76472. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  76473. + /* We are responding to a channel disable. Driver
  76474. + * state is cleared - our qtd has gone away.
  76475. + */
  76476. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  76477. + return 1;
  76478. + }
  76479. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  76480. +
  76481. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  76482. + hcint_orig = hcint;
  76483. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  76484. + DWC_DEBUGPL(DBG_HCDV,
  76485. + " hcint 0x%08x, hcintmsk 0x%08x, hcint&hcintmsk 0x%08x\n",
  76486. + hcint.d32, hcintmsk.d32, (hcint.d32 & hcintmsk.d32));
  76487. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  76488. +
  76489. + if(fiq_split_enable)
  76490. + {
  76491. + // replace with the saved interrupts from the fiq handler
  76492. + local_fiq_disable();
  76493. + hcint_orig.d32 = hcint_saved[num].d32;
  76494. + hcint.d32 = hcint_orig.d32 & hcintmsk_saved[num].d32;
  76495. + hcint_saved[num].d32 = 0;
  76496. + local_fiq_enable();
  76497. + }
  76498. +
  76499. + if (!dwc_otg_hcd->core_if->dma_enable) {
  76500. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  76501. + hcint.b.chhltd = 0;
  76502. + }
  76503. + }
  76504. +
  76505. + if (hcint.b.xfercomp) {
  76506. + retval |=
  76507. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76508. + /*
  76509. + * If NYET occurred at same time as Xfer Complete, the NYET is
  76510. + * handled by the Xfer Complete interrupt handler. Don't want
  76511. + * to call the NYET interrupt handler in this case.
  76512. + */
  76513. + hcint.b.nyet = 0;
  76514. + }
  76515. + if (hcint.b.chhltd) {
  76516. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd, hcint_orig, hcintmsk_saved[num]);
  76517. + }
  76518. + if (hcint.b.ahberr) {
  76519. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76520. + }
  76521. + if (hcint.b.stall) {
  76522. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76523. + }
  76524. + if (hcint.b.nak) {
  76525. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76526. + }
  76527. + if (hcint.b.ack) {
  76528. + if(!hcint.b.chhltd)
  76529. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76530. + }
  76531. + if (hcint.b.nyet) {
  76532. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76533. + }
  76534. + if (hcint.b.xacterr) {
  76535. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76536. + }
  76537. + if (hcint.b.bblerr) {
  76538. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76539. + }
  76540. + if (hcint.b.frmovrun) {
  76541. + retval |=
  76542. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76543. + }
  76544. + if (hcint.b.datatglerr) {
  76545. + retval |=
  76546. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  76547. + }
  76548. +
  76549. + return retval;
  76550. +}
  76551. +#endif /* DWC_DEVICE_ONLY */
  76552. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  76553. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 1970-01-01 01:00:00.000000000 +0100
  76554. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 2014-03-11 16:55:38.000000000 +0100
  76555. @@ -0,0 +1,972 @@
  76556. +
  76557. +/* ==========================================================================
  76558. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  76559. + * $Revision: #20 $
  76560. + * $Date: 2011/10/26 $
  76561. + * $Change: 1872981 $
  76562. + *
  76563. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  76564. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  76565. + * otherwise expressly agreed to in writing between Synopsys and you.
  76566. + *
  76567. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  76568. + * any End User Software License Agreement or Agreement for Licensed Product
  76569. + * with Synopsys or any supplement thereto. You are permitted to use and
  76570. + * redistribute this Software in source and binary forms, with or without
  76571. + * modification, provided that redistributions of source code must retain this
  76572. + * notice. You may not view, use, disclose, copy or distribute this file or
  76573. + * any information contained herein except pursuant to this license grant from
  76574. + * Synopsys. If you do not agree with this notice, including the disclaimer
  76575. + * below, then you are not authorized to use the Software.
  76576. + *
  76577. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  76578. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  76579. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76580. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  76581. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  76582. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  76583. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  76584. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  76585. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  76586. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  76587. + * DAMAGE.
  76588. + * ========================================================================== */
  76589. +#ifndef DWC_DEVICE_ONLY
  76590. +
  76591. +/**
  76592. + * @file
  76593. + *
  76594. + * This file contains the implementation of the HCD. In Linux, the HCD
  76595. + * implements the hc_driver API.
  76596. + */
  76597. +#include <linux/kernel.h>
  76598. +#include <linux/module.h>
  76599. +#include <linux/moduleparam.h>
  76600. +#include <linux/init.h>
  76601. +#include <linux/device.h>
  76602. +#include <linux/errno.h>
  76603. +#include <linux/list.h>
  76604. +#include <linux/interrupt.h>
  76605. +#include <linux/string.h>
  76606. +#include <linux/dma-mapping.h>
  76607. +#include <linux/version.h>
  76608. +#include <asm/io.h>
  76609. +#include <asm/fiq.h>
  76610. +#include <linux/usb.h>
  76611. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  76612. +#include <../drivers/usb/core/hcd.h>
  76613. +#else
  76614. +#include <linux/usb/hcd.h>
  76615. +#endif
  76616. +
  76617. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  76618. +#define USB_URB_EP_LINKING 1
  76619. +#else
  76620. +#define USB_URB_EP_LINKING 0
  76621. +#endif
  76622. +
  76623. +#include "dwc_otg_hcd_if.h"
  76624. +#include "dwc_otg_dbg.h"
  76625. +#include "dwc_otg_driver.h"
  76626. +#include "dwc_otg_hcd.h"
  76627. +#include "dwc_otg_mphi_fix.h"
  76628. +
  76629. +/**
  76630. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  76631. + * qualified with its direction (possible 32 endpoints per device).
  76632. + */
  76633. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  76634. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  76635. +
  76636. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  76637. +
  76638. +extern bool fiq_fix_enable;
  76639. +
  76640. +/** @name Linux HC Driver API Functions */
  76641. +/** @{ */
  76642. +/* manage i/o requests, device state */
  76643. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  76644. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  76645. + struct usb_host_endpoint *ep,
  76646. +#endif
  76647. + struct urb *urb, gfp_t mem_flags);
  76648. +
  76649. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  76650. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  76651. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  76652. +#endif
  76653. +#else /* kernels at or post 2.6.30 */
  76654. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  76655. + struct urb *urb, int status);
  76656. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  76657. +
  76658. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  76659. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  76660. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  76661. +#endif
  76662. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  76663. +extern int hcd_start(struct usb_hcd *hcd);
  76664. +extern void hcd_stop(struct usb_hcd *hcd);
  76665. +static int get_frame_number(struct usb_hcd *hcd);
  76666. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  76667. +extern int hub_control(struct usb_hcd *hcd,
  76668. + u16 typeReq,
  76669. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  76670. +
  76671. +struct wrapper_priv_data {
  76672. + dwc_otg_hcd_t *dwc_otg_hcd;
  76673. +};
  76674. +
  76675. +/** @} */
  76676. +
  76677. +static struct hc_driver dwc_otg_hc_driver = {
  76678. +
  76679. + .description = dwc_otg_hcd_name,
  76680. + .product_desc = "DWC OTG Controller",
  76681. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  76682. +
  76683. + .irq = dwc_otg_hcd_irq,
  76684. +
  76685. + .flags = HCD_MEMORY | HCD_USB2,
  76686. +
  76687. + //.reset =
  76688. + .start = hcd_start,
  76689. + //.suspend =
  76690. + //.resume =
  76691. + .stop = hcd_stop,
  76692. +
  76693. + .urb_enqueue = dwc_otg_urb_enqueue,
  76694. + .urb_dequeue = dwc_otg_urb_dequeue,
  76695. + .endpoint_disable = endpoint_disable,
  76696. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  76697. + .endpoint_reset = endpoint_reset,
  76698. +#endif
  76699. + .get_frame_number = get_frame_number,
  76700. +
  76701. + .hub_status_data = hub_status_data,
  76702. + .hub_control = hub_control,
  76703. + //.bus_suspend =
  76704. + //.bus_resume =
  76705. +};
  76706. +
  76707. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  76708. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  76709. +{
  76710. + struct wrapper_priv_data *p;
  76711. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  76712. + return p->dwc_otg_hcd;
  76713. +}
  76714. +
  76715. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  76716. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  76717. +{
  76718. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  76719. +}
  76720. +
  76721. +/** Gets the usb_host_endpoint associated with an URB. */
  76722. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  76723. +{
  76724. + struct usb_device *dev = urb->dev;
  76725. + int ep_num = usb_pipeendpoint(urb->pipe);
  76726. +
  76727. + if (usb_pipein(urb->pipe))
  76728. + return dev->ep_in[ep_num];
  76729. + else
  76730. + return dev->ep_out[ep_num];
  76731. +}
  76732. +
  76733. +static int _disconnect(dwc_otg_hcd_t * hcd)
  76734. +{
  76735. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  76736. +
  76737. + usb_hcd->self.is_b_host = 0;
  76738. + return 0;
  76739. +}
  76740. +
  76741. +static int _start(dwc_otg_hcd_t * hcd)
  76742. +{
  76743. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  76744. +
  76745. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  76746. + hcd_start(usb_hcd);
  76747. +
  76748. + return 0;
  76749. +}
  76750. +
  76751. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  76752. + uint32_t * port_addr)
  76753. +{
  76754. + struct urb *urb = (struct urb *)urb_handle;
  76755. + struct usb_bus *bus;
  76756. +#if 1 //GRAYG - temporary
  76757. + if (NULL == urb_handle)
  76758. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  76759. + if (NULL == urb->dev)
  76760. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  76761. + if (NULL == port_addr)
  76762. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  76763. +#endif
  76764. + if (urb->dev->tt) {
  76765. + if (NULL == urb->dev->tt->hub) {
  76766. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  76767. + __func__); //GRAYG
  76768. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  76769. + *hub_addr = 0; //GRAYG
  76770. + // we probably shouldn't have a transaction translator if
  76771. + // there's no associated hub?
  76772. + } else {
  76773. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  76774. + if (urb->dev->tt->hub == bus->root_hub)
  76775. + *hub_addr = 0;
  76776. + else
  76777. + *hub_addr = urb->dev->tt->hub->devnum;
  76778. + }
  76779. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  76780. + } else {
  76781. + *hub_addr = 0;
  76782. + *port_addr = urb->dev->ttport;
  76783. + }
  76784. + return 0;
  76785. +}
  76786. +
  76787. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  76788. +{
  76789. + struct urb *urb = (struct urb *)urb_handle;
  76790. + return urb->dev->speed;
  76791. +}
  76792. +
  76793. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  76794. +{
  76795. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  76796. + return usb_hcd->self.b_hnp_enable;
  76797. +}
  76798. +
  76799. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  76800. + struct urb *urb)
  76801. +{
  76802. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  76803. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  76804. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  76805. + } else {
  76806. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  76807. + }
  76808. +}
  76809. +
  76810. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  76811. + struct urb *urb)
  76812. +{
  76813. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  76814. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  76815. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  76816. + } else {
  76817. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  76818. + }
  76819. +}
  76820. +
  76821. +/**
  76822. + * Sets the final status of an URB and returns it to the device driver. Any
  76823. + * required cleanup of the URB is performed. The HCD lock should be held on
  76824. + * entry.
  76825. + */
  76826. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  76827. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  76828. +{
  76829. + struct urb *urb = (struct urb *)urb_handle;
  76830. + urb_tq_entry_t *new_entry;
  76831. + int rc = 0;
  76832. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  76833. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  76834. + __func__, urb, usb_pipedevice(urb->pipe),
  76835. + usb_pipeendpoint(urb->pipe),
  76836. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  76837. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  76838. + int i;
  76839. + for (i = 0; i < urb->number_of_packets; i++) {
  76840. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  76841. + i, urb->iso_frame_desc[i].status);
  76842. + }
  76843. + }
  76844. + }
  76845. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  76846. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  76847. + /* Convert status value. */
  76848. + switch (status) {
  76849. + case -DWC_E_PROTOCOL:
  76850. + status = -EPROTO;
  76851. + break;
  76852. + case -DWC_E_IN_PROGRESS:
  76853. + status = -EINPROGRESS;
  76854. + break;
  76855. + case -DWC_E_PIPE:
  76856. + status = -EPIPE;
  76857. + break;
  76858. + case -DWC_E_IO:
  76859. + status = -EIO;
  76860. + break;
  76861. + case -DWC_E_TIMEOUT:
  76862. + status = -ETIMEDOUT;
  76863. + break;
  76864. + case -DWC_E_OVERFLOW:
  76865. + status = -EOVERFLOW;
  76866. + break;
  76867. + case -DWC_E_SHUTDOWN:
  76868. + status = -ESHUTDOWN;
  76869. + break;
  76870. + default:
  76871. + if (status) {
  76872. + DWC_PRINTF("Uknown urb status %d\n", status);
  76873. +
  76874. + }
  76875. + }
  76876. +
  76877. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  76878. + int i;
  76879. +
  76880. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  76881. + for (i = 0; i < urb->number_of_packets; ++i) {
  76882. + urb->iso_frame_desc[i].actual_length =
  76883. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  76884. + (dwc_otg_urb, i);
  76885. + urb->iso_frame_desc[i].status =
  76886. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  76887. + }
  76888. + }
  76889. +
  76890. + urb->status = status;
  76891. + urb->hcpriv = NULL;
  76892. + if (!status) {
  76893. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  76894. + (urb->actual_length < urb->transfer_buffer_length)) {
  76895. + urb->status = -EREMOTEIO;
  76896. + }
  76897. + }
  76898. +
  76899. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  76900. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  76901. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  76902. + if (ep) {
  76903. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  76904. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  76905. + ep->hcpriv),
  76906. + urb);
  76907. + }
  76908. + }
  76909. +
  76910. + DWC_FREE(dwc_otg_urb);
  76911. + if (!new_entry) {
  76912. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  76913. + urb->status = -EPROTO;
  76914. + /* don't schedule the tasklet -
  76915. + * directly return the packet here with error. */
  76916. +#if USB_URB_EP_LINKING
  76917. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  76918. +#endif
  76919. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  76920. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  76921. +#else
  76922. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  76923. +#endif
  76924. + } else {
  76925. + new_entry->urb = urb;
  76926. +#if USB_URB_EP_LINKING
  76927. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  76928. + if(0 == rc) {
  76929. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  76930. + }
  76931. +#endif
  76932. + if(0 == rc) {
  76933. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  76934. + urb_tq_entries);
  76935. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  76936. + }
  76937. + }
  76938. + return 0;
  76939. +}
  76940. +
  76941. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  76942. + .start = _start,
  76943. + .disconnect = _disconnect,
  76944. + .hub_info = _hub_info,
  76945. + .speed = _speed,
  76946. + .complete = _complete,
  76947. + .get_b_hnp_enable = _get_b_hnp_enable,
  76948. +};
  76949. +
  76950. +static struct fiq_handler fh = {
  76951. + .name = "usb_fiq",
  76952. +};
  76953. +struct fiq_stack_s {
  76954. + int magic1;
  76955. + uint8_t stack[2048];
  76956. + int magic2;
  76957. +} fiq_stack;
  76958. +
  76959. +extern mphi_regs_t c_mphi_regs;
  76960. +/**
  76961. + * Initializes the HCD. This function allocates memory for and initializes the
  76962. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  76963. + * USB bus with the core and calls the hc_driver->start() function. It returns
  76964. + * a negative error on failure.
  76965. + */
  76966. +int hcd_init(dwc_bus_dev_t *_dev)
  76967. +{
  76968. + struct usb_hcd *hcd = NULL;
  76969. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  76970. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  76971. + int retval = 0;
  76972. + u64 dmamask;
  76973. + struct pt_regs regs;
  76974. +
  76975. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  76976. +
  76977. + /* Set device flags indicating whether the HCD supports DMA. */
  76978. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  76979. + dmamask = DMA_BIT_MASK(32);
  76980. + else
  76981. + dmamask = 0;
  76982. +
  76983. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  76984. + dma_set_mask(&_dev->dev, dmamask);
  76985. + dma_set_coherent_mask(&_dev->dev, dmamask);
  76986. +#elif defined(PCI_INTERFACE)
  76987. + pci_set_dma_mask(_dev, dmamask);
  76988. + pci_set_consistent_dma_mask(_dev, dmamask);
  76989. +#endif
  76990. +
  76991. + if (fiq_fix_enable)
  76992. + {
  76993. + // Set up fiq
  76994. + claim_fiq(&fh);
  76995. + set_fiq_handler(__FIQ_Branch, 4);
  76996. + memset(&regs,0,sizeof(regs));
  76997. + regs.ARM_r8 = (long)dwc_otg_hcd_handle_fiq;
  76998. + regs.ARM_r9 = (long)0;
  76999. + regs.ARM_sp = (long)fiq_stack.stack + sizeof(fiq_stack.stack) - 4;
  77000. + set_fiq_regs(&regs);
  77001. + fiq_stack.magic1 = 0xdeadbeef;
  77002. + fiq_stack.magic2 = 0xaa995566;
  77003. + }
  77004. +
  77005. + /*
  77006. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  77007. + * Initialize the base HCD.
  77008. + */
  77009. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  77010. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  77011. +#else
  77012. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  77013. + hcd->has_tt = 1;
  77014. +// hcd->uses_new_polling = 1;
  77015. +// hcd->poll_rh = 0;
  77016. +#endif
  77017. + if (!hcd) {
  77018. + retval = -ENOMEM;
  77019. + goto error1;
  77020. + }
  77021. +
  77022. + hcd->regs = otg_dev->os_dep.base;
  77023. +
  77024. + if (fiq_fix_enable)
  77025. + {
  77026. + volatile extern void *dwc_regs_base;
  77027. +
  77028. + //Set the mphi periph to the required registers
  77029. + c_mphi_regs.base = otg_dev->os_dep.mphi_base;
  77030. + c_mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  77031. + c_mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  77032. + c_mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  77033. + c_mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  77034. +
  77035. + dwc_regs_base = otg_dev->os_dep.base;
  77036. +
  77037. + //Enable mphi peripheral
  77038. + writel((1<<31),c_mphi_regs.ctrl);
  77039. +#ifdef DEBUG
  77040. + if (readl(c_mphi_regs.ctrl) & 0x80000000)
  77041. + DWC_DEBUGPL(DBG_USER, "MPHI periph has been enabled\n");
  77042. + else
  77043. + DWC_DEBUGPL(DBG_USER, "MPHI periph has NOT been enabled\n");
  77044. +#endif
  77045. + // Enable FIQ interrupt from USB peripheral
  77046. + enable_fiq(INTERRUPT_VC_USB);
  77047. + }
  77048. + /* Initialize the DWC OTG HCD. */
  77049. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  77050. + if (!dwc_otg_hcd) {
  77051. + goto error2;
  77052. + }
  77053. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  77054. + dwc_otg_hcd;
  77055. + otg_dev->hcd = dwc_otg_hcd;
  77056. +
  77057. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  77058. + goto error2;
  77059. + }
  77060. +
  77061. + otg_dev->hcd->otg_dev = otg_dev;
  77062. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  77063. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  77064. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  77065. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  77066. +#endif
  77067. + /* Don't support SG list at this point */
  77068. + hcd->self.sg_tablesize = 0;
  77069. +#endif
  77070. + /*
  77071. + * Finish generic HCD initialization and start the HCD. This function
  77072. + * allocates the DMA buffer pool, registers the USB bus, requests the
  77073. + * IRQ line, and calls hcd_start method.
  77074. + */
  77075. +#ifdef PLATFORM_INTERFACE
  77076. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, 0), IRQF_SHARED | IRQF_DISABLED);
  77077. +#else
  77078. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED | IRQF_DISABLED);
  77079. +#endif
  77080. + if (retval < 0) {
  77081. + goto error2;
  77082. + }
  77083. +
  77084. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  77085. + return 0;
  77086. +
  77087. +error2:
  77088. + usb_put_hcd(hcd);
  77089. +error1:
  77090. + return retval;
  77091. +}
  77092. +
  77093. +/**
  77094. + * Removes the HCD.
  77095. + * Frees memory and resources associated with the HCD and deregisters the bus.
  77096. + */
  77097. +void hcd_remove(dwc_bus_dev_t *_dev)
  77098. +{
  77099. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  77100. + dwc_otg_hcd_t *dwc_otg_hcd;
  77101. + struct usb_hcd *hcd;
  77102. +
  77103. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  77104. +
  77105. + if (!otg_dev) {
  77106. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  77107. + return;
  77108. + }
  77109. +
  77110. + dwc_otg_hcd = otg_dev->hcd;
  77111. +
  77112. + if (!dwc_otg_hcd) {
  77113. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  77114. + return;
  77115. + }
  77116. +
  77117. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  77118. +
  77119. + if (!hcd) {
  77120. + DWC_DEBUGPL(DBG_ANY,
  77121. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  77122. + __func__);
  77123. + return;
  77124. + }
  77125. + usb_remove_hcd(hcd);
  77126. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  77127. + dwc_otg_hcd_remove(dwc_otg_hcd);
  77128. + usb_put_hcd(hcd);
  77129. +}
  77130. +
  77131. +/* =========================================================================
  77132. + * Linux HC Driver Functions
  77133. + * ========================================================================= */
  77134. +
  77135. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  77136. + * mode operation. Activates the root port. Returns 0 on success and a negative
  77137. + * error code on failure. */
  77138. +int hcd_start(struct usb_hcd *hcd)
  77139. +{
  77140. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77141. + struct usb_bus *bus;
  77142. +
  77143. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  77144. + bus = hcd_to_bus(hcd);
  77145. +
  77146. + hcd->state = HC_STATE_RUNNING;
  77147. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  77148. + return 0;
  77149. + }
  77150. +
  77151. + /* Initialize and connect root hub if one is not already attached */
  77152. + if (bus->root_hub) {
  77153. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  77154. + /* Inform the HUB driver to resume. */
  77155. + usb_hcd_resume_root_hub(hcd);
  77156. + }
  77157. +
  77158. + return 0;
  77159. +}
  77160. +
  77161. +/**
  77162. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  77163. + * stopped.
  77164. + */
  77165. +void hcd_stop(struct usb_hcd *hcd)
  77166. +{
  77167. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77168. +
  77169. + dwc_otg_hcd_stop(dwc_otg_hcd);
  77170. +}
  77171. +
  77172. +/** Returns the current frame number. */
  77173. +static int get_frame_number(struct usb_hcd *hcd)
  77174. +{
  77175. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77176. +
  77177. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  77178. +}
  77179. +
  77180. +#ifdef DEBUG
  77181. +static void dump_urb_info(struct urb *urb, char *fn_name)
  77182. +{
  77183. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  77184. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  77185. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  77186. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  77187. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  77188. + char *pipetype;
  77189. + switch (usb_pipetype(urb->pipe)) {
  77190. +case PIPE_CONTROL:
  77191. +pipetype = "CONTROL"; break; case PIPE_BULK:
  77192. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  77193. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  77194. +pipetype = "ISOCHRONOUS"; break; default:
  77195. + pipetype = "UNKNOWN"; break;};
  77196. + pipetype;}
  77197. + )) ;
  77198. + DWC_PRINTF(" Speed: %s\n", ( {
  77199. + char *speed; switch (urb->dev->speed) {
  77200. +case USB_SPEED_HIGH:
  77201. +speed = "HIGH"; break; case USB_SPEED_FULL:
  77202. +speed = "FULL"; break; case USB_SPEED_LOW:
  77203. +speed = "LOW"; break; default:
  77204. + speed = "UNKNOWN"; break;};
  77205. + speed;}
  77206. + )) ;
  77207. + DWC_PRINTF(" Max packet size: %d\n",
  77208. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  77209. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  77210. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  77211. + urb->transfer_buffer, (void *)urb->transfer_dma);
  77212. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  77213. + urb->setup_packet, (void *)urb->setup_dma);
  77214. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  77215. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  77216. + int i;
  77217. + for (i = 0; i < urb->number_of_packets; i++) {
  77218. + DWC_PRINTF(" ISO Desc %d:\n", i);
  77219. + DWC_PRINTF(" offset: %d, length %d\n",
  77220. + urb->iso_frame_desc[i].offset,
  77221. + urb->iso_frame_desc[i].length);
  77222. + }
  77223. + }
  77224. +}
  77225. +#endif
  77226. +
  77227. +/** Starts processing a USB transfer request specified by a USB Request Block
  77228. + * (URB). mem_flags indicates the type of memory allocation to use while
  77229. + * processing this URB. */
  77230. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  77231. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77232. + struct usb_host_endpoint *ep,
  77233. +#endif
  77234. + struct urb *urb, gfp_t mem_flags)
  77235. +{
  77236. + int retval = 0;
  77237. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  77238. + struct usb_host_endpoint *ep = urb->ep;
  77239. +#endif
  77240. + dwc_irqflags_t irqflags;
  77241. + void **ref_ep_hcpriv = &ep->hcpriv;
  77242. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77243. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  77244. + int i;
  77245. + int alloc_bandwidth = 0;
  77246. + uint8_t ep_type = 0;
  77247. + uint32_t flags = 0;
  77248. + void *buf;
  77249. +
  77250. +#ifdef DEBUG
  77251. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  77252. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  77253. + }
  77254. +#endif
  77255. +
  77256. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  77257. + return -EINVAL;
  77258. +
  77259. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  77260. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  77261. + if (!dwc_otg_hcd_is_bandwidth_allocated
  77262. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  77263. + alloc_bandwidth = 1;
  77264. + }
  77265. + }
  77266. +
  77267. + switch (usb_pipetype(urb->pipe)) {
  77268. + case PIPE_CONTROL:
  77269. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  77270. + break;
  77271. + case PIPE_ISOCHRONOUS:
  77272. + ep_type = USB_ENDPOINT_XFER_ISOC;
  77273. + break;
  77274. + case PIPE_BULK:
  77275. + ep_type = USB_ENDPOINT_XFER_BULK;
  77276. + break;
  77277. + case PIPE_INTERRUPT:
  77278. + ep_type = USB_ENDPOINT_XFER_INT;
  77279. + break;
  77280. + default:
  77281. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  77282. + }
  77283. +
  77284. + /* # of packets is often 0 - do we really need to call this then? */
  77285. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  77286. + urb->number_of_packets,
  77287. + mem_flags == GFP_ATOMIC ? 1 : 0);
  77288. +
  77289. + if(dwc_otg_urb == NULL)
  77290. + return -ENOMEM;
  77291. +
  77292. + if (!dwc_otg_urb && urb->number_of_packets)
  77293. + return -ENOMEM;
  77294. +
  77295. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  77296. + usb_pipeendpoint(urb->pipe), ep_type,
  77297. + usb_pipein(urb->pipe),
  77298. + usb_maxpacket(urb->dev, urb->pipe,
  77299. + !(usb_pipein(urb->pipe))));
  77300. +
  77301. + buf = urb->transfer_buffer;
  77302. + if (hcd->self.uses_dma) {
  77303. + /*
  77304. + * Calculate virtual address from physical address,
  77305. + * because some class driver may not fill transfer_buffer.
  77306. + * In Buffer DMA mode virual address is used,
  77307. + * when handling non DWORD aligned buffers.
  77308. + */
  77309. + //buf = phys_to_virt(urb->transfer_dma);
  77310. + // DMA addresses are bus addresses not physical addresses!
  77311. + buf = dma_to_virt(&urb->dev->dev, urb->transfer_dma);
  77312. + }
  77313. +
  77314. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  77315. + flags |= URB_GIVEBACK_ASAP;
  77316. + if (urb->transfer_flags & URB_ZERO_PACKET)
  77317. + flags |= URB_SEND_ZERO_PACKET;
  77318. +
  77319. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  77320. + urb->transfer_dma,
  77321. + urb->transfer_buffer_length,
  77322. + urb->setup_packet,
  77323. + urb->setup_dma, flags, urb->interval);
  77324. +
  77325. + for (i = 0; i < urb->number_of_packets; ++i) {
  77326. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  77327. + urb->
  77328. + iso_frame_desc[i].offset,
  77329. + urb->
  77330. + iso_frame_desc[i].length);
  77331. + }
  77332. +
  77333. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  77334. + urb->hcpriv = dwc_otg_urb;
  77335. +#if USB_URB_EP_LINKING
  77336. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  77337. + if (0 == retval)
  77338. +#endif
  77339. + {
  77340. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  77341. + /*(dwc_otg_qh_t **)*/
  77342. + ref_ep_hcpriv, 1);
  77343. + if (0 == retval) {
  77344. + if (alloc_bandwidth) {
  77345. + allocate_bus_bandwidth(hcd,
  77346. + dwc_otg_hcd_get_ep_bandwidth(
  77347. + dwc_otg_hcd, *ref_ep_hcpriv),
  77348. + urb);
  77349. + }
  77350. + } else {
  77351. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  77352. +#if USB_URB_EP_LINKING
  77353. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  77354. +#endif
  77355. + DWC_FREE(dwc_otg_urb);
  77356. + urb->hcpriv = NULL;
  77357. + if (retval == -DWC_E_NO_DEVICE)
  77358. + retval = -ENODEV;
  77359. + }
  77360. + }
  77361. +#if USB_URB_EP_LINKING
  77362. + else
  77363. + {
  77364. + DWC_FREE(dwc_otg_urb);
  77365. + urb->hcpriv = NULL;
  77366. + }
  77367. +#endif
  77368. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  77369. + return retval;
  77370. +}
  77371. +
  77372. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  77373. + * success. */
  77374. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77375. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  77376. +#else
  77377. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  77378. +#endif
  77379. +{
  77380. + dwc_irqflags_t flags;
  77381. + dwc_otg_hcd_t *dwc_otg_hcd;
  77382. + int rc;
  77383. +
  77384. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  77385. +
  77386. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77387. +
  77388. +#ifdef DEBUG
  77389. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  77390. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  77391. + }
  77392. +#endif
  77393. +
  77394. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  77395. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  77396. + if (0 == rc) {
  77397. + if(urb->hcpriv != NULL) {
  77398. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  77399. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  77400. +
  77401. + DWC_FREE(urb->hcpriv);
  77402. + urb->hcpriv = NULL;
  77403. + }
  77404. + }
  77405. +
  77406. + if (0 == rc) {
  77407. + /* Higher layer software sets URB status. */
  77408. +#if USB_URB_EP_LINKING
  77409. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  77410. +#endif
  77411. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  77412. +
  77413. +
  77414. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  77415. + usb_hcd_giveback_urb(hcd, urb);
  77416. +#else
  77417. + usb_hcd_giveback_urb(hcd, urb, status);
  77418. +#endif
  77419. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  77420. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  77421. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  77422. + }
  77423. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  77424. + } else {
  77425. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  77426. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  77427. + rc);
  77428. + }
  77429. +
  77430. + return rc;
  77431. +}
  77432. +
  77433. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  77434. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  77435. + * must already be dequeued. */
  77436. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  77437. +{
  77438. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77439. +
  77440. + DWC_DEBUGPL(DBG_HCD,
  77441. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  77442. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  77443. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  77444. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  77445. + ep->hcpriv = NULL;
  77446. +}
  77447. +
  77448. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  77449. +/* Resets endpoint specific parameter values, in current version used to reset
  77450. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  77451. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  77452. +{
  77453. + dwc_irqflags_t flags;
  77454. + struct usb_device *udev = NULL;
  77455. + int epnum = usb_endpoint_num(&ep->desc);
  77456. + int is_out = usb_endpoint_dir_out(&ep->desc);
  77457. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  77458. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77459. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  77460. +
  77461. + if (dev)
  77462. + udev = to_usb_device(dev);
  77463. + else
  77464. + return;
  77465. +
  77466. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  77467. +
  77468. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  77469. + usb_settoggle(udev, epnum, is_out, 0);
  77470. + if (is_control)
  77471. + usb_settoggle(udev, epnum, !is_out, 0);
  77472. +
  77473. + if (ep->hcpriv) {
  77474. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  77475. + }
  77476. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  77477. +}
  77478. +#endif
  77479. +
  77480. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  77481. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  77482. + * interrupt.
  77483. + *
  77484. + * This function is called by the USB core when an interrupt occurs */
  77485. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  77486. +{
  77487. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77488. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  77489. + if (retval != 0) {
  77490. + S3C2410X_CLEAR_EINTPEND();
  77491. + }
  77492. + return IRQ_RETVAL(retval);
  77493. +}
  77494. +
  77495. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  77496. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  77497. + * is the status change indicator for the single root port. Returns 1 if either
  77498. + * change indicator is 1, otherwise returns 0. */
  77499. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  77500. +{
  77501. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  77502. +
  77503. + buf[0] = 0;
  77504. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  77505. +
  77506. + return (buf[0] != 0);
  77507. +}
  77508. +
  77509. +/** Handles hub class-specific requests. */
  77510. +int hub_control(struct usb_hcd *hcd,
  77511. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  77512. +{
  77513. + int retval;
  77514. +
  77515. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  77516. + typeReq, wValue, wIndex, buf, wLength);
  77517. +
  77518. + switch (retval) {
  77519. + case -DWC_E_INVALID:
  77520. + retval = -EINVAL;
  77521. + break;
  77522. + }
  77523. +
  77524. + return retval;
  77525. +}
  77526. +
  77527. +#endif /* DWC_DEVICE_ONLY */
  77528. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  77529. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 1970-01-01 01:00:00.000000000 +0100
  77530. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 2014-03-11 16:55:38.000000000 +0100
  77531. @@ -0,0 +1,959 @@
  77532. +/* ==========================================================================
  77533. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  77534. + * $Revision: #44 $
  77535. + * $Date: 2011/10/26 $
  77536. + * $Change: 1873028 $
  77537. + *
  77538. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  77539. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  77540. + * otherwise expressly agreed to in writing between Synopsys and you.
  77541. + *
  77542. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  77543. + * any End User Software License Agreement or Agreement for Licensed Product
  77544. + * with Synopsys or any supplement thereto. You are permitted to use and
  77545. + * redistribute this Software in source and binary forms, with or without
  77546. + * modification, provided that redistributions of source code must retain this
  77547. + * notice. You may not view, use, disclose, copy or distribute this file or
  77548. + * any information contained herein except pursuant to this license grant from
  77549. + * Synopsys. If you do not agree with this notice, including the disclaimer
  77550. + * below, then you are not authorized to use the Software.
  77551. + *
  77552. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  77553. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  77554. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  77555. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  77556. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  77557. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  77558. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  77559. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  77560. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  77561. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  77562. + * DAMAGE.
  77563. + * ========================================================================== */
  77564. +#ifndef DWC_DEVICE_ONLY
  77565. +
  77566. +/**
  77567. + * @file
  77568. + *
  77569. + * This file contains the functions to manage Queue Heads and Queue
  77570. + * Transfer Descriptors.
  77571. + */
  77572. +
  77573. +#include "dwc_otg_hcd.h"
  77574. +#include "dwc_otg_regs.h"
  77575. +#include "dwc_otg_mphi_fix.h"
  77576. +
  77577. +extern bool microframe_schedule;
  77578. +
  77579. +/**
  77580. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  77581. + * removed from a list. QTD list should already be empty if called from URB
  77582. + * Dequeue.
  77583. + *
  77584. + * @param hcd HCD instance.
  77585. + * @param qh The QH to free.
  77586. + */
  77587. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  77588. +{
  77589. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  77590. +
  77591. + /* Free each QTD in the QTD list */
  77592. + DWC_SPINLOCK(hcd->lock);
  77593. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  77594. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  77595. + dwc_otg_hcd_qtd_free(qtd);
  77596. + }
  77597. +
  77598. + if (hcd->core_if->dma_desc_enable) {
  77599. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  77600. + } else if (qh->dw_align_buf) {
  77601. + uint32_t buf_size;
  77602. + if (qh->ep_type == UE_ISOCHRONOUS) {
  77603. + buf_size = 4096;
  77604. + } else {
  77605. + buf_size = hcd->core_if->core_params->max_transfer_size;
  77606. + }
  77607. + DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
  77608. + }
  77609. +
  77610. + DWC_FREE(qh);
  77611. + DWC_SPINUNLOCK(hcd->lock);
  77612. + return;
  77613. +}
  77614. +
  77615. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  77616. +#define HS_HOST_DELAY 5 /* nanoseconds */
  77617. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  77618. +#define HUB_LS_SETUP 333 /* nanoseconds */
  77619. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  77620. + /* convert & round nanoseconds to microseconds */
  77621. +
  77622. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  77623. +{
  77624. + unsigned long retval;
  77625. +
  77626. + switch (speed) {
  77627. + case USB_SPEED_HIGH:
  77628. + if (is_isoc) {
  77629. + retval =
  77630. + ((38 * 8 * 2083) +
  77631. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  77632. + HS_HOST_DELAY;
  77633. + } else {
  77634. + retval =
  77635. + ((55 * 8 * 2083) +
  77636. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  77637. + HS_HOST_DELAY;
  77638. + }
  77639. + break;
  77640. + case USB_SPEED_FULL:
  77641. + if (is_isoc) {
  77642. + retval =
  77643. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  77644. + if (is_in) {
  77645. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  77646. + } else {
  77647. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  77648. + }
  77649. + } else {
  77650. + retval =
  77651. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  77652. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  77653. + }
  77654. + break;
  77655. + case USB_SPEED_LOW:
  77656. + if (is_in) {
  77657. + retval =
  77658. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  77659. + 1000;
  77660. + retval =
  77661. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  77662. + retval;
  77663. + } else {
  77664. + retval =
  77665. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  77666. + 1000;
  77667. + retval =
  77668. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  77669. + retval;
  77670. + }
  77671. + break;
  77672. + default:
  77673. + DWC_WARN("Unknown device speed\n");
  77674. + retval = -1;
  77675. + }
  77676. +
  77677. + return NS_TO_US(retval);
  77678. +}
  77679. +
  77680. +/**
  77681. + * Initializes a QH structure.
  77682. + *
  77683. + * @param hcd The HCD state structure for the DWC OTG controller.
  77684. + * @param qh The QH to init.
  77685. + * @param urb Holds the information about the device/endpoint that we need
  77686. + * to initialize the QH.
  77687. + */
  77688. +#define SCHEDULE_SLOP 10
  77689. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  77690. +{
  77691. + char *speed, *type;
  77692. + int dev_speed;
  77693. + uint32_t hub_addr, hub_port;
  77694. +
  77695. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  77696. +
  77697. + /* Initialize QH */
  77698. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  77699. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  77700. +
  77701. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  77702. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  77703. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  77704. + DWC_LIST_INIT(&qh->qh_list_entry);
  77705. + qh->channel = NULL;
  77706. +
  77707. + /* FS/LS Enpoint on HS Hub
  77708. + * NOT virtual root hub */
  77709. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  77710. +
  77711. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  77712. + qh->do_split = 0;
  77713. + if (microframe_schedule)
  77714. + qh->speed = dev_speed;
  77715. +
  77716. + qh->nak_frame = 0xffff;
  77717. +
  77718. + if (((dev_speed == USB_SPEED_LOW) ||
  77719. + (dev_speed == USB_SPEED_FULL)) &&
  77720. + (hub_addr != 0 && hub_addr != 1)) {
  77721. + DWC_DEBUGPL(DBG_HCD,
  77722. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  77723. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  77724. + hub_port);
  77725. + qh->do_split = 1;
  77726. + qh->skip_count = 0;
  77727. + }
  77728. +
  77729. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  77730. + /* Compute scheduling parameters once and save them. */
  77731. + hprt0_data_t hprt;
  77732. +
  77733. + /** @todo Account for split transfers in the bus time. */
  77734. + int bytecount =
  77735. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  77736. +
  77737. + qh->usecs =
  77738. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  77739. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  77740. + bytecount);
  77741. + /* Start in a slightly future (micro)frame. */
  77742. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  77743. + SCHEDULE_SLOP);
  77744. + qh->interval = urb->interval;
  77745. +
  77746. +#if 0
  77747. + /* Increase interrupt polling rate for debugging. */
  77748. + if (qh->ep_type == UE_INTERRUPT) {
  77749. + qh->interval = 8;
  77750. + }
  77751. +#endif
  77752. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  77753. + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
  77754. + ((dev_speed == USB_SPEED_LOW) ||
  77755. + (dev_speed == USB_SPEED_FULL))) {
  77756. + qh->interval *= 8;
  77757. + qh->sched_frame |= 0x7;
  77758. + qh->start_split_frame = qh->sched_frame;
  77759. + }
  77760. +
  77761. + }
  77762. +
  77763. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  77764. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  77765. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  77766. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  77767. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  77768. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  77769. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  77770. + switch (dev_speed) {
  77771. + case USB_SPEED_LOW:
  77772. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  77773. + speed = "low";
  77774. + break;
  77775. + case USB_SPEED_FULL:
  77776. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  77777. + speed = "full";
  77778. + break;
  77779. + case USB_SPEED_HIGH:
  77780. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  77781. + speed = "high";
  77782. + break;
  77783. + default:
  77784. + speed = "?";
  77785. + break;
  77786. + }
  77787. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  77788. +
  77789. + switch (qh->ep_type) {
  77790. + case UE_ISOCHRONOUS:
  77791. + type = "isochronous";
  77792. + break;
  77793. + case UE_INTERRUPT:
  77794. + type = "interrupt";
  77795. + break;
  77796. + case UE_CONTROL:
  77797. + type = "control";
  77798. + break;
  77799. + case UE_BULK:
  77800. + type = "bulk";
  77801. + break;
  77802. + default:
  77803. + type = "?";
  77804. + break;
  77805. + }
  77806. +
  77807. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  77808. +
  77809. +#ifdef DEBUG
  77810. + if (qh->ep_type == UE_INTERRUPT) {
  77811. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  77812. + qh->usecs);
  77813. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  77814. + qh->interval);
  77815. + }
  77816. +#endif
  77817. +
  77818. +}
  77819. +
  77820. +/**
  77821. + * This function allocates and initializes a QH.
  77822. + *
  77823. + * @param hcd The HCD state structure for the DWC OTG controller.
  77824. + * @param urb Holds the information about the device/endpoint that we need
  77825. + * to initialize the QH.
  77826. + * @param atomic_alloc Flag to do atomic allocation if needed
  77827. + *
  77828. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  77829. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  77830. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  77831. +{
  77832. + dwc_otg_qh_t *qh;
  77833. +
  77834. + /* Allocate memory */
  77835. + /** @todo add memflags argument */
  77836. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  77837. + if (qh == NULL) {
  77838. + DWC_ERROR("qh allocation failed");
  77839. + return NULL;
  77840. + }
  77841. +
  77842. + qh_init(hcd, qh, urb);
  77843. +
  77844. + if (hcd->core_if->dma_desc_enable
  77845. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  77846. + dwc_otg_hcd_qh_free(hcd, qh);
  77847. + return NULL;
  77848. + }
  77849. +
  77850. + return qh;
  77851. +}
  77852. +
  77853. +/* microframe_schedule=0 start */
  77854. +
  77855. +/**
  77856. + * Checks that a channel is available for a periodic transfer.
  77857. + *
  77858. + * @return 0 if successful, negative error code otherise.
  77859. + */
  77860. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  77861. +{
  77862. + /*
  77863. + * Currently assuming that there is a dedicated host channnel for each
  77864. + * periodic transaction plus at least one host channel for
  77865. + * non-periodic transactions.
  77866. + */
  77867. + int status;
  77868. + int num_channels;
  77869. +
  77870. + num_channels = hcd->core_if->core_params->host_channels;
  77871. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  77872. + && (hcd->periodic_channels < num_channels - 1)) {
  77873. + status = 0;
  77874. + } else {
  77875. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  77876. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  77877. + status = -DWC_E_NO_SPACE;
  77878. + }
  77879. +
  77880. + return status;
  77881. +}
  77882. +
  77883. +/**
  77884. + * Checks that there is sufficient bandwidth for the specified QH in the
  77885. + * periodic schedule. For simplicity, this calculation assumes that all the
  77886. + * transfers in the periodic schedule may occur in the same (micro)frame.
  77887. + *
  77888. + * @param hcd The HCD state structure for the DWC OTG controller.
  77889. + * @param qh QH containing periodic bandwidth required.
  77890. + *
  77891. + * @return 0 if successful, negative error code otherwise.
  77892. + */
  77893. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  77894. +{
  77895. + int status;
  77896. + int16_t max_claimed_usecs;
  77897. +
  77898. + status = 0;
  77899. +
  77900. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  77901. + /*
  77902. + * High speed mode.
  77903. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  77904. + */
  77905. +
  77906. + max_claimed_usecs = 100 - qh->usecs;
  77907. + } else {
  77908. + /*
  77909. + * Full speed mode.
  77910. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  77911. + */
  77912. + max_claimed_usecs = 900 - qh->usecs;
  77913. + }
  77914. +
  77915. + if (hcd->periodic_usecs > max_claimed_usecs) {
  77916. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  77917. + status = -DWC_E_NO_SPACE;
  77918. + }
  77919. +
  77920. + return status;
  77921. +}
  77922. +
  77923. +/* microframe_schedule=0 end */
  77924. +
  77925. +/**
  77926. + * Microframe scheduler
  77927. + * track the total use in hcd->frame_usecs
  77928. + * keep each qh use in qh->frame_usecs
  77929. + * when surrendering the qh then donate the time back
  77930. + */
  77931. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  77932. +
  77933. +/*
  77934. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  77935. + */
  77936. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  77937. +{
  77938. + int i;
  77939. + for (i=0; i<8; i++) {
  77940. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  77941. + }
  77942. + return 0;
  77943. +}
  77944. +
  77945. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  77946. +{
  77947. + int i;
  77948. + unsigned short utime;
  77949. + int t_left;
  77950. + int ret;
  77951. + int done;
  77952. +
  77953. + ret = -1;
  77954. + utime = _qh->usecs;
  77955. + t_left = utime;
  77956. + i = 0;
  77957. + done = 0;
  77958. + while (done == 0) {
  77959. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  77960. + if (utime <= _hcd->frame_usecs[i]) {
  77961. + _hcd->frame_usecs[i] -= utime;
  77962. + _qh->frame_usecs[i] += utime;
  77963. + t_left -= utime;
  77964. + ret = i;
  77965. + done = 1;
  77966. + return ret;
  77967. + } else {
  77968. + i++;
  77969. + if (i == 8) {
  77970. + done = 1;
  77971. + ret = -1;
  77972. + }
  77973. + }
  77974. + }
  77975. + return ret;
  77976. + }
  77977. +
  77978. +/*
  77979. + * use this for FS apps that can span multiple uframes
  77980. + */
  77981. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  77982. +{
  77983. + int i;
  77984. + int j;
  77985. + unsigned short utime;
  77986. + int t_left;
  77987. + int ret;
  77988. + int done;
  77989. + unsigned short xtime;
  77990. +
  77991. + ret = -1;
  77992. + utime = _qh->usecs;
  77993. + t_left = utime;
  77994. + i = 0;
  77995. + done = 0;
  77996. +loop:
  77997. + while (done == 0) {
  77998. + if(_hcd->frame_usecs[i] <= 0) {
  77999. + i++;
  78000. + if (i == 8) {
  78001. + done = 1;
  78002. + ret = -1;
  78003. + }
  78004. + goto loop;
  78005. + }
  78006. +
  78007. + /*
  78008. + * we need n consecutive slots
  78009. + * so use j as a start slot j plus j+1 must be enough time (for now)
  78010. + */
  78011. + xtime= _hcd->frame_usecs[i];
  78012. + for (j = i+1 ; j < 8 ; j++ ) {
  78013. + /*
  78014. + * if we add this frame remaining time to xtime we may
  78015. + * be OK, if not we need to test j for a complete frame
  78016. + */
  78017. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  78018. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  78019. + j = 8;
  78020. + ret = -1;
  78021. + continue;
  78022. + }
  78023. + }
  78024. + if (xtime >= utime) {
  78025. + ret = i;
  78026. + j = 8; /* stop loop with a good value ret */
  78027. + continue;
  78028. + }
  78029. + /* add the frame time to x time */
  78030. + xtime += _hcd->frame_usecs[j];
  78031. + /* we must have a fully available next frame or break */
  78032. + if ((xtime < utime)
  78033. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  78034. + ret = -1;
  78035. + j = 8; /* stop loop with a bad value ret */
  78036. + continue;
  78037. + }
  78038. + }
  78039. + if (ret >= 0) {
  78040. + t_left = utime;
  78041. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  78042. + t_left -= _hcd->frame_usecs[j];
  78043. + if ( t_left <= 0 ) {
  78044. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  78045. + _hcd->frame_usecs[j]= -t_left;
  78046. + ret = i;
  78047. + done = 1;
  78048. + } else {
  78049. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  78050. + _hcd->frame_usecs[j] = 0;
  78051. + }
  78052. + }
  78053. + } else {
  78054. + i++;
  78055. + if (i == 8) {
  78056. + done = 1;
  78057. + ret = -1;
  78058. + }
  78059. + }
  78060. + }
  78061. + return ret;
  78062. +}
  78063. +
  78064. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  78065. +{
  78066. + int ret;
  78067. + ret = -1;
  78068. +
  78069. + if (_qh->speed == USB_SPEED_HIGH) {
  78070. + /* if this is a hs transaction we need a full frame */
  78071. + ret = find_single_uframe(_hcd, _qh);
  78072. + } else {
  78073. + /* if this is a fs transaction we may need a sequence of frames */
  78074. + ret = find_multi_uframe(_hcd, _qh);
  78075. + }
  78076. + return ret;
  78077. +}
  78078. +
  78079. +/**
  78080. + * Checks that the max transfer size allowed in a host channel is large enough
  78081. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  78082. + * transfer.
  78083. + *
  78084. + * @param hcd The HCD state structure for the DWC OTG controller.
  78085. + * @param qh QH for a periodic endpoint.
  78086. + *
  78087. + * @return 0 if successful, negative error code otherwise.
  78088. + */
  78089. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78090. +{
  78091. + int status;
  78092. + uint32_t max_xfer_size;
  78093. + uint32_t max_channel_xfer_size;
  78094. +
  78095. + status = 0;
  78096. +
  78097. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  78098. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  78099. +
  78100. + if (max_xfer_size > max_channel_xfer_size) {
  78101. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  78102. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  78103. + status = -DWC_E_NO_SPACE;
  78104. + }
  78105. +
  78106. + return status;
  78107. +}
  78108. +
  78109. +
  78110. +extern int g_next_sched_frame, g_np_count, g_np_sent;
  78111. +
  78112. +/**
  78113. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  78114. + *
  78115. + * @param hcd The HCD state structure for the DWC OTG controller.
  78116. + * @param qh QH for the periodic transfer. The QH should already contain the
  78117. + * scheduling information.
  78118. + *
  78119. + * @return 0 if successful, negative error code otherwise.
  78120. + */
  78121. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78122. +{
  78123. + int status = 0;
  78124. +
  78125. + if (microframe_schedule) {
  78126. + int frame;
  78127. + status = find_uframe(hcd, qh);
  78128. + frame = -1;
  78129. + if (status == 0) {
  78130. + frame = 7;
  78131. + } else {
  78132. + if (status > 0 )
  78133. + frame = status-1;
  78134. + }
  78135. +
  78136. + /* Set the new frame up */
  78137. + if (frame > -1) {
  78138. + qh->sched_frame &= ~0x7;
  78139. + qh->sched_frame |= (frame & 7);
  78140. + }
  78141. +
  78142. + if (status != -1)
  78143. + status = 0;
  78144. + } else {
  78145. + status = periodic_channel_available(hcd);
  78146. + if (status) {
  78147. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  78148. + return status;
  78149. + }
  78150. +
  78151. + status = check_periodic_bandwidth(hcd, qh);
  78152. + }
  78153. + if (status) {
  78154. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  78155. + "periodic transfer.\n", __func__);
  78156. + return status;
  78157. + }
  78158. + status = check_max_xfer_size(hcd, qh);
  78159. + if (status) {
  78160. + DWC_INFO("%s: Channel max transfer size too small "
  78161. + "for periodic transfer.\n", __func__);
  78162. + return status;
  78163. + }
  78164. +
  78165. + if (hcd->core_if->dma_desc_enable) {
  78166. + /* Don't rely on SOF and start in ready schedule */
  78167. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  78168. + }
  78169. + else {
  78170. + if(DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, g_next_sched_frame))
  78171. + {
  78172. + g_next_sched_frame = qh->sched_frame;
  78173. +
  78174. + }
  78175. + /* Always start in the inactive schedule. */
  78176. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  78177. + }
  78178. +
  78179. + if (!microframe_schedule) {
  78180. + /* Reserve the periodic channel. */
  78181. + hcd->periodic_channels++;
  78182. + }
  78183. +
  78184. + /* Update claimed usecs per (micro)frame. */
  78185. + hcd->periodic_usecs += qh->usecs;
  78186. +
  78187. + return status;
  78188. +}
  78189. +
  78190. +
  78191. +/**
  78192. + * This function adds a QH to either the non periodic or periodic schedule if
  78193. + * it is not already in the schedule. If the QH is already in the schedule, no
  78194. + * action is taken.
  78195. + *
  78196. + * @return 0 if successful, negative error code otherwise.
  78197. + */
  78198. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78199. +{
  78200. + int status = 0;
  78201. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78202. +
  78203. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  78204. + /* QH already in a schedule. */
  78205. + return status;
  78206. + }
  78207. +
  78208. + /* Add the new QH to the appropriate schedule */
  78209. + if (dwc_qh_is_non_per(qh)) {
  78210. + /* Always start in the inactive schedule. */
  78211. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  78212. + &qh->qh_list_entry);
  78213. + g_np_count++;
  78214. + } else {
  78215. + status = schedule_periodic(hcd, qh);
  78216. + if ( !hcd->periodic_qh_count ) {
  78217. + intr_mask.b.sofintr = 1;
  78218. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  78219. + intr_mask.d32, intr_mask.d32);
  78220. + }
  78221. + hcd->periodic_qh_count++;
  78222. + }
  78223. +
  78224. + return status;
  78225. +}
  78226. +
  78227. +/**
  78228. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  78229. + *
  78230. + * @param hcd The HCD state structure for the DWC OTG controller.
  78231. + * @param qh QH for the periodic transfer.
  78232. + */
  78233. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78234. +{
  78235. + int i;
  78236. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  78237. +
  78238. + /* Update claimed usecs per (micro)frame. */
  78239. + hcd->periodic_usecs -= qh->usecs;
  78240. +
  78241. + if (!microframe_schedule) {
  78242. + /* Release the periodic channel reservation. */
  78243. + hcd->periodic_channels--;
  78244. + } else {
  78245. + for (i = 0; i < 8; i++) {
  78246. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  78247. + qh->frame_usecs[i] = 0;
  78248. + }
  78249. + }
  78250. +}
  78251. +
  78252. +/**
  78253. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  78254. + * not freed.
  78255. + *
  78256. + * @param hcd The HCD state structure.
  78257. + * @param qh QH to remove from schedule. */
  78258. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78259. +{
  78260. + gintmsk_data_t intr_mask = {.d32 = 0 };
  78261. +
  78262. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  78263. + /* QH is not in a schedule. */
  78264. + return;
  78265. + }
  78266. +
  78267. + if (dwc_qh_is_non_per(qh)) {
  78268. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  78269. + hcd->non_periodic_qh_ptr =
  78270. + hcd->non_periodic_qh_ptr->next;
  78271. + }
  78272. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  78273. +
  78274. + // If we've removed the last non-periodic entry then there are none left!
  78275. + g_np_count = g_np_sent;
  78276. + } else {
  78277. + deschedule_periodic(hcd, qh);
  78278. + hcd->periodic_qh_count--;
  78279. + if( !hcd->periodic_qh_count ) {
  78280. + intr_mask.b.sofintr = 1;
  78281. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  78282. + intr_mask.d32, 0);
  78283. + }
  78284. + }
  78285. +}
  78286. +
  78287. +/**
  78288. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  78289. + * non-periodic schedule. The QH is added to the inactive non-periodic
  78290. + * schedule if any QTDs are still attached to the QH.
  78291. + *
  78292. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  78293. + * there are any QTDs still attached to the QH, the QH is added to either the
  78294. + * periodic inactive schedule or the periodic ready schedule and its next
  78295. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  78296. + * the scheduled frame has been reached already. Otherwise it's placed in the
  78297. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  78298. + * completely removed from the periodic schedule.
  78299. + */
  78300. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  78301. + int sched_next_periodic_split)
  78302. +{
  78303. + if (dwc_qh_is_non_per(qh)) {
  78304. +
  78305. + dwc_otg_qh_t *qh_tmp;
  78306. + dwc_list_link_t *qh_list;
  78307. + DWC_LIST_FOREACH(qh_list, &hcd->non_periodic_sched_inactive)
  78308. + {
  78309. + qh_tmp = DWC_LIST_ENTRY(qh_list, struct dwc_otg_qh, qh_list_entry);
  78310. + if(qh_tmp == qh)
  78311. + {
  78312. + /*
  78313. + * FIQ is being disabled because this one nevers gets a np_count increment
  78314. + * This is still not absolutely correct, but it should fix itself with
  78315. + * just an unnecessary extra interrupt
  78316. + */
  78317. + g_np_sent = g_np_count;
  78318. + }
  78319. + }
  78320. +
  78321. +
  78322. + dwc_otg_hcd_qh_remove(hcd, qh);
  78323. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  78324. + /* Add back to inactive non-periodic schedule. */
  78325. + dwc_otg_hcd_qh_add(hcd, qh);
  78326. + }
  78327. + } else {
  78328. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  78329. +
  78330. + if (qh->do_split) {
  78331. + /* Schedule the next continuing periodic split transfer */
  78332. + if (sched_next_periodic_split) {
  78333. +
  78334. + qh->sched_frame = frame_number;
  78335. +
  78336. + if (dwc_frame_num_le(frame_number,
  78337. + dwc_frame_num_inc
  78338. + (qh->start_split_frame,
  78339. + 1))) {
  78340. + /*
  78341. + * Allow one frame to elapse after start
  78342. + * split microframe before scheduling
  78343. + * complete split, but DONT if we are
  78344. + * doing the next start split in the
  78345. + * same frame for an ISOC out.
  78346. + */
  78347. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  78348. + (qh->ep_is_in != 0)) {
  78349. + qh->sched_frame =
  78350. + dwc_frame_num_inc(qh->sched_frame, 1);
  78351. + }
  78352. + }
  78353. + } else {
  78354. + qh->sched_frame =
  78355. + dwc_frame_num_inc(qh->start_split_frame,
  78356. + qh->interval);
  78357. + if (dwc_frame_num_le
  78358. + (qh->sched_frame, frame_number)) {
  78359. + qh->sched_frame = frame_number;
  78360. + }
  78361. + qh->sched_frame |= 0x7;
  78362. + qh->start_split_frame = qh->sched_frame;
  78363. + }
  78364. + } else {
  78365. + qh->sched_frame =
  78366. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  78367. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  78368. + qh->sched_frame = frame_number;
  78369. + }
  78370. + }
  78371. +
  78372. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  78373. + dwc_otg_hcd_qh_remove(hcd, qh);
  78374. + } else {
  78375. + /*
  78376. + * Remove from periodic_sched_queued and move to
  78377. + * appropriate queue.
  78378. + */
  78379. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  78380. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  78381. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  78382. + &qh->qh_list_entry);
  78383. + } else {
  78384. + if(!dwc_frame_num_le(g_next_sched_frame, qh->sched_frame))
  78385. + {
  78386. + g_next_sched_frame = qh->sched_frame;
  78387. + }
  78388. +
  78389. + DWC_LIST_MOVE_HEAD
  78390. + (&hcd->periodic_sched_inactive,
  78391. + &qh->qh_list_entry);
  78392. + }
  78393. + }
  78394. + }
  78395. +}
  78396. +
  78397. +/**
  78398. + * This function allocates and initializes a QTD.
  78399. + *
  78400. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  78401. + * pointing to each other so each pair should have a unique correlation.
  78402. + * @param atomic_alloc Flag to do atomic alloc if needed
  78403. + *
  78404. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  78405. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  78406. +{
  78407. + dwc_otg_qtd_t *qtd;
  78408. +
  78409. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  78410. + if (qtd == NULL) {
  78411. + return NULL;
  78412. + }
  78413. +
  78414. + dwc_otg_hcd_qtd_init(qtd, urb);
  78415. + return qtd;
  78416. +}
  78417. +
  78418. +/**
  78419. + * Initializes a QTD structure.
  78420. + *
  78421. + * @param qtd The QTD to initialize.
  78422. + * @param urb The URB to use for initialization. */
  78423. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  78424. +{
  78425. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  78426. + qtd->urb = urb;
  78427. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  78428. + /*
  78429. + * The only time the QTD data toggle is used is on the data
  78430. + * phase of control transfers. This phase always starts with
  78431. + * DATA1.
  78432. + */
  78433. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  78434. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  78435. + }
  78436. +
  78437. + /* start split */
  78438. + qtd->complete_split = 0;
  78439. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  78440. + qtd->isoc_split_offset = 0;
  78441. + qtd->in_process = 0;
  78442. +
  78443. + /* Store the qtd ptr in the urb to reference what QTD. */
  78444. + urb->qtd = qtd;
  78445. + return;
  78446. +}
  78447. +
  78448. +/**
  78449. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  78450. + * QH to place the QTD into. If it does not find a QH, then it will create a
  78451. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  78452. + * is placed into the proper schedule based on its EP type.
  78453. + * HCD lock must be held and interrupts must be disabled on entry
  78454. + *
  78455. + * @param[in] qtd The QTD to add
  78456. + * @param[in] hcd The DWC HCD structure
  78457. + * @param[out] qh out parameter to return queue head
  78458. + * @param atomic_alloc Flag to do atomic alloc if needed
  78459. + *
  78460. + * @return 0 if successful, negative error code otherwise.
  78461. + */
  78462. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  78463. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  78464. +{
  78465. + int retval = 0;
  78466. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  78467. +
  78468. + /*
  78469. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  78470. + * doesn't exist.
  78471. + */
  78472. + if (*qh == NULL) {
  78473. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  78474. + if (*qh == NULL) {
  78475. + retval = -DWC_E_NO_MEMORY;
  78476. + goto done;
  78477. + }
  78478. + }
  78479. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  78480. + if (retval == 0) {
  78481. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  78482. + qtd_list_entry);
  78483. + qtd->qh = *qh;
  78484. + }
  78485. +done:
  78486. +
  78487. + return retval;
  78488. +}
  78489. +
  78490. +#endif /* DWC_DEVICE_ONLY */
  78491. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c
  78492. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c 1970-01-01 01:00:00.000000000 +0100
  78493. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.c 2014-03-11 16:55:38.000000000 +0100
  78494. @@ -0,0 +1,113 @@
  78495. +#include "dwc_otg_regs.h"
  78496. +#include "dwc_otg_dbg.h"
  78497. +
  78498. +void dwc_debug_print_core_int_reg(gintsts_data_t gintsts, const char* function_name)
  78499. +{
  78500. + DWC_DEBUGPL(DBG_USER, "*** Debugging from within the %s function: ***\n"
  78501. + "curmode: %1i Modemismatch: %1i otgintr: %1i sofintr: %1i\n"
  78502. + "rxstsqlvl: %1i nptxfempty : %1i ginnakeff: %1i goutnakeff: %1i\n"
  78503. + "ulpickint: %1i i2cintr: %1i erlysuspend:%1i usbsuspend: %1i\n"
  78504. + "usbreset: %1i enumdone: %1i isooutdrop: %1i eopframe: %1i\n"
  78505. + "restoredone: %1i epmismatch: %1i inepint: %1i outepintr: %1i\n"
  78506. + "incomplisoin:%1i incomplisoout:%1i fetsusp: %1i resetdet: %1i\n"
  78507. + "portintr: %1i hcintr: %1i ptxfempty: %1i lpmtranrcvd:%1i\n"
  78508. + "conidstschng:%1i disconnect: %1i sessreqintr:%1i wkupintr: %1i\n",
  78509. + function_name,
  78510. + gintsts.b.curmode,
  78511. + gintsts.b.modemismatch,
  78512. + gintsts.b.otgintr,
  78513. + gintsts.b.sofintr,
  78514. + gintsts.b.rxstsqlvl,
  78515. + gintsts.b.nptxfempty,
  78516. + gintsts.b.ginnakeff,
  78517. + gintsts.b.goutnakeff,
  78518. + gintsts.b.ulpickint,
  78519. + gintsts.b.i2cintr,
  78520. + gintsts.b.erlysuspend,
  78521. + gintsts.b.usbsuspend,
  78522. + gintsts.b.usbreset,
  78523. + gintsts.b.enumdone,
  78524. + gintsts.b.isooutdrop,
  78525. + gintsts.b.eopframe,
  78526. + gintsts.b.restoredone,
  78527. + gintsts.b.epmismatch,
  78528. + gintsts.b.inepint,
  78529. + gintsts.b.outepintr,
  78530. + gintsts.b.incomplisoin,
  78531. + gintsts.b.incomplisoout,
  78532. + gintsts.b.fetsusp,
  78533. + gintsts.b.resetdet,
  78534. + gintsts.b.portintr,
  78535. + gintsts.b.hcintr,
  78536. + gintsts.b.ptxfempty,
  78537. + gintsts.b.lpmtranrcvd,
  78538. + gintsts.b.conidstschng,
  78539. + gintsts.b.disconnect,
  78540. + gintsts.b.sessreqintr,
  78541. + gintsts.b.wkupintr);
  78542. + return;
  78543. +}
  78544. +
  78545. +void dwc_debug_core_int_mask(gintmsk_data_t gintmsk, const char* function_name)
  78546. +{
  78547. + DWC_DEBUGPL(DBG_USER, "Interrupt Mask status (called from %s) :\n"
  78548. + "modemismatch: %1i otgintr: %1i sofintr: %1i rxstsqlvl: %1i\n"
  78549. + "nptxfempty: %1i ginnakeff: %1i goutnakeff: %1i ulpickint: %1i\n"
  78550. + "i2cintr: %1i erlysuspend:%1i usbsuspend: %1i usbreset: %1i\n"
  78551. + "enumdone: %1i isooutdrop: %1i eopframe: %1i restoredone: %1i\n"
  78552. + "epmismatch: %1i inepintr: %1i outepintr: %1i incomplisoin:%1i\n"
  78553. + "incomplisoout:%1i fetsusp: %1i resetdet: %1i portintr: %1i\n"
  78554. + "hcintr: %1i ptxfempty: %1i lpmtranrcvd:%1i conidstschng:%1i\n"
  78555. + "disconnect: %1i sessreqintr:%1i wkupintr: %1i\n",
  78556. + function_name,
  78557. + gintmsk.b.modemismatch,
  78558. + gintmsk.b.otgintr,
  78559. + gintmsk.b.sofintr,
  78560. + gintmsk.b.rxstsqlvl,
  78561. + gintmsk.b.nptxfempty,
  78562. + gintmsk.b.ginnakeff,
  78563. + gintmsk.b.goutnakeff,
  78564. + gintmsk.b.ulpickint,
  78565. + gintmsk.b.i2cintr,
  78566. + gintmsk.b.erlysuspend,
  78567. + gintmsk.b.usbsuspend,
  78568. + gintmsk.b.usbreset,
  78569. + gintmsk.b.enumdone,
  78570. + gintmsk.b.isooutdrop,
  78571. + gintmsk.b.eopframe,
  78572. + gintmsk.b.restoredone,
  78573. + gintmsk.b.epmismatch,
  78574. + gintmsk.b.inepintr,
  78575. + gintmsk.b.outepintr,
  78576. + gintmsk.b.incomplisoin,
  78577. + gintmsk.b.incomplisoout,
  78578. + gintmsk.b.fetsusp,
  78579. + gintmsk.b.resetdet,
  78580. + gintmsk.b.portintr,
  78581. + gintmsk.b.hcintr,
  78582. + gintmsk.b.ptxfempty,
  78583. + gintmsk.b.lpmtranrcvd,
  78584. + gintmsk.b.conidstschng,
  78585. + gintmsk.b.disconnect,
  78586. + gintmsk.b.sessreqintr,
  78587. + gintmsk.b.wkupintr);
  78588. + return;
  78589. +}
  78590. +
  78591. +void dwc_debug_otg_int(gotgint_data_t gotgint, const char* function_name)
  78592. +{
  78593. + DWC_DEBUGPL(DBG_USER, "otg int register (from %s function):\n"
  78594. + "sesenddet:%1i sesreqsucstschung:%2i hstnegsucstschng:%1i\n"
  78595. + "hstnegdet:%1i adevtoutchng: %2i debdone: %1i\n"
  78596. + "mvic: %1i\n",
  78597. + function_name,
  78598. + gotgint.b.sesenddet,
  78599. + gotgint.b.sesreqsucstschng,
  78600. + gotgint.b.hstnegsucstschng,
  78601. + gotgint.b.hstnegdet,
  78602. + gotgint.b.adevtoutchng,
  78603. + gotgint.b.debdone,
  78604. + gotgint.b.mvic);
  78605. +
  78606. + return;
  78607. +}
  78608. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h
  78609. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h 1970-01-01 01:00:00.000000000 +0100
  78610. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_mphi_fix.h 2014-03-11 16:53:12.000000000 +0100
  78611. @@ -0,0 +1,48 @@
  78612. +#ifndef __DWC_OTG_MPHI_FIX_H__
  78613. +#define __DWC_OTG_MPHI_FIX_H__
  78614. +#define FIQ_WRITE(_addr_,_data_) (*(volatile uint32_t *) (_addr_) = (_data_))
  78615. +#define FIQ_READ(_addr_) (*(volatile uint32_t *) (_addr_))
  78616. +
  78617. +typedef struct {
  78618. + volatile void* base;
  78619. + volatile void* ctrl;
  78620. + volatile void* outdda;
  78621. + volatile void* outddb;
  78622. + volatile void* intstat;
  78623. +} mphi_regs_t;
  78624. +
  78625. +void dwc_debug_print_core_int_reg(gintsts_data_t gintsts, const char* function_name);
  78626. +void dwc_debug_core_int_mask(gintsts_data_t gintmsk, const char* function_name);
  78627. +void dwc_debug_otg_int(gotgint_data_t gotgint, const char* function_name);
  78628. +
  78629. +extern gintsts_data_t gintsts_saved;
  78630. +
  78631. +#ifdef DEBUG
  78632. +#define DWC_DBG_PRINT_CORE_INT(_arg_) dwc_debug_print_core_int_reg(_arg_,__func__)
  78633. +#define DWC_DBG_PRINT_CORE_INT_MASK(_arg_) dwc_debug_core_int_mask(_arg_,__func__)
  78634. +#define DWC_DBG_PRINT_OTG_INT(_arg_) dwc_debug_otg_int(_arg_,__func__)
  78635. +
  78636. +#else
  78637. +#define DWC_DBG_PRINT_CORE_INT(_arg_)
  78638. +#define DWC_DBG_PRINT_CORE_INT_MASK(_arg_)
  78639. +#define DWC_DBG_PRINT_OTG_INT(_arg_)
  78640. +
  78641. +#endif
  78642. +
  78643. +typedef enum {
  78644. + FIQDBG_SCHED = (1 << 0),
  78645. + FIQDBG_INT = (1 << 1),
  78646. + FIQDBG_ERR = (1 << 2),
  78647. + FIQDBG_PORTHUB = (1 << 3),
  78648. +} FIQDBG_T;
  78649. +
  78650. +void _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...);
  78651. +#ifdef FIQ_DEBUG
  78652. +#define fiq_print _fiq_print
  78653. +#else
  78654. +#define fiq_print(x, y, ...)
  78655. +#endif
  78656. +
  78657. +extern bool fiq_fix_enable, nak_holdoff_enable, fiq_split_enable;
  78658. +
  78659. +#endif
  78660. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  78661. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 1970-01-01 01:00:00.000000000 +0100
  78662. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 2014-03-11 16:55:38.000000000 +0100
  78663. @@ -0,0 +1,188 @@
  78664. +#ifndef _DWC_OS_DEP_H_
  78665. +#define _DWC_OS_DEP_H_
  78666. +
  78667. +/**
  78668. + * @file
  78669. + *
  78670. + * This file contains OS dependent structures.
  78671. + *
  78672. + */
  78673. +
  78674. +#include <linux/kernel.h>
  78675. +#include <linux/module.h>
  78676. +#include <linux/moduleparam.h>
  78677. +#include <linux/init.h>
  78678. +#include <linux/device.h>
  78679. +#include <linux/errno.h>
  78680. +#include <linux/types.h>
  78681. +#include <linux/slab.h>
  78682. +#include <linux/list.h>
  78683. +#include <linux/interrupt.h>
  78684. +#include <linux/ctype.h>
  78685. +#include <linux/string.h>
  78686. +#include <linux/dma-mapping.h>
  78687. +#include <linux/jiffies.h>
  78688. +#include <linux/delay.h>
  78689. +#include <linux/timer.h>
  78690. +#include <linux/workqueue.h>
  78691. +#include <linux/stat.h>
  78692. +#include <linux/pci.h>
  78693. +
  78694. +#include <linux/version.h>
  78695. +
  78696. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  78697. +# include <linux/irq.h>
  78698. +#endif
  78699. +
  78700. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  78701. +# include <linux/usb/ch9.h>
  78702. +#else
  78703. +# include <linux/usb_ch9.h>
  78704. +#endif
  78705. +
  78706. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  78707. +# include <linux/usb/gadget.h>
  78708. +#else
  78709. +# include <linux/usb_gadget.h>
  78710. +#endif
  78711. +
  78712. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  78713. +# include <asm/irq.h>
  78714. +#endif
  78715. +
  78716. +#ifdef PCI_INTERFACE
  78717. +# include <asm/io.h>
  78718. +#endif
  78719. +
  78720. +#ifdef LM_INTERFACE
  78721. +# include <asm/unaligned.h>
  78722. +# include <asm/sizes.h>
  78723. +# include <asm/param.h>
  78724. +# include <asm/io.h>
  78725. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  78726. +# include <asm/arch/hardware.h>
  78727. +# include <asm/arch/lm.h>
  78728. +# include <asm/arch/irqs.h>
  78729. +# include <asm/arch/regs-irq.h>
  78730. +# else
  78731. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  78732. + here we assume that the machine architecture provides definitions
  78733. + in its own header
  78734. +*/
  78735. +# include <mach/lm.h>
  78736. +# include <mach/hardware.h>
  78737. +# endif
  78738. +#endif
  78739. +
  78740. +#ifdef PLATFORM_INTERFACE
  78741. +#include <linux/platform_device.h>
  78742. +#include <asm/mach/map.h>
  78743. +#endif
  78744. +
  78745. +/** The OS page size */
  78746. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  78747. +
  78748. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  78749. +typedef int gfp_t;
  78750. +#endif
  78751. +
  78752. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  78753. +# define IRQF_SHARED SA_SHIRQ
  78754. +#endif
  78755. +
  78756. +typedef struct os_dependent {
  78757. + /** Base address returned from ioremap() */
  78758. + void *base;
  78759. +
  78760. + /** Register offset for Diagnostic API */
  78761. + uint32_t reg_offset;
  78762. +
  78763. + /** Base address for MPHI peripheral */
  78764. + void *mphi_base;
  78765. +
  78766. +#ifdef LM_INTERFACE
  78767. + struct lm_device *lmdev;
  78768. +#elif defined(PCI_INTERFACE)
  78769. + struct pci_dev *pcidev;
  78770. +
  78771. + /** Start address of a PCI region */
  78772. + resource_size_t rsrc_start;
  78773. +
  78774. + /** Length address of a PCI region */
  78775. + resource_size_t rsrc_len;
  78776. +#elif defined(PLATFORM_INTERFACE)
  78777. + struct platform_device *platformdev;
  78778. +#endif
  78779. +
  78780. +} os_dependent_t;
  78781. +
  78782. +#ifdef __cplusplus
  78783. +}
  78784. +#endif
  78785. +
  78786. +
  78787. +
  78788. +/* Type for the our device on the chosen bus */
  78789. +#if defined(LM_INTERFACE)
  78790. +typedef struct lm_device dwc_bus_dev_t;
  78791. +#elif defined(PCI_INTERFACE)
  78792. +typedef struct pci_dev dwc_bus_dev_t;
  78793. +#elif defined(PLATFORM_INTERFACE)
  78794. +typedef struct platform_device dwc_bus_dev_t;
  78795. +#endif
  78796. +
  78797. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  78798. +#if defined(LM_INTERFACE)
  78799. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  78800. +#elif defined(PCI_INTERFACE)
  78801. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  78802. +#elif defined(PLATFORM_INTERFACE)
  78803. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  78804. +#endif
  78805. +
  78806. +/**
  78807. + * Helper macro returning the otg_device structure of a given struct device
  78808. + *
  78809. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  78810. + */
  78811. +#ifdef LM_INTERFACE
  78812. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  78813. + struct lm_device *lm_dev = \
  78814. + container_of(_dev, struct lm_device, dev); \
  78815. + _var = lm_get_drvdata(lm_dev); \
  78816. + } while (0)
  78817. +
  78818. +#elif defined(PCI_INTERFACE)
  78819. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  78820. + _var = dev_get_drvdata(_dev); \
  78821. + } while (0)
  78822. +
  78823. +#elif defined(PLATFORM_INTERFACE)
  78824. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  78825. + struct platform_device *platform_dev = \
  78826. + container_of(_dev, struct platform_device, dev); \
  78827. + _var = platform_get_drvdata(platform_dev); \
  78828. + } while (0)
  78829. +#endif
  78830. +
  78831. +
  78832. +/**
  78833. + * Helper macro returning the struct dev of the given struct os_dependent
  78834. + *
  78835. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  78836. + */
  78837. +#ifdef LM_INTERFACE
  78838. +#define DWC_OTG_OS_GETDEV(_osdep) \
  78839. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  78840. +#elif defined(PCI_INTERFACE)
  78841. +#define DWC_OTG_OS_GETDEV(_osdep) \
  78842. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  78843. +#elif defined(PLATFORM_INTERFACE)
  78844. +#define DWC_OTG_OS_GETDEV(_osdep) \
  78845. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  78846. +#endif
  78847. +
  78848. +
  78849. +
  78850. +
  78851. +#endif /* _DWC_OS_DEP_H_ */
  78852. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  78853. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 1970-01-01 01:00:00.000000000 +0100
  78854. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 2014-03-11 16:55:38.000000000 +0100
  78855. @@ -0,0 +1,2708 @@
  78856. +/* ==========================================================================
  78857. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  78858. + * $Revision: #101 $
  78859. + * $Date: 2012/08/10 $
  78860. + * $Change: 2047372 $
  78861. + *
  78862. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  78863. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  78864. + * otherwise expressly agreed to in writing between Synopsys and you.
  78865. + *
  78866. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  78867. + * any End User Software License Agreement or Agreement for Licensed Product
  78868. + * with Synopsys or any supplement thereto. You are permitted to use and
  78869. + * redistribute this Software in source and binary forms, with or without
  78870. + * modification, provided that redistributions of source code must retain this
  78871. + * notice. You may not view, use, disclose, copy or distribute this file or
  78872. + * any information contained herein except pursuant to this license grant from
  78873. + * Synopsys. If you do not agree with this notice, including the disclaimer
  78874. + * below, then you are not authorized to use the Software.
  78875. + *
  78876. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  78877. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  78878. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  78879. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  78880. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78881. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  78882. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  78883. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  78884. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  78885. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  78886. + * DAMAGE.
  78887. + * ========================================================================== */
  78888. +#ifndef DWC_HOST_ONLY
  78889. +
  78890. +/** @file
  78891. + * This file implements PCD Core. All code in this file is portable and doesn't
  78892. + * use any OS specific functions.
  78893. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  78894. + * header file, which can be used to implement OS specific PCD interface.
  78895. + *
  78896. + * An important function of the PCD is managing interrupts generated
  78897. + * by the DWC_otg controller. The implementation of the DWC_otg device
  78898. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  78899. + *
  78900. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  78901. + * @todo Does it work when the request size is greater than DEPTSIZ
  78902. + * transfer size
  78903. + *
  78904. + */
  78905. +
  78906. +#include "dwc_otg_pcd.h"
  78907. +
  78908. +#ifdef DWC_UTE_CFI
  78909. +#include "dwc_otg_cfi.h"
  78910. +
  78911. +extern int init_cfi(cfiobject_t * cfiobj);
  78912. +#endif
  78913. +
  78914. +/**
  78915. + * Choose endpoint from ep arrays using usb_ep structure.
  78916. + */
  78917. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  78918. +{
  78919. + int i;
  78920. + if (pcd->ep0.priv == handle) {
  78921. + return &pcd->ep0;
  78922. + }
  78923. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  78924. + if (pcd->in_ep[i].priv == handle)
  78925. + return &pcd->in_ep[i];
  78926. + if (pcd->out_ep[i].priv == handle)
  78927. + return &pcd->out_ep[i];
  78928. + }
  78929. +
  78930. + return NULL;
  78931. +}
  78932. +
  78933. +/**
  78934. + * This function completes a request. It call's the request call back.
  78935. + */
  78936. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  78937. + int32_t status)
  78938. +{
  78939. + unsigned stopped = ep->stopped;
  78940. +
  78941. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  78942. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  78943. +
  78944. + /* don't modify queue heads during completion callback */
  78945. + ep->stopped = 1;
  78946. + /* spin_unlock/spin_lock now done in fops->complete() */
  78947. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  78948. + req->actual);
  78949. +
  78950. + if (ep->pcd->request_pending > 0) {
  78951. + --ep->pcd->request_pending;
  78952. + }
  78953. +
  78954. + ep->stopped = stopped;
  78955. + DWC_FREE(req);
  78956. +}
  78957. +
  78958. +/**
  78959. + * This function terminates all the requsts in the EP request queue.
  78960. + */
  78961. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  78962. +{
  78963. + dwc_otg_pcd_request_t *req;
  78964. +
  78965. + ep->stopped = 1;
  78966. +
  78967. + /* called with irqs blocked?? */
  78968. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  78969. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  78970. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  78971. + }
  78972. +}
  78973. +
  78974. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  78975. + const struct dwc_otg_pcd_function_ops *fops)
  78976. +{
  78977. + pcd->fops = fops;
  78978. +}
  78979. +
  78980. +/**
  78981. + * PCD Callback function for initializing the PCD when switching to
  78982. + * device mode.
  78983. + *
  78984. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  78985. + */
  78986. +static int32_t dwc_otg_pcd_start_cb(void *p)
  78987. +{
  78988. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  78989. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  78990. +
  78991. + /*
  78992. + * Initialized the Core for Device mode.
  78993. + */
  78994. + if (dwc_otg_is_device_mode(core_if)) {
  78995. + dwc_otg_core_dev_init(core_if);
  78996. + /* Set core_if's lock pointer to the pcd->lock */
  78997. + core_if->lock = pcd->lock;
  78998. + }
  78999. + return 1;
  79000. +}
  79001. +
  79002. +/** CFI-specific buffer allocation function for EP */
  79003. +#ifdef DWC_UTE_CFI
  79004. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  79005. + size_t buflen, int flags)
  79006. +{
  79007. + dwc_otg_pcd_ep_t *ep;
  79008. + ep = get_ep_from_handle(pcd, pep);
  79009. + if (!ep) {
  79010. + DWC_WARN("bad ep\n");
  79011. + return -DWC_E_INVALID;
  79012. + }
  79013. +
  79014. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  79015. + flags);
  79016. +}
  79017. +#else
  79018. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  79019. + size_t buflen, int flags);
  79020. +#endif
  79021. +
  79022. +/**
  79023. + * PCD Callback function for notifying the PCD when resuming from
  79024. + * suspend.
  79025. + *
  79026. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79027. + */
  79028. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  79029. +{
  79030. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79031. +
  79032. + if (pcd->fops->resume) {
  79033. + pcd->fops->resume(pcd);
  79034. + }
  79035. +
  79036. + /* Stop the SRP timeout timer. */
  79037. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  79038. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  79039. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  79040. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  79041. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  79042. + }
  79043. + }
  79044. + return 1;
  79045. +}
  79046. +
  79047. +/**
  79048. + * PCD Callback function for notifying the PCD device is suspended.
  79049. + *
  79050. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79051. + */
  79052. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  79053. +{
  79054. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79055. +
  79056. + if (pcd->fops->suspend) {
  79057. + DWC_SPINUNLOCK(pcd->lock);
  79058. + pcd->fops->suspend(pcd);
  79059. + DWC_SPINLOCK(pcd->lock);
  79060. + }
  79061. +
  79062. + return 1;
  79063. +}
  79064. +
  79065. +/**
  79066. + * PCD Callback function for stopping the PCD when switching to Host
  79067. + * mode.
  79068. + *
  79069. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  79070. + */
  79071. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  79072. +{
  79073. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  79074. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  79075. +
  79076. + dwc_otg_pcd_stop(pcd);
  79077. + return 1;
  79078. +}
  79079. +
  79080. +/**
  79081. + * PCD Callback structure for handling mode switching.
  79082. + */
  79083. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  79084. + .start = dwc_otg_pcd_start_cb,
  79085. + .stop = dwc_otg_pcd_stop_cb,
  79086. + .suspend = dwc_otg_pcd_suspend_cb,
  79087. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  79088. + .p = 0, /* Set at registration */
  79089. +};
  79090. +
  79091. +/**
  79092. + * This function allocates a DMA Descriptor chain for the Endpoint
  79093. + * buffer to be used for a transfer to/from the specified endpoint.
  79094. + */
  79095. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(dwc_dma_t * dma_desc_addr,
  79096. + uint32_t count)
  79097. +{
  79098. + return DWC_DMA_ALLOC_ATOMIC(count * sizeof(dwc_otg_dev_dma_desc_t),
  79099. + dma_desc_addr);
  79100. +}
  79101. +
  79102. +/**
  79103. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  79104. + */
  79105. +void dwc_otg_ep_free_desc_chain(dwc_otg_dev_dma_desc_t * desc_addr,
  79106. + uint32_t dma_desc_addr, uint32_t count)
  79107. +{
  79108. + DWC_DMA_FREE(count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  79109. + dma_desc_addr);
  79110. +}
  79111. +
  79112. +#ifdef DWC_EN_ISOC
  79113. +
  79114. +/**
  79115. + * This function initializes a descriptor chain for Isochronous transfer
  79116. + *
  79117. + * @param core_if Programming view of DWC_otg controller.
  79118. + * @param dwc_ep The EP to start the transfer on.
  79119. + *
  79120. + */
  79121. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  79122. + dwc_ep_t * dwc_ep)
  79123. +{
  79124. +
  79125. + dsts_data_t dsts = {.d32 = 0 };
  79126. + depctl_data_t depctl = {.d32 = 0 };
  79127. + volatile uint32_t *addr;
  79128. + int i, j;
  79129. + uint32_t len;
  79130. +
  79131. + if (dwc_ep->is_in)
  79132. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  79133. + else
  79134. + dwc_ep->desc_cnt =
  79135. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  79136. + dwc_ep->bInterval;
  79137. +
  79138. + /** Allocate descriptors for double buffering */
  79139. + dwc_ep->iso_desc_addr =
  79140. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  79141. + dwc_ep->desc_cnt * 2);
  79142. + if (dwc_ep->desc_addr) {
  79143. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  79144. + return;
  79145. + }
  79146. +
  79147. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  79148. +
  79149. + /** ISO OUT EP */
  79150. + if (dwc_ep->is_in == 0) {
  79151. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  79152. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  79153. + dma_addr_t dma_ad;
  79154. + uint32_t data_per_desc;
  79155. + dwc_otg_dev_out_ep_regs_t *out_regs =
  79156. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  79157. + int offset;
  79158. +
  79159. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  79160. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  79161. +
  79162. + /** Buffer 0 descriptors setup */
  79163. + dma_ad = dwc_ep->dma_addr0;
  79164. +
  79165. + sts.b_iso_out.bs = BS_HOST_READY;
  79166. + sts.b_iso_out.rxsts = 0;
  79167. + sts.b_iso_out.l = 0;
  79168. + sts.b_iso_out.sp = 0;
  79169. + sts.b_iso_out.ioc = 0;
  79170. + sts.b_iso_out.pid = 0;
  79171. + sts.b_iso_out.framenum = 0;
  79172. +
  79173. + offset = 0;
  79174. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  79175. + i += dwc_ep->pkt_per_frm) {
  79176. +
  79177. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  79178. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79179. + if (len > dwc_ep->data_per_frame)
  79180. + data_per_desc =
  79181. + dwc_ep->data_per_frame -
  79182. + j * dwc_ep->maxpacket;
  79183. + else
  79184. + data_per_desc = dwc_ep->maxpacket;
  79185. + len = data_per_desc % 4;
  79186. + if (len)
  79187. + data_per_desc += 4 - len;
  79188. +
  79189. + sts.b_iso_out.rxbytes = data_per_desc;
  79190. + dma_desc->buf = dma_ad;
  79191. + dma_desc->status.d32 = sts.d32;
  79192. +
  79193. + offset += data_per_desc;
  79194. + dma_desc++;
  79195. + dma_ad += data_per_desc;
  79196. + }
  79197. + }
  79198. +
  79199. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  79200. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79201. + if (len > dwc_ep->data_per_frame)
  79202. + data_per_desc =
  79203. + dwc_ep->data_per_frame -
  79204. + j * dwc_ep->maxpacket;
  79205. + else
  79206. + data_per_desc = dwc_ep->maxpacket;
  79207. + len = data_per_desc % 4;
  79208. + if (len)
  79209. + data_per_desc += 4 - len;
  79210. + sts.b_iso_out.rxbytes = data_per_desc;
  79211. + dma_desc->buf = dma_ad;
  79212. + dma_desc->status.d32 = sts.d32;
  79213. +
  79214. + offset += data_per_desc;
  79215. + dma_desc++;
  79216. + dma_ad += data_per_desc;
  79217. + }
  79218. +
  79219. + sts.b_iso_out.ioc = 1;
  79220. + len = (j + 1) * dwc_ep->maxpacket;
  79221. + if (len > dwc_ep->data_per_frame)
  79222. + data_per_desc =
  79223. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  79224. + else
  79225. + data_per_desc = dwc_ep->maxpacket;
  79226. + len = data_per_desc % 4;
  79227. + if (len)
  79228. + data_per_desc += 4 - len;
  79229. + sts.b_iso_out.rxbytes = data_per_desc;
  79230. +
  79231. + dma_desc->buf = dma_ad;
  79232. + dma_desc->status.d32 = sts.d32;
  79233. + dma_desc++;
  79234. +
  79235. + /** Buffer 1 descriptors setup */
  79236. + sts.b_iso_out.ioc = 0;
  79237. + dma_ad = dwc_ep->dma_addr1;
  79238. +
  79239. + offset = 0;
  79240. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  79241. + i += dwc_ep->pkt_per_frm) {
  79242. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  79243. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  79244. + if (len > dwc_ep->data_per_frame)
  79245. + data_per_desc =
  79246. + dwc_ep->data_per_frame -
  79247. + j * dwc_ep->maxpacket;
  79248. + else
  79249. + data_per_desc = dwc_ep->maxpacket;
  79250. + len = data_per_desc % 4;
  79251. + if (len)
  79252. + data_per_desc += 4 - len;
  79253. +
  79254. + data_per_desc =
  79255. + sts.b_iso_out.rxbytes = data_per_desc;
  79256. + dma_desc->buf = dma_ad;
  79257. + dma_desc->status.d32 = sts.d32;
  79258. +
  79259. + offset += data_per_desc;
  79260. + dma_desc++;
  79261. + dma_ad += data_per_desc;
  79262. + }
  79263. + }
  79264. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  79265. + data_per_desc =
  79266. + ((j + 1) * dwc_ep->maxpacket >
  79267. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  79268. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  79269. + data_per_desc +=
  79270. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  79271. + sts.b_iso_out.rxbytes = data_per_desc;
  79272. + dma_desc->buf = dma_ad;
  79273. + dma_desc->status.d32 = sts.d32;
  79274. +
  79275. + offset += data_per_desc;
  79276. + dma_desc++;
  79277. + dma_ad += data_per_desc;
  79278. + }
  79279. +
  79280. + sts.b_iso_out.ioc = 1;
  79281. + sts.b_iso_out.l = 1;
  79282. + data_per_desc =
  79283. + ((j + 1) * dwc_ep->maxpacket >
  79284. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  79285. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  79286. + data_per_desc +=
  79287. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  79288. + sts.b_iso_out.rxbytes = data_per_desc;
  79289. +
  79290. + dma_desc->buf = dma_ad;
  79291. + dma_desc->status.d32 = sts.d32;
  79292. +
  79293. + dwc_ep->next_frame = 0;
  79294. +
  79295. + /** Write dma_ad into DOEPDMA register */
  79296. + DWC_WRITE_REG32(&(out_regs->doepdma),
  79297. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  79298. +
  79299. + }
  79300. + /** ISO IN EP */
  79301. + else {
  79302. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  79303. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  79304. + dma_addr_t dma_ad;
  79305. + dwc_otg_dev_in_ep_regs_t *in_regs =
  79306. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  79307. + unsigned int frmnumber;
  79308. + fifosize_data_t txfifosize, rxfifosize;
  79309. +
  79310. + txfifosize.d32 =
  79311. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  79312. + dtxfsts);
  79313. + rxfifosize.d32 =
  79314. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  79315. +
  79316. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  79317. +
  79318. + dma_ad = dwc_ep->dma_addr0;
  79319. +
  79320. + dsts.d32 =
  79321. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  79322. +
  79323. + sts.b_iso_in.bs = BS_HOST_READY;
  79324. + sts.b_iso_in.txsts = 0;
  79325. + sts.b_iso_in.sp =
  79326. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  79327. + sts.b_iso_in.ioc = 0;
  79328. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  79329. +
  79330. + frmnumber = dwc_ep->next_frame;
  79331. +
  79332. + sts.b_iso_in.framenum = frmnumber;
  79333. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  79334. + sts.b_iso_in.l = 0;
  79335. +
  79336. + /** Buffer 0 descriptors setup */
  79337. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  79338. + dma_desc->buf = dma_ad;
  79339. + dma_desc->status.d32 = sts.d32;
  79340. + dma_desc++;
  79341. +
  79342. + dma_ad += dwc_ep->data_per_frame;
  79343. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  79344. + }
  79345. +
  79346. + sts.b_iso_in.ioc = 1;
  79347. + dma_desc->buf = dma_ad;
  79348. + dma_desc->status.d32 = sts.d32;
  79349. + ++dma_desc;
  79350. +
  79351. + /** Buffer 1 descriptors setup */
  79352. + sts.b_iso_in.ioc = 0;
  79353. + dma_ad = dwc_ep->dma_addr1;
  79354. +
  79355. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  79356. + i += dwc_ep->pkt_per_frm) {
  79357. + dma_desc->buf = dma_ad;
  79358. + dma_desc->status.d32 = sts.d32;
  79359. + dma_desc++;
  79360. +
  79361. + dma_ad += dwc_ep->data_per_frame;
  79362. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  79363. +
  79364. + sts.b_iso_in.ioc = 0;
  79365. + }
  79366. + sts.b_iso_in.ioc = 1;
  79367. + sts.b_iso_in.l = 1;
  79368. +
  79369. + dma_desc->buf = dma_ad;
  79370. + dma_desc->status.d32 = sts.d32;
  79371. +
  79372. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  79373. +
  79374. + /** Write dma_ad into diepdma register */
  79375. + DWC_WRITE_REG32(&(in_regs->diepdma),
  79376. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  79377. + }
  79378. + /** Enable endpoint, clear nak */
  79379. + depctl.d32 = 0;
  79380. + depctl.b.epena = 1;
  79381. + depctl.b.usbactep = 1;
  79382. + depctl.b.cnak = 1;
  79383. +
  79384. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  79385. + depctl.d32 = DWC_READ_REG32(addr);
  79386. +}
  79387. +
  79388. +/**
  79389. + * This function initializes a descriptor chain for Isochronous transfer
  79390. + *
  79391. + * @param core_if Programming view of DWC_otg controller.
  79392. + * @param ep The EP to start the transfer on.
  79393. + *
  79394. + */
  79395. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  79396. + dwc_ep_t * ep)
  79397. +{
  79398. + depctl_data_t depctl = {.d32 = 0 };
  79399. + volatile uint32_t *addr;
  79400. +
  79401. + if (ep->is_in) {
  79402. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  79403. + } else {
  79404. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  79405. + }
  79406. +
  79407. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  79408. + return;
  79409. + } else {
  79410. + deptsiz_data_t deptsiz = {.d32 = 0 };
  79411. +
  79412. + ep->xfer_len =
  79413. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  79414. + ep->pkt_cnt =
  79415. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  79416. + ep->xfer_count = 0;
  79417. + ep->xfer_buff =
  79418. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  79419. + ep->dma_addr =
  79420. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  79421. +
  79422. + if (ep->is_in) {
  79423. + /* Program the transfer size and packet count
  79424. + * as follows: xfersize = N * maxpacket +
  79425. + * short_packet pktcnt = N + (short_packet
  79426. + * exist ? 1 : 0)
  79427. + */
  79428. + deptsiz.b.mc = ep->pkt_per_frm;
  79429. + deptsiz.b.xfersize = ep->xfer_len;
  79430. + deptsiz.b.pktcnt =
  79431. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  79432. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  79433. + dieptsiz, deptsiz.d32);
  79434. +
  79435. + /* Write the DMA register */
  79436. + DWC_WRITE_REG32(&
  79437. + (core_if->dev_if->in_ep_regs[ep->num]->
  79438. + diepdma), (uint32_t) ep->dma_addr);
  79439. +
  79440. + } else {
  79441. + deptsiz.b.pktcnt =
  79442. + (ep->xfer_len + (ep->maxpacket - 1)) /
  79443. + ep->maxpacket;
  79444. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  79445. +
  79446. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  79447. + doeptsiz, deptsiz.d32);
  79448. +
  79449. + /* Write the DMA register */
  79450. + DWC_WRITE_REG32(&
  79451. + (core_if->dev_if->out_ep_regs[ep->num]->
  79452. + doepdma), (uint32_t) ep->dma_addr);
  79453. +
  79454. + }
  79455. + /** Enable endpoint, clear nak */
  79456. + depctl.d32 = 0;
  79457. + depctl.b.epena = 1;
  79458. + depctl.b.cnak = 1;
  79459. +
  79460. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  79461. + }
  79462. +}
  79463. +
  79464. +/**
  79465. + * This function does the setup for a data transfer for an EP and
  79466. + * starts the transfer. For an IN transfer, the packets will be
  79467. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  79468. + * the packets are unloaded from the Rx FIFO in the ISR.
  79469. + *
  79470. + * @param core_if Programming view of DWC_otg controller.
  79471. + * @param ep The EP to start the transfer on.
  79472. + */
  79473. +
  79474. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  79475. + dwc_ep_t * ep)
  79476. +{
  79477. + if (core_if->dma_enable) {
  79478. + if (core_if->dma_desc_enable) {
  79479. + if (ep->is_in) {
  79480. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  79481. + } else {
  79482. + ep->desc_cnt = ep->pkt_cnt;
  79483. + }
  79484. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  79485. + } else {
  79486. + if (core_if->pti_enh_enable) {
  79487. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  79488. + } else {
  79489. + ep->cur_pkt_addr =
  79490. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  79491. + xfer_buff0;
  79492. + ep->cur_pkt_dma_addr =
  79493. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  79494. + dma_addr0;
  79495. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  79496. + }
  79497. + }
  79498. + } else {
  79499. + ep->cur_pkt_addr =
  79500. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  79501. + ep->cur_pkt_dma_addr =
  79502. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  79503. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  79504. + }
  79505. +}
  79506. +
  79507. +/**
  79508. + * This function stops transfer for an EP and
  79509. + * resets the ep's variables.
  79510. + *
  79511. + * @param core_if Programming view of DWC_otg controller.
  79512. + * @param ep The EP to start the transfer on.
  79513. + */
  79514. +
  79515. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  79516. +{
  79517. + depctl_data_t depctl = {.d32 = 0 };
  79518. + volatile uint32_t *addr;
  79519. +
  79520. + if (ep->is_in == 1) {
  79521. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  79522. + } else {
  79523. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  79524. + }
  79525. +
  79526. + /* disable the ep */
  79527. + depctl.d32 = DWC_READ_REG32(addr);
  79528. +
  79529. + depctl.b.epdis = 1;
  79530. + depctl.b.snak = 1;
  79531. +
  79532. + DWC_WRITE_REG32(addr, depctl.d32);
  79533. +
  79534. + if (core_if->dma_desc_enable &&
  79535. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  79536. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  79537. + ep->iso_dma_desc_addr,
  79538. + ep->desc_cnt * 2);
  79539. + }
  79540. +
  79541. + /* reset varibales */
  79542. + ep->dma_addr0 = 0;
  79543. + ep->dma_addr1 = 0;
  79544. + ep->xfer_buff0 = 0;
  79545. + ep->xfer_buff1 = 0;
  79546. + ep->data_per_frame = 0;
  79547. + ep->data_pattern_frame = 0;
  79548. + ep->sync_frame = 0;
  79549. + ep->buf_proc_intrvl = 0;
  79550. + ep->bInterval = 0;
  79551. + ep->proc_buf_num = 0;
  79552. + ep->pkt_per_frm = 0;
  79553. + ep->pkt_per_frm = 0;
  79554. + ep->desc_cnt = 0;
  79555. + ep->iso_desc_addr = 0;
  79556. + ep->iso_dma_desc_addr = 0;
  79557. +}
  79558. +
  79559. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  79560. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  79561. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  79562. + int data_per_frame, int start_frame,
  79563. + int buf_proc_intrvl, void *req_handle,
  79564. + int atomic_alloc)
  79565. +{
  79566. + dwc_otg_pcd_ep_t *ep;
  79567. + dwc_irqflags_t flags = 0;
  79568. + dwc_ep_t *dwc_ep;
  79569. + int32_t frm_data;
  79570. + dsts_data_t dsts;
  79571. + dwc_otg_core_if_t *core_if;
  79572. +
  79573. + ep = get_ep_from_handle(pcd, ep_handle);
  79574. +
  79575. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  79576. + DWC_WARN("bad ep\n");
  79577. + return -DWC_E_INVALID;
  79578. + }
  79579. +
  79580. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  79581. + core_if = GET_CORE_IF(pcd);
  79582. + dwc_ep = &ep->dwc_ep;
  79583. +
  79584. + if (ep->iso_req_handle) {
  79585. + DWC_WARN("ISO request in progress\n");
  79586. + }
  79587. +
  79588. + dwc_ep->dma_addr0 = dma0;
  79589. + dwc_ep->dma_addr1 = dma1;
  79590. +
  79591. + dwc_ep->xfer_buff0 = buf0;
  79592. + dwc_ep->xfer_buff1 = buf1;
  79593. +
  79594. + dwc_ep->data_per_frame = data_per_frame;
  79595. +
  79596. + /** @todo - pattern data support is to be implemented in the future */
  79597. + dwc_ep->data_pattern_frame = dp_frame;
  79598. + dwc_ep->sync_frame = sync_frame;
  79599. +
  79600. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  79601. +
  79602. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  79603. +
  79604. + dwc_ep->proc_buf_num = 0;
  79605. +
  79606. + dwc_ep->pkt_per_frm = 0;
  79607. + frm_data = ep->dwc_ep.data_per_frame;
  79608. + while (frm_data > 0) {
  79609. + dwc_ep->pkt_per_frm++;
  79610. + frm_data -= ep->dwc_ep.maxpacket;
  79611. + }
  79612. +
  79613. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  79614. +
  79615. + if (start_frame == -1) {
  79616. + dwc_ep->next_frame = dsts.b.soffn + 1;
  79617. + if (dwc_ep->bInterval != 1) {
  79618. + dwc_ep->next_frame =
  79619. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  79620. + dwc_ep->next_frame %
  79621. + dwc_ep->bInterval);
  79622. + }
  79623. + } else {
  79624. + dwc_ep->next_frame = start_frame;
  79625. + }
  79626. +
  79627. + if (!core_if->pti_enh_enable) {
  79628. + dwc_ep->pkt_cnt =
  79629. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  79630. + dwc_ep->bInterval;
  79631. + } else {
  79632. + dwc_ep->pkt_cnt =
  79633. + (dwc_ep->data_per_frame *
  79634. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  79635. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  79636. + }
  79637. +
  79638. + if (core_if->dma_desc_enable) {
  79639. + dwc_ep->desc_cnt =
  79640. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  79641. + dwc_ep->bInterval;
  79642. + }
  79643. +
  79644. + if (atomic_alloc) {
  79645. + dwc_ep->pkt_info =
  79646. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  79647. + } else {
  79648. + dwc_ep->pkt_info =
  79649. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  79650. + }
  79651. + if (!dwc_ep->pkt_info) {
  79652. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  79653. + return -DWC_E_NO_MEMORY;
  79654. + }
  79655. + if (core_if->pti_enh_enable) {
  79656. + dwc_memset(dwc_ep->pkt_info, 0,
  79657. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  79658. + }
  79659. +
  79660. + dwc_ep->cur_pkt = 0;
  79661. + ep->iso_req_handle = req_handle;
  79662. +
  79663. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  79664. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  79665. + return 0;
  79666. +}
  79667. +
  79668. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  79669. + void *req_handle)
  79670. +{
  79671. + dwc_irqflags_t flags = 0;
  79672. + dwc_otg_pcd_ep_t *ep;
  79673. + dwc_ep_t *dwc_ep;
  79674. +
  79675. + ep = get_ep_from_handle(pcd, ep_handle);
  79676. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  79677. + DWC_WARN("bad ep\n");
  79678. + return -DWC_E_INVALID;
  79679. + }
  79680. + dwc_ep = &ep->dwc_ep;
  79681. +
  79682. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  79683. +
  79684. + DWC_FREE(dwc_ep->pkt_info);
  79685. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  79686. + if (ep->iso_req_handle != req_handle) {
  79687. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  79688. + return -DWC_E_INVALID;
  79689. + }
  79690. +
  79691. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  79692. +
  79693. + ep->iso_req_handle = 0;
  79694. + return 0;
  79695. +}
  79696. +
  79697. +/**
  79698. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  79699. + * for Isochronous EPs
  79700. + *
  79701. + * - Every time a sync period completes this function is called to
  79702. + * perform data exchange between PCD and gadget
  79703. + */
  79704. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  79705. + void *req_handle)
  79706. +{
  79707. + int i;
  79708. + dwc_ep_t *dwc_ep;
  79709. +
  79710. + dwc_ep = &ep->dwc_ep;
  79711. +
  79712. + DWC_SPINUNLOCK(ep->pcd->lock);
  79713. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  79714. + dwc_ep->proc_buf_num ^ 0x1);
  79715. + DWC_SPINLOCK(ep->pcd->lock);
  79716. +
  79717. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  79718. + dwc_ep->pkt_info[i].status = 0;
  79719. + dwc_ep->pkt_info[i].offset = 0;
  79720. + dwc_ep->pkt_info[i].length = 0;
  79721. + }
  79722. +}
  79723. +
  79724. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  79725. + void *iso_req_handle)
  79726. +{
  79727. + dwc_otg_pcd_ep_t *ep;
  79728. + dwc_ep_t *dwc_ep;
  79729. +
  79730. + ep = get_ep_from_handle(pcd, ep_handle);
  79731. + if (!ep->desc || ep->dwc_ep.num == 0) {
  79732. + DWC_WARN("bad ep\n");
  79733. + return -DWC_E_INVALID;
  79734. + }
  79735. + dwc_ep = &ep->dwc_ep;
  79736. +
  79737. + return dwc_ep->pkt_cnt;
  79738. +}
  79739. +
  79740. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  79741. + void *iso_req_handle, int packet,
  79742. + int *status, int *actual, int *offset)
  79743. +{
  79744. + dwc_otg_pcd_ep_t *ep;
  79745. + dwc_ep_t *dwc_ep;
  79746. +
  79747. + ep = get_ep_from_handle(pcd, ep_handle);
  79748. + if (!ep)
  79749. + DWC_WARN("bad ep\n");
  79750. +
  79751. + dwc_ep = &ep->dwc_ep;
  79752. +
  79753. + *status = dwc_ep->pkt_info[packet].status;
  79754. + *actual = dwc_ep->pkt_info[packet].length;
  79755. + *offset = dwc_ep->pkt_info[packet].offset;
  79756. +}
  79757. +
  79758. +#endif /* DWC_EN_ISOC */
  79759. +
  79760. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  79761. + uint32_t is_in, uint32_t ep_num)
  79762. +{
  79763. + /* Init EP structure */
  79764. + pcd_ep->desc = 0;
  79765. + pcd_ep->pcd = pcd;
  79766. + pcd_ep->stopped = 1;
  79767. + pcd_ep->queue_sof = 0;
  79768. +
  79769. + /* Init DWC ep structure */
  79770. + pcd_ep->dwc_ep.is_in = is_in;
  79771. + pcd_ep->dwc_ep.num = ep_num;
  79772. + pcd_ep->dwc_ep.active = 0;
  79773. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  79774. + /* Control until ep is actvated */
  79775. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  79776. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  79777. + pcd_ep->dwc_ep.dma_addr = 0;
  79778. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  79779. + pcd_ep->dwc_ep.xfer_buff = 0;
  79780. + pcd_ep->dwc_ep.xfer_len = 0;
  79781. + pcd_ep->dwc_ep.xfer_count = 0;
  79782. + pcd_ep->dwc_ep.sent_zlp = 0;
  79783. + pcd_ep->dwc_ep.total_len = 0;
  79784. + pcd_ep->dwc_ep.desc_addr = 0;
  79785. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  79786. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  79787. +}
  79788. +
  79789. +/**
  79790. + * Initialize ep's
  79791. + */
  79792. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  79793. +{
  79794. + int i;
  79795. + uint32_t hwcfg1;
  79796. + dwc_otg_pcd_ep_t *ep;
  79797. + int in_ep_cntr, out_ep_cntr;
  79798. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  79799. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  79800. +
  79801. + /**
  79802. + * Initialize the EP0 structure.
  79803. + */
  79804. + ep = &pcd->ep0;
  79805. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  79806. +
  79807. + in_ep_cntr = 0;
  79808. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  79809. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  79810. + if ((hwcfg1 & 0x1) == 0) {
  79811. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  79812. + in_ep_cntr++;
  79813. + /**
  79814. + * @todo NGS: Add direction to EP, based on contents
  79815. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  79816. + * sprintf(";r
  79817. + */
  79818. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  79819. +
  79820. + DWC_CIRCLEQ_INIT(&ep->queue);
  79821. + }
  79822. + hwcfg1 >>= 2;
  79823. + }
  79824. +
  79825. + out_ep_cntr = 0;
  79826. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  79827. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  79828. + if ((hwcfg1 & 0x1) == 0) {
  79829. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  79830. + out_ep_cntr++;
  79831. + /**
  79832. + * @todo NGS: Add direction to EP, based on contents
  79833. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  79834. + * sprintf(";r
  79835. + */
  79836. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  79837. + DWC_CIRCLEQ_INIT(&ep->queue);
  79838. + }
  79839. + hwcfg1 >>= 2;
  79840. + }
  79841. +
  79842. + pcd->ep0state = EP0_DISCONNECT;
  79843. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  79844. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  79845. +}
  79846. +
  79847. +/**
  79848. + * This function is called when the SRP timer expires. The SRP should
  79849. + * complete within 6 seconds.
  79850. + */
  79851. +static void srp_timeout(void *ptr)
  79852. +{
  79853. + gotgctl_data_t gotgctl;
  79854. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  79855. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  79856. +
  79857. + gotgctl.d32 = DWC_READ_REG32(addr);
  79858. +
  79859. + core_if->srp_timer_started = 0;
  79860. +
  79861. + if (core_if->adp_enable) {
  79862. + if (gotgctl.b.bsesvld == 0) {
  79863. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  79864. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  79865. + /* Power off the core */
  79866. + if (core_if->power_down == 2) {
  79867. + gpwrdn.b.pwrdnswtch = 1;
  79868. + DWC_MODIFY_REG32(&core_if->
  79869. + core_global_regs->gpwrdn,
  79870. + gpwrdn.d32, 0);
  79871. + }
  79872. +
  79873. + gpwrdn.d32 = 0;
  79874. + gpwrdn.b.pmuintsel = 1;
  79875. + gpwrdn.b.pmuactv = 1;
  79876. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  79877. + gpwrdn.d32);
  79878. + dwc_otg_adp_probe_start(core_if);
  79879. + } else {
  79880. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  79881. + core_if->op_state = B_PERIPHERAL;
  79882. + dwc_otg_core_init(core_if);
  79883. + dwc_otg_enable_global_interrupts(core_if);
  79884. + cil_pcd_start(core_if);
  79885. + }
  79886. + }
  79887. +
  79888. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  79889. + (core_if->core_params->i2c_enable)) {
  79890. + DWC_PRINTF("SRP Timeout\n");
  79891. +
  79892. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  79893. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  79894. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  79895. + }
  79896. +
  79897. + /* Clear Session Request */
  79898. + gotgctl.d32 = 0;
  79899. + gotgctl.b.sesreq = 1;
  79900. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  79901. + gotgctl.d32, 0);
  79902. +
  79903. + core_if->srp_success = 0;
  79904. + } else {
  79905. + __DWC_ERROR("Device not connected/responding\n");
  79906. + gotgctl.b.sesreq = 0;
  79907. + DWC_WRITE_REG32(addr, gotgctl.d32);
  79908. + }
  79909. + } else if (gotgctl.b.sesreq) {
  79910. + DWC_PRINTF("SRP Timeout\n");
  79911. +
  79912. + __DWC_ERROR("Device not connected/responding\n");
  79913. + gotgctl.b.sesreq = 0;
  79914. + DWC_WRITE_REG32(addr, gotgctl.d32);
  79915. + } else {
  79916. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  79917. + }
  79918. +}
  79919. +
  79920. +/**
  79921. + * Tasklet
  79922. + *
  79923. + */
  79924. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  79925. +
  79926. +static void start_xfer_tasklet_func(void *data)
  79927. +{
  79928. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  79929. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  79930. +
  79931. + int i;
  79932. + depctl_data_t diepctl;
  79933. +
  79934. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  79935. +
  79936. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  79937. +
  79938. + if (pcd->ep0.queue_sof) {
  79939. + pcd->ep0.queue_sof = 0;
  79940. + start_next_request(&pcd->ep0);
  79941. + // break;
  79942. + }
  79943. +
  79944. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  79945. + depctl_data_t diepctl;
  79946. + diepctl.d32 =
  79947. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  79948. +
  79949. + if (pcd->in_ep[i].queue_sof) {
  79950. + pcd->in_ep[i].queue_sof = 0;
  79951. + start_next_request(&pcd->in_ep[i]);
  79952. + // break;
  79953. + }
  79954. + }
  79955. +
  79956. + return;
  79957. +}
  79958. +
  79959. +/**
  79960. + * This function initialized the PCD portion of the driver.
  79961. + *
  79962. + */
  79963. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if)
  79964. +{
  79965. + dwc_otg_pcd_t *pcd = NULL;
  79966. + dwc_otg_dev_if_t *dev_if;
  79967. + int i;
  79968. +
  79969. + /*
  79970. + * Allocate PCD structure
  79971. + */
  79972. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  79973. +
  79974. + if (pcd == NULL) {
  79975. + return NULL;
  79976. + }
  79977. +
  79978. + pcd->lock = DWC_SPINLOCK_ALLOC();
  79979. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  79980. + pcd, core_if);//GRAYG
  79981. + if (!pcd->lock) {
  79982. + DWC_ERROR("Could not allocate lock for pcd");
  79983. + DWC_FREE(pcd);
  79984. + return NULL;
  79985. + }
  79986. + /* Set core_if's lock pointer to hcd->lock */
  79987. + core_if->lock = pcd->lock;
  79988. + pcd->core_if = core_if;
  79989. +
  79990. + dev_if = core_if->dev_if;
  79991. + dev_if->isoc_ep = NULL;
  79992. +
  79993. + if (core_if->hwcfg4.b.ded_fifo_en) {
  79994. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  79995. + } else {
  79996. + DWC_PRINTF("Shared Tx FIFO mode\n");
  79997. + }
  79998. +
  79999. + /*
  80000. + * Initialized the Core for Device mode here if there is nod ADP support.
  80001. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  80002. + */
  80003. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  80004. + dwc_otg_core_dev_init(core_if);
  80005. + }
  80006. +
  80007. + /*
  80008. + * Register the PCD Callbacks.
  80009. + */
  80010. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  80011. +
  80012. + /*
  80013. + * Initialize the DMA buffer for SETUP packets
  80014. + */
  80015. + if (GET_CORE_IF(pcd)->dma_enable) {
  80016. + pcd->setup_pkt =
  80017. + DWC_DMA_ALLOC(sizeof(*pcd->setup_pkt) * 5,
  80018. + &pcd->setup_pkt_dma_handle);
  80019. + if (pcd->setup_pkt == NULL) {
  80020. + DWC_FREE(pcd);
  80021. + return NULL;
  80022. + }
  80023. +
  80024. + pcd->status_buf =
  80025. + DWC_DMA_ALLOC(sizeof(uint16_t),
  80026. + &pcd->status_buf_dma_handle);
  80027. + if (pcd->status_buf == NULL) {
  80028. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  80029. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  80030. + DWC_FREE(pcd);
  80031. + return NULL;
  80032. + }
  80033. +
  80034. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  80035. + dev_if->setup_desc_addr[0] =
  80036. + dwc_otg_ep_alloc_desc_chain
  80037. + (&dev_if->dma_setup_desc_addr[0], 1);
  80038. + dev_if->setup_desc_addr[1] =
  80039. + dwc_otg_ep_alloc_desc_chain
  80040. + (&dev_if->dma_setup_desc_addr[1], 1);
  80041. + dev_if->in_desc_addr =
  80042. + dwc_otg_ep_alloc_desc_chain
  80043. + (&dev_if->dma_in_desc_addr, 1);
  80044. + dev_if->out_desc_addr =
  80045. + dwc_otg_ep_alloc_desc_chain
  80046. + (&dev_if->dma_out_desc_addr, 1);
  80047. + pcd->data_terminated = 0;
  80048. +
  80049. + if (dev_if->setup_desc_addr[0] == 0
  80050. + || dev_if->setup_desc_addr[1] == 0
  80051. + || dev_if->in_desc_addr == 0
  80052. + || dev_if->out_desc_addr == 0) {
  80053. +
  80054. + if (dev_if->out_desc_addr)
  80055. + dwc_otg_ep_free_desc_chain
  80056. + (dev_if->out_desc_addr,
  80057. + dev_if->dma_out_desc_addr, 1);
  80058. + if (dev_if->in_desc_addr)
  80059. + dwc_otg_ep_free_desc_chain
  80060. + (dev_if->in_desc_addr,
  80061. + dev_if->dma_in_desc_addr, 1);
  80062. + if (dev_if->setup_desc_addr[1])
  80063. + dwc_otg_ep_free_desc_chain
  80064. + (dev_if->setup_desc_addr[1],
  80065. + dev_if->dma_setup_desc_addr[1], 1);
  80066. + if (dev_if->setup_desc_addr[0])
  80067. + dwc_otg_ep_free_desc_chain
  80068. + (dev_if->setup_desc_addr[0],
  80069. + dev_if->dma_setup_desc_addr[0], 1);
  80070. +
  80071. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  80072. + pcd->setup_pkt,
  80073. + pcd->setup_pkt_dma_handle);
  80074. + DWC_DMA_FREE(sizeof(*pcd->status_buf),
  80075. + pcd->status_buf,
  80076. + pcd->status_buf_dma_handle);
  80077. +
  80078. + DWC_FREE(pcd);
  80079. +
  80080. + return NULL;
  80081. + }
  80082. + }
  80083. + } else {
  80084. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  80085. + if (pcd->setup_pkt == NULL) {
  80086. + DWC_FREE(pcd);
  80087. + return NULL;
  80088. + }
  80089. +
  80090. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  80091. + if (pcd->status_buf == NULL) {
  80092. + DWC_FREE(pcd->setup_pkt);
  80093. + DWC_FREE(pcd);
  80094. + return NULL;
  80095. + }
  80096. + }
  80097. +
  80098. + dwc_otg_pcd_reinit(pcd);
  80099. +
  80100. + /* Allocate the cfi object for the PCD */
  80101. +#ifdef DWC_UTE_CFI
  80102. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  80103. + if (NULL == pcd->cfi)
  80104. + goto fail;
  80105. + if (init_cfi(pcd->cfi)) {
  80106. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  80107. + goto fail;
  80108. + }
  80109. +#endif
  80110. +
  80111. + /* Initialize tasklets */
  80112. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  80113. + start_xfer_tasklet_func, pcd);
  80114. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  80115. + do_test_mode, pcd);
  80116. +
  80117. + /* Initialize SRP timer */
  80118. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  80119. +
  80120. + if (core_if->core_params->dev_out_nak) {
  80121. + /**
  80122. + * Initialize xfer timeout timer. Implemented for
  80123. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  80124. + */
  80125. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  80126. + pcd->core_if->ep_xfer_timer[i] =
  80127. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  80128. + &pcd->core_if->ep_xfer_info[i]);
  80129. + }
  80130. + }
  80131. +
  80132. + return pcd;
  80133. +#ifdef DWC_UTE_CFI
  80134. +fail:
  80135. +#endif
  80136. + if (pcd->setup_pkt)
  80137. + DWC_FREE(pcd->setup_pkt);
  80138. + if (pcd->status_buf)
  80139. + DWC_FREE(pcd->status_buf);
  80140. +#ifdef DWC_UTE_CFI
  80141. + if (pcd->cfi)
  80142. + DWC_FREE(pcd->cfi);
  80143. +#endif
  80144. + if (pcd)
  80145. + DWC_FREE(pcd);
  80146. + return NULL;
  80147. +
  80148. +}
  80149. +
  80150. +/**
  80151. + * Remove PCD specific data
  80152. + */
  80153. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  80154. +{
  80155. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  80156. + int i;
  80157. + if (pcd->core_if->core_params->dev_out_nak) {
  80158. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  80159. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  80160. + pcd->core_if->ep_xfer_info[i].state = 0;
  80161. + }
  80162. + }
  80163. +
  80164. + if (GET_CORE_IF(pcd)->dma_enable) {
  80165. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  80166. + pcd->setup_pkt_dma_handle);
  80167. + DWC_DMA_FREE(sizeof(uint16_t), pcd->status_buf,
  80168. + pcd->status_buf_dma_handle);
  80169. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  80170. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[0],
  80171. + dev_if->dma_setup_desc_addr
  80172. + [0], 1);
  80173. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[1],
  80174. + dev_if->dma_setup_desc_addr
  80175. + [1], 1);
  80176. + dwc_otg_ep_free_desc_chain(dev_if->in_desc_addr,
  80177. + dev_if->dma_in_desc_addr, 1);
  80178. + dwc_otg_ep_free_desc_chain(dev_if->out_desc_addr,
  80179. + dev_if->dma_out_desc_addr,
  80180. + 1);
  80181. + }
  80182. + } else {
  80183. + DWC_FREE(pcd->setup_pkt);
  80184. + DWC_FREE(pcd->status_buf);
  80185. + }
  80186. + DWC_SPINLOCK_FREE(pcd->lock);
  80187. + /* Set core_if's lock pointer to NULL */
  80188. + pcd->core_if->lock = NULL;
  80189. +
  80190. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  80191. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  80192. + if (pcd->core_if->core_params->dev_out_nak) {
  80193. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  80194. + if (pcd->core_if->ep_xfer_timer[i]) {
  80195. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  80196. + }
  80197. + }
  80198. + }
  80199. +
  80200. +/* Release the CFI object's dynamic memory */
  80201. +#ifdef DWC_UTE_CFI
  80202. + if (pcd->cfi->ops.release) {
  80203. + pcd->cfi->ops.release(pcd->cfi);
  80204. + }
  80205. +#endif
  80206. +
  80207. + DWC_FREE(pcd);
  80208. +}
  80209. +
  80210. +/**
  80211. + * Returns whether registered pcd is dual speed or not
  80212. + */
  80213. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  80214. +{
  80215. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  80216. +
  80217. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  80218. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  80219. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  80220. + (core_if->core_params->ulpi_fs_ls))) {
  80221. + return 0;
  80222. + }
  80223. +
  80224. + return 1;
  80225. +}
  80226. +
  80227. +/**
  80228. + * Returns whether registered pcd is OTG capable or not
  80229. + */
  80230. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  80231. +{
  80232. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  80233. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  80234. +
  80235. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  80236. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  80237. + return 0;
  80238. + }
  80239. +
  80240. + return 1;
  80241. +}
  80242. +
  80243. +/**
  80244. + * This function assigns periodic Tx FIFO to an periodic EP
  80245. + * in shared Tx FIFO mode
  80246. + */
  80247. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  80248. +{
  80249. + uint32_t TxMsk = 1;
  80250. + int i;
  80251. +
  80252. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  80253. + if ((TxMsk & core_if->tx_msk) == 0) {
  80254. + core_if->tx_msk |= TxMsk;
  80255. + return i + 1;
  80256. + }
  80257. + TxMsk <<= 1;
  80258. + }
  80259. + return 0;
  80260. +}
  80261. +
  80262. +/**
  80263. + * This function assigns periodic Tx FIFO to an periodic EP
  80264. + * in shared Tx FIFO mode
  80265. + */
  80266. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  80267. +{
  80268. + uint32_t PerTxMsk = 1;
  80269. + int i;
  80270. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  80271. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  80272. + core_if->p_tx_msk |= PerTxMsk;
  80273. + return i + 1;
  80274. + }
  80275. + PerTxMsk <<= 1;
  80276. + }
  80277. + return 0;
  80278. +}
  80279. +
  80280. +/**
  80281. + * This function releases periodic Tx FIFO
  80282. + * in shared Tx FIFO mode
  80283. + */
  80284. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  80285. + uint32_t fifo_num)
  80286. +{
  80287. + core_if->p_tx_msk =
  80288. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  80289. +}
  80290. +
  80291. +/**
  80292. + * This function releases periodic Tx FIFO
  80293. + * in shared Tx FIFO mode
  80294. + */
  80295. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  80296. +{
  80297. + core_if->tx_msk =
  80298. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  80299. +}
  80300. +
  80301. +/**
  80302. + * This function is being called from gadget
  80303. + * to enable PCD endpoint.
  80304. + */
  80305. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  80306. + const uint8_t * ep_desc, void *usb_ep)
  80307. +{
  80308. + int num, dir;
  80309. + dwc_otg_pcd_ep_t *ep = NULL;
  80310. + const usb_endpoint_descriptor_t *desc;
  80311. + dwc_irqflags_t flags;
  80312. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  80313. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  80314. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  80315. + int retval = 0;
  80316. + int i, epcount;
  80317. +
  80318. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  80319. +
  80320. + if (!desc) {
  80321. + pcd->ep0.priv = usb_ep;
  80322. + ep = &pcd->ep0;
  80323. + retval = -DWC_E_INVALID;
  80324. + goto out;
  80325. + }
  80326. +
  80327. + num = UE_GET_ADDR(desc->bEndpointAddress);
  80328. + dir = UE_GET_DIR(desc->bEndpointAddress);
  80329. +
  80330. + if (!desc->wMaxPacketSize) {
  80331. + DWC_WARN("bad maxpacketsize\n");
  80332. + retval = -DWC_E_INVALID;
  80333. + goto out;
  80334. + }
  80335. +
  80336. + if (dir == UE_DIR_IN) {
  80337. + epcount = pcd->core_if->dev_if->num_in_eps;
  80338. + for (i = 0; i < epcount; i++) {
  80339. + if (num == pcd->in_ep[i].dwc_ep.num) {
  80340. + ep = &pcd->in_ep[i];
  80341. + break;
  80342. + }
  80343. + }
  80344. + } else {
  80345. + epcount = pcd->core_if->dev_if->num_out_eps;
  80346. + for (i = 0; i < epcount; i++) {
  80347. + if (num == pcd->out_ep[i].dwc_ep.num) {
  80348. + ep = &pcd->out_ep[i];
  80349. + break;
  80350. + }
  80351. + }
  80352. + }
  80353. +
  80354. + if (!ep) {
  80355. + DWC_WARN("bad address\n");
  80356. + retval = -DWC_E_INVALID;
  80357. + goto out;
  80358. + }
  80359. +
  80360. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80361. +
  80362. + ep->desc = desc;
  80363. + ep->priv = usb_ep;
  80364. +
  80365. + /*
  80366. + * Activate the EP
  80367. + */
  80368. + ep->stopped = 0;
  80369. +
  80370. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  80371. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  80372. +
  80373. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  80374. +
  80375. + if (ep->dwc_ep.is_in) {
  80376. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  80377. + ep->dwc_ep.tx_fifo_num = 0;
  80378. +
  80379. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  80380. + /*
  80381. + * if ISOC EP then assign a Periodic Tx FIFO.
  80382. + */
  80383. + ep->dwc_ep.tx_fifo_num =
  80384. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  80385. + }
  80386. + } else {
  80387. + /*
  80388. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  80389. + */
  80390. + ep->dwc_ep.tx_fifo_num =
  80391. + assign_tx_fifo(GET_CORE_IF(pcd));
  80392. + }
  80393. +
  80394. + /* Calculating EP info controller base address */
  80395. + if (ep->dwc_ep.tx_fifo_num
  80396. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  80397. + gdfifocfg.d32 =
  80398. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  80399. + core_global_regs->gdfifocfg);
  80400. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  80401. + dptxfsiz.d32 =
  80402. + (DWC_READ_REG32
  80403. + (&GET_CORE_IF(pcd)->core_global_regs->
  80404. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  80405. + gdfifocfg.b.epinfobase =
  80406. + gdfifocfgbase.d32 + dptxfsiz.d32;
  80407. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  80408. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  80409. + core_global_regs->gdfifocfg,
  80410. + gdfifocfg.d32);
  80411. + }
  80412. + }
  80413. + }
  80414. + /* Set initial data PID. */
  80415. + if (ep->dwc_ep.type == UE_BULK) {
  80416. + ep->dwc_ep.data_pid_start = 0;
  80417. + }
  80418. +
  80419. + /* Alloc DMA Descriptors */
  80420. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  80421. +#ifndef DWC_UTE_PER_IO
  80422. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  80423. +#endif
  80424. + ep->dwc_ep.desc_addr =
  80425. + dwc_otg_ep_alloc_desc_chain(&ep->
  80426. + dwc_ep.dma_desc_addr,
  80427. + MAX_DMA_DESC_CNT);
  80428. + if (!ep->dwc_ep.desc_addr) {
  80429. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  80430. + __func__);
  80431. + retval = -DWC_E_SHUTDOWN;
  80432. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80433. + goto out;
  80434. + }
  80435. +#ifndef DWC_UTE_PER_IO
  80436. + }
  80437. +#endif
  80438. + }
  80439. +
  80440. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  80441. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  80442. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  80443. +#ifdef DWC_UTE_PER_IO
  80444. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  80445. +#endif
  80446. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  80447. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  80448. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  80449. + }
  80450. +
  80451. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  80452. +
  80453. +#ifdef DWC_UTE_CFI
  80454. + if (pcd->cfi->ops.ep_enable) {
  80455. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  80456. + }
  80457. +#endif
  80458. +
  80459. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80460. +
  80461. +out:
  80462. + return retval;
  80463. +}
  80464. +
  80465. +/**
  80466. + * This function is being called from gadget
  80467. + * to disable PCD endpoint.
  80468. + */
  80469. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  80470. +{
  80471. + dwc_otg_pcd_ep_t *ep;
  80472. + dwc_irqflags_t flags;
  80473. + dwc_otg_dev_dma_desc_t *desc_addr;
  80474. + dwc_dma_t dma_desc_addr;
  80475. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  80476. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  80477. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  80478. +
  80479. + ep = get_ep_from_handle(pcd, ep_handle);
  80480. +
  80481. + if (!ep || !ep->desc) {
  80482. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  80483. + return -DWC_E_INVALID;
  80484. + }
  80485. +
  80486. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80487. +
  80488. + dwc_otg_request_nuke(ep);
  80489. +
  80490. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  80491. + if (pcd->core_if->core_params->dev_out_nak) {
  80492. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  80493. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  80494. + }
  80495. + ep->desc = NULL;
  80496. + ep->stopped = 1;
  80497. +
  80498. + gdfifocfg.d32 =
  80499. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  80500. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  80501. +
  80502. + if (ep->dwc_ep.is_in) {
  80503. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  80504. + /* Flush the Tx FIFO */
  80505. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  80506. + ep->dwc_ep.tx_fifo_num);
  80507. + }
  80508. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  80509. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  80510. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  80511. + /* Decreasing EPinfo Base Addr */
  80512. + dptxfsiz.d32 =
  80513. + (DWC_READ_REG32
  80514. + (&GET_CORE_IF(pcd)->
  80515. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  80516. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  80517. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  80518. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  80519. + gdfifocfg.d32);
  80520. + }
  80521. + }
  80522. + }
  80523. +
  80524. + /* Free DMA Descriptors */
  80525. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  80526. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  80527. + desc_addr = ep->dwc_ep.desc_addr;
  80528. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  80529. +
  80530. + /* Cannot call dma_free_coherent() with IRQs disabled */
  80531. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80532. + dwc_otg_ep_free_desc_chain(desc_addr, dma_desc_addr,
  80533. + MAX_DMA_DESC_CNT);
  80534. +
  80535. + goto out_unlocked;
  80536. + }
  80537. + }
  80538. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80539. +
  80540. +out_unlocked:
  80541. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  80542. + ep->dwc_ep.is_in ? "IN" : "OUT");
  80543. + return 0;
  80544. +
  80545. +}
  80546. +
  80547. +/******************************************************************************/
  80548. +#ifdef DWC_UTE_PER_IO
  80549. +
  80550. +/**
  80551. + * Free the request and its extended parts
  80552. + *
  80553. + */
  80554. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  80555. +{
  80556. + DWC_FREE(req->ext_req.per_io_frame_descs);
  80557. + DWC_FREE(req);
  80558. +}
  80559. +
  80560. +/**
  80561. + * Start the next request in the endpoint's queue.
  80562. + *
  80563. + */
  80564. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  80565. + dwc_otg_pcd_ep_t * ep)
  80566. +{
  80567. + int i;
  80568. + dwc_otg_pcd_request_t *req = NULL;
  80569. + dwc_ep_t *dwcep = NULL;
  80570. + struct dwc_iso_xreq_port *ereq = NULL;
  80571. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  80572. + uint16_t nat;
  80573. + depctl_data_t diepctl;
  80574. +
  80575. + dwcep = &ep->dwc_ep;
  80576. +
  80577. + if (dwcep->xiso_active_xfers > 0) {
  80578. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  80579. + DWC_WARN("There are currently active transfers for EP%d \
  80580. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  80581. + dwcep->xiso_queued_xfers);
  80582. +#endif
  80583. + return 0;
  80584. + }
  80585. +
  80586. + nat = UGETW(ep->desc->wMaxPacketSize);
  80587. + nat = (nat >> 11) & 0x03;
  80588. +
  80589. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  80590. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  80591. + ereq = &req->ext_req;
  80592. + ep->stopped = 0;
  80593. +
  80594. + /* Get the frame number */
  80595. + dwcep->xiso_frame_num =
  80596. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  80597. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  80598. +
  80599. + ddesc_iso = ereq->per_io_frame_descs;
  80600. +
  80601. + if (dwcep->is_in) {
  80602. + /* Setup DMA Descriptor chain for IN Isoc request */
  80603. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  80604. + //if ((i % (nat + 1)) == 0)
  80605. + if ( i > 0 )
  80606. + dwcep->xiso_frame_num =
  80607. + (dwcep->xiso_bInterval +
  80608. + dwcep->xiso_frame_num) & 0x3FFF;
  80609. + dwcep->desc_addr[i].buf =
  80610. + req->dma + ddesc_iso[i].offset;
  80611. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  80612. + ddesc_iso[i].length;
  80613. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  80614. + dwcep->xiso_frame_num;
  80615. + dwcep->desc_addr[i].status.b_iso_in.bs =
  80616. + BS_HOST_READY;
  80617. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  80618. + dwcep->desc_addr[i].status.b_iso_in.sp =
  80619. + (ddesc_iso[i].length %
  80620. + dwcep->maxpacket) ? 1 : 0;
  80621. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  80622. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  80623. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  80624. +
  80625. + /* Process the last descriptor */
  80626. + if (i == ereq->pio_pkt_count - 1) {
  80627. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  80628. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  80629. + }
  80630. + }
  80631. +
  80632. + /* Setup and start the transfer for this endpoint */
  80633. + dwcep->xiso_active_xfers++;
  80634. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  80635. + in_ep_regs[dwcep->num]->diepdma,
  80636. + dwcep->dma_desc_addr);
  80637. + diepctl.d32 = 0;
  80638. + diepctl.b.epena = 1;
  80639. + diepctl.b.cnak = 1;
  80640. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  80641. + in_ep_regs[dwcep->num]->diepctl, 0,
  80642. + diepctl.d32);
  80643. + } else {
  80644. + /* Setup DMA Descriptor chain for OUT Isoc request */
  80645. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  80646. + //if ((i % (nat + 1)) == 0)
  80647. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  80648. + dwcep->xiso_frame_num) & 0x3FFF;
  80649. + dwcep->desc_addr[i].buf =
  80650. + req->dma + ddesc_iso[i].offset;
  80651. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  80652. + ddesc_iso[i].length;
  80653. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  80654. + dwcep->xiso_frame_num;
  80655. + dwcep->desc_addr[i].status.b_iso_out.bs =
  80656. + BS_HOST_READY;
  80657. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  80658. + dwcep->desc_addr[i].status.b_iso_out.sp =
  80659. + (ddesc_iso[i].length %
  80660. + dwcep->maxpacket) ? 1 : 0;
  80661. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  80662. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  80663. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  80664. +
  80665. + /* Process the last descriptor */
  80666. + if (i == ereq->pio_pkt_count - 1) {
  80667. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  80668. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  80669. + }
  80670. + }
  80671. +
  80672. + /* Setup and start the transfer for this endpoint */
  80673. + dwcep->xiso_active_xfers++;
  80674. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  80675. + dev_if->out_ep_regs[dwcep->num]->
  80676. + doepdma, dwcep->dma_desc_addr);
  80677. + diepctl.d32 = 0;
  80678. + diepctl.b.epena = 1;
  80679. + diepctl.b.cnak = 1;
  80680. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  80681. + dev_if->out_ep_regs[dwcep->num]->
  80682. + doepctl, 0, diepctl.d32);
  80683. + }
  80684. +
  80685. + } else {
  80686. + ep->stopped = 1;
  80687. + }
  80688. +
  80689. + return 0;
  80690. +}
  80691. +
  80692. +/**
  80693. + * - Remove the request from the queue
  80694. + */
  80695. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  80696. +{
  80697. + dwc_otg_pcd_request_t *req = NULL;
  80698. + struct dwc_iso_xreq_port *ereq = NULL;
  80699. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  80700. + dwc_ep_t *dwcep = NULL;
  80701. + int i;
  80702. +
  80703. + //DWC_DEBUG();
  80704. + dwcep = &ep->dwc_ep;
  80705. +
  80706. + /* Get the first pending request from the queue */
  80707. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  80708. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  80709. + if (!req) {
  80710. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  80711. + return;
  80712. + }
  80713. + dwcep->xiso_active_xfers--;
  80714. + dwcep->xiso_queued_xfers--;
  80715. + /* Remove this request from the queue */
  80716. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  80717. + } else {
  80718. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  80719. + return;
  80720. + }
  80721. +
  80722. + ep->stopped = 1;
  80723. + ereq = &req->ext_req;
  80724. + ddesc_iso = ereq->per_io_frame_descs;
  80725. +
  80726. + if (dwcep->xiso_active_xfers < 0) {
  80727. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  80728. + dwcep->xiso_active_xfers);
  80729. + }
  80730. +
  80731. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  80732. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  80733. + if (dwcep->is_in) { /* IN endpoints */
  80734. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  80735. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  80736. + ddesc_iso[i].status =
  80737. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  80738. + } else { /* OUT endpoints */
  80739. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  80740. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  80741. + ddesc_iso[i].status =
  80742. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  80743. + }
  80744. + }
  80745. +
  80746. + DWC_SPINUNLOCK(ep->pcd->lock);
  80747. +
  80748. + /* Call the completion function in the non-portable logic */
  80749. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  80750. + &req->ext_req);
  80751. +
  80752. + DWC_SPINLOCK(ep->pcd->lock);
  80753. +
  80754. + /* Free the request - specific freeing needed for extended request object */
  80755. + dwc_pcd_xiso_ereq_free(ep, req);
  80756. +
  80757. + /* Start the next request */
  80758. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  80759. +
  80760. + return;
  80761. +}
  80762. +
  80763. +/**
  80764. + * Create and initialize the Isoc pkt descriptors of the extended request.
  80765. + *
  80766. + */
  80767. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  80768. + void *ereq_nonport,
  80769. + int atomic_alloc)
  80770. +{
  80771. + struct dwc_iso_xreq_port *ereq = NULL;
  80772. + struct dwc_iso_xreq_port *req_mapped = NULL;
  80773. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  80774. + uint32_t pkt_count;
  80775. + int i;
  80776. +
  80777. + ereq = &req->ext_req;
  80778. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  80779. + pkt_count = req_mapped->pio_pkt_count;
  80780. +
  80781. + /* Create the isoc descs */
  80782. + if (atomic_alloc) {
  80783. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  80784. + } else {
  80785. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  80786. + }
  80787. +
  80788. + if (!ipds) {
  80789. + DWC_ERROR("Failed to allocate isoc descriptors");
  80790. + return -DWC_E_NO_MEMORY;
  80791. + }
  80792. +
  80793. + /* Initialize the extended request fields */
  80794. + ereq->per_io_frame_descs = ipds;
  80795. + ereq->error_count = 0;
  80796. + ereq->pio_alloc_pkt_count = pkt_count;
  80797. + ereq->pio_pkt_count = pkt_count;
  80798. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  80799. +
  80800. + /* Init the Isoc descriptors */
  80801. + for (i = 0; i < pkt_count; i++) {
  80802. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  80803. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  80804. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  80805. + ipds[i].actual_length =
  80806. + req_mapped->per_io_frame_descs[i].actual_length;
  80807. + }
  80808. +
  80809. + return 0;
  80810. +}
  80811. +
  80812. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  80813. +{
  80814. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  80815. + int i;
  80816. +
  80817. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  80818. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  80819. + DWC_DEBUG("error_count=%d", ereq->error_count);
  80820. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  80821. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  80822. + DWC_DEBUG("res=%d", ereq->res);
  80823. +
  80824. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  80825. + xfd = &ereq->per_io_frame_descs[0];
  80826. + DWC_DEBUG("FD #%d", i);
  80827. +
  80828. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  80829. + DWC_DEBUG("xfd->length=%d", xfd->length);
  80830. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  80831. + DWC_DEBUG("xfd->status=%d", xfd->status);
  80832. + }
  80833. +}
  80834. +
  80835. +/**
  80836. + *
  80837. + */
  80838. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  80839. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  80840. + int zero, void *req_handle, int atomic_alloc,
  80841. + void *ereq_nonport)
  80842. +{
  80843. + dwc_otg_pcd_request_t *req = NULL;
  80844. + dwc_otg_pcd_ep_t *ep;
  80845. + dwc_irqflags_t flags;
  80846. + int res;
  80847. +
  80848. + ep = get_ep_from_handle(pcd, ep_handle);
  80849. + if (!ep) {
  80850. + DWC_WARN("bad ep\n");
  80851. + return -DWC_E_INVALID;
  80852. + }
  80853. +
  80854. + /* We support this extension only for DDMA mode */
  80855. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  80856. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  80857. + return -DWC_E_INVALID;
  80858. +
  80859. + /* Create a dwc_otg_pcd_request_t object */
  80860. + if (atomic_alloc) {
  80861. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  80862. + } else {
  80863. + req = DWC_ALLOC(sizeof(*req));
  80864. + }
  80865. +
  80866. + if (!req) {
  80867. + return -DWC_E_NO_MEMORY;
  80868. + }
  80869. +
  80870. + /* Create the Isoc descs for this request which shall be the exact match
  80871. + * of the structure sent to us from the non-portable logic */
  80872. + res =
  80873. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  80874. + if (res) {
  80875. + DWC_WARN("Failed to init the Isoc descriptors");
  80876. + DWC_FREE(req);
  80877. + return res;
  80878. + }
  80879. +
  80880. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80881. +
  80882. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  80883. + req->buf = buf;
  80884. + req->dma = dma_buf;
  80885. + req->length = buflen;
  80886. + req->sent_zlp = zero;
  80887. + req->priv = req_handle;
  80888. +
  80889. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80890. + ep->dwc_ep.dma_addr = dma_buf;
  80891. + ep->dwc_ep.start_xfer_buff = buf;
  80892. + ep->dwc_ep.xfer_buff = buf;
  80893. + ep->dwc_ep.xfer_len = 0;
  80894. + ep->dwc_ep.xfer_count = 0;
  80895. + ep->dwc_ep.sent_zlp = 0;
  80896. + ep->dwc_ep.total_len = buflen;
  80897. +
  80898. + /* Add this request to the tail */
  80899. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  80900. + ep->dwc_ep.xiso_queued_xfers++;
  80901. +
  80902. +//DWC_DEBUG("CP_0");
  80903. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  80904. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  80905. +//prn_ext_request(&req->ext_req);
  80906. +
  80907. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80908. +
  80909. + /* If the req->status == ASAP then check if there is any active transfer
  80910. + * for this endpoint. If no active transfers, then get the first entry
  80911. + * from the queue and start that transfer
  80912. + */
  80913. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  80914. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  80915. + if (res) {
  80916. + DWC_WARN("Failed to start the next Isoc transfer");
  80917. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80918. + DWC_FREE(req);
  80919. + return res;
  80920. + }
  80921. + }
  80922. +
  80923. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80924. + return 0;
  80925. +}
  80926. +
  80927. +#endif
  80928. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  80929. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  80930. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  80931. + int zero, void *req_handle, int atomic_alloc)
  80932. +{
  80933. + dwc_irqflags_t flags;
  80934. + dwc_otg_pcd_request_t *req;
  80935. + dwc_otg_pcd_ep_t *ep;
  80936. + uint32_t max_transfer;
  80937. +
  80938. + ep = get_ep_from_handle(pcd, ep_handle);
  80939. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  80940. + DWC_WARN("bad ep\n");
  80941. + return -DWC_E_INVALID;
  80942. + }
  80943. +
  80944. + if (atomic_alloc) {
  80945. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  80946. + } else {
  80947. + req = DWC_ALLOC(sizeof(*req));
  80948. + }
  80949. +
  80950. + if (!req) {
  80951. + return -DWC_E_NO_MEMORY;
  80952. + }
  80953. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  80954. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  80955. + if (ep->dwc_ep.num != 0) {
  80956. + DWC_ERROR("queue req %p, len %d buf %p\n",
  80957. + req_handle, buflen, buf);
  80958. + }
  80959. + }
  80960. +
  80961. + req->buf = buf;
  80962. + req->dma = dma_buf;
  80963. + req->length = buflen;
  80964. + req->sent_zlp = zero;
  80965. + req->priv = req_handle;
  80966. + req->dw_align_buf = NULL;
  80967. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  80968. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  80969. + req->dw_align_buf = DWC_DMA_ALLOC(buflen,
  80970. + &req->dw_align_buf_dma);
  80971. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  80972. +
  80973. + /*
  80974. + * After adding request to the queue for IN ISOC wait for In Token Received
  80975. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  80976. + * Received when EP is disabled interrupt to obtain starting microframe
  80977. + * (odd/even) start transfer
  80978. + */
  80979. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  80980. + if (req != 0) {
  80981. + depctl_data_t depctl = {.d32 =
  80982. + DWC_READ_REG32(&pcd->core_if->dev_if->
  80983. + in_ep_regs[ep->dwc_ep.num]->
  80984. + diepctl) };
  80985. + ++pcd->request_pending;
  80986. +
  80987. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  80988. + if (ep->dwc_ep.is_in) {
  80989. + depctl.b.cnak = 1;
  80990. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  80991. + in_ep_regs[ep->dwc_ep.num]->
  80992. + diepctl, depctl.d32);
  80993. + }
  80994. +
  80995. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  80996. + }
  80997. + return 0;
  80998. + }
  80999. +
  81000. + /*
  81001. + * For EP0 IN without premature status, zlp is required?
  81002. + */
  81003. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  81004. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  81005. + //_req->zero = 1;
  81006. + }
  81007. +
  81008. + /* Start the transfer */
  81009. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  81010. + /* EP0 Transfer? */
  81011. + if (ep->dwc_ep.num == 0) {
  81012. + switch (pcd->ep0state) {
  81013. + case EP0_IN_DATA_PHASE:
  81014. + DWC_DEBUGPL(DBG_PCD,
  81015. + "%s ep0: EP0_IN_DATA_PHASE\n",
  81016. + __func__);
  81017. + break;
  81018. +
  81019. + case EP0_OUT_DATA_PHASE:
  81020. + DWC_DEBUGPL(DBG_PCD,
  81021. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  81022. + __func__);
  81023. + if (pcd->request_config) {
  81024. + /* Complete STATUS PHASE */
  81025. + ep->dwc_ep.is_in = 1;
  81026. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  81027. + }
  81028. + break;
  81029. +
  81030. + case EP0_IN_STATUS_PHASE:
  81031. + DWC_DEBUGPL(DBG_PCD,
  81032. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  81033. + __func__);
  81034. + break;
  81035. +
  81036. + default:
  81037. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  81038. + pcd->ep0state);
  81039. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81040. + return -DWC_E_SHUTDOWN;
  81041. + }
  81042. +
  81043. + ep->dwc_ep.dma_addr = dma_buf;
  81044. + ep->dwc_ep.start_xfer_buff = buf;
  81045. + ep->dwc_ep.xfer_buff = buf;
  81046. + ep->dwc_ep.xfer_len = buflen;
  81047. + ep->dwc_ep.xfer_count = 0;
  81048. + ep->dwc_ep.sent_zlp = 0;
  81049. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  81050. +
  81051. + if (zero) {
  81052. + if ((ep->dwc_ep.xfer_len %
  81053. + ep->dwc_ep.maxpacket == 0)
  81054. + && (ep->dwc_ep.xfer_len != 0)) {
  81055. + ep->dwc_ep.sent_zlp = 1;
  81056. + }
  81057. +
  81058. + }
  81059. +
  81060. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  81061. + &ep->dwc_ep);
  81062. + } // non-ep0 endpoints
  81063. + else {
  81064. +#ifdef DWC_UTE_CFI
  81065. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  81066. + /* store the request length */
  81067. + ep->dwc_ep.cfi_req_len = buflen;
  81068. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  81069. + ep, req);
  81070. + } else {
  81071. +#endif
  81072. + max_transfer =
  81073. + GET_CORE_IF(ep->pcd)->core_params->
  81074. + max_transfer_size;
  81075. +
  81076. + /* Setup and start the Transfer */
  81077. + if (req->dw_align_buf){
  81078. + if (ep->dwc_ep.is_in)
  81079. + dwc_memcpy(req->dw_align_buf,
  81080. + buf, buflen);
  81081. + ep->dwc_ep.dma_addr =
  81082. + req->dw_align_buf_dma;
  81083. + ep->dwc_ep.start_xfer_buff =
  81084. + req->dw_align_buf;
  81085. + ep->dwc_ep.xfer_buff =
  81086. + req->dw_align_buf;
  81087. + } else {
  81088. + ep->dwc_ep.dma_addr = dma_buf;
  81089. + ep->dwc_ep.start_xfer_buff = buf;
  81090. + ep->dwc_ep.xfer_buff = buf;
  81091. + }
  81092. + ep->dwc_ep.xfer_len = 0;
  81093. + ep->dwc_ep.xfer_count = 0;
  81094. + ep->dwc_ep.sent_zlp = 0;
  81095. + ep->dwc_ep.total_len = buflen;
  81096. +
  81097. + ep->dwc_ep.maxxfer = max_transfer;
  81098. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  81099. + uint32_t out_max_xfer =
  81100. + DDMA_MAX_TRANSFER_SIZE -
  81101. + (DDMA_MAX_TRANSFER_SIZE % 4);
  81102. + if (ep->dwc_ep.is_in) {
  81103. + if (ep->dwc_ep.maxxfer >
  81104. + DDMA_MAX_TRANSFER_SIZE) {
  81105. + ep->dwc_ep.maxxfer =
  81106. + DDMA_MAX_TRANSFER_SIZE;
  81107. + }
  81108. + } else {
  81109. + if (ep->dwc_ep.maxxfer >
  81110. + out_max_xfer) {
  81111. + ep->dwc_ep.maxxfer =
  81112. + out_max_xfer;
  81113. + }
  81114. + }
  81115. + }
  81116. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  81117. + ep->dwc_ep.maxxfer -=
  81118. + (ep->dwc_ep.maxxfer %
  81119. + ep->dwc_ep.maxpacket);
  81120. + }
  81121. +
  81122. + if (zero) {
  81123. + if ((ep->dwc_ep.total_len %
  81124. + ep->dwc_ep.maxpacket == 0)
  81125. + && (ep->dwc_ep.total_len != 0)) {
  81126. + ep->dwc_ep.sent_zlp = 1;
  81127. + }
  81128. + }
  81129. +#ifdef DWC_UTE_CFI
  81130. + }
  81131. +#endif
  81132. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  81133. + &ep->dwc_ep);
  81134. + }
  81135. + }
  81136. +
  81137. + if (req != 0) {
  81138. + ++pcd->request_pending;
  81139. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  81140. + if (ep->dwc_ep.is_in && ep->stopped
  81141. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  81142. + /** @todo NGS Create a function for this. */
  81143. + diepmsk_data_t diepmsk = {.d32 = 0 };
  81144. + diepmsk.b.intktxfemp = 1;
  81145. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  81146. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  81147. + dev_if->dev_global_regs->diepeachintmsk
  81148. + [ep->dwc_ep.num], 0,
  81149. + diepmsk.d32);
  81150. + } else {
  81151. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  81152. + dev_if->dev_global_regs->
  81153. + diepmsk, 0, diepmsk.d32);
  81154. + }
  81155. +
  81156. + }
  81157. + }
  81158. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81159. +
  81160. + return 0;
  81161. +}
  81162. +
  81163. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  81164. + void *req_handle)
  81165. +{
  81166. + dwc_irqflags_t flags;
  81167. + dwc_otg_pcd_request_t *req;
  81168. + dwc_otg_pcd_ep_t *ep;
  81169. +
  81170. + ep = get_ep_from_handle(pcd, ep_handle);
  81171. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  81172. + DWC_WARN("bad argument\n");
  81173. + return -DWC_E_INVALID;
  81174. + }
  81175. +
  81176. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81177. +
  81178. + /* make sure it's actually queued on this endpoint */
  81179. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  81180. + if (req->priv == (void *)req_handle) {
  81181. + break;
  81182. + }
  81183. + }
  81184. +
  81185. + if (req->priv != (void *)req_handle) {
  81186. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81187. + return -DWC_E_INVALID;
  81188. + }
  81189. +
  81190. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  81191. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  81192. + } else {
  81193. + req = NULL;
  81194. + }
  81195. +
  81196. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81197. +
  81198. + return req ? 0 : -DWC_E_SHUTDOWN;
  81199. +
  81200. +}
  81201. +
  81202. +/**
  81203. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  81204. + *
  81205. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  81206. + * requests. If the gadget driver clears the halt status, it will
  81207. + * automatically unwedge the endpoint.
  81208. + *
  81209. + * Returns zero on success, else negative DWC error code.
  81210. + */
  81211. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  81212. +{
  81213. + dwc_otg_pcd_ep_t *ep;
  81214. + dwc_irqflags_t flags;
  81215. + int retval = 0;
  81216. +
  81217. + ep = get_ep_from_handle(pcd, ep_handle);
  81218. +
  81219. + if ((!ep->desc && ep != &pcd->ep0) ||
  81220. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  81221. + DWC_WARN("%s, bad ep\n", __func__);
  81222. + return -DWC_E_INVALID;
  81223. + }
  81224. +
  81225. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81226. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81227. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  81228. + ep->dwc_ep.is_in ? "IN" : "OUT");
  81229. + retval = -DWC_E_AGAIN;
  81230. + } else {
  81231. + /* This code needs to be reviewed */
  81232. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  81233. + dtxfsts_data_t txstatus;
  81234. + fifosize_data_t txfifosize;
  81235. +
  81236. + txfifosize.d32 =
  81237. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  81238. + core_global_regs->dtxfsiz[ep->dwc_ep.
  81239. + tx_fifo_num]);
  81240. + txstatus.d32 =
  81241. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  81242. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  81243. + dtxfsts);
  81244. +
  81245. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  81246. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  81247. + retval = -DWC_E_AGAIN;
  81248. + } else {
  81249. + if (ep->dwc_ep.num == 0) {
  81250. + pcd->ep0state = EP0_STALL;
  81251. + }
  81252. +
  81253. + ep->stopped = 1;
  81254. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  81255. + &ep->dwc_ep);
  81256. + }
  81257. + } else {
  81258. + if (ep->dwc_ep.num == 0) {
  81259. + pcd->ep0state = EP0_STALL;
  81260. + }
  81261. +
  81262. + ep->stopped = 1;
  81263. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81264. + }
  81265. + }
  81266. +
  81267. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81268. +
  81269. + return retval;
  81270. +}
  81271. +
  81272. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  81273. +{
  81274. + dwc_otg_pcd_ep_t *ep;
  81275. + dwc_irqflags_t flags;
  81276. + int retval = 0;
  81277. +
  81278. + ep = get_ep_from_handle(pcd, ep_handle);
  81279. +
  81280. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  81281. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  81282. + DWC_WARN("%s, bad ep\n", __func__);
  81283. + return -DWC_E_INVALID;
  81284. + }
  81285. +
  81286. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81287. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  81288. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  81289. + ep->dwc_ep.is_in ? "IN" : "OUT");
  81290. + retval = -DWC_E_AGAIN;
  81291. + } else if (value == 0) {
  81292. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81293. + } else if (value == 1) {
  81294. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  81295. + dtxfsts_data_t txstatus;
  81296. + fifosize_data_t txfifosize;
  81297. +
  81298. + txfifosize.d32 =
  81299. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  81300. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  81301. + txstatus.d32 =
  81302. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  81303. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  81304. +
  81305. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  81306. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  81307. + retval = -DWC_E_AGAIN;
  81308. + } else {
  81309. + if (ep->dwc_ep.num == 0) {
  81310. + pcd->ep0state = EP0_STALL;
  81311. + }
  81312. +
  81313. + ep->stopped = 1;
  81314. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  81315. + &ep->dwc_ep);
  81316. + }
  81317. + } else {
  81318. + if (ep->dwc_ep.num == 0) {
  81319. + pcd->ep0state = EP0_STALL;
  81320. + }
  81321. +
  81322. + ep->stopped = 1;
  81323. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  81324. + }
  81325. + } else if (value == 2) {
  81326. + ep->dwc_ep.stall_clear_flag = 0;
  81327. + } else if (value == 3) {
  81328. + ep->dwc_ep.stall_clear_flag = 1;
  81329. + }
  81330. +
  81331. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81332. +
  81333. + return retval;
  81334. +}
  81335. +
  81336. +/**
  81337. + * This function initiates remote wakeup of the host from suspend state.
  81338. + */
  81339. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  81340. +{
  81341. + dctl_data_t dctl = { 0 };
  81342. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  81343. + dsts_data_t dsts;
  81344. +
  81345. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  81346. + if (!dsts.b.suspsts) {
  81347. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  81348. + }
  81349. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  81350. + if (pcd->remote_wakeup_enable) {
  81351. + if (set) {
  81352. +
  81353. + if (core_if->adp_enable) {
  81354. + gpwrdn_data_t gpwrdn;
  81355. +
  81356. + dwc_otg_adp_probe_stop(core_if);
  81357. +
  81358. + /* Mask SRP detected interrupt from Power Down Logic */
  81359. + gpwrdn.d32 = 0;
  81360. + gpwrdn.b.srp_det_msk = 1;
  81361. + DWC_MODIFY_REG32(&core_if->
  81362. + core_global_regs->gpwrdn,
  81363. + gpwrdn.d32, 0);
  81364. +
  81365. + /* Disable Power Down Logic */
  81366. + gpwrdn.d32 = 0;
  81367. + gpwrdn.b.pmuactv = 1;
  81368. + DWC_MODIFY_REG32(&core_if->
  81369. + core_global_regs->gpwrdn,
  81370. + gpwrdn.d32, 0);
  81371. +
  81372. + /*
  81373. + * Initialize the Core for Device mode.
  81374. + */
  81375. + core_if->op_state = B_PERIPHERAL;
  81376. + dwc_otg_core_init(core_if);
  81377. + dwc_otg_enable_global_interrupts(core_if);
  81378. + cil_pcd_start(core_if);
  81379. +
  81380. + dwc_otg_initiate_srp(core_if);
  81381. + }
  81382. +
  81383. + dctl.b.rmtwkupsig = 1;
  81384. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  81385. + dctl, 0, dctl.d32);
  81386. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  81387. +
  81388. + dwc_mdelay(2);
  81389. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  81390. + dctl, dctl.d32, 0);
  81391. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  81392. + }
  81393. + } else {
  81394. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  81395. + }
  81396. +}
  81397. +
  81398. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81399. +/**
  81400. + * This function initiates remote wakeup of the host from L1 sleep state.
  81401. + */
  81402. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  81403. +{
  81404. + glpmcfg_data_t lpmcfg;
  81405. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  81406. +
  81407. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  81408. +
  81409. + /* Check if we are in L1 state */
  81410. + if (!lpmcfg.b.prt_sleep_sts) {
  81411. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  81412. + return;
  81413. + }
  81414. +
  81415. + /* Check if host allows remote wakeup */
  81416. + if (!lpmcfg.b.rem_wkup_en) {
  81417. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  81418. + return;
  81419. + }
  81420. +
  81421. + /* Check if Resume OK */
  81422. + if (!lpmcfg.b.sleep_state_resumeok) {
  81423. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  81424. + return;
  81425. + }
  81426. +
  81427. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  81428. + lpmcfg.b.en_utmi_sleep = 0;
  81429. + lpmcfg.b.hird_thres &= (~(1 << 4));
  81430. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  81431. +
  81432. + if (set) {
  81433. + dctl_data_t dctl = {.d32 = 0 };
  81434. + dctl.b.rmtwkupsig = 1;
  81435. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  81436. + * Hardware will automatically clear this bit.
  81437. + */
  81438. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  81439. + 0, dctl.d32);
  81440. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  81441. + }
  81442. +
  81443. +}
  81444. +#endif
  81445. +
  81446. +/**
  81447. + * Performs remote wakeup.
  81448. + */
  81449. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  81450. +{
  81451. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  81452. + dwc_irqflags_t flags;
  81453. + if (dwc_otg_is_device_mode(core_if)) {
  81454. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81455. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81456. + if (core_if->lx_state == DWC_OTG_L1) {
  81457. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  81458. + } else {
  81459. +#endif
  81460. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  81461. +#ifdef CONFIG_USB_DWC_OTG_LPM
  81462. + }
  81463. +#endif
  81464. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81465. + }
  81466. + return;
  81467. +}
  81468. +
  81469. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  81470. +{
  81471. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  81472. + dctl_data_t dctl = { 0 };
  81473. +
  81474. + if (dwc_otg_is_device_mode(core_if)) {
  81475. + dctl.b.sftdiscon = 1;
  81476. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  81477. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  81478. + dwc_udelay(no_of_usecs);
  81479. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  81480. +
  81481. + } else{
  81482. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  81483. + }
  81484. + return;
  81485. +
  81486. +}
  81487. +
  81488. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  81489. +{
  81490. + dsts_data_t dsts;
  81491. + gotgctl_data_t gotgctl;
  81492. +
  81493. + /*
  81494. + * This function starts the Protocol if no session is in progress. If
  81495. + * a session is already in progress, but the device is suspended,
  81496. + * remote wakeup signaling is started.
  81497. + */
  81498. +
  81499. + /* Check if valid session */
  81500. + gotgctl.d32 =
  81501. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  81502. + if (gotgctl.b.bsesvld) {
  81503. + /* Check if suspend state */
  81504. + dsts.d32 =
  81505. + DWC_READ_REG32(&
  81506. + (GET_CORE_IF(pcd)->dev_if->
  81507. + dev_global_regs->dsts));
  81508. + if (dsts.b.suspsts) {
  81509. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  81510. + }
  81511. + } else {
  81512. + dwc_otg_pcd_initiate_srp(pcd);
  81513. + }
  81514. +
  81515. + return 0;
  81516. +
  81517. +}
  81518. +
  81519. +/**
  81520. + * Start the SRP timer to detect when the SRP does not complete within
  81521. + * 6 seconds.
  81522. + *
  81523. + * @param pcd the pcd structure.
  81524. + */
  81525. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  81526. +{
  81527. + dwc_irqflags_t flags;
  81528. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  81529. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  81530. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  81531. +}
  81532. +
  81533. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  81534. +{
  81535. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  81536. +}
  81537. +
  81538. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  81539. +{
  81540. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  81541. +}
  81542. +
  81543. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  81544. +{
  81545. + return pcd->b_hnp_enable;
  81546. +}
  81547. +
  81548. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  81549. +{
  81550. + return pcd->a_hnp_support;
  81551. +}
  81552. +
  81553. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  81554. +{
  81555. + return pcd->a_alt_hnp_support;
  81556. +}
  81557. +
  81558. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  81559. +{
  81560. + return pcd->remote_wakeup_enable;
  81561. +}
  81562. +
  81563. +#endif /* DWC_HOST_ONLY */
  81564. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  81565. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 1970-01-01 01:00:00.000000000 +0100
  81566. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 2014-03-11 16:53:12.000000000 +0100
  81567. @@ -0,0 +1,266 @@
  81568. +/* ==========================================================================
  81569. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  81570. + * $Revision: #48 $
  81571. + * $Date: 2012/08/10 $
  81572. + * $Change: 2047372 $
  81573. + *
  81574. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  81575. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  81576. + * otherwise expressly agreed to in writing between Synopsys and you.
  81577. + *
  81578. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  81579. + * any End User Software License Agreement or Agreement for Licensed Product
  81580. + * with Synopsys or any supplement thereto. You are permitted to use and
  81581. + * redistribute this Software in source and binary forms, with or without
  81582. + * modification, provided that redistributions of source code must retain this
  81583. + * notice. You may not view, use, disclose, copy or distribute this file or
  81584. + * any information contained herein except pursuant to this license grant from
  81585. + * Synopsys. If you do not agree with this notice, including the disclaimer
  81586. + * below, then you are not authorized to use the Software.
  81587. + *
  81588. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  81589. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  81590. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  81591. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  81592. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  81593. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  81594. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  81595. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  81596. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  81597. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  81598. + * DAMAGE.
  81599. + * ========================================================================== */
  81600. +#ifndef DWC_HOST_ONLY
  81601. +#if !defined(__DWC_PCD_H__)
  81602. +#define __DWC_PCD_H__
  81603. +
  81604. +#include "dwc_otg_os_dep.h"
  81605. +#include "usb.h"
  81606. +#include "dwc_otg_cil.h"
  81607. +#include "dwc_otg_pcd_if.h"
  81608. +struct cfiobject;
  81609. +
  81610. +/**
  81611. + * @file
  81612. + *
  81613. + * This file contains the structures, constants, and interfaces for
  81614. + * the Perpherial Contoller Driver (PCD).
  81615. + *
  81616. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  81617. + * Gadget API, so that the existing Gadget drivers can be used. For
  81618. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  81619. + * (FBS) driver will be used. The FBS driver supports the
  81620. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  81621. + * transports.
  81622. + *
  81623. + */
  81624. +
  81625. +/** Invalid DMA Address */
  81626. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  81627. +
  81628. +/** Max Transfer size for any EP */
  81629. +#define DDMA_MAX_TRANSFER_SIZE 65535
  81630. +
  81631. +/**
  81632. + * Get the pointer to the core_if from the pcd pointer.
  81633. + */
  81634. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  81635. +
  81636. +/**
  81637. + * States of EP0.
  81638. + */
  81639. +typedef enum ep0_state {
  81640. + EP0_DISCONNECT, /* no host */
  81641. + EP0_IDLE,
  81642. + EP0_IN_DATA_PHASE,
  81643. + EP0_OUT_DATA_PHASE,
  81644. + EP0_IN_STATUS_PHASE,
  81645. + EP0_OUT_STATUS_PHASE,
  81646. + EP0_STALL,
  81647. +} ep0state_e;
  81648. +
  81649. +/** Fordward declaration.*/
  81650. +struct dwc_otg_pcd;
  81651. +
  81652. +/** DWC_otg iso request structure.
  81653. + *
  81654. + */
  81655. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  81656. +
  81657. +#ifdef DWC_UTE_PER_IO
  81658. +
  81659. +/**
  81660. + * This shall be the exact analogy of the same type structure defined in the
  81661. + * usb_gadget.h. Each descriptor contains
  81662. + */
  81663. +struct dwc_iso_pkt_desc_port {
  81664. + uint32_t offset;
  81665. + uint32_t length; /* expected length */
  81666. + uint32_t actual_length;
  81667. + uint32_t status;
  81668. +};
  81669. +
  81670. +struct dwc_iso_xreq_port {
  81671. + /** transfer/submission flag */
  81672. + uint32_t tr_sub_flags;
  81673. + /** Start the request ASAP */
  81674. +#define DWC_EREQ_TF_ASAP 0x00000002
  81675. + /** Just enqueue the request w/o initiating a transfer */
  81676. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  81677. +
  81678. + /**
  81679. + * count of ISO packets attached to this request - shall
  81680. + * not exceed the pio_alloc_pkt_count
  81681. + */
  81682. + uint32_t pio_pkt_count;
  81683. + /** count of ISO packets allocated for this request */
  81684. + uint32_t pio_alloc_pkt_count;
  81685. + /** number of ISO packet errors */
  81686. + uint32_t error_count;
  81687. + /** reserved for future extension */
  81688. + uint32_t res;
  81689. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  81690. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  81691. +};
  81692. +#endif
  81693. +/** DWC_otg request structure.
  81694. + * This structure is a list of requests.
  81695. + */
  81696. +typedef struct dwc_otg_pcd_request {
  81697. + void *priv;
  81698. + void *buf;
  81699. + dwc_dma_t dma;
  81700. + uint32_t length;
  81701. + uint32_t actual;
  81702. + unsigned sent_zlp:1;
  81703. + /**
  81704. + * Used instead of original buffer if
  81705. + * it(physical address) is not dword-aligned.
  81706. + **/
  81707. + uint8_t *dw_align_buf;
  81708. + dwc_dma_t dw_align_buf_dma;
  81709. +
  81710. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  81711. +#ifdef DWC_UTE_PER_IO
  81712. + struct dwc_iso_xreq_port ext_req;
  81713. + //void *priv_ereq_nport; /* */
  81714. +#endif
  81715. +} dwc_otg_pcd_request_t;
  81716. +
  81717. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  81718. +
  81719. +/** PCD EP structure.
  81720. + * This structure describes an EP, there is an array of EPs in the PCD
  81721. + * structure.
  81722. + */
  81723. +typedef struct dwc_otg_pcd_ep {
  81724. + /** USB EP Descriptor */
  81725. + const usb_endpoint_descriptor_t *desc;
  81726. +
  81727. + /** queue of dwc_otg_pcd_requests. */
  81728. + struct req_list queue;
  81729. + unsigned stopped:1;
  81730. + unsigned disabling:1;
  81731. + unsigned dma:1;
  81732. + unsigned queue_sof:1;
  81733. +
  81734. +#ifdef DWC_EN_ISOC
  81735. + /** ISOC req handle passed */
  81736. + void *iso_req_handle;
  81737. +#endif //_EN_ISOC_
  81738. +
  81739. + /** DWC_otg ep data. */
  81740. + dwc_ep_t dwc_ep;
  81741. +
  81742. + /** Pointer to PCD */
  81743. + struct dwc_otg_pcd *pcd;
  81744. +
  81745. + void *priv;
  81746. +} dwc_otg_pcd_ep_t;
  81747. +
  81748. +/** DWC_otg PCD Structure.
  81749. + * This structure encapsulates the data for the dwc_otg PCD.
  81750. + */
  81751. +struct dwc_otg_pcd {
  81752. + const struct dwc_otg_pcd_function_ops *fops;
  81753. + /** The DWC otg device pointer */
  81754. + struct dwc_otg_device *otg_dev;
  81755. + /** Core Interface */
  81756. + dwc_otg_core_if_t *core_if;
  81757. + /** State of EP0 */
  81758. + ep0state_e ep0state;
  81759. + /** EP0 Request is pending */
  81760. + unsigned ep0_pending:1;
  81761. + /** Indicates when SET CONFIGURATION Request is in process */
  81762. + unsigned request_config:1;
  81763. + /** The state of the Remote Wakeup Enable. */
  81764. + unsigned remote_wakeup_enable:1;
  81765. + /** The state of the B-Device HNP Enable. */
  81766. + unsigned b_hnp_enable:1;
  81767. + /** The state of A-Device HNP Support. */
  81768. + unsigned a_hnp_support:1;
  81769. + /** The state of the A-Device Alt HNP support. */
  81770. + unsigned a_alt_hnp_support:1;
  81771. + /** Count of pending Requests */
  81772. + unsigned request_pending;
  81773. +
  81774. + /** SETUP packet for EP0
  81775. + * This structure is allocated as a DMA buffer on PCD initialization
  81776. + * with enough space for up to 3 setup packets.
  81777. + */
  81778. + union {
  81779. + usb_device_request_t req;
  81780. + uint32_t d32[2];
  81781. + } *setup_pkt;
  81782. +
  81783. + dwc_dma_t setup_pkt_dma_handle;
  81784. +
  81785. + /* Additional buffer and flag for CTRL_WR premature case */
  81786. + uint8_t *backup_buf;
  81787. + unsigned data_terminated;
  81788. +
  81789. + /** 2-byte dma buffer used to return status from GET_STATUS */
  81790. + uint16_t *status_buf;
  81791. + dwc_dma_t status_buf_dma_handle;
  81792. +
  81793. + /** EP0 */
  81794. + dwc_otg_pcd_ep_t ep0;
  81795. +
  81796. + /** Array of IN EPs. */
  81797. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  81798. + /** Array of OUT EPs. */
  81799. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  81800. + /** number of valid EPs in the above array. */
  81801. +// unsigned num_eps : 4;
  81802. + dwc_spinlock_t *lock;
  81803. +
  81804. + /** Tasklet to defer starting of TEST mode transmissions until
  81805. + * Status Phase has been completed.
  81806. + */
  81807. + dwc_tasklet_t *test_mode_tasklet;
  81808. +
  81809. + /** Tasklet to delay starting of xfer in DMA mode */
  81810. + dwc_tasklet_t *start_xfer_tasklet;
  81811. +
  81812. + /** The test mode to enter when the tasklet is executed. */
  81813. + unsigned test_mode;
  81814. + /** The cfi_api structure that implements most of the CFI API
  81815. + * and OTG specific core configuration functionality
  81816. + */
  81817. +#ifdef DWC_UTE_CFI
  81818. + struct cfiobject *cfi;
  81819. +#endif
  81820. +
  81821. +};
  81822. +
  81823. +//FIXME this functions should be static, and this prototypes should be removed
  81824. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  81825. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  81826. + dwc_otg_pcd_request_t * req, int32_t status);
  81827. +
  81828. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  81829. + void *req_handle);
  81830. +
  81831. +extern void do_test_mode(void *data);
  81832. +#endif
  81833. +#endif /* DWC_HOST_ONLY */
  81834. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  81835. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 1970-01-01 01:00:00.000000000 +0100
  81836. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 2014-03-11 16:53:12.000000000 +0100
  81837. @@ -0,0 +1,360 @@
  81838. +/* ==========================================================================
  81839. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  81840. + * $Revision: #11 $
  81841. + * $Date: 2011/10/26 $
  81842. + * $Change: 1873028 $
  81843. + *
  81844. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  81845. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  81846. + * otherwise expressly agreed to in writing between Synopsys and you.
  81847. + *
  81848. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  81849. + * any End User Software License Agreement or Agreement for Licensed Product
  81850. + * with Synopsys or any supplement thereto. You are permitted to use and
  81851. + * redistribute this Software in source and binary forms, with or without
  81852. + * modification, provided that redistributions of source code must retain this
  81853. + * notice. You may not view, use, disclose, copy or distribute this file or
  81854. + * any information contained herein except pursuant to this license grant from
  81855. + * Synopsys. If you do not agree with this notice, including the disclaimer
  81856. + * below, then you are not authorized to use the Software.
  81857. + *
  81858. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  81859. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  81860. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  81861. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  81862. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  81863. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  81864. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  81865. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  81866. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  81867. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  81868. + * DAMAGE.
  81869. + * ========================================================================== */
  81870. +#ifndef DWC_HOST_ONLY
  81871. +
  81872. +#if !defined(__DWC_PCD_IF_H__)
  81873. +#define __DWC_PCD_IF_H__
  81874. +
  81875. +//#include "dwc_os.h"
  81876. +#include "dwc_otg_core_if.h"
  81877. +
  81878. +/** @file
  81879. + * This file defines DWC_OTG PCD Core API.
  81880. + */
  81881. +
  81882. +struct dwc_otg_pcd;
  81883. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  81884. +
  81885. +/** Maxpacket size for EP0 */
  81886. +#define MAX_EP0_SIZE 64
  81887. +/** Maxpacket size for any EP */
  81888. +#define MAX_PACKET_SIZE 1024
  81889. +
  81890. +/** @name Function Driver Callbacks */
  81891. +/** @{ */
  81892. +
  81893. +/** This function will be called whenever a previously queued request has
  81894. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  81895. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  81896. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  81897. + * parameters. */
  81898. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  81899. + void *req_handle, int32_t status,
  81900. + uint32_t actual);
  81901. +/**
  81902. + * This function will be called whenever a previousle queued ISOC request has
  81903. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  81904. + * function.
  81905. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  81906. + * functions.
  81907. + */
  81908. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  81909. + void *req_handle, int proc_buf_num);
  81910. +/** This function should handle any SETUP request that cannot be handled by the
  81911. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  81912. + * class-specific requests, etc. The function must non-blocking.
  81913. + *
  81914. + * Returns 0 on success.
  81915. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  81916. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  81917. + * Returns -DWC_E_SHUTDOWN on any other error. */
  81918. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  81919. +/** This is called whenever the device has been disconnected. The function
  81920. + * driver should take appropriate action to clean up all pending requests in the
  81921. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  81922. + * state. */
  81923. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  81924. +/** This function is called when device has been connected. */
  81925. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  81926. +/** This function is called when device has been suspended */
  81927. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  81928. +/** This function is called when device has received LPM tokens, i.e.
  81929. + * device has been sent to sleep state. */
  81930. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  81931. +/** This function is called when device has been resumed
  81932. + * from suspend(L2) or L1 sleep state. */
  81933. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  81934. +/** This function is called whenever hnp params has been changed.
  81935. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  81936. + * to get hnp parameters. */
  81937. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  81938. +/** This function is called whenever USB RESET is detected. */
  81939. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  81940. +
  81941. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  81942. +
  81943. +/**
  81944. + *
  81945. + * @param ep_handle Void pointer to the usb_ep structure
  81946. + * @param ereq_port Pointer to the extended request structure created in the
  81947. + * portable part.
  81948. + */
  81949. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  81950. + void *req_handle, int32_t status,
  81951. + void *ereq_port);
  81952. +/** Function Driver Ops Data Structure */
  81953. +struct dwc_otg_pcd_function_ops {
  81954. + dwc_connect_cb_t connect;
  81955. + dwc_disconnect_cb_t disconnect;
  81956. + dwc_setup_cb_t setup;
  81957. + dwc_completion_cb_t complete;
  81958. + dwc_isoc_completion_cb_t isoc_complete;
  81959. + dwc_suspend_cb_t suspend;
  81960. + dwc_sleep_cb_t sleep;
  81961. + dwc_resume_cb_t resume;
  81962. + dwc_reset_cb_t reset;
  81963. + dwc_hnp_params_changed_cb_t hnp_changed;
  81964. + cfi_setup_cb_t cfi_setup;
  81965. +#ifdef DWC_UTE_PER_IO
  81966. + xiso_completion_cb_t xisoc_complete;
  81967. +#endif
  81968. +};
  81969. +/** @} */
  81970. +
  81971. +/** @name Function Driver Functions */
  81972. +/** @{ */
  81973. +
  81974. +/** Call this function to get pointer on dwc_otg_pcd_t,
  81975. + * this pointer will be used for all PCD API functions.
  81976. + *
  81977. + * @param core_if The DWC_OTG Core
  81978. + */
  81979. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if);
  81980. +
  81981. +/** Frees PCD allocated by dwc_otg_pcd_init
  81982. + *
  81983. + * @param pcd The PCD
  81984. + */
  81985. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  81986. +
  81987. +/** Call this to bind the function driver to the PCD Core.
  81988. + *
  81989. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  81990. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  81991. + */
  81992. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  81993. + const struct dwc_otg_pcd_function_ops *fops);
  81994. +
  81995. +/** Enables an endpoint for use. This function enables an endpoint in
  81996. + * the PCD. The endpoint is described by the ep_desc which has the
  81997. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  81998. + * to the endpoint from other API functions and in callbacks. Normally this
  81999. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  82000. + * core for that interface.
  82001. + *
  82002. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82003. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82004. + * Returns 0 on success.
  82005. + *
  82006. + * @param pcd The PCD
  82007. + * @param ep_desc Endpoint descriptor
  82008. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  82009. + */
  82010. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  82011. + const uint8_t * ep_desc, void *usb_ep);
  82012. +
  82013. +/** Disable the endpoint referenced by ep_handle.
  82014. + *
  82015. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82016. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  82017. + * Returns 0 on success. */
  82018. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  82019. +
  82020. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  82021. + * After the transfer is completes, the complete callback will be called with
  82022. + * the request status.
  82023. + *
  82024. + * @param pcd The PCD
  82025. + * @param ep_handle The handle of the endpoint
  82026. + * @param buf The buffer for the data
  82027. + * @param dma_buf The DMA buffer for the data
  82028. + * @param buflen The length of the data transfer
  82029. + * @param zero Specifies whether to send zero length last packet.
  82030. + * @param req_handle Set this handle to any value to use to reference this
  82031. + * request in the ep_dequeue function or from the complete callback
  82032. + * @param atomic_alloc If driver need to perform atomic allocations
  82033. + * for internal data structures.
  82034. + *
  82035. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82036. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82037. + * Returns 0 on success. */
  82038. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82039. + uint8_t * buf, dwc_dma_t dma_buf,
  82040. + uint32_t buflen, int zero, void *req_handle,
  82041. + int atomic_alloc);
  82042. +#ifdef DWC_UTE_PER_IO
  82043. +/**
  82044. + *
  82045. + * @param ereq_nonport Pointer to the extended request part of the
  82046. + * usb_request structure defined in usb_gadget.h file.
  82047. + */
  82048. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82049. + uint8_t * buf, dwc_dma_t dma_buf,
  82050. + uint32_t buflen, int zero,
  82051. + void *req_handle, int atomic_alloc,
  82052. + void *ereq_nonport);
  82053. +
  82054. +#endif
  82055. +
  82056. +/** De-queue the specified data transfer that has not yet completed.
  82057. + *
  82058. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82059. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82060. + * Returns 0 on success. */
  82061. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  82062. + void *req_handle);
  82063. +
  82064. +/** Halt (STALL) an endpoint or clear it.
  82065. + *
  82066. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  82067. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  82068. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  82069. + * Returns 0 on success. */
  82070. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  82071. +
  82072. +/** This function */
  82073. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  82074. +
  82075. +/** This function should be called on every hardware interrupt */
  82076. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  82077. +
  82078. +/** This function returns current frame number */
  82079. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  82080. +
  82081. +/**
  82082. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  82083. + * For isochronous transfers duble buffering is used.
  82084. + * After processing each of buffers comlete callback will be called with
  82085. + * status for each transaction.
  82086. + *
  82087. + * @param pcd The PCD
  82088. + * @param ep_handle The handle of the endpoint
  82089. + * @param buf0 The virtual address of first data buffer
  82090. + * @param buf1 The virtual address of second data buffer
  82091. + * @param dma0 The DMA address of first data buffer
  82092. + * @param dma1 The DMA address of second data buffer
  82093. + * @param sync_frame Data pattern frame number
  82094. + * @param dp_frame Data size for pattern frame
  82095. + * @param data_per_frame Data size for regular frame
  82096. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  82097. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  82098. + * @param req_handle Handle of ISOC request
  82099. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  82100. + * internal data structures.
  82101. + *
  82102. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  82103. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  82104. + * Returns -DW_E_SHUTDOWN for any other error.
  82105. + * Returns 0 on success
  82106. + */
  82107. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  82108. + uint8_t * buf0, uint8_t * buf1,
  82109. + dwc_dma_t dma0, dwc_dma_t dma1,
  82110. + int sync_frame, int dp_frame,
  82111. + int data_per_frame, int start_frame,
  82112. + int buf_proc_intrvl, void *req_handle,
  82113. + int atomic_alloc);
  82114. +
  82115. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  82116. + *
  82117. + * @param pcd The PCD
  82118. + * @param ep_handle The handle of the endpoint
  82119. + * @param req_handle Handle of ISOC request
  82120. + *
  82121. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  82122. + * Returns 0 on success
  82123. + */
  82124. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  82125. + void *req_handle);
  82126. +
  82127. +/** Get ISOC packet status.
  82128. + *
  82129. + * @param pcd The PCD
  82130. + * @param ep_handle The handle of the endpoint
  82131. + * @param iso_req_handle Isochronoush request handle
  82132. + * @param packet Number of packet
  82133. + * @param status Out parameter for returning status
  82134. + * @param actual Out parameter for returning actual length
  82135. + * @param offset Out parameter for returning offset
  82136. + *
  82137. + */
  82138. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  82139. + void *ep_handle,
  82140. + void *iso_req_handle, int packet,
  82141. + int *status, int *actual,
  82142. + int *offset);
  82143. +
  82144. +/** Get ISOC packet count.
  82145. + *
  82146. + * @param pcd The PCD
  82147. + * @param ep_handle The handle of the endpoint
  82148. + * @param iso_req_handle
  82149. + */
  82150. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  82151. + void *ep_handle,
  82152. + void *iso_req_handle);
  82153. +
  82154. +/** This function starts the SRP Protocol if no session is in progress. If
  82155. + * a session is already in progress, but the device is suspended,
  82156. + * remote wakeup signaling is started.
  82157. + */
  82158. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  82159. +
  82160. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  82161. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  82162. +
  82163. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  82164. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  82165. +
  82166. +/** Initiate SRP */
  82167. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  82168. +
  82169. +/** Starts remote wakeup signaling. */
  82170. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  82171. +
  82172. +/** Starts micorsecond soft disconnect. */
  82173. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  82174. +/** This function returns whether device is dualspeed.*/
  82175. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  82176. +
  82177. +/** This function returns whether device is otg. */
  82178. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  82179. +
  82180. +/** These functions allow to get hnp parameters */
  82181. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  82182. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  82183. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  82184. +
  82185. +/** CFI specific Interface functions */
  82186. +/** Allocate a cfi buffer */
  82187. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  82188. + dwc_dma_t * addr, size_t buflen,
  82189. + int flags);
  82190. +
  82191. +/******************************************************************************/
  82192. +
  82193. +/** @} */
  82194. +
  82195. +#endif /* __DWC_PCD_IF_H__ */
  82196. +
  82197. +#endif /* DWC_HOST_ONLY */
  82198. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  82199. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 1970-01-01 01:00:00.000000000 +0100
  82200. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 2014-03-11 16:55:38.000000000 +0100
  82201. @@ -0,0 +1,5147 @@
  82202. +/* ==========================================================================
  82203. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  82204. + * $Revision: #116 $
  82205. + * $Date: 2012/08/10 $
  82206. + * $Change: 2047372 $
  82207. + *
  82208. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82209. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82210. + * otherwise expressly agreed to in writing between Synopsys and you.
  82211. + *
  82212. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82213. + * any End User Software License Agreement or Agreement for Licensed Product
  82214. + * with Synopsys or any supplement thereto. You are permitted to use and
  82215. + * redistribute this Software in source and binary forms, with or without
  82216. + * modification, provided that redistributions of source code must retain this
  82217. + * notice. You may not view, use, disclose, copy or distribute this file or
  82218. + * any information contained herein except pursuant to this license grant from
  82219. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82220. + * below, then you are not authorized to use the Software.
  82221. + *
  82222. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82223. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82224. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82225. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82226. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82227. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82228. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82229. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82230. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82231. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82232. + * DAMAGE.
  82233. + * ========================================================================== */
  82234. +#ifndef DWC_HOST_ONLY
  82235. +
  82236. +#include "dwc_otg_pcd.h"
  82237. +
  82238. +#ifdef DWC_UTE_CFI
  82239. +#include "dwc_otg_cfi.h"
  82240. +#endif
  82241. +
  82242. +#ifdef DWC_UTE_PER_IO
  82243. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  82244. +#endif
  82245. +//#define PRINT_CFI_DMA_DESCS
  82246. +
  82247. +#define DEBUG_EP0
  82248. +
  82249. +/**
  82250. + * This function updates OTG.
  82251. + */
  82252. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  82253. +{
  82254. +
  82255. + if (reset) {
  82256. + pcd->b_hnp_enable = 0;
  82257. + pcd->a_hnp_support = 0;
  82258. + pcd->a_alt_hnp_support = 0;
  82259. + }
  82260. +
  82261. + if (pcd->fops->hnp_changed) {
  82262. + pcd->fops->hnp_changed(pcd);
  82263. + }
  82264. +}
  82265. +
  82266. +/** @file
  82267. + * This file contains the implementation of the PCD Interrupt handlers.
  82268. + *
  82269. + * The PCD handles the device interrupts. Many conditions can cause a
  82270. + * device interrupt. When an interrupt occurs, the device interrupt
  82271. + * service routine determines the cause of the interrupt and
  82272. + * dispatches handling to the appropriate function. These interrupt
  82273. + * handling functions are described below.
  82274. + * All interrupt registers are processed from LSB to MSB.
  82275. + */
  82276. +
  82277. +/**
  82278. + * This function prints the ep0 state for debug purposes.
  82279. + */
  82280. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  82281. +{
  82282. +#ifdef DEBUG
  82283. + char str[40];
  82284. +
  82285. + switch (pcd->ep0state) {
  82286. + case EP0_DISCONNECT:
  82287. + dwc_strcpy(str, "EP0_DISCONNECT");
  82288. + break;
  82289. + case EP0_IDLE:
  82290. + dwc_strcpy(str, "EP0_IDLE");
  82291. + break;
  82292. + case EP0_IN_DATA_PHASE:
  82293. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  82294. + break;
  82295. + case EP0_OUT_DATA_PHASE:
  82296. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  82297. + break;
  82298. + case EP0_IN_STATUS_PHASE:
  82299. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  82300. + break;
  82301. + case EP0_OUT_STATUS_PHASE:
  82302. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  82303. + break;
  82304. + case EP0_STALL:
  82305. + dwc_strcpy(str, "EP0_STALL");
  82306. + break;
  82307. + default:
  82308. + dwc_strcpy(str, "EP0_INVALID");
  82309. + }
  82310. +
  82311. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  82312. +#endif
  82313. +}
  82314. +
  82315. +/**
  82316. + * This function calculate the size of the payload in the memory
  82317. + * for out endpoints and prints size for debug purposes(used in
  82318. + * 2.93a DevOutNak feature).
  82319. + */
  82320. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  82321. +{
  82322. +#ifdef DEBUG
  82323. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  82324. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  82325. + int pack_num;
  82326. + unsigned payload;
  82327. +
  82328. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  82329. + deptsiz_updt.d32 =
  82330. + DWC_READ_REG32(&pcd->core_if->dev_if->
  82331. + out_ep_regs[ep->num]->doeptsiz);
  82332. + /* Payload will be */
  82333. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  82334. + /* Packet count is decremented every time a packet
  82335. + * is written to the RxFIFO not in to the external memory
  82336. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  82337. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  82338. + DWC_DEBUGPL(DBG_PCDV,
  82339. + "Payload for EP%d-%s\n",
  82340. + ep->num, (ep->is_in ? "IN" : "OUT"));
  82341. + DWC_DEBUGPL(DBG_PCDV,
  82342. + "Number of transfered bytes = 0x%08x\n", payload);
  82343. + DWC_DEBUGPL(DBG_PCDV,
  82344. + "Number of transfered packets = %d\n", pack_num);
  82345. +#endif
  82346. +}
  82347. +
  82348. +
  82349. +#ifdef DWC_UTE_CFI
  82350. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  82351. + const uint8_t * epname, int descnum)
  82352. +{
  82353. + CFI_INFO
  82354. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  82355. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  82356. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  82357. + ddesc->status.b.bs);
  82358. +}
  82359. +#endif
  82360. +
  82361. +/**
  82362. + * This function returns pointer to in ep struct with number ep_num
  82363. + */
  82364. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  82365. +{
  82366. + int i;
  82367. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  82368. + if (ep_num == 0) {
  82369. + return &pcd->ep0;
  82370. + } else {
  82371. + for (i = 0; i < num_in_eps; ++i) {
  82372. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  82373. + return &pcd->in_ep[i];
  82374. + }
  82375. + return 0;
  82376. + }
  82377. +}
  82378. +
  82379. +/**
  82380. + * This function returns pointer to out ep struct with number ep_num
  82381. + */
  82382. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  82383. +{
  82384. + int i;
  82385. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  82386. + if (ep_num == 0) {
  82387. + return &pcd->ep0;
  82388. + } else {
  82389. + for (i = 0; i < num_out_eps; ++i) {
  82390. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  82391. + return &pcd->out_ep[i];
  82392. + }
  82393. + return 0;
  82394. + }
  82395. +}
  82396. +
  82397. +/**
  82398. + * This functions gets a pointer to an EP from the wIndex address
  82399. + * value of the control request.
  82400. + */
  82401. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  82402. +{
  82403. + dwc_otg_pcd_ep_t *ep;
  82404. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  82405. +
  82406. + if (ep_num == 0) {
  82407. + ep = &pcd->ep0;
  82408. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  82409. + ep = &pcd->in_ep[ep_num - 1];
  82410. + } else {
  82411. + ep = &pcd->out_ep[ep_num - 1];
  82412. + }
  82413. +
  82414. + return ep;
  82415. +}
  82416. +
  82417. +/**
  82418. + * This function checks the EP request queue, if the queue is not
  82419. + * empty the next request is started.
  82420. + */
  82421. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  82422. +{
  82423. + dwc_otg_pcd_request_t *req = 0;
  82424. + uint32_t max_transfer =
  82425. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  82426. +
  82427. +#ifdef DWC_UTE_CFI
  82428. + struct dwc_otg_pcd *pcd;
  82429. + pcd = ep->pcd;
  82430. +#endif
  82431. +
  82432. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  82433. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  82434. +
  82435. +#ifdef DWC_UTE_CFI
  82436. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  82437. + ep->dwc_ep.cfi_req_len = req->length;
  82438. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  82439. + } else {
  82440. +#endif
  82441. + /* Setup and start the Transfer */
  82442. + if (req->dw_align_buf) {
  82443. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  82444. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  82445. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  82446. + } else {
  82447. + ep->dwc_ep.dma_addr = req->dma;
  82448. + ep->dwc_ep.start_xfer_buff = req->buf;
  82449. + ep->dwc_ep.xfer_buff = req->buf;
  82450. + }
  82451. + ep->dwc_ep.sent_zlp = 0;
  82452. + ep->dwc_ep.total_len = req->length;
  82453. + ep->dwc_ep.xfer_len = 0;
  82454. + ep->dwc_ep.xfer_count = 0;
  82455. +
  82456. + ep->dwc_ep.maxxfer = max_transfer;
  82457. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  82458. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  82459. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  82460. + if (ep->dwc_ep.is_in) {
  82461. + if (ep->dwc_ep.maxxfer >
  82462. + DDMA_MAX_TRANSFER_SIZE) {
  82463. + ep->dwc_ep.maxxfer =
  82464. + DDMA_MAX_TRANSFER_SIZE;
  82465. + }
  82466. + } else {
  82467. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  82468. + ep->dwc_ep.maxxfer =
  82469. + out_max_xfer;
  82470. + }
  82471. + }
  82472. + }
  82473. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  82474. + ep->dwc_ep.maxxfer -=
  82475. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  82476. + }
  82477. + if (req->sent_zlp) {
  82478. + if ((ep->dwc_ep.total_len %
  82479. + ep->dwc_ep.maxpacket == 0)
  82480. + && (ep->dwc_ep.total_len != 0)) {
  82481. + ep->dwc_ep.sent_zlp = 1;
  82482. + }
  82483. +
  82484. + }
  82485. +#ifdef DWC_UTE_CFI
  82486. + }
  82487. +#endif
  82488. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  82489. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  82490. + DWC_PRINTF("There are no more ISOC requests \n");
  82491. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  82492. + }
  82493. +}
  82494. +
  82495. +/**
  82496. + * This function handles the SOF Interrupts. At this time the SOF
  82497. + * Interrupt is disabled.
  82498. + */
  82499. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  82500. +{
  82501. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82502. +
  82503. + gintsts_data_t gintsts;
  82504. +
  82505. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  82506. +
  82507. + /* Clear interrupt */
  82508. + gintsts.d32 = 0;
  82509. + gintsts.b.sofintr = 1;
  82510. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  82511. +
  82512. + return 1;
  82513. +}
  82514. +
  82515. +/**
  82516. + * This function handles the Rx Status Queue Level Interrupt, which
  82517. + * indicates that there is a least one packet in the Rx FIFO. The
  82518. + * packets are moved from the FIFO to memory, where they will be
  82519. + * processed when the Endpoint Interrupt Register indicates Transfer
  82520. + * Complete or SETUP Phase Done.
  82521. + *
  82522. + * Repeat the following until the Rx Status Queue is empty:
  82523. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  82524. + * info
  82525. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  82526. + * and exit
  82527. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  82528. + * SETUP data to the buffer
  82529. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  82530. + * to the destination buffer
  82531. + */
  82532. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  82533. +{
  82534. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82535. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  82536. + gintmsk_data_t gintmask = {.d32 = 0 };
  82537. + device_grxsts_data_t status;
  82538. + dwc_otg_pcd_ep_t *ep;
  82539. + gintsts_data_t gintsts;
  82540. +#ifdef DEBUG
  82541. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  82542. +#endif
  82543. +
  82544. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  82545. + /* Disable the Rx Status Queue Level interrupt */
  82546. + gintmask.b.rxstsqlvl = 1;
  82547. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  82548. +
  82549. + /* Get the Status from the top of the FIFO */
  82550. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  82551. +
  82552. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  82553. + "pktsts:%x Frame:%d(0x%0x)\n",
  82554. + status.b.epnum, status.b.bcnt,
  82555. + dpid_str[status.b.dpid],
  82556. + status.b.pktsts, status.b.fn, status.b.fn);
  82557. + /* Get pointer to EP structure */
  82558. + ep = get_out_ep(pcd, status.b.epnum);
  82559. +
  82560. + switch (status.b.pktsts) {
  82561. + case DWC_DSTS_GOUT_NAK:
  82562. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  82563. + break;
  82564. + case DWC_STS_DATA_UPDT:
  82565. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  82566. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  82567. + /** @todo NGS Check for buffer overflow? */
  82568. + dwc_otg_read_packet(core_if,
  82569. + ep->dwc_ep.xfer_buff,
  82570. + status.b.bcnt);
  82571. + ep->dwc_ep.xfer_count += status.b.bcnt;
  82572. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  82573. + }
  82574. + break;
  82575. + case DWC_STS_XFER_COMP:
  82576. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  82577. + break;
  82578. + case DWC_DSTS_SETUP_COMP:
  82579. +#ifdef DEBUG_EP0
  82580. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  82581. +#endif
  82582. + break;
  82583. + case DWC_DSTS_SETUP_UPDT:
  82584. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  82585. +#ifdef DEBUG_EP0
  82586. + DWC_DEBUGPL(DBG_PCD,
  82587. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  82588. + pcd->setup_pkt->req.bmRequestType,
  82589. + pcd->setup_pkt->req.bRequest,
  82590. + UGETW(pcd->setup_pkt->req.wValue),
  82591. + UGETW(pcd->setup_pkt->req.wIndex),
  82592. + UGETW(pcd->setup_pkt->req.wLength));
  82593. +#endif
  82594. + ep->dwc_ep.xfer_count += status.b.bcnt;
  82595. + break;
  82596. + default:
  82597. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  82598. + status.b.pktsts);
  82599. + break;
  82600. + }
  82601. +
  82602. + /* Enable the Rx Status Queue Level interrupt */
  82603. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  82604. + /* Clear interrupt */
  82605. + gintsts.d32 = 0;
  82606. + gintsts.b.rxstsqlvl = 1;
  82607. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  82608. +
  82609. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  82610. + return 1;
  82611. +}
  82612. +
  82613. +/**
  82614. + * This function examines the Device IN Token Learning Queue to
  82615. + * determine the EP number of the last IN token received. This
  82616. + * implementation is for the Mass Storage device where there are only
  82617. + * 2 IN EPs (Control-IN and BULK-IN).
  82618. + *
  82619. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  82620. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  82621. + *
  82622. + * @param core_if Programming view of DWC_otg controller.
  82623. + *
  82624. + */
  82625. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  82626. +{
  82627. + dwc_otg_device_global_regs_t *dev_global_regs =
  82628. + core_if->dev_if->dev_global_regs;
  82629. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  82630. + /* Number of Token Queue Registers */
  82631. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  82632. + dtknq1_data_t dtknqr1;
  82633. + uint32_t in_tkn_epnums[4];
  82634. + int ndx = 0;
  82635. + int i = 0;
  82636. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  82637. + int epnum = 0;
  82638. +
  82639. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  82640. +
  82641. + /* Read the DTKNQ Registers */
  82642. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  82643. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  82644. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  82645. + in_tkn_epnums[i]);
  82646. + if (addr == &dev_global_regs->dvbusdis) {
  82647. + addr = &dev_global_regs->dtknqr3_dthrctl;
  82648. + } else {
  82649. + ++addr;
  82650. + }
  82651. +
  82652. + }
  82653. +
  82654. + /* Copy the DTKNQR1 data to the bit field. */
  82655. + dtknqr1.d32 = in_tkn_epnums[0];
  82656. + /* Get the EP numbers */
  82657. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  82658. + ndx = dtknqr1.b.intknwptr - 1;
  82659. +
  82660. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  82661. + if (ndx == -1) {
  82662. + /** @todo Find a simpler way to calculate the max
  82663. + * queue position.*/
  82664. + int cnt = TOKEN_Q_DEPTH;
  82665. + if (TOKEN_Q_DEPTH <= 6) {
  82666. + cnt = TOKEN_Q_DEPTH - 1;
  82667. + } else if (TOKEN_Q_DEPTH <= 14) {
  82668. + cnt = TOKEN_Q_DEPTH - 7;
  82669. + } else if (TOKEN_Q_DEPTH <= 22) {
  82670. + cnt = TOKEN_Q_DEPTH - 15;
  82671. + } else {
  82672. + cnt = TOKEN_Q_DEPTH - 23;
  82673. + }
  82674. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  82675. + } else {
  82676. + if (ndx <= 5) {
  82677. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  82678. + } else if (ndx <= 13) {
  82679. + ndx -= 6;
  82680. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  82681. + } else if (ndx <= 21) {
  82682. + ndx -= 14;
  82683. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  82684. + } else if (ndx <= 29) {
  82685. + ndx -= 22;
  82686. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  82687. + }
  82688. + }
  82689. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  82690. + return epnum;
  82691. +}
  82692. +
  82693. +/**
  82694. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  82695. + * The active request is checked for the next packet to be loaded into
  82696. + * the non-periodic Tx FIFO.
  82697. + */
  82698. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  82699. +{
  82700. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82701. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  82702. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  82703. + gnptxsts_data_t txstatus = {.d32 = 0 };
  82704. + gintsts_data_t gintsts;
  82705. +
  82706. + int epnum = 0;
  82707. + dwc_otg_pcd_ep_t *ep = 0;
  82708. + uint32_t len = 0;
  82709. + int dwords;
  82710. +
  82711. + /* Get the epnum from the IN Token Learning Queue. */
  82712. + epnum = get_ep_of_last_in_token(core_if);
  82713. + ep = get_in_ep(pcd, epnum);
  82714. +
  82715. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  82716. +
  82717. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  82718. +
  82719. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  82720. + if (len > ep->dwc_ep.maxpacket) {
  82721. + len = ep->dwc_ep.maxpacket;
  82722. + }
  82723. + dwords = (len + 3) / 4;
  82724. +
  82725. + /* While there is space in the queue and space in the FIFO and
  82726. + * More data to tranfer, Write packets to the Tx FIFO */
  82727. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  82728. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  82729. +
  82730. + while (txstatus.b.nptxqspcavail > 0 &&
  82731. + txstatus.b.nptxfspcavail > dwords &&
  82732. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  82733. + /* Write the FIFO */
  82734. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  82735. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  82736. +
  82737. + if (len > ep->dwc_ep.maxpacket) {
  82738. + len = ep->dwc_ep.maxpacket;
  82739. + }
  82740. +
  82741. + dwords = (len + 3) / 4;
  82742. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  82743. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  82744. + }
  82745. +
  82746. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  82747. + DWC_READ_REG32(&global_regs->gnptxsts));
  82748. +
  82749. + /* Clear interrupt */
  82750. + gintsts.d32 = 0;
  82751. + gintsts.b.nptxfempty = 1;
  82752. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  82753. +
  82754. + return 1;
  82755. +}
  82756. +
  82757. +/**
  82758. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  82759. + * The active request is checked for the next packet to be loaded into
  82760. + * apropriate Tx FIFO.
  82761. + */
  82762. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  82763. +{
  82764. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  82765. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  82766. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  82767. + dtxfsts_data_t txstatus = {.d32 = 0 };
  82768. + dwc_otg_pcd_ep_t *ep = 0;
  82769. + uint32_t len = 0;
  82770. + int dwords;
  82771. +
  82772. + ep = get_in_ep(pcd, epnum);
  82773. +
  82774. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  82775. +
  82776. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  82777. +
  82778. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  82779. +
  82780. + if (len > ep->dwc_ep.maxpacket) {
  82781. + len = ep->dwc_ep.maxpacket;
  82782. + }
  82783. +
  82784. + dwords = (len + 3) / 4;
  82785. +
  82786. + /* While there is space in the queue and space in the FIFO and
  82787. + * More data to tranfer, Write packets to the Tx FIFO */
  82788. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  82789. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  82790. +
  82791. + while (txstatus.b.txfspcavail > dwords &&
  82792. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  82793. + ep->dwc_ep.xfer_len != 0) {
  82794. + /* Write the FIFO */
  82795. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  82796. +
  82797. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  82798. + if (len > ep->dwc_ep.maxpacket) {
  82799. + len = ep->dwc_ep.maxpacket;
  82800. + }
  82801. +
  82802. + dwords = (len + 3) / 4;
  82803. + txstatus.d32 =
  82804. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  82805. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  82806. + txstatus.d32);
  82807. + }
  82808. +
  82809. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  82810. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  82811. +
  82812. + return 1;
  82813. +}
  82814. +
  82815. +/**
  82816. + * This function is called when the Device is disconnected. It stops
  82817. + * any active requests and informs the Gadget driver of the
  82818. + * disconnect.
  82819. + */
  82820. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  82821. +{
  82822. + int i, num_in_eps, num_out_eps;
  82823. + dwc_otg_pcd_ep_t *ep;
  82824. +
  82825. + gintmsk_data_t intr_mask = {.d32 = 0 };
  82826. +
  82827. + DWC_SPINLOCK(pcd->lock);
  82828. +
  82829. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  82830. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  82831. +
  82832. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  82833. + /* don't disconnect drivers more than once */
  82834. + if (pcd->ep0state == EP0_DISCONNECT) {
  82835. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  82836. + DWC_SPINUNLOCK(pcd->lock);
  82837. + return;
  82838. + }
  82839. + pcd->ep0state = EP0_DISCONNECT;
  82840. +
  82841. + /* Reset the OTG state. */
  82842. + dwc_otg_pcd_update_otg(pcd, 1);
  82843. +
  82844. + /* Disable the NP Tx Fifo Empty Interrupt. */
  82845. + intr_mask.b.nptxfempty = 1;
  82846. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  82847. + intr_mask.d32, 0);
  82848. +
  82849. + /* Flush the FIFOs */
  82850. + /**@todo NGS Flush Periodic FIFOs */
  82851. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  82852. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  82853. +
  82854. + /* prevent new request submissions, kill any outstanding requests */
  82855. + ep = &pcd->ep0;
  82856. + dwc_otg_request_nuke(ep);
  82857. + /* prevent new request submissions, kill any outstanding requests */
  82858. + for (i = 0; i < num_in_eps; i++) {
  82859. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  82860. + dwc_otg_request_nuke(ep);
  82861. + }
  82862. + /* prevent new request submissions, kill any outstanding requests */
  82863. + for (i = 0; i < num_out_eps; i++) {
  82864. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  82865. + dwc_otg_request_nuke(ep);
  82866. + }
  82867. +
  82868. + /* report disconnect; the driver is already quiesced */
  82869. + if (pcd->fops->disconnect) {
  82870. + DWC_SPINUNLOCK(pcd->lock);
  82871. + pcd->fops->disconnect(pcd);
  82872. + DWC_SPINLOCK(pcd->lock);
  82873. + }
  82874. + DWC_SPINUNLOCK(pcd->lock);
  82875. +}
  82876. +
  82877. +/**
  82878. + * This interrupt indicates that ...
  82879. + */
  82880. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  82881. +{
  82882. + gintmsk_data_t intr_mask = {.d32 = 0 };
  82883. + gintsts_data_t gintsts;
  82884. +
  82885. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  82886. + intr_mask.b.i2cintr = 1;
  82887. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  82888. + intr_mask.d32, 0);
  82889. +
  82890. + /* Clear interrupt */
  82891. + gintsts.d32 = 0;
  82892. + gintsts.b.i2cintr = 1;
  82893. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  82894. + gintsts.d32);
  82895. + return 1;
  82896. +}
  82897. +
  82898. +/**
  82899. + * This interrupt indicates that ...
  82900. + */
  82901. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  82902. +{
  82903. + gintsts_data_t gintsts;
  82904. +#if defined(VERBOSE)
  82905. + DWC_PRINTF("Early Suspend Detected\n");
  82906. +#endif
  82907. +
  82908. + /* Clear interrupt */
  82909. + gintsts.d32 = 0;
  82910. + gintsts.b.erlysuspend = 1;
  82911. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  82912. + gintsts.d32);
  82913. + return 1;
  82914. +}
  82915. +
  82916. +/**
  82917. + * This function configures EPO to receive SETUP packets.
  82918. + *
  82919. + * @todo NGS: Update the comments from the HW FS.
  82920. + *
  82921. + * -# Program the following fields in the endpoint specific registers
  82922. + * for Control OUT EP 0, in order to receive a setup packet
  82923. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  82924. + * setup packets)
  82925. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  82926. + * to back setup packets)
  82927. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  82928. + * store any setup packets received
  82929. + *
  82930. + * @param core_if Programming view of DWC_otg controller.
  82931. + * @param pcd Programming view of the PCD.
  82932. + */
  82933. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  82934. + dwc_otg_pcd_t * pcd)
  82935. +{
  82936. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  82937. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  82938. + dwc_otg_dev_dma_desc_t *dma_desc;
  82939. + depctl_data_t doepctl = {.d32 = 0 };
  82940. +
  82941. +#ifdef VERBOSE
  82942. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  82943. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  82944. +#endif
  82945. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  82946. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  82947. + if (doepctl.b.epena) {
  82948. + return;
  82949. + }
  82950. + }
  82951. +
  82952. + doeptsize0.b.supcnt = 3;
  82953. + doeptsize0.b.pktcnt = 1;
  82954. + doeptsize0.b.xfersize = 8 * 3;
  82955. +
  82956. + if (core_if->dma_enable) {
  82957. + if (!core_if->dma_desc_enable) {
  82958. + /** put here as for Hermes mode deptisz register should not be written */
  82959. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  82960. + doeptsize0.d32);
  82961. +
  82962. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  82963. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  82964. + pcd->setup_pkt_dma_handle);
  82965. + } else {
  82966. + dev_if->setup_desc_index =
  82967. + (dev_if->setup_desc_index + 1) & 1;
  82968. + dma_desc =
  82969. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  82970. +
  82971. + /** DMA Descriptor Setup */
  82972. + dma_desc->status.b.bs = BS_HOST_BUSY;
  82973. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  82974. + dma_desc->status.b.sr = 0;
  82975. + dma_desc->status.b.mtrf = 0;
  82976. + }
  82977. + dma_desc->status.b.l = 1;
  82978. + dma_desc->status.b.ioc = 1;
  82979. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  82980. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  82981. + dma_desc->status.b.sts = 0;
  82982. + dma_desc->status.b.bs = BS_HOST_READY;
  82983. +
  82984. + /** DOEPDMA0 Register write */
  82985. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  82986. + dev_if->dma_setup_desc_addr
  82987. + [dev_if->setup_desc_index]);
  82988. + }
  82989. +
  82990. + } else {
  82991. + /** put here as for Hermes mode deptisz register should not be written */
  82992. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  82993. + doeptsize0.d32);
  82994. + }
  82995. +
  82996. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  82997. + doepctl.d32 = 0;
  82998. + doepctl.b.epena = 1;
  82999. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  83000. + doepctl.b.cnak = 1;
  83001. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  83002. + } else {
  83003. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  83004. + }
  83005. +
  83006. +#ifdef VERBOSE
  83007. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  83008. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  83009. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  83010. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  83011. +#endif
  83012. +}
  83013. +
  83014. +/**
  83015. + * This interrupt occurs when a USB Reset is detected. When the USB
  83016. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  83017. + * EP0 state is set to IDLE.
  83018. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  83019. + * -# Unmask the following interrupt bits
  83020. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  83021. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  83022. + * - DOEPMSK.SETUP = 1
  83023. + * - DOEPMSK.XferCompl = 1
  83024. + * - DIEPMSK.XferCompl = 1
  83025. + * - DIEPMSK.TimeOut = 1
  83026. + * -# Program the following fields in the endpoint specific registers
  83027. + * for Control OUT EP 0, in order to receive a setup packet
  83028. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  83029. + * setup packets)
  83030. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  83031. + * to back setup packets)
  83032. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  83033. + * store any setup packets received
  83034. + * At this point, all the required initialization, except for enabling
  83035. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  83036. + */
  83037. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  83038. +{
  83039. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83040. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  83041. + depctl_data_t doepctl = {.d32 = 0 };
  83042. + depctl_data_t diepctl = {.d32 = 0 };
  83043. + daint_data_t daintmsk = {.d32 = 0 };
  83044. + doepmsk_data_t doepmsk = {.d32 = 0 };
  83045. + diepmsk_data_t diepmsk = {.d32 = 0 };
  83046. + dcfg_data_t dcfg = {.d32 = 0 };
  83047. + grstctl_t resetctl = {.d32 = 0 };
  83048. + dctl_data_t dctl = {.d32 = 0 };
  83049. + int i = 0;
  83050. + gintsts_data_t gintsts;
  83051. + pcgcctl_data_t power = {.d32 = 0 };
  83052. +
  83053. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  83054. + if (power.b.stoppclk) {
  83055. + power.d32 = 0;
  83056. + power.b.stoppclk = 1;
  83057. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83058. +
  83059. + power.b.pwrclmp = 1;
  83060. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83061. +
  83062. + power.b.rstpdwnmodule = 1;
  83063. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  83064. + }
  83065. +
  83066. + core_if->lx_state = DWC_OTG_L0;
  83067. +
  83068. + DWC_PRINTF("USB RESET\n");
  83069. +#ifdef DWC_EN_ISOC
  83070. + for (i = 1; i < 16; ++i) {
  83071. + dwc_otg_pcd_ep_t *ep;
  83072. + dwc_ep_t *dwc_ep;
  83073. + ep = get_in_ep(pcd, i);
  83074. + if (ep != 0) {
  83075. + dwc_ep = &ep->dwc_ep;
  83076. + dwc_ep->next_frame = 0xffffffff;
  83077. + }
  83078. + }
  83079. +#endif /* DWC_EN_ISOC */
  83080. +
  83081. + /* reset the HNP settings */
  83082. + dwc_otg_pcd_update_otg(pcd, 1);
  83083. +
  83084. + /* Clear the Remote Wakeup Signalling */
  83085. + dctl.b.rmtwkupsig = 1;
  83086. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  83087. +
  83088. + /* Set NAK for all OUT EPs */
  83089. + doepctl.b.snak = 1;
  83090. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  83091. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  83092. + }
  83093. +
  83094. + /* Flush the NP Tx FIFO */
  83095. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  83096. + /* Flush the Learning Queue */
  83097. + resetctl.b.intknqflsh = 1;
  83098. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  83099. +
  83100. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  83101. + core_if->start_predict = 0;
  83102. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  83103. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  83104. + }
  83105. + core_if->nextep_seq[0] = 0;
  83106. + core_if->first_in_nextep_seq = 0;
  83107. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  83108. + diepctl.b.nextep = 0;
  83109. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  83110. +
  83111. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  83112. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  83113. + dcfg.b.epmscnt = 2;
  83114. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  83115. +
  83116. + DWC_DEBUGPL(DBG_PCDV,
  83117. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  83118. + __func__, core_if->first_in_nextep_seq);
  83119. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  83120. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  83121. + }
  83122. + }
  83123. +
  83124. + if (core_if->multiproc_int_enable) {
  83125. + daintmsk.b.inep0 = 1;
  83126. + daintmsk.b.outep0 = 1;
  83127. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  83128. + daintmsk.d32);
  83129. +
  83130. + doepmsk.b.setup = 1;
  83131. + doepmsk.b.xfercompl = 1;
  83132. + doepmsk.b.ahberr = 1;
  83133. + doepmsk.b.epdisabled = 1;
  83134. +
  83135. + if ((core_if->dma_desc_enable) ||
  83136. + (core_if->dma_enable
  83137. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  83138. + doepmsk.b.stsphsercvd = 1;
  83139. + }
  83140. + if (core_if->dma_desc_enable)
  83141. + doepmsk.b.bna = 1;
  83142. +/*
  83143. + doepmsk.b.babble = 1;
  83144. + doepmsk.b.nyet = 1;
  83145. +
  83146. + if (core_if->dma_enable) {
  83147. + doepmsk.b.nak = 1;
  83148. + }
  83149. +*/
  83150. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  83151. + doepmsk.d32);
  83152. +
  83153. + diepmsk.b.xfercompl = 1;
  83154. + diepmsk.b.timeout = 1;
  83155. + diepmsk.b.epdisabled = 1;
  83156. + diepmsk.b.ahberr = 1;
  83157. + diepmsk.b.intknepmis = 1;
  83158. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  83159. + diepmsk.b.intknepmis = 0;
  83160. +
  83161. +/* if (core_if->dma_desc_enable) {
  83162. + diepmsk.b.bna = 1;
  83163. + }
  83164. +*/
  83165. +/*
  83166. + if (core_if->dma_enable) {
  83167. + diepmsk.b.nak = 1;
  83168. + }
  83169. +*/
  83170. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  83171. + diepmsk.d32);
  83172. + } else {
  83173. + daintmsk.b.inep0 = 1;
  83174. + daintmsk.b.outep0 = 1;
  83175. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  83176. + daintmsk.d32);
  83177. +
  83178. + doepmsk.b.setup = 1;
  83179. + doepmsk.b.xfercompl = 1;
  83180. + doepmsk.b.ahberr = 1;
  83181. + doepmsk.b.epdisabled = 1;
  83182. +
  83183. + if ((core_if->dma_desc_enable) ||
  83184. + (core_if->dma_enable
  83185. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  83186. + doepmsk.b.stsphsercvd = 1;
  83187. + }
  83188. + if (core_if->dma_desc_enable)
  83189. + doepmsk.b.bna = 1;
  83190. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  83191. +
  83192. + diepmsk.b.xfercompl = 1;
  83193. + diepmsk.b.timeout = 1;
  83194. + diepmsk.b.epdisabled = 1;
  83195. + diepmsk.b.ahberr = 1;
  83196. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  83197. + diepmsk.b.intknepmis = 0;
  83198. +/*
  83199. + if (core_if->dma_desc_enable) {
  83200. + diepmsk.b.bna = 1;
  83201. + }
  83202. +*/
  83203. +
  83204. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  83205. + }
  83206. +
  83207. + /* Reset Device Address */
  83208. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  83209. + dcfg.b.devaddr = 0;
  83210. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  83211. +
  83212. + /* setup EP0 to receive SETUP packets */
  83213. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  83214. + ep0_out_start(core_if, pcd);
  83215. +
  83216. + /* Clear interrupt */
  83217. + gintsts.d32 = 0;
  83218. + gintsts.b.usbreset = 1;
  83219. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  83220. +
  83221. + return 1;
  83222. +}
  83223. +
  83224. +/**
  83225. + * Get the device speed from the device status register and convert it
  83226. + * to USB speed constant.
  83227. + *
  83228. + * @param core_if Programming view of DWC_otg controller.
  83229. + */
  83230. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  83231. +{
  83232. + dsts_data_t dsts;
  83233. + int speed = 0;
  83234. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  83235. +
  83236. + switch (dsts.b.enumspd) {
  83237. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  83238. + speed = USB_SPEED_HIGH;
  83239. + break;
  83240. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  83241. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  83242. + speed = USB_SPEED_FULL;
  83243. + break;
  83244. +
  83245. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  83246. + speed = USB_SPEED_LOW;
  83247. + break;
  83248. + }
  83249. +
  83250. + return speed;
  83251. +}
  83252. +
  83253. +/**
  83254. + * Read the device status register and set the device speed in the
  83255. + * data structure.
  83256. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  83257. + */
  83258. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  83259. +{
  83260. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  83261. + gintsts_data_t gintsts;
  83262. + gusbcfg_data_t gusbcfg;
  83263. + dwc_otg_core_global_regs_t *global_regs =
  83264. + GET_CORE_IF(pcd)->core_global_regs;
  83265. + uint8_t utmi16b, utmi8b;
  83266. + int speed;
  83267. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  83268. +
  83269. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  83270. + utmi16b = 6; //vahrama old value was 6;
  83271. + utmi8b = 9;
  83272. + } else {
  83273. + utmi16b = 4;
  83274. + utmi8b = 8;
  83275. + }
  83276. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  83277. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  83278. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  83279. + }
  83280. +
  83281. +#ifdef DEBUG_EP0
  83282. + print_ep0_state(pcd);
  83283. +#endif
  83284. +
  83285. + if (pcd->ep0state == EP0_DISCONNECT) {
  83286. + pcd->ep0state = EP0_IDLE;
  83287. + } else if (pcd->ep0state == EP0_STALL) {
  83288. + pcd->ep0state = EP0_IDLE;
  83289. + }
  83290. +
  83291. + pcd->ep0state = EP0_IDLE;
  83292. +
  83293. + ep0->stopped = 0;
  83294. +
  83295. + speed = get_device_speed(GET_CORE_IF(pcd));
  83296. + pcd->fops->connect(pcd, speed);
  83297. +
  83298. + /* Set USB turnaround time based on device speed and PHY interface. */
  83299. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  83300. + if (speed == USB_SPEED_HIGH) {
  83301. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83302. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  83303. + /* ULPI interface */
  83304. + gusbcfg.b.usbtrdtim = 9;
  83305. + }
  83306. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83307. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  83308. + /* UTMI+ interface */
  83309. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  83310. + gusbcfg.b.usbtrdtim = utmi8b;
  83311. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  83312. + b.utmi_phy_data_width == 1) {
  83313. + gusbcfg.b.usbtrdtim = utmi16b;
  83314. + } else if (GET_CORE_IF(pcd)->
  83315. + core_params->phy_utmi_width == 8) {
  83316. + gusbcfg.b.usbtrdtim = utmi8b;
  83317. + } else {
  83318. + gusbcfg.b.usbtrdtim = utmi16b;
  83319. + }
  83320. + }
  83321. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  83322. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  83323. + /* UTMI+ OR ULPI interface */
  83324. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  83325. + /* ULPI interface */
  83326. + gusbcfg.b.usbtrdtim = 9;
  83327. + } else {
  83328. + /* UTMI+ interface */
  83329. + if (GET_CORE_IF(pcd)->
  83330. + core_params->phy_utmi_width == 16) {
  83331. + gusbcfg.b.usbtrdtim = utmi16b;
  83332. + } else {
  83333. + gusbcfg.b.usbtrdtim = utmi8b;
  83334. + }
  83335. + }
  83336. + }
  83337. + } else {
  83338. + /* Full or low speed */
  83339. + gusbcfg.b.usbtrdtim = 9;
  83340. + }
  83341. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  83342. +
  83343. + /* Clear interrupt */
  83344. + gintsts.d32 = 0;
  83345. + gintsts.b.enumdone = 1;
  83346. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  83347. + gintsts.d32);
  83348. + return 1;
  83349. +}
  83350. +
  83351. +/**
  83352. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  83353. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  83354. + * read all the data from the Rx FIFO.
  83355. + */
  83356. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  83357. +{
  83358. + gintmsk_data_t intr_mask = {.d32 = 0 };
  83359. + gintsts_data_t gintsts;
  83360. +
  83361. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  83362. + "ISOC Out Dropped");
  83363. +
  83364. + intr_mask.b.isooutdrop = 1;
  83365. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  83366. + intr_mask.d32, 0);
  83367. +
  83368. + /* Clear interrupt */
  83369. + gintsts.d32 = 0;
  83370. + gintsts.b.isooutdrop = 1;
  83371. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  83372. + gintsts.d32);
  83373. +
  83374. + return 1;
  83375. +}
  83376. +
  83377. +/**
  83378. + * This interrupt indicates the end of the portion of the micro-frame
  83379. + * for periodic transactions. If there is a periodic transaction for
  83380. + * the next frame, load the packets into the EP periodic Tx FIFO.
  83381. + */
  83382. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  83383. +{
  83384. + gintmsk_data_t intr_mask = {.d32 = 0 };
  83385. + gintsts_data_t gintsts;
  83386. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  83387. +
  83388. + intr_mask.b.eopframe = 1;
  83389. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  83390. + intr_mask.d32, 0);
  83391. +
  83392. + /* Clear interrupt */
  83393. + gintsts.d32 = 0;
  83394. + gintsts.b.eopframe = 1;
  83395. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  83396. + gintsts.d32);
  83397. +
  83398. + return 1;
  83399. +}
  83400. +
  83401. +/**
  83402. + * This interrupt indicates that EP of the packet on the top of the
  83403. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  83404. + *
  83405. + * The "Device IN Token Queue" Registers are read to determine the
  83406. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  83407. + * is flushed, so it can be reloaded in the order seen in the IN Token
  83408. + * Queue.
  83409. + */
  83410. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  83411. +{
  83412. + gintsts_data_t gintsts;
  83413. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83414. + dctl_data_t dctl;
  83415. + gintmsk_data_t intr_mask = {.d32 = 0 };
  83416. +
  83417. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  83418. + core_if->start_predict = 1;
  83419. +
  83420. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  83421. +
  83422. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  83423. + if (!gintsts.b.ginnakeff) {
  83424. + /* Disable EP Mismatch interrupt */
  83425. + intr_mask.d32 = 0;
  83426. + intr_mask.b.epmismatch = 1;
  83427. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  83428. + /* Enable the Global IN NAK Effective Interrupt */
  83429. + intr_mask.d32 = 0;
  83430. + intr_mask.b.ginnakeff = 1;
  83431. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  83432. + /* Set the global non-periodic IN NAK handshake */
  83433. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  83434. + dctl.b.sgnpinnak = 1;
  83435. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  83436. + } else {
  83437. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  83438. + }
  83439. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  83440. + * handler after Global IN NAK Effective interrupt will be asserted */
  83441. + }
  83442. + /* Clear interrupt */
  83443. + gintsts.d32 = 0;
  83444. + gintsts.b.epmismatch = 1;
  83445. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  83446. +
  83447. + return 1;
  83448. +}
  83449. +
  83450. +/**
  83451. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  83452. + * core has stopped fetching data for IN endpoints due to the unavailability of
  83453. + * TxFIFO space or Request Queue space. This interrupt is used by the
  83454. + * application for an endpoint mismatch algorithm.
  83455. + *
  83456. + * @param pcd The PCD
  83457. + */
  83458. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  83459. +{
  83460. + gintsts_data_t gintsts;
  83461. + gintmsk_data_t gintmsk_data;
  83462. + dctl_data_t dctl;
  83463. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83464. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  83465. +
  83466. + /* Clear the global non-periodic IN NAK handshake */
  83467. + dctl.d32 = 0;
  83468. + dctl.b.cgnpinnak = 1;
  83469. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  83470. +
  83471. + /* Mask GINTSTS.FETSUSP interrupt */
  83472. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  83473. + gintmsk_data.b.fetsusp = 0;
  83474. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  83475. +
  83476. + /* Clear interrupt */
  83477. + gintsts.d32 = 0;
  83478. + gintsts.b.fetsusp = 1;
  83479. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  83480. +
  83481. + return 1;
  83482. +}
  83483. +/**
  83484. + * This funcion stalls EP0.
  83485. + */
  83486. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  83487. +{
  83488. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  83489. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  83490. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  83491. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  83492. +
  83493. + ep0->dwc_ep.is_in = 1;
  83494. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  83495. + pcd->ep0.stopped = 1;
  83496. + pcd->ep0state = EP0_IDLE;
  83497. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  83498. +}
  83499. +
  83500. +/**
  83501. + * This functions delegates the setup command to the gadget driver.
  83502. + */
  83503. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  83504. + usb_device_request_t * ctrl)
  83505. +{
  83506. + int ret = 0;
  83507. + DWC_SPINUNLOCK(pcd->lock);
  83508. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  83509. + DWC_SPINLOCK(pcd->lock);
  83510. + if (ret < 0) {
  83511. + ep0_do_stall(pcd, ret);
  83512. + }
  83513. +
  83514. + /** @todo This is a g_file_storage gadget driver specific
  83515. + * workaround: a DELAYED_STATUS result from the fsg_setup
  83516. + * routine will result in the gadget queueing a EP0 IN status
  83517. + * phase for a two-stage control transfer. Exactly the same as
  83518. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  83519. + * specific request. Need a generic way to know when the gadget
  83520. + * driver will queue the status phase. Can we assume when we
  83521. + * call the gadget driver setup() function that it will always
  83522. + * queue and require the following flag? Need to look into
  83523. + * this.
  83524. + */
  83525. +
  83526. + if (ret == 256 + 999) {
  83527. + pcd->request_config = 1;
  83528. + }
  83529. +}
  83530. +
  83531. +#ifdef DWC_UTE_CFI
  83532. +/**
  83533. + * This functions delegates the CFI setup commands to the gadget driver.
  83534. + * This function will return a negative value to indicate a failure.
  83535. + */
  83536. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  83537. + struct cfi_usb_ctrlrequest *ctrl_req)
  83538. +{
  83539. + int ret = 0;
  83540. +
  83541. + if (pcd->fops && pcd->fops->cfi_setup) {
  83542. + DWC_SPINUNLOCK(pcd->lock);
  83543. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  83544. + DWC_SPINLOCK(pcd->lock);
  83545. + if (ret < 0) {
  83546. + ep0_do_stall(pcd, ret);
  83547. + return ret;
  83548. + }
  83549. + }
  83550. +
  83551. + return ret;
  83552. +}
  83553. +#endif
  83554. +
  83555. +/**
  83556. + * This function starts the Zero-Length Packet for the IN status phase
  83557. + * of a 2 stage control transfer.
  83558. + */
  83559. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  83560. +{
  83561. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  83562. + if (pcd->ep0state == EP0_STALL) {
  83563. + return;
  83564. + }
  83565. +
  83566. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  83567. +
  83568. + /* Prepare for more SETUP Packets */
  83569. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  83570. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  83571. + && (pcd->core_if->dma_desc_enable)
  83572. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  83573. + DWC_DEBUGPL(DBG_PCDV,
  83574. + "Data terminated wait next packet in out_desc_addr\n");
  83575. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  83576. + pcd->data_terminated = 1;
  83577. + }
  83578. + ep0->dwc_ep.xfer_len = 0;
  83579. + ep0->dwc_ep.xfer_count = 0;
  83580. + ep0->dwc_ep.is_in = 1;
  83581. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  83582. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  83583. +
  83584. + /* Prepare for more SETUP Packets */
  83585. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  83586. +}
  83587. +
  83588. +/**
  83589. + * This function starts the Zero-Length Packet for the OUT status phase
  83590. + * of a 2 stage control transfer.
  83591. + */
  83592. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  83593. +{
  83594. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  83595. + if (pcd->ep0state == EP0_STALL) {
  83596. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  83597. + return;
  83598. + }
  83599. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  83600. +
  83601. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  83602. + ep0->dwc_ep.xfer_len = 0;
  83603. + ep0->dwc_ep.xfer_count = 0;
  83604. + ep0->dwc_ep.is_in = 0;
  83605. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  83606. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  83607. +
  83608. + /* Prepare for more SETUP Packets */
  83609. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  83610. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  83611. + }
  83612. +}
  83613. +
  83614. +/**
  83615. + * Clear the EP halt (STALL) and if pending requests start the
  83616. + * transfer.
  83617. + */
  83618. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  83619. +{
  83620. + if (ep->dwc_ep.stall_clear_flag == 0)
  83621. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  83622. +
  83623. + /* Reactive the EP */
  83624. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  83625. + if (ep->stopped) {
  83626. + ep->stopped = 0;
  83627. + /* If there is a request in the EP queue start it */
  83628. +
  83629. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  83630. + * epmismatch not yet implemented. */
  83631. +
  83632. + /*
  83633. + * Above fixme is solved by implmenting a tasklet to call the
  83634. + * start_next_request(), outside of interrupt context at some
  83635. + * time after the current time, after a clear-halt setup packet.
  83636. + * Still need to implement ep mismatch in the future if a gadget
  83637. + * ever uses more than one endpoint at once
  83638. + */
  83639. + ep->queue_sof = 1;
  83640. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  83641. + }
  83642. + /* Start Control Status Phase */
  83643. + do_setup_in_status_phase(pcd);
  83644. +}
  83645. +
  83646. +/**
  83647. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  83648. + * is sent from the host. The Device Control register is written with
  83649. + * the Test Mode bits set to the specified Test Mode. This is done as
  83650. + * a tasklet so that the "Status" phase of the control transfer
  83651. + * completes before transmitting the TEST packets.
  83652. + *
  83653. + * @todo This has not been tested since the tasklet struct was put
  83654. + * into the PCD struct!
  83655. + *
  83656. + */
  83657. +void do_test_mode(void *data)
  83658. +{
  83659. + dctl_data_t dctl;
  83660. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  83661. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83662. + int test_mode = pcd->test_mode;
  83663. +
  83664. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  83665. +
  83666. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  83667. + switch (test_mode) {
  83668. + case 1: // TEST_J
  83669. + dctl.b.tstctl = 1;
  83670. + break;
  83671. +
  83672. + case 2: // TEST_K
  83673. + dctl.b.tstctl = 2;
  83674. + break;
  83675. +
  83676. + case 3: // TEST_SE0_NAK
  83677. + dctl.b.tstctl = 3;
  83678. + break;
  83679. +
  83680. + case 4: // TEST_PACKET
  83681. + dctl.b.tstctl = 4;
  83682. + break;
  83683. +
  83684. + case 5: // TEST_FORCE_ENABLE
  83685. + dctl.b.tstctl = 5;
  83686. + break;
  83687. + }
  83688. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  83689. +}
  83690. +
  83691. +/**
  83692. + * This function process the GET_STATUS Setup Commands.
  83693. + */
  83694. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  83695. +{
  83696. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  83697. + dwc_otg_pcd_ep_t *ep;
  83698. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  83699. + uint16_t *status = pcd->status_buf;
  83700. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83701. +
  83702. +#ifdef DEBUG_EP0
  83703. + DWC_DEBUGPL(DBG_PCD,
  83704. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  83705. + ctrl.bmRequestType, ctrl.bRequest,
  83706. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  83707. + UGETW(ctrl.wLength));
  83708. +#endif
  83709. +
  83710. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  83711. + case UT_DEVICE:
  83712. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  83713. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  83714. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  83715. + DWC_PRINTF("OTG CAP - %d, %d\n",
  83716. + core_if->core_params->otg_cap,
  83717. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  83718. + if (core_if->otg_ver == 1
  83719. + && core_if->core_params->otg_cap ==
  83720. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  83721. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  83722. + *otgsts = (core_if->otg_sts & 0x1);
  83723. + pcd->ep0_pending = 1;
  83724. + ep0->dwc_ep.start_xfer_buff =
  83725. + (uint8_t *) otgsts;
  83726. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  83727. + ep0->dwc_ep.dma_addr =
  83728. + pcd->status_buf_dma_handle;
  83729. + ep0->dwc_ep.xfer_len = 1;
  83730. + ep0->dwc_ep.xfer_count = 0;
  83731. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  83732. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  83733. + &ep0->dwc_ep);
  83734. + return;
  83735. + } else {
  83736. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83737. + return;
  83738. + }
  83739. + break;
  83740. + } else {
  83741. + *status = 0x1; /* Self powered */
  83742. + *status |= pcd->remote_wakeup_enable << 1;
  83743. + break;
  83744. + }
  83745. + case UT_INTERFACE:
  83746. + *status = 0;
  83747. + break;
  83748. +
  83749. + case UT_ENDPOINT:
  83750. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  83751. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  83752. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83753. + return;
  83754. + }
  83755. + /** @todo check for EP stall */
  83756. + *status = ep->stopped;
  83757. + break;
  83758. + }
  83759. + pcd->ep0_pending = 1;
  83760. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  83761. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  83762. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  83763. + ep0->dwc_ep.xfer_len = 2;
  83764. + ep0->dwc_ep.xfer_count = 0;
  83765. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  83766. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  83767. +}
  83768. +
  83769. +/**
  83770. + * This function process the SET_FEATURE Setup Commands.
  83771. + */
  83772. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  83773. +{
  83774. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83775. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  83776. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  83777. + dwc_otg_pcd_ep_t *ep = 0;
  83778. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  83779. + gotgctl_data_t gotgctl = {.d32 = 0 };
  83780. +
  83781. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  83782. + ctrl.bmRequestType, ctrl.bRequest,
  83783. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  83784. + UGETW(ctrl.wLength));
  83785. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  83786. +
  83787. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  83788. + case UT_DEVICE:
  83789. + switch (UGETW(ctrl.wValue)) {
  83790. + case UF_DEVICE_REMOTE_WAKEUP:
  83791. + pcd->remote_wakeup_enable = 1;
  83792. + break;
  83793. +
  83794. + case UF_TEST_MODE:
  83795. + /* Setup the Test Mode tasklet to do the Test
  83796. + * Packet generation after the SETUP Status
  83797. + * phase has completed. */
  83798. +
  83799. + /** @todo This has not been tested since the
  83800. + * tasklet struct was put into the PCD
  83801. + * struct! */
  83802. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  83803. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  83804. + break;
  83805. +
  83806. + case UF_DEVICE_B_HNP_ENABLE:
  83807. + DWC_DEBUGPL(DBG_PCDV,
  83808. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  83809. +
  83810. + /* dev may initiate HNP */
  83811. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  83812. + pcd->b_hnp_enable = 1;
  83813. + dwc_otg_pcd_update_otg(pcd, 0);
  83814. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  83815. + /**@todo Is the gotgctl.devhnpen cleared
  83816. + * by a USB Reset? */
  83817. + gotgctl.b.devhnpen = 1;
  83818. + gotgctl.b.hnpreq = 1;
  83819. + DWC_WRITE_REG32(&global_regs->gotgctl,
  83820. + gotgctl.d32);
  83821. + } else {
  83822. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83823. + return;
  83824. + }
  83825. + break;
  83826. +
  83827. + case UF_DEVICE_A_HNP_SUPPORT:
  83828. + /* RH port supports HNP */
  83829. + DWC_DEBUGPL(DBG_PCDV,
  83830. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  83831. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  83832. + pcd->a_hnp_support = 1;
  83833. + dwc_otg_pcd_update_otg(pcd, 0);
  83834. + } else {
  83835. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83836. + return;
  83837. + }
  83838. + break;
  83839. +
  83840. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  83841. + /* other RH port does */
  83842. + DWC_DEBUGPL(DBG_PCDV,
  83843. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  83844. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  83845. + pcd->a_alt_hnp_support = 1;
  83846. + dwc_otg_pcd_update_otg(pcd, 0);
  83847. + } else {
  83848. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83849. + return;
  83850. + }
  83851. + break;
  83852. +
  83853. + default:
  83854. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83855. + return;
  83856. +
  83857. + }
  83858. + do_setup_in_status_phase(pcd);
  83859. + break;
  83860. +
  83861. + case UT_INTERFACE:
  83862. + do_gadget_setup(pcd, &ctrl);
  83863. + break;
  83864. +
  83865. + case UT_ENDPOINT:
  83866. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  83867. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  83868. + if (ep == 0) {
  83869. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83870. + return;
  83871. + }
  83872. + ep->stopped = 1;
  83873. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  83874. + }
  83875. + do_setup_in_status_phase(pcd);
  83876. + break;
  83877. + }
  83878. +}
  83879. +
  83880. +/**
  83881. + * This function process the CLEAR_FEATURE Setup Commands.
  83882. + */
  83883. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  83884. +{
  83885. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  83886. + dwc_otg_pcd_ep_t *ep = 0;
  83887. +
  83888. + DWC_DEBUGPL(DBG_PCD,
  83889. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  83890. + ctrl.bmRequestType, ctrl.bRequest,
  83891. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  83892. + UGETW(ctrl.wLength));
  83893. +
  83894. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  83895. + case UT_DEVICE:
  83896. + switch (UGETW(ctrl.wValue)) {
  83897. + case UF_DEVICE_REMOTE_WAKEUP:
  83898. + pcd->remote_wakeup_enable = 0;
  83899. + break;
  83900. +
  83901. + case UF_TEST_MODE:
  83902. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  83903. + break;
  83904. +
  83905. + default:
  83906. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83907. + return;
  83908. + }
  83909. + do_setup_in_status_phase(pcd);
  83910. + break;
  83911. +
  83912. + case UT_ENDPOINT:
  83913. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  83914. + if (ep == 0) {
  83915. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  83916. + return;
  83917. + }
  83918. +
  83919. + pcd_clear_halt(pcd, ep);
  83920. +
  83921. + break;
  83922. + }
  83923. +}
  83924. +
  83925. +/**
  83926. + * This function process the SET_ADDRESS Setup Commands.
  83927. + */
  83928. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  83929. +{
  83930. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  83931. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  83932. +
  83933. + if (ctrl.bmRequestType == UT_DEVICE) {
  83934. + dcfg_data_t dcfg = {.d32 = 0 };
  83935. +
  83936. +#ifdef DEBUG_EP0
  83937. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  83938. +#endif
  83939. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  83940. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  83941. + do_setup_in_status_phase(pcd);
  83942. + }
  83943. +}
  83944. +
  83945. +/**
  83946. + * This function processes SETUP commands. In Linux, the USB Command
  83947. + * processing is done in two places - the first being the PCD and the
  83948. + * second in the Gadget Driver (for example, the File-Backed Storage
  83949. + * Gadget Driver).
  83950. + *
  83951. + * <table>
  83952. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  83953. + *
  83954. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  83955. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  83956. + * </td></tr>
  83957. + *
  83958. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  83959. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  83960. + * interface requests are ignored.</td></tr>
  83961. + *
  83962. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  83963. + * requests are processed by the PCD. Interface requests are passed
  83964. + * to the Gadget Driver.</td></tr>
  83965. + *
  83966. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  83967. + * with device address received </td></tr>
  83968. + *
  83969. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  83970. + * requested descriptor</td></tr>
  83971. + *
  83972. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  83973. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  83974. + *
  83975. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  83976. + * all EPs and enable EPs for new configuration.</td></tr>
  83977. + *
  83978. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  83979. + * the current configuration</td></tr>
  83980. + *
  83981. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  83982. + * EPs and enable EPs for new configuration.</td></tr>
  83983. + *
  83984. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  83985. + * current interface.</td></tr>
  83986. + *
  83987. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  83988. + * message.</td></tr>
  83989. + * </table>
  83990. + *
  83991. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  83992. + * processed by pcd_setup. Calling the Function Driver's setup function from
  83993. + * pcd_setup processes the gadget SETUP commands.
  83994. + */
  83995. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  83996. +{
  83997. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  83998. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  83999. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  84000. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  84001. +
  84002. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  84003. +
  84004. +#ifdef DWC_UTE_CFI
  84005. + int retval = 0;
  84006. + struct cfi_usb_ctrlrequest cfi_req;
  84007. +#endif
  84008. +
  84009. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  84010. +
  84011. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  84012. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  84013. + && (doeptsize0.b.supcnt < 2)
  84014. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  84015. + DWC_ERROR
  84016. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  84017. + }
  84018. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  84019. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  84020. + ctrl =
  84021. + (pcd->setup_pkt +
  84022. + (3 - doeptsize0.b.supcnt - 1 +
  84023. + ep0->dwc_ep.stp_rollover))->req;
  84024. + }
  84025. +#ifdef DEBUG_EP0
  84026. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  84027. + ctrl.bmRequestType, ctrl.bRequest,
  84028. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  84029. + UGETW(ctrl.wLength));
  84030. +#endif
  84031. +
  84032. + /* Clean up the request queue */
  84033. + dwc_otg_request_nuke(ep0);
  84034. + ep0->stopped = 0;
  84035. +
  84036. + if (ctrl.bmRequestType & UE_DIR_IN) {
  84037. + ep0->dwc_ep.is_in = 1;
  84038. + pcd->ep0state = EP0_IN_DATA_PHASE;
  84039. + } else {
  84040. + ep0->dwc_ep.is_in = 0;
  84041. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  84042. + }
  84043. +
  84044. + if (UGETW(ctrl.wLength) == 0) {
  84045. + ep0->dwc_ep.is_in = 1;
  84046. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  84047. + }
  84048. +
  84049. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  84050. +
  84051. +#ifdef DWC_UTE_CFI
  84052. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  84053. +
  84054. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  84055. + ctrl.bRequestType, ctrl.bRequest);
  84056. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  84057. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  84058. + retval = cfi_setup(pcd, &cfi_req);
  84059. + if (retval < 0) {
  84060. + ep0_do_stall(pcd, retval);
  84061. + pcd->ep0_pending = 0;
  84062. + return;
  84063. + }
  84064. +
  84065. + /* if need gadget setup then call it and check the retval */
  84066. + if (pcd->cfi->need_gadget_att) {
  84067. + retval =
  84068. + cfi_gadget_setup(pcd,
  84069. + &pcd->
  84070. + cfi->ctrl_req);
  84071. + if (retval < 0) {
  84072. + pcd->ep0_pending = 0;
  84073. + return;
  84074. + }
  84075. + }
  84076. +
  84077. + if (pcd->cfi->need_status_in_complete) {
  84078. + do_setup_in_status_phase(pcd);
  84079. + }
  84080. + return;
  84081. + }
  84082. + }
  84083. +#endif
  84084. +
  84085. + /* handle non-standard (class/vendor) requests in the gadget driver */
  84086. + do_gadget_setup(pcd, &ctrl);
  84087. + return;
  84088. + }
  84089. +
  84090. + /** @todo NGS: Handle bad setup packet? */
  84091. +
  84092. +///////////////////////////////////////////
  84093. +//// --- Standard Request handling --- ////
  84094. +
  84095. + switch (ctrl.bRequest) {
  84096. + case UR_GET_STATUS:
  84097. + do_get_status(pcd);
  84098. + break;
  84099. +
  84100. + case UR_CLEAR_FEATURE:
  84101. + do_clear_feature(pcd);
  84102. + break;
  84103. +
  84104. + case UR_SET_FEATURE:
  84105. + do_set_feature(pcd);
  84106. + break;
  84107. +
  84108. + case UR_SET_ADDRESS:
  84109. + do_set_address(pcd);
  84110. + break;
  84111. +
  84112. + case UR_SET_INTERFACE:
  84113. + case UR_SET_CONFIG:
  84114. +// _pcd->request_config = 1; /* Configuration changed */
  84115. + do_gadget_setup(pcd, &ctrl);
  84116. + break;
  84117. +
  84118. + case UR_SYNCH_FRAME:
  84119. + do_gadget_setup(pcd, &ctrl);
  84120. + break;
  84121. +
  84122. + default:
  84123. + /* Call the Gadget Driver's setup functions */
  84124. + do_gadget_setup(pcd, &ctrl);
  84125. + break;
  84126. + }
  84127. +}
  84128. +
  84129. +/**
  84130. + * This function completes the ep0 control transfer.
  84131. + */
  84132. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  84133. +{
  84134. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  84135. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  84136. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  84137. + dev_if->in_ep_regs[ep->dwc_ep.num];
  84138. +#ifdef DEBUG_EP0
  84139. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  84140. + dev_if->out_ep_regs[ep->dwc_ep.num];
  84141. +#endif
  84142. + deptsiz0_data_t deptsiz;
  84143. + dev_dma_desc_sts_t desc_sts;
  84144. + dwc_otg_pcd_request_t *req;
  84145. + int is_last = 0;
  84146. + dwc_otg_pcd_t *pcd = ep->pcd;
  84147. +
  84148. +#ifdef DWC_UTE_CFI
  84149. + struct cfi_usb_ctrlrequest *ctrlreq;
  84150. + int retval = -DWC_E_NOT_SUPPORTED;
  84151. +#endif
  84152. +
  84153. + desc_sts.b.bytes = 0;
  84154. +
  84155. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  84156. + if (ep->dwc_ep.is_in) {
  84157. +#ifdef DEBUG_EP0
  84158. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  84159. +#endif
  84160. + do_setup_out_status_phase(pcd);
  84161. + } else {
  84162. +#ifdef DEBUG_EP0
  84163. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  84164. +#endif
  84165. +
  84166. +#ifdef DWC_UTE_CFI
  84167. + ctrlreq = &pcd->cfi->ctrl_req;
  84168. +
  84169. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  84170. + if (ctrlreq->bRequest > 0xB0
  84171. + && ctrlreq->bRequest < 0xBF) {
  84172. +
  84173. + /* Return if the PCD failed to handle the request */
  84174. + if ((retval =
  84175. + pcd->cfi->ops.
  84176. + ctrl_write_complete(pcd->cfi,
  84177. + pcd)) < 0) {
  84178. + CFI_INFO
  84179. + ("ERROR setting a new value in the PCD(%d)\n",
  84180. + retval);
  84181. + ep0_do_stall(pcd, retval);
  84182. + pcd->ep0_pending = 0;
  84183. + return 0;
  84184. + }
  84185. +
  84186. + /* If the gadget needs to be notified on the request */
  84187. + if (pcd->cfi->need_gadget_att == 1) {
  84188. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  84189. + retval =
  84190. + cfi_gadget_setup(pcd,
  84191. + &pcd->cfi->
  84192. + ctrl_req);
  84193. +
  84194. + /* Return from the function if the gadget failed to process
  84195. + * the request properly - this should never happen !!!
  84196. + */
  84197. + if (retval < 0) {
  84198. + CFI_INFO
  84199. + ("ERROR setting a new value in the gadget(%d)\n",
  84200. + retval);
  84201. + pcd->ep0_pending = 0;
  84202. + return 0;
  84203. + }
  84204. + }
  84205. +
  84206. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  84207. + retval);
  84208. + /* If we hit here then the PCD and the gadget has properly
  84209. + * handled the request - so send the ZLP IN to the host.
  84210. + */
  84211. + /* @todo: MAS - decide whether we need to start the setup
  84212. + * stage based on the need_setup value of the cfi object
  84213. + */
  84214. + do_setup_in_status_phase(pcd);
  84215. + pcd->ep0_pending = 0;
  84216. + return 1;
  84217. + }
  84218. + }
  84219. +#endif
  84220. +
  84221. + do_setup_in_status_phase(pcd);
  84222. + }
  84223. + pcd->ep0_pending = 0;
  84224. + return 1;
  84225. + }
  84226. +
  84227. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  84228. + return 0;
  84229. + }
  84230. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  84231. +
  84232. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  84233. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  84234. + is_last = 1;
  84235. + } else if (ep->dwc_ep.is_in) {
  84236. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  84237. + if (core_if->dma_desc_enable != 0)
  84238. + desc_sts = dev_if->in_desc_addr->status;
  84239. +#ifdef DEBUG_EP0
  84240. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  84241. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  84242. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  84243. +#endif
  84244. +
  84245. + if (((core_if->dma_desc_enable == 0)
  84246. + && (deptsiz.b.xfersize == 0))
  84247. + || ((core_if->dma_desc_enable != 0)
  84248. + && (desc_sts.b.bytes == 0))) {
  84249. + req->actual = ep->dwc_ep.xfer_count;
  84250. + /* Is a Zero Len Packet needed? */
  84251. + if (req->sent_zlp) {
  84252. +#ifdef DEBUG_EP0
  84253. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  84254. +#endif
  84255. + req->sent_zlp = 0;
  84256. + }
  84257. + do_setup_out_status_phase(pcd);
  84258. + }
  84259. + } else {
  84260. + /* ep0-OUT */
  84261. +#ifdef DEBUG_EP0
  84262. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  84263. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  84264. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  84265. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  84266. +#endif
  84267. + req->actual = ep->dwc_ep.xfer_count;
  84268. +
  84269. + /* Is a Zero Len Packet needed? */
  84270. + if (req->sent_zlp) {
  84271. +#ifdef DEBUG_EP0
  84272. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  84273. +#endif
  84274. + req->sent_zlp = 0;
  84275. + }
  84276. + /* For older cores do setup in status phase in Slave/BDMA modes,
  84277. + * starting from 3.00 do that only in slave, and for DMA modes
  84278. + * just re-enable ep 0 OUT here*/
  84279. + if (core_if->dma_enable == 0
  84280. + || (core_if->dma_desc_enable == 0
  84281. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  84282. + do_setup_in_status_phase(pcd);
  84283. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  84284. + DWC_DEBUGPL(DBG_PCDV,
  84285. + "Enable out ep before in status phase\n");
  84286. + ep0_out_start(core_if, pcd);
  84287. + }
  84288. + }
  84289. +
  84290. + /* Complete the request */
  84291. + if (is_last) {
  84292. + dwc_otg_request_done(ep, req, 0);
  84293. + ep->dwc_ep.start_xfer_buff = 0;
  84294. + ep->dwc_ep.xfer_buff = 0;
  84295. + ep->dwc_ep.xfer_len = 0;
  84296. + return 1;
  84297. + }
  84298. + return 0;
  84299. +}
  84300. +
  84301. +#ifdef DWC_UTE_CFI
  84302. +/**
  84303. + * This function calculates traverses all the CFI DMA descriptors and
  84304. + * and accumulates the bytes that are left to be transfered.
  84305. + *
  84306. + * @return The total bytes left to transfered, or a negative value as failure
  84307. + */
  84308. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  84309. +{
  84310. + int32_t ret = 0;
  84311. + int i;
  84312. + struct dwc_otg_dma_desc *ddesc = NULL;
  84313. + struct cfi_ep *cfiep;
  84314. +
  84315. + /* See if the pcd_ep has its respective cfi_ep mapped */
  84316. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  84317. + if (!cfiep) {
  84318. + CFI_INFO("%s: Failed to find ep\n", __func__);
  84319. + return -1;
  84320. + }
  84321. +
  84322. + ddesc = ep->dwc_ep.descs;
  84323. +
  84324. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  84325. +
  84326. +#if defined(PRINT_CFI_DMA_DESCS)
  84327. + print_desc(ddesc, ep->ep.name, i);
  84328. +#endif
  84329. + ret += ddesc->status.b.bytes;
  84330. + ddesc++;
  84331. + }
  84332. +
  84333. + if (ret)
  84334. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  84335. + ret);
  84336. +
  84337. + return ret;
  84338. +}
  84339. +#endif
  84340. +
  84341. +/**
  84342. + * This function completes the request for the EP. If there are
  84343. + * additional requests for the EP in the queue they will be started.
  84344. + */
  84345. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  84346. +{
  84347. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  84348. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  84349. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  84350. + dev_if->in_ep_regs[ep->dwc_ep.num];
  84351. + deptsiz_data_t deptsiz;
  84352. + dev_dma_desc_sts_t desc_sts;
  84353. + dwc_otg_pcd_request_t *req = 0;
  84354. + dwc_otg_dev_dma_desc_t *dma_desc;
  84355. + uint32_t byte_count = 0;
  84356. + int is_last = 0;
  84357. + int i;
  84358. +
  84359. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  84360. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  84361. +
  84362. + /* Get any pending requests */
  84363. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  84364. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  84365. + if (!req) {
  84366. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  84367. + return;
  84368. + }
  84369. + } else {
  84370. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  84371. + return;
  84372. + }
  84373. +
  84374. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  84375. +
  84376. + if (ep->dwc_ep.is_in) {
  84377. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  84378. +
  84379. + if (core_if->dma_enable) {
  84380. + if (core_if->dma_desc_enable == 0) {
  84381. + if (deptsiz.b.xfersize == 0
  84382. + && deptsiz.b.pktcnt == 0) {
  84383. + byte_count =
  84384. + ep->dwc_ep.xfer_len -
  84385. + ep->dwc_ep.xfer_count;
  84386. +
  84387. + ep->dwc_ep.xfer_buff += byte_count;
  84388. + ep->dwc_ep.dma_addr += byte_count;
  84389. + ep->dwc_ep.xfer_count += byte_count;
  84390. +
  84391. + DWC_DEBUGPL(DBG_PCDV,
  84392. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  84393. + ep->dwc_ep.num,
  84394. + (ep->dwc_ep.
  84395. + is_in ? "IN" : "OUT"),
  84396. + ep->dwc_ep.xfer_len,
  84397. + deptsiz.b.xfersize,
  84398. + deptsiz.b.pktcnt);
  84399. +
  84400. + if (ep->dwc_ep.xfer_len <
  84401. + ep->dwc_ep.total_len) {
  84402. + dwc_otg_ep_start_transfer
  84403. + (core_if, &ep->dwc_ep);
  84404. + } else if (ep->dwc_ep.sent_zlp) {
  84405. + /*
  84406. + * This fragment of code should initiate 0
  84407. + * length transfer in case if it is queued
  84408. + * a transfer with size divisible to EPs max
  84409. + * packet size and with usb_request zero field
  84410. + * is set, which means that after data is transfered,
  84411. + * it is also should be transfered
  84412. + * a 0 length packet at the end. For Slave and
  84413. + * Buffer DMA modes in this case SW has
  84414. + * to initiate 2 transfers one with transfer size,
  84415. + * and the second with 0 size. For Descriptor
  84416. + * DMA mode SW is able to initiate a transfer,
  84417. + * which will handle all the packets including
  84418. + * the last 0 length.
  84419. + */
  84420. + ep->dwc_ep.sent_zlp = 0;
  84421. + dwc_otg_ep_start_zl_transfer
  84422. + (core_if, &ep->dwc_ep);
  84423. + } else {
  84424. + is_last = 1;
  84425. + }
  84426. + } else {
  84427. + if (ep->dwc_ep.type ==
  84428. + DWC_OTG_EP_TYPE_ISOC) {
  84429. + req->actual = 0;
  84430. + dwc_otg_request_done(ep, req, 0);
  84431. +
  84432. + ep->dwc_ep.start_xfer_buff = 0;
  84433. + ep->dwc_ep.xfer_buff = 0;
  84434. + ep->dwc_ep.xfer_len = 0;
  84435. +
  84436. + /* If there is a request in the queue start it. */
  84437. + start_next_request(ep);
  84438. + } else
  84439. + DWC_WARN
  84440. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  84441. + ep->dwc_ep.num,
  84442. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  84443. + deptsiz.b.xfersize,
  84444. + deptsiz.b.pktcnt);
  84445. + }
  84446. + } else {
  84447. + dma_desc = ep->dwc_ep.desc_addr;
  84448. + byte_count = 0;
  84449. + ep->dwc_ep.sent_zlp = 0;
  84450. +
  84451. +#ifdef DWC_UTE_CFI
  84452. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  84453. + ep->dwc_ep.buff_mode);
  84454. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  84455. + int residue;
  84456. +
  84457. + residue = cfi_calc_desc_residue(ep);
  84458. + if (residue < 0)
  84459. + return;
  84460. +
  84461. + byte_count = residue;
  84462. + } else {
  84463. +#endif
  84464. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  84465. + ++i) {
  84466. + desc_sts = dma_desc->status;
  84467. + byte_count += desc_sts.b.bytes;
  84468. + dma_desc++;
  84469. + }
  84470. +#ifdef DWC_UTE_CFI
  84471. + }
  84472. +#endif
  84473. + if (byte_count == 0) {
  84474. + ep->dwc_ep.xfer_count =
  84475. + ep->dwc_ep.total_len;
  84476. + is_last = 1;
  84477. + } else {
  84478. + DWC_WARN("Incomplete transfer\n");
  84479. + }
  84480. + }
  84481. + } else {
  84482. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  84483. + DWC_DEBUGPL(DBG_PCDV,
  84484. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  84485. + ep->dwc_ep.num,
  84486. + ep->dwc_ep.is_in ? "IN" : "OUT",
  84487. + ep->dwc_ep.xfer_len,
  84488. + deptsiz.b.xfersize,
  84489. + deptsiz.b.pktcnt);
  84490. +
  84491. + /* Check if the whole transfer was completed,
  84492. + * if no, setup transfer for next portion of data
  84493. + */
  84494. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  84495. + dwc_otg_ep_start_transfer(core_if,
  84496. + &ep->dwc_ep);
  84497. + } else if (ep->dwc_ep.sent_zlp) {
  84498. + /*
  84499. + * This fragment of code should initiate 0
  84500. + * length trasfer in case if it is queued
  84501. + * a trasfer with size divisible to EPs max
  84502. + * packet size and with usb_request zero field
  84503. + * is set, which means that after data is transfered,
  84504. + * it is also should be transfered
  84505. + * a 0 length packet at the end. For Slave and
  84506. + * Buffer DMA modes in this case SW has
  84507. + * to initiate 2 transfers one with transfer size,
  84508. + * and the second with 0 size. For Desriptor
  84509. + * DMA mode SW is able to initiate a transfer,
  84510. + * which will handle all the packets including
  84511. + * the last 0 legth.
  84512. + */
  84513. + ep->dwc_ep.sent_zlp = 0;
  84514. + dwc_otg_ep_start_zl_transfer(core_if,
  84515. + &ep->dwc_ep);
  84516. + } else {
  84517. + is_last = 1;
  84518. + }
  84519. + } else {
  84520. + DWC_WARN
  84521. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  84522. + ep->dwc_ep.num,
  84523. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  84524. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  84525. + }
  84526. + }
  84527. + } else {
  84528. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  84529. + dev_if->out_ep_regs[ep->dwc_ep.num];
  84530. + desc_sts.d32 = 0;
  84531. + if (core_if->dma_enable) {
  84532. + if (core_if->dma_desc_enable) {
  84533. + dma_desc = ep->dwc_ep.desc_addr;
  84534. + byte_count = 0;
  84535. + ep->dwc_ep.sent_zlp = 0;
  84536. +
  84537. +#ifdef DWC_UTE_CFI
  84538. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  84539. + ep->dwc_ep.buff_mode);
  84540. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  84541. + int residue;
  84542. + residue = cfi_calc_desc_residue(ep);
  84543. + if (residue < 0)
  84544. + return;
  84545. + byte_count = residue;
  84546. + } else {
  84547. +#endif
  84548. +
  84549. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  84550. + ++i) {
  84551. + desc_sts = dma_desc->status;
  84552. + byte_count += desc_sts.b.bytes;
  84553. + dma_desc++;
  84554. + }
  84555. +
  84556. +#ifdef DWC_UTE_CFI
  84557. + }
  84558. +#endif
  84559. + /* Checking for interrupt Out transfers with not
  84560. + * dword aligned mps sizes
  84561. + */
  84562. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  84563. + (ep->dwc_ep.maxpacket%4)) {
  84564. + ep->dwc_ep.xfer_count =
  84565. + ep->dwc_ep.total_len - byte_count;
  84566. + if ((ep->dwc_ep.xfer_len %
  84567. + ep->dwc_ep.maxpacket)
  84568. + && (ep->dwc_ep.xfer_len /
  84569. + ep->dwc_ep.maxpacket <
  84570. + MAX_DMA_DESC_CNT))
  84571. + ep->dwc_ep.xfer_len -=
  84572. + (ep->dwc_ep.desc_cnt -
  84573. + 1) * ep->dwc_ep.maxpacket +
  84574. + ep->dwc_ep.xfer_len %
  84575. + ep->dwc_ep.maxpacket;
  84576. + else
  84577. + ep->dwc_ep.xfer_len -=
  84578. + ep->dwc_ep.desc_cnt *
  84579. + ep->dwc_ep.maxpacket;
  84580. + if (ep->dwc_ep.xfer_len > 0) {
  84581. + dwc_otg_ep_start_transfer
  84582. + (core_if, &ep->dwc_ep);
  84583. + } else {
  84584. + is_last = 1;
  84585. + }
  84586. + } else {
  84587. + ep->dwc_ep.xfer_count =
  84588. + ep->dwc_ep.total_len - byte_count +
  84589. + ((4 -
  84590. + (ep->dwc_ep.
  84591. + total_len & 0x3)) & 0x3);
  84592. + is_last = 1;
  84593. + }
  84594. + } else {
  84595. + deptsiz.d32 = 0;
  84596. + deptsiz.d32 =
  84597. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  84598. +
  84599. + byte_count = (ep->dwc_ep.xfer_len -
  84600. + ep->dwc_ep.xfer_count -
  84601. + deptsiz.b.xfersize);
  84602. + ep->dwc_ep.xfer_buff += byte_count;
  84603. + ep->dwc_ep.dma_addr += byte_count;
  84604. + ep->dwc_ep.xfer_count += byte_count;
  84605. +
  84606. + /* Check if the whole transfer was completed,
  84607. + * if no, setup transfer for next portion of data
  84608. + */
  84609. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  84610. + dwc_otg_ep_start_transfer(core_if,
  84611. + &ep->dwc_ep);
  84612. + } else if (ep->dwc_ep.sent_zlp) {
  84613. + /*
  84614. + * This fragment of code should initiate 0
  84615. + * length trasfer in case if it is queued
  84616. + * a trasfer with size divisible to EPs max
  84617. + * packet size and with usb_request zero field
  84618. + * is set, which means that after data is transfered,
  84619. + * it is also should be transfered
  84620. + * a 0 length packet at the end. For Slave and
  84621. + * Buffer DMA modes in this case SW has
  84622. + * to initiate 2 transfers one with transfer size,
  84623. + * and the second with 0 size. For Desriptor
  84624. + * DMA mode SW is able to initiate a transfer,
  84625. + * which will handle all the packets including
  84626. + * the last 0 legth.
  84627. + */
  84628. + ep->dwc_ep.sent_zlp = 0;
  84629. + dwc_otg_ep_start_zl_transfer(core_if,
  84630. + &ep->dwc_ep);
  84631. + } else {
  84632. + is_last = 1;
  84633. + }
  84634. + }
  84635. + } else {
  84636. + /* Check if the whole transfer was completed,
  84637. + * if no, setup transfer for next portion of data
  84638. + */
  84639. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  84640. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  84641. + } else if (ep->dwc_ep.sent_zlp) {
  84642. + /*
  84643. + * This fragment of code should initiate 0
  84644. + * length transfer in case if it is queued
  84645. + * a transfer with size divisible to EPs max
  84646. + * packet size and with usb_request zero field
  84647. + * is set, which means that after data is transfered,
  84648. + * it is also should be transfered
  84649. + * a 0 length packet at the end. For Slave and
  84650. + * Buffer DMA modes in this case SW has
  84651. + * to initiate 2 transfers one with transfer size,
  84652. + * and the second with 0 size. For Descriptor
  84653. + * DMA mode SW is able to initiate a transfer,
  84654. + * which will handle all the packets including
  84655. + * the last 0 length.
  84656. + */
  84657. + ep->dwc_ep.sent_zlp = 0;
  84658. + dwc_otg_ep_start_zl_transfer(core_if,
  84659. + &ep->dwc_ep);
  84660. + } else {
  84661. + is_last = 1;
  84662. + }
  84663. + }
  84664. +
  84665. + DWC_DEBUGPL(DBG_PCDV,
  84666. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  84667. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  84668. + ep->dwc_ep.is_in ? "IN" : "OUT",
  84669. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  84670. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  84671. + }
  84672. +
  84673. + /* Complete the request */
  84674. + if (is_last) {
  84675. +#ifdef DWC_UTE_CFI
  84676. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  84677. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  84678. + } else {
  84679. +#endif
  84680. + req->actual = ep->dwc_ep.xfer_count;
  84681. +#ifdef DWC_UTE_CFI
  84682. + }
  84683. +#endif
  84684. + if (req->dw_align_buf) {
  84685. + if (!ep->dwc_ep.is_in) {
  84686. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  84687. + }
  84688. + DWC_DMA_FREE(req->length, req->dw_align_buf,
  84689. + req->dw_align_buf_dma);
  84690. + }
  84691. +
  84692. + dwc_otg_request_done(ep, req, 0);
  84693. +
  84694. + ep->dwc_ep.start_xfer_buff = 0;
  84695. + ep->dwc_ep.xfer_buff = 0;
  84696. + ep->dwc_ep.xfer_len = 0;
  84697. +
  84698. + /* If there is a request in the queue start it. */
  84699. + start_next_request(ep);
  84700. + }
  84701. +}
  84702. +
  84703. +#ifdef DWC_EN_ISOC
  84704. +
  84705. +/**
  84706. + * This function BNA interrupt for Isochronous EPs
  84707. + *
  84708. + */
  84709. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  84710. +{
  84711. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  84712. + volatile uint32_t *addr;
  84713. + depctl_data_t depctl = {.d32 = 0 };
  84714. + dwc_otg_pcd_t *pcd = ep->pcd;
  84715. + dwc_otg_dev_dma_desc_t *dma_desc;
  84716. + int i;
  84717. +
  84718. + dma_desc =
  84719. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  84720. +
  84721. + if (dwc_ep->is_in) {
  84722. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  84723. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  84724. + sts.d32 = dma_desc->status.d32;
  84725. + sts.b_iso_in.bs = BS_HOST_READY;
  84726. + dma_desc->status.d32 = sts.d32;
  84727. + }
  84728. + } else {
  84729. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  84730. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  84731. + sts.d32 = dma_desc->status.d32;
  84732. + sts.b_iso_out.bs = BS_HOST_READY;
  84733. + dma_desc->status.d32 = sts.d32;
  84734. + }
  84735. + }
  84736. +
  84737. + if (dwc_ep->is_in == 0) {
  84738. + addr =
  84739. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  84740. + num]->doepctl;
  84741. + } else {
  84742. + addr =
  84743. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  84744. + }
  84745. + depctl.b.epena = 1;
  84746. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  84747. +}
  84748. +
  84749. +/**
  84750. + * This function sets latest iso packet information(non-PTI mode)
  84751. + *
  84752. + * @param core_if Programming view of DWC_otg controller.
  84753. + * @param ep The EP to start the transfer on.
  84754. + *
  84755. + */
  84756. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  84757. +{
  84758. + deptsiz_data_t deptsiz = {.d32 = 0 };
  84759. + dma_addr_t dma_addr;
  84760. + uint32_t offset;
  84761. +
  84762. + if (ep->proc_buf_num)
  84763. + dma_addr = ep->dma_addr1;
  84764. + else
  84765. + dma_addr = ep->dma_addr0;
  84766. +
  84767. + if (ep->is_in) {
  84768. + deptsiz.d32 =
  84769. + DWC_READ_REG32(&core_if->dev_if->
  84770. + in_ep_regs[ep->num]->dieptsiz);
  84771. + offset = ep->data_per_frame;
  84772. + } else {
  84773. + deptsiz.d32 =
  84774. + DWC_READ_REG32(&core_if->dev_if->
  84775. + out_ep_regs[ep->num]->doeptsiz);
  84776. + offset =
  84777. + ep->data_per_frame +
  84778. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  84779. + }
  84780. +
  84781. + if (!deptsiz.b.xfersize) {
  84782. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  84783. + ep->pkt_info[ep->cur_pkt].offset =
  84784. + ep->cur_pkt_dma_addr - dma_addr;
  84785. + ep->pkt_info[ep->cur_pkt].status = 0;
  84786. + } else {
  84787. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  84788. + ep->pkt_info[ep->cur_pkt].offset =
  84789. + ep->cur_pkt_dma_addr - dma_addr;
  84790. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  84791. + }
  84792. + ep->cur_pkt_addr += offset;
  84793. + ep->cur_pkt_dma_addr += offset;
  84794. + ep->cur_pkt++;
  84795. +}
  84796. +
  84797. +/**
  84798. + * This function sets latest iso packet information(DDMA mode)
  84799. + *
  84800. + * @param core_if Programming view of DWC_otg controller.
  84801. + * @param dwc_ep The EP to start the transfer on.
  84802. + *
  84803. + */
  84804. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  84805. + dwc_ep_t * dwc_ep)
  84806. +{
  84807. + dwc_otg_dev_dma_desc_t *dma_desc;
  84808. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  84809. + iso_pkt_info_t *iso_packet;
  84810. + uint32_t data_per_desc;
  84811. + uint32_t offset;
  84812. + int i, j;
  84813. +
  84814. + iso_packet = dwc_ep->pkt_info;
  84815. +
  84816. + /** Reinit closed DMA Descriptors*/
  84817. + /** ISO OUT EP */
  84818. + if (dwc_ep->is_in == 0) {
  84819. + dma_desc =
  84820. + dwc_ep->iso_desc_addr +
  84821. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  84822. + offset = 0;
  84823. +
  84824. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  84825. + i += dwc_ep->pkt_per_frm) {
  84826. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  84827. + data_per_desc =
  84828. + ((j + 1) * dwc_ep->maxpacket >
  84829. + dwc_ep->
  84830. + data_per_frame) ? dwc_ep->data_per_frame -
  84831. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  84832. + data_per_desc +=
  84833. + (data_per_desc % 4) ? (4 -
  84834. + data_per_desc %
  84835. + 4) : 0;
  84836. +
  84837. + sts.d32 = dma_desc->status.d32;
  84838. +
  84839. + /* Write status in iso_packet_decsriptor */
  84840. + iso_packet->status =
  84841. + sts.b_iso_out.rxsts +
  84842. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  84843. + if (iso_packet->status) {
  84844. + iso_packet->status = -DWC_E_NO_DATA;
  84845. + }
  84846. +
  84847. + /* Received data length */
  84848. + if (!sts.b_iso_out.rxbytes) {
  84849. + iso_packet->length =
  84850. + data_per_desc -
  84851. + sts.b_iso_out.rxbytes;
  84852. + } else {
  84853. + iso_packet->length =
  84854. + data_per_desc -
  84855. + sts.b_iso_out.rxbytes + (4 -
  84856. + dwc_ep->data_per_frame
  84857. + % 4);
  84858. + }
  84859. +
  84860. + iso_packet->offset = offset;
  84861. +
  84862. + offset += data_per_desc;
  84863. + dma_desc++;
  84864. + iso_packet++;
  84865. + }
  84866. + }
  84867. +
  84868. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  84869. + data_per_desc =
  84870. + ((j + 1) * dwc_ep->maxpacket >
  84871. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  84872. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  84873. + data_per_desc +=
  84874. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  84875. +
  84876. + sts.d32 = dma_desc->status.d32;
  84877. +
  84878. + /* Write status in iso_packet_decsriptor */
  84879. + iso_packet->status =
  84880. + sts.b_iso_out.rxsts +
  84881. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  84882. + if (iso_packet->status) {
  84883. + iso_packet->status = -DWC_E_NO_DATA;
  84884. + }
  84885. +
  84886. + /* Received data length */
  84887. + iso_packet->length =
  84888. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  84889. +
  84890. + iso_packet->offset = offset;
  84891. +
  84892. + offset += data_per_desc;
  84893. + iso_packet++;
  84894. + dma_desc++;
  84895. + }
  84896. +
  84897. + sts.d32 = dma_desc->status.d32;
  84898. +
  84899. + /* Write status in iso_packet_decsriptor */
  84900. + iso_packet->status =
  84901. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  84902. + if (iso_packet->status) {
  84903. + iso_packet->status = -DWC_E_NO_DATA;
  84904. + }
  84905. + /* Received data length */
  84906. + if (!sts.b_iso_out.rxbytes) {
  84907. + iso_packet->length =
  84908. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  84909. + } else {
  84910. + iso_packet->length =
  84911. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  84912. + (4 - dwc_ep->data_per_frame % 4);
  84913. + }
  84914. +
  84915. + iso_packet->offset = offset;
  84916. + } else {
  84917. +/** ISO IN EP */
  84918. +
  84919. + dma_desc =
  84920. + dwc_ep->iso_desc_addr +
  84921. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  84922. +
  84923. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  84924. + sts.d32 = dma_desc->status.d32;
  84925. +
  84926. + /* Write status in iso packet descriptor */
  84927. + iso_packet->status =
  84928. + sts.b_iso_in.txsts +
  84929. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  84930. + if (iso_packet->status != 0) {
  84931. + iso_packet->status = -DWC_E_NO_DATA;
  84932. +
  84933. + }
  84934. + /* Bytes has been transfered */
  84935. + iso_packet->length =
  84936. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  84937. +
  84938. + dma_desc++;
  84939. + iso_packet++;
  84940. + }
  84941. +
  84942. + sts.d32 = dma_desc->status.d32;
  84943. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  84944. + sts.d32 = dma_desc->status.d32;
  84945. + }
  84946. +
  84947. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  84948. + iso_packet->status =
  84949. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  84950. + if (iso_packet->status != 0) {
  84951. + iso_packet->status = -DWC_E_NO_DATA;
  84952. + }
  84953. +
  84954. + /* Bytes has been transfered */
  84955. + iso_packet->length =
  84956. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  84957. + }
  84958. +}
  84959. +
  84960. +/**
  84961. + * This function reinitialize DMA Descriptors for Isochronous transfer
  84962. + *
  84963. + * @param core_if Programming view of DWC_otg controller.
  84964. + * @param dwc_ep The EP to start the transfer on.
  84965. + *
  84966. + */
  84967. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  84968. +{
  84969. + int i, j;
  84970. + dwc_otg_dev_dma_desc_t *dma_desc;
  84971. + dma_addr_t dma_ad;
  84972. + volatile uint32_t *addr;
  84973. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  84974. + uint32_t data_per_desc;
  84975. +
  84976. + if (dwc_ep->is_in == 0) {
  84977. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  84978. + } else {
  84979. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  84980. + }
  84981. +
  84982. + if (dwc_ep->proc_buf_num == 0) {
  84983. + /** Buffer 0 descriptors setup */
  84984. + dma_ad = dwc_ep->dma_addr0;
  84985. + } else {
  84986. + /** Buffer 1 descriptors setup */
  84987. + dma_ad = dwc_ep->dma_addr1;
  84988. + }
  84989. +
  84990. + /** Reinit closed DMA Descriptors*/
  84991. + /** ISO OUT EP */
  84992. + if (dwc_ep->is_in == 0) {
  84993. + dma_desc =
  84994. + dwc_ep->iso_desc_addr +
  84995. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  84996. +
  84997. + sts.b_iso_out.bs = BS_HOST_READY;
  84998. + sts.b_iso_out.rxsts = 0;
  84999. + sts.b_iso_out.l = 0;
  85000. + sts.b_iso_out.sp = 0;
  85001. + sts.b_iso_out.ioc = 0;
  85002. + sts.b_iso_out.pid = 0;
  85003. + sts.b_iso_out.framenum = 0;
  85004. +
  85005. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  85006. + i += dwc_ep->pkt_per_frm) {
  85007. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  85008. + data_per_desc =
  85009. + ((j + 1) * dwc_ep->maxpacket >
  85010. + dwc_ep->
  85011. + data_per_frame) ? dwc_ep->data_per_frame -
  85012. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85013. + data_per_desc +=
  85014. + (data_per_desc % 4) ? (4 -
  85015. + data_per_desc %
  85016. + 4) : 0;
  85017. + sts.b_iso_out.rxbytes = data_per_desc;
  85018. + dma_desc->buf = dma_ad;
  85019. + dma_desc->status.d32 = sts.d32;
  85020. +
  85021. + dma_ad += data_per_desc;
  85022. + dma_desc++;
  85023. + }
  85024. + }
  85025. +
  85026. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  85027. +
  85028. + data_per_desc =
  85029. + ((j + 1) * dwc_ep->maxpacket >
  85030. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  85031. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85032. + data_per_desc +=
  85033. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  85034. + sts.b_iso_out.rxbytes = data_per_desc;
  85035. +
  85036. + dma_desc->buf = dma_ad;
  85037. + dma_desc->status.d32 = sts.d32;
  85038. +
  85039. + dma_desc++;
  85040. + dma_ad += data_per_desc;
  85041. + }
  85042. +
  85043. + sts.b_iso_out.ioc = 1;
  85044. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  85045. +
  85046. + data_per_desc =
  85047. + ((j + 1) * dwc_ep->maxpacket >
  85048. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  85049. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  85050. + data_per_desc +=
  85051. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  85052. + sts.b_iso_out.rxbytes = data_per_desc;
  85053. +
  85054. + dma_desc->buf = dma_ad;
  85055. + dma_desc->status.d32 = sts.d32;
  85056. + } else {
  85057. +/** ISO IN EP */
  85058. +
  85059. + dma_desc =
  85060. + dwc_ep->iso_desc_addr +
  85061. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  85062. +
  85063. + sts.b_iso_in.bs = BS_HOST_READY;
  85064. + sts.b_iso_in.txsts = 0;
  85065. + sts.b_iso_in.sp = 0;
  85066. + sts.b_iso_in.ioc = 0;
  85067. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  85068. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  85069. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  85070. + sts.b_iso_in.l = 0;
  85071. +
  85072. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  85073. + dma_desc->buf = dma_ad;
  85074. + dma_desc->status.d32 = sts.d32;
  85075. +
  85076. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  85077. + dma_ad += dwc_ep->data_per_frame;
  85078. + dma_desc++;
  85079. + }
  85080. +
  85081. + sts.b_iso_in.ioc = 1;
  85082. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  85083. +
  85084. + dma_desc->buf = dma_ad;
  85085. + dma_desc->status.d32 = sts.d32;
  85086. +
  85087. + dwc_ep->next_frame =
  85088. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  85089. + }
  85090. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85091. +}
  85092. +
  85093. +/**
  85094. + * This function is to handle Iso EP transfer complete interrupt
  85095. + * in case Iso out packet was dropped
  85096. + *
  85097. + * @param core_if Programming view of DWC_otg controller.
  85098. + * @param dwc_ep The EP for wihich transfer complete was asserted
  85099. + *
  85100. + */
  85101. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  85102. + dwc_ep_t * dwc_ep)
  85103. +{
  85104. + uint32_t dma_addr;
  85105. + uint32_t drp_pkt;
  85106. + uint32_t drp_pkt_cnt;
  85107. + deptsiz_data_t deptsiz = {.d32 = 0 };
  85108. + depctl_data_t depctl = {.d32 = 0 };
  85109. + int i;
  85110. +
  85111. + deptsiz.d32 =
  85112. + DWC_READ_REG32(&core_if->dev_if->
  85113. + out_ep_regs[dwc_ep->num]->doeptsiz);
  85114. +
  85115. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  85116. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  85117. +
  85118. + /* Setting dropped packets status */
  85119. + for (i = 0; i < drp_pkt_cnt; ++i) {
  85120. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  85121. + drp_pkt++;
  85122. + deptsiz.b.pktcnt--;
  85123. + }
  85124. +
  85125. + if (deptsiz.b.pktcnt > 0) {
  85126. + deptsiz.b.xfersize =
  85127. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  85128. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  85129. + } else {
  85130. + deptsiz.b.xfersize = 0;
  85131. + deptsiz.b.pktcnt = 0;
  85132. + }
  85133. +
  85134. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  85135. + deptsiz.d32);
  85136. +
  85137. + if (deptsiz.b.pktcnt > 0) {
  85138. + if (dwc_ep->proc_buf_num) {
  85139. + dma_addr =
  85140. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  85141. + deptsiz.b.xfersize;
  85142. + } else {
  85143. + dma_addr =
  85144. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  85145. + deptsiz.b.xfersize;;
  85146. + }
  85147. +
  85148. + DWC_WRITE_REG32(&core_if->dev_if->
  85149. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  85150. +
  85151. + /** Re-enable endpoint, clear nak */
  85152. + depctl.d32 = 0;
  85153. + depctl.b.epena = 1;
  85154. + depctl.b.cnak = 1;
  85155. +
  85156. + DWC_MODIFY_REG32(&core_if->dev_if->
  85157. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  85158. + depctl.d32);
  85159. + return 0;
  85160. + } else {
  85161. + return 1;
  85162. + }
  85163. +}
  85164. +
  85165. +/**
  85166. + * This function sets iso packets information(PTI mode)
  85167. + *
  85168. + * @param core_if Programming view of DWC_otg controller.
  85169. + * @param ep The EP to start the transfer on.
  85170. + *
  85171. + */
  85172. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  85173. +{
  85174. + int i, j;
  85175. + dma_addr_t dma_ad;
  85176. + iso_pkt_info_t *packet_info = ep->pkt_info;
  85177. + uint32_t offset;
  85178. + uint32_t frame_data;
  85179. + deptsiz_data_t deptsiz;
  85180. +
  85181. + if (ep->proc_buf_num == 0) {
  85182. + /** Buffer 0 descriptors setup */
  85183. + dma_ad = ep->dma_addr0;
  85184. + } else {
  85185. + /** Buffer 1 descriptors setup */
  85186. + dma_ad = ep->dma_addr1;
  85187. + }
  85188. +
  85189. + if (ep->is_in) {
  85190. + deptsiz.d32 =
  85191. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  85192. + dieptsiz);
  85193. + } else {
  85194. + deptsiz.d32 =
  85195. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  85196. + doeptsiz);
  85197. + }
  85198. +
  85199. + if (!deptsiz.b.xfersize) {
  85200. + offset = 0;
  85201. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  85202. + frame_data = ep->data_per_frame;
  85203. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  85204. +
  85205. + /* Packet status - is not set as initially
  85206. + * it is set to 0 and if packet was sent
  85207. + successfully, status field will remain 0*/
  85208. +
  85209. + /* Bytes has been transfered */
  85210. + packet_info->length =
  85211. + (ep->maxpacket <
  85212. + frame_data) ? ep->maxpacket : frame_data;
  85213. +
  85214. + /* Received packet offset */
  85215. + packet_info->offset = offset;
  85216. + offset += packet_info->length;
  85217. + frame_data -= packet_info->length;
  85218. +
  85219. + packet_info++;
  85220. + }
  85221. + }
  85222. + return 1;
  85223. + } else {
  85224. + /* This is a workaround for in case of Transfer Complete with
  85225. + * PktDrpSts interrupts merging - in this case Transfer complete
  85226. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  85227. + * set and with DOEPTSIZ register non zero. Investigations showed,
  85228. + * that this happens when Out packet is dropped, but because of
  85229. + * interrupts merging during first interrupt handling PktDrpSts
  85230. + * bit is cleared and for next merged interrupts it is not reset.
  85231. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  85232. + */
  85233. + if (ep->is_in) {
  85234. + return 1;
  85235. + } else {
  85236. + return handle_iso_out_pkt_dropped(core_if, ep);
  85237. + }
  85238. + }
  85239. +}
  85240. +
  85241. +/**
  85242. + * This function is to handle Iso EP transfer complete interrupt
  85243. + *
  85244. + * @param pcd The PCD
  85245. + * @param ep The EP for which transfer complete was asserted
  85246. + *
  85247. + */
  85248. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  85249. +{
  85250. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  85251. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  85252. + uint8_t is_last = 0;
  85253. +
  85254. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  85255. + DWC_WARN("Next frame is not set!\n");
  85256. + return;
  85257. + }
  85258. +
  85259. + if (core_if->dma_enable) {
  85260. + if (core_if->dma_desc_enable) {
  85261. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  85262. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  85263. + is_last = 1;
  85264. + } else {
  85265. + if (core_if->pti_enh_enable) {
  85266. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  85267. + dwc_ep->proc_buf_num =
  85268. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85269. + dwc_otg_iso_ep_start_buf_transfer
  85270. + (core_if, dwc_ep);
  85271. + is_last = 1;
  85272. + }
  85273. + } else {
  85274. + set_current_pkt_info(core_if, dwc_ep);
  85275. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  85276. + is_last = 1;
  85277. + dwc_ep->cur_pkt = 0;
  85278. + dwc_ep->proc_buf_num =
  85279. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85280. + if (dwc_ep->proc_buf_num) {
  85281. + dwc_ep->cur_pkt_addr =
  85282. + dwc_ep->xfer_buff1;
  85283. + dwc_ep->cur_pkt_dma_addr =
  85284. + dwc_ep->dma_addr1;
  85285. + } else {
  85286. + dwc_ep->cur_pkt_addr =
  85287. + dwc_ep->xfer_buff0;
  85288. + dwc_ep->cur_pkt_dma_addr =
  85289. + dwc_ep->dma_addr0;
  85290. + }
  85291. +
  85292. + }
  85293. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  85294. + dwc_ep);
  85295. + }
  85296. + }
  85297. + } else {
  85298. + set_current_pkt_info(core_if, dwc_ep);
  85299. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  85300. + is_last = 1;
  85301. + dwc_ep->cur_pkt = 0;
  85302. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  85303. + if (dwc_ep->proc_buf_num) {
  85304. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  85305. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  85306. + } else {
  85307. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  85308. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  85309. + }
  85310. +
  85311. + }
  85312. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  85313. + }
  85314. + if (is_last)
  85315. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  85316. +}
  85317. +#endif /* DWC_EN_ISOC */
  85318. +
  85319. +/**
  85320. + * This function handle BNA interrupt for Non Isochronous EPs
  85321. + *
  85322. + */
  85323. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  85324. +{
  85325. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  85326. + volatile uint32_t *addr;
  85327. + depctl_data_t depctl = {.d32 = 0 };
  85328. + dwc_otg_pcd_t *pcd = ep->pcd;
  85329. + dwc_otg_dev_dma_desc_t *dma_desc;
  85330. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  85331. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  85332. + int i, start;
  85333. +
  85334. + if (!dwc_ep->desc_cnt)
  85335. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  85336. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  85337. +
  85338. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  85339. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  85340. + uint32_t doepdma;
  85341. + dwc_otg_dev_out_ep_regs_t *out_regs =
  85342. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  85343. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  85344. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  85345. + dma_desc = &(dwc_ep->desc_addr[start]);
  85346. + } else {
  85347. + start = 0;
  85348. + dma_desc = dwc_ep->desc_addr;
  85349. + }
  85350. +
  85351. +
  85352. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  85353. + sts.d32 = dma_desc->status.d32;
  85354. + sts.b.bs = BS_HOST_READY;
  85355. + dma_desc->status.d32 = sts.d32;
  85356. + }
  85357. +
  85358. + if (dwc_ep->is_in == 0) {
  85359. + addr =
  85360. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  85361. + doepctl;
  85362. + } else {
  85363. + addr =
  85364. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  85365. + }
  85366. + depctl.b.epena = 1;
  85367. + depctl.b.cnak = 1;
  85368. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  85369. +}
  85370. +
  85371. +/**
  85372. + * This function handles EP0 Control transfers.
  85373. + *
  85374. + * The state of the control transfers are tracked in
  85375. + * <code>ep0state</code>.
  85376. + */
  85377. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  85378. +{
  85379. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  85380. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  85381. + dev_dma_desc_sts_t desc_sts;
  85382. + deptsiz0_data_t deptsiz;
  85383. + uint32_t byte_count;
  85384. +
  85385. +#ifdef DEBUG_EP0
  85386. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  85387. + print_ep0_state(pcd);
  85388. +#endif
  85389. +
  85390. +// DWC_PRINTF("HANDLE EP0\n");
  85391. +
  85392. + switch (pcd->ep0state) {
  85393. + case EP0_DISCONNECT:
  85394. + break;
  85395. +
  85396. + case EP0_IDLE:
  85397. + pcd->request_config = 0;
  85398. +
  85399. + pcd_setup(pcd);
  85400. + break;
  85401. +
  85402. + case EP0_IN_DATA_PHASE:
  85403. +#ifdef DEBUG_EP0
  85404. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  85405. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  85406. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  85407. +#endif
  85408. +
  85409. + if (core_if->dma_enable != 0) {
  85410. + /*
  85411. + * For EP0 we can only program 1 packet at a time so we
  85412. + * need to do the make calculations after each complete.
  85413. + * Call write_packet to make the calculations, as in
  85414. + * slave mode, and use those values to determine if we
  85415. + * can complete.
  85416. + */
  85417. + if (core_if->dma_desc_enable == 0) {
  85418. + deptsiz.d32 =
  85419. + DWC_READ_REG32(&core_if->
  85420. + dev_if->in_ep_regs[0]->
  85421. + dieptsiz);
  85422. + byte_count =
  85423. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  85424. + } else {
  85425. + desc_sts =
  85426. + core_if->dev_if->in_desc_addr->status;
  85427. + byte_count =
  85428. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  85429. + }
  85430. + ep0->dwc_ep.xfer_count += byte_count;
  85431. + ep0->dwc_ep.xfer_buff += byte_count;
  85432. + ep0->dwc_ep.dma_addr += byte_count;
  85433. + }
  85434. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  85435. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  85436. + &ep0->dwc_ep);
  85437. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  85438. + } else if (ep0->dwc_ep.sent_zlp) {
  85439. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  85440. + &ep0->dwc_ep);
  85441. + ep0->dwc_ep.sent_zlp = 0;
  85442. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  85443. + } else {
  85444. + ep0_complete_request(ep0);
  85445. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  85446. + }
  85447. + break;
  85448. + case EP0_OUT_DATA_PHASE:
  85449. +#ifdef DEBUG_EP0
  85450. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  85451. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  85452. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  85453. +#endif
  85454. + if (core_if->dma_enable != 0) {
  85455. + if (core_if->dma_desc_enable == 0) {
  85456. + deptsiz.d32 =
  85457. + DWC_READ_REG32(&core_if->
  85458. + dev_if->out_ep_regs[0]->
  85459. + doeptsiz);
  85460. + byte_count =
  85461. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  85462. + } else {
  85463. + desc_sts =
  85464. + core_if->dev_if->out_desc_addr->status;
  85465. + byte_count =
  85466. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  85467. + }
  85468. + ep0->dwc_ep.xfer_count += byte_count;
  85469. + ep0->dwc_ep.xfer_buff += byte_count;
  85470. + ep0->dwc_ep.dma_addr += byte_count;
  85471. + }
  85472. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  85473. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  85474. + &ep0->dwc_ep);
  85475. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  85476. + } else if (ep0->dwc_ep.sent_zlp) {
  85477. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  85478. + &ep0->dwc_ep);
  85479. + ep0->dwc_ep.sent_zlp = 0;
  85480. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  85481. + } else {
  85482. + ep0_complete_request(ep0);
  85483. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  85484. + }
  85485. + break;
  85486. +
  85487. + case EP0_IN_STATUS_PHASE:
  85488. + case EP0_OUT_STATUS_PHASE:
  85489. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  85490. + ep0_complete_request(ep0);
  85491. + pcd->ep0state = EP0_IDLE;
  85492. + ep0->stopped = 1;
  85493. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  85494. +
  85495. + /* Prepare for more SETUP Packets */
  85496. + if (core_if->dma_enable) {
  85497. + ep0_out_start(core_if, pcd);
  85498. + }
  85499. + break;
  85500. +
  85501. + case EP0_STALL:
  85502. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  85503. + break;
  85504. + }
  85505. +#ifdef DEBUG_EP0
  85506. + print_ep0_state(pcd);
  85507. +#endif
  85508. +}
  85509. +
  85510. +/**
  85511. + * Restart transfer
  85512. + */
  85513. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  85514. +{
  85515. + dwc_otg_core_if_t *core_if;
  85516. + dwc_otg_dev_if_t *dev_if;
  85517. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  85518. + dwc_otg_pcd_ep_t *ep;
  85519. +
  85520. + ep = get_in_ep(pcd, epnum);
  85521. +
  85522. +#ifdef DWC_EN_ISOC
  85523. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  85524. + return;
  85525. + }
  85526. +#endif /* DWC_EN_ISOC */
  85527. +
  85528. + core_if = GET_CORE_IF(pcd);
  85529. + dev_if = core_if->dev_if;
  85530. +
  85531. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  85532. +
  85533. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  85534. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  85535. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  85536. + /*
  85537. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  85538. + */
  85539. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  85540. + ep->dwc_ep.start_xfer_buff != 0) {
  85541. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  85542. + ep->dwc_ep.xfer_count = 0;
  85543. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  85544. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  85545. + } else {
  85546. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  85547. + /* convert packet size to dwords. */
  85548. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  85549. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  85550. + }
  85551. + ep->stopped = 0;
  85552. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  85553. + "xfer_len=%0x stopped=%d\n",
  85554. + ep->dwc_ep.xfer_buff,
  85555. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  85556. + ep->stopped);
  85557. + if (epnum == 0) {
  85558. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  85559. + } else {
  85560. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  85561. + }
  85562. + }
  85563. +}
  85564. +
  85565. +/*
  85566. + * This function create new nextep sequnce based on Learn Queue.
  85567. + *
  85568. + * @param core_if Programming view of DWC_otg controller
  85569. + */
  85570. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  85571. +{
  85572. + dwc_otg_device_global_regs_t *dev_global_regs =
  85573. + core_if->dev_if->dev_global_regs;
  85574. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  85575. + /* Number of Token Queue Registers */
  85576. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  85577. + dtknq1_data_t dtknqr1;
  85578. + uint32_t in_tkn_epnums[4];
  85579. + uint8_t seqnum[MAX_EPS_CHANNELS];
  85580. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  85581. + grstctl_t resetctl = {.d32 = 0 };
  85582. + uint8_t temp;
  85583. + int ndx = 0;
  85584. + int start = 0;
  85585. + int end = 0;
  85586. + int sort_done = 0;
  85587. + int i = 0;
  85588. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  85589. +
  85590. +
  85591. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  85592. +
  85593. + /* Read the DTKNQ Registers */
  85594. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  85595. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  85596. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  85597. + in_tkn_epnums[i]);
  85598. + if (addr == &dev_global_regs->dvbusdis) {
  85599. + addr = &dev_global_regs->dtknqr3_dthrctl;
  85600. + } else {
  85601. + ++addr;
  85602. + }
  85603. +
  85604. + }
  85605. +
  85606. + /* Copy the DTKNQR1 data to the bit field. */
  85607. + dtknqr1.d32 = in_tkn_epnums[0];
  85608. + if (dtknqr1.b.wrap_bit) {
  85609. + ndx = dtknqr1.b.intknwptr;
  85610. + end = ndx -1;
  85611. + if (end < 0)
  85612. + end = TOKEN_Q_DEPTH -1;
  85613. + } else {
  85614. + ndx = 0;
  85615. + end = dtknqr1.b.intknwptr -1;
  85616. + if (end < 0)
  85617. + end = 0;
  85618. + }
  85619. + start = ndx;
  85620. +
  85621. + /* Fill seqnum[] by initial values: EP number + 31 */
  85622. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  85623. + seqnum[i] = i +31;
  85624. + }
  85625. +
  85626. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  85627. + for (i=0; i < 6; i++)
  85628. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  85629. +
  85630. + if (TOKEN_Q_DEPTH > 6) {
  85631. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  85632. + for (i=6; i < 14; i++)
  85633. + intkn_seq[i] =
  85634. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  85635. + }
  85636. +
  85637. + if (TOKEN_Q_DEPTH > 14) {
  85638. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  85639. + for (i=14; i < 22; i++)
  85640. + intkn_seq[i] =
  85641. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  85642. + }
  85643. +
  85644. + if (TOKEN_Q_DEPTH > 22) {
  85645. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  85646. + for (i=22; i < 30; i++)
  85647. + intkn_seq[i] =
  85648. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  85649. + }
  85650. +
  85651. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  85652. + start, end);
  85653. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  85654. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  85655. +
  85656. + /* Update seqnum based on intkn_seq[] */
  85657. + i = 0;
  85658. + do {
  85659. + seqnum[intkn_seq[ndx]] = i;
  85660. + ndx++;
  85661. + i++;
  85662. + if (ndx == TOKEN_Q_DEPTH)
  85663. + ndx = 0;
  85664. + } while ( i < TOKEN_Q_DEPTH );
  85665. +
  85666. + /* Mark non active EP's in seqnum[] by 0xff */
  85667. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  85668. + if (core_if->nextep_seq[i] == 0xff )
  85669. + seqnum[i] = 0xff;
  85670. + }
  85671. +
  85672. + /* Sort seqnum[] */
  85673. + sort_done = 0;
  85674. + while (!sort_done) {
  85675. + sort_done = 1;
  85676. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  85677. + if (seqnum[i] > seqnum[i+1]) {
  85678. + temp = seqnum[i];
  85679. + seqnum[i] = seqnum[i+1];
  85680. + seqnum[i+1] = temp;
  85681. + sort_done = 0;
  85682. + }
  85683. + }
  85684. + }
  85685. +
  85686. + ndx = start + seqnum[0];
  85687. + if (ndx >= TOKEN_Q_DEPTH)
  85688. + ndx = ndx % TOKEN_Q_DEPTH;
  85689. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  85690. +
  85691. + /* Update seqnum[] by EP numbers */
  85692. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  85693. + ndx = start + i;
  85694. + if (seqnum[i] < 31) {
  85695. + ndx = start + seqnum[i];
  85696. + if (ndx >= TOKEN_Q_DEPTH)
  85697. + ndx = ndx % TOKEN_Q_DEPTH;
  85698. + seqnum[i] = intkn_seq[ndx];
  85699. + } else {
  85700. + if (seqnum[i] < 0xff) {
  85701. + seqnum[i] = seqnum[i] - 31;
  85702. + } else {
  85703. + break;
  85704. + }
  85705. + }
  85706. + }
  85707. +
  85708. + /* Update nextep_seq[] based on seqnum[] */
  85709. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  85710. + if (seqnum[i] != 0xff) {
  85711. + if (seqnum[i+1] != 0xff) {
  85712. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  85713. + } else {
  85714. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  85715. + break;
  85716. + }
  85717. + } else {
  85718. + break;
  85719. + }
  85720. + }
  85721. +
  85722. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  85723. + __func__, core_if->first_in_nextep_seq);
  85724. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  85725. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  85726. + }
  85727. +
  85728. + /* Flush the Learning Queue */
  85729. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  85730. + resetctl.b.intknqflsh = 1;
  85731. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  85732. +
  85733. +
  85734. +}
  85735. +
  85736. +/**
  85737. + * handle the IN EP disable interrupt.
  85738. + */
  85739. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  85740. + const uint32_t epnum)
  85741. +{
  85742. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  85743. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  85744. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  85745. + dctl_data_t dctl = {.d32 = 0 };
  85746. + dwc_otg_pcd_ep_t *ep;
  85747. + dwc_ep_t *dwc_ep;
  85748. + gintmsk_data_t gintmsk_data;
  85749. + depctl_data_t depctl;
  85750. + uint32_t diepdma;
  85751. + uint32_t remain_to_transfer = 0;
  85752. + uint8_t i;
  85753. + uint32_t xfer_size;
  85754. +
  85755. + ep = get_in_ep(pcd, epnum);
  85756. + dwc_ep = &ep->dwc_ep;
  85757. +
  85758. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  85759. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  85760. + complete_ep(ep);
  85761. + return;
  85762. + }
  85763. +
  85764. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  85765. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  85766. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  85767. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  85768. +
  85769. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  85770. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  85771. +
  85772. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  85773. + if (ep->stopped) {
  85774. + if (core_if->en_multiple_tx_fifo)
  85775. + /* Flush the Tx FIFO */
  85776. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  85777. + /* Clear the Global IN NP NAK */
  85778. + dctl.d32 = 0;
  85779. + dctl.b.cgnpinnak = 1;
  85780. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  85781. + /* Restart the transaction */
  85782. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  85783. + restart_transfer(pcd, epnum);
  85784. + }
  85785. + } else {
  85786. + /* Restart the transaction */
  85787. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  85788. + restart_transfer(pcd, epnum);
  85789. + }
  85790. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  85791. + }
  85792. + return;
  85793. + }
  85794. +
  85795. + if (core_if->start_predict > 2) { // NP IN EP
  85796. + core_if->start_predict--;
  85797. + return;
  85798. + }
  85799. +
  85800. + core_if->start_predict--;
  85801. +
  85802. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  85803. +
  85804. + predict_nextep_seq(core_if);
  85805. +
  85806. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  85807. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  85808. + depctl.d32 =
  85809. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  85810. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  85811. + depctl.b.nextep = core_if->nextep_seq[i];
  85812. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  85813. + }
  85814. + }
  85815. + /* Flush Shared NP TxFIFO */
  85816. + dwc_otg_flush_tx_fifo(core_if, 0);
  85817. + /* Rewind buffers */
  85818. + if (!core_if->dma_desc_enable) {
  85819. + i = core_if->first_in_nextep_seq;
  85820. + do {
  85821. + ep = get_in_ep(pcd, i);
  85822. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  85823. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  85824. + if (xfer_size > ep->dwc_ep.maxxfer)
  85825. + xfer_size = ep->dwc_ep.maxxfer;
  85826. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  85827. + if (dieptsiz.b.pktcnt != 0) {
  85828. + if (xfer_size == 0) {
  85829. + remain_to_transfer = 0;
  85830. + } else {
  85831. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  85832. + remain_to_transfer =
  85833. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  85834. + } else {
  85835. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  85836. + + (xfer_size % ep->dwc_ep.maxpacket);
  85837. + }
  85838. + }
  85839. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  85840. + dieptsiz.b.xfersize = remain_to_transfer;
  85841. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  85842. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  85843. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  85844. + }
  85845. + i = core_if->nextep_seq[i];
  85846. + } while (i != core_if->first_in_nextep_seq);
  85847. + } else { // dma_desc_enable
  85848. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  85849. + }
  85850. +
  85851. + /* Restart transfers in predicted sequences */
  85852. + i = core_if->first_in_nextep_seq;
  85853. + do {
  85854. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  85855. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  85856. + if (dieptsiz.b.pktcnt != 0) {
  85857. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  85858. + depctl.b.epena = 1;
  85859. + depctl.b.cnak = 1;
  85860. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  85861. + }
  85862. + i = core_if->nextep_seq[i];
  85863. + } while (i != core_if->first_in_nextep_seq);
  85864. +
  85865. + /* Clear the global non-periodic IN NAK handshake */
  85866. + dctl.d32 = 0;
  85867. + dctl.b.cgnpinnak = 1;
  85868. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  85869. +
  85870. + /* Unmask EP Mismatch interrupt */
  85871. + gintmsk_data.d32 = 0;
  85872. + gintmsk_data.b.epmismatch = 1;
  85873. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  85874. +
  85875. + core_if->start_predict = 0;
  85876. +
  85877. + }
  85878. +}
  85879. +
  85880. +/**
  85881. + * Handler for the IN EP timeout handshake interrupt.
  85882. + */
  85883. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  85884. + const uint32_t epnum)
  85885. +{
  85886. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  85887. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  85888. +
  85889. +#ifdef DEBUG
  85890. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  85891. + uint32_t num = 0;
  85892. +#endif
  85893. + dctl_data_t dctl = {.d32 = 0 };
  85894. + dwc_otg_pcd_ep_t *ep;
  85895. +
  85896. + gintmsk_data_t intr_mask = {.d32 = 0 };
  85897. +
  85898. + ep = get_in_ep(pcd, epnum);
  85899. +
  85900. + /* Disable the NP Tx Fifo Empty Interrrupt */
  85901. + if (!core_if->dma_enable) {
  85902. + intr_mask.b.nptxfempty = 1;
  85903. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  85904. + intr_mask.d32, 0);
  85905. + }
  85906. + /** @todo NGS Check EP type.
  85907. + * Implement for Periodic EPs */
  85908. + /*
  85909. + * Non-periodic EP
  85910. + */
  85911. + /* Enable the Global IN NAK Effective Interrupt */
  85912. + intr_mask.b.ginnakeff = 1;
  85913. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  85914. +
  85915. + /* Set Global IN NAK */
  85916. + dctl.b.sgnpinnak = 1;
  85917. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  85918. +
  85919. + ep->stopped = 1;
  85920. +
  85921. +#ifdef DEBUG
  85922. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  85923. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  85924. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  85925. +#endif
  85926. +
  85927. +#ifdef DISABLE_PERIODIC_EP
  85928. + /*
  85929. + * Set the NAK bit for this EP to
  85930. + * start the disable process.
  85931. + */
  85932. + diepctl.d32 = 0;
  85933. + diepctl.b.snak = 1;
  85934. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  85935. + diepctl.d32);
  85936. + ep->disabling = 1;
  85937. + ep->stopped = 1;
  85938. +#endif
  85939. +}
  85940. +
  85941. +/**
  85942. + * Handler for the IN EP NAK interrupt.
  85943. + */
  85944. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  85945. + const uint32_t epnum)
  85946. +{
  85947. + /** @todo implement ISR */
  85948. + dwc_otg_core_if_t *core_if;
  85949. + diepmsk_data_t intr_mask = {.d32 = 0 };
  85950. +
  85951. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  85952. + core_if = GET_CORE_IF(pcd);
  85953. + intr_mask.b.nak = 1;
  85954. +
  85955. + if (core_if->multiproc_int_enable) {
  85956. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  85957. + diepeachintmsk[epnum], intr_mask.d32, 0);
  85958. + } else {
  85959. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  85960. + intr_mask.d32, 0);
  85961. + }
  85962. +
  85963. + return 1;
  85964. +}
  85965. +
  85966. +/**
  85967. + * Handler for the OUT EP Babble interrupt.
  85968. + */
  85969. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  85970. + const uint32_t epnum)
  85971. +{
  85972. + /** @todo implement ISR */
  85973. + dwc_otg_core_if_t *core_if;
  85974. + doepmsk_data_t intr_mask = {.d32 = 0 };
  85975. +
  85976. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  85977. + "OUT EP Babble");
  85978. + core_if = GET_CORE_IF(pcd);
  85979. + intr_mask.b.babble = 1;
  85980. +
  85981. + if (core_if->multiproc_int_enable) {
  85982. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  85983. + doepeachintmsk[epnum], intr_mask.d32, 0);
  85984. + } else {
  85985. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  85986. + intr_mask.d32, 0);
  85987. + }
  85988. +
  85989. + return 1;
  85990. +}
  85991. +
  85992. +/**
  85993. + * Handler for the OUT EP NAK interrupt.
  85994. + */
  85995. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  85996. + const uint32_t epnum)
  85997. +{
  85998. + /** @todo implement ISR */
  85999. + dwc_otg_core_if_t *core_if;
  86000. + doepmsk_data_t intr_mask = {.d32 = 0 };
  86001. +
  86002. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  86003. + core_if = GET_CORE_IF(pcd);
  86004. + intr_mask.b.nak = 1;
  86005. +
  86006. + if (core_if->multiproc_int_enable) {
  86007. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86008. + doepeachintmsk[epnum], intr_mask.d32, 0);
  86009. + } else {
  86010. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86011. + intr_mask.d32, 0);
  86012. + }
  86013. +
  86014. + return 1;
  86015. +}
  86016. +
  86017. +/**
  86018. + * Handler for the OUT EP NYET interrupt.
  86019. + */
  86020. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  86021. + const uint32_t epnum)
  86022. +{
  86023. + /** @todo implement ISR */
  86024. + dwc_otg_core_if_t *core_if;
  86025. + doepmsk_data_t intr_mask = {.d32 = 0 };
  86026. +
  86027. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  86028. + core_if = GET_CORE_IF(pcd);
  86029. + intr_mask.b.nyet = 1;
  86030. +
  86031. + if (core_if->multiproc_int_enable) {
  86032. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  86033. + doepeachintmsk[epnum], intr_mask.d32, 0);
  86034. + } else {
  86035. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86036. + intr_mask.d32, 0);
  86037. + }
  86038. +
  86039. + return 1;
  86040. +}
  86041. +
  86042. +/**
  86043. + * This interrupt indicates that an IN EP has a pending Interrupt.
  86044. + * The sequence for handling the IN EP interrupt is shown below:
  86045. + * -# Read the Device All Endpoint Interrupt register
  86046. + * -# Repeat the following for each IN EP interrupt bit set (from
  86047. + * LSB to MSB).
  86048. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  86049. + * -# If "Transfer Complete" call the request complete function
  86050. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  86051. + * -# If "AHB Error Interrupt" log error
  86052. + * -# If "Time-out Handshake" log error
  86053. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  86054. + * FIFO.
  86055. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  86056. + * Mismatch Interrupt)
  86057. + */
  86058. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  86059. +{
  86060. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  86061. +do { \
  86062. + diepint_data_t diepint = {.d32=0}; \
  86063. + diepint.b.__intr = 1; \
  86064. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  86065. + diepint.d32); \
  86066. +} while (0)
  86067. +
  86068. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86069. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  86070. + diepint_data_t diepint = {.d32 = 0 };
  86071. + depctl_data_t depctl = {.d32 = 0 };
  86072. + uint32_t ep_intr;
  86073. + uint32_t epnum = 0;
  86074. + dwc_otg_pcd_ep_t *ep;
  86075. + dwc_ep_t *dwc_ep;
  86076. + gintmsk_data_t intr_mask = {.d32 = 0 };
  86077. +
  86078. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  86079. +
  86080. + /* Read in the device interrupt bits */
  86081. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  86082. +
  86083. + /* Service the Device IN interrupts for each endpoint */
  86084. + while (ep_intr) {
  86085. + if (ep_intr & 0x1) {
  86086. + uint32_t empty_msk;
  86087. + /* Get EP pointer */
  86088. + ep = get_in_ep(pcd, epnum);
  86089. + dwc_ep = &ep->dwc_ep;
  86090. +
  86091. + depctl.d32 =
  86092. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  86093. + empty_msk =
  86094. + DWC_READ_REG32(&dev_if->
  86095. + dev_global_regs->dtknqr4_fifoemptymsk);
  86096. +
  86097. + DWC_DEBUGPL(DBG_PCDV,
  86098. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  86099. + epnum, empty_msk, depctl.d32);
  86100. +
  86101. + DWC_DEBUGPL(DBG_PCD,
  86102. + "EP%d-%s: type=%d, mps=%d\n",
  86103. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  86104. + dwc_ep->type, dwc_ep->maxpacket);
  86105. +
  86106. + diepint.d32 =
  86107. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  86108. +
  86109. + DWC_DEBUGPL(DBG_PCDV,
  86110. + "EP %d Interrupt Register - 0x%x\n", epnum,
  86111. + diepint.d32);
  86112. + /* Transfer complete */
  86113. + if (diepint.b.xfercompl) {
  86114. + /* Disable the NP Tx FIFO Empty
  86115. + * Interrupt */
  86116. + if (core_if->en_multiple_tx_fifo == 0) {
  86117. + intr_mask.b.nptxfempty = 1;
  86118. + DWC_MODIFY_REG32
  86119. + (&core_if->core_global_regs->gintmsk,
  86120. + intr_mask.d32, 0);
  86121. + } else {
  86122. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  86123. + uint32_t fifoemptymsk =
  86124. + 0x1 << dwc_ep->num;
  86125. + DWC_MODIFY_REG32(&core_if->
  86126. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  86127. + fifoemptymsk, 0);
  86128. + }
  86129. + /* Clear the bit in DIEPINTn for this interrupt */
  86130. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  86131. +
  86132. + /* Complete the transfer */
  86133. + if (epnum == 0) {
  86134. + handle_ep0(pcd);
  86135. + }
  86136. +#ifdef DWC_EN_ISOC
  86137. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86138. + if (!ep->stopped)
  86139. + complete_iso_ep(pcd, ep);
  86140. + }
  86141. +#endif /* DWC_EN_ISOC */
  86142. +#ifdef DWC_UTE_PER_IO
  86143. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86144. + if (!ep->stopped)
  86145. + complete_xiso_ep(ep);
  86146. + }
  86147. +#endif /* DWC_UTE_PER_IO */
  86148. + else {
  86149. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  86150. + dwc_ep->bInterval > 1) {
  86151. + dwc_ep->frame_num += dwc_ep->bInterval;
  86152. + if (dwc_ep->frame_num > 0x3FFF)
  86153. + {
  86154. + dwc_ep->frm_overrun = 1;
  86155. + dwc_ep->frame_num &= 0x3FFF;
  86156. + } else
  86157. + dwc_ep->frm_overrun = 0;
  86158. + }
  86159. + complete_ep(ep);
  86160. + if(diepint.b.nak)
  86161. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  86162. + }
  86163. + }
  86164. + /* Endpoint disable */
  86165. + if (diepint.b.epdisabled) {
  86166. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  86167. + epnum);
  86168. + handle_in_ep_disable_intr(pcd, epnum);
  86169. +
  86170. + /* Clear the bit in DIEPINTn for this interrupt */
  86171. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  86172. + }
  86173. + /* AHB Error */
  86174. + if (diepint.b.ahberr) {
  86175. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  86176. + /* Clear the bit in DIEPINTn for this interrupt */
  86177. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  86178. + }
  86179. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  86180. + if (diepint.b.timeout) {
  86181. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  86182. + handle_in_ep_timeout_intr(pcd, epnum);
  86183. +
  86184. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  86185. + }
  86186. + /** IN Token received with TxF Empty */
  86187. + if (diepint.b.intktxfemp) {
  86188. + DWC_DEBUGPL(DBG_ANY,
  86189. + "EP%d IN TKN TxFifo Empty\n",
  86190. + epnum);
  86191. + if (!ep->stopped && epnum != 0) {
  86192. +
  86193. + diepmsk_data_t diepmsk = {.d32 = 0 };
  86194. + diepmsk.b.intktxfemp = 1;
  86195. +
  86196. + if (core_if->multiproc_int_enable) {
  86197. + DWC_MODIFY_REG32
  86198. + (&dev_if->dev_global_regs->diepeachintmsk
  86199. + [epnum], diepmsk.d32, 0);
  86200. + } else {
  86201. + DWC_MODIFY_REG32
  86202. + (&dev_if->dev_global_regs->diepmsk,
  86203. + diepmsk.d32, 0);
  86204. + }
  86205. + } else if (core_if->dma_desc_enable
  86206. + && epnum == 0
  86207. + && pcd->ep0state ==
  86208. + EP0_OUT_STATUS_PHASE) {
  86209. + // EP0 IN set STALL
  86210. + depctl.d32 =
  86211. + DWC_READ_REG32(&dev_if->in_ep_regs
  86212. + [epnum]->diepctl);
  86213. +
  86214. + /* set the disable and stall bits */
  86215. + if (depctl.b.epena) {
  86216. + depctl.b.epdis = 1;
  86217. + }
  86218. + depctl.b.stall = 1;
  86219. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  86220. + [epnum]->diepctl,
  86221. + depctl.d32);
  86222. + }
  86223. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  86224. + }
  86225. + /** IN Token Received with EP mismatch */
  86226. + if (diepint.b.intknepmis) {
  86227. + DWC_DEBUGPL(DBG_ANY,
  86228. + "EP%d IN TKN EP Mismatch\n", epnum);
  86229. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  86230. + }
  86231. + /** IN Endpoint NAK Effective */
  86232. + if (diepint.b.inepnakeff) {
  86233. + DWC_DEBUGPL(DBG_ANY,
  86234. + "EP%d IN EP NAK Effective\n",
  86235. + epnum);
  86236. + /* Periodic EP */
  86237. + if (ep->disabling) {
  86238. + depctl.d32 = 0;
  86239. + depctl.b.snak = 1;
  86240. + depctl.b.epdis = 1;
  86241. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  86242. + [epnum]->diepctl,
  86243. + depctl.d32,
  86244. + depctl.d32);
  86245. + }
  86246. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  86247. +
  86248. + }
  86249. +
  86250. + /** IN EP Tx FIFO Empty Intr */
  86251. + if (diepint.b.emptyintr) {
  86252. + DWC_DEBUGPL(DBG_ANY,
  86253. + "EP%d Tx FIFO Empty Intr \n",
  86254. + epnum);
  86255. + write_empty_tx_fifo(pcd, epnum);
  86256. +
  86257. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  86258. +
  86259. + }
  86260. +
  86261. + /** IN EP BNA Intr */
  86262. + if (diepint.b.bna) {
  86263. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  86264. + if (core_if->dma_desc_enable) {
  86265. +#ifdef DWC_EN_ISOC
  86266. + if (dwc_ep->type ==
  86267. + DWC_OTG_EP_TYPE_ISOC) {
  86268. + /*
  86269. + * This checking is performed to prevent first "false" BNA
  86270. + * handling occuring right after reconnect
  86271. + */
  86272. + if (dwc_ep->next_frame !=
  86273. + 0xffffffff)
  86274. + dwc_otg_pcd_handle_iso_bna(ep);
  86275. + } else
  86276. +#endif /* DWC_EN_ISOC */
  86277. + {
  86278. + dwc_otg_pcd_handle_noniso_bna(ep);
  86279. + }
  86280. + }
  86281. + }
  86282. + /* NAK Interrutp */
  86283. + if (diepint.b.nak) {
  86284. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  86285. + epnum);
  86286. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  86287. + depctl_data_t depctl;
  86288. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  86289. + ep->dwc_ep.frame_num = core_if->frame_num;
  86290. + if (ep->dwc_ep.bInterval > 1) {
  86291. + depctl.d32 = 0;
  86292. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  86293. + if (ep->dwc_ep.frame_num & 0x1) {
  86294. + depctl.b.setd1pid = 1;
  86295. + depctl.b.setd0pid = 0;
  86296. + } else {
  86297. + depctl.b.setd0pid = 1;
  86298. + depctl.b.setd1pid = 0;
  86299. + }
  86300. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  86301. + }
  86302. + start_next_request(ep);
  86303. + }
  86304. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  86305. + if (dwc_ep->frame_num > 0x3FFF) {
  86306. + dwc_ep->frm_overrun = 1;
  86307. + dwc_ep->frame_num &= 0x3FFF;
  86308. + } else
  86309. + dwc_ep->frm_overrun = 0;
  86310. + }
  86311. +
  86312. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  86313. + }
  86314. + }
  86315. + epnum++;
  86316. + ep_intr >>= 1;
  86317. + }
  86318. +
  86319. + return 1;
  86320. +#undef CLEAR_IN_EP_INTR
  86321. +}
  86322. +
  86323. +/**
  86324. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  86325. + * The sequence for handling the OUT EP interrupt is shown below:
  86326. + * -# Read the Device All Endpoint Interrupt register
  86327. + * -# Repeat the following for each OUT EP interrupt bit set (from
  86328. + * LSB to MSB).
  86329. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  86330. + * -# If "Transfer Complete" call the request complete function
  86331. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  86332. + * -# If "AHB Error Interrupt" log error
  86333. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  86334. + * Command Processing)
  86335. + */
  86336. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  86337. +{
  86338. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  86339. +do { \
  86340. + doepint_data_t doepint = {.d32=0}; \
  86341. + doepint.b.__intr = 1; \
  86342. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  86343. + doepint.d32); \
  86344. +} while (0)
  86345. +
  86346. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  86347. + uint32_t ep_intr;
  86348. + doepint_data_t doepint = {.d32 = 0 };
  86349. + uint32_t epnum = 0;
  86350. + dwc_otg_pcd_ep_t *ep;
  86351. + dwc_ep_t *dwc_ep;
  86352. + dctl_data_t dctl = {.d32 = 0 };
  86353. + gintmsk_data_t gintmsk = {.d32 = 0 };
  86354. +
  86355. +
  86356. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  86357. +
  86358. + /* Read in the device interrupt bits */
  86359. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  86360. +
  86361. + while (ep_intr) {
  86362. + if (ep_intr & 0x1) {
  86363. + /* Get EP pointer */
  86364. + ep = get_out_ep(pcd, epnum);
  86365. + dwc_ep = &ep->dwc_ep;
  86366. +
  86367. +#ifdef VERBOSE
  86368. + DWC_DEBUGPL(DBG_PCDV,
  86369. + "EP%d-%s: type=%d, mps=%d\n",
  86370. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  86371. + dwc_ep->type, dwc_ep->maxpacket);
  86372. +#endif
  86373. + doepint.d32 =
  86374. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  86375. + /* Moved this interrupt upper due to core deffect of asserting
  86376. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  86377. + if (doepint.b.stsphsercvd) {
  86378. + deptsiz0_data_t deptsiz;
  86379. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  86380. + deptsiz.d32 =
  86381. + DWC_READ_REG32(&core_if->dev_if->
  86382. + out_ep_regs[0]->doeptsiz);
  86383. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  86384. + && core_if->dma_enable
  86385. + && core_if->dma_desc_enable == 0
  86386. + && doepint.b.xfercompl
  86387. + && deptsiz.b.xfersize == 24) {
  86388. + CLEAR_OUT_EP_INTR(core_if, epnum,
  86389. + xfercompl);
  86390. + doepint.b.xfercompl = 0;
  86391. + ep0_out_start(core_if, pcd);
  86392. + }
  86393. + if ((core_if->dma_desc_enable) ||
  86394. + (core_if->dma_enable
  86395. + && core_if->snpsid >=
  86396. + OTG_CORE_REV_3_00a)) {
  86397. + do_setup_in_status_phase(pcd);
  86398. + }
  86399. + }
  86400. + /* Transfer complete */
  86401. + if (doepint.b.xfercompl) {
  86402. +
  86403. + if (epnum == 0) {
  86404. + /* Clear the bit in DOEPINTn for this interrupt */
  86405. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  86406. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  86407. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  86408. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  86409. + doepint.d32);
  86410. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  86411. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  86412. +
  86413. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  86414. + && core_if->dma_enable == 0) {
  86415. + doepint_data_t doepint;
  86416. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  86417. + out_ep_regs[0]->doepint);
  86418. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  86419. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  86420. + goto exit_xfercompl;
  86421. + }
  86422. + }
  86423. + /* In case of DDMA look at SR bit to go to the Data Stage */
  86424. + if (core_if->dma_desc_enable) {
  86425. + dev_dma_desc_sts_t status = {.d32 = 0};
  86426. + if (pcd->ep0state == EP0_IDLE) {
  86427. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  86428. + dev_if->setup_desc_index]->status.d32;
  86429. + if(pcd->data_terminated) {
  86430. + pcd->data_terminated = 0;
  86431. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  86432. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  86433. + }
  86434. + if (status.b.sr) {
  86435. + if (doepint.b.setup) {
  86436. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  86437. + /* Already started data stage, clear setup */
  86438. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  86439. + doepint.b.setup = 0;
  86440. + handle_ep0(pcd);
  86441. + /* Prepare for more setup packets */
  86442. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  86443. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  86444. + ep0_out_start(core_if, pcd);
  86445. + }
  86446. +
  86447. + goto exit_xfercompl;
  86448. + } else {
  86449. + /* Prepare for more setup packets */
  86450. + DWC_DEBUGPL(DBG_PCDV,
  86451. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  86452. + ep0_out_start(core_if, pcd);
  86453. + }
  86454. + }
  86455. + } else {
  86456. + dwc_otg_pcd_request_t *req;
  86457. + dev_dma_desc_sts_t status = {.d32 = 0};
  86458. + diepint_data_t diepint0;
  86459. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  86460. + in_ep_regs[0]->diepint);
  86461. +
  86462. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  86463. + DWC_ERROR("EP0 is stalled/disconnected\n");
  86464. + }
  86465. +
  86466. + /* Clear IN xfercompl if set */
  86467. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  86468. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  86469. + DWC_WRITE_REG32(&core_if->dev_if->
  86470. + in_ep_regs[0]->diepint, diepint0.d32);
  86471. + }
  86472. +
  86473. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  86474. + dev_if->setup_desc_index]->status.d32;
  86475. +
  86476. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  86477. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  86478. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  86479. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  86480. + status.d32 = core_if->dev_if->
  86481. + out_desc_addr->status.d32;
  86482. +
  86483. + if (status.b.sr) {
  86484. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  86485. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  86486. + } else {
  86487. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  86488. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  86489. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  86490. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  86491. + /* Read arrived setup packet from req->buf */
  86492. + dwc_memcpy(&pcd->setup_pkt->req,
  86493. + req->buf + ep->dwc_ep.xfer_count, 8);
  86494. + }
  86495. + req->actual = ep->dwc_ep.xfer_count;
  86496. + dwc_otg_request_done(ep, req, -ECONNRESET);
  86497. + ep->dwc_ep.start_xfer_buff = 0;
  86498. + ep->dwc_ep.xfer_buff = 0;
  86499. + ep->dwc_ep.xfer_len = 0;
  86500. + }
  86501. + pcd->ep0state = EP0_IDLE;
  86502. + if (doepint.b.setup) {
  86503. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  86504. + /* Data stage started, clear setup */
  86505. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  86506. + doepint.b.setup = 0;
  86507. + handle_ep0(pcd);
  86508. + /* Prepare for setup packets if ep0in was enabled*/
  86509. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  86510. + ep0_out_start(core_if, pcd);
  86511. + }
  86512. +
  86513. + goto exit_xfercompl;
  86514. + } else {
  86515. + /* Prepare for more setup packets */
  86516. + DWC_DEBUGPL(DBG_PCDV,
  86517. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  86518. + ep0_out_start(core_if, pcd);
  86519. + }
  86520. + }
  86521. + }
  86522. + }
  86523. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  86524. + && core_if->dma_desc_enable == 0) {
  86525. + doepint_data_t doepint_temp = {.d32 = 0};
  86526. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  86527. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  86528. + out_ep_regs[ep->dwc_ep.num]->doepint);
  86529. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  86530. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  86531. + if (pcd->ep0state == EP0_IDLE) {
  86532. + if (doepint_temp.b.sr) {
  86533. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  86534. + }
  86535. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  86536. + out_ep_regs[0]->doepint);
  86537. + if (doeptsize0.b.supcnt == 3) {
  86538. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  86539. + ep->dwc_ep.stp_rollover = 1;
  86540. + }
  86541. + if (doepint.b.setup) {
  86542. +retry:
  86543. + /* Already started data stage, clear setup */
  86544. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  86545. + doepint.b.setup = 0;
  86546. + handle_ep0(pcd);
  86547. + ep->dwc_ep.stp_rollover = 0;
  86548. + /* Prepare for more setup packets */
  86549. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  86550. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  86551. + ep0_out_start(core_if, pcd);
  86552. + }
  86553. + goto exit_xfercompl;
  86554. + } else {
  86555. + /* Prepare for more setup packets */
  86556. + DWC_DEBUGPL(DBG_ANY,
  86557. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  86558. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  86559. + out_ep_regs[0]->doepint);
  86560. + if(doepint.b.setup)
  86561. + goto retry;
  86562. + ep0_out_start(core_if, pcd);
  86563. + }
  86564. + } else {
  86565. + dwc_otg_pcd_request_t *req;
  86566. + diepint_data_t diepint0 = {.d32 = 0};
  86567. + doepint_data_t doepint_temp = {.d32 = 0};
  86568. + depctl_data_t diepctl0;
  86569. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  86570. + in_ep_regs[0]->diepint);
  86571. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  86572. + in_ep_regs[0]->diepctl);
  86573. +
  86574. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  86575. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  86576. + if (diepint0.b.xfercompl) {
  86577. + DWC_WRITE_REG32(&core_if->dev_if->
  86578. + in_ep_regs[0]->diepint, diepint0.d32);
  86579. + }
  86580. + if (diepctl0.b.epena) {
  86581. + diepint_data_t diepint = {.d32 = 0};
  86582. + diepctl0.b.snak = 1;
  86583. + DWC_WRITE_REG32(&core_if->dev_if->
  86584. + in_ep_regs[0]->diepctl, diepctl0.d32);
  86585. + do {
  86586. + dwc_udelay(10);
  86587. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  86588. + in_ep_regs[0]->diepint);
  86589. + } while (!diepint.b.inepnakeff);
  86590. + diepint.b.inepnakeff = 1;
  86591. + DWC_WRITE_REG32(&core_if->dev_if->
  86592. + in_ep_regs[0]->diepint, diepint.d32);
  86593. + diepctl0.d32 = 0;
  86594. + diepctl0.b.epdis = 1;
  86595. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  86596. + diepctl0.d32);
  86597. + do {
  86598. + dwc_udelay(10);
  86599. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  86600. + in_ep_regs[0]->diepint);
  86601. + } while (!diepint.b.epdisabled);
  86602. + diepint.b.epdisabled = 1;
  86603. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  86604. + diepint.d32);
  86605. + }
  86606. + }
  86607. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  86608. + out_ep_regs[ep->dwc_ep.num]->doepint);
  86609. + if (doepint_temp.b.sr) {
  86610. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  86611. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  86612. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  86613. + } else {
  86614. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  86615. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  86616. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  86617. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  86618. + /* Read arrived setup packet from req->buf */
  86619. + dwc_memcpy(&pcd->setup_pkt->req,
  86620. + req->buf + ep->dwc_ep.xfer_count, 8);
  86621. + }
  86622. + req->actual = ep->dwc_ep.xfer_count;
  86623. + dwc_otg_request_done(ep, req, -ECONNRESET);
  86624. + ep->dwc_ep.start_xfer_buff = 0;
  86625. + ep->dwc_ep.xfer_buff = 0;
  86626. + ep->dwc_ep.xfer_len = 0;
  86627. + }
  86628. + pcd->ep0state = EP0_IDLE;
  86629. + if (doepint.b.setup) {
  86630. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  86631. + /* Data stage started, clear setup */
  86632. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  86633. + doepint.b.setup = 0;
  86634. + handle_ep0(pcd);
  86635. + /* Prepare for setup packets if ep0in was enabled*/
  86636. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  86637. + ep0_out_start(core_if, pcd);
  86638. + }
  86639. + goto exit_xfercompl;
  86640. + } else {
  86641. + /* Prepare for more setup packets */
  86642. + DWC_DEBUGPL(DBG_PCDV,
  86643. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  86644. + ep0_out_start(core_if, pcd);
  86645. + }
  86646. + }
  86647. + }
  86648. + }
  86649. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  86650. + handle_ep0(pcd);
  86651. +exit_xfercompl:
  86652. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  86653. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  86654. + } else {
  86655. + if (core_if->dma_desc_enable == 0
  86656. + || pcd->ep0state != EP0_IDLE)
  86657. + handle_ep0(pcd);
  86658. + }
  86659. +#ifdef DWC_EN_ISOC
  86660. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86661. + if (doepint.b.pktdrpsts == 0) {
  86662. + /* Clear the bit in DOEPINTn for this interrupt */
  86663. + CLEAR_OUT_EP_INTR(core_if,
  86664. + epnum,
  86665. + xfercompl);
  86666. + complete_iso_ep(pcd, ep);
  86667. + } else {
  86668. +
  86669. + doepint_data_t doepint = {.d32 = 0 };
  86670. + doepint.b.xfercompl = 1;
  86671. + doepint.b.pktdrpsts = 1;
  86672. + DWC_WRITE_REG32
  86673. + (&core_if->dev_if->out_ep_regs
  86674. + [epnum]->doepint,
  86675. + doepint.d32);
  86676. + if (handle_iso_out_pkt_dropped
  86677. + (core_if, dwc_ep)) {
  86678. + complete_iso_ep(pcd,
  86679. + ep);
  86680. + }
  86681. + }
  86682. +#endif /* DWC_EN_ISOC */
  86683. +#ifdef DWC_UTE_PER_IO
  86684. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86685. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  86686. + if (!ep->stopped)
  86687. + complete_xiso_ep(ep);
  86688. +#endif /* DWC_UTE_PER_IO */
  86689. + } else {
  86690. + /* Clear the bit in DOEPINTn for this interrupt */
  86691. + CLEAR_OUT_EP_INTR(core_if, epnum,
  86692. + xfercompl);
  86693. +
  86694. + if (core_if->core_params->dev_out_nak) {
  86695. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  86696. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  86697. +#ifdef DEBUG
  86698. + print_memory_payload(pcd, dwc_ep);
  86699. +#endif
  86700. + }
  86701. + complete_ep(ep);
  86702. + }
  86703. +
  86704. + }
  86705. +
  86706. + /* Endpoint disable */
  86707. + if (doepint.b.epdisabled) {
  86708. +
  86709. + /* Clear the bit in DOEPINTn for this interrupt */
  86710. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  86711. + if (core_if->core_params->dev_out_nak) {
  86712. +#ifdef DEBUG
  86713. + print_memory_payload(pcd, dwc_ep);
  86714. +#endif
  86715. + /* In case of timeout condition */
  86716. + if (core_if->ep_xfer_info[epnum].state == 2) {
  86717. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  86718. + dev_global_regs->dctl);
  86719. + dctl.b.cgoutnak = 1;
  86720. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  86721. + dctl.d32);
  86722. + /* Unmask goutnakeff interrupt which was masked
  86723. + * during handle nak out interrupt */
  86724. + gintmsk.b.goutnakeff = 1;
  86725. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  86726. + 0, gintmsk.d32);
  86727. +
  86728. + complete_ep(ep);
  86729. + }
  86730. + }
  86731. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  86732. + {
  86733. + dctl_data_t dctl;
  86734. + gintmsk_data_t intr_mask = {.d32 = 0};
  86735. + dwc_otg_pcd_request_t *req = 0;
  86736. +
  86737. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  86738. + dev_global_regs->dctl);
  86739. + dctl.b.cgoutnak = 1;
  86740. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  86741. + dctl.d32);
  86742. +
  86743. + intr_mask.d32 = 0;
  86744. + intr_mask.b.incomplisoout = 1;
  86745. +
  86746. + /* Get any pending requests */
  86747. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  86748. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  86749. + if (!req) {
  86750. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  86751. + } else {
  86752. + dwc_otg_request_done(ep, req, 0);
  86753. + start_next_request(ep);
  86754. + }
  86755. + } else {
  86756. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  86757. + }
  86758. + }
  86759. + }
  86760. + /* AHB Error */
  86761. + if (doepint.b.ahberr) {
  86762. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  86763. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  86764. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  86765. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  86766. + }
  86767. + /* Setup Phase Done (contorl EPs) */
  86768. + if (doepint.b.setup) {
  86769. +#ifdef DEBUG_EP0
  86770. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  86771. +#endif
  86772. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  86773. +
  86774. + handle_ep0(pcd);
  86775. + }
  86776. +
  86777. + /** OUT EP BNA Intr */
  86778. + if (doepint.b.bna) {
  86779. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  86780. + if (core_if->dma_desc_enable) {
  86781. +#ifdef DWC_EN_ISOC
  86782. + if (dwc_ep->type ==
  86783. + DWC_OTG_EP_TYPE_ISOC) {
  86784. + /*
  86785. + * This checking is performed to prevent first "false" BNA
  86786. + * handling occuring right after reconnect
  86787. + */
  86788. + if (dwc_ep->next_frame !=
  86789. + 0xffffffff)
  86790. + dwc_otg_pcd_handle_iso_bna(ep);
  86791. + } else
  86792. +#endif /* DWC_EN_ISOC */
  86793. + {
  86794. + dwc_otg_pcd_handle_noniso_bna(ep);
  86795. + }
  86796. + }
  86797. + }
  86798. + /* Babble Interrupt */
  86799. + if (doepint.b.babble) {
  86800. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  86801. + epnum);
  86802. + handle_out_ep_babble_intr(pcd, epnum);
  86803. +
  86804. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  86805. + }
  86806. + if (doepint.b.outtknepdis) {
  86807. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  86808. + disabled\n",epnum);
  86809. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  86810. + doepmsk_data_t doepmsk = {.d32 = 0};
  86811. + ep->dwc_ep.frame_num = core_if->frame_num;
  86812. + if (ep->dwc_ep.bInterval > 1) {
  86813. + depctl_data_t depctl;
  86814. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  86815. + out_ep_regs[epnum]->doepctl);
  86816. + if (ep->dwc_ep.frame_num & 0x1) {
  86817. + depctl.b.setd1pid = 1;
  86818. + depctl.b.setd0pid = 0;
  86819. + } else {
  86820. + depctl.b.setd0pid = 1;
  86821. + depctl.b.setd1pid = 0;
  86822. + }
  86823. + DWC_WRITE_REG32(&core_if->dev_if->
  86824. + out_ep_regs[epnum]->doepctl, depctl.d32);
  86825. + }
  86826. + start_next_request(ep);
  86827. + doepmsk.b.outtknepdis = 1;
  86828. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  86829. + doepmsk.d32, 0);
  86830. + }
  86831. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  86832. + }
  86833. +
  86834. + /* NAK Interrutp */
  86835. + if (doepint.b.nak) {
  86836. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  86837. + handle_out_ep_nak_intr(pcd, epnum);
  86838. +
  86839. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  86840. + }
  86841. + /* NYET Interrutp */
  86842. + if (doepint.b.nyet) {
  86843. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  86844. + handle_out_ep_nyet_intr(pcd, epnum);
  86845. +
  86846. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  86847. + }
  86848. + }
  86849. +
  86850. + epnum++;
  86851. + ep_intr >>= 1;
  86852. + }
  86853. +
  86854. + return 1;
  86855. +
  86856. +#undef CLEAR_OUT_EP_INTR
  86857. +}
  86858. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  86859. +{
  86860. + int retval = 0;
  86861. + if(!frm_overrun && curr_fr >= trgt_fr)
  86862. + retval = 1;
  86863. + else if (frm_overrun
  86864. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  86865. + retval = 1;
  86866. + return retval;
  86867. +}
  86868. +/**
  86869. + * Incomplete ISO IN Transfer Interrupt.
  86870. + * This interrupt indicates one of the following conditions occurred
  86871. + * while transmitting an ISOC transaction.
  86872. + * - Corrupted IN Token for ISOC EP.
  86873. + * - Packet not complete in FIFO.
  86874. + * The follow actions will be taken:
  86875. + * -# Determine the EP
  86876. + * -# Set incomplete flag in dwc_ep structure
  86877. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  86878. + * Flush FIFO
  86879. + */
  86880. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  86881. +{
  86882. + gintsts_data_t gintsts;
  86883. +
  86884. +#ifdef DWC_EN_ISOC
  86885. + dwc_otg_dev_if_t *dev_if;
  86886. + deptsiz_data_t deptsiz = {.d32 = 0 };
  86887. + depctl_data_t depctl = {.d32 = 0 };
  86888. + dsts_data_t dsts = {.d32 = 0 };
  86889. + dwc_ep_t *dwc_ep;
  86890. + int i;
  86891. +
  86892. + dev_if = GET_CORE_IF(pcd)->dev_if;
  86893. +
  86894. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  86895. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  86896. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86897. + deptsiz.d32 =
  86898. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  86899. + depctl.d32 =
  86900. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86901. +
  86902. + if (depctl.b.epdis && deptsiz.d32) {
  86903. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  86904. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  86905. + dwc_ep->cur_pkt = 0;
  86906. + dwc_ep->proc_buf_num =
  86907. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  86908. +
  86909. + if (dwc_ep->proc_buf_num) {
  86910. + dwc_ep->cur_pkt_addr =
  86911. + dwc_ep->xfer_buff1;
  86912. + dwc_ep->cur_pkt_dma_addr =
  86913. + dwc_ep->dma_addr1;
  86914. + } else {
  86915. + dwc_ep->cur_pkt_addr =
  86916. + dwc_ep->xfer_buff0;
  86917. + dwc_ep->cur_pkt_dma_addr =
  86918. + dwc_ep->dma_addr0;
  86919. + }
  86920. +
  86921. + }
  86922. +
  86923. + dsts.d32 =
  86924. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  86925. + dev_global_regs->dsts);
  86926. + dwc_ep->next_frame = dsts.b.soffn;
  86927. +
  86928. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  86929. + (pcd),
  86930. + dwc_ep);
  86931. + }
  86932. + }
  86933. + }
  86934. +
  86935. +#else
  86936. + depctl_data_t depctl = {.d32 = 0 };
  86937. + dwc_ep_t *dwc_ep;
  86938. + dwc_otg_dev_if_t *dev_if;
  86939. + int i;
  86940. + dev_if = GET_CORE_IF(pcd)->dev_if;
  86941. +
  86942. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  86943. +
  86944. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  86945. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  86946. + depctl.d32 =
  86947. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86948. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  86949. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  86950. + dwc_ep->frm_overrun))
  86951. + {
  86952. + depctl.d32 =
  86953. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  86954. + depctl.b.snak = 1;
  86955. + depctl.b.epdis = 1;
  86956. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  86957. + }
  86958. + }
  86959. + }
  86960. +
  86961. + /*intr_mask.b.incomplisoin = 1;
  86962. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  86963. + intr_mask.d32, 0); */
  86964. +#endif //DWC_EN_ISOC
  86965. +
  86966. + /* Clear interrupt */
  86967. + gintsts.d32 = 0;
  86968. + gintsts.b.incomplisoin = 1;
  86969. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  86970. + gintsts.d32);
  86971. +
  86972. + return 1;
  86973. +}
  86974. +
  86975. +/**
  86976. + * Incomplete ISO OUT Transfer Interrupt.
  86977. + *
  86978. + * This interrupt indicates that the core has dropped an ISO OUT
  86979. + * packet. The following conditions can be the cause:
  86980. + * - FIFO Full, the entire packet would not fit in the FIFO.
  86981. + * - CRC Error
  86982. + * - Corrupted Token
  86983. + * The follow actions will be taken:
  86984. + * -# Determine the EP
  86985. + * -# Set incomplete flag in dwc_ep structure
  86986. + * -# Read any data from the FIFO
  86987. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  86988. + * re-enable EP.
  86989. + */
  86990. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  86991. +{
  86992. +
  86993. + gintsts_data_t gintsts;
  86994. +
  86995. +#ifdef DWC_EN_ISOC
  86996. + dwc_otg_dev_if_t *dev_if;
  86997. + deptsiz_data_t deptsiz = {.d32 = 0 };
  86998. + depctl_data_t depctl = {.d32 = 0 };
  86999. + dsts_data_t dsts = {.d32 = 0 };
  87000. + dwc_ep_t *dwc_ep;
  87001. + int i;
  87002. +
  87003. + dev_if = GET_CORE_IF(pcd)->dev_if;
  87004. +
  87005. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  87006. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  87007. + if (pcd->out_ep[i].dwc_ep.active &&
  87008. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  87009. + deptsiz.d32 =
  87010. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  87011. + depctl.d32 =
  87012. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  87013. +
  87014. + if (depctl.b.epdis && deptsiz.d32) {
  87015. + set_current_pkt_info(GET_CORE_IF(pcd),
  87016. + &pcd->out_ep[i].dwc_ep);
  87017. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  87018. + dwc_ep->cur_pkt = 0;
  87019. + dwc_ep->proc_buf_num =
  87020. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  87021. +
  87022. + if (dwc_ep->proc_buf_num) {
  87023. + dwc_ep->cur_pkt_addr =
  87024. + dwc_ep->xfer_buff1;
  87025. + dwc_ep->cur_pkt_dma_addr =
  87026. + dwc_ep->dma_addr1;
  87027. + } else {
  87028. + dwc_ep->cur_pkt_addr =
  87029. + dwc_ep->xfer_buff0;
  87030. + dwc_ep->cur_pkt_dma_addr =
  87031. + dwc_ep->dma_addr0;
  87032. + }
  87033. +
  87034. + }
  87035. +
  87036. + dsts.d32 =
  87037. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  87038. + dev_global_regs->dsts);
  87039. + dwc_ep->next_frame = dsts.b.soffn;
  87040. +
  87041. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  87042. + (pcd),
  87043. + dwc_ep);
  87044. + }
  87045. + }
  87046. + }
  87047. +#else
  87048. + /** @todo implement ISR */
  87049. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87050. + dwc_otg_core_if_t *core_if;
  87051. + deptsiz_data_t deptsiz = {.d32 = 0 };
  87052. + depctl_data_t depctl = {.d32 = 0 };
  87053. + dctl_data_t dctl = {.d32 = 0 };
  87054. + dwc_ep_t *dwc_ep = NULL;
  87055. + int i;
  87056. + core_if = GET_CORE_IF(pcd);
  87057. +
  87058. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  87059. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  87060. + depctl.d32 =
  87061. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  87062. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  87063. + core_if->dev_if->isoc_ep = dwc_ep;
  87064. + deptsiz.d32 =
  87065. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  87066. + break;
  87067. + }
  87068. + }
  87069. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  87070. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  87071. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  87072. +
  87073. + if (!intr_mask.b.goutnakeff) {
  87074. + /* Unmask it */
  87075. + intr_mask.b.goutnakeff = 1;
  87076. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  87077. + }
  87078. + if (!gintsts.b.goutnakeff) {
  87079. + dctl.b.sgoutnak = 1;
  87080. + }
  87081. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  87082. +
  87083. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  87084. + if (depctl.b.epena) {
  87085. + depctl.b.epdis = 1;
  87086. + depctl.b.snak = 1;
  87087. + }
  87088. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  87089. +
  87090. + intr_mask.d32 = 0;
  87091. + intr_mask.b.incomplisoout = 1;
  87092. +
  87093. +#endif /* DWC_EN_ISOC */
  87094. +
  87095. + /* Clear interrupt */
  87096. + gintsts.d32 = 0;
  87097. + gintsts.b.incomplisoout = 1;
  87098. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87099. + gintsts.d32);
  87100. +
  87101. + return 1;
  87102. +}
  87103. +
  87104. +/**
  87105. + * This function handles the Global IN NAK Effective interrupt.
  87106. + *
  87107. + */
  87108. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  87109. +{
  87110. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  87111. + depctl_data_t diepctl = {.d32 = 0 };
  87112. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87113. + gintsts_data_t gintsts;
  87114. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  87115. + int i;
  87116. +
  87117. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  87118. +
  87119. + /* Disable all active IN EPs */
  87120. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  87121. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  87122. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  87123. + if (core_if->start_predict > 0)
  87124. + core_if->start_predict++;
  87125. + diepctl.b.epdis = 1;
  87126. + diepctl.b.snak = 1;
  87127. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  87128. + }
  87129. + }
  87130. +
  87131. +
  87132. + /* Disable the Global IN NAK Effective Interrupt */
  87133. + intr_mask.b.ginnakeff = 1;
  87134. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  87135. + intr_mask.d32, 0);
  87136. +
  87137. + /* Clear interrupt */
  87138. + gintsts.d32 = 0;
  87139. + gintsts.b.ginnakeff = 1;
  87140. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87141. + gintsts.d32);
  87142. +
  87143. + return 1;
  87144. +}
  87145. +
  87146. +/**
  87147. + * OUT NAK Effective.
  87148. + *
  87149. + */
  87150. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  87151. +{
  87152. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  87153. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87154. + gintsts_data_t gintsts;
  87155. + depctl_data_t doepctl;
  87156. + int i;
  87157. +
  87158. + /* Disable the Global OUT NAK Effective Interrupt */
  87159. + intr_mask.b.goutnakeff = 1;
  87160. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  87161. + intr_mask.d32, 0);
  87162. +
  87163. + /* If DEV OUT NAK enabled*/
  87164. + if (pcd->core_if->core_params->dev_out_nak) {
  87165. + /* Run over all out endpoints to determine the ep number on
  87166. + * which the timeout has happened
  87167. + */
  87168. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  87169. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  87170. + break;
  87171. + }
  87172. + if (i > dev_if->num_out_eps) {
  87173. + dctl_data_t dctl;
  87174. + dctl.d32 =
  87175. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  87176. + dctl.b.cgoutnak = 1;
  87177. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  87178. + dctl.d32);
  87179. + goto out;
  87180. + }
  87181. +
  87182. + /* Disable the endpoint */
  87183. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  87184. + if (doepctl.b.epena) {
  87185. + doepctl.b.epdis = 1;
  87186. + doepctl.b.snak = 1;
  87187. + }
  87188. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  87189. + return 1;
  87190. + }
  87191. + /* We come here from Incomplete ISO OUT handler */
  87192. + if (dev_if->isoc_ep) {
  87193. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  87194. + uint32_t epnum = dwc_ep->num;
  87195. + doepint_data_t doepint;
  87196. + doepint.d32 =
  87197. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  87198. + dev_if->isoc_ep = NULL;
  87199. + doepctl.d32 =
  87200. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  87201. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  87202. + if (doepctl.b.epena) {
  87203. + doepctl.b.epdis = 1;
  87204. + doepctl.b.snak = 1;
  87205. + }
  87206. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  87207. + doepctl.d32);
  87208. + return 1;
  87209. + } else
  87210. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  87211. + "Global OUT NAK Effective\n");
  87212. +
  87213. +out:
  87214. + /* Clear interrupt */
  87215. + gintsts.d32 = 0;
  87216. + gintsts.b.goutnakeff = 1;
  87217. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  87218. + gintsts.d32);
  87219. +
  87220. + return 1;
  87221. +}
  87222. +
  87223. +/**
  87224. + * PCD interrupt handler.
  87225. + *
  87226. + * The PCD handles the device interrupts. Many conditions can cause a
  87227. + * device interrupt. When an interrupt occurs, the device interrupt
  87228. + * service routine determines the cause of the interrupt and
  87229. + * dispatches handling to the appropriate function. These interrupt
  87230. + * handling functions are described below.
  87231. + *
  87232. + * All interrupt registers are processed from LSB to MSB.
  87233. + *
  87234. + */
  87235. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  87236. +{
  87237. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  87238. +#ifdef VERBOSE
  87239. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  87240. +#endif
  87241. + gintsts_data_t gintr_status;
  87242. + int32_t retval = 0;
  87243. +
  87244. + /* Exit from ISR if core is hibernated */
  87245. + if (core_if->hibernation_suspend == 1) {
  87246. + return retval;
  87247. + }
  87248. +#ifdef VERBOSE
  87249. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  87250. + __func__,
  87251. + DWC_READ_REG32(&global_regs->gintsts),
  87252. + DWC_READ_REG32(&global_regs->gintmsk));
  87253. +#endif
  87254. +
  87255. + if (dwc_otg_is_device_mode(core_if)) {
  87256. + DWC_SPINLOCK(pcd->lock);
  87257. +#ifdef VERBOSE
  87258. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  87259. + __func__,
  87260. + DWC_READ_REG32(&global_regs->gintsts),
  87261. + DWC_READ_REG32(&global_regs->gintmsk));
  87262. +#endif
  87263. +
  87264. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  87265. +
  87266. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  87267. + __func__, gintr_status.d32);
  87268. +
  87269. + if (gintr_status.b.sofintr) {
  87270. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  87271. + }
  87272. + if (gintr_status.b.rxstsqlvl) {
  87273. + retval |=
  87274. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  87275. + }
  87276. + if (gintr_status.b.nptxfempty) {
  87277. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  87278. + }
  87279. + if (gintr_status.b.goutnakeff) {
  87280. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  87281. + }
  87282. + if (gintr_status.b.i2cintr) {
  87283. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  87284. + }
  87285. + if (gintr_status.b.erlysuspend) {
  87286. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  87287. + }
  87288. + if (gintr_status.b.usbreset) {
  87289. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  87290. + }
  87291. + if (gintr_status.b.enumdone) {
  87292. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  87293. + }
  87294. + if (gintr_status.b.isooutdrop) {
  87295. + retval |=
  87296. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  87297. + (pcd);
  87298. + }
  87299. + if (gintr_status.b.eopframe) {
  87300. + retval |=
  87301. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  87302. + }
  87303. + if (gintr_status.b.inepint) {
  87304. + if (!core_if->multiproc_int_enable) {
  87305. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  87306. + }
  87307. + }
  87308. + if (gintr_status.b.outepintr) {
  87309. + if (!core_if->multiproc_int_enable) {
  87310. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  87311. + }
  87312. + }
  87313. + if (gintr_status.b.epmismatch) {
  87314. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  87315. + }
  87316. + if (gintr_status.b.fetsusp) {
  87317. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  87318. + }
  87319. + if (gintr_status.b.ginnakeff) {
  87320. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  87321. + }
  87322. + if (gintr_status.b.incomplisoin) {
  87323. + retval |=
  87324. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  87325. + }
  87326. + if (gintr_status.b.incomplisoout) {
  87327. + retval |=
  87328. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  87329. + }
  87330. +
  87331. + /* In MPI mode Device Endpoints interrupts are asserted
  87332. + * without setting outepintr and inepint bits set, so these
  87333. + * Interrupt handlers are called without checking these bit-fields
  87334. + */
  87335. + if (core_if->multiproc_int_enable) {
  87336. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  87337. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  87338. + }
  87339. +#ifdef VERBOSE
  87340. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  87341. + DWC_READ_REG32(&global_regs->gintsts));
  87342. +#endif
  87343. + DWC_SPINUNLOCK(pcd->lock);
  87344. + }
  87345. + return retval;
  87346. +}
  87347. +
  87348. +#endif /* DWC_HOST_ONLY */
  87349. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  87350. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 1970-01-01 01:00:00.000000000 +0100
  87351. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 2014-03-11 16:55:38.000000000 +0100
  87352. @@ -0,0 +1,1358 @@
  87353. + /* ==========================================================================
  87354. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  87355. + * $Revision: #21 $
  87356. + * $Date: 2012/08/10 $
  87357. + * $Change: 2047372 $
  87358. + *
  87359. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  87360. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  87361. + * otherwise expressly agreed to in writing between Synopsys and you.
  87362. + *
  87363. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  87364. + * any End User Software License Agreement or Agreement for Licensed Product
  87365. + * with Synopsys or any supplement thereto. You are permitted to use and
  87366. + * redistribute this Software in source and binary forms, with or without
  87367. + * modification, provided that redistributions of source code must retain this
  87368. + * notice. You may not view, use, disclose, copy or distribute this file or
  87369. + * any information contained herein except pursuant to this license grant from
  87370. + * Synopsys. If you do not agree with this notice, including the disclaimer
  87371. + * below, then you are not authorized to use the Software.
  87372. + *
  87373. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  87374. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  87375. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  87376. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  87377. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  87378. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  87379. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87380. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  87381. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  87382. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  87383. + * DAMAGE.
  87384. + * ========================================================================== */
  87385. +#ifndef DWC_HOST_ONLY
  87386. +
  87387. +/** @file
  87388. + * This file implements the Peripheral Controller Driver.
  87389. + *
  87390. + * The Peripheral Controller Driver (PCD) is responsible for
  87391. + * translating requests from the Function Driver into the appropriate
  87392. + * actions on the DWC_otg controller. It isolates the Function Driver
  87393. + * from the specifics of the controller by providing an API to the
  87394. + * Function Driver.
  87395. + *
  87396. + * The Peripheral Controller Driver for Linux will implement the
  87397. + * Gadget API, so that the existing Gadget drivers can be used.
  87398. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  87399. + *
  87400. + * The Linux Gadget API is defined in the header file
  87401. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  87402. + * defined in the structure <code>usb_ep_ops</code> and the USB
  87403. + * Controller API is defined in the structure
  87404. + * <code>usb_gadget_ops</code>.
  87405. + *
  87406. + */
  87407. +
  87408. +#include "dwc_otg_os_dep.h"
  87409. +#include "dwc_otg_pcd_if.h"
  87410. +#include "dwc_otg_pcd.h"
  87411. +#include "dwc_otg_driver.h"
  87412. +#include "dwc_otg_dbg.h"
  87413. +
  87414. +static struct gadget_wrapper {
  87415. + dwc_otg_pcd_t *pcd;
  87416. +
  87417. + struct usb_gadget gadget;
  87418. + struct usb_gadget_driver *driver;
  87419. +
  87420. + struct usb_ep ep0;
  87421. + struct usb_ep in_ep[16];
  87422. + struct usb_ep out_ep[16];
  87423. +
  87424. +} *gadget_wrapper;
  87425. +
  87426. +/* Display the contents of the buffer */
  87427. +extern void dump_msg(const u8 * buf, unsigned int length);
  87428. +/**
  87429. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  87430. + * if the endpoint is not found
  87431. + */
  87432. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  87433. +{
  87434. + int i;
  87435. + if (pcd->ep0.priv == handle) {
  87436. + return &pcd->ep0;
  87437. + }
  87438. +
  87439. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  87440. + if (pcd->in_ep[i].priv == handle)
  87441. + return &pcd->in_ep[i];
  87442. + if (pcd->out_ep[i].priv == handle)
  87443. + return &pcd->out_ep[i];
  87444. + }
  87445. +
  87446. + return NULL;
  87447. +}
  87448. +
  87449. +/* USB Endpoint Operations */
  87450. +/*
  87451. + * The following sections briefly describe the behavior of the Gadget
  87452. + * API endpoint operations implemented in the DWC_otg driver
  87453. + * software. Detailed descriptions of the generic behavior of each of
  87454. + * these functions can be found in the Linux header file
  87455. + * include/linux/usb_gadget.h.
  87456. + *
  87457. + * The Gadget API provides wrapper functions for each of the function
  87458. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  87459. + * function, which then calls the underlying PCD function. The
  87460. + * following sections are named according to the wrapper
  87461. + * functions. Within each section, the corresponding DWC_otg PCD
  87462. + * function name is specified.
  87463. + *
  87464. + */
  87465. +
  87466. +/**
  87467. + * This function is called by the Gadget Driver for each EP to be
  87468. + * configured for the current configuration (SET_CONFIGURATION).
  87469. + *
  87470. + * This function initializes the dwc_otg_ep_t data structure, and then
  87471. + * calls dwc_otg_ep_activate.
  87472. + */
  87473. +static int ep_enable(struct usb_ep *usb_ep,
  87474. + const struct usb_endpoint_descriptor *ep_desc)
  87475. +{
  87476. + int retval;
  87477. +
  87478. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  87479. +
  87480. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  87481. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  87482. + return -EINVAL;
  87483. + }
  87484. + if (usb_ep == &gadget_wrapper->ep0) {
  87485. + DWC_WARN("%s, bad ep(0)\n", __func__);
  87486. + return -EINVAL;
  87487. + }
  87488. +
  87489. + /* Check FIFO size? */
  87490. + if (!ep_desc->wMaxPacketSize) {
  87491. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  87492. + return -ERANGE;
  87493. + }
  87494. +
  87495. + if (!gadget_wrapper->driver ||
  87496. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  87497. + DWC_WARN("%s, bogus device state\n", __func__);
  87498. + return -ESHUTDOWN;
  87499. + }
  87500. +
  87501. + /* Delete after check - MAS */
  87502. +#if 0
  87503. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  87504. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  87505. + nat = (nat >> 11) & 0x03;
  87506. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  87507. +#endif
  87508. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  87509. + (const uint8_t *)ep_desc,
  87510. + (void *)usb_ep);
  87511. + if (retval) {
  87512. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  87513. + return -EINVAL;
  87514. + }
  87515. +
  87516. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  87517. +
  87518. + return 0;
  87519. +}
  87520. +
  87521. +/**
  87522. + * This function is called when an EP is disabled due to disconnect or
  87523. + * change in configuration. Any pending requests will terminate with a
  87524. + * status of -ESHUTDOWN.
  87525. + *
  87526. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  87527. + * and then calls dwc_otg_ep_deactivate.
  87528. + */
  87529. +static int ep_disable(struct usb_ep *usb_ep)
  87530. +{
  87531. + int retval;
  87532. +
  87533. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  87534. + if (!usb_ep) {
  87535. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  87536. + usb_ep ? usb_ep->name : NULL);
  87537. + return -EINVAL;
  87538. + }
  87539. +
  87540. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  87541. + if (retval) {
  87542. + retval = -EINVAL;
  87543. + }
  87544. +
  87545. + return retval;
  87546. +}
  87547. +
  87548. +/**
  87549. + * This function allocates a request object to use with the specified
  87550. + * endpoint.
  87551. + *
  87552. + * @param ep The endpoint to be used with with the request
  87553. + * @param gfp_flags the GFP_* flags to use.
  87554. + */
  87555. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  87556. + gfp_t gfp_flags)
  87557. +{
  87558. + struct usb_request *usb_req;
  87559. +
  87560. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  87561. + if (0 == ep) {
  87562. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  87563. + return 0;
  87564. + }
  87565. + usb_req = kmalloc(sizeof(*usb_req), gfp_flags);
  87566. + if (0 == usb_req) {
  87567. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  87568. + return 0;
  87569. + }
  87570. + memset(usb_req, 0, sizeof(*usb_req));
  87571. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  87572. +
  87573. + return usb_req;
  87574. +}
  87575. +
  87576. +/**
  87577. + * This function frees a request object.
  87578. + *
  87579. + * @param ep The endpoint associated with the request
  87580. + * @param req The request being freed
  87581. + */
  87582. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  87583. +{
  87584. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  87585. +
  87586. + if (0 == ep || 0 == req) {
  87587. + DWC_WARN("%s() %s\n", __func__,
  87588. + "Invalid ep or req argument!\n");
  87589. + return;
  87590. + }
  87591. +
  87592. + kfree(req);
  87593. +}
  87594. +
  87595. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87596. +/**
  87597. + * This function allocates an I/O buffer to be used for a transfer
  87598. + * to/from the specified endpoint.
  87599. + *
  87600. + * @param usb_ep The endpoint to be used with with the request
  87601. + * @param bytes The desired number of bytes for the buffer
  87602. + * @param dma Pointer to the buffer's DMA address; must be valid
  87603. + * @param gfp_flags the GFP_* flags to use.
  87604. + * @return address of a new buffer or null is buffer could not be allocated.
  87605. + */
  87606. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  87607. + dma_addr_t * dma, gfp_t gfp_flags)
  87608. +{
  87609. + void *buf;
  87610. + dwc_otg_pcd_t *pcd = 0;
  87611. +
  87612. + pcd = gadget_wrapper->pcd;
  87613. +
  87614. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  87615. + dma, gfp_flags);
  87616. +
  87617. + /* Check dword alignment */
  87618. + if ((bytes & 0x3UL) != 0) {
  87619. + DWC_WARN("%s() Buffer size is not a multiple of"
  87620. + "DWORD size (%d)", __func__, bytes);
  87621. + }
  87622. +
  87623. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  87624. +
  87625. + /* Check dword alignment */
  87626. + if (((int)buf & 0x3UL) != 0) {
  87627. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  87628. + __func__, buf);
  87629. + }
  87630. +
  87631. + return buf;
  87632. +}
  87633. +
  87634. +/**
  87635. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  87636. + *
  87637. + * @param usb_ep the endpoint associated with the buffer
  87638. + * @param buf address of the buffer
  87639. + * @param dma The buffer's DMA address
  87640. + * @param bytes The number of bytes of the buffer
  87641. + */
  87642. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  87643. + dma_addr_t dma, unsigned bytes)
  87644. +{
  87645. + dwc_otg_pcd_t *pcd = 0;
  87646. +
  87647. + pcd = gadget_wrapper->pcd;
  87648. +
  87649. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  87650. +
  87651. + dma_free_coherent(NULL, bytes, buf, dma);
  87652. +}
  87653. +#endif
  87654. +
  87655. +/**
  87656. + * This function is used to submit an I/O Request to an EP.
  87657. + *
  87658. + * - When the request completes the request's completion callback
  87659. + * is called to return the request to the driver.
  87660. + * - An EP, except control EPs, may have multiple requests
  87661. + * pending.
  87662. + * - Once submitted the request cannot be examined or modified.
  87663. + * - Each request is turned into one or more packets.
  87664. + * - A BULK EP can queue any amount of data; the transfer is
  87665. + * packetized.
  87666. + * - Zero length Packets are specified with the request 'zero'
  87667. + * flag.
  87668. + */
  87669. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  87670. + gfp_t gfp_flags)
  87671. +{
  87672. + dwc_otg_pcd_t *pcd;
  87673. + struct dwc_otg_pcd_ep *ep = NULL;
  87674. + int retval = 0, is_isoc_ep = 0;
  87675. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  87676. +
  87677. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  87678. + __func__, usb_ep, usb_req, gfp_flags);
  87679. +
  87680. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  87681. + DWC_WARN("bad params\n");
  87682. + return -EINVAL;
  87683. + }
  87684. +
  87685. + if (!usb_ep) {
  87686. + DWC_WARN("bad ep\n");
  87687. + return -EINVAL;
  87688. + }
  87689. +
  87690. + pcd = gadget_wrapper->pcd;
  87691. + if (!gadget_wrapper->driver ||
  87692. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  87693. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  87694. + gadget_wrapper->gadget.speed);
  87695. + DWC_WARN("bogus device state\n");
  87696. + return -ESHUTDOWN;
  87697. + }
  87698. +
  87699. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  87700. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  87701. +
  87702. + usb_req->status = -EINPROGRESS;
  87703. + usb_req->actual = 0;
  87704. +
  87705. + ep = ep_from_handle(pcd, usb_ep);
  87706. + if (ep == NULL)
  87707. + is_isoc_ep = 0;
  87708. + else
  87709. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  87710. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87711. + dma_addr = usb_req->dma;
  87712. +#else
  87713. + if (GET_CORE_IF(pcd)->dma_enable) {
  87714. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  87715. + struct device *dev = NULL;
  87716. +
  87717. + if (otg_dev != NULL)
  87718. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  87719. +
  87720. + if (usb_req->length != 0 &&
  87721. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  87722. + dma_addr = dma_map_single(dev, usb_req->buf,
  87723. + usb_req->length,
  87724. + ep->dwc_ep.is_in ?
  87725. + DMA_TO_DEVICE:
  87726. + DMA_FROM_DEVICE);
  87727. + }
  87728. + }
  87729. +#endif
  87730. +
  87731. +#ifdef DWC_UTE_PER_IO
  87732. + if (is_isoc_ep == 1) {
  87733. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  87734. + usb_req->length, usb_req->zero, usb_req,
  87735. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  87736. + if (retval)
  87737. + return -EINVAL;
  87738. +
  87739. + return 0;
  87740. + }
  87741. +#endif
  87742. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  87743. + usb_req->length, usb_req->zero, usb_req,
  87744. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  87745. + if (retval) {
  87746. + return -EINVAL;
  87747. + }
  87748. +
  87749. + return 0;
  87750. +}
  87751. +
  87752. +/**
  87753. + * This function cancels an I/O request from an EP.
  87754. + */
  87755. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  87756. +{
  87757. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  87758. +
  87759. + if (!usb_ep || !usb_req) {
  87760. + DWC_WARN("bad argument\n");
  87761. + return -EINVAL;
  87762. + }
  87763. + if (!gadget_wrapper->driver ||
  87764. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  87765. + DWC_WARN("bogus device state\n");
  87766. + return -ESHUTDOWN;
  87767. + }
  87768. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  87769. + return -EINVAL;
  87770. + }
  87771. +
  87772. + return 0;
  87773. +}
  87774. +
  87775. +/**
  87776. + * usb_ep_set_halt stalls an endpoint.
  87777. + *
  87778. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  87779. + * toggle.
  87780. + *
  87781. + * Both of these functions are implemented with the same underlying
  87782. + * function. The behavior depends on the value argument.
  87783. + *
  87784. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  87785. + * @param[in] value
  87786. + * - 0 means clear_halt.
  87787. + * - 1 means set_halt,
  87788. + * - 2 means clear stall lock flag.
  87789. + * - 3 means set stall lock flag.
  87790. + */
  87791. +static int ep_halt(struct usb_ep *usb_ep, int value)
  87792. +{
  87793. + int retval = 0;
  87794. +
  87795. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  87796. +
  87797. + if (!usb_ep) {
  87798. + DWC_WARN("bad ep\n");
  87799. + return -EINVAL;
  87800. + }
  87801. +
  87802. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  87803. + if (retval == -DWC_E_AGAIN) {
  87804. + return -EAGAIN;
  87805. + } else if (retval) {
  87806. + retval = -EINVAL;
  87807. + }
  87808. +
  87809. + return retval;
  87810. +}
  87811. +
  87812. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  87813. +#if 0
  87814. +/**
  87815. + * ep_wedge: sets the halt feature and ignores clear requests
  87816. + *
  87817. + * @usb_ep: the endpoint being wedged
  87818. + *
  87819. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  87820. + * requests. If the gadget driver clears the halt status, it will
  87821. + * automatically unwedge the endpoint.
  87822. + *
  87823. + * Returns zero on success, else negative errno. *
  87824. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  87825. + */
  87826. +static int ep_wedge(struct usb_ep *usb_ep)
  87827. +{
  87828. + int retval = 0;
  87829. +
  87830. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  87831. +
  87832. + if (!usb_ep) {
  87833. + DWC_WARN("bad ep\n");
  87834. + return -EINVAL;
  87835. + }
  87836. +
  87837. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  87838. + if (retval == -DWC_E_AGAIN) {
  87839. + retval = -EAGAIN;
  87840. + } else if (retval) {
  87841. + retval = -EINVAL;
  87842. + }
  87843. +
  87844. + return retval;
  87845. +}
  87846. +#endif
  87847. +
  87848. +#ifdef DWC_EN_ISOC
  87849. +/**
  87850. + * This function is used to submit an ISOC Transfer Request to an EP.
  87851. + *
  87852. + * - Every time a sync period completes the request's completion callback
  87853. + * is called to provide data to the gadget driver.
  87854. + * - Once submitted the request cannot be modified.
  87855. + * - Each request is turned into periodic data packets untill ISO
  87856. + * Transfer is stopped..
  87857. + */
  87858. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  87859. + gfp_t gfp_flags)
  87860. +{
  87861. + int retval = 0;
  87862. +
  87863. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  87864. + DWC_WARN("bad params\n");
  87865. + return -EINVAL;
  87866. + }
  87867. +
  87868. + if (!usb_ep) {
  87869. + DWC_PRINTF("bad params\n");
  87870. + return -EINVAL;
  87871. + }
  87872. +
  87873. + req->status = -EINPROGRESS;
  87874. +
  87875. + retval =
  87876. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  87877. + req->buf1, req->dma0, req->dma1,
  87878. + req->sync_frame, req->data_pattern_frame,
  87879. + req->data_per_frame,
  87880. + req->
  87881. + flags & USB_REQ_ISO_ASAP ? -1 :
  87882. + req->start_frame, req->buf_proc_intrvl,
  87883. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  87884. +
  87885. + if (retval) {
  87886. + return -EINVAL;
  87887. + }
  87888. +
  87889. + return retval;
  87890. +}
  87891. +
  87892. +/**
  87893. + * This function stops ISO EP Periodic Data Transfer.
  87894. + */
  87895. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  87896. +{
  87897. + int retval = 0;
  87898. + if (!usb_ep) {
  87899. + DWC_WARN("bad ep\n");
  87900. + }
  87901. +
  87902. + if (!gadget_wrapper->driver ||
  87903. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  87904. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  87905. + gadget_wrapper->gadget.speed);
  87906. + DWC_WARN("bogus device state\n");
  87907. + }
  87908. +
  87909. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  87910. + if (retval) {
  87911. + retval = -EINVAL;
  87912. + }
  87913. +
  87914. + return retval;
  87915. +}
  87916. +
  87917. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  87918. + int packets, gfp_t gfp_flags)
  87919. +{
  87920. + struct usb_iso_request *pReq = NULL;
  87921. + uint32_t req_size;
  87922. +
  87923. + req_size = sizeof(struct usb_iso_request);
  87924. + req_size +=
  87925. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  87926. +
  87927. + pReq = kmalloc(req_size, gfp_flags);
  87928. + if (!pReq) {
  87929. + DWC_WARN("Can't allocate Iso Request\n");
  87930. + return 0;
  87931. + }
  87932. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  87933. +
  87934. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  87935. +
  87936. + return pReq;
  87937. +}
  87938. +
  87939. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  87940. +{
  87941. + kfree(req);
  87942. +}
  87943. +
  87944. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  87945. + .ep_ops = {
  87946. + .enable = ep_enable,
  87947. + .disable = ep_disable,
  87948. +
  87949. + .alloc_request = dwc_otg_pcd_alloc_request,
  87950. + .free_request = dwc_otg_pcd_free_request,
  87951. +
  87952. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87953. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  87954. + .free_buffer = dwc_otg_pcd_free_buffer,
  87955. +#endif
  87956. +
  87957. + .queue = ep_queue,
  87958. + .dequeue = ep_dequeue,
  87959. +
  87960. + .set_halt = ep_halt,
  87961. + .fifo_status = 0,
  87962. + .fifo_flush = 0,
  87963. + },
  87964. + .iso_ep_start = iso_ep_start,
  87965. + .iso_ep_stop = iso_ep_stop,
  87966. + .alloc_iso_request = alloc_iso_request,
  87967. + .free_iso_request = free_iso_request,
  87968. +};
  87969. +
  87970. +#else
  87971. +
  87972. + int (*enable) (struct usb_ep *ep,
  87973. + const struct usb_endpoint_descriptor *desc);
  87974. + int (*disable) (struct usb_ep *ep);
  87975. +
  87976. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  87977. + gfp_t gfp_flags);
  87978. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  87979. +
  87980. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  87981. + gfp_t gfp_flags);
  87982. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  87983. +
  87984. + int (*set_halt) (struct usb_ep *ep, int value);
  87985. + int (*set_wedge) (struct usb_ep *ep);
  87986. +
  87987. + int (*fifo_status) (struct usb_ep *ep);
  87988. + void (*fifo_flush) (struct usb_ep *ep);
  87989. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  87990. + .enable = ep_enable,
  87991. + .disable = ep_disable,
  87992. +
  87993. + .alloc_request = dwc_otg_pcd_alloc_request,
  87994. + .free_request = dwc_otg_pcd_free_request,
  87995. +
  87996. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87997. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  87998. + .free_buffer = dwc_otg_pcd_free_buffer,
  87999. +#else
  88000. + /* .set_wedge = ep_wedge, */
  88001. + .set_wedge = NULL, /* uses set_halt instead */
  88002. +#endif
  88003. +
  88004. + .queue = ep_queue,
  88005. + .dequeue = ep_dequeue,
  88006. +
  88007. + .set_halt = ep_halt,
  88008. + .fifo_status = 0,
  88009. + .fifo_flush = 0,
  88010. +
  88011. +};
  88012. +
  88013. +#endif /* _EN_ISOC_ */
  88014. +/* Gadget Operations */
  88015. +/**
  88016. + * The following gadget operations will be implemented in the DWC_otg
  88017. + * PCD. Functions in the API that are not described below are not
  88018. + * implemented.
  88019. + *
  88020. + * The Gadget API provides wrapper functions for each of the function
  88021. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  88022. + * wrapper function, which then calls the underlying PCD function. The
  88023. + * following sections are named according to the wrapper functions
  88024. + * (except for ioctl, which doesn't have a wrapper function). Within
  88025. + * each section, the corresponding DWC_otg PCD function name is
  88026. + * specified.
  88027. + *
  88028. + */
  88029. +
  88030. +/**
  88031. + *Gets the USB Frame number of the last SOF.
  88032. + */
  88033. +static int get_frame_number(struct usb_gadget *gadget)
  88034. +{
  88035. + struct gadget_wrapper *d;
  88036. +
  88037. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  88038. +
  88039. + if (gadget == 0) {
  88040. + return -ENODEV;
  88041. + }
  88042. +
  88043. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88044. + return dwc_otg_pcd_get_frame_number(d->pcd);
  88045. +}
  88046. +
  88047. +#ifdef CONFIG_USB_DWC_OTG_LPM
  88048. +static int test_lpm_enabled(struct usb_gadget *gadget)
  88049. +{
  88050. + struct gadget_wrapper *d;
  88051. +
  88052. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88053. +
  88054. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  88055. +}
  88056. +#endif
  88057. +
  88058. +/**
  88059. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  88060. + * session is in progress. If a session is already in progress, but
  88061. + * the device is suspended, remote wakeup signaling is started.
  88062. + *
  88063. + */
  88064. +static int wakeup(struct usb_gadget *gadget)
  88065. +{
  88066. + struct gadget_wrapper *d;
  88067. +
  88068. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  88069. +
  88070. + if (gadget == 0) {
  88071. + return -ENODEV;
  88072. + } else {
  88073. + d = container_of(gadget, struct gadget_wrapper, gadget);
  88074. + }
  88075. + dwc_otg_pcd_wakeup(d->pcd);
  88076. + return 0;
  88077. +}
  88078. +
  88079. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  88080. + .get_frame = get_frame_number,
  88081. + .wakeup = wakeup,
  88082. +#ifdef CONFIG_USB_DWC_OTG_LPM
  88083. + .lpm_support = test_lpm_enabled,
  88084. +#endif
  88085. + // current versions must always be self-powered
  88086. +};
  88087. +
  88088. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  88089. +{
  88090. + int retval = -DWC_E_NOT_SUPPORTED;
  88091. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  88092. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  88093. + (struct usb_ctrlrequest
  88094. + *)bytes);
  88095. + }
  88096. +
  88097. + if (retval == -ENOTSUPP) {
  88098. + retval = -DWC_E_NOT_SUPPORTED;
  88099. + } else if (retval < 0) {
  88100. + retval = -DWC_E_INVALID;
  88101. + }
  88102. +
  88103. + return retval;
  88104. +}
  88105. +
  88106. +#ifdef DWC_EN_ISOC
  88107. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88108. + void *req_handle, int proc_buf_num)
  88109. +{
  88110. + int i, packet_count;
  88111. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  88112. + struct usb_iso_request *iso_req = req_handle;
  88113. +
  88114. + if (proc_buf_num) {
  88115. + iso_packet = iso_req->iso_packet_desc1;
  88116. + } else {
  88117. + iso_packet = iso_req->iso_packet_desc0;
  88118. + }
  88119. + packet_count =
  88120. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  88121. + for (i = 0; i < packet_count; ++i) {
  88122. + int status;
  88123. + int actual;
  88124. + int offset;
  88125. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  88126. + i, &status, &actual, &offset);
  88127. + switch (status) {
  88128. + case -DWC_E_NO_DATA:
  88129. + status = -ENODATA;
  88130. + break;
  88131. + default:
  88132. + if (status) {
  88133. + DWC_PRINTF("unknown status in isoc packet\n");
  88134. + }
  88135. +
  88136. + }
  88137. + iso_packet[i].status = status;
  88138. + iso_packet[i].offset = offset;
  88139. + iso_packet[i].actual_length = actual;
  88140. + }
  88141. +
  88142. + iso_req->status = 0;
  88143. + iso_req->process_buffer(ep_handle, iso_req);
  88144. +
  88145. + return 0;
  88146. +}
  88147. +#endif /* DWC_EN_ISOC */
  88148. +
  88149. +#ifdef DWC_UTE_PER_IO
  88150. +/**
  88151. + * Copy the contents of the extended request to the Linux usb_request's
  88152. + * extended part and call the gadget's completion.
  88153. + *
  88154. + * @param pcd Pointer to the pcd structure
  88155. + * @param ep_handle Void pointer to the usb_ep structure
  88156. + * @param req_handle Void pointer to the usb_request structure
  88157. + * @param status Request status returned from the portable logic
  88158. + * @param ereq_port Void pointer to the extended request structure
  88159. + * created in the the portable part that contains the
  88160. + * results of the processed iso packets.
  88161. + */
  88162. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88163. + void *req_handle, int32_t status, void *ereq_port)
  88164. +{
  88165. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  88166. + struct dwc_iso_xreq_port *ereqport = NULL;
  88167. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  88168. + int i;
  88169. + struct usb_request *req;
  88170. + //struct dwc_ute_iso_packet_descriptor *
  88171. + //int status = 0;
  88172. +
  88173. + req = (struct usb_request *)req_handle;
  88174. + ereqorg = &req->ext_req;
  88175. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  88176. + desc_org = ereqorg->per_io_frame_descs;
  88177. +
  88178. + if (req && req->complete) {
  88179. + /* Copy the request data from the portable logic to our request */
  88180. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  88181. + desc_org[i].actual_length =
  88182. + ereqport->per_io_frame_descs[i].actual_length;
  88183. + desc_org[i].status =
  88184. + ereqport->per_io_frame_descs[i].status;
  88185. + }
  88186. +
  88187. + switch (status) {
  88188. + case -DWC_E_SHUTDOWN:
  88189. + req->status = -ESHUTDOWN;
  88190. + break;
  88191. + case -DWC_E_RESTART:
  88192. + req->status = -ECONNRESET;
  88193. + break;
  88194. + case -DWC_E_INVALID:
  88195. + req->status = -EINVAL;
  88196. + break;
  88197. + case -DWC_E_TIMEOUT:
  88198. + req->status = -ETIMEDOUT;
  88199. + break;
  88200. + default:
  88201. + req->status = status;
  88202. + }
  88203. +
  88204. + /* And call the gadget's completion */
  88205. + req->complete(ep_handle, req);
  88206. + }
  88207. +
  88208. + return 0;
  88209. +}
  88210. +#endif /* DWC_UTE_PER_IO */
  88211. +
  88212. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  88213. + void *req_handle, int32_t status, uint32_t actual)
  88214. +{
  88215. + struct usb_request *req = (struct usb_request *)req_handle;
  88216. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  88217. + struct dwc_otg_pcd_ep *ep = NULL;
  88218. +#endif
  88219. +
  88220. + if (req && req->complete) {
  88221. + switch (status) {
  88222. + case -DWC_E_SHUTDOWN:
  88223. + req->status = -ESHUTDOWN;
  88224. + break;
  88225. + case -DWC_E_RESTART:
  88226. + req->status = -ECONNRESET;
  88227. + break;
  88228. + case -DWC_E_INVALID:
  88229. + req->status = -EINVAL;
  88230. + break;
  88231. + case -DWC_E_TIMEOUT:
  88232. + req->status = -ETIMEDOUT;
  88233. + break;
  88234. + default:
  88235. + req->status = status;
  88236. +
  88237. + }
  88238. +
  88239. + req->actual = actual;
  88240. + DWC_SPINUNLOCK(pcd->lock);
  88241. + req->complete(ep_handle, req);
  88242. + DWC_SPINLOCK(pcd->lock);
  88243. + }
  88244. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  88245. + ep = ep_from_handle(pcd, ep_handle);
  88246. + if (GET_CORE_IF(pcd)->dma_enable) {
  88247. + if (req->length != 0) {
  88248. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  88249. + struct device *dev = NULL;
  88250. +
  88251. + if (otg_dev != NULL)
  88252. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  88253. +
  88254. + dma_unmap_single(dev, req->dma, req->length,
  88255. + ep->dwc_ep.is_in ?
  88256. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  88257. + }
  88258. + }
  88259. +#endif
  88260. +
  88261. + return 0;
  88262. +}
  88263. +
  88264. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  88265. +{
  88266. + gadget_wrapper->gadget.speed = speed;
  88267. + return 0;
  88268. +}
  88269. +
  88270. +static int _disconnect(dwc_otg_pcd_t * pcd)
  88271. +{
  88272. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  88273. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  88274. + }
  88275. + return 0;
  88276. +}
  88277. +
  88278. +static int _resume(dwc_otg_pcd_t * pcd)
  88279. +{
  88280. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  88281. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  88282. + }
  88283. +
  88284. + return 0;
  88285. +}
  88286. +
  88287. +static int _suspend(dwc_otg_pcd_t * pcd)
  88288. +{
  88289. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  88290. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  88291. + }
  88292. + return 0;
  88293. +}
  88294. +
  88295. +/**
  88296. + * This function updates the otg values in the gadget structure.
  88297. + */
  88298. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  88299. +{
  88300. +
  88301. + if (!gadget_wrapper->gadget.is_otg)
  88302. + return 0;
  88303. +
  88304. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  88305. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  88306. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  88307. + return 0;
  88308. +}
  88309. +
  88310. +static int _reset(dwc_otg_pcd_t * pcd)
  88311. +{
  88312. + return 0;
  88313. +}
  88314. +
  88315. +#ifdef DWC_UTE_CFI
  88316. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  88317. +{
  88318. + int retval = -DWC_E_INVALID;
  88319. + if (gadget_wrapper->driver->cfi_feature_setup) {
  88320. + retval =
  88321. + gadget_wrapper->driver->
  88322. + cfi_feature_setup(&gadget_wrapper->gadget,
  88323. + (struct cfi_usb_ctrlrequest *)cfi_req);
  88324. + }
  88325. +
  88326. + return retval;
  88327. +}
  88328. +#endif
  88329. +
  88330. +static const struct dwc_otg_pcd_function_ops fops = {
  88331. + .complete = _complete,
  88332. +#ifdef DWC_EN_ISOC
  88333. + .isoc_complete = _isoc_complete,
  88334. +#endif
  88335. + .setup = _setup,
  88336. + .disconnect = _disconnect,
  88337. + .connect = _connect,
  88338. + .resume = _resume,
  88339. + .suspend = _suspend,
  88340. + .hnp_changed = _hnp_changed,
  88341. + .reset = _reset,
  88342. +#ifdef DWC_UTE_CFI
  88343. + .cfi_setup = _cfi_setup,
  88344. +#endif
  88345. +#ifdef DWC_UTE_PER_IO
  88346. + .xisoc_complete = _xisoc_complete,
  88347. +#endif
  88348. +};
  88349. +
  88350. +/**
  88351. + * This function is the top level PCD interrupt handler.
  88352. + */
  88353. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  88354. +{
  88355. + dwc_otg_pcd_t *pcd = dev;
  88356. + int32_t retval = IRQ_NONE;
  88357. +
  88358. + retval = dwc_otg_pcd_handle_intr(pcd);
  88359. + if (retval != 0) {
  88360. + S3C2410X_CLEAR_EINTPEND();
  88361. + }
  88362. + return IRQ_RETVAL(retval);
  88363. +}
  88364. +
  88365. +/**
  88366. + * This function initialized the usb_ep structures to there default
  88367. + * state.
  88368. + *
  88369. + * @param d Pointer on gadget_wrapper.
  88370. + */
  88371. +void gadget_add_eps(struct gadget_wrapper *d)
  88372. +{
  88373. + static const char *names[] = {
  88374. +
  88375. + "ep0",
  88376. + "ep1in",
  88377. + "ep2in",
  88378. + "ep3in",
  88379. + "ep4in",
  88380. + "ep5in",
  88381. + "ep6in",
  88382. + "ep7in",
  88383. + "ep8in",
  88384. + "ep9in",
  88385. + "ep10in",
  88386. + "ep11in",
  88387. + "ep12in",
  88388. + "ep13in",
  88389. + "ep14in",
  88390. + "ep15in",
  88391. + "ep1out",
  88392. + "ep2out",
  88393. + "ep3out",
  88394. + "ep4out",
  88395. + "ep5out",
  88396. + "ep6out",
  88397. + "ep7out",
  88398. + "ep8out",
  88399. + "ep9out",
  88400. + "ep10out",
  88401. + "ep11out",
  88402. + "ep12out",
  88403. + "ep13out",
  88404. + "ep14out",
  88405. + "ep15out"
  88406. + };
  88407. +
  88408. + int i;
  88409. + struct usb_ep *ep;
  88410. + int8_t dev_endpoints;
  88411. +
  88412. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  88413. +
  88414. + INIT_LIST_HEAD(&d->gadget.ep_list);
  88415. + d->gadget.ep0 = &d->ep0;
  88416. + d->gadget.speed = USB_SPEED_UNKNOWN;
  88417. +
  88418. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  88419. +
  88420. + /**
  88421. + * Initialize the EP0 structure.
  88422. + */
  88423. + ep = &d->ep0;
  88424. +
  88425. + /* Init the usb_ep structure. */
  88426. + ep->name = names[0];
  88427. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  88428. +
  88429. + /**
  88430. + * @todo NGS: What should the max packet size be set to
  88431. + * here? Before EP type is set?
  88432. + */
  88433. + ep->maxpacket = MAX_PACKET_SIZE;
  88434. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  88435. +
  88436. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  88437. +
  88438. + /**
  88439. + * Initialize the EP structures.
  88440. + */
  88441. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  88442. +
  88443. + for (i = 0; i < dev_endpoints; i++) {
  88444. + ep = &d->in_ep[i];
  88445. +
  88446. + /* Init the usb_ep structure. */
  88447. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  88448. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  88449. +
  88450. + /**
  88451. + * @todo NGS: What should the max packet size be set to
  88452. + * here? Before EP type is set?
  88453. + */
  88454. + ep->maxpacket = MAX_PACKET_SIZE;
  88455. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  88456. + }
  88457. +
  88458. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  88459. +
  88460. + for (i = 0; i < dev_endpoints; i++) {
  88461. + ep = &d->out_ep[i];
  88462. +
  88463. + /* Init the usb_ep structure. */
  88464. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  88465. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  88466. +
  88467. + /**
  88468. + * @todo NGS: What should the max packet size be set to
  88469. + * here? Before EP type is set?
  88470. + */
  88471. + ep->maxpacket = MAX_PACKET_SIZE;
  88472. +
  88473. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  88474. + }
  88475. +
  88476. + /* remove ep0 from the list. There is a ep0 pointer. */
  88477. + list_del_init(&d->ep0.ep_list);
  88478. +
  88479. + d->ep0.maxpacket = MAX_EP0_SIZE;
  88480. +}
  88481. +
  88482. +/**
  88483. + * This function releases the Gadget device.
  88484. + * required by device_unregister().
  88485. + *
  88486. + * @todo Should this do something? Should it free the PCD?
  88487. + */
  88488. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  88489. +{
  88490. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  88491. +}
  88492. +
  88493. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  88494. +{
  88495. + static char pcd_name[] = "dwc_otg_pcd";
  88496. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  88497. + struct gadget_wrapper *d;
  88498. + int retval;
  88499. +
  88500. + d = DWC_ALLOC(sizeof(*d));
  88501. + if (d == NULL) {
  88502. + return NULL;
  88503. + }
  88504. +
  88505. + memset(d, 0, sizeof(*d));
  88506. +
  88507. + d->gadget.name = pcd_name;
  88508. + d->pcd = otg_dev->pcd;
  88509. +
  88510. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  88511. + strcpy(d->gadget.dev.bus_id, "gadget");
  88512. +#else
  88513. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  88514. +#endif
  88515. +
  88516. + d->gadget.dev.parent = &_dev->dev;
  88517. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  88518. + d->gadget.ops = &dwc_otg_pcd_ops;
  88519. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  88520. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  88521. +
  88522. + d->driver = 0;
  88523. + /* Register the gadget device */
  88524. + retval = device_register(&d->gadget.dev);
  88525. + if (retval != 0) {
  88526. + DWC_ERROR("device_register failed\n");
  88527. + DWC_FREE(d);
  88528. + return NULL;
  88529. + }
  88530. +
  88531. + return d;
  88532. +}
  88533. +
  88534. +static void free_wrapper(struct gadget_wrapper *d)
  88535. +{
  88536. + if (d->driver) {
  88537. + /* should have been done already by driver model core */
  88538. + DWC_WARN("driver '%s' is still registered\n",
  88539. + d->driver->driver.name);
  88540. + usb_gadget_unregister_driver(d->driver);
  88541. + }
  88542. +
  88543. + device_unregister(&d->gadget.dev);
  88544. + DWC_FREE(d);
  88545. +}
  88546. +
  88547. +/**
  88548. + * This function initialized the PCD portion of the driver.
  88549. + *
  88550. + */
  88551. +int pcd_init(dwc_bus_dev_t *_dev)
  88552. +{
  88553. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  88554. + int retval = 0;
  88555. +
  88556. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  88557. +
  88558. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev->core_if);
  88559. +
  88560. + if (!otg_dev->pcd) {
  88561. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  88562. + return -ENOMEM;
  88563. + }
  88564. +
  88565. + otg_dev->pcd->otg_dev = otg_dev;
  88566. + gadget_wrapper = alloc_wrapper(_dev);
  88567. +
  88568. + /*
  88569. + * Initialize EP structures
  88570. + */
  88571. + gadget_add_eps(gadget_wrapper);
  88572. + /*
  88573. + * Setup interupt handler
  88574. + */
  88575. +#ifdef PLATFORM_INTERFACE
  88576. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  88577. + platform_get_irq(_dev, 0));
  88578. + retval = request_irq(platform_get_irq(_dev, 0), dwc_otg_pcd_irq,
  88579. + IRQF_SHARED, gadget_wrapper->gadget.name,
  88580. + otg_dev->pcd);
  88581. + if (retval != 0) {
  88582. + DWC_ERROR("request of irq%d failed\n",
  88583. + platform_get_irq(_dev, 0));
  88584. + free_wrapper(gadget_wrapper);
  88585. + return -EBUSY;
  88586. + }
  88587. +#else
  88588. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  88589. + _dev->irq);
  88590. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  88591. + IRQF_SHARED | IRQF_DISABLED,
  88592. + gadget_wrapper->gadget.name, otg_dev->pcd);
  88593. + if (retval != 0) {
  88594. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  88595. + free_wrapper(gadget_wrapper);
  88596. + return -EBUSY;
  88597. + }
  88598. +#endif
  88599. +
  88600. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  88601. +
  88602. + return retval;
  88603. +}
  88604. +
  88605. +/**
  88606. + * Cleanup the PCD.
  88607. + */
  88608. +void pcd_remove(dwc_bus_dev_t *_dev)
  88609. +{
  88610. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  88611. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  88612. +
  88613. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  88614. +
  88615. + /*
  88616. + * Free the IRQ
  88617. + */
  88618. +#ifdef PLATFORM_INTERFACE
  88619. + free_irq(platform_get_irq(_dev, 0), pcd);
  88620. +#else
  88621. + free_irq(_dev->irq, pcd);
  88622. +#endif
  88623. + dwc_otg_pcd_remove(otg_dev->pcd);
  88624. + free_wrapper(gadget_wrapper);
  88625. + otg_dev->pcd = 0;
  88626. +}
  88627. +
  88628. +/**
  88629. + * This function registers a gadget driver with the PCD.
  88630. + *
  88631. + * When a driver is successfully registered, it will receive control
  88632. + * requests including set_configuration(), which enables non-control
  88633. + * requests. then usb traffic follows until a disconnect is reported.
  88634. + * then a host may connect again, or the driver might get unbound.
  88635. + *
  88636. + * @param driver The driver being registered
  88637. + * @param bind The bind function of gadget driver
  88638. + */
  88639. +
  88640. +int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
  88641. +{
  88642. + int retval;
  88643. +
  88644. + DWC_DEBUGPL(DBG_PCD, "registering gadget driver '%s'\n",
  88645. + driver->driver.name);
  88646. +
  88647. + if (!driver || driver->max_speed == USB_SPEED_UNKNOWN ||
  88648. + !driver->bind ||
  88649. + !driver->unbind || !driver->disconnect || !driver->setup) {
  88650. + DWC_DEBUGPL(DBG_PCDV, "EINVAL\n");
  88651. + return -EINVAL;
  88652. + }
  88653. + if (gadget_wrapper == 0) {
  88654. + DWC_DEBUGPL(DBG_PCDV, "ENODEV\n");
  88655. + return -ENODEV;
  88656. + }
  88657. + if (gadget_wrapper->driver != 0) {
  88658. + DWC_DEBUGPL(DBG_PCDV, "EBUSY (%p)\n", gadget_wrapper->driver);
  88659. + return -EBUSY;
  88660. + }
  88661. +
  88662. + /* hook up the driver */
  88663. + gadget_wrapper->driver = driver;
  88664. + gadget_wrapper->gadget.dev.driver = &driver->driver;
  88665. +
  88666. + DWC_DEBUGPL(DBG_PCD, "bind to driver %s\n", driver->driver.name);
  88667. + retval = driver->bind(&gadget_wrapper->gadget, gadget_wrapper->driver);
  88668. + if (retval) {
  88669. + DWC_ERROR("bind to driver %s --> error %d\n",
  88670. + driver->driver.name, retval);
  88671. + gadget_wrapper->driver = 0;
  88672. + gadget_wrapper->gadget.dev.driver = 0;
  88673. + return retval;
  88674. + }
  88675. + DWC_DEBUGPL(DBG_ANY, "registered gadget driver '%s'\n",
  88676. + driver->driver.name);
  88677. + return 0;
  88678. +}
  88679. +EXPORT_SYMBOL(usb_gadget_probe_driver);
  88680. +
  88681. +/**
  88682. + * This function unregisters a gadget driver
  88683. + *
  88684. + * @param driver The driver being unregistered
  88685. + */
  88686. +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  88687. +{
  88688. + //DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _driver);
  88689. +
  88690. + if (gadget_wrapper == 0) {
  88691. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): s_pcd==0\n", __func__,
  88692. + -ENODEV);
  88693. + return -ENODEV;
  88694. + }
  88695. + if (driver == 0 || driver != gadget_wrapper->driver) {
  88696. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): driver?\n", __func__,
  88697. + -EINVAL);
  88698. + return -EINVAL;
  88699. + }
  88700. +
  88701. + driver->unbind(&gadget_wrapper->gadget);
  88702. + gadget_wrapper->driver = 0;
  88703. +
  88704. + DWC_DEBUGPL(DBG_ANY, "unregistered driver '%s'\n", driver->driver.name);
  88705. + return 0;
  88706. +}
  88707. +
  88708. +EXPORT_SYMBOL(usb_gadget_unregister_driver);
  88709. +
  88710. +#endif /* DWC_HOST_ONLY */
  88711. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_regs.h linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  88712. --- linux-3.13.6/drivers/usb/host/dwc_otg/dwc_otg_regs.h 1970-01-01 01:00:00.000000000 +0100
  88713. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/dwc_otg_regs.h 2014-03-11 16:55:38.000000000 +0100
  88714. @@ -0,0 +1,2550 @@
  88715. +/* ==========================================================================
  88716. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  88717. + * $Revision: #98 $
  88718. + * $Date: 2012/08/10 $
  88719. + * $Change: 2047372 $
  88720. + *
  88721. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  88722. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  88723. + * otherwise expressly agreed to in writing between Synopsys and you.
  88724. + *
  88725. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  88726. + * any End User Software License Agreement or Agreement for Licensed Product
  88727. + * with Synopsys or any supplement thereto. You are permitted to use and
  88728. + * redistribute this Software in source and binary forms, with or without
  88729. + * modification, provided that redistributions of source code must retain this
  88730. + * notice. You may not view, use, disclose, copy or distribute this file or
  88731. + * any information contained herein except pursuant to this license grant from
  88732. + * Synopsys. If you do not agree with this notice, including the disclaimer
  88733. + * below, then you are not authorized to use the Software.
  88734. + *
  88735. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  88736. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  88737. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  88738. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  88739. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  88740. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  88741. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  88742. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  88743. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  88744. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  88745. + * DAMAGE.
  88746. + * ========================================================================== */
  88747. +
  88748. +#ifndef __DWC_OTG_REGS_H__
  88749. +#define __DWC_OTG_REGS_H__
  88750. +
  88751. +#include "dwc_otg_core_if.h"
  88752. +
  88753. +/**
  88754. + * @file
  88755. + *
  88756. + * This file contains the data structures for accessing the DWC_otg core registers.
  88757. + *
  88758. + * The application interfaces with the HS OTG core by reading from and
  88759. + * writing to the Control and Status Register (CSR) space through the
  88760. + * AHB Slave interface. These registers are 32 bits wide, and the
  88761. + * addresses are 32-bit-block aligned.
  88762. + * CSRs are classified as follows:
  88763. + * - Core Global Registers
  88764. + * - Device Mode Registers
  88765. + * - Device Global Registers
  88766. + * - Device Endpoint Specific Registers
  88767. + * - Host Mode Registers
  88768. + * - Host Global Registers
  88769. + * - Host Port CSRs
  88770. + * - Host Channel Specific Registers
  88771. + *
  88772. + * Only the Core Global registers can be accessed in both Device and
  88773. + * Host modes. When the HS OTG core is operating in one mode, either
  88774. + * Device or Host, the application must not access registers from the
  88775. + * other mode. When the core switches from one mode to another, the
  88776. + * registers in the new mode of operation must be reprogrammed as they
  88777. + * would be after a power-on reset.
  88778. + */
  88779. +
  88780. +/****************************************************************************/
  88781. +/** DWC_otg Core registers .
  88782. + * The dwc_otg_core_global_regs structure defines the size
  88783. + * and relative field offsets for the Core Global registers.
  88784. + */
  88785. +typedef struct dwc_otg_core_global_regs {
  88786. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  88787. + volatile uint32_t gotgctl;
  88788. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  88789. + volatile uint32_t gotgint;
  88790. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  88791. + volatile uint32_t gahbcfg;
  88792. +
  88793. +#define DWC_GLBINTRMASK 0x0001
  88794. +#define DWC_DMAENABLE 0x0020
  88795. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  88796. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  88797. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  88798. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  88799. +
  88800. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  88801. + volatile uint32_t gusbcfg;
  88802. + /**Core Reset Register. <i>Offset: 010h</i> */
  88803. + volatile uint32_t grstctl;
  88804. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  88805. + volatile uint32_t gintsts;
  88806. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  88807. + volatile uint32_t gintmsk;
  88808. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  88809. + volatile uint32_t grxstsr;
  88810. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  88811. + volatile uint32_t grxstsp;
  88812. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  88813. + volatile uint32_t grxfsiz;
  88814. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  88815. + volatile uint32_t gnptxfsiz;
  88816. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  88817. + * Only). <i>Offset: 02Ch</i> */
  88818. + volatile uint32_t gnptxsts;
  88819. + /**I2C Access Register. <i>Offset: 030h</i> */
  88820. + volatile uint32_t gi2cctl;
  88821. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  88822. + volatile uint32_t gpvndctl;
  88823. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  88824. + volatile uint32_t ggpio;
  88825. + /**User ID Register. <i>Offset: 03Ch</i> */
  88826. + volatile uint32_t guid;
  88827. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  88828. + volatile uint32_t gsnpsid;
  88829. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  88830. + volatile uint32_t ghwcfg1;
  88831. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  88832. + volatile uint32_t ghwcfg2;
  88833. +#define DWC_SLAVE_ONLY_ARCH 0
  88834. +#define DWC_EXT_DMA_ARCH 1
  88835. +#define DWC_INT_DMA_ARCH 2
  88836. +
  88837. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  88838. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  88839. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  88840. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  88841. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  88842. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  88843. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  88844. +
  88845. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  88846. + volatile uint32_t ghwcfg3;
  88847. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  88848. + volatile uint32_t ghwcfg4;
  88849. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  88850. + volatile uint32_t glpmcfg;
  88851. + /** Global PowerDn Register <i>Offset: 058h</i> */
  88852. + volatile uint32_t gpwrdn;
  88853. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  88854. + volatile uint32_t gdfifocfg;
  88855. + /** ADP Control Register <i>Offset: 060h</i> */
  88856. + volatile uint32_t adpctl;
  88857. + /** Reserved <i>Offset: 064h-0FFh</i> */
  88858. + volatile uint32_t reserved39[39];
  88859. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  88860. + volatile uint32_t hptxfsiz;
  88861. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  88862. + otherwise Device Transmit FIFO#n Register.
  88863. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  88864. + volatile uint32_t dtxfsiz[15];
  88865. +} dwc_otg_core_global_regs_t;
  88866. +
  88867. +/**
  88868. + * This union represents the bit fields of the Core OTG Control
  88869. + * and Status Register (GOTGCTL). Set the bits using the bit
  88870. + * fields then write the <i>d32</i> value to the register.
  88871. + */
  88872. +typedef union gotgctl_data {
  88873. + /** raw register data */
  88874. + uint32_t d32;
  88875. + /** register bits */
  88876. + struct {
  88877. + unsigned sesreqscs:1;
  88878. + unsigned sesreq:1;
  88879. + unsigned vbvalidoven:1;
  88880. + unsigned vbvalidovval:1;
  88881. + unsigned avalidoven:1;
  88882. + unsigned avalidovval:1;
  88883. + unsigned bvalidoven:1;
  88884. + unsigned bvalidovval:1;
  88885. + unsigned hstnegscs:1;
  88886. + unsigned hnpreq:1;
  88887. + unsigned hstsethnpen:1;
  88888. + unsigned devhnpen:1;
  88889. + unsigned reserved12_15:4;
  88890. + unsigned conidsts:1;
  88891. + unsigned dbnctime:1;
  88892. + unsigned asesvld:1;
  88893. + unsigned bsesvld:1;
  88894. + unsigned otgver:1;
  88895. + unsigned reserved1:1;
  88896. + unsigned multvalidbc:5;
  88897. + unsigned chirpen:1;
  88898. + unsigned reserved28_31:4;
  88899. + } b;
  88900. +} gotgctl_data_t;
  88901. +
  88902. +/**
  88903. + * This union represents the bit fields of the Core OTG Interrupt Register
  88904. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  88905. + * value to the register.
  88906. + */
  88907. +typedef union gotgint_data {
  88908. + /** raw register data */
  88909. + uint32_t d32;
  88910. + /** register bits */
  88911. + struct {
  88912. + /** Current Mode */
  88913. + unsigned reserved0_1:2;
  88914. +
  88915. + /** Session End Detected */
  88916. + unsigned sesenddet:1;
  88917. +
  88918. + unsigned reserved3_7:5;
  88919. +
  88920. + /** Session Request Success Status Change */
  88921. + unsigned sesreqsucstschng:1;
  88922. + /** Host Negotiation Success Status Change */
  88923. + unsigned hstnegsucstschng:1;
  88924. +
  88925. + unsigned reserved10_16:7;
  88926. +
  88927. + /** Host Negotiation Detected */
  88928. + unsigned hstnegdet:1;
  88929. + /** A-Device Timeout Change */
  88930. + unsigned adevtoutchng:1;
  88931. + /** Debounce Done */
  88932. + unsigned debdone:1;
  88933. + /** Multi-Valued input changed */
  88934. + unsigned mvic:1;
  88935. +
  88936. + unsigned reserved31_21:11;
  88937. +
  88938. + } b;
  88939. +} gotgint_data_t;
  88940. +
  88941. +/**
  88942. + * This union represents the bit fields of the Core AHB Configuration
  88943. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  88944. + * write the <i>d32</i> value to the register.
  88945. + */
  88946. +typedef union gahbcfg_data {
  88947. + /** raw register data */
  88948. + uint32_t d32;
  88949. + /** register bits */
  88950. + struct {
  88951. + unsigned glblintrmsk:1;
  88952. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  88953. +
  88954. + unsigned hburstlen:4;
  88955. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  88956. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  88957. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  88958. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  88959. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  88960. +
  88961. + unsigned dmaenable:1;
  88962. +#define DWC_GAHBCFG_DMAENABLE 1
  88963. + unsigned reserved:1;
  88964. + unsigned nptxfemplvl_txfemplvl:1;
  88965. + unsigned ptxfemplvl:1;
  88966. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  88967. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  88968. + unsigned reserved9_20:12;
  88969. + unsigned remmemsupp:1;
  88970. + unsigned notialldmawrit:1;
  88971. + unsigned ahbsingle:1;
  88972. + unsigned reserved24_31:8;
  88973. + } b;
  88974. +} gahbcfg_data_t;
  88975. +
  88976. +/**
  88977. + * This union represents the bit fields of the Core USB Configuration
  88978. + * Register (GUSBCFG). Set the bits using the bit fields then write
  88979. + * the <i>d32</i> value to the register.
  88980. + */
  88981. +typedef union gusbcfg_data {
  88982. + /** raw register data */
  88983. + uint32_t d32;
  88984. + /** register bits */
  88985. + struct {
  88986. + unsigned toutcal:3;
  88987. + unsigned phyif:1;
  88988. + unsigned ulpi_utmi_sel:1;
  88989. + unsigned fsintf:1;
  88990. + unsigned physel:1;
  88991. + unsigned ddrsel:1;
  88992. + unsigned srpcap:1;
  88993. + unsigned hnpcap:1;
  88994. + unsigned usbtrdtim:4;
  88995. + unsigned reserved1:1;
  88996. + unsigned phylpwrclksel:1;
  88997. + unsigned otgutmifssel:1;
  88998. + unsigned ulpi_fsls:1;
  88999. + unsigned ulpi_auto_res:1;
  89000. + unsigned ulpi_clk_sus_m:1;
  89001. + unsigned ulpi_ext_vbus_drv:1;
  89002. + unsigned ulpi_int_vbus_indicator:1;
  89003. + unsigned term_sel_dl_pulse:1;
  89004. + unsigned indicator_complement:1;
  89005. + unsigned indicator_pass_through:1;
  89006. + unsigned ulpi_int_prot_dis:1;
  89007. + unsigned ic_usb_cap:1;
  89008. + unsigned ic_traffic_pull_remove:1;
  89009. + unsigned tx_end_delay:1;
  89010. + unsigned force_host_mode:1;
  89011. + unsigned force_dev_mode:1;
  89012. + unsigned reserved31:1;
  89013. + } b;
  89014. +} gusbcfg_data_t;
  89015. +
  89016. +/**
  89017. + * This union represents the bit fields of the Core Reset Register
  89018. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  89019. + * <i>d32</i> value to the register.
  89020. + */
  89021. +typedef union grstctl_data {
  89022. + /** raw register data */
  89023. + uint32_t d32;
  89024. + /** register bits */
  89025. + struct {
  89026. + /** Core Soft Reset (CSftRst) (Device and Host)
  89027. + *
  89028. + * The application can flush the control logic in the
  89029. + * entire core using this bit. This bit resets the
  89030. + * pipelines in the AHB Clock domain as well as the
  89031. + * PHY Clock domain.
  89032. + *
  89033. + * The state machines are reset to an IDLE state, the
  89034. + * control bits in the CSRs are cleared, all the
  89035. + * transmit FIFOs and the receive FIFO are flushed.
  89036. + *
  89037. + * The status mask bits that control the generation of
  89038. + * the interrupt, are cleared, to clear the
  89039. + * interrupt. The interrupt status bits are not
  89040. + * cleared, so the application can get the status of
  89041. + * any events that occurred in the core after it has
  89042. + * set this bit.
  89043. + *
  89044. + * Any transactions on the AHB are terminated as soon
  89045. + * as possible following the protocol. Any
  89046. + * transactions on the USB are terminated immediately.
  89047. + *
  89048. + * The configuration settings in the CSRs are
  89049. + * unchanged, so the software doesn't have to
  89050. + * reprogram these registers (Device
  89051. + * Configuration/Host Configuration/Core System
  89052. + * Configuration/Core PHY Configuration).
  89053. + *
  89054. + * The application can write to this bit, any time it
  89055. + * wants to reset the core. This is a self clearing
  89056. + * bit and the core clears this bit after all the
  89057. + * necessary logic is reset in the core, which may
  89058. + * take several clocks, depending on the current state
  89059. + * of the core.
  89060. + */
  89061. + unsigned csftrst:1;
  89062. + /** Hclk Soft Reset
  89063. + *
  89064. + * The application uses this bit to reset the control logic in
  89065. + * the AHB clock domain. Only AHB clock domain pipelines are
  89066. + * reset.
  89067. + */
  89068. + unsigned hsftrst:1;
  89069. + /** Host Frame Counter Reset (Host Only)<br>
  89070. + *
  89071. + * The application can reset the (micro)frame number
  89072. + * counter inside the core, using this bit. When the
  89073. + * (micro)frame counter is reset, the subsequent SOF
  89074. + * sent out by the core, will have a (micro)frame
  89075. + * number of 0.
  89076. + */
  89077. + unsigned hstfrm:1;
  89078. + /** In Token Sequence Learning Queue Flush
  89079. + * (INTknQFlsh) (Device Only)
  89080. + */
  89081. + unsigned intknqflsh:1;
  89082. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  89083. + *
  89084. + * The application can flush the entire Receive FIFO
  89085. + * using this bit. The application must first
  89086. + * ensure that the core is not in the middle of a
  89087. + * transaction. The application should write into
  89088. + * this bit, only after making sure that neither the
  89089. + * DMA engine is reading from the RxFIFO nor the MAC
  89090. + * is writing the data in to the FIFO. The
  89091. + * application should wait until the bit is cleared
  89092. + * before performing any other operations. This bit
  89093. + * will takes 8 clocks (slowest of PHY or AHB clock)
  89094. + * to clear.
  89095. + */
  89096. + unsigned rxfflsh:1;
  89097. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  89098. + *
  89099. + * This bit is used to selectively flush a single or
  89100. + * all transmit FIFOs. The application must first
  89101. + * ensure that the core is not in the middle of a
  89102. + * transaction. The application should write into
  89103. + * this bit, only after making sure that neither the
  89104. + * DMA engine is writing into the TxFIFO nor the MAC
  89105. + * is reading the data out of the FIFO. The
  89106. + * application should wait until the core clears this
  89107. + * bit, before performing any operations. This bit
  89108. + * will takes 8 clocks (slowest of PHY or AHB clock)
  89109. + * to clear.
  89110. + */
  89111. + unsigned txfflsh:1;
  89112. +
  89113. + /** TxFIFO Number (TxFNum) (Device and Host).
  89114. + *
  89115. + * This is the FIFO number which needs to be flushed,
  89116. + * using the TxFIFO Flush bit. This field should not
  89117. + * be changed until the TxFIFO Flush bit is cleared by
  89118. + * the core.
  89119. + * - 0x0 : Non Periodic TxFIFO Flush
  89120. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  89121. + * or Periodic TxFIFO in host mode
  89122. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  89123. + * - ...
  89124. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  89125. + * - 0x10: Flush all the Transmit NonPeriodic and
  89126. + * Transmit Periodic FIFOs in the core
  89127. + */
  89128. + unsigned txfnum:5;
  89129. + /** Reserved */
  89130. + unsigned reserved11_29:19;
  89131. + /** DMA Request Signal. Indicated DMA request is in
  89132. + * probress. Used for debug purpose. */
  89133. + unsigned dmareq:1;
  89134. + /** AHB Master Idle. Indicates the AHB Master State
  89135. + * Machine is in IDLE condition. */
  89136. + unsigned ahbidle:1;
  89137. + } b;
  89138. +} grstctl_t;
  89139. +
  89140. +/**
  89141. + * This union represents the bit fields of the Core Interrupt Mask
  89142. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  89143. + * write the <i>d32</i> value to the register.
  89144. + */
  89145. +typedef union gintmsk_data {
  89146. + /** raw register data */
  89147. + uint32_t d32;
  89148. + /** register bits */
  89149. + struct {
  89150. + unsigned reserved0:1;
  89151. + unsigned modemismatch:1;
  89152. + unsigned otgintr:1;
  89153. + unsigned sofintr:1;
  89154. + unsigned rxstsqlvl:1;
  89155. + unsigned nptxfempty:1;
  89156. + unsigned ginnakeff:1;
  89157. + unsigned goutnakeff:1;
  89158. + unsigned ulpickint:1;
  89159. + unsigned i2cintr:1;
  89160. + unsigned erlysuspend:1;
  89161. + unsigned usbsuspend:1;
  89162. + unsigned usbreset:1;
  89163. + unsigned enumdone:1;
  89164. + unsigned isooutdrop:1;
  89165. + unsigned eopframe:1;
  89166. + unsigned restoredone:1;
  89167. + unsigned epmismatch:1;
  89168. + unsigned inepintr:1;
  89169. + unsigned outepintr:1;
  89170. + unsigned incomplisoin:1;
  89171. + unsigned incomplisoout:1;
  89172. + unsigned fetsusp:1;
  89173. + unsigned resetdet:1;
  89174. + unsigned portintr:1;
  89175. + unsigned hcintr:1;
  89176. + unsigned ptxfempty:1;
  89177. + unsigned lpmtranrcvd:1;
  89178. + unsigned conidstschng:1;
  89179. + unsigned disconnect:1;
  89180. + unsigned sessreqintr:1;
  89181. + unsigned wkupintr:1;
  89182. + } b;
  89183. +} gintmsk_data_t;
  89184. +/**
  89185. + * This union represents the bit fields of the Core Interrupt Register
  89186. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  89187. + * <i>d32</i> value to the register.
  89188. + */
  89189. +typedef union gintsts_data {
  89190. + /** raw register data */
  89191. + uint32_t d32;
  89192. +#define DWC_SOF_INTR_MASK 0x0008
  89193. + /** register bits */
  89194. + struct {
  89195. +#define DWC_HOST_MODE 1
  89196. + unsigned curmode:1;
  89197. + unsigned modemismatch:1;
  89198. + unsigned otgintr:1;
  89199. + unsigned sofintr:1;
  89200. + unsigned rxstsqlvl:1;
  89201. + unsigned nptxfempty:1;
  89202. + unsigned ginnakeff:1;
  89203. + unsigned goutnakeff:1;
  89204. + unsigned ulpickint:1;
  89205. + unsigned i2cintr:1;
  89206. + unsigned erlysuspend:1;
  89207. + unsigned usbsuspend:1;
  89208. + unsigned usbreset:1;
  89209. + unsigned enumdone:1;
  89210. + unsigned isooutdrop:1;
  89211. + unsigned eopframe:1;
  89212. + unsigned restoredone:1;
  89213. + unsigned epmismatch:1;
  89214. + unsigned inepint:1;
  89215. + unsigned outepintr:1;
  89216. + unsigned incomplisoin:1;
  89217. + unsigned incomplisoout:1;
  89218. + unsigned fetsusp:1;
  89219. + unsigned resetdet:1;
  89220. + unsigned portintr:1;
  89221. + unsigned hcintr:1;
  89222. + unsigned ptxfempty:1;
  89223. + unsigned lpmtranrcvd:1;
  89224. + unsigned conidstschng:1;
  89225. + unsigned disconnect:1;
  89226. + unsigned sessreqintr:1;
  89227. + unsigned wkupintr:1;
  89228. + } b;
  89229. +} gintsts_data_t;
  89230. +
  89231. +/**
  89232. + * This union represents the bit fields in the Device Receive Status Read and
  89233. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  89234. + * element then read out the bits using the <i>b</i>it elements.
  89235. + */
  89236. +typedef union device_grxsts_data {
  89237. + /** raw register data */
  89238. + uint32_t d32;
  89239. + /** register bits */
  89240. + struct {
  89241. + unsigned epnum:4;
  89242. + unsigned bcnt:11;
  89243. + unsigned dpid:2;
  89244. +
  89245. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  89246. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  89247. +
  89248. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  89249. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  89250. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  89251. + unsigned pktsts:4;
  89252. + unsigned fn:4;
  89253. + unsigned reserved25_31:7;
  89254. + } b;
  89255. +} device_grxsts_data_t;
  89256. +
  89257. +/**
  89258. + * This union represents the bit fields in the Host Receive Status Read and
  89259. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  89260. + * element then read out the bits using the <i>b</i>it elements.
  89261. + */
  89262. +typedef union host_grxsts_data {
  89263. + /** raw register data */
  89264. + uint32_t d32;
  89265. + /** register bits */
  89266. + struct {
  89267. + unsigned chnum:4;
  89268. + unsigned bcnt:11;
  89269. + unsigned dpid:2;
  89270. +
  89271. + unsigned pktsts:4;
  89272. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  89273. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  89274. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  89275. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  89276. +
  89277. + unsigned reserved21_31:11;
  89278. + } b;
  89279. +} host_grxsts_data_t;
  89280. +
  89281. +/**
  89282. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  89283. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  89284. + * then read out the bits using the <i>b</i>it elements.
  89285. + */
  89286. +typedef union fifosize_data {
  89287. + /** raw register data */
  89288. + uint32_t d32;
  89289. + /** register bits */
  89290. + struct {
  89291. + unsigned startaddr:16;
  89292. + unsigned depth:16;
  89293. + } b;
  89294. +} fifosize_data_t;
  89295. +
  89296. +/**
  89297. + * This union represents the bit fields in the Non-Periodic Transmit
  89298. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  89299. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  89300. + * elements.
  89301. + */
  89302. +typedef union gnptxsts_data {
  89303. + /** raw register data */
  89304. + uint32_t d32;
  89305. + /** register bits */
  89306. + struct {
  89307. + unsigned nptxfspcavail:16;
  89308. + unsigned nptxqspcavail:8;
  89309. + /** Top of the Non-Periodic Transmit Request Queue
  89310. + * - bit 24 - Terminate (Last entry for the selected
  89311. + * channel/EP)
  89312. + * - bits 26:25 - Token Type
  89313. + * - 2'b00 - IN/OUT
  89314. + * - 2'b01 - Zero Length OUT
  89315. + * - 2'b10 - PING/Complete Split
  89316. + * - 2'b11 - Channel Halt
  89317. + * - bits 30:27 - Channel/EP Number
  89318. + */
  89319. + unsigned nptxqtop_terminate:1;
  89320. + unsigned nptxqtop_token:2;
  89321. + unsigned nptxqtop_chnep:4;
  89322. + unsigned reserved:1;
  89323. + } b;
  89324. +} gnptxsts_data_t;
  89325. +
  89326. +/**
  89327. + * This union represents the bit fields in the Transmit
  89328. + * FIFO Status Register (DTXFSTS). Read the register into the
  89329. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  89330. + * elements.
  89331. + */
  89332. +typedef union dtxfsts_data {
  89333. + /** raw register data */
  89334. + uint32_t d32;
  89335. + /** register bits */
  89336. + struct {
  89337. + unsigned txfspcavail:16;
  89338. + unsigned reserved:16;
  89339. + } b;
  89340. +} dtxfsts_data_t;
  89341. +
  89342. +/**
  89343. + * This union represents the bit fields in the I2C Control Register
  89344. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  89345. + * bits using the <i>b</i>it elements.
  89346. + */
  89347. +typedef union gi2cctl_data {
  89348. + /** raw register data */
  89349. + uint32_t d32;
  89350. + /** register bits */
  89351. + struct {
  89352. + unsigned rwdata:8;
  89353. + unsigned regaddr:8;
  89354. + unsigned addr:7;
  89355. + unsigned i2cen:1;
  89356. + unsigned ack:1;
  89357. + unsigned i2csuspctl:1;
  89358. + unsigned i2cdevaddr:2;
  89359. + unsigned i2cdatse0:1;
  89360. + unsigned reserved:1;
  89361. + unsigned rw:1;
  89362. + unsigned bsydne:1;
  89363. + } b;
  89364. +} gi2cctl_data_t;
  89365. +
  89366. +/**
  89367. + * This union represents the bit fields in the PHY Vendor Control Register
  89368. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  89369. + * bits using the <i>b</i>it elements.
  89370. + */
  89371. +typedef union gpvndctl_data {
  89372. + /** raw register data */
  89373. + uint32_t d32;
  89374. + /** register bits */
  89375. + struct {
  89376. + unsigned regdata:8;
  89377. + unsigned vctrl:8;
  89378. + unsigned regaddr16_21:6;
  89379. + unsigned regwr:1;
  89380. + unsigned reserved23_24:2;
  89381. + unsigned newregreq:1;
  89382. + unsigned vstsbsy:1;
  89383. + unsigned vstsdone:1;
  89384. + unsigned reserved28_30:3;
  89385. + unsigned disulpidrvr:1;
  89386. + } b;
  89387. +} gpvndctl_data_t;
  89388. +
  89389. +/**
  89390. + * This union represents the bit fields in the General Purpose
  89391. + * Input/Output Register (GGPIO).
  89392. + * Read the register into the <i>d32</i> element then read out the
  89393. + * bits using the <i>b</i>it elements.
  89394. + */
  89395. +typedef union ggpio_data {
  89396. + /** raw register data */
  89397. + uint32_t d32;
  89398. + /** register bits */
  89399. + struct {
  89400. + unsigned gpi:16;
  89401. + unsigned gpo:16;
  89402. + } b;
  89403. +} ggpio_data_t;
  89404. +
  89405. +/**
  89406. + * This union represents the bit fields in the User ID Register
  89407. + * (GUID). Read the register into the <i>d32</i> element then read out the
  89408. + * bits using the <i>b</i>it elements.
  89409. + */
  89410. +typedef union guid_data {
  89411. + /** raw register data */
  89412. + uint32_t d32;
  89413. + /** register bits */
  89414. + struct {
  89415. + unsigned rwdata:32;
  89416. + } b;
  89417. +} guid_data_t;
  89418. +
  89419. +/**
  89420. + * This union represents the bit fields in the Synopsys ID Register
  89421. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  89422. + * bits using the <i>b</i>it elements.
  89423. + */
  89424. +typedef union gsnpsid_data {
  89425. + /** raw register data */
  89426. + uint32_t d32;
  89427. + /** register bits */
  89428. + struct {
  89429. + unsigned rwdata:32;
  89430. + } b;
  89431. +} gsnpsid_data_t;
  89432. +
  89433. +/**
  89434. + * This union represents the bit fields in the User HW Config1
  89435. + * Register. Read the register into the <i>d32</i> element then read
  89436. + * out the bits using the <i>b</i>it elements.
  89437. + */
  89438. +typedef union hwcfg1_data {
  89439. + /** raw register data */
  89440. + uint32_t d32;
  89441. + /** register bits */
  89442. + struct {
  89443. + unsigned ep_dir0:2;
  89444. + unsigned ep_dir1:2;
  89445. + unsigned ep_dir2:2;
  89446. + unsigned ep_dir3:2;
  89447. + unsigned ep_dir4:2;
  89448. + unsigned ep_dir5:2;
  89449. + unsigned ep_dir6:2;
  89450. + unsigned ep_dir7:2;
  89451. + unsigned ep_dir8:2;
  89452. + unsigned ep_dir9:2;
  89453. + unsigned ep_dir10:2;
  89454. + unsigned ep_dir11:2;
  89455. + unsigned ep_dir12:2;
  89456. + unsigned ep_dir13:2;
  89457. + unsigned ep_dir14:2;
  89458. + unsigned ep_dir15:2;
  89459. + } b;
  89460. +} hwcfg1_data_t;
  89461. +
  89462. +/**
  89463. + * This union represents the bit fields in the User HW Config2
  89464. + * Register. Read the register into the <i>d32</i> element then read
  89465. + * out the bits using the <i>b</i>it elements.
  89466. + */
  89467. +typedef union hwcfg2_data {
  89468. + /** raw register data */
  89469. + uint32_t d32;
  89470. + /** register bits */
  89471. + struct {
  89472. + /* GHWCFG2 */
  89473. + unsigned op_mode:3;
  89474. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  89475. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  89476. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  89477. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  89478. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  89479. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  89480. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  89481. +
  89482. + unsigned architecture:2;
  89483. + unsigned point2point:1;
  89484. + unsigned hs_phy_type:2;
  89485. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  89486. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  89487. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  89488. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  89489. +
  89490. + unsigned fs_phy_type:2;
  89491. + unsigned num_dev_ep:4;
  89492. + unsigned num_host_chan:4;
  89493. + unsigned perio_ep_supported:1;
  89494. + unsigned dynamic_fifo:1;
  89495. + unsigned multi_proc_int:1;
  89496. + unsigned reserved21:1;
  89497. + unsigned nonperio_tx_q_depth:2;
  89498. + unsigned host_perio_tx_q_depth:2;
  89499. + unsigned dev_token_q_depth:5;
  89500. + unsigned otg_enable_ic_usb:1;
  89501. + } b;
  89502. +} hwcfg2_data_t;
  89503. +
  89504. +/**
  89505. + * This union represents the bit fields in the User HW Config3
  89506. + * Register. Read the register into the <i>d32</i> element then read
  89507. + * out the bits using the <i>b</i>it elements.
  89508. + */
  89509. +typedef union hwcfg3_data {
  89510. + /** raw register data */
  89511. + uint32_t d32;
  89512. + /** register bits */
  89513. + struct {
  89514. + /* GHWCFG3 */
  89515. + unsigned xfer_size_cntr_width:4;
  89516. + unsigned packet_size_cntr_width:3;
  89517. + unsigned otg_func:1;
  89518. + unsigned i2c:1;
  89519. + unsigned vendor_ctrl_if:1;
  89520. + unsigned optional_features:1;
  89521. + unsigned synch_reset_type:1;
  89522. + unsigned adp_supp:1;
  89523. + unsigned otg_enable_hsic:1;
  89524. + unsigned bc_support:1;
  89525. + unsigned otg_lpm_en:1;
  89526. + unsigned dfifo_depth:16;
  89527. + } b;
  89528. +} hwcfg3_data_t;
  89529. +
  89530. +/**
  89531. + * This union represents the bit fields in the User HW Config4
  89532. + * Register. Read the register into the <i>d32</i> element then read
  89533. + * out the bits using the <i>b</i>it elements.
  89534. + */
  89535. +typedef union hwcfg4_data {
  89536. + /** raw register data */
  89537. + uint32_t d32;
  89538. + /** register bits */
  89539. + struct {
  89540. + unsigned num_dev_perio_in_ep:4;
  89541. + unsigned power_optimiz:1;
  89542. + unsigned min_ahb_freq:1;
  89543. + unsigned hiber:1;
  89544. + unsigned xhiber:1;
  89545. + unsigned reserved:6;
  89546. + unsigned utmi_phy_data_width:2;
  89547. + unsigned num_dev_mode_ctrl_ep:4;
  89548. + unsigned iddig_filt_en:1;
  89549. + unsigned vbus_valid_filt_en:1;
  89550. + unsigned a_valid_filt_en:1;
  89551. + unsigned b_valid_filt_en:1;
  89552. + unsigned session_end_filt_en:1;
  89553. + unsigned ded_fifo_en:1;
  89554. + unsigned num_in_eps:4;
  89555. + unsigned desc_dma:1;
  89556. + unsigned desc_dma_dyn:1;
  89557. + } b;
  89558. +} hwcfg4_data_t;
  89559. +
  89560. +/**
  89561. + * This union represents the bit fields of the Core LPM Configuration
  89562. + * Register (GLPMCFG). Set the bits using bit fields then write
  89563. + * the <i>d32</i> value to the register.
  89564. + */
  89565. +typedef union glpmctl_data {
  89566. + /** raw register data */
  89567. + uint32_t d32;
  89568. + /** register bits */
  89569. + struct {
  89570. + /** LPM-Capable (LPMCap) (Device and Host)
  89571. + * The application uses this bit to control
  89572. + * the DWC_otg core LPM capabilities.
  89573. + */
  89574. + unsigned lpm_cap_en:1;
  89575. + /** LPM response programmed by application (AppL1Res) (Device)
  89576. + * Handshake response to LPM token pre-programmed
  89577. + * by device application software.
  89578. + */
  89579. + unsigned appl_resp:1;
  89580. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  89581. + * In Host mode this field indicates the value of HIRD
  89582. + * to be sent in an LPM transaction.
  89583. + * In Device mode this field is updated with the
  89584. + * Received LPM Token HIRD bmAttribute
  89585. + * when an ACK/NYET/STALL response is sent
  89586. + * to an LPM transaction.
  89587. + */
  89588. + unsigned hird:4;
  89589. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  89590. + * In Host mode this bit indicates the value of remote
  89591. + * wake up to be sent in wIndex field of LPM transaction.
  89592. + * In Device mode this field is updated with the
  89593. + * Received LPM Token bRemoteWake bmAttribute
  89594. + * when an ACK/NYET/STALL response is sent
  89595. + * to an LPM transaction.
  89596. + */
  89597. + unsigned rem_wkup_en:1;
  89598. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  89599. + * The application uses this bit to control
  89600. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  89601. + */
  89602. + unsigned en_utmi_sleep:1;
  89603. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  89604. + */
  89605. + unsigned hird_thres:5;
  89606. + /** LPM Response (CoreL1Res) (Device and Host)
  89607. + * In Host mode this bit contains handsake response to
  89608. + * LPM transaction.
  89609. + * In Device mode the response of the core to
  89610. + * LPM transaction received is reflected in these two bits.
  89611. + - 0x0 : ERROR (No handshake response)
  89612. + - 0x1 : STALL
  89613. + - 0x2 : NYET
  89614. + - 0x3 : ACK
  89615. + */
  89616. + unsigned lpm_resp:2;
  89617. + /** Port Sleep Status (SlpSts) (Device and Host)
  89618. + * This bit is set as long as a Sleep condition
  89619. + * is present on the USB bus.
  89620. + */
  89621. + unsigned prt_sleep_sts:1;
  89622. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  89623. + * Indicates that the application or host
  89624. + * can start resume from Sleep state.
  89625. + */
  89626. + unsigned sleep_state_resumeok:1;
  89627. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  89628. + * The channel number on which the LPM transaction
  89629. + * has to be applied while sending
  89630. + * an LPM transaction to the local device.
  89631. + */
  89632. + unsigned lpm_chan_index:4;
  89633. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  89634. + * Number host retries that would be performed
  89635. + * if the device response was not valid response.
  89636. + */
  89637. + unsigned retry_count:3;
  89638. + /** Send LPM Transaction (SndLPM) (Host)
  89639. + * When set by application software,
  89640. + * an LPM transaction containing two tokens
  89641. + * is sent.
  89642. + */
  89643. + unsigned send_lpm:1;
  89644. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  89645. + * Number of LPM Host Retries still remaining
  89646. + * to be transmitted for the current LPM sequence
  89647. + */
  89648. + unsigned retry_count_sts:3;
  89649. + unsigned reserved28_29:2;
  89650. + /** In host mode once this bit is set, the host
  89651. + * configures to drive the HSIC Idle state on the bus.
  89652. + * It then waits for the device to initiate the Connect sequence.
  89653. + * In device mode once this bit is set, the device waits for
  89654. + * the HSIC Idle line state on the bus. Upon receving the Idle
  89655. + * line state, it initiates the HSIC Connect sequence.
  89656. + */
  89657. + unsigned hsic_connect:1;
  89658. + /** This bit overrides and functionally inverts
  89659. + * the if_select_hsic input port signal.
  89660. + */
  89661. + unsigned inv_sel_hsic:1;
  89662. + } b;
  89663. +} glpmcfg_data_t;
  89664. +
  89665. +/**
  89666. + * This union represents the bit fields of the Core ADP Timer, Control and
  89667. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  89668. + * the <i>d32</i> value to the register.
  89669. + */
  89670. +typedef union adpctl_data {
  89671. + /** raw register data */
  89672. + uint32_t d32;
  89673. + /** register bits */
  89674. + struct {
  89675. + /** Probe Discharge (PRB_DSCHG)
  89676. + * These bits set the times for TADP_DSCHG.
  89677. + * These bits are defined as follows:
  89678. + * 2'b00 - 4 msec
  89679. + * 2'b01 - 8 msec
  89680. + * 2'b10 - 16 msec
  89681. + * 2'b11 - 32 msec
  89682. + */
  89683. + unsigned prb_dschg:2;
  89684. + /** Probe Delta (PRB_DELTA)
  89685. + * These bits set the resolution for RTIM value.
  89686. + * The bits are defined in units of 32 kHz clock cycles as follows:
  89687. + * 2'b00 - 1 cycles
  89688. + * 2'b01 - 2 cycles
  89689. + * 2'b10 - 3 cycles
  89690. + * 2'b11 - 4 cycles
  89691. + * For example if this value is chosen to 2'b01, it means that RTIM
  89692. + * increments for every 3(three) 32Khz clock cycles.
  89693. + */
  89694. + unsigned prb_delta:2;
  89695. + /** Probe Period (PRB_PER)
  89696. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  89697. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  89698. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  89699. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  89700. + * 2'b11 - Reserved
  89701. + */
  89702. + unsigned prb_per:2;
  89703. + /** These bits capture the latest time it took for VBUS to ramp from
  89704. + * VADP_SINK to VADP_PRB.
  89705. + * 0x000 - 1 cycles
  89706. + * 0x001 - 2 cycles
  89707. + * 0x002 - 3 cycles
  89708. + * etc
  89709. + * 0x7FF - 2048 cycles
  89710. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  89711. + */
  89712. + unsigned rtim:11;
  89713. + /** Enable Probe (EnaPrb)
  89714. + * When programmed to 1'b1, the core performs a probe operation.
  89715. + * This bit is valid only if OTG_Ver = 1'b1.
  89716. + */
  89717. + unsigned enaprb:1;
  89718. + /** Enable Sense (EnaSns)
  89719. + * When programmed to 1'b1, the core performs a Sense operation.
  89720. + * This bit is valid only if OTG_Ver = 1'b1.
  89721. + */
  89722. + unsigned enasns:1;
  89723. + /** ADP Reset (ADPRes)
  89724. + * When set, ADP controller is reset.
  89725. + * This bit is valid only if OTG_Ver = 1'b1.
  89726. + */
  89727. + unsigned adpres:1;
  89728. + /** ADP Enable (ADPEn)
  89729. + * When set, the core performs either ADP probing or sensing
  89730. + * based on EnaPrb or EnaSns.
  89731. + * This bit is valid only if OTG_Ver = 1'b1.
  89732. + */
  89733. + unsigned adpen:1;
  89734. + /** ADP Probe Interrupt (ADP_PRB_INT)
  89735. + * When this bit is set, it means that the VBUS
  89736. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  89737. + * This bit is valid only if OTG_Ver = 1'b1.
  89738. + */
  89739. + unsigned adp_prb_int:1;
  89740. + /**
  89741. + * ADP Sense Interrupt (ADP_SNS_INT)
  89742. + * When this bit is set, it means that the VBUS voltage is greater than
  89743. + * VADP_SNS value or VADP_SNS is reached.
  89744. + * This bit is valid only if OTG_Ver = 1'b1.
  89745. + */
  89746. + unsigned adp_sns_int:1;
  89747. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  89748. + * This bit is relevant only for an ADP probe.
  89749. + * When this bit is set, it means that the ramp time has
  89750. + * completed ie ADPCTL.RTIM has reached its terminal value
  89751. + * of 0x7FF. This is a debug feature that allows software
  89752. + * to read the ramp time after each cycle.
  89753. + * This bit is valid only if OTG_Ver = 1'b1.
  89754. + */
  89755. + unsigned adp_tmout_int:1;
  89756. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  89757. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  89758. + * This bit is valid only if OTG_Ver = 1'b1.
  89759. + */
  89760. + unsigned adp_prb_int_msk:1;
  89761. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  89762. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  89763. + * This bit is valid only if OTG_Ver = 1'b1.
  89764. + */
  89765. + unsigned adp_sns_int_msk:1;
  89766. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  89767. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  89768. + * This bit is valid only if OTG_Ver = 1'b1.
  89769. + */
  89770. + unsigned adp_tmout_int_msk:1;
  89771. + /** Access Request
  89772. + * 2'b00 - Read/Write Valid (updated by the core)
  89773. + * 2'b01 - Read
  89774. + * 2'b00 - Write
  89775. + * 2'b00 - Reserved
  89776. + */
  89777. + unsigned ar:2;
  89778. + /** Reserved */
  89779. + unsigned reserved29_31:3;
  89780. + } b;
  89781. +} adpctl_data_t;
  89782. +
  89783. +////////////////////////////////////////////
  89784. +// Device Registers
  89785. +/**
  89786. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  89787. + *
  89788. + * The following structures define the size and relative field offsets
  89789. + * for the Device Mode Registers.
  89790. + *
  89791. + * <i>These registers are visible only in Device mode and must not be
  89792. + * accessed in Host mode, as the results are unknown.</i>
  89793. + */
  89794. +typedef struct dwc_otg_dev_global_regs {
  89795. + /** Device Configuration Register. <i>Offset 800h</i> */
  89796. + volatile uint32_t dcfg;
  89797. + /** Device Control Register. <i>Offset: 804h</i> */
  89798. + volatile uint32_t dctl;
  89799. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  89800. + volatile uint32_t dsts;
  89801. + /** Reserved. <i>Offset: 80Ch</i> */
  89802. + uint32_t unused;
  89803. + /** Device IN Endpoint Common Interrupt Mask
  89804. + * Register. <i>Offset: 810h</i> */
  89805. + volatile uint32_t diepmsk;
  89806. + /** Device OUT Endpoint Common Interrupt Mask
  89807. + * Register. <i>Offset: 814h</i> */
  89808. + volatile uint32_t doepmsk;
  89809. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  89810. + volatile uint32_t daint;
  89811. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  89812. + * 81Ch</i> */
  89813. + volatile uint32_t daintmsk;
  89814. + /** Device IN Token Queue Read Register-1 (Read Only).
  89815. + * <i>Offset: 820h</i> */
  89816. + volatile uint32_t dtknqr1;
  89817. + /** Device IN Token Queue Read Register-2 (Read Only).
  89818. + * <i>Offset: 824h</i> */
  89819. + volatile uint32_t dtknqr2;
  89820. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  89821. + volatile uint32_t dvbusdis;
  89822. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  89823. + volatile uint32_t dvbuspulse;
  89824. + /** Device IN Token Queue Read Register-3 (Read Only). /
  89825. + * Device Thresholding control register (Read/Write)
  89826. + * <i>Offset: 830h</i> */
  89827. + volatile uint32_t dtknqr3_dthrctl;
  89828. + /** Device IN Token Queue Read Register-4 (Read Only). /
  89829. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  89830. + * <i>Offset: 834h</i> */
  89831. + volatile uint32_t dtknqr4_fifoemptymsk;
  89832. + /** Device Each Endpoint Interrupt Register (Read Only). /
  89833. + * <i>Offset: 838h</i> */
  89834. + volatile uint32_t deachint;
  89835. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  89836. + * <i>Offset: 83Ch</i> */
  89837. + volatile uint32_t deachintmsk;
  89838. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  89839. + * <i>Offset: 840h</i> */
  89840. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  89841. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  89842. + * <i>Offset: 880h</i> */
  89843. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  89844. +} dwc_otg_device_global_regs_t;
  89845. +
  89846. +/**
  89847. + * This union represents the bit fields in the Device Configuration
  89848. + * Register. Read the register into the <i>d32</i> member then
  89849. + * set/clear the bits using the <i>b</i>it elements. Write the
  89850. + * <i>d32</i> member to the dcfg register.
  89851. + */
  89852. +typedef union dcfg_data {
  89853. + /** raw register data */
  89854. + uint32_t d32;
  89855. + /** register bits */
  89856. + struct {
  89857. + /** Device Speed */
  89858. + unsigned devspd:2;
  89859. + /** Non Zero Length Status OUT Handshake */
  89860. + unsigned nzstsouthshk:1;
  89861. +#define DWC_DCFG_SEND_STALL 1
  89862. +
  89863. + unsigned ena32khzs:1;
  89864. + /** Device Addresses */
  89865. + unsigned devaddr:7;
  89866. + /** Periodic Frame Interval */
  89867. + unsigned perfrint:2;
  89868. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  89869. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  89870. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  89871. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  89872. +
  89873. + /** Enable Device OUT NAK for bulk in DDMA mode */
  89874. + unsigned endevoutnak:1;
  89875. +
  89876. + unsigned reserved14_17:4;
  89877. + /** In Endpoint Mis-match count */
  89878. + unsigned epmscnt:5;
  89879. + /** Enable Descriptor DMA in Device mode */
  89880. + unsigned descdma:1;
  89881. + unsigned perschintvl:2;
  89882. + unsigned resvalid:6;
  89883. + } b;
  89884. +} dcfg_data_t;
  89885. +
  89886. +/**
  89887. + * This union represents the bit fields in the Device Control
  89888. + * Register. Read the register into the <i>d32</i> member then
  89889. + * set/clear the bits using the <i>b</i>it elements.
  89890. + */
  89891. +typedef union dctl_data {
  89892. + /** raw register data */
  89893. + uint32_t d32;
  89894. + /** register bits */
  89895. + struct {
  89896. + /** Remote Wakeup */
  89897. + unsigned rmtwkupsig:1;
  89898. + /** Soft Disconnect */
  89899. + unsigned sftdiscon:1;
  89900. + /** Global Non-Periodic IN NAK Status */
  89901. + unsigned gnpinnaksts:1;
  89902. + /** Global OUT NAK Status */
  89903. + unsigned goutnaksts:1;
  89904. + /** Test Control */
  89905. + unsigned tstctl:3;
  89906. + /** Set Global Non-Periodic IN NAK */
  89907. + unsigned sgnpinnak:1;
  89908. + /** Clear Global Non-Periodic IN NAK */
  89909. + unsigned cgnpinnak:1;
  89910. + /** Set Global OUT NAK */
  89911. + unsigned sgoutnak:1;
  89912. + /** Clear Global OUT NAK */
  89913. + unsigned cgoutnak:1;
  89914. + /** Power-On Programming Done */
  89915. + unsigned pwronprgdone:1;
  89916. + /** Reserved */
  89917. + unsigned reserved:1;
  89918. + /** Global Multi Count */
  89919. + unsigned gmc:2;
  89920. + /** Ignore Frame Number for ISOC EPs */
  89921. + unsigned ifrmnum:1;
  89922. + /** NAK on Babble */
  89923. + unsigned nakonbble:1;
  89924. + /** Enable Continue on BNA */
  89925. + unsigned encontonbna:1;
  89926. +
  89927. + unsigned reserved18_31:14;
  89928. + } b;
  89929. +} dctl_data_t;
  89930. +
  89931. +/**
  89932. + * This union represents the bit fields in the Device Status
  89933. + * Register. Read the register into the <i>d32</i> member then
  89934. + * set/clear the bits using the <i>b</i>it elements.
  89935. + */
  89936. +typedef union dsts_data {
  89937. + /** raw register data */
  89938. + uint32_t d32;
  89939. + /** register bits */
  89940. + struct {
  89941. + /** Suspend Status */
  89942. + unsigned suspsts:1;
  89943. + /** Enumerated Speed */
  89944. + unsigned enumspd:2;
  89945. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  89946. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  89947. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  89948. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  89949. + /** Erratic Error */
  89950. + unsigned errticerr:1;
  89951. + unsigned reserved4_7:4;
  89952. + /** Frame or Microframe Number of the received SOF */
  89953. + unsigned soffn:14;
  89954. + unsigned reserved22_31:10;
  89955. + } b;
  89956. +} dsts_data_t;
  89957. +
  89958. +/**
  89959. + * This union represents the bit fields in the Device IN EP Interrupt
  89960. + * Register and the Device IN EP Common Mask Register.
  89961. + *
  89962. + * - Read the register into the <i>d32</i> member then set/clear the
  89963. + * bits using the <i>b</i>it elements.
  89964. + */
  89965. +typedef union diepint_data {
  89966. + /** raw register data */
  89967. + uint32_t d32;
  89968. + /** register bits */
  89969. + struct {
  89970. + /** Transfer complete mask */
  89971. + unsigned xfercompl:1;
  89972. + /** Endpoint disable mask */
  89973. + unsigned epdisabled:1;
  89974. + /** AHB Error mask */
  89975. + unsigned ahberr:1;
  89976. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  89977. + unsigned timeout:1;
  89978. + /** IN Token received with TxF Empty mask */
  89979. + unsigned intktxfemp:1;
  89980. + /** IN Token Received with EP mismatch mask */
  89981. + unsigned intknepmis:1;
  89982. + /** IN Endpoint NAK Effective mask */
  89983. + unsigned inepnakeff:1;
  89984. + /** Reserved */
  89985. + unsigned emptyintr:1;
  89986. +
  89987. + unsigned txfifoundrn:1;
  89988. +
  89989. + /** BNA Interrupt mask */
  89990. + unsigned bna:1;
  89991. +
  89992. + unsigned reserved10_12:3;
  89993. + /** BNA Interrupt mask */
  89994. + unsigned nak:1;
  89995. +
  89996. + unsigned reserved14_31:18;
  89997. + } b;
  89998. +} diepint_data_t;
  89999. +
  90000. +/**
  90001. + * This union represents the bit fields in the Device IN EP
  90002. + * Common/Dedicated Interrupt Mask Register.
  90003. + */
  90004. +typedef union diepint_data diepmsk_data_t;
  90005. +
  90006. +/**
  90007. + * This union represents the bit fields in the Device OUT EP Interrupt
  90008. + * Registerand Device OUT EP Common Interrupt Mask Register.
  90009. + *
  90010. + * - Read the register into the <i>d32</i> member then set/clear the
  90011. + * bits using the <i>b</i>it elements.
  90012. + */
  90013. +typedef union doepint_data {
  90014. + /** raw register data */
  90015. + uint32_t d32;
  90016. + /** register bits */
  90017. + struct {
  90018. + /** Transfer complete */
  90019. + unsigned xfercompl:1;
  90020. + /** Endpoint disable */
  90021. + unsigned epdisabled:1;
  90022. + /** AHB Error */
  90023. + unsigned ahberr:1;
  90024. + /** Setup Phase Done (contorl EPs) */
  90025. + unsigned setup:1;
  90026. + /** OUT Token Received when Endpoint Disabled */
  90027. + unsigned outtknepdis:1;
  90028. +
  90029. + unsigned stsphsercvd:1;
  90030. + /** Back-to-Back SETUP Packets Received */
  90031. + unsigned back2backsetup:1;
  90032. +
  90033. + unsigned reserved7:1;
  90034. + /** OUT packet Error */
  90035. + unsigned outpkterr:1;
  90036. + /** BNA Interrupt */
  90037. + unsigned bna:1;
  90038. +
  90039. + unsigned reserved10:1;
  90040. + /** Packet Drop Status */
  90041. + unsigned pktdrpsts:1;
  90042. + /** Babble Interrupt */
  90043. + unsigned babble:1;
  90044. + /** NAK Interrupt */
  90045. + unsigned nak:1;
  90046. + /** NYET Interrupt */
  90047. + unsigned nyet:1;
  90048. + /** Bit indicating setup packet received */
  90049. + unsigned sr:1;
  90050. +
  90051. + unsigned reserved16_31:16;
  90052. + } b;
  90053. +} doepint_data_t;
  90054. +
  90055. +/**
  90056. + * This union represents the bit fields in the Device OUT EP
  90057. + * Common/Dedicated Interrupt Mask Register.
  90058. + */
  90059. +typedef union doepint_data doepmsk_data_t;
  90060. +
  90061. +/**
  90062. + * This union represents the bit fields in the Device All EP Interrupt
  90063. + * and Mask Registers.
  90064. + * - Read the register into the <i>d32</i> member then set/clear the
  90065. + * bits using the <i>b</i>it elements.
  90066. + */
  90067. +typedef union daint_data {
  90068. + /** raw register data */
  90069. + uint32_t d32;
  90070. + /** register bits */
  90071. + struct {
  90072. + /** IN Endpoint bits */
  90073. + unsigned in:16;
  90074. + /** OUT Endpoint bits */
  90075. + unsigned out:16;
  90076. + } ep;
  90077. + struct {
  90078. + /** IN Endpoint bits */
  90079. + unsigned inep0:1;
  90080. + unsigned inep1:1;
  90081. + unsigned inep2:1;
  90082. + unsigned inep3:1;
  90083. + unsigned inep4:1;
  90084. + unsigned inep5:1;
  90085. + unsigned inep6:1;
  90086. + unsigned inep7:1;
  90087. + unsigned inep8:1;
  90088. + unsigned inep9:1;
  90089. + unsigned inep10:1;
  90090. + unsigned inep11:1;
  90091. + unsigned inep12:1;
  90092. + unsigned inep13:1;
  90093. + unsigned inep14:1;
  90094. + unsigned inep15:1;
  90095. + /** OUT Endpoint bits */
  90096. + unsigned outep0:1;
  90097. + unsigned outep1:1;
  90098. + unsigned outep2:1;
  90099. + unsigned outep3:1;
  90100. + unsigned outep4:1;
  90101. + unsigned outep5:1;
  90102. + unsigned outep6:1;
  90103. + unsigned outep7:1;
  90104. + unsigned outep8:1;
  90105. + unsigned outep9:1;
  90106. + unsigned outep10:1;
  90107. + unsigned outep11:1;
  90108. + unsigned outep12:1;
  90109. + unsigned outep13:1;
  90110. + unsigned outep14:1;
  90111. + unsigned outep15:1;
  90112. + } b;
  90113. +} daint_data_t;
  90114. +
  90115. +/**
  90116. + * This union represents the bit fields in the Device IN Token Queue
  90117. + * Read Registers.
  90118. + * - Read the register into the <i>d32</i> member.
  90119. + * - READ-ONLY Register
  90120. + */
  90121. +typedef union dtknq1_data {
  90122. + /** raw register data */
  90123. + uint32_t d32;
  90124. + /** register bits */
  90125. + struct {
  90126. + /** In Token Queue Write Pointer */
  90127. + unsigned intknwptr:5;
  90128. + /** Reserved */
  90129. + unsigned reserved05_06:2;
  90130. + /** write pointer has wrapped. */
  90131. + unsigned wrap_bit:1;
  90132. + /** EP Numbers of IN Tokens 0 ... 4 */
  90133. + unsigned epnums0_5:24;
  90134. + } b;
  90135. +} dtknq1_data_t;
  90136. +
  90137. +/**
  90138. + * This union represents Threshold control Register
  90139. + * - Read and write the register into the <i>d32</i> member.
  90140. + * - READ-WRITABLE Register
  90141. + */
  90142. +typedef union dthrctl_data {
  90143. + /** raw register data */
  90144. + uint32_t d32;
  90145. + /** register bits */
  90146. + struct {
  90147. + /** non ISO Tx Thr. Enable */
  90148. + unsigned non_iso_thr_en:1;
  90149. + /** ISO Tx Thr. Enable */
  90150. + unsigned iso_thr_en:1;
  90151. + /** Tx Thr. Length */
  90152. + unsigned tx_thr_len:9;
  90153. + /** AHB Threshold ratio */
  90154. + unsigned ahb_thr_ratio:2;
  90155. + /** Reserved */
  90156. + unsigned reserved13_15:3;
  90157. + /** Rx Thr. Enable */
  90158. + unsigned rx_thr_en:1;
  90159. + /** Rx Thr. Length */
  90160. + unsigned rx_thr_len:9;
  90161. + unsigned reserved26:1;
  90162. + /** Arbiter Parking Enable*/
  90163. + unsigned arbprken:1;
  90164. + /** Reserved */
  90165. + unsigned reserved28_31:4;
  90166. + } b;
  90167. +} dthrctl_data_t;
  90168. +
  90169. +/**
  90170. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  90171. + * 900h-AFCh</i>
  90172. + *
  90173. + * There will be one set of endpoint registers per logical endpoint
  90174. + * implemented.
  90175. + *
  90176. + * <i>These registers are visible only in Device mode and must not be
  90177. + * accessed in Host mode, as the results are unknown.</i>
  90178. + */
  90179. +typedef struct dwc_otg_dev_in_ep_regs {
  90180. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  90181. + * (ep_num * 20h) + 00h</i> */
  90182. + volatile uint32_t diepctl;
  90183. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  90184. + uint32_t reserved04;
  90185. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  90186. + * (ep_num * 20h) + 08h</i> */
  90187. + volatile uint32_t diepint;
  90188. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  90189. + uint32_t reserved0C;
  90190. + /** Device IN Endpoint Transfer Size
  90191. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  90192. + volatile uint32_t dieptsiz;
  90193. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  90194. + * (ep_num * 20h) + 14h</i> */
  90195. + volatile uint32_t diepdma;
  90196. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  90197. + * (ep_num * 20h) + 18h</i> */
  90198. + volatile uint32_t dtxfsts;
  90199. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  90200. + * (ep_num * 20h) + 1Ch</i> */
  90201. + volatile uint32_t diepdmab;
  90202. +} dwc_otg_dev_in_ep_regs_t;
  90203. +
  90204. +/**
  90205. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  90206. + * B00h-CFCh</i>
  90207. + *
  90208. + * There will be one set of endpoint registers per logical endpoint
  90209. + * implemented.
  90210. + *
  90211. + * <i>These registers are visible only in Device mode and must not be
  90212. + * accessed in Host mode, as the results are unknown.</i>
  90213. + */
  90214. +typedef struct dwc_otg_dev_out_ep_regs {
  90215. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  90216. + * (ep_num * 20h) + 00h</i> */
  90217. + volatile uint32_t doepctl;
  90218. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  90219. + uint32_t reserved04;
  90220. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  90221. + * (ep_num * 20h) + 08h</i> */
  90222. + volatile uint32_t doepint;
  90223. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  90224. + uint32_t reserved0C;
  90225. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  90226. + * B00h + (ep_num * 20h) + 10h</i> */
  90227. + volatile uint32_t doeptsiz;
  90228. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  90229. + * + (ep_num * 20h) + 14h</i> */
  90230. + volatile uint32_t doepdma;
  90231. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  90232. + uint32_t unused;
  90233. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  90234. + * + (ep_num * 20h) + 1Ch</i> */
  90235. + uint32_t doepdmab;
  90236. +} dwc_otg_dev_out_ep_regs_t;
  90237. +
  90238. +/**
  90239. + * This union represents the bit fields in the Device EP Control
  90240. + * Register. Read the register into the <i>d32</i> member then
  90241. + * set/clear the bits using the <i>b</i>it elements.
  90242. + */
  90243. +typedef union depctl_data {
  90244. + /** raw register data */
  90245. + uint32_t d32;
  90246. + /** register bits */
  90247. + struct {
  90248. + /** Maximum Packet Size
  90249. + * IN/OUT EPn
  90250. + * IN/OUT EP0 - 2 bits
  90251. + * 2'b00: 64 Bytes
  90252. + * 2'b01: 32
  90253. + * 2'b10: 16
  90254. + * 2'b11: 8 */
  90255. + unsigned mps:11;
  90256. +#define DWC_DEP0CTL_MPS_64 0
  90257. +#define DWC_DEP0CTL_MPS_32 1
  90258. +#define DWC_DEP0CTL_MPS_16 2
  90259. +#define DWC_DEP0CTL_MPS_8 3
  90260. +
  90261. + /** Next Endpoint
  90262. + * IN EPn/IN EP0
  90263. + * OUT EPn/OUT EP0 - reserved */
  90264. + unsigned nextep:4;
  90265. +
  90266. + /** USB Active Endpoint */
  90267. + unsigned usbactep:1;
  90268. +
  90269. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  90270. + * This field contains the PID of the packet going to
  90271. + * be received or transmitted on this endpoint. The
  90272. + * application should program the PID of the first
  90273. + * packet going to be received or transmitted on this
  90274. + * endpoint , after the endpoint is
  90275. + * activated. Application use the SetD1PID and
  90276. + * SetD0PID fields of this register to program either
  90277. + * D0 or D1 PID.
  90278. + *
  90279. + * The encoding for this field is
  90280. + * - 0: D0
  90281. + * - 1: D1
  90282. + */
  90283. + unsigned dpid:1;
  90284. +
  90285. + /** NAK Status */
  90286. + unsigned naksts:1;
  90287. +
  90288. + /** Endpoint Type
  90289. + * 2'b00: Control
  90290. + * 2'b01: Isochronous
  90291. + * 2'b10: Bulk
  90292. + * 2'b11: Interrupt */
  90293. + unsigned eptype:2;
  90294. +
  90295. + /** Snoop Mode
  90296. + * OUT EPn/OUT EP0
  90297. + * IN EPn/IN EP0 - reserved */
  90298. + unsigned snp:1;
  90299. +
  90300. + /** Stall Handshake */
  90301. + unsigned stall:1;
  90302. +
  90303. + /** Tx Fifo Number
  90304. + * IN EPn/IN EP0
  90305. + * OUT EPn/OUT EP0 - reserved */
  90306. + unsigned txfnum:4;
  90307. +
  90308. + /** Clear NAK */
  90309. + unsigned cnak:1;
  90310. + /** Set NAK */
  90311. + unsigned snak:1;
  90312. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  90313. + * Writing to this field sets the Endpoint DPID (DPID)
  90314. + * field in this register to DATA0. Set Even
  90315. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  90316. + * Writing to this field sets the Even/Odd
  90317. + * (micro)frame (EO_FrNum) field to even (micro)
  90318. + * frame.
  90319. + */
  90320. + unsigned setd0pid:1;
  90321. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  90322. + * Writing to this field sets the Endpoint DPID (DPID)
  90323. + * field in this register to DATA1 Set Odd
  90324. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  90325. + * Writing to this field sets the Even/Odd
  90326. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  90327. + */
  90328. + unsigned setd1pid:1;
  90329. +
  90330. + /** Endpoint Disable */
  90331. + unsigned epdis:1;
  90332. + /** Endpoint Enable */
  90333. + unsigned epena:1;
  90334. + } b;
  90335. +} depctl_data_t;
  90336. +
  90337. +/**
  90338. + * This union represents the bit fields in the Device EP Transfer
  90339. + * Size Register. Read the register into the <i>d32</i> member then
  90340. + * set/clear the bits using the <i>b</i>it elements.
  90341. + */
  90342. +typedef union deptsiz_data {
  90343. + /** raw register data */
  90344. + uint32_t d32;
  90345. + /** register bits */
  90346. + struct {
  90347. + /** Transfer size */
  90348. + unsigned xfersize:19;
  90349. +/** Max packet count for EP (pow(2,10)-1) */
  90350. +#define MAX_PKT_CNT 1023
  90351. + /** Packet Count */
  90352. + unsigned pktcnt:10;
  90353. + /** Multi Count - Periodic IN endpoints */
  90354. + unsigned mc:2;
  90355. + unsigned reserved:1;
  90356. + } b;
  90357. +} deptsiz_data_t;
  90358. +
  90359. +/**
  90360. + * This union represents the bit fields in the Device EP 0 Transfer
  90361. + * Size Register. Read the register into the <i>d32</i> member then
  90362. + * set/clear the bits using the <i>b</i>it elements.
  90363. + */
  90364. +typedef union deptsiz0_data {
  90365. + /** raw register data */
  90366. + uint32_t d32;
  90367. + /** register bits */
  90368. + struct {
  90369. + /** Transfer size */
  90370. + unsigned xfersize:7;
  90371. + /** Reserved */
  90372. + unsigned reserved7_18:12;
  90373. + /** Packet Count */
  90374. + unsigned pktcnt:2;
  90375. + /** Reserved */
  90376. + unsigned reserved21_28:8;
  90377. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  90378. + unsigned supcnt:2;
  90379. + unsigned reserved31;
  90380. + } b;
  90381. +} deptsiz0_data_t;
  90382. +
  90383. +/////////////////////////////////////////////////
  90384. +// DMA Descriptor Specific Structures
  90385. +//
  90386. +
  90387. +/** Buffer status definitions */
  90388. +
  90389. +#define BS_HOST_READY 0x0
  90390. +#define BS_DMA_BUSY 0x1
  90391. +#define BS_DMA_DONE 0x2
  90392. +#define BS_HOST_BUSY 0x3
  90393. +
  90394. +/** Receive/Transmit status definitions */
  90395. +
  90396. +#define RTS_SUCCESS 0x0
  90397. +#define RTS_BUFFLUSH 0x1
  90398. +#define RTS_RESERVED 0x2
  90399. +#define RTS_BUFERR 0x3
  90400. +
  90401. +/**
  90402. + * This union represents the bit fields in the DMA Descriptor
  90403. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  90404. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  90405. + * <i>b_iso_in</i> elements.
  90406. + */
  90407. +typedef union dev_dma_desc_sts {
  90408. + /** raw register data */
  90409. + uint32_t d32;
  90410. + /** quadlet bits */
  90411. + struct {
  90412. + /** Received number of bytes */
  90413. + unsigned bytes:16;
  90414. + /** NAK bit - only for OUT EPs */
  90415. + unsigned nak:1;
  90416. + unsigned reserved17_22:6;
  90417. + /** Multiple Transfer - only for OUT EPs */
  90418. + unsigned mtrf:1;
  90419. + /** Setup Packet received - only for OUT EPs */
  90420. + unsigned sr:1;
  90421. + /** Interrupt On Complete */
  90422. + unsigned ioc:1;
  90423. + /** Short Packet */
  90424. + unsigned sp:1;
  90425. + /** Last */
  90426. + unsigned l:1;
  90427. + /** Receive Status */
  90428. + unsigned sts:2;
  90429. + /** Buffer Status */
  90430. + unsigned bs:2;
  90431. + } b;
  90432. +
  90433. +//#ifdef DWC_EN_ISOC
  90434. + /** iso out quadlet bits */
  90435. + struct {
  90436. + /** Received number of bytes */
  90437. + unsigned rxbytes:11;
  90438. +
  90439. + unsigned reserved11:1;
  90440. + /** Frame Number */
  90441. + unsigned framenum:11;
  90442. + /** Received ISO Data PID */
  90443. + unsigned pid:2;
  90444. + /** Interrupt On Complete */
  90445. + unsigned ioc:1;
  90446. + /** Short Packet */
  90447. + unsigned sp:1;
  90448. + /** Last */
  90449. + unsigned l:1;
  90450. + /** Receive Status */
  90451. + unsigned rxsts:2;
  90452. + /** Buffer Status */
  90453. + unsigned bs:2;
  90454. + } b_iso_out;
  90455. +
  90456. + /** iso in quadlet bits */
  90457. + struct {
  90458. + /** Transmited number of bytes */
  90459. + unsigned txbytes:12;
  90460. + /** Frame Number */
  90461. + unsigned framenum:11;
  90462. + /** Transmited ISO Data PID */
  90463. + unsigned pid:2;
  90464. + /** Interrupt On Complete */
  90465. + unsigned ioc:1;
  90466. + /** Short Packet */
  90467. + unsigned sp:1;
  90468. + /** Last */
  90469. + unsigned l:1;
  90470. + /** Transmit Status */
  90471. + unsigned txsts:2;
  90472. + /** Buffer Status */
  90473. + unsigned bs:2;
  90474. + } b_iso_in;
  90475. +//#endif /* DWC_EN_ISOC */
  90476. +} dev_dma_desc_sts_t;
  90477. +
  90478. +/**
  90479. + * DMA Descriptor structure
  90480. + *
  90481. + * DMA Descriptor structure contains two quadlets:
  90482. + * Status quadlet and Data buffer pointer.
  90483. + */
  90484. +typedef struct dwc_otg_dev_dma_desc {
  90485. + /** DMA Descriptor status quadlet */
  90486. + dev_dma_desc_sts_t status;
  90487. + /** DMA Descriptor data buffer pointer */
  90488. + uint32_t buf;
  90489. +} dwc_otg_dev_dma_desc_t;
  90490. +
  90491. +/**
  90492. + * The dwc_otg_dev_if structure contains information needed to manage
  90493. + * the DWC_otg controller acting in device mode. It represents the
  90494. + * programming view of the device-specific aspects of the controller.
  90495. + */
  90496. +typedef struct dwc_otg_dev_if {
  90497. + /** Pointer to device Global registers.
  90498. + * Device Global Registers starting at offset 800h
  90499. + */
  90500. + dwc_otg_device_global_regs_t *dev_global_regs;
  90501. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  90502. +
  90503. + /**
  90504. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  90505. + */
  90506. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  90507. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  90508. +#define DWC_EP_REG_OFFSET 0x20
  90509. +
  90510. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  90511. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  90512. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  90513. +
  90514. + /* Device configuration information */
  90515. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  90516. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  90517. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  90518. +
  90519. + /** Size of periodic FIFOs (Bytes) */
  90520. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  90521. +
  90522. + /** Size of Tx FIFOs (Bytes) */
  90523. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  90524. +
  90525. + /** Thresholding enable flags and length varaiables **/
  90526. + uint16_t rx_thr_en;
  90527. + uint16_t iso_tx_thr_en;
  90528. + uint16_t non_iso_tx_thr_en;
  90529. +
  90530. + uint16_t rx_thr_length;
  90531. + uint16_t tx_thr_length;
  90532. +
  90533. + /**
  90534. + * Pointers to the DMA Descriptors for EP0 Control
  90535. + * transfers (virtual and physical)
  90536. + */
  90537. +
  90538. + /** 2 descriptors for SETUP packets */
  90539. + dwc_dma_t dma_setup_desc_addr[2];
  90540. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  90541. +
  90542. + /** Pointer to Descriptor with latest SETUP packet */
  90543. + dwc_otg_dev_dma_desc_t *psetup;
  90544. +
  90545. + /** Index of current SETUP handler descriptor */
  90546. + uint32_t setup_desc_index;
  90547. +
  90548. + /** Descriptor for Data In or Status In phases */
  90549. + dwc_dma_t dma_in_desc_addr;
  90550. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  90551. +
  90552. + /** Descriptor for Data Out or Status Out phases */
  90553. + dwc_dma_t dma_out_desc_addr;
  90554. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  90555. +
  90556. + /** Setup Packet Detected - if set clear NAK when queueing */
  90557. + uint32_t spd;
  90558. + /** Isoc ep pointer on which incomplete happens */
  90559. + void *isoc_ep;
  90560. +
  90561. +} dwc_otg_dev_if_t;
  90562. +
  90563. +/////////////////////////////////////////////////
  90564. +// Host Mode Register Structures
  90565. +//
  90566. +/**
  90567. + * The Host Global Registers structure defines the size and relative
  90568. + * field offsets for the Host Mode Global Registers. Host Global
  90569. + * Registers offsets 400h-7FFh.
  90570. +*/
  90571. +typedef struct dwc_otg_host_global_regs {
  90572. + /** Host Configuration Register. <i>Offset: 400h</i> */
  90573. + volatile uint32_t hcfg;
  90574. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  90575. + volatile uint32_t hfir;
  90576. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  90577. + volatile uint32_t hfnum;
  90578. + /** Reserved. <i>Offset: 40Ch</i> */
  90579. + uint32_t reserved40C;
  90580. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  90581. + volatile uint32_t hptxsts;
  90582. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  90583. + volatile uint32_t haint;
  90584. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  90585. + volatile uint32_t haintmsk;
  90586. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  90587. + volatile uint32_t hflbaddr;
  90588. +} dwc_otg_host_global_regs_t;
  90589. +
  90590. +/**
  90591. + * This union represents the bit fields in the Host Configuration Register.
  90592. + * Read the register into the <i>d32</i> member then set/clear the bits using
  90593. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  90594. + */
  90595. +typedef union hcfg_data {
  90596. + /** raw register data */
  90597. + uint32_t d32;
  90598. +
  90599. + /** register bits */
  90600. + struct {
  90601. + /** FS/LS Phy Clock Select */
  90602. + unsigned fslspclksel:2;
  90603. +#define DWC_HCFG_30_60_MHZ 0
  90604. +#define DWC_HCFG_48_MHZ 1
  90605. +#define DWC_HCFG_6_MHZ 2
  90606. +
  90607. + /** FS/LS Only Support */
  90608. + unsigned fslssupp:1;
  90609. + unsigned reserved3_6:4;
  90610. + /** Enable 32-KHz Suspend Mode */
  90611. + unsigned ena32khzs:1;
  90612. + /** Resume Validation Periiod */
  90613. + unsigned resvalid:8;
  90614. + unsigned reserved16_22:7;
  90615. + /** Enable Scatter/gather DMA in Host mode */
  90616. + unsigned descdma:1;
  90617. + /** Frame List Entries */
  90618. + unsigned frlisten:2;
  90619. + /** Enable Periodic Scheduling */
  90620. + unsigned perschedena:1;
  90621. + unsigned reserved27_30:4;
  90622. + unsigned modechtimen:1;
  90623. + } b;
  90624. +} hcfg_data_t;
  90625. +
  90626. +/**
  90627. + * This union represents the bit fields in the Host Frame Remaing/Number
  90628. + * Register.
  90629. + */
  90630. +typedef union hfir_data {
  90631. + /** raw register data */
  90632. + uint32_t d32;
  90633. +
  90634. + /** register bits */
  90635. + struct {
  90636. + unsigned frint:16;
  90637. + unsigned hfirrldctrl:1;
  90638. + unsigned reserved:15;
  90639. + } b;
  90640. +} hfir_data_t;
  90641. +
  90642. +/**
  90643. + * This union represents the bit fields in the Host Frame Remaing/Number
  90644. + * Register.
  90645. + */
  90646. +typedef union hfnum_data {
  90647. + /** raw register data */
  90648. + uint32_t d32;
  90649. +
  90650. + /** register bits */
  90651. + struct {
  90652. + unsigned frnum:16;
  90653. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  90654. + unsigned frrem:16;
  90655. + } b;
  90656. +} hfnum_data_t;
  90657. +
  90658. +typedef union hptxsts_data {
  90659. + /** raw register data */
  90660. + uint32_t d32;
  90661. +
  90662. + /** register bits */
  90663. + struct {
  90664. + unsigned ptxfspcavail:16;
  90665. + unsigned ptxqspcavail:8;
  90666. + /** Top of the Periodic Transmit Request Queue
  90667. + * - bit 24 - Terminate (last entry for the selected channel)
  90668. + * - bits 26:25 - Token Type
  90669. + * - 2'b00 - Zero length
  90670. + * - 2'b01 - Ping
  90671. + * - 2'b10 - Disable
  90672. + * - bits 30:27 - Channel Number
  90673. + * - bit 31 - Odd/even microframe
  90674. + */
  90675. + unsigned ptxqtop_terminate:1;
  90676. + unsigned ptxqtop_token:2;
  90677. + unsigned ptxqtop_chnum:4;
  90678. + unsigned ptxqtop_odd:1;
  90679. + } b;
  90680. +} hptxsts_data_t;
  90681. +
  90682. +/**
  90683. + * This union represents the bit fields in the Host Port Control and Status
  90684. + * Register. Read the register into the <i>d32</i> member then set/clear the
  90685. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  90686. + * hprt0 register.
  90687. + */
  90688. +typedef union hprt0_data {
  90689. + /** raw register data */
  90690. + uint32_t d32;
  90691. + /** register bits */
  90692. + struct {
  90693. + unsigned prtconnsts:1;
  90694. + unsigned prtconndet:1;
  90695. + unsigned prtena:1;
  90696. + unsigned prtenchng:1;
  90697. + unsigned prtovrcurract:1;
  90698. + unsigned prtovrcurrchng:1;
  90699. + unsigned prtres:1;
  90700. + unsigned prtsusp:1;
  90701. + unsigned prtrst:1;
  90702. + unsigned reserved9:1;
  90703. + unsigned prtlnsts:2;
  90704. + unsigned prtpwr:1;
  90705. + unsigned prttstctl:4;
  90706. + unsigned prtspd:2;
  90707. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  90708. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  90709. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  90710. + unsigned reserved19_31:13;
  90711. + } b;
  90712. +} hprt0_data_t;
  90713. +
  90714. +/**
  90715. + * This union represents the bit fields in the Host All Interrupt
  90716. + * Register.
  90717. + */
  90718. +typedef union haint_data {
  90719. + /** raw register data */
  90720. + uint32_t d32;
  90721. + /** register bits */
  90722. + struct {
  90723. + unsigned ch0:1;
  90724. + unsigned ch1:1;
  90725. + unsigned ch2:1;
  90726. + unsigned ch3:1;
  90727. + unsigned ch4:1;
  90728. + unsigned ch5:1;
  90729. + unsigned ch6:1;
  90730. + unsigned ch7:1;
  90731. + unsigned ch8:1;
  90732. + unsigned ch9:1;
  90733. + unsigned ch10:1;
  90734. + unsigned ch11:1;
  90735. + unsigned ch12:1;
  90736. + unsigned ch13:1;
  90737. + unsigned ch14:1;
  90738. + unsigned ch15:1;
  90739. + unsigned reserved:16;
  90740. + } b;
  90741. +
  90742. + struct {
  90743. + unsigned chint:16;
  90744. + unsigned reserved:16;
  90745. + } b2;
  90746. +} haint_data_t;
  90747. +
  90748. +/**
  90749. + * This union represents the bit fields in the Host All Interrupt
  90750. + * Register.
  90751. + */
  90752. +typedef union haintmsk_data {
  90753. + /** raw register data */
  90754. + uint32_t d32;
  90755. + /** register bits */
  90756. + struct {
  90757. + unsigned ch0:1;
  90758. + unsigned ch1:1;
  90759. + unsigned ch2:1;
  90760. + unsigned ch3:1;
  90761. + unsigned ch4:1;
  90762. + unsigned ch5:1;
  90763. + unsigned ch6:1;
  90764. + unsigned ch7:1;
  90765. + unsigned ch8:1;
  90766. + unsigned ch9:1;
  90767. + unsigned ch10:1;
  90768. + unsigned ch11:1;
  90769. + unsigned ch12:1;
  90770. + unsigned ch13:1;
  90771. + unsigned ch14:1;
  90772. + unsigned ch15:1;
  90773. + unsigned reserved:16;
  90774. + } b;
  90775. +
  90776. + struct {
  90777. + unsigned chint:16;
  90778. + unsigned reserved:16;
  90779. + } b2;
  90780. +} haintmsk_data_t;
  90781. +
  90782. +/**
  90783. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  90784. + */
  90785. +typedef struct dwc_otg_hc_regs {
  90786. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  90787. + volatile uint32_t hcchar;
  90788. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  90789. + volatile uint32_t hcsplt;
  90790. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  90791. + volatile uint32_t hcint;
  90792. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  90793. + volatile uint32_t hcintmsk;
  90794. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  90795. + volatile uint32_t hctsiz;
  90796. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  90797. + volatile uint32_t hcdma;
  90798. + volatile uint32_t reserved;
  90799. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  90800. + volatile uint32_t hcdmab;
  90801. +} dwc_otg_hc_regs_t;
  90802. +
  90803. +/**
  90804. + * This union represents the bit fields in the Host Channel Characteristics
  90805. + * Register. Read the register into the <i>d32</i> member then set/clear the
  90806. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  90807. + * hcchar register.
  90808. + */
  90809. +typedef union hcchar_data {
  90810. + /** raw register data */
  90811. + uint32_t d32;
  90812. +
  90813. + /** register bits */
  90814. + struct {
  90815. + /** Maximum packet size in bytes */
  90816. + unsigned mps:11;
  90817. +
  90818. + /** Endpoint number */
  90819. + unsigned epnum:4;
  90820. +
  90821. + /** 0: OUT, 1: IN */
  90822. + unsigned epdir:1;
  90823. +
  90824. + unsigned reserved:1;
  90825. +
  90826. + /** 0: Full/high speed device, 1: Low speed device */
  90827. + unsigned lspddev:1;
  90828. +
  90829. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  90830. + unsigned eptype:2;
  90831. +
  90832. + /** Packets per frame for periodic transfers. 0 is reserved. */
  90833. + unsigned multicnt:2;
  90834. +
  90835. + /** Device address */
  90836. + unsigned devaddr:7;
  90837. +
  90838. + /**
  90839. + * Frame to transmit periodic transaction.
  90840. + * 0: even, 1: odd
  90841. + */
  90842. + unsigned oddfrm:1;
  90843. +
  90844. + /** Channel disable */
  90845. + unsigned chdis:1;
  90846. +
  90847. + /** Channel enable */
  90848. + unsigned chen:1;
  90849. + } b;
  90850. +} hcchar_data_t;
  90851. +
  90852. +typedef union hcsplt_data {
  90853. + /** raw register data */
  90854. + uint32_t d32;
  90855. +
  90856. + /** register bits */
  90857. + struct {
  90858. + /** Port Address */
  90859. + unsigned prtaddr:7;
  90860. +
  90861. + /** Hub Address */
  90862. + unsigned hubaddr:7;
  90863. +
  90864. + /** Transaction Position */
  90865. + unsigned xactpos:2;
  90866. +#define DWC_HCSPLIT_XACTPOS_MID 0
  90867. +#define DWC_HCSPLIT_XACTPOS_END 1
  90868. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  90869. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  90870. +
  90871. + /** Do Complete Split */
  90872. + unsigned compsplt:1;
  90873. +
  90874. + /** Reserved */
  90875. + unsigned reserved:14;
  90876. +
  90877. + /** Split Enble */
  90878. + unsigned spltena:1;
  90879. + } b;
  90880. +} hcsplt_data_t;
  90881. +
  90882. +/**
  90883. + * This union represents the bit fields in the Host All Interrupt
  90884. + * Register.
  90885. + */
  90886. +typedef union hcint_data {
  90887. + /** raw register data */
  90888. + uint32_t d32;
  90889. + /** register bits */
  90890. + struct {
  90891. + /** Transfer Complete */
  90892. + unsigned xfercomp:1;
  90893. + /** Channel Halted */
  90894. + unsigned chhltd:1;
  90895. + /** AHB Error */
  90896. + unsigned ahberr:1;
  90897. + /** STALL Response Received */
  90898. + unsigned stall:1;
  90899. + /** NAK Response Received */
  90900. + unsigned nak:1;
  90901. + /** ACK Response Received */
  90902. + unsigned ack:1;
  90903. + /** NYET Response Received */
  90904. + unsigned nyet:1;
  90905. + /** Transaction Err */
  90906. + unsigned xacterr:1;
  90907. + /** Babble Error */
  90908. + unsigned bblerr:1;
  90909. + /** Frame Overrun */
  90910. + unsigned frmovrun:1;
  90911. + /** Data Toggle Error */
  90912. + unsigned datatglerr:1;
  90913. + /** Buffer Not Available (only for DDMA mode) */
  90914. + unsigned bna:1;
  90915. + /** Exessive transaction error (only for DDMA mode) */
  90916. + unsigned xcs_xact:1;
  90917. + /** Frame List Rollover interrupt */
  90918. + unsigned frm_list_roll:1;
  90919. + /** Reserved */
  90920. + unsigned reserved14_31:18;
  90921. + } b;
  90922. +} hcint_data_t;
  90923. +
  90924. +/**
  90925. + * This union represents the bit fields in the Host Channel Interrupt Mask
  90926. + * Register. Read the register into the <i>d32</i> member then set/clear the
  90927. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  90928. + * hcintmsk register.
  90929. + */
  90930. +typedef union hcintmsk_data {
  90931. + /** raw register data */
  90932. + uint32_t d32;
  90933. +
  90934. + /** register bits */
  90935. + struct {
  90936. + unsigned xfercompl:1;
  90937. + unsigned chhltd:1;
  90938. + unsigned ahberr:1;
  90939. + unsigned stall:1;
  90940. + unsigned nak:1;
  90941. + unsigned ack:1;
  90942. + unsigned nyet:1;
  90943. + unsigned xacterr:1;
  90944. + unsigned bblerr:1;
  90945. + unsigned frmovrun:1;
  90946. + unsigned datatglerr:1;
  90947. + unsigned bna:1;
  90948. + unsigned xcs_xact:1;
  90949. + unsigned frm_list_roll:1;
  90950. + unsigned reserved14_31:18;
  90951. + } b;
  90952. +} hcintmsk_data_t;
  90953. +
  90954. +/**
  90955. + * This union represents the bit fields in the Host Channel Transfer Size
  90956. + * Register. Read the register into the <i>d32</i> member then set/clear the
  90957. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  90958. + * hcchar register.
  90959. + */
  90960. +
  90961. +typedef union hctsiz_data {
  90962. + /** raw register data */
  90963. + uint32_t d32;
  90964. +
  90965. + /** register bits */
  90966. + struct {
  90967. + /** Total transfer size in bytes */
  90968. + unsigned xfersize:19;
  90969. +
  90970. + /** Data packets to transfer */
  90971. + unsigned pktcnt:10;
  90972. +
  90973. + /**
  90974. + * Packet ID for next data packet
  90975. + * 0: DATA0
  90976. + * 1: DATA2
  90977. + * 2: DATA1
  90978. + * 3: MDATA (non-Control), SETUP (Control)
  90979. + */
  90980. + unsigned pid:2;
  90981. +#define DWC_HCTSIZ_DATA0 0
  90982. +#define DWC_HCTSIZ_DATA1 2
  90983. +#define DWC_HCTSIZ_DATA2 1
  90984. +#define DWC_HCTSIZ_MDATA 3
  90985. +#define DWC_HCTSIZ_SETUP 3
  90986. +
  90987. + /** Do PING protocol when 1 */
  90988. + unsigned dopng:1;
  90989. + } b;
  90990. +
  90991. + /** register bits */
  90992. + struct {
  90993. + /** Scheduling information */
  90994. + unsigned schinfo:8;
  90995. +
  90996. + /** Number of transfer descriptors.
  90997. + * Max value:
  90998. + * 64 in general,
  90999. + * 256 only for HS isochronous endpoint.
  91000. + */
  91001. + unsigned ntd:8;
  91002. +
  91003. + /** Data packets to transfer */
  91004. + unsigned reserved16_28:13;
  91005. +
  91006. + /**
  91007. + * Packet ID for next data packet
  91008. + * 0: DATA0
  91009. + * 1: DATA2
  91010. + * 2: DATA1
  91011. + * 3: MDATA (non-Control)
  91012. + */
  91013. + unsigned pid:2;
  91014. +
  91015. + /** Do PING protocol when 1 */
  91016. + unsigned dopng:1;
  91017. + } b_ddma;
  91018. +} hctsiz_data_t;
  91019. +
  91020. +/**
  91021. + * This union represents the bit fields in the Host DMA Address
  91022. + * Register used in Descriptor DMA mode.
  91023. + */
  91024. +typedef union hcdma_data {
  91025. + /** raw register data */
  91026. + uint32_t d32;
  91027. + /** register bits */
  91028. + struct {
  91029. + unsigned reserved0_2:3;
  91030. + /** Current Transfer Descriptor. Not used for ISOC */
  91031. + unsigned ctd:8;
  91032. + /** Start Address of Descriptor List */
  91033. + unsigned dma_addr:21;
  91034. + } b;
  91035. +} hcdma_data_t;
  91036. +
  91037. +/**
  91038. + * This union represents the bit fields in the DMA Descriptor
  91039. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  91040. + * set/clear the bits using the <i>b</i>it elements.
  91041. + */
  91042. +typedef union host_dma_desc_sts {
  91043. + /** raw register data */
  91044. + uint32_t d32;
  91045. + /** quadlet bits */
  91046. +
  91047. + /* for non-isochronous */
  91048. + struct {
  91049. + /** Number of bytes */
  91050. + unsigned n_bytes:17;
  91051. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  91052. + unsigned qtd_offset:6;
  91053. + /**
  91054. + * Set to request the core to jump to alternate QTD if
  91055. + * Short Packet received - only for IN EPs
  91056. + */
  91057. + unsigned a_qtd:1;
  91058. + /**
  91059. + * Setup Packet bit. When set indicates that buffer contains
  91060. + * setup packet.
  91061. + */
  91062. + unsigned sup:1;
  91063. + /** Interrupt On Complete */
  91064. + unsigned ioc:1;
  91065. + /** End of List */
  91066. + unsigned eol:1;
  91067. + unsigned reserved27:1;
  91068. + /** Rx/Tx Status */
  91069. + unsigned sts:2;
  91070. +#define DMA_DESC_STS_PKTERR 1
  91071. + unsigned reserved30:1;
  91072. + /** Active Bit */
  91073. + unsigned a:1;
  91074. + } b;
  91075. + /* for isochronous */
  91076. + struct {
  91077. + /** Number of bytes */
  91078. + unsigned n_bytes:12;
  91079. + unsigned reserved12_24:13;
  91080. + /** Interrupt On Complete */
  91081. + unsigned ioc:1;
  91082. + unsigned reserved26_27:2;
  91083. + /** Rx/Tx Status */
  91084. + unsigned sts:2;
  91085. + unsigned reserved30:1;
  91086. + /** Active Bit */
  91087. + unsigned a:1;
  91088. + } b_isoc;
  91089. +} host_dma_desc_sts_t;
  91090. +
  91091. +#define MAX_DMA_DESC_SIZE 131071
  91092. +#define MAX_DMA_DESC_NUM_GENERIC 64
  91093. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  91094. +#define MAX_FRLIST_EN_NUM 64
  91095. +/**
  91096. + * Host-mode DMA Descriptor structure
  91097. + *
  91098. + * DMA Descriptor structure contains two quadlets:
  91099. + * Status quadlet and Data buffer pointer.
  91100. + */
  91101. +typedef struct dwc_otg_host_dma_desc {
  91102. + /** DMA Descriptor status quadlet */
  91103. + host_dma_desc_sts_t status;
  91104. + /** DMA Descriptor data buffer pointer */
  91105. + uint32_t buf;
  91106. +} dwc_otg_host_dma_desc_t;
  91107. +
  91108. +/** OTG Host Interface Structure.
  91109. + *
  91110. + * The OTG Host Interface Structure structure contains information
  91111. + * needed to manage the DWC_otg controller acting in host mode. It
  91112. + * represents the programming view of the host-specific aspects of the
  91113. + * controller.
  91114. + */
  91115. +typedef struct dwc_otg_host_if {
  91116. + /** Host Global Registers starting at offset 400h.*/
  91117. + dwc_otg_host_global_regs_t *host_global_regs;
  91118. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  91119. +
  91120. + /** Host Port 0 Control and Status Register */
  91121. + volatile uint32_t *hprt0;
  91122. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  91123. +
  91124. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  91125. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  91126. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  91127. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  91128. +
  91129. + /* Host configuration information */
  91130. + /** Number of Host Channels (range: 1-16) */
  91131. + uint8_t num_host_channels;
  91132. + /** Periodic EPs supported (0: no, 1: yes) */
  91133. + uint8_t perio_eps_supported;
  91134. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  91135. + uint16_t perio_tx_fifo_size;
  91136. +
  91137. +} dwc_otg_host_if_t;
  91138. +
  91139. +/**
  91140. + * This union represents the bit fields in the Power and Clock Gating Control
  91141. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91142. + * bits using the <i>b</i>it elements.
  91143. + */
  91144. +typedef union pcgcctl_data {
  91145. + /** raw register data */
  91146. + uint32_t d32;
  91147. +
  91148. + /** register bits */
  91149. + struct {
  91150. + /** Stop Pclk */
  91151. + unsigned stoppclk:1;
  91152. + /** Gate Hclk */
  91153. + unsigned gatehclk:1;
  91154. + /** Power Clamp */
  91155. + unsigned pwrclmp:1;
  91156. + /** Reset Power Down Modules */
  91157. + unsigned rstpdwnmodule:1;
  91158. + /** Reserved */
  91159. + unsigned reserved:1;
  91160. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  91161. + unsigned enbl_sleep_gating:1;
  91162. + /** PHY In Sleep (PhySleep) */
  91163. + unsigned phy_in_sleep:1;
  91164. + /** Deep Sleep*/
  91165. + unsigned deep_sleep:1;
  91166. + unsigned resetaftsusp:1;
  91167. + unsigned restoremode:1;
  91168. + unsigned enbl_extnd_hiber:1;
  91169. + unsigned extnd_hiber_pwrclmp:1;
  91170. + unsigned extnd_hiber_switch:1;
  91171. + unsigned ess_reg_restored:1;
  91172. + unsigned prt_clk_sel:2;
  91173. + unsigned port_power:1;
  91174. + unsigned max_xcvrselect:2;
  91175. + unsigned max_termsel:1;
  91176. + unsigned mac_dev_addr:7;
  91177. + unsigned p2hd_dev_enum_spd:2;
  91178. + unsigned p2hd_prt_spd:2;
  91179. + unsigned if_dev_mode:1;
  91180. + } b;
  91181. +} pcgcctl_data_t;
  91182. +
  91183. +/**
  91184. + * This union represents the bit fields in the Global Data FIFO Software
  91185. + * Configuration Register. Read the register into the <i>d32</i> member then
  91186. + * set/clear the bits using the <i>b</i>it elements.
  91187. + */
  91188. +typedef union gdfifocfg_data {
  91189. + /* raw register data */
  91190. + uint32_t d32;
  91191. + /** register bits */
  91192. + struct {
  91193. + /** OTG Data FIFO depth */
  91194. + unsigned gdfifocfg:16;
  91195. + /** Start address of EP info controller */
  91196. + unsigned epinfobase:16;
  91197. + } b;
  91198. +} gdfifocfg_data_t;
  91199. +
  91200. +/**
  91201. + * This union represents the bit fields in the Global Power Down Register
  91202. + * Register. Read the register into the <i>d32</i> member then set/clear the
  91203. + * bits using the <i>b</i>it elements.
  91204. + */
  91205. +typedef union gpwrdn_data {
  91206. + /* raw register data */
  91207. + uint32_t d32;
  91208. +
  91209. + /** register bits */
  91210. + struct {
  91211. + /** PMU Interrupt Select */
  91212. + unsigned pmuintsel:1;
  91213. + /** PMU Active */
  91214. + unsigned pmuactv:1;
  91215. + /** Restore */
  91216. + unsigned restore:1;
  91217. + /** Power Down Clamp */
  91218. + unsigned pwrdnclmp:1;
  91219. + /** Power Down Reset */
  91220. + unsigned pwrdnrstn:1;
  91221. + /** Power Down Switch */
  91222. + unsigned pwrdnswtch:1;
  91223. + /** Disable VBUS */
  91224. + unsigned dis_vbus:1;
  91225. + /** Line State Change */
  91226. + unsigned lnstschng:1;
  91227. + /** Line state change mask */
  91228. + unsigned lnstchng_msk:1;
  91229. + /** Reset Detected */
  91230. + unsigned rst_det:1;
  91231. + /** Reset Detect mask */
  91232. + unsigned rst_det_msk:1;
  91233. + /** Disconnect Detected */
  91234. + unsigned disconn_det:1;
  91235. + /** Disconnect Detect mask */
  91236. + unsigned disconn_det_msk:1;
  91237. + /** Connect Detected*/
  91238. + unsigned connect_det:1;
  91239. + /** Connect Detected Mask*/
  91240. + unsigned connect_det_msk:1;
  91241. + /** SRP Detected */
  91242. + unsigned srp_det:1;
  91243. + /** SRP Detect mask */
  91244. + unsigned srp_det_msk:1;
  91245. + /** Status Change Interrupt */
  91246. + unsigned sts_chngint:1;
  91247. + /** Status Change Interrupt Mask */
  91248. + unsigned sts_chngint_msk:1;
  91249. + /** Line State */
  91250. + unsigned linestate:2;
  91251. + /** Indicates current mode(status of IDDIG signal) */
  91252. + unsigned idsts:1;
  91253. + /** B Session Valid signal status*/
  91254. + unsigned bsessvld:1;
  91255. + /** ADP Event Detected */
  91256. + unsigned adp_int:1;
  91257. + /** Multi Valued ID pin */
  91258. + unsigned mult_val_id_bc:5;
  91259. + /** Reserved 24_31 */
  91260. + unsigned reserved29_31:3;
  91261. + } b;
  91262. +} gpwrdn_data_t;
  91263. +
  91264. +#endif
  91265. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/Makefile linux-raspberry-pi/drivers/usb/host/dwc_otg/Makefile
  91266. --- linux-3.13.6/drivers/usb/host/dwc_otg/Makefile 1970-01-01 01:00:00.000000000 +0100
  91267. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/Makefile 2014-03-11 16:55:38.000000000 +0100
  91268. @@ -0,0 +1,81 @@
  91269. +#
  91270. +# Makefile for DWC_otg Highspeed USB controller driver
  91271. +#
  91272. +
  91273. +ifneq ($(KERNELRELEASE),)
  91274. +
  91275. +# Use the BUS_INTERFACE variable to compile the software for either
  91276. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  91277. +ifeq ($(BUS_INTERFACE),)
  91278. +# BUS_INTERFACE = -DPCI_INTERFACE
  91279. +# BUS_INTERFACE = -DLM_INTERFACE
  91280. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  91281. +endif
  91282. +
  91283. +#EXTRA_CFLAGS += -DDEBUG
  91284. +#EXTRA_CFLAGS += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  91285. +
  91286. +# Use one of the following flags to compile the software in host-only or
  91287. +# device-only mode.
  91288. +#EXTRA_CFLAGS += -DDWC_HOST_ONLY
  91289. +#EXTRA_CFLAGS += -DDWC_DEVICE_ONLY
  91290. +
  91291. +EXTRA_CFLAGS += -Dlinux -DDWC_HS_ELECT_TST
  91292. +#EXTRA_CFLAGS += -DDWC_EN_ISOC
  91293. +EXTRA_CFLAGS += -I$(obj)/../dwc_common_port
  91294. +#EXTRA_CFLAGS += -I$(PORTLIB)
  91295. +EXTRA_CFLAGS += -DDWC_LINUX
  91296. +EXTRA_CFLAGS += $(CFI)
  91297. +EXTRA_CFLAGS += $(BUS_INTERFACE)
  91298. +#EXTRA_CFLAGS += -DDWC_DEV_SRPCAP
  91299. +
  91300. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  91301. +
  91302. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  91303. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  91304. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  91305. +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
  91306. +dwc_otg-objs += dwc_otg_adp.o
  91307. +dwc_otg-objs += dwc_otg_mphi_fix.o
  91308. +ifneq ($(CFI),)
  91309. +dwc_otg-objs += dwc_otg_cfi.o
  91310. +endif
  91311. +
  91312. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  91313. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  91314. +
  91315. +ifneq ($(kernrel3),2.6.20)
  91316. +EXTRA_CFLAGS += $(CPPFLAGS)
  91317. +endif
  91318. +
  91319. +else
  91320. +
  91321. +PWD := $(shell pwd)
  91322. +PORTLIB := $(PWD)/../dwc_common_port
  91323. +
  91324. +# Command paths
  91325. +CTAGS := $(CTAGS)
  91326. +DOXYGEN := $(DOXYGEN)
  91327. +
  91328. +default: portlib
  91329. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  91330. +
  91331. +install: default
  91332. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  91333. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  91334. +
  91335. +portlib:
  91336. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  91337. + cp $(PORTLIB)/Module.symvers $(PWD)/
  91338. +
  91339. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  91340. + $(DOXYGEN) doc/doxygen.cfg
  91341. +
  91342. +tags: $(wildcard *.[hc])
  91343. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  91344. +
  91345. +
  91346. +clean:
  91347. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  91348. +
  91349. +endif
  91350. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm linux-raspberry-pi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  91351. --- linux-3.13.6/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 1970-01-01 01:00:00.000000000 +0100
  91352. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 2014-03-11 16:55:38.000000000 +0100
  91353. @@ -0,0 +1,337 @@
  91354. +package dwc_otg_test;
  91355. +
  91356. +use strict;
  91357. +use Exporter ();
  91358. +
  91359. +use vars qw(@ISA @EXPORT
  91360. +$sysfsdir $paramdir $errors $params
  91361. +);
  91362. +
  91363. +@ISA = qw(Exporter);
  91364. +
  91365. +#
  91366. +# Globals
  91367. +#
  91368. +$sysfsdir = "/sys/devices/lm0";
  91369. +$paramdir = "/sys/module/dwc_otg";
  91370. +$errors = 0;
  91371. +
  91372. +$params = [
  91373. + {
  91374. + NAME => "otg_cap",
  91375. + DEFAULT => 0,
  91376. + ENUM => [],
  91377. + LOW => 0,
  91378. + HIGH => 2
  91379. + },
  91380. + {
  91381. + NAME => "dma_enable",
  91382. + DEFAULT => 0,
  91383. + ENUM => [],
  91384. + LOW => 0,
  91385. + HIGH => 1
  91386. + },
  91387. + {
  91388. + NAME => "dma_burst_size",
  91389. + DEFAULT => 32,
  91390. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  91391. + LOW => 1,
  91392. + HIGH => 256
  91393. + },
  91394. + {
  91395. + NAME => "host_speed",
  91396. + DEFAULT => 0,
  91397. + ENUM => [],
  91398. + LOW => 0,
  91399. + HIGH => 1
  91400. + },
  91401. + {
  91402. + NAME => "host_support_fs_ls_low_power",
  91403. + DEFAULT => 0,
  91404. + ENUM => [],
  91405. + LOW => 0,
  91406. + HIGH => 1
  91407. + },
  91408. + {
  91409. + NAME => "host_ls_low_power_phy_clk",
  91410. + DEFAULT => 0,
  91411. + ENUM => [],
  91412. + LOW => 0,
  91413. + HIGH => 1
  91414. + },
  91415. + {
  91416. + NAME => "dev_speed",
  91417. + DEFAULT => 0,
  91418. + ENUM => [],
  91419. + LOW => 0,
  91420. + HIGH => 1
  91421. + },
  91422. + {
  91423. + NAME => "enable_dynamic_fifo",
  91424. + DEFAULT => 1,
  91425. + ENUM => [],
  91426. + LOW => 0,
  91427. + HIGH => 1
  91428. + },
  91429. + {
  91430. + NAME => "data_fifo_size",
  91431. + DEFAULT => 8192,
  91432. + ENUM => [],
  91433. + LOW => 32,
  91434. + HIGH => 32768
  91435. + },
  91436. + {
  91437. + NAME => "dev_rx_fifo_size",
  91438. + DEFAULT => 1064,
  91439. + ENUM => [],
  91440. + LOW => 16,
  91441. + HIGH => 32768
  91442. + },
  91443. + {
  91444. + NAME => "dev_nperio_tx_fifo_size",
  91445. + DEFAULT => 1024,
  91446. + ENUM => [],
  91447. + LOW => 16,
  91448. + HIGH => 32768
  91449. + },
  91450. + {
  91451. + NAME => "dev_perio_tx_fifo_size_1",
  91452. + DEFAULT => 256,
  91453. + ENUM => [],
  91454. + LOW => 4,
  91455. + HIGH => 768
  91456. + },
  91457. + {
  91458. + NAME => "dev_perio_tx_fifo_size_2",
  91459. + DEFAULT => 256,
  91460. + ENUM => [],
  91461. + LOW => 4,
  91462. + HIGH => 768
  91463. + },
  91464. + {
  91465. + NAME => "dev_perio_tx_fifo_size_3",
  91466. + DEFAULT => 256,
  91467. + ENUM => [],
  91468. + LOW => 4,
  91469. + HIGH => 768
  91470. + },
  91471. + {
  91472. + NAME => "dev_perio_tx_fifo_size_4",
  91473. + DEFAULT => 256,
  91474. + ENUM => [],
  91475. + LOW => 4,
  91476. + HIGH => 768
  91477. + },
  91478. + {
  91479. + NAME => "dev_perio_tx_fifo_size_5",
  91480. + DEFAULT => 256,
  91481. + ENUM => [],
  91482. + LOW => 4,
  91483. + HIGH => 768
  91484. + },
  91485. + {
  91486. + NAME => "dev_perio_tx_fifo_size_6",
  91487. + DEFAULT => 256,
  91488. + ENUM => [],
  91489. + LOW => 4,
  91490. + HIGH => 768
  91491. + },
  91492. + {
  91493. + NAME => "dev_perio_tx_fifo_size_7",
  91494. + DEFAULT => 256,
  91495. + ENUM => [],
  91496. + LOW => 4,
  91497. + HIGH => 768
  91498. + },
  91499. + {
  91500. + NAME => "dev_perio_tx_fifo_size_8",
  91501. + DEFAULT => 256,
  91502. + ENUM => [],
  91503. + LOW => 4,
  91504. + HIGH => 768
  91505. + },
  91506. + {
  91507. + NAME => "dev_perio_tx_fifo_size_9",
  91508. + DEFAULT => 256,
  91509. + ENUM => [],
  91510. + LOW => 4,
  91511. + HIGH => 768
  91512. + },
  91513. + {
  91514. + NAME => "dev_perio_tx_fifo_size_10",
  91515. + DEFAULT => 256,
  91516. + ENUM => [],
  91517. + LOW => 4,
  91518. + HIGH => 768
  91519. + },
  91520. + {
  91521. + NAME => "dev_perio_tx_fifo_size_11",
  91522. + DEFAULT => 256,
  91523. + ENUM => [],
  91524. + LOW => 4,
  91525. + HIGH => 768
  91526. + },
  91527. + {
  91528. + NAME => "dev_perio_tx_fifo_size_12",
  91529. + DEFAULT => 256,
  91530. + ENUM => [],
  91531. + LOW => 4,
  91532. + HIGH => 768
  91533. + },
  91534. + {
  91535. + NAME => "dev_perio_tx_fifo_size_13",
  91536. + DEFAULT => 256,
  91537. + ENUM => [],
  91538. + LOW => 4,
  91539. + HIGH => 768
  91540. + },
  91541. + {
  91542. + NAME => "dev_perio_tx_fifo_size_14",
  91543. + DEFAULT => 256,
  91544. + ENUM => [],
  91545. + LOW => 4,
  91546. + HIGH => 768
  91547. + },
  91548. + {
  91549. + NAME => "dev_perio_tx_fifo_size_15",
  91550. + DEFAULT => 256,
  91551. + ENUM => [],
  91552. + LOW => 4,
  91553. + HIGH => 768
  91554. + },
  91555. + {
  91556. + NAME => "host_rx_fifo_size",
  91557. + DEFAULT => 1024,
  91558. + ENUM => [],
  91559. + LOW => 16,
  91560. + HIGH => 32768
  91561. + },
  91562. + {
  91563. + NAME => "host_nperio_tx_fifo_size",
  91564. + DEFAULT => 1024,
  91565. + ENUM => [],
  91566. + LOW => 16,
  91567. + HIGH => 32768
  91568. + },
  91569. + {
  91570. + NAME => "host_perio_tx_fifo_size",
  91571. + DEFAULT => 1024,
  91572. + ENUM => [],
  91573. + LOW => 16,
  91574. + HIGH => 32768
  91575. + },
  91576. + {
  91577. + NAME => "max_transfer_size",
  91578. + DEFAULT => 65535,
  91579. + ENUM => [],
  91580. + LOW => 2047,
  91581. + HIGH => 65535
  91582. + },
  91583. + {
  91584. + NAME => "max_packet_count",
  91585. + DEFAULT => 511,
  91586. + ENUM => [],
  91587. + LOW => 15,
  91588. + HIGH => 511
  91589. + },
  91590. + {
  91591. + NAME => "host_channels",
  91592. + DEFAULT => 12,
  91593. + ENUM => [],
  91594. + LOW => 1,
  91595. + HIGH => 16
  91596. + },
  91597. + {
  91598. + NAME => "dev_endpoints",
  91599. + DEFAULT => 6,
  91600. + ENUM => [],
  91601. + LOW => 1,
  91602. + HIGH => 15
  91603. + },
  91604. + {
  91605. + NAME => "phy_type",
  91606. + DEFAULT => 1,
  91607. + ENUM => [],
  91608. + LOW => 0,
  91609. + HIGH => 2
  91610. + },
  91611. + {
  91612. + NAME => "phy_utmi_width",
  91613. + DEFAULT => 16,
  91614. + ENUM => [8, 16],
  91615. + LOW => 8,
  91616. + HIGH => 16
  91617. + },
  91618. + {
  91619. + NAME => "phy_ulpi_ddr",
  91620. + DEFAULT => 0,
  91621. + ENUM => [],
  91622. + LOW => 0,
  91623. + HIGH => 1
  91624. + },
  91625. + ];
  91626. +
  91627. +
  91628. +#
  91629. +#
  91630. +sub check_arch {
  91631. + $_ = `uname -m`;
  91632. + chomp;
  91633. + unless (m/armv4tl/) {
  91634. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  91635. + return 0;
  91636. + }
  91637. + return 1;
  91638. +}
  91639. +
  91640. +#
  91641. +#
  91642. +sub load_module {
  91643. + my $params = shift;
  91644. + print "\nRemoving Module\n";
  91645. + system "rmmod dwc_otg";
  91646. + print "Loading Module\n";
  91647. + if ($params ne "") {
  91648. + print "Module Parameters: $params\n";
  91649. + }
  91650. + if (system("modprobe dwc_otg $params")) {
  91651. + warn "Unable to load module\n";
  91652. + return 0;
  91653. + }
  91654. + return 1;
  91655. +}
  91656. +
  91657. +#
  91658. +#
  91659. +sub test_status {
  91660. + my $arg = shift;
  91661. +
  91662. + print "\n";
  91663. +
  91664. + if (defined $arg) {
  91665. + warn "WARNING: $arg\n";
  91666. + }
  91667. +
  91668. + if ($errors > 0) {
  91669. + warn "TEST FAILED with $errors errors\n";
  91670. + return 0;
  91671. + } else {
  91672. + print "TEST PASSED\n";
  91673. + return 0 if (defined $arg);
  91674. + }
  91675. + return 1;
  91676. +}
  91677. +
  91678. +#
  91679. +#
  91680. +@EXPORT = qw(
  91681. +$sysfsdir
  91682. +$paramdir
  91683. +$params
  91684. +$errors
  91685. +check_arch
  91686. +load_module
  91687. +test_status
  91688. +);
  91689. +
  91690. +1;
  91691. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/test/Makefile linux-raspberry-pi/drivers/usb/host/dwc_otg/test/Makefile
  91692. --- linux-3.13.6/drivers/usb/host/dwc_otg/test/Makefile 1970-01-01 01:00:00.000000000 +0100
  91693. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/test/Makefile 2014-03-11 16:53:12.000000000 +0100
  91694. @@ -0,0 +1,16 @@
  91695. +
  91696. +PERL=/usr/bin/perl
  91697. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  91698. +
  91699. +.PHONY : test
  91700. +test : perl_tests
  91701. +
  91702. +perl_tests :
  91703. + @echo
  91704. + @echo Running perl tests
  91705. + @for test in $(PL_TESTS); do \
  91706. + if $(PERL) ./$$test ; then \
  91707. + echo "=======> $$test, PASSED" ; \
  91708. + else echo "=======> $$test, FAILED" ; \
  91709. + fi \
  91710. + done
  91711. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/test/test_mod_param.pl linux-raspberry-pi/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  91712. --- linux-3.13.6/drivers/usb/host/dwc_otg/test/test_mod_param.pl 1970-01-01 01:00:00.000000000 +0100
  91713. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/test/test_mod_param.pl 2014-03-11 16:55:38.000000000 +0100
  91714. @@ -0,0 +1,133 @@
  91715. +#!/usr/bin/perl -w
  91716. +#
  91717. +# Run this program on the integrator.
  91718. +#
  91719. +# - Tests module parameter default values.
  91720. +# - Tests setting of valid module parameter values via modprobe.
  91721. +# - Tests invalid module parameter values.
  91722. +# -----------------------------------------------------------------------------
  91723. +use strict;
  91724. +use dwc_otg_test;
  91725. +
  91726. +check_arch() or die;
  91727. +
  91728. +#
  91729. +#
  91730. +sub test {
  91731. + my ($param,$expected) = @_;
  91732. + my $value = get($param);
  91733. +
  91734. + if ($value == $expected) {
  91735. + print "$param = $value, okay\n";
  91736. + }
  91737. +
  91738. + else {
  91739. + warn "ERROR: value of $param != $expected, $value\n";
  91740. + $errors ++;
  91741. + }
  91742. +}
  91743. +
  91744. +#
  91745. +#
  91746. +sub get {
  91747. + my $param = shift;
  91748. + my $tmp = `cat $paramdir/$param`;
  91749. + chomp $tmp;
  91750. + return $tmp;
  91751. +}
  91752. +
  91753. +#
  91754. +#
  91755. +sub test_main {
  91756. +
  91757. + print "\nTesting Module Parameters\n";
  91758. +
  91759. + load_module("") or die;
  91760. +
  91761. + # Test initial values
  91762. + print "\nTesting Default Values\n";
  91763. + foreach (@{$params}) {
  91764. + test ($_->{NAME}, $_->{DEFAULT});
  91765. + }
  91766. +
  91767. + # Test low value
  91768. + print "\nTesting Low Value\n";
  91769. + my $cmd_params = "";
  91770. + foreach (@{$params}) {
  91771. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  91772. + }
  91773. + load_module($cmd_params) or die;
  91774. +
  91775. + foreach (@{$params}) {
  91776. + test ($_->{NAME}, $_->{LOW});
  91777. + }
  91778. +
  91779. + # Test high value
  91780. + print "\nTesting High Value\n";
  91781. + $cmd_params = "";
  91782. + foreach (@{$params}) {
  91783. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  91784. + }
  91785. + load_module($cmd_params) or die;
  91786. +
  91787. + foreach (@{$params}) {
  91788. + test ($_->{NAME}, $_->{HIGH});
  91789. + }
  91790. +
  91791. + # Test Enum
  91792. + print "\nTesting Enumerated\n";
  91793. + foreach (@{$params}) {
  91794. + if (defined $_->{ENUM}) {
  91795. + my $value;
  91796. + foreach $value (@{$_->{ENUM}}) {
  91797. + $cmd_params = "$_->{NAME}=$value";
  91798. + load_module($cmd_params) or die;
  91799. + test ($_->{NAME}, $value);
  91800. + }
  91801. + }
  91802. + }
  91803. +
  91804. + # Test Invalid Values
  91805. + print "\nTesting Invalid Values\n";
  91806. + $cmd_params = "";
  91807. + foreach (@{$params}) {
  91808. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  91809. + }
  91810. + load_module($cmd_params) or die;
  91811. +
  91812. + foreach (@{$params}) {
  91813. + test ($_->{NAME}, $_->{DEFAULT});
  91814. + }
  91815. +
  91816. + $cmd_params = "";
  91817. + foreach (@{$params}) {
  91818. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  91819. + }
  91820. + load_module($cmd_params) or die;
  91821. +
  91822. + foreach (@{$params}) {
  91823. + test ($_->{NAME}, $_->{DEFAULT});
  91824. + }
  91825. +
  91826. + print "\nTesting Enumerated\n";
  91827. + foreach (@{$params}) {
  91828. + if (defined $_->{ENUM}) {
  91829. + my $value;
  91830. + foreach $value (@{$_->{ENUM}}) {
  91831. + $value = $value + 1;
  91832. + $cmd_params = "$_->{NAME}=$value";
  91833. + load_module($cmd_params) or die;
  91834. + test ($_->{NAME}, $_->{DEFAULT});
  91835. + $value = $value - 2;
  91836. + $cmd_params = "$_->{NAME}=$value";
  91837. + load_module($cmd_params) or die;
  91838. + test ($_->{NAME}, $_->{DEFAULT});
  91839. + }
  91840. + }
  91841. + }
  91842. +
  91843. + test_status() or die;
  91844. +}
  91845. +
  91846. +test_main();
  91847. +0;
  91848. diff -Nur linux-3.13.6/drivers/usb/host/dwc_otg/test/test_sysfs.pl linux-raspberry-pi/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  91849. --- linux-3.13.6/drivers/usb/host/dwc_otg/test/test_sysfs.pl 1970-01-01 01:00:00.000000000 +0100
  91850. +++ linux-raspberry-pi/drivers/usb/host/dwc_otg/test/test_sysfs.pl 2014-03-11 16:55:38.000000000 +0100
  91851. @@ -0,0 +1,193 @@
  91852. +#!/usr/bin/perl -w
  91853. +#
  91854. +# Run this program on the integrator
  91855. +# - Tests select sysfs attributes.
  91856. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  91857. +# -----------------------------------------------------------------------------
  91858. +use strict;
  91859. +use dwc_otg_test;
  91860. +
  91861. +check_arch() or die;
  91862. +
  91863. +#
  91864. +#
  91865. +sub test {
  91866. + my ($attr,$expected) = @_;
  91867. + my $string = get($attr);
  91868. +
  91869. + if ($string eq $expected) {
  91870. + printf("$attr = $string, okay\n");
  91871. + }
  91872. + else {
  91873. + warn "ERROR: value of $attr != $expected, $string\n";
  91874. + $errors ++;
  91875. + }
  91876. +}
  91877. +
  91878. +#
  91879. +#
  91880. +sub set {
  91881. + my ($reg, $value) = @_;
  91882. + system "echo $value > $sysfsdir/$reg";
  91883. +}
  91884. +
  91885. +#
  91886. +#
  91887. +sub get {
  91888. + my $attr = shift;
  91889. + my $string = `cat $sysfsdir/$attr`;
  91890. + chomp $string;
  91891. + if ($string =~ m/\s\=\s/) {
  91892. + my $tmp;
  91893. + ($tmp, $string) = split /\s=\s/, $string;
  91894. + }
  91895. + return $string;
  91896. +}
  91897. +
  91898. +#
  91899. +#
  91900. +sub test_main {
  91901. + print("\nTesting Sysfs Attributes\n");
  91902. +
  91903. + load_module("") or die;
  91904. +
  91905. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  91906. + print("\nTesting Default Values\n");
  91907. +
  91908. + test("regoffset", "0xffffffff");
  91909. + test("regvalue", "invalid offset");
  91910. + test("guid", "0x12345678"); # this will fail if it has been changed
  91911. + test("gsnpsid", "0x4f54200a");
  91912. +
  91913. + # Test operation of regoffset/regvalue
  91914. + print("\nTesting regoffset\n");
  91915. + set('regoffset', '5a5a5a5a');
  91916. + test("regoffset", "0xffffffff");
  91917. +
  91918. + set('regoffset', '0');
  91919. + test("regoffset", "0x00000000");
  91920. +
  91921. + set('regoffset', '40000');
  91922. + test("regoffset", "0x00000000");
  91923. +
  91924. + set('regoffset', '3ffff');
  91925. + test("regoffset", "0x0003ffff");
  91926. +
  91927. + set('regoffset', '1');
  91928. + test("regoffset", "0x00000001");
  91929. +
  91930. + print("\nTesting regvalue\n");
  91931. + set('regoffset', '3c');
  91932. + test("regvalue", "0x12345678");
  91933. + set('regvalue', '5a5a5a5a');
  91934. + test("regvalue", "0x5a5a5a5a");
  91935. + set('regvalue','a5a5a5a5');
  91936. + test("regvalue", "0xa5a5a5a5");
  91937. + set('guid','12345678');
  91938. +
  91939. + # Test HNP Capable
  91940. + print("\nTesting HNP Capable bit\n");
  91941. + set('hnpcapable', '1');
  91942. + test("hnpcapable", "0x1");
  91943. + set('hnpcapable','0');
  91944. + test("hnpcapable", "0x0");
  91945. +
  91946. + set('regoffset','0c');
  91947. +
  91948. + my $old = get('gusbcfg');
  91949. + print("setting hnpcapable\n");
  91950. + set('hnpcapable', '1');
  91951. + test("hnpcapable", "0x1");
  91952. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  91953. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  91954. +
  91955. + $old = get('gusbcfg');
  91956. + print("clearing hnpcapable\n");
  91957. + set('hnpcapable', '0');
  91958. + test("hnpcapable", "0x0");
  91959. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  91960. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  91961. +
  91962. + # Test SRP Capable
  91963. + print("\nTesting SRP Capable bit\n");
  91964. + set('srpcapable', '1');
  91965. + test("srpcapable", "0x1");
  91966. + set('srpcapable','0');
  91967. + test("srpcapable", "0x0");
  91968. +
  91969. + set('regoffset','0c');
  91970. +
  91971. + $old = get('gusbcfg');
  91972. + print("setting srpcapable\n");
  91973. + set('srpcapable', '1');
  91974. + test("srpcapable", "0x1");
  91975. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  91976. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  91977. +
  91978. + $old = get('gusbcfg');
  91979. + print("clearing srpcapable\n");
  91980. + set('srpcapable', '0');
  91981. + test("srpcapable", "0x0");
  91982. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  91983. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  91984. +
  91985. + # Test GGPIO
  91986. + print("\nTesting GGPIO\n");
  91987. + set('ggpio','5a5a5a5a');
  91988. + test('ggpio','0x5a5a0000');
  91989. + set('ggpio','a5a5a5a5');
  91990. + test('ggpio','0xa5a50000');
  91991. + set('ggpio','11110000');
  91992. + test('ggpio','0x11110000');
  91993. + set('ggpio','00001111');
  91994. + test('ggpio','0x00000000');
  91995. +
  91996. + # Test DEVSPEED
  91997. + print("\nTesting DEVSPEED\n");
  91998. + set('regoffset','800');
  91999. + $old = get('regvalue');
  92000. + set('devspeed','0');
  92001. + test('devspeed','0x0');
  92002. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  92003. + set('devspeed','1');
  92004. + test('devspeed','0x1');
  92005. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  92006. + set('devspeed','2');
  92007. + test('devspeed','0x2');
  92008. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  92009. + set('devspeed','3');
  92010. + test('devspeed','0x3');
  92011. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  92012. + set('devspeed','4');
  92013. + test('devspeed','0x0');
  92014. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  92015. + set('devspeed','5');
  92016. + test('devspeed','0x1');
  92017. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  92018. +
  92019. +
  92020. + # mode Returns the current mode:0 for device mode1 for host mode Read
  92021. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  92022. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  92023. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  92024. + # bussuspend Suspend the USB bus. Read/Write
  92025. + # busconnected Get the connection status of the bus Read
  92026. +
  92027. + # gotgctl Get or set the Core Control Status Register. Read/Write
  92028. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  92029. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  92030. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  92031. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  92032. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  92033. + ## guid Get or set the value of the User ID Register Read/Write
  92034. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  92035. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  92036. + # enumspeed Gets the device enumeration Speed. Read
  92037. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  92038. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  92039. +
  92040. + test_status("TEST NYI") or die;
  92041. +}
  92042. +
  92043. +test_main();
  92044. +0;
  92045. diff -Nur linux-3.13.6/drivers/usb/host/Kconfig linux-raspberry-pi/drivers/usb/host/Kconfig
  92046. --- linux-3.13.6/drivers/usb/host/Kconfig 2014-03-07 07:07:02.000000000 +0100
  92047. +++ linux-raspberry-pi/drivers/usb/host/Kconfig 2014-03-11 16:55:38.000000000 +0100
  92048. @@ -689,6 +689,19 @@
  92049. To compile this driver a module, choose M here: the module
  92050. will be called "hwa-hc".
  92051. +config USB_DWCOTG
  92052. + tristate "Synopsis DWC host support"
  92053. + depends on USB
  92054. + help
  92055. + The Synopsis DWC controller is a dual-role
  92056. + host/peripheral/OTG ("On The Go") USB controllers.
  92057. +
  92058. + Enable this option to support this IP in host controller mode.
  92059. + If unsure, say N.
  92060. +
  92061. + To compile this driver as a module, choose M here: the
  92062. + modules built will be called dwc_otg and dwc_common_port.
  92063. +
  92064. config USB_IMX21_HCD
  92065. tristate "i.MX21 HCD support"
  92066. depends on ARM && ARCH_MXC
  92067. diff -Nur linux-3.13.6/drivers/usb/host/Makefile linux-raspberry-pi/drivers/usb/host/Makefile
  92068. --- linux-3.13.6/drivers/usb/host/Makefile 2014-03-07 07:07:02.000000000 +0100
  92069. +++ linux-raspberry-pi/drivers/usb/host/Makefile 2014-03-11 16:55:38.000000000 +0100
  92070. @@ -65,6 +65,8 @@
  92071. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  92072. obj-$(CONFIG_USB_ISP1760_HCD) += isp1760.o
  92073. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  92074. +
  92075. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  92076. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  92077. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o
  92078. obj-$(CONFIG_USB_OCTEON2_COMMON) += octeon2-common.o
  92079. diff -Nur linux-3.13.6/drivers/usb/Makefile linux-raspberry-pi/drivers/usb/Makefile
  92080. --- linux-3.13.6/drivers/usb/Makefile 2014-03-07 07:07:02.000000000 +0100
  92081. +++ linux-raspberry-pi/drivers/usb/Makefile 2014-03-11 16:55:37.000000000 +0100
  92082. @@ -23,6 +23,7 @@
  92083. obj-$(CONFIG_USB_R8A66597_HCD) += host/
  92084. obj-$(CONFIG_USB_HWA_HCD) += host/
  92085. obj-$(CONFIG_USB_ISP1760_HCD) += host/
  92086. +obj-$(CONFIG_USB_DWCOTG) += host/
  92087. obj-$(CONFIG_USB_IMX21_HCD) += host/
  92088. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += host/
  92089. obj-$(CONFIG_USB_FUSBH200_HCD) += host/
  92090. diff -Nur linux-3.13.6/drivers/video/bcm2708_fb.c linux-raspberry-pi/drivers/video/bcm2708_fb.c
  92091. --- linux-3.13.6/drivers/video/bcm2708_fb.c 1970-01-01 01:00:00.000000000 +0100
  92092. +++ linux-raspberry-pi/drivers/video/bcm2708_fb.c 2014-03-11 16:55:38.000000000 +0100
  92093. @@ -0,0 +1,765 @@
  92094. +/*
  92095. + * linux/drivers/video/bcm2708_fb.c
  92096. + *
  92097. + * Copyright (C) 2010 Broadcom
  92098. + *
  92099. + * This file is subject to the terms and conditions of the GNU General Public
  92100. + * License. See the file COPYING in the main directory of this archive
  92101. + * for more details.
  92102. + *
  92103. + * Broadcom simple framebuffer driver
  92104. + *
  92105. + * This file is derived from cirrusfb.c
  92106. + * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  92107. + *
  92108. + */
  92109. +#include <linux/module.h>
  92110. +#include <linux/kernel.h>
  92111. +#include <linux/errno.h>
  92112. +#include <linux/string.h>
  92113. +#include <linux/slab.h>
  92114. +#include <linux/mm.h>
  92115. +#include <linux/fb.h>
  92116. +#include <linux/init.h>
  92117. +#include <linux/interrupt.h>
  92118. +#include <linux/ioport.h>
  92119. +#include <linux/list.h>
  92120. +#include <linux/platform_device.h>
  92121. +#include <linux/clk.h>
  92122. +#include <linux/printk.h>
  92123. +#include <linux/console.h>
  92124. +#include <linux/debugfs.h>
  92125. +
  92126. +#include <mach/dma.h>
  92127. +#include <mach/platform.h>
  92128. +#include <mach/vcio.h>
  92129. +
  92130. +#include <asm/sizes.h>
  92131. +#include <linux/io.h>
  92132. +#include <linux/dma-mapping.h>
  92133. +
  92134. +#ifdef BCM2708_FB_DEBUG
  92135. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  92136. +#else
  92137. +#define print_debug(fmt,...)
  92138. +#endif
  92139. +
  92140. +/* This is limited to 16 characters when displayed by X startup */
  92141. +static const char *bcm2708_name = "BCM2708 FB";
  92142. +
  92143. +#define DRIVER_NAME "bcm2708_fb"
  92144. +
  92145. +static u32 dma_busy_wait_threshold = 1<<15;
  92146. +module_param(dma_busy_wait_threshold, int, 0644);
  92147. +MODULE_PARM_DESC(dma_busy_wait_threshold, "Busy-wait for DMA completion below this area");
  92148. +
  92149. +static int fbwidth = 800; /* module parameter */
  92150. +static int fbheight = 480; /* module parameter */
  92151. +static int fbdepth = 16; /* module parameter */
  92152. +static int fbswap = 0; /* module parameter */
  92153. +
  92154. +/* this data structure describes each frame buffer device we find */
  92155. +
  92156. +struct fbinfo_s {
  92157. + u32 xres, yres, xres_virtual, yres_virtual;
  92158. + u32 pitch, bpp;
  92159. + u32 xoffset, yoffset;
  92160. + u32 base;
  92161. + u32 screen_size;
  92162. + u16 cmap[256];
  92163. +};
  92164. +
  92165. +struct bcm2708_fb_stats {
  92166. + struct debugfs_regset32 regset;
  92167. + u32 dma_copies;
  92168. + u32 dma_irqs;
  92169. +};
  92170. +
  92171. +struct bcm2708_fb {
  92172. + struct fb_info fb;
  92173. + struct platform_device *dev;
  92174. + struct fbinfo_s *info;
  92175. + dma_addr_t dma;
  92176. + u32 cmap[16];
  92177. + int dma_chan;
  92178. + int dma_irq;
  92179. + void __iomem *dma_chan_base;
  92180. + void *cb_base; /* DMA control blocks */
  92181. + dma_addr_t cb_handle;
  92182. + struct dentry *debugfs_dir;
  92183. + wait_queue_head_t dma_waitq;
  92184. + struct bcm2708_fb_stats stats;
  92185. +};
  92186. +
  92187. +#define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
  92188. +
  92189. +static void bcm2708_fb_debugfs_deinit(struct bcm2708_fb *fb)
  92190. +{
  92191. + debugfs_remove_recursive(fb->debugfs_dir);
  92192. + fb->debugfs_dir = NULL;
  92193. +}
  92194. +
  92195. +static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
  92196. +{
  92197. + static struct debugfs_reg32 stats_registers[] = {
  92198. + {
  92199. + "dma_copies",
  92200. + offsetof(struct bcm2708_fb_stats, dma_copies)
  92201. + },
  92202. + {
  92203. + "dma_irqs",
  92204. + offsetof(struct bcm2708_fb_stats, dma_irqs)
  92205. + },
  92206. + };
  92207. +
  92208. + fb->debugfs_dir = debugfs_create_dir(DRIVER_NAME, NULL);
  92209. + if (!fb->debugfs_dir) {
  92210. + pr_warn("%s: could not create debugfs entry\n",
  92211. + __func__);
  92212. + return -EFAULT;
  92213. + }
  92214. +
  92215. + fb->stats.regset.regs = stats_registers;
  92216. + fb->stats.regset.nregs = ARRAY_SIZE(stats_registers);
  92217. + fb->stats.regset.base = &fb->stats;
  92218. +
  92219. + if (!debugfs_create_regset32(
  92220. + "stats", 0444, fb->debugfs_dir, &fb->stats.regset)) {
  92221. + pr_warn("%s: could not create statistics registers\n",
  92222. + __func__);
  92223. + goto fail;
  92224. + }
  92225. + return 0;
  92226. +
  92227. +fail:
  92228. + bcm2708_fb_debugfs_deinit(fb);
  92229. + return -EFAULT;
  92230. +}
  92231. +
  92232. +static int bcm2708_fb_set_bitfields(struct fb_var_screeninfo *var)
  92233. +{
  92234. + int ret = 0;
  92235. +
  92236. + memset(&var->transp, 0, sizeof(var->transp));
  92237. +
  92238. + var->red.msb_right = 0;
  92239. + var->green.msb_right = 0;
  92240. + var->blue.msb_right = 0;
  92241. +
  92242. + switch (var->bits_per_pixel) {
  92243. + case 1:
  92244. + case 2:
  92245. + case 4:
  92246. + case 8:
  92247. + var->red.length = var->bits_per_pixel;
  92248. + var->red.offset = 0;
  92249. + var->green.length = var->bits_per_pixel;
  92250. + var->green.offset = 0;
  92251. + var->blue.length = var->bits_per_pixel;
  92252. + var->blue.offset = 0;
  92253. + break;
  92254. + case 16:
  92255. + var->red.length = 5;
  92256. + var->blue.length = 5;
  92257. + /*
  92258. + * Green length can be 5 or 6 depending whether
  92259. + * we're operating in RGB555 or RGB565 mode.
  92260. + */
  92261. + if (var->green.length != 5 && var->green.length != 6)
  92262. + var->green.length = 6;
  92263. + break;
  92264. + case 24:
  92265. + var->red.length = 8;
  92266. + var->blue.length = 8;
  92267. + var->green.length = 8;
  92268. + break;
  92269. + case 32:
  92270. + var->red.length = 8;
  92271. + var->green.length = 8;
  92272. + var->blue.length = 8;
  92273. + var->transp.length = 8;
  92274. + break;
  92275. + default:
  92276. + ret = -EINVAL;
  92277. + break;
  92278. + }
  92279. +
  92280. + /*
  92281. + * >= 16bpp displays have separate colour component bitfields
  92282. + * encoded in the pixel data. Calculate their position from
  92283. + * the bitfield length defined above.
  92284. + */
  92285. + if (ret == 0 && var->bits_per_pixel >= 24 && fbswap) {
  92286. + var->blue.offset = 0;
  92287. + var->green.offset = var->blue.offset + var->blue.length;
  92288. + var->red.offset = var->green.offset + var->green.length;
  92289. + var->transp.offset = var->red.offset + var->red.length;
  92290. + } else if (ret == 0 && var->bits_per_pixel >= 24) {
  92291. + var->red.offset = 0;
  92292. + var->green.offset = var->red.offset + var->red.length;
  92293. + var->blue.offset = var->green.offset + var->green.length;
  92294. + var->transp.offset = var->blue.offset + var->blue.length;
  92295. + } else if (ret == 0 && var->bits_per_pixel >= 16) {
  92296. + var->blue.offset = 0;
  92297. + var->green.offset = var->blue.offset + var->blue.length;
  92298. + var->red.offset = var->green.offset + var->green.length;
  92299. + var->transp.offset = var->red.offset + var->red.length;
  92300. + }
  92301. +
  92302. + return ret;
  92303. +}
  92304. +
  92305. +static int bcm2708_fb_check_var(struct fb_var_screeninfo *var,
  92306. + struct fb_info *info)
  92307. +{
  92308. + /* info input, var output */
  92309. + int yres;
  92310. +
  92311. + /* info input, var output */
  92312. + print_debug("bcm2708_fb_check_var info(%p) %dx%d (%dx%d), %d, %d\n", info,
  92313. + info->var.xres, info->var.yres, info->var.xres_virtual,
  92314. + info->var.yres_virtual, (int)info->screen_size,
  92315. + info->var.bits_per_pixel);
  92316. + print_debug("bcm2708_fb_check_var var(%p) %dx%d (%dx%d), %d\n", var,
  92317. + var->xres, var->yres, var->xres_virtual, var->yres_virtual,
  92318. + var->bits_per_pixel);
  92319. +
  92320. + if (!var->bits_per_pixel)
  92321. + var->bits_per_pixel = 16;
  92322. +
  92323. + if (bcm2708_fb_set_bitfields(var) != 0) {
  92324. + pr_err("bcm2708_fb_check_var: invalid bits_per_pixel %d\n",
  92325. + var->bits_per_pixel);
  92326. + return -EINVAL;
  92327. + }
  92328. +
  92329. +
  92330. + if (var->xres_virtual < var->xres)
  92331. + var->xres_virtual = var->xres;
  92332. + /* use highest possible virtual resolution */
  92333. + if (var->yres_virtual == -1) {
  92334. + var->yres_virtual = 480;
  92335. +
  92336. + pr_err
  92337. + ("bcm2708_fb_check_var: virtual resolution set to maximum of %dx%d\n",
  92338. + var->xres_virtual, var->yres_virtual);
  92339. + }
  92340. + if (var->yres_virtual < var->yres)
  92341. + var->yres_virtual = var->yres;
  92342. +
  92343. + if (var->xoffset < 0)
  92344. + var->xoffset = 0;
  92345. + if (var->yoffset < 0)
  92346. + var->yoffset = 0;
  92347. +
  92348. + /* truncate xoffset and yoffset to maximum if too high */
  92349. + if (var->xoffset > var->xres_virtual - var->xres)
  92350. + var->xoffset = var->xres_virtual - var->xres - 1;
  92351. + if (var->yoffset > var->yres_virtual - var->yres)
  92352. + var->yoffset = var->yres_virtual - var->yres - 1;
  92353. +
  92354. + yres = var->yres;
  92355. + if (var->vmode & FB_VMODE_DOUBLE)
  92356. + yres *= 2;
  92357. + else if (var->vmode & FB_VMODE_INTERLACED)
  92358. + yres = (yres + 1) / 2;
  92359. +
  92360. + if (var->xres * yres > 1920 * 1200) {
  92361. + pr_err("bcm2708_fb_check_var: ERROR: Pixel size >= 1920x1200; "
  92362. + "special treatment required! (TODO)\n");
  92363. + return -EINVAL;
  92364. + }
  92365. +
  92366. + return 0;
  92367. +}
  92368. +
  92369. +static int bcm2708_fb_set_par(struct fb_info *info)
  92370. +{
  92371. + uint32_t val = 0;
  92372. + struct bcm2708_fb *fb = to_bcm2708(info);
  92373. + volatile struct fbinfo_s *fbinfo = fb->info;
  92374. + fbinfo->xres = info->var.xres;
  92375. + fbinfo->yres = info->var.yres;
  92376. + fbinfo->xres_virtual = info->var.xres_virtual;
  92377. + fbinfo->yres_virtual = info->var.yres_virtual;
  92378. + fbinfo->bpp = info->var.bits_per_pixel;
  92379. + fbinfo->xoffset = info->var.xoffset;
  92380. + fbinfo->yoffset = info->var.yoffset;
  92381. + fbinfo->base = 0; /* filled in by VC */
  92382. + fbinfo->pitch = 0; /* filled in by VC */
  92383. +
  92384. + print_debug("bcm2708_fb_set_par info(%p) %dx%d (%dx%d), %d, %d\n", info,
  92385. + info->var.xres, info->var.yres, info->var.xres_virtual,
  92386. + info->var.yres_virtual, (int)info->screen_size,
  92387. + info->var.bits_per_pixel);
  92388. +
  92389. + /* ensure last write to fbinfo is visible to GPU */
  92390. + wmb();
  92391. +
  92392. + /* inform vc about new framebuffer */
  92393. + bcm_mailbox_write(MBOX_CHAN_FB, fb->dma);
  92394. +
  92395. + /* TODO: replace fb driver with vchiq version */
  92396. + /* wait for response */
  92397. + bcm_mailbox_read(MBOX_CHAN_FB, &val);
  92398. +
  92399. + /* ensure GPU writes are visible to us */
  92400. + rmb();
  92401. +
  92402. + if (val == 0) {
  92403. + fb->fb.fix.line_length = fbinfo->pitch;
  92404. +
  92405. + if (info->var.bits_per_pixel <= 8)
  92406. + fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  92407. + else
  92408. + fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  92409. +
  92410. + fb->fb.fix.smem_start = fbinfo->base;
  92411. + fb->fb.fix.smem_len = fbinfo->pitch * fbinfo->yres_virtual;
  92412. + fb->fb.screen_size = fbinfo->screen_size;
  92413. + if (fb->fb.screen_base)
  92414. + iounmap(fb->fb.screen_base);
  92415. + fb->fb.screen_base =
  92416. + (void *)ioremap_wc(fb->fb.fix.smem_start, fb->fb.screen_size);
  92417. + if (!fb->fb.screen_base) {
  92418. + /* the console may currently be locked */
  92419. + console_trylock();
  92420. + console_unlock();
  92421. +
  92422. + BUG(); /* what can we do here */
  92423. + }
  92424. + }
  92425. + print_debug
  92426. + ("BCM2708FB: start = %p,%p width=%d, height=%d, bpp=%d, pitch=%d size=%d success=%d\n",
  92427. + (void *)fb->fb.screen_base, (void *)fb->fb.fix.smem_start,
  92428. + fbinfo->xres, fbinfo->yres, fbinfo->bpp,
  92429. + fbinfo->pitch, (int)fb->fb.screen_size, val);
  92430. +
  92431. + return val;
  92432. +}
  92433. +
  92434. +static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  92435. +{
  92436. + unsigned int mask = (1 << bf->length) - 1;
  92437. +
  92438. + return (val >> (16 - bf->length) & mask) << bf->offset;
  92439. +}
  92440. +
  92441. +
  92442. +static int bcm2708_fb_setcolreg(unsigned int regno, unsigned int red,
  92443. + unsigned int green, unsigned int blue,
  92444. + unsigned int transp, struct fb_info *info)
  92445. +{
  92446. + struct bcm2708_fb *fb = to_bcm2708(info);
  92447. +
  92448. + /*print_debug("BCM2708FB: setcolreg %d:(%02x,%02x,%02x,%02x) %x\n", regno, red, green, blue, transp, fb->fb.fix.visual);*/
  92449. + if (fb->fb.var.bits_per_pixel <= 8) {
  92450. + if (regno < 256) {
  92451. + /* blue [0:4], green [5:10], red [11:15] */
  92452. + fb->info->cmap[regno] = ((red >> (16-5)) & 0x1f) << 11 |
  92453. + ((green >> (16-6)) & 0x3f) << 5 |
  92454. + ((blue >> (16-5)) & 0x1f) << 0;
  92455. + }
  92456. + /* 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 */
  92457. + /* So just call it for what looks like the last colour in a list for now. */
  92458. + if (regno == 15 || regno == 255)
  92459. + bcm2708_fb_set_par(info);
  92460. + } else if (regno < 16) {
  92461. + fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  92462. + convert_bitfield(blue, &fb->fb.var.blue) |
  92463. + convert_bitfield(green, &fb->fb.var.green) |
  92464. + convert_bitfield(red, &fb->fb.var.red);
  92465. + }
  92466. + return regno > 255;
  92467. +}
  92468. +
  92469. +static int bcm2708_fb_blank(int blank_mode, struct fb_info *info)
  92470. +{
  92471. + /*print_debug("bcm2708_fb_blank\n"); */
  92472. + return -1;
  92473. +}
  92474. +
  92475. +static void bcm2708_fb_fillrect(struct fb_info *info,
  92476. + const struct fb_fillrect *rect)
  92477. +{
  92478. + /* (is called) print_debug("bcm2708_fb_fillrect\n"); */
  92479. + cfb_fillrect(info, rect);
  92480. +}
  92481. +
  92482. +/* A helper function for configuring dma control block */
  92483. +static void set_dma_cb(struct bcm2708_dma_cb *cb,
  92484. + int burst_size,
  92485. + dma_addr_t dst,
  92486. + int dst_stride,
  92487. + dma_addr_t src,
  92488. + int src_stride,
  92489. + int w,
  92490. + int h)
  92491. +{
  92492. + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
  92493. + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
  92494. + BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
  92495. + cb->dst = dst;
  92496. + cb->src = src;
  92497. + /*
  92498. + * This is not really obvious from the DMA documentation,
  92499. + * but the top 16 bits must be programmmed to "height -1"
  92500. + * and not "height" in 2D mode.
  92501. + */
  92502. + cb->length = ((h - 1) << 16) | w;
  92503. + cb->stride = ((dst_stride - w) << 16) | (u16)(src_stride - w);
  92504. + cb->pad[0] = 0;
  92505. + cb->pad[1] = 0;
  92506. +}
  92507. +
  92508. +static void bcm2708_fb_copyarea(struct fb_info *info,
  92509. + const struct fb_copyarea *region)
  92510. +{
  92511. + struct bcm2708_fb *fb = to_bcm2708(info);
  92512. + struct bcm2708_dma_cb *cb = fb->cb_base;
  92513. + int bytes_per_pixel = (info->var.bits_per_pixel + 7) >> 3;
  92514. + /* Channel 0 supports larger bursts and is a bit faster */
  92515. + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
  92516. + int pixels = region->width * region->height;
  92517. +
  92518. + /* Fallback to cfb_copyarea() if we don't like something */
  92519. + if (bytes_per_pixel > 4 ||
  92520. + info->var.xres * info->var.yres > 1920 * 1200 ||
  92521. + region->width <= 0 || region->width > info->var.xres ||
  92522. + region->height <= 0 || region->height > info->var.yres ||
  92523. + region->sx < 0 || region->sx >= info->var.xres ||
  92524. + region->sy < 0 || region->sy >= info->var.yres ||
  92525. + region->dx < 0 || region->dx >= info->var.xres ||
  92526. + region->dy < 0 || region->dy >= info->var.yres ||
  92527. + region->sx + region->width > info->var.xres ||
  92528. + region->dx + region->width > info->var.xres ||
  92529. + region->sy + region->height > info->var.yres ||
  92530. + region->dy + region->height > info->var.yres) {
  92531. + cfb_copyarea(info, region);
  92532. + return;
  92533. + }
  92534. +
  92535. + if (region->dy == region->sy && region->dx > region->sx) {
  92536. + /*
  92537. + * A difficult case of overlapped copy. Because DMA can't
  92538. + * copy individual scanlines in backwards direction, we need
  92539. + * two-pass processing. We do it by programming a chain of dma
  92540. + * control blocks in the first 16K part of the buffer and use
  92541. + * the remaining 48K as the intermediate temporary scratch
  92542. + * buffer. The buffer size is sufficient to handle up to
  92543. + * 1920x1200 resolution at 32bpp pixel depth.
  92544. + */
  92545. + int y;
  92546. + dma_addr_t control_block_pa = fb->cb_handle;
  92547. + dma_addr_t scratchbuf = fb->cb_handle + 16 * 1024;
  92548. + int scanline_size = bytes_per_pixel * region->width;
  92549. + int scanlines_per_cb = (64 * 1024 - 16 * 1024) / scanline_size;
  92550. +
  92551. + for (y = 0; y < region->height; y += scanlines_per_cb) {
  92552. + dma_addr_t src =
  92553. + fb->fb.fix.smem_start +
  92554. + bytes_per_pixel * region->sx +
  92555. + (region->sy + y) * fb->fb.fix.line_length;
  92556. + dma_addr_t dst =
  92557. + fb->fb.fix.smem_start +
  92558. + bytes_per_pixel * region->dx +
  92559. + (region->dy + y) * fb->fb.fix.line_length;
  92560. +
  92561. + if (region->height - y < scanlines_per_cb)
  92562. + scanlines_per_cb = region->height - y;
  92563. +
  92564. + set_dma_cb(cb, burst_size, scratchbuf, scanline_size,
  92565. + src, fb->fb.fix.line_length,
  92566. + scanline_size, scanlines_per_cb);
  92567. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  92568. + cb->next = control_block_pa;
  92569. + cb++;
  92570. +
  92571. + set_dma_cb(cb, burst_size, dst, fb->fb.fix.line_length,
  92572. + scratchbuf, scanline_size,
  92573. + scanline_size, scanlines_per_cb);
  92574. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  92575. + cb->next = control_block_pa;
  92576. + cb++;
  92577. + }
  92578. + /* move the pointer back to the last dma control block */
  92579. + cb--;
  92580. + } else {
  92581. + /* A single dma control block is enough. */
  92582. + int sy, dy, stride;
  92583. + if (region->dy <= region->sy) {
  92584. + /* processing from top to bottom */
  92585. + dy = region->dy;
  92586. + sy = region->sy;
  92587. + stride = fb->fb.fix.line_length;
  92588. + } else {
  92589. + /* processing from bottom to top */
  92590. + dy = region->dy + region->height - 1;
  92591. + sy = region->sy + region->height - 1;
  92592. + stride = -fb->fb.fix.line_length;
  92593. + }
  92594. + set_dma_cb(cb, burst_size,
  92595. + fb->fb.fix.smem_start + dy * fb->fb.fix.line_length +
  92596. + bytes_per_pixel * region->dx,
  92597. + stride,
  92598. + fb->fb.fix.smem_start + sy * fb->fb.fix.line_length +
  92599. + bytes_per_pixel * region->sx,
  92600. + stride,
  92601. + region->width * bytes_per_pixel,
  92602. + region->height);
  92603. + }
  92604. +
  92605. + /* end of dma control blocks chain */
  92606. + cb->next = 0;
  92607. +
  92608. +
  92609. + if (pixels < dma_busy_wait_threshold) {
  92610. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  92611. + bcm_dma_wait_idle(fb->dma_chan_base);
  92612. + } else {
  92613. + void __iomem *dma_chan = fb->dma_chan_base;
  92614. + cb->info |= BCM2708_DMA_INT_EN;
  92615. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  92616. + while (bcm_dma_is_busy(dma_chan)) {
  92617. + wait_event_interruptible(
  92618. + fb->dma_waitq,
  92619. + !bcm_dma_is_busy(dma_chan));
  92620. + }
  92621. + fb->stats.dma_irqs++;
  92622. + }
  92623. + fb->stats.dma_copies++;
  92624. +}
  92625. +
  92626. +static void bcm2708_fb_imageblit(struct fb_info *info,
  92627. + const struct fb_image *image)
  92628. +{
  92629. + /* (is called) print_debug("bcm2708_fb_imageblit\n"); */
  92630. + cfb_imageblit(info, image);
  92631. +}
  92632. +
  92633. +static irqreturn_t bcm2708_fb_dma_irq(int irq, void *cxt)
  92634. +{
  92635. + struct bcm2708_fb *fb = cxt;
  92636. +
  92637. + /* FIXME: should read status register to check if this is
  92638. + * actually interrupting us or not, in case this interrupt
  92639. + * ever becomes shared amongst several DMA channels
  92640. + *
  92641. + * readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_IRQ;
  92642. + */
  92643. +
  92644. + /* acknowledge the interrupt */
  92645. + writel(BCM2708_DMA_INT, fb->dma_chan_base + BCM2708_DMA_CS);
  92646. +
  92647. + wake_up(&fb->dma_waitq);
  92648. + return IRQ_HANDLED;
  92649. +}
  92650. +
  92651. +static struct fb_ops bcm2708_fb_ops = {
  92652. + .owner = THIS_MODULE,
  92653. + .fb_check_var = bcm2708_fb_check_var,
  92654. + .fb_set_par = bcm2708_fb_set_par,
  92655. + .fb_setcolreg = bcm2708_fb_setcolreg,
  92656. + .fb_blank = bcm2708_fb_blank,
  92657. + .fb_fillrect = bcm2708_fb_fillrect,
  92658. + .fb_copyarea = bcm2708_fb_copyarea,
  92659. + .fb_imageblit = bcm2708_fb_imageblit,
  92660. +};
  92661. +
  92662. +static int bcm2708_fb_register(struct bcm2708_fb *fb)
  92663. +{
  92664. + int ret;
  92665. + dma_addr_t dma;
  92666. + void *mem;
  92667. +
  92668. + mem =
  92669. + dma_alloc_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), &dma,
  92670. + GFP_KERNEL);
  92671. +
  92672. + if (NULL == mem) {
  92673. + pr_err(": unable to allocate fbinfo buffer\n");
  92674. + ret = -ENOMEM;
  92675. + } else {
  92676. + fb->info = (struct fbinfo_s *)mem;
  92677. + fb->dma = dma;
  92678. + }
  92679. + fb->fb.fbops = &bcm2708_fb_ops;
  92680. + fb->fb.flags = FBINFO_FLAG_DEFAULT | FBINFO_HWACCEL_COPYAREA;
  92681. + fb->fb.pseudo_palette = fb->cmap;
  92682. +
  92683. + strncpy(fb->fb.fix.id, bcm2708_name, sizeof(fb->fb.fix.id));
  92684. + fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  92685. + fb->fb.fix.type_aux = 0;
  92686. + fb->fb.fix.xpanstep = 0;
  92687. + fb->fb.fix.ypanstep = 0;
  92688. + fb->fb.fix.ywrapstep = 0;
  92689. + fb->fb.fix.accel = FB_ACCEL_NONE;
  92690. +
  92691. + fb->fb.var.xres = fbwidth;
  92692. + fb->fb.var.yres = fbheight;
  92693. + fb->fb.var.xres_virtual = fbwidth;
  92694. + fb->fb.var.yres_virtual = fbheight;
  92695. + fb->fb.var.bits_per_pixel = fbdepth;
  92696. + fb->fb.var.vmode = FB_VMODE_NONINTERLACED;
  92697. + fb->fb.var.activate = FB_ACTIVATE_NOW;
  92698. + fb->fb.var.nonstd = 0;
  92699. + fb->fb.var.height = -1; /* height of picture in mm */
  92700. + fb->fb.var.width = -1; /* width of picture in mm */
  92701. + fb->fb.var.accel_flags = 0;
  92702. +
  92703. + fb->fb.monspecs.hfmin = 0;
  92704. + fb->fb.monspecs.hfmax = 100000;
  92705. + fb->fb.monspecs.vfmin = 0;
  92706. + fb->fb.monspecs.vfmax = 400;
  92707. + fb->fb.monspecs.dclkmin = 1000000;
  92708. + fb->fb.monspecs.dclkmax = 100000000;
  92709. +
  92710. + bcm2708_fb_set_bitfields(&fb->fb.var);
  92711. + init_waitqueue_head(&fb->dma_waitq);
  92712. +
  92713. + /*
  92714. + * Allocate colourmap.
  92715. + */
  92716. +
  92717. + fb_set_var(&fb->fb, &fb->fb.var);
  92718. +
  92719. + print_debug("BCM2708FB: registering framebuffer (%dx%d@%d) (%d)\n", fbwidth
  92720. + fbheight, fbdepth, fbswap);
  92721. +
  92722. + ret = register_framebuffer(&fb->fb);
  92723. + print_debug("BCM2708FB: register framebuffer (%d)\n", ret);
  92724. + if (ret == 0)
  92725. + goto out;
  92726. +
  92727. + print_debug("BCM2708FB: cannot register framebuffer (%d)\n", ret);
  92728. +out:
  92729. + return ret;
  92730. +}
  92731. +
  92732. +static int bcm2708_fb_probe(struct platform_device *dev)
  92733. +{
  92734. + struct bcm2708_fb *fb;
  92735. + int ret;
  92736. +
  92737. + fb = kzalloc(sizeof(struct bcm2708_fb), GFP_KERNEL);
  92738. + if (!fb) {
  92739. + dev_err(&dev->dev,
  92740. + "could not allocate new bcm2708_fb struct\n");
  92741. + ret = -ENOMEM;
  92742. + goto free_region;
  92743. + }
  92744. +
  92745. + bcm2708_fb_debugfs_init(fb);
  92746. +
  92747. +
  92748. + bcm2708_fb_debugfs_init(fb);
  92749. +
  92750. + fb->cb_base = dma_alloc_writecombine(&dev->dev, SZ_64K,
  92751. + &fb->cb_handle, GFP_KERNEL);
  92752. + if (!fb->cb_base) {
  92753. + dev_err(&dev->dev, "cannot allocate DMA CBs\n");
  92754. + ret = -ENOMEM;
  92755. + goto free_fb;
  92756. + }
  92757. +
  92758. + pr_info("BCM2708FB: allocated DMA memory %08x\n",
  92759. + fb->cb_handle);
  92760. +
  92761. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_BULK,
  92762. + &fb->dma_chan_base, &fb->dma_irq);
  92763. + if (ret < 0) {
  92764. + dev_err(&dev->dev, "couldn't allocate a DMA channel\n");
  92765. + goto free_cb;
  92766. + }
  92767. + fb->dma_chan = ret;
  92768. +
  92769. + ret = request_irq(fb->dma_irq, bcm2708_fb_dma_irq,
  92770. + 0, "bcm2708_fb dma", fb);
  92771. + if (ret) {
  92772. + pr_err("%s: failed to request DMA irq\n", __func__);
  92773. + goto free_dma_chan;
  92774. + }
  92775. +
  92776. +
  92777. + pr_info("BCM2708FB: allocated DMA channel %d @ %p\n",
  92778. + fb->dma_chan, fb->dma_chan_base);
  92779. +
  92780. + fb->dev = dev;
  92781. +
  92782. + ret = bcm2708_fb_register(fb);
  92783. + if (ret == 0) {
  92784. + platform_set_drvdata(dev, fb);
  92785. + goto out;
  92786. + }
  92787. +
  92788. +free_dma_chan:
  92789. + bcm_dma_chan_free(fb->dma_chan);
  92790. +free_cb:
  92791. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  92792. +free_fb:
  92793. + kfree(fb);
  92794. +free_region:
  92795. + dev_err(&dev->dev, "probe failed, err %d\n", ret);
  92796. +out:
  92797. + return ret;
  92798. +}
  92799. +
  92800. +static int bcm2708_fb_remove(struct platform_device *dev)
  92801. +{
  92802. + struct bcm2708_fb *fb = platform_get_drvdata(dev);
  92803. +
  92804. + platform_set_drvdata(dev, NULL);
  92805. +
  92806. + if (fb->fb.screen_base)
  92807. + iounmap(fb->fb.screen_base);
  92808. + unregister_framebuffer(&fb->fb);
  92809. +
  92810. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  92811. + bcm_dma_chan_free(fb->dma_chan);
  92812. +
  92813. + dma_free_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), (void *)fb->info,
  92814. + fb->dma);
  92815. + bcm2708_fb_debugfs_deinit(fb);
  92816. +
  92817. + free_irq(fb->dma_irq, fb);
  92818. +
  92819. + kfree(fb);
  92820. +
  92821. + return 0;
  92822. +}
  92823. +
  92824. +static struct platform_driver bcm2708_fb_driver = {
  92825. + .probe = bcm2708_fb_probe,
  92826. + .remove = bcm2708_fb_remove,
  92827. + .driver = {
  92828. + .name = DRIVER_NAME,
  92829. + .owner = THIS_MODULE,
  92830. + },
  92831. +};
  92832. +
  92833. +static int __init bcm2708_fb_init(void)
  92834. +{
  92835. + return platform_driver_register(&bcm2708_fb_driver);
  92836. +}
  92837. +
  92838. +module_init(bcm2708_fb_init);
  92839. +
  92840. +static void __exit bcm2708_fb_exit(void)
  92841. +{
  92842. + platform_driver_unregister(&bcm2708_fb_driver);
  92843. +}
  92844. +
  92845. +module_exit(bcm2708_fb_exit);
  92846. +
  92847. +module_param(fbwidth, int, 0644);
  92848. +module_param(fbheight, int, 0644);
  92849. +module_param(fbdepth, int, 0644);
  92850. +module_param(fbswap, int, 0644);
  92851. +
  92852. +MODULE_DESCRIPTION("BCM2708 framebuffer driver");
  92853. +MODULE_LICENSE("GPL");
  92854. +
  92855. +MODULE_PARM_DESC(fbwidth, "Width of ARM Framebuffer");
  92856. +MODULE_PARM_DESC(fbheight, "Height of ARM Framebuffer");
  92857. +MODULE_PARM_DESC(fbdepth, "Bit depth of ARM Framebuffer");
  92858. +MODULE_PARM_DESC(fbswap, "Swap order of red and blue in 24 and 32 bit modes");
  92859. diff -Nur linux-3.13.6/drivers/video/cfbimgblt.c linux-raspberry-pi/drivers/video/cfbimgblt.c
  92860. --- linux-3.13.6/drivers/video/cfbimgblt.c 2014-03-07 07:07:02.000000000 +0100
  92861. +++ linux-raspberry-pi/drivers/video/cfbimgblt.c 2014-03-11 16:55:38.000000000 +0100
  92862. @@ -28,6 +28,11 @@
  92863. *
  92864. * Also need to add code to deal with cards endians that are different than
  92865. * the native cpu endians. I also need to deal with MSB position in the word.
  92866. + * Modified by Harm Hanemaaijer (fgenfb@yahoo.com) 2013:
  92867. + * - Provide optimized versions of fast_imageblit for 16 and 32bpp that are
  92868. + * significantly faster than the previous implementation.
  92869. + * - Simplify the fast/slow_imageblit selection code, avoiding integer
  92870. + * divides.
  92871. */
  92872. #include <linux/module.h>
  92873. #include <linux/string.h>
  92874. @@ -262,6 +267,133 @@
  92875. }
  92876. }
  92877. +/*
  92878. + * Optimized fast_imageblit for bpp == 16. ppw = 2, bit_mask = 3 folded
  92879. + * into the code, main loop unrolled.
  92880. + */
  92881. +
  92882. +static inline void fast_imageblit16(const struct fb_image *image,
  92883. + struct fb_info *p, u8 __iomem * dst1,
  92884. + u32 fgcolor, u32 bgcolor)
  92885. +{
  92886. + u32 fgx = fgcolor, bgx = bgcolor;
  92887. + u32 spitch = (image->width + 7) / 8;
  92888. + u32 end_mask, eorx;
  92889. + const char *s = image->data, *src;
  92890. + u32 __iomem *dst;
  92891. + const u32 *tab = NULL;
  92892. + int i, j, k;
  92893. +
  92894. + tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  92895. +
  92896. + fgx <<= 16;
  92897. + bgx <<= 16;
  92898. + fgx |= fgcolor;
  92899. + bgx |= bgcolor;
  92900. +
  92901. + eorx = fgx ^ bgx;
  92902. + k = image->width / 2;
  92903. +
  92904. + for (i = image->height; i--;) {
  92905. + dst = (u32 __iomem *) dst1;
  92906. + src = s;
  92907. +
  92908. + j = k;
  92909. + while (j >= 4) {
  92910. + u8 bits = *src;
  92911. + end_mask = tab[(bits >> 6) & 3];
  92912. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92913. + end_mask = tab[(bits >> 4) & 3];
  92914. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92915. + end_mask = tab[(bits >> 2) & 3];
  92916. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92917. + end_mask = tab[bits & 3];
  92918. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92919. + src++;
  92920. + j -= 4;
  92921. + }
  92922. + if (j != 0) {
  92923. + u8 bits = *src;
  92924. + end_mask = tab[(bits >> 6) & 3];
  92925. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92926. + if (j >= 2) {
  92927. + end_mask = tab[(bits >> 4) & 3];
  92928. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92929. + if (j == 3) {
  92930. + end_mask = tab[(bits >> 2) & 3];
  92931. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  92932. + }
  92933. + }
  92934. + }
  92935. + dst1 += p->fix.line_length;
  92936. + s += spitch;
  92937. + }
  92938. +}
  92939. +
  92940. +/*
  92941. + * Optimized fast_imageblit for bpp == 32. ppw = 1, bit_mask = 1 folded
  92942. + * into the code, main loop unrolled.
  92943. + */
  92944. +
  92945. +static inline void fast_imageblit32(const struct fb_image *image,
  92946. + struct fb_info *p, u8 __iomem * dst1,
  92947. + u32 fgcolor, u32 bgcolor)
  92948. +{
  92949. + u32 fgx = fgcolor, bgx = bgcolor;
  92950. + u32 spitch = (image->width + 7) / 8;
  92951. + u32 end_mask, eorx;
  92952. + const char *s = image->data, *src;
  92953. + u32 __iomem *dst;
  92954. + const u32 *tab = NULL;
  92955. + int i, j, k;
  92956. +
  92957. + tab = cfb_tab32;
  92958. +
  92959. + eorx = fgx ^ bgx;
  92960. + k = image->width;
  92961. +
  92962. + for (i = image->height; i--;) {
  92963. + dst = (u32 __iomem *) dst1;
  92964. + src = s;
  92965. +
  92966. + j = k;
  92967. + while (j >= 8) {
  92968. + u8 bits = *src;
  92969. + end_mask = tab[(bits >> 7) & 1];
  92970. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92971. + end_mask = tab[(bits >> 6) & 1];
  92972. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92973. + end_mask = tab[(bits >> 5) & 1];
  92974. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92975. + end_mask = tab[(bits >> 4) & 1];
  92976. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92977. + end_mask = tab[(bits >> 3) & 1];
  92978. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92979. + end_mask = tab[(bits >> 2) & 1];
  92980. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92981. + end_mask = tab[(bits >> 1) & 1];
  92982. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92983. + end_mask = tab[bits & 1];
  92984. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92985. + src++;
  92986. + j -= 8;
  92987. + }
  92988. + if (j != 0) {
  92989. + u32 bits = (u32) * src;
  92990. + while (j > 1) {
  92991. + end_mask = tab[(bits >> 7) & 1];
  92992. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  92993. + bits <<= 1;
  92994. + j--;
  92995. + }
  92996. + end_mask = tab[(bits >> 7) & 1];
  92997. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  92998. + }
  92999. + dst1 += p->fix.line_length;
  93000. + s += spitch;
  93001. + }
  93002. +}
  93003. +
  93004. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  93005. {
  93006. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  93007. @@ -294,11 +426,21 @@
  93008. bgcolor = image->bg_color;
  93009. }
  93010. - if (32 % bpp == 0 && !start_index && !pitch_index &&
  93011. - ((width & (32/bpp-1)) == 0) &&
  93012. - bpp >= 8 && bpp <= 32)
  93013. - fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  93014. - else
  93015. + if (!start_index && !pitch_index) {
  93016. + if (bpp == 32)
  93017. + fast_imageblit32(image, p, dst1, fgcolor,
  93018. + bgcolor);
  93019. + else if (bpp == 16 && (width & 1) == 0)
  93020. + fast_imageblit16(image, p, dst1, fgcolor,
  93021. + bgcolor);
  93022. + else if (bpp == 8 && (width & 3) == 0)
  93023. + fast_imageblit(image, p, dst1, fgcolor,
  93024. + bgcolor);
  93025. + else
  93026. + slow_imageblit(image, p, dst1, fgcolor,
  93027. + bgcolor,
  93028. + start_index, pitch_index);
  93029. + } else
  93030. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  93031. start_index, pitch_index);
  93032. } else
  93033. diff -Nur linux-3.13.6/drivers/video/fbmem.c linux-raspberry-pi/drivers/video/fbmem.c
  93034. --- linux-3.13.6/drivers/video/fbmem.c 2014-03-07 07:07:02.000000000 +0100
  93035. +++ linux-raspberry-pi/drivers/video/fbmem.c 2014-03-11 16:55:38.000000000 +0100
  93036. @@ -1083,6 +1083,25 @@
  93037. }
  93038. EXPORT_SYMBOL(fb_blank);
  93039. +static int fb_copyarea_user(struct fb_info *info,
  93040. + struct fb_copyarea *copy)
  93041. +{
  93042. + int ret = 0;
  93043. + if (!lock_fb_info(info))
  93044. + return -ENODEV;
  93045. + if (copy->dx + copy->width > info->var.xres ||
  93046. + copy->sx + copy->width > info->var.xres ||
  93047. + copy->dy + copy->height > info->var.yres ||
  93048. + copy->sy + copy->height > info->var.yres) {
  93049. + ret = -EINVAL;
  93050. + goto out;
  93051. + }
  93052. + info->fbops->fb_copyarea(info, copy);
  93053. +out:
  93054. + unlock_fb_info(info);
  93055. + return ret;
  93056. +}
  93057. +
  93058. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  93059. unsigned long arg)
  93060. {
  93061. @@ -1093,6 +1112,7 @@
  93062. struct fb_cmap cmap_from;
  93063. struct fb_cmap_user cmap;
  93064. struct fb_event event;
  93065. + struct fb_copyarea copy;
  93066. void __user *argp = (void __user *)arg;
  93067. long ret = 0;
  93068. @@ -1210,6 +1230,15 @@
  93069. unlock_fb_info(info);
  93070. console_unlock();
  93071. break;
  93072. + case FBIOCOPYAREA:
  93073. + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
  93074. + /* only provide this ioctl if it is accelerated */
  93075. + if (copy_from_user(&copy, argp, sizeof(copy)))
  93076. + return -EFAULT;
  93077. + ret = fb_copyarea_user(info, &copy);
  93078. + break;
  93079. + }
  93080. + /* fall through */
  93081. default:
  93082. if (!lock_fb_info(info))
  93083. return -ENODEV;
  93084. @@ -1364,6 +1393,7 @@
  93085. case FBIOPAN_DISPLAY:
  93086. case FBIOGET_CON2FBMAP:
  93087. case FBIOPUT_CON2FBMAP:
  93088. + case FBIOCOPYAREA:
  93089. arg = (unsigned long) compat_ptr(arg);
  93090. case FBIOBLANK:
  93091. ret = do_fb_ioctl(info, cmd, arg);
  93092. diff -Nur linux-3.13.6/drivers/video/Kconfig linux-raspberry-pi/drivers/video/Kconfig
  93093. --- linux-3.13.6/drivers/video/Kconfig 2014-03-07 07:07:02.000000000 +0100
  93094. +++ linux-raspberry-pi/drivers/video/Kconfig 2014-03-11 16:55:38.000000000 +0100
  93095. @@ -310,6 +310,20 @@
  93096. help
  93097. Support the Permedia2 FIFO disconnect feature.
  93098. +config FB_BCM2708
  93099. + tristate "BCM2708 framebuffer support"
  93100. + depends on FB && ARM
  93101. + select FB_CFB_FILLRECT
  93102. + select FB_CFB_COPYAREA
  93103. + select FB_CFB_IMAGEBLIT
  93104. + help
  93105. + This framebuffer device driver is for the BCM2708 framebuffer.
  93106. +
  93107. + If you want to compile this as a module (=code which can be
  93108. + inserted into and removed from the running kernel), say M
  93109. + here and read <file:Documentation/kbuild/modules.txt>. The module
  93110. + will be called bcm2708_fb.
  93111. +
  93112. config FB_ARMCLCD
  93113. tristate "ARM PrimeCell PL110 support"
  93114. depends on FB && ARM && ARM_AMBA
  93115. diff -Nur linux-3.13.6/drivers/video/logo/logo_linux_clut224.ppm linux-raspberry-pi/drivers/video/logo/logo_linux_clut224.ppm
  93116. --- linux-3.13.6/drivers/video/logo/logo_linux_clut224.ppm 2014-03-07 07:07:02.000000000 +0100
  93117. +++ linux-raspberry-pi/drivers/video/logo/logo_linux_clut224.ppm 2014-03-11 16:53:13.000000000 +0100
  93118. @@ -1,1604 +1,883 @@
  93119. P3
  93120. -# Standard 224-color Linux logo
  93121. -80 80
  93122. +63 80
  93123. 255
  93124. - 0 0 0 0 0 0 0 0 0 0 0 0
  93125. - 0 0 0 0 0 0 0 0 0 0 0 0
  93126. - 0 0 0 0 0 0 0 0 0 0 0 0
  93127. - 0 0 0 0 0 0 0 0 0 0 0 0
  93128. - 0 0 0 0 0 0 0 0 0 0 0 0
  93129. - 0 0 0 0 0 0 0 0 0 0 0 0
  93130. - 0 0 0 0 0 0 0 0 0 0 0 0
  93131. - 0 0 0 0 0 0 0 0 0 0 0 0
  93132. - 0 0 0 0 0 0 0 0 0 0 0 0
  93133. - 6 6 6 6 6 6 10 10 10 10 10 10
  93134. - 10 10 10 6 6 6 6 6 6 6 6 6
  93135. - 0 0 0 0 0 0 0 0 0 0 0 0
  93136. - 0 0 0 0 0 0 0 0 0 0 0 0
  93137. - 0 0 0 0 0 0 0 0 0 0 0 0
  93138. - 0 0 0 0 0 0 0 0 0 0 0 0
  93139. - 0 0 0 0 0 0 0 0 0 0 0 0
  93140. - 0 0 0 0 0 0 0 0 0 0 0 0
  93141. - 0 0 0 0 0 0 0 0 0 0 0 0
  93142. - 0 0 0 0 0 0 0 0 0 0 0 0
  93143. - 0 0 0 0 0 0 0 0 0 0 0 0
  93144. - 0 0 0 0 0 0 0 0 0 0 0 0
  93145. - 0 0 0 0 0 0 0 0 0 0 0 0
  93146. - 0 0 0 0 0 0 0 0 0 0 0 0
  93147. - 0 0 0 0 0 0 0 0 0 0 0 0
  93148. - 0 0 0 0 0 0 0 0 0 0 0 0
  93149. - 0 0 0 0 0 0 0 0 0 0 0 0
  93150. - 0 0 0 0 0 0 0 0 0 0 0 0
  93151. - 0 0 0 0 0 0 0 0 0 0 0 0
  93152. - 0 0 0 6 6 6 10 10 10 14 14 14
  93153. - 22 22 22 26 26 26 30 30 30 34 34 34
  93154. - 30 30 30 30 30 30 26 26 26 18 18 18
  93155. - 14 14 14 10 10 10 6 6 6 0 0 0
  93156. - 0 0 0 0 0 0 0 0 0 0 0 0
  93157. - 0 0 0 0 0 0 0 0 0 0 0 0
  93158. - 0 0 0 0 0 0 0 0 0 0 0 0
  93159. - 0 0 0 0 0 0 0 0 0 0 0 0
  93160. - 0 0 0 0 0 0 0 0 0 0 0 0
  93161. - 0 0 0 0 0 0 0 0 0 0 0 0
  93162. - 0 0 0 0 0 0 0 0 0 0 0 0
  93163. - 0 0 0 0 0 0 0 0 0 0 0 0
  93164. - 0 0 0 0 0 0 0 0 0 0 0 0
  93165. - 0 0 0 0 0 1 0 0 1 0 0 0
  93166. - 0 0 0 0 0 0 0 0 0 0 0 0
  93167. - 0 0 0 0 0 0 0 0 0 0 0 0
  93168. - 0 0 0 0 0 0 0 0 0 0 0 0
  93169. - 0 0 0 0 0 0 0 0 0 0 0 0
  93170. - 0 0 0 0 0 0 0 0 0 0 0 0
  93171. - 0 0 0 0 0 0 0 0 0 0 0 0
  93172. - 6 6 6 14 14 14 26 26 26 42 42 42
  93173. - 54 54 54 66 66 66 78 78 78 78 78 78
  93174. - 78 78 78 74 74 74 66 66 66 54 54 54
  93175. - 42 42 42 26 26 26 18 18 18 10 10 10
  93176. - 6 6 6 0 0 0 0 0 0 0 0 0
  93177. - 0 0 0 0 0 0 0 0 0 0 0 0
  93178. - 0 0 0 0 0 0 0 0 0 0 0 0
  93179. - 0 0 0 0 0 0 0 0 0 0 0 0
  93180. - 0 0 0 0 0 0 0 0 0 0 0 0
  93181. - 0 0 0 0 0 0 0 0 0 0 0 0
  93182. - 0 0 0 0 0 0 0 0 0 0 0 0
  93183. - 0 0 0 0 0 0 0 0 0 0 0 0
  93184. - 0 0 0 0 0 0 0 0 0 0 0 0
  93185. - 0 0 1 0 0 0 0 0 0 0 0 0
  93186. - 0 0 0 0 0 0 0 0 0 0 0 0
  93187. - 0 0 0 0 0 0 0 0 0 0 0 0
  93188. - 0 0 0 0 0 0 0 0 0 0 0 0
  93189. - 0 0 0 0 0 0 0 0 0 0 0 0
  93190. - 0 0 0 0 0 0 0 0 0 0 0 0
  93191. - 0 0 0 0 0 0 0 0 0 10 10 10
  93192. - 22 22 22 42 42 42 66 66 66 86 86 86
  93193. - 66 66 66 38 38 38 38 38 38 22 22 22
  93194. - 26 26 26 34 34 34 54 54 54 66 66 66
  93195. - 86 86 86 70 70 70 46 46 46 26 26 26
  93196. - 14 14 14 6 6 6 0 0 0 0 0 0
  93197. - 0 0 0 0 0 0 0 0 0 0 0 0
  93198. - 0 0 0 0 0 0 0 0 0 0 0 0
  93199. - 0 0 0 0 0 0 0 0 0 0 0 0
  93200. - 0 0 0 0 0 0 0 0 0 0 0 0
  93201. - 0 0 0 0 0 0 0 0 0 0 0 0
  93202. - 0 0 0 0 0 0 0 0 0 0 0 0
  93203. - 0 0 0 0 0 0 0 0 0 0 0 0
  93204. - 0 0 0 0 0 0 0 0 0 0 0 0
  93205. - 0 0 1 0 0 1 0 0 1 0 0 0
  93206. - 0 0 0 0 0 0 0 0 0 0 0 0
  93207. - 0 0 0 0 0 0 0 0 0 0 0 0
  93208. - 0 0 0 0 0 0 0 0 0 0 0 0
  93209. - 0 0 0 0 0 0 0 0 0 0 0 0
  93210. - 0 0 0 0 0 0 0 0 0 0 0 0
  93211. - 0 0 0 0 0 0 10 10 10 26 26 26
  93212. - 50 50 50 82 82 82 58 58 58 6 6 6
  93213. - 2 2 6 2 2 6 2 2 6 2 2 6
  93214. - 2 2 6 2 2 6 2 2 6 2 2 6
  93215. - 6 6 6 54 54 54 86 86 86 66 66 66
  93216. - 38 38 38 18 18 18 6 6 6 0 0 0
  93217. - 0 0 0 0 0 0 0 0 0 0 0 0
  93218. - 0 0 0 0 0 0 0 0 0 0 0 0
  93219. - 0 0 0 0 0 0 0 0 0 0 0 0
  93220. - 0 0 0 0 0 0 0 0 0 0 0 0
  93221. - 0 0 0 0 0 0 0 0 0 0 0 0
  93222. - 0 0 0 0 0 0 0 0 0 0 0 0
  93223. - 0 0 0 0 0 0 0 0 0 0 0 0
  93224. - 0 0 0 0 0 0 0 0 0 0 0 0
  93225. - 0 0 0 0 0 0 0 0 0 0 0 0
  93226. - 0 0 0 0 0 0 0 0 0 0 0 0
  93227. - 0 0 0 0 0 0 0 0 0 0 0 0
  93228. - 0 0 0 0 0 0 0 0 0 0 0 0
  93229. - 0 0 0 0 0 0 0 0 0 0 0 0
  93230. - 0 0 0 0 0 0 0 0 0 0 0 0
  93231. - 0 0 0 6 6 6 22 22 22 50 50 50
  93232. - 78 78 78 34 34 34 2 2 6 2 2 6
  93233. - 2 2 6 2 2 6 2 2 6 2 2 6
  93234. - 2 2 6 2 2 6 2 2 6 2 2 6
  93235. - 2 2 6 2 2 6 6 6 6 70 70 70
  93236. - 78 78 78 46 46 46 22 22 22 6 6 6
  93237. - 0 0 0 0 0 0 0 0 0 0 0 0
  93238. - 0 0 0 0 0 0 0 0 0 0 0 0
  93239. - 0 0 0 0 0 0 0 0 0 0 0 0
  93240. - 0 0 0 0 0 0 0 0 0 0 0 0
  93241. - 0 0 0 0 0 0 0 0 0 0 0 0
  93242. - 0 0 0 0 0 0 0 0 0 0 0 0
  93243. - 0 0 0 0 0 0 0 0 0 0 0 0
  93244. - 0 0 0 0 0 0 0 0 0 0 0 0
  93245. - 0 0 1 0 0 1 0 0 1 0 0 0
  93246. - 0 0 0 0 0 0 0 0 0 0 0 0
  93247. - 0 0 0 0 0 0 0 0 0 0 0 0
  93248. - 0 0 0 0 0 0 0 0 0 0 0 0
  93249. - 0 0 0 0 0 0 0 0 0 0 0 0
  93250. - 0 0 0 0 0 0 0 0 0 0 0 0
  93251. - 6 6 6 18 18 18 42 42 42 82 82 82
  93252. - 26 26 26 2 2 6 2 2 6 2 2 6
  93253. - 2 2 6 2 2 6 2 2 6 2 2 6
  93254. - 2 2 6 2 2 6 2 2 6 14 14 14
  93255. - 46 46 46 34 34 34 6 6 6 2 2 6
  93256. - 42 42 42 78 78 78 42 42 42 18 18 18
  93257. - 6 6 6 0 0 0 0 0 0 0 0 0
  93258. - 0 0 0 0 0 0 0 0 0 0 0 0
  93259. - 0 0 0 0 0 0 0 0 0 0 0 0
  93260. - 0 0 0 0 0 0 0 0 0 0 0 0
  93261. - 0 0 0 0 0 0 0 0 0 0 0 0
  93262. - 0 0 0 0 0 0 0 0 0 0 0 0
  93263. - 0 0 0 0 0 0 0 0 0 0 0 0
  93264. - 0 0 0 0 0 0 0 0 0 0 0 0
  93265. - 0 0 1 0 0 0 0 0 1 0 0 0
  93266. - 0 0 0 0 0 0 0 0 0 0 0 0
  93267. - 0 0 0 0 0 0 0 0 0 0 0 0
  93268. - 0 0 0 0 0 0 0 0 0 0 0 0
  93269. - 0 0 0 0 0 0 0 0 0 0 0 0
  93270. - 0 0 0 0 0 0 0 0 0 0 0 0
  93271. - 10 10 10 30 30 30 66 66 66 58 58 58
  93272. - 2 2 6 2 2 6 2 2 6 2 2 6
  93273. - 2 2 6 2 2 6 2 2 6 2 2 6
  93274. - 2 2 6 2 2 6 2 2 6 26 26 26
  93275. - 86 86 86 101 101 101 46 46 46 10 10 10
  93276. - 2 2 6 58 58 58 70 70 70 34 34 34
  93277. - 10 10 10 0 0 0 0 0 0 0 0 0
  93278. - 0 0 0 0 0 0 0 0 0 0 0 0
  93279. - 0 0 0 0 0 0 0 0 0 0 0 0
  93280. - 0 0 0 0 0 0 0 0 0 0 0 0
  93281. - 0 0 0 0 0 0 0 0 0 0 0 0
  93282. - 0 0 0 0 0 0 0 0 0 0 0 0
  93283. - 0 0 0 0 0 0 0 0 0 0 0 0
  93284. - 0 0 0 0 0 0 0 0 0 0 0 0
  93285. - 0 0 1 0 0 1 0 0 1 0 0 0
  93286. - 0 0 0 0 0 0 0 0 0 0 0 0
  93287. - 0 0 0 0 0 0 0 0 0 0 0 0
  93288. - 0 0 0 0 0 0 0 0 0 0 0 0
  93289. - 0 0 0 0 0 0 0 0 0 0 0 0
  93290. - 0 0 0 0 0 0 0 0 0 0 0 0
  93291. - 14 14 14 42 42 42 86 86 86 10 10 10
  93292. - 2 2 6 2 2 6 2 2 6 2 2 6
  93293. - 2 2 6 2 2 6 2 2 6 2 2 6
  93294. - 2 2 6 2 2 6 2 2 6 30 30 30
  93295. - 94 94 94 94 94 94 58 58 58 26 26 26
  93296. - 2 2 6 6 6 6 78 78 78 54 54 54
  93297. - 22 22 22 6 6 6 0 0 0 0 0 0
  93298. - 0 0 0 0 0 0 0 0 0 0 0 0
  93299. - 0 0 0 0 0 0 0 0 0 0 0 0
  93300. - 0 0 0 0 0 0 0 0 0 0 0 0
  93301. - 0 0 0 0 0 0 0 0 0 0 0 0
  93302. - 0 0 0 0 0 0 0 0 0 0 0 0
  93303. - 0 0 0 0 0 0 0 0 0 0 0 0
  93304. - 0 0 0 0 0 0 0 0 0 0 0 0
  93305. - 0 0 0 0 0 0 0 0 0 0 0 0
  93306. - 0 0 0 0 0 0 0 0 0 0 0 0
  93307. - 0 0 0 0 0 0 0 0 0 0 0 0
  93308. - 0 0 0 0 0 0 0 0 0 0 0 0
  93309. - 0 0 0 0 0 0 0 0 0 0 0 0
  93310. - 0 0 0 0 0 0 0 0 0 6 6 6
  93311. - 22 22 22 62 62 62 62 62 62 2 2 6
  93312. - 2 2 6 2 2 6 2 2 6 2 2 6
  93313. - 2 2 6 2 2 6 2 2 6 2 2 6
  93314. - 2 2 6 2 2 6 2 2 6 26 26 26
  93315. - 54 54 54 38 38 38 18 18 18 10 10 10
  93316. - 2 2 6 2 2 6 34 34 34 82 82 82
  93317. - 38 38 38 14 14 14 0 0 0 0 0 0
  93318. - 0 0 0 0 0 0 0 0 0 0 0 0
  93319. - 0 0 0 0 0 0 0 0 0 0 0 0
  93320. - 0 0 0 0 0 0 0 0 0 0 0 0
  93321. - 0 0 0 0 0 0 0 0 0 0 0 0
  93322. - 0 0 0 0 0 0 0 0 0 0 0 0
  93323. - 0 0 0 0 0 0 0 0 0 0 0 0
  93324. - 0 0 0 0 0 0 0 0 0 0 0 0
  93325. - 0 0 0 0 0 1 0 0 1 0 0 0
  93326. - 0 0 0 0 0 0 0 0 0 0 0 0
  93327. - 0 0 0 0 0 0 0 0 0 0 0 0
  93328. - 0 0 0 0 0 0 0 0 0 0 0 0
  93329. - 0 0 0 0 0 0 0 0 0 0 0 0
  93330. - 0 0 0 0 0 0 0 0 0 6 6 6
  93331. - 30 30 30 78 78 78 30 30 30 2 2 6
  93332. - 2 2 6 2 2 6 2 2 6 2 2 6
  93333. - 2 2 6 2 2 6 2 2 6 2 2 6
  93334. - 2 2 6 2 2 6 2 2 6 10 10 10
  93335. - 10 10 10 2 2 6 2 2 6 2 2 6
  93336. - 2 2 6 2 2 6 2 2 6 78 78 78
  93337. - 50 50 50 18 18 18 6 6 6 0 0 0
  93338. - 0 0 0 0 0 0 0 0 0 0 0 0
  93339. - 0 0 0 0 0 0 0 0 0 0 0 0
  93340. - 0 0 0 0 0 0 0 0 0 0 0 0
  93341. - 0 0 0 0 0 0 0 0 0 0 0 0
  93342. - 0 0 0 0 0 0 0 0 0 0 0 0
  93343. - 0 0 0 0 0 0 0 0 0 0 0 0
  93344. - 0 0 0 0 0 0 0 0 0 0 0 0
  93345. - 0 0 1 0 0 0 0 0 0 0 0 0
  93346. - 0 0 0 0 0 0 0 0 0 0 0 0
  93347. - 0 0 0 0 0 0 0 0 0 0 0 0
  93348. - 0 0 0 0 0 0 0 0 0 0 0 0
  93349. - 0 0 0 0 0 0 0 0 0 0 0 0
  93350. - 0 0 0 0 0 0 0 0 0 10 10 10
  93351. - 38 38 38 86 86 86 14 14 14 2 2 6
  93352. - 2 2 6 2 2 6 2 2 6 2 2 6
  93353. - 2 2 6 2 2 6 2 2 6 2 2 6
  93354. - 2 2 6 2 2 6 2 2 6 2 2 6
  93355. - 2 2 6 2 2 6 2 2 6 2 2 6
  93356. - 2 2 6 2 2 6 2 2 6 54 54 54
  93357. - 66 66 66 26 26 26 6 6 6 0 0 0
  93358. - 0 0 0 0 0 0 0 0 0 0 0 0
  93359. - 0 0 0 0 0 0 0 0 0 0 0 0
  93360. - 0 0 0 0 0 0 0 0 0 0 0 0
  93361. - 0 0 0 0 0 0 0 0 0 0 0 0
  93362. - 0 0 0 0 0 0 0 0 0 0 0 0
  93363. - 0 0 0 0 0 0 0 0 0 0 0 0
  93364. - 0 0 0 0 0 0 0 0 0 0 0 0
  93365. - 0 0 0 0 0 1 0 0 1 0 0 0
  93366. - 0 0 0 0 0 0 0 0 0 0 0 0
  93367. - 0 0 0 0 0 0 0 0 0 0 0 0
  93368. - 0 0 0 0 0 0 0 0 0 0 0 0
  93369. - 0 0 0 0 0 0 0 0 0 0 0 0
  93370. - 0 0 0 0 0 0 0 0 0 14 14 14
  93371. - 42 42 42 82 82 82 2 2 6 2 2 6
  93372. - 2 2 6 6 6 6 10 10 10 2 2 6
  93373. - 2 2 6 2 2 6 2 2 6 2 2 6
  93374. - 2 2 6 2 2 6 2 2 6 6 6 6
  93375. - 14 14 14 10 10 10 2 2 6 2 2 6
  93376. - 2 2 6 2 2 6 2 2 6 18 18 18
  93377. - 82 82 82 34 34 34 10 10 10 0 0 0
  93378. - 0 0 0 0 0 0 0 0 0 0 0 0
  93379. - 0 0 0 0 0 0 0 0 0 0 0 0
  93380. - 0 0 0 0 0 0 0 0 0 0 0 0
  93381. - 0 0 0 0 0 0 0 0 0 0 0 0
  93382. - 0 0 0 0 0 0 0 0 0 0 0 0
  93383. - 0 0 0 0 0 0 0 0 0 0 0 0
  93384. - 0 0 0 0 0 0 0 0 0 0 0 0
  93385. - 0 0 1 0 0 0 0 0 0 0 0 0
  93386. - 0 0 0 0 0 0 0 0 0 0 0 0
  93387. - 0 0 0 0 0 0 0 0 0 0 0 0
  93388. - 0 0 0 0 0 0 0 0 0 0 0 0
  93389. - 0 0 0 0 0 0 0 0 0 0 0 0
  93390. - 0 0 0 0 0 0 0 0 0 14 14 14
  93391. - 46 46 46 86 86 86 2 2 6 2 2 6
  93392. - 6 6 6 6 6 6 22 22 22 34 34 34
  93393. - 6 6 6 2 2 6 2 2 6 2 2 6
  93394. - 2 2 6 2 2 6 18 18 18 34 34 34
  93395. - 10 10 10 50 50 50 22 22 22 2 2 6
  93396. - 2 2 6 2 2 6 2 2 6 10 10 10
  93397. - 86 86 86 42 42 42 14 14 14 0 0 0
  93398. - 0 0 0 0 0 0 0 0 0 0 0 0
  93399. - 0 0 0 0 0 0 0 0 0 0 0 0
  93400. - 0 0 0 0 0 0 0 0 0 0 0 0
  93401. - 0 0 0 0 0 0 0 0 0 0 0 0
  93402. - 0 0 0 0 0 0 0 0 0 0 0 0
  93403. - 0 0 0 0 0 0 0 0 0 0 0 0
  93404. - 0 0 0 0 0 0 0 0 0 0 0 0
  93405. - 0 0 1 0 0 1 0 0 1 0 0 0
  93406. - 0 0 0 0 0 0 0 0 0 0 0 0
  93407. - 0 0 0 0 0 0 0 0 0 0 0 0
  93408. - 0 0 0 0 0 0 0 0 0 0 0 0
  93409. - 0 0 0 0 0 0 0 0 0 0 0 0
  93410. - 0 0 0 0 0 0 0 0 0 14 14 14
  93411. - 46 46 46 86 86 86 2 2 6 2 2 6
  93412. - 38 38 38 116 116 116 94 94 94 22 22 22
  93413. - 22 22 22 2 2 6 2 2 6 2 2 6
  93414. - 14 14 14 86 86 86 138 138 138 162 162 162
  93415. -154 154 154 38 38 38 26 26 26 6 6 6
  93416. - 2 2 6 2 2 6 2 2 6 2 2 6
  93417. - 86 86 86 46 46 46 14 14 14 0 0 0
  93418. - 0 0 0 0 0 0 0 0 0 0 0 0
  93419. - 0 0 0 0 0 0 0 0 0 0 0 0
  93420. - 0 0 0 0 0 0 0 0 0 0 0 0
  93421. - 0 0 0 0 0 0 0 0 0 0 0 0
  93422. - 0 0 0 0 0 0 0 0 0 0 0 0
  93423. - 0 0 0 0 0 0 0 0 0 0 0 0
  93424. - 0 0 0 0 0 0 0 0 0 0 0 0
  93425. - 0 0 0 0 0 0 0 0 0 0 0 0
  93426. - 0 0 0 0 0 0 0 0 0 0 0 0
  93427. - 0 0 0 0 0 0 0 0 0 0 0 0
  93428. - 0 0 0 0 0 0 0 0 0 0 0 0
  93429. - 0 0 0 0 0 0 0 0 0 0 0 0
  93430. - 0 0 0 0 0 0 0 0 0 14 14 14
  93431. - 46 46 46 86 86 86 2 2 6 14 14 14
  93432. -134 134 134 198 198 198 195 195 195 116 116 116
  93433. - 10 10 10 2 2 6 2 2 6 6 6 6
  93434. -101 98 89 187 187 187 210 210 210 218 218 218
  93435. -214 214 214 134 134 134 14 14 14 6 6 6
  93436. - 2 2 6 2 2 6 2 2 6 2 2 6
  93437. - 86 86 86 50 50 50 18 18 18 6 6 6
  93438. - 0 0 0 0 0 0 0 0 0 0 0 0
  93439. - 0 0 0 0 0 0 0 0 0 0 0 0
  93440. - 0 0 0 0 0 0 0 0 0 0 0 0
  93441. - 0 0 0 0 0 0 0 0 0 0 0 0
  93442. - 0 0 0 0 0 0 0 0 0 0 0 0
  93443. - 0 0 0 0 0 0 0 0 0 0 0 0
  93444. - 0 0 0 0 0 0 0 0 1 0 0 0
  93445. - 0 0 1 0 0 1 0 0 1 0 0 0
  93446. - 0 0 0 0 0 0 0 0 0 0 0 0
  93447. - 0 0 0 0 0 0 0 0 0 0 0 0
  93448. - 0 0 0 0 0 0 0 0 0 0 0 0
  93449. - 0 0 0 0 0 0 0 0 0 0 0 0
  93450. - 0 0 0 0 0 0 0 0 0 14 14 14
  93451. - 46 46 46 86 86 86 2 2 6 54 54 54
  93452. -218 218 218 195 195 195 226 226 226 246 246 246
  93453. - 58 58 58 2 2 6 2 2 6 30 30 30
  93454. -210 210 210 253 253 253 174 174 174 123 123 123
  93455. -221 221 221 234 234 234 74 74 74 2 2 6
  93456. - 2 2 6 2 2 6 2 2 6 2 2 6
  93457. - 70 70 70 58 58 58 22 22 22 6 6 6
  93458. - 0 0 0 0 0 0 0 0 0 0 0 0
  93459. - 0 0 0 0 0 0 0 0 0 0 0 0
  93460. - 0 0 0 0 0 0 0 0 0 0 0 0
  93461. - 0 0 0 0 0 0 0 0 0 0 0 0
  93462. - 0 0 0 0 0 0 0 0 0 0 0 0
  93463. - 0 0 0 0 0 0 0 0 0 0 0 0
  93464. - 0 0 0 0 0 0 0 0 0 0 0 0
  93465. - 0 0 0 0 0 0 0 0 0 0 0 0
  93466. - 0 0 0 0 0 0 0 0 0 0 0 0
  93467. - 0 0 0 0 0 0 0 0 0 0 0 0
  93468. - 0 0 0 0 0 0 0 0 0 0 0 0
  93469. - 0 0 0 0 0 0 0 0 0 0 0 0
  93470. - 0 0 0 0 0 0 0 0 0 14 14 14
  93471. - 46 46 46 82 82 82 2 2 6 106 106 106
  93472. -170 170 170 26 26 26 86 86 86 226 226 226
  93473. -123 123 123 10 10 10 14 14 14 46 46 46
  93474. -231 231 231 190 190 190 6 6 6 70 70 70
  93475. - 90 90 90 238 238 238 158 158 158 2 2 6
  93476. - 2 2 6 2 2 6 2 2 6 2 2 6
  93477. - 70 70 70 58 58 58 22 22 22 6 6 6
  93478. - 0 0 0 0 0 0 0 0 0 0 0 0
  93479. - 0 0 0 0 0 0 0 0 0 0 0 0
  93480. - 0 0 0 0 0 0 0 0 0 0 0 0
  93481. - 0 0 0 0 0 0 0 0 0 0 0 0
  93482. - 0 0 0 0 0 0 0 0 0 0 0 0
  93483. - 0 0 0 0 0 0 0 0 0 0 0 0
  93484. - 0 0 0 0 0 0 0 0 1 0 0 0
  93485. - 0 0 1 0 0 1 0 0 1 0 0 0
  93486. - 0 0 0 0 0 0 0 0 0 0 0 0
  93487. - 0 0 0 0 0 0 0 0 0 0 0 0
  93488. - 0 0 0 0 0 0 0 0 0 0 0 0
  93489. - 0 0 0 0 0 0 0 0 0 0 0 0
  93490. - 0 0 0 0 0 0 0 0 0 14 14 14
  93491. - 42 42 42 86 86 86 6 6 6 116 116 116
  93492. -106 106 106 6 6 6 70 70 70 149 149 149
  93493. -128 128 128 18 18 18 38 38 38 54 54 54
  93494. -221 221 221 106 106 106 2 2 6 14 14 14
  93495. - 46 46 46 190 190 190 198 198 198 2 2 6
  93496. - 2 2 6 2 2 6 2 2 6 2 2 6
  93497. - 74 74 74 62 62 62 22 22 22 6 6 6
  93498. - 0 0 0 0 0 0 0 0 0 0 0 0
  93499. - 0 0 0 0 0 0 0 0 0 0 0 0
  93500. - 0 0 0 0 0 0 0 0 0 0 0 0
  93501. - 0 0 0 0 0 0 0 0 0 0 0 0
  93502. - 0 0 0 0 0 0 0 0 0 0 0 0
  93503. - 0 0 0 0 0 0 0 0 0 0 0 0
  93504. - 0 0 0 0 0 0 0 0 1 0 0 0
  93505. - 0 0 1 0 0 0 0 0 1 0 0 0
  93506. - 0 0 0 0 0 0 0 0 0 0 0 0
  93507. - 0 0 0 0 0 0 0 0 0 0 0 0
  93508. - 0 0 0 0 0 0 0 0 0 0 0 0
  93509. - 0 0 0 0 0 0 0 0 0 0 0 0
  93510. - 0 0 0 0 0 0 0 0 0 14 14 14
  93511. - 42 42 42 94 94 94 14 14 14 101 101 101
  93512. -128 128 128 2 2 6 18 18 18 116 116 116
  93513. -118 98 46 121 92 8 121 92 8 98 78 10
  93514. -162 162 162 106 106 106 2 2 6 2 2 6
  93515. - 2 2 6 195 195 195 195 195 195 6 6 6
  93516. - 2 2 6 2 2 6 2 2 6 2 2 6
  93517. - 74 74 74 62 62 62 22 22 22 6 6 6
  93518. - 0 0 0 0 0 0 0 0 0 0 0 0
  93519. - 0 0 0 0 0 0 0 0 0 0 0 0
  93520. - 0 0 0 0 0 0 0 0 0 0 0 0
  93521. - 0 0 0 0 0 0 0 0 0 0 0 0
  93522. - 0 0 0 0 0 0 0 0 0 0 0 0
  93523. - 0 0 0 0 0 0 0 0 0 0 0 0
  93524. - 0 0 0 0 0 0 0 0 1 0 0 1
  93525. - 0 0 1 0 0 0 0 0 1 0 0 0
  93526. - 0 0 0 0 0 0 0 0 0 0 0 0
  93527. - 0 0 0 0 0 0 0 0 0 0 0 0
  93528. - 0 0 0 0 0 0 0 0 0 0 0 0
  93529. - 0 0 0 0 0 0 0 0 0 0 0 0
  93530. - 0 0 0 0 0 0 0 0 0 10 10 10
  93531. - 38 38 38 90 90 90 14 14 14 58 58 58
  93532. -210 210 210 26 26 26 54 38 6 154 114 10
  93533. -226 170 11 236 186 11 225 175 15 184 144 12
  93534. -215 174 15 175 146 61 37 26 9 2 2 6
  93535. - 70 70 70 246 246 246 138 138 138 2 2 6
  93536. - 2 2 6 2 2 6 2 2 6 2 2 6
  93537. - 70 70 70 66 66 66 26 26 26 6 6 6
  93538. - 0 0 0 0 0 0 0 0 0 0 0 0
  93539. - 0 0 0 0 0 0 0 0 0 0 0 0
  93540. - 0 0 0 0 0 0 0 0 0 0 0 0
  93541. - 0 0 0 0 0 0 0 0 0 0 0 0
  93542. - 0 0 0 0 0 0 0 0 0 0 0 0
  93543. - 0 0 0 0 0 0 0 0 0 0 0 0
  93544. - 0 0 0 0 0 0 0 0 0 0 0 0
  93545. - 0 0 0 0 0 0 0 0 0 0 0 0
  93546. - 0 0 0 0 0 0 0 0 0 0 0 0
  93547. - 0 0 0 0 0 0 0 0 0 0 0 0
  93548. - 0 0 0 0 0 0 0 0 0 0 0 0
  93549. - 0 0 0 0 0 0 0 0 0 0 0 0
  93550. - 0 0 0 0 0 0 0 0 0 10 10 10
  93551. - 38 38 38 86 86 86 14 14 14 10 10 10
  93552. -195 195 195 188 164 115 192 133 9 225 175 15
  93553. -239 182 13 234 190 10 232 195 16 232 200 30
  93554. -245 207 45 241 208 19 232 195 16 184 144 12
  93555. -218 194 134 211 206 186 42 42 42 2 2 6
  93556. - 2 2 6 2 2 6 2 2 6 2 2 6
  93557. - 50 50 50 74 74 74 30 30 30 6 6 6
  93558. - 0 0 0 0 0 0 0 0 0 0 0 0
  93559. - 0 0 0 0 0 0 0 0 0 0 0 0
  93560. - 0 0 0 0 0 0 0 0 0 0 0 0
  93561. - 0 0 0 0 0 0 0 0 0 0 0 0
  93562. - 0 0 0 0 0 0 0 0 0 0 0 0
  93563. - 0 0 0 0 0 0 0 0 0 0 0 0
  93564. - 0 0 0 0 0 0 0 0 0 0 0 0
  93565. - 0 0 0 0 0 0 0 0 0 0 0 0
  93566. - 0 0 0 0 0 0 0 0 0 0 0 0
  93567. - 0 0 0 0 0 0 0 0 0 0 0 0
  93568. - 0 0 0 0 0 0 0 0 0 0 0 0
  93569. - 0 0 0 0 0 0 0 0 0 0 0 0
  93570. - 0 0 0 0 0 0 0 0 0 10 10 10
  93571. - 34 34 34 86 86 86 14 14 14 2 2 6
  93572. -121 87 25 192 133 9 219 162 10 239 182 13
  93573. -236 186 11 232 195 16 241 208 19 244 214 54
  93574. -246 218 60 246 218 38 246 215 20 241 208 19
  93575. -241 208 19 226 184 13 121 87 25 2 2 6
  93576. - 2 2 6 2 2 6 2 2 6 2 2 6
  93577. - 50 50 50 82 82 82 34 34 34 10 10 10
  93578. - 0 0 0 0 0 0 0 0 0 0 0 0
  93579. - 0 0 0 0 0 0 0 0 0 0 0 0
  93580. - 0 0 0 0 0 0 0 0 0 0 0 0
  93581. - 0 0 0 0 0 0 0 0 0 0 0 0
  93582. - 0 0 0 0 0 0 0 0 0 0 0 0
  93583. - 0 0 0 0 0 0 0 0 0 0 0 0
  93584. - 0 0 0 0 0 0 0 0 0 0 0 0
  93585. - 0 0 0 0 0 0 0 0 0 0 0 0
  93586. - 0 0 0 0 0 0 0 0 0 0 0 0
  93587. - 0 0 0 0 0 0 0 0 0 0 0 0
  93588. - 0 0 0 0 0 0 0 0 0 0 0 0
  93589. - 0 0 0 0 0 0 0 0 0 0 0 0
  93590. - 0 0 0 0 0 0 0 0 0 10 10 10
  93591. - 34 34 34 82 82 82 30 30 30 61 42 6
  93592. -180 123 7 206 145 10 230 174 11 239 182 13
  93593. -234 190 10 238 202 15 241 208 19 246 218 74
  93594. -246 218 38 246 215 20 246 215 20 246 215 20
  93595. -226 184 13 215 174 15 184 144 12 6 6 6
  93596. - 2 2 6 2 2 6 2 2 6 2 2 6
  93597. - 26 26 26 94 94 94 42 42 42 14 14 14
  93598. - 0 0 0 0 0 0 0 0 0 0 0 0
  93599. - 0 0 0 0 0 0 0 0 0 0 0 0
  93600. - 0 0 0 0 0 0 0 0 0 0 0 0
  93601. - 0 0 0 0 0 0 0 0 0 0 0 0
  93602. - 0 0 0 0 0 0 0 0 0 0 0 0
  93603. - 0 0 0 0 0 0 0 0 0 0 0 0
  93604. - 0 0 0 0 0 0 0 0 0 0 0 0
  93605. - 0 0 0 0 0 0 0 0 0 0 0 0
  93606. - 0 0 0 0 0 0 0 0 0 0 0 0
  93607. - 0 0 0 0 0 0 0 0 0 0 0 0
  93608. - 0 0 0 0 0 0 0 0 0 0 0 0
  93609. - 0 0 0 0 0 0 0 0 0 0 0 0
  93610. - 0 0 0 0 0 0 0 0 0 10 10 10
  93611. - 30 30 30 78 78 78 50 50 50 104 69 6
  93612. -192 133 9 216 158 10 236 178 12 236 186 11
  93613. -232 195 16 241 208 19 244 214 54 245 215 43
  93614. -246 215 20 246 215 20 241 208 19 198 155 10
  93615. -200 144 11 216 158 10 156 118 10 2 2 6
  93616. - 2 2 6 2 2 6 2 2 6 2 2 6
  93617. - 6 6 6 90 90 90 54 54 54 18 18 18
  93618. - 6 6 6 0 0 0 0 0 0 0 0 0
  93619. - 0 0 0 0 0 0 0 0 0 0 0 0
  93620. - 0 0 0 0 0 0 0 0 0 0 0 0
  93621. - 0 0 0 0 0 0 0 0 0 0 0 0
  93622. - 0 0 0 0 0 0 0 0 0 0 0 0
  93623. - 0 0 0 0 0 0 0 0 0 0 0 0
  93624. - 0 0 0 0 0 0 0 0 0 0 0 0
  93625. - 0 0 0 0 0 0 0 0 0 0 0 0
  93626. - 0 0 0 0 0 0 0 0 0 0 0 0
  93627. - 0 0 0 0 0 0 0 0 0 0 0 0
  93628. - 0 0 0 0 0 0 0 0 0 0 0 0
  93629. - 0 0 0 0 0 0 0 0 0 0 0 0
  93630. - 0 0 0 0 0 0 0 0 0 10 10 10
  93631. - 30 30 30 78 78 78 46 46 46 22 22 22
  93632. -137 92 6 210 162 10 239 182 13 238 190 10
  93633. -238 202 15 241 208 19 246 215 20 246 215 20
  93634. -241 208 19 203 166 17 185 133 11 210 150 10
  93635. -216 158 10 210 150 10 102 78 10 2 2 6
  93636. - 6 6 6 54 54 54 14 14 14 2 2 6
  93637. - 2 2 6 62 62 62 74 74 74 30 30 30
  93638. - 10 10 10 0 0 0 0 0 0 0 0 0
  93639. - 0 0 0 0 0 0 0 0 0 0 0 0
  93640. - 0 0 0 0 0 0 0 0 0 0 0 0
  93641. - 0 0 0 0 0 0 0 0 0 0 0 0
  93642. - 0 0 0 0 0 0 0 0 0 0 0 0
  93643. - 0 0 0 0 0 0 0 0 0 0 0 0
  93644. - 0 0 0 0 0 0 0 0 0 0 0 0
  93645. - 0 0 0 0 0 0 0 0 0 0 0 0
  93646. - 0 0 0 0 0 0 0 0 0 0 0 0
  93647. - 0 0 0 0 0 0 0 0 0 0 0 0
  93648. - 0 0 0 0 0 0 0 0 0 0 0 0
  93649. - 0 0 0 0 0 0 0 0 0 0 0 0
  93650. - 0 0 0 0 0 0 0 0 0 10 10 10
  93651. - 34 34 34 78 78 78 50 50 50 6 6 6
  93652. - 94 70 30 139 102 15 190 146 13 226 184 13
  93653. -232 200 30 232 195 16 215 174 15 190 146 13
  93654. -168 122 10 192 133 9 210 150 10 213 154 11
  93655. -202 150 34 182 157 106 101 98 89 2 2 6
  93656. - 2 2 6 78 78 78 116 116 116 58 58 58
  93657. - 2 2 6 22 22 22 90 90 90 46 46 46
  93658. - 18 18 18 6 6 6 0 0 0 0 0 0
  93659. - 0 0 0 0 0 0 0 0 0 0 0 0
  93660. - 0 0 0 0 0 0 0 0 0 0 0 0
  93661. - 0 0 0 0 0 0 0 0 0 0 0 0
  93662. - 0 0 0 0 0 0 0 0 0 0 0 0
  93663. - 0 0 0 0 0 0 0 0 0 0 0 0
  93664. - 0 0 0 0 0 0 0 0 0 0 0 0
  93665. - 0 0 0 0 0 0 0 0 0 0 0 0
  93666. - 0 0 0 0 0 0 0 0 0 0 0 0
  93667. - 0 0 0 0 0 0 0 0 0 0 0 0
  93668. - 0 0 0 0 0 0 0 0 0 0 0 0
  93669. - 0 0 0 0 0 0 0 0 0 0 0 0
  93670. - 0 0 0 0 0 0 0 0 0 10 10 10
  93671. - 38 38 38 86 86 86 50 50 50 6 6 6
  93672. -128 128 128 174 154 114 156 107 11 168 122 10
  93673. -198 155 10 184 144 12 197 138 11 200 144 11
  93674. -206 145 10 206 145 10 197 138 11 188 164 115
  93675. -195 195 195 198 198 198 174 174 174 14 14 14
  93676. - 2 2 6 22 22 22 116 116 116 116 116 116
  93677. - 22 22 22 2 2 6 74 74 74 70 70 70
  93678. - 30 30 30 10 10 10 0 0 0 0 0 0
  93679. - 0 0 0 0 0 0 0 0 0 0 0 0
  93680. - 0 0 0 0 0 0 0 0 0 0 0 0
  93681. - 0 0 0 0 0 0 0 0 0 0 0 0
  93682. - 0 0 0 0 0 0 0 0 0 0 0 0
  93683. - 0 0 0 0 0 0 0 0 0 0 0 0
  93684. - 0 0 0 0 0 0 0 0 0 0 0 0
  93685. - 0 0 0 0 0 0 0 0 0 0 0 0
  93686. - 0 0 0 0 0 0 0 0 0 0 0 0
  93687. - 0 0 0 0 0 0 0 0 0 0 0 0
  93688. - 0 0 0 0 0 0 0 0 0 0 0 0
  93689. - 0 0 0 0 0 0 0 0 0 0 0 0
  93690. - 0 0 0 0 0 0 6 6 6 18 18 18
  93691. - 50 50 50 101 101 101 26 26 26 10 10 10
  93692. -138 138 138 190 190 190 174 154 114 156 107 11
  93693. -197 138 11 200 144 11 197 138 11 192 133 9
  93694. -180 123 7 190 142 34 190 178 144 187 187 187
  93695. -202 202 202 221 221 221 214 214 214 66 66 66
  93696. - 2 2 6 2 2 6 50 50 50 62 62 62
  93697. - 6 6 6 2 2 6 10 10 10 90 90 90
  93698. - 50 50 50 18 18 18 6 6 6 0 0 0
  93699. - 0 0 0 0 0 0 0 0 0 0 0 0
  93700. - 0 0 0 0 0 0 0 0 0 0 0 0
  93701. - 0 0 0 0 0 0 0 0 0 0 0 0
  93702. - 0 0 0 0 0 0 0 0 0 0 0 0
  93703. - 0 0 0 0 0 0 0 0 0 0 0 0
  93704. - 0 0 0 0 0 0 0 0 0 0 0 0
  93705. - 0 0 0 0 0 0 0 0 0 0 0 0
  93706. - 0 0 0 0 0 0 0 0 0 0 0 0
  93707. - 0 0 0 0 0 0 0 0 0 0 0 0
  93708. - 0 0 0 0 0 0 0 0 0 0 0 0
  93709. - 0 0 0 0 0 0 0 0 0 0 0 0
  93710. - 0 0 0 0 0 0 10 10 10 34 34 34
  93711. - 74 74 74 74 74 74 2 2 6 6 6 6
  93712. -144 144 144 198 198 198 190 190 190 178 166 146
  93713. -154 121 60 156 107 11 156 107 11 168 124 44
  93714. -174 154 114 187 187 187 190 190 190 210 210 210
  93715. -246 246 246 253 253 253 253 253 253 182 182 182
  93716. - 6 6 6 2 2 6 2 2 6 2 2 6
  93717. - 2 2 6 2 2 6 2 2 6 62 62 62
  93718. - 74 74 74 34 34 34 14 14 14 0 0 0
  93719. - 0 0 0 0 0 0 0 0 0 0 0 0
  93720. - 0 0 0 0 0 0 0 0 0 0 0 0
  93721. - 0 0 0 0 0 0 0 0 0 0 0 0
  93722. - 0 0 0 0 0 0 0 0 0 0 0 0
  93723. - 0 0 0 0 0 0 0 0 0 0 0 0
  93724. - 0 0 0 0 0 0 0 0 0 0 0 0
  93725. - 0 0 0 0 0 0 0 0 0 0 0 0
  93726. - 0 0 0 0 0 0 0 0 0 0 0 0
  93727. - 0 0 0 0 0 0 0 0 0 0 0 0
  93728. - 0 0 0 0 0 0 0 0 0 0 0 0
  93729. - 0 0 0 0 0 0 0 0 0 0 0 0
  93730. - 0 0 0 10 10 10 22 22 22 54 54 54
  93731. - 94 94 94 18 18 18 2 2 6 46 46 46
  93732. -234 234 234 221 221 221 190 190 190 190 190 190
  93733. -190 190 190 187 187 187 187 187 187 190 190 190
  93734. -190 190 190 195 195 195 214 214 214 242 242 242
  93735. -253 253 253 253 253 253 253 253 253 253 253 253
  93736. - 82 82 82 2 2 6 2 2 6 2 2 6
  93737. - 2 2 6 2 2 6 2 2 6 14 14 14
  93738. - 86 86 86 54 54 54 22 22 22 6 6 6
  93739. - 0 0 0 0 0 0 0 0 0 0 0 0
  93740. - 0 0 0 0 0 0 0 0 0 0 0 0
  93741. - 0 0 0 0 0 0 0 0 0 0 0 0
  93742. - 0 0 0 0 0 0 0 0 0 0 0 0
  93743. - 0 0 0 0 0 0 0 0 0 0 0 0
  93744. - 0 0 0 0 0 0 0 0 0 0 0 0
  93745. - 0 0 0 0 0 0 0 0 0 0 0 0
  93746. - 0 0 0 0 0 0 0 0 0 0 0 0
  93747. - 0 0 0 0 0 0 0 0 0 0 0 0
  93748. - 0 0 0 0 0 0 0 0 0 0 0 0
  93749. - 0 0 0 0 0 0 0 0 0 0 0 0
  93750. - 6 6 6 18 18 18 46 46 46 90 90 90
  93751. - 46 46 46 18 18 18 6 6 6 182 182 182
  93752. -253 253 253 246 246 246 206 206 206 190 190 190
  93753. -190 190 190 190 190 190 190 190 190 190 190 190
  93754. -206 206 206 231 231 231 250 250 250 253 253 253
  93755. -253 253 253 253 253 253 253 253 253 253 253 253
  93756. -202 202 202 14 14 14 2 2 6 2 2 6
  93757. - 2 2 6 2 2 6 2 2 6 2 2 6
  93758. - 42 42 42 86 86 86 42 42 42 18 18 18
  93759. - 6 6 6 0 0 0 0 0 0 0 0 0
  93760. - 0 0 0 0 0 0 0 0 0 0 0 0
  93761. - 0 0 0 0 0 0 0 0 0 0 0 0
  93762. - 0 0 0 0 0 0 0 0 0 0 0 0
  93763. - 0 0 0 0 0 0 0 0 0 0 0 0
  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 6 6 6
  93770. - 14 14 14 38 38 38 74 74 74 66 66 66
  93771. - 2 2 6 6 6 6 90 90 90 250 250 250
  93772. -253 253 253 253 253 253 238 238 238 198 198 198
  93773. -190 190 190 190 190 190 195 195 195 221 221 221
  93774. -246 246 246 253 253 253 253 253 253 253 253 253
  93775. -253 253 253 253 253 253 253 253 253 253 253 253
  93776. -253 253 253 82 82 82 2 2 6 2 2 6
  93777. - 2 2 6 2 2 6 2 2 6 2 2 6
  93778. - 2 2 6 78 78 78 70 70 70 34 34 34
  93779. - 14 14 14 6 6 6 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 14 14 14
  93790. - 34 34 34 66 66 66 78 78 78 6 6 6
  93791. - 2 2 6 18 18 18 218 218 218 253 253 253
  93792. -253 253 253 253 253 253 253 253 253 246 246 246
  93793. -226 226 226 231 231 231 246 246 246 253 253 253
  93794. -253 253 253 253 253 253 253 253 253 253 253 253
  93795. -253 253 253 253 253 253 253 253 253 253 253 253
  93796. -253 253 253 178 178 178 2 2 6 2 2 6
  93797. - 2 2 6 2 2 6 2 2 6 2 2 6
  93798. - 2 2 6 18 18 18 90 90 90 62 62 62
  93799. - 30 30 30 10 10 10 0 0 0 0 0 0
  93800. - 0 0 0 0 0 0 0 0 0 0 0 0
  93801. - 0 0 0 0 0 0 0 0 0 0 0 0
  93802. - 0 0 0 0 0 0 0 0 0 0 0 0
  93803. - 0 0 0 0 0 0 0 0 0 0 0 0
  93804. - 0 0 0 0 0 0 0 0 0 0 0 0
  93805. - 0 0 0 0 0 0 0 0 0 0 0 0
  93806. - 0 0 0 0 0 0 0 0 0 0 0 0
  93807. - 0 0 0 0 0 0 0 0 0 0 0 0
  93808. - 0 0 0 0 0 0 0 0 0 0 0 0
  93809. - 0 0 0 0 0 0 10 10 10 26 26 26
  93810. - 58 58 58 90 90 90 18 18 18 2 2 6
  93811. - 2 2 6 110 110 110 253 253 253 253 253 253
  93812. -253 253 253 253 253 253 253 253 253 253 253 253
  93813. -250 250 250 253 253 253 253 253 253 253 253 253
  93814. -253 253 253 253 253 253 253 253 253 253 253 253
  93815. -253 253 253 253 253 253 253 253 253 253 253 253
  93816. -253 253 253 231 231 231 18 18 18 2 2 6
  93817. - 2 2 6 2 2 6 2 2 6 2 2 6
  93818. - 2 2 6 2 2 6 18 18 18 94 94 94
  93819. - 54 54 54 26 26 26 10 10 10 0 0 0
  93820. - 0 0 0 0 0 0 0 0 0 0 0 0
  93821. - 0 0 0 0 0 0 0 0 0 0 0 0
  93822. - 0 0 0 0 0 0 0 0 0 0 0 0
  93823. - 0 0 0 0 0 0 0 0 0 0 0 0
  93824. - 0 0 0 0 0 0 0 0 0 0 0 0
  93825. - 0 0 0 0 0 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 6 6 6 22 22 22 50 50 50
  93830. - 90 90 90 26 26 26 2 2 6 2 2 6
  93831. - 14 14 14 195 195 195 250 250 250 253 253 253
  93832. -253 253 253 253 253 253 253 253 253 253 253 253
  93833. -253 253 253 253 253 253 253 253 253 253 253 253
  93834. -253 253 253 253 253 253 253 253 253 253 253 253
  93835. -253 253 253 253 253 253 253 253 253 253 253 253
  93836. -250 250 250 242 242 242 54 54 54 2 2 6
  93837. - 2 2 6 2 2 6 2 2 6 2 2 6
  93838. - 2 2 6 2 2 6 2 2 6 38 38 38
  93839. - 86 86 86 50 50 50 22 22 22 6 6 6
  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 0 0 0 0 0 0 0 0 0 0
  93846. - 0 0 0 0 0 0 0 0 0 0 0 0
  93847. - 0 0 0 0 0 0 0 0 0 0 0 0
  93848. - 0 0 0 0 0 0 0 0 0 0 0 0
  93849. - 6 6 6 14 14 14 38 38 38 82 82 82
  93850. - 34 34 34 2 2 6 2 2 6 2 2 6
  93851. - 42 42 42 195 195 195 246 246 246 253 253 253
  93852. -253 253 253 253 253 253 253 253 253 250 250 250
  93853. -242 242 242 242 242 242 250 250 250 253 253 253
  93854. -253 253 253 253 253 253 253 253 253 253 253 253
  93855. -253 253 253 250 250 250 246 246 246 238 238 238
  93856. -226 226 226 231 231 231 101 101 101 6 6 6
  93857. - 2 2 6 2 2 6 2 2 6 2 2 6
  93858. - 2 2 6 2 2 6 2 2 6 2 2 6
  93859. - 38 38 38 82 82 82 42 42 42 14 14 14
  93860. - 6 6 6 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. - 10 10 10 26 26 26 62 62 62 66 66 66
  93870. - 2 2 6 2 2 6 2 2 6 6 6 6
  93871. - 70 70 70 170 170 170 206 206 206 234 234 234
  93872. -246 246 246 250 250 250 250 250 250 238 238 238
  93873. -226 226 226 231 231 231 238 238 238 250 250 250
  93874. -250 250 250 250 250 250 246 246 246 231 231 231
  93875. -214 214 214 206 206 206 202 202 202 202 202 202
  93876. -198 198 198 202 202 202 182 182 182 18 18 18
  93877. - 2 2 6 2 2 6 2 2 6 2 2 6
  93878. - 2 2 6 2 2 6 2 2 6 2 2 6
  93879. - 2 2 6 62 62 62 66 66 66 30 30 30
  93880. - 10 10 10 0 0 0 0 0 0 0 0 0
  93881. - 0 0 0 0 0 0 0 0 0 0 0 0
  93882. - 0 0 0 0 0 0 0 0 0 0 0 0
  93883. - 0 0 0 0 0 0 0 0 0 0 0 0
  93884. - 0 0 0 0 0 0 0 0 0 0 0 0
  93885. - 0 0 0 0 0 0 0 0 0 0 0 0
  93886. - 0 0 0 0 0 0 0 0 0 0 0 0
  93887. - 0 0 0 0 0 0 0 0 0 0 0 0
  93888. - 0 0 0 0 0 0 0 0 0 0 0 0
  93889. - 14 14 14 42 42 42 82 82 82 18 18 18
  93890. - 2 2 6 2 2 6 2 2 6 10 10 10
  93891. - 94 94 94 182 182 182 218 218 218 242 242 242
  93892. -250 250 250 253 253 253 253 253 253 250 250 250
  93893. -234 234 234 253 253 253 253 253 253 253 253 253
  93894. -253 253 253 253 253 253 253 253 253 246 246 246
  93895. -238 238 238 226 226 226 210 210 210 202 202 202
  93896. -195 195 195 195 195 195 210 210 210 158 158 158
  93897. - 6 6 6 14 14 14 50 50 50 14 14 14
  93898. - 2 2 6 2 2 6 2 2 6 2 2 6
  93899. - 2 2 6 6 6 6 86 86 86 46 46 46
  93900. - 18 18 18 6 6 6 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 0 0 0 0 0 0 0 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 6 6 6
  93909. - 22 22 22 54 54 54 70 70 70 2 2 6
  93910. - 2 2 6 10 10 10 2 2 6 22 22 22
  93911. -166 166 166 231 231 231 250 250 250 253 253 253
  93912. -253 253 253 253 253 253 253 253 253 250 250 250
  93913. -242 242 242 253 253 253 253 253 253 253 253 253
  93914. -253 253 253 253 253 253 253 253 253 253 253 253
  93915. -253 253 253 253 253 253 253 253 253 246 246 246
  93916. -231 231 231 206 206 206 198 198 198 226 226 226
  93917. - 94 94 94 2 2 6 6 6 6 38 38 38
  93918. - 30 30 30 2 2 6 2 2 6 2 2 6
  93919. - 2 2 6 2 2 6 62 62 62 66 66 66
  93920. - 26 26 26 10 10 10 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 0 0 0 0 0 0 0 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 10 10 10
  93929. - 30 30 30 74 74 74 50 50 50 2 2 6
  93930. - 26 26 26 26 26 26 2 2 6 106 106 106
  93931. -238 238 238 253 253 253 253 253 253 253 253 253
  93932. -253 253 253 253 253 253 253 253 253 253 253 253
  93933. -253 253 253 253 253 253 253 253 253 253 253 253
  93934. -253 253 253 253 253 253 253 253 253 253 253 253
  93935. -253 253 253 253 253 253 253 253 253 253 253 253
  93936. -253 253 253 246 246 246 218 218 218 202 202 202
  93937. -210 210 210 14 14 14 2 2 6 2 2 6
  93938. - 30 30 30 22 22 22 2 2 6 2 2 6
  93939. - 2 2 6 2 2 6 18 18 18 86 86 86
  93940. - 42 42 42 14 14 14 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 14 14 14
  93949. - 42 42 42 90 90 90 22 22 22 2 2 6
  93950. - 42 42 42 2 2 6 18 18 18 218 218 218
  93951. -253 253 253 253 253 253 253 253 253 253 253 253
  93952. -253 253 253 253 253 253 253 253 253 253 253 253
  93953. -253 253 253 253 253 253 253 253 253 253 253 253
  93954. -253 253 253 253 253 253 253 253 253 253 253 253
  93955. -253 253 253 253 253 253 253 253 253 253 253 253
  93956. -253 253 253 253 253 253 250 250 250 221 221 221
  93957. -218 218 218 101 101 101 2 2 6 14 14 14
  93958. - 18 18 18 38 38 38 10 10 10 2 2 6
  93959. - 2 2 6 2 2 6 2 2 6 78 78 78
  93960. - 58 58 58 22 22 22 6 6 6 0 0 0
  93961. - 0 0 0 0 0 0 0 0 0 0 0 0
  93962. - 0 0 0 0 0 0 0 0 0 0 0 0
  93963. - 0 0 0 0 0 0 0 0 0 0 0 0
  93964. - 0 0 0 0 0 0 0 0 0 0 0 0
  93965. - 0 0 0 0 0 0 0 0 0 0 0 0
  93966. - 0 0 0 0 0 0 0 0 0 0 0 0
  93967. - 0 0 0 0 0 0 0 0 0 0 0 0
  93968. - 0 0 0 0 0 0 6 6 6 18 18 18
  93969. - 54 54 54 82 82 82 2 2 6 26 26 26
  93970. - 22 22 22 2 2 6 123 123 123 253 253 253
  93971. -253 253 253 253 253 253 253 253 253 253 253 253
  93972. -253 253 253 253 253 253 253 253 253 253 253 253
  93973. -253 253 253 253 253 253 253 253 253 253 253 253
  93974. -253 253 253 253 253 253 253 253 253 253 253 253
  93975. -253 253 253 253 253 253 253 253 253 253 253 253
  93976. -253 253 253 253 253 253 253 253 253 250 250 250
  93977. -238 238 238 198 198 198 6 6 6 38 38 38
  93978. - 58 58 58 26 26 26 38 38 38 2 2 6
  93979. - 2 2 6 2 2 6 2 2 6 46 46 46
  93980. - 78 78 78 30 30 30 10 10 10 0 0 0
  93981. - 0 0 0 0 0 0 0 0 0 0 0 0
  93982. - 0 0 0 0 0 0 0 0 0 0 0 0
  93983. - 0 0 0 0 0 0 0 0 0 0 0 0
  93984. - 0 0 0 0 0 0 0 0 0 0 0 0
  93985. - 0 0 0 0 0 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 10 10 10 30 30 30
  93989. - 74 74 74 58 58 58 2 2 6 42 42 42
  93990. - 2 2 6 22 22 22 231 231 231 253 253 253
  93991. -253 253 253 253 253 253 253 253 253 253 253 253
  93992. -253 253 253 253 253 253 253 253 253 250 250 250
  93993. -253 253 253 253 253 253 253 253 253 253 253 253
  93994. -253 253 253 253 253 253 253 253 253 253 253 253
  93995. -253 253 253 253 253 253 253 253 253 253 253 253
  93996. -253 253 253 253 253 253 253 253 253 253 253 253
  93997. -253 253 253 246 246 246 46 46 46 38 38 38
  93998. - 42 42 42 14 14 14 38 38 38 14 14 14
  93999. - 2 2 6 2 2 6 2 2 6 6 6 6
  94000. - 86 86 86 46 46 46 14 14 14 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 0 0 0 0 0 0 0
  94006. - 0 0 0 0 0 0 0 0 0 0 0 0
  94007. - 0 0 0 0 0 0 0 0 0 0 0 0
  94008. - 0 0 0 6 6 6 14 14 14 42 42 42
  94009. - 90 90 90 18 18 18 18 18 18 26 26 26
  94010. - 2 2 6 116 116 116 253 253 253 253 253 253
  94011. -253 253 253 253 253 253 253 253 253 253 253 253
  94012. -253 253 253 253 253 253 250 250 250 238 238 238
  94013. -253 253 253 253 253 253 253 253 253 253 253 253
  94014. -253 253 253 253 253 253 253 253 253 253 253 253
  94015. -253 253 253 253 253 253 253 253 253 253 253 253
  94016. -253 253 253 253 253 253 253 253 253 253 253 253
  94017. -253 253 253 253 253 253 94 94 94 6 6 6
  94018. - 2 2 6 2 2 6 10 10 10 34 34 34
  94019. - 2 2 6 2 2 6 2 2 6 2 2 6
  94020. - 74 74 74 58 58 58 22 22 22 6 6 6
  94021. - 0 0 0 0 0 0 0 0 0 0 0 0
  94022. - 0 0 0 0 0 0 0 0 0 0 0 0
  94023. - 0 0 0 0 0 0 0 0 0 0 0 0
  94024. - 0 0 0 0 0 0 0 0 0 0 0 0
  94025. - 0 0 0 0 0 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 10 10 10 26 26 26 66 66 66
  94029. - 82 82 82 2 2 6 38 38 38 6 6 6
  94030. - 14 14 14 210 210 210 253 253 253 253 253 253
  94031. -253 253 253 253 253 253 253 253 253 253 253 253
  94032. -253 253 253 253 253 253 246 246 246 242 242 242
  94033. -253 253 253 253 253 253 253 253 253 253 253 253
  94034. -253 253 253 253 253 253 253 253 253 253 253 253
  94035. -253 253 253 253 253 253 253 253 253 253 253 253
  94036. -253 253 253 253 253 253 253 253 253 253 253 253
  94037. -253 253 253 253 253 253 144 144 144 2 2 6
  94038. - 2 2 6 2 2 6 2 2 6 46 46 46
  94039. - 2 2 6 2 2 6 2 2 6 2 2 6
  94040. - 42 42 42 74 74 74 30 30 30 10 10 10
  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 0 0 0 0 0 0 0 0 0 0
  94046. - 0 0 0 0 0 0 0 0 0 0 0 0
  94047. - 0 0 0 0 0 0 0 0 0 0 0 0
  94048. - 6 6 6 14 14 14 42 42 42 90 90 90
  94049. - 26 26 26 6 6 6 42 42 42 2 2 6
  94050. - 74 74 74 250 250 250 253 253 253 253 253 253
  94051. -253 253 253 253 253 253 253 253 253 253 253 253
  94052. -253 253 253 253 253 253 242 242 242 242 242 242
  94053. -253 253 253 253 253 253 253 253 253 253 253 253
  94054. -253 253 253 253 253 253 253 253 253 253 253 253
  94055. -253 253 253 253 253 253 253 253 253 253 253 253
  94056. -253 253 253 253 253 253 253 253 253 253 253 253
  94057. -253 253 253 253 253 253 182 182 182 2 2 6
  94058. - 2 2 6 2 2 6 2 2 6 46 46 46
  94059. - 2 2 6 2 2 6 2 2 6 2 2 6
  94060. - 10 10 10 86 86 86 38 38 38 10 10 10
  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. - 10 10 10 26 26 26 66 66 66 82 82 82
  94069. - 2 2 6 22 22 22 18 18 18 2 2 6
  94070. -149 149 149 253 253 253 253 253 253 253 253 253
  94071. -253 253 253 253 253 253 253 253 253 253 253 253
  94072. -253 253 253 253 253 253 234 234 234 242 242 242
  94073. -253 253 253 253 253 253 253 253 253 253 253 253
  94074. -253 253 253 253 253 253 253 253 253 253 253 253
  94075. -253 253 253 253 253 253 253 253 253 253 253 253
  94076. -253 253 253 253 253 253 253 253 253 253 253 253
  94077. -253 253 253 253 253 253 206 206 206 2 2 6
  94078. - 2 2 6 2 2 6 2 2 6 38 38 38
  94079. - 2 2 6 2 2 6 2 2 6 2 2 6
  94080. - 6 6 6 86 86 86 46 46 46 14 14 14
  94081. - 0 0 0 0 0 0 0 0 0 0 0 0
  94082. - 0 0 0 0 0 0 0 0 0 0 0 0
  94083. - 0 0 0 0 0 0 0 0 0 0 0 0
  94084. - 0 0 0 0 0 0 0 0 0 0 0 0
  94085. - 0 0 0 0 0 0 0 0 0 0 0 0
  94086. - 0 0 0 0 0 0 0 0 0 0 0 0
  94087. - 0 0 0 0 0 0 0 0 0 6 6 6
  94088. - 18 18 18 46 46 46 86 86 86 18 18 18
  94089. - 2 2 6 34 34 34 10 10 10 6 6 6
  94090. -210 210 210 253 253 253 253 253 253 253 253 253
  94091. -253 253 253 253 253 253 253 253 253 253 253 253
  94092. -253 253 253 253 253 253 234 234 234 242 242 242
  94093. -253 253 253 253 253 253 253 253 253 253 253 253
  94094. -253 253 253 253 253 253 253 253 253 253 253 253
  94095. -253 253 253 253 253 253 253 253 253 253 253 253
  94096. -253 253 253 253 253 253 253 253 253 253 253 253
  94097. -253 253 253 253 253 253 221 221 221 6 6 6
  94098. - 2 2 6 2 2 6 6 6 6 30 30 30
  94099. - 2 2 6 2 2 6 2 2 6 2 2 6
  94100. - 2 2 6 82 82 82 54 54 54 18 18 18
  94101. - 6 6 6 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 10 10 10
  94108. - 26 26 26 66 66 66 62 62 62 2 2 6
  94109. - 2 2 6 38 38 38 10 10 10 26 26 26
  94110. -238 238 238 253 253 253 253 253 253 253 253 253
  94111. -253 253 253 253 253 253 253 253 253 253 253 253
  94112. -253 253 253 253 253 253 231 231 231 238 238 238
  94113. -253 253 253 253 253 253 253 253 253 253 253 253
  94114. -253 253 253 253 253 253 253 253 253 253 253 253
  94115. -253 253 253 253 253 253 253 253 253 253 253 253
  94116. -253 253 253 253 253 253 253 253 253 253 253 253
  94117. -253 253 253 253 253 253 231 231 231 6 6 6
  94118. - 2 2 6 2 2 6 10 10 10 30 30 30
  94119. - 2 2 6 2 2 6 2 2 6 2 2 6
  94120. - 2 2 6 66 66 66 58 58 58 22 22 22
  94121. - 6 6 6 0 0 0 0 0 0 0 0 0
  94122. - 0 0 0 0 0 0 0 0 0 0 0 0
  94123. - 0 0 0 0 0 0 0 0 0 0 0 0
  94124. - 0 0 0 0 0 0 0 0 0 0 0 0
  94125. - 0 0 0 0 0 0 0 0 0 0 0 0
  94126. - 0 0 0 0 0 0 0 0 0 0 0 0
  94127. - 0 0 0 0 0 0 0 0 0 10 10 10
  94128. - 38 38 38 78 78 78 6 6 6 2 2 6
  94129. - 2 2 6 46 46 46 14 14 14 42 42 42
  94130. -246 246 246 253 253 253 253 253 253 253 253 253
  94131. -253 253 253 253 253 253 253 253 253 253 253 253
  94132. -253 253 253 253 253 253 231 231 231 242 242 242
  94133. -253 253 253 253 253 253 253 253 253 253 253 253
  94134. -253 253 253 253 253 253 253 253 253 253 253 253
  94135. -253 253 253 253 253 253 253 253 253 253 253 253
  94136. -253 253 253 253 253 253 253 253 253 253 253 253
  94137. -253 253 253 253 253 253 234 234 234 10 10 10
  94138. - 2 2 6 2 2 6 22 22 22 14 14 14
  94139. - 2 2 6 2 2 6 2 2 6 2 2 6
  94140. - 2 2 6 66 66 66 62 62 62 22 22 22
  94141. - 6 6 6 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 0 0 0 0
  94145. - 0 0 0 0 0 0 0 0 0 0 0 0
  94146. - 0 0 0 0 0 0 0 0 0 0 0 0
  94147. - 0 0 0 0 0 0 6 6 6 18 18 18
  94148. - 50 50 50 74 74 74 2 2 6 2 2 6
  94149. - 14 14 14 70 70 70 34 34 34 62 62 62
  94150. -250 250 250 253 253 253 253 253 253 253 253 253
  94151. -253 253 253 253 253 253 253 253 253 253 253 253
  94152. -253 253 253 253 253 253 231 231 231 246 246 246
  94153. -253 253 253 253 253 253 253 253 253 253 253 253
  94154. -253 253 253 253 253 253 253 253 253 253 253 253
  94155. -253 253 253 253 253 253 253 253 253 253 253 253
  94156. -253 253 253 253 253 253 253 253 253 253 253 253
  94157. -253 253 253 253 253 253 234 234 234 14 14 14
  94158. - 2 2 6 2 2 6 30 30 30 2 2 6
  94159. - 2 2 6 2 2 6 2 2 6 2 2 6
  94160. - 2 2 6 66 66 66 62 62 62 22 22 22
  94161. - 6 6 6 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 0 0 0 0
  94165. - 0 0 0 0 0 0 0 0 0 0 0 0
  94166. - 0 0 0 0 0 0 0 0 0 0 0 0
  94167. - 0 0 0 0 0 0 6 6 6 18 18 18
  94168. - 54 54 54 62 62 62 2 2 6 2 2 6
  94169. - 2 2 6 30 30 30 46 46 46 70 70 70
  94170. -250 250 250 253 253 253 253 253 253 253 253 253
  94171. -253 253 253 253 253 253 253 253 253 253 253 253
  94172. -253 253 253 253 253 253 231 231 231 246 246 246
  94173. -253 253 253 253 253 253 253 253 253 253 253 253
  94174. -253 253 253 253 253 253 253 253 253 253 253 253
  94175. -253 253 253 253 253 253 253 253 253 253 253 253
  94176. -253 253 253 253 253 253 253 253 253 253 253 253
  94177. -253 253 253 253 253 253 226 226 226 10 10 10
  94178. - 2 2 6 6 6 6 30 30 30 2 2 6
  94179. - 2 2 6 2 2 6 2 2 6 2 2 6
  94180. - 2 2 6 66 66 66 58 58 58 22 22 22
  94181. - 6 6 6 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 6 6 6 22 22 22
  94188. - 58 58 58 62 62 62 2 2 6 2 2 6
  94189. - 2 2 6 2 2 6 30 30 30 78 78 78
  94190. -250 250 250 253 253 253 253 253 253 253 253 253
  94191. -253 253 253 253 253 253 253 253 253 253 253 253
  94192. -253 253 253 253 253 253 231 231 231 246 246 246
  94193. -253 253 253 253 253 253 253 253 253 253 253 253
  94194. -253 253 253 253 253 253 253 253 253 253 253 253
  94195. -253 253 253 253 253 253 253 253 253 253 253 253
  94196. -253 253 253 253 253 253 253 253 253 253 253 253
  94197. -253 253 253 253 253 253 206 206 206 2 2 6
  94198. - 22 22 22 34 34 34 18 14 6 22 22 22
  94199. - 26 26 26 18 18 18 6 6 6 2 2 6
  94200. - 2 2 6 82 82 82 54 54 54 18 18 18
  94201. - 6 6 6 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 6 6 6 26 26 26
  94208. - 62 62 62 106 106 106 74 54 14 185 133 11
  94209. -210 162 10 121 92 8 6 6 6 62 62 62
  94210. -238 238 238 253 253 253 253 253 253 253 253 253
  94211. -253 253 253 253 253 253 253 253 253 253 253 253
  94212. -253 253 253 253 253 253 231 231 231 246 246 246
  94213. -253 253 253 253 253 253 253 253 253 253 253 253
  94214. -253 253 253 253 253 253 253 253 253 253 253 253
  94215. -253 253 253 253 253 253 253 253 253 253 253 253
  94216. -253 253 253 253 253 253 253 253 253 253 253 253
  94217. -253 253 253 253 253 253 158 158 158 18 18 18
  94218. - 14 14 14 2 2 6 2 2 6 2 2 6
  94219. - 6 6 6 18 18 18 66 66 66 38 38 38
  94220. - 6 6 6 94 94 94 50 50 50 18 18 18
  94221. - 6 6 6 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 6 6 6
  94227. - 10 10 10 10 10 10 18 18 18 38 38 38
  94228. - 78 78 78 142 134 106 216 158 10 242 186 14
  94229. -246 190 14 246 190 14 156 118 10 10 10 10
  94230. - 90 90 90 238 238 238 253 253 253 253 253 253
  94231. -253 253 253 253 253 253 253 253 253 253 253 253
  94232. -253 253 253 253 253 253 231 231 231 250 250 250
  94233. -253 253 253 253 253 253 253 253 253 253 253 253
  94234. -253 253 253 253 253 253 253 253 253 253 253 253
  94235. -253 253 253 253 253 253 253 253 253 253 253 253
  94236. -253 253 253 253 253 253 253 253 253 246 230 190
  94237. -238 204 91 238 204 91 181 142 44 37 26 9
  94238. - 2 2 6 2 2 6 2 2 6 2 2 6
  94239. - 2 2 6 2 2 6 38 38 38 46 46 46
  94240. - 26 26 26 106 106 106 54 54 54 18 18 18
  94241. - 6 6 6 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 6 6 6 14 14 14 22 22 22
  94247. - 30 30 30 38 38 38 50 50 50 70 70 70
  94248. -106 106 106 190 142 34 226 170 11 242 186 14
  94249. -246 190 14 246 190 14 246 190 14 154 114 10
  94250. - 6 6 6 74 74 74 226 226 226 253 253 253
  94251. -253 253 253 253 253 253 253 253 253 253 253 253
  94252. -253 253 253 253 253 253 231 231 231 250 250 250
  94253. -253 253 253 253 253 253 253 253 253 253 253 253
  94254. -253 253 253 253 253 253 253 253 253 253 253 253
  94255. -253 253 253 253 253 253 253 253 253 253 253 253
  94256. -253 253 253 253 253 253 253 253 253 228 184 62
  94257. -241 196 14 241 208 19 232 195 16 38 30 10
  94258. - 2 2 6 2 2 6 2 2 6 2 2 6
  94259. - 2 2 6 6 6 6 30 30 30 26 26 26
  94260. -203 166 17 154 142 90 66 66 66 26 26 26
  94261. - 6 6 6 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. - 6 6 6 18 18 18 38 38 38 58 58 58
  94267. - 78 78 78 86 86 86 101 101 101 123 123 123
  94268. -175 146 61 210 150 10 234 174 13 246 186 14
  94269. -246 190 14 246 190 14 246 190 14 238 190 10
  94270. -102 78 10 2 2 6 46 46 46 198 198 198
  94271. -253 253 253 253 253 253 253 253 253 253 253 253
  94272. -253 253 253 253 253 253 234 234 234 242 242 242
  94273. -253 253 253 253 253 253 253 253 253 253 253 253
  94274. -253 253 253 253 253 253 253 253 253 253 253 253
  94275. -253 253 253 253 253 253 253 253 253 253 253 253
  94276. -253 253 253 253 253 253 253 253 253 224 178 62
  94277. -242 186 14 241 196 14 210 166 10 22 18 6
  94278. - 2 2 6 2 2 6 2 2 6 2 2 6
  94279. - 2 2 6 2 2 6 6 6 6 121 92 8
  94280. -238 202 15 232 195 16 82 82 82 34 34 34
  94281. - 10 10 10 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. - 14 14 14 38 38 38 70 70 70 154 122 46
  94287. -190 142 34 200 144 11 197 138 11 197 138 11
  94288. -213 154 11 226 170 11 242 186 14 246 190 14
  94289. -246 190 14 246 190 14 246 190 14 246 190 14
  94290. -225 175 15 46 32 6 2 2 6 22 22 22
  94291. -158 158 158 250 250 250 253 253 253 253 253 253
  94292. -253 253 253 253 253 253 253 253 253 253 253 253
  94293. -253 253 253 253 253 253 253 253 253 253 253 253
  94294. -253 253 253 253 253 253 253 253 253 253 253 253
  94295. -253 253 253 253 253 253 253 253 253 253 253 253
  94296. -253 253 253 250 250 250 242 242 242 224 178 62
  94297. -239 182 13 236 186 11 213 154 11 46 32 6
  94298. - 2 2 6 2 2 6 2 2 6 2 2 6
  94299. - 2 2 6 2 2 6 61 42 6 225 175 15
  94300. -238 190 10 236 186 11 112 100 78 42 42 42
  94301. - 14 14 14 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 6 6 6
  94306. - 22 22 22 54 54 54 154 122 46 213 154 11
  94307. -226 170 11 230 174 11 226 170 11 226 170 11
  94308. -236 178 12 242 186 14 246 190 14 246 190 14
  94309. -246 190 14 246 190 14 246 190 14 246 190 14
  94310. -241 196 14 184 144 12 10 10 10 2 2 6
  94311. - 6 6 6 116 116 116 242 242 242 253 253 253
  94312. -253 253 253 253 253 253 253 253 253 253 253 253
  94313. -253 253 253 253 253 253 253 253 253 253 253 253
  94314. -253 253 253 253 253 253 253 253 253 253 253 253
  94315. -253 253 253 253 253 253 253 253 253 253 253 253
  94316. -253 253 253 231 231 231 198 198 198 214 170 54
  94317. -236 178 12 236 178 12 210 150 10 137 92 6
  94318. - 18 14 6 2 2 6 2 2 6 2 2 6
  94319. - 6 6 6 70 47 6 200 144 11 236 178 12
  94320. -239 182 13 239 182 13 124 112 88 58 58 58
  94321. - 22 22 22 6 6 6 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 10 10 10
  94326. - 30 30 30 70 70 70 180 133 36 226 170 11
  94327. -239 182 13 242 186 14 242 186 14 246 186 14
  94328. -246 190 14 246 190 14 246 190 14 246 190 14
  94329. -246 190 14 246 190 14 246 190 14 246 190 14
  94330. -246 190 14 232 195 16 98 70 6 2 2 6
  94331. - 2 2 6 2 2 6 66 66 66 221 221 221
  94332. -253 253 253 253 253 253 253 253 253 253 253 253
  94333. -253 253 253 253 253 253 253 253 253 253 253 253
  94334. -253 253 253 253 253 253 253 253 253 253 253 253
  94335. -253 253 253 253 253 253 253 253 253 253 253 253
  94336. -253 253 253 206 206 206 198 198 198 214 166 58
  94337. -230 174 11 230 174 11 216 158 10 192 133 9
  94338. -163 110 8 116 81 8 102 78 10 116 81 8
  94339. -167 114 7 197 138 11 226 170 11 239 182 13
  94340. -242 186 14 242 186 14 162 146 94 78 78 78
  94341. - 34 34 34 14 14 14 6 6 6 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 6 6 6
  94346. - 30 30 30 78 78 78 190 142 34 226 170 11
  94347. -239 182 13 246 190 14 246 190 14 246 190 14
  94348. -246 190 14 246 190 14 246 190 14 246 190 14
  94349. -246 190 14 246 190 14 246 190 14 246 190 14
  94350. -246 190 14 241 196 14 203 166 17 22 18 6
  94351. - 2 2 6 2 2 6 2 2 6 38 38 38
  94352. -218 218 218 253 253 253 253 253 253 253 253 253
  94353. -253 253 253 253 253 253 253 253 253 253 253 253
  94354. -253 253 253 253 253 253 253 253 253 253 253 253
  94355. -253 253 253 253 253 253 253 253 253 253 253 253
  94356. -250 250 250 206 206 206 198 198 198 202 162 69
  94357. -226 170 11 236 178 12 224 166 10 210 150 10
  94358. -200 144 11 197 138 11 192 133 9 197 138 11
  94359. -210 150 10 226 170 11 242 186 14 246 190 14
  94360. -246 190 14 246 186 14 225 175 15 124 112 88
  94361. - 62 62 62 30 30 30 14 14 14 6 6 6
  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 10 10 10
  94366. - 30 30 30 78 78 78 174 135 50 224 166 10
  94367. -239 182 13 246 190 14 246 190 14 246 190 14
  94368. -246 190 14 246 190 14 246 190 14 246 190 14
  94369. -246 190 14 246 190 14 246 190 14 246 190 14
  94370. -246 190 14 246 190 14 241 196 14 139 102 15
  94371. - 2 2 6 2 2 6 2 2 6 2 2 6
  94372. - 78 78 78 250 250 250 253 253 253 253 253 253
  94373. -253 253 253 253 253 253 253 253 253 253 253 253
  94374. -253 253 253 253 253 253 253 253 253 253 253 253
  94375. -253 253 253 253 253 253 253 253 253 253 253 253
  94376. -250 250 250 214 214 214 198 198 198 190 150 46
  94377. -219 162 10 236 178 12 234 174 13 224 166 10
  94378. -216 158 10 213 154 11 213 154 11 216 158 10
  94379. -226 170 11 239 182 13 246 190 14 246 190 14
  94380. -246 190 14 246 190 14 242 186 14 206 162 42
  94381. -101 101 101 58 58 58 30 30 30 14 14 14
  94382. - 6 6 6 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 10 10 10
  94386. - 30 30 30 74 74 74 174 135 50 216 158 10
  94387. -236 178 12 246 190 14 246 190 14 246 190 14
  94388. -246 190 14 246 190 14 246 190 14 246 190 14
  94389. -246 190 14 246 190 14 246 190 14 246 190 14
  94390. -246 190 14 246 190 14 241 196 14 226 184 13
  94391. - 61 42 6 2 2 6 2 2 6 2 2 6
  94392. - 22 22 22 238 238 238 253 253 253 253 253 253
  94393. -253 253 253 253 253 253 253 253 253 253 253 253
  94394. -253 253 253 253 253 253 253 253 253 253 253 253
  94395. -253 253 253 253 253 253 253 253 253 253 253 253
  94396. -253 253 253 226 226 226 187 187 187 180 133 36
  94397. -216 158 10 236 178 12 239 182 13 236 178 12
  94398. -230 174 11 226 170 11 226 170 11 230 174 11
  94399. -236 178 12 242 186 14 246 190 14 246 190 14
  94400. -246 190 14 246 190 14 246 186 14 239 182 13
  94401. -206 162 42 106 106 106 66 66 66 34 34 34
  94402. - 14 14 14 6 6 6 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 6 6 6
  94406. - 26 26 26 70 70 70 163 133 67 213 154 11
  94407. -236 178 12 246 190 14 246 190 14 246 190 14
  94408. -246 190 14 246 190 14 246 190 14 246 190 14
  94409. -246 190 14 246 190 14 246 190 14 246 190 14
  94410. -246 190 14 246 190 14 246 190 14 241 196 14
  94411. -190 146 13 18 14 6 2 2 6 2 2 6
  94412. - 46 46 46 246 246 246 253 253 253 253 253 253
  94413. -253 253 253 253 253 253 253 253 253 253 253 253
  94414. -253 253 253 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 221 221 221 86 86 86 156 107 11
  94417. -216 158 10 236 178 12 242 186 14 246 186 14
  94418. -242 186 14 239 182 13 239 182 13 242 186 14
  94419. -242 186 14 246 186 14 246 190 14 246 190 14
  94420. -246 190 14 246 190 14 246 190 14 246 190 14
  94421. -242 186 14 225 175 15 142 122 72 66 66 66
  94422. - 30 30 30 10 10 10 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 6 6 6
  94426. - 26 26 26 70 70 70 163 133 67 210 150 10
  94427. -236 178 12 246 190 14 246 190 14 246 190 14
  94428. -246 190 14 246 190 14 246 190 14 246 190 14
  94429. -246 190 14 246 190 14 246 190 14 246 190 14
  94430. -246 190 14 246 190 14 246 190 14 246 190 14
  94431. -232 195 16 121 92 8 34 34 34 106 106 106
  94432. -221 221 221 253 253 253 253 253 253 253 253 253
  94433. -253 253 253 253 253 253 253 253 253 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. -242 242 242 82 82 82 18 14 6 163 110 8
  94437. -216 158 10 236 178 12 242 186 14 246 190 14
  94438. -246 190 14 246 190 14 246 190 14 246 190 14
  94439. -246 190 14 246 190 14 246 190 14 246 190 14
  94440. -246 190 14 246 190 14 246 190 14 246 190 14
  94441. -246 190 14 246 190 14 242 186 14 163 133 67
  94442. - 46 46 46 18 18 18 6 6 6 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 10 10 10
  94446. - 30 30 30 78 78 78 163 133 67 210 150 10
  94447. -236 178 12 246 186 14 246 190 14 246 190 14
  94448. -246 190 14 246 190 14 246 190 14 246 190 14
  94449. -246 190 14 246 190 14 246 190 14 246 190 14
  94450. -246 190 14 246 190 14 246 190 14 246 190 14
  94451. -241 196 14 215 174 15 190 178 144 253 253 253
  94452. -253 253 253 253 253 253 253 253 253 253 253 253
  94453. -253 253 253 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 218 218 218
  94456. - 58 58 58 2 2 6 22 18 6 167 114 7
  94457. -216 158 10 236 178 12 246 186 14 246 190 14
  94458. -246 190 14 246 190 14 246 190 14 246 190 14
  94459. -246 190 14 246 190 14 246 190 14 246 190 14
  94460. -246 190 14 246 190 14 246 190 14 246 190 14
  94461. -246 190 14 246 186 14 242 186 14 190 150 46
  94462. - 54 54 54 22 22 22 6 6 6 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 14 14 14
  94466. - 38 38 38 86 86 86 180 133 36 213 154 11
  94467. -236 178 12 246 186 14 246 190 14 246 190 14
  94468. -246 190 14 246 190 14 246 190 14 246 190 14
  94469. -246 190 14 246 190 14 246 190 14 246 190 14
  94470. -246 190 14 246 190 14 246 190 14 246 190 14
  94471. -246 190 14 232 195 16 190 146 13 214 214 214
  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 250 250 250 170 170 170 26 26 26
  94476. - 2 2 6 2 2 6 37 26 9 163 110 8
  94477. -219 162 10 239 182 13 246 186 14 246 190 14
  94478. -246 190 14 246 190 14 246 190 14 246 190 14
  94479. -246 190 14 246 190 14 246 190 14 246 190 14
  94480. -246 190 14 246 190 14 246 190 14 246 190 14
  94481. -246 186 14 236 178 12 224 166 10 142 122 72
  94482. - 46 46 46 18 18 18 6 6 6 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 6 6 6 18 18 18
  94486. - 50 50 50 109 106 95 192 133 9 224 166 10
  94487. -242 186 14 246 190 14 246 190 14 246 190 14
  94488. -246 190 14 246 190 14 246 190 14 246 190 14
  94489. -246 190 14 246 190 14 246 190 14 246 190 14
  94490. -246 190 14 246 190 14 246 190 14 246 190 14
  94491. -242 186 14 226 184 13 210 162 10 142 110 46
  94492. -226 226 226 253 253 253 253 253 253 253 253 253
  94493. -253 253 253 253 253 253 253 253 253 253 253 253
  94494. -253 253 253 253 253 253 253 253 253 253 253 253
  94495. -198 198 198 66 66 66 2 2 6 2 2 6
  94496. - 2 2 6 2 2 6 50 34 6 156 107 11
  94497. -219 162 10 239 182 13 246 186 14 246 190 14
  94498. -246 190 14 246 190 14 246 190 14 246 190 14
  94499. -246 190 14 246 190 14 246 190 14 246 190 14
  94500. -246 190 14 246 190 14 246 190 14 242 186 14
  94501. -234 174 13 213 154 11 154 122 46 66 66 66
  94502. - 30 30 30 10 10 10 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 6 6 6 22 22 22
  94506. - 58 58 58 154 121 60 206 145 10 234 174 13
  94507. -242 186 14 246 186 14 246 190 14 246 190 14
  94508. -246 190 14 246 190 14 246 190 14 246 190 14
  94509. -246 190 14 246 190 14 246 190 14 246 190 14
  94510. -246 190 14 246 190 14 246 190 14 246 190 14
  94511. -246 186 14 236 178 12 210 162 10 163 110 8
  94512. - 61 42 6 138 138 138 218 218 218 250 250 250
  94513. -253 253 253 253 253 253 253 253 253 250 250 250
  94514. -242 242 242 210 210 210 144 144 144 66 66 66
  94515. - 6 6 6 2 2 6 2 2 6 2 2 6
  94516. - 2 2 6 2 2 6 61 42 6 163 110 8
  94517. -216 158 10 236 178 12 246 190 14 246 190 14
  94518. -246 190 14 246 190 14 246 190 14 246 190 14
  94519. -246 190 14 246 190 14 246 190 14 246 190 14
  94520. -246 190 14 239 182 13 230 174 11 216 158 10
  94521. -190 142 34 124 112 88 70 70 70 38 38 38
  94522. - 18 18 18 6 6 6 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 6 6 6 22 22 22
  94526. - 62 62 62 168 124 44 206 145 10 224 166 10
  94527. -236 178 12 239 182 13 242 186 14 242 186 14
  94528. -246 186 14 246 190 14 246 190 14 246 190 14
  94529. -246 190 14 246 190 14 246 190 14 246 190 14
  94530. -246 190 14 246 190 14 246 190 14 246 190 14
  94531. -246 190 14 236 178 12 216 158 10 175 118 6
  94532. - 80 54 7 2 2 6 6 6 6 30 30 30
  94533. - 54 54 54 62 62 62 50 50 50 38 38 38
  94534. - 14 14 14 2 2 6 2 2 6 2 2 6
  94535. - 2 2 6 2 2 6 2 2 6 2 2 6
  94536. - 2 2 6 6 6 6 80 54 7 167 114 7
  94537. -213 154 11 236 178 12 246 190 14 246 190 14
  94538. -246 190 14 246 190 14 246 190 14 246 190 14
  94539. -246 190 14 242 186 14 239 182 13 239 182 13
  94540. -230 174 11 210 150 10 174 135 50 124 112 88
  94541. - 82 82 82 54 54 54 34 34 34 18 18 18
  94542. - 6 6 6 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 6 6 6 18 18 18
  94546. - 50 50 50 158 118 36 192 133 9 200 144 11
  94547. -216 158 10 219 162 10 224 166 10 226 170 11
  94548. -230 174 11 236 178 12 239 182 13 239 182 13
  94549. -242 186 14 246 186 14 246 190 14 246 190 14
  94550. -246 190 14 246 190 14 246 190 14 246 190 14
  94551. -246 186 14 230 174 11 210 150 10 163 110 8
  94552. -104 69 6 10 10 10 2 2 6 2 2 6
  94553. - 2 2 6 2 2 6 2 2 6 2 2 6
  94554. - 2 2 6 2 2 6 2 2 6 2 2 6
  94555. - 2 2 6 2 2 6 2 2 6 2 2 6
  94556. - 2 2 6 6 6 6 91 60 6 167 114 7
  94557. -206 145 10 230 174 11 242 186 14 246 190 14
  94558. -246 190 14 246 190 14 246 186 14 242 186 14
  94559. -239 182 13 230 174 11 224 166 10 213 154 11
  94560. -180 133 36 124 112 88 86 86 86 58 58 58
  94561. - 38 38 38 22 22 22 10 10 10 6 6 6
  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 14 14 14
  94566. - 34 34 34 70 70 70 138 110 50 158 118 36
  94567. -167 114 7 180 123 7 192 133 9 197 138 11
  94568. -200 144 11 206 145 10 213 154 11 219 162 10
  94569. -224 166 10 230 174 11 239 182 13 242 186 14
  94570. -246 186 14 246 186 14 246 186 14 246 186 14
  94571. -239 182 13 216 158 10 185 133 11 152 99 6
  94572. -104 69 6 18 14 6 2 2 6 2 2 6
  94573. - 2 2 6 2 2 6 2 2 6 2 2 6
  94574. - 2 2 6 2 2 6 2 2 6 2 2 6
  94575. - 2 2 6 2 2 6 2 2 6 2 2 6
  94576. - 2 2 6 6 6 6 80 54 7 152 99 6
  94577. -192 133 9 219 162 10 236 178 12 239 182 13
  94578. -246 186 14 242 186 14 239 182 13 236 178 12
  94579. -224 166 10 206 145 10 192 133 9 154 121 60
  94580. - 94 94 94 62 62 62 42 42 42 22 22 22
  94581. - 14 14 14 6 6 6 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 6 6 6
  94586. - 18 18 18 34 34 34 58 58 58 78 78 78
  94587. -101 98 89 124 112 88 142 110 46 156 107 11
  94588. -163 110 8 167 114 7 175 118 6 180 123 7
  94589. -185 133 11 197 138 11 210 150 10 219 162 10
  94590. -226 170 11 236 178 12 236 178 12 234 174 13
  94591. -219 162 10 197 138 11 163 110 8 130 83 6
  94592. - 91 60 6 10 10 10 2 2 6 2 2 6
  94593. - 18 18 18 38 38 38 38 38 38 38 38 38
  94594. - 38 38 38 38 38 38 38 38 38 38 38 38
  94595. - 38 38 38 38 38 38 26 26 26 2 2 6
  94596. - 2 2 6 6 6 6 70 47 6 137 92 6
  94597. -175 118 6 200 144 11 219 162 10 230 174 11
  94598. -234 174 13 230 174 11 219 162 10 210 150 10
  94599. -192 133 9 163 110 8 124 112 88 82 82 82
  94600. - 50 50 50 30 30 30 14 14 14 6 6 6
  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. - 6 6 6 14 14 14 22 22 22 34 34 34
  94607. - 42 42 42 58 58 58 74 74 74 86 86 86
  94608. -101 98 89 122 102 70 130 98 46 121 87 25
  94609. -137 92 6 152 99 6 163 110 8 180 123 7
  94610. -185 133 11 197 138 11 206 145 10 200 144 11
  94611. -180 123 7 156 107 11 130 83 6 104 69 6
  94612. - 50 34 6 54 54 54 110 110 110 101 98 89
  94613. - 86 86 86 82 82 82 78 78 78 78 78 78
  94614. - 78 78 78 78 78 78 78 78 78 78 78 78
  94615. - 78 78 78 82 82 82 86 86 86 94 94 94
  94616. -106 106 106 101 101 101 86 66 34 124 80 6
  94617. -156 107 11 180 123 7 192 133 9 200 144 11
  94618. -206 145 10 200 144 11 192 133 9 175 118 6
  94619. -139 102 15 109 106 95 70 70 70 42 42 42
  94620. - 22 22 22 10 10 10 0 0 0 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 6 6 6 10 10 10
  94627. - 14 14 14 22 22 22 30 30 30 38 38 38
  94628. - 50 50 50 62 62 62 74 74 74 90 90 90
  94629. -101 98 89 112 100 78 121 87 25 124 80 6
  94630. -137 92 6 152 99 6 152 99 6 152 99 6
  94631. -138 86 6 124 80 6 98 70 6 86 66 30
  94632. -101 98 89 82 82 82 58 58 58 46 46 46
  94633. - 38 38 38 34 34 34 34 34 34 34 34 34
  94634. - 34 34 34 34 34 34 34 34 34 34 34 34
  94635. - 34 34 34 34 34 34 38 38 38 42 42 42
  94636. - 54 54 54 82 82 82 94 86 76 91 60 6
  94637. -134 86 6 156 107 11 167 114 7 175 118 6
  94638. -175 118 6 167 114 7 152 99 6 121 87 25
  94639. -101 98 89 62 62 62 34 34 34 18 18 18
  94640. - 6 6 6 0 0 0 0 0 0 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 6 6 6 6 6 6 10 10 10
  94648. - 18 18 18 22 22 22 30 30 30 42 42 42
  94649. - 50 50 50 66 66 66 86 86 86 101 98 89
  94650. -106 86 58 98 70 6 104 69 6 104 69 6
  94651. -104 69 6 91 60 6 82 62 34 90 90 90
  94652. - 62 62 62 38 38 38 22 22 22 14 14 14
  94653. - 10 10 10 10 10 10 10 10 10 10 10 10
  94654. - 10 10 10 10 10 10 6 6 6 10 10 10
  94655. - 10 10 10 10 10 10 10 10 10 14 14 14
  94656. - 22 22 22 42 42 42 70 70 70 89 81 66
  94657. - 80 54 7 104 69 6 124 80 6 137 92 6
  94658. -134 86 6 116 81 8 100 82 52 86 86 86
  94659. - 58 58 58 30 30 30 14 14 14 6 6 6
  94660. - 0 0 0 0 0 0 0 0 0 0 0 0
  94661. - 0 0 0 0 0 0 0 0 0 0 0 0
  94662. - 0 0 0 0 0 0 0 0 0 0 0 0
  94663. - 0 0 0 0 0 0 0 0 0 0 0 0
  94664. - 0 0 0 0 0 0 0 0 0 0 0 0
  94665. - 0 0 0 0 0 0 0 0 0 0 0 0
  94666. - 0 0 0 0 0 0 0 0 0 0 0 0
  94667. - 0 0 0 0 0 0 0 0 0 0 0 0
  94668. - 0 0 0 6 6 6 10 10 10 14 14 14
  94669. - 18 18 18 26 26 26 38 38 38 54 54 54
  94670. - 70 70 70 86 86 86 94 86 76 89 81 66
  94671. - 89 81 66 86 86 86 74 74 74 50 50 50
  94672. - 30 30 30 14 14 14 6 6 6 0 0 0
  94673. - 0 0 0 0 0 0 0 0 0 0 0 0
  94674. - 0 0 0 0 0 0 0 0 0 0 0 0
  94675. - 0 0 0 0 0 0 0 0 0 0 0 0
  94676. - 6 6 6 18 18 18 34 34 34 58 58 58
  94677. - 82 82 82 89 81 66 89 81 66 89 81 66
  94678. - 94 86 66 94 86 76 74 74 74 50 50 50
  94679. - 26 26 26 14 14 14 6 6 6 0 0 0
  94680. - 0 0 0 0 0 0 0 0 0 0 0 0
  94681. - 0 0 0 0 0 0 0 0 0 0 0 0
  94682. - 0 0 0 0 0 0 0 0 0 0 0 0
  94683. - 0 0 0 0 0 0 0 0 0 0 0 0
  94684. - 0 0 0 0 0 0 0 0 0 0 0 0
  94685. - 0 0 0 0 0 0 0 0 0 0 0 0
  94686. - 0 0 0 0 0 0 0 0 0 0 0 0
  94687. - 0 0 0 0 0 0 0 0 0 0 0 0
  94688. - 0 0 0 0 0 0 0 0 0 0 0 0
  94689. - 6 6 6 6 6 6 14 14 14 18 18 18
  94690. - 30 30 30 38 38 38 46 46 46 54 54 54
  94691. - 50 50 50 42 42 42 30 30 30 18 18 18
  94692. - 10 10 10 0 0 0 0 0 0 0 0 0
  94693. - 0 0 0 0 0 0 0 0 0 0 0 0
  94694. - 0 0 0 0 0 0 0 0 0 0 0 0
  94695. - 0 0 0 0 0 0 0 0 0 0 0 0
  94696. - 0 0 0 6 6 6 14 14 14 26 26 26
  94697. - 38 38 38 50 50 50 58 58 58 58 58 58
  94698. - 54 54 54 42 42 42 30 30 30 18 18 18
  94699. - 10 10 10 0 0 0 0 0 0 0 0 0
  94700. - 0 0 0 0 0 0 0 0 0 0 0 0
  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. - 0 0 0 0 0 0 0 0 0 0 0 0
  94709. - 0 0 0 0 0 0 0 0 0 6 6 6
  94710. - 6 6 6 10 10 10 14 14 14 18 18 18
  94711. - 18 18 18 14 14 14 10 10 10 6 6 6
  94712. - 0 0 0 0 0 0 0 0 0 0 0 0
  94713. - 0 0 0 0 0 0 0 0 0 0 0 0
  94714. - 0 0 0 0 0 0 0 0 0 0 0 0
  94715. - 0 0 0 0 0 0 0 0 0 0 0 0
  94716. - 0 0 0 0 0 0 0 0 0 6 6 6
  94717. - 14 14 14 18 18 18 22 22 22 22 22 22
  94718. - 18 18 18 14 14 14 10 10 10 6 6 6
  94719. - 0 0 0 0 0 0 0 0 0 0 0 0
  94720. - 0 0 0 0 0 0 0 0 0 0 0 0
  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 0 0 0 0 0 0
  94725. +0 0 0 0 0 0 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 0 0 0 0 0 0
  94727. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94728. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94729. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94730. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94731. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94732. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94733. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94734. +0 0 0 0 0 0 0 0 0
  94735. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94736. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94737. +0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0
  94738. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94739. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94740. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94741. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94742. +0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
  94743. +0 1 0 0 0 0 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 0 0 0 0 0 0
  94745. +0 0 0 0 0 0 0 0 0
  94746. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94747. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  94748. +10 15 3 2 3 1 12 18 4 42 61 14 19 27 6 11 16 4
  94749. +38 55 13 10 15 3 3 4 1 10 15 3 0 0 0 0 0 0
  94750. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94751. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94752. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 1
  94753. +12 18 4 1 1 0 23 34 8 31 45 11 10 15 3 32 47 11
  94754. +34 49 12 3 4 1 3 4 1 3 4 1 0 0 0 0 0 0
  94755. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94756. +0 0 0 0 0 0 0 0 0
  94757. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94758. +0 0 0 0 0 0 10 15 3 29 42 10 26 37 9 12 18 4
  94759. +55 80 19 81 118 28 55 80 19 92 132 31 106 153 36 69 100 23
  94760. +100 144 34 80 116 27 42 61 14 81 118 28 23 34 8 27 40 9
  94761. +15 21 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94762. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94763. +0 0 0 0 0 0 1 1 0 29 42 10 15 21 5 50 72 17
  94764. +74 107 25 45 64 15 102 148 35 80 116 27 84 121 28 111 160 38
  94765. +69 100 23 65 94 22 81 118 28 29 42 10 17 25 6 29 42 10
  94766. +23 34 8 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  94767. +0 0 0 0 0 0 0 0 0
  94768. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  94769. +15 21 5 15 21 5 34 49 12 101 146 34 111 161 38 97 141 33
  94770. +97 141 33 119 172 41 117 170 40 116 167 40 118 170 40 118 171 40
  94771. +117 169 40 118 170 40 111 160 38 118 170 40 96 138 32 89 128 30
  94772. +81 118 28 11 16 4 10 15 3 1 1 0 0 0 0 0 0 0
  94773. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94774. +3 4 1 3 4 1 34 49 12 101 146 34 79 115 27 111 160 38
  94775. +114 165 39 113 163 39 118 170 40 117 169 40 118 171 40 117 169 40
  94776. +116 167 40 119 172 41 113 163 39 92 132 31 105 151 36 113 163 39
  94777. +75 109 26 19 27 6 16 23 5 11 16 4 0 1 0 0 0 0
  94778. +0 0 0 0 0 0 0 0 0
  94779. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  94780. +80 116 27 106 153 36 105 151 36 114 165 39 118 170 40 118 171 40
  94781. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94782. +117 169 40 117 169 40 117 170 40 117 169 40 118 170 40 118 170 40
  94783. +117 170 40 75 109 26 75 109 26 34 49 12 0 0 0 0 0 0
  94784. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  94785. +64 92 22 65 94 22 100 144 34 118 171 40 118 170 40 117 169 40
  94786. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94787. +117 169 40 117 169 40 117 169 40 118 171 41 118 170 40 117 169 40
  94788. +109 158 37 105 151 36 104 150 35 47 69 16 0 0 0 0 0 0
  94789. +0 0 0 0 0 0 0 0 0
  94790. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94791. +42 61 14 115 167 39 118 170 40 117 169 40 117 169 40 117 169 40
  94792. +117 170 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  94793. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94794. +117 169 40 117 169 40 118 170 40 96 138 32 17 25 6 0 0 0
  94795. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47 69 16
  94796. +114 165 39 117 168 40 117 170 40 117 169 40 117 169 40 117 169 40
  94797. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94798. +117 169 40 117 169 40 118 170 40 117 169 40 117 169 40 117 169 40
  94799. +117 170 40 119 172 41 96 138 32 12 18 4 0 0 0 0 0 0
  94800. +0 0 0 0 0 0 0 0 0
  94801. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  94802. +32 47 11 105 151 36 118 170 40 117 169 40 117 169 40 116 168 40
  94803. +109 157 37 111 160 38 117 169 40 118 171 40 117 169 40 117 169 40
  94804. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94805. +117 169 40 117 169 40 117 169 40 118 171 40 69 100 23 2 3 1
  94806. +0 0 0 0 0 0 0 0 0 0 0 0 19 27 6 101 146 34
  94807. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94808. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 170 40
  94809. +118 171 40 115 166 39 107 154 36 111 161 38 117 169 40 117 169 40
  94810. +117 169 40 118 171 40 75 109 26 19 27 6 2 3 1 0 0 0
  94811. +0 0 0 0 0 0 0 0 0
  94812. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 23 5
  94813. +89 128 30 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94814. +111 160 38 92 132 31 79 115 27 96 138 32 115 166 39 119 171 41
  94815. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94816. +117 169 40 117 169 40 117 169 40 118 170 40 109 157 37 26 37 9
  94817. +0 0 0 0 0 0 0 0 0 0 0 0 64 92 22 118 171 40
  94818. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94819. +117 169 40 117 169 40 117 169 40 118 170 40 118 171 40 109 157 37
  94820. +89 128 30 81 118 28 100 144 34 115 166 39 117 169 40 117 169 40
  94821. +117 169 40 117 170 40 113 163 39 60 86 20 1 1 0 0 0 0
  94822. +0 0 0 0 0 0 0 0 0
  94823. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94824. +27 40 9 96 138 32 118 170 40 117 169 40 117 169 40 117 169 40
  94825. +117 170 40 117 169 40 101 146 34 67 96 23 55 80 19 84 121 28
  94826. +113 163 39 119 171 41 117 169 40 117 169 40 117 169 40 117 169 40
  94827. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 65 94 22
  94828. +0 0 0 0 0 0 0 0 0 15 21 5 101 146 34 118 171 40
  94829. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94830. +117 169 40 118 170 40 118 171 40 104 150 35 69 100 23 53 76 18
  94831. +81 118 28 111 160 38 118 170 40 117 169 40 117 169 40 117 169 40
  94832. +117 169 40 114 165 39 69 100 23 10 15 3 0 0 0 0 0 0
  94833. +0 0 0 0 0 0 0 0 0
  94834. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  94835. +31 45 11 77 111 26 117 169 40 117 169 40 117 169 40 117 169 40
  94836. +117 169 40 117 169 40 118 170 40 116 168 40 92 132 31 47 69 16
  94837. +38 55 13 81 118 28 113 163 39 119 171 41 117 169 40 117 169 40
  94838. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 41 92 132 31
  94839. +10 15 3 0 0 0 0 0 0 36 52 12 115 166 39 117 169 40
  94840. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  94841. +118 171 40 102 148 35 64 92 22 34 49 12 65 94 22 106 153 36
  94842. +118 171 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  94843. +118 170 40 107 154 36 55 80 19 15 21 5 0 0 0 0 0 0
  94844. +0 0 0 0 0 0 0 0 0
  94845. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94846. +29 42 10 101 146 34 118 171 40 117 169 40 117 169 40 117 169 40
  94847. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 113 163 39
  94848. +75 109 26 27 40 9 36 52 12 89 128 30 116 167 40 118 171 40
  94849. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 104 150 35
  94850. +16 23 5 0 0 0 0 0 0 53 76 18 118 171 40 117 169 40
  94851. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 109 157 37
  94852. +67 96 23 23 34 8 42 61 14 96 138 32 118 170 40 118 170 40
  94853. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94854. +117 169 40 117 169 40 74 107 25 10 15 3 0 0 0 0 0 0
  94855. +0 0 0 0 0 0 0 0 0
  94856. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94857. +0 0 0 31 45 11 101 146 34 118 170 40 117 169 40 117 169 40
  94858. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94859. +119 171 41 102 148 35 47 69 16 14 20 5 50 72 17 102 148 35
  94860. +118 171 40 117 169 40 117 169 40 117 169 40 118 170 40 102 148 35
  94861. +15 21 5 0 0 0 0 0 0 50 72 17 118 170 40 117 169 40
  94862. +117 169 40 117 169 40 118 170 40 116 167 40 84 121 28 27 40 9
  94863. +19 27 6 74 107 25 114 165 39 118 171 40 117 169 40 117 169 40
  94864. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94865. +117 169 40 75 109 26 10 15 4 0 0 0 0 0 0 0 0 0
  94866. +0 0 0 0 0 0 0 0 0
  94867. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94868. +0 0 0 38 55 13 102 148 35 118 171 40 117 169 40 117 169 40
  94869. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94870. +117 169 40 118 170 40 115 167 39 77 111 26 17 25 6 19 27 6
  94871. +77 111 26 115 166 39 118 170 40 117 169 40 119 172 41 81 118 28
  94872. +3 4 1 0 0 0 0 0 0 27 40 9 111 160 38 118 170 40
  94873. +117 169 40 118 171 40 105 151 36 50 72 17 10 15 3 38 55 13
  94874. +100 144 34 118 171 40 117 169 40 117 169 40 117 169 40 117 169 40
  94875. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94876. +117 169 40 79 115 27 15 21 5 0 0 0 0 0 0 0 0 0
  94877. +0 0 0 0 0 0 0 0 0
  94878. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94879. +0 0 0 10 15 3 64 92 22 111 160 38 117 169 40 117 169 40
  94880. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94881. +117 169 40 117 169 40 117 169 40 118 171 40 96 138 32 32 47 11
  94882. +3 4 1 50 72 17 107 154 36 120 173 41 105 151 36 31 45 11
  94883. +0 0 0 0 0 0 0 0 0 3 4 1 65 94 22 117 169 40
  94884. +118 170 40 89 128 30 26 37 9 3 4 1 60 86 20 111 161 38
  94885. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94886. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94887. +97 141 33 36 52 12 1 1 0 0 0 0 0 0 0 0 0 0
  94888. +0 0 0 0 0 0 0 0 0
  94889. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94890. +0 0 0 0 0 0 14 20 5 75 109 26 117 168 40 117 169 40
  94891. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94892. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 107 154 36
  94893. +45 64 15 2 3 1 31 45 11 75 109 26 32 47 11 0 1 0
  94894. +0 0 0 0 0 0 0 0 0 0 0 0 10 15 3 55 80 19
  94895. +65 94 22 11 16 4 11 16 4 75 109 26 116 168 40 118 170 40
  94896. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94897. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 107 154 36
  94898. +47 69 16 3 4 1 0 0 0 0 0 0 0 0 0 0 0 0
  94899. +0 0 0 0 0 0 0 0 0
  94900. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94901. +0 0 0 0 0 0 12 18 4 69 100 23 111 161 38 118 171 40
  94902. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94903. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  94904. +111 160 38 50 72 17 2 3 1 2 3 1 0 0 0 0 0 0
  94905. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  94906. +1 1 0 12 18 4 81 118 28 118 170 40 117 169 40 117 169 40
  94907. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94908. +117 169 40 117 169 40 117 169 40 117 170 40 118 171 40 101 146 34
  94909. +42 61 14 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  94910. +0 0 0 0 0 0 0 0 0
  94911. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94912. +0 0 0 0 0 0 0 0 0 3 4 1 36 52 12 89 128 30
  94913. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94914. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94915. +118 171 41 101 146 34 14 20 5 0 0 0 0 0 0 0 0 0
  94916. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94917. +0 0 0 47 69 16 118 170 40 117 169 40 117 169 40 117 169 40
  94918. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94919. +117 169 40 117 169 40 117 170 40 111 160 38 69 100 23 19 27 6
  94920. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94921. +0 0 0 0 0 0 0 0 0
  94922. +0 0 0 0 0 0 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 11 16 4 69 100 23
  94924. +115 167 39 119 172 41 117 169 40 117 169 40 117 169 40 117 169 40
  94925. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94926. +119 172 41 75 109 26 3 4 1 0 0 0 0 0 0 0 0 0
  94927. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94928. +0 0 0 23 34 8 106 153 36 118 170 40 117 169 40 117 169 40
  94929. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94930. +117 169 40 118 170 40 119 172 41 105 151 36 42 61 14 2 3 1
  94931. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94932. +0 0 0 0 0 0 0 0 0
  94933. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94934. +0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 15 21 5
  94935. +45 64 15 80 116 27 114 165 39 118 170 40 117 169 40 117 169 40
  94936. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 119 172 41
  94937. +97 141 33 20 30 7 0 0 0 0 0 0 0 0 0 0 0 0
  94938. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94939. +0 0 0 1 1 0 53 76 18 114 165 39 118 171 40 117 169 40
  94940. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  94941. +118 171 40 104 150 35 64 92 22 31 45 11 10 15 3 0 0 0
  94942. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94943. +0 0 0 0 0 0 0 0 0
  94944. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94945. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94946. +0 0 0 36 52 12 97 141 33 109 158 37 113 163 39 116 168 40
  94947. +117 169 40 117 170 40 118 170 40 119 172 41 115 167 39 84 121 28
  94948. +23 34 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94949. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94950. +0 0 0 0 0 0 3 4 1 50 72 17 102 148 35 118 171 40
  94951. +119 171 41 118 170 40 117 169 40 117 169 40 115 166 39 111 161 38
  94952. +109 157 37 79 115 27 12 18 4 0 0 0 0 0 0 0 0 0
  94953. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94954. +0 0 0 0 0 0 0 0 0
  94955. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94956. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94957. +0 0 0 3 4 1 15 21 5 23 34 8 45 64 15 106 153 36
  94958. +116 167 40 111 160 38 101 146 34 79 115 27 42 61 14 10 15 3
  94959. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94960. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94961. +0 0 0 0 0 0 0 0 0 1 1 0 20 30 7 60 86 20
  94962. +89 128 30 106 153 36 113 163 39 117 169 40 84 121 28 29 42 10
  94963. +19 27 6 10 15 3 2 3 1 0 0 0 0 0 0 0 0 0
  94964. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94965. +0 0 0 0 0 0 0 0 0
  94966. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94967. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94968. +0 0 0 0 0 0 0 0 0 0 0 0 16 23 5 38 55 13
  94969. +36 52 12 26 37 9 12 18 4 2 3 1 0 0 0 0 0 0
  94970. +0 0 0 0 0 0 0 0 0 1 0 0 19 2 7 52 5 18
  94971. +78 7 27 88 8 31 81 7 29 56 5 19 25 2 9 3 0 1
  94972. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94973. +3 4 1 19 27 6 31 45 11 38 55 13 32 47 11 3 4 1
  94974. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94975. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94976. +0 0 0 0 0 0 0 0 0
  94977. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94978. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94979. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  94980. +9 0 3 12 1 4 9 0 3 4 0 1 0 0 0 0 0 0
  94981. +0 0 0 0 0 0 28 3 10 99 9 35 156 14 55 182 16 64
  94982. +189 17 66 190 17 67 189 17 66 184 17 65 166 15 58 118 13 41
  94983. +45 4 16 3 0 1 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 0 0 0 0 0 0
  94985. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94986. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94987. +0 0 0 0 0 0 0 0 0
  94988. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94989. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94990. +0 0 0 0 0 0 11 1 4 52 5 18 101 9 35 134 12 47
  94991. +151 14 53 154 14 54 151 14 53 113 10 40 11 1 4 0 0 0
  94992. +3 0 1 67 6 24 159 14 56 190 17 67 190 17 67 188 17 66
  94993. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 191 17 67
  94994. +174 16 61 101 9 35 14 1 5 0 0 0 35 3 12 108 10 38
  94995. +122 11 43 122 11 43 112 10 39 87 8 30 50 5 17 13 1 5
  94996. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94997. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  94998. +0 0 0 0 0 0 0 0 0
  94999. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95000. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95001. +3 0 1 56 5 19 141 13 49 182 16 64 191 17 67 191 17 67
  95002. +190 17 67 190 17 67 191 17 67 113 10 40 3 0 1 1 0 0
  95003. +79 7 28 180 16 63 190 17 67 188 17 66 188 17 66 188 17 66
  95004. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95005. +189 17 66 188 17 66 122 11 43 11 1 4 41 4 14 176 16 62
  95006. +191 17 67 191 17 67 191 17 67 190 17 67 181 16 63 146 13 51
  95007. +75 7 26 10 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  95008. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95009. +0 0 0 0 0 0 0 0 0
  95010. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95011. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 1 2
  95012. +90 8 32 178 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  95013. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 41 4 14
  95014. +173 16 61 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95015. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95016. +188 17 66 188 17 66 188 17 66 88 8 31 1 0 0 89 8 31
  95017. +185 17 65 189 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  95018. +186 17 65 124 11 43 25 2 9 0 0 0 0 0 0 0 0 0
  95019. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95020. +0 0 0 0 0 0 0 0 0
  95021. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95022. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 89 8 31
  95023. +184 17 65 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95024. +190 17 67 151 14 53 34 3 12 0 0 0 0 0 0 79 7 28
  95025. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95026. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95027. +188 17 66 188 17 66 191 17 67 146 13 51 9 1 3 7 1 2
  95028. +108 10 38 187 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  95029. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 0 0 0
  95030. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95031. +0 0 0 0 0 0 0 0 0
  95032. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95033. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 176 16 62
  95034. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95035. +151 14 53 38 3 13 0 0 0 0 0 0 0 0 0 50 5 17
  95036. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95037. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95038. +188 17 66 188 17 66 191 17 67 141 13 49 7 1 3 0 0 0
  95039. +11 1 4 112 10 39 187 17 66 189 17 66 188 17 66 188 17 66
  95040. +188 17 66 188 17 66 190 17 67 113 10 40 5 0 2 0 0 0
  95041. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95042. +0 0 0 0 0 0 0 0 0
  95043. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95044. +0 0 0 0 0 0 0 0 0 7 1 3 132 12 46 191 17 67
  95045. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 146 13 51
  95046. +35 3 12 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  95047. +101 9 35 185 17 65 190 17 67 188 17 66 188 17 66 188 17 66
  95048. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95049. +188 17 66 190 17 67 180 16 63 67 6 24 0 0 0 0 0 0
  95050. +0 0 0 11 1 4 108 10 38 186 17 65 189 17 66 188 17 66
  95051. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  95052. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95053. +0 0 0 0 0 0 0 0 0
  95054. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95055. +0 0 0 0 0 0 0 0 0 44 4 15 177 16 62 189 17 66
  95056. +188 17 66 188 17 66 189 17 66 189 17 66 134 12 47 28 3 10
  95057. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95058. +8 1 3 79 7 28 159 14 56 188 17 66 191 17 67 190 17 67
  95059. +189 17 66 189 17 66 189 17 66 189 17 66 190 17 67 191 17 67
  95060. +188 17 66 158 14 55 72 7 25 4 0 1 0 0 0 0 0 0
  95061. +0 0 0 0 0 0 8 1 3 95 9 33 182 16 64 189 17 67
  95062. +188 17 66 188 17 66 188 17 66 191 17 67 122 11 43 3 0 1
  95063. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95064. +0 0 0 0 0 0 0 0 0
  95065. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95066. +0 0 0 0 0 0 0 0 0 88 8 31 190 17 67 188 17 66
  95067. +188 17 66 189 17 66 185 17 65 113 10 40 18 2 6 0 0 0
  95068. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95069. +0 0 0 1 0 0 24 2 8 77 7 27 124 11 43 154 14 54
  95070. +168 15 59 173 16 61 173 16 61 168 15 59 154 14 54 124 11 43
  95071. +77 7 27 22 2 8 0 0 0 0 0 0 0 0 0 0 0 0
  95072. +0 0 0 0 0 0 0 0 0 5 0 2 77 7 27 173 16 61
  95073. +190 17 67 188 17 66 188 17 66 190 17 67 164 15 57 23 2 8
  95074. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95075. +0 0 0 0 0 0 0 0 0
  95076. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95077. +0 0 0 0 0 0 1 0 0 118 13 41 191 17 67 188 17 66
  95078. +190 17 67 174 16 61 87 8 30 8 1 3 0 0 0 0 0 0
  95079. +0 0 0 0 0 0 10 1 4 29 3 10 40 4 14 36 3 13
  95080. +18 2 6 2 0 1 0 0 0 0 0 0 3 0 1 14 1 5
  95081. +26 2 9 33 3 11 32 3 11 25 2 9 13 1 5 3 0 1
  95082. +0 0 0 14 1 5 56 5 19 95 9 33 109 10 38 101 9 35
  95083. +77 7 27 35 3 12 5 0 2 0 0 0 1 0 0 56 5 19
  95084. +156 14 55 190 17 67 188 17 66 188 17 66 182 16 64 50 5 17
  95085. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95086. +0 0 0 0 0 0 0 0 0
  95087. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95088. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 189 17 66
  95089. +151 14 53 52 5 18 2 0 1 0 0 0 0 0 0 1 0 0
  95090. +28 3 10 90 8 32 146 13 51 170 15 60 178 16 62 174 16 61
  95091. +158 14 55 112 10 39 40 4 14 1 0 0 0 0 0 0 0 0
  95092. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  95093. +56 5 19 146 13 51 183 17 64 191 17 67 191 17 67 191 17 67
  95094. +188 17 66 173 16 61 122 11 43 41 4 14 1 0 0 0 0 0
  95095. +30 3 10 124 11 43 185 17 65 190 17 67 187 17 66 67 6 24
  95096. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95097. +0 0 0 0 0 0 0 0 0
  95098. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95099. +0 0 0 0 0 0 6 1 2 134 12 47 168 15 59 99 9 35
  95100. +21 2 7 0 0 0 0 0 0 0 0 0 6 1 2 77 7 27
  95101. +162 15 57 190 17 67 191 17 67 189 17 66 189 17 66 189 17 66
  95102. +190 17 67 191 17 67 169 15 59 75 7 26 3 0 1 0 0 0
  95103. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 79 7 28
  95104. +178 16 62 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95105. +188 17 66 189 17 66 191 17 67 170 15 60 79 7 28 5 0 2
  95106. +0 0 0 10 1 3 78 7 27 159 14 56 188 17 66 75 7 26
  95107. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95108. +0 0 0 0 0 0 0 0 0
  95109. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95110. +0 0 0 0 0 0 1 0 0 35 3 12 29 3 10 2 0 1
  95111. +0 0 0 0 0 0 0 0 0 9 1 3 101 9 35 183 17 64
  95112. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95113. +188 17 66 188 17 66 190 17 67 178 16 63 67 6 23 0 0 0
  95114. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 174 16 61
  95115. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95116. +188 17 66 188 17 66 188 17 66 190 17 67 182 16 64 89 8 31
  95117. +4 0 1 0 0 0 0 0 0 25 2 9 73 7 26 31 3 11
  95118. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95119. +0 0 0 0 0 0 0 0 0
  95120. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95121. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95122. +0 0 0 0 0 0 4 0 1 98 9 34 187 17 66 189 17 66
  95123. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95124. +188 17 66 188 17 66 188 17 66 190 17 67 158 14 55 25 2 9
  95125. +0 0 0 0 0 0 0 0 0 8 1 3 134 12 47 191 17 67
  95126. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95127. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 180 16 63
  95128. +68 6 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95129. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95130. +0 0 0 0 0 0 0 0 0
  95131. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95132. +0 0 0 6 1 2 19 2 7 3 0 1 0 0 0 0 0 0
  95133. +0 0 0 0 0 0 65 6 23 180 16 63 189 17 66 188 17 66
  95134. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95135. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 83 8 29
  95136. +0 0 0 0 0 0 0 0 0 41 4 14 177 16 62 189 17 66
  95137. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95138. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95139. +159 14 56 28 3 10 0 0 0 0 0 0 0 0 0 23 2 8
  95140. +41 4 14 5 0 2 0 0 0 0 0 0 0 0 0 0 0 0
  95141. +0 0 0 0 0 0 0 0 0
  95142. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95143. +23 2 8 113 10 40 159 14 56 65 6 23 0 0 0 0 0 0
  95144. +0 0 0 16 1 6 146 13 51 191 17 67 188 17 66 188 17 66
  95145. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95146. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 132 12 46
  95147. +5 0 2 0 0 0 0 0 0 77 7 27 189 17 66 188 17 66
  95148. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95149. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95150. +190 17 67 98 9 34 0 0 0 0 0 0 12 1 4 134 12 47
  95151. +178 16 63 108 10 38 16 1 6 0 0 0 0 0 0 0 0 0
  95152. +0 0 0 0 0 0 0 0 0
  95153. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 3 10
  95154. +141 13 49 190 17 67 191 17 67 134 12 47 6 1 2 0 0 0
  95155. +0 0 0 68 6 24 186 17 65 188 17 66 188 17 66 188 17 66
  95156. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95157. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 156 14 55
  95158. +14 1 5 0 0 0 0 0 0 98 9 34 191 17 67 188 17 66
  95159. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95160. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95161. +190 17 67 156 14 55 19 2 7 0 0 0 47 4 16 181 16 63
  95162. +190 17 67 189 17 66 126 14 44 17 2 6 0 0 0 0 0 0
  95163. +0 0 0 0 0 0 0 0 0
  95164. +0 0 0 0 0 0 0 0 0 0 0 0 16 1 6 134 12 47
  95165. +191 17 67 188 17 66 190 17 67 162 15 57 19 2 7 0 0 0
  95166. +3 0 1 123 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  95167. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95168. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 163 15 57
  95169. +20 2 7 0 0 0 0 0 0 101 9 35 191 17 67 188 17 66
  95170. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95171. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95172. +188 17 66 182 16 64 52 5 18 0 0 0 73 7 26 188 17 66
  95173. +188 17 66 188 17 66 189 17 66 109 10 38 5 0 2 0 0 0
  95174. +0 0 0 0 0 0 0 0 0
  95175. +0 0 0 0 0 0 0 0 0 0 0 0 95 9 33 189 17 66
  95176. +188 17 66 188 17 66 189 17 66 171 15 60 29 3 10 0 0 0
  95177. +16 1 6 156 14 55 190 17 67 188 17 66 188 17 66 188 17 66
  95178. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95179. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 158 14 55
  95180. +17 2 6 0 0 0 0 0 0 85 8 30 190 17 67 188 17 66
  95181. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95182. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95183. +188 17 66 189 17 66 81 7 29 0 0 0 85 8 30 190 17 67
  95184. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  95185. +0 0 0 0 0 0 0 0 0
  95186. +0 0 0 0 0 0 0 0 0 25 2 9 162 15 57 190 17 67
  95187. +188 17 66 188 17 66 189 17 66 173 16 61 31 3 11 0 0 0
  95188. +30 3 10 171 15 60 189 17 66 188 17 66 188 17 66 188 17 66
  95189. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95190. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 141 13 49
  95191. +7 1 2 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  95192. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95193. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95194. +188 17 66 191 17 67 98 9 34 0 0 0 88 8 31 190 17 67
  95195. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 5 0 2
  95196. +0 0 0 0 0 0 0 0 0
  95197. +0 0 0 0 0 0 0 0 0 68 6 24 187 17 66 188 17 66
  95198. +188 17 66 188 17 66 189 17 66 170 15 60 28 3 10 0 0 0
  95199. +34 3 12 174 16 61 189 17 66 188 17 66 188 17 66 188 17 66
  95200. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95201. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 101 9 35
  95202. +0 0 0 0 0 0 0 0 0 21 2 7 159 14 56 190 17 67
  95203. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95204. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95205. +188 17 66 191 17 67 98 9 34 0 0 0 81 7 29 189 17 66
  95206. +188 17 66 188 17 66 188 17 66 189 17 66 168 15 59 28 3 10
  95207. +0 0 0 0 0 0 0 0 0
  95208. +0 0 0 0 0 0 0 0 0 109 10 38 191 17 67 188 17 66
  95209. +188 17 66 188 17 66 190 17 67 163 15 57 21 2 7 0 0 0
  95210. +26 2 9 168 15 59 189 17 66 188 17 66 188 17 66 188 17 66
  95211. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95212. +188 17 66 188 17 66 188 17 66 189 17 66 180 16 63 47 4 16
  95213. +0 0 0 0 0 0 0 0 0 0 0 0 108 10 38 190 17 67
  95214. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95215. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95216. +188 17 66 189 17 66 78 7 27 0 0 0 68 6 24 187 17 66
  95217. +188 17 66 188 17 66 188 17 66 188 17 66 183 17 64 56 5 19
  95218. +0 0 0 0 0 0 0 0 0
  95219. +0 0 0 0 0 0 3 0 1 131 12 46 191 17 67 188 17 66
  95220. +188 17 66 188 17 66 190 17 67 151 14 53 12 1 4 0 0 0
  95221. +11 1 4 146 13 51 190 17 67 188 17 66 188 17 66 188 17 66
  95222. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95223. +188 17 66 188 17 66 188 17 66 191 17 67 126 14 44 7 1 2
  95224. +0 0 0 0 0 0 0 0 0 0 0 0 32 3 11 164 15 58
  95225. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95226. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95227. +189 17 66 178 16 62 44 4 15 0 0 0 50 5 17 182 16 64
  95228. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  95229. +0 0 0 0 0 0 0 0 0
  95230. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 188 17 66
  95231. +188 17 66 188 17 66 191 17 67 131 12 46 3 0 1 0 0 0
  95232. +0 0 0 101 9 35 190 17 67 188 17 66 188 17 66 188 17 66
  95233. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95234. +188 17 66 188 17 66 190 17 67 170 15 60 44 4 15 0 0 0
  95235. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 77 7 27
  95236. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95237. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95238. +191 17 67 134 12 47 9 1 3 0 0 0 31 3 11 171 15 60
  95239. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  95240. +0 0 0 0 0 0 0 0 0
  95241. +0 0 0 0 0 0 2 0 1 124 11 43 191 17 67 188 17 66
  95242. +188 17 66 188 17 66 191 17 67 101 9 35 0 0 0 0 0 0
  95243. +0 0 0 35 3 12 168 15 59 190 17 67 188 17 66 188 17 66
  95244. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95245. +188 17 66 189 17 66 182 16 64 77 7 27 0 0 0 0 0 0
  95246. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 1 2
  95247. +99 9 35 185 17 65 189 17 66 188 17 66 188 17 66 188 17 66
  95248. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  95249. +177 16 62 56 5 19 0 0 0 0 0 0 13 1 5 151 14 53
  95250. +190 17 67 188 17 66 188 17 66 188 17 66 185 17 65 56 5 19
  95251. +0 0 0 0 0 0 0 0 0
  95252. +0 0 0 0 0 0 0 0 0 99 9 35 191 17 67 188 17 66
  95253. +188 17 66 188 17 66 186 17 65 65 6 23 0 0 0 0 0 0
  95254. +0 0 0 0 0 0 79 7 28 182 16 64 190 17 67 188 17 66
  95255. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95256. +191 17 67 177 16 62 83 8 29 4 0 1 0 0 0 0 0 0
  95257. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95258. +8 1 3 89 8 31 175 16 62 191 17 67 189 17 66 188 17 66
  95259. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 181 16 63
  95260. +85 8 30 3 0 1 0 0 0 0 0 0 1 0 0 118 13 41
  95261. +191 17 67 188 17 66 188 17 66 189 17 66 173 16 61 34 3 12
  95262. +0 0 0 0 0 0 0 0 0
  95263. +0 0 0 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  95264. +188 17 66 189 17 66 169 15 59 30 3 10 0 0 0 0 0 0
  95265. +0 0 0 0 0 0 5 0 2 83 8 29 173 16 61 191 17 67
  95266. +190 17 67 189 17 66 189 17 66 190 17 67 191 17 67 187 17 66
  95267. +151 14 53 56 5 19 3 0 1 0 0 0 16 1 6 50 5 17
  95268. +79 7 28 95 9 33 95 9 33 75 7 26 41 4 14 10 1 4
  95269. +0 0 0 2 0 1 50 5 17 132 12 46 178 16 62 190 17 67
  95270. +191 17 67 191 17 67 191 17 67 186 17 65 154 14 54 68 6 24
  95271. +4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  95272. +187 17 66 188 17 66 188 17 66 191 17 67 141 13 49 9 1 3
  95273. +0 0 0 0 0 0 0 0 0
  95274. +0 0 0 0 0 0 0 0 0 14 1 5 151 14 53 190 17 67
  95275. +188 17 66 191 17 67 131 12 46 5 0 2 0 0 0 0 0 0
  95276. +0 0 0 0 0 0 0 0 0 2 0 1 44 4 15 113 10 40
  95277. +156 14 55 173 16 61 174 16 61 164 15 58 134 12 47 77 7 27
  95278. +18 2 6 0 0 0 16 1 6 85 8 30 151 14 53 182 16 64
  95279. +189 17 66 191 17 67 190 17 67 188 17 66 177 16 62 141 13 49
  95280. +68 6 24 8 1 3 0 0 0 8 1 3 44 4 15 88 8 31
  95281. +113 10 40 122 11 43 108 10 38 67 6 24 20 2 7 0 0 0
  95282. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 3 10
  95283. +166 15 58 190 17 67 188 17 66 187 17 66 79 7 28 0 0 0
  95284. +0 0 0 0 0 0 0 0 0
  95285. +0 0 0 0 0 0 0 0 0 0 0 0 73 7 26 185 17 65
  95286. +189 17 66 184 17 65 65 6 23 0 0 0 0 0 0 0 0 0
  95287. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1
  95288. +17 2 6 32 3 11 34 3 12 22 2 8 6 1 2 0 0 0
  95289. +0 0 0 38 3 13 141 13 49 188 17 66 190 17 67 188 17 66
  95290. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  95291. +184 17 65 122 11 43 21 2 7 0 0 0 0 0 0 0 0 0
  95292. +0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95293. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  95294. +108 10 38 191 17 67 191 17 67 141 13 49 16 1 6 0 0 0
  95295. +0 0 0 0 0 0 0 0 0
  95296. +0 0 0 0 0 0 0 0 0 0 0 0 8 1 3 112 10 39
  95297. +186 17 65 124 11 43 10 1 4 0 0 0 0 0 0 0 0 0
  95298. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95299. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95300. +36 3 13 156 14 55 191 17 67 188 17 66 188 17 66 188 17 66
  95301. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95302. +189 17 66 190 17 67 134 12 47 18 2 6 0 0 0 0 0 0
  95303. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95304. +0 0 0 7 1 2 41 4 14 75 7 26 66 5 23 19 2 7
  95305. +26 2 9 144 13 50 154 14 54 40 4 14 0 0 0 0 0 0
  95306. +0 0 0 0 0 0 0 0 0
  95307. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  95308. +56 5 19 19 2 7 0 0 0 7 1 2 29 3 10 35 3 12
  95309. +19 2 7 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  95310. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  95311. +134 12 47 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95312. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95313. +188 17 66 188 17 66 189 17 67 108 10 38 3 0 1 0 0 0
  95314. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  95315. +40 4 14 124 11 43 177 16 62 188 17 66 187 17 66 144 13 50
  95316. +24 2 8 17 2 6 22 2 8 0 0 0 0 0 0 0 0 0
  95317. +0 0 0 0 0 0 0 0 0
  95318. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95319. +0 0 0 0 0 0 19 2 7 122 11 43 171 15 60 175 16 62
  95320. +159 14 56 112 10 39 40 4 14 2 0 1 0 0 0 0 0 0
  95321. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  95322. +186 17 65 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95323. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95324. +188 17 66 188 17 66 189 17 66 174 16 61 41 4 14 0 0 0
  95325. +0 0 0 0 0 0 0 0 0 0 0 0 3 0 1 72 7 25
  95326. +168 15 59 191 17 67 189 17 66 188 17 66 188 17 66 190 17 67
  95327. +95 9 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95328. +0 0 0 0 0 0 0 0 0
  95329. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95330. +0 0 0 0 0 0 95 9 33 191 17 67 189 17 66 189 17 66
  95331. +190 17 67 191 17 67 171 15 60 90 8 32 12 1 4 0 0 0
  95332. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 132 12 46
  95333. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95334. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95335. +188 17 66 188 17 66 188 17 66 190 17 67 98 9 34 0 0 0
  95336. +0 0 0 0 0 0 0 0 0 5 0 2 88 8 31 180 16 63
  95337. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 191 17 67
  95338. +146 13 51 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  95339. +0 0 0 0 0 0 0 0 0
  95340. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95341. +0 0 0 9 1 3 144 13 50 191 17 67 188 17 66 188 17 66
  95342. +188 17 66 188 17 66 189 17 66 187 17 66 123 11 43 20 2 7
  95343. +0 0 0 0 0 0 0 0 0 0 0 0 21 2 7 163 15 57
  95344. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95345. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95346. +188 17 66 188 17 66 188 17 66 191 17 67 134 12 47 5 0 2
  95347. +0 0 0 0 0 0 3 0 1 88 8 31 182 16 64 189 17 66
  95348. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  95349. +171 15 60 31 3 11 0 0 0 0 0 0 0 0 0 0 0 0
  95350. +0 0 0 0 0 0 0 0 0
  95351. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95352. +0 0 0 20 2 7 162 15 57 190 17 67 188 17 66 188 17 66
  95353. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 132 12 46
  95354. +20 2 7 0 0 0 0 0 0 0 0 0 32 3 11 173 16 61
  95355. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95356. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95357. +188 17 66 188 17 66 188 17 66 190 17 67 151 14 53 12 1 4
  95358. +0 0 0 0 0 0 72 7 25 180 16 63 189 17 66 188 17 66
  95359. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95360. +181 16 63 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  95361. +0 0 0 0 0 0 0 0 0
  95362. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95363. +0 0 0 21 2 7 163 15 57 190 17 67 188 17 66 188 17 66
  95364. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95365. +122 11 43 9 1 3 0 0 0 0 0 0 30 3 10 171 15 60
  95366. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95367. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95368. +188 17 66 188 17 66 188 17 66 190 17 67 146 13 51 10 1 4
  95369. +0 0 0 38 3 13 166 15 58 190 17 67 188 17 66 188 17 66
  95370. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95371. +183 17 64 52 5 18 0 0 0 0 0 0 0 0 0 0 0 0
  95372. +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 13 1 5 154 14 54 190 17 67 188 17 66 188 17 66
  95375. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95376. +186 17 65 79 7 28 0 0 0 0 0 0 14 1 5 156 14 54
  95377. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95378. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95379. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 2 0 1
  95380. +5 0 2 122 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  95381. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95382. +182 16 64 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  95383. +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 3 0 1 126 14 44 191 17 67 188 17 66 188 17 66
  95386. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95387. +190 17 67 158 14 55 23 2 8 0 0 0 1 0 0 113 10 40
  95388. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95389. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95390. +188 17 66 188 17 66 188 17 66 188 17 66 78 7 27 0 0 0
  95391. +47 4 16 177 16 62 189 17 66 188 17 66 188 17 66 188 17 66
  95392. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  95393. +173 16 61 34 3 12 0 0 0 0 0 0 0 0 0 0 0 0
  95394. +0 0 0 0 0 0 0 0 0
  95395. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95396. +0 0 0 0 0 0 85 8 30 189 17 66 188 17 66 188 17 66
  95397. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95398. +188 17 66 188 17 66 79 7 28 0 0 0 0 0 0 47 4 16
  95399. +175 16 62 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95400. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95401. +188 17 66 188 17 66 190 17 67 156 14 55 22 2 8 0 0 0
  95402. +109 10 38 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95403. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95404. +151 14 53 13 1 5 0 0 0 0 0 0 0 0 0 0 0 0
  95405. +0 0 0 0 0 0 0 0 0
  95406. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95407. +0 0 0 0 0 0 35 3 12 173 16 61 189 17 66 188 17 66
  95408. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95409. +188 17 66 191 17 67 134 12 47 7 1 2 0 0 0 3 0 1
  95410. +99 9 35 188 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  95411. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95412. +188 17 66 189 17 66 181 16 63 68 6 24 0 0 0 18 2 6
  95413. +156 14 55 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95414. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  95415. +101 9 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95416. +0 0 0 0 0 0 0 0 0
  95417. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95418. +0 0 0 0 0 0 3 0 1 118 13 41 191 17 67 188 17 66
  95419. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95420. +188 17 66 189 17 66 168 15 59 28 3 10 0 0 0 0 0 0
  95421. +12 1 4 113 10 40 187 17 66 189 17 67 188 17 66 188 17 66
  95422. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95423. +190 17 67 180 16 63 88 8 31 4 0 1 0 0 0 47 4 16
  95424. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95425. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 168 15 59
  95426. +36 3 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95427. +0 0 0 0 0 0 0 0 0
  95428. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95429. +0 0 0 0 0 0 0 0 0 38 3 13 164 15 58 190 17 67
  95430. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95431. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  95432. +0 0 0 11 1 4 90 8 32 169 15 59 190 17 67 190 17 67
  95433. +189 17 66 189 17 66 189 17 66 189 17 66 191 17 67 189 17 66
  95434. +158 14 55 68 6 24 4 0 1 0 0 0 0 0 0 73 7 26
  95435. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95436. +188 17 66 188 17 66 188 17 66 189 17 66 185 17 65 83 8 29
  95437. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95438. +0 0 0 0 0 0 0 0 0
  95439. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95440. +0 0 0 0 0 0 0 0 0 0 0 0 65 6 23 174 16 61
  95441. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95442. +188 17 66 188 17 66 185 17 65 56 5 19 0 0 0 0 0 0
  95443. +0 0 0 0 0 0 2 0 1 35 3 12 99 9 35 146 13 51
  95444. +170 15 60 177 16 62 177 16 62 166 15 58 141 13 49 85 8 30
  95445. +24 2 8 0 0 0 0 0 0 0 0 0 0 0 0 85 8 30
  95446. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95447. +188 17 66 188 17 66 188 17 66 189 17 66 112 10 39 8 1 3
  95448. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95449. +0 0 0 0 0 0 0 0 0
  95450. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95451. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 68 6 24
  95452. +170 15 60 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  95453. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  95454. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 11 1 4
  95455. +28 3 10 40 4 14 38 3 13 25 2 9 8 1 3 0 0 0
  95456. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 78 7 27
  95457. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95458. +188 17 66 189 17 66 187 17 66 113 10 40 14 1 5 0 0 0
  95459. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95460. +0 0 0 0 0 0 0 0 0
  95461. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95462. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  95463. +47 4 16 141 13 49 186 17 65 191 17 67 190 17 67 189 17 66
  95464. +189 17 66 191 17 67 156 14 55 20 2 7 0 0 0 0 0 0
  95465. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95466. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95467. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 4 15
  95468. +178 16 62 190 17 67 188 17 66 188 17 66 188 17 66 190 17 67
  95469. +191 17 67 173 16 61 90 8 32 10 1 4 0 0 0 0 0 0
  95470. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95471. +0 0 0 0 0 0 0 0 0
  95472. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95473. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95474. +0 0 0 14 1 5 68 6 24 131 12 46 162 15 57 174 16 61
  95475. +171 15 60 146 13 51 56 5 19 0 0 0 0 0 0 0 0 0
  95476. +0 0 0 0 0 0 0 0 0 3 0 1 14 1 5 29 3 10
  95477. +41 4 14 47 4 16 50 5 17 45 4 16 34 3 12 18 2 6
  95478. +5 0 2 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  95479. +90 8 32 169 15 59 185 17 65 187 17 66 182 16 64 163 15 57
  95480. +113 10 40 41 4 14 2 0 1 0 0 0 0 0 0 0 0 0
  95481. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95482. +0 0 0 0 0 0 0 0 0
  95483. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95484. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95485. +0 0 0 0 0 0 0 0 0 5 0 2 21 2 7 34 3 12
  95486. +29 3 10 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  95487. +3 0 1 32 3 11 79 7 28 124 11 43 154 14 54 171 15 60
  95488. +180 16 63 182 16 64 182 16 64 180 16 63 174 16 61 159 14 56
  95489. +132 12 46 88 8 31 34 3 12 3 0 1 0 0 0 0 0 0
  95490. +3 0 1 29 3 10 56 5 19 65 6 23 50 5 17 23 2 8
  95491. +3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95492. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95493. +0 0 0 0 0 0 0 0 0
  95494. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95495. +0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 25 2 9
  95498. +109 10 38 169 15 59 189 17 66 191 17 67 190 17 67 189 17 66
  95499. +189 17 66 188 17 66 188 17 66 188 17 66 189 17 66 190 17 67
  95500. +191 17 67 190 17 67 171 15 60 98 9 34 10 1 3 0 0 0
  95501. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95502. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95503. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95504. +0 0 0 0 0 0 0 0 0
  95505. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95506. +0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 14 1 5 141 13 49
  95509. +191 17 67 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95510. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95511. +188 17 66 188 17 66 189 17 67 186 17 65 65 6 23 0 0 0
  95512. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95513. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95514. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95515. +0 0 0 0 0 0 0 0 0
  95516. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95517. +0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 23 2 8 166 15 58
  95520. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95521. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95522. +188 17 66 188 17 66 189 17 66 176 16 62 45 4 16 0 0 0
  95523. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95524. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95525. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95526. +0 0 0 0 0 0 0 0 0
  95527. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95528. +0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 1 0 0 83 8 29
  95531. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95532. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95533. +188 17 66 189 17 66 185 17 65 95 9 33 3 0 1 0 0 0
  95534. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95535. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95536. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95537. +0 0 0 0 0 0 0 0 0
  95538. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95539. +0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 5 0 2
  95542. +85 8 30 176 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  95543. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  95544. +191 17 67 180 16 63 95 9 33 7 1 3 0 0 0 0 0 0
  95545. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95546. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95547. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95548. +0 0 0 0 0 0 0 0 0
  95549. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95550. +0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0
  95553. +2 0 1 52 5 18 141 13 49 185 17 65 191 17 67 189 17 67
  95554. +189 17 66 188 17 66 188 17 66 189 17 66 191 17 67 187 17 66
  95555. +146 13 51 56 5 19 4 0 1 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95558. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95559. +0 0 0 0 0 0 0 0 0
  95560. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95561. +0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0
  95564. +0 0 0 0 0 0 14 1 5 68 6 24 131 12 46 166 15 58
  95565. +180 16 63 183 17 64 180 16 63 168 15 59 134 12 47 75 7 26
  95566. +17 2 6 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95569. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95570. +0 0 0 0 0 0 0 0 0
  95571. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95572. +0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0
  95575. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 24 2 8
  95576. +44 4 15 52 5 18 45 4 16 26 2 9 6 1 2 0 0 0
  95577. +0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95580. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95581. +0 0 0 0 0 0 0 0 0
  95582. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95583. +0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95587. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95588. +0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0
  95591. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95592. +0 0 0 0 0 0 0 0 0
  95593. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95594. +0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95598. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95599. +0 0 0 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 0 0 0 0 0 0 0 0 0
  95602. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  95603. +0 0 0 0 0 0 0 0 0
  95604. diff -Nur linux-3.13.6/drivers/video/Makefile linux-raspberry-pi/drivers/video/Makefile
  95605. --- linux-3.13.6/drivers/video/Makefile 2014-03-07 07:07:02.000000000 +0100
  95606. +++ linux-raspberry-pi/drivers/video/Makefile 2014-03-11 16:53:13.000000000 +0100
  95607. @@ -100,6 +100,7 @@
  95608. obj-$(CONFIG_FB_VOODOO1) += sstfb.o
  95609. obj-$(CONFIG_FB_ARMCLCD) += amba-clcd.o
  95610. obj-$(CONFIG_FB_GOLDFISH) += goldfishfb.o
  95611. +obj-$(CONFIG_FB_BCM2708) += bcm2708_fb.o
  95612. obj-$(CONFIG_FB_68328) += 68328fb.o
  95613. obj-$(CONFIG_FB_GBE) += gbefb.o
  95614. obj-$(CONFIG_FB_CIRRUS) += cirrusfb.o
  95615. diff -Nur linux-3.13.6/drivers/w1/masters/w1-gpio.c linux-raspberry-pi/drivers/w1/masters/w1-gpio.c
  95616. --- linux-3.13.6/drivers/w1/masters/w1-gpio.c 2014-03-07 07:07:02.000000000 +0100
  95617. +++ linux-raspberry-pi/drivers/w1/masters/w1-gpio.c 2014-03-11 16:55:38.000000000 +0100
  95618. @@ -22,6 +22,9 @@
  95619. #include "../w1.h"
  95620. #include "../w1_int.h"
  95621. +static int w1_gpio_pullup = 0;
  95622. +module_param_named(pullup, w1_gpio_pullup, int, 0);
  95623. +
  95624. static void w1_gpio_write_bit_dir(void *data, u8 bit)
  95625. {
  95626. struct w1_gpio_platform_data *pdata = data;
  95627. @@ -46,6 +49,16 @@
  95628. return gpio_get_value(pdata->pin) ? 1 : 0;
  95629. }
  95630. +static void w1_gpio_bitbang_pullup(void *data, u8 on)
  95631. +{
  95632. + struct w1_gpio_platform_data *pdata = data;
  95633. +
  95634. + if (on)
  95635. + gpio_direction_output(pdata->pin, 1);
  95636. + else
  95637. + gpio_direction_input(pdata->pin);
  95638. +}
  95639. +
  95640. #if defined(CONFIG_OF)
  95641. static struct of_device_id w1_gpio_dt_ids[] = {
  95642. { .compatible = "w1-gpio" },
  95643. @@ -134,6 +147,13 @@
  95644. master->write_bit = w1_gpio_write_bit_dir;
  95645. }
  95646. + if (w1_gpio_pullup)
  95647. + if (pdata->is_open_drain)
  95648. + printk(KERN_ERR "w1-gpio 'pullup' option "
  95649. + "doesn't work with open drain GPIO\n");
  95650. + else
  95651. + master->bitbang_pullup = w1_gpio_bitbang_pullup;
  95652. +
  95653. err = w1_add_master_device(master);
  95654. if (err) {
  95655. dev_err(&pdev->dev, "w1_add_master device failed\n");
  95656. diff -Nur linux-3.13.6/drivers/w1/w1.h linux-raspberry-pi/drivers/w1/w1.h
  95657. --- linux-3.13.6/drivers/w1/w1.h 2014-03-07 07:07:02.000000000 +0100
  95658. +++ linux-raspberry-pi/drivers/w1/w1.h 2014-03-11 16:55:38.000000000 +0100
  95659. @@ -148,6 +148,12 @@
  95660. */
  95661. u8 (*set_pullup)(void *, int);
  95662. + /**
  95663. + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
  95664. + * @return -1=Error, 0=completed
  95665. + */
  95666. + void (*bitbang_pullup) (void *, u8);
  95667. +
  95668. /** Really nice hardware can handles the different types of ROM search
  95669. * w1_master* is passed to the slave found callback.
  95670. */
  95671. diff -Nur linux-3.13.6/drivers/w1/w1_int.c linux-raspberry-pi/drivers/w1/w1_int.c
  95672. --- linux-3.13.6/drivers/w1/w1_int.c 2014-03-07 07:07:02.000000000 +0100
  95673. +++ linux-raspberry-pi/drivers/w1/w1_int.c 2014-03-11 16:53:14.000000000 +0100
  95674. @@ -117,19 +117,21 @@
  95675. printk(KERN_ERR "w1_add_master_device: invalid function set\n");
  95676. return(-EINVAL);
  95677. }
  95678. - /* While it would be electrically possible to make a device that
  95679. - * generated a strong pullup in bit bang mode, only hardware that
  95680. - * controls 1-wire time frames are even expected to support a strong
  95681. - * pullup. w1_io.c would need to support calling set_pullup before
  95682. - * the last write_bit operation of a w1_write_8 which it currently
  95683. - * doesn't.
  95684. - */
  95685. +
  95686. + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
  95687. + * and takes care of timing itself */
  95688. if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  95689. printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  95690. "write_byte or touch_bit, disabling\n");
  95691. master->set_pullup = NULL;
  95692. }
  95693. + if (master->set_pullup && master->bitbang_pullup) {
  95694. + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
  95695. + "be set when bitbang_pullup is used, disabling\n");
  95696. + master->set_pullup = NULL;
  95697. + }
  95698. +
  95699. /* Lock until the device is added (or not) to w1_masters. */
  95700. mutex_lock(&w1_mlock);
  95701. /* Search for the first available id (starting at 1). */
  95702. diff -Nur linux-3.13.6/drivers/w1/w1_io.c linux-raspberry-pi/drivers/w1/w1_io.c
  95703. --- linux-3.13.6/drivers/w1/w1_io.c 2014-03-07 07:07:02.000000000 +0100
  95704. +++ linux-raspberry-pi/drivers/w1/w1_io.c 2014-03-11 16:53:14.000000000 +0100
  95705. @@ -127,10 +127,22 @@
  95706. static void w1_post_write(struct w1_master *dev)
  95707. {
  95708. if (dev->pullup_duration) {
  95709. - if (dev->enable_pullup && dev->bus_master->set_pullup)
  95710. - dev->bus_master->set_pullup(dev->bus_master->data, 0);
  95711. - else
  95712. + if (dev->enable_pullup) {
  95713. + if (dev->bus_master->set_pullup) {
  95714. + dev->bus_master->set_pullup(dev->
  95715. + bus_master->data,
  95716. + 0);
  95717. + } else if (dev->bus_master->bitbang_pullup) {
  95718. + dev->bus_master->
  95719. + bitbang_pullup(dev->bus_master->data, 1);
  95720. msleep(dev->pullup_duration);
  95721. + dev->bus_master->
  95722. + bitbang_pullup(dev->bus_master->data, 0);
  95723. + }
  95724. + } else {
  95725. + msleep(dev->pullup_duration);
  95726. + }
  95727. +
  95728. dev->pullup_duration = 0;
  95729. }
  95730. }
  95731. diff -Nur linux-3.13.6/drivers/watchdog/bcm2708_wdog.c linux-raspberry-pi/drivers/watchdog/bcm2708_wdog.c
  95732. --- linux-3.13.6/drivers/watchdog/bcm2708_wdog.c 1970-01-01 01:00:00.000000000 +0100
  95733. +++ linux-raspberry-pi/drivers/watchdog/bcm2708_wdog.c 2014-03-11 16:55:38.000000000 +0100
  95734. @@ -0,0 +1,384 @@
  95735. +/*
  95736. + * Broadcom BCM2708 watchdog driver.
  95737. + *
  95738. + * (c) Copyright 2010 Broadcom Europe Ltd
  95739. + *
  95740. + * This program is free software; you can redistribute it and/or
  95741. + * modify it under the terms of the GNU General Public License
  95742. + * as published by the Free Software Foundation; either version
  95743. + * 2 of the License, or (at your option) any later version.
  95744. + *
  95745. + * BCM2708 watchdog driver. Loosely based on wdt driver.
  95746. + */
  95747. +
  95748. +#include <linux/interrupt.h>
  95749. +#include <linux/module.h>
  95750. +#include <linux/moduleparam.h>
  95751. +#include <linux/types.h>
  95752. +#include <linux/miscdevice.h>
  95753. +#include <linux/watchdog.h>
  95754. +#include <linux/fs.h>
  95755. +#include <linux/ioport.h>
  95756. +#include <linux/notifier.h>
  95757. +#include <linux/reboot.h>
  95758. +#include <linux/init.h>
  95759. +#include <linux/io.h>
  95760. +#include <linux/uaccess.h>
  95761. +#include <mach/platform.h>
  95762. +
  95763. +#include <asm/system.h>
  95764. +
  95765. +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  95766. +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  95767. +
  95768. +static unsigned long wdog_is_open;
  95769. +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
  95770. +static char expect_close;
  95771. +
  95772. +/*
  95773. + * Module parameters
  95774. + */
  95775. +
  95776. +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
  95777. +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
  95778. +
  95779. +module_param(heartbeat, int, 0);
  95780. +MODULE_PARM_DESC(heartbeat,
  95781. + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
  95782. + __MODULE_STRING(WD_TIMO) ")");
  95783. +
  95784. +static int nowayout = WATCHDOG_NOWAYOUT;
  95785. +module_param(nowayout, int, 0);
  95786. +MODULE_PARM_DESC(nowayout,
  95787. + "Watchdog cannot be stopped once started (default="
  95788. + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  95789. +
  95790. +static DEFINE_SPINLOCK(wdog_lock);
  95791. +
  95792. +/**
  95793. + * Start the watchdog driver.
  95794. + */
  95795. +
  95796. +static int wdog_start(unsigned long timeout)
  95797. +{
  95798. + uint32_t cur;
  95799. + unsigned long flags;
  95800. + spin_lock_irqsave(&wdog_lock, flags);
  95801. +
  95802. + /* enable the watchdog */
  95803. + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
  95804. + __io_address(PM_WDOG));
  95805. + cur = ioread32(__io_address(PM_RSTC));
  95806. + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  95807. + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
  95808. +
  95809. + spin_unlock_irqrestore(&wdog_lock, flags);
  95810. + return 0;
  95811. +}
  95812. +
  95813. +/**
  95814. + * Stop the watchdog driver.
  95815. + */
  95816. +
  95817. +static int wdog_stop(void)
  95818. +{
  95819. + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
  95820. + printk(KERN_INFO "watchdog stopped\n");
  95821. + return 0;
  95822. +}
  95823. +
  95824. +/**
  95825. + * Reload counter one with the watchdog heartbeat. We don't bother
  95826. + * reloading the cascade counter.
  95827. + */
  95828. +
  95829. +static void wdog_ping(void)
  95830. +{
  95831. + wdog_start(wdog_ticks);
  95832. +}
  95833. +
  95834. +/**
  95835. + * @t: the new heartbeat value that needs to be set.
  95836. + *
  95837. + * Set a new heartbeat value for the watchdog device. If the heartbeat
  95838. + * value is incorrect we keep the old value and return -EINVAL. If
  95839. + * successful we return 0.
  95840. + */
  95841. +
  95842. +static int wdog_set_heartbeat(int t)
  95843. +{
  95844. + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
  95845. + return -EINVAL;
  95846. +
  95847. + heartbeat = t;
  95848. + wdog_ticks = SECS_TO_WDOG_TICKS(t);
  95849. + return 0;
  95850. +}
  95851. +
  95852. +/**
  95853. + * @file: file handle to the watchdog
  95854. + * @buf: buffer to write (unused as data does not matter here
  95855. + * @count: count of bytes
  95856. + * @ppos: pointer to the position to write. No seeks allowed
  95857. + *
  95858. + * A write to a watchdog device is defined as a keepalive signal.
  95859. + *
  95860. + * if 'nowayout' is set then normally a close() is ignored. But
  95861. + * if you write 'V' first then the close() will stop the timer.
  95862. + */
  95863. +
  95864. +static ssize_t wdog_write(struct file *file, const char __user *buf,
  95865. + size_t count, loff_t *ppos)
  95866. +{
  95867. + if (count) {
  95868. + if (!nowayout) {
  95869. + size_t i;
  95870. +
  95871. + /* In case it was set long ago */
  95872. + expect_close = 0;
  95873. +
  95874. + for (i = 0; i != count; i++) {
  95875. + char c;
  95876. + if (get_user(c, buf + i))
  95877. + return -EFAULT;
  95878. + if (c == 'V')
  95879. + expect_close = 42;
  95880. + }
  95881. + }
  95882. + wdog_ping();
  95883. + }
  95884. + return count;
  95885. +}
  95886. +
  95887. +static int wdog_get_status(void)
  95888. +{
  95889. + unsigned long flags;
  95890. + int status = 0;
  95891. + spin_lock_irqsave(&wdog_lock, flags);
  95892. + /* FIXME: readback reset reason */
  95893. + spin_unlock_irqrestore(&wdog_lock, flags);
  95894. + return status;
  95895. +}
  95896. +
  95897. +static uint32_t wdog_get_remaining(void)
  95898. +{
  95899. + uint32_t ret = ioread32(__io_address(PM_WDOG));
  95900. + return ret & PM_WDOG_TIME_SET;
  95901. +}
  95902. +
  95903. +/**
  95904. + * @file: file handle to the device
  95905. + * @cmd: watchdog command
  95906. + * @arg: argument pointer
  95907. + *
  95908. + * The watchdog API defines a common set of functions for all watchdogs
  95909. + * according to their available features. We only actually usefully support
  95910. + * querying capabilities and current status.
  95911. + */
  95912. +
  95913. +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  95914. +{
  95915. + void __user *argp = (void __user *)arg;
  95916. + int __user *p = argp;
  95917. + int new_heartbeat;
  95918. + int status;
  95919. + int options;
  95920. + uint32_t remaining;
  95921. +
  95922. + struct watchdog_info ident = {
  95923. + .options = WDIOF_SETTIMEOUT|
  95924. + WDIOF_MAGICCLOSE|
  95925. + WDIOF_KEEPALIVEPING,
  95926. + .firmware_version = 1,
  95927. + .identity = "BCM2708",
  95928. + };
  95929. +
  95930. + switch (cmd) {
  95931. + case WDIOC_GETSUPPORT:
  95932. + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  95933. + case WDIOC_GETSTATUS:
  95934. + status = wdog_get_status();
  95935. + return put_user(status, p);
  95936. + case WDIOC_GETBOOTSTATUS:
  95937. + return put_user(0, p);
  95938. + case WDIOC_KEEPALIVE:
  95939. + wdog_ping();
  95940. + return 0;
  95941. + case WDIOC_SETTIMEOUT:
  95942. + if (get_user(new_heartbeat, p))
  95943. + return -EFAULT;
  95944. + if (wdog_set_heartbeat(new_heartbeat))
  95945. + return -EINVAL;
  95946. + wdog_ping();
  95947. + /* Fall */
  95948. + case WDIOC_GETTIMEOUT:
  95949. + return put_user(heartbeat, p);
  95950. + case WDIOC_GETTIMELEFT:
  95951. + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
  95952. + return put_user(remaining, p);
  95953. + case WDIOC_SETOPTIONS:
  95954. + if (get_user(options, p))
  95955. + return -EFAULT;
  95956. + if (options & WDIOS_DISABLECARD)
  95957. + wdog_stop();
  95958. + if (options & WDIOS_ENABLECARD)
  95959. + wdog_start(wdog_ticks);
  95960. + return 0;
  95961. + default:
  95962. + return -ENOTTY;
  95963. + }
  95964. +}
  95965. +
  95966. +/**
  95967. + * @inode: inode of device
  95968. + * @file: file handle to device
  95969. + *
  95970. + * The watchdog device has been opened. The watchdog device is single
  95971. + * open and on opening we load the counters.
  95972. + */
  95973. +
  95974. +static int wdog_open(struct inode *inode, struct file *file)
  95975. +{
  95976. + if (test_and_set_bit(0, &wdog_is_open))
  95977. + return -EBUSY;
  95978. + /*
  95979. + * Activate
  95980. + */
  95981. + wdog_start(wdog_ticks);
  95982. + return nonseekable_open(inode, file);
  95983. +}
  95984. +
  95985. +/**
  95986. + * @inode: inode to board
  95987. + * @file: file handle to board
  95988. + *
  95989. + * The watchdog has a configurable API. There is a religious dispute
  95990. + * between people who want their watchdog to be able to shut down and
  95991. + * those who want to be sure if the watchdog manager dies the machine
  95992. + * reboots. In the former case we disable the counters, in the latter
  95993. + * case you have to open it again very soon.
  95994. + */
  95995. +
  95996. +static int wdog_release(struct inode *inode, struct file *file)
  95997. +{
  95998. + if (expect_close == 42) {
  95999. + wdog_stop();
  96000. + } else {
  96001. + printk(KERN_CRIT
  96002. + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  96003. + wdog_ping();
  96004. + }
  96005. + clear_bit(0, &wdog_is_open);
  96006. + expect_close = 0;
  96007. + return 0;
  96008. +}
  96009. +
  96010. +/**
  96011. + * @this: our notifier block
  96012. + * @code: the event being reported
  96013. + * @unused: unused
  96014. + *
  96015. + * Our notifier is called on system shutdowns. Turn the watchdog
  96016. + * off so that it does not fire during the next reboot.
  96017. + */
  96018. +
  96019. +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
  96020. + void *unused)
  96021. +{
  96022. + if (code == SYS_DOWN || code == SYS_HALT)
  96023. + wdog_stop();
  96024. + return NOTIFY_DONE;
  96025. +}
  96026. +
  96027. +/*
  96028. + * Kernel Interfaces
  96029. + */
  96030. +
  96031. +
  96032. +static const struct file_operations wdog_fops = {
  96033. + .owner = THIS_MODULE,
  96034. + .llseek = no_llseek,
  96035. + .write = wdog_write,
  96036. + .unlocked_ioctl = wdog_ioctl,
  96037. + .open = wdog_open,
  96038. + .release = wdog_release,
  96039. +};
  96040. +
  96041. +static struct miscdevice wdog_miscdev = {
  96042. + .minor = WATCHDOG_MINOR,
  96043. + .name = "watchdog",
  96044. + .fops = &wdog_fops,
  96045. +};
  96046. +
  96047. +/*
  96048. + * The WDT card needs to learn about soft shutdowns in order to
  96049. + * turn the timebomb registers off.
  96050. + */
  96051. +
  96052. +static struct notifier_block wdog_notifier = {
  96053. + .notifier_call = wdog_notify_sys,
  96054. +};
  96055. +
  96056. +/**
  96057. + * cleanup_module:
  96058. + *
  96059. + * Unload the watchdog. You cannot do this with any file handles open.
  96060. + * If your watchdog is set to continue ticking on close and you unload
  96061. + * it, well it keeps ticking. We won't get the interrupt but the board
  96062. + * will not touch PC memory so all is fine. You just have to load a new
  96063. + * module in 60 seconds or reboot.
  96064. + */
  96065. +
  96066. +static void __exit wdog_exit(void)
  96067. +{
  96068. + misc_deregister(&wdog_miscdev);
  96069. + unregister_reboot_notifier(&wdog_notifier);
  96070. +}
  96071. +
  96072. +static int __init wdog_init(void)
  96073. +{
  96074. + int ret;
  96075. +
  96076. + /* Check that the heartbeat value is within it's range;
  96077. + if not reset to the default */
  96078. + if (wdog_set_heartbeat(heartbeat)) {
  96079. + wdog_set_heartbeat(WD_TIMO);
  96080. + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
  96081. + "0 < heartbeat < %d, using %d\n",
  96082. + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  96083. + WD_TIMO);
  96084. + }
  96085. +
  96086. + ret = register_reboot_notifier(&wdog_notifier);
  96087. + if (ret) {
  96088. + printk(KERN_ERR
  96089. + "wdt: cannot register reboot notifier (err=%d)\n", ret);
  96090. + goto out_reboot;
  96091. + }
  96092. +
  96093. + ret = misc_register(&wdog_miscdev);
  96094. + if (ret) {
  96095. + printk(KERN_ERR
  96096. + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  96097. + WATCHDOG_MINOR, ret);
  96098. + goto out_misc;
  96099. + }
  96100. +
  96101. + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
  96102. + heartbeat, nowayout);
  96103. + return 0;
  96104. +
  96105. +out_misc:
  96106. + unregister_reboot_notifier(&wdog_notifier);
  96107. +out_reboot:
  96108. + return ret;
  96109. +}
  96110. +
  96111. +module_init(wdog_init);
  96112. +module_exit(wdog_exit);
  96113. +
  96114. +MODULE_AUTHOR("Luke Diamand");
  96115. +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
  96116. +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  96117. +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  96118. +MODULE_LICENSE("GPL");
  96119. diff -Nur linux-3.13.6/drivers/watchdog/Kconfig linux-raspberry-pi/drivers/watchdog/Kconfig
  96120. --- linux-3.13.6/drivers/watchdog/Kconfig 2014-03-07 07:07:02.000000000 +0100
  96121. +++ linux-raspberry-pi/drivers/watchdog/Kconfig 2014-03-11 16:55:38.000000000 +0100
  96122. @@ -392,6 +392,12 @@
  96123. To compile this driver as a module, choose M here: the
  96124. module will be called retu_wdt.
  96125. +config BCM2708_WDT
  96126. + tristate "BCM2708 Watchdog"
  96127. + depends on ARCH_BCM2708
  96128. + help
  96129. + Enables BCM2708 watchdog support.
  96130. +
  96131. config MOXART_WDT
  96132. tristate "MOXART watchdog"
  96133. depends on ARCH_MOXART
  96134. diff -Nur linux-3.13.6/drivers/watchdog/Makefile linux-raspberry-pi/drivers/watchdog/Makefile
  96135. --- linux-3.13.6/drivers/watchdog/Makefile 2014-03-07 07:07:02.000000000 +0100
  96136. +++ linux-raspberry-pi/drivers/watchdog/Makefile 2014-03-11 16:55:38.000000000 +0100
  96137. @@ -54,6 +54,7 @@
  96138. obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
  96139. obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
  96140. obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
  96141. +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
  96142. obj-$(CONFIG_BCM2835_WDT) += bcm2835_wdt.o
  96143. obj-$(CONFIG_MOXART_WDT) += moxart_wdt.o
  96144. obj-$(CONFIG_SIRFSOC_WATCHDOG) += sirfsoc_wdt.o
  96145. diff -Nur linux-3.13.6/include/linux/broadcom/vc_cma.h linux-raspberry-pi/include/linux/broadcom/vc_cma.h
  96146. --- linux-3.13.6/include/linux/broadcom/vc_cma.h 1970-01-01 01:00:00.000000000 +0100
  96147. +++ linux-raspberry-pi/include/linux/broadcom/vc_cma.h 2014-03-11 16:55:38.000000000 +0100
  96148. @@ -0,0 +1,29 @@
  96149. +/*****************************************************************************
  96150. +* Copyright 2012 Broadcom Corporation. All rights reserved.
  96151. +*
  96152. +* Unless you and Broadcom execute a separate written software license
  96153. +* agreement governing use of this software, this software is licensed to you
  96154. +* under the terms of the GNU General Public License version 2, available at
  96155. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  96156. +*
  96157. +* Notwithstanding the above, under no circumstances may you combine this
  96158. +* software in any way with any other Broadcom software provided under a
  96159. +* license other than the GPL, without Broadcom's express prior written
  96160. +* consent.
  96161. +*****************************************************************************/
  96162. +
  96163. +#if !defined( VC_CMA_H )
  96164. +#define VC_CMA_H
  96165. +
  96166. +#include <linux/ioctl.h>
  96167. +
  96168. +#define VC_CMA_IOC_MAGIC 0xc5
  96169. +
  96170. +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
  96171. +
  96172. +#ifdef __KERNEL__
  96173. +extern void __init vc_cma_early_init(void);
  96174. +extern void __init vc_cma_reserve(void);
  96175. +#endif
  96176. +
  96177. +#endif /* VC_CMA_H */
  96178. diff -Nur linux-3.13.6/include/linux/mmc/host.h linux-raspberry-pi/include/linux/mmc/host.h
  96179. --- linux-3.13.6/include/linux/mmc/host.h 2014-03-07 07:07:02.000000000 +0100
  96180. +++ linux-raspberry-pi/include/linux/mmc/host.h 2014-03-11 16:55:39.000000000 +0100
  96181. @@ -282,6 +282,7 @@
  96182. MMC_CAP2_PACKED_WR)
  96183. #define MMC_CAP2_NO_PRESCAN_POWERUP (1 << 14) /* Don't power up before scan */
  96184. #define MMC_CAP2_SANITIZE (1 << 15) /* Support Sanitize */
  96185. +#define MMC_CAP2_FORCE_MULTIBLOCK (1 << 31) /* Always use multiblock transfers */
  96186. mmc_pm_flag_t pm_caps; /* supported pm features */
  96187. diff -Nur linux-3.13.6/include/linux/mmc/sdhci.h linux-raspberry-pi/include/linux/mmc/sdhci.h
  96188. --- linux-3.13.6/include/linux/mmc/sdhci.h 2014-03-07 07:07:02.000000000 +0100
  96189. +++ linux-raspberry-pi/include/linux/mmc/sdhci.h 2014-03-11 16:55:39.000000000 +0100
  96190. @@ -102,6 +102,7 @@
  96191. #define SDHCI_QUIRK2_BROKEN_HS200 (1<<6)
  96192. int irq; /* Device IRQ */
  96193. + int second_irq; /* Additional IRQ to disable/enable in low-latency mode */
  96194. void __iomem *ioaddr; /* Mapped address */
  96195. const struct sdhci_ops *ops; /* Low level hw interface */
  96196. @@ -133,6 +134,7 @@
  96197. #define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  96198. #define SDHCI_SDR104_NEEDS_TUNING (1<<10) /* SDR104/HS200 needs tuning */
  96199. #define SDHCI_USING_RETUNING_TIMER (1<<11) /* Host is using a retuning timer for the card */
  96200. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  96201. unsigned int version; /* SDHCI spec. version */
  96202. @@ -148,6 +150,7 @@
  96203. struct mmc_request *mrq; /* Current request */
  96204. struct mmc_command *cmd; /* Current command */
  96205. + int last_cmdop; /* Opcode of last cmd sent */
  96206. struct mmc_data *data; /* Current data request */
  96207. unsigned int data_early:1; /* Data finished before cmd */
  96208. diff -Nur linux-3.13.6/include/uapi/linux/fb.h linux-raspberry-pi/include/uapi/linux/fb.h
  96209. --- linux-3.13.6/include/uapi/linux/fb.h 2014-03-07 07:07:02.000000000 +0100
  96210. +++ linux-raspberry-pi/include/uapi/linux/fb.h 2014-03-11 16:53:22.000000000 +0100
  96211. @@ -34,6 +34,11 @@
  96212. #define FBIOPUT_MODEINFO 0x4617
  96213. #define FBIOGET_DISPINFO 0x4618
  96214. #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
  96215. +/*
  96216. + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
  96217. + * be concurrently added to the mainline kernel
  96218. + */
  96219. +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
  96220. #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
  96221. #define FB_TYPE_PLANES 1 /* Non interleaved planes */
  96222. diff -Nur linux-3.13.6/kernel/cgroup.c linux-raspberry-pi/kernel/cgroup.c
  96223. --- linux-3.13.6/kernel/cgroup.c 2014-03-07 07:07:02.000000000 +0100
  96224. +++ linux-raspberry-pi/kernel/cgroup.c 2014-03-11 16:55:43.000000000 +0100
  96225. @@ -5485,6 +5485,33 @@
  96226. }
  96227. __setup("cgroup_disable=", cgroup_disable);
  96228. +static int __init cgroup_enable(char *str)
  96229. +{
  96230. + struct cgroup_subsys *ss;
  96231. + char *token;
  96232. + int i;
  96233. +
  96234. + while ((token = strsep(&str, ",")) != NULL) {
  96235. + if (!*token)
  96236. + continue;
  96237. +
  96238. + /*
  96239. + * cgroup_disable, being at boot time, can't know about
  96240. + * module subsystems, so we don't worry about them.
  96241. + */
  96242. + for_each_builtin_subsys(ss, i) {
  96243. + if (!strcmp(token, ss->name)) {
  96244. + ss->disabled = 0;
  96245. + printk(KERN_INFO "Disabling %s control group"
  96246. + " subsystem\n", ss->name);
  96247. + break;
  96248. + }
  96249. + }
  96250. + }
  96251. + return 1;
  96252. +}
  96253. +__setup("cgroup_enable=", cgroup_enable);
  96254. +
  96255. /**
  96256. * css_from_dir - get corresponding css from the dentry of a cgroup dir
  96257. * @dentry: directory dentry of interest
  96258. diff -Nur linux-3.13.6/mm/memcontrol.c linux-raspberry-pi/mm/memcontrol.c
  96259. --- linux-3.13.6/mm/memcontrol.c 2014-03-07 07:07:02.000000000 +0100
  96260. +++ linux-raspberry-pi/mm/memcontrol.c 2014-03-11 16:55:47.000000000 +0100
  96261. @@ -7030,6 +7030,7 @@
  96262. .bind = mem_cgroup_bind,
  96263. .base_cftypes = mem_cgroup_files,
  96264. .early_init = 0,
  96265. + .disabled = 1,
  96266. };
  96267. #ifdef CONFIG_MEMCG_SWAP
  96268. diff -Nur linux-3.13.6/sound/arm/bcm2835.c linux-raspberry-pi/sound/arm/bcm2835.c
  96269. --- linux-3.13.6/sound/arm/bcm2835.c 1970-01-01 01:00:00.000000000 +0100
  96270. +++ linux-raspberry-pi/sound/arm/bcm2835.c 2014-03-11 16:55:50.000000000 +0100
  96271. @@ -0,0 +1,413 @@
  96272. +/*****************************************************************************
  96273. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  96274. +*
  96275. +* Unless you and Broadcom execute a separate written software license
  96276. +* agreement governing use of this software, this software is licensed to you
  96277. +* under the terms of the GNU General Public License version 2, available at
  96278. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  96279. +*
  96280. +* Notwithstanding the above, under no circumstances may you combine this
  96281. +* software in any way with any other Broadcom software provided under a
  96282. +* license other than the GPL, without Broadcom's express prior written
  96283. +* consent.
  96284. +*****************************************************************************/
  96285. +
  96286. +#include <linux/platform_device.h>
  96287. +
  96288. +#include <linux/init.h>
  96289. +#include <linux/slab.h>
  96290. +#include <linux/module.h>
  96291. +
  96292. +#include "bcm2835.h"
  96293. +
  96294. +/* module parameters (see "Module Parameters") */
  96295. +/* SNDRV_CARDS: maximum number of cards supported by this module */
  96296. +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
  96297. +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
  96298. +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
  96299. +
  96300. +/* HACKY global pointers needed for successive probes to work : ssp
  96301. + * But compared against the changes we will have to do in VC audio_ipc code
  96302. + * to export 8 audio_ipc devices as a single IPC device and then monitor all
  96303. + * four devices in a thread, this gets things done quickly and should be easier
  96304. + * to debug if we run into issues
  96305. + */
  96306. +
  96307. +static struct snd_card *g_card = NULL;
  96308. +static bcm2835_chip_t *g_chip = NULL;
  96309. +
  96310. +static int snd_bcm2835_free(bcm2835_chip_t * chip)
  96311. +{
  96312. + kfree(chip);
  96313. + return 0;
  96314. +}
  96315. +
  96316. +/* component-destructor
  96317. + * (see "Management of Cards and Components")
  96318. + */
  96319. +static int snd_bcm2835_dev_free(struct snd_device *device)
  96320. +{
  96321. + return snd_bcm2835_free(device->device_data);
  96322. +}
  96323. +
  96324. +/* chip-specific constructor
  96325. + * (see "Management of Cards and Components")
  96326. + */
  96327. +static int snd_bcm2835_create(struct snd_card *card,
  96328. + struct platform_device *pdev,
  96329. + bcm2835_chip_t ** rchip)
  96330. +{
  96331. + bcm2835_chip_t *chip;
  96332. + int err;
  96333. + static struct snd_device_ops ops = {
  96334. + .dev_free = snd_bcm2835_dev_free,
  96335. + };
  96336. +
  96337. + *rchip = NULL;
  96338. +
  96339. + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  96340. + if (chip == NULL)
  96341. + return -ENOMEM;
  96342. +
  96343. + chip->card = card;
  96344. +
  96345. + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  96346. + if (err < 0) {
  96347. + snd_bcm2835_free(chip);
  96348. + return err;
  96349. + }
  96350. +
  96351. + *rchip = chip;
  96352. + return 0;
  96353. +}
  96354. +
  96355. +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
  96356. +{
  96357. + static int dev;
  96358. + bcm2835_chip_t *chip;
  96359. + struct snd_card *card;
  96360. + int err;
  96361. +
  96362. + if (dev >= MAX_SUBSTREAMS)
  96363. + return -ENODEV;
  96364. +
  96365. + if (!enable[dev]) {
  96366. + dev++;
  96367. + return -ENOENT;
  96368. + }
  96369. +
  96370. + if (dev > 0)
  96371. + goto add_register_map;
  96372. +
  96373. + err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &g_card);
  96374. + if (err < 0)
  96375. + goto out;
  96376. +
  96377. + snd_card_set_dev(g_card, &pdev->dev);
  96378. + strcpy(g_card->driver, "BRCM bcm2835 ALSA Driver");
  96379. + strcpy(g_card->shortname, "bcm2835 ALSA");
  96380. + sprintf(g_card->longname, "%s", g_card->shortname);
  96381. +
  96382. + err = snd_bcm2835_create(g_card, pdev, &chip);
  96383. + if (err < 0) {
  96384. + dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
  96385. + goto out_bcm2835_create;
  96386. + }
  96387. +
  96388. + g_chip = chip;
  96389. + err = snd_bcm2835_new_pcm(chip);
  96390. + if (err < 0) {
  96391. + dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
  96392. + goto out_bcm2835_new_pcm;
  96393. + }
  96394. +
  96395. + err = snd_bcm2835_new_ctl(chip);
  96396. + if (err < 0) {
  96397. + dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
  96398. + goto out_bcm2835_new_ctl;
  96399. + }
  96400. +
  96401. +add_register_map:
  96402. + card = g_card;
  96403. + chip = g_chip;
  96404. +
  96405. + BUG_ON(!(card && chip));
  96406. +
  96407. + chip->avail_substreams |= (1 << dev);
  96408. + chip->pdev[dev] = pdev;
  96409. +
  96410. + if (dev == 0) {
  96411. + err = snd_card_register(card);
  96412. + if (err < 0) {
  96413. + dev_err(&pdev->dev,
  96414. + "Failed to register bcm2835 ALSA card \n");
  96415. + goto out_card_register;
  96416. + }
  96417. + platform_set_drvdata(pdev, card);
  96418. + audio_info("bcm2835 ALSA card created!\n");
  96419. + } else {
  96420. + audio_info("bcm2835 ALSA chip created!\n");
  96421. + platform_set_drvdata(pdev, (void *)dev);
  96422. + }
  96423. +
  96424. + dev++;
  96425. +
  96426. + return 0;
  96427. +
  96428. +out_card_register:
  96429. +out_bcm2835_new_ctl:
  96430. +out_bcm2835_new_pcm:
  96431. +out_bcm2835_create:
  96432. + BUG_ON(!g_card);
  96433. + if (snd_card_free(g_card))
  96434. + dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
  96435. + g_card = NULL;
  96436. +out:
  96437. + dev = SNDRV_CARDS; /* stop more avail_substreams from being probed */
  96438. + dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
  96439. + return err;
  96440. +}
  96441. +
  96442. +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
  96443. +{
  96444. + uint32_t idx;
  96445. + void *drv_data;
  96446. +
  96447. + drv_data = platform_get_drvdata(pdev);
  96448. +
  96449. + if (drv_data == (void *)g_card) {
  96450. + /* This is the card device */
  96451. + snd_card_free((struct snd_card *)drv_data);
  96452. + g_card = NULL;
  96453. + g_chip = NULL;
  96454. + } else {
  96455. + idx = (uint32_t) drv_data;
  96456. + if (g_card != NULL) {
  96457. + BUG_ON(!g_chip);
  96458. + /* We pass chip device numbers in audio ipc devices
  96459. + * other than the one we registered our card with
  96460. + */
  96461. + idx = (uint32_t) drv_data;
  96462. + BUG_ON(!idx || idx > MAX_SUBSTREAMS);
  96463. + g_chip->avail_substreams &= ~(1 << idx);
  96464. + /* There should be atleast one substream registered
  96465. + * after we are done here, as it wil be removed when
  96466. + * the *remove* is called for the card device
  96467. + */
  96468. + BUG_ON(!g_chip->avail_substreams);
  96469. + }
  96470. + }
  96471. +
  96472. + platform_set_drvdata(pdev, NULL);
  96473. +
  96474. + return 0;
  96475. +}
  96476. +
  96477. +#ifdef CONFIG_PM
  96478. +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
  96479. + pm_message_t state)
  96480. +{
  96481. + return 0;
  96482. +}
  96483. +
  96484. +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
  96485. +{
  96486. + return 0;
  96487. +}
  96488. +
  96489. +#endif
  96490. +
  96491. +static struct platform_driver bcm2835_alsa0_driver = {
  96492. + .probe = snd_bcm2835_alsa_probe,
  96493. + .remove = snd_bcm2835_alsa_remove,
  96494. +#ifdef CONFIG_PM
  96495. + .suspend = snd_bcm2835_alsa_suspend,
  96496. + .resume = snd_bcm2835_alsa_resume,
  96497. +#endif
  96498. + .driver = {
  96499. + .name = "bcm2835_AUD0",
  96500. + .owner = THIS_MODULE,
  96501. + },
  96502. +};
  96503. +
  96504. +static struct platform_driver bcm2835_alsa1_driver = {
  96505. + .probe = snd_bcm2835_alsa_probe,
  96506. + .remove = snd_bcm2835_alsa_remove,
  96507. +#ifdef CONFIG_PM
  96508. + .suspend = snd_bcm2835_alsa_suspend,
  96509. + .resume = snd_bcm2835_alsa_resume,
  96510. +#endif
  96511. + .driver = {
  96512. + .name = "bcm2835_AUD1",
  96513. + .owner = THIS_MODULE,
  96514. + },
  96515. +};
  96516. +
  96517. +static struct platform_driver bcm2835_alsa2_driver = {
  96518. + .probe = snd_bcm2835_alsa_probe,
  96519. + .remove = snd_bcm2835_alsa_remove,
  96520. +#ifdef CONFIG_PM
  96521. + .suspend = snd_bcm2835_alsa_suspend,
  96522. + .resume = snd_bcm2835_alsa_resume,
  96523. +#endif
  96524. + .driver = {
  96525. + .name = "bcm2835_AUD2",
  96526. + .owner = THIS_MODULE,
  96527. + },
  96528. +};
  96529. +
  96530. +static struct platform_driver bcm2835_alsa3_driver = {
  96531. + .probe = snd_bcm2835_alsa_probe,
  96532. + .remove = snd_bcm2835_alsa_remove,
  96533. +#ifdef CONFIG_PM
  96534. + .suspend = snd_bcm2835_alsa_suspend,
  96535. + .resume = snd_bcm2835_alsa_resume,
  96536. +#endif
  96537. + .driver = {
  96538. + .name = "bcm2835_AUD3",
  96539. + .owner = THIS_MODULE,
  96540. + },
  96541. +};
  96542. +
  96543. +static struct platform_driver bcm2835_alsa4_driver = {
  96544. + .probe = snd_bcm2835_alsa_probe,
  96545. + .remove = snd_bcm2835_alsa_remove,
  96546. +#ifdef CONFIG_PM
  96547. + .suspend = snd_bcm2835_alsa_suspend,
  96548. + .resume = snd_bcm2835_alsa_resume,
  96549. +#endif
  96550. + .driver = {
  96551. + .name = "bcm2835_AUD4",
  96552. + .owner = THIS_MODULE,
  96553. + },
  96554. +};
  96555. +
  96556. +static struct platform_driver bcm2835_alsa5_driver = {
  96557. + .probe = snd_bcm2835_alsa_probe,
  96558. + .remove = snd_bcm2835_alsa_remove,
  96559. +#ifdef CONFIG_PM
  96560. + .suspend = snd_bcm2835_alsa_suspend,
  96561. + .resume = snd_bcm2835_alsa_resume,
  96562. +#endif
  96563. + .driver = {
  96564. + .name = "bcm2835_AUD5",
  96565. + .owner = THIS_MODULE,
  96566. + },
  96567. +};
  96568. +
  96569. +static struct platform_driver bcm2835_alsa6_driver = {
  96570. + .probe = snd_bcm2835_alsa_probe,
  96571. + .remove = snd_bcm2835_alsa_remove,
  96572. +#ifdef CONFIG_PM
  96573. + .suspend = snd_bcm2835_alsa_suspend,
  96574. + .resume = snd_bcm2835_alsa_resume,
  96575. +#endif
  96576. + .driver = {
  96577. + .name = "bcm2835_AUD6",
  96578. + .owner = THIS_MODULE,
  96579. + },
  96580. +};
  96581. +
  96582. +static struct platform_driver bcm2835_alsa7_driver = {
  96583. + .probe = snd_bcm2835_alsa_probe,
  96584. + .remove = snd_bcm2835_alsa_remove,
  96585. +#ifdef CONFIG_PM
  96586. + .suspend = snd_bcm2835_alsa_suspend,
  96587. + .resume = snd_bcm2835_alsa_resume,
  96588. +#endif
  96589. + .driver = {
  96590. + .name = "bcm2835_AUD7",
  96591. + .owner = THIS_MODULE,
  96592. + },
  96593. +};
  96594. +
  96595. +static int bcm2835_alsa_device_init(void)
  96596. +{
  96597. + int err;
  96598. + err = platform_driver_register(&bcm2835_alsa0_driver);
  96599. + if (err) {
  96600. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  96601. + goto out;
  96602. + }
  96603. +
  96604. + err = platform_driver_register(&bcm2835_alsa1_driver);
  96605. + if (err) {
  96606. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  96607. + goto unregister_0;
  96608. + }
  96609. +
  96610. + err = platform_driver_register(&bcm2835_alsa2_driver);
  96611. + if (err) {
  96612. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  96613. + goto unregister_1;
  96614. + }
  96615. +
  96616. + err = platform_driver_register(&bcm2835_alsa3_driver);
  96617. + if (err) {
  96618. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  96619. + goto unregister_2;
  96620. + }
  96621. +
  96622. + err = platform_driver_register(&bcm2835_alsa4_driver);
  96623. + if (err) {
  96624. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  96625. + goto unregister_3;
  96626. + }
  96627. +
  96628. + err = platform_driver_register(&bcm2835_alsa5_driver);
  96629. + if (err) {
  96630. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  96631. + goto unregister_4;
  96632. + }
  96633. +
  96634. + err = platform_driver_register(&bcm2835_alsa6_driver);
  96635. + if (err) {
  96636. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  96637. + goto unregister_5;
  96638. + }
  96639. +
  96640. + err = platform_driver_register(&bcm2835_alsa7_driver);
  96641. + if (err) {
  96642. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  96643. + goto unregister_6;
  96644. + }
  96645. +
  96646. + return 0;
  96647. +
  96648. +unregister_6:
  96649. + platform_driver_unregister(&bcm2835_alsa6_driver);
  96650. +unregister_5:
  96651. + platform_driver_unregister(&bcm2835_alsa5_driver);
  96652. +unregister_4:
  96653. + platform_driver_unregister(&bcm2835_alsa4_driver);
  96654. +unregister_3:
  96655. + platform_driver_unregister(&bcm2835_alsa3_driver);
  96656. +unregister_2:
  96657. + platform_driver_unregister(&bcm2835_alsa2_driver);
  96658. +unregister_1:
  96659. + platform_driver_unregister(&bcm2835_alsa1_driver);
  96660. +unregister_0:
  96661. + platform_driver_unregister(&bcm2835_alsa0_driver);
  96662. +out:
  96663. + return err;
  96664. +}
  96665. +
  96666. +static void bcm2835_alsa_device_exit(void)
  96667. +{
  96668. + platform_driver_unregister(&bcm2835_alsa0_driver);
  96669. + platform_driver_unregister(&bcm2835_alsa1_driver);
  96670. + platform_driver_unregister(&bcm2835_alsa2_driver);
  96671. + platform_driver_unregister(&bcm2835_alsa3_driver);
  96672. + platform_driver_unregister(&bcm2835_alsa4_driver);
  96673. + platform_driver_unregister(&bcm2835_alsa5_driver);
  96674. + platform_driver_unregister(&bcm2835_alsa6_driver);
  96675. + platform_driver_unregister(&bcm2835_alsa7_driver);
  96676. +}
  96677. +
  96678. +late_initcall(bcm2835_alsa_device_init);
  96679. +module_exit(bcm2835_alsa_device_exit);
  96680. +
  96681. +MODULE_AUTHOR("Dom Cobley");
  96682. +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
  96683. +MODULE_LICENSE("GPL");
  96684. +MODULE_ALIAS("platform:bcm2835_alsa");
  96685. diff -Nur linux-3.13.6/sound/arm/bcm2835-ctl.c linux-raspberry-pi/sound/arm/bcm2835-ctl.c
  96686. --- linux-3.13.6/sound/arm/bcm2835-ctl.c 1970-01-01 01:00:00.000000000 +0100
  96687. +++ linux-raspberry-pi/sound/arm/bcm2835-ctl.c 2014-03-11 16:55:50.000000000 +0100
  96688. @@ -0,0 +1,200 @@
  96689. +/*****************************************************************************
  96690. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  96691. +*
  96692. +* Unless you and Broadcom execute a separate written software license
  96693. +* agreement governing use of this software, this software is licensed to you
  96694. +* under the terms of the GNU General Public License version 2, available at
  96695. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  96696. +*
  96697. +* Notwithstanding the above, under no circumstances may you combine this
  96698. +* software in any way with any other Broadcom software provided under a
  96699. +* license other than the GPL, without Broadcom's express prior written
  96700. +* consent.
  96701. +*****************************************************************************/
  96702. +
  96703. +#include <linux/platform_device.h>
  96704. +#include <linux/init.h>
  96705. +#include <linux/io.h>
  96706. +#include <linux/jiffies.h>
  96707. +#include <linux/slab.h>
  96708. +#include <linux/time.h>
  96709. +#include <linux/wait.h>
  96710. +#include <linux/delay.h>
  96711. +#include <linux/moduleparam.h>
  96712. +#include <linux/sched.h>
  96713. +
  96714. +#include <sound/core.h>
  96715. +#include <sound/control.h>
  96716. +#include <sound/pcm.h>
  96717. +#include <sound/pcm_params.h>
  96718. +#include <sound/rawmidi.h>
  96719. +#include <sound/initval.h>
  96720. +#include <sound/tlv.h>
  96721. +
  96722. +#include "bcm2835.h"
  96723. +
  96724. +/* volume maximum and minimum in terms of 0.01dB */
  96725. +#define CTRL_VOL_MAX 400
  96726. +#define CTRL_VOL_MIN -10239 /* originally -10240 */
  96727. +
  96728. +
  96729. +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
  96730. + struct snd_ctl_elem_info *uinfo)
  96731. +{
  96732. + audio_info(" ... IN\n");
  96733. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  96734. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  96735. + uinfo->count = 1;
  96736. + uinfo->value.integer.min = CTRL_VOL_MIN;
  96737. + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
  96738. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  96739. + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  96740. + uinfo->count = 1;
  96741. + uinfo->value.integer.min = 0;
  96742. + uinfo->value.integer.max = 1;
  96743. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  96744. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  96745. + uinfo->count = 1;
  96746. + uinfo->value.integer.min = 0;
  96747. + uinfo->value.integer.max = AUDIO_DEST_MAX-1;
  96748. + }
  96749. + audio_info(" ... OUT\n");
  96750. + return 0;
  96751. +}
  96752. +
  96753. +/* toggles mute on or off depending on the value of nmute, and returns
  96754. + * 1 if the mute value was changed, otherwise 0
  96755. + */
  96756. +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
  96757. +{
  96758. + /* if settings are ok, just return 0 */
  96759. + if(chip->mute == nmute)
  96760. + return 0;
  96761. +
  96762. + /* if the sound is muted then we need to unmute */
  96763. + if(chip->mute == CTRL_VOL_MUTE)
  96764. + {
  96765. + chip->volume = chip->old_volume; /* copy the old volume back */
  96766. + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  96767. + }
  96768. + else /* otherwise we mute */
  96769. + {
  96770. + chip->old_volume = chip->volume;
  96771. + chip->volume = 26214; /* set volume to minimum level AKA mute */
  96772. + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  96773. + }
  96774. +
  96775. + chip->mute = nmute;
  96776. + return 1;
  96777. +}
  96778. +
  96779. +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
  96780. + struct snd_ctl_elem_value *ucontrol)
  96781. +{
  96782. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  96783. +
  96784. + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
  96785. +
  96786. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
  96787. + ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
  96788. + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
  96789. + ucontrol->value.integer.value[0] = chip->mute;
  96790. + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
  96791. + ucontrol->value.integer.value[0] = chip->dest;
  96792. +
  96793. + return 0;
  96794. +}
  96795. +
  96796. +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
  96797. + struct snd_ctl_elem_value *ucontrol)
  96798. +{
  96799. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  96800. + int changed = 0;
  96801. +
  96802. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  96803. + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
  96804. + if (chip->mute == CTRL_VOL_MUTE) {
  96805. + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
  96806. + return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
  96807. + }
  96808. + if (changed
  96809. + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
  96810. +
  96811. + chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
  96812. + changed = 1;
  96813. + }
  96814. +
  96815. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  96816. + /* Now implemented */
  96817. + audio_info(" Mute attempted\n");
  96818. + changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
  96819. +
  96820. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  96821. + if (ucontrol->value.integer.value[0] != chip->dest) {
  96822. + chip->dest = ucontrol->value.integer.value[0];
  96823. + changed = 1;
  96824. + }
  96825. + }
  96826. +
  96827. + if (changed) {
  96828. + if (bcm2835_audio_set_ctls(chip))
  96829. + printk(KERN_ERR "Failed to set ALSA controls..\n");
  96830. + }
  96831. +
  96832. + return changed;
  96833. +}
  96834. +
  96835. +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
  96836. +
  96837. +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
  96838. + {
  96839. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  96840. + .name = "PCM Playback Volume",
  96841. + .index = 0,
  96842. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  96843. + .private_value = PCM_PLAYBACK_VOLUME,
  96844. + .info = snd_bcm2835_ctl_info,
  96845. + .get = snd_bcm2835_ctl_get,
  96846. + .put = snd_bcm2835_ctl_put,
  96847. + .count = 1,
  96848. + .tlv = {.p = snd_bcm2835_db_scale}
  96849. + },
  96850. + {
  96851. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  96852. + .name = "PCM Playback Switch",
  96853. + .index = 0,
  96854. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  96855. + .private_value = PCM_PLAYBACK_MUTE,
  96856. + .info = snd_bcm2835_ctl_info,
  96857. + .get = snd_bcm2835_ctl_get,
  96858. + .put = snd_bcm2835_ctl_put,
  96859. + .count = 1,
  96860. + },
  96861. + {
  96862. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  96863. + .name = "PCM Playback Route",
  96864. + .index = 0,
  96865. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  96866. + .private_value = PCM_PLAYBACK_DEVICE,
  96867. + .info = snd_bcm2835_ctl_info,
  96868. + .get = snd_bcm2835_ctl_get,
  96869. + .put = snd_bcm2835_ctl_put,
  96870. + .count = 1,
  96871. + },
  96872. +};
  96873. +
  96874. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
  96875. +{
  96876. + int err;
  96877. + unsigned int idx;
  96878. +
  96879. + strcpy(chip->card->mixername, "Broadcom Mixer");
  96880. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
  96881. + err =
  96882. + snd_ctl_add(chip->card,
  96883. + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
  96884. + if (err < 0)
  96885. + return err;
  96886. + }
  96887. + return 0;
  96888. +}
  96889. diff -Nur linux-3.13.6/sound/arm/bcm2835.h linux-raspberry-pi/sound/arm/bcm2835.h
  96890. --- linux-3.13.6/sound/arm/bcm2835.h 1970-01-01 01:00:00.000000000 +0100
  96891. +++ linux-raspberry-pi/sound/arm/bcm2835.h 2014-03-11 16:55:50.000000000 +0100
  96892. @@ -0,0 +1,157 @@
  96893. +/*****************************************************************************
  96894. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  96895. +*
  96896. +* Unless you and Broadcom execute a separate written software license
  96897. +* agreement governing use of this software, this software is licensed to you
  96898. +* under the terms of the GNU General Public License version 2, available at
  96899. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  96900. +*
  96901. +* Notwithstanding the above, under no circumstances may you combine this
  96902. +* software in any way with any other Broadcom software provided under a
  96903. +* license other than the GPL, without Broadcom's express prior written
  96904. +* consent.
  96905. +*****************************************************************************/
  96906. +
  96907. +#ifndef __SOUND_ARM_BCM2835_H
  96908. +#define __SOUND_ARM_BCM2835_H
  96909. +
  96910. +#include <linux/device.h>
  96911. +#include <linux/list.h>
  96912. +#include <linux/interrupt.h>
  96913. +#include <linux/wait.h>
  96914. +#include <sound/core.h>
  96915. +#include <sound/initval.h>
  96916. +#include <sound/pcm.h>
  96917. +#include <sound/pcm_params.h>
  96918. +#include <sound/pcm-indirect.h>
  96919. +#include <linux/workqueue.h>
  96920. +
  96921. +/*
  96922. +#define AUDIO_DEBUG_ENABLE
  96923. +#define AUDIO_VERBOSE_DEBUG_ENABLE
  96924. +*/
  96925. +
  96926. +/* Debug macros */
  96927. +
  96928. +#ifdef AUDIO_DEBUG_ENABLE
  96929. +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
  96930. +
  96931. +#define audio_debug(fmt, arg...) \
  96932. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  96933. +
  96934. +#define audio_info(fmt, arg...) \
  96935. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  96936. +
  96937. +#else
  96938. +
  96939. +#define audio_debug(fmt, arg...)
  96940. +
  96941. +#define audio_info(fmt, arg...)
  96942. +
  96943. +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
  96944. +
  96945. +#else
  96946. +
  96947. +#define audio_debug(fmt, arg...)
  96948. +
  96949. +#define audio_info(fmt, arg...)
  96950. +
  96951. +#endif /* AUDIO_DEBUG_ENABLE */
  96952. +
  96953. +#define audio_error(fmt, arg...) \
  96954. + printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
  96955. +
  96956. +#define audio_warning(fmt, arg...) \
  96957. + printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
  96958. +
  96959. +#define audio_alert(fmt, arg...) \
  96960. + printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
  96961. +
  96962. +#define MAX_SUBSTREAMS (8)
  96963. +#define AVAIL_SUBSTREAMS_MASK (0xff)
  96964. +enum {
  96965. + CTRL_VOL_MUTE,
  96966. + CTRL_VOL_UNMUTE
  96967. +};
  96968. +
  96969. +/* macros for alsa2chip and chip2alsa, instead of functions */
  96970. +
  96971. +#define alsa2chip(vol) (uint)(-((vol << 8) / 100)) /* convert alsa to chip volume (defined as macro rather than function call) */
  96972. +#define chip2alsa(vol) -((vol * 100) >> 8) /* convert chip to alsa volume */
  96973. +
  96974. +/* Some constants for values .. */
  96975. +typedef enum {
  96976. + AUDIO_DEST_AUTO = 0,
  96977. + AUDIO_DEST_HEADPHONES = 1,
  96978. + AUDIO_DEST_HDMI = 2,
  96979. + AUDIO_DEST_MAX,
  96980. +} SND_BCM2835_ROUTE_T;
  96981. +
  96982. +typedef enum {
  96983. + PCM_PLAYBACK_VOLUME,
  96984. + PCM_PLAYBACK_MUTE,
  96985. + PCM_PLAYBACK_DEVICE,
  96986. +} SND_BCM2835_CTRL_T;
  96987. +
  96988. +/* definition of the chip-specific record */
  96989. +typedef struct bcm2835_chip {
  96990. + struct snd_card *card;
  96991. + struct snd_pcm *pcm;
  96992. + /* Bitmat for valid reg_base and irq numbers */
  96993. + uint32_t avail_substreams;
  96994. + struct platform_device *pdev[MAX_SUBSTREAMS];
  96995. + struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
  96996. +
  96997. + int volume;
  96998. + int old_volume; /* stores the volume value whist muted */
  96999. + int dest;
  97000. + int mute;
  97001. +} bcm2835_chip_t;
  97002. +
  97003. +typedef struct bcm2835_alsa_stream {
  97004. + bcm2835_chip_t *chip;
  97005. + struct snd_pcm_substream *substream;
  97006. + struct snd_pcm_indirect pcm_indirect;
  97007. +
  97008. + struct semaphore buffers_update_sem;
  97009. + struct semaphore control_sem;
  97010. + spinlock_t lock;
  97011. + volatile uint32_t control;
  97012. + volatile uint32_t status;
  97013. +
  97014. + int open;
  97015. + int running;
  97016. + int draining;
  97017. +
  97018. + unsigned int pos;
  97019. + unsigned int buffer_size;
  97020. + unsigned int period_size;
  97021. +
  97022. + uint32_t enable_fifo_irq;
  97023. + irq_handler_t fifo_irq_handler;
  97024. +
  97025. + atomic_t retrieved;
  97026. + struct opaque_AUDIO_INSTANCE_T *instance;
  97027. + struct workqueue_struct *my_wq;
  97028. + int idx;
  97029. +} bcm2835_alsa_stream_t;
  97030. +
  97031. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
  97032. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
  97033. +
  97034. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
  97035. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
  97036. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  97037. + uint32_t channels, uint32_t samplerate,
  97038. + uint32_t bps);
  97039. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
  97040. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
  97041. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
  97042. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
  97043. +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
  97044. + void *src);
  97045. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97046. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97047. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
  97048. +
  97049. +#endif /* __SOUND_ARM_BCM2835_H */
  97050. diff -Nur linux-3.13.6/sound/arm/bcm2835-pcm.c linux-raspberry-pi/sound/arm/bcm2835-pcm.c
  97051. --- linux-3.13.6/sound/arm/bcm2835-pcm.c 1970-01-01 01:00:00.000000000 +0100
  97052. +++ linux-raspberry-pi/sound/arm/bcm2835-pcm.c 2014-03-11 16:55:50.000000000 +0100
  97053. @@ -0,0 +1,426 @@
  97054. +/*****************************************************************************
  97055. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  97056. +*
  97057. +* Unless you and Broadcom execute a separate written software license
  97058. +* agreement governing use of this software, this software is licensed to you
  97059. +* under the terms of the GNU General Public License version 2, available at
  97060. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  97061. +*
  97062. +* Notwithstanding the above, under no circumstances may you combine this
  97063. +* software in any way with any other Broadcom software provided under a
  97064. +* license other than the GPL, without Broadcom's express prior written
  97065. +* consent.
  97066. +*****************************************************************************/
  97067. +
  97068. +#include <linux/interrupt.h>
  97069. +#include <linux/slab.h>
  97070. +
  97071. +#include "bcm2835.h"
  97072. +
  97073. +/* hardware definition */
  97074. +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
  97075. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  97076. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  97077. + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  97078. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  97079. + .rate_min = 8000,
  97080. + .rate_max = 48000,
  97081. + .channels_min = 1,
  97082. + .channels_max = 2,
  97083. + .buffer_bytes_max = 128 * 1024,
  97084. + .period_bytes_min = 1 * 1024,
  97085. + .period_bytes_max = 128 * 1024,
  97086. + .periods_min = 1,
  97087. + .periods_max = 128,
  97088. +};
  97089. +
  97090. +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
  97091. +{
  97092. + audio_info("Freeing up alsa stream here ..\n");
  97093. + if (runtime->private_data)
  97094. + kfree(runtime->private_data);
  97095. + runtime->private_data = NULL;
  97096. +}
  97097. +
  97098. +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
  97099. +{
  97100. + bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
  97101. + uint32_t consumed = 0;
  97102. + int new_period = 0;
  97103. +
  97104. + audio_info(" .. IN\n");
  97105. +
  97106. + audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
  97107. + alsa_stream ? alsa_stream->substream : 0);
  97108. +
  97109. + if (alsa_stream->open)
  97110. + consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
  97111. +
  97112. + /* We get called only if playback was triggered, So, the number of buffers we retrieve in
  97113. + * each iteration are the buffers that have been played out already
  97114. + */
  97115. +
  97116. + if (alsa_stream->period_size) {
  97117. + if ((alsa_stream->pos / alsa_stream->period_size) !=
  97118. + ((alsa_stream->pos + consumed) / alsa_stream->period_size))
  97119. + new_period = 1;
  97120. + }
  97121. + audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
  97122. + alsa_stream->pos,
  97123. + consumed,
  97124. + alsa_stream->buffer_size,
  97125. + (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
  97126. + frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
  97127. + new_period);
  97128. + if (alsa_stream->buffer_size) {
  97129. + alsa_stream->pos += consumed &~ (1<<30);
  97130. + alsa_stream->pos %= alsa_stream->buffer_size;
  97131. + }
  97132. +
  97133. + if (alsa_stream->substream) {
  97134. + if (new_period)
  97135. + snd_pcm_period_elapsed(alsa_stream->substream);
  97136. + } else {
  97137. + audio_warning(" unexpected NULL substream\n");
  97138. + }
  97139. + audio_info(" .. OUT\n");
  97140. +
  97141. + return IRQ_HANDLED;
  97142. +}
  97143. +
  97144. +/* open callback */
  97145. +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
  97146. +{
  97147. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  97148. + struct snd_pcm_runtime *runtime = substream->runtime;
  97149. + bcm2835_alsa_stream_t *alsa_stream;
  97150. + int idx;
  97151. + int err;
  97152. +
  97153. + audio_info(" .. IN (%d)\n", substream->number);
  97154. +
  97155. + audio_info("Alsa open (%d)\n", substream->number);
  97156. + idx = substream->number;
  97157. +
  97158. + if (idx > MAX_SUBSTREAMS) {
  97159. + audio_error
  97160. + ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
  97161. + idx, MAX_SUBSTREAMS);
  97162. + err = -ENODEV;
  97163. + goto out;
  97164. + }
  97165. +
  97166. + /* Check if we are ready */
  97167. + if (!(chip->avail_substreams & (1 << idx))) {
  97168. + /* We are not ready yet */
  97169. + audio_error("substream(%d) device is not ready yet\n", idx);
  97170. + err = -EAGAIN;
  97171. + goto out;
  97172. + }
  97173. +
  97174. + alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
  97175. + if (alsa_stream == NULL) {
  97176. + return -ENOMEM;
  97177. + }
  97178. +
  97179. + /* Initialise alsa_stream */
  97180. + alsa_stream->chip = chip;
  97181. + alsa_stream->substream = substream;
  97182. + alsa_stream->idx = idx;
  97183. + chip->alsa_stream[idx] = alsa_stream;
  97184. +
  97185. + sema_init(&alsa_stream->buffers_update_sem, 0);
  97186. + sema_init(&alsa_stream->control_sem, 0);
  97187. + spin_lock_init(&alsa_stream->lock);
  97188. +
  97189. + /* Enabled in start trigger, called on each "fifo irq" after that */
  97190. + alsa_stream->enable_fifo_irq = 0;
  97191. + alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
  97192. +
  97193. + runtime->private_data = alsa_stream;
  97194. + runtime->private_free = snd_bcm2835_playback_free;
  97195. + runtime->hw = snd_bcm2835_playback_hw;
  97196. + /* minimum 16 bytes alignment (for vchiq bulk transfers) */
  97197. + snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  97198. + 16);
  97199. +
  97200. + err = bcm2835_audio_open(alsa_stream);
  97201. + if (err != 0) {
  97202. + kfree(alsa_stream);
  97203. + return err;
  97204. + }
  97205. +
  97206. + alsa_stream->open = 1;
  97207. + alsa_stream->draining = 1;
  97208. +
  97209. +out:
  97210. + audio_info(" .. OUT =%d\n", err);
  97211. +
  97212. + return err;
  97213. +}
  97214. +
  97215. +/* close callback */
  97216. +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
  97217. +{
  97218. + /* the hardware-specific codes will be here */
  97219. +
  97220. + struct snd_pcm_runtime *runtime = substream->runtime;
  97221. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97222. +
  97223. + audio_info(" .. IN\n");
  97224. + audio_info("Alsa close\n");
  97225. +
  97226. + /*
  97227. + * Call stop if it's still running. This happens when app
  97228. + * is force killed and we don't get a stop trigger.
  97229. + */
  97230. + if (alsa_stream->running) {
  97231. + int err;
  97232. + err = bcm2835_audio_stop(alsa_stream);
  97233. + alsa_stream->running = 0;
  97234. + if (err != 0)
  97235. + audio_error(" Failed to STOP alsa device\n");
  97236. + }
  97237. +
  97238. + alsa_stream->period_size = 0;
  97239. + alsa_stream->buffer_size = 0;
  97240. +
  97241. + if (alsa_stream->open) {
  97242. + alsa_stream->open = 0;
  97243. + bcm2835_audio_close(alsa_stream);
  97244. + }
  97245. + if (alsa_stream->chip)
  97246. + alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
  97247. + /*
  97248. + * Do not free up alsa_stream here, it will be freed up by
  97249. + * runtime->private_free callback we registered in *_open above
  97250. + */
  97251. +
  97252. + audio_info(" .. OUT\n");
  97253. +
  97254. + return 0;
  97255. +}
  97256. +
  97257. +/* hw_params callback */
  97258. +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
  97259. + struct snd_pcm_hw_params *params)
  97260. +{
  97261. + int err;
  97262. + struct snd_pcm_runtime *runtime = substream->runtime;
  97263. + bcm2835_alsa_stream_t *alsa_stream =
  97264. + (bcm2835_alsa_stream_t *) runtime->private_data;
  97265. +
  97266. + audio_info(" .. IN\n");
  97267. +
  97268. + err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  97269. + if (err < 0) {
  97270. + audio_error
  97271. + (" pcm_lib_malloc failed to allocated pages for buffers\n");
  97272. + return err;
  97273. + }
  97274. +
  97275. + err = bcm2835_audio_set_params(alsa_stream, params_channels(params),
  97276. + params_rate(params),
  97277. + snd_pcm_format_width(params_format
  97278. + (params)));
  97279. + if (err < 0) {
  97280. + audio_error(" error setting hw params\n");
  97281. + }
  97282. +
  97283. + bcm2835_audio_setup(alsa_stream);
  97284. +
  97285. + /* in preparation of the stream, set the controls (volume level) of the stream */
  97286. + bcm2835_audio_set_ctls(alsa_stream->chip);
  97287. +
  97288. + audio_info(" .. OUT\n");
  97289. +
  97290. + return err;
  97291. +}
  97292. +
  97293. +/* hw_free callback */
  97294. +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
  97295. +{
  97296. + audio_info(" .. IN\n");
  97297. + return snd_pcm_lib_free_pages(substream);
  97298. +}
  97299. +
  97300. +/* prepare callback */
  97301. +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
  97302. +{
  97303. + struct snd_pcm_runtime *runtime = substream->runtime;
  97304. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97305. +
  97306. + audio_info(" .. IN\n");
  97307. +
  97308. + memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
  97309. +
  97310. + alsa_stream->pcm_indirect.hw_buffer_size =
  97311. + alsa_stream->pcm_indirect.sw_buffer_size =
  97312. + snd_pcm_lib_buffer_bytes(substream);
  97313. +
  97314. + alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
  97315. + alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
  97316. + alsa_stream->pos = 0;
  97317. +
  97318. + audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
  97319. + alsa_stream->buffer_size, alsa_stream->period_size,
  97320. + alsa_stream->pos, runtime->frame_bits);
  97321. +
  97322. + audio_info(" .. OUT\n");
  97323. + return 0;
  97324. +}
  97325. +
  97326. +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
  97327. + struct snd_pcm_indirect *rec, size_t bytes)
  97328. +{
  97329. + struct snd_pcm_runtime *runtime = substream->runtime;
  97330. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97331. + void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
  97332. + int err;
  97333. +
  97334. + err = bcm2835_audio_write(alsa_stream, bytes, src);
  97335. + if (err)
  97336. + audio_error(" Failed to transfer to alsa device (%d)\n", err);
  97337. +
  97338. +}
  97339. +
  97340. +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
  97341. +{
  97342. + struct snd_pcm_runtime *runtime = substream->runtime;
  97343. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97344. + struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
  97345. +
  97346. + pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
  97347. + snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
  97348. + snd_bcm2835_pcm_transfer);
  97349. + return 0;
  97350. +}
  97351. +
  97352. +/* trigger callback */
  97353. +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  97354. +{
  97355. + struct snd_pcm_runtime *runtime = substream->runtime;
  97356. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97357. + int err = 0;
  97358. +
  97359. + audio_info(" .. IN\n");
  97360. +
  97361. + switch (cmd) {
  97362. + case SNDRV_PCM_TRIGGER_START:
  97363. + audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
  97364. + alsa_stream->running);
  97365. + if (!alsa_stream->running) {
  97366. + err = bcm2835_audio_start(alsa_stream);
  97367. + if (err == 0) {
  97368. + alsa_stream->pcm_indirect.hw_io =
  97369. + alsa_stream->pcm_indirect.hw_data =
  97370. + bytes_to_frames(runtime,
  97371. + alsa_stream->pos);
  97372. + substream->ops->ack(substream);
  97373. + alsa_stream->running = 1;
  97374. + alsa_stream->draining = 1;
  97375. + } else {
  97376. + audio_error(" Failed to START alsa device (%d)\n", err);
  97377. + }
  97378. + }
  97379. + break;
  97380. + case SNDRV_PCM_TRIGGER_STOP:
  97381. + audio_debug
  97382. + ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
  97383. + alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
  97384. + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  97385. + audio_info("DRAINING\n");
  97386. + alsa_stream->draining = 1;
  97387. + } else {
  97388. + audio_info("DROPPING\n");
  97389. + alsa_stream->draining = 0;
  97390. + }
  97391. + if (alsa_stream->running) {
  97392. + err = bcm2835_audio_stop(alsa_stream);
  97393. + if (err != 0)
  97394. + audio_error(" Failed to STOP alsa device (%d)\n", err);
  97395. + alsa_stream->running = 0;
  97396. + }
  97397. + break;
  97398. + default:
  97399. + err = -EINVAL;
  97400. + }
  97401. +
  97402. + audio_info(" .. OUT\n");
  97403. + return err;
  97404. +}
  97405. +
  97406. +/* pointer callback */
  97407. +static snd_pcm_uframes_t
  97408. +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
  97409. +{
  97410. + struct snd_pcm_runtime *runtime = substream->runtime;
  97411. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  97412. +
  97413. + audio_info(" .. IN\n");
  97414. +
  97415. + audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
  97416. + frames_to_bytes(runtime, runtime->status->hw_ptr),
  97417. + frames_to_bytes(runtime, runtime->control->appl_ptr),
  97418. + alsa_stream->pos);
  97419. +
  97420. + audio_info(" .. OUT\n");
  97421. + return snd_pcm_indirect_playback_pointer(substream,
  97422. + &alsa_stream->pcm_indirect,
  97423. + alsa_stream->pos);
  97424. +}
  97425. +
  97426. +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  97427. + unsigned int cmd, void *arg)
  97428. +{
  97429. + int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  97430. + audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
  97431. + cmd, arg, arg ? *(unsigned *)arg : 0, ret);
  97432. + return ret;
  97433. +}
  97434. +
  97435. +/* operators */
  97436. +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
  97437. + .open = snd_bcm2835_playback_open,
  97438. + .close = snd_bcm2835_playback_close,
  97439. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  97440. + .hw_params = snd_bcm2835_pcm_hw_params,
  97441. + .hw_free = snd_bcm2835_pcm_hw_free,
  97442. + .prepare = snd_bcm2835_pcm_prepare,
  97443. + .trigger = snd_bcm2835_pcm_trigger,
  97444. + .pointer = snd_bcm2835_pcm_pointer,
  97445. + .ack = snd_bcm2835_pcm_ack,
  97446. +};
  97447. +
  97448. +/* create a pcm device */
  97449. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
  97450. +{
  97451. + struct snd_pcm *pcm;
  97452. + int err;
  97453. +
  97454. + audio_info(" .. IN\n");
  97455. + err =
  97456. + snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
  97457. + if (err < 0)
  97458. + return err;
  97459. + pcm->private_data = chip;
  97460. + strcpy(pcm->name, "bcm2835 ALSA");
  97461. + chip->pcm = pcm;
  97462. + chip->dest = AUDIO_DEST_AUTO;
  97463. + chip->volume = alsa2chip(0);
  97464. + chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
  97465. + /* set operators */
  97466. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  97467. + &snd_bcm2835_playback_ops);
  97468. +
  97469. + /* pre-allocation of buffers */
  97470. + /* NOTE: this may fail */
  97471. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  97472. + snd_dma_continuous_data
  97473. + (GFP_KERNEL), 64 * 1024,
  97474. + 64 * 1024);
  97475. +
  97476. + audio_info(" .. OUT\n");
  97477. +
  97478. + return 0;
  97479. +}
  97480. diff -Nur linux-3.13.6/sound/arm/bcm2835-vchiq.c linux-raspberry-pi/sound/arm/bcm2835-vchiq.c
  97481. --- linux-3.13.6/sound/arm/bcm2835-vchiq.c 1970-01-01 01:00:00.000000000 +0100
  97482. +++ linux-raspberry-pi/sound/arm/bcm2835-vchiq.c 2014-03-11 16:55:50.000000000 +0100
  97483. @@ -0,0 +1,879 @@
  97484. +/*****************************************************************************
  97485. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  97486. +*
  97487. +* Unless you and Broadcom execute a separate written software license
  97488. +* agreement governing use of this software, this software is licensed to you
  97489. +* under the terms of the GNU General Public License version 2, available at
  97490. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  97491. +*
  97492. +* Notwithstanding the above, under no circumstances may you combine this
  97493. +* software in any way with any other Broadcom software provided under a
  97494. +* license other than the GPL, without Broadcom's express prior written
  97495. +* consent.
  97496. +*****************************************************************************/
  97497. +
  97498. +#include <linux/device.h>
  97499. +#include <sound/core.h>
  97500. +#include <sound/initval.h>
  97501. +#include <sound/pcm.h>
  97502. +#include <linux/io.h>
  97503. +#include <linux/interrupt.h>
  97504. +#include <linux/fs.h>
  97505. +#include <linux/file.h>
  97506. +#include <linux/mm.h>
  97507. +#include <linux/syscalls.h>
  97508. +#include <asm/uaccess.h>
  97509. +#include <linux/slab.h>
  97510. +#include <linux/delay.h>
  97511. +#include <linux/atomic.h>
  97512. +#include <linux/module.h>
  97513. +#include <linux/completion.h>
  97514. +
  97515. +#include "bcm2835.h"
  97516. +
  97517. +/* ---- Include Files -------------------------------------------------------- */
  97518. +
  97519. +#include "interface/vchi/vchi.h"
  97520. +#include "vc_vchi_audioserv_defs.h"
  97521. +
  97522. +/* ---- Private Constants and Types ------------------------------------------ */
  97523. +
  97524. +#define BCM2835_AUDIO_STOP 0
  97525. +#define BCM2835_AUDIO_START 1
  97526. +#define BCM2835_AUDIO_WRITE 2
  97527. +
  97528. +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
  97529. +#ifdef AUDIO_DEBUG_ENABLE
  97530. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  97531. + #define LOG_WARN( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  97532. + #define LOG_INFO( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  97533. + #define LOG_DBG( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  97534. +#else
  97535. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  97536. + #define LOG_WARN( fmt, arg... )
  97537. + #define LOG_INFO( fmt, arg... )
  97538. + #define LOG_DBG( fmt, arg... )
  97539. +#endif
  97540. +
  97541. +typedef struct opaque_AUDIO_INSTANCE_T {
  97542. + uint32_t num_connections;
  97543. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  97544. + struct completion msg_avail_comp;
  97545. + struct mutex vchi_mutex;
  97546. + bcm2835_alsa_stream_t *alsa_stream;
  97547. + int32_t result;
  97548. + short peer_version;
  97549. +} AUDIO_INSTANCE_T;
  97550. +
  97551. +bool force_bulk = false;
  97552. +
  97553. +/* ---- Private Variables ---------------------------------------------------- */
  97554. +
  97555. +/* ---- Private Function Prototypes ------------------------------------------ */
  97556. +
  97557. +/* ---- Private Functions ---------------------------------------------------- */
  97558. +
  97559. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
  97560. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
  97561. +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  97562. + uint32_t count, void *src);
  97563. +
  97564. +typedef struct {
  97565. + struct work_struct my_work;
  97566. + bcm2835_alsa_stream_t *alsa_stream;
  97567. + int cmd;
  97568. + void *src;
  97569. + uint32_t count;
  97570. +} my_work_t;
  97571. +
  97572. +static void my_wq_function(struct work_struct *work)
  97573. +{
  97574. + my_work_t *w = (my_work_t *) work;
  97575. + int ret = -9;
  97576. + LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
  97577. + switch (w->cmd) {
  97578. + case BCM2835_AUDIO_START:
  97579. + ret = bcm2835_audio_start_worker(w->alsa_stream);
  97580. + break;
  97581. + case BCM2835_AUDIO_STOP:
  97582. + ret = bcm2835_audio_stop_worker(w->alsa_stream);
  97583. + break;
  97584. + case BCM2835_AUDIO_WRITE:
  97585. + ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
  97586. + w->src);
  97587. + break;
  97588. + default:
  97589. + LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
  97590. + break;
  97591. + }
  97592. + kfree((void *)work);
  97593. + LOG_DBG(" .. OUT %d\n", ret);
  97594. +}
  97595. +
  97596. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
  97597. +{
  97598. + int ret = -1;
  97599. + LOG_DBG(" .. IN\n");
  97600. + if (alsa_stream->my_wq) {
  97601. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  97602. + /*--- Queue some work (item 1) ---*/
  97603. + if (work) {
  97604. + INIT_WORK((struct work_struct *)work, my_wq_function);
  97605. + work->alsa_stream = alsa_stream;
  97606. + work->cmd = BCM2835_AUDIO_START;
  97607. + if (queue_work
  97608. + (alsa_stream->my_wq, (struct work_struct *)work))
  97609. + ret = 0;
  97610. + } else
  97611. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  97612. + }
  97613. + LOG_DBG(" .. OUT %d\n", ret);
  97614. + return ret;
  97615. +}
  97616. +
  97617. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
  97618. +{
  97619. + int ret = -1;
  97620. + LOG_DBG(" .. IN\n");
  97621. + if (alsa_stream->my_wq) {
  97622. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  97623. + /*--- Queue some work (item 1) ---*/
  97624. + if (work) {
  97625. + INIT_WORK((struct work_struct *)work, my_wq_function);
  97626. + work->alsa_stream = alsa_stream;
  97627. + work->cmd = BCM2835_AUDIO_STOP;
  97628. + if (queue_work
  97629. + (alsa_stream->my_wq, (struct work_struct *)work))
  97630. + ret = 0;
  97631. + } else
  97632. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  97633. + }
  97634. + LOG_DBG(" .. OUT %d\n", ret);
  97635. + return ret;
  97636. +}
  97637. +
  97638. +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
  97639. + uint32_t count, void *src)
  97640. +{
  97641. + int ret = -1;
  97642. + LOG_DBG(" .. IN\n");
  97643. + if (alsa_stream->my_wq) {
  97644. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  97645. + /*--- Queue some work (item 1) ---*/
  97646. + if (work) {
  97647. + INIT_WORK((struct work_struct *)work, my_wq_function);
  97648. + work->alsa_stream = alsa_stream;
  97649. + work->cmd = BCM2835_AUDIO_WRITE;
  97650. + work->src = src;
  97651. + work->count = count;
  97652. + if (queue_work
  97653. + (alsa_stream->my_wq, (struct work_struct *)work))
  97654. + ret = 0;
  97655. + } else
  97656. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  97657. + }
  97658. + LOG_DBG(" .. OUT %d\n", ret);
  97659. + return ret;
  97660. +}
  97661. +
  97662. +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
  97663. +{
  97664. + alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
  97665. + return;
  97666. +}
  97667. +
  97668. +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
  97669. +{
  97670. + if (alsa_stream->my_wq) {
  97671. + flush_workqueue(alsa_stream->my_wq);
  97672. + destroy_workqueue(alsa_stream->my_wq);
  97673. + alsa_stream->my_wq = NULL;
  97674. + }
  97675. + return;
  97676. +}
  97677. +
  97678. +static void audio_vchi_callback(void *param,
  97679. + const VCHI_CALLBACK_REASON_T reason,
  97680. + void *msg_handle)
  97681. +{
  97682. + AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
  97683. + int32_t status;
  97684. + int32_t msg_len;
  97685. + VC_AUDIO_MSG_T m;
  97686. + bcm2835_alsa_stream_t *alsa_stream = 0;
  97687. + LOG_DBG(" .. IN instance=%p, param=%p, reason=%d, handle=%p\n",
  97688. + instance, param, reason, msg_handle);
  97689. +
  97690. + if (!instance || reason != VCHI_CALLBACK_MSG_AVAILABLE) {
  97691. + return;
  97692. + }
  97693. + alsa_stream = instance->alsa_stream;
  97694. + status = vchi_msg_dequeue(instance->vchi_handle[0],
  97695. + &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
  97696. + if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
  97697. + LOG_DBG
  97698. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
  97699. + instance, m.u.result.success);
  97700. + instance->result = m.u.result.success;
  97701. + complete(&instance->msg_avail_comp);
  97702. + } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
  97703. + irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
  97704. + LOG_DBG
  97705. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
  97706. + instance, m.u.complete.count);
  97707. + if (alsa_stream && callback) {
  97708. + atomic_add(m.u.complete.count, &alsa_stream->retrieved);
  97709. + callback(0, alsa_stream);
  97710. + } else {
  97711. + LOG_DBG(" .. unexpected alsa_stream=%p, callback=%p\n",
  97712. + alsa_stream, callback);
  97713. + }
  97714. + } else {
  97715. + LOG_DBG(" .. unexpected m.type=%d\n", m.type);
  97716. + }
  97717. + LOG_DBG(" .. OUT\n");
  97718. +}
  97719. +
  97720. +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
  97721. + VCHI_CONNECTION_T **
  97722. + vchi_connections,
  97723. + uint32_t num_connections)
  97724. +{
  97725. + uint32_t i;
  97726. + AUDIO_INSTANCE_T *instance;
  97727. + int status;
  97728. +
  97729. + LOG_DBG("%s: start", __func__);
  97730. +
  97731. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  97732. + LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
  97733. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  97734. +
  97735. + return NULL;
  97736. + }
  97737. + /* Allocate memory for this instance */
  97738. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  97739. +
  97740. + memset(instance, 0, sizeof(*instance));
  97741. + instance->num_connections = num_connections;
  97742. +
  97743. + /* Create a lock for exclusive, serialized VCHI connection access */
  97744. + mutex_init(&instance->vchi_mutex);
  97745. + /* Open the VCHI service connections */
  97746. + for (i = 0; i < num_connections; i++) {
  97747. + SERVICE_CREATION_T params = {
  97748. + VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
  97749. + VC_AUDIO_SERVER_NAME, // 4cc service code
  97750. + vchi_connections[i], // passed in fn pointers
  97751. + 0, // rx fifo size (unused)
  97752. + 0, // tx fifo size (unused)
  97753. + audio_vchi_callback, // service callback
  97754. + instance, // service callback parameter
  97755. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk recieves
  97756. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk transmits
  97757. + 0 // want crc check on bulk transfers
  97758. + };
  97759. +
  97760. + status = vchi_service_open(vchi_instance, &params,
  97761. + &instance->vchi_handle[i]);
  97762. + if (status) {
  97763. + LOG_ERR
  97764. + ("%s: failed to open VCHI service connection (status=%d)\n",
  97765. + __func__, status);
  97766. +
  97767. + goto err_close_services;
  97768. + }
  97769. + /* Finished with the service for now */
  97770. + vchi_service_release(instance->vchi_handle[i]);
  97771. + }
  97772. +
  97773. + return instance;
  97774. +
  97775. +err_close_services:
  97776. + for (i = 0; i < instance->num_connections; i++) {
  97777. + vchi_service_close(instance->vchi_handle[i]);
  97778. + }
  97779. +
  97780. + kfree(instance);
  97781. +
  97782. + return NULL;
  97783. +}
  97784. +
  97785. +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
  97786. +{
  97787. + uint32_t i;
  97788. +
  97789. + LOG_DBG(" .. IN\n");
  97790. +
  97791. + if (instance == NULL) {
  97792. + LOG_ERR("%s: invalid handle %p\n", __func__, instance);
  97793. +
  97794. + return -1;
  97795. + }
  97796. +
  97797. + LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
  97798. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  97799. + {
  97800. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  97801. + return -EINTR;
  97802. + }
  97803. +
  97804. + /* Close all VCHI service connections */
  97805. + for (i = 0; i < instance->num_connections; i++) {
  97806. + int32_t success;
  97807. + LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
  97808. + vchi_service_use(instance->vchi_handle[i]);
  97809. +
  97810. + success = vchi_service_close(instance->vchi_handle[i]);
  97811. + if (success != 0) {
  97812. + LOG_ERR
  97813. + ("%s: failed to close VCHI service connection (status=%d)\n",
  97814. + __func__, success);
  97815. + }
  97816. + }
  97817. +
  97818. + mutex_unlock(&instance->vchi_mutex);
  97819. +
  97820. + kfree(instance);
  97821. +
  97822. + LOG_DBG(" .. OUT\n");
  97823. +
  97824. + return 0;
  97825. +}
  97826. +
  97827. +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
  97828. +{
  97829. + static VCHI_INSTANCE_T vchi_instance;
  97830. + static VCHI_CONNECTION_T *vchi_connection;
  97831. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  97832. + int ret;
  97833. + LOG_DBG(" .. IN\n");
  97834. +
  97835. + LOG_INFO("%s: start", __func__);
  97836. + //BUG_ON(instance);
  97837. + if (instance) {
  97838. + LOG_ERR("%s: VCHI instance already open (%p)\n",
  97839. + __func__, instance);
  97840. + instance->alsa_stream = alsa_stream;
  97841. + alsa_stream->instance = instance;
  97842. + ret = 0; // xxx todo -1;
  97843. + goto err_free_mem;
  97844. + }
  97845. +
  97846. + /* Initialize and create a VCHI connection */
  97847. + ret = vchi_initialise(&vchi_instance);
  97848. + if (ret != 0) {
  97849. + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
  97850. + __func__, ret);
  97851. +
  97852. + ret = -EIO;
  97853. + goto err_free_mem;
  97854. + }
  97855. + ret = vchi_connect(NULL, 0, vchi_instance);
  97856. + if (ret != 0) {
  97857. + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
  97858. + __func__, ret);
  97859. +
  97860. + ret = -EIO;
  97861. + goto err_free_mem;
  97862. + }
  97863. +
  97864. + /* Initialize an instance of the audio service */
  97865. + instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
  97866. +
  97867. + if (instance == NULL /*|| audio_handle != instance */ ) {
  97868. + LOG_ERR("%s: failed to initialize audio service\n", __func__);
  97869. +
  97870. + ret = -EPERM;
  97871. + goto err_free_mem;
  97872. + }
  97873. +
  97874. + instance->alsa_stream = alsa_stream;
  97875. + alsa_stream->instance = instance;
  97876. +
  97877. + LOG_DBG(" success !\n");
  97878. +err_free_mem:
  97879. + LOG_DBG(" .. OUT\n");
  97880. +
  97881. + return ret;
  97882. +}
  97883. +
  97884. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
  97885. +{
  97886. + AUDIO_INSTANCE_T *instance;
  97887. + VC_AUDIO_MSG_T m;
  97888. + int32_t success;
  97889. + int ret;
  97890. + LOG_DBG(" .. IN\n");
  97891. +
  97892. + my_workqueue_init(alsa_stream);
  97893. +
  97894. + ret = bcm2835_audio_open_connection(alsa_stream);
  97895. + if (ret != 0) {
  97896. + ret = -1;
  97897. + goto exit;
  97898. + }
  97899. + instance = alsa_stream->instance;
  97900. +
  97901. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  97902. + {
  97903. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  97904. + return -EINTR;
  97905. + }
  97906. + vchi_service_use(instance->vchi_handle[0]);
  97907. +
  97908. + m.type = VC_AUDIO_MSG_TYPE_OPEN;
  97909. +
  97910. + /* Send the message to the videocore */
  97911. + success = vchi_msg_queue(instance->vchi_handle[0],
  97912. + &m, sizeof m,
  97913. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  97914. +
  97915. + if (success != 0) {
  97916. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  97917. + __func__, success);
  97918. +
  97919. + ret = -1;
  97920. + goto unlock;
  97921. + }
  97922. +
  97923. + ret = 0;
  97924. +
  97925. +unlock:
  97926. + vchi_service_release(instance->vchi_handle[0]);
  97927. + mutex_unlock(&instance->vchi_mutex);
  97928. +exit:
  97929. + LOG_DBG(" .. OUT\n");
  97930. + return ret;
  97931. +}
  97932. +
  97933. +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
  97934. + bcm2835_chip_t * chip)
  97935. +{
  97936. + VC_AUDIO_MSG_T m;
  97937. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  97938. + int32_t success;
  97939. + int ret;
  97940. + LOG_DBG(" .. IN\n");
  97941. +
  97942. + LOG_INFO
  97943. + (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  97944. +
  97945. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  97946. + {
  97947. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  97948. + return -EINTR;
  97949. + }
  97950. + vchi_service_use(instance->vchi_handle[0]);
  97951. +
  97952. + instance->result = -1;
  97953. +
  97954. + m.type = VC_AUDIO_MSG_TYPE_CONTROL;
  97955. + m.u.control.dest = chip->dest;
  97956. + m.u.control.volume = chip->volume;
  97957. +
  97958. + /* Create the message available completion */
  97959. + init_completion(&instance->msg_avail_comp);
  97960. +
  97961. + /* Send the message to the videocore */
  97962. + success = vchi_msg_queue(instance->vchi_handle[0],
  97963. + &m, sizeof m,
  97964. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  97965. +
  97966. + if (success != 0) {
  97967. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  97968. + __func__, success);
  97969. +
  97970. + ret = -1;
  97971. + goto unlock;
  97972. + }
  97973. +
  97974. + /* We are expecting a reply from the videocore */
  97975. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  97976. + if (ret) {
  97977. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  97978. + __func__, success);
  97979. + goto unlock;
  97980. + }
  97981. +
  97982. + if (instance->result != 0) {
  97983. + LOG_ERR("%s: result=%d\n", __func__, instance->result);
  97984. +
  97985. + ret = -1;
  97986. + goto unlock;
  97987. + }
  97988. +
  97989. + ret = 0;
  97990. +
  97991. +unlock:
  97992. + vchi_service_release(instance->vchi_handle[0]);
  97993. + mutex_unlock(&instance->vchi_mutex);
  97994. +
  97995. + LOG_DBG(" .. OUT\n");
  97996. + return ret;
  97997. +}
  97998. +
  97999. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
  98000. +{
  98001. + int i;
  98002. + int ret = 0;
  98003. + LOG_DBG(" .. IN\n");
  98004. +
  98005. + /* change ctls for all substreams */
  98006. + for (i = 0; i < MAX_SUBSTREAMS; i++) {
  98007. + if (chip->avail_substreams & (1 << i)) {
  98008. + if (!chip->alsa_stream[i])
  98009. + {
  98010. + LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
  98011. + ret = 0;
  98012. + }
  98013. + else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
  98014. + (chip->alsa_stream[i], chip) != 0)
  98015. + {
  98016. + LOG_DBG("Couldn't set the controls for stream %d\n", i);
  98017. + ret = -1;
  98018. + }
  98019. + else LOG_DBG(" Controls set for stream %d\n", i);
  98020. + }
  98021. + }
  98022. + LOG_DBG(" .. OUT ret=%d\n", ret);
  98023. + return ret;
  98024. +}
  98025. +
  98026. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  98027. + uint32_t channels, uint32_t samplerate,
  98028. + uint32_t bps)
  98029. +{
  98030. + VC_AUDIO_MSG_T m;
  98031. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98032. + int32_t success;
  98033. + int ret;
  98034. + LOG_DBG(" .. IN\n");
  98035. +
  98036. + LOG_INFO
  98037. + (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
  98038. + channels, samplerate, bps);
  98039. +
  98040. + /* resend ctls - alsa_stream may not have been open when first send */
  98041. + ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
  98042. + if (ret != 0) {
  98043. + LOG_ERR(" Alsa controls not supported\n");
  98044. + return -EINVAL;
  98045. + }
  98046. +
  98047. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98048. + {
  98049. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98050. + return -EINTR;
  98051. + }
  98052. + vchi_service_use(instance->vchi_handle[0]);
  98053. +
  98054. + instance->result = -1;
  98055. +
  98056. + m.type = VC_AUDIO_MSG_TYPE_CONFIG;
  98057. + m.u.config.channels = channels;
  98058. + m.u.config.samplerate = samplerate;
  98059. + m.u.config.bps = bps;
  98060. +
  98061. + /* Create the message available completion */
  98062. + init_completion(&instance->msg_avail_comp);
  98063. +
  98064. + /* Send the message to the videocore */
  98065. + success = vchi_msg_queue(instance->vchi_handle[0],
  98066. + &m, sizeof m,
  98067. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98068. +
  98069. + if (success != 0) {
  98070. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  98071. + __func__, success);
  98072. +
  98073. + ret = -1;
  98074. + goto unlock;
  98075. + }
  98076. +
  98077. + /* We are expecting a reply from the videocore */
  98078. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  98079. + if (ret) {
  98080. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  98081. + __func__, success);
  98082. + goto unlock;
  98083. + }
  98084. +
  98085. + if (instance->result != 0) {
  98086. + LOG_ERR("%s: result=%d", __func__, instance->result);
  98087. +
  98088. + ret = -1;
  98089. + goto unlock;
  98090. + }
  98091. +
  98092. + ret = 0;
  98093. +
  98094. +unlock:
  98095. + vchi_service_release(instance->vchi_handle[0]);
  98096. + mutex_unlock(&instance->vchi_mutex);
  98097. +
  98098. + LOG_DBG(" .. OUT\n");
  98099. + return ret;
  98100. +}
  98101. +
  98102. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
  98103. +{
  98104. + LOG_DBG(" .. IN\n");
  98105. +
  98106. + LOG_DBG(" .. OUT\n");
  98107. +
  98108. + return 0;
  98109. +}
  98110. +
  98111. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
  98112. +{
  98113. + VC_AUDIO_MSG_T m;
  98114. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98115. + int32_t success;
  98116. + int ret;
  98117. + LOG_DBG(" .. IN\n");
  98118. +
  98119. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98120. + {
  98121. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98122. + return -EINTR;
  98123. + }
  98124. + vchi_service_use(instance->vchi_handle[0]);
  98125. +
  98126. + m.type = VC_AUDIO_MSG_TYPE_START;
  98127. +
  98128. + /* Send the message to the videocore */
  98129. + success = vchi_msg_queue(instance->vchi_handle[0],
  98130. + &m, sizeof m,
  98131. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98132. +
  98133. + if (success != 0) {
  98134. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98135. + __func__, success);
  98136. +
  98137. + ret = -1;
  98138. + goto unlock;
  98139. + }
  98140. +
  98141. + ret = 0;
  98142. +
  98143. +unlock:
  98144. + vchi_service_release(instance->vchi_handle[0]);
  98145. + mutex_unlock(&instance->vchi_mutex);
  98146. + LOG_DBG(" .. OUT\n");
  98147. + return ret;
  98148. +}
  98149. +
  98150. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
  98151. +{
  98152. + VC_AUDIO_MSG_T m;
  98153. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98154. + int32_t success;
  98155. + int ret;
  98156. + LOG_DBG(" .. IN\n");
  98157. +
  98158. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98159. + {
  98160. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98161. + return -EINTR;
  98162. + }
  98163. + vchi_service_use(instance->vchi_handle[0]);
  98164. +
  98165. + m.type = VC_AUDIO_MSG_TYPE_STOP;
  98166. + m.u.stop.draining = alsa_stream->draining;
  98167. +
  98168. + /* Send the message to the videocore */
  98169. + success = vchi_msg_queue(instance->vchi_handle[0],
  98170. + &m, sizeof m,
  98171. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98172. +
  98173. + if (success != 0) {
  98174. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98175. + __func__, success);
  98176. +
  98177. + ret = -1;
  98178. + goto unlock;
  98179. + }
  98180. +
  98181. + ret = 0;
  98182. +
  98183. +unlock:
  98184. + vchi_service_release(instance->vchi_handle[0]);
  98185. + mutex_unlock(&instance->vchi_mutex);
  98186. + LOG_DBG(" .. OUT\n");
  98187. + return ret;
  98188. +}
  98189. +
  98190. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
  98191. +{
  98192. + VC_AUDIO_MSG_T m;
  98193. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98194. + int32_t success;
  98195. + int ret;
  98196. + LOG_DBG(" .. IN\n");
  98197. +
  98198. + my_workqueue_quit(alsa_stream);
  98199. +
  98200. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98201. + {
  98202. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98203. + return -EINTR;
  98204. + }
  98205. + vchi_service_use(instance->vchi_handle[0]);
  98206. +
  98207. + m.type = VC_AUDIO_MSG_TYPE_CLOSE;
  98208. +
  98209. + /* Create the message available completion */
  98210. + init_completion(&instance->msg_avail_comp);
  98211. +
  98212. + /* Send the message to the videocore */
  98213. + success = vchi_msg_queue(instance->vchi_handle[0],
  98214. + &m, sizeof m,
  98215. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98216. +
  98217. + if (success != 0) {
  98218. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98219. + __func__, success);
  98220. + ret = -1;
  98221. + goto unlock;
  98222. + }
  98223. +
  98224. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  98225. + if (ret) {
  98226. + LOG_ERR("%s: failed on waiting for event (status=%d)",
  98227. + __func__, success);
  98228. + goto unlock;
  98229. + }
  98230. + if (instance->result != 0) {
  98231. + LOG_ERR("%s: failed result (status=%d)",
  98232. + __func__, instance->result);
  98233. +
  98234. + ret = -1;
  98235. + goto unlock;
  98236. + }
  98237. +
  98238. + ret = 0;
  98239. +
  98240. +unlock:
  98241. + vchi_service_release(instance->vchi_handle[0]);
  98242. + mutex_unlock(&instance->vchi_mutex);
  98243. +
  98244. + /* Stop the audio service */
  98245. + if (instance) {
  98246. + vc_vchi_audio_deinit(instance);
  98247. + alsa_stream->instance = NULL;
  98248. + }
  98249. + LOG_DBG(" .. OUT\n");
  98250. + return ret;
  98251. +}
  98252. +
  98253. +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  98254. + uint32_t count, void *src)
  98255. +{
  98256. + VC_AUDIO_MSG_T m;
  98257. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  98258. + int32_t success;
  98259. + int ret;
  98260. +
  98261. + LOG_DBG(" .. IN\n");
  98262. +
  98263. + LOG_INFO(" Writing %d bytes from %p\n", count, src);
  98264. +
  98265. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  98266. + {
  98267. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  98268. + return -EINTR;
  98269. + }
  98270. + vchi_service_use(instance->vchi_handle[0]);
  98271. +
  98272. + if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
  98273. + LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
  98274. + }
  98275. + m.type = VC_AUDIO_MSG_TYPE_WRITE;
  98276. + m.u.write.count = count;
  98277. + // old version uses bulk, new version uses control
  98278. + m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
  98279. + m.u.write.callback = alsa_stream->fifo_irq_handler;
  98280. + m.u.write.cookie = alsa_stream;
  98281. + m.u.write.silence = src == NULL;
  98282. +
  98283. + /* Send the message to the videocore */
  98284. + success = vchi_msg_queue(instance->vchi_handle[0],
  98285. + &m, sizeof m,
  98286. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98287. +
  98288. + if (success != 0) {
  98289. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)",
  98290. + __func__, success);
  98291. +
  98292. + ret = -1;
  98293. + goto unlock;
  98294. + }
  98295. + if (!m.u.write.silence) {
  98296. + if (m.u.write.max_packet == 0) {
  98297. + /* Send the message to the videocore */
  98298. + success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
  98299. + src, count,
  98300. + 0 *
  98301. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED
  98302. + +
  98303. + 1 *
  98304. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
  98305. + NULL);
  98306. + } else {
  98307. + while (count > 0) {
  98308. + int bytes = min((int)m.u.write.max_packet, (int)count);
  98309. + success = vchi_msg_queue(instance->vchi_handle[0],
  98310. + src, bytes,
  98311. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  98312. + src = (char *)src + bytes;
  98313. + count -= bytes;
  98314. + }
  98315. + }
  98316. + if (success != 0) {
  98317. + LOG_ERR
  98318. + ("%s: failed on vchi_bulk_queue_transmit (status=%d)",
  98319. + __func__, success);
  98320. +
  98321. + ret = -1;
  98322. + goto unlock;
  98323. + }
  98324. + }
  98325. + ret = 0;
  98326. +
  98327. +unlock:
  98328. + vchi_service_release(instance->vchi_handle[0]);
  98329. + mutex_unlock(&instance->vchi_mutex);
  98330. + LOG_DBG(" .. OUT\n");
  98331. + return ret;
  98332. +}
  98333. +
  98334. +/**
  98335. + * Returns all buffers from arm->vc
  98336. + */
  98337. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
  98338. +{
  98339. + LOG_DBG(" .. IN\n");
  98340. + LOG_DBG(" .. OUT\n");
  98341. + return;
  98342. +}
  98343. +
  98344. +/**
  98345. + * Forces VC to flush(drop) its filled playback buffers and
  98346. + * return them the us. (VC->ARM)
  98347. + */
  98348. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
  98349. +{
  98350. + LOG_DBG(" .. IN\n");
  98351. + LOG_DBG(" .. OUT\n");
  98352. +}
  98353. +
  98354. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
  98355. +{
  98356. + uint32_t count = atomic_read(&alsa_stream->retrieved);
  98357. + atomic_sub(count, &alsa_stream->retrieved);
  98358. + return count;
  98359. +}
  98360. +
  98361. +module_param(force_bulk, bool, 0444);
  98362. +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
  98363. diff -Nur linux-3.13.6/sound/arm/Kconfig linux-raspberry-pi/sound/arm/Kconfig
  98364. --- linux-3.13.6/sound/arm/Kconfig 2014-03-07 07:07:02.000000000 +0100
  98365. +++ linux-raspberry-pi/sound/arm/Kconfig 2014-03-11 16:53:23.000000000 +0100
  98366. @@ -39,5 +39,12 @@
  98367. Say Y or M if you want to support any AC97 codec attached to
  98368. the PXA2xx AC97 interface.
  98369. +config SND_BCM2835
  98370. + tristate "BCM2835 ALSA driver"
  98371. + depends on ARCH_BCM2708 && BCM2708_VCHIQ && SND
  98372. + select SND_PCM
  98373. + help
  98374. + Say Y or M if you want to support BCM2835 Alsa pcm card driver
  98375. +
  98376. endif # SND_ARM
  98377. diff -Nur linux-3.13.6/sound/arm/Makefile linux-raspberry-pi/sound/arm/Makefile
  98378. --- linux-3.13.6/sound/arm/Makefile 2014-03-07 07:07:02.000000000 +0100
  98379. +++ linux-raspberry-pi/sound/arm/Makefile 2014-03-11 16:55:50.000000000 +0100
  98380. @@ -14,3 +14,8 @@
  98381. obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
  98382. snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
  98383. +
  98384. +obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
  98385. +snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
  98386. +
  98387. +EXTRA_CFLAGS += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  98388. diff -Nur linux-3.13.6/sound/arm/vc_vchi_audioserv_defs.h linux-raspberry-pi/sound/arm/vc_vchi_audioserv_defs.h
  98389. --- linux-3.13.6/sound/arm/vc_vchi_audioserv_defs.h 1970-01-01 01:00:00.000000000 +0100
  98390. +++ linux-raspberry-pi/sound/arm/vc_vchi_audioserv_defs.h 2014-03-11 16:53:23.000000000 +0100
  98391. @@ -0,0 +1,116 @@
  98392. +/*****************************************************************************
  98393. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  98394. +*
  98395. +* Unless you and Broadcom execute a separate written software license
  98396. +* agreement governing use of this software, this software is licensed to you
  98397. +* under the terms of the GNU General Public License version 2, available at
  98398. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  98399. +*
  98400. +* Notwithstanding the above, under no circumstances may you combine this
  98401. +* software in any way with any other Broadcom software provided under a
  98402. +* license other than the GPL, without Broadcom's express prior written
  98403. +* consent.
  98404. +*****************************************************************************/
  98405. +
  98406. +#ifndef _VC_AUDIO_DEFS_H_
  98407. +#define _VC_AUDIO_DEFS_H_
  98408. +
  98409. +#define VC_AUDIOSERV_MIN_VER 1
  98410. +#define VC_AUDIOSERV_VER 2
  98411. +
  98412. +// FourCC code used for VCHI connection
  98413. +#define VC_AUDIO_SERVER_NAME MAKE_FOURCC("AUDS")
  98414. +
  98415. +// Maximum message length
  98416. +#define VC_AUDIO_MAX_MSG_LEN (sizeof( VC_AUDIO_MSG_T ))
  98417. +
  98418. +// List of screens that are currently supported
  98419. +// All message types supported for HOST->VC direction
  98420. +typedef enum {
  98421. + VC_AUDIO_MSG_TYPE_RESULT, // Generic result
  98422. + VC_AUDIO_MSG_TYPE_COMPLETE, // Generic result
  98423. + VC_AUDIO_MSG_TYPE_CONFIG, // Configure audio
  98424. + VC_AUDIO_MSG_TYPE_CONTROL, // Configure audio
  98425. + VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
  98426. + VC_AUDIO_MSG_TYPE_CLOSE, // Configure audio
  98427. + VC_AUDIO_MSG_TYPE_START, // Configure audio
  98428. + VC_AUDIO_MSG_TYPE_STOP, // Configure audio
  98429. + VC_AUDIO_MSG_TYPE_WRITE, // Configure audio
  98430. + VC_AUDIO_MSG_TYPE_MAX
  98431. +} VC_AUDIO_MSG_TYPE;
  98432. +
  98433. +// configure the audio
  98434. +typedef struct {
  98435. + uint32_t channels;
  98436. + uint32_t samplerate;
  98437. + uint32_t bps;
  98438. +
  98439. +} VC_AUDIO_CONFIG_T;
  98440. +
  98441. +typedef struct {
  98442. + uint32_t volume;
  98443. + uint32_t dest;
  98444. +
  98445. +} VC_AUDIO_CONTROL_T;
  98446. +
  98447. +// audio
  98448. +typedef struct {
  98449. + uint32_t dummy;
  98450. +
  98451. +} VC_AUDIO_OPEN_T;
  98452. +
  98453. +// audio
  98454. +typedef struct {
  98455. + uint32_t dummy;
  98456. +
  98457. +} VC_AUDIO_CLOSE_T;
  98458. +// audio
  98459. +typedef struct {
  98460. + uint32_t dummy;
  98461. +
  98462. +} VC_AUDIO_START_T;
  98463. +// audio
  98464. +typedef struct {
  98465. + uint32_t draining;
  98466. +
  98467. +} VC_AUDIO_STOP_T;
  98468. +
  98469. +// configure the write audio samples
  98470. +typedef struct {
  98471. + uint32_t count; // in bytes
  98472. + void *callback;
  98473. + void *cookie;
  98474. + uint16_t silence;
  98475. + uint16_t max_packet;
  98476. +} VC_AUDIO_WRITE_T;
  98477. +
  98478. +// Generic result for a request (VC->HOST)
  98479. +typedef struct {
  98480. + int32_t success; // Success value
  98481. +
  98482. +} VC_AUDIO_RESULT_T;
  98483. +
  98484. +// Generic result for a request (VC->HOST)
  98485. +typedef struct {
  98486. + int32_t count; // Success value
  98487. + void *callback;
  98488. + void *cookie;
  98489. +} VC_AUDIO_COMPLETE_T;
  98490. +
  98491. +// Message header for all messages in HOST->VC direction
  98492. +typedef struct {
  98493. + int32_t type; // Message type (VC_AUDIO_MSG_TYPE)
  98494. + union {
  98495. + VC_AUDIO_CONFIG_T config;
  98496. + VC_AUDIO_CONTROL_T control;
  98497. + VC_AUDIO_OPEN_T open;
  98498. + VC_AUDIO_CLOSE_T close;
  98499. + VC_AUDIO_START_T start;
  98500. + VC_AUDIO_STOP_T stop;
  98501. + VC_AUDIO_WRITE_T write;
  98502. + VC_AUDIO_RESULT_T result;
  98503. + VC_AUDIO_COMPLETE_T complete;
  98504. + } u;
  98505. +} VC_AUDIO_MSG_T;
  98506. +
  98507. +#endif // _VC_AUDIO_DEFS_H_
  98508. diff -Nur linux-3.13.6/sound/soc/bcm/bcm2708-i2s.c linux-raspberry-pi/sound/soc/bcm/bcm2708-i2s.c
  98509. --- linux-3.13.6/sound/soc/bcm/bcm2708-i2s.c 1970-01-01 01:00:00.000000000 +0100
  98510. +++ linux-raspberry-pi/sound/soc/bcm/bcm2708-i2s.c 2014-03-11 16:53:24.000000000 +0100
  98511. @@ -0,0 +1,945 @@
  98512. +/*
  98513. + * ALSA SoC I2S Audio Layer for Broadcom BCM2708 SoC
  98514. + *
  98515. + * Author: Florian Meier <florian.meier@koalo.de>
  98516. + * Copyright 2013
  98517. + *
  98518. + * Based on
  98519. + * Raspberry Pi PCM I2S ALSA Driver
  98520. + * Copyright (c) by Phil Poole 2013
  98521. + *
  98522. + * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
  98523. + * Vladimir Barinov, <vbarinov@embeddedalley.com>
  98524. + * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  98525. + *
  98526. + * OMAP ALSA SoC DAI driver using McBSP port
  98527. + * Copyright (C) 2008 Nokia Corporation
  98528. + * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  98529. + * Peter Ujfalusi <peter.ujfalusi@ti.com>
  98530. + *
  98531. + * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  98532. + * Author: Timur Tabi <timur@freescale.com>
  98533. + * Copyright 2007-2010 Freescale Semiconductor, Inc.
  98534. + *
  98535. + * This program is free software; you can redistribute it and/or
  98536. + * modify it under the terms of the GNU General Public License
  98537. + * version 2 as published by the Free Software Foundation.
  98538. + *
  98539. + * This program is distributed in the hope that it will be useful, but
  98540. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  98541. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  98542. + * General Public License for more details.
  98543. + */
  98544. +
  98545. +#include <linux/init.h>
  98546. +#include <linux/module.h>
  98547. +#include <linux/device.h>
  98548. +#include <linux/slab.h>
  98549. +#include <linux/delay.h>
  98550. +#include <linux/io.h>
  98551. +#include <linux/clk.h>
  98552. +
  98553. +#include <sound/core.h>
  98554. +#include <sound/pcm.h>
  98555. +#include <sound/pcm_params.h>
  98556. +#include <sound/initval.h>
  98557. +#include <sound/soc.h>
  98558. +#include <sound/dmaengine_pcm.h>
  98559. +
  98560. +/* Clock registers */
  98561. +#define BCM2708_CLK_PCMCTL_REG 0x00
  98562. +#define BCM2708_CLK_PCMDIV_REG 0x04
  98563. +
  98564. +/* Clock register settings */
  98565. +#define BCM2708_CLK_PASSWD (0x5a000000)
  98566. +#define BCM2708_CLK_PASSWD_MASK (0xff000000)
  98567. +#define BCM2708_CLK_MASH(v) ((v) << 9)
  98568. +#define BCM2708_CLK_FLIP BIT(8)
  98569. +#define BCM2708_CLK_BUSY BIT(7)
  98570. +#define BCM2708_CLK_KILL BIT(5)
  98571. +#define BCM2708_CLK_ENAB BIT(4)
  98572. +#define BCM2708_CLK_SRC(v) (v)
  98573. +
  98574. +#define BCM2708_CLK_SHIFT (12)
  98575. +#define BCM2708_CLK_DIVI(v) ((v) << BCM2708_CLK_SHIFT)
  98576. +#define BCM2708_CLK_DIVF(v) (v)
  98577. +#define BCM2708_CLK_DIVF_MASK (0xFFF)
  98578. +
  98579. +enum {
  98580. + BCM2708_CLK_MASH_0 = 0,
  98581. + BCM2708_CLK_MASH_1,
  98582. + BCM2708_CLK_MASH_2,
  98583. + BCM2708_CLK_MASH_3,
  98584. +};
  98585. +
  98586. +enum {
  98587. + BCM2708_CLK_SRC_GND = 0,
  98588. + BCM2708_CLK_SRC_OSC,
  98589. + BCM2708_CLK_SRC_DBG0,
  98590. + BCM2708_CLK_SRC_DBG1,
  98591. + BCM2708_CLK_SRC_PLLA,
  98592. + BCM2708_CLK_SRC_PLLC,
  98593. + BCM2708_CLK_SRC_PLLD,
  98594. + BCM2708_CLK_SRC_HDMI,
  98595. +};
  98596. +
  98597. +/* Most clocks are not useable (freq = 0) */
  98598. +static const unsigned int bcm2708_clk_freq[BCM2708_CLK_SRC_HDMI+1] = {
  98599. + [BCM2708_CLK_SRC_GND] = 0,
  98600. + [BCM2708_CLK_SRC_OSC] = 19200000,
  98601. + [BCM2708_CLK_SRC_DBG0] = 0,
  98602. + [BCM2708_CLK_SRC_DBG1] = 0,
  98603. + [BCM2708_CLK_SRC_PLLA] = 0,
  98604. + [BCM2708_CLK_SRC_PLLC] = 0,
  98605. + [BCM2708_CLK_SRC_PLLD] = 500000000,
  98606. + [BCM2708_CLK_SRC_HDMI] = 0,
  98607. +};
  98608. +
  98609. +/* I2S registers */
  98610. +#define BCM2708_I2S_CS_A_REG 0x00
  98611. +#define BCM2708_I2S_FIFO_A_REG 0x04
  98612. +#define BCM2708_I2S_MODE_A_REG 0x08
  98613. +#define BCM2708_I2S_RXC_A_REG 0x0c
  98614. +#define BCM2708_I2S_TXC_A_REG 0x10
  98615. +#define BCM2708_I2S_DREQ_A_REG 0x14
  98616. +#define BCM2708_I2S_INTEN_A_REG 0x18
  98617. +#define BCM2708_I2S_INTSTC_A_REG 0x1c
  98618. +#define BCM2708_I2S_GRAY_REG 0x20
  98619. +
  98620. +/* I2S register settings */
  98621. +#define BCM2708_I2S_STBY BIT(25)
  98622. +#define BCM2708_I2S_SYNC BIT(24)
  98623. +#define BCM2708_I2S_RXSEX BIT(23)
  98624. +#define BCM2708_I2S_RXF BIT(22)
  98625. +#define BCM2708_I2S_TXE BIT(21)
  98626. +#define BCM2708_I2S_RXD BIT(20)
  98627. +#define BCM2708_I2S_TXD BIT(19)
  98628. +#define BCM2708_I2S_RXR BIT(18)
  98629. +#define BCM2708_I2S_TXW BIT(17)
  98630. +#define BCM2708_I2S_CS_RXERR BIT(16)
  98631. +#define BCM2708_I2S_CS_TXERR BIT(15)
  98632. +#define BCM2708_I2S_RXSYNC BIT(14)
  98633. +#define BCM2708_I2S_TXSYNC BIT(13)
  98634. +#define BCM2708_I2S_DMAEN BIT(9)
  98635. +#define BCM2708_I2S_RXTHR(v) ((v) << 7)
  98636. +#define BCM2708_I2S_TXTHR(v) ((v) << 5)
  98637. +#define BCM2708_I2S_RXCLR BIT(4)
  98638. +#define BCM2708_I2S_TXCLR BIT(3)
  98639. +#define BCM2708_I2S_TXON BIT(2)
  98640. +#define BCM2708_I2S_RXON BIT(1)
  98641. +#define BCM2708_I2S_EN (1)
  98642. +
  98643. +#define BCM2708_I2S_CLKDIS BIT(28)
  98644. +#define BCM2708_I2S_PDMN BIT(27)
  98645. +#define BCM2708_I2S_PDME BIT(26)
  98646. +#define BCM2708_I2S_FRXP BIT(25)
  98647. +#define BCM2708_I2S_FTXP BIT(24)
  98648. +#define BCM2708_I2S_CLKM BIT(23)
  98649. +#define BCM2708_I2S_CLKI BIT(22)
  98650. +#define BCM2708_I2S_FSM BIT(21)
  98651. +#define BCM2708_I2S_FSI BIT(20)
  98652. +#define BCM2708_I2S_FLEN(v) ((v) << 10)
  98653. +#define BCM2708_I2S_FSLEN(v) (v)
  98654. +
  98655. +#define BCM2708_I2S_CHWEX BIT(15)
  98656. +#define BCM2708_I2S_CHEN BIT(14)
  98657. +#define BCM2708_I2S_CHPOS(v) ((v) << 4)
  98658. +#define BCM2708_I2S_CHWID(v) (v)
  98659. +#define BCM2708_I2S_CH1(v) ((v) << 16)
  98660. +#define BCM2708_I2S_CH2(v) (v)
  98661. +
  98662. +#define BCM2708_I2S_TX_PANIC(v) ((v) << 24)
  98663. +#define BCM2708_I2S_RX_PANIC(v) ((v) << 16)
  98664. +#define BCM2708_I2S_TX(v) ((v) << 8)
  98665. +#define BCM2708_I2S_RX(v) (v)
  98666. +
  98667. +#define BCM2708_I2S_INT_RXERR BIT(3)
  98668. +#define BCM2708_I2S_INT_TXERR BIT(2)
  98669. +#define BCM2708_I2S_INT_RXR BIT(1)
  98670. +#define BCM2708_I2S_INT_TXW BIT(0)
  98671. +
  98672. +/* I2S DMA interface */
  98673. +#define BCM2708_I2S_FIFO_PHYSICAL_ADDR 0x7E203004
  98674. +#define BCM2708_DMA_DREQ_PCM_TX 2
  98675. +#define BCM2708_DMA_DREQ_PCM_RX 3
  98676. +
  98677. +/* General device struct */
  98678. +struct bcm2708_i2s_dev {
  98679. + struct device *dev;
  98680. + struct snd_dmaengine_dai_dma_data dma_data[2];
  98681. + unsigned int fmt;
  98682. + unsigned int bclk_ratio;
  98683. +
  98684. + struct regmap *i2s_regmap;
  98685. + struct regmap *clk_regmap;
  98686. +};
  98687. +
  98688. +static void bcm2708_i2s_start_clock(struct bcm2708_i2s_dev *dev)
  98689. +{
  98690. + /* Start the clock if in master mode */
  98691. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  98692. +
  98693. + switch (master) {
  98694. + case SND_SOC_DAIFMT_CBS_CFS:
  98695. + case SND_SOC_DAIFMT_CBS_CFM:
  98696. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  98697. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  98698. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  98699. + break;
  98700. + default:
  98701. + break;
  98702. + }
  98703. +}
  98704. +
  98705. +static void bcm2708_i2s_stop_clock(struct bcm2708_i2s_dev *dev)
  98706. +{
  98707. + uint32_t clkreg;
  98708. + int timeout = 1000;
  98709. +
  98710. + /* Stop clock */
  98711. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  98712. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  98713. + BCM2708_CLK_PASSWD);
  98714. +
  98715. + /* Wait for the BUSY flag going down */
  98716. + while (--timeout) {
  98717. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  98718. + if (!(clkreg & BCM2708_CLK_BUSY))
  98719. + break;
  98720. + }
  98721. +
  98722. + if (!timeout) {
  98723. + /* KILL the clock */
  98724. + dev_err(dev->dev, "I2S clock didn't stop. Kill the clock!\n");
  98725. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  98726. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD_MASK,
  98727. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD);
  98728. + }
  98729. +}
  98730. +
  98731. +static void bcm2708_i2s_clear_fifos(struct bcm2708_i2s_dev *dev,
  98732. + bool tx, bool rx)
  98733. +{
  98734. + int timeout = 1000;
  98735. + uint32_t syncval;
  98736. + uint32_t csreg;
  98737. + uint32_t i2s_active_state;
  98738. + uint32_t clkreg;
  98739. + uint32_t clk_active_state;
  98740. + uint32_t off;
  98741. + uint32_t clr;
  98742. +
  98743. + off = tx ? BCM2708_I2S_TXON : 0;
  98744. + off |= rx ? BCM2708_I2S_RXON : 0;
  98745. +
  98746. + clr = tx ? BCM2708_I2S_TXCLR : 0;
  98747. + clr |= rx ? BCM2708_I2S_RXCLR : 0;
  98748. +
  98749. + /* Backup the current state */
  98750. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  98751. + i2s_active_state = csreg & (BCM2708_I2S_RXON | BCM2708_I2S_TXON);
  98752. +
  98753. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  98754. + clk_active_state = clkreg & BCM2708_CLK_ENAB;
  98755. +
  98756. + /* Start clock if not running */
  98757. + if (!clk_active_state) {
  98758. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  98759. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  98760. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  98761. + }
  98762. +
  98763. + /* Stop I2S module */
  98764. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, off, 0);
  98765. +
  98766. + /*
  98767. + * Clear the FIFOs
  98768. + * Requires at least 2 PCM clock cycles to take effect
  98769. + */
  98770. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, clr, clr);
  98771. +
  98772. + /* Wait for 2 PCM clock cycles */
  98773. +
  98774. + /*
  98775. + * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
  98776. + * FIXME: This does not seem to work for slave mode!
  98777. + */
  98778. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &syncval);
  98779. + syncval &= BCM2708_I2S_SYNC;
  98780. +
  98781. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  98782. + BCM2708_I2S_SYNC, ~syncval);
  98783. +
  98784. + /* Wait for the SYNC flag changing it's state */
  98785. + while (--timeout) {
  98786. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  98787. + if ((csreg & BCM2708_I2S_SYNC) != syncval)
  98788. + break;
  98789. + }
  98790. +
  98791. + if (!timeout)
  98792. + dev_err(dev->dev, "I2S SYNC error!\n");
  98793. +
  98794. + /* Stop clock if it was not running before */
  98795. + if (!clk_active_state)
  98796. + bcm2708_i2s_stop_clock(dev);
  98797. +
  98798. + /* Restore I2S state */
  98799. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  98800. + BCM2708_I2S_RXON | BCM2708_I2S_TXON, i2s_active_state);
  98801. +}
  98802. +
  98803. +static int bcm2708_i2s_set_dai_fmt(struct snd_soc_dai *dai,
  98804. + unsigned int fmt)
  98805. +{
  98806. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  98807. + dev->fmt = fmt;
  98808. + return 0;
  98809. +}
  98810. +
  98811. +static int bcm2708_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  98812. + unsigned int ratio)
  98813. +{
  98814. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  98815. + dev->bclk_ratio = ratio;
  98816. + return 0;
  98817. +}
  98818. +
  98819. +static int bcm2708_i2s_hw_params(struct snd_pcm_substream *substream,
  98820. + struct snd_pcm_hw_params *params,
  98821. + struct snd_soc_dai *dai)
  98822. +{
  98823. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  98824. +
  98825. + unsigned int sampling_rate = params_rate(params);
  98826. + unsigned int data_length, data_delay, bclk_ratio;
  98827. + unsigned int ch1pos, ch2pos, mode, format;
  98828. + unsigned int mash = BCM2708_CLK_MASH_1;
  98829. + unsigned int divi, divf, target_frequency;
  98830. + int clk_src = -1;
  98831. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  98832. + bool bit_master = (master == SND_SOC_DAIFMT_CBS_CFS
  98833. + || master == SND_SOC_DAIFMT_CBS_CFM);
  98834. +
  98835. + bool frame_master = (master == SND_SOC_DAIFMT_CBS_CFS
  98836. + || master == SND_SOC_DAIFMT_CBM_CFS);
  98837. + uint32_t csreg;
  98838. +
  98839. + /*
  98840. + * If a stream is already enabled,
  98841. + * the registers are already set properly.
  98842. + */
  98843. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  98844. +
  98845. + if (csreg & (BCM2708_I2S_TXON | BCM2708_I2S_RXON))
  98846. + return 0;
  98847. +
  98848. + /*
  98849. + * Adjust the data length according to the format.
  98850. + * We prefill the half frame length with an integer
  98851. + * divider of 2400 as explained at the clock settings.
  98852. + * Maybe it is overwritten there, if the Integer mode
  98853. + * does not apply.
  98854. + */
  98855. + switch (params_format(params)) {
  98856. + case SNDRV_PCM_FORMAT_S16_LE:
  98857. + data_length = 16;
  98858. + bclk_ratio = 40;
  98859. + break;
  98860. + case SNDRV_PCM_FORMAT_S24_LE:
  98861. + data_length = 24;
  98862. + bclk_ratio = 40;
  98863. + break;
  98864. + case SNDRV_PCM_FORMAT_S32_LE:
  98865. + data_length = 32;
  98866. + bclk_ratio = 80;
  98867. + break;
  98868. + default:
  98869. + return -EINVAL;
  98870. + }
  98871. +
  98872. + /* If bclk_ratio already set, use that one. */
  98873. + if (dev->bclk_ratio)
  98874. + bclk_ratio = dev->bclk_ratio;
  98875. +
  98876. + /*
  98877. + * Clock Settings
  98878. + *
  98879. + * The target frequency of the bit clock is
  98880. + * sampling rate * frame length
  98881. + *
  98882. + * Integer mode:
  98883. + * Sampling rates that are multiples of 8000 kHz
  98884. + * can be driven by the oscillator of 19.2 MHz
  98885. + * with an integer divider as long as the frame length
  98886. + * is an integer divider of 19200000/8000=2400 as set up above.
  98887. + * This is no longer possible if the sampling rate
  98888. + * is too high (e.g. 192 kHz), because the oscillator is too slow.
  98889. + *
  98890. + * MASH mode:
  98891. + * For all other sampling rates, it is not possible to
  98892. + * have an integer divider. Approximate the clock
  98893. + * with the MASH module that induces a slight frequency
  98894. + * variance. To minimize that it is best to have the fastest
  98895. + * clock here. That is PLLD with 500 MHz.
  98896. + */
  98897. + target_frequency = sampling_rate * bclk_ratio;
  98898. + clk_src = BCM2708_CLK_SRC_OSC;
  98899. + mash = BCM2708_CLK_MASH_0;
  98900. +
  98901. + if (bcm2708_clk_freq[clk_src] % target_frequency == 0
  98902. + && bit_master && frame_master) {
  98903. + divi = bcm2708_clk_freq[clk_src] / target_frequency;
  98904. + divf = 0;
  98905. + } else {
  98906. + uint64_t dividend;
  98907. +
  98908. + if (!dev->bclk_ratio) {
  98909. + /*
  98910. + * Overwrite bclk_ratio, because the
  98911. + * above trick is not needed or can
  98912. + * not be used.
  98913. + */
  98914. + bclk_ratio = 2 * data_length;
  98915. + }
  98916. +
  98917. + target_frequency = sampling_rate * bclk_ratio;
  98918. +
  98919. + clk_src = BCM2708_CLK_SRC_PLLD;
  98920. + mash = BCM2708_CLK_MASH_1;
  98921. +
  98922. + dividend = bcm2708_clk_freq[clk_src];
  98923. + dividend <<= BCM2708_CLK_SHIFT;
  98924. + do_div(dividend, target_frequency);
  98925. + divi = dividend >> BCM2708_CLK_SHIFT;
  98926. + divf = dividend & BCM2708_CLK_DIVF_MASK;
  98927. + }
  98928. +
  98929. + /* Set clock divider */
  98930. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMDIV_REG, BCM2708_CLK_PASSWD
  98931. + | BCM2708_CLK_DIVI(divi)
  98932. + | BCM2708_CLK_DIVF(divf));
  98933. +
  98934. + /* Setup clock, but don't start it yet */
  98935. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, BCM2708_CLK_PASSWD
  98936. + | BCM2708_CLK_MASH(mash)
  98937. + | BCM2708_CLK_SRC(clk_src));
  98938. +
  98939. + /* Setup the frame format */
  98940. + format = BCM2708_I2S_CHEN;
  98941. +
  98942. + if (data_length >= 24)
  98943. + format |= BCM2708_I2S_CHWEX;
  98944. +
  98945. + format |= BCM2708_I2S_CHWID((data_length-8)&0xf);
  98946. +
  98947. + switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  98948. + case SND_SOC_DAIFMT_I2S:
  98949. + data_delay = 1;
  98950. + break;
  98951. + default:
  98952. + /*
  98953. + * TODO
  98954. + * Others are possible but are not implemented at the moment.
  98955. + */
  98956. + dev_err(dev->dev, "%s:bad format\n", __func__);
  98957. + return -EINVAL;
  98958. + }
  98959. +
  98960. + ch1pos = data_delay;
  98961. + ch2pos = bclk_ratio / 2 + data_delay;
  98962. +
  98963. + switch (params_channels(params)) {
  98964. + case 2:
  98965. + format = BCM2708_I2S_CH1(format) | BCM2708_I2S_CH2(format);
  98966. + format |= BCM2708_I2S_CH1(BCM2708_I2S_CHPOS(ch1pos));
  98967. + format |= BCM2708_I2S_CH2(BCM2708_I2S_CHPOS(ch2pos));
  98968. + break;
  98969. + default:
  98970. + return -EINVAL;
  98971. + }
  98972. +
  98973. + /*
  98974. + * Set format for both streams.
  98975. + * We cannot set another frame length
  98976. + * (and therefore word length) anyway,
  98977. + * so the format will be the same.
  98978. + */
  98979. + regmap_write(dev->i2s_regmap, BCM2708_I2S_RXC_A_REG, format);
  98980. + regmap_write(dev->i2s_regmap, BCM2708_I2S_TXC_A_REG, format);
  98981. +
  98982. + /* Setup the I2S mode */
  98983. + mode = 0;
  98984. +
  98985. + if (data_length <= 16) {
  98986. + /*
  98987. + * Use frame packed mode (2 channels per 32 bit word)
  98988. + * We cannot set another frame length in the second stream
  98989. + * (and therefore word length) anyway,
  98990. + * so the format will be the same.
  98991. + */
  98992. + mode |= BCM2708_I2S_FTXP | BCM2708_I2S_FRXP;
  98993. + }
  98994. +
  98995. + mode |= BCM2708_I2S_FLEN(bclk_ratio - 1);
  98996. + mode |= BCM2708_I2S_FSLEN(bclk_ratio / 2);
  98997. +
  98998. + /* Master or slave? */
  98999. + switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  99000. + case SND_SOC_DAIFMT_CBS_CFS:
  99001. + /* CPU is master */
  99002. + break;
  99003. + case SND_SOC_DAIFMT_CBM_CFS:
  99004. + /*
  99005. + * CODEC is bit clock master
  99006. + * CPU is frame master
  99007. + */
  99008. + mode |= BCM2708_I2S_CLKM;
  99009. + break;
  99010. + case SND_SOC_DAIFMT_CBS_CFM:
  99011. + /*
  99012. + * CODEC is frame master
  99013. + * CPU is bit clock master
  99014. + */
  99015. + mode |= BCM2708_I2S_FSM;
  99016. + break;
  99017. + case SND_SOC_DAIFMT_CBM_CFM:
  99018. + /* CODEC is master */
  99019. + mode |= BCM2708_I2S_CLKM;
  99020. + mode |= BCM2708_I2S_FSM;
  99021. + break;
  99022. + default:
  99023. + dev_err(dev->dev, "%s:bad master\n", __func__);
  99024. + return -EINVAL;
  99025. + }
  99026. +
  99027. + /*
  99028. + * Invert clocks?
  99029. + *
  99030. + * The BCM approach seems to be inverted to the classical I2S approach.
  99031. + */
  99032. + switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
  99033. + case SND_SOC_DAIFMT_NB_NF:
  99034. + /* None. Therefore, both for BCM */
  99035. + mode |= BCM2708_I2S_CLKI;
  99036. + mode |= BCM2708_I2S_FSI;
  99037. + break;
  99038. + case SND_SOC_DAIFMT_IB_IF:
  99039. + /* Both. Therefore, none for BCM */
  99040. + break;
  99041. + case SND_SOC_DAIFMT_NB_IF:
  99042. + /*
  99043. + * Invert only frame sync. Therefore,
  99044. + * invert only bit clock for BCM
  99045. + */
  99046. + mode |= BCM2708_I2S_CLKI;
  99047. + break;
  99048. + case SND_SOC_DAIFMT_IB_NF:
  99049. + /*
  99050. + * Invert only bit clock. Therefore,
  99051. + * invert only frame sync for BCM
  99052. + */
  99053. + mode |= BCM2708_I2S_FSI;
  99054. + break;
  99055. + default:
  99056. + return -EINVAL;
  99057. + }
  99058. +
  99059. + regmap_write(dev->i2s_regmap, BCM2708_I2S_MODE_A_REG, mode);
  99060. +
  99061. + /* Setup the DMA parameters */
  99062. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99063. + BCM2708_I2S_RXTHR(1)
  99064. + | BCM2708_I2S_TXTHR(1)
  99065. + | BCM2708_I2S_DMAEN, 0xffffffff);
  99066. +
  99067. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_DREQ_A_REG,
  99068. + BCM2708_I2S_TX_PANIC(0x10)
  99069. + | BCM2708_I2S_RX_PANIC(0x30)
  99070. + | BCM2708_I2S_TX(0x30)
  99071. + | BCM2708_I2S_RX(0x20), 0xffffffff);
  99072. +
  99073. + /* Clear FIFOs */
  99074. + bcm2708_i2s_clear_fifos(dev, true, true);
  99075. +
  99076. + return 0;
  99077. +}
  99078. +
  99079. +static int bcm2708_i2s_prepare(struct snd_pcm_substream *substream,
  99080. + struct snd_soc_dai *dai)
  99081. +{
  99082. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99083. + uint32_t cs_reg;
  99084. +
  99085. + bcm2708_i2s_start_clock(dev);
  99086. +
  99087. + /*
  99088. + * Clear both FIFOs if the one that should be started
  99089. + * is not empty at the moment. This should only happen
  99090. + * after overrun. Otherwise, hw_params would have cleared
  99091. + * the FIFO.
  99092. + */
  99093. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &cs_reg);
  99094. +
  99095. + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  99096. + && !(cs_reg & BCM2708_I2S_TXE))
  99097. + bcm2708_i2s_clear_fifos(dev, true, false);
  99098. + else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
  99099. + && (cs_reg & BCM2708_I2S_RXD))
  99100. + bcm2708_i2s_clear_fifos(dev, false, true);
  99101. +
  99102. + return 0;
  99103. +}
  99104. +
  99105. +static void bcm2708_i2s_stop(struct bcm2708_i2s_dev *dev,
  99106. + struct snd_pcm_substream *substream,
  99107. + struct snd_soc_dai *dai)
  99108. +{
  99109. + uint32_t mask;
  99110. +
  99111. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  99112. + mask = BCM2708_I2S_RXON;
  99113. + else
  99114. + mask = BCM2708_I2S_TXON;
  99115. +
  99116. + regmap_update_bits(dev->i2s_regmap,
  99117. + BCM2708_I2S_CS_A_REG, mask, 0);
  99118. +
  99119. + /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
  99120. + if (!dai->active && !(dev->fmt & SND_SOC_DAIFMT_CONT))
  99121. + bcm2708_i2s_stop_clock(dev);
  99122. +}
  99123. +
  99124. +static int bcm2708_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  99125. + struct snd_soc_dai *dai)
  99126. +{
  99127. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99128. + uint32_t mask;
  99129. +
  99130. + switch (cmd) {
  99131. + case SNDRV_PCM_TRIGGER_START:
  99132. + case SNDRV_PCM_TRIGGER_RESUME:
  99133. + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  99134. + bcm2708_i2s_start_clock(dev);
  99135. +
  99136. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  99137. + mask = BCM2708_I2S_RXON;
  99138. + else
  99139. + mask = BCM2708_I2S_TXON;
  99140. +
  99141. + regmap_update_bits(dev->i2s_regmap,
  99142. + BCM2708_I2S_CS_A_REG, mask, mask);
  99143. + break;
  99144. +
  99145. + case SNDRV_PCM_TRIGGER_STOP:
  99146. + case SNDRV_PCM_TRIGGER_SUSPEND:
  99147. + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  99148. + bcm2708_i2s_stop(dev, substream, dai);
  99149. + break;
  99150. + default:
  99151. + return -EINVAL;
  99152. + }
  99153. +
  99154. + return 0;
  99155. +}
  99156. +
  99157. +static int bcm2708_i2s_startup(struct snd_pcm_substream *substream,
  99158. + struct snd_soc_dai *dai)
  99159. +{
  99160. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99161. +
  99162. + if (dai->active)
  99163. + return 0;
  99164. +
  99165. + /* Should this still be running stop it */
  99166. + bcm2708_i2s_stop_clock(dev);
  99167. +
  99168. + /* Enable PCM block */
  99169. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99170. + BCM2708_I2S_EN, BCM2708_I2S_EN);
  99171. +
  99172. + /*
  99173. + * Disable STBY.
  99174. + * Requires at least 4 PCM clock cycles to take effect.
  99175. + */
  99176. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99177. + BCM2708_I2S_STBY, BCM2708_I2S_STBY);
  99178. +
  99179. + return 0;
  99180. +}
  99181. +
  99182. +static void bcm2708_i2s_shutdown(struct snd_pcm_substream *substream,
  99183. + struct snd_soc_dai *dai)
  99184. +{
  99185. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99186. +
  99187. + bcm2708_i2s_stop(dev, substream, dai);
  99188. +
  99189. + /* If both streams are stopped, disable module and clock */
  99190. + if (dai->active)
  99191. + return;
  99192. +
  99193. + /* Disable the module */
  99194. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  99195. + BCM2708_I2S_EN, 0);
  99196. +
  99197. + /*
  99198. + * Stopping clock is necessary, because stop does
  99199. + * not stop the clock when SND_SOC_DAIFMT_CONT
  99200. + */
  99201. + bcm2708_i2s_stop_clock(dev);
  99202. +}
  99203. +
  99204. +static const struct snd_soc_dai_ops bcm2708_i2s_dai_ops = {
  99205. + .startup = bcm2708_i2s_startup,
  99206. + .shutdown = bcm2708_i2s_shutdown,
  99207. + .prepare = bcm2708_i2s_prepare,
  99208. + .trigger = bcm2708_i2s_trigger,
  99209. + .hw_params = bcm2708_i2s_hw_params,
  99210. + .set_fmt = bcm2708_i2s_set_dai_fmt,
  99211. + .set_bclk_ratio = bcm2708_i2s_set_dai_bclk_ratio
  99212. +};
  99213. +
  99214. +static int bcm2708_i2s_dai_probe(struct snd_soc_dai *dai)
  99215. +{
  99216. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  99217. +
  99218. + dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  99219. + dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  99220. +
  99221. + return 0;
  99222. +}
  99223. +
  99224. +static struct snd_soc_dai_driver bcm2708_i2s_dai = {
  99225. + .name = "bcm2708-i2s",
  99226. + .probe = bcm2708_i2s_dai_probe,
  99227. + .playback = {
  99228. + .channels_min = 2,
  99229. + .channels_max = 2,
  99230. + .rates = SNDRV_PCM_RATE_8000_192000,
  99231. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  99232. + | SNDRV_PCM_FMTBIT_S24_LE
  99233. + | SNDRV_PCM_FMTBIT_S32_LE
  99234. + },
  99235. + .capture = {
  99236. + .channels_min = 2,
  99237. + .channels_max = 2,
  99238. + .rates = SNDRV_PCM_RATE_8000_192000,
  99239. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  99240. + | SNDRV_PCM_FMTBIT_S24_LE
  99241. + | SNDRV_PCM_FMTBIT_S32_LE
  99242. + },
  99243. + .ops = &bcm2708_i2s_dai_ops,
  99244. + .symmetric_rates = 1
  99245. +};
  99246. +
  99247. +static bool bcm2708_i2s_volatile_reg(struct device *dev, unsigned int reg)
  99248. +{
  99249. + switch (reg) {
  99250. + case BCM2708_I2S_CS_A_REG:
  99251. + case BCM2708_I2S_FIFO_A_REG:
  99252. + case BCM2708_I2S_INTSTC_A_REG:
  99253. + case BCM2708_I2S_GRAY_REG:
  99254. + return true;
  99255. + default:
  99256. + return false;
  99257. + };
  99258. +}
  99259. +
  99260. +static bool bcm2708_i2s_precious_reg(struct device *dev, unsigned int reg)
  99261. +{
  99262. + switch (reg) {
  99263. + case BCM2708_I2S_FIFO_A_REG:
  99264. + return true;
  99265. + default:
  99266. + return false;
  99267. + };
  99268. +}
  99269. +
  99270. +static bool bcm2708_clk_volatile_reg(struct device *dev, unsigned int reg)
  99271. +{
  99272. + switch (reg) {
  99273. + case BCM2708_CLK_PCMCTL_REG:
  99274. + return true;
  99275. + default:
  99276. + return false;
  99277. + };
  99278. +}
  99279. +
  99280. +static const struct regmap_config bcm2708_regmap_config[] = {
  99281. + {
  99282. + .reg_bits = 32,
  99283. + .reg_stride = 4,
  99284. + .val_bits = 32,
  99285. + .max_register = BCM2708_I2S_GRAY_REG,
  99286. + .precious_reg = bcm2708_i2s_precious_reg,
  99287. + .volatile_reg = bcm2708_i2s_volatile_reg,
  99288. + .cache_type = REGCACHE_RBTREE,
  99289. + },
  99290. + {
  99291. + .reg_bits = 32,
  99292. + .reg_stride = 4,
  99293. + .val_bits = 32,
  99294. + .max_register = BCM2708_CLK_PCMDIV_REG,
  99295. + .volatile_reg = bcm2708_clk_volatile_reg,
  99296. + .cache_type = REGCACHE_RBTREE,
  99297. + },
  99298. +};
  99299. +
  99300. +static const struct snd_soc_component_driver bcm2708_i2s_component = {
  99301. + .name = "bcm2708-i2s-comp",
  99302. +};
  99303. +
  99304. +
  99305. +static void bcm2708_i2s_setup_gpio(void)
  99306. +{
  99307. + /*
  99308. + * This is the common way to handle the GPIO pins for
  99309. + * the Raspberry Pi.
  99310. + * TODO Better way would be to handle
  99311. + * this in the device tree!
  99312. + */
  99313. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  99314. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  99315. +
  99316. + unsigned int *gpio;
  99317. + int pin;
  99318. + gpio = ioremap(GPIO_BASE, SZ_16K);
  99319. +
  99320. + /* SPI is on GPIO 7..11 */
  99321. + for (pin = 28; pin <= 31; pin++) {
  99322. + INP_GPIO(pin); /* set mode to GPIO input first */
  99323. + SET_GPIO_ALT(pin, 2); /* set mode to ALT 0 */
  99324. + }
  99325. +#undef INP_GPIO
  99326. +#undef SET_GPIO_ALT
  99327. +}
  99328. +
  99329. +static const struct snd_pcm_hardware bcm2708_pcm_hardware = {
  99330. + .info = SNDRV_PCM_INFO_INTERLEAVED |
  99331. + SNDRV_PCM_INFO_JOINT_DUPLEX,
  99332. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  99333. + SNDRV_PCM_FMTBIT_S24_LE |
  99334. + SNDRV_PCM_FMTBIT_S32_LE,
  99335. + .period_bytes_min = 32,
  99336. + .period_bytes_max = 64 * PAGE_SIZE,
  99337. + .periods_min = 2,
  99338. + .periods_max = 255,
  99339. + .buffer_bytes_max = 128 * PAGE_SIZE,
  99340. +};
  99341. +
  99342. +static const struct snd_dmaengine_pcm_config bcm2708_dmaengine_pcm_config = {
  99343. + .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  99344. + .pcm_hardware = &bcm2708_pcm_hardware,
  99345. + .prealloc_buffer_size = 256 * PAGE_SIZE,
  99346. +};
  99347. +
  99348. +
  99349. +static int bcm2708_i2s_probe(struct platform_device *pdev)
  99350. +{
  99351. + struct bcm2708_i2s_dev *dev;
  99352. + int i;
  99353. + int ret;
  99354. + struct regmap *regmap[2];
  99355. + struct resource *mem[2];
  99356. +
  99357. + /* Request both ioareas */
  99358. + for (i = 0; i <= 1; i++) {
  99359. + void __iomem *base;
  99360. +
  99361. + mem[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
  99362. + base = devm_ioremap_resource(&pdev->dev, mem[i]);
  99363. + if (IS_ERR(base))
  99364. + return PTR_ERR(base);
  99365. +
  99366. + regmap[i] = devm_regmap_init_mmio(&pdev->dev, base,
  99367. + &bcm2708_regmap_config[i]);
  99368. + if (IS_ERR(regmap[i])) {
  99369. + dev_err(&pdev->dev, "I2S probe: regmap init failed\n");
  99370. + return PTR_ERR(regmap[i]);
  99371. + }
  99372. + }
  99373. +
  99374. + dev = devm_kzalloc(&pdev->dev, sizeof(*dev),
  99375. + GFP_KERNEL);
  99376. + if (IS_ERR(dev))
  99377. + return PTR_ERR(dev);
  99378. +
  99379. + bcm2708_i2s_setup_gpio();
  99380. +
  99381. + dev->i2s_regmap = regmap[0];
  99382. + dev->clk_regmap = regmap[1];
  99383. +
  99384. + /* Set the DMA address */
  99385. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
  99386. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  99387. +
  99388. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
  99389. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  99390. +
  99391. + /* Set the DREQ */
  99392. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].slave_id =
  99393. + BCM2708_DMA_DREQ_PCM_TX;
  99394. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].slave_id =
  99395. + BCM2708_DMA_DREQ_PCM_RX;
  99396. +
  99397. + /* Set the bus width */
  99398. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
  99399. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  99400. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
  99401. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  99402. +
  99403. + /* Set burst */
  99404. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
  99405. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
  99406. +
  99407. + /* BCLK ratio - use default */
  99408. + dev->bclk_ratio = 0;
  99409. +
  99410. + /* Store the pdev */
  99411. + dev->dev = &pdev->dev;
  99412. + dev_set_drvdata(&pdev->dev, dev);
  99413. +
  99414. + ret = snd_soc_register_component(&pdev->dev,
  99415. + &bcm2708_i2s_component, &bcm2708_i2s_dai, 1);
  99416. +
  99417. + if (ret) {
  99418. + dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  99419. + ret = -ENOMEM;
  99420. + return ret;
  99421. + }
  99422. +
  99423. + ret = snd_dmaengine_pcm_register(&pdev->dev,
  99424. + &bcm2708_dmaengine_pcm_config,
  99425. + SND_DMAENGINE_PCM_FLAG_COMPAT);
  99426. + if (ret) {
  99427. + dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  99428. + snd_soc_unregister_component(&pdev->dev);
  99429. + return ret;
  99430. + }
  99431. +
  99432. + return 0;
  99433. +}
  99434. +
  99435. +static int bcm2708_i2s_remove(struct platform_device *pdev)
  99436. +{
  99437. + snd_dmaengine_pcm_unregister(&pdev->dev);
  99438. + snd_soc_unregister_component(&pdev->dev);
  99439. + return 0;
  99440. +}
  99441. +
  99442. +static struct platform_driver bcm2708_i2s_driver = {
  99443. + .probe = bcm2708_i2s_probe,
  99444. + .remove = bcm2708_i2s_remove,
  99445. + .driver = {
  99446. + .name = "bcm2708-i2s",
  99447. + .owner = THIS_MODULE,
  99448. + },
  99449. +};
  99450. +
  99451. +module_platform_driver(bcm2708_i2s_driver);
  99452. +
  99453. +MODULE_ALIAS("platform:bcm2708-i2s");
  99454. +MODULE_DESCRIPTION("BCM2708 I2S interface");
  99455. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  99456. +MODULE_LICENSE("GPL v2");
  99457. diff -Nur linux-3.13.6/sound/soc/bcm/hifiberry_dac.c linux-raspberry-pi/sound/soc/bcm/hifiberry_dac.c
  99458. --- linux-3.13.6/sound/soc/bcm/hifiberry_dac.c 1970-01-01 01:00:00.000000000 +0100
  99459. +++ linux-raspberry-pi/sound/soc/bcm/hifiberry_dac.c 2014-03-11 16:53:24.000000000 +0100
  99460. @@ -0,0 +1,100 @@
  99461. +/*
  99462. + * ASoC Driver for HifiBerry DAC
  99463. + *
  99464. + * Author: Florian Meier <florian.meier@koalo.de>
  99465. + * Copyright 2013
  99466. + *
  99467. + * This program is free software; you can redistribute it and/or
  99468. + * modify it under the terms of the GNU General Public License
  99469. + * version 2 as published by the Free Software Foundation.
  99470. + *
  99471. + * This program is distributed in the hope that it will be useful, but
  99472. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  99473. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  99474. + * General Public License for more details.
  99475. + */
  99476. +
  99477. +#include <linux/module.h>
  99478. +#include <linux/platform_device.h>
  99479. +
  99480. +#include <sound/core.h>
  99481. +#include <sound/pcm.h>
  99482. +#include <sound/pcm_params.h>
  99483. +#include <sound/soc.h>
  99484. +#include <sound/jack.h>
  99485. +
  99486. +static int snd_rpi_hifiberry_dac_init(struct snd_soc_pcm_runtime *rtd)
  99487. +{
  99488. + return 0;
  99489. +}
  99490. +
  99491. +static int snd_rpi_hifiberry_dac_hw_params(struct snd_pcm_substream *substream,
  99492. + struct snd_pcm_hw_params *params)
  99493. +{
  99494. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  99495. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  99496. +
  99497. + unsigned int sample_bits =
  99498. + snd_pcm_format_physical_width(params_format(params));
  99499. +
  99500. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  99501. +}
  99502. +
  99503. +/* machine stream operations */
  99504. +static struct snd_soc_ops snd_rpi_hifiberry_dac_ops = {
  99505. + .hw_params = snd_rpi_hifiberry_dac_hw_params,
  99506. +};
  99507. +
  99508. +static struct snd_soc_dai_link snd_rpi_hifiberry_dac_dai[] = {
  99509. +{
  99510. + .name = "HifiBerry DAC",
  99511. + .stream_name = "HifiBerry DAC HiFi",
  99512. + .cpu_dai_name = "bcm2708-i2s.0",
  99513. + .codec_dai_name = "pcm5102a-hifi",
  99514. + .platform_name = "bcm2708-i2s.0",
  99515. + .codec_name = "pcm5102a-codec",
  99516. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  99517. + SND_SOC_DAIFMT_CBS_CFS,
  99518. + .ops = &snd_rpi_hifiberry_dac_ops,
  99519. + .init = snd_rpi_hifiberry_dac_init,
  99520. +},
  99521. +};
  99522. +
  99523. +/* audio machine driver */
  99524. +static struct snd_soc_card snd_rpi_hifiberry_dac = {
  99525. + .name = "snd_rpi_hifiberry_dac",
  99526. + .dai_link = snd_rpi_hifiberry_dac_dai,
  99527. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dac_dai),
  99528. +};
  99529. +
  99530. +static int snd_rpi_hifiberry_dac_probe(struct platform_device *pdev)
  99531. +{
  99532. + int ret = 0;
  99533. +
  99534. + snd_rpi_hifiberry_dac.dev = &pdev->dev;
  99535. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dac);
  99536. + if (ret)
  99537. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  99538. +
  99539. + return ret;
  99540. +}
  99541. +
  99542. +static int snd_rpi_hifiberry_dac_remove(struct platform_device *pdev)
  99543. +{
  99544. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dac);
  99545. +}
  99546. +
  99547. +static struct platform_driver snd_rpi_hifiberry_dac_driver = {
  99548. + .driver = {
  99549. + .name = "snd-hifiberry-dac",
  99550. + .owner = THIS_MODULE,
  99551. + },
  99552. + .probe = snd_rpi_hifiberry_dac_probe,
  99553. + .remove = snd_rpi_hifiberry_dac_remove,
  99554. +};
  99555. +
  99556. +module_platform_driver(snd_rpi_hifiberry_dac_driver);
  99557. +
  99558. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  99559. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry DAC");
  99560. +MODULE_LICENSE("GPL v2");
  99561. diff -Nur linux-3.13.6/sound/soc/bcm/hifiberry_digi.c linux-raspberry-pi/sound/soc/bcm/hifiberry_digi.c
  99562. --- linux-3.13.6/sound/soc/bcm/hifiberry_digi.c 1970-01-01 01:00:00.000000000 +0100
  99563. +++ linux-raspberry-pi/sound/soc/bcm/hifiberry_digi.c 2014-03-11 16:55:50.000000000 +0100
  99564. @@ -0,0 +1,153 @@
  99565. +/*
  99566. + * ASoC Driver for HifiBerry Digi
  99567. + *
  99568. + * Author: Daniel Matuschek <info@crazy-audio.com>
  99569. + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
  99570. + * Copyright 2013
  99571. + *
  99572. + * This program is free software; you can redistribute it and/or
  99573. + * modify it under the terms of the GNU General Public License
  99574. + * version 2 as published by the Free Software Foundation.
  99575. + *
  99576. + * This program is distributed in the hope that it will be useful, but
  99577. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  99578. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  99579. + * General Public License for more details.
  99580. + */
  99581. +
  99582. +#include <linux/module.h>
  99583. +#include <linux/platform_device.h>
  99584. +
  99585. +#include <sound/core.h>
  99586. +#include <sound/pcm.h>
  99587. +#include <sound/pcm_params.h>
  99588. +#include <sound/soc.h>
  99589. +#include <sound/jack.h>
  99590. +
  99591. +#include "../codecs/wm8804.h"
  99592. +
  99593. +static int samplerate=44100;
  99594. +
  99595. +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
  99596. +{
  99597. + struct snd_soc_codec *codec = rtd->codec;
  99598. +
  99599. + /* enable TX output */
  99600. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  99601. +
  99602. + return 0;
  99603. +}
  99604. +
  99605. +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
  99606. + struct snd_pcm_hw_params *params)
  99607. +{
  99608. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  99609. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  99610. + struct snd_soc_codec *codec = rtd->codec;
  99611. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  99612. +
  99613. + int sysclk = 27000000; /* This is fixed on this board */
  99614. +
  99615. + long mclk_freq=0;
  99616. + int mclk_div=1;
  99617. +
  99618. + int ret;
  99619. +
  99620. + samplerate = params_rate(params);
  99621. +
  99622. + switch (samplerate) {
  99623. + case 44100:
  99624. + case 48000:
  99625. + case 88200:
  99626. + case 96000:
  99627. + mclk_freq=samplerate*256;
  99628. + mclk_div=WM8804_MCLKDIV_256FS;
  99629. + break;
  99630. + case 176400:
  99631. + case 192000:
  99632. + mclk_freq=samplerate*128;
  99633. + mclk_div=WM8804_MCLKDIV_128FS;
  99634. + break;
  99635. + default:
  99636. + dev_err(substream->pcm->dev,
  99637. + "Failed to set WM8804 SYSCLK, unsupported samplerate\n");
  99638. + }
  99639. +
  99640. + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
  99641. + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
  99642. +
  99643. + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
  99644. + sysclk, SND_SOC_CLOCK_OUT);
  99645. + if (ret < 0) {
  99646. + dev_err(substream->pcm->dev,
  99647. + "Failed to set WM8804 SYSCLK: %d\n", ret);
  99648. + return ret;
  99649. + }
  99650. +
  99651. + /* Enable TX output */
  99652. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  99653. +
  99654. + /* Power on */
  99655. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
  99656. +
  99657. + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
  99658. +}
  99659. +
  99660. +/* machine stream operations */
  99661. +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
  99662. + .hw_params = snd_rpi_hifiberry_digi_hw_params,
  99663. +};
  99664. +
  99665. +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
  99666. +{
  99667. + .name = "HifiBerry Digi",
  99668. + .stream_name = "HifiBerry Digi HiFi",
  99669. + .cpu_dai_name = "bcm2708-i2s.0",
  99670. + .codec_dai_name = "wm8804-spdif",
  99671. + .platform_name = "bcm2708-i2s.0",
  99672. + .codec_name = "wm8804.1-003b",
  99673. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  99674. + SND_SOC_DAIFMT_CBM_CFM,
  99675. + .ops = &snd_rpi_hifiberry_digi_ops,
  99676. + .init = snd_rpi_hifiberry_digi_init,
  99677. +},
  99678. +};
  99679. +
  99680. +/* audio machine driver */
  99681. +static struct snd_soc_card snd_rpi_hifiberry_digi = {
  99682. + .name = "snd_rpi_hifiberry_digi",
  99683. + .dai_link = snd_rpi_hifiberry_digi_dai,
  99684. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
  99685. +};
  99686. +
  99687. +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
  99688. +{
  99689. + int ret = 0;
  99690. +
  99691. + snd_rpi_hifiberry_digi.dev = &pdev->dev;
  99692. + ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
  99693. + if (ret)
  99694. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  99695. +
  99696. + return ret;
  99697. +}
  99698. +
  99699. +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
  99700. +{
  99701. + return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
  99702. +}
  99703. +
  99704. +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
  99705. + .driver = {
  99706. + .name = "snd-hifiberry-digi",
  99707. + .owner = THIS_MODULE,
  99708. + },
  99709. + .probe = snd_rpi_hifiberry_digi_probe,
  99710. + .remove = snd_rpi_hifiberry_digi_remove,
  99711. +};
  99712. +
  99713. +module_platform_driver(snd_rpi_hifiberry_digi_driver);
  99714. +
  99715. +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
  99716. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
  99717. +MODULE_LICENSE("GPL v2");
  99718. diff -Nur linux-3.13.6/sound/soc/bcm/Kconfig linux-raspberry-pi/sound/soc/bcm/Kconfig
  99719. --- linux-3.13.6/sound/soc/bcm/Kconfig 1970-01-01 01:00:00.000000000 +0100
  99720. +++ linux-raspberry-pi/sound/soc/bcm/Kconfig 2014-03-11 16:57:48.000000000 +0100
  99721. @@ -0,0 +1,73 @@
  99722. +config SND_BCM2708_SOC_I2S
  99723. + tristate "SoC Audio support for the Broadcom BCM2708 I2S module"
  99724. + depends on MACH_BCM2708
  99725. + select REGMAP_MMIO
  99726. + select SND_SOC_DMAENGINE_PCM
  99727. + select SND_SOC_GENERIC_DMAENGINE_PCM
  99728. + help
  99729. + Say Y or M if you want to add support for codecs attached to
  99730. + the BCM2708 I2S interface. You will also need
  99731. + to select the audio interfaces to support below.
  99732. +
  99733. +config SND_BCM2708_SOC_HIFIBERRY_DAC
  99734. + tristate "Support for HifiBerry DAC"
  99735. + depends on SND_BCM2708_SOC_I2S
  99736. + select SND_SOC_PCM5102A
  99737. + help
  99738. + Say Y or M if you want to add support for HifiBerry DAC.
  99739. +
  99740. +config SND_BCM2708_SOC_HIFIBERRY_DIGI
  99741. + tristate "Support for HifiBerry Digi"
  99742. + depends on SND_BCM2708_SOC_I2S
  99743. + select SND_SOC_WM8804
  99744. + help
  99745. + Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
  99746. +
  99747. +config SND_BCM2708_SOC_RPI_DAC
  99748. + tristate "Support for RPi-DAC"
  99749. + depends on SND_BCM2708_SOC_I2S
  99750. + select SND_SOC_PCM1794A
  99751. + help
  99752. + Say Y or M if you want to add support for RPi-DAC.
  99753. +
  99754. +config SND_BCM2708_SOC_HIFIBERRY_MINI
  99755. + tristate "Support for HifiBerry Mini"
  99756. + depends on SND_BCM2708_SOC_I2S
  99757. + select SND_SOC_PCM5102A
  99758. + help
  99759. + Say Y or M if you want to add support for HifiBerry Mini.
  99760. +
  99761. +config SND_BCM2708_SOC_RPI_CODEC_MBED
  99762. + tristate "Support for AudioCODEC for mbed (TLV320AIC32B)"
  99763. + depends on SND_BCM2708_SOC_I2S
  99764. + select SND_SOC_TLV320AIC3X
  99765. + help
  99766. + Say Y if you want to add support for AudioCODEC for mbed (TLV320AIC32B)
  99767. +
  99768. +config SND_BCM2708_SOC_RPI_CODEC_TDA1541A
  99769. + tristate "Support for TDA1541A"
  99770. + depends on SND_BCM2708_SOC_I2S
  99771. + select SND_SOC_TDA1541A
  99772. + help
  99773. + Say Y if you want to add support for TDA1541A
  99774. +
  99775. +config SND_BCM2708_SOC_RPI_CODEC_PROTO
  99776. + tristate "Support for Audio Codec Board - PROTO (WM8731)"
  99777. + depends on SND_BCM2708_SOC_I2S
  99778. + select SND_SOC_WM8731
  99779. + help
  99780. + Say Y if you want to add support for Audio Codec Board - PROTO (WM8731)
  99781. +
  99782. +config SND_BCM2708_SOC_RPI_CODEC_ESS9018
  99783. + tristate "Support for ESS9018"
  99784. + depends on SND_BCM2708_SOC_I2S
  99785. + select SND_SOC_ESS9018
  99786. + help
  99787. + Say Y if you want to add support for ESS9018
  99788. +
  99789. +config SND_BCM2708_SOC_RPI_CODEC_PCM5102A
  99790. + tristate "Support for PCM5102A"
  99791. + depends on SND_BCM2708_SOC_I2S
  99792. + select SND_SOC_PCM5102A
  99793. + help
  99794. + Say Y if you want to add support for PCM5102A
  99795. diff -Nur linux-3.13.6/sound/soc/bcm/Makefile linux-raspberry-pi/sound/soc/bcm/Makefile
  99796. --- linux-3.13.6/sound/soc/bcm/Makefile 1970-01-01 01:00:00.000000000 +0100
  99797. +++ linux-raspberry-pi/sound/soc/bcm/Makefile 2014-03-11 16:55:50.000000000 +0100
  99798. @@ -0,0 +1,23 @@
  99799. +# BCM2708 Platform Support
  99800. +snd-soc-bcm2708-i2s-objs := bcm2708-i2s.o
  99801. +
  99802. +obj-$(CONFIG_SND_BCM2708_SOC_I2S) += snd-soc-bcm2708-i2s.o
  99803. +
  99804. +# BCM2708 Machine Support
  99805. +snd-soc-hifiberry-dac-objs := hifiberry_dac.o
  99806. +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
  99807. +snd-soc-rpi-dac-objs := rpi-dac.o
  99808. +snd-soc-rpi-mbed-objs := rpi-mbed.o
  99809. +snd-soc-rpi-tda1541a-objs := rpi-tda1541a.o
  99810. +snd-soc-rpi-proto-objs := rpi-proto.o
  99811. +snd-soc-rpi-ess9018-objs := rpi-ess9018.o
  99812. +snd-soc-rpi-pcm5102a-objs := rpi-pcm5102a.o
  99813. +
  99814. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
  99815. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
  99816. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
  99817. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_CODEC_MBED) += snd-soc-rpi-mbed.o
  99818. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_CODEC_TDA1541A) += snd-soc-rpi-tda1541a.o
  99819. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_CODEC_PROTO) += snd-soc-rpi-proto.o
  99820. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_CODEC_ESS9018) += snd-soc-rpi-ess9018.o
  99821. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_CODEC_PCM5102A) += snd-soc-rpi-pcm5102a.o
  99822. diff -Nur linux-3.13.6/sound/soc/bcm/rpi-cs534x.c linux-raspberry-pi/sound/soc/bcm/rpi-cs534x.c
  99823. --- linux-3.13.6/sound/soc/bcm/rpi-cs534x.c 1970-01-01 01:00:00.000000000 +0100
  99824. +++ linux-raspberry-pi/sound/soc/bcm/rpi-cs534x.c 2014-03-11 16:55:50.000000000 +0100
  99825. @@ -0,0 +1,105 @@
  99826. +/*
  99827. + * ASoC driver for CS5343/CS5344 ADC
  99828. + * connected to a Raspberry Pi
  99829. + *
  99830. + * Author: Wojciech M. Zabolotny <wzab01@gmail.com>
  99831. + * Based on rpi-ess9018.c by: Florian Meier, <koalo@koalo.de>
  99832. + * Copyright 2013
  99833. + *
  99834. + * This program is free software; you can redistribute it and/or modify
  99835. + * it under the terms of the GNU General Public License version 2 as
  99836. + * published by the Free Software Foundation.
  99837. + */
  99838. +
  99839. +#include <linux/module.h>
  99840. +#include <linux/platform_device.h>
  99841. +
  99842. +#include <sound/core.h>
  99843. +#include <sound/pcm.h>
  99844. +#include <sound/soc.h>
  99845. +#include <sound/jack.h>
  99846. +
  99847. +static int snd_rpi_cs534x_init(struct snd_soc_pcm_runtime *rtd)
  99848. +{
  99849. + return 0;
  99850. +}
  99851. +
  99852. +static int snd_rpi_cs534x_hw_params(struct snd_pcm_substream *substream,
  99853. + struct snd_pcm_hw_params *params)
  99854. +{
  99855. + return 0;
  99856. +}
  99857. +
  99858. +/* machine stream operations */
  99859. +static struct snd_soc_ops snd_rpi_cs534x_ops = {
  99860. + .hw_params = snd_rpi_cs534x_hw_params,
  99861. +};
  99862. +
  99863. +static struct snd_soc_dai_link snd_rpi_cs534x_dai[] = {
  99864. +{
  99865. + .name = "cs5343",
  99866. + .stream_name = "cs5343 HiFi",
  99867. + .cpu_dai_name = "bcm2708-i2s.0",
  99868. + .codec_dai_name = "cs534x-hifi",
  99869. + .platform_name = "bcm2708-pcm-audio.0",
  99870. + .codec_name = "cs534x-codec",
  99871. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  99872. + SND_SOC_DAIFMT_CBM_CFM,
  99873. + .ops = &snd_rpi_cs534x_ops,
  99874. + .init = snd_rpi_cs534x_init,
  99875. +},
  99876. +{
  99877. + .name = "cs5344",
  99878. + .stream_name = "cs5344 HiFi",
  99879. + .cpu_dai_name = "bcm2708-i2s.0",
  99880. + .codec_dai_name = "cs534x-hifi",
  99881. + .platform_name = "bcm2708-pcm-audio.0",
  99882. + .codec_name = "cs534x-codec",
  99883. + .dai_fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF |
  99884. + SND_SOC_DAIFMT_CBM_CFM,
  99885. + .ops = &snd_rpi_cs534x_ops,
  99886. + .init = snd_rpi_cs534x_init,
  99887. +},
  99888. +};
  99889. +
  99890. +/* audio machine driver */
  99891. +static struct snd_soc_card snd_rpi_cs534x = {
  99892. + .name = "snd_rpi_cs534x",
  99893. + .dai_link = snd_rpi_cs534x_dai,
  99894. + .num_links = ARRAY_SIZE(snd_rpi_cs534x_dai),
  99895. +};
  99896. +
  99897. +static int snd_rpi_cs534x_probe(struct platform_device *pdev)
  99898. +{
  99899. + int ret = 0;
  99900. +
  99901. + snd_rpi_cs534x.dev = &pdev->dev;
  99902. + ret = snd_soc_register_card(&snd_rpi_cs534x);
  99903. + if (ret)
  99904. + {
  99905. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  99906. + }
  99907. +
  99908. + return ret;
  99909. +}
  99910. +
  99911. +
  99912. +static int snd_rpi_cs534x_remove(struct platform_device *pdev)
  99913. +{
  99914. + return snd_soc_unregister_card(&snd_rpi_cs534x);
  99915. +}
  99916. +
  99917. +static struct platform_driver snd_rpi_cs534x_driver = {
  99918. + .driver = {
  99919. + .name = "snd-rpi-cs534x",
  99920. + .owner = THIS_MODULE,
  99921. + },
  99922. + .probe = snd_rpi_cs534x_probe,
  99923. + .remove = snd_rpi_cs534x_remove,
  99924. +};
  99925. +
  99926. +module_platform_driver(snd_rpi_cs534x_driver);
  99927. +
  99928. +MODULE_AUTHOR("Wojciech M. Zabolotny");
  99929. +MODULE_DESCRIPTION("ASoC Driver for Raspberry Pi connected to a cs534x");
  99930. +MODULE_LICENSE("GPL");
  99931. diff -Nur linux-3.13.6/sound/soc/bcm/rpi-dac.c linux-raspberry-pi/sound/soc/bcm/rpi-dac.c
  99932. --- linux-3.13.6/sound/soc/bcm/rpi-dac.c 1970-01-01 01:00:00.000000000 +0100
  99933. +++ linux-raspberry-pi/sound/soc/bcm/rpi-dac.c 2014-03-11 16:53:24.000000000 +0100
  99934. @@ -0,0 +1,97 @@
  99935. +/*
  99936. + * ASoC Driver for RPi-DAC.
  99937. + *
  99938. + * Author: Florian Meier <florian.meier@koalo.de>
  99939. + * Copyright 2013
  99940. + *
  99941. + * This program is free software; you can redistribute it and/or
  99942. + * modify it under the terms of the GNU General Public License
  99943. + * version 2 as published by the Free Software Foundation.
  99944. + *
  99945. + * This program is distributed in the hope that it will be useful, but
  99946. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  99947. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  99948. + * General Public License for more details.
  99949. + */
  99950. +
  99951. +#include <linux/module.h>
  99952. +#include <linux/platform_device.h>
  99953. +
  99954. +#include <sound/core.h>
  99955. +#include <sound/pcm.h>
  99956. +#include <sound/pcm_params.h>
  99957. +#include <sound/soc.h>
  99958. +#include <sound/jack.h>
  99959. +
  99960. +static int snd_rpi_rpi_dac_init(struct snd_soc_pcm_runtime *rtd)
  99961. +{
  99962. + return 0;
  99963. +}
  99964. +
  99965. +static int snd_rpi_rpi_dac_hw_params(struct snd_pcm_substream *substream,
  99966. + struct snd_pcm_hw_params *params)
  99967. +{
  99968. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  99969. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  99970. +
  99971. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 32*2);
  99972. +}
  99973. +
  99974. +/* machine stream operations */
  99975. +static struct snd_soc_ops snd_rpi_rpi_dac_ops = {
  99976. + .hw_params = snd_rpi_rpi_dac_hw_params,
  99977. +};
  99978. +
  99979. +static struct snd_soc_dai_link snd_rpi_rpi_dac_dai[] = {
  99980. +{
  99981. + .name = "HifiBerry Mini",
  99982. + .stream_name = "HifiBerry Mini HiFi",
  99983. + .cpu_dai_name = "bcm2708-i2s.0",
  99984. + .codec_dai_name = "pcm1794a-hifi",
  99985. + .platform_name = "bcm2708-i2s.0",
  99986. + .codec_name = "pcm1794a-codec",
  99987. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  99988. + SND_SOC_DAIFMT_CBS_CFS,
  99989. + .ops = &snd_rpi_rpi_dac_ops,
  99990. + .init = snd_rpi_rpi_dac_init,
  99991. +},
  99992. +};
  99993. +
  99994. +/* audio machine driver */
  99995. +static struct snd_soc_card snd_rpi_rpi_dac = {
  99996. + .name = "snd_rpi_rpi_dac",
  99997. + .dai_link = snd_rpi_rpi_dac_dai,
  99998. + .num_links = ARRAY_SIZE(snd_rpi_rpi_dac_dai),
  99999. +};
  100000. +
  100001. +static int snd_rpi_rpi_dac_probe(struct platform_device *pdev)
  100002. +{
  100003. + int ret = 0;
  100004. +
  100005. + snd_rpi_rpi_dac.dev = &pdev->dev;
  100006. + ret = snd_soc_register_card(&snd_rpi_rpi_dac);
  100007. + if (ret)
  100008. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100009. +
  100010. + return ret;
  100011. +}
  100012. +
  100013. +static int snd_rpi_rpi_dac_remove(struct platform_device *pdev)
  100014. +{
  100015. + return snd_soc_unregister_card(&snd_rpi_rpi_dac);
  100016. +}
  100017. +
  100018. +static struct platform_driver snd_rpi_rpi_dac_driver = {
  100019. + .driver = {
  100020. + .name = "snd-rpi-dac",
  100021. + .owner = THIS_MODULE,
  100022. + },
  100023. + .probe = snd_rpi_rpi_dac_probe,
  100024. + .remove = snd_rpi_rpi_dac_remove,
  100025. +};
  100026. +
  100027. +module_platform_driver(snd_rpi_rpi_dac_driver);
  100028. +
  100029. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100030. +MODULE_DESCRIPTION("ASoC Driver for RPi-DAC");
  100031. +MODULE_LICENSE("GPL v2");
  100032. diff -Nur linux-3.13.6/sound/soc/bcm/rpi-ess9018.c linux-raspberry-pi/sound/soc/bcm/rpi-ess9018.c
  100033. --- linux-3.13.6/sound/soc/bcm/rpi-ess9018.c 1970-01-01 01:00:00.000000000 +0100
  100034. +++ linux-raspberry-pi/sound/soc/bcm/rpi-ess9018.c 2014-03-11 16:55:50.000000000 +0100
  100035. @@ -0,0 +1,92 @@
  100036. +/*
  100037. + * ASoC driver for ESS9018 codec
  100038. + * connected to a Raspberry Pi
  100039. + *
  100040. + * Author: Florian Meier, <koalo@koalo.de>
  100041. + * Copyright 2013
  100042. + *
  100043. + * This program is free software; you can redistribute it and/or modify
  100044. + * it under the terms of the GNU General Public License version 2 as
  100045. + * published by the Free Software Foundation.
  100046. + */
  100047. +
  100048. +#include <linux/module.h>
  100049. +#include <linux/platform_device.h>
  100050. +
  100051. +#include <sound/core.h>
  100052. +#include <sound/pcm.h>
  100053. +#include <sound/soc.h>
  100054. +#include <sound/jack.h>
  100055. +
  100056. +static int snd_rpi_ess9018_init(struct snd_soc_pcm_runtime *rtd)
  100057. +{
  100058. + return 0;
  100059. +}
  100060. +
  100061. +static int snd_rpi_ess9018_hw_params(struct snd_pcm_substream *substream,
  100062. + struct snd_pcm_hw_params *params)
  100063. +{
  100064. + return 0;
  100065. +}
  100066. +
  100067. +/* machine stream operations */
  100068. +static struct snd_soc_ops snd_rpi_ess9018_ops = {
  100069. + .hw_params = snd_rpi_ess9018_hw_params,
  100070. +};
  100071. +
  100072. +static struct snd_soc_dai_link snd_rpi_ess9018_dai[] = {
  100073. +{
  100074. + .name = "ESS9018",
  100075. + .stream_name = "ESS9018 HiFi",
  100076. + .cpu_dai_name = "bcm2708-i2s.0",
  100077. + .codec_dai_name = "ess9018-hifi",
  100078. + .platform_name = "bcm2708-i2s.0",
  100079. + .codec_name = "ess9018-codec",
  100080. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100081. + SND_SOC_DAIFMT_CBS_CFS,
  100082. + .ops = &snd_rpi_ess9018_ops,
  100083. + .init = snd_rpi_ess9018_init,
  100084. +},
  100085. +};
  100086. +
  100087. +/* audio machine driver */
  100088. +static struct snd_soc_card snd_rpi_ess9018 = {
  100089. + .name = "snd_rpi_ess9018",
  100090. + .dai_link = snd_rpi_ess9018_dai,
  100091. + .num_links = ARRAY_SIZE(snd_rpi_ess9018_dai),
  100092. +};
  100093. +
  100094. +static int snd_rpi_ess9018_probe(struct platform_device *pdev)
  100095. +{
  100096. + int ret = 0;
  100097. +
  100098. + snd_rpi_ess9018.dev = &pdev->dev;
  100099. + ret = snd_soc_register_card(&snd_rpi_ess9018);
  100100. + if (ret)
  100101. + {
  100102. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100103. + }
  100104. +
  100105. + return ret;
  100106. +}
  100107. +
  100108. +
  100109. +static int snd_rpi_ess9018_remove(struct platform_device *pdev)
  100110. +{
  100111. + return snd_soc_unregister_card(&snd_rpi_ess9018);
  100112. +}
  100113. +
  100114. +static struct platform_driver snd_rpi_ess9018_driver = {
  100115. + .driver = {
  100116. + .name = "snd-rpi-ess9018",
  100117. + .owner = THIS_MODULE,
  100118. + },
  100119. + .probe = snd_rpi_ess9018_probe,
  100120. + .remove = snd_rpi_ess9018_remove,
  100121. +};
  100122. +
  100123. +module_platform_driver(snd_rpi_ess9018_driver);
  100124. +
  100125. +MODULE_AUTHOR("Florian Meier");
  100126. +MODULE_DESCRIPTION("ASoC Driver for Raspberry Pi connected to a ESS9018");
  100127. +MODULE_LICENSE("GPL");
  100128. diff -Nur linux-3.13.6/sound/soc/bcm/rpi-mbed.c linux-raspberry-pi/sound/soc/bcm/rpi-mbed.c
  100129. --- linux-3.13.6/sound/soc/bcm/rpi-mbed.c 1970-01-01 01:00:00.000000000 +0100
  100130. +++ linux-raspberry-pi/sound/soc/bcm/rpi-mbed.c 2014-03-11 16:55:50.000000000 +0100
  100131. @@ -0,0 +1,103 @@
  100132. +/*
  100133. + * ASoC driver for mbed AudioCODEC (with a TLV320AIC23b)
  100134. + * connected to a Raspberry Pi
  100135. + *
  100136. + * Author: Florian Meier, <koalo@koalo.de>
  100137. + * Copyright 2013
  100138. + *
  100139. + * This program is free software; you can redistribute it and/or modify
  100140. + * it under the terms of the GNU General Public License version 2 as
  100141. + * published by the Free Software Foundation.
  100142. + */
  100143. +
  100144. +#include <linux/module.h>
  100145. +#include <linux/platform_device.h>
  100146. +
  100147. +#include <sound/core.h>
  100148. +#include <sound/pcm.h>
  100149. +#include <sound/soc.h>
  100150. +#include <sound/jack.h>
  100151. +
  100152. +#include "../codecs/tlv320aic23.h"
  100153. +
  100154. +static int snd_rpi_mbed_init(struct snd_soc_pcm_runtime *rtd)
  100155. +{
  100156. + return 0;
  100157. +}
  100158. +
  100159. +static int snd_rpi_mbed_hw_params(struct snd_pcm_substream *substream,
  100160. + struct snd_pcm_hw_params *params)
  100161. +{
  100162. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100163. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  100164. + int sysclk;
  100165. +
  100166. + sysclk = 12000000; /* this is fixed on this board */
  100167. +
  100168. + /* set tlv320aic23 sysclk */
  100169. + snd_soc_dai_set_sysclk(codec_dai, 0, sysclk, 0);
  100170. +
  100171. + return 0;
  100172. +}
  100173. +
  100174. +/* machine stream operations */
  100175. +static struct snd_soc_ops snd_rpi_mbed_ops = {
  100176. + .hw_params = snd_rpi_mbed_hw_params,
  100177. +};
  100178. +
  100179. +static struct snd_soc_dai_link snd_rpi_mbed_dai[] = {
  100180. +{
  100181. + .name = "TLV320AIC23",
  100182. + .stream_name = "TLV320AIC23 HiFi",
  100183. + .cpu_dai_name = "bcm2708-i2s.0",
  100184. + .codec_dai_name = "tlv320aic23-hifi",
  100185. + .platform_name = "bcm2708-i2s.0",
  100186. + .codec_name = "tlv320aic23-codec.1-001b",
  100187. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100188. + SND_SOC_DAIFMT_CBS_CFS,
  100189. + .ops = &snd_rpi_mbed_ops,
  100190. + .init = snd_rpi_mbed_init,
  100191. +},
  100192. +};
  100193. +
  100194. +/* audio machine driver */
  100195. +static struct snd_soc_card snd_rpi_mbed = {
  100196. + .name = "snd_rpi_mbed",
  100197. + .dai_link = snd_rpi_mbed_dai,
  100198. + .num_links = ARRAY_SIZE(snd_rpi_mbed_dai),
  100199. +};
  100200. +
  100201. +static int snd_rpi_mbed_probe(struct platform_device *pdev)
  100202. +{
  100203. + int ret = 0;
  100204. +
  100205. + snd_rpi_mbed.dev = &pdev->dev;
  100206. + ret = snd_soc_register_card(&snd_rpi_mbed);
  100207. + if (ret) {
  100208. + dev_err(&pdev->dev,
  100209. + "snd_soc_register_card() failed: %d\n", ret);
  100210. + }
  100211. +
  100212. + return ret;
  100213. +}
  100214. +
  100215. +
  100216. +static int snd_rpi_mbed_remove(struct platform_device *pdev)
  100217. +{
  100218. + return snd_soc_unregister_card(&snd_rpi_mbed);
  100219. +}
  100220. +
  100221. +static struct platform_driver snd_rpi_mbed_driver = {
  100222. + .driver = {
  100223. + .name = "snd-rpi-mbed",
  100224. + .owner = THIS_MODULE,
  100225. + },
  100226. + .probe = snd_rpi_mbed_probe,
  100227. + .remove = snd_rpi_mbed_remove,
  100228. +};
  100229. +
  100230. +module_platform_driver(snd_rpi_mbed_driver);
  100231. +
  100232. +MODULE_AUTHOR("Florian Meier");
  100233. +MODULE_DESCRIPTION("ASoC Driver for Raspberry Pi connected to mbed AudioCODEC");
  100234. +MODULE_LICENSE("GPL");
  100235. diff -Nur linux-3.13.6/sound/soc/bcm/rpi-pcm5102a.c linux-raspberry-pi/sound/soc/bcm/rpi-pcm5102a.c
  100236. --- linux-3.13.6/sound/soc/bcm/rpi-pcm5102a.c 1970-01-01 01:00:00.000000000 +0100
  100237. +++ linux-raspberry-pi/sound/soc/bcm/rpi-pcm5102a.c 2014-03-11 16:55:50.000000000 +0100
  100238. @@ -0,0 +1,93 @@
  100239. +/*
  100240. + * ASoC driver for PCM5102A codec
  100241. + * connected to a Raspberry Pi
  100242. + *
  100243. + * Author: Francesco Valla, <valla.francesco@gmail.com>
  100244. + * Based on rpi-ess9018.c by: Florian Meier, <koalo@koalo.de>
  100245. + * Copyright 2013
  100246. + *
  100247. + * This program is free software; you can redistribute it and/or modify
  100248. + * it under the terms of the GNU General Public License version 2 as
  100249. + * published by the Free Software Foundation.
  100250. + */
  100251. +
  100252. +#include <linux/module.h>
  100253. +#include <linux/platform_device.h>
  100254. +
  100255. +#include <sound/core.h>
  100256. +#include <sound/pcm.h>
  100257. +#include <sound/soc.h>
  100258. +#include <sound/jack.h>
  100259. +
  100260. +static int snd_rpi_pcm5102a_init(struct snd_soc_pcm_runtime *rtd)
  100261. +{
  100262. + return 0;
  100263. +}
  100264. +
  100265. +static int snd_rpi_pcm5102a_hw_params(struct snd_pcm_substream *substream,
  100266. + struct snd_pcm_hw_params *params)
  100267. +{
  100268. + return 0;
  100269. +}
  100270. +
  100271. +/* machine stream operations */
  100272. +static struct snd_soc_ops snd_rpi_pcm5102a_ops = {
  100273. + .hw_params = snd_rpi_pcm5102a_hw_params,
  100274. +};
  100275. +
  100276. +static struct snd_soc_dai_link snd_rpi_pcm5102a_dai[] = {
  100277. +{
  100278. + .name = "PCM5102A",
  100279. + .stream_name = "PCM5102A HiFi",
  100280. + .cpu_dai_name = "bcm2708-i2s.0",
  100281. + .codec_dai_name = "pcm5102a-hifi",
  100282. + .platform_name = "bcm2708-pcm-audio.0",
  100283. + .codec_name = "pcm5102a-codec",
  100284. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100285. + SND_SOC_DAIFMT_CBS_CFS,
  100286. + .ops = &snd_rpi_pcm5102a_ops,
  100287. + .init = snd_rpi_pcm5102a_init,
  100288. +},
  100289. +};
  100290. +
  100291. +/* audio machine driver */
  100292. +static struct snd_soc_card snd_rpi_pcm5102a = {
  100293. + .name = "snd_rpi_pcm5102a",
  100294. + .dai_link = snd_rpi_pcm5102a_dai,
  100295. + .num_links = ARRAY_SIZE(snd_rpi_pcm5102a_dai),
  100296. +};
  100297. +
  100298. +static int snd_rpi_pcm5102a_probe(struct platform_device *pdev)
  100299. +{
  100300. + int ret = 0;
  100301. +
  100302. + snd_rpi_pcm5102a.dev = &pdev->dev;
  100303. + ret = snd_soc_register_card(&snd_rpi_pcm5102a);
  100304. + if (ret)
  100305. + {
  100306. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100307. + }
  100308. +
  100309. + return ret;
  100310. +}
  100311. +
  100312. +
  100313. +static int snd_rpi_pcm5102a_remove(struct platform_device *pdev)
  100314. +{
  100315. + return snd_soc_unregister_card(&snd_rpi_pcm5102a);
  100316. +}
  100317. +
  100318. +static struct platform_driver snd_rpi_pcm5102a_driver = {
  100319. + .driver = {
  100320. + .name = "snd-rpi-pcm5102a",
  100321. + .owner = THIS_MODULE,
  100322. + },
  100323. + .probe = snd_rpi_pcm5102a_probe,
  100324. + .remove = snd_rpi_pcm5102a_remove,
  100325. +};
  100326. +
  100327. +module_platform_driver(snd_rpi_pcm5102a_driver);
  100328. +
  100329. +MODULE_AUTHOR("Francesco Valla");
  100330. +MODULE_DESCRIPTION("ASoC Driver for Raspberry Pi connected to a PCM5102A");
  100331. +MODULE_LICENSE("GPL");
  100332. diff -Nur linux-3.13.6/sound/soc/bcm/rpi-proto.c linux-raspberry-pi/sound/soc/bcm/rpi-proto.c
  100333. --- linux-3.13.6/sound/soc/bcm/rpi-proto.c 1970-01-01 01:00:00.000000000 +0100
  100334. +++ linux-raspberry-pi/sound/soc/bcm/rpi-proto.c 2014-03-11 16:55:50.000000000 +0100
  100335. @@ -0,0 +1,130 @@
  100336. +/*
  100337. + * ASoC driver for PROTO AudioCODEC (with a WM8731)
  100338. + * connected to a Raspberry Pi
  100339. + *
  100340. + * Author: Florian Meier, <koalo@koalo.de>
  100341. + * Copyright 2013
  100342. + *
  100343. + * This program is free software; you can redistribute it and/or modify
  100344. + * it under the terms of the GNU General Public License version 2 as
  100345. + * published by the Free Software Foundation.
  100346. + */
  100347. +
  100348. +#include <linux/module.h>
  100349. +#include <linux/platform_device.h>
  100350. +
  100351. +#include <sound/core.h>
  100352. +#include <sound/pcm.h>
  100353. +#include <sound/soc.h>
  100354. +#include <sound/jack.h>
  100355. +
  100356. +#include "../codecs/wm8731.h"
  100357. +
  100358. +static const unsigned int wm8731_rates_12288000[] = {
  100359. + 8000, 32000, 48000, 96000,
  100360. +};
  100361. +
  100362. +static struct snd_pcm_hw_constraint_list wm8731_constraints_12288000 = {
  100363. + .list = wm8731_rates_12288000,
  100364. + .count = ARRAY_SIZE(wm8731_rates_12288000),
  100365. +};
  100366. +
  100367. +static int snd_rpi_proto_startup(struct snd_pcm_substream *substream)
  100368. +{
  100369. + /* Setup constraints, because there is a 12.288 MHz XTAL on the board */
  100370. + snd_pcm_hw_constraint_list(substream->runtime, 0,
  100371. + SNDRV_PCM_HW_PARAM_RATE,
  100372. + &wm8731_constraints_12288000);
  100373. + return 0;
  100374. +}
  100375. +
  100376. +static int snd_rpi_proto_hw_params(struct snd_pcm_substream *substream,
  100377. + struct snd_pcm_hw_params *params)
  100378. +{
  100379. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  100380. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  100381. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  100382. + int sysclk = 12288000; /* This is fixed on this board */
  100383. +
  100384. + /* Set proto bclk */
  100385. + int ret = snd_soc_dai_set_bclk_ratio(cpu_dai,32*2);
  100386. + if (ret < 0){
  100387. + dev_err(substream->pcm->dev,
  100388. + "Failed to set WM8731 BCLK ratio %d\n", ret);
  100389. + return ret;
  100390. + }
  100391. +
  100392. + /* Set proto sysclk */
  100393. + ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL,
  100394. + sysclk, SND_SOC_CLOCK_IN);
  100395. + if (ret < 0) {
  100396. + dev_err(substream->pcm->dev,
  100397. + "Failed to set WM8731 SYSCLK: %d\n", ret);
  100398. + return ret;
  100399. + }
  100400. +
  100401. + return 0;
  100402. +}
  100403. +
  100404. +/* machine stream operations */
  100405. +static struct snd_soc_ops snd_rpi_proto_ops = {
  100406. + .startup = snd_rpi_proto_startup,
  100407. + .hw_params = snd_rpi_proto_hw_params,
  100408. +};
  100409. +
  100410. +static struct snd_soc_dai_link snd_rpi_proto_dai[] = {
  100411. +{
  100412. + .name = "WM8731",
  100413. + .stream_name = "WM8731 HiFi",
  100414. + .cpu_dai_name = "bcm2708-i2s.0",
  100415. + .codec_dai_name = "wm8731-hifi",
  100416. + .platform_name = "bcm2708-i2s.0",
  100417. + .codec_name = "wm8731.1-001a",
  100418. + .dai_fmt = SND_SOC_DAIFMT_I2S
  100419. + | SND_SOC_DAIFMT_NB_NF
  100420. + | SND_SOC_DAIFMT_CBM_CFM,
  100421. + .ops = &snd_rpi_proto_ops,
  100422. +},
  100423. +};
  100424. +
  100425. +/* audio machine driver */
  100426. +static struct snd_soc_card snd_rpi_proto = {
  100427. + .name = "snd_rpi_proto",
  100428. + .dai_link = snd_rpi_proto_dai,
  100429. + .num_links = ARRAY_SIZE(snd_rpi_proto_dai),
  100430. +};
  100431. +
  100432. +static int snd_rpi_proto_probe(struct platform_device *pdev)
  100433. +{
  100434. + int ret = 0;
  100435. +
  100436. + snd_rpi_proto.dev = &pdev->dev;
  100437. + ret = snd_soc_register_card(&snd_rpi_proto);
  100438. + if (ret) {
  100439. + dev_err(&pdev->dev,
  100440. + "snd_soc_register_card() failed: %d\n", ret);
  100441. + }
  100442. +
  100443. + return ret;
  100444. +}
  100445. +
  100446. +
  100447. +static int snd_rpi_proto_remove(struct platform_device *pdev)
  100448. +{
  100449. + return snd_soc_unregister_card(&snd_rpi_proto);
  100450. +}
  100451. +
  100452. +static struct platform_driver snd_rpi_proto_driver = {
  100453. + .driver = {
  100454. + .name = "snd-rpi-proto",
  100455. + .owner = THIS_MODULE,
  100456. + },
  100457. + .probe = snd_rpi_proto_probe,
  100458. + .remove = snd_rpi_proto_remove,
  100459. +};
  100460. +
  100461. +module_platform_driver(snd_rpi_proto_driver);
  100462. +
  100463. +MODULE_AUTHOR("Florian Meier");
  100464. +MODULE_DESCRIPTION("ASoC Driver for Raspberry Pi connected to PROTO board (WM8731)");
  100465. +MODULE_LICENSE("GPL");
  100466. diff -Nur linux-3.13.6/sound/soc/bcm/rpi-tda1541a.c linux-raspberry-pi/sound/soc/bcm/rpi-tda1541a.c
  100467. --- linux-3.13.6/sound/soc/bcm/rpi-tda1541a.c 1970-01-01 01:00:00.000000000 +0100
  100468. +++ linux-raspberry-pi/sound/soc/bcm/rpi-tda1541a.c 2014-03-11 16:55:50.000000000 +0100
  100469. @@ -0,0 +1,92 @@
  100470. +/*
  100471. + * ASoC driver for TDA1541A codec
  100472. + * connected to a Raspberry Pi
  100473. + *
  100474. + * Author: Florian Meier, <koalo@koalo.de>
  100475. + * Copyright 2013
  100476. + *
  100477. + * This program is free software; you can redistribute it and/or modify
  100478. + * it under the terms of the GNU General Public License version 2 as
  100479. + * published by the Free Software Foundation.
  100480. + */
  100481. +
  100482. +#include <linux/module.h>
  100483. +#include <linux/platform_device.h>
  100484. +
  100485. +#include <sound/core.h>
  100486. +#include <sound/pcm.h>
  100487. +#include <sound/soc.h>
  100488. +#include <sound/jack.h>
  100489. +
  100490. +static int snd_rpi_tda1541a_init(struct snd_soc_pcm_runtime *rtd)
  100491. +{
  100492. + return 0;
  100493. +}
  100494. +
  100495. +static int snd_rpi_tda1541a_hw_params(struct snd_pcm_substream *substream,
  100496. + struct snd_pcm_hw_params *params)
  100497. +{
  100498. + return 0;
  100499. +}
  100500. +
  100501. +/* machine stream operations */
  100502. +static struct snd_soc_ops snd_rpi_tda1541a_ops = {
  100503. + .hw_params = snd_rpi_tda1541a_hw_params,
  100504. +};
  100505. +
  100506. +static struct snd_soc_dai_link snd_rpi_tda1541a_dai[] = {
  100507. +{
  100508. + .name = "TDA1541A",
  100509. + .stream_name = "TDA1541A HiFi",
  100510. + .cpu_dai_name = "bcm2708-i2s.0",
  100511. + .codec_dai_name = "tda1541a-hifi",
  100512. + .platform_name = "bcm2708-i2s.0",
  100513. + .codec_name = "tda1541a-codec",
  100514. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  100515. + SND_SOC_DAIFMT_CBS_CFS,
  100516. + .ops = &snd_rpi_tda1541a_ops,
  100517. + .init = snd_rpi_tda1541a_init,
  100518. +},
  100519. +};
  100520. +
  100521. +/* audio machine driver */
  100522. +static struct snd_soc_card snd_rpi_tda1541a = {
  100523. + .name = "snd_rpi_tda1541a",
  100524. + .dai_link = snd_rpi_tda1541a_dai,
  100525. + .num_links = ARRAY_SIZE(snd_rpi_tda1541a_dai),
  100526. +};
  100527. +
  100528. +static int snd_rpi_tda1541a_probe(struct platform_device *pdev)
  100529. +{
  100530. + int ret = 0;
  100531. +
  100532. + snd_rpi_tda1541a.dev = &pdev->dev;
  100533. + ret = snd_soc_register_card(&snd_rpi_tda1541a);
  100534. + if (ret)
  100535. + {
  100536. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  100537. + }
  100538. +
  100539. + return ret;
  100540. +}
  100541. +
  100542. +
  100543. +static int snd_rpi_tda1541a_remove(struct platform_device *pdev)
  100544. +{
  100545. + return snd_soc_unregister_card(&snd_rpi_tda1541a);
  100546. +}
  100547. +
  100548. +static struct platform_driver snd_rpi_tda1541a_driver = {
  100549. + .driver = {
  100550. + .name = "snd-rpi-tda1541a",
  100551. + .owner = THIS_MODULE,
  100552. + },
  100553. + .probe = snd_rpi_tda1541a_probe,
  100554. + .remove = snd_rpi_tda1541a_remove,
  100555. +};
  100556. +
  100557. +module_platform_driver(snd_rpi_tda1541a_driver);
  100558. +
  100559. +MODULE_AUTHOR("Florian Meier");
  100560. +MODULE_DESCRIPTION("ASoC Driver for Raspberry Pi connected to a TDA1541A");
  100561. +MODULE_LICENSE("GPL");
  100562. diff -Nur linux-3.13.6/sound/soc/codecs/Kconfig linux-raspberry-pi/sound/soc/codecs/Kconfig
  100563. --- linux-3.13.6/sound/soc/codecs/Kconfig 2014-03-07 07:07:02.000000000 +0100
  100564. +++ linux-raspberry-pi/sound/soc/codecs/Kconfig 2014-03-11 16:55:50.000000000 +0100
  100565. @@ -59,6 +59,8 @@
  100566. select SND_SOC_PCM1681 if I2C
  100567. select SND_SOC_PCM1792A if SPI_MASTER
  100568. select SND_SOC_PCM3008
  100569. + select SND_SOC_PCM1794A
  100570. + select SND_SOC_PCM5102A
  100571. select SND_SOC_RT5631 if I2C
  100572. select SND_SOC_RT5640 if I2C
  100573. select SND_SOC_SGTL5000 if I2C
  100574. @@ -311,6 +313,12 @@
  100575. config SND_SOC_PCM3008
  100576. tristate
  100577. +config SND_SOC_PCM1794A
  100578. + tristate
  100579. +
  100580. +config SND_SOC_PCM5102A
  100581. + tristate
  100582. +
  100583. config SND_SOC_RT5631
  100584. tristate
  100585. diff -Nur linux-3.13.6/sound/soc/codecs/Makefile linux-raspberry-pi/sound/soc/codecs/Makefile
  100586. --- linux-3.13.6/sound/soc/codecs/Makefile 2014-03-07 07:07:02.000000000 +0100
  100587. +++ linux-raspberry-pi/sound/soc/codecs/Makefile 2014-03-11 16:55:50.000000000 +0100
  100588. @@ -46,6 +46,8 @@
  100589. snd-soc-pcm1681-objs := pcm1681.o
  100590. snd-soc-pcm1792a-codec-objs := pcm1792a.o
  100591. snd-soc-pcm3008-objs := pcm3008.o
  100592. +snd-soc-pcm1794a-objs := pcm1794a.o
  100593. +snd-soc-pcm5102a-objs := pcm5102a.o
  100594. snd-soc-rt5631-objs := rt5631.o
  100595. snd-soc-rt5640-objs := rt5640.o
  100596. snd-soc-sgtl5000-objs := sgtl5000.o
  100597. @@ -179,6 +181,8 @@
  100598. obj-$(CONFIG_SND_SOC_PCM1681) += snd-soc-pcm1681.o
  100599. obj-$(CONFIG_SND_SOC_PCM1792A) += snd-soc-pcm1792a-codec.o
  100600. obj-$(CONFIG_SND_SOC_PCM3008) += snd-soc-pcm3008.o
  100601. +obj-$(CONFIG_SND_SOC_PCM1794A) += snd-soc-pcm1794a.o
  100602. +obj-$(CONFIG_SND_SOC_PCM5102A) += snd-soc-pcm5102a.o
  100603. obj-$(CONFIG_SND_SOC_RT5631) += snd-soc-rt5631.o
  100604. obj-$(CONFIG_SND_SOC_RT5640) += snd-soc-rt5640.o
  100605. obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o
  100606. diff -Nur linux-3.13.6/sound/soc/codecs/pcm1794a.c linux-raspberry-pi/sound/soc/codecs/pcm1794a.c
  100607. --- linux-3.13.6/sound/soc/codecs/pcm1794a.c 1970-01-01 01:00:00.000000000 +0100
  100608. +++ linux-raspberry-pi/sound/soc/codecs/pcm1794a.c 2014-03-11 16:53:24.000000000 +0100
  100609. @@ -0,0 +1,62 @@
  100610. +/*
  100611. + * Driver for the PCM1794A codec
  100612. + *
  100613. + * Author: Florian Meier <florian.meier@koalo.de>
  100614. + * Copyright 2013
  100615. + *
  100616. + * This program is free software; you can redistribute it and/or
  100617. + * modify it under the terms of the GNU General Public License
  100618. + * version 2 as published by the Free Software Foundation.
  100619. + *
  100620. + * This program is distributed in the hope that it will be useful, but
  100621. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100622. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100623. + * General Public License for more details.
  100624. + */
  100625. +
  100626. +
  100627. +#include <linux/init.h>
  100628. +#include <linux/module.h>
  100629. +#include <linux/platform_device.h>
  100630. +
  100631. +#include <sound/soc.h>
  100632. +
  100633. +static struct snd_soc_dai_driver pcm1794a_dai = {
  100634. + .name = "pcm1794a-hifi",
  100635. + .playback = {
  100636. + .channels_min = 2,
  100637. + .channels_max = 2,
  100638. + .rates = SNDRV_PCM_RATE_8000_192000,
  100639. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  100640. + SNDRV_PCM_FMTBIT_S24_LE
  100641. + },
  100642. +};
  100643. +
  100644. +static struct snd_soc_codec_driver soc_codec_dev_pcm1794a;
  100645. +
  100646. +static int pcm1794a_probe(struct platform_device *pdev)
  100647. +{
  100648. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm1794a,
  100649. + &pcm1794a_dai, 1);
  100650. +}
  100651. +
  100652. +static int pcm1794a_remove(struct platform_device *pdev)
  100653. +{
  100654. + snd_soc_unregister_codec(&pdev->dev);
  100655. + return 0;
  100656. +}
  100657. +
  100658. +static struct platform_driver pcm1794a_codec_driver = {
  100659. + .probe = pcm1794a_probe,
  100660. + .remove = pcm1794a_remove,
  100661. + .driver = {
  100662. + .name = "pcm1794a-codec",
  100663. + .owner = THIS_MODULE,
  100664. + },
  100665. +};
  100666. +
  100667. +module_platform_driver(pcm1794a_codec_driver);
  100668. +
  100669. +MODULE_DESCRIPTION("ASoC PCM1794A codec driver");
  100670. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100671. +MODULE_LICENSE("GPL v2");
  100672. diff -Nur linux-3.13.6/sound/soc/codecs/pcm5102a.c linux-raspberry-pi/sound/soc/codecs/pcm5102a.c
  100673. --- linux-3.13.6/sound/soc/codecs/pcm5102a.c 1970-01-01 01:00:00.000000000 +0100
  100674. +++ linux-raspberry-pi/sound/soc/codecs/pcm5102a.c 2014-03-11 16:55:50.000000000 +0100
  100675. @@ -0,0 +1,63 @@
  100676. +/*
  100677. + * Driver for the PCM5102A codec
  100678. + *
  100679. + * Author: Florian Meier <florian.meier@koalo.de>
  100680. + * Copyright 2013
  100681. + *
  100682. + * This program is free software; you can redistribute it and/or
  100683. + * modify it under the terms of the GNU General Public License
  100684. + * version 2 as published by the Free Software Foundation.
  100685. + *
  100686. + * This program is distributed in the hope that it will be useful, but
  100687. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  100688. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  100689. + * General Public License for more details.
  100690. + */
  100691. +
  100692. +
  100693. +#include <linux/init.h>
  100694. +#include <linux/module.h>
  100695. +#include <linux/platform_device.h>
  100696. +
  100697. +#include <sound/soc.h>
  100698. +
  100699. +static struct snd_soc_dai_driver pcm5102a_dai = {
  100700. + .name = "pcm5102a-hifi",
  100701. + .playback = {
  100702. + .channels_min = 2,
  100703. + .channels_max = 2,
  100704. + .rates = SNDRV_PCM_RATE_8000_192000,
  100705. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  100706. + SNDRV_PCM_FMTBIT_S24_LE |
  100707. + SNDRV_PCM_FMTBIT_S32_LE
  100708. + },
  100709. +};
  100710. +
  100711. +static struct snd_soc_codec_driver soc_codec_dev_pcm5102a;
  100712. +
  100713. +static int pcm5102a_probe(struct platform_device *pdev)
  100714. +{
  100715. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm5102a,
  100716. + &pcm5102a_dai, 1);
  100717. +}
  100718. +
  100719. +static int pcm5102a_remove(struct platform_device *pdev)
  100720. +{
  100721. + snd_soc_unregister_codec(&pdev->dev);
  100722. + return 0;
  100723. +}
  100724. +
  100725. +static struct platform_driver pcm5102a_codec_driver = {
  100726. + .probe = pcm5102a_probe,
  100727. + .remove = pcm5102a_remove,
  100728. + .driver = {
  100729. + .name = "pcm5102a-codec",
  100730. + .owner = THIS_MODULE,
  100731. + },
  100732. +};
  100733. +
  100734. +module_platform_driver(pcm5102a_codec_driver);
  100735. +
  100736. +MODULE_DESCRIPTION("ASoC PCM5102A codec driver");
  100737. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  100738. +MODULE_LICENSE("GPL v2");
  100739. diff -Nur linux-3.13.6/sound/soc/codecs/wm8804.c linux-raspberry-pi/sound/soc/codecs/wm8804.c
  100740. --- linux-3.13.6/sound/soc/codecs/wm8804.c 2014-03-07 07:07:02.000000000 +0100
  100741. +++ linux-raspberry-pi/sound/soc/codecs/wm8804.c 2014-03-11 16:53:24.000000000 +0100
  100742. @@ -63,6 +63,7 @@
  100743. struct regmap *regmap;
  100744. struct regulator_bulk_data supplies[WM8804_NUM_SUPPLIES];
  100745. struct notifier_block disable_nb[WM8804_NUM_SUPPLIES];
  100746. + int mclk_div;
  100747. };
  100748. static int txsrc_get(struct snd_kcontrol *kcontrol,
  100749. @@ -277,6 +278,7 @@
  100750. blen = 0x1;
  100751. break;
  100752. case SNDRV_PCM_FORMAT_S24_LE:
  100753. + case SNDRV_PCM_FORMAT_S32_LE:
  100754. blen = 0x2;
  100755. break;
  100756. default:
  100757. @@ -318,7 +320,7 @@
  100758. #define FIXED_PLL_SIZE ((1ULL << 22) * 10)
  100759. static int pll_factors(struct pll_div *pll_div, unsigned int target,
  100760. - unsigned int source)
  100761. + unsigned int source, unsigned int mclk_div)
  100762. {
  100763. u64 Kpart;
  100764. unsigned long int K, Ndiv, Nmod, tmp;
  100765. @@ -330,7 +332,8 @@
  100766. */
  100767. for (i = 0; i < ARRAY_SIZE(post_table); i++) {
  100768. tmp = target * post_table[i].div;
  100769. - if (tmp >= 90000000 && tmp <= 100000000) {
  100770. + if ((tmp >= 90000000 && tmp <= 100000000) &&
  100771. + (mclk_div == post_table[i].mclkdiv)) {
  100772. pll_div->freqmode = post_table[i].freqmode;
  100773. pll_div->mclkdiv = post_table[i].mclkdiv;
  100774. target *= post_table[i].div;
  100775. @@ -387,8 +390,11 @@
  100776. } else {
  100777. int ret;
  100778. struct pll_div pll_div;
  100779. + struct wm8804_priv *wm8804;
  100780. - ret = pll_factors(&pll_div, freq_out, freq_in);
  100781. + wm8804 = snd_soc_codec_get_drvdata(codec);
  100782. +
  100783. + ret = pll_factors(&pll_div, freq_out, freq_in, wm8804->mclk_div);
  100784. if (ret)
  100785. return ret;
  100786. @@ -452,6 +458,7 @@
  100787. int div_id, int div)
  100788. {
  100789. struct snd_soc_codec *codec;
  100790. + struct wm8804_priv *wm8804;
  100791. codec = dai->codec;
  100792. switch (div_id) {
  100793. @@ -459,6 +466,10 @@
  100794. snd_soc_update_bits(codec, WM8804_PLL5, 0x30,
  100795. (div & 0x3) << 4);
  100796. break;
  100797. + case WM8804_MCLK_DIV:
  100798. + wm8804 = snd_soc_codec_get_drvdata(codec);
  100799. + wm8804->mclk_div = div;
  100800. + break;
  100801. default:
  100802. dev_err(dai->dev, "Unknown clock divider: %d\n", div_id);
  100803. return -EINVAL;
  100804. @@ -641,7 +652,7 @@
  100805. };
  100806. #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  100807. - SNDRV_PCM_FMTBIT_S24_LE)
  100808. + SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  100809. #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  100810. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
  100811. @@ -674,7 +685,7 @@
  100812. .suspend = wm8804_suspend,
  100813. .resume = wm8804_resume,
  100814. .set_bias_level = wm8804_set_bias_level,
  100815. - .idle_bias_off = true,
  100816. + .idle_bias_off = false,
  100817. .controls = wm8804_snd_controls,
  100818. .num_controls = ARRAY_SIZE(wm8804_snd_controls),
  100819. diff -Nur linux-3.13.6/sound/soc/codecs/wm8804.h linux-raspberry-pi/sound/soc/codecs/wm8804.h
  100820. --- linux-3.13.6/sound/soc/codecs/wm8804.h 2014-03-07 07:07:02.000000000 +0100
  100821. +++ linux-raspberry-pi/sound/soc/codecs/wm8804.h 2014-03-11 16:53:24.000000000 +0100
  100822. @@ -57,5 +57,9 @@
  100823. #define WM8804_CLKOUT_SRC_OSCCLK 4
  100824. #define WM8804_CLKOUT_DIV 1
  100825. +#define WM8804_MCLK_DIV 2
  100826. +
  100827. +#define WM8804_MCLKDIV_256FS 0
  100828. +#define WM8804_MCLKDIV_128FS 1
  100829. #endif /* _WM8804_H */
  100830. diff -Nur linux-3.13.6/sound/soc/Kconfig linux-raspberry-pi/sound/soc/Kconfig
  100831. --- linux-3.13.6/sound/soc/Kconfig 2014-03-07 07:07:02.000000000 +0100
  100832. +++ linux-raspberry-pi/sound/soc/Kconfig 2014-03-11 16:55:50.000000000 +0100
  100833. @@ -33,6 +33,7 @@
  100834. # All the supported SoCs
  100835. source "sound/soc/atmel/Kconfig"
  100836. source "sound/soc/au1x/Kconfig"
  100837. +source "sound/soc/bcm/Kconfig"
  100838. source "sound/soc/blackfin/Kconfig"
  100839. source "sound/soc/cirrus/Kconfig"
  100840. source "sound/soc/davinci/Kconfig"
  100841. diff -Nur linux-3.13.6/sound/soc/Makefile linux-raspberry-pi/sound/soc/Makefile
  100842. --- linux-3.13.6/sound/soc/Makefile 2014-03-07 07:07:02.000000000 +0100
  100843. +++ linux-raspberry-pi/sound/soc/Makefile 2014-03-11 16:55:50.000000000 +0100
  100844. @@ -10,6 +10,7 @@
  100845. obj-$(CONFIG_SND_SOC) += generic/
  100846. obj-$(CONFIG_SND_SOC) += atmel/
  100847. obj-$(CONFIG_SND_SOC) += au1x/
  100848. +obj-$(CONFIG_SND_SOC) += bcm/
  100849. obj-$(CONFIG_SND_SOC) += blackfin/
  100850. obj-$(CONFIG_SND_SOC) += cirrus/
  100851. obj-$(CONFIG_SND_SOC) += davinci/