zoran_driver.c 114 KB

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