0dc316063b5d6c1282d65586934a0f65af30bb69
[deliverable/linux.git] / drivers / staging / media / go7007 / go7007-v4l2.c
1 /*
2 * Copyright (C) 2005-2006 Micronas USA Inc.
3 *
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.
7 *
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.
12 *
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.
16 */
17
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>
25 #include <linux/fs.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>
40
41 #include "go7007.h"
42 #include "go7007-priv.h"
43
44 #define call_all(dev, o, f, args...) \
45 v4l2_device_call_until_err(dev, 0, o, f, ##args)
46
47 static bool valid_pixelformat(u32 pixelformat)
48 {
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:
54 return true;
55 default:
56 return false;
57 }
58 }
59
60 static u32 get_frame_type_flag(struct go7007_buffer *vb, int format)
61 {
62 u8 *ptr = vb2_plane_vaddr(&vb->vb, 0);
63
64 switch (format) {
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) {
69 case 0:
70 return V4L2_BUF_FLAG_KEYFRAME;
71 case 1:
72 return V4L2_BUF_FLAG_PFRAME;
73 case 2:
74 return V4L2_BUF_FLAG_BFRAME;
75 default:
76 return 0;
77 }
78 case V4L2_PIX_FMT_MPEG1:
79 case V4L2_PIX_FMT_MPEG2:
80 switch ((ptr[vb->frame_offset + 5] >> 3) & 0x7) {
81 case 1:
82 return V4L2_BUF_FLAG_KEYFRAME;
83 case 2:
84 return V4L2_BUF_FLAG_PFRAME;
85 case 3:
86 return V4L2_BUF_FLAG_BFRAME;
87 default:
88 return 0;
89 }
90 }
91
92 return 0;
93 }
94
95 static void get_resolution(struct go7007 *go, int *width, int *height)
96 {
97 switch (go->standard) {
98 case GO7007_STD_NTSC:
99 *width = 720;
100 *height = 480;
101 break;
102 case GO7007_STD_PAL:
103 *width = 720;
104 *height = 576;
105 break;
106 case GO7007_STD_OTHER:
107 default:
108 *width = go->board_info->sensor_width;
109 *height = go->board_info->sensor_height;
110 break;
111 }
112 }
113
114 static void set_formatting(struct go7007 *go)
115 {
116 if (go->format == V4L2_PIX_FMT_MJPEG) {
117 go->pali = 0;
118 go->aspect_ratio = GO7007_RATIO_1_1;
119 go->gop_size = 0;
120 go->ipb = 0;
121 go->closed_gop = 0;
122 go->repeat_seqhead = 0;
123 go->seq_header_enable = 0;
124 go->gop_header_enable = 0;
125 go->dvd_mode = 0;
126 return;
127 }
128
129 switch (go->format) {
130 case V4L2_PIX_FMT_MPEG1:
131 go->pali = 0;
132 break;
133 default:
134 case V4L2_PIX_FMT_MPEG2:
135 go->pali = 0x48;
136 break;
137 case V4L2_PIX_FMT_MPEG4:
138 /* For future reference: this is the list of MPEG4
139 * profiles that are available, although they are
140 * untested:
141 *
142 * Profile pali
143 * -------------- ----
144 * PROFILE_S_L0 0x08
145 * PROFILE_S_L1 0x01
146 * PROFILE_S_L2 0x02
147 * PROFILE_S_L3 0x03
148 * PROFILE_ARTS_L1 0x91
149 * PROFILE_ARTS_L2 0x92
150 * PROFILE_ARTS_L3 0x93
151 * PROFILE_ARTS_L4 0x94
152 * PROFILE_AS_L0 0xf0
153 * PROFILE_AS_L1 0xf1
154 * PROFILE_AS_L2 0xf2
155 * PROFILE_AS_L3 0xf3
156 * PROFILE_AS_L4 0xf4
157 * PROFILE_AS_L5 0xf5
158 */
159 go->pali = 0xf5;
160 break;
161 }
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;
167 go->dvd_mode = 0;
168 if (go->format == V4L2_PIX_FMT_MPEG2)
169 go->dvd_mode =
170 go->bitrate == 9800000 &&
171 go->gop_size == 15 &&
172 go->ipb == 0 &&
173 go->closed_gop;
174 go->repeat_seqhead = go->dvd_mode;
175
176 switch (v4l2_ctrl_g_ctrl(go->mpeg_video_aspect_ratio)) {
177 default:
178 case V4L2_MPEG_VIDEO_ASPECT_1x1:
179 go->aspect_ratio = GO7007_RATIO_1_1;
180 break;
181 case V4L2_MPEG_VIDEO_ASPECT_4x3:
182 go->aspect_ratio = GO7007_RATIO_4_3;
183 break;
184 case V4L2_MPEG_VIDEO_ASPECT_16x9:
185 go->aspect_ratio = GO7007_RATIO_16_9;
186 break;
187 }
188 }
189
190 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
191 {
192 int sensor_height = 0, sensor_width = 0;
193 int width, height, i;
194
195 if (fmt != NULL && !valid_pixelformat(fmt->fmt.pix.pixelformat))
196 return -EINVAL;
197
198 get_resolution(go, &sensor_width, &sensor_height);
199
200 if (fmt == NULL) {
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)
207 width = 144;
208 else
209 width = fmt->fmt.pix.width & ~0x0f;
210
211 if (fmt->fmt.pix.height > sensor_height)
212 height = sensor_height;
213 else if (fmt->fmt.pix.height < 96)
214 height = 96;
215 else
216 height = fmt->fmt.pix.height & ~0x0f;
217 } else {
218 width = fmt->fmt.pix.width;
219
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;
226 } else {
227 width = sensor_width;
228 height = sensor_height;
229 }
230 width &= ~0xf;
231 height &= ~0xf;
232 }
233
234 if (fmt != NULL) {
235 u32 pixelformat = fmt->fmt.pix.pixelformat;
236
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;
246 }
247
248 if (try)
249 return 0;
250
251 if (fmt)
252 go->format = fmt->fmt.pix.pixelformat;
253 go->width = width;
254 go->height = height;
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;
261
262 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
263 struct v4l2_mbus_framefmt mbus_fmt;
264
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);
272 } else {
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;
281 } else {
282 go->encoder_h_halve = 0;
283 go->encoder_v_halve = 0;
284 go->encoder_subsample = 0;
285 }
286 }
287 return 0;
288 }
289
290 #if 0
291 static int clip_to_modet_map(struct go7007 *go, int region,
292 struct v4l2_clip *clip_list)
293 {
294 struct v4l2_clip clip, *clip_ptr;
295 int x, y, mbnum;
296
297 /* Check if coordinates are OK and if any macroblocks are already
298 * used by other regions (besides 0) */
299 clip_ptr = clip_list;
300 while (clip_ptr) {
301 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
302 return -EFAULT;
303 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
304 clip.c.width <= 0 || (clip.c.width & 0xF))
305 return -EINVAL;
306 if (clip.c.left + clip.c.width > go->width)
307 return -EINVAL;
308 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
309 clip.c.height <= 0 || (clip.c.height & 0xF))
310 return -EINVAL;
311 if (clip.c.top + clip.c.height > go->height)
312 return -EINVAL;
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)
320 return -EBUSY;
321 }
322 clip_ptr = clip.next;
323 }
324
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;
329
330 /* Claim macroblocks in this list */
331 clip_ptr = clip_list;
332 while (clip_ptr) {
333 if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
334 return -EFAULT;
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;
341 }
342 clip_ptr = clip.next;
343 }
344 return 0;
345 }
346 #endif
347
348 static int vidioc_querycap(struct file *file, void *priv,
349 struct v4l2_capability *cap)
350 {
351 struct go7007 *go = video_drvdata(file);
352
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));
356
357 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
358 V4L2_CAP_STREAMING;
359
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;
365 return 0;
366 }
367
368 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
369 struct v4l2_fmtdesc *fmt)
370 {
371 char *desc = NULL;
372
373 switch (fmt->index) {
374 case 0:
375 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
376 desc = "Motion JPEG";
377 break;
378 case 1:
379 fmt->pixelformat = V4L2_PIX_FMT_MPEG1;
380 desc = "MPEG-1 ES";
381 break;
382 case 2:
383 fmt->pixelformat = V4L2_PIX_FMT_MPEG2;
384 desc = "MPEG-2 ES";
385 break;
386 case 3:
387 fmt->pixelformat = V4L2_PIX_FMT_MPEG4;
388 desc = "MPEG-4 ES";
389 break;
390 default:
391 return -EINVAL;
392 }
393 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
394 fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
395
396 strncpy(fmt->description, desc, sizeof(fmt->description));
397
398 return 0;
399 }
400
401 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
402 struct v4l2_format *fmt)
403 {
404 struct go7007 *go = video_drvdata(file);
405
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;
414
415 return 0;
416 }
417
418 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
419 struct v4l2_format *fmt)
420 {
421 struct go7007 *go = video_drvdata(file);
422
423 return set_capture_size(go, fmt, 1);
424 }
425
426 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
427 struct v4l2_format *fmt)
428 {
429 struct go7007 *go = video_drvdata(file);
430
431 if (vb2_is_busy(&go->vidq))
432 return -EBUSY;
433
434 return set_capture_size(go, fmt, 0);
435 }
436
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[])
440 {
441 sizes[0] = GO7007_BUF_SIZE;
442 *num_planes = 1;
443
444 if (*num_buffers < 2)
445 *num_buffers = 2;
446
447 return 0;
448 }
449
450 static void go7007_buf_queue(struct vb2_buffer *vb)
451 {
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);
456 unsigned long flags;
457
458 spin_lock_irqsave(&go->spinlock, flags);
459 list_add_tail(&go7007_vb->list, &go->vidq_active);
460 spin_unlock_irqrestore(&go->spinlock, flags);
461 }
462
463 static int go7007_buf_prepare(struct vb2_buffer *vb)
464 {
465 struct go7007_buffer *go7007_vb =
466 container_of(vb, struct go7007_buffer, vb);
467
468 go7007_vb->modet_active = 0;
469 go7007_vb->frame_offset = 0;
470 vb->v4l2_planes[0].bytesused = 0;
471 return 0;
472 }
473
474 static int go7007_buf_finish(struct vb2_buffer *vb)
475 {
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;
482
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;
487 return 0;
488 }
489
490 static int go7007_start_streaming(struct vb2_queue *q, unsigned int count)
491 {
492 struct go7007 *go = vb2_get_drv_priv(q);
493 int ret;
494
495 set_formatting(go);
496 mutex_lock(&go->hw_lock);
497 go->next_seq = 0;
498 go->active_buf = NULL;
499 q->streaming = 1;
500 if (go7007_start_encoder(go) < 0)
501 ret = -EIO;
502 else
503 ret = 0;
504 mutex_unlock(&go->hw_lock);
505 if (ret) {
506 q->streaming = 0;
507 return ret;
508 }
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);
514 return ret;
515 }
516
517 static int go7007_stop_streaming(struct vb2_queue *q)
518 {
519 struct go7007 *go = vb2_get_drv_priv(q);
520 unsigned long flags;
521
522 q->streaming = 0;
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);
528
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);
536 return 0;
537 }
538
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,
548 };
549
550 static int vidioc_g_parm(struct file *filp, void *priv,
551 struct v4l2_streamparm *parm)
552 {
553 struct go7007 *go = video_drvdata(filp);
554 struct v4l2_fract timeperframe = {
555 .numerator = 1001 * go->fps_scale,
556 .denominator = go->sensor_framerate,
557 };
558
559 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
560 return -EINVAL;
561
562 parm->parm.capture.readbuffers = 2;
563 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
564 parm->parm.capture.timeperframe = timeperframe;
565
566 return 0;
567 }
568
569 static int vidioc_s_parm(struct file *filp, void *priv,
570 struct v4l2_streamparm *parm)
571 {
572 struct go7007 *go = video_drvdata(filp);
573 unsigned int n, d;
574
575 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
576 return -EINVAL;
577
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;
583 else
584 go->fps_scale = 1;
585
586 return vidioc_g_parm(filp, priv, parm);
587 }
588
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.
593
594 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
595 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
596
597 The two functions below implement the newer ioctls
598 */
599 static int vidioc_enum_framesizes(struct file *filp, void *priv,
600 struct v4l2_frmsizeenum *fsize)
601 {
602 struct go7007 *go = video_drvdata(filp);
603 int width, height;
604
605 if (fsize->index > 2)
606 return -EINVAL;
607
608 if (!valid_pixelformat(fsize->pixel_format))
609 return -EINVAL;
610
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;
615 return 0;
616 }
617
618 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
619 struct v4l2_frmivalenum *fival)
620 {
621 struct go7007 *go = video_drvdata(filp);
622 int width, height;
623 int i;
624
625 if (fival->index > 4)
626 return -EINVAL;
627
628 if (!valid_pixelformat(fival->pixel_format))
629 return -EINVAL;
630
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))
636 break;
637 if (i > 2)
638 return -EINVAL;
639 }
640 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
641 fival->discrete.numerator = 1001 * (fival->index + 1);
642 fival->discrete.denominator = go->sensor_framerate;
643 return 0;
644 }
645
646 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
647 {
648 struct go7007 *go = video_drvdata(file);
649
650 *std = go->std;
651 return 0;
652 }
653
654 static int go7007_s_std(struct go7007 *go)
655 {
656 if (go->std & V4L2_STD_625_50) {
657 go->standard = GO7007_STD_PAL;
658 go->sensor_framerate = 25025;
659 } else {
660 go->standard = GO7007_STD_NTSC;
661 go->sensor_framerate = 30000;
662 }
663
664 call_all(&go->v4l2_dev, core, s_std, go->std);
665 set_capture_size(go, NULL, 0);
666 return 0;
667 }
668
669 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
670 {
671 struct go7007 *go = video_drvdata(file);
672
673 if (vb2_is_busy(&go->vidq))
674 return -EBUSY;
675
676 go->std = std;
677
678 return go7007_s_std(go);
679 }
680
681 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
682 {
683 struct go7007 *go = video_drvdata(file);
684
685 return call_all(&go->v4l2_dev, video, querystd, std);
686 }
687
688 static int vidioc_enum_input(struct file *file, void *priv,
689 struct v4l2_input *inp)
690 {
691 struct go7007 *go = video_drvdata(file);
692
693 if (inp->index >= go->board_info->num_inputs)
694 return -EINVAL;
695
696 strncpy(inp->name, go->board_info->inputs[inp->index].name,
697 sizeof(inp->name));
698
699 /* If this board has a tuner, it will be the first input */
700 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
701 inp->index == 0)
702 inp->type = V4L2_INPUT_TYPE_TUNER;
703 else
704 inp->type = V4L2_INPUT_TYPE_CAMERA;
705
706 if (go->board_info->num_aud_inputs)
707 inp->audioset = (1 << go->board_info->num_aud_inputs) - 1;
708 else
709 inp->audioset = 0;
710 inp->tuner = 0;
711 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
712 inp->std = video_devdata(file)->tvnorms;
713 else
714 inp->std = 0;
715
716 return 0;
717 }
718
719
720 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
721 {
722 struct go7007 *go = video_drvdata(file);
723
724 *input = go->input;
725
726 return 0;
727 }
728
729 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
730 {
731 struct go7007 *go = video_drvdata(file);
732
733 if (a->index >= go->board_info->num_aud_inputs)
734 return -EINVAL;
735 strlcpy(a->name, go->board_info->aud_inputs[a->index].name, sizeof(a->name));
736 a->capability = V4L2_AUDCAP_STEREO;
737 return 0;
738 }
739
740 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
741 {
742 struct go7007 *go = video_drvdata(file);
743
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;
747 return 0;
748 }
749
750 static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
751 {
752 struct go7007 *go = video_drvdata(file);
753
754 if (a->index >= go->board_info->num_aud_inputs)
755 return -EINVAL;
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);
759 return 0;
760 }
761
762 static void go7007_s_input(struct go7007 *go)
763 {
764 unsigned int input = go->input;
765
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;
771
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;
775 }
776 }
777
778 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
779 {
780 struct go7007 *go = video_drvdata(file);
781
782 if (input >= go->board_info->num_inputs)
783 return -EINVAL;
784 if (vb2_is_busy(&go->vidq))
785 return -EBUSY;
786
787 go->input = input;
788 go7007_s_input(go);
789
790 return 0;
791 }
792
793 static int vidioc_g_tuner(struct file *file, void *priv,
794 struct v4l2_tuner *t)
795 {
796 struct go7007 *go = video_drvdata(file);
797
798 if (t->index != 0)
799 return -EINVAL;
800
801 strlcpy(t->name, "Tuner", sizeof(t->name));
802 return call_all(&go->v4l2_dev, tuner, g_tuner, t);
803 }
804
805 static int vidioc_s_tuner(struct file *file, void *priv,
806 const struct v4l2_tuner *t)
807 {
808 struct go7007 *go = video_drvdata(file);
809
810 if (t->index != 0)
811 return -EINVAL;
812
813 return call_all(&go->v4l2_dev, tuner, s_tuner, t);
814 }
815
816 static int vidioc_g_frequency(struct file *file, void *priv,
817 struct v4l2_frequency *f)
818 {
819 struct go7007 *go = video_drvdata(file);
820
821 if (f->tuner)
822 return -EINVAL;
823
824 return call_all(&go->v4l2_dev, tuner, g_frequency, f);
825 }
826
827 static int vidioc_s_frequency(struct file *file, void *priv,
828 const struct v4l2_frequency *f)
829 {
830 struct go7007 *go = video_drvdata(file);
831
832 if (f->tuner)
833 return -EINVAL;
834
835 return call_all(&go->v4l2_dev, tuner, s_frequency, f);
836 }
837
838 static int vidioc_log_status(struct file *file, void *priv)
839 {
840 struct go7007 *go = video_drvdata(file);
841
842 v4l2_ctrl_log_status(file, priv);
843 return call_all(&go->v4l2_dev, core, log_status);
844 }
845
846 /* FIXME:
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()
851 */
852
853 #if 0
854 case GO7007IOC_S_MD_PARAMS:
855 {
856 struct go7007_md_params *mdp = arg;
857
858 if (mdp->region > 3)
859 return -EINVAL;
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 =
866 mdp->trigger >> 1;
867 go->modet[mdp->region].enable = 1;
868 } else
869 go->modet[mdp->region].enable = 0;
870 /* fall-through */
871 }
872 case GO7007IOC_S_MD_REGION:
873 {
874 struct go7007_md_region *region = arg;
875
876 if (region->region < 1 || region->region > 3)
877 return -EINVAL;
878 return clip_to_modet_map(go, region->region, region->clips);
879 }
880 #endif
881
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,
890 };
891
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,
924 };
925
926 static struct video_device go7007_template = {
927 .name = "go7007",
928 .fops = &go7007_fops,
929 .release = video_device_release_empty,
930 .ioctl_ops = &video_ioctl_ops,
931 .tvnorms = V4L2_STD_ALL,
932 };
933
934 int go7007_v4l2_ctrl_init(struct go7007 *go)
935 {
936 struct v4l2_ctrl_handler *hdl = &go->hdl;
937 struct v4l2_ctrl *ctrl;
938
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);
949
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);
958 if (ctrl)
959 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
960 if (hdl->error) {
961 int rv = hdl->error;
962
963 v4l2_err(&go->v4l2_dev, "Could not register controls\n");
964 return rv;
965 }
966 go->v4l2_dev.ctrl_handler = hdl;
967 return 0;
968 }
969
970 int go7007_v4l2_init(struct go7007 *go)
971 {
972 struct video_device *vdev = &go->vdev;
973 int rv;
974
975 mutex_init(&go->serialize_lock);
976 mutex_init(&go->queue_lock);
977
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);
988 if (rv)
989 return rv;
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);
1003 } else {
1004 struct v4l2_frequency f = {
1005 .type = V4L2_TUNER_ANALOG_TV,
1006 .frequency = 980,
1007 };
1008
1009 call_all(&go->v4l2_dev, tuner, s_frequency, &f);
1010 }
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);
1014 vdev->tvnorms = 0;
1015 }
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);
1022 }
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);
1029 go7007_s_input(go);
1030 if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1031 go7007_s_std(go);
1032 rv = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1033 if (rv < 0)
1034 return rv;
1035 dev_info(go->dev, "registered device %s [v4l2]\n",
1036 video_device_node_name(vdev));
1037
1038 return 0;
1039 }
1040
1041 void go7007_v4l2_remove(struct go7007 *go)
1042 {
1043 v4l2_ctrl_handler_free(&go->hdl);
1044 }
This page took 0.050488 seconds and 4 git commands to generate.