2 * Copyright (C) 2005-2006 Micronas USA Inc.
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.
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.
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.
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/slab.h>
26 #include <linux/unistd.h>
27 #include <linux/time.h>
28 #include <linux/vmalloc.h>
29 #include <linux/pagemap.h>
30 #include <linux/videodev2.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-ioctl.h>
33 #include <media/v4l2-subdev.h>
34 #include <linux/i2c.h>
35 #include <linux/mutex.h>
36 #include <linux/uaccess.h>
39 #include "go7007-priv.h"
42 /* Temporary defines until accepted in v4l-dvb */
43 #ifndef V4L2_MPEG_STREAM_TYPE_MPEG_ELEM
44 #define V4L2_MPEG_STREAM_TYPE_MPEG_ELEM 6 /* MPEG elementary stream */
46 #ifndef V4L2_MPEG_VIDEO_ENCODING_MPEG_4
47 #define V4L2_MPEG_VIDEO_ENCODING_MPEG_4 3
50 #define call_all(dev, o, f, args...) \
51 v4l2_device_call_until_err(dev, 0, o, f, ##args)
53 static void deactivate_buffer(struct go7007_buffer *gobuf)
57 if (gobuf->state != BUF_STATE_IDLE) {
58 list_del(&gobuf->stream);
59 gobuf->state = BUF_STATE_IDLE;
61 if (gobuf->page_count > 0) {
62 for (i = 0; i < gobuf->page_count; ++i)
63 page_cache_release(gobuf->pages[i]);
64 gobuf->page_count = 0;
68 static void abort_queued(struct go7007 *go)
70 struct go7007_buffer *gobuf, *next;
72 list_for_each_entry_safe(gobuf, next, &go->stream, stream) {
73 deactivate_buffer(gobuf);
77 static int go7007_streamoff(struct go7007 *go)
82 mutex_lock(&go->hw_lock);
85 go7007_stream_stop(go);
86 spin_lock_irqsave(&go->spinlock, flags);
88 spin_unlock_irqrestore(&go->spinlock, flags);
89 go7007_reset_encoder(go);
92 mutex_unlock(&go->hw_lock);
96 static int go7007_open(struct file *file)
98 struct go7007 *go = video_get_drvdata(video_devdata(file));
99 struct go7007_file *gofh;
101 if (go->status != STATUS_ONLINE)
103 gofh = kmalloc(sizeof(struct go7007_file), GFP_KERNEL);
108 mutex_init(&gofh->lock);
110 file->private_data = gofh;
114 static int go7007_release(struct file *file)
116 struct go7007_file *gofh = file->private_data;
117 struct go7007 *go = gofh->go;
119 if (gofh->buf_count > 0) {
120 go7007_streamoff(go);
126 if (--go->ref_count == 0)
128 file->private_data = NULL;
132 static u32 get_frame_type_flag(struct go7007_buffer *gobuf, int format)
134 u8 *f = page_address(gobuf->pages[0]);
137 case GO7007_FORMAT_MJPEG:
138 return V4L2_BUF_FLAG_KEYFRAME;
139 case GO7007_FORMAT_MPEG4:
140 switch ((f[gobuf->frame_offset + 4] >> 6) & 0x3) {
142 return V4L2_BUF_FLAG_KEYFRAME;
144 return V4L2_BUF_FLAG_PFRAME;
146 return V4L2_BUF_FLAG_BFRAME;
150 case GO7007_FORMAT_MPEG1:
151 case GO7007_FORMAT_MPEG2:
152 switch ((f[gobuf->frame_offset + 5] >> 3) & 0x7) {
154 return V4L2_BUF_FLAG_KEYFRAME;
156 return V4L2_BUF_FLAG_PFRAME;
158 return V4L2_BUF_FLAG_BFRAME;
167 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
169 int sensor_height = 0, sensor_width = 0;
170 int width, height, i;
172 if (fmt != NULL && fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
173 fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG &&
174 fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG4)
177 switch (go->standard) {
178 case GO7007_STD_NTSC:
186 case GO7007_STD_OTHER:
187 sensor_width = go->board_info->sensor_width;
188 sensor_height = go->board_info->sensor_height;
193 width = sensor_width;
194 height = sensor_height;
195 } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
196 if (fmt->fmt.pix.width > sensor_width)
197 width = sensor_width;
198 else if (fmt->fmt.pix.width < 144)
201 width = fmt->fmt.pix.width & ~0x0f;
203 if (fmt->fmt.pix.height > sensor_height)
204 height = sensor_height;
205 else if (fmt->fmt.pix.height < 96)
208 height = fmt->fmt.pix.height & ~0x0f;
210 int requested_size = fmt->fmt.pix.width * fmt->fmt.pix.height;
211 int sensor_size = sensor_width * sensor_height;
213 if (64 * requested_size < 9 * sensor_size) {
214 width = sensor_width / 4;
215 height = sensor_height / 4;
216 } else if (64 * requested_size < 36 * sensor_size) {
217 width = sensor_width / 2;
218 height = sensor_height / 2;
220 width = sensor_width;
221 height = sensor_height;
228 u32 pixelformat = fmt->fmt.pix.pixelformat;
230 memset(fmt, 0, sizeof(*fmt));
231 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
232 fmt->fmt.pix.width = width;
233 fmt->fmt.pix.height = height;
234 fmt->fmt.pix.pixelformat = pixelformat;
235 fmt->fmt.pix.field = V4L2_FIELD_NONE;
236 fmt->fmt.pix.bytesperline = 0;
237 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
238 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
246 go->encoder_h_offset = go->board_info->sensor_h_offset;
247 go->encoder_v_offset = go->board_info->sensor_v_offset;
248 for (i = 0; i < 4; ++i)
249 go->modet[i].enable = 0;
250 for (i = 0; i < 1624; ++i)
251 go->modet_map[i] = 0;
253 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
254 struct v4l2_mbus_framefmt mbus_fmt;
256 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
258 mbus_fmt.width = fmt->fmt.pix.width;
260 mbus_fmt.width = width;
262 if (height > sensor_height / 2) {
263 mbus_fmt.height = height / 2;
264 go->encoder_v_halve = 0;
266 mbus_fmt.height = height;
267 go->encoder_v_halve = 1;
269 call_all(&go->v4l2_dev, video, s_mbus_fmt, &mbus_fmt);
271 if (width <= sensor_width / 4) {
272 go->encoder_h_halve = 1;
273 go->encoder_v_halve = 1;
274 go->encoder_subsample = 1;
275 } else if (width <= sensor_width / 2) {
276 go->encoder_h_halve = 1;
277 go->encoder_v_halve = 1;
278 go->encoder_subsample = 0;
280 go->encoder_h_halve = 0;
281 go->encoder_v_halve = 0;
282 go->encoder_subsample = 0;
289 switch (fmt->fmt.pix.pixelformat) {
290 case V4L2_PIX_FMT_MPEG:
291 if (go->format == GO7007_FORMAT_MPEG1 ||
292 go->format == GO7007_FORMAT_MPEG2 ||
293 go->format == GO7007_FORMAT_MPEG4)
295 go->format = GO7007_FORMAT_MPEG1;
297 go->aspect_ratio = GO7007_RATIO_1_1;
298 go->gop_size = go->sensor_framerate / 1000;
301 go->repeat_seqhead = 1;
302 go->seq_header_enable = 1;
303 go->gop_header_enable = 1;
306 /* Backwards compatibility only! */
307 case V4L2_PIX_FMT_MPEG4:
308 if (go->format == GO7007_FORMAT_MPEG4)
310 go->format = GO7007_FORMAT_MPEG4;
312 go->aspect_ratio = GO7007_RATIO_1_1;
313 go->gop_size = go->sensor_framerate / 1000;
316 go->repeat_seqhead = 1;
317 go->seq_header_enable = 1;
318 go->gop_header_enable = 1;
321 case V4L2_PIX_FMT_MJPEG:
322 go->format = GO7007_FORMAT_MJPEG;
324 go->aspect_ratio = GO7007_RATIO_1_1;
328 go->repeat_seqhead = 0;
329 go->seq_header_enable = 0;
330 go->gop_header_enable = 0;
338 static int clip_to_modet_map(struct go7007 *go, int region,
339 struct v4l2_clip *clip_list)
341 struct v4l2_clip clip, *clip_ptr;
344 /* Check if coordinates are OK and if any macroblocks are already
345 * used by other regions (besides 0) */
346 clip_ptr = clip_list;
348 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
350 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
351 clip.c.width <= 0 || (clip.c.width & 0xF))
353 if (clip.c.left + clip.c.width > go->width)
355 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
356 clip.c.height <= 0 || (clip.c.height & 0xF))
358 if (clip.c.top + clip.c.height > go->height)
360 for (y = 0; y < clip.c.height; y += 16)
361 for (x = 0; x < clip.c.width; x += 16) {
362 mbnum = (go->width >> 4) *
363 ((clip.c.top + y) >> 4) +
364 ((clip.c.left + x) >> 4);
365 if (go->modet_map[mbnum] != 0 &&
366 go->modet_map[mbnum] != region)
369 clip_ptr = clip.next;
372 /* Clear old region macroblocks */
373 for (mbnum = 0; mbnum < 1624; ++mbnum)
374 if (go->modet_map[mbnum] == region)
375 go->modet_map[mbnum] = 0;
377 /* Claim macroblocks in this list */
378 clip_ptr = clip_list;
380 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
382 for (y = 0; y < clip.c.height; y += 16)
383 for (x = 0; x < clip.c.width; x += 16) {
384 mbnum = (go->width >> 4) *
385 ((clip.c.top + y) >> 4) +
386 ((clip.c.left + x) >> 4);
387 go->modet_map[mbnum] = region;
389 clip_ptr = clip.next;
395 static int mpeg_query_ctrl(struct v4l2_queryctrl *ctrl)
397 static const u32 mpeg_ctrls[] = {
399 V4L2_CID_MPEG_STREAM_TYPE,
400 V4L2_CID_MPEG_VIDEO_ENCODING,
401 V4L2_CID_MPEG_VIDEO_ASPECT,
402 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
403 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
404 V4L2_CID_MPEG_VIDEO_BITRATE,
407 static const u32 *ctrl_classes[] = {
412 ctrl->id = v4l2_ctrl_next(ctrl_classes, ctrl->id);
415 case V4L2_CID_MPEG_CLASS:
416 return v4l2_ctrl_query_fill(ctrl, 0, 0, 0, 0);
417 case V4L2_CID_MPEG_STREAM_TYPE:
418 return v4l2_ctrl_query_fill(ctrl,
419 V4L2_MPEG_STREAM_TYPE_MPEG2_DVD,
420 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM, 1,
421 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM);
422 case V4L2_CID_MPEG_VIDEO_ENCODING:
423 return v4l2_ctrl_query_fill(ctrl,
424 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
425 V4L2_MPEG_VIDEO_ENCODING_MPEG_4, 1,
426 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
427 case V4L2_CID_MPEG_VIDEO_ASPECT:
428 return v4l2_ctrl_query_fill(ctrl,
429 V4L2_MPEG_VIDEO_ASPECT_1x1,
430 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
431 V4L2_MPEG_VIDEO_ASPECT_1x1);
432 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
433 return v4l2_ctrl_query_fill(ctrl, 0, 34, 1, 15);
434 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
435 return v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
436 case V4L2_CID_MPEG_VIDEO_BITRATE:
437 return v4l2_ctrl_query_fill(ctrl,
447 static int mpeg_s_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
449 /* pretty sure we can't change any of these while streaming */
454 case V4L2_CID_MPEG_STREAM_TYPE:
455 switch (ctrl->value) {
456 case V4L2_MPEG_STREAM_TYPE_MPEG2_DVD:
457 go->format = GO7007_FORMAT_MPEG2;
458 go->bitrate = 9800000;
462 go->repeat_seqhead = 0;
463 go->seq_header_enable = 1;
464 go->gop_header_enable = 1;
467 case V4L2_MPEG_STREAM_TYPE_MPEG_ELEM:
474 case V4L2_CID_MPEG_VIDEO_ENCODING:
475 switch (ctrl->value) {
476 case V4L2_MPEG_VIDEO_ENCODING_MPEG_1:
477 go->format = GO7007_FORMAT_MPEG1;
480 case V4L2_MPEG_VIDEO_ENCODING_MPEG_2:
481 go->format = GO7007_FORMAT_MPEG2;
482 /*if (mpeg->pali >> 24 == 2)
483 go->pali = mpeg->pali & 0xff;
487 case V4L2_MPEG_VIDEO_ENCODING_MPEG_4:
488 go->format = GO7007_FORMAT_MPEG4;
489 /*if (mpeg->pali >> 24 == 4)
490 go->pali = mpeg->pali & 0xff;
497 go->gop_header_enable =
498 /*mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
500 /*if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
501 go->repeat_seqhead = 1;
503 go->repeat_seqhead = 0;
506 case V4L2_CID_MPEG_VIDEO_ASPECT:
507 if (go->format == GO7007_FORMAT_MJPEG)
509 switch (ctrl->value) {
510 case V4L2_MPEG_VIDEO_ASPECT_1x1:
511 go->aspect_ratio = GO7007_RATIO_1_1;
513 case V4L2_MPEG_VIDEO_ASPECT_4x3:
514 go->aspect_ratio = GO7007_RATIO_4_3;
516 case V4L2_MPEG_VIDEO_ASPECT_16x9:
517 go->aspect_ratio = GO7007_RATIO_16_9;
519 case V4L2_MPEG_VIDEO_ASPECT_221x100:
524 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
525 if (ctrl->value < 0 || ctrl->value > 34)
527 go->gop_size = ctrl->value;
529 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
530 if (ctrl->value != 0 && ctrl->value != 1)
532 go->closed_gop = ctrl->value;
534 case V4L2_CID_MPEG_VIDEO_BITRATE:
535 /* Upper bound is kind of arbitrary here */
536 if (ctrl->value < 64000 || ctrl->value > 10000000)
538 go->bitrate = ctrl->value;
546 static int mpeg_g_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
549 case V4L2_CID_MPEG_STREAM_TYPE:
551 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
553 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG_ELEM;
555 case V4L2_CID_MPEG_VIDEO_ENCODING:
556 switch (go->format) {
557 case GO7007_FORMAT_MPEG1:
558 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
560 case GO7007_FORMAT_MPEG2:
561 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
563 case GO7007_FORMAT_MPEG4:
564 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4;
570 case V4L2_CID_MPEG_VIDEO_ASPECT:
571 switch (go->aspect_ratio) {
572 case GO7007_RATIO_1_1:
573 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_1x1;
575 case GO7007_RATIO_4_3:
576 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_4x3;
578 case GO7007_RATIO_16_9:
579 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_16x9;
585 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
586 ctrl->value = go->gop_size;
588 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
589 ctrl->value = go->closed_gop;
591 case V4L2_CID_MPEG_VIDEO_BITRATE:
592 ctrl->value = go->bitrate;
600 static int vidioc_querycap(struct file *file, void *priv,
601 struct v4l2_capability *cap)
603 struct go7007 *go = ((struct go7007_file *) priv)->go;
605 strlcpy(cap->driver, "go7007", sizeof(cap->driver));
606 strlcpy(cap->card, go->name, sizeof(cap->card));
608 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
611 cap->version = KERNEL_VERSION(0, 9, 8);
613 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
614 V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
616 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
617 cap->capabilities |= V4L2_CAP_TUNER;
622 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
623 struct v4l2_fmtdesc *fmt)
627 switch (fmt->index) {
629 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
630 desc = "Motion-JPEG";
633 fmt->pixelformat = V4L2_PIX_FMT_MPEG;
634 desc = "MPEG1/MPEG2/MPEG4";
639 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
640 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
642 strncpy(fmt->description, desc, sizeof(fmt->description));
647 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
648 struct v4l2_format *fmt)
650 struct go7007 *go = ((struct go7007_file *) priv)->go;
652 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
653 fmt->fmt.pix.width = go->width;
654 fmt->fmt.pix.height = go->height;
655 fmt->fmt.pix.pixelformat = (go->format == GO7007_FORMAT_MJPEG) ?
656 V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
657 fmt->fmt.pix.field = V4L2_FIELD_NONE;
658 fmt->fmt.pix.bytesperline = 0;
659 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
660 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
665 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
666 struct v4l2_format *fmt)
668 struct go7007 *go = ((struct go7007_file *) priv)->go;
670 return set_capture_size(go, fmt, 1);
673 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
674 struct v4l2_format *fmt)
676 struct go7007 *go = ((struct go7007_file *) priv)->go;
681 return set_capture_size(go, fmt, 0);
684 static int vidioc_reqbufs(struct file *file, void *priv,
685 struct v4l2_requestbuffers *req)
687 struct go7007_file *gofh = priv;
688 struct go7007 *go = gofh->go;
690 unsigned int count, i;
695 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
696 req->memory != V4L2_MEMORY_MMAP)
699 mutex_lock(&gofh->lock);
700 for (i = 0; i < gofh->buf_count; ++i)
701 if (gofh->bufs[i].mapped > 0)
702 goto unlock_and_return;
704 mutex_lock(&go->hw_lock);
705 if (go->in_use > 0 && gofh->buf_count == 0) {
706 mutex_unlock(&go->hw_lock);
707 goto unlock_and_return;
710 if (gofh->buf_count > 0)
721 gofh->bufs = kcalloc(count, sizeof(struct go7007_buffer),
725 mutex_unlock(&go->hw_lock);
726 goto unlock_and_return;
729 for (i = 0; i < count; ++i) {
730 gofh->bufs[i].go = go;
731 gofh->bufs[i].index = i;
732 gofh->bufs[i].state = BUF_STATE_IDLE;
733 gofh->bufs[i].mapped = 0;
741 gofh->buf_count = count;
742 mutex_unlock(&go->hw_lock);
743 mutex_unlock(&gofh->lock);
745 memset(req, 0, sizeof(*req));
748 req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
749 req->memory = V4L2_MEMORY_MMAP;
754 mutex_unlock(&gofh->lock);
758 static int vidioc_querybuf(struct file *file, void *priv,
759 struct v4l2_buffer *buf)
761 struct go7007_file *gofh = priv;
762 int retval = -EINVAL;
765 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
770 mutex_lock(&gofh->lock);
771 if (index >= gofh->buf_count)
772 goto unlock_and_return;
774 memset(buf, 0, sizeof(*buf));
776 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
778 switch (gofh->bufs[index].state) {
779 case BUF_STATE_QUEUED:
780 buf->flags = V4L2_BUF_FLAG_QUEUED;
783 buf->flags = V4L2_BUF_FLAG_DONE;
789 if (gofh->bufs[index].mapped)
790 buf->flags |= V4L2_BUF_FLAG_MAPPED;
791 buf->memory = V4L2_MEMORY_MMAP;
792 buf->m.offset = index * GO7007_BUF_SIZE;
793 buf->length = GO7007_BUF_SIZE;
794 mutex_unlock(&gofh->lock);
799 mutex_unlock(&gofh->lock);
803 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
805 struct go7007_file *gofh = priv;
806 struct go7007 *go = gofh->go;
807 struct go7007_buffer *gobuf;
809 int retval = -EINVAL;
812 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
813 buf->memory != V4L2_MEMORY_MMAP)
816 mutex_lock(&gofh->lock);
817 if (buf->index < 0 || buf->index >= gofh->buf_count)
818 goto unlock_and_return;
820 gobuf = &gofh->bufs[buf->index];
822 goto unlock_and_return;
825 if (gobuf->state != BUF_STATE_IDLE)
826 goto unlock_and_return;
828 /* offset will be 0 until we really support USERPTR streaming */
829 gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
830 gobuf->bytesused = 0;
831 gobuf->frame_offset = 0;
832 gobuf->modet_active = 0;
833 if (gobuf->offset > 0)
834 gobuf->page_count = GO7007_BUF_PAGES + 1;
836 gobuf->page_count = GO7007_BUF_PAGES;
839 down_read(¤t->mm->mmap_sem);
840 ret = get_user_pages(current, current->mm,
841 gobuf->user_addr & PAGE_MASK, gobuf->page_count,
842 1, 1, gobuf->pages, NULL);
843 up_read(¤t->mm->mmap_sem);
845 if (ret != gobuf->page_count) {
847 for (i = 0; i < ret; ++i)
848 page_cache_release(gobuf->pages[i]);
849 gobuf->page_count = 0;
850 goto unlock_and_return;
853 gobuf->state = BUF_STATE_QUEUED;
854 spin_lock_irqsave(&go->spinlock, flags);
855 list_add_tail(&gobuf->stream, &go->stream);
856 spin_unlock_irqrestore(&go->spinlock, flags);
857 mutex_unlock(&gofh->lock);
862 mutex_unlock(&gofh->lock);
867 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
869 struct go7007_file *gofh = priv;
870 struct go7007 *go = gofh->go;
871 struct go7007_buffer *gobuf;
872 int retval = -EINVAL;
877 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
879 if (buf->memory != V4L2_MEMORY_MMAP)
882 mutex_lock(&gofh->lock);
883 if (list_empty(&go->stream))
884 goto unlock_and_return;
885 gobuf = list_entry(go->stream.next,
886 struct go7007_buffer, stream);
889 if (gobuf->state != BUF_STATE_DONE &&
890 !(file->f_flags & O_NONBLOCK)) {
892 prepare_to_wait(&go->frame_waitq, &wait,
894 if (gobuf->state == BUF_STATE_DONE)
896 if (signal_pending(current)) {
897 retval = -ERESTARTSYS;
902 finish_wait(&go->frame_waitq, &wait);
904 if (gobuf->state != BUF_STATE_DONE)
905 goto unlock_and_return;
907 spin_lock_irqsave(&go->spinlock, flags);
908 deactivate_buffer(gobuf);
909 spin_unlock_irqrestore(&go->spinlock, flags);
910 frame_type_flag = get_frame_type_flag(gobuf, go->format);
911 gobuf->state = BUF_STATE_IDLE;
913 memset(buf, 0, sizeof(*buf));
914 buf->index = gobuf->index;
915 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
916 buf->bytesused = gobuf->bytesused;
917 buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
918 buf->field = V4L2_FIELD_NONE;
919 buf->timestamp = gobuf->timestamp;
920 buf->sequence = gobuf->seq;
921 buf->memory = V4L2_MEMORY_MMAP;
922 buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
923 buf->length = GO7007_BUF_SIZE;
924 buf->reserved = gobuf->modet_active;
926 mutex_unlock(&gofh->lock);
930 mutex_unlock(&gofh->lock);
934 static int vidioc_streamon(struct file *file, void *priv,
935 enum v4l2_buf_type type)
937 struct go7007_file *gofh = priv;
938 struct go7007 *go = gofh->go;
941 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
944 mutex_lock(&gofh->lock);
945 mutex_lock(&go->hw_lock);
947 if (!go->streaming) {
950 go->active_buf = NULL;
951 if (go7007_start_encoder(go) < 0)
956 mutex_unlock(&go->hw_lock);
957 mutex_unlock(&gofh->lock);
962 static int vidioc_streamoff(struct file *file, void *priv,
963 enum v4l2_buf_type type)
965 struct go7007_file *gofh = priv;
966 struct go7007 *go = gofh->go;
968 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
970 mutex_lock(&gofh->lock);
971 go7007_streamoff(go);
972 mutex_unlock(&gofh->lock);
977 static int vidioc_queryctrl(struct file *file, void *priv,
978 struct v4l2_queryctrl *query)
980 struct go7007 *go = ((struct go7007_file *) priv)->go;
983 if (0 == call_all(&go->v4l2_dev, core, queryctrl, query))
987 return mpeg_query_ctrl(query);
990 static int vidioc_g_ctrl(struct file *file, void *priv,
991 struct v4l2_control *ctrl)
993 struct go7007 *go = ((struct go7007_file *) priv)->go;
995 if (0 == call_all(&go->v4l2_dev, core, g_ctrl, ctrl))
998 return mpeg_g_ctrl(ctrl, go);
1001 static int vidioc_s_ctrl(struct file *file, void *priv,
1002 struct v4l2_control *ctrl)
1004 struct go7007 *go = ((struct go7007_file *) priv)->go;
1006 if (0 == call_all(&go->v4l2_dev, core, s_ctrl, ctrl))
1009 return mpeg_s_ctrl(ctrl, go);
1012 static int vidioc_g_parm(struct file *filp, void *priv,
1013 struct v4l2_streamparm *parm)
1015 struct go7007 *go = ((struct go7007_file *) priv)->go;
1016 struct v4l2_fract timeperframe = {
1017 .numerator = 1001 * go->fps_scale,
1018 .denominator = go->sensor_framerate,
1021 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1024 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
1025 parm->parm.capture.timeperframe = timeperframe;
1030 static int vidioc_s_parm(struct file *filp, void *priv,
1031 struct v4l2_streamparm *parm)
1033 struct go7007 *go = ((struct go7007_file *) priv)->go;
1036 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1038 if (parm->parm.capture.capturemode != 0)
1041 n = go->sensor_framerate *
1042 parm->parm.capture.timeperframe.numerator;
1043 d = 1001 * parm->parm.capture.timeperframe.denominator;
1044 if (n != 0 && d != 0 && n > d)
1045 go->fps_scale = (n + d/2) / d;
1052 /* VIDIOC_ENUMSTD on go7007 were used for enumerating the supported fps and
1053 its resolution, when the device is not connected to TV.
1054 This is were an API abuse, probably used by the lack of specific IOCTL's to
1055 enumerate it, by the time the driver was written.
1057 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
1058 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
1060 The two functions below implement the newer ioctls
1062 static int vidioc_enum_framesizes(struct file *filp, void *priv,
1063 struct v4l2_frmsizeenum *fsize)
1065 struct go7007 *go = ((struct go7007_file *) priv)->go;
1067 /* Return -EINVAL, if it is a TV board */
1068 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1069 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1072 if (fsize->index > 0)
1075 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1076 fsize->discrete.width = go->board_info->sensor_width;
1077 fsize->discrete.height = go->board_info->sensor_height;
1082 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1083 struct v4l2_frmivalenum *fival)
1085 struct go7007 *go = ((struct go7007_file *) priv)->go;
1087 /* Return -EINVAL, if it is a TV board */
1088 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1089 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1092 if (fival->index > 0)
1095 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1096 fival->discrete.numerator = 1001;
1097 fival->discrete.denominator = go->board_info->sensor_framerate;
1102 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
1104 struct go7007 *go = ((struct go7007_file *) priv)->go;
1106 switch (go->standard) {
1107 case GO7007_STD_NTSC:
1108 *std = V4L2_STD_NTSC;
1110 case GO7007_STD_PAL:
1111 *std = V4L2_STD_PAL;
1120 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
1122 struct go7007 *go = ((struct go7007_file *) priv)->go;
1127 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) && *std != 0)
1133 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1134 go->input == go->board_info->num_inputs - 1) {
1135 if (!go->i2c_adapter_online)
1137 if (call_all(&go->v4l2_dev, core, s_std, *std) < 0)
1141 if (*std & V4L2_STD_NTSC) {
1142 go->standard = GO7007_STD_NTSC;
1143 go->sensor_framerate = 30000;
1144 } else if (*std & V4L2_STD_PAL) {
1145 go->standard = GO7007_STD_PAL;
1146 go->sensor_framerate = 25025;
1147 } else if (*std & V4L2_STD_SECAM) {
1148 go->standard = GO7007_STD_PAL;
1149 go->sensor_framerate = 25025;
1153 call_all(&go->v4l2_dev, core, s_std, *std);
1154 set_capture_size(go, NULL, 0);
1159 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
1161 struct go7007 *go = ((struct go7007_file *) priv)->go;
1163 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1164 go->input == go->board_info->num_inputs - 1) {
1165 if (!go->i2c_adapter_online)
1167 return call_all(&go->v4l2_dev, video, querystd, std);
1168 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1169 *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
1176 static int vidioc_enum_input(struct file *file, void *priv,
1177 struct v4l2_input *inp)
1179 struct go7007 *go = ((struct go7007_file *) priv)->go;
1181 if (inp->index >= go->board_info->num_inputs)
1184 strncpy(inp->name, go->board_info->inputs[inp->index].name,
1187 /* If this board has a tuner, it will be the last input */
1188 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1189 inp->index == go->board_info->num_inputs - 1)
1190 inp->type = V4L2_INPUT_TYPE_TUNER;
1192 inp->type = V4L2_INPUT_TYPE_CAMERA;
1196 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1197 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
1206 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
1208 struct go7007 *go = ((struct go7007_file *) priv)->go;
1215 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
1217 struct go7007 *go = ((struct go7007_file *) priv)->go;
1219 if (input >= go->board_info->num_inputs)
1226 return call_all(&go->v4l2_dev, video, s_routing, input, 0, 0);
1229 static int vidioc_g_tuner(struct file *file, void *priv,
1230 struct v4l2_tuner *t)
1232 struct go7007 *go = ((struct go7007_file *) priv)->go;
1234 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1238 if (!go->i2c_adapter_online)
1241 return call_all(&go->v4l2_dev, tuner, g_tuner, t);
1244 static int vidioc_s_tuner(struct file *file, void *priv,
1245 struct v4l2_tuner *t)
1247 struct go7007 *go = ((struct go7007_file *) priv)->go;
1249 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1253 if (!go->i2c_adapter_online)
1256 switch (go->board_id) {
1257 case GO7007_BOARDID_PX_TV402U_NA:
1258 case GO7007_BOARDID_PX_TV402U_JP:
1259 /* No selectable options currently */
1260 if (t->audmode != V4L2_TUNER_MODE_STEREO)
1265 return call_all(&go->v4l2_dev, tuner, s_tuner, t);
1268 static int vidioc_g_frequency(struct file *file, void *priv,
1269 struct v4l2_frequency *f)
1271 struct go7007 *go = ((struct go7007_file *) priv)->go;
1273 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1275 if (!go->i2c_adapter_online)
1278 f->type = V4L2_TUNER_ANALOG_TV;
1280 return call_all(&go->v4l2_dev, tuner, g_frequency, f);
1283 static int vidioc_s_frequency(struct file *file, void *priv,
1284 struct v4l2_frequency *f)
1286 struct go7007 *go = ((struct go7007_file *) priv)->go;
1288 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1290 if (!go->i2c_adapter_online)
1293 return call_all(&go->v4l2_dev, tuner, s_frequency, f);
1296 static int vidioc_cropcap(struct file *file, void *priv,
1297 struct v4l2_cropcap *cropcap)
1299 struct go7007 *go = ((struct go7007_file *) priv)->go;
1301 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1304 /* These specify the raw input of the sensor */
1305 switch (go->standard) {
1306 case GO7007_STD_NTSC:
1307 cropcap->bounds.top = 0;
1308 cropcap->bounds.left = 0;
1309 cropcap->bounds.width = 720;
1310 cropcap->bounds.height = 480;
1311 cropcap->defrect.top = 0;
1312 cropcap->defrect.left = 0;
1313 cropcap->defrect.width = 720;
1314 cropcap->defrect.height = 480;
1316 case GO7007_STD_PAL:
1317 cropcap->bounds.top = 0;
1318 cropcap->bounds.left = 0;
1319 cropcap->bounds.width = 720;
1320 cropcap->bounds.height = 576;
1321 cropcap->defrect.top = 0;
1322 cropcap->defrect.left = 0;
1323 cropcap->defrect.width = 720;
1324 cropcap->defrect.height = 576;
1326 case GO7007_STD_OTHER:
1327 cropcap->bounds.top = 0;
1328 cropcap->bounds.left = 0;
1329 cropcap->bounds.width = go->board_info->sensor_width;
1330 cropcap->bounds.height = go->board_info->sensor_height;
1331 cropcap->defrect.top = 0;
1332 cropcap->defrect.left = 0;
1333 cropcap->defrect.width = go->board_info->sensor_width;
1334 cropcap->defrect.height = go->board_info->sensor_height;
1341 static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1343 struct go7007 *go = ((struct go7007_file *) priv)->go;
1345 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1348 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1350 /* These specify the raw input of the sensor */
1351 switch (go->standard) {
1352 case GO7007_STD_NTSC:
1355 crop->c.width = 720;
1356 crop->c.height = 480;
1358 case GO7007_STD_PAL:
1361 crop->c.width = 720;
1362 crop->c.height = 576;
1364 case GO7007_STD_OTHER:
1367 crop->c.width = go->board_info->sensor_width;
1368 crop->c.height = go->board_info->sensor_height;
1375 /* FIXME: vidioc_s_crop is not really implemented!!!
1377 static int vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1379 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1385 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1386 struct v4l2_jpegcompression *params)
1388 memset(params, 0, sizeof(*params));
1389 params->quality = 50; /* ?? */
1390 params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
1391 V4L2_JPEG_MARKER_DQT;
1396 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1397 struct v4l2_jpegcompression *params)
1399 if (params->quality != 50 ||
1400 params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
1401 V4L2_JPEG_MARKER_DQT))
1408 Those ioctls are private, and not needed, since several standard
1409 extended controls already provide streaming control.
1410 So, those ioctls should be converted into vidioc_g_ext_ctrls()
1411 and vidioc_s_ext_ctrls()
1415 /* Temporary ioctls for controlling compression characteristics */
1416 case GO7007IOC_S_BITRATE:
1422 /* Upper bound is kind of arbitrary here */
1423 if (*bitrate < 64000 || *bitrate > 10000000)
1425 go->bitrate = *bitrate;
1428 case GO7007IOC_G_BITRATE:
1432 *bitrate = go->bitrate;
1435 case GO7007IOC_S_COMP_PARAMS:
1437 struct go7007_comp_params *comp = arg;
1439 if (go->format == GO7007_FORMAT_MJPEG)
1441 if (comp->gop_size > 0)
1442 go->gop_size = comp->gop_size;
1444 go->gop_size = go->sensor_framerate / 1000;
1445 if (go->gop_size != 15)
1447 /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
1448 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
1449 switch (comp->aspect_ratio) {
1450 case GO7007_ASPECT_RATIO_4_3_NTSC:
1451 case GO7007_ASPECT_RATIO_4_3_PAL:
1452 go->aspect_ratio = GO7007_RATIO_4_3;
1454 case GO7007_ASPECT_RATIO_16_9_NTSC:
1455 case GO7007_ASPECT_RATIO_16_9_PAL:
1456 go->aspect_ratio = GO7007_RATIO_16_9;
1459 go->aspect_ratio = GO7007_RATIO_1_1;
1463 if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
1465 go->seq_header_enable = 0;
1467 go->seq_header_enable = 1;
1471 case GO7007IOC_G_COMP_PARAMS:
1473 struct go7007_comp_params *comp = arg;
1475 if (go->format == GO7007_FORMAT_MJPEG)
1477 memset(comp, 0, sizeof(*comp));
1478 comp->gop_size = go->gop_size;
1479 comp->max_b_frames = go->ipb ? 2 : 0;
1480 switch (go->aspect_ratio) {
1481 case GO7007_RATIO_4_3:
1482 if (go->standard == GO7007_STD_NTSC)
1483 comp->aspect_ratio =
1484 GO7007_ASPECT_RATIO_4_3_NTSC;
1486 comp->aspect_ratio =
1487 GO7007_ASPECT_RATIO_4_3_PAL;
1489 case GO7007_RATIO_16_9:
1490 if (go->standard == GO7007_STD_NTSC)
1491 comp->aspect_ratio =
1492 GO7007_ASPECT_RATIO_16_9_NTSC;
1494 comp->aspect_ratio =
1495 GO7007_ASPECT_RATIO_16_9_PAL;
1498 comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
1502 comp->flags |= GO7007_COMP_CLOSED_GOP;
1503 if (!go->seq_header_enable)
1504 comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
1507 case GO7007IOC_S_MPEG_PARAMS:
1509 struct go7007_mpeg_params *mpeg = arg;
1511 if (go->format != GO7007_FORMAT_MPEG1 &&
1512 go->format != GO7007_FORMAT_MPEG2 &&
1513 go->format != GO7007_FORMAT_MPEG4)
1516 if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
1517 go->format = GO7007_FORMAT_MPEG2;
1518 go->bitrate = 9800000;
1522 go->repeat_seqhead = 0;
1523 go->seq_header_enable = 1;
1524 go->gop_header_enable = 1;
1527 switch (mpeg->mpeg_video_standard) {
1528 case GO7007_MPEG_VIDEO_MPEG1:
1529 go->format = GO7007_FORMAT_MPEG1;
1532 case GO7007_MPEG_VIDEO_MPEG2:
1533 go->format = GO7007_FORMAT_MPEG2;
1534 if (mpeg->pali >> 24 == 2)
1535 go->pali = mpeg->pali & 0xff;
1539 case GO7007_MPEG_VIDEO_MPEG4:
1540 go->format = GO7007_FORMAT_MPEG4;
1541 if (mpeg->pali >> 24 == 4)
1542 go->pali = mpeg->pali & 0xff;
1549 go->gop_header_enable =
1550 mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
1552 if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
1553 go->repeat_seqhead = 1;
1555 go->repeat_seqhead = 0;
1560 case GO7007IOC_G_MPEG_PARAMS:
1562 struct go7007_mpeg_params *mpeg = arg;
1564 memset(mpeg, 0, sizeof(*mpeg));
1565 switch (go->format) {
1566 case GO7007_FORMAT_MPEG1:
1567 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
1570 case GO7007_FORMAT_MPEG2:
1571 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
1572 mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
1574 case GO7007_FORMAT_MPEG4:
1575 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
1576 mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
1581 if (!go->gop_header_enable)
1582 mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
1583 if (go->repeat_seqhead)
1584 mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
1586 mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
1589 case GO7007IOC_S_MD_PARAMS:
1591 struct go7007_md_params *mdp = arg;
1593 if (mdp->region > 3)
1595 if (mdp->trigger > 0) {
1596 go->modet[mdp->region].pixel_threshold =
1597 mdp->pixel_threshold >> 1;
1598 go->modet[mdp->region].motion_threshold =
1599 mdp->motion_threshold >> 1;
1600 go->modet[mdp->region].mb_threshold =
1602 go->modet[mdp->region].enable = 1;
1604 go->modet[mdp->region].enable = 0;
1607 case GO7007IOC_G_MD_PARAMS:
1609 struct go7007_md_params *mdp = arg;
1610 int region = mdp->region;
1612 if (mdp->region > 3)
1614 memset(mdp, 0, sizeof(struct go7007_md_params));
1615 mdp->region = region;
1616 if (!go->modet[region].enable)
1618 mdp->pixel_threshold =
1619 (go->modet[region].pixel_threshold << 1) + 1;
1620 mdp->motion_threshold =
1621 (go->modet[region].motion_threshold << 1) + 1;
1623 (go->modet[region].mb_threshold << 1) + 1;
1626 case GO7007IOC_S_MD_REGION:
1628 struct go7007_md_region *region = arg;
1630 if (region->region < 1 || region->region > 3)
1632 return clip_to_modet_map(go, region->region, region->clips);
1636 static ssize_t go7007_read(struct file *file, char __user *data,
1637 size_t count, loff_t *ppos)
1642 static void go7007_vm_open(struct vm_area_struct *vma)
1644 struct go7007_buffer *gobuf = vma->vm_private_data;
1649 static void go7007_vm_close(struct vm_area_struct *vma)
1651 struct go7007_buffer *gobuf = vma->vm_private_data;
1652 unsigned long flags;
1654 if (--gobuf->mapped == 0) {
1655 spin_lock_irqsave(&gobuf->go->spinlock, flags);
1656 deactivate_buffer(gobuf);
1657 spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
1661 /* Copied from videobuf-dma-sg.c */
1662 static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1666 page = alloc_page(GFP_USER | __GFP_DMA32);
1668 return VM_FAULT_OOM;
1669 clear_user_highpage(page, (unsigned long)vmf->virtual_address);
1674 static struct vm_operations_struct go7007_vm_ops = {
1675 .open = go7007_vm_open,
1676 .close = go7007_vm_close,
1677 .fault = go7007_vm_fault,
1680 static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
1682 struct go7007_file *gofh = file->private_data;
1685 if (gofh->go->status != STATUS_ONLINE)
1687 if (!(vma->vm_flags & VM_SHARED))
1688 return -EINVAL; /* only support VM_SHARED mapping */
1689 if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
1690 return -EINVAL; /* must map exactly one full buffer */
1691 mutex_lock(&gofh->lock);
1692 index = vma->vm_pgoff / GO7007_BUF_PAGES;
1693 if (index >= gofh->buf_count) {
1694 mutex_unlock(&gofh->lock);
1695 return -EINVAL; /* trying to map beyond requested buffers */
1697 if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
1698 mutex_unlock(&gofh->lock);
1699 return -EINVAL; /* offset is not aligned on buffer boundary */
1701 if (gofh->bufs[index].mapped > 0) {
1702 mutex_unlock(&gofh->lock);
1705 gofh->bufs[index].mapped = 1;
1706 gofh->bufs[index].user_addr = vma->vm_start;
1707 vma->vm_ops = &go7007_vm_ops;
1708 vma->vm_flags |= VM_DONTEXPAND;
1709 vma->vm_flags &= ~VM_IO;
1710 vma->vm_private_data = &gofh->bufs[index];
1711 mutex_unlock(&gofh->lock);
1715 static unsigned int go7007_poll(struct file *file, poll_table *wait)
1717 struct go7007_file *gofh = file->private_data;
1718 struct go7007_buffer *gobuf;
1720 if (list_empty(&gofh->go->stream))
1722 gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
1723 poll_wait(file, &gofh->go->frame_waitq, wait);
1724 if (gobuf->state == BUF_STATE_DONE)
1725 return POLLIN | POLLRDNORM;
1729 static void go7007_vfl_release(struct video_device *vfd)
1731 struct go7007 *go = video_get_drvdata(vfd);
1733 video_device_release(vfd);
1734 if (--go->ref_count == 0)
1738 static struct v4l2_file_operations go7007_fops = {
1739 .owner = THIS_MODULE,
1740 .open = go7007_open,
1741 .release = go7007_release,
1742 .ioctl = video_ioctl2,
1743 .read = go7007_read,
1744 .mmap = go7007_mmap,
1745 .poll = go7007_poll,
1748 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1749 .vidioc_querycap = vidioc_querycap,
1750 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1751 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1752 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1753 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1754 .vidioc_reqbufs = vidioc_reqbufs,
1755 .vidioc_querybuf = vidioc_querybuf,
1756 .vidioc_qbuf = vidioc_qbuf,
1757 .vidioc_dqbuf = vidioc_dqbuf,
1758 .vidioc_g_std = vidioc_g_std,
1759 .vidioc_s_std = vidioc_s_std,
1760 .vidioc_querystd = vidioc_querystd,
1761 .vidioc_enum_input = vidioc_enum_input,
1762 .vidioc_g_input = vidioc_g_input,
1763 .vidioc_s_input = vidioc_s_input,
1764 .vidioc_queryctrl = vidioc_queryctrl,
1765 .vidioc_g_ctrl = vidioc_g_ctrl,
1766 .vidioc_s_ctrl = vidioc_s_ctrl,
1767 .vidioc_streamon = vidioc_streamon,
1768 .vidioc_streamoff = vidioc_streamoff,
1769 .vidioc_g_tuner = vidioc_g_tuner,
1770 .vidioc_s_tuner = vidioc_s_tuner,
1771 .vidioc_g_frequency = vidioc_g_frequency,
1772 .vidioc_s_frequency = vidioc_s_frequency,
1773 .vidioc_g_parm = vidioc_g_parm,
1774 .vidioc_s_parm = vidioc_s_parm,
1775 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1776 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1777 .vidioc_cropcap = vidioc_cropcap,
1778 .vidioc_g_crop = vidioc_g_crop,
1779 .vidioc_s_crop = vidioc_s_crop,
1780 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1781 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1784 static struct video_device go7007_template = {
1786 .fops = &go7007_fops,
1787 .release = go7007_vfl_release,
1788 .ioctl_ops = &video_ioctl_ops,
1789 .tvnorms = V4L2_STD_ALL,
1790 .current_norm = V4L2_STD_NTSC,
1793 int go7007_v4l2_init(struct go7007 *go)
1797 go->video_dev = video_device_alloc();
1798 if (go->video_dev == NULL)
1800 *go->video_dev = go7007_template;
1801 go->video_dev->parent = go->dev;
1802 rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
1804 video_device_release(go->video_dev);
1805 go->video_dev = NULL;
1808 rv = v4l2_device_register(go->dev, &go->v4l2_dev);
1810 video_device_release(go->video_dev);
1811 go->video_dev = NULL;
1814 video_set_drvdata(go->video_dev, go);
1816 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1817 go->video_dev->name, video_device_node_name(go->video_dev));
1822 void go7007_v4l2_remove(struct go7007 *go)
1824 unsigned long flags;
1826 mutex_lock(&go->hw_lock);
1827 if (go->streaming) {
1829 go7007_stream_stop(go);
1830 spin_lock_irqsave(&go->spinlock, flags);
1832 spin_unlock_irqrestore(&go->spinlock, flags);
1834 mutex_unlock(&go->hw_lock);
1836 video_unregister_device(go->video_dev);
1837 v4l2_device_unregister(&go->v4l2_dev);