zoran_driver.c 87 KB

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