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 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 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 long go7007_do_ioctl(struct file *file, unsigned int cmd, void *arg)
380 struct go7007_file *gofh = file->private_data;
381 struct go7007 *go = gofh->go;
386 case VIDIOC_QUERYCAP:
388 struct v4l2_capability *cap = arg;
390 memset(cap, 0, sizeof(*cap));
391 strcpy(cap->driver, "go7007");
392 strncpy(cap->card, go->name, sizeof(cap->card));
393 cap->version = KERNEL_VERSION(0, 9, 8);
394 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
395 V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
396 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
397 cap->capabilities |= V4L2_CAP_TUNER;
400 case VIDIOC_ENUM_FMT:
402 struct v4l2_fmtdesc *fmt = arg;
407 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
409 switch (fmt->index) {
411 pixelformat = V4L2_PIX_FMT_MJPEG;
412 desc = "Motion-JPEG";
415 pixelformat = V4L2_PIX_FMT_MPEG;
416 desc = "MPEG1/MPEG2/MPEG4";
422 memset(fmt, 0, sizeof(*fmt));
424 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
425 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
426 strncpy(fmt->description, desc, sizeof(fmt->description));
427 fmt->pixelformat = pixelformat;
433 struct v4l2_format *fmt = arg;
435 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
437 return set_capture_size(go, fmt, 1);
441 struct v4l2_format *fmt = arg;
443 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
445 memset(fmt, 0, sizeof(*fmt));
446 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
447 fmt->fmt.pix.width = go->width;
448 fmt->fmt.pix.height = go->height;
449 fmt->fmt.pix.pixelformat = go->format == GO7007_FORMAT_MJPEG ?
450 V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
451 fmt->fmt.pix.field = V4L2_FIELD_NONE;
452 fmt->fmt.pix.bytesperline = 0;
453 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
454 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
459 struct v4l2_format *fmt = arg;
461 if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
465 return set_capture_size(go, fmt, 0);
472 struct v4l2_requestbuffers *req = arg;
473 unsigned int count, i;
477 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
478 req->memory != V4L2_MEMORY_MMAP)
483 for (i = 0; i < gofh->buf_count; ++i)
484 if (gofh->bufs[i].mapped > 0)
485 goto unlock_and_return;
487 if (go->in_use > 0 && gofh->buf_count == 0) {
489 goto unlock_and_return;
491 if (gofh->buf_count > 0)
500 gofh->bufs = kmalloc(count *
501 sizeof(struct go7007_buffer),
503 if (gofh->bufs == NULL) {
505 goto unlock_and_return;
507 memset(gofh->bufs, 0,
508 count * sizeof(struct go7007_buffer));
509 for (i = 0; i < count; ++i) {
510 gofh->bufs[i].go = go;
511 gofh->bufs[i].index = i;
512 gofh->bufs[i].state = BUF_STATE_IDLE;
513 gofh->bufs[i].mapped = 0;
519 gofh->buf_count = count;
522 memset(req, 0, sizeof(*req));
524 req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
525 req->memory = V4L2_MEMORY_MMAP;
528 case VIDIOC_QUERYBUF:
530 struct v4l2_buffer *buf = arg;
534 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
538 if (index >= gofh->buf_count)
539 goto unlock_and_return;
540 memset(buf, 0, sizeof(*buf));
542 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
543 switch (gofh->bufs[index].state) {
544 case BUF_STATE_QUEUED:
545 buf->flags = V4L2_BUF_FLAG_QUEUED;
548 buf->flags = V4L2_BUF_FLAG_DONE;
553 if (gofh->bufs[index].mapped)
554 buf->flags |= V4L2_BUF_FLAG_MAPPED;
555 buf->memory = V4L2_MEMORY_MMAP;
556 buf->m.offset = index * GO7007_BUF_SIZE;
557 buf->length = GO7007_BUF_SIZE;
564 struct v4l2_buffer *buf = arg;
565 struct go7007_buffer *gobuf;
569 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
570 buf->memory != V4L2_MEMORY_MMAP)
573 if (buf->index < 0 || buf->index >= gofh->buf_count)
574 goto unlock_and_return;
575 gobuf = &gofh->bufs[buf->index];
576 if (gobuf->mapped == 0)
577 goto unlock_and_return;
579 if (gobuf->state != BUF_STATE_IDLE)
580 goto unlock_and_return;
581 /* offset will be 0 until we really support USERPTR streaming */
582 gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
583 gobuf->bytesused = 0;
584 gobuf->frame_offset = 0;
585 gobuf->modet_active = 0;
586 if (gobuf->offset > 0)
587 gobuf->page_count = GO7007_BUF_PAGES + 1;
589 gobuf->page_count = GO7007_BUF_PAGES;
591 down_read(¤t->mm->mmap_sem);
592 ret = get_user_pages(current, current->mm,
593 gobuf->user_addr & PAGE_MASK, gobuf->page_count,
594 1, 1, gobuf->pages, NULL);
595 up_read(¤t->mm->mmap_sem);
596 if (ret != gobuf->page_count) {
598 for (i = 0; i < ret; ++i)
599 page_cache_release(gobuf->pages[i]);
600 gobuf->page_count = 0;
601 goto unlock_and_return;
603 gobuf->state = BUF_STATE_QUEUED;
604 spin_lock_irqsave(&go->spinlock, flags);
605 list_add_tail(&gobuf->stream, &go->stream);
606 spin_unlock_irqrestore(&go->spinlock, flags);
612 struct v4l2_buffer *buf = arg;
613 struct go7007_buffer *gobuf;
617 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
619 if (buf->memory != V4L2_MEMORY_MMAP)
623 if (list_empty(&go->stream))
624 goto unlock_and_return;
625 gobuf = list_entry(go->stream.next,
626 struct go7007_buffer, stream);
628 if (gobuf->state != BUF_STATE_DONE &&
629 !(file->f_flags & O_NONBLOCK)) {
631 prepare_to_wait(&go->frame_waitq, &wait,
633 if (gobuf->state == BUF_STATE_DONE)
635 if (signal_pending(current)) {
636 retval = -ERESTARTSYS;
641 finish_wait(&go->frame_waitq, &wait);
643 if (gobuf->state != BUF_STATE_DONE)
644 goto unlock_and_return;
645 spin_lock_irqsave(&go->spinlock, flags);
646 deactivate_buffer(gobuf);
647 spin_unlock_irqrestore(&go->spinlock, flags);
648 frame_type_flag = get_frame_type_flag(gobuf, go->format);
649 gobuf->state = BUF_STATE_IDLE;
650 memset(buf, 0, sizeof(*buf));
651 buf->index = gobuf->index;
652 buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
653 buf->bytesused = gobuf->bytesused;
654 buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
655 buf->field = V4L2_FIELD_NONE;
656 buf->timestamp = gobuf->timestamp;
657 buf->sequence = gobuf->seq;
658 buf->memory = V4L2_MEMORY_MMAP;
659 buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
660 buf->length = GO7007_BUF_SIZE;
661 buf->reserved = gobuf->modet_active;
665 case VIDIOC_STREAMON:
667 unsigned int *type = arg;
669 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
673 if (!go->streaming) {
676 go->active_buf = NULL;
677 if (go7007_start_encoder(go) < 0)
686 case VIDIOC_STREAMOFF:
688 unsigned int *type = arg;
690 if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
693 go7007_streamoff(go);
697 case VIDIOC_QUERYCTRL:
699 struct v4l2_queryctrl *ctrl = arg;
702 if (!go->i2c_adapter_online)
705 memset(ctrl, 0, sizeof(*ctrl));
707 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, arg);
708 return ctrl->name[0] == 0 ? -EINVAL : 0;
712 struct v4l2_control *ctrl = arg;
713 struct v4l2_queryctrl query;
715 if (!go->i2c_adapter_online)
717 memset(&query, 0, sizeof(query));
719 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
720 if (query.name[0] == 0)
722 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_CTRL, arg);
727 struct v4l2_control *ctrl = arg;
728 struct v4l2_queryctrl query;
730 if (!go->i2c_adapter_online)
732 memset(&query, 0, sizeof(query));
734 i2c_clients_command(&go->i2c_adapter, VIDIOC_QUERYCTRL, &query);
735 if (query.name[0] == 0)
737 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_CTRL, arg);
742 struct v4l2_streamparm *parm = arg;
743 struct v4l2_fract timeperframe = {
744 .numerator = 1001 * go->fps_scale,
745 .denominator = go->sensor_framerate,
748 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
750 memset(parm, 0, sizeof(*parm));
751 parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
752 parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
753 parm->parm.capture.timeperframe = timeperframe;
758 struct v4l2_streamparm *parm = arg;
761 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
763 if (parm->parm.capture.capturemode != 0)
765 n = go->sensor_framerate *
766 parm->parm.capture.timeperframe.numerator;
767 d = 1001 * parm->parm.capture.timeperframe.denominator;
768 if (n != 0 && d != 0 && n > d)
769 go->fps_scale = (n + d/2) / d;
776 struct v4l2_standard *std = arg;
778 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
779 go->input == go->board_info->num_inputs - 1) {
780 if (!go->i2c_adapter_online)
782 i2c_clients_command(&go->i2c_adapter,
783 VIDIOC_ENUMSTD, arg);
784 if (!std->id) /* hack to indicate EINVAL from tuner */
786 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
787 switch (std->index) {
789 v4l2_video_std_construct(std,
790 V4L2_STD_NTSC, "NTSC");
793 v4l2_video_std_construct(std,
794 V4L2_STD_PAL | V4L2_STD_SECAM,
803 memset(std, 0, sizeof(*std));
804 snprintf(std->name, sizeof(std->name), "%dx%d, %dfps",
805 go->board_info->sensor_width,
806 go->board_info->sensor_height,
807 go->board_info->sensor_framerate / 1000);
808 std->frameperiod.numerator = 1001;
809 std->frameperiod.denominator =
810 go->board_info->sensor_framerate;
816 v4l2_std_id *std = arg;
818 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
819 go->input == go->board_info->num_inputs - 1) {
820 if (!go->i2c_adapter_online)
822 i2c_clients_command(&go->i2c_adapter,
824 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
825 if (go->standard == GO7007_STD_NTSC)
826 *std = V4L2_STD_NTSC;
828 *std = V4L2_STD_PAL | V4L2_STD_SECAM;
835 v4l2_std_id *std = arg;
839 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) &&
844 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
845 go->input == go->board_info->num_inputs - 1) {
846 if (!go->i2c_adapter_online)
848 i2c_clients_command(&go->i2c_adapter,
850 if (!*std) /* hack to indicate EINVAL from tuner */
853 if (*std & V4L2_STD_NTSC) {
854 go->standard = GO7007_STD_NTSC;
855 go->sensor_framerate = 30000;
856 } else if (*std & V4L2_STD_PAL) {
857 go->standard = GO7007_STD_PAL;
858 go->sensor_framerate = 25025;
859 } else if (*std & V4L2_STD_SECAM) {
860 go->standard = GO7007_STD_PAL;
861 go->sensor_framerate = 25025;
864 if (go->i2c_adapter_online)
865 i2c_clients_command(&go->i2c_adapter,
867 set_capture_size(go, NULL, 0);
870 case VIDIOC_QUERYSTD:
872 v4l2_std_id *std = arg;
874 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
875 go->input == go->board_info->num_inputs - 1) {
876 if (!go->i2c_adapter_online)
878 i2c_clients_command(&go->i2c_adapter,
879 VIDIOC_QUERYSTD, arg);
880 } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
881 *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
886 case VIDIOC_ENUMINPUT:
888 struct v4l2_input *inp = arg;
891 if (inp->index >= go->board_info->num_inputs)
894 memset(inp, 0, sizeof(*inp));
896 strncpy(inp->name, go->board_info->inputs[index].name,
898 /* If this board has a tuner, it will be the last input */
899 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
900 index == go->board_info->num_inputs - 1)
901 inp->type = V4L2_INPUT_TYPE_TUNER;
903 inp->type = V4L2_INPUT_TYPE_CAMERA;
906 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
907 inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
924 if (*input >= go->board_info->num_inputs)
929 if (go->i2c_adapter_online) {
930 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_INPUT,
931 &go->board_info->inputs[*input].video_input);
932 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_AUDIO,
933 &go->board_info->inputs[*input].audio_input);
939 struct v4l2_tuner *t = arg;
941 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
945 if (!go->i2c_adapter_online)
947 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_TUNER, arg);
953 struct v4l2_tuner *t = arg;
955 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
959 if (!go->i2c_adapter_online)
961 switch (go->board_id) {
962 case GO7007_BOARDID_PX_TV402U_NA:
963 case GO7007_BOARDID_PX_TV402U_JP:
964 /* No selectable options currently */
965 if (t->audmode != V4L2_TUNER_MODE_STEREO)
969 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_TUNER, arg);
972 case VIDIOC_G_FREQUENCY:
974 struct v4l2_frequency *f = arg;
976 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
978 if (!go->i2c_adapter_online)
980 memset(f, 0, sizeof(*f));
981 f->type = V4L2_TUNER_ANALOG_TV;
982 i2c_clients_command(&go->i2c_adapter, VIDIOC_G_FREQUENCY, arg);
985 case VIDIOC_S_FREQUENCY:
987 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
989 if (!go->i2c_adapter_online)
991 i2c_clients_command(&go->i2c_adapter, VIDIOC_S_FREQUENCY, arg);
996 struct v4l2_cropcap *cropcap = arg;
998 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1000 memset(cropcap, 0, sizeof(*cropcap));
1001 cropcap->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1002 /* These specify the raw input of the sensor */
1003 switch (go->standard) {
1004 case GO7007_STD_NTSC:
1005 cropcap->bounds.top = 0;
1006 cropcap->bounds.left = 0;
1007 cropcap->bounds.width = 720;
1008 cropcap->bounds.height = 480;
1009 cropcap->defrect.top = 0;
1010 cropcap->defrect.left = 0;
1011 cropcap->defrect.width = 720;
1012 cropcap->defrect.height = 480;
1014 case GO7007_STD_PAL:
1015 cropcap->bounds.top = 0;
1016 cropcap->bounds.left = 0;
1017 cropcap->bounds.width = 720;
1018 cropcap->bounds.height = 576;
1019 cropcap->defrect.top = 0;
1020 cropcap->defrect.left = 0;
1021 cropcap->defrect.width = 720;
1022 cropcap->defrect.height = 576;
1024 case GO7007_STD_OTHER:
1025 cropcap->bounds.top = 0;
1026 cropcap->bounds.left = 0;
1027 cropcap->bounds.width = go->board_info->sensor_width;
1028 cropcap->bounds.height = go->board_info->sensor_height;
1029 cropcap->defrect.top = 0;
1030 cropcap->defrect.left = 0;
1031 cropcap->defrect.width = go->board_info->sensor_width;
1032 cropcap->defrect.height = go->board_info->sensor_height;
1040 struct v4l2_crop *crop = arg;
1042 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1044 memset(crop, 0, sizeof(*crop));
1045 crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1046 /* These specify the raw input of the sensor */
1047 switch (go->standard) {
1048 case GO7007_STD_NTSC:
1051 crop->c.width = 720;
1052 crop->c.height = 480;
1054 case GO7007_STD_PAL:
1057 crop->c.width = 720;
1058 crop->c.height = 576;
1060 case GO7007_STD_OTHER:
1063 crop->c.width = go->board_info->sensor_width;
1064 crop->c.height = go->board_info->sensor_height;
1072 struct v4l2_crop *crop = arg;
1074 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1078 case VIDIOC_G_JPEGCOMP:
1080 struct v4l2_jpegcompression *params = arg;
1082 memset(params, 0, sizeof(*params));
1083 params->quality = 50; /* ?? */
1084 params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
1085 V4L2_JPEG_MARKER_DQT;
1089 case VIDIOC_S_JPEGCOMP:
1091 struct v4l2_jpegcompression *params = arg;
1093 if (params->quality != 50 ||
1094 params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
1095 V4L2_JPEG_MARKER_DQT))
1099 /* Temporary ioctls for controlling compression characteristics */
1100 case GO7007IOC_S_BITRATE:
1106 /* Upper bound is kind of arbitrary here */
1107 if (*bitrate < 64000 || *bitrate > 10000000)
1109 go->bitrate = *bitrate;
1112 case GO7007IOC_G_BITRATE:
1116 *bitrate = go->bitrate;
1119 case GO7007IOC_S_COMP_PARAMS:
1121 struct go7007_comp_params *comp = arg;
1123 if (go->format == GO7007_FORMAT_MJPEG)
1125 if (comp->gop_size > 0)
1126 go->gop_size = comp->gop_size;
1128 go->gop_size = go->sensor_framerate / 1000;
1129 if (go->gop_size != 15)
1131 /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
1132 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
1133 switch (comp->aspect_ratio) {
1134 case GO7007_ASPECT_RATIO_4_3_NTSC:
1135 case GO7007_ASPECT_RATIO_4_3_PAL:
1136 go->aspect_ratio = GO7007_RATIO_4_3;
1138 case GO7007_ASPECT_RATIO_16_9_NTSC:
1139 case GO7007_ASPECT_RATIO_16_9_PAL:
1140 go->aspect_ratio = GO7007_RATIO_16_9;
1143 go->aspect_ratio = GO7007_RATIO_1_1;
1147 if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
1149 go->seq_header_enable = 0;
1151 go->seq_header_enable = 1;
1155 case GO7007IOC_G_COMP_PARAMS:
1157 struct go7007_comp_params *comp = arg;
1159 if (go->format == GO7007_FORMAT_MJPEG)
1161 memset(comp, 0, sizeof(*comp));
1162 comp->gop_size = go->gop_size;
1163 comp->max_b_frames = go->ipb ? 2 : 0;
1164 switch (go->aspect_ratio) {
1165 case GO7007_RATIO_4_3:
1166 if (go->standard == GO7007_STD_NTSC)
1167 comp->aspect_ratio =
1168 GO7007_ASPECT_RATIO_4_3_NTSC;
1170 comp->aspect_ratio =
1171 GO7007_ASPECT_RATIO_4_3_PAL;
1173 case GO7007_RATIO_16_9:
1174 if (go->standard == GO7007_STD_NTSC)
1175 comp->aspect_ratio =
1176 GO7007_ASPECT_RATIO_16_9_NTSC;
1178 comp->aspect_ratio =
1179 GO7007_ASPECT_RATIO_16_9_PAL;
1182 comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
1186 comp->flags |= GO7007_COMP_CLOSED_GOP;
1187 if (!go->seq_header_enable)
1188 comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
1191 case GO7007IOC_S_MPEG_PARAMS:
1193 struct go7007_mpeg_params *mpeg = arg;
1195 if (go->format != GO7007_FORMAT_MPEG1 &&
1196 go->format != GO7007_FORMAT_MPEG2 &&
1197 go->format != GO7007_FORMAT_MPEG4)
1200 if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
1201 go->format = GO7007_FORMAT_MPEG2;
1202 go->bitrate = 9800000;
1206 go->repeat_seqhead = 0;
1207 go->seq_header_enable = 1;
1208 go->gop_header_enable = 1;
1211 switch (mpeg->mpeg_video_standard) {
1212 case GO7007_MPEG_VIDEO_MPEG1:
1213 go->format = GO7007_FORMAT_MPEG1;
1216 case GO7007_MPEG_VIDEO_MPEG2:
1217 go->format = GO7007_FORMAT_MPEG2;
1218 if (mpeg->pali >> 24 == 2)
1219 go->pali = mpeg->pali & 0xff;
1223 case GO7007_MPEG_VIDEO_MPEG4:
1224 go->format = GO7007_FORMAT_MPEG4;
1225 if (mpeg->pali >> 24 == 4)
1226 go->pali = mpeg->pali & 0xff;
1233 go->gop_header_enable =
1234 mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
1236 if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
1237 go->repeat_seqhead = 1;
1239 go->repeat_seqhead = 0;
1244 case GO7007IOC_G_MPEG_PARAMS:
1246 struct go7007_mpeg_params *mpeg = arg;
1248 memset(mpeg, 0, sizeof(*mpeg));
1249 switch (go->format) {
1250 case GO7007_FORMAT_MPEG1:
1251 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
1254 case GO7007_FORMAT_MPEG2:
1255 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
1256 mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
1258 case GO7007_FORMAT_MPEG4:
1259 mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
1260 mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
1265 if (!go->gop_header_enable)
1266 mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
1267 if (go->repeat_seqhead)
1268 mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
1270 mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
1273 case GO7007IOC_S_MD_PARAMS:
1275 struct go7007_md_params *mdp = arg;
1277 if (mdp->region > 3)
1279 if (mdp->trigger > 0) {
1280 go->modet[mdp->region].pixel_threshold =
1281 mdp->pixel_threshold >> 1;
1282 go->modet[mdp->region].motion_threshold =
1283 mdp->motion_threshold >> 1;
1284 go->modet[mdp->region].mb_threshold =
1286 go->modet[mdp->region].enable = 1;
1288 go->modet[mdp->region].enable = 0;
1291 case GO7007IOC_G_MD_PARAMS:
1293 struct go7007_md_params *mdp = arg;
1294 int region = mdp->region;
1296 if (mdp->region > 3)
1298 memset(mdp, 0, sizeof(struct go7007_md_params));
1299 mdp->region = region;
1300 if (!go->modet[region].enable)
1302 mdp->pixel_threshold =
1303 (go->modet[region].pixel_threshold << 1) + 1;
1304 mdp->motion_threshold =
1305 (go->modet[region].motion_threshold << 1) + 1;
1307 (go->modet[region].mb_threshold << 1) + 1;
1310 case GO7007IOC_S_MD_REGION:
1312 struct go7007_md_region *region = arg;
1314 if (region->region < 1 || region->region > 3)
1316 return clip_to_modet_map(go, region->region, region->clips);
1319 printk(KERN_DEBUG "go7007: unsupported ioctl %d\n", cmd);
1320 return -ENOIOCTLCMD;
1329 static long go7007_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1331 struct go7007_file *gofh = file->private_data;
1333 if (gofh->go->status != STATUS_ONLINE)
1336 return video_usercopy(file, cmd, arg, go7007_do_ioctl);
1339 static ssize_t go7007_read(struct file *file, char __user *data,
1340 size_t count, loff_t *ppos)
1345 static void go7007_vm_open(struct vm_area_struct *vma)
1347 struct go7007_buffer *gobuf = vma->vm_private_data;
1352 static void go7007_vm_close(struct vm_area_struct *vma)
1354 struct go7007_buffer *gobuf = vma->vm_private_data;
1355 unsigned long flags;
1357 if (--gobuf->mapped == 0) {
1358 spin_lock_irqsave(&gobuf->go->spinlock, flags);
1359 deactivate_buffer(gobuf);
1360 spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
1364 /* Copied from videobuf-dma-sg.c */
1365 static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1369 page = alloc_page(GFP_USER | __GFP_DMA32);
1371 return VM_FAULT_OOM;
1372 clear_user_page(page_address(page), (unsigned long)vmf->virtual_address,
1378 static struct vm_operations_struct go7007_vm_ops = {
1379 .open = go7007_vm_open,
1380 .close = go7007_vm_close,
1381 .fault = go7007_vm_fault,
1384 static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
1386 struct go7007_file *gofh = file->private_data;
1389 if (gofh->go->status != STATUS_ONLINE)
1391 if (!(vma->vm_flags & VM_SHARED))
1392 return -EINVAL; /* only support VM_SHARED mapping */
1393 if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
1394 return -EINVAL; /* must map exactly one full buffer */
1396 index = vma->vm_pgoff / GO7007_BUF_PAGES;
1397 if (index >= gofh->buf_count) {
1399 return -EINVAL; /* trying to map beyond requested buffers */
1401 if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
1403 return -EINVAL; /* offset is not aligned on buffer boundary */
1405 if (gofh->bufs[index].mapped > 0) {
1409 gofh->bufs[index].mapped = 1;
1410 gofh->bufs[index].user_addr = vma->vm_start;
1411 vma->vm_ops = &go7007_vm_ops;
1412 vma->vm_flags |= VM_DONTEXPAND;
1413 vma->vm_flags &= ~VM_IO;
1414 vma->vm_private_data = &gofh->bufs[index];
1419 static unsigned int go7007_poll(struct file *file, poll_table *wait)
1421 struct go7007_file *gofh = file->private_data;
1422 struct go7007_buffer *gobuf;
1424 if (list_empty(&gofh->go->stream))
1426 gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
1427 poll_wait(file, &gofh->go->frame_waitq, wait);
1428 if (gobuf->state == BUF_STATE_DONE)
1429 return POLLIN | POLLRDNORM;
1433 static void go7007_vfl_release(struct video_device *vfd)
1435 struct go7007 *go = video_get_drvdata(vfd);
1437 video_device_release(vfd);
1438 if (--go->ref_count == 0)
1442 static struct v4l2_file_operations go7007_fops = {
1443 .owner = THIS_MODULE,
1444 .open = go7007_open,
1445 .release = go7007_release,
1446 .ioctl = go7007_ioctl,
1447 .read = go7007_read,
1448 .mmap = go7007_mmap,
1449 .poll = go7007_poll,
1452 static struct video_device go7007_template = {
1454 .vfl_type = VID_TYPE_CAPTURE,
1455 .fops = &go7007_fops,
1457 .release = go7007_vfl_release,
1460 int go7007_v4l2_init(struct go7007 *go)
1464 go->video_dev = video_device_alloc();
1465 if (go->video_dev == NULL)
1467 memcpy(go->video_dev, &go7007_template, sizeof(go7007_template));
1468 go->video_dev->parent = go->dev;
1469 rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
1471 video_device_release(go->video_dev);
1472 go->video_dev = NULL;
1475 video_set_drvdata(go->video_dev, go);
1481 void go7007_v4l2_remove(struct go7007 *go)
1483 unsigned long flags;
1486 if (go->streaming) {
1488 go7007_stream_stop(go);
1489 spin_lock_irqsave(&go->spinlock, flags);
1491 spin_unlock_irqrestore(&go->spinlock, flags);
1495 video_unregister_device(go->video_dev);