zoran_driver.c 73 KB

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