zoran_driver.c 79 KB

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