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