zoran_driver.c 114 KB

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