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>
24 #include <linux/slab.h>
26 #include <linux/unistd.h>
27 #include <linux/time.h>
28 #include <linux/vmalloc.h>
29 #include <linux/pagemap.h>
30 #include <linux/i2c.h>
31 #include <linux/mutex.h>
32 #include <linux/uaccess.h>
33 #include <linux/videodev2.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-ioctl.h>
36 #include <media/v4l2-subdev.h>
37 #include <media/v4l2-event.h>
38 #include <media/videobuf2-vmalloc.h>
39 #include <media/saa7115.h>
42 #include "go7007-priv.h"
44 #define call_all(dev, o, f, args...) \
45 v4l2_device_call_until_err(dev, 0, o, f, ##args)
47 static bool valid_pixelformat(u32 pixelformat
)
49 switch (pixelformat
) {
50 case V4L2_PIX_FMT_MJPEG
:
51 case V4L2_PIX_FMT_MPEG1
:
52 case V4L2_PIX_FMT_MPEG2
:
53 case V4L2_PIX_FMT_MPEG4
:
60 static u32
get_frame_type_flag(struct go7007_buffer
*vb
, int format
)
62 u8
*ptr
= vb2_plane_vaddr(&vb
->vb
, 0);
65 case V4L2_PIX_FMT_MJPEG
:
66 return V4L2_BUF_FLAG_KEYFRAME
;
67 case V4L2_PIX_FMT_MPEG4
:
68 switch ((ptr
[vb
->frame_offset
+ 4] >> 6) & 0x3) {
70 return V4L2_BUF_FLAG_KEYFRAME
;
72 return V4L2_BUF_FLAG_PFRAME
;
74 return V4L2_BUF_FLAG_BFRAME
;
78 case V4L2_PIX_FMT_MPEG1
:
79 case V4L2_PIX_FMT_MPEG2
:
80 switch ((ptr
[vb
->frame_offset
+ 5] >> 3) & 0x7) {
82 return V4L2_BUF_FLAG_KEYFRAME
;
84 return V4L2_BUF_FLAG_PFRAME
;
86 return V4L2_BUF_FLAG_BFRAME
;
95 static void get_resolution(struct go7007
*go
, int *width
, int *height
)
97 switch (go
->standard
) {
106 case GO7007_STD_OTHER
:
108 *width
= go
->board_info
->sensor_width
;
109 *height
= go
->board_info
->sensor_height
;
114 static void set_formatting(struct go7007
*go
)
116 if (go
->format
== V4L2_PIX_FMT_MJPEG
) {
118 go
->aspect_ratio
= GO7007_RATIO_1_1
;
122 go
->repeat_seqhead
= 0;
123 go
->seq_header_enable
= 0;
124 go
->gop_header_enable
= 0;
129 switch (go
->format
) {
130 case V4L2_PIX_FMT_MPEG1
:
134 case V4L2_PIX_FMT_MPEG2
:
137 case V4L2_PIX_FMT_MPEG4
:
138 /* For future reference: this is the list of MPEG4
139 * profiles that are available, although they are
143 * -------------- ----
148 * PROFILE_ARTS_L1 0x91
149 * PROFILE_ARTS_L2 0x92
150 * PROFILE_ARTS_L3 0x93
151 * PROFILE_ARTS_L4 0x94
162 go
->gop_size
= v4l2_ctrl_g_ctrl(go
->mpeg_video_gop_size
);
163 go
->closed_gop
= v4l2_ctrl_g_ctrl(go
->mpeg_video_gop_closure
);
164 go
->ipb
= v4l2_ctrl_g_ctrl(go
->mpeg_video_b_frames
) != 0;
165 go
->bitrate
= v4l2_ctrl_g_ctrl(go
->mpeg_video_bitrate
);
166 go
->gop_header_enable
= 1;
168 if (go
->format
== V4L2_PIX_FMT_MPEG2
)
170 go
->bitrate
== 9800000 &&
171 go
->gop_size
== 15 &&
174 go
->repeat_seqhead
= go
->dvd_mode
;
176 switch (v4l2_ctrl_g_ctrl(go
->mpeg_video_aspect_ratio
)) {
178 case V4L2_MPEG_VIDEO_ASPECT_1x1
:
179 go
->aspect_ratio
= GO7007_RATIO_1_1
;
181 case V4L2_MPEG_VIDEO_ASPECT_4x3
:
182 go
->aspect_ratio
= GO7007_RATIO_4_3
;
184 case V4L2_MPEG_VIDEO_ASPECT_16x9
:
185 go
->aspect_ratio
= GO7007_RATIO_16_9
;
190 static int set_capture_size(struct go7007
*go
, struct v4l2_format
*fmt
, int try)
192 int sensor_height
= 0, sensor_width
= 0;
193 int width
, height
, i
;
195 if (fmt
!= NULL
&& !valid_pixelformat(fmt
->fmt
.pix
.pixelformat
))
198 get_resolution(go
, &sensor_width
, &sensor_height
);
201 width
= sensor_width
;
202 height
= sensor_height
;
203 } else if (go
->board_info
->sensor_flags
& GO7007_SENSOR_SCALING
) {
204 if (fmt
->fmt
.pix
.width
> sensor_width
)
205 width
= sensor_width
;
206 else if (fmt
->fmt
.pix
.width
< 144)
209 width
= fmt
->fmt
.pix
.width
& ~0x0f;
211 if (fmt
->fmt
.pix
.height
> sensor_height
)
212 height
= sensor_height
;
213 else if (fmt
->fmt
.pix
.height
< 96)
216 height
= fmt
->fmt
.pix
.height
& ~0x0f;
218 width
= fmt
->fmt
.pix
.width
;
220 if (width
<= sensor_width
/ 4) {
221 width
= sensor_width
/ 4;
222 height
= sensor_height
/ 4;
223 } else if (width
<= sensor_width
/ 2) {
224 width
= sensor_width
/ 2;
225 height
= sensor_height
/ 2;
227 width
= sensor_width
;
228 height
= sensor_height
;
235 u32 pixelformat
= fmt
->fmt
.pix
.pixelformat
;
237 memset(fmt
, 0, sizeof(*fmt
));
238 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
239 fmt
->fmt
.pix
.width
= width
;
240 fmt
->fmt
.pix
.height
= height
;
241 fmt
->fmt
.pix
.pixelformat
= pixelformat
;
242 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
243 fmt
->fmt
.pix
.bytesperline
= 0;
244 fmt
->fmt
.pix
.sizeimage
= GO7007_BUF_SIZE
;
245 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
252 go
->format
= fmt
->fmt
.pix
.pixelformat
;
255 go
->encoder_h_offset
= go
->board_info
->sensor_h_offset
;
256 go
->encoder_v_offset
= go
->board_info
->sensor_v_offset
;
257 for (i
= 0; i
< 4; ++i
)
258 go
->modet
[i
].enable
= 0;
259 for (i
= 0; i
< 1624; ++i
)
260 go
->modet_map
[i
] = 0;
262 if (go
->board_info
->sensor_flags
& GO7007_SENSOR_SCALING
) {
263 struct v4l2_mbus_framefmt mbus_fmt
;
265 mbus_fmt
.code
= V4L2_MBUS_FMT_FIXED
;
266 mbus_fmt
.width
= fmt
? fmt
->fmt
.pix
.width
: width
;
267 mbus_fmt
.height
= height
;
268 go
->encoder_h_halve
= 0;
269 go
->encoder_v_halve
= 0;
270 go
->encoder_subsample
= 0;
271 call_all(&go
->v4l2_dev
, video
, s_mbus_fmt
, &mbus_fmt
);
273 if (width
<= sensor_width
/ 4) {
274 go
->encoder_h_halve
= 1;
275 go
->encoder_v_halve
= 1;
276 go
->encoder_subsample
= 1;
277 } else if (width
<= sensor_width
/ 2) {
278 go
->encoder_h_halve
= 1;
279 go
->encoder_v_halve
= 1;
280 go
->encoder_subsample
= 0;
282 go
->encoder_h_halve
= 0;
283 go
->encoder_v_halve
= 0;
284 go
->encoder_subsample
= 0;
291 static int clip_to_modet_map(struct go7007
*go
, int region
,
292 struct v4l2_clip
*clip_list
)
294 struct v4l2_clip clip
, *clip_ptr
;
297 /* Check if coordinates are OK and if any macroblocks are already
298 * used by other regions (besides 0) */
299 clip_ptr
= clip_list
;
301 if (copy_from_user(&clip
, clip_ptr
, sizeof(clip
)))
303 if (clip
.c
.left
< 0 || (clip
.c
.left
& 0xF) ||
304 clip
.c
.width
<= 0 || (clip
.c
.width
& 0xF))
306 if (clip
.c
.left
+ clip
.c
.width
> go
->width
)
308 if (clip
.c
.top
< 0 || (clip
.c
.top
& 0xF) ||
309 clip
.c
.height
<= 0 || (clip
.c
.height
& 0xF))
311 if (clip
.c
.top
+ clip
.c
.height
> go
->height
)
313 for (y
= 0; y
< clip
.c
.height
; y
+= 16)
314 for (x
= 0; x
< clip
.c
.width
; x
+= 16) {
315 mbnum
= (go
->width
>> 4) *
316 ((clip
.c
.top
+ y
) >> 4) +
317 ((clip
.c
.left
+ x
) >> 4);
318 if (go
->modet_map
[mbnum
] != 0 &&
319 go
->modet_map
[mbnum
] != region
)
322 clip_ptr
= clip
.next
;
325 /* Clear old region macroblocks */
326 for (mbnum
= 0; mbnum
< 1624; ++mbnum
)
327 if (go
->modet_map
[mbnum
] == region
)
328 go
->modet_map
[mbnum
] = 0;
330 /* Claim macroblocks in this list */
331 clip_ptr
= clip_list
;
333 if (copy_from_user(&clip
, clip_ptr
, sizeof(clip
)))
335 for (y
= 0; y
< clip
.c
.height
; y
+= 16)
336 for (x
= 0; x
< clip
.c
.width
; x
+= 16) {
337 mbnum
= (go
->width
>> 4) *
338 ((clip
.c
.top
+ y
) >> 4) +
339 ((clip
.c
.left
+ x
) >> 4);
340 go
->modet_map
[mbnum
] = region
;
342 clip_ptr
= clip
.next
;
348 static int vidioc_querycap(struct file
*file
, void *priv
,
349 struct v4l2_capability
*cap
)
351 struct go7007
*go
= video_drvdata(file
);
353 strlcpy(cap
->driver
, "go7007", sizeof(cap
->driver
));
354 strlcpy(cap
->card
, go
->name
, sizeof(cap
->card
));
355 strlcpy(cap
->bus_info
, go
->bus_info
, sizeof(cap
->bus_info
));
357 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_READWRITE
|
360 if (go
->board_info
->num_aud_inputs
)
361 cap
->device_caps
|= V4L2_CAP_AUDIO
;
362 if (go
->board_info
->flags
& GO7007_BOARD_HAS_TUNER
)
363 cap
->device_caps
|= V4L2_CAP_TUNER
;
364 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
368 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
369 struct v4l2_fmtdesc
*fmt
)
373 switch (fmt
->index
) {
375 fmt
->pixelformat
= V4L2_PIX_FMT_MJPEG
;
376 desc
= "Motion JPEG";
379 fmt
->pixelformat
= V4L2_PIX_FMT_MPEG1
;
383 fmt
->pixelformat
= V4L2_PIX_FMT_MPEG2
;
387 fmt
->pixelformat
= V4L2_PIX_FMT_MPEG4
;
393 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
394 fmt
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
396 strncpy(fmt
->description
, desc
, sizeof(fmt
->description
));
401 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
402 struct v4l2_format
*fmt
)
404 struct go7007
*go
= video_drvdata(file
);
406 fmt
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
407 fmt
->fmt
.pix
.width
= go
->width
;
408 fmt
->fmt
.pix
.height
= go
->height
;
409 fmt
->fmt
.pix
.pixelformat
= go
->format
;
410 fmt
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
411 fmt
->fmt
.pix
.bytesperline
= 0;
412 fmt
->fmt
.pix
.sizeimage
= GO7007_BUF_SIZE
;
413 fmt
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
418 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
419 struct v4l2_format
*fmt
)
421 struct go7007
*go
= video_drvdata(file
);
423 return set_capture_size(go
, fmt
, 1);
426 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
427 struct v4l2_format
*fmt
)
429 struct go7007
*go
= video_drvdata(file
);
431 if (vb2_is_busy(&go
->vidq
))
434 return set_capture_size(go
, fmt
, 0);
437 static int go7007_queue_setup(struct vb2_queue
*q
, 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 int 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
;
490 static int go7007_start_streaming(struct vb2_queue
*q
, unsigned int count
)
492 struct go7007
*go
= vb2_get_drv_priv(q
);
496 mutex_lock(&go
->hw_lock
);
498 go
->active_buf
= NULL
;
500 if (go7007_start_encoder(go
) < 0)
504 mutex_unlock(&go
->hw_lock
);
509 call_all(&go
->v4l2_dev
, video
, s_stream
, 1);
510 v4l2_ctrl_grab(go
->mpeg_video_gop_size
, true);
511 v4l2_ctrl_grab(go
->mpeg_video_gop_closure
, true);
512 v4l2_ctrl_grab(go
->mpeg_video_bitrate
, true);
513 v4l2_ctrl_grab(go
->mpeg_video_aspect_ratio
, true);
517 static int go7007_stop_streaming(struct vb2_queue
*q
)
519 struct go7007
*go
= vb2_get_drv_priv(q
);
523 go7007_stream_stop(go
);
524 mutex_lock(&go
->hw_lock
);
525 go7007_reset_encoder(go
);
526 mutex_unlock(&go
->hw_lock
);
527 call_all(&go
->v4l2_dev
, video
, s_stream
, 0);
529 spin_lock_irqsave(&go
->spinlock
, flags
);
530 INIT_LIST_HEAD(&go
->vidq_active
);
531 spin_unlock_irqrestore(&go
->spinlock
, flags
);
532 v4l2_ctrl_grab(go
->mpeg_video_gop_size
, false);
533 v4l2_ctrl_grab(go
->mpeg_video_gop_closure
, false);
534 v4l2_ctrl_grab(go
->mpeg_video_bitrate
, false);
535 v4l2_ctrl_grab(go
->mpeg_video_aspect_ratio
, false);
539 static struct vb2_ops go7007_video_qops
= {
540 .queue_setup
= go7007_queue_setup
,
541 .buf_queue
= go7007_buf_queue
,
542 .buf_prepare
= go7007_buf_prepare
,
543 .buf_finish
= go7007_buf_finish
,
544 .start_streaming
= go7007_start_streaming
,
545 .stop_streaming
= go7007_stop_streaming
,
546 .wait_prepare
= vb2_ops_wait_prepare
,
547 .wait_finish
= vb2_ops_wait_finish
,
550 static int vidioc_g_parm(struct file
*filp
, void *priv
,
551 struct v4l2_streamparm
*parm
)
553 struct go7007
*go
= video_drvdata(filp
);
554 struct v4l2_fract timeperframe
= {
555 .numerator
= 1001 * go
->fps_scale
,
556 .denominator
= go
->sensor_framerate
,
559 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
562 parm
->parm
.capture
.readbuffers
= 2;
563 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
564 parm
->parm
.capture
.timeperframe
= timeperframe
;
569 static int vidioc_s_parm(struct file
*filp
, void *priv
,
570 struct v4l2_streamparm
*parm
)
572 struct go7007
*go
= video_drvdata(filp
);
575 if (parm
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
578 n
= go
->sensor_framerate
*
579 parm
->parm
.capture
.timeperframe
.numerator
;
580 d
= 1001 * parm
->parm
.capture
.timeperframe
.denominator
;
581 if (n
!= 0 && d
!= 0 && n
> d
)
582 go
->fps_scale
= (n
+ d
/2) / d
;
586 return vidioc_g_parm(filp
, priv
, parm
);
589 /* VIDIOC_ENUMSTD on go7007 were used for enumerating the supported fps and
590 its resolution, when the device is not connected to TV.
591 This is were an API abuse, probably used by the lack of specific IOCTL's to
592 enumerate it, by the time the driver was written.
594 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
595 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
597 The two functions below implement the newer ioctls
599 static int vidioc_enum_framesizes(struct file
*filp
, void *priv
,
600 struct v4l2_frmsizeenum
*fsize
)
602 struct go7007
*go
= video_drvdata(filp
);
605 if (fsize
->index
> 2)
608 if (!valid_pixelformat(fsize
->pixel_format
))
611 get_resolution(go
, &width
, &height
);
612 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
613 fsize
->discrete
.width
= (width
>> fsize
->index
) & ~0xf;
614 fsize
->discrete
.height
= (height
>> fsize
->index
) & ~0xf;
618 static int vidioc_enum_frameintervals(struct file
*filp
, void *priv
,
619 struct v4l2_frmivalenum
*fival
)
621 struct go7007
*go
= video_drvdata(filp
);
625 if (fival
->index
> 4)
628 if (!valid_pixelformat(fival
->pixel_format
))
631 if (!(go
->board_info
->sensor_flags
& GO7007_SENSOR_SCALING
)) {
632 get_resolution(go
, &width
, &height
);
633 for (i
= 0; i
<= 2; i
++)
634 if (fival
->width
== ((width
>> i
) & ~0xf) &&
635 fival
->height
== ((height
>> i
) & ~0xf))
640 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
641 fival
->discrete
.numerator
= 1001 * (fival
->index
+ 1);
642 fival
->discrete
.denominator
= go
->sensor_framerate
;
646 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*std
)
648 struct go7007
*go
= video_drvdata(file
);
654 static int go7007_s_std(struct go7007
*go
)
656 if (go
->std
& V4L2_STD_625_50
) {
657 go
->standard
= GO7007_STD_PAL
;
658 go
->sensor_framerate
= 25025;
660 go
->standard
= GO7007_STD_NTSC
;
661 go
->sensor_framerate
= 30000;
664 call_all(&go
->v4l2_dev
, core
, s_std
, go
->std
);
665 set_capture_size(go
, NULL
, 0);
669 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id std
)
671 struct go7007
*go
= video_drvdata(file
);
673 if (vb2_is_busy(&go
->vidq
))
678 return go7007_s_std(go
);
681 static int vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
683 struct go7007
*go
= video_drvdata(file
);
685 return call_all(&go
->v4l2_dev
, video
, querystd
, std
);
688 static int vidioc_enum_input(struct file
*file
, void *priv
,
689 struct v4l2_input
*inp
)
691 struct go7007
*go
= video_drvdata(file
);
693 if (inp
->index
>= go
->board_info
->num_inputs
)
696 strncpy(inp
->name
, go
->board_info
->inputs
[inp
->index
].name
,
699 /* If this board has a tuner, it will be the first input */
700 if ((go
->board_info
->flags
& GO7007_BOARD_HAS_TUNER
) &&
702 inp
->type
= V4L2_INPUT_TYPE_TUNER
;
704 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
706 if (go
->board_info
->num_aud_inputs
)
707 inp
->audioset
= (1 << go
->board_info
->num_aud_inputs
) - 1;
711 if (go
->board_info
->sensor_flags
& GO7007_SENSOR_TV
)
712 inp
->std
= video_devdata(file
)->tvnorms
;
720 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *input
)
722 struct go7007
*go
= video_drvdata(file
);
729 static int vidioc_enumaudio(struct file
*file
, void *fh
, struct v4l2_audio
*a
)
731 struct go7007
*go
= video_drvdata(file
);
733 if (a
->index
>= go
->board_info
->num_aud_inputs
)
735 strlcpy(a
->name
, go
->board_info
->aud_inputs
[a
->index
].name
, sizeof(a
->name
));
736 a
->capability
= V4L2_AUDCAP_STEREO
;
740 static int vidioc_g_audio(struct file
*file
, void *fh
, struct v4l2_audio
*a
)
742 struct go7007
*go
= video_drvdata(file
);
744 a
->index
= go
->aud_input
;
745 strlcpy(a
->name
, go
->board_info
->aud_inputs
[go
->aud_input
].name
, sizeof(a
->name
));
746 a
->capability
= V4L2_AUDCAP_STEREO
;
750 static int vidioc_s_audio(struct file
*file
, void *fh
, const struct v4l2_audio
*a
)
752 struct go7007
*go
= video_drvdata(file
);
754 if (a
->index
>= go
->board_info
->num_aud_inputs
)
756 go
->aud_input
= a
->index
;
757 v4l2_subdev_call(go
->sd_audio
, audio
, s_routing
,
758 go
->board_info
->aud_inputs
[go
->aud_input
].audio_input
, 0, 0);
762 static void go7007_s_input(struct go7007
*go
)
764 unsigned int input
= go
->input
;
766 v4l2_subdev_call(go
->sd_video
, video
, s_routing
,
767 go
->board_info
->inputs
[input
].video_input
, 0,
768 go
->board_info
->video_config
);
769 if (go
->board_info
->num_aud_inputs
) {
770 int aud_input
= go
->board_info
->inputs
[input
].audio_index
;
772 v4l2_subdev_call(go
->sd_audio
, audio
, s_routing
,
773 go
->board_info
->aud_inputs
[aud_input
].audio_input
, 0, 0);
774 go
->aud_input
= aud_input
;
778 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int input
)
780 struct go7007
*go
= video_drvdata(file
);
782 if (input
>= go
->board_info
->num_inputs
)
784 if (vb2_is_busy(&go
->vidq
))
793 static int vidioc_g_tuner(struct file
*file
, void *priv
,
794 struct v4l2_tuner
*t
)
796 struct go7007
*go
= video_drvdata(file
);
801 strlcpy(t
->name
, "Tuner", sizeof(t
->name
));
802 return call_all(&go
->v4l2_dev
, tuner
, g_tuner
, t
);
805 static int vidioc_s_tuner(struct file
*file
, void *priv
,
806 const struct v4l2_tuner
*t
)
808 struct go7007
*go
= video_drvdata(file
);
813 return call_all(&go
->v4l2_dev
, tuner
, s_tuner
, t
);
816 static int vidioc_g_frequency(struct file
*file
, void *priv
,
817 struct v4l2_frequency
*f
)
819 struct go7007
*go
= video_drvdata(file
);
824 return call_all(&go
->v4l2_dev
, tuner
, g_frequency
, f
);
827 static int vidioc_s_frequency(struct file
*file
, void *priv
,
828 const struct v4l2_frequency
*f
)
830 struct go7007
*go
= video_drvdata(file
);
835 return call_all(&go
->v4l2_dev
, tuner
, s_frequency
, f
);
838 static int vidioc_log_status(struct file
*file
, void *priv
)
840 struct go7007
*go
= video_drvdata(file
);
842 v4l2_ctrl_log_status(file
, priv
);
843 return call_all(&go
->v4l2_dev
, core
, log_status
);
847 Those ioctls are private, and not needed, since several standard
848 extended controls already provide streaming control.
849 So, those ioctls should be converted into vidioc_g_ext_ctrls()
850 and vidioc_s_ext_ctrls()
854 case GO7007IOC_S_MD_PARAMS
:
856 struct go7007_md_params
*mdp
= arg
;
860 if (mdp
->trigger
> 0) {
861 go
->modet
[mdp
->region
].pixel_threshold
=
862 mdp
->pixel_threshold
>> 1;
863 go
->modet
[mdp
->region
].motion_threshold
=
864 mdp
->motion_threshold
>> 1;
865 go
->modet
[mdp
->region
].mb_threshold
=
867 go
->modet
[mdp
->region
].enable
= 1;
869 go
->modet
[mdp
->region
].enable
= 0;
872 case GO7007IOC_S_MD_REGION
:
874 struct go7007_md_region
*region
= arg
;
876 if (region
->region
< 1 || region
->region
> 3)
878 return clip_to_modet_map(go
, region
->region
, region
->clips
);
882 static struct v4l2_file_operations go7007_fops
= {
883 .owner
= THIS_MODULE
,
884 .open
= v4l2_fh_open
,
885 .release
= vb2_fop_release
,
886 .unlocked_ioctl
= video_ioctl2
,
887 .read
= vb2_fop_read
,
888 .mmap
= vb2_fop_mmap
,
889 .poll
= vb2_fop_poll
,
892 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
893 .vidioc_querycap
= vidioc_querycap
,
894 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
895 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
896 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
897 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
898 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
899 .vidioc_querybuf
= vb2_ioctl_querybuf
,
900 .vidioc_qbuf
= vb2_ioctl_qbuf
,
901 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
902 .vidioc_g_std
= vidioc_g_std
,
903 .vidioc_s_std
= vidioc_s_std
,
904 .vidioc_querystd
= vidioc_querystd
,
905 .vidioc_enum_input
= vidioc_enum_input
,
906 .vidioc_g_input
= vidioc_g_input
,
907 .vidioc_s_input
= vidioc_s_input
,
908 .vidioc_enumaudio
= vidioc_enumaudio
,
909 .vidioc_g_audio
= vidioc_g_audio
,
910 .vidioc_s_audio
= vidioc_s_audio
,
911 .vidioc_streamon
= vb2_ioctl_streamon
,
912 .vidioc_streamoff
= vb2_ioctl_streamoff
,
913 .vidioc_g_tuner
= vidioc_g_tuner
,
914 .vidioc_s_tuner
= vidioc_s_tuner
,
915 .vidioc_g_frequency
= vidioc_g_frequency
,
916 .vidioc_s_frequency
= vidioc_s_frequency
,
917 .vidioc_g_parm
= vidioc_g_parm
,
918 .vidioc_s_parm
= vidioc_s_parm
,
919 .vidioc_enum_framesizes
= vidioc_enum_framesizes
,
920 .vidioc_enum_frameintervals
= vidioc_enum_frameintervals
,
921 .vidioc_log_status
= vidioc_log_status
,
922 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
923 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
926 static struct video_device go7007_template
= {
928 .fops
= &go7007_fops
,
929 .release
= video_device_release_empty
,
930 .ioctl_ops
= &video_ioctl_ops
,
931 .tvnorms
= V4L2_STD_ALL
,
934 int go7007_v4l2_ctrl_init(struct go7007
*go
)
936 struct v4l2_ctrl_handler
*hdl
= &go
->hdl
;
937 struct v4l2_ctrl
*ctrl
;
939 v4l2_ctrl_handler_init(hdl
, 13);
940 go
->mpeg_video_gop_size
= v4l2_ctrl_new_std(hdl
, NULL
,
941 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 0, 34, 1, 15);
942 go
->mpeg_video_gop_closure
= v4l2_ctrl_new_std(hdl
, NULL
,
943 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE
, 0, 1, 1, 1);
944 go
->mpeg_video_bitrate
= v4l2_ctrl_new_std(hdl
, NULL
,
945 V4L2_CID_MPEG_VIDEO_BITRATE
,
946 64000, 10000000, 1, 9800000);
947 go
->mpeg_video_b_frames
= v4l2_ctrl_new_std(hdl
, NULL
,
948 V4L2_CID_MPEG_VIDEO_B_FRAMES
, 0, 2, 2, 0);
950 go
->mpeg_video_aspect_ratio
= v4l2_ctrl_new_std_menu(hdl
, NULL
,
951 V4L2_CID_MPEG_VIDEO_ASPECT
,
952 V4L2_MPEG_VIDEO_ASPECT_16x9
, 0,
953 V4L2_MPEG_VIDEO_ASPECT_1x1
);
954 ctrl
= v4l2_ctrl_new_std(hdl
, NULL
,
955 V4L2_CID_JPEG_ACTIVE_MARKER
, 0,
956 V4L2_JPEG_ACTIVE_MARKER_DQT
| V4L2_JPEG_ACTIVE_MARKER_DHT
, 0,
957 V4L2_JPEG_ACTIVE_MARKER_DQT
| V4L2_JPEG_ACTIVE_MARKER_DHT
);
959 ctrl
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
963 v4l2_err(&go
->v4l2_dev
, "Could not register controls\n");
966 go
->v4l2_dev
.ctrl_handler
= hdl
;
970 int go7007_v4l2_init(struct go7007
*go
)
972 struct video_device
*vdev
= &go
->vdev
;
975 mutex_init(&go
->serialize_lock
);
976 mutex_init(&go
->queue_lock
);
978 INIT_LIST_HEAD(&go
->vidq_active
);
979 go
->vidq
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
980 go
->vidq
.io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_READ
;
981 go
->vidq
.ops
= &go7007_video_qops
;
982 go
->vidq
.mem_ops
= &vb2_vmalloc_memops
;
983 go
->vidq
.drv_priv
= go
;
984 go
->vidq
.buf_struct_size
= sizeof(struct go7007_buffer
);
985 go
->vidq
.timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
986 go
->vidq
.lock
= &go
->queue_lock
;
987 rv
= vb2_queue_init(&go
->vidq
);
990 *vdev
= go7007_template
;
991 vdev
->lock
= &go
->serialize_lock
;
992 vdev
->queue
= &go
->vidq
;
993 set_bit(V4L2_FL_USE_FH_PRIO
, &vdev
->flags
);
994 video_set_drvdata(vdev
, go
);
995 vdev
->v4l2_dev
= &go
->v4l2_dev
;
996 if (!v4l2_device_has_op(&go
->v4l2_dev
, video
, querystd
))
997 v4l2_disable_ioctl(vdev
, VIDIOC_QUERYSTD
);
998 if (!(go
->board_info
->flags
& GO7007_BOARD_HAS_TUNER
)) {
999 v4l2_disable_ioctl(vdev
, VIDIOC_S_FREQUENCY
);
1000 v4l2_disable_ioctl(vdev
, VIDIOC_G_FREQUENCY
);
1001 v4l2_disable_ioctl(vdev
, VIDIOC_S_TUNER
);
1002 v4l2_disable_ioctl(vdev
, VIDIOC_G_TUNER
);
1004 struct v4l2_frequency f
= {
1005 .type
= V4L2_TUNER_ANALOG_TV
,
1009 call_all(&go
->v4l2_dev
, tuner
, s_frequency
, &f
);
1011 if (!(go
->board_info
->sensor_flags
& GO7007_SENSOR_TV
)) {
1012 v4l2_disable_ioctl(vdev
, VIDIOC_G_STD
);
1013 v4l2_disable_ioctl(vdev
, VIDIOC_S_STD
);
1016 if (go
->board_info
->sensor_flags
& GO7007_SENSOR_SCALING
)
1017 v4l2_disable_ioctl(vdev
, VIDIOC_ENUM_FRAMESIZES
);
1018 if (go
->board_info
->num_aud_inputs
== 0) {
1019 v4l2_disable_ioctl(vdev
, VIDIOC_G_AUDIO
);
1020 v4l2_disable_ioctl(vdev
, VIDIOC_S_AUDIO
);
1021 v4l2_disable_ioctl(vdev
, VIDIOC_ENUMAUDIO
);
1023 /* Setup correct crystal frequency on this board */
1024 if (go
->board_info
->sensor_flags
& GO7007_SENSOR_SAA7115
)
1025 v4l2_subdev_call(go
->sd_video
, video
, s_crystal_freq
,
1026 SAA7115_FREQ_24_576_MHZ
,
1027 SAA7115_FREQ_FL_APLL
| SAA7115_FREQ_FL_UCGC
|
1028 SAA7115_FREQ_FL_DOUBLE_ASCLK
);
1030 if (go
->board_info
->sensor_flags
& GO7007_SENSOR_TV
)
1032 rv
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
1035 dev_info(go
->dev
, "registered device %s [v4l2]\n",
1036 video_device_node_name(vdev
));
1041 void go7007_v4l2_remove(struct go7007
*go
)
1043 v4l2_ctrl_handler_free(&go
->hdl
);