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 <media/v4l2-subdev.h>
33 #include <linux/i2c.h>
34 #include <linux/mutex.h>
35 #include <linux/uaccess.h>
36 #include <asm/system.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_format res;
259 res.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
260 res.fmt.pix.width = width;
263 if (height > sensor_height / 2) {
264 res.fmt.pix.height = height / 2;
265 go->encoder_v_halve = 0;
267 res.fmt.pix.height = height;
268 go->encoder_v_halve = 1;
270 call_all(&go->v4l2_dev, video, s_fmt, &res);
272 if (width <= sensor_width / 4) {
273 go->encoder_h_halve = 1;
274 go->encoder_v_halve = 1;
275 go->encoder_subsample = 1;
276 } else if (width <= sensor_width / 2) {
277 go->encoder_h_halve = 1;
278 go->encoder_v_halve = 1;
279 go->encoder_subsample = 0;
281 go->encoder_h_halve = 0;
282 go->encoder_v_halve = 0;
283 go->encoder_subsample = 0;
290 switch (fmt->fmt.pix.pixelformat) {
291 case V4L2_PIX_FMT_MPEG:
292 if (go->format == GO7007_FORMAT_MPEG1 ||
293 go->format == GO7007_FORMAT_MPEG2 ||
294 go->format == GO7007_FORMAT_MPEG4)
296 go->format = GO7007_FORMAT_MPEG1;
298 go->aspect_ratio = GO7007_RATIO_1_1;
299 go->gop_size = go->sensor_framerate / 1000;
302 go->repeat_seqhead = 1;
303 go->seq_header_enable = 1;
304 go->gop_header_enable = 1;
307 /* Backwards compatibility only! */
308 case V4L2_PIX_FMT_MPEG4:
309 if (go->format == GO7007_FORMAT_MPEG4)
311 go->format = GO7007_FORMAT_MPEG4;
313 go->aspect_ratio = GO7007_RATIO_1_1;
314 go->gop_size = go->sensor_framerate / 1000;
317 go->repeat_seqhead = 1;
318 go->seq_header_enable = 1;
319 go->gop_header_enable = 1;
322 case V4L2_PIX_FMT_MJPEG:
323 go->format = GO7007_FORMAT_MJPEG;
325 go->aspect_ratio = GO7007_RATIO_1_1;
329 go->repeat_seqhead = 0;
330 go->seq_header_enable = 0;
331 go->gop_header_enable = 0;
339 static int clip_to_modet_map(struct go7007 *go, int region,
340 struct v4l2_clip *clip_list)
342 struct v4l2_clip clip, *clip_ptr;
345 /* Check if coordinates are OK and if any macroblocks are already
346 * used by other regions (besides 0) */
347 clip_ptr = clip_list;
349 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
351 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
352 clip.c.width <= 0 || (clip.c.width & 0xF))
354 if (clip.c.left + clip.c.width > go->width)
356 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
357 clip.c.height <= 0 || (clip.c.height & 0xF))
359 if (clip.c.top + clip.c.height > go->height)
361 for (y = 0; y < clip.c.height; y += 16)
362 for (x = 0; x < clip.c.width; x += 16) {
363 mbnum = (go->width >> 4) *
364 ((clip.c.top + y) >> 4) +
365 ((clip.c.left + x) >> 4);
366 if (go->modet_map[mbnum] != 0 &&
367 go->modet_map[mbnum] != region)
370 clip_ptr = clip.next;
373 /* Clear old region macroblocks */
374 for (mbnum = 0; mbnum < 1624; ++mbnum)
375 if (go->modet_map[mbnum] == region)
376 go->modet_map[mbnum] = 0;
378 /* Claim macroblocks in this list */
379 clip_ptr = clip_list;
381 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
383 for (y = 0; y < clip.c.height; y += 16)
384 for (x = 0; x < clip.c.width; x += 16) {
385 mbnum = (go->width >> 4) *
386 ((clip.c.top + y) >> 4) +
387 ((clip.c.left + x) >> 4);
388 go->modet_map[mbnum] = region;
390 clip_ptr = clip.next;
396 static int mpeg_query_ctrl(struct v4l2_queryctrl *ctrl)
398 static const u32 mpeg_ctrls[] = {
400 V4L2_CID_MPEG_STREAM_TYPE,
401 V4L2_CID_MPEG_VIDEO_ENCODING,
402 V4L2_CID_MPEG_VIDEO_ASPECT,
403 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
404 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
405 V4L2_CID_MPEG_VIDEO_BITRATE,
408 static const u32 *ctrl_classes[] = {
413 ctrl->id = v4l2_ctrl_next(ctrl_classes, ctrl->id);
416 case V4L2_CID_MPEG_CLASS:
417 return v4l2_ctrl_query_fill(ctrl, 0, 0, 0, 0);
418 case V4L2_CID_MPEG_STREAM_TYPE:
419 return v4l2_ctrl_query_fill(ctrl,
420 V4L2_MPEG_STREAM_TYPE_MPEG2_DVD,
421 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM, 1,
422 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM);
423 case V4L2_CID_MPEG_VIDEO_ENCODING:
424 return v4l2_ctrl_query_fill(ctrl,
425 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
426 V4L2_MPEG_VIDEO_ENCODING_MPEG_4, 1,
427 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
428 case V4L2_CID_MPEG_VIDEO_ASPECT:
429 return v4l2_ctrl_query_fill(ctrl,
430 V4L2_MPEG_VIDEO_ASPECT_1x1,
431 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
432 V4L2_MPEG_VIDEO_ASPECT_1x1);
433 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
434 return v4l2_ctrl_query_fill(ctrl, 0, 34, 1, 15);
435 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
436 return v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
437 case V4L2_CID_MPEG_VIDEO_BITRATE:
438 return v4l2_ctrl_query_fill(ctrl,
448 static int mpeg_s_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
450 /* pretty sure we can't change any of these while streaming */
455 case V4L2_CID_MPEG_STREAM_TYPE:
456 switch (ctrl->value) {
457 case V4L2_MPEG_STREAM_TYPE_MPEG2_DVD:
458 go->format = GO7007_FORMAT_MPEG2;
459 go->bitrate = 9800000;
463 go->repeat_seqhead = 0;
464 go->seq_header_enable = 1;
465 go->gop_header_enable = 1;
468 case V4L2_MPEG_STREAM_TYPE_MPEG_ELEM:
475 case V4L2_CID_MPEG_VIDEO_ENCODING:
476 switch (ctrl->value) {
477 case V4L2_MPEG_VIDEO_ENCODING_MPEG_1:
478 go->format = GO7007_FORMAT_MPEG1;
481 case V4L2_MPEG_VIDEO_ENCODING_MPEG_2:
482 go->format = GO7007_FORMAT_MPEG2;
483 /*if (mpeg->pali >> 24 == 2)
484 go->pali = mpeg->pali & 0xff;
488 case V4L2_MPEG_VIDEO_ENCODING_MPEG_4:
489 go->format = GO7007_FORMAT_MPEG4;
490 /*if (mpeg->pali >> 24 == 4)
491 go->pali = mpeg->pali & 0xff;
498 go->gop_header_enable =
499 /*mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
501 /*if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
502 go->repeat_seqhead = 1;
504 go->repeat_seqhead = 0;
507 case V4L2_CID_MPEG_VIDEO_ASPECT:
508 if (go->format == GO7007_FORMAT_MJPEG)
510 switch (ctrl->value) {
511 case V4L2_MPEG_VIDEO_ASPECT_1x1:
512 go->aspect_ratio = GO7007_RATIO_1_1;
514 case V4L2_MPEG_VIDEO_ASPECT_4x3:
515 go->aspect_ratio = GO7007_RATIO_4_3;
517 case V4L2_MPEG_VIDEO_ASPECT_16x9:
518 go->aspect_ratio = GO7007_RATIO_16_9;
520 case V4L2_MPEG_VIDEO_ASPECT_221x100:
525 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
526 if (ctrl->value < 0 || ctrl->value > 34)
528 go->gop_size = ctrl->value;
530 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
531 if (ctrl->value != 0 && ctrl->value != 1)
533 go->closed_gop = ctrl->value;
535 case V4L2_CID_MPEG_VIDEO_BITRATE:
536 /* Upper bound is kind of arbitrary here */
537 if (ctrl->value < 64000 || ctrl->value > 10000000)
539 go->bitrate = ctrl->value;
547 static int mpeg_g_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
550 case V4L2_CID_MPEG_STREAM_TYPE:
552 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
554 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG_ELEM;
556 case V4L2_CID_MPEG_VIDEO_ENCODING:
557 switch (go->format) {
558 case GO7007_FORMAT_MPEG1:
559 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
561 case GO7007_FORMAT_MPEG2:
562 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
564 case GO7007_FORMAT_MPEG4:
565 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4;
571 case V4L2_CID_MPEG_VIDEO_ASPECT:
572 switch (go->aspect_ratio) {
573 case GO7007_RATIO_1_1:
574 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_1x1;
576 case GO7007_RATIO_4_3:
577 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_4x3;
579 case GO7007_RATIO_16_9:
580 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_16x9;
586 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
587 ctrl->value = go->gop_size;
589 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
590 ctrl->value = go->closed_gop;
592 case V4L2_CID_MPEG_VIDEO_BITRATE:
593 ctrl->value = go->bitrate;
601 static int vidioc_querycap(struct file *file, void *priv,
602 struct v4l2_capability *cap)
604 struct go7007 *go = ((struct go7007_file *) priv)->go;
606 strlcpy(cap->driver, "go7007", sizeof(cap->driver));
607 strlcpy(cap->card, go->name, sizeof(cap->card));
609 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
612 cap->version = KERNEL_VERSION(0, 9, 8);
614 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
615 V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
617 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
618 cap->capabilities |= V4L2_CAP_TUNER;
623 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
624 struct v4l2_fmtdesc *fmt)
628 switch (fmt->index) {
630 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
631 desc = "Motion-JPEG";
634 fmt->pixelformat = V4L2_PIX_FMT_MPEG;
635 desc = "MPEG1/MPEG2/MPEG4";
640 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
641 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
643 strncpy(fmt->description, desc, sizeof(fmt->description));
648 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
649 struct v4l2_format *fmt)
651 struct go7007 *go = ((struct go7007_file *) priv)->go;
653 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
654 fmt->fmt.pix.width = go->width;
655 fmt->fmt.pix.height = go->height;
656 fmt->fmt.pix.pixelformat = (go->format == GO7007_FORMAT_MJPEG) ?
657 V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
658 fmt->fmt.pix.field = V4L2_FIELD_NONE;
659 fmt->fmt.pix.bytesperline = 0;
660 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
661 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
666 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
667 struct v4l2_format *fmt)
669 struct go7007 *go = ((struct go7007_file *) priv)->go;
671 return set_capture_size(go, fmt, 1);
674 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
675 struct v4l2_format *fmt)
677 struct go7007 *go = ((struct go7007_file *) priv)->go;
682 return set_capture_size(go, fmt, 0);
685 static int vidioc_reqbufs(struct file *file, void *priv,
686 struct v4l2_requestbuffers *req)
688 struct go7007_file *gofh = priv;
689 struct go7007 *go = gofh->go;
691 unsigned int count, i;
696 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
697 req->memory != V4L2_MEMORY_MMAP)
700 mutex_lock(&gofh->lock);
701 for (i = 0; i < gofh->buf_count; ++i)
702 if (gofh->bufs[i].mapped > 0)
703 goto unlock_and_return;
705 mutex_lock(&go->hw_lock);
706 if (go->in_use > 0 && gofh->buf_count == 0) {
707 mutex_unlock(&go->hw_lock);
708 goto unlock_and_return;
711 if (gofh->buf_count > 0)
722 gofh->bufs = kmalloc(count * sizeof(struct go7007_buffer),
726 mutex_unlock(&go->hw_lock);
727 goto unlock_and_return;
730 memset(gofh->bufs, 0, count * sizeof(struct go7007_buffer));
732 for (i = 0; i < count; ++i) {
733 gofh->bufs[i].go = go;
734 gofh->bufs[i].index = i;
735 gofh->bufs[i].state = BUF_STATE_IDLE;
736 gofh->bufs[i].mapped = 0;
744 gofh->buf_count = count;
745 mutex_unlock(&go->hw_lock);
746 mutex_unlock(&gofh->lock);
748 memset(req, 0, sizeof(*req));
751 req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
752 req->memory = V4L2_MEMORY_MMAP;
757 mutex_unlock(&gofh->lock);
761 static int vidioc_querybuf(struct file *file, void *priv,
762 struct v4l2_buffer *buf)
764 struct go7007_file *gofh = priv;
765 int retval = -EINVAL;
768 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
773 mutex_lock(&gofh->lock);
774 if (index >= gofh->buf_count)
775 goto unlock_and_return;
777 memset(buf, 0, sizeof(*buf));
779 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
781 switch (gofh->bufs[index].state) {
782 case BUF_STATE_QUEUED:
783 buf->flags = V4L2_BUF_FLAG_QUEUED;
786 buf->flags = V4L2_BUF_FLAG_DONE;
792 if (gofh->bufs[index].mapped)
793 buf->flags |= V4L2_BUF_FLAG_MAPPED;
794 buf->memory = V4L2_MEMORY_MMAP;
795 buf->m.offset = index * GO7007_BUF_SIZE;
796 buf->length = GO7007_BUF_SIZE;
797 mutex_unlock(&gofh->lock);
802 mutex_unlock(&gofh->lock);
806 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
808 struct go7007_file *gofh = priv;
809 struct go7007 *go = gofh->go;
810 struct go7007_buffer *gobuf;
812 int retval = -EINVAL;
815 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
816 buf->memory != V4L2_MEMORY_MMAP)
819 mutex_lock(&gofh->lock);
820 if (buf->index < 0 || buf->index >= gofh->buf_count)
821 goto unlock_and_return;
823 gobuf = &gofh->bufs[buf->index];
825 goto unlock_and_return;
828 if (gobuf->state != BUF_STATE_IDLE)
829 goto unlock_and_return;
831 /* offset will be 0 until we really support USERPTR streaming */
832 gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
833 gobuf->bytesused = 0;
834 gobuf->frame_offset = 0;
835 gobuf->modet_active = 0;
836 if (gobuf->offset > 0)
837 gobuf->page_count = GO7007_BUF_PAGES + 1;
839 gobuf->page_count = GO7007_BUF_PAGES;
842 down_read(¤t->mm->mmap_sem);
843 ret = get_user_pages(current, current->mm,
844 gobuf->user_addr & PAGE_MASK, gobuf->page_count,
845 1, 1, gobuf->pages, NULL);
846 up_read(¤t->mm->mmap_sem);
848 if (ret != gobuf->page_count) {
850 for (i = 0; i < ret; ++i)
851 page_cache_release(gobuf->pages[i]);
852 gobuf->page_count = 0;
853 goto unlock_and_return;
856 gobuf->state = BUF_STATE_QUEUED;
857 spin_lock_irqsave(&go->spinlock, flags);
858 list_add_tail(&gobuf->stream, &go->stream);
859 spin_unlock_irqrestore(&go->spinlock, flags);
860 mutex_unlock(&gofh->lock);
865 mutex_unlock(&gofh->lock);
870 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
872 struct go7007_file *gofh = priv;
873 struct go7007 *go = gofh->go;
874 struct go7007_buffer *gobuf;
875 int retval = -EINVAL;
880 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
882 if (buf->memory != V4L2_MEMORY_MMAP)
885 mutex_lock(&gofh->lock);
886 if (list_empty(&go->stream))
887 goto unlock_and_return;
888 gobuf = list_entry(go->stream.next,
889 struct go7007_buffer, stream);
892 if (gobuf->state != BUF_STATE_DONE &&
893 !(file->f_flags & O_NONBLOCK)) {
895 prepare_to_wait(&go->frame_waitq, &wait,
897 if (gobuf->state == BUF_STATE_DONE)
899 if (signal_pending(current)) {
900 retval = -ERESTARTSYS;
905 finish_wait(&go->frame_waitq, &wait);
907 if (gobuf->state != BUF_STATE_DONE)
908 goto unlock_and_return;
910 spin_lock_irqsave(&go->spinlock, flags);
911 deactivate_buffer(gobuf);
912 spin_unlock_irqrestore(&go->spinlock, flags);
913 frame_type_flag = get_frame_type_flag(gobuf, go->format);
914 gobuf->state = BUF_STATE_IDLE;
916 memset(buf, 0, sizeof(*buf));
917 buf->index = gobuf->index;
918 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
919 buf->bytesused = gobuf->bytesused;
920 buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
921 buf->field = V4L2_FIELD_NONE;
922 buf->timestamp = gobuf->timestamp;
923 buf->sequence = gobuf->seq;
924 buf->memory = V4L2_MEMORY_MMAP;
925 buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
926 buf->length = GO7007_BUF_SIZE;
927 buf->reserved = gobuf->modet_active;
929 mutex_unlock(&gofh->lock);
933 mutex_unlock(&gofh->lock);
937 static int vidioc_streamon(struct file *file, void *priv,
938 enum v4l2_buf_type type)
940 struct go7007_file *gofh = priv;
941 struct go7007 *go = gofh->go;
944 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
947 mutex_lock(&gofh->lock);
948 mutex_lock(&go->hw_lock);
950 if (!go->streaming) {
953 go->active_buf = NULL;
954 if (go7007_start_encoder(go) < 0)
959 mutex_unlock(&go->hw_lock);
960 mutex_unlock(&gofh->lock);
965 static int vidioc_streamoff(struct file *file, void *priv,
966 enum v4l2_buf_type type)
968 struct go7007_file *gofh = priv;
969 struct go7007 *go = gofh->go;
971 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
973 mutex_lock(&gofh->lock);
974 go7007_streamoff(go);
975 mutex_unlock(&gofh->lock);
980 static int vidioc_queryctrl(struct file *file, void *priv,
981 struct v4l2_queryctrl *query)
983 struct go7007 *go = ((struct go7007_file *) priv)->go;
986 if (0 == call_all(&go->v4l2_dev, core, queryctrl, query))
990 return mpeg_query_ctrl(query);
993 static int vidioc_g_ctrl(struct file *file, void *priv,
994 struct v4l2_control *ctrl)
996 struct go7007 *go = ((struct go7007_file *) priv)->go;
998 if (0 == call_all(&go->v4l2_dev, core, g_ctrl, ctrl))
1001 return mpeg_g_ctrl(ctrl, go);
1004 static int vidioc_s_ctrl(struct file *file, void *priv,
1005 struct v4l2_control *ctrl)
1007 struct go7007 *go = ((struct go7007_file *) priv)->go;
1009 if (0 == call_all(&go->v4l2_dev, core, s_ctrl, ctrl))
1012 return mpeg_s_ctrl(ctrl, go);
1015 static int vidioc_g_parm(struct file *filp, void *priv,
1016 struct v4l2_streamparm *parm)
1018 struct go7007 *go = ((struct go7007_file *) priv)->go;
1019 struct v4l2_fract timeperframe = {
1020 .numerator = 1001 * go->fps_scale,
1021 .denominator = go->sensor_framerate,
1024 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1027 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
1028 parm->parm.capture.timeperframe = timeperframe;
1033 static int vidioc_s_parm(struct file *filp, void *priv,
1034 struct v4l2_streamparm *parm)
1036 struct go7007 *go = ((struct go7007_file *) priv)->go;
1039 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1041 if (parm->parm.capture.capturemode != 0)
1044 n = go->sensor_framerate *
1045 parm->parm.capture.timeperframe.numerator;
1046 d = 1001 * parm->parm.capture.timeperframe.denominator;
1047 if (n != 0 && d != 0 && n > d)
1048 go->fps_scale = (n + d/2) / d;
1055 /* VIDIOC_ENUMSTD on go7007 were used for enumberating the supported fps and
1056 its resolution, when the device is not connected to TV.
1057 This were an API abuse, probably used by the lack of specific IOCTL's to
1058 enumberate it, by the time the driver were written.
1060 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
1061 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
1063 The two functions bellow implements the newer ioctls
1065 static int vidioc_enum_framesizes(struct file *filp, void *priv,
1066 struct v4l2_frmsizeenum *fsize)
1068 struct go7007 *go = ((struct go7007_file *) priv)->go;
1070 /* Return -EINVAL, if it is a TV board */
1071 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1072 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1075 if (fsize->index > 0)
1078 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1079 fsize->discrete.width = go->board_info->sensor_width;
1080 fsize->discrete.height = go->board_info->sensor_height;
1085 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1086 struct v4l2_frmivalenum *fival)
1088 struct go7007 *go = ((struct go7007_file *) priv)->go;
1090 /* Return -EINVAL, if it is a TV board */
1091 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1092 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1095 if (fival->index > 0)
1098 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1099 fival->discrete.numerator = 1001;
1100 fival->discrete.denominator = go->board_info->sensor_framerate;
1105 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
1107 struct go7007 *go = ((struct go7007_file *) priv)->go;
1109 switch (go->standard) {
1110 case GO7007_STD_NTSC:
1111 *std = V4L2_STD_NTSC;
1113 case GO7007_STD_PAL:
1114 *std = V4L2_STD_PAL;
1123 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
1125 struct go7007 *go = ((struct go7007_file *) priv)->go;
1130 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) && *std != 0)
1136 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1137 go->input == go->board_info->num_inputs - 1) {
1138 if (!go->i2c_adapter_online)
1140 if (call_all(&go->v4l2_dev, core, s_std, *std) < 0)
1144 if (*std & V4L2_STD_NTSC) {
1145 go->standard = GO7007_STD_NTSC;
1146 go->sensor_framerate = 30000;
1147 } else if (*std & V4L2_STD_PAL) {
1148 go->standard = GO7007_STD_PAL;
1149 go->sensor_framerate = 25025;
1150 } else if (*std & V4L2_STD_SECAM) {
1151 go->standard = GO7007_STD_PAL;
1152 go->sensor_framerate = 25025;
1156 call_all(&go->v4l2_dev, core, s_std, *std);
1157 set_capture_size(go, NULL, 0);
1162 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
1164 struct go7007 *go = ((struct go7007_file *) priv)->go;
1166 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1167 go->input == go->board_info->num_inputs - 1) {
1168 if (!go->i2c_adapter_online)
1170 return call_all(&go->v4l2_dev, video, querystd, std);
1171 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1172 *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
1179 static int vidioc_enum_input(struct file *file, void *priv,
1180 struct v4l2_input *inp)
1182 struct go7007 *go = ((struct go7007_file *) priv)->go;
1184 if (inp->index >= go->board_info->num_inputs)
1187 strncpy(inp->name, go->board_info->inputs[inp->index].name,
1190 /* If this board has a tuner, it will be the last input */
1191 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1192 inp->index == go->board_info->num_inputs - 1)
1193 inp->type = V4L2_INPUT_TYPE_TUNER;
1195 inp->type = V4L2_INPUT_TYPE_CAMERA;
1199 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1200 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
1209 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
1211 struct go7007 *go = ((struct go7007_file *) priv)->go;
1218 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
1220 struct go7007 *go = ((struct go7007_file *) priv)->go;
1222 if (input >= go->board_info->num_inputs)
1229 return call_all(&go->v4l2_dev, video, s_routing, input, 0, 0);
1232 static int vidioc_g_tuner(struct file *file, void *priv,
1233 struct v4l2_tuner *t)
1235 struct go7007 *go = ((struct go7007_file *) priv)->go;
1237 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1241 if (!go->i2c_adapter_online)
1244 return call_all(&go->v4l2_dev, tuner, g_tuner, t);
1247 static int vidioc_s_tuner(struct file *file, void *priv,
1248 struct v4l2_tuner *t)
1250 struct go7007 *go = ((struct go7007_file *) priv)->go;
1252 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1256 if (!go->i2c_adapter_online)
1259 switch (go->board_id) {
1260 case GO7007_BOARDID_PX_TV402U_NA:
1261 case GO7007_BOARDID_PX_TV402U_JP:
1262 /* No selectable options currently */
1263 if (t->audmode != V4L2_TUNER_MODE_STEREO)
1268 return call_all(&go->v4l2_dev, tuner, s_tuner, t);
1271 static int vidioc_g_frequency(struct file *file, void *priv,
1272 struct v4l2_frequency *f)
1274 struct go7007 *go = ((struct go7007_file *) priv)->go;
1276 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1278 if (!go->i2c_adapter_online)
1281 f->type = V4L2_TUNER_ANALOG_TV;
1283 return call_all(&go->v4l2_dev, tuner, g_frequency, f);
1286 static int vidioc_s_frequency(struct file *file, void *priv,
1287 struct v4l2_frequency *f)
1289 struct go7007 *go = ((struct go7007_file *) priv)->go;
1291 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1293 if (!go->i2c_adapter_online)
1296 return call_all(&go->v4l2_dev, tuner, s_frequency, f);
1299 static int vidioc_cropcap(struct file *file, void *priv,
1300 struct v4l2_cropcap *cropcap)
1302 struct go7007 *go = ((struct go7007_file *) priv)->go;
1304 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1307 /* These specify the raw input of the sensor */
1308 switch (go->standard) {
1309 case GO7007_STD_NTSC:
1310 cropcap->bounds.top = 0;
1311 cropcap->bounds.left = 0;
1312 cropcap->bounds.width = 720;
1313 cropcap->bounds.height = 480;
1314 cropcap->defrect.top = 0;
1315 cropcap->defrect.left = 0;
1316 cropcap->defrect.width = 720;
1317 cropcap->defrect.height = 480;
1319 case GO7007_STD_PAL:
1320 cropcap->bounds.top = 0;
1321 cropcap->bounds.left = 0;
1322 cropcap->bounds.width = 720;
1323 cropcap->bounds.height = 576;
1324 cropcap->defrect.top = 0;
1325 cropcap->defrect.left = 0;
1326 cropcap->defrect.width = 720;
1327 cropcap->defrect.height = 576;
1329 case GO7007_STD_OTHER:
1330 cropcap->bounds.top = 0;
1331 cropcap->bounds.left = 0;
1332 cropcap->bounds.width = go->board_info->sensor_width;
1333 cropcap->bounds.height = go->board_info->sensor_height;
1334 cropcap->defrect.top = 0;
1335 cropcap->defrect.left = 0;
1336 cropcap->defrect.width = go->board_info->sensor_width;
1337 cropcap->defrect.height = go->board_info->sensor_height;
1344 static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1346 struct go7007 *go = ((struct go7007_file *) priv)->go;
1348 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1351 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1353 /* These specify the raw input of the sensor */
1354 switch (go->standard) {
1355 case GO7007_STD_NTSC:
1358 crop->c.width = 720;
1359 crop->c.height = 480;
1361 case GO7007_STD_PAL:
1364 crop->c.width = 720;
1365 crop->c.height = 576;
1367 case GO7007_STD_OTHER:
1370 crop->c.width = go->board_info->sensor_width;
1371 crop->c.height = go->board_info->sensor_height;
1378 /* FIXME: vidioc_s_crop is not really implemented!!!
1380 static int vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1382 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1388 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1389 struct v4l2_jpegcompression *params)
1391 memset(params, 0, sizeof(*params));
1392 params->quality = 50; /* ?? */
1393 params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
1394 V4L2_JPEG_MARKER_DQT;
1399 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1400 struct v4l2_jpegcompression *params)
1402 if (params->quality != 50 ||
1403 params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
1404 V4L2_JPEG_MARKER_DQT))
1411 Those ioctls are private, and not needed, since several standard
1412 extended controls already provide streaming control.
1413 So, those ioctls should be converted into vidioc_g_ext_ctrls()
1414 and vidioc_s_ext_ctrls()
1418 /* Temporary ioctls for controlling compression characteristics */
1419 case GO7007IOC_S_BITRATE:
1425 /* Upper bound is kind of arbitrary here */
1426 if (*bitrate < 64000 || *bitrate > 10000000)
1428 go->bitrate = *bitrate;
1431 case GO7007IOC_G_BITRATE:
1435 *bitrate = go->bitrate;
1438 case GO7007IOC_S_COMP_PARAMS:
1440 struct go7007_comp_params *comp = arg;
1442 if (go->format == GO7007_FORMAT_MJPEG)
1444 if (comp->gop_size > 0)
1445 go->gop_size = comp->gop_size;
1447 go->gop_size = go->sensor_framerate / 1000;
1448 if (go->gop_size != 15)
1450 /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
1451 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
1452 switch (comp->aspect_ratio) {
1453 case GO7007_ASPECT_RATIO_4_3_NTSC:
1454 case GO7007_ASPECT_RATIO_4_3_PAL:
1455 go->aspect_ratio = GO7007_RATIO_4_3;
1457 case GO7007_ASPECT_RATIO_16_9_NTSC:
1458 case GO7007_ASPECT_RATIO_16_9_PAL:
1459 go->aspect_ratio = GO7007_RATIO_16_9;
1462 go->aspect_ratio = GO7007_RATIO_1_1;
1466 if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
1468 go->seq_header_enable = 0;
1470 go->seq_header_enable = 1;
1474 case GO7007IOC_G_COMP_PARAMS:
1476 struct go7007_comp_params *comp = arg;
1478 if (go->format == GO7007_FORMAT_MJPEG)
1480 memset(comp, 0, sizeof(*comp));
1481 comp->gop_size = go->gop_size;
1482 comp->max_b_frames = go->ipb ? 2 : 0;
1483 switch (go->aspect_ratio) {
1484 case GO7007_RATIO_4_3:
1485 if (go->standard == GO7007_STD_NTSC)
1486 comp->aspect_ratio =
1487 GO7007_ASPECT_RATIO_4_3_NTSC;
1489 comp->aspect_ratio =
1490 GO7007_ASPECT_RATIO_4_3_PAL;
1492 case GO7007_RATIO_16_9:
1493 if (go->standard == GO7007_STD_NTSC)
1494 comp->aspect_ratio =
1495 GO7007_ASPECT_RATIO_16_9_NTSC;
1497 comp->aspect_ratio =
1498 GO7007_ASPECT_RATIO_16_9_PAL;
1501 comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
1505 comp->flags |= GO7007_COMP_CLOSED_GOP;
1506 if (!go->seq_header_enable)
1507 comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
1510 case GO7007IOC_S_MPEG_PARAMS:
1512 struct go7007_mpeg_params *mpeg = arg;
1514 if (go->format != GO7007_FORMAT_MPEG1 &&
1515 go->format != GO7007_FORMAT_MPEG2 &&
1516 go->format != GO7007_FORMAT_MPEG4)
1519 if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
1520 go->format = GO7007_FORMAT_MPEG2;
1521 go->bitrate = 9800000;
1525 go->repeat_seqhead = 0;
1526 go->seq_header_enable = 1;
1527 go->gop_header_enable = 1;
1530 switch (mpeg->mpeg_video_standard) {
1531 case GO7007_MPEG_VIDEO_MPEG1:
1532 go->format = GO7007_FORMAT_MPEG1;
1535 case GO7007_MPEG_VIDEO_MPEG2:
1536 go->format = GO7007_FORMAT_MPEG2;
1537 if (mpeg->pali >> 24 == 2)
1538 go->pali = mpeg->pali & 0xff;
1542 case GO7007_MPEG_VIDEO_MPEG4:
1543 go->format = GO7007_FORMAT_MPEG4;
1544 if (mpeg->pali >> 24 == 4)
1545 go->pali = mpeg->pali & 0xff;
1552 go->gop_header_enable =
1553 mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
1555 if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
1556 go->repeat_seqhead = 1;
1558 go->repeat_seqhead = 0;
1563 case GO7007IOC_G_MPEG_PARAMS:
1565 struct go7007_mpeg_params *mpeg = arg;
1567 memset(mpeg, 0, sizeof(*mpeg));
1568 switch (go->format) {
1569 case GO7007_FORMAT_MPEG1:
1570 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
1573 case GO7007_FORMAT_MPEG2:
1574 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
1575 mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
1577 case GO7007_FORMAT_MPEG4:
1578 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
1579 mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
1584 if (!go->gop_header_enable)
1585 mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
1586 if (go->repeat_seqhead)
1587 mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
1589 mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
1592 case GO7007IOC_S_MD_PARAMS:
1594 struct go7007_md_params *mdp = arg;
1596 if (mdp->region > 3)
1598 if (mdp->trigger > 0) {
1599 go->modet[mdp->region].pixel_threshold =
1600 mdp->pixel_threshold >> 1;
1601 go->modet[mdp->region].motion_threshold =
1602 mdp->motion_threshold >> 1;
1603 go->modet[mdp->region].mb_threshold =
1605 go->modet[mdp->region].enable = 1;
1607 go->modet[mdp->region].enable = 0;
1610 case GO7007IOC_G_MD_PARAMS:
1612 struct go7007_md_params *mdp = arg;
1613 int region = mdp->region;
1615 if (mdp->region > 3)
1617 memset(mdp, 0, sizeof(struct go7007_md_params));
1618 mdp->region = region;
1619 if (!go->modet[region].enable)
1621 mdp->pixel_threshold =
1622 (go->modet[region].pixel_threshold << 1) + 1;
1623 mdp->motion_threshold =
1624 (go->modet[region].motion_threshold << 1) + 1;
1626 (go->modet[region].mb_threshold << 1) + 1;
1629 case GO7007IOC_S_MD_REGION:
1631 struct go7007_md_region *region = arg;
1633 if (region->region < 1 || region->region > 3)
1635 return clip_to_modet_map(go, region->region, region->clips);
1639 static ssize_t go7007_read(struct file *file, char __user *data,
1640 size_t count, loff_t *ppos)
1645 static void go7007_vm_open(struct vm_area_struct *vma)
1647 struct go7007_buffer *gobuf = vma->vm_private_data;
1652 static void go7007_vm_close(struct vm_area_struct *vma)
1654 struct go7007_buffer *gobuf = vma->vm_private_data;
1655 unsigned long flags;
1657 if (--gobuf->mapped == 0) {
1658 spin_lock_irqsave(&gobuf->go->spinlock, flags);
1659 deactivate_buffer(gobuf);
1660 spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
1664 /* Copied from videobuf-dma-sg.c */
1665 static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1669 page = alloc_page(GFP_USER | __GFP_DMA32);
1671 return VM_FAULT_OOM;
1672 clear_user_highpage(page, (unsigned long)vmf->virtual_address);
1677 static struct vm_operations_struct go7007_vm_ops = {
1678 .open = go7007_vm_open,
1679 .close = go7007_vm_close,
1680 .fault = go7007_vm_fault,
1683 static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
1685 struct go7007_file *gofh = file->private_data;
1688 if (gofh->go->status != STATUS_ONLINE)
1690 if (!(vma->vm_flags & VM_SHARED))
1691 return -EINVAL; /* only support VM_SHARED mapping */
1692 if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
1693 return -EINVAL; /* must map exactly one full buffer */
1694 mutex_lock(&gofh->lock);
1695 index = vma->vm_pgoff / GO7007_BUF_PAGES;
1696 if (index >= gofh->buf_count) {
1697 mutex_unlock(&gofh->lock);
1698 return -EINVAL; /* trying to map beyond requested buffers */
1700 if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
1701 mutex_unlock(&gofh->lock);
1702 return -EINVAL; /* offset is not aligned on buffer boundary */
1704 if (gofh->bufs[index].mapped > 0) {
1705 mutex_unlock(&gofh->lock);
1708 gofh->bufs[index].mapped = 1;
1709 gofh->bufs[index].user_addr = vma->vm_start;
1710 vma->vm_ops = &go7007_vm_ops;
1711 vma->vm_flags |= VM_DONTEXPAND;
1712 vma->vm_flags &= ~VM_IO;
1713 vma->vm_private_data = &gofh->bufs[index];
1714 mutex_unlock(&gofh->lock);
1718 static unsigned int go7007_poll(struct file *file, poll_table *wait)
1720 struct go7007_file *gofh = file->private_data;
1721 struct go7007_buffer *gobuf;
1723 if (list_empty(&gofh->go->stream))
1725 gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
1726 poll_wait(file, &gofh->go->frame_waitq, wait);
1727 if (gobuf->state == BUF_STATE_DONE)
1728 return POLLIN | POLLRDNORM;
1732 static void go7007_vfl_release(struct video_device *vfd)
1734 struct go7007 *go = video_get_drvdata(vfd);
1736 video_device_release(vfd);
1737 if (--go->ref_count == 0)
1741 static struct v4l2_file_operations go7007_fops = {
1742 .owner = THIS_MODULE,
1743 .open = go7007_open,
1744 .release = go7007_release,
1745 .ioctl = video_ioctl2,
1746 .read = go7007_read,
1747 .mmap = go7007_mmap,
1748 .poll = go7007_poll,
1751 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1752 .vidioc_querycap = vidioc_querycap,
1753 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1754 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1755 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1756 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1757 .vidioc_reqbufs = vidioc_reqbufs,
1758 .vidioc_querybuf = vidioc_querybuf,
1759 .vidioc_qbuf = vidioc_qbuf,
1760 .vidioc_dqbuf = vidioc_dqbuf,
1761 .vidioc_g_std = vidioc_g_std,
1762 .vidioc_s_std = vidioc_s_std,
1763 .vidioc_querystd = vidioc_querystd,
1764 .vidioc_enum_input = vidioc_enum_input,
1765 .vidioc_g_input = vidioc_g_input,
1766 .vidioc_s_input = vidioc_s_input,
1767 .vidioc_queryctrl = vidioc_queryctrl,
1768 .vidioc_g_ctrl = vidioc_g_ctrl,
1769 .vidioc_s_ctrl = vidioc_s_ctrl,
1770 .vidioc_streamon = vidioc_streamon,
1771 .vidioc_streamoff = vidioc_streamoff,
1772 .vidioc_g_tuner = vidioc_g_tuner,
1773 .vidioc_s_tuner = vidioc_s_tuner,
1774 .vidioc_g_frequency = vidioc_g_frequency,
1775 .vidioc_s_frequency = vidioc_s_frequency,
1776 .vidioc_g_parm = vidioc_g_parm,
1777 .vidioc_s_parm = vidioc_s_parm,
1778 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1779 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1780 .vidioc_cropcap = vidioc_cropcap,
1781 .vidioc_g_crop = vidioc_g_crop,
1782 .vidioc_s_crop = vidioc_s_crop,
1783 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1784 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1787 static struct video_device go7007_template = {
1789 .fops = &go7007_fops,
1790 .release = go7007_vfl_release,
1791 .ioctl_ops = &video_ioctl_ops,
1792 .tvnorms = V4L2_STD_ALL,
1793 .current_norm = V4L2_STD_NTSC,
1796 int go7007_v4l2_init(struct go7007 *go)
1800 go->video_dev = video_device_alloc();
1801 if (go->video_dev == NULL)
1803 *go->video_dev = go7007_template;
1804 go->video_dev->parent = go->dev;
1805 rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
1807 video_device_release(go->video_dev);
1808 go->video_dev = NULL;
1811 rv = v4l2_device_register(go->dev, &go->v4l2_dev);
1813 video_device_release(go->video_dev);
1814 go->video_dev = NULL;
1817 video_set_drvdata(go->video_dev, go);
1819 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1820 go->video_dev->name, video_device_node_name(go->video_dev));
1825 void go7007_v4l2_remove(struct go7007 *go)
1827 unsigned long flags;
1829 mutex_lock(&go->hw_lock);
1830 if (go->streaming) {
1832 go7007_stream_stop(go);
1833 spin_lock_irqsave(&go->spinlock, flags);
1835 spin_unlock_irqrestore(&go->spinlock, flags);
1837 mutex_unlock(&go->hw_lock);
1839 video_unregister_device(go->video_dev);
1840 v4l2_device_unregister(&go->v4l2_dev);