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/delay.h>
20 #include <linux/sched.h>
21 #include <linux/spinlock.h>
22 #include <linux/slab.h>
24 #include <linux/unistd.h>
25 #include <linux/time.h>
26 #include <linux/vmalloc.h>
27 #include <linux/pagemap.h>
28 #include <linux/i2c.h>
29 #include <linux/mutex.h>
30 #include <linux/uaccess.h>
31 #include <linux/videodev2.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-subdev.h>
35 #include <media/v4l2-event.h>
36 #include <media/videobuf2-vmalloc.h>
37 #include <media/saa7115.h>
40 #include "go7007-priv.h"
42 #define call_all(dev, o, f, args...) \
43 v4l2_device_call_until_err(dev, 0, o, f, ##args)
45 static bool valid_pixelformat(u32 pixelformat)
47 switch (pixelformat) {
48 case V4L2_PIX_FMT_MJPEG:
49 case V4L2_PIX_FMT_MPEG1:
50 case V4L2_PIX_FMT_MPEG2:
51 case V4L2_PIX_FMT_MPEG4:
58 static u32 get_frame_type_flag(struct go7007_buffer *vb, int format)
60 u8 *ptr = vb2_plane_vaddr(&vb->vb, 0);
63 case V4L2_PIX_FMT_MJPEG:
64 return V4L2_BUF_FLAG_KEYFRAME;
65 case V4L2_PIX_FMT_MPEG4:
66 switch ((ptr[vb->frame_offset + 4] >> 6) & 0x3) {
68 return V4L2_BUF_FLAG_KEYFRAME;
70 return V4L2_BUF_FLAG_PFRAME;
72 return V4L2_BUF_FLAG_BFRAME;
76 case V4L2_PIX_FMT_MPEG1:
77 case V4L2_PIX_FMT_MPEG2:
78 switch ((ptr[vb->frame_offset + 5] >> 3) & 0x7) {
80 return V4L2_BUF_FLAG_KEYFRAME;
82 return V4L2_BUF_FLAG_PFRAME;
84 return V4L2_BUF_FLAG_BFRAME;
93 static void get_resolution(struct go7007 *go, int *width, int *height)
95 switch (go->standard) {
104 case GO7007_STD_OTHER:
106 *width = go->board_info->sensor_width;
107 *height = go->board_info->sensor_height;
112 static void set_formatting(struct go7007 *go)
114 if (go->format == V4L2_PIX_FMT_MJPEG) {
116 go->aspect_ratio = GO7007_RATIO_1_1;
120 go->repeat_seqhead = 0;
121 go->seq_header_enable = 0;
122 go->gop_header_enable = 0;
127 switch (go->format) {
128 case V4L2_PIX_FMT_MPEG1:
132 case V4L2_PIX_FMT_MPEG2:
135 case V4L2_PIX_FMT_MPEG4:
136 /* For future reference: this is the list of MPEG4
137 * profiles that are available, although they are
141 * -------------- ----
146 * PROFILE_ARTS_L1 0x91
147 * PROFILE_ARTS_L2 0x92
148 * PROFILE_ARTS_L3 0x93
149 * PROFILE_ARTS_L4 0x94
160 go->gop_size = v4l2_ctrl_g_ctrl(go->mpeg_video_gop_size);
161 go->closed_gop = v4l2_ctrl_g_ctrl(go->mpeg_video_gop_closure);
162 go->ipb = v4l2_ctrl_g_ctrl(go->mpeg_video_b_frames) != 0;
163 go->bitrate = v4l2_ctrl_g_ctrl(go->mpeg_video_bitrate);
164 go->repeat_seqhead = v4l2_ctrl_g_ctrl(go->mpeg_video_rep_seqheader);
165 go->gop_header_enable = 1;
167 if (go->format == V4L2_PIX_FMT_MPEG2)
169 go->bitrate == 9800000 &&
170 go->gop_size == 15 &&
172 go->repeat_seqhead == 1 &&
175 switch (v4l2_ctrl_g_ctrl(go->mpeg_video_aspect_ratio)) {
177 case V4L2_MPEG_VIDEO_ASPECT_1x1:
178 go->aspect_ratio = GO7007_RATIO_1_1;
180 case V4L2_MPEG_VIDEO_ASPECT_4x3:
181 go->aspect_ratio = GO7007_RATIO_4_3;
183 case V4L2_MPEG_VIDEO_ASPECT_16x9:
184 go->aspect_ratio = GO7007_RATIO_16_9;
189 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
191 int sensor_height = 0, sensor_width = 0;
192 int width, height, i;
194 if (fmt != NULL && !valid_pixelformat(fmt->fmt.pix.pixelformat))
197 get_resolution(go, &sensor_width, &sensor_height);
200 width = sensor_width;
201 height = sensor_height;
202 } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
203 if (fmt->fmt.pix.width > sensor_width)
204 width = sensor_width;
205 else if (fmt->fmt.pix.width < 144)
208 width = fmt->fmt.pix.width & ~0x0f;
210 if (fmt->fmt.pix.height > sensor_height)
211 height = sensor_height;
212 else if (fmt->fmt.pix.height < 96)
215 height = fmt->fmt.pix.height & ~0x0f;
217 width = fmt->fmt.pix.width;
219 if (width <= sensor_width / 4) {
220 width = sensor_width / 4;
221 height = sensor_height / 4;
222 } else if (width <= sensor_width / 2) {
223 width = sensor_width / 2;
224 height = sensor_height / 2;
226 width = sensor_width;
227 height = sensor_height;
234 u32 pixelformat = fmt->fmt.pix.pixelformat;
236 memset(fmt, 0, sizeof(*fmt));
237 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
238 fmt->fmt.pix.width = width;
239 fmt->fmt.pix.height = height;
240 fmt->fmt.pix.pixelformat = pixelformat;
241 fmt->fmt.pix.field = V4L2_FIELD_NONE;
242 fmt->fmt.pix.bytesperline = 0;
243 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
244 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
251 go->format = fmt->fmt.pix.pixelformat;
254 go->encoder_h_offset = go->board_info->sensor_h_offset;
255 go->encoder_v_offset = go->board_info->sensor_v_offset;
256 for (i = 0; i < 4; ++i)
257 go->modet[i].enable = 0;
258 for (i = 0; i < 1624; ++i)
259 go->modet_map[i] = 0;
261 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
262 struct v4l2_mbus_framefmt mbus_fmt;
264 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
265 mbus_fmt.width = fmt ? fmt->fmt.pix.width : width;
266 mbus_fmt.height = height;
267 go->encoder_h_halve = 0;
268 go->encoder_v_halve = 0;
269 go->encoder_subsample = 0;
270 call_all(&go->v4l2_dev, video, s_mbus_fmt, &mbus_fmt);
272 if (width <= sensor_width / 4) {
273 go->encoder_h_halve = 1;
274 go->encoder_v_halve = 1;
275 go->encoder_subsample = 1;
276 } else if (width <= sensor_width / 2) {
277 go->encoder_h_halve = 1;
278 go->encoder_v_halve = 1;
279 go->encoder_subsample = 0;
281 go->encoder_h_halve = 0;
282 go->encoder_v_halve = 0;
283 go->encoder_subsample = 0;
290 static int clip_to_modet_map(struct go7007 *go, int region,
291 struct v4l2_clip *clip_list)
293 struct v4l2_clip clip, *clip_ptr;
296 /* Check if coordinates are OK and if any macroblocks are already
297 * used by other regions (besides 0) */
298 clip_ptr = clip_list;
300 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
302 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
303 clip.c.width <= 0 || (clip.c.width & 0xF))
305 if (clip.c.left + clip.c.width > go->width)
307 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
308 clip.c.height <= 0 || (clip.c.height & 0xF))
310 if (clip.c.top + clip.c.height > go->height)
312 for (y = 0; y < clip.c.height; y += 16)
313 for (x = 0; x < clip.c.width; x += 16) {
314 mbnum = (go->width >> 4) *
315 ((clip.c.top + y) >> 4) +
316 ((clip.c.left + x) >> 4);
317 if (go->modet_map[mbnum] != 0 &&
318 go->modet_map[mbnum] != region)
321 clip_ptr = clip.next;
324 /* Clear old region macroblocks */
325 for (mbnum = 0; mbnum < 1624; ++mbnum)
326 if (go->modet_map[mbnum] == region)
327 go->modet_map[mbnum] = 0;
329 /* Claim macroblocks in this list */
330 clip_ptr = clip_list;
332 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
334 for (y = 0; y < clip.c.height; y += 16)
335 for (x = 0; x < clip.c.width; x += 16) {
336 mbnum = (go->width >> 4) *
337 ((clip.c.top + y) >> 4) +
338 ((clip.c.left + x) >> 4);
339 go->modet_map[mbnum] = region;
341 clip_ptr = clip.next;
347 static int vidioc_querycap(struct file *file, void *priv,
348 struct v4l2_capability *cap)
350 struct go7007 *go = video_drvdata(file);
352 strlcpy(cap->driver, "go7007", sizeof(cap->driver));
353 strlcpy(cap->card, go->name, sizeof(cap->card));
354 strlcpy(cap->bus_info, go->bus_info, sizeof(cap->bus_info));
356 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
359 if (go->board_info->num_aud_inputs)
360 cap->device_caps |= V4L2_CAP_AUDIO;
361 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
362 cap->device_caps |= V4L2_CAP_TUNER;
363 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
367 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
368 struct v4l2_fmtdesc *fmt)
372 switch (fmt->index) {
374 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
375 desc = "Motion JPEG";
378 fmt->pixelformat = V4L2_PIX_FMT_MPEG1;
382 fmt->pixelformat = V4L2_PIX_FMT_MPEG2;
386 fmt->pixelformat = V4L2_PIX_FMT_MPEG4;
392 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
393 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
395 strncpy(fmt->description, desc, sizeof(fmt->description));
400 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
401 struct v4l2_format *fmt)
403 struct go7007 *go = video_drvdata(file);
405 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
406 fmt->fmt.pix.width = go->width;
407 fmt->fmt.pix.height = go->height;
408 fmt->fmt.pix.pixelformat = go->format;
409 fmt->fmt.pix.field = V4L2_FIELD_NONE;
410 fmt->fmt.pix.bytesperline = 0;
411 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
412 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
417 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
418 struct v4l2_format *fmt)
420 struct go7007 *go = video_drvdata(file);
422 return set_capture_size(go, fmt, 1);
425 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
426 struct v4l2_format *fmt)
428 struct go7007 *go = video_drvdata(file);
430 if (vb2_is_busy(&go->vidq))
433 return set_capture_size(go, fmt, 0);
436 static int go7007_queue_setup(struct vb2_queue *q,
437 const struct v4l2_format *fmt,
438 unsigned int *num_buffers, unsigned int *num_planes,
439 unsigned int sizes[], void *alloc_ctxs[])
441 sizes[0] = GO7007_BUF_SIZE;
444 if (*num_buffers < 2)
450 static void go7007_buf_queue(struct vb2_buffer *vb)
452 struct vb2_queue *vq = vb->vb2_queue;
453 struct go7007 *go = vb2_get_drv_priv(vq);
454 struct go7007_buffer *go7007_vb =
455 container_of(vb, struct go7007_buffer, vb);
458 spin_lock_irqsave(&go->spinlock, flags);
459 list_add_tail(&go7007_vb->list, &go->vidq_active);
460 spin_unlock_irqrestore(&go->spinlock, flags);
463 static int go7007_buf_prepare(struct vb2_buffer *vb)
465 struct go7007_buffer *go7007_vb =
466 container_of(vb, struct go7007_buffer, vb);
468 go7007_vb->modet_active = 0;
469 go7007_vb->frame_offset = 0;
470 vb->v4l2_planes[0].bytesused = 0;
474 static void go7007_buf_finish(struct vb2_buffer *vb)
476 struct vb2_queue *vq = vb->vb2_queue;
477 struct go7007 *go = vb2_get_drv_priv(vq);
478 struct go7007_buffer *go7007_vb =
479 container_of(vb, struct go7007_buffer, vb);
480 u32 frame_type_flag = get_frame_type_flag(go7007_vb, go->format);
481 struct v4l2_buffer *buf = &vb->v4l2_buf;
483 buf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_BFRAME |
484 V4L2_BUF_FLAG_PFRAME);
485 buf->flags |= frame_type_flag;
486 buf->field = V4L2_FIELD_NONE;
489 static int go7007_start_streaming(struct vb2_queue *q, unsigned int count)
491 struct go7007 *go = vb2_get_drv_priv(q);
495 mutex_lock(&go->hw_lock);
497 go->active_buf = NULL;
499 if (go7007_start_encoder(go) < 0)
503 mutex_unlock(&go->hw_lock);
508 call_all(&go->v4l2_dev, video, s_stream, 1);
509 v4l2_ctrl_grab(go->mpeg_video_gop_size, true);
510 v4l2_ctrl_grab(go->mpeg_video_gop_closure, true);
511 v4l2_ctrl_grab(go->mpeg_video_bitrate, true);
512 v4l2_ctrl_grab(go->mpeg_video_aspect_ratio, true);
513 /* Turn on Capture LED */
514 if (go->board_id == GO7007_BOARDID_ADS_USBAV_709)
515 go7007_write_addr(go, 0x3c82, 0x0005);
519 static int go7007_stop_streaming(struct vb2_queue *q)
521 struct go7007 *go = vb2_get_drv_priv(q);
525 go7007_stream_stop(go);
526 mutex_lock(&go->hw_lock);
527 go7007_reset_encoder(go);
528 mutex_unlock(&go->hw_lock);
529 call_all(&go->v4l2_dev, video, s_stream, 0);
531 spin_lock_irqsave(&go->spinlock, flags);
532 INIT_LIST_HEAD(&go->vidq_active);
533 spin_unlock_irqrestore(&go->spinlock, flags);
534 v4l2_ctrl_grab(go->mpeg_video_gop_size, false);
535 v4l2_ctrl_grab(go->mpeg_video_gop_closure, false);
536 v4l2_ctrl_grab(go->mpeg_video_bitrate, false);
537 v4l2_ctrl_grab(go->mpeg_video_aspect_ratio, false);
538 /* Turn on Capture LED */
539 if (go->board_id == GO7007_BOARDID_ADS_USBAV_709)
540 go7007_write_addr(go, 0x3c82, 0x000d);
544 static struct vb2_ops go7007_video_qops = {
545 .queue_setup = go7007_queue_setup,
546 .buf_queue = go7007_buf_queue,
547 .buf_prepare = go7007_buf_prepare,
548 .buf_finish = go7007_buf_finish,
549 .start_streaming = go7007_start_streaming,
550 .stop_streaming = go7007_stop_streaming,
551 .wait_prepare = vb2_ops_wait_prepare,
552 .wait_finish = vb2_ops_wait_finish,
555 static int vidioc_g_parm(struct file *filp, void *priv,
556 struct v4l2_streamparm *parm)
558 struct go7007 *go = video_drvdata(filp);
559 struct v4l2_fract timeperframe = {
560 .numerator = 1001 * go->fps_scale,
561 .denominator = go->sensor_framerate,
564 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
567 parm->parm.capture.readbuffers = 2;
568 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
569 parm->parm.capture.timeperframe = timeperframe;
574 static int vidioc_s_parm(struct file *filp, void *priv,
575 struct v4l2_streamparm *parm)
577 struct go7007 *go = video_drvdata(filp);
580 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
583 n = go->sensor_framerate *
584 parm->parm.capture.timeperframe.numerator;
585 d = 1001 * parm->parm.capture.timeperframe.denominator;
586 if (n != 0 && d != 0 && n > d)
587 go->fps_scale = (n + d/2) / d;
591 return vidioc_g_parm(filp, priv, parm);
594 /* VIDIOC_ENUMSTD on go7007 were used for enumerating the supported fps and
595 its resolution, when the device is not connected to TV.
596 This is were an API abuse, probably used by the lack of specific IOCTL's to
597 enumerate it, by the time the driver was written.
599 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
600 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
602 The two functions below implement the newer ioctls
604 static int vidioc_enum_framesizes(struct file *filp, void *priv,
605 struct v4l2_frmsizeenum *fsize)
607 struct go7007 *go = video_drvdata(filp);
610 if (fsize->index > 2)
613 if (!valid_pixelformat(fsize->pixel_format))
616 get_resolution(go, &width, &height);
617 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
618 fsize->discrete.width = (width >> fsize->index) & ~0xf;
619 fsize->discrete.height = (height >> fsize->index) & ~0xf;
623 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
624 struct v4l2_frmivalenum *fival)
626 struct go7007 *go = video_drvdata(filp);
630 if (fival->index > 4)
633 if (!valid_pixelformat(fival->pixel_format))
636 if (!(go->board_info->sensor_flags & GO7007_SENSOR_SCALING)) {
637 get_resolution(go, &width, &height);
638 for (i = 0; i <= 2; i++)
639 if (fival->width == ((width >> i) & ~0xf) &&
640 fival->height == ((height >> i) & ~0xf))
645 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
646 fival->discrete.numerator = 1001 * (fival->index + 1);
647 fival->discrete.denominator = go->sensor_framerate;
651 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
653 struct go7007 *go = video_drvdata(file);
659 static int go7007_s_std(struct go7007 *go)
661 if (go->std & V4L2_STD_625_50) {
662 go->standard = GO7007_STD_PAL;
663 go->sensor_framerate = 25025;
665 go->standard = GO7007_STD_NTSC;
666 go->sensor_framerate = 30000;
669 call_all(&go->v4l2_dev, core, s_std, go->std);
670 set_capture_size(go, NULL, 0);
674 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
676 struct go7007 *go = video_drvdata(file);
678 if (vb2_is_busy(&go->vidq))
683 return go7007_s_std(go);
686 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
688 struct go7007 *go = video_drvdata(file);
690 return call_all(&go->v4l2_dev, video, querystd, std);
693 static int vidioc_enum_input(struct file *file, void *priv,
694 struct v4l2_input *inp)
696 struct go7007 *go = video_drvdata(file);
698 if (inp->index >= go->board_info->num_inputs)
701 strncpy(inp->name, go->board_info->inputs[inp->index].name,
704 /* If this board has a tuner, it will be the first input */
705 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
707 inp->type = V4L2_INPUT_TYPE_TUNER;
709 inp->type = V4L2_INPUT_TYPE_CAMERA;
711 if (go->board_info->num_aud_inputs)
712 inp->audioset = (1 << go->board_info->num_aud_inputs) - 1;
716 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
717 inp->std = video_devdata(file)->tvnorms;
725 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
727 struct go7007 *go = video_drvdata(file);
734 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
736 struct go7007 *go = video_drvdata(file);
738 if (a->index >= go->board_info->num_aud_inputs)
740 strlcpy(a->name, go->board_info->aud_inputs[a->index].name,
742 a->capability = V4L2_AUDCAP_STEREO;
746 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
748 struct go7007 *go = video_drvdata(file);
750 a->index = go->aud_input;
751 strlcpy(a->name, go->board_info->aud_inputs[go->aud_input].name,
753 a->capability = V4L2_AUDCAP_STEREO;
757 static int vidioc_s_audio(struct file *file, void *fh,
758 const struct v4l2_audio *a)
760 struct go7007 *go = video_drvdata(file);
762 if (a->index >= go->board_info->num_aud_inputs)
764 go->aud_input = a->index;
765 v4l2_subdev_call(go->sd_audio, audio, s_routing,
766 go->board_info->aud_inputs[go->aud_input].audio_input, 0, 0);
770 static void go7007_s_input(struct go7007 *go)
772 unsigned int input = go->input;
774 v4l2_subdev_call(go->sd_video, video, s_routing,
775 go->board_info->inputs[input].video_input, 0,
776 go->board_info->video_config);
777 if (go->board_info->num_aud_inputs) {
778 int aud_input = go->board_info->inputs[input].audio_index;
780 v4l2_subdev_call(go->sd_audio, audio, s_routing,
781 go->board_info->aud_inputs[aud_input].audio_input, 0, 0);
782 go->aud_input = aud_input;
786 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
788 struct go7007 *go = video_drvdata(file);
790 if (input >= go->board_info->num_inputs)
792 if (vb2_is_busy(&go->vidq))
801 static int vidioc_g_tuner(struct file *file, void *priv,
802 struct v4l2_tuner *t)
804 struct go7007 *go = video_drvdata(file);
809 strlcpy(t->name, "Tuner", sizeof(t->name));
810 return call_all(&go->v4l2_dev, tuner, g_tuner, t);
813 static int vidioc_s_tuner(struct file *file, void *priv,
814 const struct v4l2_tuner *t)
816 struct go7007 *go = video_drvdata(file);
821 return call_all(&go->v4l2_dev, tuner, s_tuner, t);
824 static int vidioc_g_frequency(struct file *file, void *priv,
825 struct v4l2_frequency *f)
827 struct go7007 *go = video_drvdata(file);
832 return call_all(&go->v4l2_dev, tuner, g_frequency, f);
835 static int vidioc_s_frequency(struct file *file, void *priv,
836 const struct v4l2_frequency *f)
838 struct go7007 *go = video_drvdata(file);
843 return call_all(&go->v4l2_dev, tuner, s_frequency, f);
846 static int vidioc_log_status(struct file *file, void *priv)
848 struct go7007 *go = video_drvdata(file);
850 v4l2_ctrl_log_status(file, priv);
851 return call_all(&go->v4l2_dev, core, log_status);
855 Those ioctls are private, and not needed, since several standard
856 extended controls already provide streaming control.
857 So, those ioctls should be converted into vidioc_g_ext_ctrls()
858 and vidioc_s_ext_ctrls()
862 case GO7007IOC_S_MD_PARAMS:
864 struct go7007_md_params *mdp = arg;
868 if (mdp->trigger > 0) {
869 go->modet[mdp->region].pixel_threshold =
870 mdp->pixel_threshold >> 1;
871 go->modet[mdp->region].motion_threshold =
872 mdp->motion_threshold >> 1;
873 go->modet[mdp->region].mb_threshold =
875 go->modet[mdp->region].enable = 1;
877 go->modet[mdp->region].enable = 0;
880 case GO7007IOC_S_MD_REGION:
882 struct go7007_md_region *region = arg;
884 if (region->region < 1 || region->region > 3)
886 return clip_to_modet_map(go, region->region, region->clips);
890 static struct v4l2_file_operations go7007_fops = {
891 .owner = THIS_MODULE,
892 .open = v4l2_fh_open,
893 .release = vb2_fop_release,
894 .unlocked_ioctl = video_ioctl2,
895 .read = vb2_fop_read,
896 .mmap = vb2_fop_mmap,
897 .poll = vb2_fop_poll,
900 static const struct v4l2_ioctl_ops video_ioctl_ops = {
901 .vidioc_querycap = vidioc_querycap,
902 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
903 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
904 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
905 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
906 .vidioc_reqbufs = vb2_ioctl_reqbufs,
907 .vidioc_querybuf = vb2_ioctl_querybuf,
908 .vidioc_qbuf = vb2_ioctl_qbuf,
909 .vidioc_dqbuf = vb2_ioctl_dqbuf,
910 .vidioc_g_std = vidioc_g_std,
911 .vidioc_s_std = vidioc_s_std,
912 .vidioc_querystd = vidioc_querystd,
913 .vidioc_enum_input = vidioc_enum_input,
914 .vidioc_g_input = vidioc_g_input,
915 .vidioc_s_input = vidioc_s_input,
916 .vidioc_enumaudio = vidioc_enumaudio,
917 .vidioc_g_audio = vidioc_g_audio,
918 .vidioc_s_audio = vidioc_s_audio,
919 .vidioc_streamon = vb2_ioctl_streamon,
920 .vidioc_streamoff = vb2_ioctl_streamoff,
921 .vidioc_g_tuner = vidioc_g_tuner,
922 .vidioc_s_tuner = vidioc_s_tuner,
923 .vidioc_g_frequency = vidioc_g_frequency,
924 .vidioc_s_frequency = vidioc_s_frequency,
925 .vidioc_g_parm = vidioc_g_parm,
926 .vidioc_s_parm = vidioc_s_parm,
927 .vidioc_enum_framesizes = vidioc_enum_framesizes,
928 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
929 .vidioc_log_status = vidioc_log_status,
930 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
931 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
934 static struct video_device go7007_template = {
936 .fops = &go7007_fops,
937 .release = video_device_release_empty,
938 .ioctl_ops = &video_ioctl_ops,
939 .tvnorms = V4L2_STD_ALL,
942 int go7007_v4l2_ctrl_init(struct go7007 *go)
944 struct v4l2_ctrl_handler *hdl = &go->hdl;
945 struct v4l2_ctrl *ctrl;
947 v4l2_ctrl_handler_init(hdl, 13);
948 go->mpeg_video_gop_size = v4l2_ctrl_new_std(hdl, NULL,
949 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, 34, 1, 15);
950 go->mpeg_video_gop_closure = v4l2_ctrl_new_std(hdl, NULL,
951 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE, 0, 1, 1, 1);
952 go->mpeg_video_bitrate = v4l2_ctrl_new_std(hdl, NULL,
953 V4L2_CID_MPEG_VIDEO_BITRATE,
954 64000, 10000000, 1, 9800000);
955 go->mpeg_video_b_frames = v4l2_ctrl_new_std(hdl, NULL,
956 V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 2, 2, 0);
957 go->mpeg_video_rep_seqheader = v4l2_ctrl_new_std(hdl, NULL,
958 V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, 0, 1, 1, 1);
960 go->mpeg_video_aspect_ratio = v4l2_ctrl_new_std_menu(hdl, NULL,
961 V4L2_CID_MPEG_VIDEO_ASPECT,
962 V4L2_MPEG_VIDEO_ASPECT_16x9, 0,
963 V4L2_MPEG_VIDEO_ASPECT_1x1);
964 ctrl = v4l2_ctrl_new_std(hdl, NULL,
965 V4L2_CID_JPEG_ACTIVE_MARKER, 0,
966 V4L2_JPEG_ACTIVE_MARKER_DQT |
967 V4L2_JPEG_ACTIVE_MARKER_DHT, 0,
968 V4L2_JPEG_ACTIVE_MARKER_DQT |
969 V4L2_JPEG_ACTIVE_MARKER_DHT);
971 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
975 v4l2_err(&go->v4l2_dev, "Could not register controls\n");
978 go->v4l2_dev.ctrl_handler = hdl;
982 int go7007_v4l2_init(struct go7007 *go)
984 struct video_device *vdev = &go->vdev;
987 mutex_init(&go->serialize_lock);
988 mutex_init(&go->queue_lock);
990 INIT_LIST_HEAD(&go->vidq_active);
991 go->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
992 go->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
993 go->vidq.ops = &go7007_video_qops;
994 go->vidq.mem_ops = &vb2_vmalloc_memops;
995 go->vidq.drv_priv = go;
996 go->vidq.buf_struct_size = sizeof(struct go7007_buffer);
997 go->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
998 go->vidq.lock = &go->queue_lock;
999 rv = vb2_queue_init(&go->vidq);
1002 *vdev = go7007_template;
1003 vdev->lock = &go->serialize_lock;
1004 vdev->queue = &go->vidq;
1005 set_bit(V4L2_FL_USE_FH_PRIO, &vdev->flags);
1006 video_set_drvdata(vdev, go);
1007 vdev->v4l2_dev = &go->v4l2_dev;
1008 if (!v4l2_device_has_op(&go->v4l2_dev, video, querystd))
1009 v4l2_disable_ioctl(vdev, VIDIOC_QUERYSTD);
1010 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER)) {
1011 v4l2_disable_ioctl(vdev, VIDIOC_S_FREQUENCY);
1012 v4l2_disable_ioctl(vdev, VIDIOC_G_FREQUENCY);
1013 v4l2_disable_ioctl(vdev, VIDIOC_S_TUNER);
1014 v4l2_disable_ioctl(vdev, VIDIOC_G_TUNER);
1016 struct v4l2_frequency f = {
1017 .type = V4L2_TUNER_ANALOG_TV,
1021 call_all(&go->v4l2_dev, tuner, s_frequency, &f);
1023 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV)) {
1024 v4l2_disable_ioctl(vdev, VIDIOC_G_STD);
1025 v4l2_disable_ioctl(vdev, VIDIOC_S_STD);
1028 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING)
1029 v4l2_disable_ioctl(vdev, VIDIOC_ENUM_FRAMESIZES);
1030 if (go->board_info->num_aud_inputs == 0) {
1031 v4l2_disable_ioctl(vdev, VIDIOC_G_AUDIO);
1032 v4l2_disable_ioctl(vdev, VIDIOC_S_AUDIO);
1033 v4l2_disable_ioctl(vdev, VIDIOC_ENUMAUDIO);
1035 /* Setup correct crystal frequency on this board */
1036 if (go->board_info->sensor_flags & GO7007_SENSOR_SAA7115)
1037 v4l2_subdev_call(go->sd_video, video, s_crystal_freq,
1038 SAA7115_FREQ_24_576_MHZ,
1039 SAA7115_FREQ_FL_APLL | SAA7115_FREQ_FL_UCGC |
1040 SAA7115_FREQ_FL_DOUBLE_ASCLK);
1042 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1044 rv = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1047 dev_info(go->dev, "registered device %s [v4l2]\n",
1048 video_device_node_name(vdev));
1053 void go7007_v4l2_remove(struct go7007 *go)
1055 v4l2_ctrl_handler_free(&go->hdl);