zoran_driver.c 115 KB

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