zoran_driver.c 114 KB

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