zoran_driver.c 79 KB

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