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>
25 #include <linux/unistd.h>
26 #include <linux/time.h>
27 #include <linux/vmalloc.h>
28 #include <linux/pagemap.h>
29 #include <linux/videodev2.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-ioctl.h>
32 #include <linux/i2c.h>
33 #include <linux/semaphore.h>
34 #include <linux/uaccess.h>
35 #include <asm/system.h>
38 #include "go7007-priv.h"
41 /* Temporary defines until accepted in v4l-dvb */
42 #ifndef V4L2_MPEG_STREAM_TYPE_MPEG_ELEM
43 #define V4L2_MPEG_STREAM_TYPE_MPEG_ELEM 6 /* MPEG elementary stream */
45 #ifndef V4L2_MPEG_VIDEO_ENCODING_MPEG_4
46 #define V4L2_MPEG_VIDEO_ENCODING_MPEG_4 3
49 static void deactivate_buffer(struct go7007_buffer *gobuf)
53 if (gobuf->state != BUF_STATE_IDLE) {
54 list_del(&gobuf->stream);
55 gobuf->state = BUF_STATE_IDLE;
57 if (gobuf->page_count > 0) {
58 for (i = 0; i < gobuf->page_count; ++i)
59 page_cache_release(gobuf->pages[i]);
60 gobuf->page_count = 0;
64 static void abort_queued(struct go7007 *go)
66 struct go7007_buffer *gobuf, *next;
68 list_for_each_entry_safe(gobuf, next, &go->stream, stream) {
69 deactivate_buffer(gobuf);
73 static int go7007_streamoff(struct go7007 *go)
81 go7007_stream_stop(go);
82 spin_lock_irqsave(&go->spinlock, flags);
84 spin_unlock_irqrestore(&go->spinlock, flags);
85 go7007_reset_encoder(go);
92 static int go7007_open(struct file *file)
94 struct go7007 *go = video_get_drvdata(video_devdata(file));
95 struct go7007_file *gofh;
97 if (go->status != STATUS_ONLINE)
99 gofh = kmalloc(sizeof(struct go7007_file), GFP_KERNEL);
104 init_MUTEX(&gofh->lock);
106 file->private_data = gofh;
110 static int go7007_release(struct file *file)
112 struct go7007_file *gofh = file->private_data;
113 struct go7007 *go = gofh->go;
115 if (gofh->buf_count > 0) {
116 go7007_streamoff(go);
122 if (--go->ref_count == 0)
124 file->private_data = NULL;
128 static u32 get_frame_type_flag(struct go7007_buffer *gobuf, int format)
130 u8 *f = page_address(gobuf->pages[0]);
133 case GO7007_FORMAT_MJPEG:
134 return V4L2_BUF_FLAG_KEYFRAME;
135 case GO7007_FORMAT_MPEG4:
136 switch ((f[gobuf->frame_offset + 4] >> 6) & 0x3) {
138 return V4L2_BUF_FLAG_KEYFRAME;
140 return V4L2_BUF_FLAG_PFRAME;
142 return V4L2_BUF_FLAG_BFRAME;
146 case GO7007_FORMAT_MPEG1:
147 case GO7007_FORMAT_MPEG2:
148 switch ((f[gobuf->frame_offset + 5] >> 3) & 0x7) {
150 return V4L2_BUF_FLAG_KEYFRAME;
152 return V4L2_BUF_FLAG_PFRAME;
154 return V4L2_BUF_FLAG_BFRAME;
163 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
165 int sensor_height = 0, sensor_width = 0;
166 int width, height, i;
168 if (fmt != NULL && fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
169 fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG &&
170 fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG4)
173 switch (go->standard) {
174 case GO7007_STD_NTSC:
182 case GO7007_STD_OTHER:
183 sensor_width = go->board_info->sensor_width;
184 sensor_height = go->board_info->sensor_height;
189 width = sensor_width;
190 height = sensor_height;
191 } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
192 if (fmt->fmt.pix.width > sensor_width)
193 width = sensor_width;
194 else if (fmt->fmt.pix.width < 144)
197 width = fmt->fmt.pix.width & ~0x0f;
199 if (fmt->fmt.pix.height > sensor_height)
200 height = sensor_height;
201 else if (fmt->fmt.pix.height < 96)
204 height = fmt->fmt.pix.height & ~0x0f;
206 int requested_size = fmt->fmt.pix.width * fmt->fmt.pix.height;
207 int sensor_size = sensor_width * sensor_height;
209 if (64 * requested_size < 9 * sensor_size) {
210 width = sensor_width / 4;
211 height = sensor_height / 4;
212 } else if (64 * requested_size < 36 * sensor_size) {
213 width = sensor_width / 2;
214 height = sensor_height / 2;
216 width = sensor_width;
217 height = sensor_height;
224 u32 pixelformat = fmt->fmt.pix.pixelformat;
226 memset(fmt, 0, sizeof(*fmt));
227 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
228 fmt->fmt.pix.width = width;
229 fmt->fmt.pix.height = height;
230 fmt->fmt.pix.pixelformat = pixelformat;
231 fmt->fmt.pix.field = V4L2_FIELD_NONE;
232 fmt->fmt.pix.bytesperline = 0;
233 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
234 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
242 go->encoder_h_offset = go->board_info->sensor_h_offset;
243 go->encoder_v_offset = go->board_info->sensor_v_offset;
244 for (i = 0; i < 4; ++i)
245 go->modet[i].enable = 0;
246 for (i = 0; i < 1624; ++i)
247 go->modet_map[i] = 0;
249 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
250 struct video_decoder_resolution res;
253 if (height > sensor_height / 2) {
254 res.height = height / 2;
255 go->encoder_v_halve = 0;
258 go->encoder_v_halve = 1;
260 if (go->i2c_adapter_online)
261 i2c_clients_command(&go->i2c_adapter,
262 DECODER_SET_RESOLUTION, &res);
264 if (width <= sensor_width / 4) {
265 go->encoder_h_halve = 1;
266 go->encoder_v_halve = 1;
267 go->encoder_subsample = 1;
268 } else if (width <= sensor_width / 2) {
269 go->encoder_h_halve = 1;
270 go->encoder_v_halve = 1;
271 go->encoder_subsample = 0;
273 go->encoder_h_halve = 0;
274 go->encoder_v_halve = 0;
275 go->encoder_subsample = 0;
282 switch (fmt->fmt.pix.pixelformat) {
283 case V4L2_PIX_FMT_MPEG:
284 if (go->format == GO7007_FORMAT_MPEG1 ||
285 go->format == GO7007_FORMAT_MPEG2 ||
286 go->format == GO7007_FORMAT_MPEG4)
288 go->format = GO7007_FORMAT_MPEG1;
290 go->aspect_ratio = GO7007_RATIO_1_1;
291 go->gop_size = go->sensor_framerate / 1000;
294 go->repeat_seqhead = 1;
295 go->seq_header_enable = 1;
296 go->gop_header_enable = 1;
299 /* Backwards compatibility only! */
300 case V4L2_PIX_FMT_MPEG4:
301 if (go->format == GO7007_FORMAT_MPEG4)
303 go->format = GO7007_FORMAT_MPEG4;
305 go->aspect_ratio = GO7007_RATIO_1_1;
306 go->gop_size = go->sensor_framerate / 1000;
309 go->repeat_seqhead = 1;
310 go->seq_header_enable = 1;
311 go->gop_header_enable = 1;
314 case V4L2_PIX_FMT_MJPEG:
315 go->format = GO7007_FORMAT_MJPEG;
317 go->aspect_ratio = GO7007_RATIO_1_1;
321 go->repeat_seqhead = 0;
322 go->seq_header_enable = 0;
323 go->gop_header_enable = 0;
330 static int clip_to_modet_map(struct go7007 *go, int region,
331 struct v4l2_clip *clip_list)
333 struct v4l2_clip clip, *clip_ptr;
336 /* Check if coordinates are OK and if any macroblocks are already
337 * used by other regions (besides 0) */
338 clip_ptr = clip_list;
340 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
342 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
343 clip.c.width <= 0 || (clip.c.width & 0xF))
345 if (clip.c.left + clip.c.width > go->width)
347 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
348 clip.c.height <= 0 || (clip.c.height & 0xF))
350 if (clip.c.top + clip.c.height > go->height)
352 for (y = 0; y < clip.c.height; y += 16)
353 for (x = 0; x < clip.c.width; x += 16) {
354 mbnum = (go->width >> 4) *
355 ((clip.c.top + y) >> 4) +
356 ((clip.c.left + x) >> 4);
357 if (go->modet_map[mbnum] != 0 &&
358 go->modet_map[mbnum] != region)
361 clip_ptr = clip.next;
364 /* Clear old region macroblocks */
365 for (mbnum = 0; mbnum < 1624; ++mbnum)
366 if (go->modet_map[mbnum] == region)
367 go->modet_map[mbnum] = 0;
369 /* Claim macroblocks in this list */
370 clip_ptr = clip_list;
372 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
374 for (y = 0; y < clip.c.height; y += 16)
375 for (x = 0; x < clip.c.width; x += 16) {
376 mbnum = (go->width >> 4) *
377 ((clip.c.top + y) >> 4) +
378 ((clip.c.left + x) >> 4);
379 go->modet_map[mbnum] = region;
381 clip_ptr = clip.next;
386 static int mpeg_queryctrl(u32 id, struct v4l2_queryctrl *ctrl)
388 static const u32 user_ctrls[] = {
392 static const u32 mpeg_ctrls[] = {
394 V4L2_CID_MPEG_STREAM_TYPE,
395 V4L2_CID_MPEG_VIDEO_ENCODING,
396 V4L2_CID_MPEG_VIDEO_ASPECT,
397 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
398 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
399 V4L2_CID_MPEG_VIDEO_BITRATE,
402 static const u32 *ctrl_classes[] = {
408 /* The ctrl may already contain the queried i2c controls,
409 * query the mpeg controls if the existing ctrl id is
410 * greater than the next mpeg ctrl id.
412 id = v4l2_ctrl_next(ctrl_classes, id);
413 if (id >= ctrl->id && ctrl->name[0])
416 memset(ctrl, 0, sizeof(*ctrl));
420 case V4L2_CID_USER_CLASS:
421 case V4L2_CID_MPEG_CLASS:
422 return v4l2_ctrl_query_fill_std(ctrl);
423 case V4L2_CID_MPEG_STREAM_TYPE:
424 return v4l2_ctrl_query_fill(ctrl,
425 V4L2_MPEG_STREAM_TYPE_MPEG2_DVD,
426 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM, 1,
427 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM);
428 case V4L2_CID_MPEG_VIDEO_ENCODING:
429 return v4l2_ctrl_query_fill(ctrl,
430 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
431 V4L2_MPEG_VIDEO_ENCODING_MPEG_4, 1,
432 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
433 case V4L2_CID_MPEG_VIDEO_ASPECT:
434 return v4l2_ctrl_query_fill(ctrl,
435 V4L2_MPEG_VIDEO_ASPECT_1x1,
436 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
437 V4L2_MPEG_VIDEO_ASPECT_1x1);
438 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
439 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
440 return v4l2_ctrl_query_fill_std(ctrl);
441 case V4L2_CID_MPEG_VIDEO_BITRATE:
442 return v4l2_ctrl_query_fill(ctrl,
452 static int mpeg_s_control(struct v4l2_control *ctrl, struct go7007 *go)
454 /* pretty sure we can't change any of these while streaming */
459 case V4L2_CID_MPEG_STREAM_TYPE:
460 switch (ctrl->value) {
461 case V4L2_MPEG_STREAM_TYPE_MPEG2_DVD:
462 go->format = GO7007_FORMAT_MPEG2;
463 go->bitrate = 9800000;
467 go->repeat_seqhead = 0;
468 go->seq_header_enable = 1;
469 go->gop_header_enable = 1;
472 case V4L2_MPEG_STREAM_TYPE_MPEG_ELEM:
479 case V4L2_CID_MPEG_VIDEO_ENCODING:
480 switch (ctrl->value) {
481 case V4L2_MPEG_VIDEO_ENCODING_MPEG_1:
482 go->format = GO7007_FORMAT_MPEG1;
485 case V4L2_MPEG_VIDEO_ENCODING_MPEG_2:
486 go->format = GO7007_FORMAT_MPEG2;
487 /*if (mpeg->pali >> 24 == 2)
488 go->pali = mpeg->pali & 0xff;
492 case V4L2_MPEG_VIDEO_ENCODING_MPEG_4:
493 go->format = GO7007_FORMAT_MPEG4;
494 /*if (mpeg->pali >> 24 == 4)
495 go->pali = mpeg->pali & 0xff;
502 go->gop_header_enable =
503 /*mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
505 /*if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
506 go->repeat_seqhead = 1;
508 go->repeat_seqhead = 0;
511 case V4L2_CID_MPEG_VIDEO_ASPECT:
512 if (go->format == GO7007_FORMAT_MJPEG)
514 switch (ctrl->value) {
515 case V4L2_MPEG_VIDEO_ASPECT_1x1:
516 go->aspect_ratio = GO7007_RATIO_1_1;
518 case V4L2_MPEG_VIDEO_ASPECT_4x3:
519 go->aspect_ratio = GO7007_RATIO_4_3;
521 case V4L2_MPEG_VIDEO_ASPECT_16x9:
522 go->aspect_ratio = GO7007_RATIO_16_9;
524 case V4L2_MPEG_VIDEO_ASPECT_221x100:
529 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
530 go->gop_size = ctrl->value;
532 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
533 if (ctrl->value != 0 && ctrl->value != 1)
535 go->closed_gop = ctrl->value;
537 case V4L2_CID_MPEG_VIDEO_BITRATE:
538 /* Upper bound is kind of arbitrary here */
539 if (ctrl->value < 64000 || ctrl->value > 10000000)
541 go->bitrate = ctrl->value;
549 static int mpeg_g_control(struct v4l2_control *ctrl, struct go7007 *go)
552 case V4L2_CID_MPEG_STREAM_TYPE:
554 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
556 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG_ELEM;
558 case V4L2_CID_MPEG_VIDEO_ENCODING:
559 switch (go->format) {
560 case GO7007_FORMAT_MPEG1:
561 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
563 case GO7007_FORMAT_MPEG2:
564 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
566 case GO7007_FORMAT_MPEG4:
567 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4;
573 case V4L2_CID_MPEG_VIDEO_ASPECT:
574 switch (go->aspect_ratio) {
575 case GO7007_RATIO_1_1:
576 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_1x1;
578 case GO7007_RATIO_4_3:
579 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_4x3;
581 case GO7007_RATIO_16_9:
582 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_16x9;
588 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
589 ctrl->value = go->gop_size;
591 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
592 ctrl->value = go->closed_gop;
594 case V4L2_CID_MPEG_VIDEO_BITRATE:
595 ctrl->value = go->bitrate;
603 static int vidioc_querycap(struct file *file, void *priv,
604 struct v4l2_capability *cap)
606 struct go7007_file *gofh = priv;
607 struct go7007 *go = gofh->go;
609 strlcpy(cap->driver, "go7007", sizeof(cap->driver));
610 strlcpy(cap->card, go->name, sizeof(cap->card));
612 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
615 cap->version = KERNEL_VERSION(0, 9, 8);
617 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
618 V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
620 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
621 cap->capabilities |= V4L2_CAP_TUNER;
626 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
627 struct v4l2_fmtdesc *fmt)
631 switch (fmt->index) {
633 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
634 desc = "Motion-JPEG";
637 fmt->pixelformat = V4L2_PIX_FMT_MPEG;
638 desc = "MPEG1/MPEG2/MPEG4";
643 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
644 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
646 strncpy(fmt->description, desc, sizeof(fmt->description));
651 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
652 struct v4l2_format *fmt)
654 struct go7007_file *gofh = priv;
655 struct go7007 *go = gofh->go;
657 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
658 fmt->fmt.pix.width = go->width;
659 fmt->fmt.pix.height = go->height;
660 fmt->fmt.pix.pixelformat = (go->format == GO7007_FORMAT_MJPEG) ?
661 V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
662 fmt->fmt.pix.field = V4L2_FIELD_NONE;
663 fmt->fmt.pix.bytesperline = 0;
664 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
665 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
670 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
671 struct v4l2_format *fmt)
673 struct go7007_file *gofh = priv;
674 struct go7007 *go = gofh->go;
676 return set_capture_size(go, fmt, 1);
679 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
680 struct v4l2_format *fmt)
682 struct go7007_file *gofh = priv;
683 struct go7007 *go = gofh->go;
688 return set_capture_size(go, fmt, 0);
691 static int vidioc_reqbufs(struct file *file, void *priv,
692 struct v4l2_requestbuffers *req)
694 struct go7007_file *gofh = priv;
695 struct go7007 *go = gofh->go;
697 unsigned int count, i;
702 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
703 req->memory != V4L2_MEMORY_MMAP)
707 for (i = 0; i < gofh->buf_count; ++i)
708 if (gofh->bufs[i].mapped > 0)
709 goto unlock_and_return;
712 if (go->in_use > 0 && gofh->buf_count == 0) {
714 goto unlock_and_return;
717 if (gofh->buf_count > 0)
728 gofh->bufs = kmalloc(count * sizeof(struct go7007_buffer),
733 goto unlock_and_return;
736 memset(gofh->bufs, 0, count * sizeof(struct go7007_buffer));
738 for (i = 0; i < count; ++i) {
739 gofh->bufs[i].go = go;
740 gofh->bufs[i].index = i;
741 gofh->bufs[i].state = BUF_STATE_IDLE;
742 gofh->bufs[i].mapped = 0;
750 gofh->buf_count = count;
754 memset(req, 0, sizeof(*req));
757 req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
758 req->memory = V4L2_MEMORY_MMAP;
767 static int vidioc_querybuf(struct file *file, void *priv,
768 struct v4l2_buffer *buf)
770 struct go7007_file *gofh = priv;
771 int retval = -EINVAL;
774 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
780 if (index >= gofh->buf_count)
781 goto unlock_and_return;
783 memset(buf, 0, sizeof(*buf));
785 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
787 switch (gofh->bufs[index].state) {
788 case BUF_STATE_QUEUED:
789 buf->flags = V4L2_BUF_FLAG_QUEUED;
792 buf->flags = V4L2_BUF_FLAG_DONE;
798 if (gofh->bufs[index].mapped)
799 buf->flags |= V4L2_BUF_FLAG_MAPPED;
800 buf->memory = V4L2_MEMORY_MMAP;
801 buf->m.offset = index * GO7007_BUF_SIZE;
802 buf->length = GO7007_BUF_SIZE;
812 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
814 struct go7007_file *gofh = priv;
815 struct go7007 *go = gofh->go;
816 struct go7007_buffer *gobuf;
818 int retval = -EINVAL;
821 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
822 buf->memory != V4L2_MEMORY_MMAP)
826 if (buf->index < 0 || buf->index >= gofh->buf_count)
827 goto unlock_and_return;
829 gobuf = &gofh->bufs[buf->index];
831 goto unlock_and_return;
834 if (gobuf->state != BUF_STATE_IDLE)
835 goto unlock_and_return;
837 /* offset will be 0 until we really support USERPTR streaming */
838 gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
839 gobuf->bytesused = 0;
840 gobuf->frame_offset = 0;
841 gobuf->modet_active = 0;
842 if (gobuf->offset > 0)
843 gobuf->page_count = GO7007_BUF_PAGES + 1;
845 gobuf->page_count = GO7007_BUF_PAGES;
848 down_read(¤t->mm->mmap_sem);
849 ret = get_user_pages(current, current->mm,
850 gobuf->user_addr & PAGE_MASK, gobuf->page_count,
851 1, 1, gobuf->pages, NULL);
852 up_read(¤t->mm->mmap_sem);
854 if (ret != gobuf->page_count) {
856 for (i = 0; i < ret; ++i)
857 page_cache_release(gobuf->pages[i]);
858 gobuf->page_count = 0;
859 goto unlock_and_return;
862 gobuf->state = BUF_STATE_QUEUED;
863 spin_lock_irqsave(&go->spinlock, flags);
864 list_add_tail(&gobuf->stream, &go->stream);
865 spin_unlock_irqrestore(&go->spinlock, flags);
876 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
878 struct go7007_file *gofh = priv;
879 struct go7007 *go = gofh->go;
880 struct go7007_buffer *gobuf;
881 int retval = -EINVAL;
886 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
888 if (buf->memory != V4L2_MEMORY_MMAP)
892 if (list_empty(&go->stream))
893 goto unlock_and_return;
894 gobuf = list_entry(go->stream.next,
895 struct go7007_buffer, stream);
898 if (gobuf->state != BUF_STATE_DONE &&
899 !(file->f_flags & O_NONBLOCK)) {
901 prepare_to_wait(&go->frame_waitq, &wait,
903 if (gobuf->state == BUF_STATE_DONE)
905 if (signal_pending(current)) {
906 retval = -ERESTARTSYS;
911 finish_wait(&go->frame_waitq, &wait);
913 if (gobuf->state != BUF_STATE_DONE)
914 goto unlock_and_return;
916 spin_lock_irqsave(&go->spinlock, flags);
917 deactivate_buffer(gobuf);
918 spin_unlock_irqrestore(&go->spinlock, flags);
919 frame_type_flag = get_frame_type_flag(gobuf, go->format);
920 gobuf->state = BUF_STATE_IDLE;
922 memset(buf, 0, sizeof(*buf));
923 buf->index = gobuf->index;
924 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
925 buf->bytesused = gobuf->bytesused;
926 buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
927 buf->field = V4L2_FIELD_NONE;
928 buf->timestamp = gobuf->timestamp;
929 buf->sequence = gobuf->seq;
930 buf->memory = V4L2_MEMORY_MMAP;
931 buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
932 buf->length = GO7007_BUF_SIZE;
933 buf->reserved = gobuf->modet_active;
943 static int vidioc_streamon(struct file *file, void *priv,
944 enum v4l2_buf_type type)
946 struct go7007_file *gofh = priv;
947 struct go7007 *go = gofh->go;
950 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
956 if (!go->streaming) {
959 go->active_buf = NULL;
960 if (go7007_start_encoder(go) < 0)
971 static int vidioc_streamoff(struct file *file, void *priv,
972 enum v4l2_buf_type type)
974 struct go7007_file *gofh = priv;
975 struct go7007 *go = gofh->go;
977 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
980 go7007_streamoff(go);
986 static int vidioc_queryctrl(struct file *file, void *priv,
987 struct v4l2_queryctrl *query)
989 struct go7007_file *gofh = priv;
990 struct go7007 *go = gofh->go;
992 if (!go->i2c_adapter_online)
995 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, query);
997 return (!query->name[0]) ? -EINVAL : 0;
1000 static int vidioc_g_ctrl(struct file *file, void *priv,
1001 struct v4l2_control *ctrl)
1003 struct go7007_file *gofh = priv;
1004 struct go7007 *go = gofh->go;
1005 struct v4l2_queryctrl query;
1007 if (!go->i2c_adapter_online)
1010 memset(&query, 0, sizeof(query));
1011 query.id = ctrl->id;
1012 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
1013 if (query.name[0] == 0)
1015 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_CTRL, ctrl);
1020 static int vidioc_s_ctrl(struct file *file, void *priv,
1021 struct v4l2_control *ctrl)
1023 struct go7007_file *gofh = priv;
1024 struct go7007 *go = gofh->go;
1025 struct v4l2_queryctrl query;
1027 if (!go->i2c_adapter_online)
1030 memset(&query, 0, sizeof(query));
1031 query.id = ctrl->id;
1032 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
1033 if (query.name[0] == 0)
1035 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_CTRL, ctrl);
1040 static int vidioc_g_parm(struct file *filp, void *priv,
1041 struct v4l2_streamparm *parm)
1043 struct go7007_file *gofh = priv;
1044 struct go7007 *go = gofh->go;
1045 struct v4l2_fract timeperframe = {
1046 .numerator = 1001 * go->fps_scale,
1047 .denominator = go->sensor_framerate,
1050 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1053 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
1054 parm->parm.capture.timeperframe = timeperframe;
1059 static int vidioc_s_parm(struct file *filp, void *priv,
1060 struct v4l2_streamparm *parm)
1062 struct go7007_file *gofh = priv;
1063 struct go7007 *go = gofh->go;
1066 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1068 if (parm->parm.capture.capturemode != 0)
1071 n = go->sensor_framerate *
1072 parm->parm.capture.timeperframe.numerator;
1073 d = 1001 * parm->parm.capture.timeperframe.denominator;
1074 if (n != 0 && d != 0 && n > d)
1075 go->fps_scale = (n + d/2) / d;
1082 /* VIDIOC_ENUMSTD on go7007 were used for enumberating the supported fps and
1083 its resolution, when the device is not connected to TV.
1084 This were an API abuse, probably used by the lack of specific IOCTL's to
1085 enumberate it, by the time the driver were written.
1087 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
1088 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
1090 The two functions bellow implements the newer ioctls
1093 static int vidioc_enum_framesizes(struct file *filp, void *priv,
1094 struct v4l2_frmsizeenum *fsize)
1096 struct go7007_file *gofh = priv;
1097 struct go7007 *go = gofh->go;
1099 /* Return -EINVAL, if it is a TV board */
1100 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1101 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1104 if (fsize->index > 0)
1107 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1108 fsize->discrete.width = go->board_info->sensor_width;
1109 fsize->discrete.height = go->board_info->sensor_height;
1114 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1115 struct v4l2_frmivalenum *fival)
1117 struct go7007_file *gofh = priv;
1118 struct go7007 *go = gofh->go;
1120 /* Return -EINVAL, if it is a TV board */
1121 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1122 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1125 if (fival->index > 0)
1128 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1129 fival->discrete.numerator = 1001;
1130 fival->discrete.denominator = go->board_info->sensor_framerate;
1135 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
1137 struct go7007_file *gofh = priv;
1138 struct go7007 *go = gofh->go;
1143 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) &&
1150 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1151 go->input == go->board_info->num_inputs - 1) {
1152 if (!go->i2c_adapter_online)
1154 i2c_clients_command(&go->i2c_adapter,
1156 if (!*std) /* hack to indicate EINVAL from tuner */
1160 if (*std & V4L2_STD_NTSC) {
1161 go->standard = GO7007_STD_NTSC;
1162 go->sensor_framerate = 30000;
1163 } else if (*std & V4L2_STD_PAL) {
1164 go->standard = GO7007_STD_PAL;
1165 go->sensor_framerate = 25025;
1166 } else if (*std & V4L2_STD_SECAM) {
1167 go->standard = GO7007_STD_PAL;
1168 go->sensor_framerate = 25025;
1172 if (go->i2c_adapter_online)
1173 i2c_clients_command(&go->i2c_adapter,
1175 set_capture_size(go, NULL, 0);
1181 case VIDIOC_QUERYSTD:
1183 v4l2_std_id *std = arg;
1185 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1186 go->input == go->board_info->num_inputs - 1) {
1187 if (!go->i2c_adapter_online)
1189 i2c_clients_command(&go->i2c_adapter,
1190 VIDIOC_QUERYSTD, arg);
1191 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1192 *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
1199 static int vidioc_enum_input(struct file *file, void *priv,
1200 struct v4l2_input *inp)
1202 struct go7007_file *gofh = priv;
1203 struct go7007 *go = gofh->go;
1205 if (inp->index >= go->board_info->num_inputs)
1208 strncpy(inp->name, go->board_info->inputs[inp->index].name,
1211 /* If this board has a tuner, it will be the last input */
1212 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1213 inp->index == go->board_info->num_inputs - 1)
1214 inp->type = V4L2_INPUT_TYPE_TUNER;
1216 inp->type = V4L2_INPUT_TYPE_CAMERA;
1220 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1221 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
1230 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
1232 struct go7007_file *gofh = priv;
1233 struct go7007 *go = gofh->go;
1240 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
1242 struct go7007_file *gofh = priv;
1243 struct go7007 *go = gofh->go;
1245 if (input >= go->board_info->num_inputs)
1251 if (go->i2c_adapter_online) {
1252 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_INPUT,
1253 &go->board_info->inputs[input].video_input);
1254 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_AUDIO,
1255 &go->board_info->inputs[input].audio_input);
1261 static int vidioc_g_tuner(struct file *file, void *priv,
1262 struct v4l2_tuner *t)
1264 struct go7007_file *gofh = priv;
1265 struct go7007 *go = gofh->go;
1267 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1271 if (!go->i2c_adapter_online)
1274 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_TUNER, t);
1280 static int vidioc_s_tuner(struct file *file, void *priv,
1281 struct v4l2_tuner *t)
1283 struct go7007_file *gofh = priv;
1284 struct go7007 *go = gofh->go;
1286 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1290 if (!go->i2c_adapter_online)
1293 switch (go->board_id) {
1294 case GO7007_BOARDID_PX_TV402U_NA:
1295 case GO7007_BOARDID_PX_TV402U_JP:
1296 /* No selectable options currently */
1297 if (t->audmode != V4L2_TUNER_MODE_STEREO)
1302 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_TUNER, t);
1307 static int vidioc_g_frequency(struct file *file, void *priv,
1308 struct v4l2_frequency *f)
1310 struct go7007_file *gofh = priv;
1311 struct go7007 *go = gofh->go;
1313 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1315 if (!go->i2c_adapter_online)
1318 f->type = V4L2_TUNER_ANALOG_TV;
1319 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_FREQUENCY, f);
1323 static int vidioc_s_frequency(struct file *file, void *priv,
1324 struct v4l2_frequency *f)
1326 struct go7007_file *gofh = priv;
1327 struct go7007 *go = gofh->go;
1329 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1331 if (!go->i2c_adapter_online)
1334 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_FREQUENCY, f);
1339 static int vidioc_cropcap(struct file *file, void *priv,
1340 struct v4l2_cropcap *cropcap)
1342 struct go7007_file *gofh = priv;
1343 struct go7007 *go = gofh->go;
1345 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1348 /* These specify the raw input of the sensor */
1349 switch (go->standard) {
1350 case GO7007_STD_NTSC:
1351 cropcap->bounds.top = 0;
1352 cropcap->bounds.left = 0;
1353 cropcap->bounds.width = 720;
1354 cropcap->bounds.height = 480;
1355 cropcap->defrect.top = 0;
1356 cropcap->defrect.left = 0;
1357 cropcap->defrect.width = 720;
1358 cropcap->defrect.height = 480;
1360 case GO7007_STD_PAL:
1361 cropcap->bounds.top = 0;
1362 cropcap->bounds.left = 0;
1363 cropcap->bounds.width = 720;
1364 cropcap->bounds.height = 576;
1365 cropcap->defrect.top = 0;
1366 cropcap->defrect.left = 0;
1367 cropcap->defrect.width = 720;
1368 cropcap->defrect.height = 576;
1370 case GO7007_STD_OTHER:
1371 cropcap->bounds.top = 0;
1372 cropcap->bounds.left = 0;
1373 cropcap->bounds.width = go->board_info->sensor_width;
1374 cropcap->bounds.height = go->board_info->sensor_height;
1375 cropcap->defrect.top = 0;
1376 cropcap->defrect.left = 0;
1377 cropcap->defrect.width = go->board_info->sensor_width;
1378 cropcap->defrect.height = go->board_info->sensor_height;
1385 static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1387 struct go7007_file *gofh = priv;
1388 struct go7007 *go = gofh->go;
1390 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1393 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1395 /* These specify the raw input of the sensor */
1396 switch (go->standard) {
1397 case GO7007_STD_NTSC:
1400 crop->c.width = 720;
1401 crop->c.height = 480;
1403 case GO7007_STD_PAL:
1406 crop->c.width = 720;
1407 crop->c.height = 576;
1409 case GO7007_STD_OTHER:
1412 crop->c.width = go->board_info->sensor_width;
1413 crop->c.height = go->board_info->sensor_height;
1420 /* FIXME: vidioc_s_crop is not really implemented!!!
1422 static int vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1424 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1430 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1431 struct v4l2_jpegcompression *params)
1433 memset(params, 0, sizeof(*params));
1434 params->quality = 50; /* ?? */
1435 params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
1436 V4L2_JPEG_MARKER_DQT;
1441 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1442 struct v4l2_jpegcompression *params)
1444 if (params->quality != 50 ||
1445 params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
1446 V4L2_JPEG_MARKER_DQT))
1453 Those ioctls are private, and not needed, since several standard
1454 extended controls already provide streaming control.
1455 So, those ioctls should be converted into vidioc_g_ext_ctrls()
1456 and vidioc_s_ext_ctrls()
1460 /* Temporary ioctls for controlling compression characteristics */
1461 case GO7007IOC_S_BITRATE:
1467 /* Upper bound is kind of arbitrary here */
1468 if (*bitrate < 64000 || *bitrate > 10000000)
1470 go->bitrate = *bitrate;
1473 case GO7007IOC_G_BITRATE:
1477 *bitrate = go->bitrate;
1480 case GO7007IOC_S_COMP_PARAMS:
1482 struct go7007_comp_params *comp = arg;
1484 if (go->format == GO7007_FORMAT_MJPEG)
1486 if (comp->gop_size > 0)
1487 go->gop_size = comp->gop_size;
1489 go->gop_size = go->sensor_framerate / 1000;
1490 if (go->gop_size != 15)
1492 /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
1493 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
1494 switch (comp->aspect_ratio) {
1495 case GO7007_ASPECT_RATIO_4_3_NTSC:
1496 case GO7007_ASPECT_RATIO_4_3_PAL:
1497 go->aspect_ratio = GO7007_RATIO_4_3;
1499 case GO7007_ASPECT_RATIO_16_9_NTSC:
1500 case GO7007_ASPECT_RATIO_16_9_PAL:
1501 go->aspect_ratio = GO7007_RATIO_16_9;
1504 go->aspect_ratio = GO7007_RATIO_1_1;
1508 if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
1510 go->seq_header_enable = 0;
1512 go->seq_header_enable = 1;
1516 case GO7007IOC_G_COMP_PARAMS:
1518 struct go7007_comp_params *comp = arg;
1520 if (go->format == GO7007_FORMAT_MJPEG)
1522 memset(comp, 0, sizeof(*comp));
1523 comp->gop_size = go->gop_size;
1524 comp->max_b_frames = go->ipb ? 2 : 0;
1525 switch (go->aspect_ratio) {
1526 case GO7007_RATIO_4_3:
1527 if (go->standard == GO7007_STD_NTSC)
1528 comp->aspect_ratio =
1529 GO7007_ASPECT_RATIO_4_3_NTSC;
1531 comp->aspect_ratio =
1532 GO7007_ASPECT_RATIO_4_3_PAL;
1534 case GO7007_RATIO_16_9:
1535 if (go->standard == GO7007_STD_NTSC)
1536 comp->aspect_ratio =
1537 GO7007_ASPECT_RATIO_16_9_NTSC;
1539 comp->aspect_ratio =
1540 GO7007_ASPECT_RATIO_16_9_PAL;
1543 comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
1547 comp->flags |= GO7007_COMP_CLOSED_GOP;
1548 if (!go->seq_header_enable)
1549 comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
1552 case GO7007IOC_S_MPEG_PARAMS:
1554 struct go7007_mpeg_params *mpeg = arg;
1556 if (go->format != GO7007_FORMAT_MPEG1 &&
1557 go->format != GO7007_FORMAT_MPEG2 &&
1558 go->format != GO7007_FORMAT_MPEG4)
1561 if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
1562 go->format = GO7007_FORMAT_MPEG2;
1563 go->bitrate = 9800000;
1567 go->repeat_seqhead = 0;
1568 go->seq_header_enable = 1;
1569 go->gop_header_enable = 1;
1572 switch (mpeg->mpeg_video_standard) {
1573 case GO7007_MPEG_VIDEO_MPEG1:
1574 go->format = GO7007_FORMAT_MPEG1;
1577 case GO7007_MPEG_VIDEO_MPEG2:
1578 go->format = GO7007_FORMAT_MPEG2;
1579 if (mpeg->pali >> 24 == 2)
1580 go->pali = mpeg->pali & 0xff;
1584 case GO7007_MPEG_VIDEO_MPEG4:
1585 go->format = GO7007_FORMAT_MPEG4;
1586 if (mpeg->pali >> 24 == 4)
1587 go->pali = mpeg->pali & 0xff;
1594 go->gop_header_enable =
1595 mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
1597 if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
1598 go->repeat_seqhead = 1;
1600 go->repeat_seqhead = 0;
1605 case GO7007IOC_G_MPEG_PARAMS:
1607 struct go7007_mpeg_params *mpeg = arg;
1609 memset(mpeg, 0, sizeof(*mpeg));
1610 switch (go->format) {
1611 case GO7007_FORMAT_MPEG1:
1612 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
1615 case GO7007_FORMAT_MPEG2:
1616 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
1617 mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
1619 case GO7007_FORMAT_MPEG4:
1620 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
1621 mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
1626 if (!go->gop_header_enable)
1627 mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
1628 if (go->repeat_seqhead)
1629 mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
1631 mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
1634 case GO7007IOC_S_MD_PARAMS:
1636 struct go7007_md_params *mdp = arg;
1638 if (mdp->region > 3)
1640 if (mdp->trigger > 0) {
1641 go->modet[mdp->region].pixel_threshold =
1642 mdp->pixel_threshold >> 1;
1643 go->modet[mdp->region].motion_threshold =
1644 mdp->motion_threshold >> 1;
1645 go->modet[mdp->region].mb_threshold =
1647 go->modet[mdp->region].enable = 1;
1649 go->modet[mdp->region].enable = 0;
1652 case GO7007IOC_G_MD_PARAMS:
1654 struct go7007_md_params *mdp = arg;
1655 int region = mdp->region;
1657 if (mdp->region > 3)
1659 memset(mdp, 0, sizeof(struct go7007_md_params));
1660 mdp->region = region;
1661 if (!go->modet[region].enable)
1663 mdp->pixel_threshold =
1664 (go->modet[region].pixel_threshold << 1) + 1;
1665 mdp->motion_threshold =
1666 (go->modet[region].motion_threshold << 1) + 1;
1668 (go->modet[region].mb_threshold << 1) + 1;
1671 case GO7007IOC_S_MD_REGION:
1673 struct go7007_md_region *region = arg;
1675 if (region->region < 1 || region->region > 3)
1677 return clip_to_modet_map(go, region->region, region->clips);
1681 static ssize_t go7007_read(struct file *file, char __user *data,
1682 size_t count, loff_t *ppos)
1687 static void go7007_vm_open(struct vm_area_struct *vma)
1689 struct go7007_buffer *gobuf = vma->vm_private_data;
1694 static void go7007_vm_close(struct vm_area_struct *vma)
1696 struct go7007_buffer *gobuf = vma->vm_private_data;
1697 unsigned long flags;
1699 if (--gobuf->mapped == 0) {
1700 spin_lock_irqsave(&gobuf->go->spinlock, flags);
1701 deactivate_buffer(gobuf);
1702 spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
1706 /* Copied from videobuf-dma-sg.c */
1707 static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1711 page = alloc_page(GFP_USER | __GFP_DMA32);
1713 return VM_FAULT_OOM;
1714 clear_user_page(page_address(page), (unsigned long)vmf->virtual_address,
1720 static struct vm_operations_struct go7007_vm_ops = {
1721 .open = go7007_vm_open,
1722 .close = go7007_vm_close,
1723 .fault = go7007_vm_fault,
1726 static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
1728 struct go7007_file *gofh = file->private_data;
1731 if (gofh->go->status != STATUS_ONLINE)
1733 if (!(vma->vm_flags & VM_SHARED))
1734 return -EINVAL; /* only support VM_SHARED mapping */
1735 if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
1736 return -EINVAL; /* must map exactly one full buffer */
1738 index = vma->vm_pgoff / GO7007_BUF_PAGES;
1739 if (index >= gofh->buf_count) {
1741 return -EINVAL; /* trying to map beyond requested buffers */
1743 if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
1745 return -EINVAL; /* offset is not aligned on buffer boundary */
1747 if (gofh->bufs[index].mapped > 0) {
1751 gofh->bufs[index].mapped = 1;
1752 gofh->bufs[index].user_addr = vma->vm_start;
1753 vma->vm_ops = &go7007_vm_ops;
1754 vma->vm_flags |= VM_DONTEXPAND;
1755 vma->vm_flags &= ~VM_IO;
1756 vma->vm_private_data = &gofh->bufs[index];
1761 static unsigned int go7007_poll(struct file *file, poll_table *wait)
1763 struct go7007_file *gofh = file->private_data;
1764 struct go7007_buffer *gobuf;
1766 if (list_empty(&gofh->go->stream))
1768 gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
1769 poll_wait(file, &gofh->go->frame_waitq, wait);
1770 if (gobuf->state == BUF_STATE_DONE)
1771 return POLLIN | POLLRDNORM;
1775 static void go7007_vfl_release(struct video_device *vfd)
1777 struct go7007 *go = video_get_drvdata(vfd);
1779 video_device_release(vfd);
1780 if (--go->ref_count == 0)
1784 static struct v4l2_file_operations go7007_fops = {
1785 .owner = THIS_MODULE,
1786 .open = go7007_open,
1787 .release = go7007_release,
1788 .ioctl = video_ioctl2,
1789 .read = go7007_read,
1790 .mmap = go7007_mmap,
1791 .poll = go7007_poll,
1794 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1795 .vidioc_querycap = vidioc_querycap,
1796 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1797 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1798 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1799 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1800 .vidioc_reqbufs = vidioc_reqbufs,
1801 .vidioc_querybuf = vidioc_querybuf,
1802 .vidioc_qbuf = vidioc_qbuf,
1803 .vidioc_dqbuf = vidioc_dqbuf,
1804 .vidioc_s_std = vidioc_s_std,
1805 .vidioc_enum_input = vidioc_enum_input,
1806 .vidioc_g_input = vidioc_g_input,
1807 .vidioc_s_input = vidioc_s_input,
1808 .vidioc_queryctrl = vidioc_queryctrl,
1809 .vidioc_g_ctrl = vidioc_g_ctrl,
1810 .vidioc_s_ctrl = vidioc_s_ctrl,
1811 .vidioc_streamon = vidioc_streamon,
1812 .vidioc_streamoff = vidioc_streamoff,
1813 .vidioc_g_tuner = vidioc_g_tuner,
1814 .vidioc_s_tuner = vidioc_s_tuner,
1815 .vidioc_g_frequency = vidioc_g_frequency,
1816 .vidioc_s_frequency = vidioc_s_frequency,
1817 .vidioc_g_parm = vidioc_g_parm,
1818 .vidioc_s_parm = vidioc_s_parm,
1819 #if 0 /* FIXME take out after 2.6.29-rc1 merge happens */
1820 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1821 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1823 .vidioc_cropcap = vidioc_cropcap,
1824 .vidioc_g_crop = vidioc_g_crop,
1825 .vidioc_s_crop = vidioc_s_crop,
1826 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1827 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1830 static struct video_device go7007_template = {
1832 .vfl_type = VID_TYPE_CAPTURE,
1833 .fops = &go7007_fops,
1835 .release = go7007_vfl_release,
1836 .ioctl_ops = &video_ioctl_ops,
1837 .tvnorms = V4L2_STD_ALL,
1838 .current_norm = V4L2_STD_NTSC,
1841 int go7007_v4l2_init(struct go7007 *go)
1845 go->video_dev = video_device_alloc();
1846 if (go->video_dev == NULL)
1848 memcpy(go->video_dev, &go7007_template, sizeof(go7007_template));
1849 go->video_dev->parent = go->dev;
1850 rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
1852 video_device_release(go->video_dev);
1853 go->video_dev = NULL;
1856 video_set_drvdata(go->video_dev, go);
1858 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
1859 go->video_dev->name, go->video_dev->num);
1864 void go7007_v4l2_remove(struct go7007 *go)
1866 unsigned long flags;
1869 if (go->streaming) {
1871 go7007_stream_stop(go);
1872 spin_lock_irqsave(&go->spinlock, flags);
1874 spin_unlock_irqrestore(&go->spinlock, flags);
1878 video_unregister_device(go->video_dev);