Greg Kroah-Hartman | 866b869 | 2008-02-15 16:53:09 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2005-2006 Micronas USA Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License (Version 2) as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software Foundation, |
| 15 | * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/version.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/spinlock.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/unistd.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include <linux/pagemap.h> |
| 29 | #include <linux/videodev.h> |
| 30 | #include <linux/video_decoder.h> |
| 31 | #include <media/v4l2-common.h> |
| 32 | #include <media/v4l2-ioctl.h> |
| 33 | #include <linux/i2c.h> |
| 34 | #include <linux/semaphore.h> |
| 35 | #include <linux/uaccess.h> |
| 36 | #include <asm/system.h> |
| 37 | |
| 38 | #include "go7007.h" |
| 39 | #include "go7007-priv.h" |
| 40 | #include "wis-i2c.h" |
| 41 | |
| 42 | static void deactivate_buffer(struct go7007_buffer *gobuf) |
| 43 | { |
| 44 | int i; |
| 45 | |
| 46 | if (gobuf->state != BUF_STATE_IDLE) { |
| 47 | list_del(&gobuf->stream); |
| 48 | gobuf->state = BUF_STATE_IDLE; |
| 49 | } |
| 50 | if (gobuf->page_count > 0) { |
| 51 | for (i = 0; i < gobuf->page_count; ++i) |
| 52 | page_cache_release(gobuf->pages[i]); |
| 53 | gobuf->page_count = 0; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static void abort_queued(struct go7007 *go) |
| 58 | { |
| 59 | struct go7007_buffer *gobuf, *next; |
| 60 | |
| 61 | list_for_each_entry_safe(gobuf, next, &go->stream, stream) { |
| 62 | deactivate_buffer(gobuf); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | static int go7007_streamoff(struct go7007 *go) |
| 67 | { |
| 68 | int retval = -EINVAL; |
| 69 | unsigned long flags; |
| 70 | |
| 71 | down(&go->hw_lock); |
| 72 | if (go->streaming) { |
| 73 | go->streaming = 0; |
| 74 | go7007_stream_stop(go); |
| 75 | spin_lock_irqsave(&go->spinlock, flags); |
| 76 | abort_queued(go); |
| 77 | spin_unlock_irqrestore(&go->spinlock, flags); |
| 78 | go7007_reset_encoder(go); |
| 79 | retval = 0; |
| 80 | } |
| 81 | up(&go->hw_lock); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int go7007_open(struct inode *inode, struct file *file) |
| 86 | { |
| 87 | struct go7007 *go = video_get_drvdata(video_devdata(file)); |
| 88 | struct go7007_file *gofh; |
| 89 | |
| 90 | if (go->status != STATUS_ONLINE) |
| 91 | return -EBUSY; |
| 92 | gofh = kmalloc(sizeof(struct go7007_file), GFP_KERNEL); |
| 93 | if (gofh == NULL) |
| 94 | return -ENOMEM; |
| 95 | ++go->ref_count; |
| 96 | gofh->go = go; |
| 97 | init_MUTEX(&gofh->lock); |
| 98 | gofh->buf_count = 0; |
| 99 | file->private_data = gofh; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int go7007_release(struct inode *inode, struct file *file) |
| 104 | { |
| 105 | struct go7007_file *gofh = file->private_data; |
| 106 | struct go7007 *go = gofh->go; |
| 107 | |
| 108 | if (gofh->buf_count > 0) { |
| 109 | go7007_streamoff(go); |
| 110 | go->in_use = 0; |
| 111 | kfree(gofh->bufs); |
| 112 | gofh->buf_count = 0; |
| 113 | } |
| 114 | kfree(gofh); |
| 115 | if (--go->ref_count == 0) |
| 116 | kfree(go); |
| 117 | file->private_data = NULL; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static u32 get_frame_type_flag(struct go7007_buffer *gobuf, int format) |
| 122 | { |
| 123 | u8 *f = page_address(gobuf->pages[0]); |
| 124 | |
| 125 | switch (format) { |
| 126 | case GO7007_FORMAT_MJPEG: |
| 127 | return V4L2_BUF_FLAG_KEYFRAME; |
| 128 | case GO7007_FORMAT_MPEG4: |
| 129 | switch ((f[gobuf->frame_offset + 4] >> 6) & 0x3) { |
| 130 | case 0: |
| 131 | return V4L2_BUF_FLAG_KEYFRAME; |
| 132 | case 1: |
| 133 | return V4L2_BUF_FLAG_PFRAME; |
| 134 | case 2: |
| 135 | return V4L2_BUF_FLAG_BFRAME; |
| 136 | default: |
| 137 | return 0; |
| 138 | } |
| 139 | case GO7007_FORMAT_MPEG1: |
| 140 | case GO7007_FORMAT_MPEG2: |
| 141 | switch ((f[gobuf->frame_offset + 5] >> 3) & 0x7) { |
| 142 | case 1: |
| 143 | return V4L2_BUF_FLAG_KEYFRAME; |
| 144 | case 2: |
| 145 | return V4L2_BUF_FLAG_PFRAME; |
| 146 | case 3: |
| 147 | return V4L2_BUF_FLAG_BFRAME; |
| 148 | default: |
| 149 | return 0; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try) |
| 157 | { |
| 158 | int sensor_height = 0, sensor_width = 0; |
| 159 | int width, height, i; |
| 160 | |
| 161 | if (fmt != NULL && fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG && |
| 162 | fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG && |
| 163 | fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG4) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | switch (go->standard) { |
| 167 | case GO7007_STD_NTSC: |
| 168 | sensor_width = 720; |
| 169 | sensor_height = 480; |
| 170 | break; |
| 171 | case GO7007_STD_PAL: |
| 172 | sensor_width = 720; |
| 173 | sensor_height = 576; |
| 174 | break; |
| 175 | case GO7007_STD_OTHER: |
| 176 | sensor_width = go->board_info->sensor_width; |
| 177 | sensor_height = go->board_info->sensor_height; |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | if (fmt == NULL) { |
| 182 | width = sensor_width; |
| 183 | height = sensor_height; |
| 184 | } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) { |
| 185 | if (fmt->fmt.pix.width > sensor_width) |
| 186 | width = sensor_width; |
| 187 | else if (fmt->fmt.pix.width < 144) |
| 188 | width = 144; |
| 189 | else |
| 190 | width = fmt->fmt.pix.width & ~0x0f; |
| 191 | |
| 192 | if (fmt->fmt.pix.height > sensor_height) |
| 193 | height = sensor_height; |
| 194 | else if (fmt->fmt.pix.height < 96) |
| 195 | height = 96; |
| 196 | else |
| 197 | height = fmt->fmt.pix.height & ~0x0f; |
| 198 | } else { |
| 199 | int requested_size = fmt->fmt.pix.width * fmt->fmt.pix.height; |
| 200 | int sensor_size = sensor_width * sensor_height; |
| 201 | |
| 202 | if (64 * requested_size < 9 * sensor_size) { |
| 203 | width = sensor_width / 4; |
| 204 | height = sensor_height / 4; |
| 205 | } else if (64 * requested_size < 36 * sensor_size) { |
| 206 | width = sensor_width / 2; |
| 207 | height = sensor_height / 2; |
| 208 | } else { |
| 209 | width = sensor_width; |
| 210 | height = sensor_height; |
| 211 | } |
| 212 | width &= ~0xf; |
| 213 | height &= ~0xf; |
| 214 | } |
| 215 | |
| 216 | if (fmt != NULL) { |
| 217 | u32 pixelformat = fmt->fmt.pix.pixelformat; |
| 218 | |
| 219 | memset(fmt, 0, sizeof(*fmt)); |
| 220 | fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 221 | fmt->fmt.pix.width = width; |
| 222 | fmt->fmt.pix.height = height; |
| 223 | fmt->fmt.pix.pixelformat = pixelformat; |
| 224 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 225 | fmt->fmt.pix.bytesperline = 0; |
| 226 | fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE; |
| 227 | fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */ |
| 228 | } |
| 229 | |
| 230 | if (try) |
| 231 | return 0; |
| 232 | |
| 233 | go->width = width; |
| 234 | go->height = height; |
| 235 | go->encoder_h_offset = go->board_info->sensor_h_offset; |
| 236 | go->encoder_v_offset = go->board_info->sensor_v_offset; |
| 237 | for (i = 0; i < 4; ++i) |
| 238 | go->modet[i].enable = 0; |
| 239 | for (i = 0; i < 1624; ++i) |
| 240 | go->modet_map[i] = 0; |
| 241 | |
| 242 | if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) { |
| 243 | struct video_decoder_resolution res; |
| 244 | |
| 245 | res.width = width; |
| 246 | if (height > sensor_height / 2) { |
| 247 | res.height = height / 2; |
| 248 | go->encoder_v_halve = 0; |
| 249 | } else { |
| 250 | res.height = height; |
| 251 | go->encoder_v_halve = 1; |
| 252 | } |
| 253 | if (go->i2c_adapter_online) |
| 254 | i2c_clients_command(&go->i2c_adapter, |
| 255 | DECODER_SET_RESOLUTION, &res); |
| 256 | } else { |
| 257 | if (width <= sensor_width / 4) { |
| 258 | go->encoder_h_halve = 1; |
| 259 | go->encoder_v_halve = 1; |
| 260 | go->encoder_subsample = 1; |
| 261 | } else if (width <= sensor_width / 2) { |
| 262 | go->encoder_h_halve = 1; |
| 263 | go->encoder_v_halve = 1; |
| 264 | go->encoder_subsample = 0; |
| 265 | } else { |
| 266 | go->encoder_h_halve = 0; |
| 267 | go->encoder_v_halve = 0; |
| 268 | go->encoder_subsample = 0; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (fmt == NULL) |
| 273 | return 0; |
| 274 | |
| 275 | switch (fmt->fmt.pix.pixelformat) { |
| 276 | case V4L2_PIX_FMT_MPEG: |
| 277 | if (go->format == GO7007_FORMAT_MPEG1 || |
| 278 | go->format == GO7007_FORMAT_MPEG2 || |
| 279 | go->format == GO7007_FORMAT_MPEG4) |
| 280 | break; |
| 281 | go->format = GO7007_FORMAT_MPEG1; |
| 282 | go->pali = 0; |
| 283 | go->aspect_ratio = GO7007_RATIO_1_1; |
| 284 | go->gop_size = go->sensor_framerate / 1000; |
| 285 | go->ipb = 0; |
| 286 | go->closed_gop = 1; |
| 287 | go->repeat_seqhead = 1; |
| 288 | go->seq_header_enable = 1; |
| 289 | go->gop_header_enable = 1; |
| 290 | go->dvd_mode = 0; |
| 291 | break; |
| 292 | /* Backwards compatibility only! */ |
| 293 | case V4L2_PIX_FMT_MPEG4: |
| 294 | if (go->format == GO7007_FORMAT_MPEG4) |
| 295 | break; |
| 296 | go->format = GO7007_FORMAT_MPEG4; |
| 297 | go->pali = 0xf5; |
| 298 | go->aspect_ratio = GO7007_RATIO_1_1; |
| 299 | go->gop_size = go->sensor_framerate / 1000; |
| 300 | go->ipb = 0; |
| 301 | go->closed_gop = 1; |
| 302 | go->repeat_seqhead = 1; |
| 303 | go->seq_header_enable = 1; |
| 304 | go->gop_header_enable = 1; |
| 305 | go->dvd_mode = 0; |
| 306 | break; |
| 307 | case V4L2_PIX_FMT_MJPEG: |
| 308 | go->format = GO7007_FORMAT_MJPEG; |
| 309 | go->pali = 0; |
| 310 | go->aspect_ratio = GO7007_RATIO_1_1; |
| 311 | go->gop_size = 0; |
| 312 | go->ipb = 0; |
| 313 | go->closed_gop = 0; |
| 314 | go->repeat_seqhead = 0; |
| 315 | go->seq_header_enable = 0; |
| 316 | go->gop_header_enable = 0; |
| 317 | go->dvd_mode = 0; |
| 318 | break; |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static int clip_to_modet_map(struct go7007 *go, int region, |
| 324 | struct v4l2_clip *clip_list) |
| 325 | { |
| 326 | struct v4l2_clip clip, *clip_ptr; |
| 327 | int x, y, mbnum; |
| 328 | |
| 329 | /* Check if coordinates are OK and if any macroblocks are already |
| 330 | * used by other regions (besides 0) */ |
| 331 | clip_ptr = clip_list; |
| 332 | while (clip_ptr) { |
| 333 | if (copy_from_user(&clip, clip_ptr, sizeof(clip))) |
| 334 | return -EFAULT; |
| 335 | if (clip.c.left < 0 || (clip.c.left & 0xF) || |
| 336 | clip.c.width <= 0 || (clip.c.width & 0xF)) |
| 337 | return -EINVAL; |
| 338 | if (clip.c.left + clip.c.width > go->width) |
| 339 | return -EINVAL; |
| 340 | if (clip.c.top < 0 || (clip.c.top & 0xF) || |
| 341 | clip.c.height <= 0 || (clip.c.height & 0xF)) |
| 342 | return -EINVAL; |
| 343 | if (clip.c.top + clip.c.height > go->height) |
| 344 | return -EINVAL; |
| 345 | for (y = 0; y < clip.c.height; y += 16) |
| 346 | for (x = 0; x < clip.c.width; x += 16) { |
| 347 | mbnum = (go->width >> 4) * |
| 348 | ((clip.c.top + y) >> 4) + |
| 349 | ((clip.c.left + x) >> 4); |
| 350 | if (go->modet_map[mbnum] != 0 && |
| 351 | go->modet_map[mbnum] != region) |
| 352 | return -EBUSY; |
| 353 | } |
| 354 | clip_ptr = clip.next; |
| 355 | } |
| 356 | |
| 357 | /* Clear old region macroblocks */ |
| 358 | for (mbnum = 0; mbnum < 1624; ++mbnum) |
| 359 | if (go->modet_map[mbnum] == region) |
| 360 | go->modet_map[mbnum] = 0; |
| 361 | |
| 362 | /* Claim macroblocks in this list */ |
| 363 | clip_ptr = clip_list; |
| 364 | while (clip_ptr) { |
| 365 | if (copy_from_user(&clip, clip_ptr, sizeof(clip))) |
| 366 | return -EFAULT; |
| 367 | for (y = 0; y < clip.c.height; y += 16) |
| 368 | for (x = 0; x < clip.c.width; x += 16) { |
| 369 | mbnum = (go->width >> 4) * |
| 370 | ((clip.c.top + y) >> 4) + |
| 371 | ((clip.c.left + x) >> 4); |
| 372 | go->modet_map[mbnum] = region; |
| 373 | } |
| 374 | clip_ptr = clip.next; |
| 375 | } |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int go7007_do_ioctl(struct inode *inode, struct file *file, |
| 380 | unsigned int cmd, void *arg) |
| 381 | { |
| 382 | struct go7007_file *gofh = file->private_data; |
| 383 | struct go7007 *go = gofh->go; |
| 384 | unsigned long flags; |
| 385 | int retval = 0; |
| 386 | |
| 387 | switch (cmd) { |
| 388 | case VIDIOC_QUERYCAP: |
| 389 | { |
| 390 | struct v4l2_capability *cap = arg; |
| 391 | |
| 392 | memset(cap, 0, sizeof(*cap)); |
| 393 | strcpy(cap->driver, "go7007"); |
| 394 | strncpy(cap->card, go->name, sizeof(cap->card)); |
| 395 | cap->version = KERNEL_VERSION(0, 9, 8); |
| 396 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | |
| 397 | V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */ |
| 398 | if (go->board_info->flags & GO7007_BOARD_HAS_TUNER) |
| 399 | cap->capabilities |= V4L2_CAP_TUNER; |
| 400 | return 0; |
| 401 | } |
| 402 | case VIDIOC_ENUM_FMT: |
| 403 | { |
| 404 | struct v4l2_fmtdesc *fmt = arg; |
| 405 | unsigned int index; |
| 406 | char *desc; |
| 407 | u32 pixelformat; |
| 408 | |
| 409 | if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 410 | return -EINVAL; |
| 411 | switch (fmt->index) { |
| 412 | case 0: |
| 413 | pixelformat = V4L2_PIX_FMT_MJPEG; |
| 414 | desc = "Motion-JPEG"; |
| 415 | break; |
| 416 | case 1: |
| 417 | pixelformat = V4L2_PIX_FMT_MPEG; |
| 418 | desc = "MPEG1/MPEG2/MPEG4"; |
| 419 | break; |
| 420 | default: |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | index = fmt->index; |
| 424 | memset(fmt, 0, sizeof(*fmt)); |
| 425 | fmt->index = index; |
| 426 | fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 427 | fmt->flags = V4L2_FMT_FLAG_COMPRESSED; |
| 428 | strncpy(fmt->description, desc, sizeof(fmt->description)); |
| 429 | fmt->pixelformat = pixelformat; |
| 430 | |
| 431 | return 0; |
| 432 | } |
| 433 | case VIDIOC_TRY_FMT: |
| 434 | { |
| 435 | struct v4l2_format *fmt = arg; |
| 436 | |
| 437 | if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 438 | return -EINVAL; |
| 439 | return set_capture_size(go, fmt, 1); |
| 440 | } |
| 441 | case VIDIOC_G_FMT: |
| 442 | { |
| 443 | struct v4l2_format *fmt = arg; |
| 444 | |
| 445 | if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 446 | return -EINVAL; |
| 447 | memset(fmt, 0, sizeof(*fmt)); |
| 448 | fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 449 | fmt->fmt.pix.width = go->width; |
| 450 | fmt->fmt.pix.height = go->height; |
| 451 | fmt->fmt.pix.pixelformat = go->format == GO7007_FORMAT_MJPEG ? |
| 452 | V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG; |
| 453 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 454 | fmt->fmt.pix.bytesperline = 0; |
| 455 | fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE; |
| 456 | fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */ |
| 457 | return 0; |
| 458 | } |
| 459 | case VIDIOC_S_FMT: |
| 460 | { |
| 461 | struct v4l2_format *fmt = arg; |
| 462 | |
| 463 | if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 464 | return -EINVAL; |
| 465 | if (go->streaming) |
| 466 | return -EBUSY; |
| 467 | return set_capture_size(go, fmt, 0); |
| 468 | } |
| 469 | case VIDIOC_G_FBUF: |
| 470 | case VIDIOC_S_FBUF: |
| 471 | return -EINVAL; |
| 472 | case VIDIOC_REQBUFS: |
| 473 | { |
| 474 | struct v4l2_requestbuffers *req = arg; |
| 475 | unsigned int count, i; |
| 476 | |
| 477 | if (go->streaming) |
| 478 | return -EBUSY; |
| 479 | if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 480 | req->memory != V4L2_MEMORY_MMAP) |
| 481 | return -EINVAL; |
| 482 | |
| 483 | down(&gofh->lock); |
| 484 | retval = -EBUSY; |
| 485 | for (i = 0; i < gofh->buf_count; ++i) |
| 486 | if (gofh->bufs[i].mapped > 0) |
| 487 | goto unlock_and_return; |
| 488 | down(&go->hw_lock); |
| 489 | if (go->in_use > 0 && gofh->buf_count == 0) { |
| 490 | up(&go->hw_lock); |
| 491 | goto unlock_and_return; |
| 492 | } |
| 493 | if (gofh->buf_count > 0) |
| 494 | kfree(gofh->bufs); |
| 495 | retval = -ENOMEM; |
| 496 | count = req->count; |
| 497 | if (count > 0) { |
| 498 | if (count < 2) |
| 499 | count = 2; |
| 500 | if (count > 32) |
| 501 | count = 32; |
| 502 | gofh->bufs = kmalloc(count * |
| 503 | sizeof(struct go7007_buffer), |
| 504 | GFP_KERNEL); |
| 505 | if (gofh->bufs == NULL) { |
| 506 | up(&go->hw_lock); |
| 507 | goto unlock_and_return; |
| 508 | } |
| 509 | memset(gofh->bufs, 0, |
| 510 | count * sizeof(struct go7007_buffer)); |
| 511 | for (i = 0; i < count; ++i) { |
| 512 | gofh->bufs[i].go = go; |
| 513 | gofh->bufs[i].index = i; |
| 514 | gofh->bufs[i].state = BUF_STATE_IDLE; |
| 515 | gofh->bufs[i].mapped = 0; |
| 516 | } |
| 517 | go->in_use = 1; |
| 518 | } else { |
| 519 | go->in_use = 0; |
| 520 | } |
| 521 | gofh->buf_count = count; |
| 522 | up(&go->hw_lock); |
| 523 | up(&gofh->lock); |
| 524 | memset(req, 0, sizeof(*req)); |
| 525 | req->count = count; |
| 526 | req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 527 | req->memory = V4L2_MEMORY_MMAP; |
| 528 | return 0; |
| 529 | } |
| 530 | case VIDIOC_QUERYBUF: |
| 531 | { |
| 532 | struct v4l2_buffer *buf = arg; |
| 533 | unsigned int index; |
| 534 | |
| 535 | retval = -EINVAL; |
| 536 | if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 537 | return -EINVAL; |
| 538 | index = buf->index; |
| 539 | down(&gofh->lock); |
| 540 | if (index >= gofh->buf_count) |
| 541 | goto unlock_and_return; |
| 542 | memset(buf, 0, sizeof(*buf)); |
| 543 | buf->index = index; |
| 544 | buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 545 | switch (gofh->bufs[index].state) { |
| 546 | case BUF_STATE_QUEUED: |
| 547 | buf->flags = V4L2_BUF_FLAG_QUEUED; |
| 548 | break; |
| 549 | case BUF_STATE_DONE: |
| 550 | buf->flags = V4L2_BUF_FLAG_DONE; |
| 551 | break; |
| 552 | default: |
| 553 | buf->flags = 0; |
| 554 | } |
| 555 | if (gofh->bufs[index].mapped) |
| 556 | buf->flags |= V4L2_BUF_FLAG_MAPPED; |
| 557 | buf->memory = V4L2_MEMORY_MMAP; |
| 558 | buf->m.offset = index * GO7007_BUF_SIZE; |
| 559 | buf->length = GO7007_BUF_SIZE; |
| 560 | up(&gofh->lock); |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | case VIDIOC_QBUF: |
| 565 | { |
| 566 | struct v4l2_buffer *buf = arg; |
| 567 | struct go7007_buffer *gobuf; |
| 568 | int ret; |
| 569 | |
| 570 | retval = -EINVAL; |
| 571 | if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || |
| 572 | buf->memory != V4L2_MEMORY_MMAP) |
| 573 | return -EINVAL; |
| 574 | down(&gofh->lock); |
| 575 | if (buf->index < 0 || buf->index >= gofh->buf_count) |
| 576 | goto unlock_and_return; |
| 577 | gobuf = &gofh->bufs[buf->index]; |
| 578 | if (gobuf->mapped == 0) |
| 579 | goto unlock_and_return; |
| 580 | retval = -EBUSY; |
| 581 | if (gobuf->state != BUF_STATE_IDLE) |
| 582 | goto unlock_and_return; |
| 583 | /* offset will be 0 until we really support USERPTR streaming */ |
| 584 | gobuf->offset = gobuf->user_addr & ~PAGE_MASK; |
| 585 | gobuf->bytesused = 0; |
| 586 | gobuf->frame_offset = 0; |
| 587 | gobuf->modet_active = 0; |
| 588 | if (gobuf->offset > 0) |
| 589 | gobuf->page_count = GO7007_BUF_PAGES + 1; |
| 590 | else |
| 591 | gobuf->page_count = GO7007_BUF_PAGES; |
| 592 | retval = -ENOMEM; |
| 593 | down_read(¤t->mm->mmap_sem); |
| 594 | ret = get_user_pages(current, current->mm, |
| 595 | gobuf->user_addr & PAGE_MASK, gobuf->page_count, |
| 596 | 1, 1, gobuf->pages, NULL); |
| 597 | up_read(¤t->mm->mmap_sem); |
| 598 | if (ret != gobuf->page_count) { |
| 599 | int i; |
| 600 | for (i = 0; i < ret; ++i) |
| 601 | page_cache_release(gobuf->pages[i]); |
| 602 | gobuf->page_count = 0; |
| 603 | goto unlock_and_return; |
| 604 | } |
| 605 | gobuf->state = BUF_STATE_QUEUED; |
| 606 | spin_lock_irqsave(&go->spinlock, flags); |
| 607 | list_add_tail(&gobuf->stream, &go->stream); |
| 608 | spin_unlock_irqrestore(&go->spinlock, flags); |
| 609 | up(&gofh->lock); |
| 610 | return 0; |
| 611 | } |
| 612 | case VIDIOC_DQBUF: |
| 613 | { |
| 614 | struct v4l2_buffer *buf = arg; |
| 615 | struct go7007_buffer *gobuf; |
| 616 | u32 frame_type_flag; |
| 617 | DEFINE_WAIT(wait); |
| 618 | |
| 619 | if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 620 | return -EINVAL; |
| 621 | if (buf->memory != V4L2_MEMORY_MMAP) |
| 622 | return -EINVAL; |
| 623 | down(&gofh->lock); |
| 624 | retval = -EINVAL; |
| 625 | if (list_empty(&go->stream)) |
| 626 | goto unlock_and_return; |
| 627 | gobuf = list_entry(go->stream.next, |
| 628 | struct go7007_buffer, stream); |
| 629 | retval = -EAGAIN; |
| 630 | if (gobuf->state != BUF_STATE_DONE && |
| 631 | !(file->f_flags & O_NONBLOCK)) { |
| 632 | for (;;) { |
| 633 | prepare_to_wait(&go->frame_waitq, &wait, |
| 634 | TASK_INTERRUPTIBLE); |
| 635 | if (gobuf->state == BUF_STATE_DONE) |
| 636 | break; |
| 637 | if (signal_pending(current)) { |
| 638 | retval = -ERESTARTSYS; |
| 639 | break; |
| 640 | } |
| 641 | schedule(); |
| 642 | } |
| 643 | finish_wait(&go->frame_waitq, &wait); |
| 644 | } |
| 645 | if (gobuf->state != BUF_STATE_DONE) |
| 646 | goto unlock_and_return; |
| 647 | spin_lock_irqsave(&go->spinlock, flags); |
| 648 | deactivate_buffer(gobuf); |
| 649 | spin_unlock_irqrestore(&go->spinlock, flags); |
| 650 | frame_type_flag = get_frame_type_flag(gobuf, go->format); |
| 651 | gobuf->state = BUF_STATE_IDLE; |
| 652 | memset(buf, 0, sizeof(*buf)); |
| 653 | buf->index = gobuf->index; |
| 654 | buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 655 | buf->bytesused = gobuf->bytesused; |
| 656 | buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag; |
| 657 | buf->field = V4L2_FIELD_NONE; |
| 658 | buf->timestamp = gobuf->timestamp; |
| 659 | buf->sequence = gobuf->seq; |
| 660 | buf->memory = V4L2_MEMORY_MMAP; |
| 661 | buf->m.offset = gobuf->index * GO7007_BUF_SIZE; |
| 662 | buf->length = GO7007_BUF_SIZE; |
| 663 | buf->reserved = gobuf->modet_active; |
| 664 | up(&gofh->lock); |
| 665 | return 0; |
| 666 | } |
| 667 | case VIDIOC_STREAMON: |
| 668 | { |
| 669 | unsigned int *type = arg; |
| 670 | |
| 671 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 672 | return -EINVAL; |
| 673 | down(&gofh->lock); |
| 674 | down(&go->hw_lock); |
| 675 | if (!go->streaming) { |
| 676 | go->streaming = 1; |
| 677 | go->next_seq = 0; |
| 678 | go->active_buf = NULL; |
| 679 | if (go7007_start_encoder(go) < 0) |
| 680 | retval = -EIO; |
| 681 | else |
| 682 | retval = 0; |
| 683 | } |
| 684 | up(&go->hw_lock); |
| 685 | up(&gofh->lock); |
| 686 | return retval; |
| 687 | } |
| 688 | case VIDIOC_STREAMOFF: |
| 689 | { |
| 690 | unsigned int *type = arg; |
| 691 | |
| 692 | if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 693 | return -EINVAL; |
| 694 | down(&gofh->lock); |
| 695 | go7007_streamoff(go); |
| 696 | up(&gofh->lock); |
| 697 | return 0; |
| 698 | } |
| 699 | case VIDIOC_QUERYCTRL: |
| 700 | { |
| 701 | struct v4l2_queryctrl *ctrl = arg; |
| 702 | u32 id; |
| 703 | |
| 704 | if (!go->i2c_adapter_online) |
| 705 | return -EIO; |
| 706 | id = ctrl->id; |
| 707 | memset(ctrl, 0, sizeof(*ctrl)); |
| 708 | ctrl->id = id; |
| 709 | i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, arg); |
| 710 | return ctrl->name[0] == 0 ? -EINVAL : 0; |
| 711 | } |
| 712 | case VIDIOC_G_CTRL: |
| 713 | { |
| 714 | struct v4l2_control *ctrl = arg; |
| 715 | struct v4l2_queryctrl query; |
| 716 | |
| 717 | if (!go->i2c_adapter_online) |
| 718 | return -EIO; |
| 719 | memset(&query, 0, sizeof(query)); |
| 720 | query.id = ctrl->id; |
| 721 | i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query); |
| 722 | if (query.name[0] == 0) |
| 723 | return -EINVAL; |
| 724 | i2c_clients_command(&go->i2c_adapter, VIDIOC_G_CTRL, arg); |
| 725 | return 0; |
| 726 | } |
| 727 | case VIDIOC_S_CTRL: |
| 728 | { |
| 729 | struct v4l2_control *ctrl = arg; |
| 730 | struct v4l2_queryctrl query; |
| 731 | |
| 732 | if (!go->i2c_adapter_online) |
| 733 | return -EIO; |
| 734 | memset(&query, 0, sizeof(query)); |
| 735 | query.id = ctrl->id; |
| 736 | i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query); |
| 737 | if (query.name[0] == 0) |
| 738 | return -EINVAL; |
| 739 | i2c_clients_command(&go->i2c_adapter, VIDIOC_S_CTRL, arg); |
| 740 | return 0; |
| 741 | } |
| 742 | case VIDIOC_G_PARM: |
| 743 | { |
| 744 | struct v4l2_streamparm *parm = arg; |
| 745 | struct v4l2_fract timeperframe = { |
| 746 | .numerator = 1001 * go->fps_scale, |
| 747 | .denominator = go->sensor_framerate, |
| 748 | }; |
| 749 | |
| 750 | if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 751 | return -EINVAL; |
| 752 | memset(parm, 0, sizeof(*parm)); |
| 753 | parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 754 | parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME; |
| 755 | parm->parm.capture.timeperframe = timeperframe; |
| 756 | return 0; |
| 757 | } |
| 758 | case VIDIOC_S_PARM: |
| 759 | { |
| 760 | struct v4l2_streamparm *parm = arg; |
| 761 | unsigned int n, d; |
| 762 | |
| 763 | if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 764 | return -EINVAL; |
| 765 | if (parm->parm.capture.capturemode != 0) |
| 766 | return -EINVAL; |
| 767 | n = go->sensor_framerate * |
| 768 | parm->parm.capture.timeperframe.numerator; |
| 769 | d = 1001 * parm->parm.capture.timeperframe.denominator; |
| 770 | if (n != 0 && d != 0 && n > d) |
| 771 | go->fps_scale = (n + d/2) / d; |
| 772 | else |
| 773 | go->fps_scale = 1; |
| 774 | return 0; |
| 775 | } |
| 776 | case VIDIOC_ENUMSTD: |
| 777 | { |
| 778 | struct v4l2_standard *std = arg; |
| 779 | |
| 780 | if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) && |
| 781 | go->input == go->board_info->num_inputs - 1) { |
| 782 | if (!go->i2c_adapter_online) |
| 783 | return -EIO; |
| 784 | i2c_clients_command(&go->i2c_adapter, |
| 785 | VIDIOC_ENUMSTD, arg); |
| 786 | if (!std->id) /* hack to indicate EINVAL from tuner */ |
| 787 | return -EINVAL; |
| 788 | } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) { |
| 789 | switch (std->index) { |
| 790 | case 0: |
| 791 | v4l2_video_std_construct(std, |
| 792 | V4L2_STD_NTSC, "NTSC"); |
| 793 | break; |
| 794 | case 1: |
| 795 | v4l2_video_std_construct(std, |
| 796 | V4L2_STD_PAL | V4L2_STD_SECAM, |
| 797 | "PAL/SECAM"); |
| 798 | break; |
| 799 | default: |
| 800 | return -EINVAL; |
| 801 | } |
| 802 | } else { |
| 803 | if (std->index != 0) |
| 804 | return -EINVAL; |
| 805 | memset(std, 0, sizeof(*std)); |
| 806 | snprintf(std->name, sizeof(std->name), "%dx%d, %dfps", |
| 807 | go->board_info->sensor_width, |
| 808 | go->board_info->sensor_height, |
| 809 | go->board_info->sensor_framerate / 1000); |
| 810 | std->frameperiod.numerator = 1001; |
| 811 | std->frameperiod.denominator = |
| 812 | go->board_info->sensor_framerate; |
| 813 | } |
| 814 | return 0; |
| 815 | } |
| 816 | case VIDIOC_G_STD: |
| 817 | { |
| 818 | v4l2_std_id *std = arg; |
| 819 | |
| 820 | if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) && |
| 821 | go->input == go->board_info->num_inputs - 1) { |
| 822 | if (!go->i2c_adapter_online) |
| 823 | return -EIO; |
| 824 | i2c_clients_command(&go->i2c_adapter, |
| 825 | VIDIOC_G_STD, arg); |
| 826 | } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) { |
| 827 | if (go->standard == GO7007_STD_NTSC) |
| 828 | *std = V4L2_STD_NTSC; |
| 829 | else |
| 830 | *std = V4L2_STD_PAL | V4L2_STD_SECAM; |
| 831 | } else |
| 832 | *std = 0; |
| 833 | return 0; |
| 834 | } |
| 835 | case VIDIOC_S_STD: |
| 836 | { |
| 837 | v4l2_std_id *std = arg; |
| 838 | int norm; |
| 839 | |
| 840 | if (go->streaming) |
| 841 | return -EBUSY; |
| 842 | if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) && |
| 843 | *std != 0) |
| 844 | return -EINVAL; |
| 845 | if (*std == 0) |
| 846 | return -EINVAL; |
| 847 | if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) && |
| 848 | go->input == go->board_info->num_inputs - 1) { |
| 849 | if (!go->i2c_adapter_online) |
| 850 | return -EIO; |
| 851 | i2c_clients_command(&go->i2c_adapter, |
| 852 | VIDIOC_S_STD, arg); |
| 853 | if (!*std) /* hack to indicate EINVAL from tuner */ |
| 854 | return -EINVAL; |
| 855 | } |
| 856 | if (*std & V4L2_STD_NTSC) { |
| 857 | go->standard = GO7007_STD_NTSC; |
| 858 | go->sensor_framerate = 30000; |
| 859 | norm = VIDEO_MODE_NTSC; |
| 860 | } else if (*std & V4L2_STD_PAL) { |
| 861 | go->standard = GO7007_STD_PAL; |
| 862 | go->sensor_framerate = 25025; |
| 863 | norm = VIDEO_MODE_PAL; |
| 864 | } else if (*std & V4L2_STD_SECAM) { |
| 865 | go->standard = GO7007_STD_PAL; |
| 866 | go->sensor_framerate = 25025; |
| 867 | norm = VIDEO_MODE_SECAM; |
| 868 | } else |
| 869 | return -EINVAL; |
| 870 | if (go->i2c_adapter_online) |
| 871 | i2c_clients_command(&go->i2c_adapter, |
| 872 | DECODER_SET_NORM, &norm); |
| 873 | set_capture_size(go, NULL, 0); |
| 874 | return 0; |
| 875 | } |
| 876 | case VIDIOC_QUERYSTD: |
| 877 | { |
| 878 | v4l2_std_id *std = arg; |
| 879 | |
| 880 | if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) && |
| 881 | go->input == go->board_info->num_inputs - 1) { |
| 882 | if (!go->i2c_adapter_online) |
| 883 | return -EIO; |
| 884 | i2c_clients_command(&go->i2c_adapter, |
| 885 | VIDIOC_QUERYSTD, arg); |
| 886 | } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) |
| 887 | *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM; |
| 888 | else |
| 889 | *std = 0; |
| 890 | return 0; |
| 891 | } |
| 892 | case VIDIOC_ENUMINPUT: |
| 893 | { |
| 894 | struct v4l2_input *inp = arg; |
| 895 | int index; |
| 896 | |
| 897 | if (inp->index >= go->board_info->num_inputs) |
| 898 | return -EINVAL; |
| 899 | index = inp->index; |
| 900 | memset(inp, 0, sizeof(*inp)); |
| 901 | inp->index = index; |
| 902 | strncpy(inp->name, go->board_info->inputs[index].name, |
| 903 | sizeof(inp->name)); |
| 904 | /* If this board has a tuner, it will be the last input */ |
| 905 | if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) && |
| 906 | index == go->board_info->num_inputs - 1) |
| 907 | inp->type = V4L2_INPUT_TYPE_TUNER; |
| 908 | else |
| 909 | inp->type = V4L2_INPUT_TYPE_CAMERA; |
| 910 | inp->audioset = 0; |
| 911 | inp->tuner = 0; |
| 912 | if (go->board_info->sensor_flags & GO7007_SENSOR_TV) |
| 913 | inp->std = V4L2_STD_NTSC | V4L2_STD_PAL | |
| 914 | V4L2_STD_SECAM; |
| 915 | else |
| 916 | inp->std = 0; |
| 917 | return 0; |
| 918 | } |
| 919 | case VIDIOC_G_INPUT: |
| 920 | { |
| 921 | int *input = arg; |
| 922 | |
| 923 | *input = go->input; |
| 924 | return 0; |
| 925 | } |
| 926 | case VIDIOC_S_INPUT: |
| 927 | { |
| 928 | int *input = arg; |
| 929 | |
| 930 | if (*input >= go->board_info->num_inputs) |
| 931 | return -EINVAL; |
| 932 | if (go->streaming) |
| 933 | return -EBUSY; |
| 934 | go->input = *input; |
| 935 | if (go->i2c_adapter_online) { |
| 936 | i2c_clients_command(&go->i2c_adapter, DECODER_SET_INPUT, |
| 937 | &go->board_info->inputs[*input].video_input); |
| 938 | i2c_clients_command(&go->i2c_adapter, VIDIOC_S_AUDIO, |
| 939 | &go->board_info->inputs[*input].audio_input); |
| 940 | } |
| 941 | return 0; |
| 942 | } |
| 943 | case VIDIOC_G_TUNER: |
| 944 | { |
| 945 | struct v4l2_tuner *t = arg; |
| 946 | |
| 947 | if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER)) |
| 948 | return -EINVAL; |
| 949 | if (t->index != 0) |
| 950 | return -EINVAL; |
| 951 | if (!go->i2c_adapter_online) |
| 952 | return -EIO; |
| 953 | i2c_clients_command(&go->i2c_adapter, VIDIOC_G_TUNER, arg); |
| 954 | t->index = 0; |
| 955 | return 0; |
| 956 | } |
| 957 | case VIDIOC_S_TUNER: |
| 958 | { |
| 959 | struct v4l2_tuner *t = arg; |
| 960 | |
| 961 | if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER)) |
| 962 | return -EINVAL; |
| 963 | if (t->index != 0) |
| 964 | return -EINVAL; |
| 965 | if (!go->i2c_adapter_online) |
| 966 | return -EIO; |
| 967 | switch (go->board_id) { |
| 968 | case GO7007_BOARDID_PX_TV402U_NA: |
| 969 | case GO7007_BOARDID_PX_TV402U_JP: |
| 970 | /* No selectable options currently */ |
| 971 | if (t->audmode != V4L2_TUNER_MODE_STEREO) |
| 972 | return -EINVAL; |
| 973 | break; |
| 974 | } |
| 975 | i2c_clients_command(&go->i2c_adapter, VIDIOC_S_TUNER, arg); |
| 976 | return 0; |
| 977 | } |
| 978 | case VIDIOC_G_FREQUENCY: |
| 979 | { |
| 980 | struct v4l2_frequency *f = arg; |
| 981 | |
| 982 | if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER)) |
| 983 | return -EINVAL; |
| 984 | if (!go->i2c_adapter_online) |
| 985 | return -EIO; |
| 986 | memset(f, 0, sizeof(*f)); |
| 987 | f->type = V4L2_TUNER_ANALOG_TV; |
| 988 | i2c_clients_command(&go->i2c_adapter, VIDIOC_G_FREQUENCY, arg); |
| 989 | return 0; |
| 990 | } |
| 991 | case VIDIOC_S_FREQUENCY: |
| 992 | { |
| 993 | if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER)) |
| 994 | return -EINVAL; |
| 995 | if (!go->i2c_adapter_online) |
| 996 | return -EIO; |
| 997 | i2c_clients_command(&go->i2c_adapter, VIDIOC_S_FREQUENCY, arg); |
| 998 | return 0; |
| 999 | } |
| 1000 | case VIDIOC_CROPCAP: |
| 1001 | { |
| 1002 | struct v4l2_cropcap *cropcap = arg; |
| 1003 | |
| 1004 | if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1005 | return -EINVAL; |
| 1006 | memset(cropcap, 0, sizeof(*cropcap)); |
| 1007 | cropcap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1008 | /* These specify the raw input of the sensor */ |
| 1009 | switch (go->standard) { |
| 1010 | case GO7007_STD_NTSC: |
| 1011 | cropcap->bounds.top = 0; |
| 1012 | cropcap->bounds.left = 0; |
| 1013 | cropcap->bounds.width = 720; |
| 1014 | cropcap->bounds.height = 480; |
| 1015 | cropcap->defrect.top = 0; |
| 1016 | cropcap->defrect.left = 0; |
| 1017 | cropcap->defrect.width = 720; |
| 1018 | cropcap->defrect.height = 480; |
| 1019 | break; |
| 1020 | case GO7007_STD_PAL: |
| 1021 | cropcap->bounds.top = 0; |
| 1022 | cropcap->bounds.left = 0; |
| 1023 | cropcap->bounds.width = 720; |
| 1024 | cropcap->bounds.height = 576; |
| 1025 | cropcap->defrect.top = 0; |
| 1026 | cropcap->defrect.left = 0; |
| 1027 | cropcap->defrect.width = 720; |
| 1028 | cropcap->defrect.height = 576; |
| 1029 | break; |
| 1030 | case GO7007_STD_OTHER: |
| 1031 | cropcap->bounds.top = 0; |
| 1032 | cropcap->bounds.left = 0; |
| 1033 | cropcap->bounds.width = go->board_info->sensor_width; |
| 1034 | cropcap->bounds.height = go->board_info->sensor_height; |
| 1035 | cropcap->defrect.top = 0; |
| 1036 | cropcap->defrect.left = 0; |
| 1037 | cropcap->defrect.width = go->board_info->sensor_width; |
| 1038 | cropcap->defrect.height = go->board_info->sensor_height; |
| 1039 | break; |
| 1040 | } |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | case VIDIOC_G_CROP: |
| 1045 | { |
| 1046 | struct v4l2_crop *crop = arg; |
| 1047 | |
| 1048 | if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1049 | return -EINVAL; |
| 1050 | memset(crop, 0, sizeof(*crop)); |
| 1051 | crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1052 | /* These specify the raw input of the sensor */ |
| 1053 | switch (go->standard) { |
| 1054 | case GO7007_STD_NTSC: |
| 1055 | crop->c.top = 0; |
| 1056 | crop->c.left = 0; |
| 1057 | crop->c.width = 720; |
| 1058 | crop->c.height = 480; |
| 1059 | break; |
| 1060 | case GO7007_STD_PAL: |
| 1061 | crop->c.top = 0; |
| 1062 | crop->c.left = 0; |
| 1063 | crop->c.width = 720; |
| 1064 | crop->c.height = 576; |
| 1065 | break; |
| 1066 | case GO7007_STD_OTHER: |
| 1067 | crop->c.top = 0; |
| 1068 | crop->c.left = 0; |
| 1069 | crop->c.width = go->board_info->sensor_width; |
| 1070 | crop->c.height = go->board_info->sensor_height; |
| 1071 | break; |
| 1072 | } |
| 1073 | |
| 1074 | return 0; |
| 1075 | } |
| 1076 | case VIDIOC_S_CROP: |
| 1077 | { |
| 1078 | struct v4l2_crop *crop = arg; |
| 1079 | |
| 1080 | if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1081 | return -EINVAL; |
| 1082 | return 0; |
| 1083 | } |
| 1084 | case VIDIOC_G_JPEGCOMP: |
| 1085 | { |
| 1086 | struct v4l2_jpegcompression *params = arg; |
| 1087 | |
| 1088 | memset(params, 0, sizeof(*params)); |
| 1089 | params->quality = 50; /* ?? */ |
| 1090 | params->jpeg_markers = V4L2_JPEG_MARKER_DHT | |
| 1091 | V4L2_JPEG_MARKER_DQT; |
| 1092 | |
| 1093 | return 0; |
| 1094 | } |
| 1095 | case VIDIOC_S_JPEGCOMP: |
| 1096 | { |
| 1097 | struct v4l2_jpegcompression *params = arg; |
| 1098 | |
| 1099 | if (params->quality != 50 || |
| 1100 | params->jpeg_markers != (V4L2_JPEG_MARKER_DHT | |
| 1101 | V4L2_JPEG_MARKER_DQT)) |
| 1102 | return -EINVAL; |
| 1103 | return 0; |
| 1104 | } |
| 1105 | /* Temporary ioctls for controlling compression characteristics */ |
| 1106 | case GO7007IOC_S_BITRATE: |
| 1107 | { |
| 1108 | int *bitrate = arg; |
| 1109 | |
| 1110 | if (go->streaming) |
| 1111 | return -EINVAL; |
| 1112 | /* Upper bound is kind of arbitrary here */ |
| 1113 | if (*bitrate < 64000 || *bitrate > 10000000) |
| 1114 | return -EINVAL; |
| 1115 | go->bitrate = *bitrate; |
| 1116 | return 0; |
| 1117 | } |
| 1118 | case GO7007IOC_G_BITRATE: |
| 1119 | { |
| 1120 | int *bitrate = arg; |
| 1121 | |
| 1122 | *bitrate = go->bitrate; |
| 1123 | return 0; |
| 1124 | } |
| 1125 | case GO7007IOC_S_COMP_PARAMS: |
| 1126 | { |
| 1127 | struct go7007_comp_params *comp = arg; |
| 1128 | |
| 1129 | if (go->format == GO7007_FORMAT_MJPEG) |
| 1130 | return -EINVAL; |
| 1131 | if (comp->gop_size > 0) |
| 1132 | go->gop_size = comp->gop_size; |
| 1133 | else |
| 1134 | go->gop_size = go->sensor_framerate / 1000; |
| 1135 | if (go->gop_size != 15) |
| 1136 | go->dvd_mode = 0; |
| 1137 | /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */ |
| 1138 | if (go->board_info->sensor_flags & GO7007_SENSOR_TV) { |
| 1139 | switch (comp->aspect_ratio) { |
| 1140 | case GO7007_ASPECT_RATIO_4_3_NTSC: |
| 1141 | case GO7007_ASPECT_RATIO_4_3_PAL: |
| 1142 | go->aspect_ratio = GO7007_RATIO_4_3; |
| 1143 | break; |
| 1144 | case GO7007_ASPECT_RATIO_16_9_NTSC: |
| 1145 | case GO7007_ASPECT_RATIO_16_9_PAL: |
| 1146 | go->aspect_ratio = GO7007_RATIO_16_9; |
| 1147 | break; |
| 1148 | default: |
| 1149 | go->aspect_ratio = GO7007_RATIO_1_1; |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
| 1153 | if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) { |
| 1154 | go->dvd_mode = 0; |
| 1155 | go->seq_header_enable = 0; |
| 1156 | } else { |
| 1157 | go->seq_header_enable = 1; |
| 1158 | } |
| 1159 | /* fall-through */ |
| 1160 | } |
| 1161 | case GO7007IOC_G_COMP_PARAMS: |
| 1162 | { |
| 1163 | struct go7007_comp_params *comp = arg; |
| 1164 | |
| 1165 | if (go->format == GO7007_FORMAT_MJPEG) |
| 1166 | return -EINVAL; |
| 1167 | memset(comp, 0, sizeof(*comp)); |
| 1168 | comp->gop_size = go->gop_size; |
| 1169 | comp->max_b_frames = go->ipb ? 2 : 0; |
| 1170 | switch (go->aspect_ratio) { |
| 1171 | case GO7007_RATIO_4_3: |
| 1172 | if (go->standard == GO7007_STD_NTSC) |
| 1173 | comp->aspect_ratio = |
| 1174 | GO7007_ASPECT_RATIO_4_3_NTSC; |
| 1175 | else |
| 1176 | comp->aspect_ratio = |
| 1177 | GO7007_ASPECT_RATIO_4_3_PAL; |
| 1178 | break; |
| 1179 | case GO7007_RATIO_16_9: |
| 1180 | if (go->standard == GO7007_STD_NTSC) |
| 1181 | comp->aspect_ratio = |
| 1182 | GO7007_ASPECT_RATIO_16_9_NTSC; |
| 1183 | else |
| 1184 | comp->aspect_ratio = |
| 1185 | GO7007_ASPECT_RATIO_16_9_PAL; |
| 1186 | break; |
| 1187 | default: |
| 1188 | comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1; |
| 1189 | break; |
| 1190 | } |
| 1191 | if (go->closed_gop) |
| 1192 | comp->flags |= GO7007_COMP_CLOSED_GOP; |
| 1193 | if (!go->seq_header_enable) |
| 1194 | comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER; |
| 1195 | return 0; |
| 1196 | } |
| 1197 | case GO7007IOC_S_MPEG_PARAMS: |
| 1198 | { |
| 1199 | struct go7007_mpeg_params *mpeg = arg; |
| 1200 | |
| 1201 | if (go->format != GO7007_FORMAT_MPEG1 && |
| 1202 | go->format != GO7007_FORMAT_MPEG2 && |
| 1203 | go->format != GO7007_FORMAT_MPEG4) |
| 1204 | return -EINVAL; |
| 1205 | |
| 1206 | if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) { |
| 1207 | go->format = GO7007_FORMAT_MPEG2; |
| 1208 | go->bitrate = 9800000; |
| 1209 | go->gop_size = 15; |
| 1210 | go->pali = 0x48; |
| 1211 | go->closed_gop = 1; |
| 1212 | go->repeat_seqhead = 0; |
| 1213 | go->seq_header_enable = 1; |
| 1214 | go->gop_header_enable = 1; |
| 1215 | go->dvd_mode = 1; |
| 1216 | } else { |
| 1217 | switch (mpeg->mpeg_video_standard) { |
| 1218 | case GO7007_MPEG_VIDEO_MPEG1: |
| 1219 | go->format = GO7007_FORMAT_MPEG1; |
| 1220 | go->pali = 0; |
| 1221 | break; |
| 1222 | case GO7007_MPEG_VIDEO_MPEG2: |
| 1223 | go->format = GO7007_FORMAT_MPEG2; |
| 1224 | if (mpeg->pali >> 24 == 2) |
| 1225 | go->pali = mpeg->pali & 0xff; |
| 1226 | else |
| 1227 | go->pali = 0x48; |
| 1228 | break; |
| 1229 | case GO7007_MPEG_VIDEO_MPEG4: |
| 1230 | go->format = GO7007_FORMAT_MPEG4; |
| 1231 | if (mpeg->pali >> 24 == 4) |
| 1232 | go->pali = mpeg->pali & 0xff; |
| 1233 | else |
| 1234 | go->pali = 0xf5; |
| 1235 | break; |
| 1236 | default: |
| 1237 | return -EINVAL; |
| 1238 | } |
| 1239 | go->gop_header_enable = |
| 1240 | mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER |
| 1241 | ? 0 : 1; |
| 1242 | if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER) |
| 1243 | go->repeat_seqhead = 1; |
| 1244 | else |
| 1245 | go->repeat_seqhead = 0; |
| 1246 | go->dvd_mode = 0; |
| 1247 | } |
| 1248 | /* fall-through */ |
| 1249 | } |
| 1250 | case GO7007IOC_G_MPEG_PARAMS: |
| 1251 | { |
| 1252 | struct go7007_mpeg_params *mpeg = arg; |
| 1253 | |
| 1254 | memset(mpeg, 0, sizeof(*mpeg)); |
| 1255 | switch (go->format) { |
| 1256 | case GO7007_FORMAT_MPEG1: |
| 1257 | mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1; |
| 1258 | mpeg->pali = 0; |
| 1259 | break; |
| 1260 | case GO7007_FORMAT_MPEG2: |
| 1261 | mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2; |
| 1262 | mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali); |
| 1263 | break; |
| 1264 | case GO7007_FORMAT_MPEG4: |
| 1265 | mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4; |
| 1266 | mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali); |
| 1267 | break; |
| 1268 | default: |
| 1269 | return -EINVAL; |
| 1270 | } |
| 1271 | if (!go->gop_header_enable) |
| 1272 | mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER; |
| 1273 | if (go->repeat_seqhead) |
| 1274 | mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER; |
| 1275 | if (go->dvd_mode) |
| 1276 | mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE; |
| 1277 | return 0; |
| 1278 | } |
| 1279 | case GO7007IOC_S_MD_PARAMS: |
| 1280 | { |
| 1281 | struct go7007_md_params *mdp = arg; |
| 1282 | |
| 1283 | if (mdp->region > 3) |
| 1284 | return -EINVAL; |
| 1285 | if (mdp->trigger > 0) { |
| 1286 | go->modet[mdp->region].pixel_threshold = |
| 1287 | mdp->pixel_threshold >> 1; |
| 1288 | go->modet[mdp->region].motion_threshold = |
| 1289 | mdp->motion_threshold >> 1; |
| 1290 | go->modet[mdp->region].mb_threshold = |
| 1291 | mdp->trigger >> 1; |
| 1292 | go->modet[mdp->region].enable = 1; |
| 1293 | } else |
| 1294 | go->modet[mdp->region].enable = 0; |
| 1295 | /* fall-through */ |
| 1296 | } |
| 1297 | case GO7007IOC_G_MD_PARAMS: |
| 1298 | { |
| 1299 | struct go7007_md_params *mdp = arg; |
| 1300 | int region = mdp->region; |
| 1301 | |
| 1302 | if (mdp->region > 3) |
| 1303 | return -EINVAL; |
| 1304 | memset(mdp, 0, sizeof(struct go7007_md_params)); |
| 1305 | mdp->region = region; |
| 1306 | if (!go->modet[region].enable) |
| 1307 | return 0; |
| 1308 | mdp->pixel_threshold = |
| 1309 | (go->modet[region].pixel_threshold << 1) + 1; |
| 1310 | mdp->motion_threshold = |
| 1311 | (go->modet[region].motion_threshold << 1) + 1; |
| 1312 | mdp->trigger = |
| 1313 | (go->modet[region].mb_threshold << 1) + 1; |
| 1314 | return 0; |
| 1315 | } |
| 1316 | case GO7007IOC_S_MD_REGION: |
| 1317 | { |
| 1318 | struct go7007_md_region *region = arg; |
| 1319 | |
| 1320 | if (region->region < 1 || region->region > 3) |
| 1321 | return -EINVAL; |
| 1322 | return clip_to_modet_map(go, region->region, region->clips); |
| 1323 | } |
| 1324 | default: |
| 1325 | printk(KERN_DEBUG "go7007: unsupported ioctl %d\n", cmd); |
| 1326 | return -ENOIOCTLCMD; |
| 1327 | } |
| 1328 | return 0; |
| 1329 | |
| 1330 | unlock_and_return: |
| 1331 | up(&gofh->lock); |
| 1332 | return retval; |
| 1333 | } |
| 1334 | |
| 1335 | static int go7007_ioctl(struct inode *inode, struct file *file, |
| 1336 | unsigned int cmd, unsigned long arg) |
| 1337 | { |
| 1338 | struct go7007_file *gofh = file->private_data; |
| 1339 | |
| 1340 | if (gofh->go->status != STATUS_ONLINE) |
| 1341 | return -EIO; |
| 1342 | |
| 1343 | return video_usercopy(inode, file, cmd, arg, go7007_do_ioctl); |
| 1344 | } |
| 1345 | |
| 1346 | static ssize_t go7007_read(struct file *file, char __user *data, |
| 1347 | size_t count, loff_t *ppos) |
| 1348 | { |
| 1349 | return -EINVAL; |
| 1350 | } |
| 1351 | |
| 1352 | static void go7007_vm_open(struct vm_area_struct *vma) |
| 1353 | { |
| 1354 | struct go7007_buffer *gobuf = vma->vm_private_data; |
| 1355 | |
| 1356 | ++gobuf->mapped; |
| 1357 | } |
| 1358 | |
| 1359 | static void go7007_vm_close(struct vm_area_struct *vma) |
| 1360 | { |
| 1361 | struct go7007_buffer *gobuf = vma->vm_private_data; |
| 1362 | unsigned long flags; |
| 1363 | |
| 1364 | if (--gobuf->mapped == 0) { |
| 1365 | spin_lock_irqsave(&gobuf->go->spinlock, flags); |
| 1366 | deactivate_buffer(gobuf); |
| 1367 | spin_unlock_irqrestore(&gobuf->go->spinlock, flags); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | /* Copied from videobuf-dma-sg.c */ |
| 1372 | static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1373 | { |
| 1374 | struct page *page; |
| 1375 | |
| 1376 | page = alloc_page(GFP_USER | __GFP_DMA32); |
| 1377 | if (!page) |
| 1378 | return VM_FAULT_OOM; |
| 1379 | clear_user_page(page_address(page), (unsigned long)vmf->virtual_address, |
| 1380 | page); |
| 1381 | vmf->page = page; |
| 1382 | return 0; |
| 1383 | } |
| 1384 | |
| 1385 | static struct vm_operations_struct go7007_vm_ops = { |
| 1386 | .open = go7007_vm_open, |
| 1387 | .close = go7007_vm_close, |
| 1388 | .fault = go7007_vm_fault, |
| 1389 | }; |
| 1390 | |
| 1391 | static int go7007_mmap(struct file *file, struct vm_area_struct *vma) |
| 1392 | { |
| 1393 | struct go7007_file *gofh = file->private_data; |
| 1394 | unsigned int index; |
| 1395 | |
| 1396 | if (gofh->go->status != STATUS_ONLINE) |
| 1397 | return -EIO; |
| 1398 | if (!(vma->vm_flags & VM_SHARED)) |
| 1399 | return -EINVAL; /* only support VM_SHARED mapping */ |
| 1400 | if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE) |
| 1401 | return -EINVAL; /* must map exactly one full buffer */ |
| 1402 | down(&gofh->lock); |
| 1403 | index = vma->vm_pgoff / GO7007_BUF_PAGES; |
| 1404 | if (index >= gofh->buf_count) { |
| 1405 | up(&gofh->lock); |
| 1406 | return -EINVAL; /* trying to map beyond requested buffers */ |
| 1407 | } |
| 1408 | if (index * GO7007_BUF_PAGES != vma->vm_pgoff) { |
| 1409 | up(&gofh->lock); |
| 1410 | return -EINVAL; /* offset is not aligned on buffer boundary */ |
| 1411 | } |
| 1412 | if (gofh->bufs[index].mapped > 0) { |
| 1413 | up(&gofh->lock); |
| 1414 | return -EBUSY; |
| 1415 | } |
| 1416 | gofh->bufs[index].mapped = 1; |
| 1417 | gofh->bufs[index].user_addr = vma->vm_start; |
| 1418 | vma->vm_ops = &go7007_vm_ops; |
| 1419 | vma->vm_flags |= VM_DONTEXPAND; |
| 1420 | vma->vm_flags &= ~VM_IO; |
| 1421 | vma->vm_private_data = &gofh->bufs[index]; |
| 1422 | up(&gofh->lock); |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
| 1426 | static unsigned int go7007_poll(struct file *file, poll_table *wait) |
| 1427 | { |
| 1428 | struct go7007_file *gofh = file->private_data; |
| 1429 | struct go7007_buffer *gobuf; |
| 1430 | |
| 1431 | if (list_empty(&gofh->go->stream)) |
| 1432 | return POLLERR; |
| 1433 | gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream); |
| 1434 | poll_wait(file, &gofh->go->frame_waitq, wait); |
| 1435 | if (gobuf->state == BUF_STATE_DONE) |
| 1436 | return POLLIN | POLLRDNORM; |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | static void go7007_vfl_release(struct video_device *vfd) |
| 1441 | { |
| 1442 | struct go7007 *go = video_get_drvdata(vfd); |
| 1443 | |
| 1444 | video_device_release(vfd); |
| 1445 | if (--go->ref_count == 0) |
| 1446 | kfree(go); |
| 1447 | } |
| 1448 | |
| 1449 | static struct file_operations go7007_fops = { |
| 1450 | .owner = THIS_MODULE, |
| 1451 | .open = go7007_open, |
| 1452 | .release = go7007_release, |
| 1453 | .ioctl = go7007_ioctl, |
| 1454 | .llseek = no_llseek, |
| 1455 | .read = go7007_read, |
| 1456 | .mmap = go7007_mmap, |
| 1457 | .poll = go7007_poll, |
| 1458 | }; |
| 1459 | |
| 1460 | static struct video_device go7007_template = { |
| 1461 | .name = "go7007", |
| 1462 | .fops = &go7007_fops, |
| 1463 | .minor = -1, |
| 1464 | .release = go7007_vfl_release, |
| 1465 | }; |
| 1466 | |
| 1467 | int go7007_v4l2_init(struct go7007 *go) |
| 1468 | { |
| 1469 | int rv; |
| 1470 | |
| 1471 | go->video_dev = video_device_alloc(); |
| 1472 | if (go->video_dev == NULL) |
| 1473 | return -ENOMEM; |
| 1474 | memcpy(go->video_dev, &go7007_template, sizeof(go7007_template)); |
| 1475 | go->video_dev->parent = go->dev; |
| 1476 | rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1); |
| 1477 | if (rv < 0) { |
| 1478 | video_device_release(go->video_dev); |
| 1479 | go->video_dev = NULL; |
| 1480 | return rv; |
| 1481 | } |
| 1482 | video_set_drvdata(go->video_dev, go); |
| 1483 | ++go->ref_count; |
| 1484 | |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | void go7007_v4l2_remove(struct go7007 *go) |
| 1489 | { |
| 1490 | unsigned long flags; |
| 1491 | |
| 1492 | down(&go->hw_lock); |
| 1493 | if (go->streaming) { |
| 1494 | go->streaming = 0; |
| 1495 | go7007_stream_stop(go); |
| 1496 | spin_lock_irqsave(&go->spinlock, flags); |
| 1497 | abort_queued(go); |
| 1498 | spin_unlock_irqrestore(&go->spinlock, flags); |
| 1499 | } |
| 1500 | up(&go->hw_lock); |
| 1501 | if (go->video_dev) |
| 1502 | video_unregister_device(go->video_dev); |
| 1503 | } |