zoran_driver.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405
  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. #include <linux/videodev.h>
  59. #include <media/v4l2-common.h>
  60. #include <media/v4l2-ioctl.h>
  61. #include "videocodec.h"
  62. #include <asm/byteorder.h>
  63. #include <asm/io.h>
  64. #include <asm/uaccess.h>
  65. #include <linux/proc_fs.h>
  66. #include <linux/mutex.h>
  67. #include "zoran.h"
  68. #include "zoran_device.h"
  69. #include "zoran_card.h"
  70. const struct zoran_format zoran_formats[] = {
  71. {
  72. .name = "15-bit RGB LE",
  73. .fourcc = V4L2_PIX_FMT_RGB555,
  74. .colorspace = V4L2_COLORSPACE_SRGB,
  75. .depth = 15,
  76. .flags = ZORAN_FORMAT_CAPTURE |
  77. ZORAN_FORMAT_OVERLAY,
  78. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif|
  79. ZR36057_VFESPFR_LittleEndian,
  80. }, {
  81. .name = "15-bit RGB BE",
  82. .fourcc = V4L2_PIX_FMT_RGB555X,
  83. .colorspace = V4L2_COLORSPACE_SRGB,
  84. .depth = 15,
  85. .flags = ZORAN_FORMAT_CAPTURE |
  86. ZORAN_FORMAT_OVERLAY,
  87. .vfespfr = ZR36057_VFESPFR_RGB555|ZR36057_VFESPFR_ErrDif,
  88. }, {
  89. .name = "16-bit RGB LE",
  90. .fourcc = V4L2_PIX_FMT_RGB565,
  91. .colorspace = V4L2_COLORSPACE_SRGB,
  92. .depth = 16,
  93. .flags = ZORAN_FORMAT_CAPTURE |
  94. ZORAN_FORMAT_OVERLAY,
  95. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif|
  96. ZR36057_VFESPFR_LittleEndian,
  97. }, {
  98. .name = "16-bit RGB BE",
  99. .fourcc = V4L2_PIX_FMT_RGB565X,
  100. .colorspace = V4L2_COLORSPACE_SRGB,
  101. .depth = 16,
  102. .flags = ZORAN_FORMAT_CAPTURE |
  103. ZORAN_FORMAT_OVERLAY,
  104. .vfespfr = ZR36057_VFESPFR_RGB565|ZR36057_VFESPFR_ErrDif,
  105. }, {
  106. .name = "24-bit RGB",
  107. .fourcc = V4L2_PIX_FMT_BGR24,
  108. .colorspace = V4L2_COLORSPACE_SRGB,
  109. .depth = 24,
  110. .flags = ZORAN_FORMAT_CAPTURE |
  111. ZORAN_FORMAT_OVERLAY,
  112. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_Pack24,
  113. }, {
  114. .name = "32-bit RGB LE",
  115. .fourcc = V4L2_PIX_FMT_BGR32,
  116. .colorspace = V4L2_COLORSPACE_SRGB,
  117. .depth = 32,
  118. .flags = ZORAN_FORMAT_CAPTURE |
  119. ZORAN_FORMAT_OVERLAY,
  120. .vfespfr = ZR36057_VFESPFR_RGB888|ZR36057_VFESPFR_LittleEndian,
  121. }, {
  122. .name = "32-bit RGB BE",
  123. .fourcc = V4L2_PIX_FMT_RGB32,
  124. .colorspace = V4L2_COLORSPACE_SRGB,
  125. .depth = 32,
  126. .flags = ZORAN_FORMAT_CAPTURE |
  127. ZORAN_FORMAT_OVERLAY,
  128. .vfespfr = ZR36057_VFESPFR_RGB888,
  129. }, {
  130. .name = "4:2:2, packed, YUYV",
  131. .fourcc = V4L2_PIX_FMT_YUYV,
  132. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  133. .depth = 16,
  134. .flags = ZORAN_FORMAT_CAPTURE |
  135. ZORAN_FORMAT_OVERLAY,
  136. .vfespfr = ZR36057_VFESPFR_YUV422,
  137. }, {
  138. .name = "4:2:2, packed, UYVY",
  139. .fourcc = V4L2_PIX_FMT_UYVY,
  140. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  141. .depth = 16,
  142. .flags = ZORAN_FORMAT_CAPTURE |
  143. ZORAN_FORMAT_OVERLAY,
  144. .vfespfr = ZR36057_VFESPFR_YUV422|ZR36057_VFESPFR_LittleEndian,
  145. }, {
  146. .name = "Hardware-encoded Motion-JPEG",
  147. .fourcc = V4L2_PIX_FMT_MJPEG,
  148. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  149. .depth = 0,
  150. .flags = ZORAN_FORMAT_CAPTURE |
  151. ZORAN_FORMAT_PLAYBACK |
  152. ZORAN_FORMAT_COMPRESSED,
  153. }
  154. };
  155. #define NUM_FORMATS ARRAY_SIZE(zoran_formats)
  156. /* small helper function for calculating buffersizes for v4l2
  157. * we calculate the nearest higher power-of-two, which
  158. * will be the recommended buffersize */
  159. static __u32
  160. zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
  161. {
  162. __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
  163. __u32 num = (1024 * 512) / (div);
  164. __u32 result = 2;
  165. num--;
  166. while (num) {
  167. num >>= 1;
  168. result <<= 1;
  169. }
  170. if (result > jpg_bufsize)
  171. return jpg_bufsize;
  172. if (result < 8192)
  173. return 8192;
  174. return result;
  175. }
  176. /* forward references */
  177. static void v4l_fbuffer_free(struct zoran_fh *fh);
  178. static void jpg_fbuffer_free(struct zoran_fh *fh);
  179. /* Set mapping mode */
  180. static void map_mode_raw(struct zoran_fh *fh)
  181. {
  182. fh->map_mode = ZORAN_MAP_MODE_RAW;
  183. fh->buffers.buffer_size = v4l_bufsize;
  184. fh->buffers.num_buffers = v4l_nbufs;
  185. }
  186. static void map_mode_jpg(struct zoran_fh *fh, int play)
  187. {
  188. fh->map_mode = play ? ZORAN_MAP_MODE_JPG_PLAY : ZORAN_MAP_MODE_JPG_REC;
  189. fh->buffers.buffer_size = jpg_bufsize;
  190. fh->buffers.num_buffers = jpg_nbufs;
  191. }
  192. static inline const char *mode_name(enum zoran_map_mode mode)
  193. {
  194. return mode == ZORAN_MAP_MODE_RAW ? "V4L" : "JPG";
  195. }
  196. /*
  197. * Allocate the V4L grab buffers
  198. *
  199. * These have to be pysically contiguous.
  200. */
  201. static int v4l_fbuffer_alloc(struct zoran_fh *fh)
  202. {
  203. struct zoran *zr = fh->zr;
  204. int i, off;
  205. unsigned char *mem;
  206. for (i = 0; i < fh->buffers.num_buffers; i++) {
  207. if (fh->buffers.buffer[i].v4l.fbuffer)
  208. dprintk(2,
  209. KERN_WARNING
  210. "%s: %s - buffer %d already allocated!?\n",
  211. ZR_DEVNAME(zr), __func__, i);
  212. //udelay(20);
  213. mem = kmalloc(fh->buffers.buffer_size,
  214. GFP_KERNEL | __GFP_NOWARN);
  215. if (!mem) {
  216. dprintk(1,
  217. KERN_ERR
  218. "%s: %s - kmalloc for V4L buf %d failed\n",
  219. ZR_DEVNAME(zr), __func__, i);
  220. v4l_fbuffer_free(fh);
  221. return -ENOBUFS;
  222. }
  223. fh->buffers.buffer[i].v4l.fbuffer = mem;
  224. fh->buffers.buffer[i].v4l.fbuffer_phys = virt_to_phys(mem);
  225. fh->buffers.buffer[i].v4l.fbuffer_bus = virt_to_bus(mem);
  226. for (off = 0; off < fh->buffers.buffer_size;
  227. off += PAGE_SIZE)
  228. SetPageReserved(virt_to_page(mem + off));
  229. dprintk(4,
  230. KERN_INFO
  231. "%s: %s - V4L frame %d mem 0x%lx (bus: 0x%llx)\n",
  232. ZR_DEVNAME(zr), __func__, i, (unsigned long) mem,
  233. (unsigned long long)virt_to_bus(mem));
  234. }
  235. fh->buffers.allocated = 1;
  236. return 0;
  237. }
  238. /* free the V4L grab buffers */
  239. static void v4l_fbuffer_free(struct zoran_fh *fh)
  240. {
  241. struct zoran *zr = fh->zr;
  242. int i, off;
  243. unsigned char *mem;
  244. dprintk(4, KERN_INFO "%s: %s\n", ZR_DEVNAME(zr), __func__);
  245. for (i = 0; i < fh->buffers.num_buffers; i++) {
  246. if (!fh->buffers.buffer[i].v4l.fbuffer)
  247. continue;
  248. mem = fh->buffers.buffer[i].v4l.fbuffer;
  249. for (off = 0; off < fh->buffers.buffer_size;
  250. off += PAGE_SIZE)
  251. ClearPageReserved(virt_to_page(mem + off));
  252. kfree(fh->buffers.buffer[i].v4l.fbuffer);
  253. fh->buffers.buffer[i].v4l.fbuffer = NULL;
  254. }
  255. fh->buffers.allocated = 0;
  256. }
  257. /*
  258. * Allocate the MJPEG grab buffers.
  259. *
  260. * If a Natoma chipset is present and this is a revision 1 zr36057,
  261. * each MJPEG buffer needs to be physically contiguous.
  262. * (RJ: This statement is from Dave Perks' original driver,
  263. * I could never check it because I have a zr36067)
  264. *
  265. * RJ: The contents grab buffers needs never be accessed in the driver.
  266. * Therefore there is no need to allocate them with vmalloc in order
  267. * to get a contiguous virtual memory space.
  268. * I don't understand why many other drivers first allocate them with
  269. * vmalloc (which uses internally also get_zeroed_page, but delivers you
  270. * virtual addresses) and then again have to make a lot of efforts
  271. * to get the physical address.
  272. *
  273. * Ben Capper:
  274. * On big-endian architectures (such as ppc) some extra steps
  275. * are needed. When reading and writing to the stat_com array
  276. * and fragment buffers, the device expects to see little-
  277. * endian values. The use of cpu_to_le32() and le32_to_cpu()
  278. * in this function (and one or two others in zoran_device.c)
  279. * ensure that these values are always stored in little-endian
  280. * form, regardless of architecture. The zr36057 does Very Bad
  281. * Things on big endian architectures if the stat_com array
  282. * and fragment buffers are not little-endian.
  283. */
  284. static int jpg_fbuffer_alloc(struct zoran_fh *fh)
  285. {
  286. struct zoran *zr = fh->zr;
  287. int i, j, off;
  288. u8 *mem;
  289. for (i = 0; i < fh->buffers.num_buffers; i++) {
  290. if (fh->buffers.buffer[i].jpg.frag_tab)
  291. dprintk(2,
  292. KERN_WARNING
  293. "%s: %s - buffer %d already allocated!?\n",
  294. ZR_DEVNAME(zr), __func__, i);
  295. /* Allocate fragment table for this buffer */
  296. mem = (void *)get_zeroed_page(GFP_KERNEL);
  297. if (mem == 0) {
  298. dprintk(1,
  299. KERN_ERR
  300. "%s: %s - get_zeroed_page (frag_tab) failed for buffer %d\n",
  301. ZR_DEVNAME(zr), __func__, i);
  302. jpg_fbuffer_free(fh);
  303. return -ENOBUFS;
  304. }
  305. fh->buffers.buffer[i].jpg.frag_tab = (__le32 *)mem;
  306. fh->buffers.buffer[i].jpg.frag_tab_bus = virt_to_bus(mem);
  307. if (fh->buffers.need_contiguous) {
  308. mem = kmalloc(fh->buffers.buffer_size, GFP_KERNEL);
  309. if (mem == NULL) {
  310. dprintk(1,
  311. KERN_ERR
  312. "%s: %s - kmalloc failed for buffer %d\n",
  313. ZR_DEVNAME(zr), __func__, i);
  314. jpg_fbuffer_free(fh);
  315. return -ENOBUFS;
  316. }
  317. fh->buffers.buffer[i].jpg.frag_tab[0] =
  318. cpu_to_le32(virt_to_bus(mem));
  319. fh->buffers.buffer[i].jpg.frag_tab[1] =
  320. cpu_to_le32((fh->buffers.buffer_size >> 1) | 1);
  321. for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
  322. SetPageReserved(virt_to_page(mem + off));
  323. } else {
  324. /* jpg_bufsize is already page aligned */
  325. for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
  326. mem = (void *)get_zeroed_page(GFP_KERNEL);
  327. if (mem == NULL) {
  328. dprintk(1,
  329. KERN_ERR
  330. "%s: %s - get_zeroed_page failed for buffer %d\n",
  331. ZR_DEVNAME(zr), __func__, i);
  332. jpg_fbuffer_free(fh);
  333. return -ENOBUFS;
  334. }
  335. fh->buffers.buffer[i].jpg.frag_tab[2 * j] =
  336. cpu_to_le32(virt_to_bus(mem));
  337. fh->buffers.buffer[i].jpg.frag_tab[2 * j + 1] =
  338. cpu_to_le32((PAGE_SIZE >> 2) << 1);
  339. SetPageReserved(virt_to_page(mem));
  340. }
  341. fh->buffers.buffer[i].jpg.frag_tab[2 * j - 1] |= cpu_to_le32(1);
  342. }
  343. }
  344. dprintk(4,
  345. KERN_DEBUG "%s: %s - %d KB allocated\n",
  346. ZR_DEVNAME(zr), __func__,
  347. (fh->buffers.num_buffers * fh->buffers.buffer_size) >> 10);
  348. fh->buffers.allocated = 1;
  349. return 0;
  350. }
  351. /* free the MJPEG grab buffers */
  352. static void jpg_fbuffer_free(struct zoran_fh *fh)
  353. {
  354. struct zoran *zr = fh->zr;
  355. int i, j, off;
  356. unsigned char *mem;
  357. __le32 frag_tab;
  358. struct zoran_buffer *buffer;
  359. dprintk(4, KERN_DEBUG "%s: %s\n", ZR_DEVNAME(zr), __func__);
  360. for (i = 0, buffer = &fh->buffers.buffer[0];
  361. i < fh->buffers.num_buffers; i++, buffer++) {
  362. if (!buffer->jpg.frag_tab)
  363. continue;
  364. if (fh->buffers.need_contiguous) {
  365. frag_tab = buffer->jpg.frag_tab[0];
  366. if (frag_tab) {
  367. mem = bus_to_virt(le32_to_cpu(frag_tab));
  368. for (off = 0; off < fh->buffers.buffer_size; off += PAGE_SIZE)
  369. ClearPageReserved(virt_to_page(mem + off));
  370. kfree(mem);
  371. buffer->jpg.frag_tab[0] = 0;
  372. buffer->jpg.frag_tab[1] = 0;
  373. }
  374. } else {
  375. for (j = 0; j < fh->buffers.buffer_size / PAGE_SIZE; j++) {
  376. frag_tab = buffer->jpg.frag_tab[2 * j];
  377. if (!frag_tab)
  378. break;
  379. ClearPageReserved(virt_to_page(bus_to_virt(le32_to_cpu(frag_tab))));
  380. free_page((unsigned long)bus_to_virt(le32_to_cpu(frag_tab)));
  381. buffer->jpg.frag_tab[2 * j] = 0;
  382. buffer->jpg.frag_tab[2 * j + 1] = 0;
  383. }
  384. }
  385. free_page((unsigned long)buffer->jpg.frag_tab);
  386. buffer->jpg.frag_tab = NULL;
  387. }
  388. fh->buffers.allocated = 0;
  389. }
  390. /*
  391. * V4L Buffer grabbing
  392. */
  393. static int
  394. zoran_v4l_set_format (struct zoran_fh *fh,
  395. int width,
  396. int height,
  397. const struct zoran_format *format)
  398. {
  399. struct zoran *zr = fh->zr;
  400. int bpp;
  401. /* Check size and format of the grab wanted */
  402. if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
  403. height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
  404. dprintk(1,
  405. KERN_ERR
  406. "%s: %s - wrong frame size (%dx%d)\n",
  407. ZR_DEVNAME(zr), __func__, width, height);
  408. return -EINVAL;
  409. }
  410. bpp = (format->depth + 7) / 8;
  411. /* Check against available buffer size */
  412. if (height * width * bpp > fh->buffers.buffer_size) {
  413. dprintk(1,
  414. KERN_ERR
  415. "%s: %s - video buffer size (%d kB) is too small\n",
  416. ZR_DEVNAME(zr), __func__, fh->buffers.buffer_size >> 10);
  417. return -EINVAL;
  418. }
  419. /* The video front end needs 4-byte alinged line sizes */
  420. if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
  421. dprintk(1,
  422. KERN_ERR
  423. "%s: %s - wrong frame alignment\n",
  424. ZR_DEVNAME(zr), __func__);
  425. return -EINVAL;
  426. }
  427. fh->v4l_settings.width = width;
  428. fh->v4l_settings.height = height;
  429. fh->v4l_settings.format = format;
  430. fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
  431. return 0;
  432. }
  433. static int zoran_v4l_queue_frame(struct zoran_fh *fh, int num)
  434. {
  435. struct zoran *zr = fh->zr;
  436. unsigned long flags;
  437. int res = 0;
  438. if (!fh->buffers.allocated) {
  439. dprintk(1,
  440. KERN_ERR
  441. "%s: %s - buffers not yet allocated\n",
  442. ZR_DEVNAME(zr), __func__);
  443. res = -ENOMEM;
  444. }
  445. /* No grabbing outside the buffer range! */
  446. if (num >= fh->buffers.num_buffers || num < 0) {
  447. dprintk(1,
  448. KERN_ERR
  449. "%s: %s - buffer %d is out of range\n",
  450. ZR_DEVNAME(zr), __func__, num);
  451. res = -EINVAL;
  452. }
  453. spin_lock_irqsave(&zr->spinlock, flags);
  454. if (fh->buffers.active == ZORAN_FREE) {
  455. if (zr->v4l_buffers.active == ZORAN_FREE) {
  456. zr->v4l_buffers = fh->buffers;
  457. fh->buffers.active = ZORAN_ACTIVE;
  458. } else {
  459. dprintk(1,
  460. KERN_ERR
  461. "%s: %s - another session is already capturing\n",
  462. ZR_DEVNAME(zr), __func__);
  463. res = -EBUSY;
  464. }
  465. }
  466. /* make sure a grab isn't going on currently with this buffer */
  467. if (!res) {
  468. switch (zr->v4l_buffers.buffer[num].state) {
  469. default:
  470. case BUZ_STATE_PEND:
  471. if (zr->v4l_buffers.active == ZORAN_FREE) {
  472. fh->buffers.active = ZORAN_FREE;
  473. zr->v4l_buffers.allocated = 0;
  474. }
  475. res = -EBUSY; /* what are you doing? */
  476. break;
  477. case BUZ_STATE_DONE:
  478. dprintk(2,
  479. KERN_WARNING
  480. "%s: %s - queueing buffer %d in state DONE!?\n",
  481. ZR_DEVNAME(zr), __func__, num);
  482. case BUZ_STATE_USER:
  483. /* since there is at least one unused buffer there's room for at least
  484. * one more pend[] entry */
  485. zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] = num;
  486. zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
  487. zr->v4l_buffers.buffer[num].bs.length =
  488. fh->v4l_settings.bytesperline *
  489. zr->v4l_settings.height;
  490. fh->buffers.buffer[num] = zr->v4l_buffers.buffer[num];
  491. break;
  492. }
  493. }
  494. spin_unlock_irqrestore(&zr->spinlock, flags);
  495. if (!res && zr->v4l_buffers.active == ZORAN_FREE)
  496. zr->v4l_buffers.active = fh->buffers.active;
  497. return res;
  498. }
  499. /*
  500. * Sync on a V4L buffer
  501. */
  502. static int v4l_sync(struct zoran_fh *fh, int frame)
  503. {
  504. struct zoran *zr = fh->zr;
  505. unsigned long flags;
  506. if (fh->buffers.active == ZORAN_FREE) {
  507. dprintk(1,
  508. KERN_ERR
  509. "%s: %s - no grab active for this session\n",
  510. ZR_DEVNAME(zr), __func__);
  511. return -EINVAL;
  512. }
  513. /* check passed-in frame number */
  514. if (frame >= fh->buffers.num_buffers || frame < 0) {
  515. dprintk(1,
  516. KERN_ERR "%s: %s - frame %d is invalid\n",
  517. ZR_DEVNAME(zr), __func__, frame);
  518. return -EINVAL;
  519. }
  520. /* Check if is buffer was queued at all */
  521. if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
  522. dprintk(1,
  523. KERN_ERR
  524. "%s: %s - attempt to sync on a buffer which was not queued?\n",
  525. ZR_DEVNAME(zr), __func__);
  526. return -EPROTO;
  527. }
  528. /* wait on this buffer to get ready */
  529. if (!wait_event_interruptible_timeout(zr->v4l_capq,
  530. (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_PEND), 10*HZ))
  531. return -ETIME;
  532. if (signal_pending(current))
  533. return -ERESTARTSYS;
  534. /* buffer should now be in BUZ_STATE_DONE */
  535. if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
  536. dprintk(2,
  537. KERN_ERR "%s: %s - internal state error\n",
  538. ZR_DEVNAME(zr), __func__);
  539. zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
  540. fh->buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
  541. spin_lock_irqsave(&zr->spinlock, flags);
  542. /* Check if streaming capture has finished */
  543. if (zr->v4l_pend_tail == zr->v4l_pend_head) {
  544. zr36057_set_memgrab(zr, 0);
  545. if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
  546. fh->buffers.active = zr->v4l_buffers.active = ZORAN_FREE;
  547. zr->v4l_buffers.allocated = 0;
  548. }
  549. }
  550. spin_unlock_irqrestore(&zr->spinlock, flags);
  551. return 0;
  552. }
  553. /*
  554. * Queue a MJPEG buffer for capture/playback
  555. */
  556. static int zoran_jpg_queue_frame(struct zoran_fh *fh, int num,
  557. enum zoran_codec_mode mode)
  558. {
  559. struct zoran *zr = fh->zr;
  560. unsigned long flags;
  561. int res = 0;
  562. /* Check if buffers are allocated */
  563. if (!fh->buffers.allocated) {
  564. dprintk(1,
  565. KERN_ERR
  566. "%s: %s - buffers not yet allocated\n",
  567. ZR_DEVNAME(zr), __func__);
  568. return -ENOMEM;
  569. }
  570. /* No grabbing outside the buffer range! */
  571. if (num >= fh->buffers.num_buffers || num < 0) {
  572. dprintk(1,
  573. KERN_ERR
  574. "%s: %s - buffer %d out of range\n",
  575. ZR_DEVNAME(zr), __func__, num);
  576. return -EINVAL;
  577. }
  578. /* what is the codec mode right now? */
  579. if (zr->codec_mode == BUZ_MODE_IDLE) {
  580. zr->jpg_settings = fh->jpg_settings;
  581. } else if (zr->codec_mode != mode) {
  582. /* wrong codec mode active - invalid */
  583. dprintk(1,
  584. KERN_ERR
  585. "%s: %s - codec in wrong mode\n",
  586. ZR_DEVNAME(zr), __func__);
  587. return -EINVAL;
  588. }
  589. if (fh->buffers.active == ZORAN_FREE) {
  590. if (zr->jpg_buffers.active == ZORAN_FREE) {
  591. zr->jpg_buffers = fh->buffers;
  592. fh->buffers.active = ZORAN_ACTIVE;
  593. } else {
  594. dprintk(1,
  595. KERN_ERR
  596. "%s: %s - another session is already capturing\n",
  597. ZR_DEVNAME(zr), __func__);
  598. res = -EBUSY;
  599. }
  600. }
  601. if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
  602. /* Ok load up the jpeg codec */
  603. zr36057_enable_jpg(zr, mode);
  604. }
  605. spin_lock_irqsave(&zr->spinlock, flags);
  606. if (!res) {
  607. switch (zr->jpg_buffers.buffer[num].state) {
  608. case BUZ_STATE_DONE:
  609. dprintk(2,
  610. KERN_WARNING
  611. "%s: %s - queing frame in BUZ_STATE_DONE state!?\n",
  612. ZR_DEVNAME(zr), __func__);
  613. case BUZ_STATE_USER:
  614. /* since there is at least one unused buffer there's room for at
  615. *least one more pend[] entry */
  616. zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = num;
  617. zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
  618. fh->buffers.buffer[num] = zr->jpg_buffers.buffer[num];
  619. zoran_feed_stat_com(zr);
  620. break;
  621. default:
  622. case BUZ_STATE_DMA:
  623. case BUZ_STATE_PEND:
  624. if (zr->jpg_buffers.active == ZORAN_FREE) {
  625. fh->buffers.active = ZORAN_FREE;
  626. zr->jpg_buffers.allocated = 0;
  627. }
  628. res = -EBUSY; /* what are you doing? */
  629. break;
  630. }
  631. }
  632. spin_unlock_irqrestore(&zr->spinlock, flags);
  633. if (!res && zr->jpg_buffers.active == ZORAN_FREE)
  634. zr->jpg_buffers.active = fh->buffers.active;
  635. return res;
  636. }
  637. static int jpg_qbuf(struct zoran_fh *fh, int frame, enum zoran_codec_mode mode)
  638. {
  639. struct zoran *zr = fh->zr;
  640. int res = 0;
  641. /* Does the user want to stop streaming? */
  642. if (frame < 0) {
  643. if (zr->codec_mode == mode) {
  644. if (fh->buffers.active == ZORAN_FREE) {
  645. dprintk(1,
  646. KERN_ERR
  647. "%s: %s(-1) - session not active\n",
  648. ZR_DEVNAME(zr), __func__);
  649. return -EINVAL;
  650. }
  651. fh->buffers.active = zr->jpg_buffers.active = ZORAN_FREE;
  652. zr->jpg_buffers.allocated = 0;
  653. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  654. return 0;
  655. } else {
  656. dprintk(1,
  657. KERN_ERR
  658. "%s: %s - stop streaming but not in streaming mode\n",
  659. ZR_DEVNAME(zr), __func__);
  660. return -EINVAL;
  661. }
  662. }
  663. if ((res = zoran_jpg_queue_frame(fh, frame, mode)))
  664. return res;
  665. /* Start the jpeg codec when the first frame is queued */
  666. if (!res && zr->jpg_que_head == 1)
  667. jpeg_start(zr);
  668. return res;
  669. }
  670. /*
  671. * Sync on a MJPEG buffer
  672. */
  673. static int jpg_sync(struct zoran_fh *fh, struct zoran_sync *bs)
  674. {
  675. struct zoran *zr = fh->zr;
  676. unsigned long flags;
  677. int frame;
  678. if (fh->buffers.active == ZORAN_FREE) {
  679. dprintk(1,
  680. KERN_ERR
  681. "%s: %s - capture is not currently active\n",
  682. ZR_DEVNAME(zr), __func__);
  683. return -EINVAL;
  684. }
  685. if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
  686. zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
  687. dprintk(1,
  688. KERN_ERR
  689. "%s: %s - codec not in streaming mode\n",
  690. ZR_DEVNAME(zr), __func__);
  691. return -EINVAL;
  692. }
  693. if (!wait_event_interruptible_timeout(zr->jpg_capq,
  694. (zr->jpg_que_tail != zr->jpg_dma_tail ||
  695. zr->jpg_dma_tail == zr->jpg_dma_head),
  696. 10*HZ)) {
  697. int isr;
  698. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  699. udelay(1);
  700. zr->codec->control(zr->codec, CODEC_G_STATUS,
  701. sizeof(isr), &isr);
  702. dprintk(1,
  703. KERN_ERR
  704. "%s: %s - timeout: codec isr=0x%02x\n",
  705. ZR_DEVNAME(zr), __func__, isr);
  706. return -ETIME;
  707. }
  708. if (signal_pending(current))
  709. return -ERESTARTSYS;
  710. spin_lock_irqsave(&zr->spinlock, flags);
  711. if (zr->jpg_dma_tail != zr->jpg_dma_head)
  712. frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
  713. else
  714. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  715. /* buffer should now be in BUZ_STATE_DONE */
  716. if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
  717. dprintk(2,
  718. KERN_ERR "%s: %s - internal state error\n",
  719. ZR_DEVNAME(zr), __func__);
  720. *bs = zr->jpg_buffers.buffer[frame].bs;
  721. bs->frame = frame;
  722. zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
  723. fh->buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
  724. spin_unlock_irqrestore(&zr->spinlock, flags);
  725. return 0;
  726. }
  727. static void zoran_open_init_session(struct zoran_fh *fh)
  728. {
  729. int i;
  730. struct zoran *zr = fh->zr;
  731. /* Per default, map the V4L Buffers */
  732. map_mode_raw(fh);
  733. /* take over the card's current settings */
  734. fh->overlay_settings = zr->overlay_settings;
  735. fh->overlay_settings.is_set = 0;
  736. fh->overlay_settings.format = zr->overlay_settings.format;
  737. fh->overlay_active = ZORAN_FREE;
  738. /* v4l settings */
  739. fh->v4l_settings = zr->v4l_settings;
  740. /* jpg settings */
  741. fh->jpg_settings = zr->jpg_settings;
  742. /* buffers */
  743. memset(&fh->buffers, 0, sizeof(fh->buffers));
  744. for (i = 0; i < MAX_FRAME; i++) {
  745. fh->buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  746. fh->buffers.buffer[i].bs.frame = i;
  747. }
  748. fh->buffers.allocated = 0;
  749. fh->buffers.active = ZORAN_FREE;
  750. }
  751. static void zoran_close_end_session(struct zoran_fh *fh)
  752. {
  753. struct zoran *zr = fh->zr;
  754. /* overlay */
  755. if (fh->overlay_active != ZORAN_FREE) {
  756. fh->overlay_active = zr->overlay_active = ZORAN_FREE;
  757. zr->v4l_overlay_active = 0;
  758. if (!zr->v4l_memgrab_active)
  759. zr36057_overlay(zr, 0);
  760. zr->overlay_mask = NULL;
  761. }
  762. if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
  763. /* v4l capture */
  764. if (fh->buffers.active != ZORAN_FREE) {
  765. unsigned long flags;
  766. spin_lock_irqsave(&zr->spinlock, flags);
  767. zr36057_set_memgrab(zr, 0);
  768. zr->v4l_buffers.allocated = 0;
  769. zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
  770. spin_unlock_irqrestore(&zr->spinlock, flags);
  771. }
  772. /* v4l buffers */
  773. if (fh->buffers.allocated)
  774. v4l_fbuffer_free(fh);
  775. } else {
  776. /* jpg capture */
  777. if (fh->buffers.active != ZORAN_FREE) {
  778. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  779. zr->jpg_buffers.allocated = 0;
  780. zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
  781. }
  782. /* jpg buffers */
  783. if (fh->buffers.allocated)
  784. jpg_fbuffer_free(fh);
  785. }
  786. }
  787. /*
  788. * Open a zoran card. Right now the flags stuff is just playing
  789. */
  790. static int zoran_open(struct file *file)
  791. {
  792. struct zoran *zr = video_drvdata(file);
  793. struct zoran_fh *fh;
  794. int res, first_open = 0;
  795. dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(-)=%d\n",
  796. ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user + 1);
  797. lock_kernel();
  798. if (zr->user >= 2048) {
  799. dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
  800. ZR_DEVNAME(zr), zr->user);
  801. res = -EBUSY;
  802. goto fail_unlock;
  803. }
  804. /* now, create the open()-specific file_ops struct */
  805. fh = kzalloc(sizeof(struct zoran_fh), GFP_KERNEL);
  806. if (!fh) {
  807. dprintk(1,
  808. KERN_ERR
  809. "%s: %s - allocation of zoran_fh failed\n",
  810. ZR_DEVNAME(zr), __func__);
  811. res = -ENOMEM;
  812. goto fail_unlock;
  813. }
  814. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  815. * on norm-change! */
  816. fh->overlay_mask =
  817. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  818. if (!fh->overlay_mask) {
  819. dprintk(1,
  820. KERN_ERR
  821. "%s: %s - allocation of overlay_mask failed\n",
  822. ZR_DEVNAME(zr), __func__);
  823. res = -ENOMEM;
  824. goto fail_fh;
  825. }
  826. if (zr->user++ == 0)
  827. first_open = 1;
  828. /*mutex_unlock(&zr->resource_lock);*/
  829. /* default setup - TODO: look at flags */
  830. if (first_open) { /* First device open */
  831. zr36057_restart(zr);
  832. zoran_open_init_params(zr);
  833. zoran_init_hardware(zr);
  834. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  835. }
  836. /* set file_ops stuff */
  837. file->private_data = fh;
  838. fh->zr = zr;
  839. zoran_open_init_session(fh);
  840. unlock_kernel();
  841. return 0;
  842. fail_fh:
  843. kfree(fh);
  844. fail_unlock:
  845. unlock_kernel();
  846. dprintk(2, KERN_INFO "%s: open failed (%d), users(-)=%d\n",
  847. ZR_DEVNAME(zr), res, zr->user);
  848. return res;
  849. }
  850. static int
  851. zoran_close(struct file *file)
  852. {
  853. struct zoran_fh *fh = file->private_data;
  854. struct zoran *zr = fh->zr;
  855. dprintk(2, KERN_INFO "%s: %s(%s, pid=[%d]), users(+)=%d\n",
  856. ZR_DEVNAME(zr), __func__, current->comm, task_pid_nr(current), zr->user - 1);
  857. /* kernel locks (fs/device.c), so don't do that ourselves
  858. * (prevents deadlocks) */
  859. /*mutex_lock(&zr->resource_lock);*/
  860. zoran_close_end_session(fh);
  861. if (zr->user-- == 1) { /* Last process */
  862. /* Clean up JPEG process */
  863. wake_up_interruptible(&zr->jpg_capq);
  864. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  865. zr->jpg_buffers.allocated = 0;
  866. zr->jpg_buffers.active = ZORAN_FREE;
  867. /* disable interrupts */
  868. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  869. if (zr36067_debug > 1)
  870. print_interrupts(zr);
  871. /* Overlay off */
  872. zr->v4l_overlay_active = 0;
  873. zr36057_overlay(zr, 0);
  874. zr->overlay_mask = NULL;
  875. /* capture off */
  876. wake_up_interruptible(&zr->v4l_capq);
  877. zr36057_set_memgrab(zr, 0);
  878. zr->v4l_buffers.allocated = 0;
  879. zr->v4l_buffers.active = ZORAN_FREE;
  880. zoran_set_pci_master(zr, 0);
  881. if (!pass_through) { /* Switch to color bar */
  882. struct v4l2_routing route = { 2, 0 };
  883. decoder_call(zr, video, s_stream, 0);
  884. encoder_call(zr, video, s_routing, &route);
  885. }
  886. }
  887. file->private_data = NULL;
  888. kfree(fh->overlay_mask);
  889. kfree(fh);
  890. dprintk(4, KERN_INFO "%s: %s done\n", ZR_DEVNAME(zr), __func__);
  891. return 0;
  892. }
  893. static ssize_t
  894. zoran_read (struct file *file,
  895. char __user *data,
  896. size_t count,
  897. loff_t *ppos)
  898. {
  899. /* we simply don't support read() (yet)... */
  900. return -EINVAL;
  901. }
  902. static ssize_t
  903. zoran_write (struct file *file,
  904. const char __user *data,
  905. size_t count,
  906. loff_t *ppos)
  907. {
  908. /* ...and the same goes for write() */
  909. return -EINVAL;
  910. }
  911. static int setup_fbuffer(struct zoran_fh *fh,
  912. void *base,
  913. const struct zoran_format *fmt,
  914. int width,
  915. int height,
  916. int bytesperline)
  917. {
  918. struct zoran *zr = fh->zr;
  919. /* (Ronald) v4l/v4l2 guidelines */
  920. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  921. return -EPERM;
  922. /* Don't allow frame buffer overlay if PCI or AGP is buggy, or on
  923. ALi Magik (that needs very low latency while the card needs a
  924. higher value always) */
  925. if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
  926. return -ENXIO;
  927. /* we need a bytesperline value, even if not given */
  928. if (!bytesperline)
  929. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  930. #if 0
  931. if (zr->overlay_active) {
  932. /* dzjee... stupid users... don't even bother to turn off
  933. * overlay before changing the memory location...
  934. * normally, we would return errors here. However, one of
  935. * the tools that does this is... xawtv! and since xawtv
  936. * is used by +/- 99% of the users, we'd rather be user-
  937. * friendly and silently do as if nothing went wrong */
  938. dprintk(3,
  939. KERN_ERR
  940. "%s: %s - forced overlay turnoff because framebuffer changed\n",
  941. ZR_DEVNAME(zr), __func__);
  942. zr36057_overlay(zr, 0);
  943. }
  944. #endif
  945. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  946. dprintk(1,
  947. KERN_ERR
  948. "%s: %s - no valid overlay format given\n",
  949. ZR_DEVNAME(zr), __func__);
  950. return -EINVAL;
  951. }
  952. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  953. dprintk(1,
  954. KERN_ERR
  955. "%s: %s - invalid height/width/bpl value (%d|%d|%d)\n",
  956. ZR_DEVNAME(zr), __func__, width, height, bytesperline);
  957. return -EINVAL;
  958. }
  959. if (bytesperline & 3) {
  960. dprintk(1,
  961. KERN_ERR
  962. "%s: %s - bytesperline (%d) must be 4-byte aligned\n",
  963. ZR_DEVNAME(zr), __func__, bytesperline);
  964. return -EINVAL;
  965. }
  966. zr->vbuf_base = (void *) ((unsigned long) base & ~3);
  967. zr->vbuf_height = height;
  968. zr->vbuf_width = width;
  969. zr->vbuf_depth = fmt->depth;
  970. zr->overlay_settings.format = fmt;
  971. zr->vbuf_bytesperline = bytesperline;
  972. /* The user should set new window parameters */
  973. zr->overlay_settings.is_set = 0;
  974. return 0;
  975. }
  976. static int setup_window(struct zoran_fh *fh, int x, int y, int width, int height,
  977. struct v4l2_clip __user *clips, int clipcount, void __user *bitmap)
  978. {
  979. struct zoran *zr = fh->zr;
  980. struct v4l2_clip *vcp = NULL;
  981. int on, end;
  982. if (!zr->vbuf_base) {
  983. dprintk(1,
  984. KERN_ERR
  985. "%s: %s - frame buffer has to be set first\n",
  986. ZR_DEVNAME(zr), __func__);
  987. return -EINVAL;
  988. }
  989. if (!fh->overlay_settings.format) {
  990. dprintk(1,
  991. KERN_ERR
  992. "%s: %s - no overlay format set\n",
  993. ZR_DEVNAME(zr), __func__);
  994. return -EINVAL;
  995. }
  996. /*
  997. * The video front end needs 4-byte alinged line sizes, we correct that
  998. * silently here if necessary
  999. */
  1000. if (zr->vbuf_depth == 15 || zr->vbuf_depth == 16) {
  1001. end = (x + width) & ~1; /* round down */
  1002. x = (x + 1) & ~1; /* round up */
  1003. width = end - x;
  1004. }
  1005. if (zr->vbuf_depth == 24) {
  1006. end = (x + width) & ~3; /* round down */
  1007. x = (x + 3) & ~3; /* round up */
  1008. width = end - x;
  1009. }
  1010. if (width > BUZ_MAX_WIDTH)
  1011. width = BUZ_MAX_WIDTH;
  1012. if (height > BUZ_MAX_HEIGHT)
  1013. height = BUZ_MAX_HEIGHT;
  1014. /* Check for vaild parameters */
  1015. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1016. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1017. dprintk(1,
  1018. KERN_ERR
  1019. "%s: %s - width = %d or height = %d invalid\n",
  1020. ZR_DEVNAME(zr), __func__, width, height);
  1021. return -EINVAL;
  1022. }
  1023. fh->overlay_settings.x = x;
  1024. fh->overlay_settings.y = y;
  1025. fh->overlay_settings.width = width;
  1026. fh->overlay_settings.height = height;
  1027. fh->overlay_settings.clipcount = clipcount;
  1028. /*
  1029. * If an overlay is running, we have to switch it off
  1030. * and switch it on again in order to get the new settings in effect.
  1031. *
  1032. * We also want to avoid that the overlay mask is written
  1033. * when an overlay is running.
  1034. */
  1035. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1036. zr->overlay_active != ZORAN_FREE &&
  1037. fh->overlay_active != ZORAN_FREE;
  1038. if (on)
  1039. zr36057_overlay(zr, 0);
  1040. /*
  1041. * Write the overlay mask if clips are wanted.
  1042. * We prefer a bitmap.
  1043. */
  1044. if (bitmap) {
  1045. /* fake value - it just means we want clips */
  1046. fh->overlay_settings.clipcount = 1;
  1047. if (copy_from_user(fh->overlay_mask, bitmap,
  1048. (width * height + 7) / 8)) {
  1049. return -EFAULT;
  1050. }
  1051. } else if (clipcount > 0) {
  1052. /* write our own bitmap from the clips */
  1053. vcp = vmalloc(sizeof(struct v4l2_clip) * (clipcount + 4));
  1054. if (vcp == NULL) {
  1055. dprintk(1,
  1056. KERN_ERR
  1057. "%s: %s - Alloc of clip mask failed\n",
  1058. ZR_DEVNAME(zr), __func__);
  1059. return -ENOMEM;
  1060. }
  1061. if (copy_from_user
  1062. (vcp, clips, sizeof(struct v4l2_clip) * clipcount)) {
  1063. vfree(vcp);
  1064. return -EFAULT;
  1065. }
  1066. write_overlay_mask(fh, vcp, clipcount);
  1067. vfree(vcp);
  1068. }
  1069. fh->overlay_settings.is_set = 1;
  1070. if (fh->overlay_active != ZORAN_FREE &&
  1071. zr->overlay_active != ZORAN_FREE)
  1072. zr->overlay_settings = fh->overlay_settings;
  1073. if (on)
  1074. zr36057_overlay(zr, 1);
  1075. /* Make sure the changes come into effect */
  1076. return wait_grab_pending(zr);
  1077. }
  1078. static int setup_overlay(struct zoran_fh *fh, int on)
  1079. {
  1080. struct zoran *zr = fh->zr;
  1081. /* If there is nothing to do, return immediatly */
  1082. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1083. (!on && fh->overlay_active == ZORAN_FREE))
  1084. return 0;
  1085. /* check whether we're touching someone else's overlay */
  1086. if (on && zr->overlay_active != ZORAN_FREE &&
  1087. fh->overlay_active == ZORAN_FREE) {
  1088. dprintk(1,
  1089. KERN_ERR
  1090. "%s: %s - overlay is already active for another session\n",
  1091. ZR_DEVNAME(zr), __func__);
  1092. return -EBUSY;
  1093. }
  1094. if (!on && zr->overlay_active != ZORAN_FREE &&
  1095. fh->overlay_active == ZORAN_FREE) {
  1096. dprintk(1,
  1097. KERN_ERR
  1098. "%s: %s - you cannot cancel someone else's session\n",
  1099. ZR_DEVNAME(zr), __func__);
  1100. return -EPERM;
  1101. }
  1102. if (on == 0) {
  1103. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1104. zr->v4l_overlay_active = 0;
  1105. /* When a grab is running, the video simply
  1106. * won't be switched on any more */
  1107. if (!zr->v4l_memgrab_active)
  1108. zr36057_overlay(zr, 0);
  1109. zr->overlay_mask = NULL;
  1110. } else {
  1111. if (!zr->vbuf_base || !fh->overlay_settings.is_set) {
  1112. dprintk(1,
  1113. KERN_ERR
  1114. "%s: %s - buffer or window not set\n",
  1115. ZR_DEVNAME(zr), __func__);
  1116. return -EINVAL;
  1117. }
  1118. if (!fh->overlay_settings.format) {
  1119. dprintk(1,
  1120. KERN_ERR
  1121. "%s: %s - no overlay format set\n",
  1122. ZR_DEVNAME(zr), __func__);
  1123. return -EINVAL;
  1124. }
  1125. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1126. zr->v4l_overlay_active = 1;
  1127. zr->overlay_mask = fh->overlay_mask;
  1128. zr->overlay_settings = fh->overlay_settings;
  1129. if (!zr->v4l_memgrab_active)
  1130. zr36057_overlay(zr, 1);
  1131. /* When a grab is running, the video will be
  1132. * switched on when grab is finished */
  1133. }
  1134. /* Make sure the changes come into effect */
  1135. return wait_grab_pending(zr);
  1136. }
  1137. /* get the status of a buffer in the clients buffer queue */
  1138. static int zoran_v4l2_buffer_status(struct zoran_fh *fh,
  1139. struct v4l2_buffer *buf, int num)
  1140. {
  1141. struct zoran *zr = fh->zr;
  1142. unsigned long flags;
  1143. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1144. switch (fh->map_mode) {
  1145. case ZORAN_MAP_MODE_RAW:
  1146. /* check range */
  1147. if (num < 0 || num >= fh->buffers.num_buffers ||
  1148. !fh->buffers.allocated) {
  1149. dprintk(1,
  1150. KERN_ERR
  1151. "%s: %s - wrong number or buffers not allocated\n",
  1152. ZR_DEVNAME(zr), __func__);
  1153. return -EINVAL;
  1154. }
  1155. spin_lock_irqsave(&zr->spinlock, flags);
  1156. dprintk(3,
  1157. KERN_DEBUG
  1158. "%s: %s() - raw active=%c, buffer %d: state=%c, map=%c\n",
  1159. ZR_DEVNAME(zr), __func__,
  1160. "FAL"[fh->buffers.active], num,
  1161. "UPMD"[zr->v4l_buffers.buffer[num].state],
  1162. fh->buffers.buffer[num].map ? 'Y' : 'N');
  1163. spin_unlock_irqrestore(&zr->spinlock, flags);
  1164. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1165. buf->length = fh->buffers.buffer_size;
  1166. /* get buffer */
  1167. buf->bytesused = fh->buffers.buffer[num].bs.length;
  1168. if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
  1169. fh->buffers.buffer[num].state == BUZ_STATE_USER) {
  1170. buf->sequence = fh->buffers.buffer[num].bs.seq;
  1171. buf->flags |= V4L2_BUF_FLAG_DONE;
  1172. buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
  1173. } else {
  1174. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1175. }
  1176. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1177. buf->field = V4L2_FIELD_TOP;
  1178. else
  1179. buf->field = V4L2_FIELD_INTERLACED;
  1180. break;
  1181. case ZORAN_MAP_MODE_JPG_REC:
  1182. case ZORAN_MAP_MODE_JPG_PLAY:
  1183. /* check range */
  1184. if (num < 0 || num >= fh->buffers.num_buffers ||
  1185. !fh->buffers.allocated) {
  1186. dprintk(1,
  1187. KERN_ERR
  1188. "%s: %s - wrong number or buffers not allocated\n",
  1189. ZR_DEVNAME(zr), __func__);
  1190. return -EINVAL;
  1191. }
  1192. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1193. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1194. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1195. buf->length = fh->buffers.buffer_size;
  1196. /* these variables are only written after frame has been captured */
  1197. if (fh->buffers.buffer[num].state == BUZ_STATE_DONE ||
  1198. fh->buffers.buffer[num].state == BUZ_STATE_USER) {
  1199. buf->sequence = fh->buffers.buffer[num].bs.seq;
  1200. buf->timestamp = fh->buffers.buffer[num].bs.timestamp;
  1201. buf->bytesused = fh->buffers.buffer[num].bs.length;
  1202. buf->flags |= V4L2_BUF_FLAG_DONE;
  1203. } else {
  1204. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1205. }
  1206. /* which fields are these? */
  1207. if (fh->jpg_settings.TmpDcm != 1)
  1208. buf->field = fh->jpg_settings.odd_even ?
  1209. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1210. else
  1211. buf->field = fh->jpg_settings.odd_even ?
  1212. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT;
  1213. break;
  1214. default:
  1215. dprintk(5,
  1216. KERN_ERR
  1217. "%s: %s - invalid buffer type|map_mode (%d|%d)\n",
  1218. ZR_DEVNAME(zr), __func__, buf->type, fh->map_mode);
  1219. return -EINVAL;
  1220. }
  1221. buf->memory = V4L2_MEMORY_MMAP;
  1222. buf->index = num;
  1223. buf->m.offset = buf->length * num;
  1224. return 0;
  1225. }
  1226. static int
  1227. zoran_set_norm (struct zoran *zr,
  1228. v4l2_std_id norm)
  1229. {
  1230. int on;
  1231. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1232. zr->jpg_buffers.active != ZORAN_FREE) {
  1233. dprintk(1,
  1234. KERN_WARNING
  1235. "%s: %s called while in playback/capture mode\n",
  1236. ZR_DEVNAME(zr), __func__);
  1237. return -EBUSY;
  1238. }
  1239. if (!(norm & zr->card.norms)) {
  1240. dprintk(1,
  1241. KERN_ERR "%s: %s - unsupported norm %llx\n",
  1242. ZR_DEVNAME(zr), __func__, norm);
  1243. return -EINVAL;
  1244. }
  1245. if (norm == V4L2_STD_ALL) {
  1246. int status = 0;
  1247. v4l2_std_id std = 0;
  1248. decoder_call(zr, video, querystd, &std);
  1249. decoder_call(zr, core, s_std, std);
  1250. /* let changes come into effect */
  1251. ssleep(2);
  1252. decoder_call(zr, video, g_input_status, &status);
  1253. if (status & V4L2_IN_ST_NO_SIGNAL) {
  1254. dprintk(1,
  1255. KERN_ERR
  1256. "%s: %s - no norm detected\n",
  1257. ZR_DEVNAME(zr), __func__);
  1258. /* reset norm */
  1259. decoder_call(zr, core, s_std, zr->norm);
  1260. return -EIO;
  1261. }
  1262. norm = std;
  1263. }
  1264. if (norm & V4L2_STD_SECAM)
  1265. zr->timing = zr->card.tvn[2];
  1266. else if (norm & V4L2_STD_NTSC)
  1267. zr->timing = zr->card.tvn[1];
  1268. else
  1269. zr->timing = zr->card.tvn[0];
  1270. /* We switch overlay off and on since a change in the
  1271. * norm needs different VFE settings */
  1272. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1273. if (on)
  1274. zr36057_overlay(zr, 0);
  1275. decoder_call(zr, core, s_std, norm);
  1276. encoder_call(zr, video, s_std_output, norm);
  1277. if (on)
  1278. zr36057_overlay(zr, 1);
  1279. /* Make sure the changes come into effect */
  1280. zr->norm = norm;
  1281. return 0;
  1282. }
  1283. static int
  1284. zoran_set_input (struct zoran *zr,
  1285. int input)
  1286. {
  1287. struct v4l2_routing route = { 0, 0 };
  1288. if (input == zr->input) {
  1289. return 0;
  1290. }
  1291. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1292. zr->jpg_buffers.active != ZORAN_FREE) {
  1293. dprintk(1,
  1294. KERN_WARNING
  1295. "%s: %s called while in playback/capture mode\n",
  1296. ZR_DEVNAME(zr), __func__);
  1297. return -EBUSY;
  1298. }
  1299. if (input < 0 || input >= zr->card.inputs) {
  1300. dprintk(1,
  1301. KERN_ERR
  1302. "%s: %s - unnsupported input %d\n",
  1303. ZR_DEVNAME(zr), __func__, input);
  1304. return -EINVAL;
  1305. }
  1306. route.input = zr->card.input[input].muxsel;
  1307. zr->input = input;
  1308. decoder_call(zr, video, s_routing, &route);
  1309. return 0;
  1310. }
  1311. /*
  1312. * ioctl routine
  1313. */
  1314. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1315. static long zoran_default(struct file *file, void *__fh, int cmd, void *arg)
  1316. {
  1317. struct zoran_fh *fh = __fh;
  1318. struct zoran *zr = fh->zr;
  1319. struct zoran_jpg_settings settings;
  1320. switch (cmd) {
  1321. case BUZIOC_G_PARAMS:
  1322. {
  1323. struct zoran_params *bparams = arg;
  1324. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  1325. memset(bparams, 0, sizeof(struct zoran_params));
  1326. bparams->major_version = MAJOR_VERSION;
  1327. bparams->minor_version = MINOR_VERSION;
  1328. mutex_lock(&zr->resource_lock);
  1329. if (zr->norm & V4L2_STD_NTSC)
  1330. bparams->norm = VIDEO_MODE_NTSC;
  1331. else if (zr->norm & V4L2_STD_PAL)
  1332. bparams->norm = VIDEO_MODE_PAL;
  1333. else
  1334. bparams->norm = VIDEO_MODE_SECAM;
  1335. bparams->input = zr->input;
  1336. bparams->decimation = fh->jpg_settings.decimation;
  1337. bparams->HorDcm = fh->jpg_settings.HorDcm;
  1338. bparams->VerDcm = fh->jpg_settings.VerDcm;
  1339. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  1340. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  1341. bparams->img_x = fh->jpg_settings.img_x;
  1342. bparams->img_y = fh->jpg_settings.img_y;
  1343. bparams->img_width = fh->jpg_settings.img_width;
  1344. bparams->img_height = fh->jpg_settings.img_height;
  1345. bparams->odd_even = fh->jpg_settings.odd_even;
  1346. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  1347. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  1348. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  1349. memcpy(bparams->APP_data,
  1350. fh->jpg_settings.jpg_comp.APP_data,
  1351. sizeof(bparams->APP_data));
  1352. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  1353. memcpy(bparams->COM_data,
  1354. fh->jpg_settings.jpg_comp.COM_data,
  1355. sizeof(bparams->COM_data));
  1356. bparams->jpeg_markers =
  1357. fh->jpg_settings.jpg_comp.jpeg_markers;
  1358. mutex_unlock(&zr->resource_lock);
  1359. bparams->VFIFO_FB = 0;
  1360. return 0;
  1361. }
  1362. case BUZIOC_S_PARAMS:
  1363. {
  1364. struct zoran_params *bparams = arg;
  1365. int res = 0;
  1366. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  1367. settings.decimation = bparams->decimation;
  1368. settings.HorDcm = bparams->HorDcm;
  1369. settings.VerDcm = bparams->VerDcm;
  1370. settings.TmpDcm = bparams->TmpDcm;
  1371. settings.field_per_buff = bparams->field_per_buff;
  1372. settings.img_x = bparams->img_x;
  1373. settings.img_y = bparams->img_y;
  1374. settings.img_width = bparams->img_width;
  1375. settings.img_height = bparams->img_height;
  1376. settings.odd_even = bparams->odd_even;
  1377. settings.jpg_comp.quality = bparams->quality;
  1378. settings.jpg_comp.APPn = bparams->APPn;
  1379. settings.jpg_comp.APP_len = bparams->APP_len;
  1380. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  1381. sizeof(bparams->APP_data));
  1382. settings.jpg_comp.COM_len = bparams->COM_len;
  1383. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  1384. sizeof(bparams->COM_data));
  1385. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  1386. mutex_lock(&zr->resource_lock);
  1387. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1388. dprintk(1,
  1389. KERN_ERR
  1390. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  1391. ZR_DEVNAME(zr));
  1392. res = -EINVAL;
  1393. goto sparams_unlock_and_return;
  1394. }
  1395. /* Check the params first before overwriting our
  1396. * nternal values */
  1397. if (zoran_check_jpg_settings(zr, &settings, 0)) {
  1398. res = -EINVAL;
  1399. goto sparams_unlock_and_return;
  1400. }
  1401. fh->jpg_settings = settings;
  1402. sparams_unlock_and_return:
  1403. mutex_unlock(&zr->resource_lock);
  1404. return res;
  1405. }
  1406. case BUZIOC_REQBUFS:
  1407. {
  1408. struct zoran_requestbuffers *breq = arg;
  1409. int res = 0;
  1410. dprintk(3,
  1411. KERN_DEBUG
  1412. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  1413. ZR_DEVNAME(zr), breq->count, breq->size);
  1414. /* Enforce reasonable lower and upper limits */
  1415. if (breq->count < 4)
  1416. breq->count = 4; /* Could be choosen smaller */
  1417. if (breq->count > jpg_nbufs)
  1418. breq->count = jpg_nbufs;
  1419. breq->size = PAGE_ALIGN(breq->size);
  1420. if (breq->size < 8192)
  1421. breq->size = 8192; /* Arbitrary */
  1422. /* breq->size is limited by 1 page for the stat_com
  1423. * tables to a Maximum of 2 MB */
  1424. if (breq->size > jpg_bufsize)
  1425. breq->size = jpg_bufsize;
  1426. mutex_lock(&zr->resource_lock);
  1427. if (fh->buffers.allocated) {
  1428. dprintk(1,
  1429. KERN_ERR
  1430. "%s: BUZIOC_REQBUFS - buffers already allocated\n",
  1431. ZR_DEVNAME(zr));
  1432. res = -EBUSY;
  1433. goto jpgreqbuf_unlock_and_return;
  1434. }
  1435. /* The next mmap will map the MJPEG buffers - could
  1436. * also be *_PLAY, but it doesn't matter here */
  1437. map_mode_jpg(fh, 0);
  1438. fh->buffers.num_buffers = breq->count;
  1439. fh->buffers.buffer_size = breq->size;
  1440. if (jpg_fbuffer_alloc(fh)) {
  1441. res = -ENOMEM;
  1442. goto jpgreqbuf_unlock_and_return;
  1443. }
  1444. jpgreqbuf_unlock_and_return:
  1445. mutex_unlock(&zr->resource_lock);
  1446. return res;
  1447. }
  1448. case BUZIOC_QBUF_CAPT:
  1449. {
  1450. int *frame = arg, res;
  1451. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  1452. ZR_DEVNAME(zr), *frame);
  1453. mutex_lock(&zr->resource_lock);
  1454. res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_COMPRESS);
  1455. mutex_unlock(&zr->resource_lock);
  1456. return res;
  1457. }
  1458. case BUZIOC_QBUF_PLAY:
  1459. {
  1460. int *frame = arg, res;
  1461. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  1462. ZR_DEVNAME(zr), *frame);
  1463. mutex_lock(&zr->resource_lock);
  1464. res = jpg_qbuf(fh, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  1465. mutex_unlock(&zr->resource_lock);
  1466. return res;
  1467. }
  1468. case BUZIOC_SYNC:
  1469. {
  1470. struct zoran_sync *bsync = arg;
  1471. int res;
  1472. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  1473. mutex_lock(&zr->resource_lock);
  1474. if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
  1475. dprintk(2, KERN_WARNING
  1476. "%s: %s - not in jpg capture mode\n",
  1477. ZR_DEVNAME(zr), __func__);
  1478. res = -EINVAL;
  1479. } else {
  1480. res = jpg_sync(fh, bsync);
  1481. }
  1482. mutex_unlock(&zr->resource_lock);
  1483. return res;
  1484. }
  1485. case BUZIOC_G_STATUS:
  1486. {
  1487. struct zoran_status *bstat = arg;
  1488. struct v4l2_routing route = { 0, 0 };
  1489. int status = 0, res = 0;
  1490. v4l2_std_id norm;
  1491. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  1492. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1493. dprintk(1,
  1494. KERN_ERR
  1495. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  1496. ZR_DEVNAME(zr));
  1497. return -EINVAL;
  1498. }
  1499. route.input = zr->card.input[bstat->input].muxsel;
  1500. mutex_lock(&zr->resource_lock);
  1501. if (zr->codec_mode != BUZ_MODE_IDLE) {
  1502. dprintk(1,
  1503. KERN_ERR
  1504. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  1505. ZR_DEVNAME(zr));
  1506. res = -EINVAL;
  1507. goto gstat_unlock_and_return;
  1508. }
  1509. decoder_call(zr, video, s_routing, &route);
  1510. /* sleep 1 second */
  1511. ssleep(1);
  1512. /* Get status of video decoder */
  1513. decoder_call(zr, video, querystd, &norm);
  1514. decoder_call(zr, video, g_input_status, &status);
  1515. /* restore previous input and norm */
  1516. route.input = zr->card.input[zr->input].muxsel;
  1517. decoder_call(zr, video, s_routing, &route);
  1518. gstat_unlock_and_return:
  1519. mutex_unlock(&zr->resource_lock);
  1520. if (!res) {
  1521. bstat->signal =
  1522. (status & V4L2_IN_ST_NO_SIGNAL) ? 0 : 1;
  1523. if (norm & V4L2_STD_NTSC)
  1524. bstat->norm = VIDEO_MODE_NTSC;
  1525. else if (norm & V4L2_STD_SECAM)
  1526. bstat->norm = VIDEO_MODE_SECAM;
  1527. else
  1528. bstat->norm = VIDEO_MODE_PAL;
  1529. bstat->color =
  1530. (status & V4L2_IN_ST_NO_COLOR) ? 0 : 1;
  1531. }
  1532. return res;
  1533. }
  1534. default:
  1535. return -EINVAL;
  1536. }
  1537. }
  1538. static int zoran_vidiocgmbuf(struct file *file, void *__fh, struct video_mbuf *vmbuf)
  1539. {
  1540. struct zoran_fh *fh = __fh;
  1541. struct zoran *zr = fh->zr;
  1542. int i, res = 0;
  1543. mutex_lock(&zr->resource_lock);
  1544. if (fh->buffers.allocated) {
  1545. dprintk(1,
  1546. KERN_ERR
  1547. "%s: VIDIOCGMBUF - buffers already allocated\n",
  1548. ZR_DEVNAME(zr));
  1549. res = -EINVAL;
  1550. goto v4l1reqbuf_unlock_and_return;
  1551. }
  1552. /* The next mmap will map the V4L buffers */
  1553. map_mode_raw(fh);
  1554. if (v4l_fbuffer_alloc(fh)) {
  1555. res = -ENOMEM;
  1556. goto v4l1reqbuf_unlock_and_return;
  1557. }
  1558. vmbuf->size = fh->buffers.num_buffers * fh->buffers.buffer_size;
  1559. vmbuf->frames = fh->buffers.num_buffers;
  1560. for (i = 0; i < vmbuf->frames; i++)
  1561. vmbuf->offsets[i] = i * fh->buffers.buffer_size;
  1562. v4l1reqbuf_unlock_and_return:
  1563. mutex_unlock(&zr->resource_lock);
  1564. return res;
  1565. }
  1566. #endif
  1567. static int zoran_querycap(struct file *file, void *__fh, struct v4l2_capability *cap)
  1568. {
  1569. struct zoran_fh *fh = __fh;
  1570. struct zoran *zr = fh->zr;
  1571. memset(cap, 0, sizeof(*cap));
  1572. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card)-1);
  1573. strncpy(cap->driver, "zoran", sizeof(cap->driver)-1);
  1574. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  1575. pci_name(zr->pci_dev));
  1576. cap->version = KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  1577. RELEASE_VERSION);
  1578. cap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
  1579. V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_VIDEO_OVERLAY;
  1580. return 0;
  1581. }
  1582. static int zoran_enum_fmt(struct zoran *zr, struct v4l2_fmtdesc *fmt, int flag)
  1583. {
  1584. int num = -1, i;
  1585. for (i = 0; i < NUM_FORMATS; i++) {
  1586. if (zoran_formats[i].flags & flag)
  1587. num++;
  1588. if (num == fmt->index)
  1589. break;
  1590. }
  1591. if (fmt->index < 0 /* late, but not too late */ || i == NUM_FORMATS)
  1592. return -EINVAL;
  1593. strncpy(fmt->description, zoran_formats[i].name, sizeof(fmt->description)-1);
  1594. fmt->pixelformat = zoran_formats[i].fourcc;
  1595. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  1596. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  1597. return 0;
  1598. }
  1599. static int zoran_enum_fmt_vid_cap(struct file *file, void *__fh,
  1600. struct v4l2_fmtdesc *f)
  1601. {
  1602. struct zoran_fh *fh = __fh;
  1603. struct zoran *zr = fh->zr;
  1604. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_CAPTURE);
  1605. }
  1606. static int zoran_enum_fmt_vid_out(struct file *file, void *__fh,
  1607. struct v4l2_fmtdesc *f)
  1608. {
  1609. struct zoran_fh *fh = __fh;
  1610. struct zoran *zr = fh->zr;
  1611. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_PLAYBACK);
  1612. }
  1613. static int zoran_enum_fmt_vid_overlay(struct file *file, void *__fh,
  1614. struct v4l2_fmtdesc *f)
  1615. {
  1616. struct zoran_fh *fh = __fh;
  1617. struct zoran *zr = fh->zr;
  1618. return zoran_enum_fmt(zr, f, ZORAN_FORMAT_OVERLAY);
  1619. }
  1620. static int zoran_g_fmt_vid_out(struct file *file, void *__fh,
  1621. struct v4l2_format *fmt)
  1622. {
  1623. struct zoran_fh *fh = __fh;
  1624. struct zoran *zr = fh->zr;
  1625. mutex_lock(&zr->resource_lock);
  1626. fmt->fmt.pix.width = fh->jpg_settings.img_width / fh->jpg_settings.HorDcm;
  1627. fmt->fmt.pix.height = fh->jpg_settings.img_height * 2 /
  1628. (fh->jpg_settings.VerDcm * fh->jpg_settings.TmpDcm);
  1629. fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  1630. fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
  1631. if (fh->jpg_settings.TmpDcm == 1)
  1632. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1633. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  1634. else
  1635. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1636. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  1637. fmt->fmt.pix.bytesperline = 0;
  1638. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1639. mutex_unlock(&zr->resource_lock);
  1640. return 0;
  1641. }
  1642. static int zoran_g_fmt_vid_cap(struct file *file, void *__fh,
  1643. struct v4l2_format *fmt)
  1644. {
  1645. struct zoran_fh *fh = __fh;
  1646. struct zoran *zr = fh->zr;
  1647. if (fh->map_mode != ZORAN_MAP_MODE_RAW)
  1648. return zoran_g_fmt_vid_out(file, fh, fmt);
  1649. mutex_lock(&zr->resource_lock);
  1650. fmt->fmt.pix.width = fh->v4l_settings.width;
  1651. fmt->fmt.pix.height = fh->v4l_settings.height;
  1652. fmt->fmt.pix.sizeimage = fh->v4l_settings.bytesperline *
  1653. fh->v4l_settings.height;
  1654. fmt->fmt.pix.pixelformat = fh->v4l_settings.format->fourcc;
  1655. fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
  1656. fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
  1657. if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
  1658. fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1659. else
  1660. fmt->fmt.pix.field = V4L2_FIELD_TOP;
  1661. mutex_unlock(&zr->resource_lock);
  1662. return 0;
  1663. }
  1664. static int zoran_g_fmt_vid_overlay(struct file *file, void *__fh,
  1665. struct v4l2_format *fmt)
  1666. {
  1667. struct zoran_fh *fh = __fh;
  1668. struct zoran *zr = fh->zr;
  1669. mutex_lock(&zr->resource_lock);
  1670. fmt->fmt.win.w.left = fh->overlay_settings.x;
  1671. fmt->fmt.win.w.top = fh->overlay_settings.y;
  1672. fmt->fmt.win.w.width = fh->overlay_settings.width;
  1673. fmt->fmt.win.w.height = fh->overlay_settings.height;
  1674. if (fh->overlay_settings.width * 2 > BUZ_MAX_HEIGHT)
  1675. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  1676. else
  1677. fmt->fmt.win.field = V4L2_FIELD_TOP;
  1678. mutex_unlock(&zr->resource_lock);
  1679. return 0;
  1680. }
  1681. static int zoran_try_fmt_vid_overlay(struct file *file, void *__fh,
  1682. struct v4l2_format *fmt)
  1683. {
  1684. struct zoran_fh *fh = __fh;
  1685. struct zoran *zr = fh->zr;
  1686. mutex_lock(&zr->resource_lock);
  1687. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  1688. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  1689. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  1690. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  1691. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  1692. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  1693. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  1694. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  1695. mutex_unlock(&zr->resource_lock);
  1696. return 0;
  1697. }
  1698. static int zoran_try_fmt_vid_out(struct file *file, void *__fh,
  1699. struct v4l2_format *fmt)
  1700. {
  1701. struct zoran_fh *fh = __fh;
  1702. struct zoran *zr = fh->zr;
  1703. struct zoran_jpg_settings settings;
  1704. int res = 0;
  1705. if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1706. return -EINVAL;
  1707. mutex_lock(&zr->resource_lock);
  1708. settings = fh->jpg_settings;
  1709. /* we actually need to set 'real' parameters now */
  1710. if ((fmt->fmt.pix.height * 2) > BUZ_MAX_HEIGHT)
  1711. settings.TmpDcm = 1;
  1712. else
  1713. settings.TmpDcm = 2;
  1714. settings.decimation = 0;
  1715. if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
  1716. settings.VerDcm = 2;
  1717. else
  1718. settings.VerDcm = 1;
  1719. if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
  1720. settings.HorDcm = 4;
  1721. else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
  1722. settings.HorDcm = 2;
  1723. else
  1724. settings.HorDcm = 1;
  1725. if (settings.TmpDcm == 1)
  1726. settings.field_per_buff = 2;
  1727. else
  1728. settings.field_per_buff = 1;
  1729. if (settings.HorDcm > 1) {
  1730. settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
  1731. settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
  1732. } else {
  1733. settings.img_x = 0;
  1734. settings.img_width = BUZ_MAX_WIDTH;
  1735. }
  1736. /* check */
  1737. res = zoran_check_jpg_settings(zr, &settings, 1);
  1738. if (res)
  1739. goto tryfmt_unlock_and_return;
  1740. /* tell the user what we actually did */
  1741. fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
  1742. fmt->fmt.pix.height = settings.img_height * 2 /
  1743. (settings.TmpDcm * settings.VerDcm);
  1744. if (settings.TmpDcm == 1)
  1745. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1746. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  1747. else
  1748. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1749. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  1750. fmt->fmt.pix.sizeimage = zoran_v4l2_calc_bufsize(&settings);
  1751. fmt->fmt.pix.bytesperline = 0;
  1752. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1753. tryfmt_unlock_and_return:
  1754. mutex_unlock(&zr->resource_lock);
  1755. return res;
  1756. }
  1757. static int zoran_try_fmt_vid_cap(struct file *file, void *__fh,
  1758. struct v4l2_format *fmt)
  1759. {
  1760. struct zoran_fh *fh = __fh;
  1761. struct zoran *zr = fh->zr;
  1762. int bpp;
  1763. int i;
  1764. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
  1765. return zoran_try_fmt_vid_out(file, fh, fmt);
  1766. mutex_lock(&zr->resource_lock);
  1767. for (i = 0; i < NUM_FORMATS; i++)
  1768. if (zoran_formats[i].fourcc == fmt->fmt.pix.pixelformat)
  1769. break;
  1770. if (i == NUM_FORMATS) {
  1771. mutex_unlock(&zr->resource_lock);
  1772. return -EINVAL;
  1773. }
  1774. bpp = (zoran_formats[i].depth + 7) / 8;
  1775. fmt->fmt.pix.width &= ~((bpp == 2) ? 1 : 3);
  1776. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  1777. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  1778. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  1779. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  1780. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  1781. fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
  1782. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  1783. fmt->fmt.pix.height = BUZ_MIN_HEIGHT;
  1784. mutex_unlock(&zr->resource_lock);
  1785. return 0;
  1786. }
  1787. static int zoran_s_fmt_vid_overlay(struct file *file, void *__fh,
  1788. struct v4l2_format *fmt)
  1789. {
  1790. struct zoran_fh *fh = __fh;
  1791. struct zoran *zr = fh->zr;
  1792. int res;
  1793. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  1794. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  1795. fmt->fmt.win.w.width,
  1796. fmt->fmt.win.w.height,
  1797. fmt->fmt.win.clipcount,
  1798. fmt->fmt.win.bitmap);
  1799. mutex_lock(&zr->resource_lock);
  1800. res = setup_window(fh, fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  1801. fmt->fmt.win.w.width, fmt->fmt.win.w.height,
  1802. (struct v4l2_clip __user *)fmt->fmt.win.clips,
  1803. fmt->fmt.win.clipcount, fmt->fmt.win.bitmap);
  1804. mutex_unlock(&zr->resource_lock);
  1805. return res;
  1806. }
  1807. static int zoran_s_fmt_vid_out(struct file *file, void *__fh,
  1808. struct v4l2_format *fmt)
  1809. {
  1810. struct zoran_fh *fh = __fh;
  1811. struct zoran *zr = fh->zr;
  1812. __le32 printformat = __cpu_to_le32(fmt->fmt.pix.pixelformat);
  1813. struct zoran_jpg_settings settings;
  1814. int res = 0;
  1815. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  1816. fmt->fmt.pix.width, fmt->fmt.pix.height,
  1817. fmt->fmt.pix.pixelformat,
  1818. (char *) &printformat);
  1819. if (fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG)
  1820. return -EINVAL;
  1821. mutex_lock(&zr->resource_lock);
  1822. if (fh->buffers.allocated) {
  1823. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  1824. ZR_DEVNAME(zr));
  1825. res = -EBUSY;
  1826. goto sfmtjpg_unlock_and_return;
  1827. }
  1828. settings = fh->jpg_settings;
  1829. /* we actually need to set 'real' parameters now */
  1830. if (fmt->fmt.pix.height * 2 > BUZ_MAX_HEIGHT)
  1831. settings.TmpDcm = 1;
  1832. else
  1833. settings.TmpDcm = 2;
  1834. settings.decimation = 0;
  1835. if (fmt->fmt.pix.height <= fh->jpg_settings.img_height / 2)
  1836. settings.VerDcm = 2;
  1837. else
  1838. settings.VerDcm = 1;
  1839. if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 4)
  1840. settings.HorDcm = 4;
  1841. else if (fmt->fmt.pix.width <= fh->jpg_settings.img_width / 2)
  1842. settings.HorDcm = 2;
  1843. else
  1844. settings.HorDcm = 1;
  1845. if (settings.TmpDcm == 1)
  1846. settings.field_per_buff = 2;
  1847. else
  1848. settings.field_per_buff = 1;
  1849. if (settings.HorDcm > 1) {
  1850. settings.img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
  1851. settings.img_width = (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
  1852. } else {
  1853. settings.img_x = 0;
  1854. settings.img_width = BUZ_MAX_WIDTH;
  1855. }
  1856. /* check */
  1857. res = zoran_check_jpg_settings(zr, &settings, 0);
  1858. if (res)
  1859. goto sfmtjpg_unlock_and_return;
  1860. /* it's ok, so set them */
  1861. fh->jpg_settings = settings;
  1862. map_mode_jpg(fh, fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
  1863. fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  1864. /* tell the user what we actually did */
  1865. fmt->fmt.pix.width = settings.img_width / settings.HorDcm;
  1866. fmt->fmt.pix.height = settings.img_height * 2 /
  1867. (settings.TmpDcm * settings.VerDcm);
  1868. if (settings.TmpDcm == 1)
  1869. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1870. V4L2_FIELD_SEQ_TB : V4L2_FIELD_SEQ_BT);
  1871. else
  1872. fmt->fmt.pix.field = (fh->jpg_settings.odd_even ?
  1873. V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM);
  1874. fmt->fmt.pix.bytesperline = 0;
  1875. fmt->fmt.pix.sizeimage = fh->buffers.buffer_size;
  1876. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1877. sfmtjpg_unlock_and_return:
  1878. mutex_unlock(&zr->resource_lock);
  1879. return res;
  1880. }
  1881. static int zoran_s_fmt_vid_cap(struct file *file, void *__fh,
  1882. struct v4l2_format *fmt)
  1883. {
  1884. struct zoran_fh *fh = __fh;
  1885. struct zoran *zr = fh->zr;
  1886. int i;
  1887. int res = 0;
  1888. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG)
  1889. return zoran_s_fmt_vid_out(file, fh, fmt);
  1890. for (i = 0; i < NUM_FORMATS; i++)
  1891. if (fmt->fmt.pix.pixelformat == zoran_formats[i].fourcc)
  1892. break;
  1893. if (i == NUM_FORMATS) {
  1894. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x\n",
  1895. ZR_DEVNAME(zr), fmt->fmt.pix.pixelformat);
  1896. return -EINVAL;
  1897. }
  1898. mutex_lock(&zr->resource_lock);
  1899. if ((fh->map_mode != ZORAN_MAP_MODE_RAW && fh->buffers.allocated) ||
  1900. fh->buffers.active != ZORAN_FREE) {
  1901. dprintk(1, KERN_ERR "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  1902. ZR_DEVNAME(zr));
  1903. res = -EBUSY;
  1904. goto sfmtv4l_unlock_and_return;
  1905. }
  1906. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  1907. fmt->fmt.pix.height = BUZ_MAX_HEIGHT;
  1908. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  1909. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  1910. map_mode_raw(fh);
  1911. res = zoran_v4l_set_format(fh, fmt->fmt.pix.width, fmt->fmt.pix.height,
  1912. &zoran_formats[i]);
  1913. if (res)
  1914. goto sfmtv4l_unlock_and_return;
  1915. /* tell the user the results/missing stuff */
  1916. fmt->fmt.pix.bytesperline = fh->v4l_settings.bytesperline;
  1917. fmt->fmt.pix.sizeimage = fh->v4l_settings.height * fh->v4l_settings.bytesperline;
  1918. fmt->fmt.pix.colorspace = fh->v4l_settings.format->colorspace;
  1919. if (BUZ_MAX_HEIGHT < (fh->v4l_settings.height * 2))
  1920. fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1921. else
  1922. fmt->fmt.pix.field = V4L2_FIELD_TOP;
  1923. sfmtv4l_unlock_and_return:
  1924. mutex_unlock(&zr->resource_lock);
  1925. return res;
  1926. }
  1927. static int zoran_g_fbuf(struct file *file, void *__fh,
  1928. struct v4l2_framebuffer *fb)
  1929. {
  1930. struct zoran_fh *fh = __fh;
  1931. struct zoran *zr = fh->zr;
  1932. memset(fb, 0, sizeof(*fb));
  1933. mutex_lock(&zr->resource_lock);
  1934. fb->base = zr->vbuf_base;
  1935. fb->fmt.width = zr->vbuf_width;
  1936. fb->fmt.height = zr->vbuf_height;
  1937. if (zr->overlay_settings.format)
  1938. fb->fmt.pixelformat = fh->overlay_settings.format->fourcc;
  1939. fb->fmt.bytesperline = zr->vbuf_bytesperline;
  1940. mutex_unlock(&zr->resource_lock);
  1941. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  1942. fb->fmt.field = V4L2_FIELD_INTERLACED;
  1943. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  1944. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  1945. return 0;
  1946. }
  1947. static int zoran_s_fbuf(struct file *file, void *__fh,
  1948. struct v4l2_framebuffer *fb)
  1949. {
  1950. struct zoran_fh *fh = __fh;
  1951. struct zoran *zr = fh->zr;
  1952. int i, res = 0;
  1953. __le32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  1954. for (i = 0; i < NUM_FORMATS; i++)
  1955. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  1956. break;
  1957. if (i == NUM_FORMATS) {
  1958. dprintk(1, KERN_ERR "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  1959. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  1960. (char *)&printformat);
  1961. return -EINVAL;
  1962. }
  1963. mutex_lock(&zr->resource_lock);
  1964. res = setup_fbuffer(fh, fb->base, &zoran_formats[i], fb->fmt.width,
  1965. fb->fmt.height, fb->fmt.bytesperline);
  1966. mutex_unlock(&zr->resource_lock);
  1967. return res;
  1968. }
  1969. static int zoran_overlay(struct file *file, void *__fh, unsigned int on)
  1970. {
  1971. struct zoran_fh *fh = __fh;
  1972. struct zoran *zr = fh->zr;
  1973. int res;
  1974. mutex_lock(&zr->resource_lock);
  1975. res = setup_overlay(fh, on);
  1976. mutex_unlock(&zr->resource_lock);
  1977. return res;
  1978. }
  1979. static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type);
  1980. static int zoran_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *req)
  1981. {
  1982. struct zoran_fh *fh = __fh;
  1983. struct zoran *zr = fh->zr;
  1984. int res = 0;
  1985. if (req->memory != V4L2_MEMORY_MMAP) {
  1986. dprintk(2,
  1987. KERN_ERR
  1988. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  1989. ZR_DEVNAME(zr), req->memory);
  1990. return -EINVAL;
  1991. }
  1992. if (req->count == 0)
  1993. return zoran_streamoff(file, fh, req->type);
  1994. mutex_lock(&zr->resource_lock);
  1995. if (fh->buffers.allocated) {
  1996. dprintk(2,
  1997. KERN_ERR
  1998. "%s: VIDIOC_REQBUFS - buffers already allocated\n",
  1999. ZR_DEVNAME(zr));
  2000. res = -EBUSY;
  2001. goto v4l2reqbuf_unlock_and_return;
  2002. }
  2003. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2004. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2005. /* control user input */
  2006. if (req->count < 2)
  2007. req->count = 2;
  2008. if (req->count > v4l_nbufs)
  2009. req->count = v4l_nbufs;
  2010. /* The next mmap will map the V4L buffers */
  2011. map_mode_raw(fh);
  2012. fh->buffers.num_buffers = req->count;
  2013. if (v4l_fbuffer_alloc(fh)) {
  2014. res = -ENOMEM;
  2015. goto v4l2reqbuf_unlock_and_return;
  2016. }
  2017. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2018. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2019. /* we need to calculate size ourselves now */
  2020. if (req->count < 4)
  2021. req->count = 4;
  2022. if (req->count > jpg_nbufs)
  2023. req->count = jpg_nbufs;
  2024. /* The next mmap will map the MJPEG buffers */
  2025. map_mode_jpg(fh, req->type == V4L2_BUF_TYPE_VIDEO_OUTPUT);
  2026. fh->buffers.num_buffers = req->count;
  2027. fh->buffers.buffer_size = zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2028. if (jpg_fbuffer_alloc(fh)) {
  2029. res = -ENOMEM;
  2030. goto v4l2reqbuf_unlock_and_return;
  2031. }
  2032. } else {
  2033. dprintk(1,
  2034. KERN_ERR
  2035. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2036. ZR_DEVNAME(zr), req->type);
  2037. res = -EINVAL;
  2038. goto v4l2reqbuf_unlock_and_return;
  2039. }
  2040. v4l2reqbuf_unlock_and_return:
  2041. mutex_unlock(&zr->resource_lock);
  2042. return res;
  2043. }
  2044. static int zoran_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2045. {
  2046. struct zoran_fh *fh = __fh;
  2047. struct zoran *zr = fh->zr;
  2048. int res;
  2049. mutex_lock(&zr->resource_lock);
  2050. res = zoran_v4l2_buffer_status(fh, buf, buf->index);
  2051. mutex_unlock(&zr->resource_lock);
  2052. return res;
  2053. }
  2054. static int zoran_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2055. {
  2056. struct zoran_fh *fh = __fh;
  2057. struct zoran *zr = fh->zr;
  2058. int res = 0, codec_mode, buf_type;
  2059. mutex_lock(&zr->resource_lock);
  2060. switch (fh->map_mode) {
  2061. case ZORAN_MAP_MODE_RAW:
  2062. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2063. dprintk(1, KERN_ERR
  2064. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2065. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2066. res = -EINVAL;
  2067. goto qbuf_unlock_and_return;
  2068. }
  2069. res = zoran_v4l_queue_frame(fh, buf->index);
  2070. if (res)
  2071. goto qbuf_unlock_and_return;
  2072. if (!zr->v4l_memgrab_active && fh->buffers.active == ZORAN_LOCKED)
  2073. zr36057_set_memgrab(zr, 1);
  2074. break;
  2075. case ZORAN_MAP_MODE_JPG_REC:
  2076. case ZORAN_MAP_MODE_JPG_PLAY:
  2077. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2078. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2079. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2080. } else {
  2081. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2082. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2083. }
  2084. if (buf->type != buf_type) {
  2085. dprintk(1, KERN_ERR
  2086. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2087. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2088. res = -EINVAL;
  2089. goto qbuf_unlock_and_return;
  2090. }
  2091. res = zoran_jpg_queue_frame(fh, buf->index, codec_mode);
  2092. if (res != 0)
  2093. goto qbuf_unlock_and_return;
  2094. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2095. fh->buffers.active == ZORAN_LOCKED)
  2096. zr36057_enable_jpg(zr, codec_mode);
  2097. break;
  2098. default:
  2099. dprintk(1, KERN_ERR
  2100. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2101. ZR_DEVNAME(zr), buf->type);
  2102. res = -EINVAL;
  2103. break;
  2104. }
  2105. qbuf_unlock_and_return:
  2106. mutex_unlock(&zr->resource_lock);
  2107. return res;
  2108. }
  2109. static int zoran_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf)
  2110. {
  2111. struct zoran_fh *fh = __fh;
  2112. struct zoran *zr = fh->zr;
  2113. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2114. mutex_lock(&zr->resource_lock);
  2115. switch (fh->map_mode) {
  2116. case ZORAN_MAP_MODE_RAW:
  2117. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2118. dprintk(1, KERN_ERR
  2119. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2120. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2121. res = -EINVAL;
  2122. goto dqbuf_unlock_and_return;
  2123. }
  2124. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2125. if (file->f_flags & O_NONBLOCK &&
  2126. zr->v4l_buffers.buffer[num].state != BUZ_STATE_DONE) {
  2127. res = -EAGAIN;
  2128. goto dqbuf_unlock_and_return;
  2129. }
  2130. res = v4l_sync(fh, num);
  2131. if (res)
  2132. goto dqbuf_unlock_and_return;
  2133. zr->v4l_sync_tail++;
  2134. res = zoran_v4l2_buffer_status(fh, buf, num);
  2135. break;
  2136. case ZORAN_MAP_MODE_JPG_REC:
  2137. case ZORAN_MAP_MODE_JPG_PLAY:
  2138. {
  2139. struct zoran_sync bs;
  2140. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2141. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2142. else
  2143. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2144. if (buf->type != buf_type) {
  2145. dprintk(1, KERN_ERR
  2146. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2147. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2148. res = -EINVAL;
  2149. goto dqbuf_unlock_and_return;
  2150. }
  2151. num = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  2152. if (file->f_flags & O_NONBLOCK &&
  2153. zr->jpg_buffers.buffer[num].state != BUZ_STATE_DONE) {
  2154. res = -EAGAIN;
  2155. goto dqbuf_unlock_and_return;
  2156. }
  2157. res = jpg_sync(fh, &bs);
  2158. if (res)
  2159. goto dqbuf_unlock_and_return;
  2160. res = zoran_v4l2_buffer_status(fh, buf, bs.frame);
  2161. break;
  2162. }
  2163. default:
  2164. dprintk(1, KERN_ERR
  2165. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2166. ZR_DEVNAME(zr), buf->type);
  2167. res = -EINVAL;
  2168. break;
  2169. }
  2170. dqbuf_unlock_and_return:
  2171. mutex_unlock(&zr->resource_lock);
  2172. return res;
  2173. }
  2174. static int zoran_streamon(struct file *file, void *__fh, enum v4l2_buf_type type)
  2175. {
  2176. struct zoran_fh *fh = __fh;
  2177. struct zoran *zr = fh->zr;
  2178. int res = 0;
  2179. mutex_lock(&zr->resource_lock);
  2180. switch (fh->map_mode) {
  2181. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2182. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2183. fh->buffers.active != ZORAN_ACTIVE) {
  2184. res = -EBUSY;
  2185. goto strmon_unlock_and_return;
  2186. }
  2187. zr->v4l_buffers.active = fh->buffers.active = ZORAN_LOCKED;
  2188. zr->v4l_settings = fh->v4l_settings;
  2189. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2190. if (!zr->v4l_memgrab_active &&
  2191. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2192. zr36057_set_memgrab(zr, 1);
  2193. }
  2194. break;
  2195. case ZORAN_MAP_MODE_JPG_REC:
  2196. case ZORAN_MAP_MODE_JPG_PLAY:
  2197. /* what is the codec mode right now? */
  2198. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2199. fh->buffers.active != ZORAN_ACTIVE) {
  2200. res = -EBUSY;
  2201. goto strmon_unlock_and_return;
  2202. }
  2203. zr->jpg_buffers.active = fh->buffers.active = ZORAN_LOCKED;
  2204. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2205. /* Start the jpeg codec when the first frame is queued */
  2206. jpeg_start(zr);
  2207. }
  2208. break;
  2209. default:
  2210. dprintk(1,
  2211. KERN_ERR
  2212. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2213. ZR_DEVNAME(zr), fh->map_mode);
  2214. res = -EINVAL;
  2215. break;
  2216. }
  2217. strmon_unlock_and_return:
  2218. mutex_unlock(&zr->resource_lock);
  2219. return res;
  2220. }
  2221. static int zoran_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type)
  2222. {
  2223. struct zoran_fh *fh = __fh;
  2224. struct zoran *zr = fh->zr;
  2225. int i, res = 0;
  2226. unsigned long flags;
  2227. mutex_lock(&zr->resource_lock);
  2228. switch (fh->map_mode) {
  2229. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2230. if (fh->buffers.active == ZORAN_FREE &&
  2231. zr->v4l_buffers.active != ZORAN_FREE) {
  2232. res = -EPERM; /* stay off other's settings! */
  2233. goto strmoff_unlock_and_return;
  2234. }
  2235. if (zr->v4l_buffers.active == ZORAN_FREE)
  2236. goto strmoff_unlock_and_return;
  2237. spin_lock_irqsave(&zr->spinlock, flags);
  2238. /* unload capture */
  2239. if (zr->v4l_memgrab_active) {
  2240. zr36057_set_memgrab(zr, 0);
  2241. }
  2242. for (i = 0; i < fh->buffers.num_buffers; i++)
  2243. zr->v4l_buffers.buffer[i].state = BUZ_STATE_USER;
  2244. fh->buffers = zr->v4l_buffers;
  2245. zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
  2246. zr->v4l_grab_seq = 0;
  2247. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  2248. zr->v4l_sync_tail = 0;
  2249. spin_unlock_irqrestore(&zr->spinlock, flags);
  2250. break;
  2251. case ZORAN_MAP_MODE_JPG_REC:
  2252. case ZORAN_MAP_MODE_JPG_PLAY:
  2253. if (fh->buffers.active == ZORAN_FREE &&
  2254. zr->jpg_buffers.active != ZORAN_FREE) {
  2255. res = -EPERM; /* stay off other's settings! */
  2256. goto strmoff_unlock_and_return;
  2257. }
  2258. if (zr->jpg_buffers.active == ZORAN_FREE)
  2259. goto strmoff_unlock_and_return;
  2260. res = jpg_qbuf(fh, -1,
  2261. (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  2262. BUZ_MODE_MOTION_COMPRESS :
  2263. BUZ_MODE_MOTION_DECOMPRESS);
  2264. if (res)
  2265. goto strmoff_unlock_and_return;
  2266. break;
  2267. default:
  2268. dprintk(1, KERN_ERR
  2269. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  2270. ZR_DEVNAME(zr), fh->map_mode);
  2271. res = -EINVAL;
  2272. break;
  2273. }
  2274. strmoff_unlock_and_return:
  2275. mutex_unlock(&zr->resource_lock);
  2276. return res;
  2277. }
  2278. static int zoran_queryctrl(struct file *file, void *__fh,
  2279. struct v4l2_queryctrl *ctrl)
  2280. {
  2281. struct zoran_fh *fh = __fh;
  2282. struct zoran *zr = fh->zr;
  2283. /* we only support hue/saturation/contrast/brightness */
  2284. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2285. ctrl->id > V4L2_CID_HUE)
  2286. return -EINVAL;
  2287. decoder_call(zr, core, queryctrl, ctrl);
  2288. return 0;
  2289. }
  2290. static int zoran_g_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
  2291. {
  2292. struct zoran_fh *fh = __fh;
  2293. struct zoran *zr = fh->zr;
  2294. /* we only support hue/saturation/contrast/brightness */
  2295. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2296. ctrl->id > V4L2_CID_HUE)
  2297. return -EINVAL;
  2298. mutex_lock(&zr->resource_lock);
  2299. decoder_call(zr, core, g_ctrl, ctrl);
  2300. mutex_unlock(&zr->resource_lock);
  2301. return 0;
  2302. }
  2303. static int zoran_s_ctrl(struct file *file, void *__fh, struct v4l2_control *ctrl)
  2304. {
  2305. struct zoran_fh *fh = __fh;
  2306. struct zoran *zr = fh->zr;
  2307. /* we only support hue/saturation/contrast/brightness */
  2308. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  2309. ctrl->id > V4L2_CID_HUE)
  2310. return -EINVAL;
  2311. mutex_lock(&zr->resource_lock);
  2312. decoder_call(zr, core, s_ctrl, ctrl);
  2313. mutex_unlock(&zr->resource_lock);
  2314. return 0;
  2315. }
  2316. static int zoran_g_std(struct file *file, void *__fh, v4l2_std_id *std)
  2317. {
  2318. struct zoran_fh *fh = __fh;
  2319. struct zoran *zr = fh->zr;
  2320. mutex_lock(&zr->resource_lock);
  2321. *std = zr->norm;
  2322. mutex_unlock(&zr->resource_lock);
  2323. return 0;
  2324. }
  2325. static int zoran_s_std(struct file *file, void *__fh, v4l2_std_id *std)
  2326. {
  2327. struct zoran_fh *fh = __fh;
  2328. struct zoran *zr = fh->zr;
  2329. int res = 0;
  2330. mutex_lock(&zr->resource_lock);
  2331. res = zoran_set_norm(zr, *std);
  2332. if (res)
  2333. goto sstd_unlock_and_return;
  2334. res = wait_grab_pending(zr);
  2335. sstd_unlock_and_return:
  2336. mutex_unlock(&zr->resource_lock);
  2337. return res;
  2338. }
  2339. static int zoran_enum_input(struct file *file, void *__fh,
  2340. struct v4l2_input *inp)
  2341. {
  2342. struct zoran_fh *fh = __fh;
  2343. struct zoran *zr = fh->zr;
  2344. if (inp->index < 0 || inp->index >= zr->card.inputs)
  2345. return -EINVAL;
  2346. else {
  2347. int id = inp->index;
  2348. memset(inp, 0, sizeof(*inp));
  2349. inp->index = id;
  2350. }
  2351. strncpy(inp->name, zr->card.input[inp->index].name,
  2352. sizeof(inp->name) - 1);
  2353. inp->type = V4L2_INPUT_TYPE_CAMERA;
  2354. inp->std = V4L2_STD_ALL;
  2355. /* Get status of video decoder */
  2356. mutex_lock(&zr->resource_lock);
  2357. decoder_call(zr, video, g_input_status, &inp->status);
  2358. mutex_unlock(&zr->resource_lock);
  2359. return 0;
  2360. }
  2361. static int zoran_g_input(struct file *file, void *__fh, unsigned int *input)
  2362. {
  2363. struct zoran_fh *fh = __fh;
  2364. struct zoran *zr = fh->zr;
  2365. mutex_lock(&zr->resource_lock);
  2366. *input = zr->input;
  2367. mutex_unlock(&zr->resource_lock);
  2368. return 0;
  2369. }
  2370. static int zoran_s_input(struct file *file, void *__fh, unsigned int input)
  2371. {
  2372. struct zoran_fh *fh = __fh;
  2373. struct zoran *zr = fh->zr;
  2374. int res;
  2375. mutex_lock(&zr->resource_lock);
  2376. res = zoran_set_input(zr, input);
  2377. if (res)
  2378. goto sinput_unlock_and_return;
  2379. /* Make sure the changes come into effect */
  2380. res = wait_grab_pending(zr);
  2381. sinput_unlock_and_return:
  2382. mutex_unlock(&zr->resource_lock);
  2383. return res;
  2384. }
  2385. static int zoran_enum_output(struct file *file, void *__fh,
  2386. struct v4l2_output *outp)
  2387. {
  2388. if (outp->index != 0)
  2389. return -EINVAL;
  2390. memset(outp, 0, sizeof(*outp));
  2391. outp->index = 0;
  2392. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  2393. strncpy(outp->name, "Autodetect", sizeof(outp->name)-1);
  2394. return 0;
  2395. }
  2396. static int zoran_g_output(struct file *file, void *__fh, unsigned int *output)
  2397. {
  2398. *output = 0;
  2399. return 0;
  2400. }
  2401. static int zoran_s_output(struct file *file, void *__fh, unsigned int output)
  2402. {
  2403. if (output != 0)
  2404. return -EINVAL;
  2405. return 0;
  2406. }
  2407. /* cropping (sub-frame capture) */
  2408. static int zoran_cropcap(struct file *file, void *__fh,
  2409. struct v4l2_cropcap *cropcap)
  2410. {
  2411. struct zoran_fh *fh = __fh;
  2412. struct zoran *zr = fh->zr;
  2413. int type = cropcap->type, res = 0;
  2414. memset(cropcap, 0, sizeof(*cropcap));
  2415. cropcap->type = type;
  2416. mutex_lock(&zr->resource_lock);
  2417. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2418. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2419. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2420. dprintk(1, KERN_ERR
  2421. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  2422. ZR_DEVNAME(zr));
  2423. res = -EINVAL;
  2424. goto cropcap_unlock_and_return;
  2425. }
  2426. cropcap->bounds.top = cropcap->bounds.left = 0;
  2427. cropcap->bounds.width = BUZ_MAX_WIDTH;
  2428. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  2429. cropcap->defrect.top = cropcap->defrect.left = 0;
  2430. cropcap->defrect.width = BUZ_MIN_WIDTH;
  2431. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  2432. cropcap_unlock_and_return:
  2433. mutex_unlock(&zr->resource_lock);
  2434. return res;
  2435. }
  2436. static int zoran_g_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
  2437. {
  2438. struct zoran_fh *fh = __fh;
  2439. struct zoran *zr = fh->zr;
  2440. int type = crop->type, res = 0;
  2441. memset(crop, 0, sizeof(*crop));
  2442. crop->type = type;
  2443. mutex_lock(&zr->resource_lock);
  2444. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2445. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2446. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2447. dprintk(1,
  2448. KERN_ERR
  2449. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  2450. ZR_DEVNAME(zr));
  2451. res = -EINVAL;
  2452. goto gcrop_unlock_and_return;
  2453. }
  2454. crop->c.top = fh->jpg_settings.img_y;
  2455. crop->c.left = fh->jpg_settings.img_x;
  2456. crop->c.width = fh->jpg_settings.img_width;
  2457. crop->c.height = fh->jpg_settings.img_height;
  2458. gcrop_unlock_and_return:
  2459. mutex_unlock(&zr->resource_lock);
  2460. return res;
  2461. }
  2462. static int zoran_s_crop(struct file *file, void *__fh, struct v4l2_crop *crop)
  2463. {
  2464. struct zoran_fh *fh = __fh;
  2465. struct zoran *zr = fh->zr;
  2466. int res = 0;
  2467. struct zoran_jpg_settings settings;
  2468. settings = fh->jpg_settings;
  2469. mutex_lock(&zr->resource_lock);
  2470. if (fh->buffers.allocated) {
  2471. dprintk(1, KERN_ERR
  2472. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  2473. ZR_DEVNAME(zr));
  2474. res = -EBUSY;
  2475. goto scrop_unlock_and_return;
  2476. }
  2477. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2478. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2479. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  2480. dprintk(1, KERN_ERR
  2481. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  2482. ZR_DEVNAME(zr));
  2483. res = -EINVAL;
  2484. goto scrop_unlock_and_return;
  2485. }
  2486. /* move into a form that we understand */
  2487. settings.img_x = crop->c.left;
  2488. settings.img_y = crop->c.top;
  2489. settings.img_width = crop->c.width;
  2490. settings.img_height = crop->c.height;
  2491. /* check validity */
  2492. res = zoran_check_jpg_settings(zr, &settings, 0);
  2493. if (res)
  2494. goto scrop_unlock_and_return;
  2495. /* accept */
  2496. fh->jpg_settings = settings;
  2497. scrop_unlock_and_return:
  2498. mutex_unlock(&zr->resource_lock);
  2499. return res;
  2500. }
  2501. static int zoran_g_jpegcomp(struct file *file, void *__fh,
  2502. struct v4l2_jpegcompression *params)
  2503. {
  2504. struct zoran_fh *fh = __fh;
  2505. struct zoran *zr = fh->zr;
  2506. memset(params, 0, sizeof(*params));
  2507. mutex_lock(&zr->resource_lock);
  2508. params->quality = fh->jpg_settings.jpg_comp.quality;
  2509. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  2510. memcpy(params->APP_data,
  2511. fh->jpg_settings.jpg_comp.APP_data,
  2512. fh->jpg_settings.jpg_comp.APP_len);
  2513. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2514. memcpy(params->COM_data,
  2515. fh->jpg_settings.jpg_comp.COM_data,
  2516. fh->jpg_settings.jpg_comp.COM_len);
  2517. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  2518. params->jpeg_markers =
  2519. fh->jpg_settings.jpg_comp.jpeg_markers;
  2520. mutex_unlock(&zr->resource_lock);
  2521. return 0;
  2522. }
  2523. static int zoran_s_jpegcomp(struct file *file, void *__fh,
  2524. struct v4l2_jpegcompression *params)
  2525. {
  2526. struct zoran_fh *fh = __fh;
  2527. struct zoran *zr = fh->zr;
  2528. int res = 0;
  2529. struct zoran_jpg_settings settings;
  2530. settings = fh->jpg_settings;
  2531. settings.jpg_comp = *params;
  2532. mutex_lock(&zr->resource_lock);
  2533. if (fh->buffers.active != ZORAN_FREE) {
  2534. dprintk(1, KERN_WARNING
  2535. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  2536. ZR_DEVNAME(zr));
  2537. res = -EBUSY;
  2538. goto sjpegc_unlock_and_return;
  2539. }
  2540. res = zoran_check_jpg_settings(zr, &settings, 0);
  2541. if (res)
  2542. goto sjpegc_unlock_and_return;
  2543. if (!fh->buffers.allocated)
  2544. fh->buffers.buffer_size =
  2545. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2546. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  2547. sjpegc_unlock_and_return:
  2548. mutex_unlock(&zr->resource_lock);
  2549. return res;
  2550. }
  2551. static unsigned int
  2552. zoran_poll (struct file *file,
  2553. poll_table *wait)
  2554. {
  2555. struct zoran_fh *fh = file->private_data;
  2556. struct zoran *zr = fh->zr;
  2557. int res = 0, frame;
  2558. unsigned long flags;
  2559. /* we should check whether buffers are ready to be synced on
  2560. * (w/o waits - O_NONBLOCK) here
  2561. * if ready for read (sync), return POLLIN|POLLRDNORM,
  2562. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  2563. * if error, return POLLERR,
  2564. * if no buffers queued or so, return POLLNVAL
  2565. */
  2566. mutex_lock(&zr->resource_lock);
  2567. switch (fh->map_mode) {
  2568. case ZORAN_MAP_MODE_RAW:
  2569. poll_wait(file, &zr->v4l_capq, wait);
  2570. frame = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2571. spin_lock_irqsave(&zr->spinlock, flags);
  2572. dprintk(3,
  2573. KERN_DEBUG
  2574. "%s: %s() raw - active=%c, sync_tail=%lu/%c, pend_tail=%lu, pend_head=%lu\n",
  2575. ZR_DEVNAME(zr), __func__,
  2576. "FAL"[fh->buffers.active], zr->v4l_sync_tail,
  2577. "UPMD"[zr->v4l_buffers.buffer[frame].state],
  2578. zr->v4l_pend_tail, zr->v4l_pend_head);
  2579. /* Process is the one capturing? */
  2580. if (fh->buffers.active != ZORAN_FREE &&
  2581. /* Buffer ready to DQBUF? */
  2582. zr->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  2583. res = POLLIN | POLLRDNORM;
  2584. spin_unlock_irqrestore(&zr->spinlock, flags);
  2585. break;
  2586. case ZORAN_MAP_MODE_JPG_REC:
  2587. case ZORAN_MAP_MODE_JPG_PLAY:
  2588. poll_wait(file, &zr->jpg_capq, wait);
  2589. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  2590. spin_lock_irqsave(&zr->spinlock, flags);
  2591. dprintk(3,
  2592. KERN_DEBUG
  2593. "%s: %s() jpg - active=%c, que_tail=%lu/%c, que_head=%lu, dma=%lu/%lu\n",
  2594. ZR_DEVNAME(zr), __func__,
  2595. "FAL"[fh->buffers.active], zr->jpg_que_tail,
  2596. "UPMD"[zr->jpg_buffers.buffer[frame].state],
  2597. zr->jpg_que_head, zr->jpg_dma_tail, zr->jpg_dma_head);
  2598. if (fh->buffers.active != ZORAN_FREE &&
  2599. zr->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  2600. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  2601. res = POLLIN | POLLRDNORM;
  2602. else
  2603. res = POLLOUT | POLLWRNORM;
  2604. }
  2605. spin_unlock_irqrestore(&zr->spinlock, flags);
  2606. break;
  2607. default:
  2608. dprintk(1,
  2609. KERN_ERR
  2610. "%s: %s - internal error, unknown map_mode=%d\n",
  2611. ZR_DEVNAME(zr), __func__, fh->map_mode);
  2612. res = POLLNVAL;
  2613. }
  2614. mutex_unlock(&zr->resource_lock);
  2615. return res;
  2616. }
  2617. /*
  2618. * This maps the buffers to user space.
  2619. *
  2620. * Depending on the state of fh->map_mode
  2621. * the V4L or the MJPEG buffers are mapped
  2622. * per buffer or all together
  2623. *
  2624. * Note that we need to connect to some
  2625. * unmap signal event to unmap the de-allocate
  2626. * the buffer accordingly (zoran_vm_close())
  2627. */
  2628. static void
  2629. zoran_vm_open (struct vm_area_struct *vma)
  2630. {
  2631. struct zoran_mapping *map = vma->vm_private_data;
  2632. map->count++;
  2633. }
  2634. static void
  2635. zoran_vm_close (struct vm_area_struct *vma)
  2636. {
  2637. struct zoran_mapping *map = vma->vm_private_data;
  2638. struct zoran_fh *fh = map->file->private_data;
  2639. struct zoran *zr = fh->zr;
  2640. int i;
  2641. if (--map->count > 0)
  2642. return;
  2643. dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
  2644. __func__, mode_name(fh->map_mode));
  2645. for (i = 0; i < fh->buffers.num_buffers; i++) {
  2646. if (fh->buffers.buffer[i].map == map)
  2647. fh->buffers.buffer[i].map = NULL;
  2648. }
  2649. kfree(map);
  2650. /* Any buffers still mapped? */
  2651. for (i = 0; i < fh->buffers.num_buffers; i++)
  2652. if (fh->buffers.buffer[i].map)
  2653. return;
  2654. dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
  2655. __func__, mode_name(fh->map_mode));
  2656. mutex_lock(&zr->resource_lock);
  2657. if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2658. if (fh->buffers.active != ZORAN_FREE) {
  2659. unsigned long flags;
  2660. spin_lock_irqsave(&zr->spinlock, flags);
  2661. zr36057_set_memgrab(zr, 0);
  2662. zr->v4l_buffers.allocated = 0;
  2663. zr->v4l_buffers.active = fh->buffers.active = ZORAN_FREE;
  2664. spin_unlock_irqrestore(&zr->spinlock, flags);
  2665. }
  2666. v4l_fbuffer_free(fh);
  2667. } else {
  2668. if (fh->buffers.active != ZORAN_FREE) {
  2669. jpg_qbuf(fh, -1, zr->codec_mode);
  2670. zr->jpg_buffers.allocated = 0;
  2671. zr->jpg_buffers.active = fh->buffers.active = ZORAN_FREE;
  2672. }
  2673. jpg_fbuffer_free(fh);
  2674. }
  2675. mutex_unlock(&zr->resource_lock);
  2676. }
  2677. static struct vm_operations_struct zoran_vm_ops = {
  2678. .open = zoran_vm_open,
  2679. .close = zoran_vm_close,
  2680. };
  2681. static int
  2682. zoran_mmap (struct file *file,
  2683. struct vm_area_struct *vma)
  2684. {
  2685. struct zoran_fh *fh = file->private_data;
  2686. struct zoran *zr = fh->zr;
  2687. unsigned long size = (vma->vm_end - vma->vm_start);
  2688. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  2689. int i, j;
  2690. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  2691. int first, last;
  2692. struct zoran_mapping *map;
  2693. int res = 0;
  2694. dprintk(3,
  2695. KERN_INFO "%s: %s(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  2696. ZR_DEVNAME(zr), __func__,
  2697. mode_name(fh->map_mode), vma->vm_start, vma->vm_end, size);
  2698. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  2699. !(vma->vm_flags & VM_WRITE)) {
  2700. dprintk(1,
  2701. KERN_ERR
  2702. "%s: %s - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  2703. ZR_DEVNAME(zr), __func__);
  2704. return -EINVAL;
  2705. }
  2706. mutex_lock(&zr->resource_lock);
  2707. if (!fh->buffers.allocated) {
  2708. dprintk(1,
  2709. KERN_ERR
  2710. "%s: %s(%s) - buffers not yet allocated\n",
  2711. ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode));
  2712. res = -ENOMEM;
  2713. goto mmap_unlock_and_return;
  2714. }
  2715. first = offset / fh->buffers.buffer_size;
  2716. last = first - 1 + size / fh->buffers.buffer_size;
  2717. if (offset % fh->buffers.buffer_size != 0 ||
  2718. size % fh->buffers.buffer_size != 0 || first < 0 ||
  2719. last < 0 || first >= fh->buffers.num_buffers ||
  2720. last >= fh->buffers.buffer_size) {
  2721. dprintk(1,
  2722. KERN_ERR
  2723. "%s: %s(%s) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  2724. ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), offset, size,
  2725. fh->buffers.buffer_size,
  2726. fh->buffers.num_buffers);
  2727. res = -EINVAL;
  2728. goto mmap_unlock_and_return;
  2729. }
  2730. /* Check if any buffers are already mapped */
  2731. for (i = first; i <= last; i++) {
  2732. if (fh->buffers.buffer[i].map) {
  2733. dprintk(1,
  2734. KERN_ERR
  2735. "%s: %s(%s) - buffer %d already mapped\n",
  2736. ZR_DEVNAME(zr), __func__, mode_name(fh->map_mode), i);
  2737. res = -EBUSY;
  2738. goto mmap_unlock_and_return;
  2739. }
  2740. }
  2741. /* map these buffers */
  2742. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  2743. if (!map) {
  2744. res = -ENOMEM;
  2745. goto mmap_unlock_and_return;
  2746. }
  2747. map->file = file;
  2748. map->count = 1;
  2749. vma->vm_ops = &zoran_vm_ops;
  2750. vma->vm_flags |= VM_DONTEXPAND;
  2751. vma->vm_private_data = map;
  2752. if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2753. for (i = first; i <= last; i++) {
  2754. todo = size;
  2755. if (todo > fh->buffers.buffer_size)
  2756. todo = fh->buffers.buffer_size;
  2757. page = fh->buffers.buffer[i].v4l.fbuffer_phys;
  2758. if (remap_pfn_range(vma, start, page >> PAGE_SHIFT,
  2759. todo, PAGE_SHARED)) {
  2760. dprintk(1,
  2761. KERN_ERR
  2762. "%s: %s(V4L) - remap_pfn_range failed\n",
  2763. ZR_DEVNAME(zr), __func__);
  2764. res = -EAGAIN;
  2765. goto mmap_unlock_and_return;
  2766. }
  2767. size -= todo;
  2768. start += todo;
  2769. fh->buffers.buffer[i].map = map;
  2770. if (size == 0)
  2771. break;
  2772. }
  2773. } else {
  2774. for (i = first; i <= last; i++) {
  2775. for (j = 0;
  2776. j < fh->buffers.buffer_size / PAGE_SIZE;
  2777. j++) {
  2778. fraglen =
  2779. (le32_to_cpu(fh->buffers.buffer[i].jpg.
  2780. frag_tab[2 * j + 1]) & ~1) << 1;
  2781. todo = size;
  2782. if (todo > fraglen)
  2783. todo = fraglen;
  2784. pos =
  2785. le32_to_cpu(fh->buffers.
  2786. buffer[i].jpg.frag_tab[2 * j]);
  2787. /* should just be pos on i386 */
  2788. page = virt_to_phys(bus_to_virt(pos))
  2789. >> PAGE_SHIFT;
  2790. if (remap_pfn_range(vma, start, page,
  2791. todo, PAGE_SHARED)) {
  2792. dprintk(1,
  2793. KERN_ERR
  2794. "%s: %s(V4L) - remap_pfn_range failed\n",
  2795. ZR_DEVNAME(zr), __func__);
  2796. res = -EAGAIN;
  2797. goto mmap_unlock_and_return;
  2798. }
  2799. size -= todo;
  2800. start += todo;
  2801. if (size == 0)
  2802. break;
  2803. if (le32_to_cpu(fh->buffers.buffer[i].jpg.
  2804. frag_tab[2 * j + 1]) & 1)
  2805. break; /* was last fragment */
  2806. }
  2807. fh->buffers.buffer[i].map = map;
  2808. if (size == 0)
  2809. break;
  2810. }
  2811. }
  2812. mmap_unlock_and_return:
  2813. mutex_unlock(&zr->resource_lock);
  2814. return 0;
  2815. }
  2816. static const struct v4l2_ioctl_ops zoran_ioctl_ops = {
  2817. .vidioc_querycap = zoran_querycap,
  2818. .vidioc_cropcap = zoran_cropcap,
  2819. .vidioc_s_crop = zoran_s_crop,
  2820. .vidioc_g_crop = zoran_g_crop,
  2821. .vidioc_enum_input = zoran_enum_input,
  2822. .vidioc_g_input = zoran_g_input,
  2823. .vidioc_s_input = zoran_s_input,
  2824. .vidioc_enum_output = zoran_enum_output,
  2825. .vidioc_g_output = zoran_g_output,
  2826. .vidioc_s_output = zoran_s_output,
  2827. .vidioc_g_fbuf = zoran_g_fbuf,
  2828. .vidioc_s_fbuf = zoran_s_fbuf,
  2829. .vidioc_g_std = zoran_g_std,
  2830. .vidioc_s_std = zoran_s_std,
  2831. .vidioc_g_jpegcomp = zoran_g_jpegcomp,
  2832. .vidioc_s_jpegcomp = zoran_s_jpegcomp,
  2833. .vidioc_overlay = zoran_overlay,
  2834. .vidioc_reqbufs = zoran_reqbufs,
  2835. .vidioc_querybuf = zoran_querybuf,
  2836. .vidioc_qbuf = zoran_qbuf,
  2837. .vidioc_dqbuf = zoran_dqbuf,
  2838. .vidioc_streamon = zoran_streamon,
  2839. .vidioc_streamoff = zoran_streamoff,
  2840. .vidioc_enum_fmt_vid_cap = zoran_enum_fmt_vid_cap,
  2841. .vidioc_enum_fmt_vid_out = zoran_enum_fmt_vid_out,
  2842. .vidioc_enum_fmt_vid_overlay = zoran_enum_fmt_vid_overlay,
  2843. .vidioc_g_fmt_vid_cap = zoran_g_fmt_vid_cap,
  2844. .vidioc_g_fmt_vid_out = zoran_g_fmt_vid_out,
  2845. .vidioc_g_fmt_vid_overlay = zoran_g_fmt_vid_overlay,
  2846. .vidioc_s_fmt_vid_cap = zoran_s_fmt_vid_cap,
  2847. .vidioc_s_fmt_vid_out = zoran_s_fmt_vid_out,
  2848. .vidioc_s_fmt_vid_overlay = zoran_s_fmt_vid_overlay,
  2849. .vidioc_try_fmt_vid_cap = zoran_try_fmt_vid_cap,
  2850. .vidioc_try_fmt_vid_out = zoran_try_fmt_vid_out,
  2851. .vidioc_try_fmt_vid_overlay = zoran_try_fmt_vid_overlay,
  2852. .vidioc_queryctrl = zoran_queryctrl,
  2853. .vidioc_s_ctrl = zoran_s_ctrl,
  2854. .vidioc_g_ctrl = zoran_g_ctrl,
  2855. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  2856. .vidioc_default = zoran_default,
  2857. .vidiocgmbuf = zoran_vidiocgmbuf,
  2858. #endif
  2859. };
  2860. static const struct v4l2_file_operations zoran_fops = {
  2861. .owner = THIS_MODULE,
  2862. .open = zoran_open,
  2863. .release = zoran_close,
  2864. .ioctl = video_ioctl2,
  2865. .read = zoran_read,
  2866. .write = zoran_write,
  2867. .mmap = zoran_mmap,
  2868. .poll = zoran_poll,
  2869. };
  2870. struct video_device zoran_template __devinitdata = {
  2871. .name = ZORAN_NAME,
  2872. .fops = &zoran_fops,
  2873. .ioctl_ops = &zoran_ioctl_ops,
  2874. .release = &zoran_vdev_release,
  2875. .tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
  2876. .minor = -1
  2877. };