zoran_driver.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602
  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 zoran_open(struct file *file)
  1055. {
  1056. struct zoran *zr = video_drvdata(file);
  1057. struct zoran_fh *fh;
  1058. int res, first_open = 0;
  1059. dprintk(2, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
  1060. ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user + 1);
  1061. lock_kernel();
  1062. /* see fs/device.c - the kernel already locks during open(),
  1063. * so locking ourselves only causes deadlocks */
  1064. /*mutex_lock(&zr->resource_lock);*/
  1065. if (zr->user >= 2048) {
  1066. dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
  1067. ZR_DEVNAME(zr), zr->user);
  1068. res = -EBUSY;
  1069. goto fail_unlock;
  1070. }
  1071. if (!zr->decoder) {
  1072. dprintk(1,
  1073. KERN_ERR "%s: no TV decoder loaded for device!\n",
  1074. ZR_DEVNAME(zr));
  1075. res = -EIO;
  1076. goto fail_unlock;
  1077. }
  1078. if (!try_module_get(zr->decoder->driver->driver.owner)) {
  1079. dprintk(1,
  1080. KERN_ERR
  1081. "%s: failed to grab ownership of video decoder\n",
  1082. ZR_DEVNAME(zr));
  1083. res = -EIO;
  1084. goto fail_unlock;
  1085. }
  1086. if (zr->encoder &&
  1087. !try_module_get(zr->encoder->driver->driver.owner)) {
  1088. dprintk(1,
  1089. KERN_ERR
  1090. "%s: failed to grab ownership of video encoder\n",
  1091. ZR_DEVNAME(zr));
  1092. res = -EIO;
  1093. goto fail_decoder;
  1094. }
  1095. /* now, create the open()-specific file_ops struct */
  1096. fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
  1097. if (!fh) {
  1098. dprintk(1,
  1099. KERN_ERR
  1100. "%s: zoran_open() - allocation of zoran_fh failed\n",
  1101. ZR_DEVNAME(zr));
  1102. res = -ENOMEM;
  1103. goto fail_encoder;
  1104. }
  1105. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  1106. * on norm-change! */
  1107. fh->overlay_mask =
  1108. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  1109. if (!fh->overlay_mask) {
  1110. dprintk(1,
  1111. KERN_ERR
  1112. "%s: zoran_open() - allocation of overlay_mask failed\n",
  1113. ZR_DEVNAME(zr));
  1114. res = -ENOMEM;
  1115. goto fail_fh;
  1116. }
  1117. if (zr->user++ == 0)
  1118. first_open = 1;
  1119. /*mutex_unlock(&zr->resource_lock);*/
  1120. /* default setup - TODO: look at flags */
  1121. if (first_open) { /* First device open */
  1122. zr36057_restart(zr);
  1123. zoran_open_init_params(zr);
  1124. zoran_init_hardware(zr);
  1125. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1126. }
  1127. /* set file_ops stuff */
  1128. file->private_data = fh;
  1129. fh->zr = zr;
  1130. zoran_open_init_session(file);
  1131. unlock_kernel();
  1132. return 0;
  1133. fail_fh:
  1134. kfree(fh);
  1135. fail_encoder:
  1136. if (zr->encoder)
  1137. module_put(zr->encoder->driver->driver.owner);
  1138. fail_decoder:
  1139. module_put(zr->decoder->driver->driver.owner);
  1140. fail_unlock:
  1141. unlock_kernel();
  1142. dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
  1143. ZR_DEVNAME(zr), res, zr->user);
  1144. return res;
  1145. }
  1146. static int
  1147. zoran_close(struct file *file)
  1148. {
  1149. struct zoran_fh *fh = file->private_data;
  1150. struct zoran *zr = fh->zr;
  1151. dprintk(2, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
  1152. ZR_DEVNAME(zr), current->comm, task_pid_nr(current), zr->user - 1);
  1153. /* kernel locks (fs/device.c), so don't do that ourselves
  1154. * (prevents deadlocks) */
  1155. /*mutex_lock(&zr->resource_lock);*/
  1156. zoran_close_end_session(file);
  1157. if (zr->user-- == 1) { /* Last process */
  1158. /* Clean up JPEG process */
  1159. wake_up_interruptible(&zr->jpg_capq);
  1160. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1161. zr->jpg_buffers.allocated = 0;
  1162. zr->jpg_buffers.active = ZORAN_FREE;
  1163. /* disable interrupts */
  1164. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1165. if (zr36067_debug > 1)
  1166. print_interrupts(zr);
  1167. /* Overlay off */
  1168. zr->v4l_overlay_active = 0;
  1169. zr36057_overlay(zr, 0);
  1170. zr->overlay_mask = NULL;
  1171. /* capture off */
  1172. wake_up_interruptible(&zr->v4l_capq);
  1173. zr36057_set_memgrab(zr, 0);
  1174. zr->v4l_buffers.allocated = 0;
  1175. zr->v4l_buffers.active = ZORAN_FREE;
  1176. zoran_set_pci_master(zr, 0);
  1177. if (!pass_through) { /* Switch to color bar */
  1178. int zero = 0, two = 2;
  1179. decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
  1180. encoder_command(zr, ENCODER_SET_INPUT, &two);
  1181. }
  1182. }
  1183. file->private_data = NULL;
  1184. kfree(fh->overlay_mask);
  1185. kfree(fh);
  1186. /* release locks on the i2c modules */
  1187. module_put(zr->decoder->driver->driver.owner);
  1188. if (zr->encoder)
  1189. module_put(zr->encoder->driver->driver.owner);
  1190. /*mutex_unlock(&zr->resource_lock);*/
  1191. dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
  1192. return 0;
  1193. }
  1194. static ssize_t
  1195. zoran_read (struct file *file,
  1196. char __user *data,
  1197. size_t count,
  1198. loff_t *ppos)
  1199. {
  1200. /* we simply don't support read() (yet)... */
  1201. return -EINVAL;
  1202. }
  1203. static ssize_t
  1204. zoran_write (struct file *file,
  1205. const char __user *data,
  1206. size_t count,
  1207. loff_t *ppos)
  1208. {
  1209. /* ...and the same goes for write() */
  1210. return -EINVAL;
  1211. }
  1212. static int
  1213. setup_fbuffer (struct file *file,
  1214. void *base,
  1215. const struct zoran_format *fmt,
  1216. int width,
  1217. int height,
  1218. int bytesperline)
  1219. {
  1220. struct zoran_fh *fh = file->private_data;
  1221. struct zoran *zr = fh->zr;
  1222. /* (Ronald) v4l/v4l2 guidelines */
  1223. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1224. return -EPERM;
  1225. /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
  1226. ALi Magik (that needs very low latency while the card needs a
  1227. higher value always) */
  1228. if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
  1229. return -ENXIO;
  1230. /* we need a bytesperline value, even if not given */
  1231. if (!bytesperline)
  1232. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  1233. #if 0
  1234. if (zr->overlay_active) {
  1235. /* dzjee... stupid users... don't even bother to turn off
  1236. * overlay before changing the memory location...
  1237. * normally, we would return errors here. However, one of
  1238. * the tools that does this is... xawtv! and since xawtv
  1239. * is used by +/- 99% of the users, we'd rather be user-
  1240. * friendly and silently do as if nothing went wrong */
  1241. dprintk(3,
  1242. KERN_ERR
  1243. "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
  1244. ZR_DEVNAME(zr));
  1245. zr36057_overlay(zr, 0);
  1246. }
  1247. #endif
  1248. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  1249. dprintk(1,
  1250. KERN_ERR
  1251. "%s: setup_fbuffer() - no valid overlay format given\n",
  1252. ZR_DEVNAME(zr));
  1253. return -EINVAL;
  1254. }
  1255. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  1256. dprintk(1,
  1257. KERN_ERR
  1258. "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
  1259. ZR_DEVNAME(zr), width, height, bytesperline);
  1260. return -EINVAL;
  1261. }
  1262. if (bytesperline & 3) {
  1263. dprintk(1,
  1264. KERN_ERR
  1265. "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
  1266. ZR_DEVNAME(zr), bytesperline);
  1267. return -EINVAL;
  1268. }
  1269. zr->buffer.base = (void *) ((unsigned long) base & ~3);
  1270. zr->buffer.height = height;
  1271. zr->buffer.width = width;
  1272. zr->buffer.depth = fmt->depth;
  1273. zr->overlay_settings.format = fmt;
  1274. zr->buffer.bytesperline = bytesperline;
  1275. /* The user should set new window parameters */
  1276. zr->overlay_settings.is_set = 0;
  1277. return 0;
  1278. }
  1279. static int
  1280. setup_window (struct file *file,
  1281. int x,
  1282. int y,
  1283. int width,
  1284. int height,
  1285. struct video_clip __user *clips,
  1286. int clipcount,
  1287. void __user *bitmap)
  1288. {
  1289. struct zoran_fh *fh = file->private_data;
  1290. struct zoran *zr = fh->zr;
  1291. struct video_clip *vcp = NULL;
  1292. int on, end;
  1293. if (!zr->buffer.base) {
  1294. dprintk(1,
  1295. KERN_ERR
  1296. "%s: setup_window() - frame buffer has to be set first\n",
  1297. ZR_DEVNAME(zr));
  1298. return -EINVAL;
  1299. }
  1300. if (!fh->overlay_settings.format) {
  1301. dprintk(1,
  1302. KERN_ERR
  1303. "%s: setup_window() - no overlay format set\n",
  1304. ZR_DEVNAME(zr));
  1305. return -EINVAL;
  1306. }
  1307. /*
  1308. * The video front end needs 4-byte alinged line sizes, we correct that
  1309. * silently here if necessary
  1310. */
  1311. if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
  1312. end = (x + width) & ~1; /* round down */
  1313. x = (x + 1) & ~1; /* round up */
  1314. width = end - x;
  1315. }
  1316. if (zr->buffer.depth == 24) {
  1317. end = (x + width) & ~3; /* round down */
  1318. x = (x + 3) & ~3; /* round up */
  1319. width = end - x;
  1320. }
  1321. if (width > BUZ_MAX_WIDTH)
  1322. width = BUZ_MAX_WIDTH;
  1323. if (height > BUZ_MAX_HEIGHT)
  1324. height = BUZ_MAX_HEIGHT;
  1325. /* Check for vaild parameters */
  1326. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1327. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1328. dprintk(1,
  1329. KERN_ERR
  1330. "%s: setup_window() - width = %d or height = %d invalid\n",
  1331. ZR_DEVNAME(zr), width, height);
  1332. return -EINVAL;
  1333. }
  1334. fh->overlay_settings.x = x;
  1335. fh->overlay_settings.y = y;
  1336. fh->overlay_settings.width = width;
  1337. fh->overlay_settings.height = height;
  1338. fh->overlay_settings.clipcount = clipcount;
  1339. /*
  1340. * If an overlay is running, we have to switch it off
  1341. * and switch it on again in order to get the new settings in effect.
  1342. *
  1343. * We also want to avoid that the overlay mask is written
  1344. * when an overlay is running.
  1345. */
  1346. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1347. zr->overlay_active != ZORAN_FREE &&
  1348. fh->overlay_active != ZORAN_FREE;
  1349. if (on)
  1350. zr36057_overlay(zr, 0);
  1351. /*
  1352. * Write the overlay mask if clips are wanted.
  1353. * We prefer a bitmap.
  1354. */
  1355. if (bitmap) {
  1356. /* fake value - it just means we want clips */
  1357. fh->overlay_settings.clipcount = 1;
  1358. if (copy_from_user(fh->overlay_mask, bitmap,
  1359. (width * height + 7) / 8)) {
  1360. return -EFAULT;
  1361. }
  1362. } else if (clipcount > 0) {
  1363. /* write our own bitmap from the clips */
  1364. vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
  1365. if (vcp == NULL) {
  1366. dprintk(1,
  1367. KERN_ERR
  1368. "%s: setup_window() - Alloc of clip mask failed\n",
  1369. ZR_DEVNAME(zr));
  1370. return -ENOMEM;
  1371. }
  1372. if (copy_from_user
  1373. (vcp, clips, sizeof(struct video_clip) * clipcount)) {
  1374. vfree(vcp);
  1375. return -EFAULT;
  1376. }
  1377. write_overlay_mask(file, vcp, clipcount);
  1378. vfree(vcp);
  1379. }
  1380. fh->overlay_settings.is_set = 1;
  1381. if (fh->overlay_active != ZORAN_FREE &&
  1382. zr->overlay_active != ZORAN_FREE)
  1383. zr->overlay_settings = fh->overlay_settings;
  1384. if (on)
  1385. zr36057_overlay(zr, 1);
  1386. /* Make sure the changes come into effect */
  1387. return wait_grab_pending(zr);
  1388. }
  1389. static int
  1390. setup_overlay (struct file *file,
  1391. int on)
  1392. {
  1393. struct zoran_fh *fh = file->private_data;
  1394. struct zoran *zr = fh->zr;
  1395. /* If there is nothing to do, return immediatly */
  1396. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1397. (!on && fh->overlay_active == ZORAN_FREE))
  1398. return 0;
  1399. /* check whether we're touching someone else's overlay */
  1400. if (on && zr->overlay_active != ZORAN_FREE &&
  1401. fh->overlay_active == ZORAN_FREE) {
  1402. dprintk(1,
  1403. KERN_ERR
  1404. "%s: setup_overlay() - overlay is already active for another session\n",
  1405. ZR_DEVNAME(zr));
  1406. return -EBUSY;
  1407. }
  1408. if (!on && zr->overlay_active != ZORAN_FREE &&
  1409. fh->overlay_active == ZORAN_FREE) {
  1410. dprintk(1,
  1411. KERN_ERR
  1412. "%s: setup_overlay() - you cannot cancel someone else's session\n",
  1413. ZR_DEVNAME(zr));
  1414. return -EPERM;
  1415. }
  1416. if (on == 0) {
  1417. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1418. zr->v4l_overlay_active = 0;
  1419. /* When a grab is running, the video simply
  1420. * won't be switched on any more */
  1421. if (!zr->v4l_memgrab_active)
  1422. zr36057_overlay(zr, 0);
  1423. zr->overlay_mask = NULL;
  1424. } else {
  1425. if (!zr->buffer.base || !fh->overlay_settings.is_set) {
  1426. dprintk(1,
  1427. KERN_ERR
  1428. "%s: setup_overlay() - buffer or window not set\n",
  1429. ZR_DEVNAME(zr));
  1430. return -EINVAL;
  1431. }
  1432. if (!fh->overlay_settings.format) {
  1433. dprintk(1,
  1434. KERN_ERR
  1435. "%s: setup_overlay() - no overlay format set\n",
  1436. ZR_DEVNAME(zr));
  1437. return -EINVAL;
  1438. }
  1439. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1440. zr->v4l_overlay_active = 1;
  1441. zr->overlay_mask = fh->overlay_mask;
  1442. zr->overlay_settings = fh->overlay_settings;
  1443. if (!zr->v4l_memgrab_active)
  1444. zr36057_overlay(zr, 1);
  1445. /* When a grab is running, the video will be
  1446. * switched on when grab is finished */
  1447. }
  1448. /* Make sure the changes come into effect */
  1449. return wait_grab_pending(zr);
  1450. }
  1451. /* get the status of a buffer in the clients buffer queue */
  1452. static int
  1453. zoran_v4l2_buffer_status (struct file *file,
  1454. struct v4l2_buffer *buf,
  1455. int num)
  1456. {
  1457. struct zoran_fh *fh = file->private_data;
  1458. struct zoran *zr = fh->zr;
  1459. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1460. switch (fh->map_mode) {
  1461. case ZORAN_MAP_MODE_RAW:
  1462. /* check range */
  1463. if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
  1464. !fh->v4l_buffers.allocated) {
  1465. dprintk(1,
  1466. KERN_ERR
  1467. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1468. ZR_DEVNAME(zr));
  1469. return -EINVAL;
  1470. }
  1471. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1472. buf->length = fh->v4l_buffers.buffer_size;
  1473. /* get buffer */
  1474. buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
  1475. if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1476. fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
  1477. buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
  1478. buf->flags |= V4L2_BUF_FLAG_DONE;
  1479. buf->timestamp =
  1480. fh->v4l_buffers.buffer[num].bs.timestamp;
  1481. } else {
  1482. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1483. }
  1484. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1485. buf->field = V4L2_FIELD_TOP;
  1486. else
  1487. buf->field = V4L2_FIELD_INTERLACED;
  1488. break;
  1489. case ZORAN_MAP_MODE_JPG_REC:
  1490. case ZORAN_MAP_MODE_JPG_PLAY:
  1491. /* check range */
  1492. if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
  1493. !fh->jpg_buffers.allocated) {
  1494. dprintk(1,
  1495. KERN_ERR
  1496. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1497. ZR_DEVNAME(zr));
  1498. return -EINVAL;
  1499. }
  1500. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1501. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1502. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1503. buf->length = fh->jpg_buffers.buffer_size;
  1504. /* these variables are only written after frame has been captured */
  1505. if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1506. fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
  1507. buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
  1508. buf->timestamp =
  1509. fh->jpg_buffers.buffer[num].bs.timestamp;
  1510. buf->bytesused =
  1511. fh->jpg_buffers.buffer[num].bs.length;
  1512. buf->flags |= V4L2_BUF_FLAG_DONE;
  1513. } else {
  1514. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1515. }
  1516. /* which fields are these? */
  1517. if (fh->jpg_settings.TmpDcm != 1)
  1518. buf->field =
  1519. fh->jpg_settings.
  1520. odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1521. else
  1522. buf->field =
  1523. fh->jpg_settings.
  1524. odd_even ? V4L2_FIELD_SEQ_TB :
  1525. V4L2_FIELD_SEQ_BT;
  1526. break;
  1527. default:
  1528. dprintk(5,
  1529. KERN_ERR
  1530. "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
  1531. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  1532. return -EINVAL;
  1533. }
  1534. buf->memory = V4L2_MEMORY_MMAP;
  1535. buf->index = num;
  1536. buf->m.offset = buf->length * num;
  1537. return 0;
  1538. }
  1539. static int
  1540. zoran_set_norm (struct zoran *zr,
  1541. int norm) /* VIDEO_MODE_* */
  1542. {
  1543. int norm_encoder, on;
  1544. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1545. zr->jpg_buffers.active != ZORAN_FREE) {
  1546. dprintk(1,
  1547. KERN_WARNING
  1548. "%s: set_norm() called while in playback/capture mode\n",
  1549. ZR_DEVNAME(zr));
  1550. return -EBUSY;
  1551. }
  1552. if (lock_norm && norm != zr->norm) {
  1553. if (lock_norm > 1) {
  1554. dprintk(1,
  1555. KERN_WARNING
  1556. "%s: set_norm() - TV standard is locked, can not switch norm\n",
  1557. ZR_DEVNAME(zr));
  1558. return -EPERM;
  1559. } else {
  1560. dprintk(1,
  1561. KERN_WARNING
  1562. "%s: set_norm() - TV standard is locked, norm was not changed\n",
  1563. ZR_DEVNAME(zr));
  1564. norm = zr->norm;
  1565. }
  1566. }
  1567. if (norm != VIDEO_MODE_AUTO &&
  1568. (norm < 0 || norm >= zr->card.norms ||
  1569. !zr->card.tvn[norm])) {
  1570. dprintk(1,
  1571. KERN_ERR "%s: set_norm() - unsupported norm %d\n",
  1572. ZR_DEVNAME(zr), norm);
  1573. return -EINVAL;
  1574. }
  1575. if (norm == VIDEO_MODE_AUTO) {
  1576. int status;
  1577. /* if we have autodetect, ... */
  1578. struct video_decoder_capability caps;
  1579. decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
  1580. if (!(caps.flags & VIDEO_DECODER_AUTO)) {
  1581. dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
  1582. ZR_DEVNAME(zr));
  1583. return -EINVAL;
  1584. }
  1585. decoder_command(zr, DECODER_SET_NORM, &norm);
  1586. /* let changes come into effect */
  1587. ssleep(2);
  1588. decoder_command(zr, DECODER_GET_STATUS, &status);
  1589. if (!(status & DECODER_STATUS_GOOD)) {
  1590. dprintk(1,
  1591. KERN_ERR
  1592. "%s: set_norm() - no norm detected\n",
  1593. ZR_DEVNAME(zr));
  1594. /* reset norm */
  1595. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  1596. return -EIO;
  1597. }
  1598. if (status & DECODER_STATUS_NTSC)
  1599. norm = VIDEO_MODE_NTSC;
  1600. else if (status & DECODER_STATUS_SECAM)
  1601. norm = VIDEO_MODE_SECAM;
  1602. else
  1603. norm = VIDEO_MODE_PAL;
  1604. }
  1605. zr->timing = zr->card.tvn[norm];
  1606. norm_encoder = norm;
  1607. /* We switch overlay off and on since a change in the
  1608. * norm needs different VFE settings */
  1609. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1610. if (on)
  1611. zr36057_overlay(zr, 0);
  1612. decoder_command(zr, DECODER_SET_NORM, &norm);
  1613. encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
  1614. if (on)
  1615. zr36057_overlay(zr, 1);
  1616. /* Make sure the changes come into effect */
  1617. zr->norm = norm;
  1618. return 0;
  1619. }
  1620. static int
  1621. zoran_set_input (struct zoran *zr,
  1622. int input)
  1623. {
  1624. int realinput;
  1625. if (input == zr->input) {
  1626. return 0;
  1627. }
  1628. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1629. zr->jpg_buffers.active != ZORAN_FREE) {
  1630. dprintk(1,
  1631. KERN_WARNING
  1632. "%s: set_input() called while in playback/capture mode\n",
  1633. ZR_DEVNAME(zr));
  1634. return -EBUSY;
  1635. }
  1636. if (input < 0 || input >= zr->card.inputs) {
  1637. dprintk(1,
  1638. KERN_ERR
  1639. "%s: set_input() - unnsupported input %d\n",
  1640. ZR_DEVNAME(zr), input);
  1641. return -EINVAL;
  1642. }
  1643. realinput = zr->card.input[input].muxsel;
  1644. zr->input = input;
  1645. decoder_command(zr, DECODER_SET_INPUT, &realinput);
  1646. return 0;
  1647. }
  1648. /*
  1649. * ioctl routine
  1650. */
  1651. static long zoran_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  1652. {
  1653. struct zoran_fh *fh = file->private_data;
  1654. struct zoran *zr = fh->zr;
  1655. /* CAREFUL: used in multiple places here */
  1656. struct zoran_jpg_settings settings;
  1657. /* we might have older buffers lying around... We don't want
  1658. * to wait, but we do want to try cleaning them up ASAP. So
  1659. * we try to obtain the lock and free them. If that fails, we
  1660. * don't do anything and wait for the next turn. In the end,
  1661. * zoran_close() or a new allocation will still free them...
  1662. * This is just a 'the sooner the better' extra 'feature'
  1663. *
  1664. * We don't free the buffers right on munmap() because that
  1665. * causes oopses (kfree() inside munmap() oopses for no
  1666. * apparent reason - it's also not reproduceable in any way,
  1667. * but moving the free code outside the munmap() handler fixes
  1668. * all this... If someone knows why, please explain me (Ronald)
  1669. */
  1670. if (mutex_trylock(&zr->resource_lock)) {
  1671. /* we obtained it! Let's try to free some things */
  1672. if (fh->jpg_buffers.ready_to_be_freed)
  1673. jpg_fbuffer_free(file);
  1674. if (fh->v4l_buffers.ready_to_be_freed)
  1675. v4l_fbuffer_free(file);
  1676. mutex_unlock(&zr->resource_lock);
  1677. }
  1678. switch (cmd) {
  1679. case VIDIOCGCAP:
  1680. {
  1681. struct video_capability *vcap = arg;
  1682. dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
  1683. memset(vcap, 0, sizeof(struct video_capability));
  1684. strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name)-1);
  1685. vcap->type = ZORAN_VID_TYPE;
  1686. vcap->channels = zr->card.inputs;
  1687. vcap->audios = 0;
  1688. mutex_lock(&zr->resource_lock);
  1689. vcap->maxwidth = BUZ_MAX_WIDTH;
  1690. vcap->maxheight = BUZ_MAX_HEIGHT;
  1691. vcap->minwidth = BUZ_MIN_WIDTH;
  1692. vcap->minheight = BUZ_MIN_HEIGHT;
  1693. mutex_unlock(&zr->resource_lock);
  1694. return 0;
  1695. }
  1696. break;
  1697. case VIDIOCGCHAN:
  1698. {
  1699. struct video_channel *vchan = arg;
  1700. int channel = vchan->channel;
  1701. dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
  1702. ZR_DEVNAME(zr), vchan->channel);
  1703. memset(vchan, 0, sizeof(struct video_channel));
  1704. if (channel > zr->card.inputs || channel < 0) {
  1705. dprintk(1,
  1706. KERN_ERR
  1707. "%s: VIDIOCGCHAN on not existing channel %d\n",
  1708. ZR_DEVNAME(zr), channel);
  1709. return -EINVAL;
  1710. }
  1711. strcpy(vchan->name, zr->card.input[channel].name);
  1712. vchan->tuners = 0;
  1713. vchan->flags = 0;
  1714. vchan->type = VIDEO_TYPE_CAMERA;
  1715. mutex_lock(&zr->resource_lock);
  1716. vchan->norm = zr->norm;
  1717. mutex_unlock(&zr->resource_lock);
  1718. vchan->channel = channel;
  1719. return 0;
  1720. }
  1721. break;
  1722. /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
  1723. *
  1724. * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
  1725. * * ^^^^^^^
  1726. * * The famos BTTV driver has it implemented with a struct video_channel argument
  1727. * * and we follow it for compatibility reasons
  1728. * *
  1729. * * BTW: this is the only way the user can set the norm!
  1730. */
  1731. case VIDIOCSCHAN:
  1732. {
  1733. struct video_channel *vchan = arg;
  1734. int res;
  1735. dprintk(3,
  1736. KERN_DEBUG
  1737. "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
  1738. ZR_DEVNAME(zr), vchan->channel, vchan->norm);
  1739. mutex_lock(&zr->resource_lock);
  1740. if ((res = zoran_set_input(zr, vchan->channel)))
  1741. goto schan_unlock_and_return;
  1742. if ((res = zoran_set_norm(zr, vchan->norm)))
  1743. goto schan_unlock_and_return;
  1744. /* Make sure the changes come into effect */
  1745. res = wait_grab_pending(zr);
  1746. schan_unlock_and_return:
  1747. mutex_unlock(&zr->resource_lock);
  1748. return res;
  1749. }
  1750. break;
  1751. case VIDIOCGPICT:
  1752. {
  1753. struct video_picture *vpict = arg;
  1754. dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
  1755. memset(vpict, 0, sizeof(struct video_picture));
  1756. mutex_lock(&zr->resource_lock);
  1757. vpict->hue = zr->hue;
  1758. vpict->brightness = zr->brightness;
  1759. vpict->contrast = zr->contrast;
  1760. vpict->colour = zr->saturation;
  1761. if (fh->overlay_settings.format) {
  1762. vpict->depth = fh->overlay_settings.format->depth;
  1763. vpict->palette = fh->overlay_settings.format->palette;
  1764. } else {
  1765. vpict->depth = 0;
  1766. }
  1767. mutex_unlock(&zr->resource_lock);
  1768. return 0;
  1769. }
  1770. break;
  1771. case VIDIOCSPICT:
  1772. {
  1773. struct video_picture *vpict = arg;
  1774. int i;
  1775. dprintk(3,
  1776. KERN_DEBUG
  1777. "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
  1778. ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
  1779. vpict->colour, vpict->contrast, vpict->depth,
  1780. vpict->palette);
  1781. for (i = 0; i < NUM_FORMATS; i++) {
  1782. const struct zoran_format *fmt = &zoran_formats[i];
  1783. if (fmt->palette != -1 &&
  1784. fmt->flags & ZORAN_FORMAT_OVERLAY &&
  1785. fmt->palette == vpict->palette &&
  1786. fmt->depth == vpict->depth)
  1787. break;
  1788. }
  1789. if (i == NUM_FORMATS) {
  1790. dprintk(1,
  1791. KERN_ERR
  1792. "%s: VIDIOCSPICT - Invalid palette %d\n",
  1793. ZR_DEVNAME(zr), vpict->palette);
  1794. return -EINVAL;
  1795. }
  1796. mutex_lock(&zr->resource_lock);
  1797. decoder_command(zr, DECODER_SET_PICTURE, vpict);
  1798. zr->hue = vpict->hue;
  1799. zr->contrast = vpict->contrast;
  1800. zr->saturation = vpict->colour;
  1801. zr->brightness = vpict->brightness;
  1802. fh->overlay_settings.format = &zoran_formats[i];
  1803. mutex_unlock(&zr->resource_lock);
  1804. return 0;
  1805. }
  1806. break;
  1807. case VIDIOCCAPTURE:
  1808. {
  1809. int *on = arg, res;
  1810. dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
  1811. ZR_DEVNAME(zr), *on);
  1812. mutex_lock(&zr->resource_lock);
  1813. res = setup_overlay(file, *on);
  1814. mutex_unlock(&zr->resource_lock);
  1815. return res;
  1816. }
  1817. break;
  1818. case VIDIOCGWIN:
  1819. {
  1820. struct video_window *vwin = arg;
  1821. dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
  1822. memset(vwin, 0, sizeof(struct video_window));
  1823. mutex_lock(&zr->resource_lock);
  1824. vwin->x = fh->overlay_settings.x;
  1825. vwin->y = fh->overlay_settings.y;
  1826. vwin->width = fh->overlay_settings.width;
  1827. vwin->height = fh->overlay_settings.height;
  1828. mutex_unlock(&zr->resource_lock);
  1829. vwin->clipcount = 0;
  1830. return 0;
  1831. }
  1832. break;
  1833. case VIDIOCSWIN:
  1834. {
  1835. struct video_window *vwin = arg;
  1836. int res;
  1837. dprintk(3,
  1838. KERN_DEBUG
  1839. "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
  1840. ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
  1841. vwin->height, vwin->clipcount);
  1842. mutex_lock(&zr->resource_lock);
  1843. res =
  1844. setup_window(file, vwin->x, vwin->y, vwin->width,
  1845. vwin->height, vwin->clips,
  1846. vwin->clipcount, NULL);
  1847. mutex_unlock(&zr->resource_lock);
  1848. return res;
  1849. }
  1850. break;
  1851. case VIDIOCGFBUF:
  1852. {
  1853. struct video_buffer *vbuf = arg;
  1854. dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
  1855. mutex_lock(&zr->resource_lock);
  1856. *vbuf = zr->buffer;
  1857. mutex_unlock(&zr->resource_lock);
  1858. return 0;
  1859. }
  1860. break;
  1861. case VIDIOCSFBUF:
  1862. {
  1863. struct video_buffer *vbuf = arg;
  1864. int i, res = 0;
  1865. dprintk(3,
  1866. KERN_DEBUG
  1867. "%s: VIDIOCSFBUF - base=%p, w=%d, h=%d, depth=%d, bpl=%d\n",
  1868. ZR_DEVNAME(zr), vbuf->base, vbuf->width,
  1869. vbuf->height, vbuf->depth, vbuf->bytesperline);
  1870. for (i = 0; i < NUM_FORMATS; i++)
  1871. if (zoran_formats[i].depth == vbuf->depth)
  1872. break;
  1873. if (i == NUM_FORMATS) {
  1874. dprintk(1,
  1875. KERN_ERR
  1876. "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
  1877. ZR_DEVNAME(zr), vbuf->depth);
  1878. return -EINVAL;
  1879. }
  1880. mutex_lock(&zr->resource_lock);
  1881. res =
  1882. setup_fbuffer(file, vbuf->base, &zoran_formats[i],
  1883. vbuf->width, vbuf->height,
  1884. vbuf->bytesperline);
  1885. mutex_unlock(&zr->resource_lock);
  1886. return res;
  1887. }
  1888. break;
  1889. case VIDIOCSYNC:
  1890. {
  1891. int *frame = arg, res;
  1892. dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
  1893. ZR_DEVNAME(zr), *frame);
  1894. mutex_lock(&zr->resource_lock);
  1895. res = v4l_sync(file, *frame);
  1896. mutex_unlock(&zr->resource_lock);
  1897. if (!res)
  1898. zr->v4l_sync_tail++;
  1899. return res;
  1900. }
  1901. break;
  1902. case VIDIOCMCAPTURE:
  1903. {
  1904. struct video_mmap *vmap = arg;
  1905. int res;
  1906. dprintk(3,
  1907. KERN_DEBUG
  1908. "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
  1909. ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
  1910. vmap->format);
  1911. mutex_lock(&zr->resource_lock);
  1912. res = v4l_grab(file, vmap);
  1913. mutex_unlock(&zr->resource_lock);
  1914. return res;
  1915. }
  1916. break;
  1917. case VIDIOCGMBUF:
  1918. {
  1919. struct video_mbuf *vmbuf = arg;
  1920. int i, res = 0;
  1921. dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
  1922. vmbuf->size =
  1923. fh->v4l_buffers.num_buffers *
  1924. fh->v4l_buffers.buffer_size;
  1925. vmbuf->frames = fh->v4l_buffers.num_buffers;
  1926. for (i = 0; i < vmbuf->frames; i++) {
  1927. vmbuf->offsets[i] =
  1928. i * fh->v4l_buffers.buffer_size;
  1929. }
  1930. mutex_lock(&zr->resource_lock);
  1931. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  1932. dprintk(1,
  1933. KERN_ERR
  1934. "%s: VIDIOCGMBUF - buffers already allocated\n",
  1935. ZR_DEVNAME(zr));
  1936. res = -EINVAL;
  1937. goto v4l1reqbuf_unlock_and_return;
  1938. }
  1939. if (v4l_fbuffer_alloc(file)) {
  1940. res = -ENOMEM;
  1941. goto v4l1reqbuf_unlock_and_return;
  1942. }
  1943. /* The next mmap will map the V4L buffers */
  1944. fh->map_mode = ZORAN_MAP_MODE_RAW;
  1945. v4l1reqbuf_unlock_and_return:
  1946. mutex_unlock(&zr->resource_lock);
  1947. return res;
  1948. }
  1949. break;
  1950. case VIDIOCGUNIT:
  1951. {
  1952. struct video_unit *vunit = arg;
  1953. dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
  1954. vunit->video = zr->video_dev->minor;
  1955. vunit->vbi = VIDEO_NO_UNIT;
  1956. vunit->radio = VIDEO_NO_UNIT;
  1957. vunit->audio = VIDEO_NO_UNIT;
  1958. vunit->teletext = VIDEO_NO_UNIT;
  1959. return 0;
  1960. }
  1961. break;
  1962. /*
  1963. * RJ: In principal we could support subcaptures for V4L grabbing.
  1964. * Not even the famous BTTV driver has them, however.
  1965. * If there should be a strong demand, one could consider
  1966. * to implement them.
  1967. */
  1968. case VIDIOCGCAPTURE:
  1969. {
  1970. dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
  1971. ZR_DEVNAME(zr));
  1972. return -EINVAL;
  1973. }
  1974. break;
  1975. case VIDIOCSCAPTURE:
  1976. {
  1977. dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
  1978. ZR_DEVNAME(zr));
  1979. return -EINVAL;
  1980. }
  1981. break;
  1982. case BUZIOC_G_PARAMS:
  1983. {
  1984. struct zoran_params *bparams = arg;
  1985. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  1986. memset(bparams, 0, sizeof(struct zoran_params));
  1987. bparams->major_version = MAJOR_VERSION;
  1988. bparams->minor_version = MINOR_VERSION;
  1989. mutex_lock(&zr->resource_lock);
  1990. bparams->norm = zr->norm;
  1991. bparams->input = zr->input;
  1992. bparams->decimation = fh->jpg_settings.decimation;
  1993. bparams->HorDcm = fh->jpg_settings.HorDcm;
  1994. bparams->VerDcm = fh->jpg_settings.VerDcm;
  1995. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  1996. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  1997. bparams->img_x = fh->jpg_settings.img_x;
  1998. bparams->img_y = fh->jpg_settings.img_y;
  1999. bparams->img_width = fh->jpg_settings.img_width;
  2000. bparams->img_height = fh->jpg_settings.img_height;
  2001. bparams->odd_even = fh->jpg_settings.odd_even;
  2002. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  2003. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  2004. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2005. memcpy(bparams->APP_data,
  2006. fh->jpg_settings.jpg_comp.APP_data,
  2007. sizeof(bparams->APP_data));
  2008. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  2009. memcpy(bparams->COM_data,
  2010. fh->jpg_settings.jpg_comp.COM_data,
  2011. sizeof(bparams->COM_data));
  2012. bparams->jpeg_markers =
  2013. fh->jpg_settings.jpg_comp.jpeg_markers;
  2014. mutex_unlock(&zr->resource_lock);
  2015. bparams->VFIFO_FB = 0;
  2016. return 0;
  2017. }
  2018. break;
  2019. case BUZIOC_S_PARAMS:
  2020. {
  2021. struct zoran_params *bparams = arg;
  2022. int res = 0;
  2023. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  2024. settings.decimation = bparams->decimation;
  2025. settings.HorDcm = bparams->HorDcm;
  2026. settings.VerDcm = bparams->VerDcm;
  2027. settings.TmpDcm = bparams->TmpDcm;
  2028. settings.field_per_buff = bparams->field_per_buff;
  2029. settings.img_x = bparams->img_x;
  2030. settings.img_y = bparams->img_y;
  2031. settings.img_width = bparams->img_width;
  2032. settings.img_height = bparams->img_height;
  2033. settings.odd_even = bparams->odd_even;
  2034. settings.jpg_comp.quality = bparams->quality;
  2035. settings.jpg_comp.APPn = bparams->APPn;
  2036. settings.jpg_comp.APP_len = bparams->APP_len;
  2037. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  2038. sizeof(bparams->APP_data));
  2039. settings.jpg_comp.COM_len = bparams->COM_len;
  2040. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  2041. sizeof(bparams->COM_data));
  2042. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  2043. mutex_lock(&zr->resource_lock);
  2044. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2045. dprintk(1,
  2046. KERN_ERR
  2047. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  2048. ZR_DEVNAME(zr));
  2049. res = -EINVAL;
  2050. goto sparams_unlock_and_return;
  2051. }
  2052. /* Check the params first before overwriting our
  2053. * nternal values */
  2054. if (zoran_check_jpg_settings(zr, &settings)) {
  2055. res = -EINVAL;
  2056. goto sparams_unlock_and_return;
  2057. }
  2058. fh->jpg_settings = settings;
  2059. sparams_unlock_and_return:
  2060. mutex_unlock(&zr->resource_lock);
  2061. return res;
  2062. }
  2063. break;
  2064. case BUZIOC_REQBUFS:
  2065. {
  2066. struct zoran_requestbuffers *breq = arg;
  2067. int res = 0;
  2068. dprintk(3,
  2069. KERN_DEBUG
  2070. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  2071. ZR_DEVNAME(zr), breq->count, breq->size);
  2072. /* Enforce reasonable lower and upper limits */
  2073. if (breq->count < 4)
  2074. breq->count = 4; /* Could be choosen smaller */
  2075. if (breq->count > jpg_nbufs)
  2076. breq->count = jpg_nbufs;
  2077. breq->size = PAGE_ALIGN(breq->size);
  2078. if (breq->size < 8192)
  2079. breq->size = 8192; /* Arbitrary */
  2080. /* breq->size is limited by 1 page for the stat_com
  2081. * tables to a Maximum of 2 MB */
  2082. if (breq->size > jpg_bufsize)
  2083. breq->size = jpg_bufsize;
  2084. if (fh->jpg_buffers.need_contiguous &&
  2085. breq->size > MAX_KMALLOC_MEM)
  2086. breq->size = MAX_KMALLOC_MEM;
  2087. mutex_lock(&zr->resource_lock);
  2088. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2089. dprintk(1,
  2090. KERN_ERR
  2091. "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
  2092. ZR_DEVNAME(zr));
  2093. res = -EBUSY;
  2094. goto jpgreqbuf_unlock_and_return;
  2095. }
  2096. fh->jpg_buffers.num_buffers = breq->count;
  2097. fh->jpg_buffers.buffer_size = breq->size;
  2098. if (jpg_fbuffer_alloc(file)) {
  2099. res = -ENOMEM;
  2100. goto jpgreqbuf_unlock_and_return;
  2101. }
  2102. /* The next mmap will map the MJPEG buffers - could
  2103. * also be *_PLAY, but it doesn't matter here */
  2104. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2105. jpgreqbuf_unlock_and_return:
  2106. mutex_unlock(&zr->resource_lock);
  2107. return res;
  2108. }
  2109. break;
  2110. case BUZIOC_QBUF_CAPT:
  2111. {
  2112. int *frame = arg, res;
  2113. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  2114. ZR_DEVNAME(zr), *frame);
  2115. mutex_lock(&zr->resource_lock);
  2116. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
  2117. mutex_unlock(&zr->resource_lock);
  2118. return res;
  2119. }
  2120. break;
  2121. case BUZIOC_QBUF_PLAY:
  2122. {
  2123. int *frame = arg, res;
  2124. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  2125. ZR_DEVNAME(zr), *frame);
  2126. mutex_lock(&zr->resource_lock);
  2127. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  2128. mutex_unlock(&zr->resource_lock);
  2129. return res;
  2130. }
  2131. break;
  2132. case BUZIOC_SYNC:
  2133. {
  2134. struct zoran_sync *bsync = arg;
  2135. int res;
  2136. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  2137. mutex_lock(&zr->resource_lock);
  2138. res = jpg_sync(file, bsync);
  2139. mutex_unlock(&zr->resource_lock);
  2140. return res;
  2141. }
  2142. break;
  2143. case BUZIOC_G_STATUS:
  2144. {
  2145. struct zoran_status *bstat = arg;
  2146. int norm, input, status, res = 0;
  2147. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  2148. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2149. dprintk(1,
  2150. KERN_ERR
  2151. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  2152. ZR_DEVNAME(zr));
  2153. return -EINVAL;
  2154. }
  2155. input = zr->card.input[bstat->input].muxsel;
  2156. norm = VIDEO_MODE_AUTO;
  2157. mutex_lock(&zr->resource_lock);
  2158. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2159. dprintk(1,
  2160. KERN_ERR
  2161. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  2162. ZR_DEVNAME(zr));
  2163. res = -EINVAL;
  2164. goto gstat_unlock_and_return;
  2165. }
  2166. decoder_command(zr, DECODER_SET_INPUT, &input);
  2167. decoder_command(zr, DECODER_SET_NORM, &norm);
  2168. /* sleep 1 second */
  2169. ssleep(1);
  2170. /* Get status of video decoder */
  2171. decoder_command(zr, DECODER_GET_STATUS, &status);
  2172. /* restore previous input and norm */
  2173. input = zr->card.input[zr->input].muxsel;
  2174. decoder_command(zr, DECODER_SET_INPUT, &input);
  2175. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  2176. gstat_unlock_and_return:
  2177. mutex_unlock(&zr->resource_lock);
  2178. if (!res) {
  2179. bstat->signal =
  2180. (status & DECODER_STATUS_GOOD) ? 1 : 0;
  2181. if (status & DECODER_STATUS_NTSC)
  2182. bstat->norm = VIDEO_MODE_NTSC;
  2183. else if (status & DECODER_STATUS_SECAM)
  2184. bstat->norm = VIDEO_MODE_SECAM;
  2185. else
  2186. bstat->norm = VIDEO_MODE_PAL;
  2187. bstat->color =
  2188. (status & DECODER_STATUS_COLOR) ? 1 : 0;
  2189. }
  2190. return res;
  2191. }
  2192. break;
  2193. /* The new video4linux2 capture interface - much nicer than video4linux1, since
  2194. * it allows for integrating the JPEG capturing calls inside standard v4l2
  2195. */
  2196. case VIDIOC_QUERYCAP:
  2197. {
  2198. struct v4l2_capability *cap = arg;
  2199. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
  2200. memset(cap, 0, sizeof(*cap));
  2201. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
  2202. strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
  2203. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  2204. pci_name(zr->pci_dev));
  2205. cap->version =
  2206. KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  2207. RELEASE_VERSION);
  2208. cap->capabilities = ZORAN_V4L2_VID_FLAGS;
  2209. return 0;
  2210. }
  2211. break;
  2212. case VIDIOC_ENUM_FMT:
  2213. {
  2214. struct v4l2_fmtdesc *fmt = arg;
  2215. int index = fmt->index, num = -1, i, flag = 0, type =
  2216. fmt->type;
  2217. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
  2218. ZR_DEVNAME(zr), fmt->index);
  2219. switch (fmt->type) {
  2220. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2221. flag = ZORAN_FORMAT_CAPTURE;
  2222. break;
  2223. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2224. flag = ZORAN_FORMAT_PLAYBACK;
  2225. break;
  2226. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2227. flag = ZORAN_FORMAT_OVERLAY;
  2228. break;
  2229. default:
  2230. dprintk(1,
  2231. KERN_ERR
  2232. "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
  2233. ZR_DEVNAME(zr), fmt->type);
  2234. return -EINVAL;
  2235. }
  2236. for (i = 0; i < NUM_FORMATS; i++) {
  2237. if (zoran_formats[i].flags & flag)
  2238. num++;
  2239. if (num == fmt->index)
  2240. break;
  2241. }
  2242. if (fmt->index < 0 /* late, but not too late */ ||
  2243. i == NUM_FORMATS)
  2244. return -EINVAL;
  2245. memset(fmt, 0, sizeof(*fmt));
  2246. fmt->index = index;
  2247. fmt->type = type;
  2248. strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
  2249. fmt->pixelformat = zoran_formats[i].fourcc;
  2250. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  2251. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  2252. return 0;
  2253. }
  2254. break;
  2255. case VIDIOC_G_FMT:
  2256. {
  2257. struct v4l2_format *fmt = arg;
  2258. int type = fmt->type;
  2259. dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
  2260. memset(fmt, 0, sizeof(*fmt));
  2261. fmt->type = type;
  2262. switch (fmt->type) {
  2263. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2264. mutex_lock(&zr->resource_lock);
  2265. fmt->fmt.win.w.left = fh->overlay_settings.x;
  2266. fmt->fmt.win.w.top = fh->overlay_settings.y;
  2267. fmt->fmt.win.w.width = fh->overlay_settings.width;
  2268. fmt->fmt.win.w.height =
  2269. fh->overlay_settings.height;
  2270. if (fh->overlay_settings.width * 2 >
  2271. BUZ_MAX_HEIGHT)
  2272. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  2273. else
  2274. fmt->fmt.win.field = V4L2_FIELD_TOP;
  2275. mutex_unlock(&zr->resource_lock);
  2276. break;
  2277. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2278. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2279. mutex_lock(&zr->resource_lock);
  2280. if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2281. fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2282. fmt->fmt.pix.width =
  2283. fh->v4l_settings.width;
  2284. fmt->fmt.pix.height =
  2285. fh->v4l_settings.height;
  2286. fmt->fmt.pix.sizeimage =
  2287. fh->v4l_settings.bytesperline *
  2288. fh->v4l_settings.height;
  2289. fmt->fmt.pix.pixelformat =
  2290. fh->v4l_settings.format->fourcc;
  2291. fmt->fmt.pix.colorspace =
  2292. fh->v4l_settings.format->colorspace;
  2293. fmt->fmt.pix.bytesperline =
  2294. fh->v4l_settings.bytesperline;
  2295. if (BUZ_MAX_HEIGHT <
  2296. (fh->v4l_settings.height * 2))
  2297. fmt->fmt.pix.field =
  2298. V4L2_FIELD_INTERLACED;
  2299. else
  2300. fmt->fmt.pix.field =
  2301. V4L2_FIELD_TOP;
  2302. } else {
  2303. fmt->fmt.pix.width =
  2304. fh->jpg_settings.img_width /
  2305. fh->jpg_settings.HorDcm;
  2306. fmt->fmt.pix.height =
  2307. fh->jpg_settings.img_height /
  2308. (fh->jpg_settings.VerDcm *
  2309. fh->jpg_settings.TmpDcm);
  2310. fmt->fmt.pix.sizeimage =
  2311. zoran_v4l2_calc_bufsize(&fh->
  2312. jpg_settings);
  2313. fmt->fmt.pix.pixelformat =
  2314. V4L2_PIX_FMT_MJPEG;
  2315. if (fh->jpg_settings.TmpDcm == 1)
  2316. fmt->fmt.pix.field =
  2317. (fh->jpg_settings.
  2318. odd_even ? V4L2_FIELD_SEQ_BT :
  2319. V4L2_FIELD_SEQ_BT);
  2320. else
  2321. fmt->fmt.pix.field =
  2322. (fh->jpg_settings.
  2323. odd_even ? V4L2_FIELD_TOP :
  2324. V4L2_FIELD_BOTTOM);
  2325. fmt->fmt.pix.bytesperline = 0;
  2326. fmt->fmt.pix.colorspace =
  2327. V4L2_COLORSPACE_SMPTE170M;
  2328. }
  2329. mutex_unlock(&zr->resource_lock);
  2330. break;
  2331. default:
  2332. dprintk(1,
  2333. KERN_ERR
  2334. "%s: VIDIOC_G_FMT - unsupported type %d\n",
  2335. ZR_DEVNAME(zr), fmt->type);
  2336. return -EINVAL;
  2337. }
  2338. return 0;
  2339. }
  2340. break;
  2341. case VIDIOC_S_FMT:
  2342. {
  2343. struct v4l2_format *fmt = arg;
  2344. int i, res = 0;
  2345. __le32 printformat;
  2346. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
  2347. ZR_DEVNAME(zr), fmt->type);
  2348. switch (fmt->type) {
  2349. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2350. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  2351. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  2352. fmt->fmt.win.w.width,
  2353. fmt->fmt.win.w.height,
  2354. fmt->fmt.win.clipcount,
  2355. fmt->fmt.win.bitmap);
  2356. mutex_lock(&zr->resource_lock);
  2357. res =
  2358. setup_window(file, fmt->fmt.win.w.left,
  2359. fmt->fmt.win.w.top,
  2360. fmt->fmt.win.w.width,
  2361. fmt->fmt.win.w.height,
  2362. (struct video_clip __user *)
  2363. fmt->fmt.win.clips,
  2364. fmt->fmt.win.clipcount,
  2365. fmt->fmt.win.bitmap);
  2366. mutex_unlock(&zr->resource_lock);
  2367. return res;
  2368. break;
  2369. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2370. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2371. printformat =
  2372. __cpu_to_le32(fmt->fmt.pix.pixelformat);
  2373. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  2374. fmt->fmt.pix.width, fmt->fmt.pix.height,
  2375. fmt->fmt.pix.pixelformat,
  2376. (char *) &printformat);
  2377. /* we can be requested to do JPEG/raw playback/capture */
  2378. if (!
  2379. (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2380. (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2381. fmt->fmt.pix.pixelformat ==
  2382. V4L2_PIX_FMT_MJPEG))) {
  2383. dprintk(1,
  2384. KERN_ERR
  2385. "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
  2386. ZR_DEVNAME(zr), fmt->type,
  2387. fmt->fmt.pix.pixelformat,
  2388. (char *) &printformat);
  2389. return -EINVAL;
  2390. }
  2391. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  2392. mutex_lock(&zr->resource_lock);
  2393. settings = fh->jpg_settings;
  2394. if (fh->v4l_buffers.allocated ||
  2395. fh->jpg_buffers.allocated) {
  2396. dprintk(1,
  2397. KERN_ERR
  2398. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2399. ZR_DEVNAME(zr));
  2400. res = -EBUSY;
  2401. goto sfmtjpg_unlock_and_return;
  2402. }
  2403. /* we actually need to set 'real' parameters now */
  2404. if ((fmt->fmt.pix.height * 2) >
  2405. BUZ_MAX_HEIGHT)
  2406. settings.TmpDcm = 1;
  2407. else
  2408. settings.TmpDcm = 2;
  2409. settings.decimation = 0;
  2410. if (fmt->fmt.pix.height <=
  2411. fh->jpg_settings.img_height / 2)
  2412. settings.VerDcm = 2;
  2413. else
  2414. settings.VerDcm = 1;
  2415. if (fmt->fmt.pix.width <=
  2416. fh->jpg_settings.img_width / 4)
  2417. settings.HorDcm = 4;
  2418. else if (fmt->fmt.pix.width <=
  2419. fh->jpg_settings.img_width / 2)
  2420. settings.HorDcm = 2;
  2421. else
  2422. settings.HorDcm = 1;
  2423. if (settings.TmpDcm == 1)
  2424. settings.field_per_buff = 2;
  2425. else
  2426. settings.field_per_buff = 1;
  2427. /* check */
  2428. if ((res =
  2429. zoran_check_jpg_settings(zr,
  2430. &settings)))
  2431. goto sfmtjpg_unlock_and_return;
  2432. /* it's ok, so set them */
  2433. fh->jpg_settings = settings;
  2434. /* tell the user what we actually did */
  2435. fmt->fmt.pix.width =
  2436. settings.img_width / settings.HorDcm;
  2437. fmt->fmt.pix.height =
  2438. settings.img_height * 2 /
  2439. (settings.TmpDcm * settings.VerDcm);
  2440. if (settings.TmpDcm == 1)
  2441. fmt->fmt.pix.field =
  2442. (fh->jpg_settings.
  2443. odd_even ? V4L2_FIELD_SEQ_TB :
  2444. V4L2_FIELD_SEQ_BT);
  2445. else
  2446. fmt->fmt.pix.field =
  2447. (fh->jpg_settings.
  2448. odd_even ? V4L2_FIELD_TOP :
  2449. V4L2_FIELD_BOTTOM);
  2450. fh->jpg_buffers.buffer_size =
  2451. zoran_v4l2_calc_bufsize(&fh->
  2452. jpg_settings);
  2453. fmt->fmt.pix.bytesperline = 0;
  2454. fmt->fmt.pix.sizeimage =
  2455. fh->jpg_buffers.buffer_size;
  2456. fmt->fmt.pix.colorspace =
  2457. V4L2_COLORSPACE_SMPTE170M;
  2458. /* we hereby abuse this variable to show that
  2459. * we're gonna do mjpeg capture */
  2460. fh->map_mode =
  2461. (fmt->type ==
  2462. V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  2463. ZORAN_MAP_MODE_JPG_REC :
  2464. ZORAN_MAP_MODE_JPG_PLAY;
  2465. sfmtjpg_unlock_and_return:
  2466. mutex_unlock(&zr->resource_lock);
  2467. } else {
  2468. for (i = 0; i < NUM_FORMATS; i++)
  2469. if (fmt->fmt.pix.pixelformat ==
  2470. zoran_formats[i].fourcc)
  2471. break;
  2472. if (i == NUM_FORMATS) {
  2473. dprintk(1,
  2474. KERN_ERR
  2475. "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
  2476. ZR_DEVNAME(zr),
  2477. fmt->fmt.pix.pixelformat,
  2478. (char *) &printformat);
  2479. return -EINVAL;
  2480. }
  2481. mutex_lock(&zr->resource_lock);
  2482. if (fh->jpg_buffers.allocated ||
  2483. (fh->v4l_buffers.allocated &&
  2484. fh->v4l_buffers.active !=
  2485. ZORAN_FREE)) {
  2486. dprintk(1,
  2487. KERN_ERR
  2488. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2489. ZR_DEVNAME(zr));
  2490. res = -EBUSY;
  2491. goto sfmtv4l_unlock_and_return;
  2492. }
  2493. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  2494. fmt->fmt.pix.height =
  2495. BUZ_MAX_HEIGHT;
  2496. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  2497. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  2498. if ((res =
  2499. zoran_v4l_set_format(file,
  2500. fmt->fmt.pix.
  2501. width,
  2502. fmt->fmt.pix.
  2503. height,
  2504. &zoran_formats
  2505. [i])))
  2506. goto sfmtv4l_unlock_and_return;
  2507. /* tell the user the
  2508. * results/missing stuff */
  2509. fmt->fmt.pix.bytesperline =
  2510. fh->v4l_settings.bytesperline;
  2511. fmt->fmt.pix.sizeimage =
  2512. fh->v4l_settings.height *
  2513. fh->v4l_settings.bytesperline;
  2514. fmt->fmt.pix.colorspace =
  2515. fh->v4l_settings.format->colorspace;
  2516. if (BUZ_MAX_HEIGHT <
  2517. (fh->v4l_settings.height * 2))
  2518. fmt->fmt.pix.field =
  2519. V4L2_FIELD_INTERLACED;
  2520. else
  2521. fmt->fmt.pix.field =
  2522. V4L2_FIELD_TOP;
  2523. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2524. sfmtv4l_unlock_and_return:
  2525. mutex_unlock(&zr->resource_lock);
  2526. }
  2527. break;
  2528. default:
  2529. dprintk(1,
  2530. KERN_ERR
  2531. "%s: VIDIOC_S_FMT - unsupported type %d\n",
  2532. ZR_DEVNAME(zr), fmt->type);
  2533. return -EINVAL;
  2534. }
  2535. return res;
  2536. }
  2537. break;
  2538. case VIDIOC_G_FBUF:
  2539. {
  2540. struct v4l2_framebuffer *fb = arg;
  2541. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
  2542. memset(fb, 0, sizeof(*fb));
  2543. mutex_lock(&zr->resource_lock);
  2544. fb->base = zr->buffer.base;
  2545. fb->fmt.width = zr->buffer.width;
  2546. fb->fmt.height = zr->buffer.height;
  2547. if (zr->overlay_settings.format) {
  2548. fb->fmt.pixelformat =
  2549. fh->overlay_settings.format->fourcc;
  2550. }
  2551. fb->fmt.bytesperline = zr->buffer.bytesperline;
  2552. mutex_unlock(&zr->resource_lock);
  2553. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  2554. fb->fmt.field = V4L2_FIELD_INTERLACED;
  2555. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  2556. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2557. return 0;
  2558. }
  2559. break;
  2560. case VIDIOC_S_FBUF:
  2561. {
  2562. int i, res = 0;
  2563. struct v4l2_framebuffer *fb = arg;
  2564. __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  2565. dprintk(3,
  2566. KERN_DEBUG
  2567. "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
  2568. ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
  2569. fb->fmt.bytesperline, fb->fmt.pixelformat,
  2570. (char *) &printformat);
  2571. for (i = 0; i < NUM_FORMATS; i++)
  2572. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  2573. break;
  2574. if (i == NUM_FORMATS) {
  2575. dprintk(1,
  2576. KERN_ERR
  2577. "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  2578. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  2579. (char *) &printformat);
  2580. return -EINVAL;
  2581. }
  2582. mutex_lock(&zr->resource_lock);
  2583. res =
  2584. setup_fbuffer(file, fb->base, &zoran_formats[i],
  2585. fb->fmt.width, fb->fmt.height,
  2586. fb->fmt.bytesperline);
  2587. mutex_unlock(&zr->resource_lock);
  2588. return res;
  2589. }
  2590. break;
  2591. case VIDIOC_OVERLAY:
  2592. {
  2593. int *on = arg, res;
  2594. dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
  2595. ZR_DEVNAME(zr), *on);
  2596. mutex_lock(&zr->resource_lock);
  2597. res = setup_overlay(file, *on);
  2598. mutex_unlock(&zr->resource_lock);
  2599. return res;
  2600. }
  2601. break;
  2602. case VIDIOC_REQBUFS:
  2603. {
  2604. struct v4l2_requestbuffers *req = arg;
  2605. int res = 0;
  2606. dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
  2607. ZR_DEVNAME(zr), req->type);
  2608. if (req->memory != V4L2_MEMORY_MMAP) {
  2609. dprintk(1,
  2610. KERN_ERR
  2611. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  2612. ZR_DEVNAME(zr), req->memory);
  2613. return -EINVAL;
  2614. }
  2615. mutex_lock(&zr->resource_lock);
  2616. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  2617. dprintk(1,
  2618. KERN_ERR
  2619. "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
  2620. ZR_DEVNAME(zr));
  2621. res = -EBUSY;
  2622. goto v4l2reqbuf_unlock_and_return;
  2623. }
  2624. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2625. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2626. /* control user input */
  2627. if (req->count < 2)
  2628. req->count = 2;
  2629. if (req->count > v4l_nbufs)
  2630. req->count = v4l_nbufs;
  2631. fh->v4l_buffers.num_buffers = req->count;
  2632. if (v4l_fbuffer_alloc(file)) {
  2633. res = -ENOMEM;
  2634. goto v4l2reqbuf_unlock_and_return;
  2635. }
  2636. /* The next mmap will map the V4L buffers */
  2637. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2638. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2639. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2640. /* we need to calculate size ourselves now */
  2641. if (req->count < 4)
  2642. req->count = 4;
  2643. if (req->count > jpg_nbufs)
  2644. req->count = jpg_nbufs;
  2645. fh->jpg_buffers.num_buffers = req->count;
  2646. fh->jpg_buffers.buffer_size =
  2647. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2648. if (jpg_fbuffer_alloc(file)) {
  2649. res = -ENOMEM;
  2650. goto v4l2reqbuf_unlock_and_return;
  2651. }
  2652. /* The next mmap will map the MJPEG buffers */
  2653. if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2654. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2655. else
  2656. fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
  2657. } else {
  2658. dprintk(1,
  2659. KERN_ERR
  2660. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2661. ZR_DEVNAME(zr), req->type);
  2662. res = -EINVAL;
  2663. goto v4l2reqbuf_unlock_and_return;
  2664. }
  2665. v4l2reqbuf_unlock_and_return:
  2666. mutex_unlock(&zr->resource_lock);
  2667. return 0;
  2668. }
  2669. break;
  2670. case VIDIOC_QUERYBUF:
  2671. {
  2672. struct v4l2_buffer *buf = arg;
  2673. __u32 type = buf->type;
  2674. int index = buf->index, res;
  2675. dprintk(3,
  2676. KERN_DEBUG
  2677. "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
  2678. ZR_DEVNAME(zr), buf->index, buf->type);
  2679. memset(buf, 0, sizeof(*buf));
  2680. buf->type = type;
  2681. buf->index = index;
  2682. mutex_lock(&zr->resource_lock);
  2683. res = zoran_v4l2_buffer_status(file, buf, buf->index);
  2684. mutex_unlock(&zr->resource_lock);
  2685. return res;
  2686. }
  2687. break;
  2688. case VIDIOC_QBUF:
  2689. {
  2690. struct v4l2_buffer *buf = arg;
  2691. int res = 0, codec_mode, buf_type;
  2692. dprintk(3,
  2693. KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
  2694. ZR_DEVNAME(zr), buf->type, buf->index);
  2695. mutex_lock(&zr->resource_lock);
  2696. switch (fh->map_mode) {
  2697. case ZORAN_MAP_MODE_RAW:
  2698. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2699. dprintk(1,
  2700. KERN_ERR
  2701. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2702. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2703. res = -EINVAL;
  2704. goto qbuf_unlock_and_return;
  2705. }
  2706. res = zoran_v4l_queue_frame(file, buf->index);
  2707. if (res)
  2708. goto qbuf_unlock_and_return;
  2709. if (!zr->v4l_memgrab_active &&
  2710. fh->v4l_buffers.active == ZORAN_LOCKED)
  2711. zr36057_set_memgrab(zr, 1);
  2712. break;
  2713. case ZORAN_MAP_MODE_JPG_REC:
  2714. case ZORAN_MAP_MODE_JPG_PLAY:
  2715. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2716. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2717. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2718. } else {
  2719. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2720. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2721. }
  2722. if (buf->type != buf_type) {
  2723. dprintk(1,
  2724. KERN_ERR
  2725. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2726. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2727. res = -EINVAL;
  2728. goto qbuf_unlock_and_return;
  2729. }
  2730. res =
  2731. zoran_jpg_queue_frame(file, buf->index,
  2732. codec_mode);
  2733. if (res != 0)
  2734. goto qbuf_unlock_and_return;
  2735. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2736. fh->jpg_buffers.active == ZORAN_LOCKED) {
  2737. zr36057_enable_jpg(zr, codec_mode);
  2738. }
  2739. break;
  2740. default:
  2741. dprintk(1,
  2742. KERN_ERR
  2743. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2744. ZR_DEVNAME(zr), buf->type);
  2745. res = -EINVAL;
  2746. goto qbuf_unlock_and_return;
  2747. }
  2748. qbuf_unlock_and_return:
  2749. mutex_unlock(&zr->resource_lock);
  2750. return res;
  2751. }
  2752. break;
  2753. case VIDIOC_DQBUF:
  2754. {
  2755. struct v4l2_buffer *buf = arg;
  2756. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2757. dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
  2758. ZR_DEVNAME(zr), buf->type);
  2759. mutex_lock(&zr->resource_lock);
  2760. switch (fh->map_mode) {
  2761. case ZORAN_MAP_MODE_RAW:
  2762. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2763. dprintk(1,
  2764. KERN_ERR
  2765. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2766. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2767. res = -EINVAL;
  2768. goto dqbuf_unlock_and_return;
  2769. }
  2770. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2771. if (file->f_flags & O_NONBLOCK &&
  2772. zr->v4l_buffers.buffer[num].state !=
  2773. BUZ_STATE_DONE) {
  2774. res = -EAGAIN;
  2775. goto dqbuf_unlock_and_return;
  2776. }
  2777. res = v4l_sync(file, num);
  2778. if (res)
  2779. goto dqbuf_unlock_and_return;
  2780. else
  2781. zr->v4l_sync_tail++;
  2782. res = zoran_v4l2_buffer_status(file, buf, num);
  2783. break;
  2784. case ZORAN_MAP_MODE_JPG_REC:
  2785. case ZORAN_MAP_MODE_JPG_PLAY:
  2786. {
  2787. struct zoran_sync bs;
  2788. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2789. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2790. else
  2791. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2792. if (buf->type != buf_type) {
  2793. dprintk(1,
  2794. KERN_ERR
  2795. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2796. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2797. res = -EINVAL;
  2798. goto dqbuf_unlock_and_return;
  2799. }
  2800. num =
  2801. zr->jpg_pend[zr->
  2802. jpg_que_tail & BUZ_MASK_FRAME];
  2803. if (file->f_flags & O_NONBLOCK &&
  2804. zr->jpg_buffers.buffer[num].state !=
  2805. BUZ_STATE_DONE) {
  2806. res = -EAGAIN;
  2807. goto dqbuf_unlock_and_return;
  2808. }
  2809. res = jpg_sync(file, &bs);
  2810. if (res)
  2811. goto dqbuf_unlock_and_return;
  2812. res =
  2813. zoran_v4l2_buffer_status(file, buf, bs.frame);
  2814. break;
  2815. }
  2816. default:
  2817. dprintk(1,
  2818. KERN_ERR
  2819. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2820. ZR_DEVNAME(zr), buf->type);
  2821. res = -EINVAL;
  2822. goto dqbuf_unlock_and_return;
  2823. }
  2824. dqbuf_unlock_and_return:
  2825. mutex_unlock(&zr->resource_lock);
  2826. return res;
  2827. }
  2828. break;
  2829. case VIDIOC_STREAMON:
  2830. {
  2831. int res = 0;
  2832. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
  2833. mutex_lock(&zr->resource_lock);
  2834. switch (fh->map_mode) {
  2835. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2836. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2837. fh->v4l_buffers.active != ZORAN_ACTIVE) {
  2838. res = -EBUSY;
  2839. goto strmon_unlock_and_return;
  2840. }
  2841. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2842. ZORAN_LOCKED;
  2843. zr->v4l_settings = fh->v4l_settings;
  2844. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2845. if (!zr->v4l_memgrab_active &&
  2846. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2847. zr36057_set_memgrab(zr, 1);
  2848. }
  2849. break;
  2850. case ZORAN_MAP_MODE_JPG_REC:
  2851. case ZORAN_MAP_MODE_JPG_PLAY:
  2852. /* what is the codec mode right now? */
  2853. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2854. fh->jpg_buffers.active != ZORAN_ACTIVE) {
  2855. res = -EBUSY;
  2856. goto strmon_unlock_and_return;
  2857. }
  2858. zr->jpg_buffers.active = fh->jpg_buffers.active =
  2859. ZORAN_LOCKED;
  2860. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2861. /* Start the jpeg codec when the first frame is queued */
  2862. jpeg_start(zr);
  2863. }
  2864. break;
  2865. default:
  2866. dprintk(1,
  2867. KERN_ERR
  2868. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2869. ZR_DEVNAME(zr), fh->map_mode);
  2870. res = -EINVAL;
  2871. goto strmon_unlock_and_return;
  2872. }
  2873. strmon_unlock_and_return:
  2874. mutex_unlock(&zr->resource_lock);
  2875. return res;
  2876. }
  2877. break;
  2878. case VIDIOC_STREAMOFF:
  2879. {
  2880. int i, res = 0;
  2881. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
  2882. mutex_lock(&zr->resource_lock);
  2883. switch (fh->map_mode) {
  2884. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2885. if (fh->v4l_buffers.active == ZORAN_FREE &&
  2886. zr->v4l_buffers.active != ZORAN_FREE) {
  2887. res = -EPERM; /* stay off other's settings! */
  2888. goto strmoff_unlock_and_return;
  2889. }
  2890. if (zr->v4l_buffers.active == ZORAN_FREE)
  2891. goto strmoff_unlock_and_return;
  2892. /* unload capture */
  2893. if (zr->v4l_memgrab_active) {
  2894. unsigned long flags;
  2895. spin_lock_irqsave(&zr->spinlock, flags);
  2896. zr36057_set_memgrab(zr, 0);
  2897. spin_unlock_irqrestore(&zr->spinlock, flags);
  2898. }
  2899. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2900. zr->v4l_buffers.buffer[i].state =
  2901. BUZ_STATE_USER;
  2902. fh->v4l_buffers = zr->v4l_buffers;
  2903. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2904. ZORAN_FREE;
  2905. zr->v4l_grab_seq = 0;
  2906. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  2907. zr->v4l_sync_tail = 0;
  2908. break;
  2909. case ZORAN_MAP_MODE_JPG_REC:
  2910. case ZORAN_MAP_MODE_JPG_PLAY:
  2911. if (fh->jpg_buffers.active == ZORAN_FREE &&
  2912. zr->jpg_buffers.active != ZORAN_FREE) {
  2913. res = -EPERM; /* stay off other's settings! */
  2914. goto strmoff_unlock_and_return;
  2915. }
  2916. if (zr->jpg_buffers.active == ZORAN_FREE)
  2917. goto strmoff_unlock_and_return;
  2918. res =
  2919. jpg_qbuf(file, -1,
  2920. (fh->map_mode ==
  2921. ZORAN_MAP_MODE_JPG_REC) ?
  2922. BUZ_MODE_MOTION_COMPRESS :
  2923. BUZ_MODE_MOTION_DECOMPRESS);
  2924. if (res)
  2925. goto strmoff_unlock_and_return;
  2926. break;
  2927. default:
  2928. dprintk(1,
  2929. KERN_ERR
  2930. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  2931. ZR_DEVNAME(zr), fh->map_mode);
  2932. res = -EINVAL;
  2933. goto strmoff_unlock_and_return;
  2934. }
  2935. strmoff_unlock_and_return:
  2936. mutex_unlock(&zr->resource_lock);
  2937. return res;
  2938. }
  2939. break;
  2940. case VIDIOC_QUERYCTRL:
  2941. {
  2942. struct v4l2_queryctrl *ctrl = arg;
  2943. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
  2944. ZR_DEVNAME(zr), ctrl->id);
  2945. /* we only support hue/saturation/contrast/brightness */
  2946. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2947. ctrl->id > V4L2_CID_HUE)
  2948. return -EINVAL;
  2949. else {
  2950. int id = ctrl->id;
  2951. memset(ctrl, 0, sizeof(*ctrl));
  2952. ctrl->id = id;
  2953. }
  2954. switch (ctrl->id) {
  2955. case V4L2_CID_BRIGHTNESS:
  2956. strncpy(ctrl->name, "Brightness", sizeof(ctrl->name)-1);
  2957. break;
  2958. case V4L2_CID_CONTRAST:
  2959. strncpy(ctrl->name, "Contrast", sizeof(ctrl->name)-1);
  2960. break;
  2961. case V4L2_CID_SATURATION:
  2962. strncpy(ctrl->name, "Saturation", sizeof(ctrl->name)-1);
  2963. break;
  2964. case V4L2_CID_HUE:
  2965. strncpy(ctrl->name, "Hue", sizeof(ctrl->name)-1);
  2966. break;
  2967. }
  2968. ctrl->minimum = 0;
  2969. ctrl->maximum = 65535;
  2970. ctrl->step = 1;
  2971. ctrl->default_value = 32768;
  2972. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  2973. return 0;
  2974. }
  2975. break;
  2976. case VIDIOC_G_CTRL:
  2977. {
  2978. struct v4l2_control *ctrl = arg;
  2979. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
  2980. ZR_DEVNAME(zr), ctrl->id);
  2981. /* we only support hue/saturation/contrast/brightness */
  2982. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2983. ctrl->id > V4L2_CID_HUE)
  2984. return -EINVAL;
  2985. mutex_lock(&zr->resource_lock);
  2986. switch (ctrl->id) {
  2987. case V4L2_CID_BRIGHTNESS:
  2988. ctrl->value = zr->brightness;
  2989. break;
  2990. case V4L2_CID_CONTRAST:
  2991. ctrl->value = zr->contrast;
  2992. break;
  2993. case V4L2_CID_SATURATION:
  2994. ctrl->value = zr->saturation;
  2995. break;
  2996. case V4L2_CID_HUE:
  2997. ctrl->value = zr->hue;
  2998. break;
  2999. }
  3000. mutex_unlock(&zr->resource_lock);
  3001. return 0;
  3002. }
  3003. break;
  3004. case VIDIOC_S_CTRL:
  3005. {
  3006. struct v4l2_control *ctrl = arg;
  3007. struct video_picture pict;
  3008. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
  3009. ZR_DEVNAME(zr), ctrl->id);
  3010. /* we only support hue/saturation/contrast/brightness */
  3011. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3012. ctrl->id > V4L2_CID_HUE)
  3013. return -EINVAL;
  3014. if (ctrl->value < 0 || ctrl->value > 65535) {
  3015. dprintk(1,
  3016. KERN_ERR
  3017. "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
  3018. ZR_DEVNAME(zr), ctrl->value, ctrl->id);
  3019. return -EINVAL;
  3020. }
  3021. mutex_lock(&zr->resource_lock);
  3022. switch (ctrl->id) {
  3023. case V4L2_CID_BRIGHTNESS:
  3024. zr->brightness = ctrl->value;
  3025. break;
  3026. case V4L2_CID_CONTRAST:
  3027. zr->contrast = ctrl->value;
  3028. break;
  3029. case V4L2_CID_SATURATION:
  3030. zr->saturation = ctrl->value;
  3031. break;
  3032. case V4L2_CID_HUE:
  3033. zr->hue = ctrl->value;
  3034. break;
  3035. }
  3036. pict.brightness = zr->brightness;
  3037. pict.contrast = zr->contrast;
  3038. pict.colour = zr->saturation;
  3039. pict.hue = zr->hue;
  3040. decoder_command(zr, DECODER_SET_PICTURE, &pict);
  3041. mutex_unlock(&zr->resource_lock);
  3042. return 0;
  3043. }
  3044. break;
  3045. case VIDIOC_ENUMSTD:
  3046. {
  3047. struct v4l2_standard *std = arg;
  3048. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
  3049. ZR_DEVNAME(zr), std->index);
  3050. if (std->index < 0 || std->index >= (zr->card.norms + 1))
  3051. return -EINVAL;
  3052. else {
  3053. int id = std->index;
  3054. memset(std, 0, sizeof(*std));
  3055. std->index = id;
  3056. }
  3057. if (std->index == zr->card.norms) {
  3058. /* if we have autodetect, ... */
  3059. struct video_decoder_capability caps;
  3060. decoder_command(zr, DECODER_GET_CAPABILITIES,
  3061. &caps);
  3062. if (caps.flags & VIDEO_DECODER_AUTO) {
  3063. std->id = V4L2_STD_ALL;
  3064. strncpy(std->name, "Autodetect", sizeof(std->name)-1);
  3065. return 0;
  3066. } else
  3067. return -EINVAL;
  3068. }
  3069. switch (std->index) {
  3070. case 0:
  3071. std->id = V4L2_STD_PAL;
  3072. strncpy(std->name, "PAL", sizeof(std->name)-1);
  3073. std->frameperiod.numerator = 1;
  3074. std->frameperiod.denominator = 25;
  3075. std->framelines = zr->card.tvn[0]->Ht;
  3076. break;
  3077. case 1:
  3078. std->id = V4L2_STD_NTSC;
  3079. strncpy(std->name, "NTSC", sizeof(std->name)-1);
  3080. std->frameperiod.numerator = 1001;
  3081. std->frameperiod.denominator = 30000;
  3082. std->framelines = zr->card.tvn[1]->Ht;
  3083. break;
  3084. case 2:
  3085. std->id = V4L2_STD_SECAM;
  3086. strncpy(std->name, "SECAM", sizeof(std->name)-1);
  3087. std->frameperiod.numerator = 1;
  3088. std->frameperiod.denominator = 25;
  3089. std->framelines = zr->card.tvn[2]->Ht;
  3090. break;
  3091. }
  3092. return 0;
  3093. }
  3094. break;
  3095. case VIDIOC_G_STD:
  3096. {
  3097. v4l2_std_id *std = arg;
  3098. int norm;
  3099. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
  3100. mutex_lock(&zr->resource_lock);
  3101. norm = zr->norm;
  3102. mutex_unlock(&zr->resource_lock);
  3103. switch (norm) {
  3104. case VIDEO_MODE_PAL:
  3105. *std = V4L2_STD_PAL;
  3106. break;
  3107. case VIDEO_MODE_NTSC:
  3108. *std = V4L2_STD_NTSC;
  3109. break;
  3110. case VIDEO_MODE_SECAM:
  3111. *std = V4L2_STD_SECAM;
  3112. break;
  3113. }
  3114. return 0;
  3115. }
  3116. break;
  3117. case VIDIOC_S_STD:
  3118. {
  3119. int norm = -1, res = 0;
  3120. v4l2_std_id *std = arg;
  3121. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
  3122. ZR_DEVNAME(zr), (unsigned long long)*std);
  3123. if ((*std & V4L2_STD_PAL) && !(*std & ~V4L2_STD_PAL))
  3124. norm = VIDEO_MODE_PAL;
  3125. else if ((*std & V4L2_STD_NTSC) && !(*std & ~V4L2_STD_NTSC))
  3126. norm = VIDEO_MODE_NTSC;
  3127. else if ((*std & V4L2_STD_SECAM) && !(*std & ~V4L2_STD_SECAM))
  3128. norm = VIDEO_MODE_SECAM;
  3129. else if (*std == V4L2_STD_ALL)
  3130. norm = VIDEO_MODE_AUTO;
  3131. else {
  3132. dprintk(1,
  3133. KERN_ERR
  3134. "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
  3135. ZR_DEVNAME(zr), (unsigned long long)*std);
  3136. return -EINVAL;
  3137. }
  3138. mutex_lock(&zr->resource_lock);
  3139. if ((res = zoran_set_norm(zr, norm)))
  3140. goto sstd_unlock_and_return;
  3141. res = wait_grab_pending(zr);
  3142. sstd_unlock_and_return:
  3143. mutex_unlock(&zr->resource_lock);
  3144. return res;
  3145. }
  3146. break;
  3147. case VIDIOC_ENUMINPUT:
  3148. {
  3149. struct v4l2_input *inp = arg;
  3150. int status;
  3151. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
  3152. ZR_DEVNAME(zr), inp->index);
  3153. if (inp->index < 0 || inp->index >= zr->card.inputs)
  3154. return -EINVAL;
  3155. else {
  3156. int id = inp->index;
  3157. memset(inp, 0, sizeof(*inp));
  3158. inp->index = id;
  3159. }
  3160. strncpy(inp->name, zr->card.input[inp->index].name,
  3161. sizeof(inp->name) - 1);
  3162. inp->type = V4L2_INPUT_TYPE_CAMERA;
  3163. inp->std = V4L2_STD_ALL;
  3164. /* Get status of video decoder */
  3165. mutex_lock(&zr->resource_lock);
  3166. decoder_command(zr, DECODER_GET_STATUS, &status);
  3167. mutex_unlock(&zr->resource_lock);
  3168. if (!(status & DECODER_STATUS_GOOD)) {
  3169. inp->status |= V4L2_IN_ST_NO_POWER;
  3170. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  3171. }
  3172. if (!(status & DECODER_STATUS_COLOR))
  3173. inp->status |= V4L2_IN_ST_NO_COLOR;
  3174. return 0;
  3175. }
  3176. break;
  3177. case VIDIOC_G_INPUT:
  3178. {
  3179. int *input = arg;
  3180. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
  3181. mutex_lock(&zr->resource_lock);
  3182. *input = zr->input;
  3183. mutex_unlock(&zr->resource_lock);
  3184. return 0;
  3185. }
  3186. break;
  3187. case VIDIOC_S_INPUT:
  3188. {
  3189. int *input = arg, res = 0;
  3190. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
  3191. ZR_DEVNAME(zr), *input);
  3192. mutex_lock(&zr->resource_lock);
  3193. if ((res = zoran_set_input(zr, *input)))
  3194. goto sinput_unlock_and_return;
  3195. /* Make sure the changes come into effect */
  3196. res = wait_grab_pending(zr);
  3197. sinput_unlock_and_return:
  3198. mutex_unlock(&zr->resource_lock);
  3199. return res;
  3200. }
  3201. break;
  3202. case VIDIOC_ENUMOUTPUT:
  3203. {
  3204. struct v4l2_output *outp = arg;
  3205. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
  3206. ZR_DEVNAME(zr), outp->index);
  3207. if (outp->index != 0)
  3208. return -EINVAL;
  3209. memset(outp, 0, sizeof(*outp));
  3210. outp->index = 0;
  3211. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  3212. strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
  3213. return 0;
  3214. }
  3215. break;
  3216. case VIDIOC_G_OUTPUT:
  3217. {
  3218. int *output = arg;
  3219. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
  3220. *output = 0;
  3221. return 0;
  3222. }
  3223. break;
  3224. case VIDIOC_S_OUTPUT:
  3225. {
  3226. int *output = arg;
  3227. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
  3228. ZR_DEVNAME(zr), *output);
  3229. if (*output != 0)
  3230. return -EINVAL;
  3231. return 0;
  3232. }
  3233. break;
  3234. /* cropping (sub-frame capture) */
  3235. case VIDIOC_CROPCAP:
  3236. {
  3237. struct v4l2_cropcap *cropcap = arg;
  3238. int type = cropcap->type, res = 0;
  3239. dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
  3240. ZR_DEVNAME(zr), cropcap->type);
  3241. memset(cropcap, 0, sizeof(*cropcap));
  3242. cropcap->type = type;
  3243. mutex_lock(&zr->resource_lock);
  3244. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3245. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3246. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3247. dprintk(1,
  3248. KERN_ERR
  3249. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  3250. ZR_DEVNAME(zr));
  3251. res = -EINVAL;
  3252. goto cropcap_unlock_and_return;
  3253. }
  3254. cropcap->bounds.top = cropcap->bounds.left = 0;
  3255. cropcap->bounds.width = BUZ_MAX_WIDTH;
  3256. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  3257. cropcap->defrect.top = cropcap->defrect.left = 0;
  3258. cropcap->defrect.width = BUZ_MIN_WIDTH;
  3259. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  3260. cropcap_unlock_and_return:
  3261. mutex_unlock(&zr->resource_lock);
  3262. return res;
  3263. }
  3264. break;
  3265. case VIDIOC_G_CROP:
  3266. {
  3267. struct v4l2_crop *crop = arg;
  3268. int type = crop->type, res = 0;
  3269. dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
  3270. ZR_DEVNAME(zr), crop->type);
  3271. memset(crop, 0, sizeof(*crop));
  3272. crop->type = type;
  3273. mutex_lock(&zr->resource_lock);
  3274. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3275. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3276. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3277. dprintk(1,
  3278. KERN_ERR
  3279. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3280. ZR_DEVNAME(zr));
  3281. res = -EINVAL;
  3282. goto gcrop_unlock_and_return;
  3283. }
  3284. crop->c.top = fh->jpg_settings.img_y;
  3285. crop->c.left = fh->jpg_settings.img_x;
  3286. crop->c.width = fh->jpg_settings.img_width;
  3287. crop->c.height = fh->jpg_settings.img_height;
  3288. gcrop_unlock_and_return:
  3289. mutex_unlock(&zr->resource_lock);
  3290. return res;
  3291. }
  3292. break;
  3293. case VIDIOC_S_CROP:
  3294. {
  3295. struct v4l2_crop *crop = arg;
  3296. int res = 0;
  3297. settings = fh->jpg_settings;
  3298. dprintk(3,
  3299. KERN_ERR
  3300. "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
  3301. ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
  3302. crop->c.width, crop->c.height);
  3303. mutex_lock(&zr->resource_lock);
  3304. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  3305. dprintk(1,
  3306. KERN_ERR
  3307. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  3308. ZR_DEVNAME(zr));
  3309. res = -EBUSY;
  3310. goto scrop_unlock_and_return;
  3311. }
  3312. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3313. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3314. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3315. dprintk(1,
  3316. KERN_ERR
  3317. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3318. ZR_DEVNAME(zr));
  3319. res = -EINVAL;
  3320. goto scrop_unlock_and_return;
  3321. }
  3322. /* move into a form that we understand */
  3323. settings.img_x = crop->c.left;
  3324. settings.img_y = crop->c.top;
  3325. settings.img_width = crop->c.width;
  3326. settings.img_height = crop->c.height;
  3327. /* check validity */
  3328. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3329. goto scrop_unlock_and_return;
  3330. /* accept */
  3331. fh->jpg_settings = settings;
  3332. scrop_unlock_and_return:
  3333. mutex_unlock(&zr->resource_lock);
  3334. return res;
  3335. }
  3336. break;
  3337. case VIDIOC_G_JPEGCOMP:
  3338. {
  3339. struct v4l2_jpegcompression *params = arg;
  3340. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
  3341. ZR_DEVNAME(zr));
  3342. memset(params, 0, sizeof(*params));
  3343. mutex_lock(&zr->resource_lock);
  3344. params->quality = fh->jpg_settings.jpg_comp.quality;
  3345. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  3346. memcpy(params->APP_data,
  3347. fh->jpg_settings.jpg_comp.APP_data,
  3348. fh->jpg_settings.jpg_comp.APP_len);
  3349. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  3350. memcpy(params->COM_data,
  3351. fh->jpg_settings.jpg_comp.COM_data,
  3352. fh->jpg_settings.jpg_comp.COM_len);
  3353. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  3354. params->jpeg_markers =
  3355. fh->jpg_settings.jpg_comp.jpeg_markers;
  3356. mutex_unlock(&zr->resource_lock);
  3357. return 0;
  3358. }
  3359. break;
  3360. case VIDIOC_S_JPEGCOMP:
  3361. {
  3362. struct v4l2_jpegcompression *params = arg;
  3363. int res = 0;
  3364. settings = fh->jpg_settings;
  3365. dprintk(3,
  3366. KERN_DEBUG
  3367. "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
  3368. ZR_DEVNAME(zr), params->quality, params->APPn,
  3369. params->APP_len, params->COM_len);
  3370. settings.jpg_comp = *params;
  3371. mutex_lock(&zr->resource_lock);
  3372. if (fh->v4l_buffers.active != ZORAN_FREE ||
  3373. fh->jpg_buffers.active != ZORAN_FREE) {
  3374. dprintk(1,
  3375. KERN_WARNING
  3376. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  3377. ZR_DEVNAME(zr));
  3378. res = -EBUSY;
  3379. goto sjpegc_unlock_and_return;
  3380. }
  3381. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3382. goto sjpegc_unlock_and_return;
  3383. if (!fh->jpg_buffers.allocated)
  3384. fh->jpg_buffers.buffer_size =
  3385. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  3386. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  3387. sjpegc_unlock_and_return:
  3388. mutex_unlock(&zr->resource_lock);
  3389. return 0;
  3390. }
  3391. break;
  3392. case VIDIOC_QUERYSTD: /* why is this useful? */
  3393. {
  3394. v4l2_std_id *std = arg;
  3395. dprintk(3,
  3396. KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
  3397. ZR_DEVNAME(zr), (unsigned long long)*std);
  3398. if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
  3399. *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
  3400. zr->card.norms == 3)) {
  3401. return 0;
  3402. }
  3403. return -EINVAL;
  3404. }
  3405. break;
  3406. case VIDIOC_TRY_FMT:
  3407. {
  3408. struct v4l2_format *fmt = arg;
  3409. int res = 0;
  3410. dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
  3411. ZR_DEVNAME(zr), fmt->type);
  3412. switch (fmt->type) {
  3413. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3414. mutex_lock(&zr->resource_lock);
  3415. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  3416. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  3417. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  3418. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  3419. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  3420. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  3421. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  3422. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  3423. mutex_unlock(&zr->resource_lock);
  3424. break;
  3425. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  3426. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  3427. if (fmt->fmt.pix.bytesperline > 0)
  3428. return -EINVAL;
  3429. mutex_lock(&zr->resource_lock);
  3430. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  3431. settings = fh->jpg_settings;
  3432. /* we actually need to set 'real' parameters now */
  3433. if ((fmt->fmt.pix.height * 2) >
  3434. BUZ_MAX_HEIGHT)
  3435. settings.TmpDcm = 1;
  3436. else
  3437. settings.TmpDcm = 2;
  3438. settings.decimation = 0;
  3439. if (fmt->fmt.pix.height <=
  3440. fh->jpg_settings.img_height / 2)
  3441. settings.VerDcm = 2;
  3442. else
  3443. settings.VerDcm = 1;
  3444. if (fmt->fmt.pix.width <=
  3445. fh->jpg_settings.img_width / 4)
  3446. settings.HorDcm = 4;
  3447. else if (fmt->fmt.pix.width <=
  3448. fh->jpg_settings.img_width / 2)
  3449. settings.HorDcm = 2;
  3450. else
  3451. settings.HorDcm = 1;
  3452. if (settings.TmpDcm == 1)
  3453. settings.field_per_buff = 2;
  3454. else
  3455. settings.field_per_buff = 1;
  3456. /* check */
  3457. if ((res =
  3458. zoran_check_jpg_settings(zr,
  3459. &settings)))
  3460. goto tryfmt_unlock_and_return;
  3461. /* tell the user what we actually did */
  3462. fmt->fmt.pix.width =
  3463. settings.img_width / settings.HorDcm;
  3464. fmt->fmt.pix.height =
  3465. settings.img_height * 2 /
  3466. (settings.TmpDcm * settings.VerDcm);
  3467. if (settings.TmpDcm == 1)
  3468. fmt->fmt.pix.field =
  3469. (fh->jpg_settings.
  3470. odd_even ? V4L2_FIELD_SEQ_TB :
  3471. V4L2_FIELD_SEQ_BT);
  3472. else
  3473. fmt->fmt.pix.field =
  3474. (fh->jpg_settings.
  3475. odd_even ? V4L2_FIELD_TOP :
  3476. V4L2_FIELD_BOTTOM);
  3477. fmt->fmt.pix.sizeimage =
  3478. zoran_v4l2_calc_bufsize(&settings);
  3479. } else if (fmt->type ==
  3480. V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  3481. int i;
  3482. for (i = 0; i < NUM_FORMATS; i++)
  3483. if (zoran_formats[i].fourcc ==
  3484. fmt->fmt.pix.pixelformat)
  3485. break;
  3486. if (i == NUM_FORMATS) {
  3487. res = -EINVAL;
  3488. goto tryfmt_unlock_and_return;
  3489. }
  3490. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  3491. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  3492. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  3493. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  3494. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  3495. fmt->fmt.pix.height =
  3496. BUZ_MAX_HEIGHT;
  3497. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  3498. fmt->fmt.pix.height =
  3499. BUZ_MIN_HEIGHT;
  3500. } else {
  3501. res = -EINVAL;
  3502. goto tryfmt_unlock_and_return;
  3503. }
  3504. tryfmt_unlock_and_return:
  3505. mutex_unlock(&zr->resource_lock);
  3506. return res;
  3507. break;
  3508. default:
  3509. return -EINVAL;
  3510. }
  3511. return 0;
  3512. }
  3513. break;
  3514. default:
  3515. dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
  3516. ZR_DEVNAME(zr), cmd);
  3517. return -ENOIOCTLCMD;
  3518. break;
  3519. }
  3520. return 0;
  3521. }
  3522. static long
  3523. zoran_ioctl(struct file *file,
  3524. unsigned int cmd,
  3525. unsigned long arg)
  3526. {
  3527. return video_usercopy(file, cmd, arg, zoran_do_ioctl);
  3528. }
  3529. static unsigned int
  3530. zoran_poll (struct file *file,
  3531. poll_table *wait)
  3532. {
  3533. struct zoran_fh *fh = file->private_data;
  3534. struct zoran *zr = fh->zr;
  3535. int res = 0, frame;
  3536. unsigned long flags;
  3537. /* we should check whether buffers are ready to be synced on
  3538. * (w/o waits - O_NONBLOCK) here
  3539. * if ready for read (sync), return POLLIN|POLLRDNORM,
  3540. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  3541. * if error, return POLLERR,
  3542. * if no buffers queued or so, return POLLNVAL
  3543. */
  3544. mutex_lock(&zr->resource_lock);
  3545. switch (fh->map_mode) {
  3546. case ZORAN_MAP_MODE_RAW:
  3547. poll_wait(file, &zr->v4l_capq, wait);
  3548. frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  3549. spin_lock_irqsave(&zr->spinlock, flags);
  3550. dprintk(3,
  3551. KERN_DEBUG
  3552. "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
  3553. ZR_DEVNAME(zr), __func__,
  3554. "FAL"[fh->v4l_buffers.active], zr->v4l_sync_tail,
  3555. "UPMD"[zr->v4l_buffers.buffer[frame].state],
  3556. zr->v4l_pend_tail, zr->v4l_pend_head);
  3557. /* Process is the one capturing? */
  3558. if (fh->v4l_buffers.active != ZORAN_FREE &&
  3559. /* Buffer ready to DQBUF? */
  3560. zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  3561. res = POLLIN | POLLRDNORM;
  3562. spin_unlock_irqrestore(&zr->spinlock, flags);
  3563. break;
  3564. case ZORAN_MAP_MODE_JPG_REC:
  3565. case ZORAN_MAP_MODE_JPG_PLAY:
  3566. poll_wait(file, &zr->jpg_capq, wait);
  3567. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  3568. spin_lock_irqsave(&zr->spinlock, flags);
  3569. dprintk(3,
  3570. KERN_DEBUG
  3571. "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
  3572. ZR_DEVNAME(zr), __func__,
  3573. "FAL"[fh->jpg_buffers.active], zr->jpg_que_tail,
  3574. "UPMD"[zr->jpg_buffers.buffer[frame].state],
  3575. zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
  3576. if (fh->jpg_buffers.active != ZORAN_FREE &&
  3577. zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  3578. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  3579. res = POLLIN | POLLRDNORM;
  3580. else
  3581. res = POLLOUT | POLLWRNORM;
  3582. }
  3583. spin_unlock_irqrestore(&zr->spinlock, flags);
  3584. break;
  3585. default:
  3586. dprintk(1,
  3587. KERN_ERR
  3588. "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
  3589. ZR_DEVNAME(zr), fh->map_mode);
  3590. res = POLLNVAL;
  3591. }
  3592. mutex_unlock(&zr->resource_lock);
  3593. return res;
  3594. }
  3595. /*
  3596. * This maps the buffers to user space.
  3597. *
  3598. * Depending on the state of fh->map_mode
  3599. * the V4L or the MJPEG buffers are mapped
  3600. * per buffer or all together
  3601. *
  3602. * Note that we need to connect to some
  3603. * unmap signal event to unmap the de-allocate
  3604. * the buffer accordingly (zoran_vm_close())
  3605. */
  3606. static void
  3607. zoran_vm_open (struct vm_area_struct *vma)
  3608. {
  3609. struct zoran_mapping *map = vma->vm_private_data;
  3610. map->count++;
  3611. }
  3612. static void
  3613. zoran_vm_close (struct vm_area_struct *vma)
  3614. {
  3615. struct zoran_mapping *map = vma->vm_private_data;
  3616. struct file *file = map->file;
  3617. struct zoran_fh *fh = file->private_data;
  3618. struct zoran *zr = fh->zr;
  3619. int i;
  3620. map->count--;
  3621. if (map->count == 0) {
  3622. switch (fh->map_mode) {
  3623. case ZORAN_MAP_MODE_JPG_REC:
  3624. case ZORAN_MAP_MODE_JPG_PLAY:
  3625. dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
  3626. ZR_DEVNAME(zr));
  3627. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  3628. if (fh->jpg_buffers.buffer[i].map == map) {
  3629. fh->jpg_buffers.buffer[i].map =
  3630. NULL;
  3631. }
  3632. }
  3633. kfree(map);
  3634. for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
  3635. if (fh->jpg_buffers.buffer[i].map)
  3636. break;
  3637. if (i == fh->jpg_buffers.num_buffers) {
  3638. mutex_lock(&zr->resource_lock);
  3639. if (fh->jpg_buffers.active != ZORAN_FREE) {
  3640. jpg_qbuf(file, -1, zr->codec_mode);
  3641. zr->jpg_buffers.allocated = 0;
  3642. zr->jpg_buffers.active =
  3643. fh->jpg_buffers.active =
  3644. ZORAN_FREE;
  3645. }
  3646. //jpg_fbuffer_free(file);
  3647. fh->jpg_buffers.allocated = 0;
  3648. fh->jpg_buffers.ready_to_be_freed = 1;
  3649. mutex_unlock(&zr->resource_lock);
  3650. }
  3651. break;
  3652. case ZORAN_MAP_MODE_RAW:
  3653. dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
  3654. ZR_DEVNAME(zr));
  3655. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  3656. if (fh->v4l_buffers.buffer[i].map == map) {
  3657. /* unqueue/unmap */
  3658. fh->v4l_buffers.buffer[i].map =
  3659. NULL;
  3660. }
  3661. }
  3662. kfree(map);
  3663. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  3664. if (fh->v4l_buffers.buffer[i].map)
  3665. break;
  3666. if (i == fh->v4l_buffers.num_buffers) {
  3667. mutex_lock(&zr->resource_lock);
  3668. if (fh->v4l_buffers.active != ZORAN_FREE) {
  3669. unsigned long flags;
  3670. spin_lock_irqsave(&zr->spinlock, flags);
  3671. zr36057_set_memgrab(zr, 0);
  3672. zr->v4l_buffers.allocated = 0;
  3673. zr->v4l_buffers.active =
  3674. fh->v4l_buffers.active =
  3675. ZORAN_FREE;
  3676. spin_unlock_irqrestore(&zr->spinlock, flags);
  3677. }
  3678. //v4l_fbuffer_free(file);
  3679. fh->v4l_buffers.allocated = 0;
  3680. fh->v4l_buffers.ready_to_be_freed = 1;
  3681. mutex_unlock(&zr->resource_lock);
  3682. }
  3683. break;
  3684. default:
  3685. printk(KERN_ERR
  3686. "%s: munmap() - internal error - unknown map mode %d\n",
  3687. ZR_DEVNAME(zr), fh->map_mode);
  3688. break;
  3689. }
  3690. }
  3691. }
  3692. static struct vm_operations_struct zoran_vm_ops = {
  3693. .open = zoran_vm_open,
  3694. .close = zoran_vm_close,
  3695. };
  3696. static int
  3697. zoran_mmap (struct file *file,
  3698. struct vm_area_struct *vma)
  3699. {
  3700. struct zoran_fh *fh = file->private_data;
  3701. struct zoran *zr = fh->zr;
  3702. unsigned long size = (vma->vm_end - vma->vm_start);
  3703. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3704. int i, j;
  3705. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  3706. int first, last;
  3707. struct zoran_mapping *map;
  3708. int res = 0;
  3709. dprintk(3,
  3710. KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  3711. ZR_DEVNAME(zr),
  3712. fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
  3713. vma->vm_start, vma->vm_end, size);
  3714. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  3715. !(vma->vm_flags & VM_WRITE)) {
  3716. dprintk(1,
  3717. KERN_ERR
  3718. "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  3719. ZR_DEVNAME(zr));
  3720. return -EINVAL;
  3721. }
  3722. switch (fh->map_mode) {
  3723. case ZORAN_MAP_MODE_JPG_REC:
  3724. case ZORAN_MAP_MODE_JPG_PLAY:
  3725. /* lock */
  3726. mutex_lock(&zr->resource_lock);
  3727. /* Map the MJPEG buffers */
  3728. if (!fh->jpg_buffers.allocated) {
  3729. dprintk(1,
  3730. KERN_ERR
  3731. "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
  3732. ZR_DEVNAME(zr));
  3733. res = -ENOMEM;
  3734. goto jpg_mmap_unlock_and_return;
  3735. }
  3736. first = offset / fh->jpg_buffers.buffer_size;
  3737. last = first - 1 + size / fh->jpg_buffers.buffer_size;
  3738. if (offset % fh->jpg_buffers.buffer_size != 0 ||
  3739. size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
  3740. last < 0 || first >= fh->jpg_buffers.num_buffers ||
  3741. last >= fh->jpg_buffers.num_buffers) {
  3742. dprintk(1,
  3743. KERN_ERR
  3744. "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3745. ZR_DEVNAME(zr), offset, size,
  3746. fh->jpg_buffers.buffer_size,
  3747. fh->jpg_buffers.num_buffers);
  3748. res = -EINVAL;
  3749. goto jpg_mmap_unlock_and_return;
  3750. }
  3751. for (i = first; i <= last; i++) {
  3752. if (fh->jpg_buffers.buffer[i].map) {
  3753. dprintk(1,
  3754. KERN_ERR
  3755. "%s: mmap(MJPEG) - buffer %d already mapped\n",
  3756. ZR_DEVNAME(zr), i);
  3757. res = -EBUSY;
  3758. goto jpg_mmap_unlock_and_return;
  3759. }
  3760. }
  3761. /* map these buffers (v4l_buffers[i]) */
  3762. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3763. if (!map) {
  3764. res = -ENOMEM;
  3765. goto jpg_mmap_unlock_and_return;
  3766. }
  3767. map->file = file;
  3768. map->count = 1;
  3769. vma->vm_ops = &zoran_vm_ops;
  3770. vma->vm_flags |= VM_DONTEXPAND;
  3771. vma->vm_private_data = map;
  3772. for (i = first; i <= last; i++) {
  3773. for (j = 0;
  3774. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  3775. j++) {
  3776. fraglen =
  3777. (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3778. frag_tab[2 * j + 1]) & ~1) << 1;
  3779. todo = size;
  3780. if (todo > fraglen)
  3781. todo = fraglen;
  3782. pos =
  3783. le32_to_cpu(fh->jpg_buffers.
  3784. buffer[i].frag_tab[2 * j]);
  3785. /* should just be pos on i386 */
  3786. page = virt_to_phys(bus_to_virt(pos))
  3787. >> PAGE_SHIFT;
  3788. if (remap_pfn_range(vma, start, page,
  3789. todo, PAGE_SHARED)) {
  3790. dprintk(1,
  3791. KERN_ERR
  3792. "%s: zoran_mmap(V4L) - remap_pfn_range failed\n",
  3793. ZR_DEVNAME(zr));
  3794. res = -EAGAIN;
  3795. goto jpg_mmap_unlock_and_return;
  3796. }
  3797. size -= todo;
  3798. start += todo;
  3799. if (size == 0)
  3800. break;
  3801. if (le32_to_cpu(fh->jpg_buffers.buffer[i].
  3802. frag_tab[2 * j + 1]) & 1)
  3803. break; /* was last fragment */
  3804. }
  3805. fh->jpg_buffers.buffer[i].map = map;
  3806. if (size == 0)
  3807. break;
  3808. }
  3809. jpg_mmap_unlock_and_return:
  3810. mutex_unlock(&zr->resource_lock);
  3811. break;
  3812. case ZORAN_MAP_MODE_RAW:
  3813. mutex_lock(&zr->resource_lock);
  3814. /* Map the V4L buffers */
  3815. if (!fh->v4l_buffers.allocated) {
  3816. dprintk(1,
  3817. KERN_ERR
  3818. "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
  3819. ZR_DEVNAME(zr));
  3820. res = -ENOMEM;
  3821. goto v4l_mmap_unlock_and_return;
  3822. }
  3823. first = offset / fh->v4l_buffers.buffer_size;
  3824. last = first - 1 + size / fh->v4l_buffers.buffer_size;
  3825. if (offset % fh->v4l_buffers.buffer_size != 0 ||
  3826. size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
  3827. last < 0 || first >= fh->v4l_buffers.num_buffers ||
  3828. last >= fh->v4l_buffers.buffer_size) {
  3829. dprintk(1,
  3830. KERN_ERR
  3831. "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3832. ZR_DEVNAME(zr), offset, size,
  3833. fh->v4l_buffers.buffer_size,
  3834. fh->v4l_buffers.num_buffers);
  3835. res = -EINVAL;
  3836. goto v4l_mmap_unlock_and_return;
  3837. }
  3838. for (i = first; i <= last; i++) {
  3839. if (fh->v4l_buffers.buffer[i].map) {
  3840. dprintk(1,
  3841. KERN_ERR
  3842. "%s: mmap(V4L) - buffer %d already mapped\n",
  3843. ZR_DEVNAME(zr), i);
  3844. res = -EBUSY;
  3845. goto v4l_mmap_unlock_and_return;
  3846. }
  3847. }
  3848. /* map these buffers (v4l_buffers[i]) */
  3849. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3850. if (!map) {
  3851. res = -ENOMEM;
  3852. goto v4l_mmap_unlock_and_return;
  3853. }
  3854. map->file = file;
  3855. map->count = 1;
  3856. vma->vm_ops = &zoran_vm_ops;
  3857. vma->vm_flags |= VM_DONTEXPAND;
  3858. vma->vm_private_data = map;
  3859. for (i = first; i <= last; i++) {
  3860. todo = size;
  3861. if (todo > fh->v4l_buffers.buffer_size)
  3862. todo = fh->v4l_buffers.buffer_size;
  3863. page = fh->v4l_buffers.buffer[i].fbuffer_phys;
  3864. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  3865. todo, PAGE_SHARED)) {
  3866. dprintk(1,
  3867. KERN_ERR
  3868. "%s: zoran_mmap(V4L)i - remap_pfn_range failed\n",
  3869. ZR_DEVNAME(zr));
  3870. res = -EAGAIN;
  3871. goto v4l_mmap_unlock_and_return;
  3872. }
  3873. size -= todo;
  3874. start += todo;
  3875. fh->v4l_buffers.buffer[i].map = map;
  3876. if (size == 0)
  3877. break;
  3878. }
  3879. v4l_mmap_unlock_and_return:
  3880. mutex_unlock(&zr->resource_lock);
  3881. break;
  3882. default:
  3883. dprintk(1,
  3884. KERN_ERR
  3885. "%s: zoran_mmap() - internal error - unknown map mode %d\n",
  3886. ZR_DEVNAME(zr), fh->map_mode);
  3887. break;
  3888. }
  3889. return 0;
  3890. }
  3891. static const struct v4l2_file_operations zoran_fops = {
  3892. .owner = THIS_MODULE,
  3893. .open = zoran_open,
  3894. .release = zoran_close,
  3895. .ioctl = zoran_ioctl,
  3896. .read = zoran_read,
  3897. .write = zoran_write,
  3898. .mmap = zoran_mmap,
  3899. .poll = zoran_poll,
  3900. };
  3901. struct video_device zoran_template __devinitdata = {
  3902. .name = ZORAN_NAME,
  3903. .fops = &zoran_fops,
  3904. .release = &zoran_vdev_release,
  3905. .minor = -1
  3906. };