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 static void deactivate_buffer(struct go7007_buffer *gobuf)
45 if (gobuf->state != BUF_STATE_IDLE) {
46 list_del(&gobuf->stream);
47 gobuf->state = BUF_STATE_IDLE;
49 if (gobuf->page_count > 0) {
50 for (i = 0; i < gobuf->page_count; ++i)
51 page_cache_release(gobuf->pages[i]);
52 gobuf->page_count = 0;
56 static void abort_queued(struct go7007 *go)
58 struct go7007_buffer *gobuf, *next;
60 list_for_each_entry_safe(gobuf, next, &go->stream, stream) {
61 deactivate_buffer(gobuf);
65 static int go7007_streamoff(struct go7007 *go)
73 go7007_stream_stop(go);
74 spin_lock_irqsave(&go->spinlock, flags);
76 spin_unlock_irqrestore(&go->spinlock, flags);
77 go7007_reset_encoder(go);
84 static int go7007_open(struct inode *inode, struct file *file)
86 struct go7007 *go = video_get_drvdata(video_devdata(file));
87 struct go7007_file *gofh;
89 if (go->status != STATUS_ONLINE)
91 gofh = kmalloc(sizeof(struct go7007_file), GFP_KERNEL);
96 init_MUTEX(&gofh->lock);
98 file->private_data = gofh;
102 static int go7007_release(struct inode *inode, struct file *file)
104 struct go7007_file *gofh = file->private_data;
105 struct go7007 *go = gofh->go;
107 if (gofh->buf_count > 0) {
108 go7007_streamoff(go);
114 if (--go->ref_count == 0)
116 file->private_data = NULL;
120 static u32 get_frame_type_flag(struct go7007_buffer *gobuf, int format)
122 u8 *f = page_address(gobuf->pages[0]);
125 case GO7007_FORMAT_MJPEG:
126 return V4L2_BUF_FLAG_KEYFRAME;
127 case GO7007_FORMAT_MPEG4:
128 switch ((f[gobuf->frame_offset + 4] >> 6) & 0x3) {
130 return V4L2_BUF_FLAG_KEYFRAME;
132 return V4L2_BUF_FLAG_PFRAME;
134 return V4L2_BUF_FLAG_BFRAME;
138 case GO7007_FORMAT_MPEG1:
139 case GO7007_FORMAT_MPEG2:
140 switch ((f[gobuf->frame_offset + 5] >> 3) & 0x7) {
142 return V4L2_BUF_FLAG_KEYFRAME;
144 return V4L2_BUF_FLAG_PFRAME;
146 return V4L2_BUF_FLAG_BFRAME;
155 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
157 int sensor_height = 0, sensor_width = 0;
158 int width, height, i;
160 if (fmt != NULL && fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
161 fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG &&
162 fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG4)
165 switch (go->standard) {
166 case GO7007_STD_NTSC:
174 case GO7007_STD_OTHER:
175 sensor_width = go->board_info->sensor_width;
176 sensor_height = go->board_info->sensor_height;
181 width = sensor_width;
182 height = sensor_height;
183 } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
184 if (fmt->fmt.pix.width > sensor_width)
185 width = sensor_width;
186 else if (fmt->fmt.pix.width < 144)
189 width = fmt->fmt.pix.width & ~0x0f;
191 if (fmt->fmt.pix.height > sensor_height)
192 height = sensor_height;
193 else if (fmt->fmt.pix.height < 96)
196 height = fmt->fmt.pix.height & ~0x0f;
198 int requested_size = fmt->fmt.pix.width * fmt->fmt.pix.height;
199 int sensor_size = sensor_width * sensor_height;
201 if (64 * requested_size < 9 * sensor_size) {
202 width = sensor_width / 4;
203 height = sensor_height / 4;
204 } else if (64 * requested_size < 36 * sensor_size) {
205 width = sensor_width / 2;
206 height = sensor_height / 2;
208 width = sensor_width;
209 height = sensor_height;
216 u32 pixelformat = fmt->fmt.pix.pixelformat;
218 memset(fmt, 0, sizeof(*fmt));
219 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
220 fmt->fmt.pix.width = width;
221 fmt->fmt.pix.height = height;
222 fmt->fmt.pix.pixelformat = pixelformat;
223 fmt->fmt.pix.field = V4L2_FIELD_NONE;
224 fmt->fmt.pix.bytesperline = 0;
225 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
226 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
234 go->encoder_h_offset = go->board_info->sensor_h_offset;
235 go->encoder_v_offset = go->board_info->sensor_v_offset;
236 for (i = 0; i < 4; ++i)
237 go->modet[i].enable = 0;
238 for (i = 0; i < 1624; ++i)
239 go->modet_map[i] = 0;
241 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
242 struct video_decoder_resolution res;
245 if (height > sensor_height / 2) {
246 res.height = height / 2;
247 go->encoder_v_halve = 0;
250 go->encoder_v_halve = 1;
252 if (go->i2c_adapter_online)
253 i2c_clients_command(&go->i2c_adapter,
254 DECODER_SET_RESOLUTION, &res);
256 if (width <= sensor_width / 4) {
257 go->encoder_h_halve = 1;
258 go->encoder_v_halve = 1;
259 go->encoder_subsample = 1;
260 } else if (width <= sensor_width / 2) {
261 go->encoder_h_halve = 1;
262 go->encoder_v_halve = 1;
263 go->encoder_subsample = 0;
265 go->encoder_h_halve = 0;
266 go->encoder_v_halve = 0;
267 go->encoder_subsample = 0;
274 switch (fmt->fmt.pix.pixelformat) {
275 case V4L2_PIX_FMT_MPEG:
276 if (go->format == GO7007_FORMAT_MPEG1 ||
277 go->format == GO7007_FORMAT_MPEG2 ||
278 go->format == GO7007_FORMAT_MPEG4)
280 go->format = GO7007_FORMAT_MPEG1;
282 go->aspect_ratio = GO7007_RATIO_1_1;
283 go->gop_size = go->sensor_framerate / 1000;
286 go->repeat_seqhead = 1;
287 go->seq_header_enable = 1;
288 go->gop_header_enable = 1;
291 /* Backwards compatibility only! */
292 case V4L2_PIX_FMT_MPEG4:
293 if (go->format == GO7007_FORMAT_MPEG4)
295 go->format = GO7007_FORMAT_MPEG4;
297 go->aspect_ratio = GO7007_RATIO_1_1;
298 go->gop_size = go->sensor_framerate / 1000;
301 go->repeat_seqhead = 1;
302 go->seq_header_enable = 1;
303 go->gop_header_enable = 1;
306 case V4L2_PIX_FMT_MJPEG:
307 go->format = GO7007_FORMAT_MJPEG;
309 go->aspect_ratio = GO7007_RATIO_1_1;
313 go->repeat_seqhead = 0;
314 go->seq_header_enable = 0;
315 go->gop_header_enable = 0;
322 static int clip_to_modet_map(struct go7007 *go, int region,
323 struct v4l2_clip *clip_list)
325 struct v4l2_clip clip, *clip_ptr;
328 /* Check if coordinates are OK and if any macroblocks are already
329 * used by other regions (besides 0) */
330 clip_ptr = clip_list;
332 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
334 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
335 clip.c.width <= 0 || (clip.c.width & 0xF))
337 if (clip.c.left + clip.c.width > go->width)
339 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
340 clip.c.height <= 0 || (clip.c.height & 0xF))
342 if (clip.c.top + clip.c.height > go->height)
344 for (y = 0; y < clip.c.height; y += 16)
345 for (x = 0; x < clip.c.width; x += 16) {
346 mbnum = (go->width >> 4) *
347 ((clip.c.top + y) >> 4) +
348 ((clip.c.left + x) >> 4);
349 if (go->modet_map[mbnum] != 0 &&
350 go->modet_map[mbnum] != region)
353 clip_ptr = clip.next;
356 /* Clear old region macroblocks */
357 for (mbnum = 0; mbnum < 1624; ++mbnum)
358 if (go->modet_map[mbnum] == region)
359 go->modet_map[mbnum] = 0;
361 /* Claim macroblocks in this list */
362 clip_ptr = clip_list;
364 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
366 for (y = 0; y < clip.c.height; y += 16)
367 for (x = 0; x < clip.c.width; x += 16) {
368 mbnum = (go->width >> 4) *
369 ((clip.c.top + y) >> 4) +
370 ((clip.c.left + x) >> 4);
371 go->modet_map[mbnum] = region;
373 clip_ptr = clip.next;
378 static int go7007_do_ioctl(struct inode *inode, struct file *file,
379 unsigned int cmd, void *arg)
381 struct go7007_file *gofh = file->private_data;
382 struct go7007 *go = gofh->go;
387 case VIDIOC_QUERYCAP:
389 struct v4l2_capability *cap = arg;
391 memset(cap, 0, sizeof(*cap));
392 strcpy(cap->driver, "go7007");
393 strncpy(cap->card, go->name, sizeof(cap->card));
394 cap->version = KERNEL_VERSION(0, 9, 8);
395 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
396 V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
397 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
398 cap->capabilities |= V4L2_CAP_TUNER;
401 case VIDIOC_ENUM_FMT:
403 struct v4l2_fmtdesc *fmt = arg;
408 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
410 switch (fmt->index) {
412 pixelformat = V4L2_PIX_FMT_MJPEG;
413 desc = "Motion-JPEG";
416 pixelformat = V4L2_PIX_FMT_MPEG;
417 desc = "MPEG1/MPEG2/MPEG4";
423 memset(fmt, 0, sizeof(*fmt));
425 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
426 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
427 strncpy(fmt->description, desc, sizeof(fmt->description));
428 fmt->pixelformat = pixelformat;
434 struct v4l2_format *fmt = arg;
436 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
438 return set_capture_size(go, fmt, 1);
442 struct v4l2_format *fmt = arg;
444 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
446 memset(fmt, 0, sizeof(*fmt));
447 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
448 fmt->fmt.pix.width = go->width;
449 fmt->fmt.pix.height = go->height;
450 fmt->fmt.pix.pixelformat = go->format == GO7007_FORMAT_MJPEG ?
451 V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
452 fmt->fmt.pix.field = V4L2_FIELD_NONE;
453 fmt->fmt.pix.bytesperline = 0;
454 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
455 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
460 struct v4l2_format *fmt = arg;
462 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
466 return set_capture_size(go, fmt, 0);
473 struct v4l2_requestbuffers *req = arg;
474 unsigned int count, i;
478 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
479 req->memory != V4L2_MEMORY_MMAP)
484 for (i = 0; i < gofh->buf_count; ++i)
485 if (gofh->bufs[i].mapped > 0)
486 goto unlock_and_return;
488 if (go->in_use > 0 && gofh->buf_count == 0) {
490 goto unlock_and_return;
492 if (gofh->buf_count > 0)
501 gofh->bufs = kmalloc(count *
502 sizeof(struct go7007_buffer),
504 if (gofh->bufs == NULL) {
506 goto unlock_and_return;
508 memset(gofh->bufs, 0,
509 count * sizeof(struct go7007_buffer));
510 for (i = 0; i < count; ++i) {
511 gofh->bufs[i].go = go;
512 gofh->bufs[i].index = i;
513 gofh->bufs[i].state = BUF_STATE_IDLE;
514 gofh->bufs[i].mapped = 0;
520 gofh->buf_count = count;
523 memset(req, 0, sizeof(*req));
525 req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
526 req->memory = V4L2_MEMORY_MMAP;
529 case VIDIOC_QUERYBUF:
531 struct v4l2_buffer *buf = arg;
535 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
539 if (index >= gofh->buf_count)
540 goto unlock_and_return;
541 memset(buf, 0, sizeof(*buf));
543 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
544 switch (gofh->bufs[index].state) {
545 case BUF_STATE_QUEUED:
546 buf->flags = V4L2_BUF_FLAG_QUEUED;
549 buf->flags = V4L2_BUF_FLAG_DONE;
554 if (gofh->bufs[index].mapped)
555 buf->flags |= V4L2_BUF_FLAG_MAPPED;
556 buf->memory = V4L2_MEMORY_MMAP;
557 buf->m.offset = index * GO7007_BUF_SIZE;
558 buf->length = GO7007_BUF_SIZE;
565 struct v4l2_buffer *buf = arg;
566 struct go7007_buffer *gobuf;
570 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
571 buf->memory != V4L2_MEMORY_MMAP)
574 if (buf->index < 0 || buf->index >= gofh->buf_count)
575 goto unlock_and_return;
576 gobuf = &gofh->bufs[buf->index];
577 if (gobuf->mapped == 0)
578 goto unlock_and_return;
580 if (gobuf->state != BUF_STATE_IDLE)
581 goto unlock_and_return;
582 /* offset will be 0 until we really support USERPTR streaming */
583 gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
584 gobuf->bytesused = 0;
585 gobuf->frame_offset = 0;
586 gobuf->modet_active = 0;
587 if (gobuf->offset > 0)
588 gobuf->page_count = GO7007_BUF_PAGES + 1;
590 gobuf->page_count = GO7007_BUF_PAGES;
592 down_read(¤t->mm->mmap_sem);
593 ret = get_user_pages(current, current->mm,
594 gobuf->user_addr & PAGE_MASK, gobuf->page_count,
595 1, 1, gobuf->pages, NULL);
596 up_read(¤t->mm->mmap_sem);
597 if (ret != gobuf->page_count) {
599 for (i = 0; i < ret; ++i)
600 page_cache_release(gobuf->pages[i]);
601 gobuf->page_count = 0;
602 goto unlock_and_return;
604 gobuf->state = BUF_STATE_QUEUED;
605 spin_lock_irqsave(&go->spinlock, flags);
606 list_add_tail(&gobuf->stream, &go->stream);
607 spin_unlock_irqrestore(&go->spinlock, flags);
613 struct v4l2_buffer *buf = arg;
614 struct go7007_buffer *gobuf;
618 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
620 if (buf->memory != V4L2_MEMORY_MMAP)
624 if (list_empty(&go->stream))
625 goto unlock_and_return;
626 gobuf = list_entry(go->stream.next,
627 struct go7007_buffer, stream);
629 if (gobuf->state != BUF_STATE_DONE &&
630 !(file->f_flags & O_NONBLOCK)) {
632 prepare_to_wait(&go->frame_waitq, &wait,
634 if (gobuf->state == BUF_STATE_DONE)
636 if (signal_pending(current)) {
637 retval = -ERESTARTSYS;
642 finish_wait(&go->frame_waitq, &wait);
644 if (gobuf->state != BUF_STATE_DONE)
645 goto unlock_and_return;
646 spin_lock_irqsave(&go->spinlock, flags);
647 deactivate_buffer(gobuf);
648 spin_unlock_irqrestore(&go->spinlock, flags);
649 frame_type_flag = get_frame_type_flag(gobuf, go->format);
650 gobuf->state = BUF_STATE_IDLE;
651 memset(buf, 0, sizeof(*buf));
652 buf->index = gobuf->index;
653 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
654 buf->bytesused = gobuf->bytesused;
655 buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
656 buf->field = V4L2_FIELD_NONE;
657 buf->timestamp = gobuf->timestamp;
658 buf->sequence = gobuf->seq;
659 buf->memory = V4L2_MEMORY_MMAP;
660 buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
661 buf->length = GO7007_BUF_SIZE;
662 buf->reserved = gobuf->modet_active;
666 case VIDIOC_STREAMON:
668 unsigned int *type = arg;
670 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
674 if (!go->streaming) {
677 go->active_buf = NULL;
678 if (go7007_start_encoder(go) < 0)
687 case VIDIOC_STREAMOFF:
689 unsigned int *type = arg;
691 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
694 go7007_streamoff(go);
698 case VIDIOC_QUERYCTRL:
700 struct v4l2_queryctrl *ctrl = arg;
703 if (!go->i2c_adapter_online)
706 memset(ctrl, 0, sizeof(*ctrl));
708 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, arg);
709 return ctrl->name[0] == 0 ? -EINVAL : 0;
713 struct v4l2_control *ctrl = arg;
714 struct v4l2_queryctrl query;
716 if (!go->i2c_adapter_online)
718 memset(&query, 0, sizeof(query));
720 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
721 if (query.name[0] == 0)
723 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_CTRL, arg);
728 struct v4l2_control *ctrl = arg;
729 struct v4l2_queryctrl query;
731 if (!go->i2c_adapter_online)
733 memset(&query, 0, sizeof(query));
735 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
736 if (query.name[0] == 0)
738 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_CTRL, arg);
743 struct v4l2_streamparm *parm = arg;
744 struct v4l2_fract timeperframe = {
745 .numerator = 1001 * go->fps_scale,
746 .denominator = go->sensor_framerate,
749 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
751 memset(parm, 0, sizeof(*parm));
752 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
753 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
754 parm->parm.capture.timeperframe = timeperframe;
759 struct v4l2_streamparm *parm = arg;
762 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
764 if (parm->parm.capture.capturemode != 0)
766 n = go->sensor_framerate *
767 parm->parm.capture.timeperframe.numerator;
768 d = 1001 * parm->parm.capture.timeperframe.denominator;
769 if (n != 0 && d != 0 && n > d)
770 go->fps_scale = (n + d/2) / d;
777 struct v4l2_standard *std = arg;
779 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
780 go->input == go->board_info->num_inputs - 1) {
781 if (!go->i2c_adapter_online)
783 i2c_clients_command(&go->i2c_adapter,
784 VIDIOC_ENUMSTD, arg);
785 if (!std->id) /* hack to indicate EINVAL from tuner */
787 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
788 switch (std->index) {
790 v4l2_video_std_construct(std,
791 V4L2_STD_NTSC, "NTSC");
794 v4l2_video_std_construct(std,
795 V4L2_STD_PAL | V4L2_STD_SECAM,
804 memset(std, 0, sizeof(*std));
805 snprintf(std->name, sizeof(std->name), "%dx%d, %dfps",
806 go->board_info->sensor_width,
807 go->board_info->sensor_height,
808 go->board_info->sensor_framerate / 1000);
809 std->frameperiod.numerator = 1001;
810 std->frameperiod.denominator =
811 go->board_info->sensor_framerate;
817 v4l2_std_id *std = arg;
819 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
820 go->input == go->board_info->num_inputs - 1) {
821 if (!go->i2c_adapter_online)
823 i2c_clients_command(&go->i2c_adapter,
825 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
826 if (go->standard == GO7007_STD_NTSC)
827 *std = V4L2_STD_NTSC;
829 *std = V4L2_STD_PAL | V4L2_STD_SECAM;
836 v4l2_std_id *std = arg;
840 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) &&
845 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
846 go->input == go->board_info->num_inputs - 1) {
847 if (!go->i2c_adapter_online)
849 i2c_clients_command(&go->i2c_adapter,
851 if (!*std) /* hack to indicate EINVAL from tuner */
854 if (*std & V4L2_STD_NTSC) {
855 go->standard = GO7007_STD_NTSC;
856 go->sensor_framerate = 30000;
857 } else if (*std & V4L2_STD_PAL) {
858 go->standard = GO7007_STD_PAL;
859 go->sensor_framerate = 25025;
860 } else if (*std & V4L2_STD_SECAM) {
861 go->standard = GO7007_STD_PAL;
862 go->sensor_framerate = 25025;
865 if (go->i2c_adapter_online)
866 i2c_clients_command(&go->i2c_adapter,
868 set_capture_size(go, NULL, 0);
871 case VIDIOC_QUERYSTD:
873 v4l2_std_id *std = arg;
875 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
876 go->input == go->board_info->num_inputs - 1) {
877 if (!go->i2c_adapter_online)
879 i2c_clients_command(&go->i2c_adapter,
880 VIDIOC_QUERYSTD, arg);
881 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
882 *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
887 case VIDIOC_ENUMINPUT:
889 struct v4l2_input *inp = arg;
892 if (inp->index >= go->board_info->num_inputs)
895 memset(inp, 0, sizeof(*inp));
897 strncpy(inp->name, go->board_info->inputs[index].name,
899 /* If this board has a tuner, it will be the last input */
900 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
901 index == go->board_info->num_inputs - 1)
902 inp->type = V4L2_INPUT_TYPE_TUNER;
904 inp->type = V4L2_INPUT_TYPE_CAMERA;
907 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
908 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
925 if (*input >= go->board_info->num_inputs)
930 if (go->i2c_adapter_online) {
931 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_INPUT,
932 &go->board_info->inputs[*input].video_input);
933 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_AUDIO,
934 &go->board_info->inputs[*input].audio_input);
940 struct v4l2_tuner *t = arg;
942 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
946 if (!go->i2c_adapter_online)
948 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_TUNER, arg);
954 struct v4l2_tuner *t = arg;
956 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
960 if (!go->i2c_adapter_online)
962 switch (go->board_id) {
963 case GO7007_BOARDID_PX_TV402U_NA:
964 case GO7007_BOARDID_PX_TV402U_JP:
965 /* No selectable options currently */
966 if (t->audmode != V4L2_TUNER_MODE_STEREO)
970 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_TUNER, arg);
973 case VIDIOC_G_FREQUENCY:
975 struct v4l2_frequency *f = arg;
977 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
979 if (!go->i2c_adapter_online)
981 memset(f, 0, sizeof(*f));
982 f->type = V4L2_TUNER_ANALOG_TV;
983 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_FREQUENCY, arg);
986 case VIDIOC_S_FREQUENCY:
988 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
990 if (!go->i2c_adapter_online)
992 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_FREQUENCY, arg);
997 struct v4l2_cropcap *cropcap = arg;
999 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1001 memset(cropcap, 0, sizeof(*cropcap));
1002 cropcap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1003 /* These specify the raw input of the sensor */
1004 switch (go->standard) {
1005 case GO7007_STD_NTSC:
1006 cropcap->bounds.top = 0;
1007 cropcap->bounds.left = 0;
1008 cropcap->bounds.width = 720;
1009 cropcap->bounds.height = 480;
1010 cropcap->defrect.top = 0;
1011 cropcap->defrect.left = 0;
1012 cropcap->defrect.width = 720;
1013 cropcap->defrect.height = 480;
1015 case GO7007_STD_PAL:
1016 cropcap->bounds.top = 0;
1017 cropcap->bounds.left = 0;
1018 cropcap->bounds.width = 720;
1019 cropcap->bounds.height = 576;
1020 cropcap->defrect.top = 0;
1021 cropcap->defrect.left = 0;
1022 cropcap->defrect.width = 720;
1023 cropcap->defrect.height = 576;
1025 case GO7007_STD_OTHER:
1026 cropcap->bounds.top = 0;
1027 cropcap->bounds.left = 0;
1028 cropcap->bounds.width = go->board_info->sensor_width;
1029 cropcap->bounds.height = go->board_info->sensor_height;
1030 cropcap->defrect.top = 0;
1031 cropcap->defrect.left = 0;
1032 cropcap->defrect.width = go->board_info->sensor_width;
1033 cropcap->defrect.height = go->board_info->sensor_height;
1041 struct v4l2_crop *crop = arg;
1043 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1045 memset(crop, 0, sizeof(*crop));
1046 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1047 /* These specify the raw input of the sensor */
1048 switch (go->standard) {
1049 case GO7007_STD_NTSC:
1052 crop->c.width = 720;
1053 crop->c.height = 480;
1055 case GO7007_STD_PAL:
1058 crop->c.width = 720;
1059 crop->c.height = 576;
1061 case GO7007_STD_OTHER:
1064 crop->c.width = go->board_info->sensor_width;
1065 crop->c.height = go->board_info->sensor_height;
1073 struct v4l2_crop *crop = arg;
1075 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1079 case VIDIOC_G_JPEGCOMP:
1081 struct v4l2_jpegcompression *params = arg;
1083 memset(params, 0, sizeof(*params));
1084 params->quality = 50; /* ?? */
1085 params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
1086 V4L2_JPEG_MARKER_DQT;
1090 case VIDIOC_S_JPEGCOMP:
1092 struct v4l2_jpegcompression *params = arg;
1094 if (params->quality != 50 ||
1095 params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
1096 V4L2_JPEG_MARKER_DQT))
1100 /* Temporary ioctls for controlling compression characteristics */
1101 case GO7007IOC_S_BITRATE:
1107 /* Upper bound is kind of arbitrary here */
1108 if (*bitrate < 64000 || *bitrate > 10000000)
1110 go->bitrate = *bitrate;
1113 case GO7007IOC_G_BITRATE:
1117 *bitrate = go->bitrate;
1120 case GO7007IOC_S_COMP_PARAMS:
1122 struct go7007_comp_params *comp = arg;
1124 if (go->format == GO7007_FORMAT_MJPEG)
1126 if (comp->gop_size > 0)
1127 go->gop_size = comp->gop_size;
1129 go->gop_size = go->sensor_framerate / 1000;
1130 if (go->gop_size != 15)
1132 /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
1133 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
1134 switch (comp->aspect_ratio) {
1135 case GO7007_ASPECT_RATIO_4_3_NTSC:
1136 case GO7007_ASPECT_RATIO_4_3_PAL:
1137 go->aspect_ratio = GO7007_RATIO_4_3;
1139 case GO7007_ASPECT_RATIO_16_9_NTSC:
1140 case GO7007_ASPECT_RATIO_16_9_PAL:
1141 go->aspect_ratio = GO7007_RATIO_16_9;
1144 go->aspect_ratio = GO7007_RATIO_1_1;
1148 if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
1150 go->seq_header_enable = 0;
1152 go->seq_header_enable = 1;
1156 case GO7007IOC_G_COMP_PARAMS:
1158 struct go7007_comp_params *comp = arg;
1160 if (go->format == GO7007_FORMAT_MJPEG)
1162 memset(comp, 0, sizeof(*comp));
1163 comp->gop_size = go->gop_size;
1164 comp->max_b_frames = go->ipb ? 2 : 0;
1165 switch (go->aspect_ratio) {
1166 case GO7007_RATIO_4_3:
1167 if (go->standard == GO7007_STD_NTSC)
1168 comp->aspect_ratio =
1169 GO7007_ASPECT_RATIO_4_3_NTSC;
1171 comp->aspect_ratio =
1172 GO7007_ASPECT_RATIO_4_3_PAL;
1174 case GO7007_RATIO_16_9:
1175 if (go->standard == GO7007_STD_NTSC)
1176 comp->aspect_ratio =
1177 GO7007_ASPECT_RATIO_16_9_NTSC;
1179 comp->aspect_ratio =
1180 GO7007_ASPECT_RATIO_16_9_PAL;
1183 comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
1187 comp->flags |= GO7007_COMP_CLOSED_GOP;
1188 if (!go->seq_header_enable)
1189 comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
1192 case GO7007IOC_S_MPEG_PARAMS:
1194 struct go7007_mpeg_params *mpeg = arg;
1196 if (go->format != GO7007_FORMAT_MPEG1 &&
1197 go->format != GO7007_FORMAT_MPEG2 &&
1198 go->format != GO7007_FORMAT_MPEG4)
1201 if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
1202 go->format = GO7007_FORMAT_MPEG2;
1203 go->bitrate = 9800000;
1207 go->repeat_seqhead = 0;
1208 go->seq_header_enable = 1;
1209 go->gop_header_enable = 1;
1212 switch (mpeg->mpeg_video_standard) {
1213 case GO7007_MPEG_VIDEO_MPEG1:
1214 go->format = GO7007_FORMAT_MPEG1;
1217 case GO7007_MPEG_VIDEO_MPEG2:
1218 go->format = GO7007_FORMAT_MPEG2;
1219 if (mpeg->pali >> 24 == 2)
1220 go->pali = mpeg->pali & 0xff;
1224 case GO7007_MPEG_VIDEO_MPEG4:
1225 go->format = GO7007_FORMAT_MPEG4;
1226 if (mpeg->pali >> 24 == 4)
1227 go->pali = mpeg->pali & 0xff;
1234 go->gop_header_enable =
1235 mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
1237 if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
1238 go->repeat_seqhead = 1;
1240 go->repeat_seqhead = 0;
1245 case GO7007IOC_G_MPEG_PARAMS:
1247 struct go7007_mpeg_params *mpeg = arg;
1249 memset(mpeg, 0, sizeof(*mpeg));
1250 switch (go->format) {
1251 case GO7007_FORMAT_MPEG1:
1252 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
1255 case GO7007_FORMAT_MPEG2:
1256 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
1257 mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
1259 case GO7007_FORMAT_MPEG4:
1260 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
1261 mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
1266 if (!go->gop_header_enable)
1267 mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
1268 if (go->repeat_seqhead)
1269 mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
1271 mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
1274 case GO7007IOC_S_MD_PARAMS:
1276 struct go7007_md_params *mdp = arg;
1278 if (mdp->region > 3)
1280 if (mdp->trigger > 0) {
1281 go->modet[mdp->region].pixel_threshold =
1282 mdp->pixel_threshold >> 1;
1283 go->modet[mdp->region].motion_threshold =
1284 mdp->motion_threshold >> 1;
1285 go->modet[mdp->region].mb_threshold =
1287 go->modet[mdp->region].enable = 1;
1289 go->modet[mdp->region].enable = 0;
1292 case GO7007IOC_G_MD_PARAMS:
1294 struct go7007_md_params *mdp = arg;
1295 int region = mdp->region;
1297 if (mdp->region > 3)
1299 memset(mdp, 0, sizeof(struct go7007_md_params));
1300 mdp->region = region;
1301 if (!go->modet[region].enable)
1303 mdp->pixel_threshold =
1304 (go->modet[region].pixel_threshold << 1) + 1;
1305 mdp->motion_threshold =
1306 (go->modet[region].motion_threshold << 1) + 1;
1308 (go->modet[region].mb_threshold << 1) + 1;
1311 case GO7007IOC_S_MD_REGION:
1313 struct go7007_md_region *region = arg;
1315 if (region->region < 1 || region->region > 3)
1317 return clip_to_modet_map(go, region->region, region->clips);
1320 printk(KERN_DEBUG "go7007: unsupported ioctl %d\n", cmd);
1321 return -ENOIOCTLCMD;
1330 static int go7007_ioctl(struct inode *inode, struct file *file,
1331 unsigned int cmd, unsigned long arg)
1333 struct go7007_file *gofh = file->private_data;
1335 if (gofh->go->status != STATUS_ONLINE)
1338 return video_usercopy(inode, file, cmd, arg, go7007_do_ioctl);
1341 static ssize_t go7007_read(struct file *file, char __user *data,
1342 size_t count, loff_t *ppos)
1347 static void go7007_vm_open(struct vm_area_struct *vma)
1349 struct go7007_buffer *gobuf = vma->vm_private_data;
1354 static void go7007_vm_close(struct vm_area_struct *vma)
1356 struct go7007_buffer *gobuf = vma->vm_private_data;
1357 unsigned long flags;
1359 if (--gobuf->mapped == 0) {
1360 spin_lock_irqsave(&gobuf->go->spinlock, flags);
1361 deactivate_buffer(gobuf);
1362 spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
1366 /* Copied from videobuf-dma-sg.c */
1367 static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1371 page = alloc_page(GFP_USER | __GFP_DMA32);
1373 return VM_FAULT_OOM;
1374 clear_user_page(page_address(page), (unsigned long)vmf->virtual_address,
1380 static struct vm_operations_struct go7007_vm_ops = {
1381 .open = go7007_vm_open,
1382 .close = go7007_vm_close,
1383 .fault = go7007_vm_fault,
1386 static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
1388 struct go7007_file *gofh = file->private_data;
1391 if (gofh->go->status != STATUS_ONLINE)
1393 if (!(vma->vm_flags & VM_SHARED))
1394 return -EINVAL; /* only support VM_SHARED mapping */
1395 if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
1396 return -EINVAL; /* must map exactly one full buffer */
1398 index = vma->vm_pgoff / GO7007_BUF_PAGES;
1399 if (index >= gofh->buf_count) {
1401 return -EINVAL; /* trying to map beyond requested buffers */
1403 if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
1405 return -EINVAL; /* offset is not aligned on buffer boundary */
1407 if (gofh->bufs[index].mapped > 0) {
1411 gofh->bufs[index].mapped = 1;
1412 gofh->bufs[index].user_addr = vma->vm_start;
1413 vma->vm_ops = &go7007_vm_ops;
1414 vma->vm_flags |= VM_DONTEXPAND;
1415 vma->vm_flags &= ~VM_IO;
1416 vma->vm_private_data = &gofh->bufs[index];
1421 static unsigned int go7007_poll(struct file *file, poll_table *wait)
1423 struct go7007_file *gofh = file->private_data;
1424 struct go7007_buffer *gobuf;
1426 if (list_empty(&gofh->go->stream))
1428 gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
1429 poll_wait(file, &gofh->go->frame_waitq, wait);
1430 if (gobuf->state == BUF_STATE_DONE)
1431 return POLLIN | POLLRDNORM;
1435 static void go7007_vfl_release(struct video_device *vfd)
1437 struct go7007 *go = video_get_drvdata(vfd);
1439 video_device_release(vfd);
1440 if (--go->ref_count == 0)
1444 static struct file_operations go7007_fops = {
1445 .owner = THIS_MODULE,
1446 .open = go7007_open,
1447 .release = go7007_release,
1448 .ioctl = go7007_ioctl,
1449 .llseek = no_llseek,
1450 .read = go7007_read,
1451 .mmap = go7007_mmap,
1452 .poll = go7007_poll,
1455 static struct video_device go7007_template = {
1457 .vfl_type = VID_TYPE_CAPTURE,
1458 .fops = &go7007_fops,
1460 .release = go7007_vfl_release,
1463 int go7007_v4l2_init(struct go7007 *go)
1467 go->video_dev = video_device_alloc();
1468 if (go->video_dev == NULL)
1470 memcpy(go->video_dev, &go7007_template, sizeof(go7007_template));
1471 go->video_dev->parent = go->dev;
1472 rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
1474 video_device_release(go->video_dev);
1475 go->video_dev = NULL;
1478 video_set_drvdata(go->video_dev, go);
1484 void go7007_v4l2_remove(struct go7007 *go)
1486 unsigned long flags;
1489 if (go->streaming) {
1491 go7007_stream_stop(go);
1492 spin_lock_irqsave(&go->spinlock, flags);
1494 spin_unlock_irqrestore(&go->spinlock, flags);
1498 video_unregister_device(go->video_dev);