zoran_driver.c 74 KB

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