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>
37 #include <asm/system.h>
40 #include "go7007-priv.h"
43 /* Temporary defines until accepted in v4l-dvb */
44 #ifndef V4L2_MPEG_STREAM_TYPE_MPEG_ELEM
45 #define V4L2_MPEG_STREAM_TYPE_MPEG_ELEM 6 /* MPEG elementary stream */
47 #ifndef V4L2_MPEG_VIDEO_ENCODING_MPEG_4
48 #define V4L2_MPEG_VIDEO_ENCODING_MPEG_4 3
51 #define call_all(dev, o, f, args...) \
52 v4l2_device_call_until_err(dev, 0, o, f, ##args)
54 static void deactivate_buffer(struct go7007_buffer *gobuf)
58 if (gobuf->state != BUF_STATE_IDLE) {
59 list_del(&gobuf->stream);
60 gobuf->state = BUF_STATE_IDLE;
62 if (gobuf->page_count > 0) {
63 for (i = 0; i < gobuf->page_count; ++i)
64 page_cache_release(gobuf->pages[i]);
65 gobuf->page_count = 0;
69 static void abort_queued(struct go7007 *go)
71 struct go7007_buffer *gobuf, *next;
73 list_for_each_entry_safe(gobuf, next, &go->stream, stream) {
74 deactivate_buffer(gobuf);
78 static int go7007_streamoff(struct go7007 *go)
83 mutex_lock(&go->hw_lock);
86 go7007_stream_stop(go);
87 spin_lock_irqsave(&go->spinlock, flags);
89 spin_unlock_irqrestore(&go->spinlock, flags);
90 go7007_reset_encoder(go);
93 mutex_unlock(&go->hw_lock);
97 static int go7007_open(struct file *file)
99 struct go7007 *go = video_get_drvdata(video_devdata(file));
100 struct go7007_file *gofh;
102 if (go->status != STATUS_ONLINE)
104 gofh = kmalloc(sizeof(struct go7007_file), GFP_KERNEL);
109 mutex_init(&gofh->lock);
111 file->private_data = gofh;
115 static int go7007_release(struct file *file)
117 struct go7007_file *gofh = file->private_data;
118 struct go7007 *go = gofh->go;
120 if (gofh->buf_count > 0) {
121 go7007_streamoff(go);
127 if (--go->ref_count == 0)
129 file->private_data = NULL;
133 static u32 get_frame_type_flag(struct go7007_buffer *gobuf, int format)
135 u8 *f = page_address(gobuf->pages[0]);
138 case GO7007_FORMAT_MJPEG:
139 return V4L2_BUF_FLAG_KEYFRAME;
140 case GO7007_FORMAT_MPEG4:
141 switch ((f[gobuf->frame_offset + 4] >> 6) & 0x3) {
143 return V4L2_BUF_FLAG_KEYFRAME;
145 return V4L2_BUF_FLAG_PFRAME;
147 return V4L2_BUF_FLAG_BFRAME;
151 case GO7007_FORMAT_MPEG1:
152 case GO7007_FORMAT_MPEG2:
153 switch ((f[gobuf->frame_offset + 5] >> 3) & 0x7) {
155 return V4L2_BUF_FLAG_KEYFRAME;
157 return V4L2_BUF_FLAG_PFRAME;
159 return V4L2_BUF_FLAG_BFRAME;
168 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
170 int sensor_height = 0, sensor_width = 0;
171 int width, height, i;
173 if (fmt != NULL && fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
174 fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG &&
175 fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG4)
178 switch (go->standard) {
179 case GO7007_STD_NTSC:
187 case GO7007_STD_OTHER:
188 sensor_width = go->board_info->sensor_width;
189 sensor_height = go->board_info->sensor_height;
194 width = sensor_width;
195 height = sensor_height;
196 } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
197 if (fmt->fmt.pix.width > sensor_width)
198 width = sensor_width;
199 else if (fmt->fmt.pix.width < 144)
202 width = fmt->fmt.pix.width & ~0x0f;
204 if (fmt->fmt.pix.height > sensor_height)
205 height = sensor_height;
206 else if (fmt->fmt.pix.height < 96)
209 height = fmt->fmt.pix.height & ~0x0f;
211 int requested_size = fmt->fmt.pix.width * fmt->fmt.pix.height;
212 int sensor_size = sensor_width * sensor_height;
214 if (64 * requested_size < 9 * sensor_size) {
215 width = sensor_width / 4;
216 height = sensor_height / 4;
217 } else if (64 * requested_size < 36 * sensor_size) {
218 width = sensor_width / 2;
219 height = sensor_height / 2;
221 width = sensor_width;
222 height = sensor_height;
229 u32 pixelformat = fmt->fmt.pix.pixelformat;
231 memset(fmt, 0, sizeof(*fmt));
232 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
233 fmt->fmt.pix.width = width;
234 fmt->fmt.pix.height = height;
235 fmt->fmt.pix.pixelformat = pixelformat;
236 fmt->fmt.pix.field = V4L2_FIELD_NONE;
237 fmt->fmt.pix.bytesperline = 0;
238 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
239 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
247 go->encoder_h_offset = go->board_info->sensor_h_offset;
248 go->encoder_v_offset = go->board_info->sensor_v_offset;
249 for (i = 0; i < 4; ++i)
250 go->modet[i].enable = 0;
251 for (i = 0; i < 1624; ++i)
252 go->modet_map[i] = 0;
254 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
255 struct v4l2_format res;
260 res.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
261 res.fmt.pix.width = width;
264 if (height > sensor_height / 2) {
265 res.fmt.pix.height = height / 2;
266 go->encoder_v_halve = 0;
268 res.fmt.pix.height = height;
269 go->encoder_v_halve = 1;
271 call_all(&go->v4l2_dev, video, s_fmt, &res);
273 if (width <= sensor_width / 4) {
274 go->encoder_h_halve = 1;
275 go->encoder_v_halve = 1;
276 go->encoder_subsample = 1;
277 } else if (width <= sensor_width / 2) {
278 go->encoder_h_halve = 1;
279 go->encoder_v_halve = 1;
280 go->encoder_subsample = 0;
282 go->encoder_h_halve = 0;
283 go->encoder_v_halve = 0;
284 go->encoder_subsample = 0;
291 switch (fmt->fmt.pix.pixelformat) {
292 case V4L2_PIX_FMT_MPEG:
293 if (go->format == GO7007_FORMAT_MPEG1 ||
294 go->format == GO7007_FORMAT_MPEG2 ||
295 go->format == GO7007_FORMAT_MPEG4)
297 go->format = GO7007_FORMAT_MPEG1;
299 go->aspect_ratio = GO7007_RATIO_1_1;
300 go->gop_size = go->sensor_framerate / 1000;
303 go->repeat_seqhead = 1;
304 go->seq_header_enable = 1;
305 go->gop_header_enable = 1;
308 /* Backwards compatibility only! */
309 case V4L2_PIX_FMT_MPEG4:
310 if (go->format == GO7007_FORMAT_MPEG4)
312 go->format = GO7007_FORMAT_MPEG4;
314 go->aspect_ratio = GO7007_RATIO_1_1;
315 go->gop_size = go->sensor_framerate / 1000;
318 go->repeat_seqhead = 1;
319 go->seq_header_enable = 1;
320 go->gop_header_enable = 1;
323 case V4L2_PIX_FMT_MJPEG:
324 go->format = GO7007_FORMAT_MJPEG;
326 go->aspect_ratio = GO7007_RATIO_1_1;
330 go->repeat_seqhead = 0;
331 go->seq_header_enable = 0;
332 go->gop_header_enable = 0;
340 static int clip_to_modet_map(struct go7007 *go, int region,
341 struct v4l2_clip *clip_list)
343 struct v4l2_clip clip, *clip_ptr;
346 /* Check if coordinates are OK and if any macroblocks are already
347 * used by other regions (besides 0) */
348 clip_ptr = clip_list;
350 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
352 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
353 clip.c.width <= 0 || (clip.c.width & 0xF))
355 if (clip.c.left + clip.c.width > go->width)
357 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
358 clip.c.height <= 0 || (clip.c.height & 0xF))
360 if (clip.c.top + clip.c.height > go->height)
362 for (y = 0; y < clip.c.height; y += 16)
363 for (x = 0; x < clip.c.width; x += 16) {
364 mbnum = (go->width >> 4) *
365 ((clip.c.top + y) >> 4) +
366 ((clip.c.left + x) >> 4);
367 if (go->modet_map[mbnum] != 0 &&
368 go->modet_map[mbnum] != region)
371 clip_ptr = clip.next;
374 /* Clear old region macroblocks */
375 for (mbnum = 0; mbnum < 1624; ++mbnum)
376 if (go->modet_map[mbnum] == region)
377 go->modet_map[mbnum] = 0;
379 /* Claim macroblocks in this list */
380 clip_ptr = clip_list;
382 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
384 for (y = 0; y < clip.c.height; y += 16)
385 for (x = 0; x < clip.c.width; x += 16) {
386 mbnum = (go->width >> 4) *
387 ((clip.c.top + y) >> 4) +
388 ((clip.c.left + x) >> 4);
389 go->modet_map[mbnum] = region;
391 clip_ptr = clip.next;
397 static int mpeg_query_ctrl(struct v4l2_queryctrl *ctrl)
399 static const u32 mpeg_ctrls[] = {
401 V4L2_CID_MPEG_STREAM_TYPE,
402 V4L2_CID_MPEG_VIDEO_ENCODING,
403 V4L2_CID_MPEG_VIDEO_ASPECT,
404 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
405 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
406 V4L2_CID_MPEG_VIDEO_BITRATE,
409 static const u32 *ctrl_classes[] = {
414 ctrl->id = v4l2_ctrl_next(ctrl_classes, ctrl->id);
417 case V4L2_CID_MPEG_CLASS:
418 return v4l2_ctrl_query_fill(ctrl, 0, 0, 0, 0);
419 case V4L2_CID_MPEG_STREAM_TYPE:
420 return v4l2_ctrl_query_fill(ctrl,
421 V4L2_MPEG_STREAM_TYPE_MPEG2_DVD,
422 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM, 1,
423 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM);
424 case V4L2_CID_MPEG_VIDEO_ENCODING:
425 return v4l2_ctrl_query_fill(ctrl,
426 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
427 V4L2_MPEG_VIDEO_ENCODING_MPEG_4, 1,
428 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
429 case V4L2_CID_MPEG_VIDEO_ASPECT:
430 return v4l2_ctrl_query_fill(ctrl,
431 V4L2_MPEG_VIDEO_ASPECT_1x1,
432 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
433 V4L2_MPEG_VIDEO_ASPECT_1x1);
434 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
435 return v4l2_ctrl_query_fill(ctrl, 0, 34, 1, 15);
436 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
437 return v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
438 case V4L2_CID_MPEG_VIDEO_BITRATE:
439 return v4l2_ctrl_query_fill(ctrl,
449 static int mpeg_s_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
451 /* pretty sure we can't change any of these while streaming */
456 case V4L2_CID_MPEG_STREAM_TYPE:
457 switch (ctrl->value) {
458 case V4L2_MPEG_STREAM_TYPE_MPEG2_DVD:
459 go->format = GO7007_FORMAT_MPEG2;
460 go->bitrate = 9800000;
464 go->repeat_seqhead = 0;
465 go->seq_header_enable = 1;
466 go->gop_header_enable = 1;
469 case V4L2_MPEG_STREAM_TYPE_MPEG_ELEM:
476 case V4L2_CID_MPEG_VIDEO_ENCODING:
477 switch (ctrl->value) {
478 case V4L2_MPEG_VIDEO_ENCODING_MPEG_1:
479 go->format = GO7007_FORMAT_MPEG1;
482 case V4L2_MPEG_VIDEO_ENCODING_MPEG_2:
483 go->format = GO7007_FORMAT_MPEG2;
484 /*if (mpeg->pali >> 24 == 2)
485 go->pali = mpeg->pali & 0xff;
489 case V4L2_MPEG_VIDEO_ENCODING_MPEG_4:
490 go->format = GO7007_FORMAT_MPEG4;
491 /*if (mpeg->pali >> 24 == 4)
492 go->pali = mpeg->pali & 0xff;
499 go->gop_header_enable =
500 /*mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
502 /*if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
503 go->repeat_seqhead = 1;
505 go->repeat_seqhead = 0;
508 case V4L2_CID_MPEG_VIDEO_ASPECT:
509 if (go->format == GO7007_FORMAT_MJPEG)
511 switch (ctrl->value) {
512 case V4L2_MPEG_VIDEO_ASPECT_1x1:
513 go->aspect_ratio = GO7007_RATIO_1_1;
515 case V4L2_MPEG_VIDEO_ASPECT_4x3:
516 go->aspect_ratio = GO7007_RATIO_4_3;
518 case V4L2_MPEG_VIDEO_ASPECT_16x9:
519 go->aspect_ratio = GO7007_RATIO_16_9;
521 case V4L2_MPEG_VIDEO_ASPECT_221x100:
526 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
527 if (ctrl->value < 0 || ctrl->value > 34)
529 go->gop_size = ctrl->value;
531 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
532 if (ctrl->value != 0 && ctrl->value != 1)
534 go->closed_gop = ctrl->value;
536 case V4L2_CID_MPEG_VIDEO_BITRATE:
537 /* Upper bound is kind of arbitrary here */
538 if (ctrl->value < 64000 || ctrl->value > 10000000)
540 go->bitrate = ctrl->value;
548 static int mpeg_g_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
551 case V4L2_CID_MPEG_STREAM_TYPE:
553 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
555 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG_ELEM;
557 case V4L2_CID_MPEG_VIDEO_ENCODING:
558 switch (go->format) {
559 case GO7007_FORMAT_MPEG1:
560 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
562 case GO7007_FORMAT_MPEG2:
563 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
565 case GO7007_FORMAT_MPEG4:
566 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4;
572 case V4L2_CID_MPEG_VIDEO_ASPECT:
573 switch (go->aspect_ratio) {
574 case GO7007_RATIO_1_1:
575 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_1x1;
577 case GO7007_RATIO_4_3:
578 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_4x3;
580 case GO7007_RATIO_16_9:
581 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_16x9;
587 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
588 ctrl->value = go->gop_size;
590 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
591 ctrl->value = go->closed_gop;
593 case V4L2_CID_MPEG_VIDEO_BITRATE:
594 ctrl->value = go->bitrate;
602 static int vidioc_querycap(struct file *file, void *priv,
603 struct v4l2_capability *cap)
605 struct go7007 *go = ((struct go7007_file *) priv)->go;
607 strlcpy(cap->driver, "go7007", sizeof(cap->driver));
608 strlcpy(cap->card, go->name, sizeof(cap->card));
610 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
613 cap->version = KERNEL_VERSION(0, 9, 8);
615 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
616 V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
618 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
619 cap->capabilities |= V4L2_CAP_TUNER;
624 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
625 struct v4l2_fmtdesc *fmt)
629 switch (fmt->index) {
631 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
632 desc = "Motion-JPEG";
635 fmt->pixelformat = V4L2_PIX_FMT_MPEG;
636 desc = "MPEG1/MPEG2/MPEG4";
641 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
642 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
644 strncpy(fmt->description, desc, sizeof(fmt->description));
649 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
650 struct v4l2_format *fmt)
652 struct go7007 *go = ((struct go7007_file *) priv)->go;
654 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
655 fmt->fmt.pix.width = go->width;
656 fmt->fmt.pix.height = go->height;
657 fmt->fmt.pix.pixelformat = (go->format == GO7007_FORMAT_MJPEG) ?
658 V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
659 fmt->fmt.pix.field = V4L2_FIELD_NONE;
660 fmt->fmt.pix.bytesperline = 0;
661 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
662 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
667 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
668 struct v4l2_format *fmt)
670 struct go7007 *go = ((struct go7007_file *) priv)->go;
672 return set_capture_size(go, fmt, 1);
675 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
676 struct v4l2_format *fmt)
678 struct go7007 *go = ((struct go7007_file *) priv)->go;
683 return set_capture_size(go, fmt, 0);
686 static int vidioc_reqbufs(struct file *file, void *priv,
687 struct v4l2_requestbuffers *req)
689 struct go7007_file *gofh = priv;
690 struct go7007 *go = gofh->go;
692 unsigned int count, i;
697 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
698 req->memory != V4L2_MEMORY_MMAP)
701 mutex_lock(&gofh->lock);
702 for (i = 0; i < gofh->buf_count; ++i)
703 if (gofh->bufs[i].mapped > 0)
704 goto unlock_and_return;
706 mutex_lock(&go->hw_lock);
707 if (go->in_use > 0 && gofh->buf_count == 0) {
708 mutex_unlock(&go->hw_lock);
709 goto unlock_and_return;
712 if (gofh->buf_count > 0)
723 gofh->bufs = kcalloc(count, sizeof(struct go7007_buffer),
727 mutex_unlock(&go->hw_lock);
728 goto unlock_and_return;
731 for (i = 0; i < count; ++i) {
732 gofh->bufs[i].go = go;
733 gofh->bufs[i].index = i;
734 gofh->bufs[i].state = BUF_STATE_IDLE;
735 gofh->bufs[i].mapped = 0;
743 gofh->buf_count = count;
744 mutex_unlock(&go->hw_lock);
745 mutex_unlock(&gofh->lock);
747 memset(req, 0, sizeof(*req));
750 req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
751 req->memory = V4L2_MEMORY_MMAP;
756 mutex_unlock(&gofh->lock);
760 static int vidioc_querybuf(struct file *file, void *priv,
761 struct v4l2_buffer *buf)
763 struct go7007_file *gofh = priv;
764 int retval = -EINVAL;
767 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
772 mutex_lock(&gofh->lock);
773 if (index >= gofh->buf_count)
774 goto unlock_and_return;
776 memset(buf, 0, sizeof(*buf));
778 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
780 switch (gofh->bufs[index].state) {
781 case BUF_STATE_QUEUED:
782 buf->flags = V4L2_BUF_FLAG_QUEUED;
785 buf->flags = V4L2_BUF_FLAG_DONE;
791 if (gofh->bufs[index].mapped)
792 buf->flags |= V4L2_BUF_FLAG_MAPPED;
793 buf->memory = V4L2_MEMORY_MMAP;
794 buf->m.offset = index * GO7007_BUF_SIZE;
795 buf->length = GO7007_BUF_SIZE;
796 mutex_unlock(&gofh->lock);
801 mutex_unlock(&gofh->lock);
805 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
807 struct go7007_file *gofh = priv;
808 struct go7007 *go = gofh->go;
809 struct go7007_buffer *gobuf;
811 int retval = -EINVAL;
814 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
815 buf->memory != V4L2_MEMORY_MMAP)
818 mutex_lock(&gofh->lock);
819 if (buf->index < 0 || buf->index >= gofh->buf_count)
820 goto unlock_and_return;
822 gobuf = &gofh->bufs[buf->index];
824 goto unlock_and_return;
827 if (gobuf->state != BUF_STATE_IDLE)
828 goto unlock_and_return;
830 /* offset will be 0 until we really support USERPTR streaming */
831 gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
832 gobuf->bytesused = 0;
833 gobuf->frame_offset = 0;
834 gobuf->modet_active = 0;
835 if (gobuf->offset > 0)
836 gobuf->page_count = GO7007_BUF_PAGES + 1;
838 gobuf->page_count = GO7007_BUF_PAGES;
841 down_read(¤t->mm->mmap_sem);
842 ret = get_user_pages(current, current->mm,
843 gobuf->user_addr & PAGE_MASK, gobuf->page_count,
844 1, 1, gobuf->pages, NULL);
845 up_read(¤t->mm->mmap_sem);
847 if (ret != gobuf->page_count) {
849 for (i = 0; i < ret; ++i)
850 page_cache_release(gobuf->pages[i]);
851 gobuf->page_count = 0;
852 goto unlock_and_return;
855 gobuf->state = BUF_STATE_QUEUED;
856 spin_lock_irqsave(&go->spinlock, flags);
857 list_add_tail(&gobuf->stream, &go->stream);
858 spin_unlock_irqrestore(&go->spinlock, flags);
859 mutex_unlock(&gofh->lock);
864 mutex_unlock(&gofh->lock);
869 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
871 struct go7007_file *gofh = priv;
872 struct go7007 *go = gofh->go;
873 struct go7007_buffer *gobuf;
874 int retval = -EINVAL;
879 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
881 if (buf->memory != V4L2_MEMORY_MMAP)
884 mutex_lock(&gofh->lock);
885 if (list_empty(&go->stream))
886 goto unlock_and_return;
887 gobuf = list_entry(go->stream.next,
888 struct go7007_buffer, stream);
891 if (gobuf->state != BUF_STATE_DONE &&
892 !(file->f_flags & O_NONBLOCK)) {
894 prepare_to_wait(&go->frame_waitq, &wait,
896 if (gobuf->state == BUF_STATE_DONE)
898 if (signal_pending(current)) {
899 retval = -ERESTARTSYS;
904 finish_wait(&go->frame_waitq, &wait);
906 if (gobuf->state != BUF_STATE_DONE)
907 goto unlock_and_return;
909 spin_lock_irqsave(&go->spinlock, flags);
910 deactivate_buffer(gobuf);
911 spin_unlock_irqrestore(&go->spinlock, flags);
912 frame_type_flag = get_frame_type_flag(gobuf, go->format);
913 gobuf->state = BUF_STATE_IDLE;
915 memset(buf, 0, sizeof(*buf));
916 buf->index = gobuf->index;
917 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
918 buf->bytesused = gobuf->bytesused;
919 buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
920 buf->field = V4L2_FIELD_NONE;
921 buf->timestamp = gobuf->timestamp;
922 buf->sequence = gobuf->seq;
923 buf->memory = V4L2_MEMORY_MMAP;
924 buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
925 buf->length = GO7007_BUF_SIZE;
926 buf->reserved = gobuf->modet_active;
928 mutex_unlock(&gofh->lock);
932 mutex_unlock(&gofh->lock);
936 static int vidioc_streamon(struct file *file, void *priv,
937 enum v4l2_buf_type type)
939 struct go7007_file *gofh = priv;
940 struct go7007 *go = gofh->go;
943 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
946 mutex_lock(&gofh->lock);
947 mutex_lock(&go->hw_lock);
949 if (!go->streaming) {
952 go->active_buf = NULL;
953 if (go7007_start_encoder(go) < 0)
958 mutex_unlock(&go->hw_lock);
959 mutex_unlock(&gofh->lock);
964 static int vidioc_streamoff(struct file *file, void *priv,
965 enum v4l2_buf_type type)
967 struct go7007_file *gofh = priv;
968 struct go7007 *go = gofh->go;
970 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
972 mutex_lock(&gofh->lock);
973 go7007_streamoff(go);
974 mutex_unlock(&gofh->lock);
979 static int vidioc_queryctrl(struct file *file, void *priv,
980 struct v4l2_queryctrl *query)
982 struct go7007 *go = ((struct go7007_file *) priv)->go;
985 if (0 == call_all(&go->v4l2_dev, core, queryctrl, query))
989 return mpeg_query_ctrl(query);
992 static int vidioc_g_ctrl(struct file *file, void *priv,
993 struct v4l2_control *ctrl)
995 struct go7007 *go = ((struct go7007_file *) priv)->go;
997 if (0 == call_all(&go->v4l2_dev, core, g_ctrl, ctrl))
1000 return mpeg_g_ctrl(ctrl, go);
1003 static int vidioc_s_ctrl(struct file *file, void *priv,
1004 struct v4l2_control *ctrl)
1006 struct go7007 *go = ((struct go7007_file *) priv)->go;
1008 if (0 == call_all(&go->v4l2_dev, core, s_ctrl, ctrl))
1011 return mpeg_s_ctrl(ctrl, go);
1014 static int vidioc_g_parm(struct file *filp, void *priv,
1015 struct v4l2_streamparm *parm)
1017 struct go7007 *go = ((struct go7007_file *) priv)->go;
1018 struct v4l2_fract timeperframe = {
1019 .numerator = 1001 * go->fps_scale,
1020 .denominator = go->sensor_framerate,
1023 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1026 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
1027 parm->parm.capture.timeperframe = timeperframe;
1032 static int vidioc_s_parm(struct file *filp, void *priv,
1033 struct v4l2_streamparm *parm)
1035 struct go7007 *go = ((struct go7007_file *) priv)->go;
1038 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1040 if (parm->parm.capture.capturemode != 0)
1043 n = go->sensor_framerate *
1044 parm->parm.capture.timeperframe.numerator;
1045 d = 1001 * parm->parm.capture.timeperframe.denominator;
1046 if (n != 0 && d != 0 && n > d)
1047 go->fps_scale = (n + d/2) / d;
1054 /* VIDIOC_ENUMSTD on go7007 were used for enumberating the supported fps and
1055 its resolution, when the device is not connected to TV.
1056 This were an API abuse, probably used by the lack of specific IOCTL's to
1057 enumberate it, by the time the driver were written.
1059 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
1060 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
1062 The two functions bellow implements the newer ioctls
1064 static int vidioc_enum_framesizes(struct file *filp, void *priv,
1065 struct v4l2_frmsizeenum *fsize)
1067 struct go7007 *go = ((struct go7007_file *) priv)->go;
1069 /* Return -EINVAL, if it is a TV board */
1070 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1071 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1074 if (fsize->index > 0)
1077 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1078 fsize->discrete.width = go->board_info->sensor_width;
1079 fsize->discrete.height = go->board_info->sensor_height;
1084 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1085 struct v4l2_frmivalenum *fival)
1087 struct go7007 *go = ((struct go7007_file *) priv)->go;
1089 /* Return -EINVAL, if it is a TV board */
1090 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1091 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1094 if (fival->index > 0)
1097 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1098 fival->discrete.numerator = 1001;
1099 fival->discrete.denominator = go->board_info->sensor_framerate;
1104 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
1106 struct go7007 *go = ((struct go7007_file *) priv)->go;
1108 switch (go->standard) {
1109 case GO7007_STD_NTSC:
1110 *std = V4L2_STD_NTSC;
1112 case GO7007_STD_PAL:
1113 *std = V4L2_STD_PAL;
1122 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
1124 struct go7007 *go = ((struct go7007_file *) priv)->go;
1129 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) && *std != 0)
1135 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1136 go->input == go->board_info->num_inputs - 1) {
1137 if (!go->i2c_adapter_online)
1139 if (call_all(&go->v4l2_dev, core, s_std, *std) < 0)
1143 if (*std & V4L2_STD_NTSC) {
1144 go->standard = GO7007_STD_NTSC;
1145 go->sensor_framerate = 30000;
1146 } else if (*std & V4L2_STD_PAL) {
1147 go->standard = GO7007_STD_PAL;
1148 go->sensor_framerate = 25025;
1149 } else if (*std & V4L2_STD_SECAM) {
1150 go->standard = GO7007_STD_PAL;
1151 go->sensor_framerate = 25025;
1155 call_all(&go->v4l2_dev, core, s_std, *std);
1156 set_capture_size(go, NULL, 0);
1161 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
1163 struct go7007 *go = ((struct go7007_file *) priv)->go;
1165 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1166 go->input == go->board_info->num_inputs - 1) {
1167 if (!go->i2c_adapter_online)
1169 return call_all(&go->v4l2_dev, video, querystd, std);
1170 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1171 *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
1178 static int vidioc_enum_input(struct file *file, void *priv,
1179 struct v4l2_input *inp)
1181 struct go7007 *go = ((struct go7007_file *) priv)->go;
1183 if (inp->index >= go->board_info->num_inputs)
1186 strncpy(inp->name, go->board_info->inputs[inp->index].name,
1189 /* If this board has a tuner, it will be the last input */
1190 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1191 inp->index == go->board_info->num_inputs - 1)
1192 inp->type = V4L2_INPUT_TYPE_TUNER;
1194 inp->type = V4L2_INPUT_TYPE_CAMERA;
1198 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1199 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
1208 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
1210 struct go7007 *go = ((struct go7007_file *) priv)->go;
1217 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
1219 struct go7007 *go = ((struct go7007_file *) priv)->go;
1221 if (input >= go->board_info->num_inputs)
1228 return call_all(&go->v4l2_dev, video, s_routing, input, 0, 0);
1231 static int vidioc_g_tuner(struct file *file, void *priv,
1232 struct v4l2_tuner *t)
1234 struct go7007 *go = ((struct go7007_file *) priv)->go;
1236 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1240 if (!go->i2c_adapter_online)
1243 return call_all(&go->v4l2_dev, tuner, g_tuner, t);
1246 static int vidioc_s_tuner(struct file *file, void *priv,
1247 struct v4l2_tuner *t)
1249 struct go7007 *go = ((struct go7007_file *) priv)->go;
1251 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1255 if (!go->i2c_adapter_online)
1258 switch (go->board_id) {
1259 case GO7007_BOARDID_PX_TV402U_NA:
1260 case GO7007_BOARDID_PX_TV402U_JP:
1261 /* No selectable options currently */
1262 if (t->audmode != V4L2_TUNER_MODE_STEREO)
1267 return call_all(&go->v4l2_dev, tuner, s_tuner, t);
1270 static int vidioc_g_frequency(struct file *file, void *priv,
1271 struct v4l2_frequency *f)
1273 struct go7007 *go = ((struct go7007_file *) priv)->go;
1275 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1277 if (!go->i2c_adapter_online)
1280 f->type = V4L2_TUNER_ANALOG_TV;
1282 return call_all(&go->v4l2_dev, tuner, g_frequency, f);
1285 static int vidioc_s_frequency(struct file *file, void *priv,
1286 struct v4l2_frequency *f)
1288 struct go7007 *go = ((struct go7007_file *) priv)->go;
1290 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1292 if (!go->i2c_adapter_online)
1295 return call_all(&go->v4l2_dev, tuner, s_frequency, f);
1298 static int vidioc_cropcap(struct file *file, void *priv,
1299 struct v4l2_cropcap *cropcap)
1301 struct go7007 *go = ((struct go7007_file *) priv)->go;
1303 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1306 /* These specify the raw input of the sensor */
1307 switch (go->standard) {
1308 case GO7007_STD_NTSC:
1309 cropcap->bounds.top = 0;
1310 cropcap->bounds.left = 0;
1311 cropcap->bounds.width = 720;
1312 cropcap->bounds.height = 480;
1313 cropcap->defrect.top = 0;
1314 cropcap->defrect.left = 0;
1315 cropcap->defrect.width = 720;
1316 cropcap->defrect.height = 480;
1318 case GO7007_STD_PAL:
1319 cropcap->bounds.top = 0;
1320 cropcap->bounds.left = 0;
1321 cropcap->bounds.width = 720;
1322 cropcap->bounds.height = 576;
1323 cropcap->defrect.top = 0;
1324 cropcap->defrect.left = 0;
1325 cropcap->defrect.width = 720;
1326 cropcap->defrect.height = 576;
1328 case GO7007_STD_OTHER:
1329 cropcap->bounds.top = 0;
1330 cropcap->bounds.left = 0;
1331 cropcap->bounds.width = go->board_info->sensor_width;
1332 cropcap->bounds.height = go->board_info->sensor_height;
1333 cropcap->defrect.top = 0;
1334 cropcap->defrect.left = 0;
1335 cropcap->defrect.width = go->board_info->sensor_width;
1336 cropcap->defrect.height = go->board_info->sensor_height;
1343 static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1345 struct go7007 *go = ((struct go7007_file *) priv)->go;
1347 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1350 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1352 /* These specify the raw input of the sensor */
1353 switch (go->standard) {
1354 case GO7007_STD_NTSC:
1357 crop->c.width = 720;
1358 crop->c.height = 480;
1360 case GO7007_STD_PAL:
1363 crop->c.width = 720;
1364 crop->c.height = 576;
1366 case GO7007_STD_OTHER:
1369 crop->c.width = go->board_info->sensor_width;
1370 crop->c.height = go->board_info->sensor_height;
1377 /* FIXME: vidioc_s_crop is not really implemented!!!
1379 static int vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1381 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1387 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1388 struct v4l2_jpegcompression *params)
1390 memset(params, 0, sizeof(*params));
1391 params->quality = 50; /* ?? */
1392 params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
1393 V4L2_JPEG_MARKER_DQT;
1398 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1399 struct v4l2_jpegcompression *params)
1401 if (params->quality != 50 ||
1402 params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
1403 V4L2_JPEG_MARKER_DQT))
1410 Those ioctls are private, and not needed, since several standard
1411 extended controls already provide streaming control.
1412 So, those ioctls should be converted into vidioc_g_ext_ctrls()
1413 and vidioc_s_ext_ctrls()
1417 /* Temporary ioctls for controlling compression characteristics */
1418 case GO7007IOC_S_BITRATE:
1424 /* Upper bound is kind of arbitrary here */
1425 if (*bitrate < 64000 || *bitrate > 10000000)
1427 go->bitrate = *bitrate;
1430 case GO7007IOC_G_BITRATE:
1434 *bitrate = go->bitrate;
1437 case GO7007IOC_S_COMP_PARAMS:
1439 struct go7007_comp_params *comp = arg;
1441 if (go->format == GO7007_FORMAT_MJPEG)
1443 if (comp->gop_size > 0)
1444 go->gop_size = comp->gop_size;
1446 go->gop_size = go->sensor_framerate / 1000;
1447 if (go->gop_size != 15)
1449 /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
1450 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
1451 switch (comp->aspect_ratio) {
1452 case GO7007_ASPECT_RATIO_4_3_NTSC:
1453 case GO7007_ASPECT_RATIO_4_3_PAL:
1454 go->aspect_ratio = GO7007_RATIO_4_3;
1456 case GO7007_ASPECT_RATIO_16_9_NTSC:
1457 case GO7007_ASPECT_RATIO_16_9_PAL:
1458 go->aspect_ratio = GO7007_RATIO_16_9;
1461 go->aspect_ratio = GO7007_RATIO_1_1;
1465 if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
1467 go->seq_header_enable = 0;
1469 go->seq_header_enable = 1;
1473 case GO7007IOC_G_COMP_PARAMS:
1475 struct go7007_comp_params *comp = arg;
1477 if (go->format == GO7007_FORMAT_MJPEG)
1479 memset(comp, 0, sizeof(*comp));
1480 comp->gop_size = go->gop_size;
1481 comp->max_b_frames = go->ipb ? 2 : 0;
1482 switch (go->aspect_ratio) {
1483 case GO7007_RATIO_4_3:
1484 if (go->standard == GO7007_STD_NTSC)
1485 comp->aspect_ratio =
1486 GO7007_ASPECT_RATIO_4_3_NTSC;
1488 comp->aspect_ratio =
1489 GO7007_ASPECT_RATIO_4_3_PAL;
1491 case GO7007_RATIO_16_9:
1492 if (go->standard == GO7007_STD_NTSC)
1493 comp->aspect_ratio =
1494 GO7007_ASPECT_RATIO_16_9_NTSC;
1496 comp->aspect_ratio =
1497 GO7007_ASPECT_RATIO_16_9_PAL;
1500 comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
1504 comp->flags |= GO7007_COMP_CLOSED_GOP;
1505 if (!go->seq_header_enable)
1506 comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
1509 case GO7007IOC_S_MPEG_PARAMS:
1511 struct go7007_mpeg_params *mpeg = arg;
1513 if (go->format != GO7007_FORMAT_MPEG1 &&
1514 go->format != GO7007_FORMAT_MPEG2 &&
1515 go->format != GO7007_FORMAT_MPEG4)
1518 if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
1519 go->format = GO7007_FORMAT_MPEG2;
1520 go->bitrate = 9800000;
1524 go->repeat_seqhead = 0;
1525 go->seq_header_enable = 1;
1526 go->gop_header_enable = 1;
1529 switch (mpeg->mpeg_video_standard) {
1530 case GO7007_MPEG_VIDEO_MPEG1:
1531 go->format = GO7007_FORMAT_MPEG1;
1534 case GO7007_MPEG_VIDEO_MPEG2:
1535 go->format = GO7007_FORMAT_MPEG2;
1536 if (mpeg->pali >> 24 == 2)
1537 go->pali = mpeg->pali & 0xff;
1541 case GO7007_MPEG_VIDEO_MPEG4:
1542 go->format = GO7007_FORMAT_MPEG4;
1543 if (mpeg->pali >> 24 == 4)
1544 go->pali = mpeg->pali & 0xff;
1551 go->gop_header_enable =
1552 mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
1554 if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
1555 go->repeat_seqhead = 1;
1557 go->repeat_seqhead = 0;
1562 case GO7007IOC_G_MPEG_PARAMS:
1564 struct go7007_mpeg_params *mpeg = arg;
1566 memset(mpeg, 0, sizeof(*mpeg));
1567 switch (go->format) {
1568 case GO7007_FORMAT_MPEG1:
1569 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
1572 case GO7007_FORMAT_MPEG2:
1573 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
1574 mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
1576 case GO7007_FORMAT_MPEG4:
1577 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
1578 mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
1583 if (!go->gop_header_enable)
1584 mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
1585 if (go->repeat_seqhead)
1586 mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
1588 mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
1591 case GO7007IOC_S_MD_PARAMS:
1593 struct go7007_md_params *mdp = arg;
1595 if (mdp->region > 3)
1597 if (mdp->trigger > 0) {
1598 go->modet[mdp->region].pixel_threshold =
1599 mdp->pixel_threshold >> 1;
1600 go->modet[mdp->region].motion_threshold =
1601 mdp->motion_threshold >> 1;
1602 go->modet[mdp->region].mb_threshold =
1604 go->modet[mdp->region].enable = 1;
1606 go->modet[mdp->region].enable = 0;
1609 case GO7007IOC_G_MD_PARAMS:
1611 struct go7007_md_params *mdp = arg;
1612 int region = mdp->region;
1614 if (mdp->region > 3)
1616 memset(mdp, 0, sizeof(struct go7007_md_params));
1617 mdp->region = region;
1618 if (!go->modet[region].enable)
1620 mdp->pixel_threshold =
1621 (go->modet[region].pixel_threshold << 1) + 1;
1622 mdp->motion_threshold =
1623 (go->modet[region].motion_threshold << 1) + 1;
1625 (go->modet[region].mb_threshold << 1) + 1;
1628 case GO7007IOC_S_MD_REGION:
1630 struct go7007_md_region *region = arg;
1632 if (region->region < 1 || region->region > 3)
1634 return clip_to_modet_map(go, region->region, region->clips);
1638 static ssize_t go7007_read(struct file *file, char __user *data,
1639 size_t count, loff_t *ppos)
1644 static void go7007_vm_open(struct vm_area_struct *vma)
1646 struct go7007_buffer *gobuf = vma->vm_private_data;
1651 static void go7007_vm_close(struct vm_area_struct *vma)
1653 struct go7007_buffer *gobuf = vma->vm_private_data;
1654 unsigned long flags;
1656 if (--gobuf->mapped == 0) {
1657 spin_lock_irqsave(&gobuf->go->spinlock, flags);
1658 deactivate_buffer(gobuf);
1659 spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
1663 /* Copied from videobuf-dma-sg.c */
1664 static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1668 page = alloc_page(GFP_USER | __GFP_DMA32);
1670 return VM_FAULT_OOM;
1671 clear_user_highpage(page, (unsigned long)vmf->virtual_address);
1676 static struct vm_operations_struct go7007_vm_ops = {
1677 .open = go7007_vm_open,
1678 .close = go7007_vm_close,
1679 .fault = go7007_vm_fault,
1682 static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
1684 struct go7007_file *gofh = file->private_data;
1687 if (gofh->go->status != STATUS_ONLINE)
1689 if (!(vma->vm_flags & VM_SHARED))
1690 return -EINVAL; /* only support VM_SHARED mapping */
1691 if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
1692 return -EINVAL; /* must map exactly one full buffer */
1693 mutex_lock(&gofh->lock);
1694 index = vma->vm_pgoff / GO7007_BUF_PAGES;
1695 if (index >= gofh->buf_count) {
1696 mutex_unlock(&gofh->lock);
1697 return -EINVAL; /* trying to map beyond requested buffers */
1699 if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
1700 mutex_unlock(&gofh->lock);
1701 return -EINVAL; /* offset is not aligned on buffer boundary */
1703 if (gofh->bufs[index].mapped > 0) {
1704 mutex_unlock(&gofh->lock);
1707 gofh->bufs[index].mapped = 1;
1708 gofh->bufs[index].user_addr = vma->vm_start;
1709 vma->vm_ops = &go7007_vm_ops;
1710 vma->vm_flags |= VM_DONTEXPAND;
1711 vma->vm_flags &= ~VM_IO;
1712 vma->vm_private_data = &gofh->bufs[index];
1713 mutex_unlock(&gofh->lock);
1717 static unsigned int go7007_poll(struct file *file, poll_table *wait)
1719 struct go7007_file *gofh = file->private_data;
1720 struct go7007_buffer *gobuf;
1722 if (list_empty(&gofh->go->stream))
1724 gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
1725 poll_wait(file, &gofh->go->frame_waitq, wait);
1726 if (gobuf->state == BUF_STATE_DONE)
1727 return POLLIN | POLLRDNORM;
1731 static void go7007_vfl_release(struct video_device *vfd)
1733 struct go7007 *go = video_get_drvdata(vfd);
1735 video_device_release(vfd);
1736 if (--go->ref_count == 0)
1740 static struct v4l2_file_operations go7007_fops = {
1741 .owner = THIS_MODULE,
1742 .open = go7007_open,
1743 .release = go7007_release,
1744 .ioctl = video_ioctl2,
1745 .read = go7007_read,
1746 .mmap = go7007_mmap,
1747 .poll = go7007_poll,
1750 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1751 .vidioc_querycap = vidioc_querycap,
1752 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1753 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1754 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1755 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1756 .vidioc_reqbufs = vidioc_reqbufs,
1757 .vidioc_querybuf = vidioc_querybuf,
1758 .vidioc_qbuf = vidioc_qbuf,
1759 .vidioc_dqbuf = vidioc_dqbuf,
1760 .vidioc_g_std = vidioc_g_std,
1761 .vidioc_s_std = vidioc_s_std,
1762 .vidioc_querystd = vidioc_querystd,
1763 .vidioc_enum_input = vidioc_enum_input,
1764 .vidioc_g_input = vidioc_g_input,
1765 .vidioc_s_input = vidioc_s_input,
1766 .vidioc_queryctrl = vidioc_queryctrl,
1767 .vidioc_g_ctrl = vidioc_g_ctrl,
1768 .vidioc_s_ctrl = vidioc_s_ctrl,
1769 .vidioc_streamon = vidioc_streamon,
1770 .vidioc_streamoff = vidioc_streamoff,
1771 .vidioc_g_tuner = vidioc_g_tuner,
1772 .vidioc_s_tuner = vidioc_s_tuner,
1773 .vidioc_g_frequency = vidioc_g_frequency,
1774 .vidioc_s_frequency = vidioc_s_frequency,
1775 .vidioc_g_parm = vidioc_g_parm,
1776 .vidioc_s_parm = vidioc_s_parm,
1777 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1778 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1779 .vidioc_cropcap = vidioc_cropcap,
1780 .vidioc_g_crop = vidioc_g_crop,
1781 .vidioc_s_crop = vidioc_s_crop,
1782 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1783 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1786 static struct video_device go7007_template = {
1788 .fops = &go7007_fops,
1789 .release = go7007_vfl_release,
1790 .ioctl_ops = &video_ioctl_ops,
1791 .tvnorms = V4L2_STD_ALL,
1792 .current_norm = V4L2_STD_NTSC,
1795 int go7007_v4l2_init(struct go7007 *go)
1799 go->video_dev = video_device_alloc();
1800 if (go->video_dev == NULL)
1802 *go->video_dev = go7007_template;
1803 go->video_dev->parent = go->dev;
1804 rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
1806 video_device_release(go->video_dev);
1807 go->video_dev = NULL;
1810 rv = v4l2_device_register(go->dev, &go->v4l2_dev);
1812 video_device_release(go->video_dev);
1813 go->video_dev = NULL;
1816 video_set_drvdata(go->video_dev, go);
1818 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1819 go->video_dev->name, video_device_node_name(go->video_dev));
1824 void go7007_v4l2_remove(struct go7007 *go)
1826 unsigned long flags;
1828 mutex_lock(&go->hw_lock);
1829 if (go->streaming) {
1831 go7007_stream_stop(go);
1832 spin_lock_irqsave(&go->spinlock, flags);
1834 spin_unlock_irqrestore(&go->spinlock, flags);
1836 mutex_unlock(&go->hw_lock);
1838 video_unregister_device(go->video_dev);
1839 v4l2_device_unregister(&go->v4l2_dev);