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;
331 static int clip_to_modet_map(struct go7007 *go, int region,
332 struct v4l2_clip *clip_list)
334 struct v4l2_clip clip, *clip_ptr;
337 /* Check if coordinates are OK and if any macroblocks are already
338 * used by other regions (besides 0) */
339 clip_ptr = clip_list;
341 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
343 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
344 clip.c.width <= 0 || (clip.c.width & 0xF))
346 if (clip.c.left + clip.c.width > go->width)
348 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
349 clip.c.height <= 0 || (clip.c.height & 0xF))
351 if (clip.c.top + clip.c.height > go->height)
353 for (y = 0; y < clip.c.height; y += 16)
354 for (x = 0; x < clip.c.width; x += 16) {
355 mbnum = (go->width >> 4) *
356 ((clip.c.top + y) >> 4) +
357 ((clip.c.left + x) >> 4);
358 if (go->modet_map[mbnum] != 0 &&
359 go->modet_map[mbnum] != region)
362 clip_ptr = clip.next;
365 /* Clear old region macroblocks */
366 for (mbnum = 0; mbnum < 1624; ++mbnum)
367 if (go->modet_map[mbnum] == region)
368 go->modet_map[mbnum] = 0;
370 /* Claim macroblocks in this list */
371 clip_ptr = clip_list;
373 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
375 for (y = 0; y < clip.c.height; y += 16)
376 for (x = 0; x < clip.c.width; x += 16) {
377 mbnum = (go->width >> 4) *
378 ((clip.c.top + y) >> 4) +
379 ((clip.c.left + x) >> 4);
380 go->modet_map[mbnum] = region;
382 clip_ptr = clip.next;
387 static int mpeg_queryctrl(u32 id, struct v4l2_queryctrl *ctrl)
389 static const u32 user_ctrls[] = {
393 static const u32 mpeg_ctrls[] = {
395 V4L2_CID_MPEG_STREAM_TYPE,
396 V4L2_CID_MPEG_VIDEO_ENCODING,
397 V4L2_CID_MPEG_VIDEO_ASPECT,
398 V4L2_CID_MPEG_VIDEO_GOP_SIZE,
399 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
400 V4L2_CID_MPEG_VIDEO_BITRATE,
403 static const u32 *ctrl_classes[] = {
409 /* The ctrl may already contain the queried i2c controls,
410 * query the mpeg controls if the existing ctrl id is
411 * greater than the next mpeg ctrl id.
413 id = v4l2_ctrl_next(ctrl_classes, id);
414 if (id >= ctrl->id && ctrl->name[0])
417 memset(ctrl, 0, sizeof(*ctrl));
421 case V4L2_CID_USER_CLASS:
422 case V4L2_CID_MPEG_CLASS:
423 return v4l2_ctrl_query_fill_std(ctrl);
424 case V4L2_CID_MPEG_STREAM_TYPE:
425 return v4l2_ctrl_query_fill(ctrl,
426 V4L2_MPEG_STREAM_TYPE_MPEG2_DVD,
427 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM, 1,
428 V4L2_MPEG_STREAM_TYPE_MPEG_ELEM);
429 case V4L2_CID_MPEG_VIDEO_ENCODING:
430 return v4l2_ctrl_query_fill(ctrl,
431 V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
432 V4L2_MPEG_VIDEO_ENCODING_MPEG_4, 1,
433 V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
434 case V4L2_CID_MPEG_VIDEO_ASPECT:
435 return v4l2_ctrl_query_fill(ctrl,
436 V4L2_MPEG_VIDEO_ASPECT_1x1,
437 V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
438 V4L2_MPEG_VIDEO_ASPECT_1x1);
439 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
440 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
441 return v4l2_ctrl_query_fill_std(ctrl);
442 case V4L2_CID_MPEG_VIDEO_BITRATE:
443 return v4l2_ctrl_query_fill(ctrl,
453 static int mpeg_s_control(struct v4l2_control *ctrl, struct go7007 *go)
455 /* pretty sure we can't change any of these while streaming */
460 case V4L2_CID_MPEG_STREAM_TYPE:
461 switch (ctrl->value) {
462 case V4L2_MPEG_STREAM_TYPE_MPEG2_DVD:
463 go->format = GO7007_FORMAT_MPEG2;
464 go->bitrate = 9800000;
468 go->repeat_seqhead = 0;
469 go->seq_header_enable = 1;
470 go->gop_header_enable = 1;
473 case V4L2_MPEG_STREAM_TYPE_MPEG_ELEM:
480 case V4L2_CID_MPEG_VIDEO_ENCODING:
481 switch (ctrl->value) {
482 case V4L2_MPEG_VIDEO_ENCODING_MPEG_1:
483 go->format = GO7007_FORMAT_MPEG1;
486 case V4L2_MPEG_VIDEO_ENCODING_MPEG_2:
487 go->format = GO7007_FORMAT_MPEG2;
488 /*if (mpeg->pali >> 24 == 2)
489 go->pali = mpeg->pali & 0xff;
493 case V4L2_MPEG_VIDEO_ENCODING_MPEG_4:
494 go->format = GO7007_FORMAT_MPEG4;
495 /*if (mpeg->pali >> 24 == 4)
496 go->pali = mpeg->pali & 0xff;
503 go->gop_header_enable =
504 /*mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
506 /*if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
507 go->repeat_seqhead = 1;
509 go->repeat_seqhead = 0;
512 case V4L2_CID_MPEG_VIDEO_ASPECT:
513 if (go->format == GO7007_FORMAT_MJPEG)
515 switch (ctrl->value) {
516 case V4L2_MPEG_VIDEO_ASPECT_1x1:
517 go->aspect_ratio = GO7007_RATIO_1_1;
519 case V4L2_MPEG_VIDEO_ASPECT_4x3:
520 go->aspect_ratio = GO7007_RATIO_4_3;
522 case V4L2_MPEG_VIDEO_ASPECT_16x9:
523 go->aspect_ratio = GO7007_RATIO_16_9;
525 case V4L2_MPEG_VIDEO_ASPECT_221x100:
530 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
531 go->gop_size = ctrl->value;
533 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
534 if (ctrl->value != 0 && ctrl->value != 1)
536 go->closed_gop = ctrl->value;
538 case V4L2_CID_MPEG_VIDEO_BITRATE:
539 /* Upper bound is kind of arbitrary here */
540 if (ctrl->value < 64000 || ctrl->value > 10000000)
542 go->bitrate = ctrl->value;
550 static int mpeg_g_control(struct v4l2_control *ctrl, struct go7007 *go)
553 case V4L2_CID_MPEG_STREAM_TYPE:
555 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
557 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG_ELEM;
559 case V4L2_CID_MPEG_VIDEO_ENCODING:
560 switch (go->format) {
561 case GO7007_FORMAT_MPEG1:
562 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
564 case GO7007_FORMAT_MPEG2:
565 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
567 case GO7007_FORMAT_MPEG4:
568 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4;
574 case V4L2_CID_MPEG_VIDEO_ASPECT:
575 switch (go->aspect_ratio) {
576 case GO7007_RATIO_1_1:
577 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_1x1;
579 case GO7007_RATIO_4_3:
580 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_4x3;
582 case GO7007_RATIO_16_9:
583 ctrl->value = V4L2_MPEG_VIDEO_ASPECT_16x9;
589 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
590 ctrl->value = go->gop_size;
592 case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
593 ctrl->value = go->closed_gop;
595 case V4L2_CID_MPEG_VIDEO_BITRATE:
596 ctrl->value = go->bitrate;
605 static int vidioc_querycap(struct file *file, void *priv,
606 struct v4l2_capability *cap)
608 struct go7007_file *gofh = priv;
609 struct go7007 *go = gofh->go;
611 strlcpy(cap->driver, "go7007", sizeof(cap->driver));
612 strlcpy(cap->card, go->name, sizeof(cap->card));
614 strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
617 cap->version = KERNEL_VERSION(0, 9, 8);
619 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
620 V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
622 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
623 cap->capabilities |= V4L2_CAP_TUNER;
628 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
629 struct v4l2_fmtdesc *fmt)
633 switch (fmt->index) {
635 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
636 desc = "Motion-JPEG";
639 fmt->pixelformat = V4L2_PIX_FMT_MPEG;
640 desc = "MPEG1/MPEG2/MPEG4";
645 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
646 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
648 strncpy(fmt->description, desc, sizeof(fmt->description));
653 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
654 struct v4l2_format *fmt)
656 struct go7007_file *gofh = priv;
657 struct go7007 *go = gofh->go;
659 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
660 fmt->fmt.pix.width = go->width;
661 fmt->fmt.pix.height = go->height;
662 fmt->fmt.pix.pixelformat = (go->format == GO7007_FORMAT_MJPEG) ?
663 V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
664 fmt->fmt.pix.field = V4L2_FIELD_NONE;
665 fmt->fmt.pix.bytesperline = 0;
666 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
667 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
672 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
673 struct v4l2_format *fmt)
675 struct go7007_file *gofh = priv;
676 struct go7007 *go = gofh->go;
678 return set_capture_size(go, fmt, 1);
681 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
682 struct v4l2_format *fmt)
684 struct go7007_file *gofh = priv;
685 struct go7007 *go = gofh->go;
690 return set_capture_size(go, fmt, 0);
693 static int vidioc_reqbufs(struct file *file, void *priv,
694 struct v4l2_requestbuffers *req)
696 struct go7007_file *gofh = priv;
697 struct go7007 *go = gofh->go;
699 unsigned int count, i;
704 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
705 req->memory != V4L2_MEMORY_MMAP)
709 for (i = 0; i < gofh->buf_count; ++i)
710 if (gofh->bufs[i].mapped > 0)
711 goto unlock_and_return;
714 if (go->in_use > 0 && gofh->buf_count == 0) {
716 goto unlock_and_return;
719 if (gofh->buf_count > 0)
730 gofh->bufs = kmalloc(count * sizeof(struct go7007_buffer),
735 goto unlock_and_return;
738 memset(gofh->bufs, 0, count * sizeof(struct go7007_buffer));
740 for (i = 0; i < count; ++i) {
741 gofh->bufs[i].go = go;
742 gofh->bufs[i].index = i;
743 gofh->bufs[i].state = BUF_STATE_IDLE;
744 gofh->bufs[i].mapped = 0;
752 gofh->buf_count = count;
756 memset(req, 0, sizeof(*req));
759 req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
760 req->memory = V4L2_MEMORY_MMAP;
769 static int vidioc_querybuf(struct file *file, void *priv,
770 struct v4l2_buffer *buf)
772 struct go7007_file *gofh = priv;
773 int retval = -EINVAL;
776 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
782 if (index >= gofh->buf_count)
783 goto unlock_and_return;
785 memset(buf, 0, sizeof(*buf));
787 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
789 switch (gofh->bufs[index].state) {
790 case BUF_STATE_QUEUED:
791 buf->flags = V4L2_BUF_FLAG_QUEUED;
794 buf->flags = V4L2_BUF_FLAG_DONE;
800 if (gofh->bufs[index].mapped)
801 buf->flags |= V4L2_BUF_FLAG_MAPPED;
802 buf->memory = V4L2_MEMORY_MMAP;
803 buf->m.offset = index * GO7007_BUF_SIZE;
804 buf->length = GO7007_BUF_SIZE;
814 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
816 struct go7007_file *gofh = priv;
817 struct go7007 *go = gofh->go;
818 struct go7007_buffer *gobuf;
820 int retval = -EINVAL;
823 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
824 buf->memory != V4L2_MEMORY_MMAP)
828 if (buf->index < 0 || buf->index >= gofh->buf_count)
829 goto unlock_and_return;
831 gobuf = &gofh->bufs[buf->index];
833 goto unlock_and_return;
836 if (gobuf->state != BUF_STATE_IDLE)
837 goto unlock_and_return;
839 /* offset will be 0 until we really support USERPTR streaming */
840 gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
841 gobuf->bytesused = 0;
842 gobuf->frame_offset = 0;
843 gobuf->modet_active = 0;
844 if (gobuf->offset > 0)
845 gobuf->page_count = GO7007_BUF_PAGES + 1;
847 gobuf->page_count = GO7007_BUF_PAGES;
850 down_read(¤t->mm->mmap_sem);
851 ret = get_user_pages(current, current->mm,
852 gobuf->user_addr & PAGE_MASK, gobuf->page_count,
853 1, 1, gobuf->pages, NULL);
854 up_read(¤t->mm->mmap_sem);
856 if (ret != gobuf->page_count) {
858 for (i = 0; i < ret; ++i)
859 page_cache_release(gobuf->pages[i]);
860 gobuf->page_count = 0;
861 goto unlock_and_return;
864 gobuf->state = BUF_STATE_QUEUED;
865 spin_lock_irqsave(&go->spinlock, flags);
866 list_add_tail(&gobuf->stream, &go->stream);
867 spin_unlock_irqrestore(&go->spinlock, flags);
878 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
880 struct go7007_file *gofh = priv;
881 struct go7007 *go = gofh->go;
882 struct go7007_buffer *gobuf;
883 int retval = -EINVAL;
888 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
890 if (buf->memory != V4L2_MEMORY_MMAP)
894 if (list_empty(&go->stream))
895 goto unlock_and_return;
896 gobuf = list_entry(go->stream.next,
897 struct go7007_buffer, stream);
900 if (gobuf->state != BUF_STATE_DONE &&
901 !(file->f_flags & O_NONBLOCK)) {
903 prepare_to_wait(&go->frame_waitq, &wait,
905 if (gobuf->state == BUF_STATE_DONE)
907 if (signal_pending(current)) {
908 retval = -ERESTARTSYS;
913 finish_wait(&go->frame_waitq, &wait);
915 if (gobuf->state != BUF_STATE_DONE)
916 goto unlock_and_return;
918 spin_lock_irqsave(&go->spinlock, flags);
919 deactivate_buffer(gobuf);
920 spin_unlock_irqrestore(&go->spinlock, flags);
921 frame_type_flag = get_frame_type_flag(gobuf, go->format);
922 gobuf->state = BUF_STATE_IDLE;
924 memset(buf, 0, sizeof(*buf));
925 buf->index = gobuf->index;
926 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
927 buf->bytesused = gobuf->bytesused;
928 buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
929 buf->field = V4L2_FIELD_NONE;
930 buf->timestamp = gobuf->timestamp;
931 buf->sequence = gobuf->seq;
932 buf->memory = V4L2_MEMORY_MMAP;
933 buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
934 buf->length = GO7007_BUF_SIZE;
935 buf->reserved = gobuf->modet_active;
945 static int vidioc_streamon(struct file *file, void *priv,
946 enum v4l2_buf_type type)
948 struct go7007_file *gofh = priv;
949 struct go7007 *go = gofh->go;
952 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
958 if (!go->streaming) {
961 go->active_buf = NULL;
962 if (go7007_start_encoder(go) < 0)
973 static int vidioc_streamoff(struct file *file, void *priv,
974 enum v4l2_buf_type type)
976 struct go7007_file *gofh = priv;
977 struct go7007 *go = gofh->go;
979 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
982 go7007_streamoff(go);
988 static int vidioc_queryctrl(struct file *file, void *priv,
989 struct v4l2_queryctrl *query)
991 struct go7007_file *gofh = priv;
992 struct go7007 *go = gofh->go;
994 if (!go->i2c_adapter_online)
997 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, query);
999 return (!query->name[0]) ? -EINVAL : 0;
1002 static int vidioc_g_ctrl(struct file *file, void *priv,
1003 struct v4l2_control *ctrl)
1005 struct go7007_file *gofh = priv;
1006 struct go7007 *go = gofh->go;
1007 struct v4l2_queryctrl query;
1009 if (!go->i2c_adapter_online)
1012 memset(&query, 0, sizeof(query));
1013 query.id = ctrl->id;
1014 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
1015 if (query.name[0] == 0)
1017 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_CTRL, ctrl);
1022 static int vidioc_s_ctrl(struct file *file, void *priv,
1023 struct v4l2_control *ctrl)
1025 struct go7007_file *gofh = priv;
1026 struct go7007 *go = gofh->go;
1027 struct v4l2_queryctrl query;
1029 if (!go->i2c_adapter_online)
1032 memset(&query, 0, sizeof(query));
1033 query.id = ctrl->id;
1034 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
1035 if (query.name[0] == 0)
1037 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_CTRL, ctrl);
1042 static int vidioc_g_parm(struct file *filp, void *priv,
1043 struct v4l2_streamparm *parm)
1045 struct go7007_file *gofh = priv;
1046 struct go7007 *go = gofh->go;
1047 struct v4l2_fract timeperframe = {
1048 .numerator = 1001 * go->fps_scale,
1049 .denominator = go->sensor_framerate,
1052 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1055 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
1056 parm->parm.capture.timeperframe = timeperframe;
1061 static int vidioc_s_parm(struct file *filp, void *priv,
1062 struct v4l2_streamparm *parm)
1064 struct go7007_file *gofh = priv;
1065 struct go7007 *go = gofh->go;
1068 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1070 if (parm->parm.capture.capturemode != 0)
1073 n = go->sensor_framerate *
1074 parm->parm.capture.timeperframe.numerator;
1075 d = 1001 * parm->parm.capture.timeperframe.denominator;
1076 if (n != 0 && d != 0 && n > d)
1077 go->fps_scale = (n + d/2) / d;
1084 /* VIDIOC_ENUMSTD on go7007 were used for enumberating the supported fps and
1085 its resolution, when the device is not connected to TV.
1086 This were an API abuse, probably used by the lack of specific IOCTL's to
1087 enumberate it, by the time the driver were written.
1089 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
1090 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
1092 The two functions bellow implements the newer ioctls
1094 static int vidioc_enum_framesizes(struct file *filp, void *priv,
1095 struct v4l2_frmsizeenum *fsize)
1097 struct go7007_file *gofh = priv;
1098 struct go7007 *go = gofh->go;
1100 /* Return -EINVAL, if it is a TV board */
1101 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1102 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1105 if (fsize->index > 0)
1108 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1109 fsize->discrete.width = go->board_info->sensor_width;
1110 fsize->discrete.height = go->board_info->sensor_height;
1115 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
1116 struct v4l2_frmivalenum *fival)
1118 struct go7007_file *gofh = priv;
1119 struct go7007 *go = gofh->go;
1121 /* Return -EINVAL, if it is a TV board */
1122 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
1123 (go->board_info->sensor_flags & GO7007_SENSOR_TV))
1126 if (fival->index > 0)
1129 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1130 fival->discrete.numerator = 1001;
1131 fival->discrete.denominator = go->board_info->sensor_framerate;
1136 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
1138 struct go7007_file *gofh = priv;
1139 struct go7007 *go = gofh->go;
1144 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) &&
1151 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1152 go->input == go->board_info->num_inputs - 1) {
1153 if (!go->i2c_adapter_online)
1155 i2c_clients_command(&go->i2c_adapter,
1157 if (!*std) /* hack to indicate EINVAL from tuner */
1161 if (*std & V4L2_STD_NTSC) {
1162 go->standard = GO7007_STD_NTSC;
1163 go->sensor_framerate = 30000;
1164 } else if (*std & V4L2_STD_PAL) {
1165 go->standard = GO7007_STD_PAL;
1166 go->sensor_framerate = 25025;
1167 } else if (*std & V4L2_STD_SECAM) {
1168 go->standard = GO7007_STD_PAL;
1169 go->sensor_framerate = 25025;
1173 if (go->i2c_adapter_online)
1174 i2c_clients_command(&go->i2c_adapter,
1176 set_capture_size(go, NULL, 0);
1182 case VIDIOC_QUERYSTD:
1184 v4l2_std_id *std = arg;
1186 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1187 go->input == go->board_info->num_inputs - 1) {
1188 if (!go->i2c_adapter_online)
1190 i2c_clients_command(&go->i2c_adapter,
1191 VIDIOC_QUERYSTD, arg);
1192 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1193 *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
1200 static int vidioc_enum_input(struct file *file, void *priv,
1201 struct v4l2_input *inp)
1203 struct go7007_file *gofh = priv;
1204 struct go7007 *go = gofh->go;
1206 if (inp->index >= go->board_info->num_inputs)
1209 strncpy(inp->name, go->board_info->inputs[inp->index].name,
1212 /* If this board has a tuner, it will be the last input */
1213 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
1214 inp->index == go->board_info->num_inputs - 1)
1215 inp->type = V4L2_INPUT_TYPE_TUNER;
1217 inp->type = V4L2_INPUT_TYPE_CAMERA;
1221 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1222 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
1231 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
1233 struct go7007_file *gofh = priv;
1234 struct go7007 *go = gofh->go;
1241 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
1243 struct go7007_file *gofh = priv;
1244 struct go7007 *go = gofh->go;
1246 if (input >= go->board_info->num_inputs)
1252 if (go->i2c_adapter_online) {
1253 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_INPUT,
1254 &go->board_info->inputs[input].video_input);
1255 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_AUDIO,
1256 &go->board_info->inputs[input].audio_input);
1262 static int vidioc_g_tuner(struct file *file, void *priv,
1263 struct v4l2_tuner *t)
1265 struct go7007_file *gofh = priv;
1266 struct go7007 *go = gofh->go;
1268 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1272 if (!go->i2c_adapter_online)
1275 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_TUNER, t);
1281 static int vidioc_s_tuner(struct file *file, void *priv,
1282 struct v4l2_tuner *t)
1284 struct go7007_file *gofh = priv;
1285 struct go7007 *go = gofh->go;
1287 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1291 if (!go->i2c_adapter_online)
1294 switch (go->board_id) {
1295 case GO7007_BOARDID_PX_TV402U_NA:
1296 case GO7007_BOARDID_PX_TV402U_JP:
1297 /* No selectable options currently */
1298 if (t->audmode != V4L2_TUNER_MODE_STEREO)
1303 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_TUNER, t);
1308 static int vidioc_g_frequency(struct file *file, void *priv,
1309 struct v4l2_frequency *f)
1311 struct go7007_file *gofh = priv;
1312 struct go7007 *go = gofh->go;
1314 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1316 if (!go->i2c_adapter_online)
1319 f->type = V4L2_TUNER_ANALOG_TV;
1320 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_FREQUENCY, f);
1324 static int vidioc_s_frequency(struct file *file, void *priv,
1325 struct v4l2_frequency *f)
1327 struct go7007_file *gofh = priv;
1328 struct go7007 *go = gofh->go;
1330 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
1332 if (!go->i2c_adapter_online)
1335 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_FREQUENCY, f);
1340 static int vidioc_cropcap(struct file *file, void *priv,
1341 struct v4l2_cropcap *cropcap)
1343 struct go7007_file *gofh = priv;
1344 struct go7007 *go = gofh->go;
1346 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1349 /* These specify the raw input of the sensor */
1350 switch (go->standard) {
1351 case GO7007_STD_NTSC:
1352 cropcap->bounds.top = 0;
1353 cropcap->bounds.left = 0;
1354 cropcap->bounds.width = 720;
1355 cropcap->bounds.height = 480;
1356 cropcap->defrect.top = 0;
1357 cropcap->defrect.left = 0;
1358 cropcap->defrect.width = 720;
1359 cropcap->defrect.height = 480;
1361 case GO7007_STD_PAL:
1362 cropcap->bounds.top = 0;
1363 cropcap->bounds.left = 0;
1364 cropcap->bounds.width = 720;
1365 cropcap->bounds.height = 576;
1366 cropcap->defrect.top = 0;
1367 cropcap->defrect.left = 0;
1368 cropcap->defrect.width = 720;
1369 cropcap->defrect.height = 576;
1371 case GO7007_STD_OTHER:
1372 cropcap->bounds.top = 0;
1373 cropcap->bounds.left = 0;
1374 cropcap->bounds.width = go->board_info->sensor_width;
1375 cropcap->bounds.height = go->board_info->sensor_height;
1376 cropcap->defrect.top = 0;
1377 cropcap->defrect.left = 0;
1378 cropcap->defrect.width = go->board_info->sensor_width;
1379 cropcap->defrect.height = go->board_info->sensor_height;
1386 static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1388 struct go7007_file *gofh = priv;
1389 struct go7007 *go = gofh->go;
1391 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1394 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1396 /* These specify the raw input of the sensor */
1397 switch (go->standard) {
1398 case GO7007_STD_NTSC:
1401 crop->c.width = 720;
1402 crop->c.height = 480;
1404 case GO7007_STD_PAL:
1407 crop->c.width = 720;
1408 crop->c.height = 576;
1410 case GO7007_STD_OTHER:
1413 crop->c.width = go->board_info->sensor_width;
1414 crop->c.height = go->board_info->sensor_height;
1421 /* FIXME: vidioc_s_crop is not really implemented!!!
1423 static int vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1425 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1431 static int vidioc_g_jpegcomp(struct file *file, void *priv,
1432 struct v4l2_jpegcompression *params)
1434 memset(params, 0, sizeof(*params));
1435 params->quality = 50; /* ?? */
1436 params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
1437 V4L2_JPEG_MARKER_DQT;
1442 static int vidioc_s_jpegcomp(struct file *file, void *priv,
1443 struct v4l2_jpegcompression *params)
1445 if (params->quality != 50 ||
1446 params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
1447 V4L2_JPEG_MARKER_DQT))
1454 Those ioctls are private, and not needed, since several standard
1455 extended controls already provide streaming control.
1456 So, those ioctls should be converted into vidioc_g_ext_ctrls()
1457 and vidioc_s_ext_ctrls()
1461 /* Temporary ioctls for controlling compression characteristics */
1462 case GO7007IOC_S_BITRATE:
1468 /* Upper bound is kind of arbitrary here */
1469 if (*bitrate < 64000 || *bitrate > 10000000)
1471 go->bitrate = *bitrate;
1474 case GO7007IOC_G_BITRATE:
1478 *bitrate = go->bitrate;
1481 case GO7007IOC_S_COMP_PARAMS:
1483 struct go7007_comp_params *comp = arg;
1485 if (go->format == GO7007_FORMAT_MJPEG)
1487 if (comp->gop_size > 0)
1488 go->gop_size = comp->gop_size;
1490 go->gop_size = go->sensor_framerate / 1000;
1491 if (go->gop_size != 15)
1493 /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
1494 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
1495 switch (comp->aspect_ratio) {
1496 case GO7007_ASPECT_RATIO_4_3_NTSC:
1497 case GO7007_ASPECT_RATIO_4_3_PAL:
1498 go->aspect_ratio = GO7007_RATIO_4_3;
1500 case GO7007_ASPECT_RATIO_16_9_NTSC:
1501 case GO7007_ASPECT_RATIO_16_9_PAL:
1502 go->aspect_ratio = GO7007_RATIO_16_9;
1505 go->aspect_ratio = GO7007_RATIO_1_1;
1509 if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
1511 go->seq_header_enable = 0;
1513 go->seq_header_enable = 1;
1517 case GO7007IOC_G_COMP_PARAMS:
1519 struct go7007_comp_params *comp = arg;
1521 if (go->format == GO7007_FORMAT_MJPEG)
1523 memset(comp, 0, sizeof(*comp));
1524 comp->gop_size = go->gop_size;
1525 comp->max_b_frames = go->ipb ? 2 : 0;
1526 switch (go->aspect_ratio) {
1527 case GO7007_RATIO_4_3:
1528 if (go->standard == GO7007_STD_NTSC)
1529 comp->aspect_ratio =
1530 GO7007_ASPECT_RATIO_4_3_NTSC;
1532 comp->aspect_ratio =
1533 GO7007_ASPECT_RATIO_4_3_PAL;
1535 case GO7007_RATIO_16_9:
1536 if (go->standard == GO7007_STD_NTSC)
1537 comp->aspect_ratio =
1538 GO7007_ASPECT_RATIO_16_9_NTSC;
1540 comp->aspect_ratio =
1541 GO7007_ASPECT_RATIO_16_9_PAL;
1544 comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
1548 comp->flags |= GO7007_COMP_CLOSED_GOP;
1549 if (!go->seq_header_enable)
1550 comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
1553 case GO7007IOC_S_MPEG_PARAMS:
1555 struct go7007_mpeg_params *mpeg = arg;
1557 if (go->format != GO7007_FORMAT_MPEG1 &&
1558 go->format != GO7007_FORMAT_MPEG2 &&
1559 go->format != GO7007_FORMAT_MPEG4)
1562 if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
1563 go->format = GO7007_FORMAT_MPEG2;
1564 go->bitrate = 9800000;
1568 go->repeat_seqhead = 0;
1569 go->seq_header_enable = 1;
1570 go->gop_header_enable = 1;
1573 switch (mpeg->mpeg_video_standard) {
1574 case GO7007_MPEG_VIDEO_MPEG1:
1575 go->format = GO7007_FORMAT_MPEG1;
1578 case GO7007_MPEG_VIDEO_MPEG2:
1579 go->format = GO7007_FORMAT_MPEG2;
1580 if (mpeg->pali >> 24 == 2)
1581 go->pali = mpeg->pali & 0xff;
1585 case GO7007_MPEG_VIDEO_MPEG4:
1586 go->format = GO7007_FORMAT_MPEG4;
1587 if (mpeg->pali >> 24 == 4)
1588 go->pali = mpeg->pali & 0xff;
1595 go->gop_header_enable =
1596 mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
1598 if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
1599 go->repeat_seqhead = 1;
1601 go->repeat_seqhead = 0;
1606 case GO7007IOC_G_MPEG_PARAMS:
1608 struct go7007_mpeg_params *mpeg = arg;
1610 memset(mpeg, 0, sizeof(*mpeg));
1611 switch (go->format) {
1612 case GO7007_FORMAT_MPEG1:
1613 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
1616 case GO7007_FORMAT_MPEG2:
1617 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
1618 mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
1620 case GO7007_FORMAT_MPEG4:
1621 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
1622 mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
1627 if (!go->gop_header_enable)
1628 mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
1629 if (go->repeat_seqhead)
1630 mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
1632 mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
1635 case GO7007IOC_S_MD_PARAMS:
1637 struct go7007_md_params *mdp = arg;
1639 if (mdp->region > 3)
1641 if (mdp->trigger > 0) {
1642 go->modet[mdp->region].pixel_threshold =
1643 mdp->pixel_threshold >> 1;
1644 go->modet[mdp->region].motion_threshold =
1645 mdp->motion_threshold >> 1;
1646 go->modet[mdp->region].mb_threshold =
1648 go->modet[mdp->region].enable = 1;
1650 go->modet[mdp->region].enable = 0;
1653 case GO7007IOC_G_MD_PARAMS:
1655 struct go7007_md_params *mdp = arg;
1656 int region = mdp->region;
1658 if (mdp->region > 3)
1660 memset(mdp, 0, sizeof(struct go7007_md_params));
1661 mdp->region = region;
1662 if (!go->modet[region].enable)
1664 mdp->pixel_threshold =
1665 (go->modet[region].pixel_threshold << 1) + 1;
1666 mdp->motion_threshold =
1667 (go->modet[region].motion_threshold << 1) + 1;
1669 (go->modet[region].mb_threshold << 1) + 1;
1672 case GO7007IOC_S_MD_REGION:
1674 struct go7007_md_region *region = arg;
1676 if (region->region < 1 || region->region > 3)
1678 return clip_to_modet_map(go, region->region, region->clips);
1682 static ssize_t go7007_read(struct file *file, char __user *data,
1683 size_t count, loff_t *ppos)
1688 static void go7007_vm_open(struct vm_area_struct *vma)
1690 struct go7007_buffer *gobuf = vma->vm_private_data;
1695 static void go7007_vm_close(struct vm_area_struct *vma)
1697 struct go7007_buffer *gobuf = vma->vm_private_data;
1698 unsigned long flags;
1700 if (--gobuf->mapped == 0) {
1701 spin_lock_irqsave(&gobuf->go->spinlock, flags);
1702 deactivate_buffer(gobuf);
1703 spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
1707 /* Copied from videobuf-dma-sg.c */
1708 static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1712 page = alloc_page(GFP_USER | __GFP_DMA32);
1714 return VM_FAULT_OOM;
1715 clear_user_highpage(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 .vidioc_enum_framesizes = vidioc_enum_framesizes,
1820 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
1821 .vidioc_cropcap = vidioc_cropcap,
1822 .vidioc_g_crop = vidioc_g_crop,
1823 .vidioc_s_crop = vidioc_s_crop,
1824 .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
1825 .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
1828 static struct video_device go7007_template = {
1830 .vfl_type = VID_TYPE_CAPTURE,
1831 .fops = &go7007_fops,
1833 .release = go7007_vfl_release,
1834 .ioctl_ops = &video_ioctl_ops,
1835 .tvnorms = V4L2_STD_ALL,
1836 .current_norm = V4L2_STD_NTSC,
1839 int go7007_v4l2_init(struct go7007 *go)
1843 go->video_dev = video_device_alloc();
1844 if (go->video_dev == NULL)
1846 memcpy(go->video_dev, &go7007_template, sizeof(go7007_template));
1847 go->video_dev->parent = go->dev;
1848 rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
1850 video_device_release(go->video_dev);
1851 go->video_dev = NULL;
1854 video_set_drvdata(go->video_dev, go);
1856 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
1857 go->video_dev->name, go->video_dev->num);
1862 void go7007_v4l2_remove(struct go7007 *go)
1864 unsigned long flags;
1867 if (go->streaming) {
1869 go7007_stream_stop(go);
1870 spin_lock_irqsave(&go->spinlock, flags);
1872 spin_unlock_irqrestore(&go->spinlock, flags);
1876 video_unregister_device(go->video_dev);