zoran_driver.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680
  1. /*
  2. * Zoran zr36057/zr36067 PCI controller driver, for the
  3. * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
  4. * Media Labs LML33/LML33R10.
  5. *
  6. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  7. *
  8. * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
  9. *
  10. * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
  11. *
  12. * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
  13. *
  14. * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
  15. *
  16. * Based on
  17. *
  18. * Miro DC10 driver
  19. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  20. *
  21. * Iomega Buz driver version 1.0
  22. * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  23. *
  24. * buz.0.0.3
  25. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  26. *
  27. * bttv - Bt848 frame grabber driver
  28. * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
  29. * & Marcus Metzler (mocm@thp.uni-koeln.de)
  30. *
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  45. */
  46. #include <linux/version.h>
  47. #include <linux/init.h>
  48. #include <linux/module.h>
  49. #include <linux/delay.h>
  50. #include <linux/slab.h>
  51. #include <linux/pci.h>
  52. #include <linux/vmalloc.h>
  53. #include <linux/wait.h>
  54. #include <linux/byteorder/generic.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/i2c.h>
  57. #include <linux/i2c-algo-bit.h>
  58. #include <linux/spinlock.h>
  59. #define MAP_NR(x) virt_to_page(x)
  60. #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
  61. #define ZORAN_VID_TYPE ( \
  62. VID_TYPE_CAPTURE | \
  63. VID_TYPE_OVERLAY | \
  64. VID_TYPE_CLIPPING | \
  65. VID_TYPE_FRAMERAM | \
  66. VID_TYPE_SCALES | \
  67. VID_TYPE_MJPEG_DECODER | \
  68. VID_TYPE_MJPEG_ENCODER \
  69. )
  70. #include <linux/videodev.h>
  71. #include <media/v4l2-common.h>
  72. #include "videocodec.h"
  73. #include <asm/io.h>
  74. #include <asm/uaccess.h>
  75. #include <linux/proc_fs.h>
  76. #include <linux/video_decoder.h>
  77. #include <linux/video_encoder.h>
  78. #include <linux/mutex.h>
  79. #include "zoran.h"
  80. #include "zoran_device.h"
  81. #include "zoran_card.h"
  82. #ifdef CONFIG_VIDEO_V4L2
  83. /* we declare some card type definitions here, they mean
  84. * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
  85. #define ZORAN_V4L2_VID_FLAGS ( \
  86. V4L2_CAP_STREAMING |\
  87. V4L2_CAP_VIDEO_CAPTURE |\
  88. V4L2_CAP_VIDEO_OUTPUT |\
  89. V4L2_CAP_VIDEO_OVERLAY \
  90. )
  91. #endif
  92. #include <asm/byteorder.h>
  93. #if defined(CONFIG_VIDEO_V4L2) && defined(CONFIG_VIDEO_V4L1_COMPAT)
  94. #define ZFMT(pal, fcc, cs) \
  95. .palette = (pal), .fourcc = (fcc), .colorspace = (cs)
  96. #elif defined(CONFIG_VIDEO_V4L2)
  97. #define ZFMT(pal, fcc, cs) \
  98. .fourcc = (fcc), .colorspace = (cs)
  99. #else
  100. #define ZFMT(pal, fcc, cs) \
  101. .palette = (pal)
  102. #endif
  103. const struct zoran_format zoran_formats[] = {
  104. {
  105. .name = "15-bit RGB LE",
  106. ZFMT(VIDEO_PALETTE_RGB555,
  107. V4L2_PIX_FMT_RGB555, V4L2_COLORSPACE_SRGB),
  108. .depth = 15,
  109. .flags = ZORAN_FORMAT_CAPTURE |
  110. ZORAN_FORMAT_OVERLAY,
  111. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
  112. ZR36057_VFESPFR_LittleEndian,
  113. }, {
  114. .name = "15-bit RGB BE",
  115. ZFMT(-1,
  116. V4L2_PIX_FMT_RGB555X, V4L2_COLORSPACE_SRGB),
  117. .depth = 15,
  118. .flags = ZORAN_FORMAT_CAPTURE |
  119. ZORAN_FORMAT_OVERLAY,
  120. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
  121. }, {
  122. .name = "16-bit RGB LE",
  123. ZFMT(VIDEO_PALETTE_RGB565,
  124. V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
  125. .depth = 16,
  126. .flags = ZORAN_FORMAT_CAPTURE |
  127. ZORAN_FORMAT_OVERLAY,
  128. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
  129. ZR36057_VFESPFR_LittleEndian,
  130. }, {
  131. .name = "16-bit RGB BE",
  132. ZFMT(-1,
  133. V4L2_PIX_FMT_RGB565, V4L2_COLORSPACE_SRGB),
  134. .depth = 16,
  135. .flags = ZORAN_FORMAT_CAPTURE |
  136. ZORAN_FORMAT_OVERLAY,
  137. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
  138. }, {
  139. .name = "24-bit RGB",
  140. ZFMT(VIDEO_PALETTE_RGB24,
  141. V4L2_PIX_FMT_BGR24, V4L2_COLORSPACE_SRGB),
  142. .depth = 24,
  143. .flags = ZORAN_FORMAT_CAPTURE |
  144. ZORAN_FORMAT_OVERLAY,
  145. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
  146. }, {
  147. .name = "32-bit RGB LE",
  148. ZFMT(VIDEO_PALETTE_RGB32,
  149. V4L2_PIX_FMT_BGR32, V4L2_COLORSPACE_SRGB),
  150. .depth = 32,
  151. .flags = ZORAN_FORMAT_CAPTURE |
  152. ZORAN_FORMAT_OVERLAY,
  153. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
  154. }, {
  155. .name = "32-bit RGB BE",
  156. ZFMT(-1,
  157. V4L2_PIX_FMT_RGB32, V4L2_COLORSPACE_SRGB),
  158. .depth = 32,
  159. .flags = ZORAN_FORMAT_CAPTURE |
  160. ZORAN_FORMAT_OVERLAY,
  161. .vfespfr = ZR36057_VFESPFR_RGB888,
  162. }, {
  163. .name = "4:2:2, packed, YUYV",
  164. ZFMT(VIDEO_PALETTE_YUV422,
  165. V4L2_PIX_FMT_YUYV, V4L2_COLORSPACE_SMPTE170M),
  166. .depth = 16,
  167. .flags = ZORAN_FORMAT_CAPTURE |
  168. ZORAN_FORMAT_OVERLAY,
  169. .vfespfr = ZR36057_VFESPFR_YUV422,
  170. }, {
  171. .name = "4:2:2, packed, UYVY",
  172. ZFMT(VIDEO_PALETTE_UYVY,
  173. V4L2_PIX_FMT_UYVY, V4L2_COLORSPACE_SMPTE170M),
  174. .depth = 16,
  175. .flags = ZORAN_FORMAT_CAPTURE |
  176. ZORAN_FORMAT_OVERLAY,
  177. .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
  178. }, {
  179. .name = "Hardware-encoded Motion-JPEG",
  180. ZFMT(-1,
  181. V4L2_PIX_FMT_MJPEG, V4L2_COLORSPACE_SMPTE170M),
  182. .depth = 0,
  183. .flags = ZORAN_FORMAT_CAPTURE |
  184. ZORAN_FORMAT_PLAYBACK |
  185. ZORAN_FORMAT_COMPRESSED,
  186. }
  187. };
  188. #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
  189. // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
  190. extern int *zr_debug;
  191. #define dprintk(num, format, args...) \
  192. do { \
  193. if (*zr_debug >= num) \
  194. printk(format, ##args); \
  195. } while (0)
  196. extern int v4l_nbufs;
  197. extern int v4l_bufsize;
  198. extern int jpg_nbufs;
  199. extern int jpg_bufsize;
  200. extern int pass_through;
  201. static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
  202. module_param(lock_norm, int, 0);
  203. MODULE_PARM_DESC(lock_norm, "Users can't change norm");
  204. #ifdef CONFIG_VIDEO_V4L2
  205. /* small helper function for calculating buffersizes for v4l2
  206. * we calculate the nearest higher power-of-two, which
  207. * will be the recommended buffersize */
  208. static __u32
  209. zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
  210. {
  211. __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
  212. __u32 num = (1024 * 512) / (div);
  213. __u32 result = 2;
  214. num--;
  215. while (num) {
  216. num >>= 1;
  217. result <<= 1;
  218. }
  219. if (result > jpg_bufsize)
  220. return jpg_bufsize;
  221. if (result < 8192)
  222. return 8192;
  223. return result;
  224. }
  225. #endif
  226. /* forward references */
  227. static void v4l_fbuffer_free(struct file *file);
  228. static void jpg_fbuffer_free(struct file *file);
  229. /*
  230. * Allocate the V4L grab buffers
  231. *
  232. * These have to be pysically contiguous.
  233. * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
  234. * else we try to allocate them with bigphysarea_alloc_pages
  235. * if the bigphysarea patch is present in the kernel,
  236. * else we try to use high memory (if the user has bootet
  237. * Linux with the necessary memory left over).
  238. */
  239. static unsigned long
  240. get_high_mem (unsigned long size)
  241. {
  242. /*
  243. * Check if there is usable memory at the end of Linux memory
  244. * of at least size. Return the physical address of this memory,
  245. * return 0 on failure.
  246. *
  247. * The idea is from Alexandro Rubini's book "Linux device drivers".
  248. * The driver from him which is downloadable from O'Reilly's
  249. * web site misses the "virt_to_phys(high_memory)" part
  250. * (and therefore doesn't work at all - at least with 2.2.x kernels).
  251. *
  252. * It should be unnecessary to mention that THIS IS DANGEROUS,
  253. * if more than one driver at a time has the idea to use this memory!!!!
  254. */
  255. volatile unsigned char __iomem *mem;
  256. unsigned char c;
  257. unsigned long hi_mem_ph;
  258. unsigned long i;
  259. /* Map the high memory to user space */
  260. hi_mem_ph = virt_to_phys(high_memory);
  261. mem = ioremap(hi_mem_ph, size);
  262. if (!mem) {
  263. dprintk(1,
  264. KERN_ERR "%s: get_high_mem() - ioremap failed\n",
  265. ZORAN_NAME);
  266. return 0;
  267. }
  268. for (i = 0; i < size; i++) {
  269. /* Check if it is memory */
  270. c = i & 0xff;
  271. writeb(c, mem + i);
  272. if (readb(mem + i) != c)
  273. break;
  274. c = 255 - c;
  275. writeb(c, mem + i);
  276. if (readb(mem + i) != c)
  277. break;
  278. writeb(0, mem + i); /* zero out memory */
  279. /* give the kernel air to breath */
  280. if ((i & 0x3ffff) == 0x3ffff)
  281. schedule();
  282. }
  283. iounmap(mem);
  284. if (i != size) {
  285. dprintk(1,
  286. KERN_ERR
  287. "%s: get_high_mem() - requested %lu, avail %lu\n",
  288. ZORAN_NAME, size, i);
  289. return 0;
  290. }
  291. return hi_mem_ph;
  292. }
  293. static int
  294. v4l_fbuffer_alloc (struct file *file)
  295. {
  296. struct zoran_fh *fh = file->private_data;
  297. struct zoran *zr = fh->zr;
  298. int i, off;
  299. unsigned char *mem;
  300. unsigned long pmem = 0;
  301. /* we might have old buffers lying around... */
  302. if (fh->v4l_buffers.ready_to_be_freed) {
  303. v4l_fbuffer_free(file);
  304. }
  305. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  306. if (fh->v4l_buffers.buffer[i].fbuffer)
  307. dprintk(2,
  308. KERN_WARNING
  309. "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
  310. ZR_DEVNAME(zr), i);
  311. //udelay(20);
  312. if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
  313. /* Use kmalloc */
  314. mem =
  315. (unsigned char *) kmalloc(fh->v4l_buffers.
  316. buffer_size,
  317. GFP_KERNEL);
  318. if (mem == 0) {
  319. dprintk(1,
  320. KERN_ERR
  321. "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
  322. ZR_DEVNAME(zr), i);
  323. v4l_fbuffer_free(file);
  324. return -ENOBUFS;
  325. }
  326. fh->v4l_buffers.buffer[i].fbuffer = mem;
  327. fh->v4l_buffers.buffer[i].fbuffer_phys =
  328. virt_to_phys(mem);
  329. fh->v4l_buffers.buffer[i].fbuffer_bus =
  330. virt_to_bus(mem);
  331. for (off = 0; off < fh->v4l_buffers.buffer_size;
  332. off += PAGE_SIZE)
  333. SetPageReserved(MAP_NR(mem + off));
  334. dprintk(4,
  335. KERN_INFO
  336. "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
  337. ZR_DEVNAME(zr), i, (unsigned long) mem,
  338. virt_to_bus(mem));
  339. } else {
  340. /* Use high memory which has been left at boot time */
  341. /* Ok., Ok. this is an evil hack - we make
  342. * the assumption that physical addresses are
  343. * the same as bus addresses (true at least
  344. * for Intel processors). The whole method of
  345. * obtaining and using this memory is not very
  346. * nice - but I hope it saves some poor users
  347. * from kernel hacking, which might have even
  348. * more evil results */
  349. if (i == 0) {
  350. int size =
  351. fh->v4l_buffers.num_buffers *
  352. fh->v4l_buffers.buffer_size;
  353. pmem = get_high_mem(size);
  354. if (pmem == 0) {
  355. dprintk(1,
  356. KERN_ERR
  357. "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
  358. ZR_DEVNAME(zr), size >> 10);
  359. return -ENOBUFS;
  360. }
  361. fh->v4l_buffers.buffer[0].fbuffer = NULL;
  362. fh->v4l_buffers.buffer[0].fbuffer_phys = pmem;
  363. fh->v4l_buffers.buffer[0].fbuffer_bus = pmem;
  364. dprintk(4,
  365. KERN_INFO
  366. "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
  367. ZR_DEVNAME(zr), size >> 10);
  368. } else {
  369. fh->v4l_buffers.buffer[i].fbuffer = NULL;
  370. fh->v4l_buffers.buffer[i].fbuffer_phys =
  371. pmem + i * fh->v4l_buffers.buffer_size;
  372. fh->v4l_buffers.buffer[i].fbuffer_bus =
  373. pmem + i * fh->v4l_buffers.buffer_size;
  374. }
  375. }
  376. }
  377. fh->v4l_buffers.allocated = 1;
  378. return 0;
  379. }
  380. /* free the V4L grab buffers */
  381. static void
  382. v4l_fbuffer_free (struct file *file)
  383. {
  384. struct zoran_fh *fh = file->private_data;
  385. struct zoran *zr = fh->zr;
  386. int i, off;
  387. unsigned char *mem;
  388. dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
  389. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  390. if (!fh->v4l_buffers.buffer[i].fbuffer)
  391. continue;
  392. if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
  393. mem = fh->v4l_buffers.buffer[i].fbuffer;
  394. for (off = 0; off < fh->v4l_buffers.buffer_size;
  395. off += PAGE_SIZE)
  396. ClearPageReserved(MAP_NR(mem + off));
  397. kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
  398. }
  399. fh->v4l_buffers.buffer[i].fbuffer = NULL;
  400. }
  401. fh->v4l_buffers.allocated = 0;
  402. fh->v4l_buffers.ready_to_be_freed = 0;
  403. }
  404. /*
  405. * Allocate the MJPEG grab buffers.
  406. *
  407. * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
  408. * kmalloc is used to request a physically contiguous area,
  409. * else we allocate the memory in framgents with get_zeroed_page.
  410. *
  411. * If a Natoma chipset is present and this is a revision 1 zr36057,
  412. * each MJPEG buffer needs to be physically contiguous.
  413. * (RJ: This statement is from Dave Perks' original driver,
  414. * I could never check it because I have a zr36067)
  415. * The driver cares about this because it reduces the buffer
  416. * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
  417. *
  418. * RJ: The contents grab buffers needs never be accessed in the driver.
  419. * Therefore there is no need to allocate them with vmalloc in order
  420. * to get a contiguous virtual memory space.
  421. * I don't understand why many other drivers first allocate them with
  422. * vmalloc (which uses internally also get_zeroed_page, but delivers you
  423. * virtual addresses) and then again have to make a lot of efforts
  424. * to get the physical address.
  425. *
  426. * Ben Capper:
  427. * On big-endian architectures (such as ppc) some extra steps
  428. * are needed. When reading and writing to the stat_com array
  429. * and fragment buffers, the device expects to see little-
  430. * endian values. The use of cpu_to_le32() and le32_to_cpu()
  431. * in this function (and one or two others in zoran_device.c)
  432. * ensure that these values are always stored in little-endian
  433. * form, regardless of architecture. The zr36057 does Very Bad
  434. * Things on big endian architectures if the stat_com array
  435. * and fragment buffers are not little-endian.
  436. */
  437. static int
  438. jpg_fbuffer_alloc (struct file *file)
  439. {
  440. struct zoran_fh *fh = file->private_data;
  441. struct zoran *zr = fh->zr;
  442. int i, j, off;
  443. unsigned long mem;
  444. /* we might have old buffers lying around */
  445. if (fh->jpg_buffers.ready_to_be_freed) {
  446. jpg_fbuffer_free(file);
  447. }
  448. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  449. if (fh->jpg_buffers.buffer[i].frag_tab)
  450. dprintk(2,
  451. KERN_WARNING
  452. "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
  453. ZR_DEVNAME(zr), i);
  454. /* Allocate fragment table for this buffer */
  455. mem = get_zeroed_page(GFP_KERNEL);
  456. if (mem == 0) {
  457. dprintk(1,
  458. KERN_ERR
  459. "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
  460. ZR_DEVNAME(zr), i);
  461. jpg_fbuffer_free(file);
  462. return -ENOBUFS;
  463. }
  464. fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
  465. fh->jpg_buffers.buffer[i].frag_tab_bus =
  466. virt_to_bus((void *) mem);
  467. //if (alloc_contig) {
  468. if (fh->jpg_buffers.need_contiguous) {
  469. mem =
  470. (unsigned long) kmalloc(fh->jpg_buffers.
  471. buffer_size,
  472. GFP_KERNEL);
  473. if (mem == 0) {
  474. dprintk(1,
  475. KERN_ERR
  476. "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
  477. ZR_DEVNAME(zr), i);
  478. jpg_fbuffer_free(file);
  479. return -ENOBUFS;
  480. }
  481. fh->jpg_buffers.buffer[i].frag_tab[0] =
  482. cpu_to_le32(virt_to_bus((void *) mem));
  483. fh->jpg_buffers.buffer[i].frag_tab[1] =
  484. cpu_to_le32(((fh->jpg_buffers.buffer_size / 4) << 1) | 1);
  485. for (off = 0; off < fh->jpg_buffers.buffer_size;
  486. off += PAGE_SIZE)
  487. SetPageReserved(MAP_NR(mem + off));
  488. } else {
  489. /* jpg_bufsize is allreay page aligned */
  490. for (j = 0;
  491. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  492. j++) {
  493. mem = get_zeroed_page(GFP_KERNEL);
  494. if (mem == 0) {
  495. dprintk(1,
  496. KERN_ERR
  497. "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
  498. ZR_DEVNAME(zr), i);
  499. jpg_fbuffer_free(file);
  500. return -ENOBUFS;
  501. }
  502. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  503. cpu_to_le32(virt_to_bus((void *) mem));
  504. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  505. 1] =
  506. cpu_to_le32((PAGE_SIZE / 4) << 1);
  507. SetPageReserved(MAP_NR(mem));
  508. }
  509. fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= cpu_to_le32(1);
  510. }
  511. }
  512. dprintk(4,
  513. KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
  514. ZR_DEVNAME(zr),
  515. (fh->jpg_buffers.num_buffers *
  516. fh->jpg_buffers.buffer_size) >> 10);
  517. fh->jpg_buffers.allocated = 1;
  518. return 0;
  519. }
  520. /* free the MJPEG grab buffers */
  521. static void
  522. jpg_fbuffer_free (struct file *file)
  523. {
  524. struct zoran_fh *fh = file->private_data;
  525. struct zoran *zr = fh->zr;
  526. int i, j, off;
  527. unsigned char *mem;
  528. dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
  529. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  530. if (!fh->jpg_buffers.buffer[i].frag_tab)
  531. continue;
  532. //if (alloc_contig) {
  533. if (fh->jpg_buffers.need_contiguous) {
  534. if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
  535. mem = (unsigned char *) bus_to_virt(le32_to_cpu(
  536. fh->jpg_buffers.buffer[i].frag_tab[0]));
  537. for (off = 0;
  538. off < fh->jpg_buffers.buffer_size;
  539. off += PAGE_SIZE)
  540. ClearPageReserved(MAP_NR
  541. (mem + off));
  542. kfree(mem);
  543. fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
  544. fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
  545. }
  546. } else {
  547. for (j = 0;
  548. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  549. j++) {
  550. if (!fh->jpg_buffers.buffer[i].
  551. frag_tab[2 * j])
  552. break;
  553. ClearPageReserved(MAP_NR
  554. (bus_to_virt
  555. (le32_to_cpu
  556. (fh->jpg_buffers.
  557. buffer[i].frag_tab[2 *
  558. j]))));
  559. free_page((unsigned long)
  560. bus_to_virt
  561. (le32_to_cpu
  562. (fh->jpg_buffers.
  563. buffer[i].
  564. frag_tab[2 * j])));
  565. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  566. 0;
  567. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  568. 1] = 0;
  569. }
  570. }
  571. free_page((unsigned long) fh->jpg_buffers.buffer[i].
  572. frag_tab);
  573. fh->jpg_buffers.buffer[i].frag_tab = NULL;
  574. }
  575. fh->jpg_buffers.allocated = 0;
  576. fh->jpg_buffers.ready_to_be_freed = 0;
  577. }
  578. /*
  579. * V4L Buffer grabbing
  580. */
  581. static int
  582. zoran_v4l_set_format (struct file *file,
  583. int width,
  584. int height,
  585. const struct zoran_format *format)
  586. {
  587. struct zoran_fh *fh = file->private_data;
  588. struct zoran *zr = fh->zr;
  589. int bpp;
  590. /* Check size and format of the grab wanted */
  591. if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
  592. height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
  593. dprintk(1,
  594. KERN_ERR
  595. "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
  596. ZR_DEVNAME(zr), width, height);
  597. return -EINVAL;
  598. }
  599. bpp = (format->depth + 7) / 8;
  600. /* Check against available buffer size */
  601. if (height * width * bpp > fh->v4l_buffers.buffer_size) {
  602. dprintk(1,
  603. KERN_ERR
  604. "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
  605. ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
  606. return -EINVAL;
  607. }
  608. /* The video front end needs 4-byte alinged line sizes */
  609. if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
  610. dprintk(1,
  611. KERN_ERR
  612. "%s: v4l_set_format() - wrong frame alingment\n",
  613. ZR_DEVNAME(zr));
  614. return -EINVAL;
  615. }
  616. fh->v4l_settings.width = width;
  617. fh->v4l_settings.height = height;
  618. fh->v4l_settings.format = format;
  619. fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
  620. return 0;
  621. }
  622. static int
  623. zoran_v4l_queue_frame (struct file *file,
  624. int num)
  625. {
  626. struct zoran_fh *fh = file->private_data;
  627. struct zoran *zr = fh->zr;
  628. unsigned long flags;
  629. int res = 0;
  630. if (!fh->v4l_buffers.allocated) {
  631. dprintk(1,
  632. KERN_ERR
  633. "%s: v4l_queue_frame() - buffers not yet allocated\n",
  634. ZR_DEVNAME(zr));
  635. res = -ENOMEM;
  636. }
  637. /* No grabbing outside the buffer range! */
  638. if (num >= fh->v4l_buffers.num_buffers || num < 0) {
  639. dprintk(1,
  640. KERN_ERR
  641. "%s: v4l_queue_frame() - buffer %d is out of range\n",
  642. ZR_DEVNAME(zr), num);
  643. res = -EINVAL;
  644. }
  645. spin_lock_irqsave(&zr->spinlock, flags);
  646. if (fh->v4l_buffers.active == ZORAN_FREE) {
  647. if (zr->v4l_buffers.active == ZORAN_FREE) {
  648. zr->v4l_buffers = fh->v4l_buffers;
  649. fh->v4l_buffers.active = ZORAN_ACTIVE;
  650. } else {
  651. dprintk(1,
  652. KERN_ERR
  653. "%s: v4l_queue_frame() - another session is already capturing\n",
  654. ZR_DEVNAME(zr));
  655. res = -EBUSY;
  656. }
  657. }
  658. /* make sure a grab isn't going on currently with this buffer */
  659. if (!res) {
  660. switch (zr->v4l_buffers.buffer[num].state) {
  661. default:
  662. case BUZ_STATE_PEND:
  663. if (zr->v4l_buffers.active == ZORAN_FREE) {
  664. fh->v4l_buffers.active = ZORAN_FREE;
  665. zr->v4l_buffers.allocated = 0;
  666. }
  667. res = -EBUSY; /* what are you doing? */
  668. break;
  669. case BUZ_STATE_DONE:
  670. dprintk(2,
  671. KERN_WARNING
  672. "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
  673. ZR_DEVNAME(zr), num);
  674. case BUZ_STATE_USER:
  675. /* since there is at least one unused buffer there's room for at least
  676. * one more pend[] entry */
  677. zr->v4l_pend[zr->v4l_pend_head++ &
  678. V4L_MASK_FRAME] = num;
  679. zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
  680. zr->v4l_buffers.buffer[num].bs.length =
  681. fh->v4l_settings.bytesperline *
  682. zr->v4l_settings.height;
  683. fh->v4l_buffers.buffer[num] =
  684. zr->v4l_buffers.buffer[num];
  685. break;
  686. }
  687. }
  688. spin_unlock_irqrestore(&zr->spinlock, flags);
  689. if (!res && zr->v4l_buffers.active == ZORAN_FREE)
  690. zr->v4l_buffers.active = fh->v4l_buffers.active;
  691. return res;
  692. }
  693. static int
  694. v4l_grab (struct file *file,
  695. struct video_mmap *mp)
  696. {
  697. struct zoran_fh *fh = file->private_data;
  698. struct zoran *zr = fh->zr;
  699. int res = 0, i;
  700. for (i = 0; i < NUM_FORMATS; i++) {
  701. if (zoran_formats[i].palette == mp->format &&
  702. zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
  703. !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
  704. break;
  705. }
  706. if (i == NUM_FORMATS || zoran_formats[i].depth == 0) {
  707. dprintk(1,
  708. KERN_ERR
  709. "%s: v4l_grab() - wrong bytes-per-pixel format\n",
  710. ZR_DEVNAME(zr));
  711. return -EINVAL;
  712. }
  713. /*
  714. * To minimize the time spent in the IRQ routine, we avoid setting up
  715. * the video front end there.
  716. * If this grab has different parameters from a running streaming capture
  717. * we stop the streaming capture and start it over again.
  718. */
  719. if (zr->v4l_memgrab_active &&
  720. (zr->v4l_settings.width != mp->width ||
  721. zr->v4l_settings.height != mp->height ||
  722. zr->v4l_settings.format->palette != mp->format)) {
  723. res = wait_grab_pending(zr);
  724. if (res)
  725. return res;
  726. }
  727. if ((res = zoran_v4l_set_format(file,
  728. mp->width,
  729. mp->height,
  730. &zoran_formats[i])))
  731. return res;
  732. zr->v4l_settings = fh->v4l_settings;
  733. /* queue the frame in the pending queue */
  734. if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
  735. fh->v4l_buffers.active = ZORAN_FREE;
  736. return res;
  737. }
  738. /* put the 36057 into frame grabbing mode */
  739. if (!res && !zr->v4l_memgrab_active)
  740. zr36057_set_memgrab(zr, 1);
  741. //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
  742. return res;
  743. }
  744. /*
  745. * Sync on a V4L buffer
  746. */
  747. static int
  748. v4l_sync (struct file *file,
  749. int frame)
  750. {
  751. struct zoran_fh *fh = file->private_data;
  752. struct zoran *zr = fh->zr;
  753. unsigned long flags;
  754. if (fh->v4l_buffers.active == ZORAN_FREE) {
  755. dprintk(1,
  756. KERN_ERR
  757. "%s: v4l_sync() - no grab active for this session\n",
  758. ZR_DEVNAME(zr));
  759. return -EINVAL;
  760. }
  761. /* check passed-in frame number */
  762. if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
  763. dprintk(1,
  764. KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
  765. ZR_DEVNAME(zr), frame);
  766. return -EINVAL;
  767. }
  768. /* Check if is buffer was queued at all */
  769. if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
  770. dprintk(1,
  771. KERN_ERR
  772. "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
  773. ZR_DEVNAME(zr));
  774. return -EPROTO;
  775. }
  776. /* wait on this buffer to get ready */
  777. if (!wait_event_interruptible_timeout(zr->v4l_capq,
  778. (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND),
  779. 10*HZ))
  780. return -ETIME;
  781. if (signal_pending(current))
  782. return -ERESTARTSYS;
  783. /* buffer should now be in BUZ_STATE_DONE */
  784. if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
  785. dprintk(2,
  786. KERN_ERR "%s: v4l_sync() - internal state error\n",
  787. ZR_DEVNAME(zr));
  788. zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
  789. fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
  790. spin_lock_irqsave(&zr->spinlock, flags);
  791. /* Check if streaming capture has finished */
  792. if (zr->v4l_pend_tail == zr->v4l_pend_head) {
  793. zr36057_set_memgrab(zr, 0);
  794. if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
  795. fh->v4l_buffers.active = zr->v4l_buffers.active =
  796. ZORAN_FREE;
  797. zr->v4l_buffers.allocated = 0;
  798. }
  799. }
  800. spin_unlock_irqrestore(&zr->spinlock, flags);
  801. return 0;
  802. }
  803. /*
  804. * Queue a MJPEG buffer for capture/playback
  805. */
  806. static int
  807. zoran_jpg_queue_frame (struct file *file,
  808. int num,
  809. enum zoran_codec_mode mode)
  810. {
  811. struct zoran_fh *fh = file->private_data;
  812. struct zoran *zr = fh->zr;
  813. unsigned long flags;
  814. int res = 0;
  815. /* Check if buffers are allocated */
  816. if (!fh->jpg_buffers.allocated) {
  817. dprintk(1,
  818. KERN_ERR
  819. "%s: jpg_queue_frame() - buffers not yet allocated\n",
  820. ZR_DEVNAME(zr));
  821. return -ENOMEM;
  822. }
  823. /* No grabbing outside the buffer range! */
  824. if (num >= fh->jpg_buffers.num_buffers || num < 0) {
  825. dprintk(1,
  826. KERN_ERR
  827. "%s: jpg_queue_frame() - buffer %d out of range\n",
  828. ZR_DEVNAME(zr), num);
  829. return -EINVAL;
  830. }
  831. /* what is the codec mode right now? */
  832. if (zr->codec_mode == BUZ_MODE_IDLE) {
  833. zr->jpg_settings = fh->jpg_settings;
  834. } else if (zr->codec_mode != mode) {
  835. /* wrong codec mode active - invalid */
  836. dprintk(1,
  837. KERN_ERR
  838. "%s: jpg_queue_frame() - codec in wrong mode\n",
  839. ZR_DEVNAME(zr));
  840. return -EINVAL;
  841. }
  842. if (fh->jpg_buffers.active == ZORAN_FREE) {
  843. if (zr->jpg_buffers.active == ZORAN_FREE) {
  844. zr->jpg_buffers = fh->jpg_buffers;
  845. fh->jpg_buffers.active = ZORAN_ACTIVE;
  846. } else {
  847. dprintk(1,
  848. KERN_ERR
  849. "%s: jpg_queue_frame() - another session is already capturing\n",
  850. ZR_DEVNAME(zr));
  851. res = -EBUSY;
  852. }
  853. }
  854. if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
  855. /* Ok load up the jpeg codec */
  856. zr36057_enable_jpg(zr, mode);
  857. }
  858. spin_lock_irqsave(&zr->spinlock, flags);
  859. if (!res) {
  860. switch (zr->jpg_buffers.buffer[num].state) {
  861. case BUZ_STATE_DONE:
  862. dprintk(2,
  863. KERN_WARNING
  864. "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
  865. ZR_DEVNAME(zr));
  866. case BUZ_STATE_USER:
  867. /* since there is at least one unused buffer there's room for at
  868. *least one more pend[] entry */
  869. zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
  870. num;
  871. zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
  872. fh->jpg_buffers.buffer[num] =
  873. zr->jpg_buffers.buffer[num];
  874. zoran_feed_stat_com(zr);
  875. break;
  876. default:
  877. case BUZ_STATE_DMA:
  878. case BUZ_STATE_PEND:
  879. if (zr->jpg_buffers.active == ZORAN_FREE) {
  880. fh->jpg_buffers.active = ZORAN_FREE;
  881. zr->jpg_buffers.allocated = 0;
  882. }
  883. res = -EBUSY; /* what are you doing? */
  884. break;
  885. }
  886. }
  887. spin_unlock_irqrestore(&zr->spinlock, flags);
  888. if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
  889. zr->jpg_buffers.active = fh->jpg_buffers.active;
  890. }
  891. return res;
  892. }
  893. static int
  894. jpg_qbuf (struct file *file,
  895. int frame,
  896. enum zoran_codec_mode mode)
  897. {
  898. struct zoran_fh *fh = file->private_data;
  899. struct zoran *zr = fh->zr;
  900. int res = 0;
  901. /* Does the user want to stop streaming? */
  902. if (frame < 0) {
  903. if (zr->codec_mode == mode) {
  904. if (fh->jpg_buffers.active == ZORAN_FREE) {
  905. dprintk(1,
  906. KERN_ERR
  907. "%s: jpg_qbuf(-1) - session not active\n",
  908. ZR_DEVNAME(zr));
  909. return -EINVAL;
  910. }
  911. fh->jpg_buffers.active = zr->jpg_buffers.active =
  912. ZORAN_FREE;
  913. zr->jpg_buffers.allocated = 0;
  914. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  915. return 0;
  916. } else {
  917. dprintk(1,
  918. KERN_ERR
  919. "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
  920. ZR_DEVNAME(zr));
  921. return -EINVAL;
  922. }
  923. }
  924. if ((res = zoran_jpg_queue_frame(file, frame, mode)))
  925. return res;
  926. /* Start the jpeg codec when the first frame is queued */
  927. if (!res && zr->jpg_que_head == 1)
  928. jpeg_start(zr);
  929. return res;
  930. }
  931. /*
  932. * Sync on a MJPEG buffer
  933. */
  934. static int
  935. jpg_sync (struct file *file,
  936. struct zoran_sync *bs)
  937. {
  938. struct zoran_fh *fh = file->private_data;
  939. struct zoran *zr = fh->zr;
  940. unsigned long flags;
  941. int frame;
  942. if (fh->jpg_buffers.active == ZORAN_FREE) {
  943. dprintk(1,
  944. KERN_ERR
  945. "%s: jpg_sync() - capture is not currently active\n",
  946. ZR_DEVNAME(zr));
  947. return -EINVAL;
  948. }
  949. if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
  950. zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
  951. dprintk(1,
  952. KERN_ERR
  953. "%s: jpg_sync() - codec not in streaming mode\n",
  954. ZR_DEVNAME(zr));
  955. return -EINVAL;
  956. }
  957. if (!wait_event_interruptible_timeout(zr->jpg_capq,
  958. (zr->jpg_que_tail != zr->jpg_dma_tail ||
  959. zr->jpg_dma_tail == zr->jpg_dma_head),
  960. 10*HZ)) {
  961. int isr;
  962. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  963. udelay(1);
  964. zr->codec->control(zr->codec, CODEC_G_STATUS,
  965. sizeof(isr), &isr);
  966. dprintk(1,
  967. KERN_ERR
  968. "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
  969. ZR_DEVNAME(zr), isr);
  970. return -ETIME;
  971. }
  972. if (signal_pending(current))
  973. return -ERESTARTSYS;
  974. spin_lock_irqsave(&zr->spinlock, flags);
  975. if (zr->jpg_dma_tail != zr->jpg_dma_head)
  976. frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
  977. else
  978. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  979. /* buffer should now be in BUZ_STATE_DONE */
  980. if (*zr_debug > 0)
  981. if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
  982. dprintk(2,
  983. KERN_ERR
  984. "%s: jpg_sync() - internal state error\n",
  985. ZR_DEVNAME(zr));
  986. *bs = zr->jpg_buffers.buffer[frame].bs;
  987. bs->frame = frame;
  988. zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
  989. fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
  990. spin_unlock_irqrestore(&zr->spinlock, flags);
  991. return 0;
  992. }
  993. static void
  994. zoran_open_init_session (struct file *file)
  995. {
  996. int i;
  997. struct zoran_fh *fh = file->private_data;
  998. struct zoran *zr = fh->zr;
  999. /* Per default, map the V4L Buffers */
  1000. fh->map_mode = ZORAN_MAP_MODE_RAW;
  1001. /* take over the card's current settings */
  1002. fh->overlay_settings = zr->overlay_settings;
  1003. fh->overlay_settings.is_set = 0;
  1004. fh->overlay_settings.format = zr->overlay_settings.format;
  1005. fh->overlay_active = ZORAN_FREE;
  1006. /* v4l settings */
  1007. fh->v4l_settings = zr->v4l_settings;
  1008. /* v4l_buffers */
  1009. memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
  1010. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  1011. fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  1012. fh->v4l_buffers.buffer[i].bs.frame = i;
  1013. }
  1014. fh->v4l_buffers.allocated = 0;
  1015. fh->v4l_buffers.ready_to_be_freed = 0;
  1016. fh->v4l_buffers.active = ZORAN_FREE;
  1017. fh->v4l_buffers.buffer_size = v4l_bufsize;
  1018. fh->v4l_buffers.num_buffers = v4l_nbufs;
  1019. /* jpg settings */
  1020. fh->jpg_settings = zr->jpg_settings;
  1021. /* jpg_buffers */
  1022. memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
  1023. for (i = 0; i < BUZ_MAX_FRAME; i++) {
  1024. fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  1025. fh->jpg_buffers.buffer[i].bs.frame = i;
  1026. }
  1027. fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
  1028. fh->jpg_buffers.allocated = 0;
  1029. fh->jpg_buffers.ready_to_be_freed = 0;
  1030. fh->jpg_buffers.active = ZORAN_FREE;
  1031. fh->jpg_buffers.buffer_size = jpg_bufsize;
  1032. fh->jpg_buffers.num_buffers = jpg_nbufs;
  1033. }
  1034. static void
  1035. zoran_close_end_session (struct file *file)
  1036. {
  1037. struct zoran_fh *fh = file->private_data;
  1038. struct zoran *zr = fh->zr;
  1039. /* overlay */
  1040. if (fh->overlay_active != ZORAN_FREE) {
  1041. fh->overlay_active = zr->overlay_active = ZORAN_FREE;
  1042. zr->v4l_overlay_active = 0;
  1043. if (!zr->v4l_memgrab_active)
  1044. zr36057_overlay(zr, 0);
  1045. zr->overlay_mask = NULL;
  1046. }
  1047. /* v4l capture */
  1048. if (fh->v4l_buffers.active != ZORAN_FREE) {
  1049. long flags;
  1050. spin_lock_irqsave(&zr->spinlock, flags);
  1051. zr36057_set_memgrab(zr, 0);
  1052. zr->v4l_buffers.allocated = 0;
  1053. zr->v4l_buffers.active = fh->v4l_buffers.active =
  1054. ZORAN_FREE;
  1055. spin_unlock_irqrestore(&zr->spinlock, flags);
  1056. }
  1057. /* v4l buffers */
  1058. if (fh->v4l_buffers.allocated ||
  1059. fh->v4l_buffers.ready_to_be_freed) {
  1060. v4l_fbuffer_free(file);
  1061. }
  1062. /* jpg capture */
  1063. if (fh->jpg_buffers.active != ZORAN_FREE) {
  1064. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1065. zr->jpg_buffers.allocated = 0;
  1066. zr->jpg_buffers.active = fh->jpg_buffers.active =
  1067. ZORAN_FREE;
  1068. }
  1069. /* jpg buffers */
  1070. if (fh->jpg_buffers.allocated ||
  1071. fh->jpg_buffers.ready_to_be_freed) {
  1072. jpg_fbuffer_free(file);
  1073. }
  1074. }
  1075. /*
  1076. * Open a zoran card. Right now the flags stuff is just playing
  1077. */
  1078. static int
  1079. zoran_open (struct inode *inode,
  1080. struct file *file)
  1081. {
  1082. unsigned int minor = iminor(inode);
  1083. struct zoran *zr = NULL;
  1084. struct zoran_fh *fh;
  1085. int i, res, first_open = 0, have_module_locks = 0;
  1086. /* find the device */
  1087. for (i = 0; i < zoran_num; i++) {
  1088. if (zoran[i].video_dev->minor == minor) {
  1089. zr = &zoran[i];
  1090. break;
  1091. }
  1092. }
  1093. if (!zr) {
  1094. dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
  1095. res = -ENODEV;
  1096. goto open_unlock_and_return;
  1097. }
  1098. /* see fs/device.c - the kernel already locks during open(),
  1099. * so locking ourselves only causes deadlocks */
  1100. /*mutex_lock(&zr->resource_lock);*/
  1101. if (!zr->decoder) {
  1102. dprintk(1,
  1103. KERN_ERR "%s: no TV decoder loaded for device!\n",
  1104. ZR_DEVNAME(zr));
  1105. res = -EIO;
  1106. goto open_unlock_and_return;
  1107. }
  1108. /* try to grab a module lock */
  1109. if (!try_module_get(THIS_MODULE)) {
  1110. dprintk(1,
  1111. KERN_ERR
  1112. "%s: failed to acquire my own lock! PANIC!\n",
  1113. ZR_DEVNAME(zr));
  1114. res = -ENODEV;
  1115. goto open_unlock_and_return;
  1116. }
  1117. if (!try_module_get(zr->decoder->driver->driver.owner)) {
  1118. dprintk(1,
  1119. KERN_ERR
  1120. "%s: failed to grab ownership of i2c decoder\n",
  1121. ZR_DEVNAME(zr));
  1122. res = -EIO;
  1123. module_put(THIS_MODULE);
  1124. goto open_unlock_and_return;
  1125. }
  1126. if (zr->encoder &&
  1127. !try_module_get(zr->encoder->driver->driver.owner)) {
  1128. dprintk(1,
  1129. KERN_ERR
  1130. "%s: failed to grab ownership of i2c encoder\n",
  1131. ZR_DEVNAME(zr));
  1132. res = -EIO;
  1133. module_put(zr->decoder->driver->driver.owner);
  1134. module_put(THIS_MODULE);
  1135. goto open_unlock_and_return;
  1136. }
  1137. have_module_locks = 1;
  1138. if (zr->user >= 2048) {
  1139. dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
  1140. ZR_DEVNAME(zr), zr->user);
  1141. res = -EBUSY;
  1142. goto open_unlock_and_return;
  1143. }
  1144. dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
  1145. ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
  1146. /* now, create the open()-specific file_ops struct */
  1147. fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
  1148. if (!fh) {
  1149. dprintk(1,
  1150. KERN_ERR
  1151. "%s: zoran_open() - allocation of zoran_fh failed\n",
  1152. ZR_DEVNAME(zr));
  1153. res = -ENOMEM;
  1154. goto open_unlock_and_return;
  1155. }
  1156. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  1157. * on norm-change! */
  1158. fh->overlay_mask =
  1159. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  1160. if (!fh->overlay_mask) {
  1161. dprintk(1,
  1162. KERN_ERR
  1163. "%s: zoran_open() - allocation of overlay_mask failed\n",
  1164. ZR_DEVNAME(zr));
  1165. kfree(fh);
  1166. res = -ENOMEM;
  1167. goto open_unlock_and_return;
  1168. }
  1169. if (zr->user++ == 0)
  1170. first_open = 1;
  1171. /*mutex_unlock(&zr->resource_lock);*/
  1172. /* default setup - TODO: look at flags */
  1173. if (first_open) { /* First device open */
  1174. zr36057_restart(zr);
  1175. zoran_open_init_params(zr);
  1176. zoran_init_hardware(zr);
  1177. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1178. }
  1179. /* set file_ops stuff */
  1180. file->private_data = fh;
  1181. fh->zr = zr;
  1182. zoran_open_init_session(file);
  1183. return 0;
  1184. open_unlock_and_return:
  1185. /* if we grabbed locks, release them accordingly */
  1186. if (have_module_locks) {
  1187. module_put(zr->decoder->driver->driver.owner);
  1188. if (zr->encoder) {
  1189. module_put(zr->encoder->driver->driver.owner);
  1190. }
  1191. module_put(THIS_MODULE);
  1192. }
  1193. /* if there's no device found, we didn't obtain the lock either */
  1194. if (zr) {
  1195. /*mutex_unlock(&zr->resource_lock);*/
  1196. }
  1197. return res;
  1198. }
  1199. static int
  1200. zoran_close (struct inode *inode,
  1201. struct file *file)
  1202. {
  1203. struct zoran_fh *fh = file->private_data;
  1204. struct zoran *zr = fh->zr;
  1205. dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
  1206. ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
  1207. /* kernel locks (fs/device.c), so don't do that ourselves
  1208. * (prevents deadlocks) */
  1209. /*mutex_lock(&zr->resource_lock);*/
  1210. zoran_close_end_session(file);
  1211. if (zr->user-- == 1) { /* Last process */
  1212. /* Clean up JPEG process */
  1213. wake_up_interruptible(&zr->jpg_capq);
  1214. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1215. zr->jpg_buffers.allocated = 0;
  1216. zr->jpg_buffers.active = ZORAN_FREE;
  1217. /* disable interrupts */
  1218. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1219. if (*zr_debug > 1)
  1220. print_interrupts(zr);
  1221. /* Overlay off */
  1222. zr->v4l_overlay_active = 0;
  1223. zr36057_overlay(zr, 0);
  1224. zr->overlay_mask = NULL;
  1225. /* capture off */
  1226. wake_up_interruptible(&zr->v4l_capq);
  1227. zr36057_set_memgrab(zr, 0);
  1228. zr->v4l_buffers.allocated = 0;
  1229. zr->v4l_buffers.active = ZORAN_FREE;
  1230. zoran_set_pci_master(zr, 0);
  1231. if (!pass_through) { /* Switch to color bar */
  1232. int zero = 0, two = 2;
  1233. decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
  1234. encoder_command(zr, ENCODER_SET_INPUT, &two);
  1235. }
  1236. }
  1237. file->private_data = NULL;
  1238. kfree(fh->overlay_mask);
  1239. kfree(fh);
  1240. /* release locks on the i2c modules */
  1241. module_put(zr->decoder->driver->driver.owner);
  1242. if (zr->encoder) {
  1243. module_put(zr->encoder->driver->driver.owner);
  1244. }
  1245. module_put(THIS_MODULE);
  1246. /*mutex_unlock(&zr->resource_lock);*/
  1247. dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
  1248. return 0;
  1249. }
  1250. static ssize_t
  1251. zoran_read (struct file *file,
  1252. char __user *data,
  1253. size_t count,
  1254. loff_t *ppos)
  1255. {
  1256. /* we simply don't support read() (yet)... */
  1257. return -EINVAL;
  1258. }
  1259. static ssize_t
  1260. zoran_write (struct file *file,
  1261. const char __user *data,
  1262. size_t count,
  1263. loff_t *ppos)
  1264. {
  1265. /* ...and the same goes for write() */
  1266. return -EINVAL;
  1267. }
  1268. static int
  1269. setup_fbuffer (struct file *file,
  1270. void *base,
  1271. const struct zoran_format *fmt,
  1272. int width,
  1273. int height,
  1274. int bytesperline)
  1275. {
  1276. struct zoran_fh *fh = file->private_data;
  1277. struct zoran *zr = fh->zr;
  1278. /* (Ronald) v4l/v4l2 guidelines */
  1279. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1280. return -EPERM;
  1281. /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
  1282. ALi Magik (that needs very low latency while the card needs a
  1283. higher value always) */
  1284. if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
  1285. return -ENXIO;
  1286. /* we need a bytesperline value, even if not given */
  1287. if (!bytesperline)
  1288. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  1289. #if 0
  1290. if (zr->overlay_active) {
  1291. /* dzjee... stupid users... don't even bother to turn off
  1292. * overlay before changing the memory location...
  1293. * normally, we would return errors here. However, one of
  1294. * the tools that does this is... xawtv! and since xawtv
  1295. * is used by +/- 99% of the users, we'd rather be user-
  1296. * friendly and silently do as if nothing went wrong */
  1297. dprintk(3,
  1298. KERN_ERR
  1299. "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
  1300. ZR_DEVNAME(zr));
  1301. zr36057_overlay(zr, 0);
  1302. }
  1303. #endif
  1304. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  1305. dprintk(1,
  1306. KERN_ERR
  1307. "%s: setup_fbuffer() - no valid overlay format given\n",
  1308. ZR_DEVNAME(zr));
  1309. return -EINVAL;
  1310. }
  1311. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  1312. dprintk(1,
  1313. KERN_ERR
  1314. "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
  1315. ZR_DEVNAME(zr), width, height, bytesperline);
  1316. return -EINVAL;
  1317. }
  1318. if (bytesperline & 3) {
  1319. dprintk(1,
  1320. KERN_ERR
  1321. "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
  1322. ZR_DEVNAME(zr), bytesperline);
  1323. return -EINVAL;
  1324. }
  1325. zr->buffer.base = (void *) ((unsigned long) base & ~3);
  1326. zr->buffer.height = height;
  1327. zr->buffer.width = width;
  1328. zr->buffer.depth = fmt->depth;
  1329. zr->overlay_settings.format = fmt;
  1330. zr->buffer.bytesperline = bytesperline;
  1331. /* The user should set new window parameters */
  1332. zr->overlay_settings.is_set = 0;
  1333. return 0;
  1334. }
  1335. static int
  1336. setup_window (struct file *file,
  1337. int x,
  1338. int y,
  1339. int width,
  1340. int height,
  1341. struct video_clip __user *clips,
  1342. int clipcount,
  1343. void __user *bitmap)
  1344. {
  1345. struct zoran_fh *fh = file->private_data;
  1346. struct zoran *zr = fh->zr;
  1347. struct video_clip *vcp = NULL;
  1348. int on, end;
  1349. if (!zr->buffer.base) {
  1350. dprintk(1,
  1351. KERN_ERR
  1352. "%s: setup_window() - frame buffer has to be set first\n",
  1353. ZR_DEVNAME(zr));
  1354. return -EINVAL;
  1355. }
  1356. if (!fh->overlay_settings.format) {
  1357. dprintk(1,
  1358. KERN_ERR
  1359. "%s: setup_window() - no overlay format set\n",
  1360. ZR_DEVNAME(zr));
  1361. return -EINVAL;
  1362. }
  1363. /*
  1364. * The video front end needs 4-byte alinged line sizes, we correct that
  1365. * silently here if necessary
  1366. */
  1367. if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
  1368. end = (x + width) & ~1; /* round down */
  1369. x = (x + 1) & ~1; /* round up */
  1370. width = end - x;
  1371. }
  1372. if (zr->buffer.depth == 24) {
  1373. end = (x + width) & ~3; /* round down */
  1374. x = (x + 3) & ~3; /* round up */
  1375. width = end - x;
  1376. }
  1377. if (width > BUZ_MAX_WIDTH)
  1378. width = BUZ_MAX_WIDTH;
  1379. if (height > BUZ_MAX_HEIGHT)
  1380. height = BUZ_MAX_HEIGHT;
  1381. /* Check for vaild parameters */
  1382. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1383. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1384. dprintk(1,
  1385. KERN_ERR
  1386. "%s: setup_window() - width = %d or height = %d invalid\n",
  1387. ZR_DEVNAME(zr), width, height);
  1388. return -EINVAL;
  1389. }
  1390. fh->overlay_settings.x = x;
  1391. fh->overlay_settings.y = y;
  1392. fh->overlay_settings.width = width;
  1393. fh->overlay_settings.height = height;
  1394. fh->overlay_settings.clipcount = clipcount;
  1395. /*
  1396. * If an overlay is running, we have to switch it off
  1397. * and switch it on again in order to get the new settings in effect.
  1398. *
  1399. * We also want to avoid that the overlay mask is written
  1400. * when an overlay is running.
  1401. */
  1402. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1403. zr->overlay_active != ZORAN_FREE &&
  1404. fh->overlay_active != ZORAN_FREE;
  1405. if (on)
  1406. zr36057_overlay(zr, 0);
  1407. /*
  1408. * Write the overlay mask if clips are wanted.
  1409. * We prefer a bitmap.
  1410. */
  1411. if (bitmap) {
  1412. /* fake value - it just means we want clips */
  1413. fh->overlay_settings.clipcount = 1;
  1414. if (copy_from_user(fh->overlay_mask, bitmap,
  1415. (width * height + 7) / 8)) {
  1416. return -EFAULT;
  1417. }
  1418. } else if (clipcount > 0) {
  1419. /* write our own bitmap from the clips */
  1420. vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
  1421. if (vcp == NULL) {
  1422. dprintk(1,
  1423. KERN_ERR
  1424. "%s: setup_window() - Alloc of clip mask failed\n",
  1425. ZR_DEVNAME(zr));
  1426. return -ENOMEM;
  1427. }
  1428. if (copy_from_user
  1429. (vcp, clips, sizeof(struct video_clip) * clipcount)) {
  1430. vfree(vcp);
  1431. return -EFAULT;
  1432. }
  1433. write_overlay_mask(file, vcp, clipcount);
  1434. vfree(vcp);
  1435. }
  1436. fh->overlay_settings.is_set = 1;
  1437. if (fh->overlay_active != ZORAN_FREE &&
  1438. zr->overlay_active != ZORAN_FREE)
  1439. zr->overlay_settings = fh->overlay_settings;
  1440. if (on)
  1441. zr36057_overlay(zr, 1);
  1442. /* Make sure the changes come into effect */
  1443. return wait_grab_pending(zr);
  1444. }
  1445. static int
  1446. setup_overlay (struct file *file,
  1447. int on)
  1448. {
  1449. struct zoran_fh *fh = file->private_data;
  1450. struct zoran *zr = fh->zr;
  1451. /* If there is nothing to do, return immediatly */
  1452. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1453. (!on && fh->overlay_active == ZORAN_FREE))
  1454. return 0;
  1455. /* check whether we're touching someone else's overlay */
  1456. if (on && zr->overlay_active != ZORAN_FREE &&
  1457. fh->overlay_active == ZORAN_FREE) {
  1458. dprintk(1,
  1459. KERN_ERR
  1460. "%s: setup_overlay() - overlay is already active for another session\n",
  1461. ZR_DEVNAME(zr));
  1462. return -EBUSY;
  1463. }
  1464. if (!on && zr->overlay_active != ZORAN_FREE &&
  1465. fh->overlay_active == ZORAN_FREE) {
  1466. dprintk(1,
  1467. KERN_ERR
  1468. "%s: setup_overlay() - you cannot cancel someone else's session\n",
  1469. ZR_DEVNAME(zr));
  1470. return -EPERM;
  1471. }
  1472. if (on == 0) {
  1473. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1474. zr->v4l_overlay_active = 0;
  1475. /* When a grab is running, the video simply
  1476. * won't be switched on any more */
  1477. if (!zr->v4l_memgrab_active)
  1478. zr36057_overlay(zr, 0);
  1479. zr->overlay_mask = NULL;
  1480. } else {
  1481. if (!zr->buffer.base || !fh->overlay_settings.is_set) {
  1482. dprintk(1,
  1483. KERN_ERR
  1484. "%s: setup_overlay() - buffer or window not set\n",
  1485. ZR_DEVNAME(zr));
  1486. return -EINVAL;
  1487. }
  1488. if (!fh->overlay_settings.format) {
  1489. dprintk(1,
  1490. KERN_ERR
  1491. "%s: setup_overlay() - no overlay format set\n",
  1492. ZR_DEVNAME(zr));
  1493. return -EINVAL;
  1494. }
  1495. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1496. zr->v4l_overlay_active = 1;
  1497. zr->overlay_mask = fh->overlay_mask;
  1498. zr->overlay_settings = fh->overlay_settings;
  1499. if (!zr->v4l_memgrab_active)
  1500. zr36057_overlay(zr, 1);
  1501. /* When a grab is running, the video will be
  1502. * switched on when grab is finished */
  1503. }
  1504. /* Make sure the changes come into effect */
  1505. return wait_grab_pending(zr);
  1506. }
  1507. #ifdef CONFIG_VIDEO_V4L2
  1508. /* get the status of a buffer in the clients buffer queue */
  1509. static int
  1510. zoran_v4l2_buffer_status (struct file *file,
  1511. struct v4l2_buffer *buf,
  1512. int num)
  1513. {
  1514. struct zoran_fh *fh = file->private_data;
  1515. struct zoran *zr = fh->zr;
  1516. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1517. switch (fh->map_mode) {
  1518. case ZORAN_MAP_MODE_RAW:
  1519. /* check range */
  1520. if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
  1521. !fh->v4l_buffers.allocated) {
  1522. dprintk(1,
  1523. KERN_ERR
  1524. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1525. ZR_DEVNAME(zr));
  1526. return -EINVAL;
  1527. }
  1528. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1529. buf->length = fh->v4l_buffers.buffer_size;
  1530. /* get buffer */
  1531. buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
  1532. if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1533. fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
  1534. buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
  1535. buf->flags |= V4L2_BUF_FLAG_DONE;
  1536. buf->timestamp =
  1537. fh->v4l_buffers.buffer[num].bs.timestamp;
  1538. } else {
  1539. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1540. }
  1541. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1542. buf->field = V4L2_FIELD_TOP;
  1543. else
  1544. buf->field = V4L2_FIELD_INTERLACED;
  1545. break;
  1546. case ZORAN_MAP_MODE_JPG_REC:
  1547. case ZORAN_MAP_MODE_JPG_PLAY:
  1548. /* check range */
  1549. if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
  1550. !fh->jpg_buffers.allocated) {
  1551. dprintk(1,
  1552. KERN_ERR
  1553. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1554. ZR_DEVNAME(zr));
  1555. return -EINVAL;
  1556. }
  1557. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1558. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1559. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1560. buf->length = fh->jpg_buffers.buffer_size;
  1561. /* these variables are only written after frame has been captured */
  1562. if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1563. fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
  1564. buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
  1565. buf->timestamp =
  1566. fh->jpg_buffers.buffer[num].bs.timestamp;
  1567. buf->bytesused =
  1568. fh->jpg_buffers.buffer[num].bs.length;
  1569. buf->flags |= V4L2_BUF_FLAG_DONE;
  1570. } else {
  1571. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1572. }
  1573. /* which fields are these? */
  1574. if (fh->jpg_settings.TmpDcm != 1)
  1575. buf->field =
  1576. fh->jpg_settings.
  1577. odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1578. else
  1579. buf->field =
  1580. fh->jpg_settings.
  1581. odd_even ? V4L2_FIELD_SEQ_TB :
  1582. V4L2_FIELD_SEQ_BT;
  1583. break;
  1584. default:
  1585. dprintk(5,
  1586. KERN_ERR
  1587. "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
  1588. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  1589. return -EINVAL;
  1590. }
  1591. buf->memory = V4L2_MEMORY_MMAP;
  1592. buf->index = num;
  1593. buf->m.offset = buf->length * num;
  1594. return 0;
  1595. }
  1596. #endif
  1597. static int
  1598. zoran_set_norm (struct zoran *zr,
  1599. int norm) /* VIDEO_MODE_* */
  1600. {
  1601. int norm_encoder, on;
  1602. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1603. zr->jpg_buffers.active != ZORAN_FREE) {
  1604. dprintk(1,
  1605. KERN_WARNING
  1606. "%s: set_norm() called while in playback/capture mode\n",
  1607. ZR_DEVNAME(zr));
  1608. return -EBUSY;
  1609. }
  1610. if (lock_norm && norm != zr->norm) {
  1611. if (lock_norm > 1) {
  1612. dprintk(1,
  1613. KERN_WARNING
  1614. "%s: set_norm() - TV standard is locked, can not switch norm\n",
  1615. ZR_DEVNAME(zr));
  1616. return -EPERM;
  1617. } else {
  1618. dprintk(1,
  1619. KERN_WARNING
  1620. "%s: set_norm() - TV standard is locked, norm was not changed\n",
  1621. ZR_DEVNAME(zr));
  1622. norm = zr->norm;
  1623. }
  1624. }
  1625. if (norm != VIDEO_MODE_AUTO &&
  1626. (norm < 0 || norm >= zr->card.norms ||
  1627. !zr->card.tvn[norm])) {
  1628. dprintk(1,
  1629. KERN_ERR "%s: set_norm() - unsupported norm %d\n",
  1630. ZR_DEVNAME(zr), norm);
  1631. return -EINVAL;
  1632. }
  1633. if (norm == VIDEO_MODE_AUTO) {
  1634. int status;
  1635. /* if we have autodetect, ... */
  1636. struct video_decoder_capability caps;
  1637. decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
  1638. if (!(caps.flags & VIDEO_DECODER_AUTO)) {
  1639. dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
  1640. ZR_DEVNAME(zr));
  1641. return -EINVAL;
  1642. }
  1643. decoder_command(zr, DECODER_SET_NORM, &norm);
  1644. /* let changes come into effect */
  1645. ssleep(2);
  1646. decoder_command(zr, DECODER_GET_STATUS, &status);
  1647. if (!(status & DECODER_STATUS_GOOD)) {
  1648. dprintk(1,
  1649. KERN_ERR
  1650. "%s: set_norm() - no norm detected\n",
  1651. ZR_DEVNAME(zr));
  1652. /* reset norm */
  1653. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  1654. return -EIO;
  1655. }
  1656. if (status & DECODER_STATUS_NTSC)
  1657. norm = VIDEO_MODE_NTSC;
  1658. else if (status & DECODER_STATUS_SECAM)
  1659. norm = VIDEO_MODE_SECAM;
  1660. else
  1661. norm = VIDEO_MODE_PAL;
  1662. }
  1663. zr->timing = zr->card.tvn[norm];
  1664. norm_encoder = norm;
  1665. /* We switch overlay off and on since a change in the
  1666. * norm needs different VFE settings */
  1667. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1668. if (on)
  1669. zr36057_overlay(zr, 0);
  1670. decoder_command(zr, DECODER_SET_NORM, &norm);
  1671. encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
  1672. if (on)
  1673. zr36057_overlay(zr, 1);
  1674. /* Make sure the changes come into effect */
  1675. zr->norm = norm;
  1676. return 0;
  1677. }
  1678. static int
  1679. zoran_set_input (struct zoran *zr,
  1680. int input)
  1681. {
  1682. int realinput;
  1683. if (input == zr->input) {
  1684. return 0;
  1685. }
  1686. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1687. zr->jpg_buffers.active != ZORAN_FREE) {
  1688. dprintk(1,
  1689. KERN_WARNING
  1690. "%s: set_input() called while in playback/capture mode\n",
  1691. ZR_DEVNAME(zr));
  1692. return -EBUSY;
  1693. }
  1694. if (input < 0 || input >= zr->card.inputs) {
  1695. dprintk(1,
  1696. KERN_ERR
  1697. "%s: set_input() - unnsupported input %d\n",
  1698. ZR_DEVNAME(zr), input);
  1699. return -EINVAL;
  1700. }
  1701. realinput = zr->card.input[input].muxsel;
  1702. zr->input = input;
  1703. decoder_command(zr, DECODER_SET_INPUT, &realinput);
  1704. return 0;
  1705. }
  1706. /*
  1707. * ioctl routine
  1708. */
  1709. static int
  1710. zoran_do_ioctl (struct inode *inode,
  1711. struct file *file,
  1712. unsigned int cmd,
  1713. void *arg)
  1714. {
  1715. struct zoran_fh *fh = file->private_data;
  1716. struct zoran *zr = fh->zr;
  1717. /* CAREFUL: used in multiple places here */
  1718. struct zoran_jpg_settings settings;
  1719. /* we might have older buffers lying around... We don't want
  1720. * to wait, but we do want to try cleaning them up ASAP. So
  1721. * we try to obtain the lock and free them. If that fails, we
  1722. * don't do anything and wait for the next turn. In the end,
  1723. * zoran_close() or a new allocation will still free them...
  1724. * This is just a 'the sooner the better' extra 'feature'
  1725. *
  1726. * We don't free the buffers right on munmap() because that
  1727. * causes oopses (kfree() inside munmap() oopses for no
  1728. * apparent reason - it's also not reproduceable in any way,
  1729. * but moving the free code outside the munmap() handler fixes
  1730. * all this... If someone knows why, please explain me (Ronald)
  1731. */
  1732. if (mutex_trylock(&zr->resource_lock)) {
  1733. /* we obtained it! Let's try to free some things */
  1734. if (fh->jpg_buffers.ready_to_be_freed)
  1735. jpg_fbuffer_free(file);
  1736. if (fh->v4l_buffers.ready_to_be_freed)
  1737. v4l_fbuffer_free(file);
  1738. mutex_unlock(&zr->resource_lock);
  1739. }
  1740. switch (cmd) {
  1741. case VIDIOCGCAP:
  1742. {
  1743. struct video_capability *vcap = arg;
  1744. dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
  1745. memset(vcap, 0, sizeof(struct video_capability));
  1746. strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
  1747. vcap->type = ZORAN_VID_TYPE;
  1748. vcap->channels = zr->card.inputs;
  1749. vcap->audios = 0;
  1750. mutex_lock(&zr->resource_lock);
  1751. vcap->maxwidth = BUZ_MAX_WIDTH;
  1752. vcap->maxheight = BUZ_MAX_HEIGHT;
  1753. vcap->minwidth = BUZ_MIN_WIDTH;
  1754. vcap->minheight = BUZ_MIN_HEIGHT;
  1755. mutex_unlock(&zr->resource_lock);
  1756. return 0;
  1757. }
  1758. break;
  1759. case VIDIOCGCHAN:
  1760. {
  1761. struct video_channel *vchan = arg;
  1762. int channel = vchan->channel;
  1763. dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
  1764. ZR_DEVNAME(zr), vchan->channel);
  1765. memset(vchan, 0, sizeof(struct video_channel));
  1766. if (channel > zr->card.inputs || channel < 0) {
  1767. dprintk(1,
  1768. KERN_ERR
  1769. "%s: VIDIOCGCHAN on not existing channel %d\n",
  1770. ZR_DEVNAME(zr), channel);
  1771. return -EINVAL;
  1772. }
  1773. strcpy(vchan->name, zr->card.input[channel].name);
  1774. vchan->tuners = 0;
  1775. vchan->flags = 0;
  1776. vchan->type = VIDEO_TYPE_CAMERA;
  1777. mutex_lock(&zr->resource_lock);
  1778. vchan->norm = zr->norm;
  1779. mutex_unlock(&zr->resource_lock);
  1780. vchan->channel = channel;
  1781. return 0;
  1782. }
  1783. break;
  1784. /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
  1785. *
  1786. * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
  1787. * * ^^^^^^^
  1788. * * The famos BTTV driver has it implemented with a struct video_channel argument
  1789. * * and we follow it for compatibility reasons
  1790. * *
  1791. * * BTW: this is the only way the user can set the norm!
  1792. */
  1793. case VIDIOCSCHAN:
  1794. {
  1795. struct video_channel *vchan = arg;
  1796. int res;
  1797. dprintk(3,
  1798. KERN_DEBUG
  1799. "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
  1800. ZR_DEVNAME(zr), vchan->channel, vchan->norm);
  1801. mutex_lock(&zr->resource_lock);
  1802. if ((res = zoran_set_input(zr, vchan->channel)))
  1803. goto schan_unlock_and_return;
  1804. if ((res = zoran_set_norm(zr, vchan->norm)))
  1805. goto schan_unlock_and_return;
  1806. /* Make sure the changes come into effect */
  1807. res = wait_grab_pending(zr);
  1808. schan_unlock_and_return:
  1809. mutex_unlock(&zr->resource_lock);
  1810. return res;
  1811. }
  1812. break;
  1813. case VIDIOCGPICT:
  1814. {
  1815. struct video_picture *vpict = arg;
  1816. dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
  1817. memset(vpict, 0, sizeof(struct video_picture));
  1818. mutex_lock(&zr->resource_lock);
  1819. vpict->hue = zr->hue;
  1820. vpict->brightness = zr->brightness;
  1821. vpict->contrast = zr->contrast;
  1822. vpict->colour = zr->saturation;
  1823. if (fh->overlay_settings.format) {
  1824. vpict->depth = fh->overlay_settings.format->depth;
  1825. vpict->palette = fh->overlay_settings.format->palette;
  1826. } else {
  1827. vpict->depth = 0;
  1828. }
  1829. mutex_unlock(&zr->resource_lock);
  1830. return 0;
  1831. }
  1832. break;
  1833. case VIDIOCSPICT:
  1834. {
  1835. struct video_picture *vpict = arg;
  1836. int i;
  1837. dprintk(3,
  1838. KERN_DEBUG
  1839. "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
  1840. ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
  1841. vpict->colour, vpict->contrast, vpict->depth,
  1842. vpict->palette);
  1843. for (i = 0; i < NUM_FORMATS; i++) {
  1844. const struct zoran_format *fmt = &zoran_formats[i];
  1845. if (fmt->palette != -1 &&
  1846. fmt->flags & ZORAN_FORMAT_OVERLAY &&
  1847. fmt->palette == vpict->palette &&
  1848. fmt->depth == vpict->depth)
  1849. break;
  1850. }
  1851. if (i == NUM_FORMATS) {
  1852. dprintk(1,
  1853. KERN_ERR
  1854. "%s: VIDIOCSPICT - Invalid palette %d\n",
  1855. ZR_DEVNAME(zr), vpict->palette);
  1856. return -EINVAL;
  1857. }
  1858. mutex_lock(&zr->resource_lock);
  1859. decoder_command(zr, DECODER_SET_PICTURE, vpict);
  1860. zr->hue = vpict->hue;
  1861. zr->contrast = vpict->contrast;
  1862. zr->saturation = vpict->colour;
  1863. zr->brightness = vpict->brightness;
  1864. fh->overlay_settings.format = &zoran_formats[i];
  1865. mutex_unlock(&zr->resource_lock);
  1866. return 0;
  1867. }
  1868. break;
  1869. case VIDIOCCAPTURE:
  1870. {
  1871. int *on = arg, res;
  1872. dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
  1873. ZR_DEVNAME(zr), *on);
  1874. mutex_lock(&zr->resource_lock);
  1875. res = setup_overlay(file, *on);
  1876. mutex_unlock(&zr->resource_lock);
  1877. return res;
  1878. }
  1879. break;
  1880. case VIDIOCGWIN:
  1881. {
  1882. struct video_window *vwin = arg;
  1883. dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
  1884. memset(vwin, 0, sizeof(struct video_window));
  1885. mutex_lock(&zr->resource_lock);
  1886. vwin->x = fh->overlay_settings.x;
  1887. vwin->y = fh->overlay_settings.y;
  1888. vwin->width = fh->overlay_settings.width;
  1889. vwin->height = fh->overlay_settings.height;
  1890. mutex_unlock(&zr->resource_lock);
  1891. vwin->clipcount = 0;
  1892. return 0;
  1893. }
  1894. break;
  1895. case VIDIOCSWIN:
  1896. {
  1897. struct video_window *vwin = arg;
  1898. int res;
  1899. dprintk(3,
  1900. KERN_DEBUG
  1901. "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
  1902. ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
  1903. vwin->height, vwin->clipcount);
  1904. mutex_lock(&zr->resource_lock);
  1905. res =
  1906. setup_window(file, vwin->x, vwin->y, vwin->width,
  1907. vwin->height, vwin->clips,
  1908. vwin->clipcount, NULL);
  1909. mutex_unlock(&zr->resource_lock);
  1910. return res;
  1911. }
  1912. break;
  1913. case VIDIOCGFBUF:
  1914. {
  1915. struct video_buffer *vbuf = arg;
  1916. dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
  1917. mutex_lock(&zr->resource_lock);
  1918. *vbuf = zr->buffer;
  1919. mutex_unlock(&zr->resource_lock);
  1920. return 0;
  1921. }
  1922. break;
  1923. case VIDIOCSFBUF:
  1924. {
  1925. struct video_buffer *vbuf = arg;
  1926. int i, res = 0;
  1927. dprintk(3,
  1928. KERN_DEBUG
  1929. "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
  1930. ZR_DEVNAME(zr), vbuf->base, vbuf->width,
  1931. vbuf->height, vbuf->depth, vbuf->bytesperline);
  1932. for (i = 0; i < NUM_FORMATS; i++)
  1933. if (zoran_formats[i].depth == vbuf->depth)
  1934. break;
  1935. if (i == NUM_FORMATS) {
  1936. dprintk(1,
  1937. KERN_ERR
  1938. "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
  1939. ZR_DEVNAME(zr), vbuf->depth);
  1940. return -EINVAL;
  1941. }
  1942. mutex_lock(&zr->resource_lock);
  1943. res =
  1944. setup_fbuffer(file, vbuf->base, &zoran_formats[i],
  1945. vbuf->width, vbuf->height,
  1946. vbuf->bytesperline);
  1947. mutex_unlock(&zr->resource_lock);
  1948. return res;
  1949. }
  1950. break;
  1951. case VIDIOCSYNC:
  1952. {
  1953. int *frame = arg, res;
  1954. dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
  1955. ZR_DEVNAME(zr), *frame);
  1956. mutex_lock(&zr->resource_lock);
  1957. res = v4l_sync(file, *frame);
  1958. mutex_unlock(&zr->resource_lock);
  1959. if (!res)
  1960. zr->v4l_sync_tail++;
  1961. return res;
  1962. }
  1963. break;
  1964. case VIDIOCMCAPTURE:
  1965. {
  1966. struct video_mmap *vmap = arg;
  1967. int res;
  1968. dprintk(3,
  1969. KERN_DEBUG
  1970. "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
  1971. ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
  1972. vmap->format);
  1973. mutex_lock(&zr->resource_lock);
  1974. res = v4l_grab(file, vmap);
  1975. mutex_unlock(&zr->resource_lock);
  1976. return res;
  1977. }
  1978. break;
  1979. case VIDIOCGMBUF:
  1980. {
  1981. struct video_mbuf *vmbuf = arg;
  1982. int i, res = 0;
  1983. dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
  1984. vmbuf->size =
  1985. fh->v4l_buffers.num_buffers *
  1986. fh->v4l_buffers.buffer_size;
  1987. vmbuf->frames = fh->v4l_buffers.num_buffers;
  1988. for (i = 0; i < vmbuf->frames; i++) {
  1989. vmbuf->offsets[i] =
  1990. i * fh->v4l_buffers.buffer_size;
  1991. }
  1992. mutex_lock(&zr->resource_lock);
  1993. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  1994. dprintk(1,
  1995. KERN_ERR
  1996. "%s: VIDIOCGMBUF - buffers already allocated\n",
  1997. ZR_DEVNAME(zr));
  1998. res = -EINVAL;
  1999. goto v4l1reqbuf_unlock_and_return;
  2000. }
  2001. if (v4l_fbuffer_alloc(file)) {
  2002. res = -ENOMEM;
  2003. goto v4l1reqbuf_unlock_and_return;
  2004. }
  2005. /* The next mmap will map the V4L buffers */
  2006. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2007. v4l1reqbuf_unlock_and_return:
  2008. mutex_unlock(&zr->resource_lock);
  2009. return res;
  2010. }
  2011. break;
  2012. case VIDIOCGUNIT:
  2013. {
  2014. struct video_unit *vunit = arg;
  2015. dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
  2016. vunit->video = zr->video_dev->minor;
  2017. vunit->vbi = VIDEO_NO_UNIT;
  2018. vunit->radio = VIDEO_NO_UNIT;
  2019. vunit->audio = VIDEO_NO_UNIT;
  2020. vunit->teletext = VIDEO_NO_UNIT;
  2021. return 0;
  2022. }
  2023. break;
  2024. /*
  2025. * RJ: In principal we could support subcaptures for V4L grabbing.
  2026. * Not even the famous BTTV driver has them, however.
  2027. * If there should be a strong demand, one could consider
  2028. * to implement them.
  2029. */
  2030. case VIDIOCGCAPTURE:
  2031. {
  2032. dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
  2033. ZR_DEVNAME(zr));
  2034. return -EINVAL;
  2035. }
  2036. break;
  2037. case VIDIOCSCAPTURE:
  2038. {
  2039. dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
  2040. ZR_DEVNAME(zr));
  2041. return -EINVAL;
  2042. }
  2043. break;
  2044. case BUZIOC_G_PARAMS:
  2045. {
  2046. struct zoran_params *bparams = arg;
  2047. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  2048. memset(bparams, 0, sizeof(struct zoran_params));
  2049. bparams->major_version = MAJOR_VERSION;
  2050. bparams->minor_version = MINOR_VERSION;
  2051. mutex_lock(&zr->resource_lock);
  2052. bparams->norm = zr->norm;
  2053. bparams->input = zr->input;
  2054. bparams->decimation = fh->jpg_settings.decimation;
  2055. bparams->HorDcm = fh->jpg_settings.HorDcm;
  2056. bparams->VerDcm = fh->jpg_settings.VerDcm;
  2057. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  2058. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  2059. bparams->img_x = fh->jpg_settings.img_x;
  2060. bparams->img_y = fh->jpg_settings.img_y;
  2061. bparams->img_width = fh->jpg_settings.img_width;
  2062. bparams->img_height = fh->jpg_settings.img_height;
  2063. bparams->odd_even = fh->jpg_settings.odd_even;
  2064. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  2065. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  2066. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2067. memcpy(bparams->APP_data,
  2068. fh->jpg_settings.jpg_comp.APP_data,
  2069. sizeof(bparams->APP_data));
  2070. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  2071. memcpy(bparams->COM_data,
  2072. fh->jpg_settings.jpg_comp.COM_data,
  2073. sizeof(bparams->COM_data));
  2074. bparams->jpeg_markers =
  2075. fh->jpg_settings.jpg_comp.jpeg_markers;
  2076. mutex_unlock(&zr->resource_lock);
  2077. bparams->VFIFO_FB = 0;
  2078. return 0;
  2079. }
  2080. break;
  2081. case BUZIOC_S_PARAMS:
  2082. {
  2083. struct zoran_params *bparams = arg;
  2084. int res = 0;
  2085. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  2086. settings.decimation = bparams->decimation;
  2087. settings.HorDcm = bparams->HorDcm;
  2088. settings.VerDcm = bparams->VerDcm;
  2089. settings.TmpDcm = bparams->TmpDcm;
  2090. settings.field_per_buff = bparams->field_per_buff;
  2091. settings.img_x = bparams->img_x;
  2092. settings.img_y = bparams->img_y;
  2093. settings.img_width = bparams->img_width;
  2094. settings.img_height = bparams->img_height;
  2095. settings.odd_even = bparams->odd_even;
  2096. settings.jpg_comp.quality = bparams->quality;
  2097. settings.jpg_comp.APPn = bparams->APPn;
  2098. settings.jpg_comp.APP_len = bparams->APP_len;
  2099. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  2100. sizeof(bparams->APP_data));
  2101. settings.jpg_comp.COM_len = bparams->COM_len;
  2102. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  2103. sizeof(bparams->COM_data));
  2104. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  2105. mutex_lock(&zr->resource_lock);
  2106. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2107. dprintk(1,
  2108. KERN_ERR
  2109. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  2110. ZR_DEVNAME(zr));
  2111. res = -EINVAL;
  2112. goto sparams_unlock_and_return;
  2113. }
  2114. /* Check the params first before overwriting our
  2115. * nternal values */
  2116. if (zoran_check_jpg_settings(zr, &settings)) {
  2117. res = -EINVAL;
  2118. goto sparams_unlock_and_return;
  2119. }
  2120. fh->jpg_settings = settings;
  2121. sparams_unlock_and_return:
  2122. mutex_unlock(&zr->resource_lock);
  2123. return res;
  2124. }
  2125. break;
  2126. case BUZIOC_REQBUFS:
  2127. {
  2128. struct zoran_requestbuffers *breq = arg;
  2129. int res = 0;
  2130. dprintk(3,
  2131. KERN_DEBUG
  2132. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  2133. ZR_DEVNAME(zr), breq->count, breq->size);
  2134. /* Enforce reasonable lower and upper limits */
  2135. if (breq->count < 4)
  2136. breq->count = 4; /* Could be choosen smaller */
  2137. if (breq->count > jpg_nbufs)
  2138. breq->count = jpg_nbufs;
  2139. breq->size = PAGE_ALIGN(breq->size);
  2140. if (breq->size < 8192)
  2141. breq->size = 8192; /* Arbitrary */
  2142. /* breq->size is limited by 1 page for the stat_com
  2143. * tables to a Maximum of 2 MB */
  2144. if (breq->size > jpg_bufsize)
  2145. breq->size = jpg_bufsize;
  2146. if (fh->jpg_buffers.need_contiguous &&
  2147. breq->size > MAX_KMALLOC_MEM)
  2148. breq->size = MAX_KMALLOC_MEM;
  2149. mutex_lock(&zr->resource_lock);
  2150. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2151. dprintk(1,
  2152. KERN_ERR
  2153. "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
  2154. ZR_DEVNAME(zr));
  2155. res = -EBUSY;
  2156. goto jpgreqbuf_unlock_and_return;
  2157. }
  2158. fh->jpg_buffers.num_buffers = breq->count;
  2159. fh->jpg_buffers.buffer_size = breq->size;
  2160. if (jpg_fbuffer_alloc(file)) {
  2161. res = -ENOMEM;
  2162. goto jpgreqbuf_unlock_and_return;
  2163. }
  2164. /* The next mmap will map the MJPEG buffers - could
  2165. * also be *_PLAY, but it doesn't matter here */
  2166. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2167. jpgreqbuf_unlock_and_return:
  2168. mutex_unlock(&zr->resource_lock);
  2169. return res;
  2170. }
  2171. break;
  2172. case BUZIOC_QBUF_CAPT:
  2173. {
  2174. int *frame = arg, res;
  2175. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  2176. ZR_DEVNAME(zr), *frame);
  2177. mutex_lock(&zr->resource_lock);
  2178. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
  2179. mutex_unlock(&zr->resource_lock);
  2180. return res;
  2181. }
  2182. break;
  2183. case BUZIOC_QBUF_PLAY:
  2184. {
  2185. int *frame = arg, res;
  2186. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  2187. ZR_DEVNAME(zr), *frame);
  2188. mutex_lock(&zr->resource_lock);
  2189. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  2190. mutex_unlock(&zr->resource_lock);
  2191. return res;
  2192. }
  2193. break;
  2194. case BUZIOC_SYNC:
  2195. {
  2196. struct zoran_sync *bsync = arg;
  2197. int res;
  2198. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  2199. mutex_lock(&zr->resource_lock);
  2200. res = jpg_sync(file, bsync);
  2201. mutex_unlock(&zr->resource_lock);
  2202. return res;
  2203. }
  2204. break;
  2205. case BUZIOC_G_STATUS:
  2206. {
  2207. struct zoran_status *bstat = arg;
  2208. int norm, input, status, res = 0;
  2209. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  2210. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2211. dprintk(1,
  2212. KERN_ERR
  2213. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  2214. ZR_DEVNAME(zr));
  2215. return -EINVAL;
  2216. }
  2217. input = zr->card.input[bstat->input].muxsel;
  2218. norm = VIDEO_MODE_AUTO;
  2219. mutex_lock(&zr->resource_lock);
  2220. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2221. dprintk(1,
  2222. KERN_ERR
  2223. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  2224. ZR_DEVNAME(zr));
  2225. res = -EINVAL;
  2226. goto gstat_unlock_and_return;
  2227. }
  2228. decoder_command(zr, DECODER_SET_INPUT, &input);
  2229. decoder_command(zr, DECODER_SET_NORM, &norm);
  2230. /* sleep 1 second */
  2231. ssleep(1);
  2232. /* Get status of video decoder */
  2233. decoder_command(zr, DECODER_GET_STATUS, &status);
  2234. /* restore previous input and norm */
  2235. input = zr->card.input[zr->input].muxsel;
  2236. decoder_command(zr, DECODER_SET_INPUT, &input);
  2237. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  2238. gstat_unlock_and_return:
  2239. mutex_unlock(&zr->resource_lock);
  2240. if (!res) {
  2241. bstat->signal =
  2242. (status & DECODER_STATUS_GOOD) ? 1 : 0;
  2243. if (status & DECODER_STATUS_NTSC)
  2244. bstat->norm = VIDEO_MODE_NTSC;
  2245. else if (status & DECODER_STATUS_SECAM)
  2246. bstat->norm = VIDEO_MODE_SECAM;
  2247. else
  2248. bstat->norm = VIDEO_MODE_PAL;
  2249. bstat->color =
  2250. (status & DECODER_STATUS_COLOR) ? 1 : 0;
  2251. }
  2252. return res;
  2253. }
  2254. break;
  2255. #ifdef CONFIG_VIDEO_V4L2
  2256. /* The new video4linux2 capture interface - much nicer than video4linux1, since
  2257. * it allows for integrating the JPEG capturing calls inside standard v4l2
  2258. */
  2259. case VIDIOC_QUERYCAP:
  2260. {
  2261. struct v4l2_capability *cap = arg;
  2262. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
  2263. memset(cap, 0, sizeof(*cap));
  2264. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
  2265. strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
  2266. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  2267. pci_name(zr->pci_dev));
  2268. cap->version =
  2269. KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  2270. RELEASE_VERSION);
  2271. cap->capabilities = ZORAN_V4L2_VID_FLAGS;
  2272. return 0;
  2273. }
  2274. break;
  2275. case VIDIOC_ENUM_FMT:
  2276. {
  2277. struct v4l2_fmtdesc *fmt = arg;
  2278. int index = fmt->index, num = -1, i, flag = 0, type =
  2279. fmt->type;
  2280. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
  2281. ZR_DEVNAME(zr), fmt->index);
  2282. switch (fmt->type) {
  2283. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2284. flag = ZORAN_FORMAT_CAPTURE;
  2285. break;
  2286. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2287. flag = ZORAN_FORMAT_PLAYBACK;
  2288. break;
  2289. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2290. flag = ZORAN_FORMAT_OVERLAY;
  2291. break;
  2292. default:
  2293. dprintk(1,
  2294. KERN_ERR
  2295. "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
  2296. ZR_DEVNAME(zr), fmt->type);
  2297. return -EINVAL;
  2298. }
  2299. for (i = 0; i < NUM_FORMATS; i++) {
  2300. if (zoran_formats[i].flags & flag)
  2301. num++;
  2302. if (num == fmt->index)
  2303. break;
  2304. }
  2305. if (fmt->index < 0 /* late, but not too late */ ||
  2306. i == NUM_FORMATS)
  2307. return -EINVAL;
  2308. memset(fmt, 0, sizeof(*fmt));
  2309. fmt->index = index;
  2310. fmt->type = type;
  2311. strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
  2312. fmt->pixelformat = zoran_formats[i].fourcc;
  2313. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  2314. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  2315. return 0;
  2316. }
  2317. break;
  2318. case VIDIOC_G_FMT:
  2319. {
  2320. struct v4l2_format *fmt = arg;
  2321. int type = fmt->type;
  2322. dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
  2323. memset(fmt, 0, sizeof(*fmt));
  2324. fmt->type = type;
  2325. switch (fmt->type) {
  2326. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2327. mutex_lock(&zr->resource_lock);
  2328. fmt->fmt.win.w.left = fh->overlay_settings.x;
  2329. fmt->fmt.win.w.top = fh->overlay_settings.y;
  2330. fmt->fmt.win.w.width = fh->overlay_settings.width;
  2331. fmt->fmt.win.w.height =
  2332. fh->overlay_settings.height;
  2333. if (fh->overlay_settings.width * 2 >
  2334. BUZ_MAX_HEIGHT)
  2335. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  2336. else
  2337. fmt->fmt.win.field = V4L2_FIELD_TOP;
  2338. mutex_unlock(&zr->resource_lock);
  2339. break;
  2340. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2341. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2342. mutex_lock(&zr->resource_lock);
  2343. if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2344. fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2345. fmt->fmt.pix.width =
  2346. fh->v4l_settings.width;
  2347. fmt->fmt.pix.height =
  2348. fh->v4l_settings.height;
  2349. fmt->fmt.pix.sizeimage =
  2350. fh->v4l_settings.bytesperline *
  2351. fh->v4l_settings.height;
  2352. fmt->fmt.pix.pixelformat =
  2353. fh->v4l_settings.format->fourcc;
  2354. fmt->fmt.pix.colorspace =
  2355. fh->v4l_settings.format->colorspace;
  2356. fmt->fmt.pix.bytesperline = 0;
  2357. if (BUZ_MAX_HEIGHT <
  2358. (fh->v4l_settings.height * 2))
  2359. fmt->fmt.pix.field =
  2360. V4L2_FIELD_INTERLACED;
  2361. else
  2362. fmt->fmt.pix.field =
  2363. V4L2_FIELD_TOP;
  2364. } else {
  2365. fmt->fmt.pix.width =
  2366. fh->jpg_settings.img_width /
  2367. fh->jpg_settings.HorDcm;
  2368. fmt->fmt.pix.height =
  2369. fh->jpg_settings.img_height /
  2370. (fh->jpg_settings.VerDcm *
  2371. fh->jpg_settings.TmpDcm);
  2372. fmt->fmt.pix.sizeimage =
  2373. zoran_v4l2_calc_bufsize(&fh->
  2374. jpg_settings);
  2375. fmt->fmt.pix.pixelformat =
  2376. V4L2_PIX_FMT_MJPEG;
  2377. if (fh->jpg_settings.TmpDcm == 1)
  2378. fmt->fmt.pix.field =
  2379. (fh->jpg_settings.
  2380. odd_even ? V4L2_FIELD_SEQ_BT :
  2381. V4L2_FIELD_SEQ_BT);
  2382. else
  2383. fmt->fmt.pix.field =
  2384. (fh->jpg_settings.
  2385. odd_even ? V4L2_FIELD_TOP :
  2386. V4L2_FIELD_BOTTOM);
  2387. fmt->fmt.pix.bytesperline = 0;
  2388. fmt->fmt.pix.colorspace =
  2389. V4L2_COLORSPACE_SMPTE170M;
  2390. }
  2391. mutex_unlock(&zr->resource_lock);
  2392. break;
  2393. default:
  2394. dprintk(1,
  2395. KERN_ERR
  2396. "%s: VIDIOC_G_FMT - unsupported type %d\n",
  2397. ZR_DEVNAME(zr), fmt->type);
  2398. return -EINVAL;
  2399. }
  2400. return 0;
  2401. }
  2402. break;
  2403. case VIDIOC_S_FMT:
  2404. {
  2405. struct v4l2_format *fmt = arg;
  2406. int i, res = 0;
  2407. __u32 printformat;
  2408. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
  2409. ZR_DEVNAME(zr), fmt->type);
  2410. switch (fmt->type) {
  2411. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2412. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  2413. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  2414. fmt->fmt.win.w.width,
  2415. fmt->fmt.win.w.height,
  2416. fmt->fmt.win.clipcount,
  2417. fmt->fmt.win.bitmap);
  2418. mutex_lock(&zr->resource_lock);
  2419. res =
  2420. setup_window(file, fmt->fmt.win.w.left,
  2421. fmt->fmt.win.w.top,
  2422. fmt->fmt.win.w.width,
  2423. fmt->fmt.win.w.height,
  2424. (struct video_clip __user *)
  2425. fmt->fmt.win.clips,
  2426. fmt->fmt.win.clipcount,
  2427. fmt->fmt.win.bitmap);
  2428. mutex_unlock(&zr->resource_lock);
  2429. return res;
  2430. break;
  2431. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2432. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2433. printformat =
  2434. __cpu_to_le32(fmt->fmt.pix.pixelformat);
  2435. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  2436. fmt->fmt.pix.width, fmt->fmt.pix.height,
  2437. fmt->fmt.pix.pixelformat,
  2438. (char *) &printformat);
  2439. if (fmt->fmt.pix.bytesperline > 0) {
  2440. dprintk(5,
  2441. KERN_ERR "%s: bpl not supported\n",
  2442. ZR_DEVNAME(zr));
  2443. return -EINVAL;
  2444. }
  2445. /* we can be requested to do JPEG/raw playback/capture */
  2446. if (!
  2447. (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2448. (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2449. fmt->fmt.pix.pixelformat ==
  2450. V4L2_PIX_FMT_MJPEG))) {
  2451. dprintk(1,
  2452. KERN_ERR
  2453. "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
  2454. ZR_DEVNAME(zr), fmt->type,
  2455. fmt->fmt.pix.pixelformat,
  2456. (char *) &printformat);
  2457. return -EINVAL;
  2458. }
  2459. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  2460. mutex_lock(&zr->resource_lock);
  2461. settings = fh->jpg_settings;
  2462. if (fh->v4l_buffers.allocated ||
  2463. fh->jpg_buffers.allocated) {
  2464. dprintk(1,
  2465. KERN_ERR
  2466. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2467. ZR_DEVNAME(zr));
  2468. res = -EBUSY;
  2469. goto sfmtjpg_unlock_and_return;
  2470. }
  2471. /* we actually need to set 'real' parameters now */
  2472. if ((fmt->fmt.pix.height * 2) >
  2473. BUZ_MAX_HEIGHT)
  2474. settings.TmpDcm = 1;
  2475. else
  2476. settings.TmpDcm = 2;
  2477. settings.decimation = 0;
  2478. if (fmt->fmt.pix.height <=
  2479. fh->jpg_settings.img_height / 2)
  2480. settings.VerDcm = 2;
  2481. else
  2482. settings.VerDcm = 1;
  2483. if (fmt->fmt.pix.width <=
  2484. fh->jpg_settings.img_width / 4)
  2485. settings.HorDcm = 4;
  2486. else if (fmt->fmt.pix.width <=
  2487. fh->jpg_settings.img_width / 2)
  2488. settings.HorDcm = 2;
  2489. else
  2490. settings.HorDcm = 1;
  2491. if (settings.TmpDcm == 1)
  2492. settings.field_per_buff = 2;
  2493. else
  2494. settings.field_per_buff = 1;
  2495. /* check */
  2496. if ((res =
  2497. zoran_check_jpg_settings(zr,
  2498. &settings)))
  2499. goto sfmtjpg_unlock_and_return;
  2500. /* it's ok, so set them */
  2501. fh->jpg_settings = settings;
  2502. /* tell the user what we actually did */
  2503. fmt->fmt.pix.width =
  2504. settings.img_width / settings.HorDcm;
  2505. fmt->fmt.pix.height =
  2506. settings.img_height * 2 /
  2507. (settings.TmpDcm * settings.VerDcm);
  2508. if (settings.TmpDcm == 1)
  2509. fmt->fmt.pix.field =
  2510. (fh->jpg_settings.
  2511. odd_even ? V4L2_FIELD_SEQ_TB :
  2512. V4L2_FIELD_SEQ_BT);
  2513. else
  2514. fmt->fmt.pix.field =
  2515. (fh->jpg_settings.
  2516. odd_even ? V4L2_FIELD_TOP :
  2517. V4L2_FIELD_BOTTOM);
  2518. fh->jpg_buffers.buffer_size =
  2519. zoran_v4l2_calc_bufsize(&fh->
  2520. jpg_settings);
  2521. fmt->fmt.pix.sizeimage =
  2522. fh->jpg_buffers.buffer_size;
  2523. /* we hereby abuse this variable to show that
  2524. * we're gonna do mjpeg capture */
  2525. fh->map_mode =
  2526. (fmt->type ==
  2527. V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  2528. ZORAN_MAP_MODE_JPG_REC :
  2529. ZORAN_MAP_MODE_JPG_PLAY;
  2530. sfmtjpg_unlock_and_return:
  2531. mutex_unlock(&zr->resource_lock);
  2532. } else {
  2533. for (i = 0; i < NUM_FORMATS; i++)
  2534. if (fmt->fmt.pix.pixelformat ==
  2535. zoran_formats[i].fourcc)
  2536. break;
  2537. if (i == NUM_FORMATS) {
  2538. dprintk(1,
  2539. KERN_ERR
  2540. "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
  2541. ZR_DEVNAME(zr),
  2542. fmt->fmt.pix.pixelformat,
  2543. (char *) &printformat);
  2544. return -EINVAL;
  2545. }
  2546. mutex_lock(&zr->resource_lock);
  2547. if (fh->jpg_buffers.allocated ||
  2548. (fh->v4l_buffers.allocated &&
  2549. fh->v4l_buffers.active !=
  2550. ZORAN_FREE)) {
  2551. dprintk(1,
  2552. KERN_ERR
  2553. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2554. ZR_DEVNAME(zr));
  2555. res = -EBUSY;
  2556. goto sfmtv4l_unlock_and_return;
  2557. }
  2558. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  2559. fmt->fmt.pix.height =
  2560. BUZ_MAX_HEIGHT;
  2561. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  2562. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  2563. if ((res =
  2564. zoran_v4l_set_format(file,
  2565. fmt->fmt.pix.
  2566. width,
  2567. fmt->fmt.pix.
  2568. height,
  2569. &zoran_formats
  2570. [i])))
  2571. goto sfmtv4l_unlock_and_return;
  2572. /* tell the user the
  2573. * results/missing stuff */
  2574. fmt->fmt.pix.sizeimage =
  2575. fh->v4l_settings.height *
  2576. fh->v4l_settings.bytesperline;
  2577. if (BUZ_MAX_HEIGHT <
  2578. (fh->v4l_settings.height * 2))
  2579. fmt->fmt.pix.field =
  2580. V4L2_FIELD_INTERLACED;
  2581. else
  2582. fmt->fmt.pix.field =
  2583. V4L2_FIELD_TOP;
  2584. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2585. sfmtv4l_unlock_and_return:
  2586. mutex_unlock(&zr->resource_lock);
  2587. }
  2588. break;
  2589. default:
  2590. dprintk(3, "unsupported\n");
  2591. dprintk(1,
  2592. KERN_ERR
  2593. "%s: VIDIOC_S_FMT - unsupported type %d\n",
  2594. ZR_DEVNAME(zr), fmt->type);
  2595. return -EINVAL;
  2596. }
  2597. return res;
  2598. }
  2599. break;
  2600. case VIDIOC_G_FBUF:
  2601. {
  2602. struct v4l2_framebuffer *fb = arg;
  2603. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
  2604. memset(fb, 0, sizeof(*fb));
  2605. mutex_lock(&zr->resource_lock);
  2606. fb->base = zr->buffer.base;
  2607. fb->fmt.width = zr->buffer.width;
  2608. fb->fmt.height = zr->buffer.height;
  2609. if (zr->overlay_settings.format) {
  2610. fb->fmt.pixelformat =
  2611. fh->overlay_settings.format->fourcc;
  2612. }
  2613. fb->fmt.bytesperline = zr->buffer.bytesperline;
  2614. mutex_unlock(&zr->resource_lock);
  2615. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  2616. fb->fmt.field = V4L2_FIELD_INTERLACED;
  2617. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  2618. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2619. return 0;
  2620. }
  2621. break;
  2622. case VIDIOC_S_FBUF:
  2623. {
  2624. int i, res = 0;
  2625. struct v4l2_framebuffer *fb = arg;
  2626. __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  2627. dprintk(3,
  2628. KERN_DEBUG
  2629. "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
  2630. ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
  2631. fb->fmt.bytesperline, fb->fmt.pixelformat,
  2632. (char *) &printformat);
  2633. for (i = 0; i < NUM_FORMATS; i++)
  2634. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  2635. break;
  2636. if (i == NUM_FORMATS) {
  2637. dprintk(1,
  2638. KERN_ERR
  2639. "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  2640. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  2641. (char *) &printformat);
  2642. return -EINVAL;
  2643. }
  2644. mutex_lock(&zr->resource_lock);
  2645. res =
  2646. setup_fbuffer(file, fb->base, &zoran_formats[i],
  2647. fb->fmt.width, fb->fmt.height,
  2648. fb->fmt.bytesperline);
  2649. mutex_unlock(&zr->resource_lock);
  2650. return res;
  2651. }
  2652. break;
  2653. case VIDIOC_OVERLAY:
  2654. {
  2655. int *on = arg, res;
  2656. dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
  2657. ZR_DEVNAME(zr), *on);
  2658. mutex_lock(&zr->resource_lock);
  2659. res = setup_overlay(file, *on);
  2660. mutex_unlock(&zr->resource_lock);
  2661. return res;
  2662. }
  2663. break;
  2664. case VIDIOC_REQBUFS:
  2665. {
  2666. struct v4l2_requestbuffers *req = arg;
  2667. int res = 0;
  2668. dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
  2669. ZR_DEVNAME(zr), req->type);
  2670. if (req->memory != V4L2_MEMORY_MMAP) {
  2671. dprintk(1,
  2672. KERN_ERR
  2673. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  2674. ZR_DEVNAME(zr), req->memory);
  2675. return -EINVAL;
  2676. }
  2677. mutex_lock(&zr->resource_lock);
  2678. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  2679. dprintk(1,
  2680. KERN_ERR
  2681. "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
  2682. ZR_DEVNAME(zr));
  2683. res = -EBUSY;
  2684. goto v4l2reqbuf_unlock_and_return;
  2685. }
  2686. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2687. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2688. /* control user input */
  2689. if (req->count < 2)
  2690. req->count = 2;
  2691. if (req->count > v4l_nbufs)
  2692. req->count = v4l_nbufs;
  2693. fh->v4l_buffers.num_buffers = req->count;
  2694. if (v4l_fbuffer_alloc(file)) {
  2695. res = -ENOMEM;
  2696. goto v4l2reqbuf_unlock_and_return;
  2697. }
  2698. /* The next mmap will map the V4L buffers */
  2699. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2700. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2701. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2702. /* we need to calculate size ourselves now */
  2703. if (req->count < 4)
  2704. req->count = 4;
  2705. if (req->count > jpg_nbufs)
  2706. req->count = jpg_nbufs;
  2707. fh->jpg_buffers.num_buffers = req->count;
  2708. fh->jpg_buffers.buffer_size =
  2709. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2710. if (jpg_fbuffer_alloc(file)) {
  2711. res = -ENOMEM;
  2712. goto v4l2reqbuf_unlock_and_return;
  2713. }
  2714. /* The next mmap will map the MJPEG buffers */
  2715. if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2716. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2717. else
  2718. fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
  2719. } else {
  2720. dprintk(1,
  2721. KERN_ERR
  2722. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2723. ZR_DEVNAME(zr), req->type);
  2724. res = -EINVAL;
  2725. goto v4l2reqbuf_unlock_and_return;
  2726. }
  2727. v4l2reqbuf_unlock_and_return:
  2728. mutex_unlock(&zr->resource_lock);
  2729. return 0;
  2730. }
  2731. break;
  2732. case VIDIOC_QUERYBUF:
  2733. {
  2734. struct v4l2_buffer *buf = arg;
  2735. __u32 type = buf->type;
  2736. int index = buf->index, res;
  2737. dprintk(3,
  2738. KERN_DEBUG
  2739. "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
  2740. ZR_DEVNAME(zr), buf->index, buf->type);
  2741. memset(buf, 0, sizeof(buf));
  2742. buf->type = type;
  2743. buf->index = index;
  2744. mutex_lock(&zr->resource_lock);
  2745. res = zoran_v4l2_buffer_status(file, buf, buf->index);
  2746. mutex_unlock(&zr->resource_lock);
  2747. return res;
  2748. }
  2749. break;
  2750. case VIDIOC_QBUF:
  2751. {
  2752. struct v4l2_buffer *buf = arg;
  2753. int res = 0, codec_mode, buf_type;
  2754. dprintk(3,
  2755. KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
  2756. ZR_DEVNAME(zr), buf->type, buf->index);
  2757. mutex_lock(&zr->resource_lock);
  2758. switch (fh->map_mode) {
  2759. case ZORAN_MAP_MODE_RAW:
  2760. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2761. dprintk(1,
  2762. KERN_ERR
  2763. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2764. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2765. res = -EINVAL;
  2766. goto qbuf_unlock_and_return;
  2767. }
  2768. res = zoran_v4l_queue_frame(file, buf->index);
  2769. if (res)
  2770. goto qbuf_unlock_and_return;
  2771. if (!zr->v4l_memgrab_active &&
  2772. fh->v4l_buffers.active == ZORAN_LOCKED)
  2773. zr36057_set_memgrab(zr, 1);
  2774. break;
  2775. case ZORAN_MAP_MODE_JPG_REC:
  2776. case ZORAN_MAP_MODE_JPG_PLAY:
  2777. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2778. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2779. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2780. } else {
  2781. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2782. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2783. }
  2784. if (buf->type != buf_type) {
  2785. dprintk(1,
  2786. KERN_ERR
  2787. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2788. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2789. res = -EINVAL;
  2790. goto qbuf_unlock_and_return;
  2791. }
  2792. res =
  2793. zoran_jpg_queue_frame(file, buf->index,
  2794. codec_mode);
  2795. if (res != 0)
  2796. goto qbuf_unlock_and_return;
  2797. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2798. fh->jpg_buffers.active == ZORAN_LOCKED) {
  2799. zr36057_enable_jpg(zr, codec_mode);
  2800. }
  2801. break;
  2802. default:
  2803. dprintk(1,
  2804. KERN_ERR
  2805. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2806. ZR_DEVNAME(zr), buf->type);
  2807. res = -EINVAL;
  2808. goto qbuf_unlock_and_return;
  2809. }
  2810. qbuf_unlock_and_return:
  2811. mutex_unlock(&zr->resource_lock);
  2812. return res;
  2813. }
  2814. break;
  2815. case VIDIOC_DQBUF:
  2816. {
  2817. struct v4l2_buffer *buf = arg;
  2818. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2819. dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
  2820. ZR_DEVNAME(zr), buf->type);
  2821. mutex_lock(&zr->resource_lock);
  2822. switch (fh->map_mode) {
  2823. case ZORAN_MAP_MODE_RAW:
  2824. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2825. dprintk(1,
  2826. KERN_ERR
  2827. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2828. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2829. res = -EINVAL;
  2830. goto dqbuf_unlock_and_return;
  2831. }
  2832. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2833. if (file->f_flags & O_NONBLOCK &&
  2834. zr->v4l_buffers.buffer[num].state !=
  2835. BUZ_STATE_DONE) {
  2836. res = -EAGAIN;
  2837. goto dqbuf_unlock_and_return;
  2838. }
  2839. res = v4l_sync(file, num);
  2840. if (res)
  2841. goto dqbuf_unlock_and_return;
  2842. else
  2843. zr->v4l_sync_tail++;
  2844. res = zoran_v4l2_buffer_status(file, buf, num);
  2845. break;
  2846. case ZORAN_MAP_MODE_JPG_REC:
  2847. case ZORAN_MAP_MODE_JPG_PLAY:
  2848. {
  2849. struct zoran_sync bs;
  2850. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2851. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2852. else
  2853. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2854. if (buf->type != buf_type) {
  2855. dprintk(1,
  2856. KERN_ERR
  2857. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2858. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2859. res = -EINVAL;
  2860. goto dqbuf_unlock_and_return;
  2861. }
  2862. num =
  2863. zr->jpg_pend[zr->
  2864. jpg_que_tail & BUZ_MASK_FRAME];
  2865. if (file->f_flags & O_NONBLOCK &&
  2866. zr->jpg_buffers.buffer[num].state !=
  2867. BUZ_STATE_DONE) {
  2868. res = -EAGAIN;
  2869. goto dqbuf_unlock_and_return;
  2870. }
  2871. res = jpg_sync(file, &bs);
  2872. if (res)
  2873. goto dqbuf_unlock_and_return;
  2874. res =
  2875. zoran_v4l2_buffer_status(file, buf, bs.frame);
  2876. break;
  2877. }
  2878. default:
  2879. dprintk(1,
  2880. KERN_ERR
  2881. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2882. ZR_DEVNAME(zr), buf->type);
  2883. res = -EINVAL;
  2884. goto dqbuf_unlock_and_return;
  2885. }
  2886. dqbuf_unlock_and_return:
  2887. mutex_unlock(&zr->resource_lock);
  2888. return res;
  2889. }
  2890. break;
  2891. case VIDIOC_STREAMON:
  2892. {
  2893. int res = 0;
  2894. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
  2895. mutex_lock(&zr->resource_lock);
  2896. switch (fh->map_mode) {
  2897. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2898. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2899. fh->v4l_buffers.active != ZORAN_ACTIVE) {
  2900. res = -EBUSY;
  2901. goto strmon_unlock_and_return;
  2902. }
  2903. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2904. ZORAN_LOCKED;
  2905. zr->v4l_settings = fh->v4l_settings;
  2906. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2907. if (!zr->v4l_memgrab_active &&
  2908. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2909. zr36057_set_memgrab(zr, 1);
  2910. }
  2911. break;
  2912. case ZORAN_MAP_MODE_JPG_REC:
  2913. case ZORAN_MAP_MODE_JPG_PLAY:
  2914. /* what is the codec mode right now? */
  2915. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2916. fh->jpg_buffers.active != ZORAN_ACTIVE) {
  2917. res = -EBUSY;
  2918. goto strmon_unlock_and_return;
  2919. }
  2920. zr->jpg_buffers.active = fh->jpg_buffers.active =
  2921. ZORAN_LOCKED;
  2922. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2923. /* Start the jpeg codec when the first frame is queued */
  2924. jpeg_start(zr);
  2925. }
  2926. break;
  2927. default:
  2928. dprintk(1,
  2929. KERN_ERR
  2930. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2931. ZR_DEVNAME(zr), fh->map_mode);
  2932. res = -EINVAL;
  2933. goto strmon_unlock_and_return;
  2934. }
  2935. strmon_unlock_and_return:
  2936. mutex_unlock(&zr->resource_lock);
  2937. return res;
  2938. }
  2939. break;
  2940. case VIDIOC_STREAMOFF:
  2941. {
  2942. int i, res = 0;
  2943. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
  2944. mutex_lock(&zr->resource_lock);
  2945. switch (fh->map_mode) {
  2946. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2947. if (fh->v4l_buffers.active == ZORAN_FREE &&
  2948. zr->v4l_buffers.active != ZORAN_FREE) {
  2949. res = -EPERM; /* stay off other's settings! */
  2950. goto strmoff_unlock_and_return;
  2951. }
  2952. if (zr->v4l_buffers.active == ZORAN_FREE)
  2953. goto strmoff_unlock_and_return;
  2954. /* unload capture */
  2955. if (zr->v4l_memgrab_active) {
  2956. long flags;
  2957. spin_lock_irqsave(&zr->spinlock, flags);
  2958. zr36057_set_memgrab(zr, 0);
  2959. spin_unlock_irqrestore(&zr->spinlock, flags);
  2960. }
  2961. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2962. zr->v4l_buffers.buffer[i].state =
  2963. BUZ_STATE_USER;
  2964. fh->v4l_buffers = zr->v4l_buffers;
  2965. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2966. ZORAN_FREE;
  2967. zr->v4l_grab_seq = 0;
  2968. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  2969. zr->v4l_sync_tail = 0;
  2970. break;
  2971. case ZORAN_MAP_MODE_JPG_REC:
  2972. case ZORAN_MAP_MODE_JPG_PLAY:
  2973. if (fh->jpg_buffers.active == ZORAN_FREE &&
  2974. zr->jpg_buffers.active != ZORAN_FREE) {
  2975. res = -EPERM; /* stay off other's settings! */
  2976. goto strmoff_unlock_and_return;
  2977. }
  2978. if (zr->jpg_buffers.active == ZORAN_FREE)
  2979. goto strmoff_unlock_and_return;
  2980. res =
  2981. jpg_qbuf(file, -1,
  2982. (fh->map_mode ==
  2983. ZORAN_MAP_MODE_JPG_REC) ?
  2984. BUZ_MODE_MOTION_COMPRESS :
  2985. BUZ_MODE_MOTION_DECOMPRESS);
  2986. if (res)
  2987. goto strmoff_unlock_and_return;
  2988. break;
  2989. default:
  2990. dprintk(1,
  2991. KERN_ERR
  2992. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  2993. ZR_DEVNAME(zr), fh->map_mode);
  2994. res = -EINVAL;
  2995. goto strmoff_unlock_and_return;
  2996. }
  2997. strmoff_unlock_and_return:
  2998. mutex_unlock(&zr->resource_lock);
  2999. return res;
  3000. }
  3001. break;
  3002. case VIDIOC_QUERYCTRL:
  3003. {
  3004. struct v4l2_queryctrl *ctrl = arg;
  3005. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
  3006. ZR_DEVNAME(zr), ctrl->id);
  3007. /* we only support hue/saturation/contrast/brightness */
  3008. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3009. ctrl->id > V4L2_CID_HUE)
  3010. return -EINVAL;
  3011. else {
  3012. int id = ctrl->id;
  3013. memset(ctrl, 0, sizeof(*ctrl));
  3014. ctrl->id = id;
  3015. }
  3016. switch (ctrl->id) {
  3017. case V4L2_CID_BRIGHTNESS:
  3018. strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
  3019. break;
  3020. case V4L2_CID_CONTRAST:
  3021. strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
  3022. break;
  3023. case V4L2_CID_SATURATION:
  3024. strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
  3025. break;
  3026. case V4L2_CID_HUE:
  3027. strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
  3028. break;
  3029. }
  3030. ctrl->minimum = 0;
  3031. ctrl->maximum = 65535;
  3032. ctrl->step = 1;
  3033. ctrl->default_value = 32768;
  3034. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  3035. return 0;
  3036. }
  3037. break;
  3038. case VIDIOC_G_CTRL:
  3039. {
  3040. struct v4l2_control *ctrl = arg;
  3041. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
  3042. ZR_DEVNAME(zr), ctrl->id);
  3043. /* we only support hue/saturation/contrast/brightness */
  3044. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3045. ctrl->id > V4L2_CID_HUE)
  3046. return -EINVAL;
  3047. mutex_lock(&zr->resource_lock);
  3048. switch (ctrl->id) {
  3049. case V4L2_CID_BRIGHTNESS:
  3050. ctrl->value = zr->brightness;
  3051. break;
  3052. case V4L2_CID_CONTRAST:
  3053. ctrl->value = zr->contrast;
  3054. break;
  3055. case V4L2_CID_SATURATION:
  3056. ctrl->value = zr->saturation;
  3057. break;
  3058. case V4L2_CID_HUE:
  3059. ctrl->value = zr->hue;
  3060. break;
  3061. }
  3062. mutex_unlock(&zr->resource_lock);
  3063. return 0;
  3064. }
  3065. break;
  3066. case VIDIOC_S_CTRL:
  3067. {
  3068. struct v4l2_control *ctrl = arg;
  3069. struct video_picture pict;
  3070. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
  3071. ZR_DEVNAME(zr), ctrl->id);
  3072. /* we only support hue/saturation/contrast/brightness */
  3073. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3074. ctrl->id > V4L2_CID_HUE)
  3075. return -EINVAL;
  3076. if (ctrl->value < 0 || ctrl->value > 65535) {
  3077. dprintk(1,
  3078. KERN_ERR
  3079. "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
  3080. ZR_DEVNAME(zr), ctrl->value, ctrl->id);
  3081. return -EINVAL;
  3082. }
  3083. mutex_lock(&zr->resource_lock);
  3084. switch (ctrl->id) {
  3085. case V4L2_CID_BRIGHTNESS:
  3086. zr->brightness = ctrl->value;
  3087. break;
  3088. case V4L2_CID_CONTRAST:
  3089. zr->contrast = ctrl->value;
  3090. break;
  3091. case V4L2_CID_SATURATION:
  3092. zr->saturation = ctrl->value;
  3093. break;
  3094. case V4L2_CID_HUE:
  3095. zr->hue = ctrl->value;
  3096. break;
  3097. }
  3098. pict.brightness = zr->brightness;
  3099. pict.contrast = zr->contrast;
  3100. pict.colour = zr->saturation;
  3101. pict.hue = zr->hue;
  3102. decoder_command(zr, DECODER_SET_PICTURE, &pict);
  3103. mutex_unlock(&zr->resource_lock);
  3104. return 0;
  3105. }
  3106. break;
  3107. case VIDIOC_ENUMSTD:
  3108. {
  3109. struct v4l2_standard *std = arg;
  3110. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
  3111. ZR_DEVNAME(zr), std->index);
  3112. if (std->index < 0 || std->index >= (zr->card.norms + 1))
  3113. return -EINVAL;
  3114. else {
  3115. int id = std->index;
  3116. memset(std, 0, sizeof(*std));
  3117. std->index = id;
  3118. }
  3119. if (std->index == zr->card.norms) {
  3120. /* if we have autodetect, ... */
  3121. struct video_decoder_capability caps;
  3122. decoder_command(zr, DECODER_GET_CAPABILITIES,
  3123. &caps);
  3124. if (caps.flags & VIDEO_DECODER_AUTO) {
  3125. std->id = V4L2_STD_ALL;
  3126. strncpy(std->name, "Autodetect", sizeof(std->name)-1);
  3127. return 0;
  3128. } else
  3129. return -EINVAL;
  3130. }
  3131. switch (std->index) {
  3132. case 0:
  3133. std->id = V4L2_STD_PAL;
  3134. strncpy(std->name, "PAL", sizeof(std->name)-1);
  3135. std->frameperiod.numerator = 1;
  3136. std->frameperiod.denominator = 25;
  3137. std->framelines = zr->card.tvn[0]->Ht;
  3138. break;
  3139. case 1:
  3140. std->id = V4L2_STD_NTSC;
  3141. strncpy(std->name, "NTSC", sizeof(std->name)-1);
  3142. std->frameperiod.numerator = 1001;
  3143. std->frameperiod.denominator = 30000;
  3144. std->framelines = zr->card.tvn[1]->Ht;
  3145. break;
  3146. case 2:
  3147. std->id = V4L2_STD_SECAM;
  3148. strncpy(std->name, "SECAM", sizeof(std->name)-1);
  3149. std->frameperiod.numerator = 1;
  3150. std->frameperiod.denominator = 25;
  3151. std->framelines = zr->card.tvn[2]->Ht;
  3152. break;
  3153. }
  3154. return 0;
  3155. }
  3156. break;
  3157. case VIDIOC_G_STD:
  3158. {
  3159. v4l2_std_id *std = arg;
  3160. int norm;
  3161. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
  3162. mutex_lock(&zr->resource_lock);
  3163. norm = zr->norm;
  3164. mutex_unlock(&zr->resource_lock);
  3165. switch (norm) {
  3166. case VIDEO_MODE_PAL:
  3167. *std = V4L2_STD_PAL;
  3168. break;
  3169. case VIDEO_MODE_NTSC:
  3170. *std = V4L2_STD_NTSC;
  3171. break;
  3172. case VIDEO_MODE_SECAM:
  3173. *std = V4L2_STD_SECAM;
  3174. break;
  3175. }
  3176. return 0;
  3177. }
  3178. break;
  3179. case VIDIOC_S_STD:
  3180. {
  3181. int norm = -1, res = 0;
  3182. v4l2_std_id *std = arg;
  3183. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
  3184. ZR_DEVNAME(zr), (unsigned long long)*std);
  3185. if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
  3186. norm = VIDEO_MODE_PAL;
  3187. else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
  3188. norm = VIDEO_MODE_NTSC;
  3189. else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
  3190. norm = VIDEO_MODE_SECAM;
  3191. else if (*std == V4L2_STD_ALL)
  3192. norm = VIDEO_MODE_AUTO;
  3193. else {
  3194. dprintk(1,
  3195. KERN_ERR
  3196. "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
  3197. ZR_DEVNAME(zr), (unsigned long long)*std);
  3198. return -EINVAL;
  3199. }
  3200. mutex_lock(&zr->resource_lock);
  3201. if ((res = zoran_set_norm(zr, norm)))
  3202. goto sstd_unlock_and_return;
  3203. res = wait_grab_pending(zr);
  3204. sstd_unlock_and_return:
  3205. mutex_unlock(&zr->resource_lock);
  3206. return res;
  3207. }
  3208. break;
  3209. case VIDIOC_ENUMINPUT:
  3210. {
  3211. struct v4l2_input *inp = arg;
  3212. int status;
  3213. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
  3214. ZR_DEVNAME(zr), inp->index);
  3215. if (inp->index < 0 || inp->index >= zr->card.inputs)
  3216. return -EINVAL;
  3217. else {
  3218. int id = inp->index;
  3219. memset(inp, 0, sizeof(*inp));
  3220. inp->index = id;
  3221. }
  3222. strncpy(inp->name, zr->card.input[inp->index].name,
  3223. sizeof(inp->name) - 1);
  3224. inp->type = V4L2_INPUT_TYPE_CAMERA;
  3225. inp->std = V4L2_STD_ALL;
  3226. /* Get status of video decoder */
  3227. mutex_lock(&zr->resource_lock);
  3228. decoder_command(zr, DECODER_GET_STATUS, &status);
  3229. mutex_unlock(&zr->resource_lock);
  3230. if (!(status & DECODER_STATUS_GOOD)) {
  3231. inp->status |= V4L2_IN_ST_NO_POWER;
  3232. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  3233. }
  3234. if (!(status & DECODER_STATUS_COLOR))
  3235. inp->status |= V4L2_IN_ST_NO_COLOR;
  3236. return 0;
  3237. }
  3238. break;
  3239. case VIDIOC_G_INPUT:
  3240. {
  3241. int *input = arg;
  3242. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
  3243. mutex_lock(&zr->resource_lock);
  3244. *input = zr->input;
  3245. mutex_unlock(&zr->resource_lock);
  3246. return 0;
  3247. }
  3248. break;
  3249. case VIDIOC_S_INPUT:
  3250. {
  3251. int *input = arg, res = 0;
  3252. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
  3253. ZR_DEVNAME(zr), *input);
  3254. mutex_lock(&zr->resource_lock);
  3255. if ((res = zoran_set_input(zr, *input)))
  3256. goto sinput_unlock_and_return;
  3257. /* Make sure the changes come into effect */
  3258. res = wait_grab_pending(zr);
  3259. sinput_unlock_and_return:
  3260. mutex_unlock(&zr->resource_lock);
  3261. return res;
  3262. }
  3263. break;
  3264. case VIDIOC_ENUMOUTPUT:
  3265. {
  3266. struct v4l2_output *outp = arg;
  3267. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
  3268. ZR_DEVNAME(zr), outp->index);
  3269. if (outp->index != 0)
  3270. return -EINVAL;
  3271. memset(outp, 0, sizeof(*outp));
  3272. outp->index = 0;
  3273. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  3274. strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
  3275. return 0;
  3276. }
  3277. break;
  3278. case VIDIOC_G_OUTPUT:
  3279. {
  3280. int *output = arg;
  3281. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
  3282. *output = 0;
  3283. return 0;
  3284. }
  3285. break;
  3286. case VIDIOC_S_OUTPUT:
  3287. {
  3288. int *output = arg;
  3289. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
  3290. ZR_DEVNAME(zr), *output);
  3291. if (*output != 0)
  3292. return -EINVAL;
  3293. return 0;
  3294. }
  3295. break;
  3296. /* cropping (sub-frame capture) */
  3297. case VIDIOC_CROPCAP:
  3298. {
  3299. struct v4l2_cropcap *cropcap = arg;
  3300. int type = cropcap->type, res = 0;
  3301. dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
  3302. ZR_DEVNAME(zr), cropcap->type);
  3303. memset(cropcap, 0, sizeof(*cropcap));
  3304. cropcap->type = type;
  3305. mutex_lock(&zr->resource_lock);
  3306. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3307. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3308. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3309. dprintk(1,
  3310. KERN_ERR
  3311. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  3312. ZR_DEVNAME(zr));
  3313. res = -EINVAL;
  3314. goto cropcap_unlock_and_return;
  3315. }
  3316. cropcap->bounds.top = cropcap->bounds.left = 0;
  3317. cropcap->bounds.width = BUZ_MAX_WIDTH;
  3318. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  3319. cropcap->defrect.top = cropcap->defrect.left = 0;
  3320. cropcap->defrect.width = BUZ_MIN_WIDTH;
  3321. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  3322. cropcap_unlock_and_return:
  3323. mutex_unlock(&zr->resource_lock);
  3324. return res;
  3325. }
  3326. break;
  3327. case VIDIOC_G_CROP:
  3328. {
  3329. struct v4l2_crop *crop = arg;
  3330. int type = crop->type, res = 0;
  3331. dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
  3332. ZR_DEVNAME(zr), crop->type);
  3333. memset(crop, 0, sizeof(*crop));
  3334. crop->type = type;
  3335. mutex_lock(&zr->resource_lock);
  3336. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3337. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3338. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3339. dprintk(1,
  3340. KERN_ERR
  3341. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3342. ZR_DEVNAME(zr));
  3343. res = -EINVAL;
  3344. goto gcrop_unlock_and_return;
  3345. }
  3346. crop->c.top = fh->jpg_settings.img_y;
  3347. crop->c.left = fh->jpg_settings.img_x;
  3348. crop->c.width = fh->jpg_settings.img_width;
  3349. crop->c.height = fh->jpg_settings.img_height;
  3350. gcrop_unlock_and_return:
  3351. mutex_unlock(&zr->resource_lock);
  3352. return res;
  3353. }
  3354. break;
  3355. case VIDIOC_S_CROP:
  3356. {
  3357. struct v4l2_crop *crop = arg;
  3358. int res = 0;
  3359. settings = fh->jpg_settings;
  3360. dprintk(3,
  3361. KERN_ERR
  3362. "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
  3363. ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
  3364. crop->c.width, crop->c.height);
  3365. mutex_lock(&zr->resource_lock);
  3366. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  3367. dprintk(1,
  3368. KERN_ERR
  3369. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  3370. ZR_DEVNAME(zr));
  3371. res = -EBUSY;
  3372. goto scrop_unlock_and_return;
  3373. }
  3374. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3375. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3376. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3377. dprintk(1,
  3378. KERN_ERR
  3379. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3380. ZR_DEVNAME(zr));
  3381. res = -EINVAL;
  3382. goto scrop_unlock_and_return;
  3383. }
  3384. /* move into a form that we understand */
  3385. settings.img_x = crop->c.left;
  3386. settings.img_y = crop->c.top;
  3387. settings.img_width = crop->c.width;
  3388. settings.img_height = crop->c.height;
  3389. /* check validity */
  3390. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3391. goto scrop_unlock_and_return;
  3392. /* accept */
  3393. fh->jpg_settings = settings;
  3394. scrop_unlock_and_return:
  3395. mutex_unlock(&zr->resource_lock);
  3396. return res;
  3397. }
  3398. break;
  3399. case VIDIOC_G_JPEGCOMP:
  3400. {
  3401. struct v4l2_jpegcompression *params = arg;
  3402. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
  3403. ZR_DEVNAME(zr));
  3404. memset(params, 0, sizeof(*params));
  3405. mutex_lock(&zr->resource_lock);
  3406. params->quality = fh->jpg_settings.jpg_comp.quality;
  3407. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  3408. memcpy(params->APP_data,
  3409. fh->jpg_settings.jpg_comp.APP_data,
  3410. fh->jpg_settings.jpg_comp.APP_len);
  3411. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  3412. memcpy(params->COM_data,
  3413. fh->jpg_settings.jpg_comp.COM_data,
  3414. fh->jpg_settings.jpg_comp.COM_len);
  3415. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  3416. params->jpeg_markers =
  3417. fh->jpg_settings.jpg_comp.jpeg_markers;
  3418. mutex_unlock(&zr->resource_lock);
  3419. return 0;
  3420. }
  3421. break;
  3422. case VIDIOC_S_JPEGCOMP:
  3423. {
  3424. struct v4l2_jpegcompression *params = arg;
  3425. int res = 0;
  3426. settings = fh->jpg_settings;
  3427. dprintk(3,
  3428. KERN_DEBUG
  3429. "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
  3430. ZR_DEVNAME(zr), params->quality, params->APPn,
  3431. params->APP_len, params->COM_len);
  3432. settings.jpg_comp = *params;
  3433. mutex_lock(&zr->resource_lock);
  3434. if (fh->v4l_buffers.active != ZORAN_FREE ||
  3435. fh->jpg_buffers.active != ZORAN_FREE) {
  3436. dprintk(1,
  3437. KERN_WARNING
  3438. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  3439. ZR_DEVNAME(zr));
  3440. res = -EBUSY;
  3441. goto sjpegc_unlock_and_return;
  3442. }
  3443. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3444. goto sjpegc_unlock_and_return;
  3445. if (!fh->jpg_buffers.allocated)
  3446. fh->jpg_buffers.buffer_size =
  3447. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  3448. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  3449. sjpegc_unlock_and_return:
  3450. mutex_unlock(&zr->resource_lock);
  3451. return 0;
  3452. }
  3453. break;
  3454. case VIDIOC_QUERYSTD: /* why is this useful? */
  3455. {
  3456. v4l2_std_id *std = arg;
  3457. dprintk(3,
  3458. KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
  3459. ZR_DEVNAME(zr), (unsigned long long)*std);
  3460. if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
  3461. *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
  3462. zr->card.norms == 3)) {
  3463. return 0;
  3464. }
  3465. return -EINVAL;
  3466. }
  3467. break;
  3468. case VIDIOC_TRY_FMT:
  3469. {
  3470. struct v4l2_format *fmt = arg;
  3471. int res = 0;
  3472. dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
  3473. ZR_DEVNAME(zr), fmt->type);
  3474. switch (fmt->type) {
  3475. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3476. mutex_lock(&zr->resource_lock);
  3477. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  3478. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  3479. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  3480. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  3481. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  3482. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  3483. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  3484. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  3485. mutex_unlock(&zr->resource_lock);
  3486. break;
  3487. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  3488. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  3489. if (fmt->fmt.pix.bytesperline > 0)
  3490. return -EINVAL;
  3491. mutex_lock(&zr->resource_lock);
  3492. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  3493. settings = fh->jpg_settings;
  3494. /* we actually need to set 'real' parameters now */
  3495. if ((fmt->fmt.pix.height * 2) >
  3496. BUZ_MAX_HEIGHT)
  3497. settings.TmpDcm = 1;
  3498. else
  3499. settings.TmpDcm = 2;
  3500. settings.decimation = 0;
  3501. if (fmt->fmt.pix.height <=
  3502. fh->jpg_settings.img_height / 2)
  3503. settings.VerDcm = 2;
  3504. else
  3505. settings.VerDcm = 1;
  3506. if (fmt->fmt.pix.width <=
  3507. fh->jpg_settings.img_width / 4)
  3508. settings.HorDcm = 4;
  3509. else if (fmt->fmt.pix.width <=
  3510. fh->jpg_settings.img_width / 2)
  3511. settings.HorDcm = 2;
  3512. else
  3513. settings.HorDcm = 1;
  3514. if (settings.TmpDcm == 1)
  3515. settings.field_per_buff = 2;
  3516. else
  3517. settings.field_per_buff = 1;
  3518. /* check */
  3519. if ((res =
  3520. zoran_check_jpg_settings(zr,
  3521. &settings)))
  3522. goto tryfmt_unlock_and_return;
  3523. /* tell the user what we actually did */
  3524. fmt->fmt.pix.width =
  3525. settings.img_width / settings.HorDcm;
  3526. fmt->fmt.pix.height =
  3527. settings.img_height * 2 /
  3528. (settings.TmpDcm * settings.VerDcm);
  3529. if (settings.TmpDcm == 1)
  3530. fmt->fmt.pix.field =
  3531. (fh->jpg_settings.
  3532. odd_even ? V4L2_FIELD_SEQ_TB :
  3533. V4L2_FIELD_SEQ_BT);
  3534. else
  3535. fmt->fmt.pix.field =
  3536. (fh->jpg_settings.
  3537. odd_even ? V4L2_FIELD_TOP :
  3538. V4L2_FIELD_BOTTOM);
  3539. fmt->fmt.pix.sizeimage =
  3540. zoran_v4l2_calc_bufsize(&settings);
  3541. } else if (fmt->type ==
  3542. V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  3543. int i;
  3544. for (i = 0; i < NUM_FORMATS; i++)
  3545. if (zoran_formats[i].fourcc ==
  3546. fmt->fmt.pix.pixelformat)
  3547. break;
  3548. if (i == NUM_FORMATS) {
  3549. res = -EINVAL;
  3550. goto tryfmt_unlock_and_return;
  3551. }
  3552. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  3553. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  3554. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  3555. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  3556. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  3557. fmt->fmt.pix.height =
  3558. BUZ_MAX_HEIGHT;
  3559. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  3560. fmt->fmt.pix.height =
  3561. BUZ_MIN_HEIGHT;
  3562. } else {
  3563. res = -EINVAL;
  3564. goto tryfmt_unlock_and_return;
  3565. }
  3566. tryfmt_unlock_and_return:
  3567. mutex_unlock(&zr->resource_lock);
  3568. return res;
  3569. break;
  3570. default:
  3571. return -EINVAL;
  3572. }
  3573. return 0;
  3574. }
  3575. break;
  3576. #endif
  3577. default:
  3578. dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
  3579. ZR_DEVNAME(zr), cmd);
  3580. return -ENOIOCTLCMD;
  3581. break;
  3582. }
  3583. return 0;
  3584. }
  3585. static int
  3586. zoran_ioctl (struct inode *inode,
  3587. struct file *file,
  3588. unsigned int cmd,
  3589. unsigned long arg)
  3590. {
  3591. return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
  3592. }
  3593. static unsigned int
  3594. zoran_poll (struct file *file,
  3595. poll_table *wait)
  3596. {
  3597. struct zoran_fh *fh = file->private_data;
  3598. struct zoran *zr = fh->zr;
  3599. int res = 0, frame;
  3600. unsigned long flags;
  3601. /* we should check whether buffers are ready to be synced on
  3602. * (w/o waits - O_NONBLOCK) here
  3603. * if ready for read (sync), return POLLIN|POLLRDNORM,
  3604. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  3605. * if error, return POLLERR,
  3606. * if no buffers queued or so, return POLLNVAL
  3607. */
  3608. mutex_lock(&zr->resource_lock);
  3609. switch (fh->map_mode) {
  3610. case ZORAN_MAP_MODE_RAW:
  3611. poll_wait(file, &zr->v4l_capq, wait);
  3612. frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  3613. spin_lock_irqsave(&zr->spinlock, flags);
  3614. dprintk(3,
  3615. KERN_DEBUG
  3616. "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
  3617. ZR_DEVNAME(zr), __FUNCTION__,
  3618. "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
  3619. "UPMD"[zr->v4l_buffers.buffer[frame].state],
  3620. zr->v4l_pend_tail, zr->v4l_pend_head);
  3621. /* Process is the one capturing? */
  3622. if (fh->v4l_buffers.active != ZORAN_FREE &&
  3623. /* Buffer ready to DQBUF? */
  3624. zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  3625. res = POLLIN | POLLRDNORM;
  3626. spin_unlock_irqrestore(&zr->spinlock, flags);
  3627. break;
  3628. case ZORAN_MAP_MODE_JPG_REC:
  3629. case ZORAN_MAP_MODE_JPG_PLAY:
  3630. poll_wait(file, &zr->jpg_capq, wait);
  3631. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  3632. spin_lock_irqsave(&zr->spinlock, flags);
  3633. dprintk(3,
  3634. KERN_DEBUG
  3635. "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
  3636. ZR_DEVNAME(zr), __FUNCTION__,
  3637. "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
  3638. "UPMD"[zr->jpg_buffers.buffer[frame].state],
  3639. zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
  3640. if (fh->jpg_buffers.active != ZORAN_FREE &&
  3641. zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  3642. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  3643. res = POLLIN | POLLRDNORM;
  3644. else
  3645. res = POLLOUT | POLLWRNORM;
  3646. }
  3647. spin_unlock_irqrestore(&zr->spinlock, flags);
  3648. break;
  3649. default:
  3650. dprintk(1,
  3651. KERN_ERR
  3652. "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
  3653. ZR_DEVNAME(zr), fh->map_mode);
  3654. res = POLLNVAL;
  3655. }
  3656. mutex_unlock(&zr->resource_lock);
  3657. return res;
  3658. }
  3659. /*
  3660. * This maps the buffers to user space.
  3661. *
  3662. * Depending on the state of fh->map_mode
  3663. * the V4L or the MJPEG buffers are mapped
  3664. * per buffer or all together
  3665. *
  3666. * Note that we need to connect to some
  3667. * unmap signal event to unmap the de-allocate
  3668. * the buffer accordingly (zoran_vm_close())
  3669. */
  3670. static void
  3671. zoran_vm_open (struct vm_area_struct *vma)
  3672. {
  3673. struct zoran_mapping *map = vma->vm_private_data;
  3674. map->count++;
  3675. }
  3676. static void
  3677. zoran_vm_close (struct vm_area_struct *vma)
  3678. {
  3679. struct zoran_mapping *map = vma->vm_private_data;
  3680. struct file *file = map->file;
  3681. struct zoran_fh *fh = file->private_data;
  3682. struct zoran *zr = fh->zr;
  3683. int i;
  3684. map->count--;
  3685. if (map->count == 0) {
  3686. switch (fh->map_mode) {
  3687. case ZORAN_MAP_MODE_JPG_REC:
  3688. case ZORAN_MAP_MODE_JPG_PLAY:
  3689. dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
  3690. ZR_DEVNAME(zr));
  3691. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  3692. if (fh->jpg_buffers.buffer[i].map == map) {
  3693. fh->jpg_buffers.buffer[i].map =
  3694. NULL;
  3695. }
  3696. }
  3697. kfree(map);
  3698. for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
  3699. if (fh->jpg_buffers.buffer[i].map)
  3700. break;
  3701. if (i == fh->jpg_buffers.num_buffers) {
  3702. mutex_lock(&zr->resource_lock);
  3703. if (fh->jpg_buffers.active != ZORAN_FREE) {
  3704. jpg_qbuf(file, -1, zr->codec_mode);
  3705. zr->jpg_buffers.allocated = 0;
  3706. zr->jpg_buffers.active =
  3707. fh->jpg_buffers.active =
  3708. ZORAN_FREE;
  3709. }
  3710. //jpg_fbuffer_free(file);
  3711. fh->jpg_buffers.allocated = 0;
  3712. fh->jpg_buffers.ready_to_be_freed = 1;
  3713. mutex_unlock(&zr->resource_lock);
  3714. }
  3715. break;
  3716. case ZORAN_MAP_MODE_RAW:
  3717. dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
  3718. ZR_DEVNAME(zr));
  3719. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  3720. if (fh->v4l_buffers.buffer[i].map == map) {
  3721. /* unqueue/unmap */
  3722. fh->v4l_buffers.buffer[i].map =
  3723. NULL;
  3724. }
  3725. }
  3726. kfree(map);
  3727. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  3728. if (fh->v4l_buffers.buffer[i].map)
  3729. break;
  3730. if (i == fh->v4l_buffers.num_buffers) {
  3731. mutex_lock(&zr->resource_lock);
  3732. if (fh->v4l_buffers.active != ZORAN_FREE) {
  3733. long flags;
  3734. spin_lock_irqsave(&zr->spinlock, flags);
  3735. zr36057_set_memgrab(zr, 0);
  3736. zr->v4l_buffers.allocated = 0;
  3737. zr->v4l_buffers.active =
  3738. fh->v4l_buffers.active =
  3739. ZORAN_FREE;
  3740. spin_unlock_irqrestore(&zr->spinlock, flags);
  3741. }
  3742. //v4l_fbuffer_free(file);
  3743. fh->v4l_buffers.allocated = 0;
  3744. fh->v4l_buffers.ready_to_be_freed = 1;
  3745. mutex_unlock(&zr->resource_lock);
  3746. }
  3747. break;
  3748. default:
  3749. printk(KERN_ERR
  3750. "%s: munmap() - internal error - unknown map mode %d\n",
  3751. ZR_DEVNAME(zr), fh->map_mode);
  3752. break;
  3753. }
  3754. }
  3755. }
  3756. static struct vm_operations_struct zoran_vm_ops = {
  3757. .open = zoran_vm_open,
  3758. .close = zoran_vm_close,
  3759. };
  3760. static int
  3761. zoran_mmap (struct file *file,
  3762. struct vm_area_struct *vma)
  3763. {
  3764. struct zoran_fh *fh = file->private_data;
  3765. struct zoran *zr = fh->zr;
  3766. unsigned long size = (vma->vm_end - vma->vm_start);
  3767. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3768. int i, j;
  3769. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  3770. int first, last;
  3771. struct zoran_mapping *map;
  3772. int res = 0;
  3773. dprintk(3,
  3774. KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  3775. ZR_DEVNAME(zr),
  3776. fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
  3777. vma->vm_start, vma->vm_end, size);
  3778. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  3779. !(vma->vm_flags & VM_WRITE)) {
  3780. dprintk(1,
  3781. KERN_ERR
  3782. "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  3783. ZR_DEVNAME(zr));
  3784. return -EINVAL;
  3785. }
  3786. switch (fh->map_mode) {
  3787. case ZORAN_MAP_MODE_JPG_REC:
  3788. case ZORAN_MAP_MODE_JPG_PLAY:
  3789. /* lock */
  3790. mutex_lock(&zr->resource_lock);
  3791. /* Map the MJPEG buffers */
  3792. if (!fh->jpg_buffers.allocated) {
  3793. dprintk(1,
  3794. KERN_ERR
  3795. "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
  3796. ZR_DEVNAME(zr));
  3797. res = -ENOMEM;
  3798. goto jpg_mmap_unlock_and_return;
  3799. }
  3800. first = offset / fh->jpg_buffers.buffer_size;
  3801. last = first - 1 + size / fh->jpg_buffers.buffer_size;
  3802. if (offset % fh->jpg_buffers.buffer_size != 0 ||
  3803. size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
  3804. last < 0 || first >= fh->jpg_buffers.num_buffers ||
  3805. last >= fh->jpg_buffers.num_buffers) {
  3806. dprintk(1,
  3807. KERN_ERR
  3808. "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3809. ZR_DEVNAME(zr), offset, size,
  3810. fh->jpg_buffers.buffer_size,
  3811. fh->jpg_buffers.num_buffers);
  3812. res = -EINVAL;
  3813. goto jpg_mmap_unlock_and_return;
  3814. }
  3815. for (i = first; i <= last; i++) {
  3816. if (fh->jpg_buffers.buffer[i].map) {
  3817. dprintk(1,
  3818. KERN_ERR
  3819. "%s: mmap(MJPEG) - buffer %d already mapped\n",
  3820. ZR_DEVNAME(zr), i);
  3821. res = -EBUSY;
  3822. goto jpg_mmap_unlock_and_return;
  3823. }
  3824. }
  3825. /* map these buffers (v4l_buffers[i]) */
  3826. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3827. if (!map) {
  3828. res = -ENOMEM;
  3829. goto jpg_mmap_unlock_and_return;
  3830. }
  3831. map->file = file;
  3832. map->count = 1;
  3833. vma->vm_ops = &zoran_vm_ops;
  3834. vma->vm_flags |= VM_DONTEXPAND;
  3835. vma->vm_private_data = map;
  3836. for (i = first; i <= last; i++) {
  3837. for (j = 0;
  3838. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  3839. j++) {
  3840. fraglen =
  3841. (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3842. frag_tab[2 * j + 1]) & ~1) << 1;
  3843. todo = size;
  3844. if (todo > fraglen)
  3845. todo = fraglen;
  3846. pos =
  3847. le32_to_cpu((unsigned long) fh->jpg_buffers.
  3848. buffer[i].frag_tab[2 * j]);
  3849. /* should just be pos on i386 */
  3850. page = virt_to_phys(bus_to_virt(pos))
  3851. >> PAGE_SHIFT;
  3852. if (remap_pfn_range(vma, start, page,
  3853. todo, PAGE_SHARED)) {
  3854. dprintk(1,
  3855. KERN_ERR
  3856. "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
  3857. ZR_DEVNAME(zr));
  3858. res = -EAGAIN;
  3859. goto jpg_mmap_unlock_and_return;
  3860. }
  3861. size -= todo;
  3862. start += todo;
  3863. if (size == 0)
  3864. break;
  3865. if (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3866. frag_tab[2 * j + 1]) & 1)
  3867. break; /* was last fragment */
  3868. }
  3869. fh->jpg_buffers.buffer[i].map = map;
  3870. if (size == 0)
  3871. break;
  3872. }
  3873. jpg_mmap_unlock_and_return:
  3874. mutex_unlock(&zr->resource_lock);
  3875. break;
  3876. case ZORAN_MAP_MODE_RAW:
  3877. mutex_lock(&zr->resource_lock);
  3878. /* Map the V4L buffers */
  3879. if (!fh->v4l_buffers.allocated) {
  3880. dprintk(1,
  3881. KERN_ERR
  3882. "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
  3883. ZR_DEVNAME(zr));
  3884. res = -ENOMEM;
  3885. goto v4l_mmap_unlock_and_return;
  3886. }
  3887. first = offset / fh->v4l_buffers.buffer_size;
  3888. last = first - 1 + size / fh->v4l_buffers.buffer_size;
  3889. if (offset % fh->v4l_buffers.buffer_size != 0 ||
  3890. size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
  3891. last < 0 || first >= fh->v4l_buffers.num_buffers ||
  3892. last >= fh->v4l_buffers.buffer_size) {
  3893. dprintk(1,
  3894. KERN_ERR
  3895. "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3896. ZR_DEVNAME(zr), offset, size,
  3897. fh->v4l_buffers.buffer_size,
  3898. fh->v4l_buffers.num_buffers);
  3899. res = -EINVAL;
  3900. goto v4l_mmap_unlock_and_return;
  3901. }
  3902. for (i = first; i <= last; i++) {
  3903. if (fh->v4l_buffers.buffer[i].map) {
  3904. dprintk(1,
  3905. KERN_ERR
  3906. "%s: mmap(V4L) - buffer %d already mapped\n",
  3907. ZR_DEVNAME(zr), i);
  3908. res = -EBUSY;
  3909. goto v4l_mmap_unlock_and_return;
  3910. }
  3911. }
  3912. /* map these buffers (v4l_buffers[i]) */
  3913. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3914. if (!map) {
  3915. res = -ENOMEM;
  3916. goto v4l_mmap_unlock_and_return;
  3917. }
  3918. map->file = file;
  3919. map->count = 1;
  3920. vma->vm_ops = &zoran_vm_ops;
  3921. vma->vm_flags |= VM_DONTEXPAND;
  3922. vma->vm_private_data = map;
  3923. for (i = first; i <= last; i++) {
  3924. todo = size;
  3925. if (todo > fh->v4l_buffers.buffer_size)
  3926. todo = fh->v4l_buffers.buffer_size;
  3927. page = fh->v4l_buffers.buffer[i].fbuffer_phys;
  3928. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  3929. todo, PAGE_SHARED)) {
  3930. dprintk(1,
  3931. KERN_ERR
  3932. "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
  3933. ZR_DEVNAME(zr));
  3934. res = -EAGAIN;
  3935. goto v4l_mmap_unlock_and_return;
  3936. }
  3937. size -= todo;
  3938. start += todo;
  3939. fh->v4l_buffers.buffer[i].map = map;
  3940. if (size == 0)
  3941. break;
  3942. }
  3943. v4l_mmap_unlock_and_return:
  3944. mutex_unlock(&zr->resource_lock);
  3945. break;
  3946. default:
  3947. dprintk(1,
  3948. KERN_ERR
  3949. "%s: zoran_mmap() - internal error - unknown map mode %d\n",
  3950. ZR_DEVNAME(zr), fh->map_mode);
  3951. break;
  3952. }
  3953. return 0;
  3954. }
  3955. static const struct file_operations zoran_fops = {
  3956. .owner = THIS_MODULE,
  3957. .open = zoran_open,
  3958. .release = zoran_close,
  3959. .ioctl = zoran_ioctl,
  3960. .compat_ioctl = v4l_compat_ioctl32,
  3961. .llseek = no_llseek,
  3962. .read = zoran_read,
  3963. .write = zoran_write,
  3964. .mmap = zoran_mmap,
  3965. .poll = zoran_poll,
  3966. };
  3967. struct video_device zoran_template __devinitdata = {
  3968. .name = ZORAN_NAME,
  3969. .type = ZORAN_VID_TYPE,
  3970. #ifdef CONFIG_VIDEO_V4L2
  3971. .type2 = ZORAN_V4L2_VID_FLAGS,
  3972. #endif
  3973. .hardware = ZORAN_HARDWARE,
  3974. .fops = &zoran_fops,
  3975. .release = &zoran_vdev_release,
  3976. .minor = -1
  3977. };