Linux-libre 5.3-gnu
[librecmc/linux-libre.git] / drivers / media / usb / go7007 / go7007-v4l2.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2005-2006 Micronas USA Inc.
4  */
5
6 #include <linux/module.h>
7 #include <linux/delay.h>
8 #include <linux/sched.h>
9 #include <linux/spinlock.h>
10 #include <linux/slab.h>
11 #include <linux/fs.h>
12 #include <linux/unistd.h>
13 #include <linux/time.h>
14 #include <linux/vmalloc.h>
15 #include <linux/pagemap.h>
16 #include <linux/i2c.h>
17 #include <linux/mutex.h>
18 #include <linux/uaccess.h>
19 #include <linux/videodev2.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-ioctl.h>
22 #include <media/v4l2-subdev.h>
23 #include <media/v4l2-event.h>
24 #include <media/videobuf2-vmalloc.h>
25 #include <media/i2c/saa7115.h>
26
27 #include "go7007-priv.h"
28
29 #define call_all(dev, o, f, args...) \
30         v4l2_device_call_until_err(dev, 0, o, f, ##args)
31
32 static bool valid_pixelformat(u32 pixelformat)
33 {
34         switch (pixelformat) {
35         case V4L2_PIX_FMT_MJPEG:
36         case V4L2_PIX_FMT_MPEG1:
37         case V4L2_PIX_FMT_MPEG2:
38         case V4L2_PIX_FMT_MPEG4:
39                 return true;
40         default:
41                 return false;
42         }
43 }
44
45 static u32 get_frame_type_flag(struct go7007_buffer *vb, int format)
46 {
47         u8 *ptr = vb2_plane_vaddr(&vb->vb.vb2_buf, 0);
48
49         switch (format) {
50         case V4L2_PIX_FMT_MJPEG:
51                 return V4L2_BUF_FLAG_KEYFRAME;
52         case V4L2_PIX_FMT_MPEG4:
53                 switch ((ptr[vb->frame_offset + 4] >> 6) & 0x3) {
54                 case 0:
55                         return V4L2_BUF_FLAG_KEYFRAME;
56                 case 1:
57                         return V4L2_BUF_FLAG_PFRAME;
58                 case 2:
59                         return V4L2_BUF_FLAG_BFRAME;
60                 default:
61                         return 0;
62                 }
63         case V4L2_PIX_FMT_MPEG1:
64         case V4L2_PIX_FMT_MPEG2:
65                 switch ((ptr[vb->frame_offset + 5] >> 3) & 0x7) {
66                 case 1:
67                         return V4L2_BUF_FLAG_KEYFRAME;
68                 case 2:
69                         return V4L2_BUF_FLAG_PFRAME;
70                 case 3:
71                         return V4L2_BUF_FLAG_BFRAME;
72                 default:
73                         return 0;
74                 }
75         }
76
77         return 0;
78 }
79
80 static void get_resolution(struct go7007 *go, int *width, int *height)
81 {
82         switch (go->standard) {
83         case GO7007_STD_NTSC:
84                 *width = 720;
85                 *height = 480;
86                 break;
87         case GO7007_STD_PAL:
88                 *width = 720;
89                 *height = 576;
90                 break;
91         case GO7007_STD_OTHER:
92         default:
93                 *width = go->board_info->sensor_width;
94                 *height = go->board_info->sensor_height;
95                 break;
96         }
97 }
98
99 static void set_formatting(struct go7007 *go)
100 {
101         if (go->format == V4L2_PIX_FMT_MJPEG) {
102                 go->pali = 0;
103                 go->aspect_ratio = GO7007_RATIO_1_1;
104                 go->gop_size = 0;
105                 go->ipb = 0;
106                 go->closed_gop = 0;
107                 go->repeat_seqhead = 0;
108                 go->seq_header_enable = 0;
109                 go->gop_header_enable = 0;
110                 go->dvd_mode = 0;
111                 return;
112         }
113
114         switch (go->format) {
115         case V4L2_PIX_FMT_MPEG1:
116                 go->pali = 0;
117                 break;
118         default:
119         case V4L2_PIX_FMT_MPEG2:
120                 go->pali = 0x48;
121                 break;
122         case V4L2_PIX_FMT_MPEG4:
123                 /* For future reference: this is the list of MPEG4
124                  * profiles that are available, although they are
125                  * untested:
126                  *
127                  * Profile              pali
128                  * --------------       ----
129                  * PROFILE_S_L0         0x08
130                  * PROFILE_S_L1         0x01
131                  * PROFILE_S_L2         0x02
132                  * PROFILE_S_L3         0x03
133                  * PROFILE_ARTS_L1      0x91
134                  * PROFILE_ARTS_L2      0x92
135                  * PROFILE_ARTS_L3      0x93
136                  * PROFILE_ARTS_L4      0x94
137                  * PROFILE_AS_L0        0xf0
138                  * PROFILE_AS_L1        0xf1
139                  * PROFILE_AS_L2        0xf2
140                  * PROFILE_AS_L3        0xf3
141                  * PROFILE_AS_L4        0xf4
142                  * PROFILE_AS_L5        0xf5
143                  */
144                 go->pali = 0xf5;
145                 break;
146         }
147         go->gop_size = v4l2_ctrl_g_ctrl(go->mpeg_video_gop_size);
148         go->closed_gop = v4l2_ctrl_g_ctrl(go->mpeg_video_gop_closure);
149         go->ipb = v4l2_ctrl_g_ctrl(go->mpeg_video_b_frames) != 0;
150         go->bitrate = v4l2_ctrl_g_ctrl(go->mpeg_video_bitrate);
151         go->repeat_seqhead = v4l2_ctrl_g_ctrl(go->mpeg_video_rep_seqheader);
152         go->gop_header_enable = 1;
153         go->dvd_mode = 0;
154         if (go->format == V4L2_PIX_FMT_MPEG2)
155                 go->dvd_mode =
156                         go->bitrate == 9800000 &&
157                         go->gop_size == 15 &&
158                         go->ipb == 0 &&
159                         go->repeat_seqhead == 1 &&
160                         go->closed_gop;
161
162         switch (v4l2_ctrl_g_ctrl(go->mpeg_video_aspect_ratio)) {
163         default:
164         case V4L2_MPEG_VIDEO_ASPECT_1x1:
165                 go->aspect_ratio = GO7007_RATIO_1_1;
166                 break;
167         case V4L2_MPEG_VIDEO_ASPECT_4x3:
168                 go->aspect_ratio = GO7007_RATIO_4_3;
169                 break;
170         case V4L2_MPEG_VIDEO_ASPECT_16x9:
171                 go->aspect_ratio = GO7007_RATIO_16_9;
172                 break;
173         }
174 }
175
176 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
177 {
178         int sensor_height = 0, sensor_width = 0;
179         int width, height;
180
181         if (fmt != NULL && !valid_pixelformat(fmt->fmt.pix.pixelformat))
182                 return -EINVAL;
183
184         get_resolution(go, &sensor_width, &sensor_height);
185
186         if (fmt == NULL) {
187                 width = sensor_width;
188                 height = sensor_height;
189         } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
190                 if (fmt->fmt.pix.width > sensor_width)
191                         width = sensor_width;
192                 else if (fmt->fmt.pix.width < 144)
193                         width = 144;
194                 else
195                         width = fmt->fmt.pix.width & ~0x0f;
196
197                 if (fmt->fmt.pix.height > sensor_height)
198                         height = sensor_height;
199                 else if (fmt->fmt.pix.height < 96)
200                         height = 96;
201                 else
202                         height = fmt->fmt.pix.height & ~0x0f;
203         } else {
204                 width = fmt->fmt.pix.width;
205
206                 if (width <= sensor_width / 4) {
207                         width = sensor_width / 4;
208                         height = sensor_height / 4;
209                 } else if (width <= sensor_width / 2) {
210                         width = sensor_width / 2;
211                         height = sensor_height / 2;
212                 } else {
213                         width = sensor_width;
214                         height = sensor_height;
215                 }
216                 width &= ~0xf;
217                 height &= ~0xf;
218         }
219
220         if (fmt != NULL) {
221                 u32 pixelformat = fmt->fmt.pix.pixelformat;
222
223                 memset(fmt, 0, sizeof(*fmt));
224                 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
225                 fmt->fmt.pix.width = width;
226                 fmt->fmt.pix.height = height;
227                 fmt->fmt.pix.pixelformat = pixelformat;
228                 fmt->fmt.pix.field = V4L2_FIELD_NONE;
229                 fmt->fmt.pix.bytesperline = 0;
230                 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
231                 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
232         }
233
234         if (try)
235                 return 0;
236
237         if (fmt)
238                 go->format = fmt->fmt.pix.pixelformat;
239         go->width = width;
240         go->height = height;
241         go->encoder_h_offset = go->board_info->sensor_h_offset;
242         go->encoder_v_offset = go->board_info->sensor_v_offset;
243
244         if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
245                 struct v4l2_subdev_format format = {
246                         .which = V4L2_SUBDEV_FORMAT_ACTIVE,
247                 };
248
249                 format.format.code = MEDIA_BUS_FMT_FIXED;
250                 format.format.width = fmt ? fmt->fmt.pix.width : width;
251                 format.format.height = height;
252                 go->encoder_h_halve = 0;
253                 go->encoder_v_halve = 0;
254                 go->encoder_subsample = 0;
255                 call_all(&go->v4l2_dev, pad, set_fmt, NULL, &format);
256         } else {
257                 if (width <= sensor_width / 4) {
258                         go->encoder_h_halve = 1;
259                         go->encoder_v_halve = 1;
260                         go->encoder_subsample = 1;
261                 } else if (width <= sensor_width / 2) {
262                         go->encoder_h_halve = 1;
263                         go->encoder_v_halve = 1;
264                         go->encoder_subsample = 0;
265                 } else {
266                         go->encoder_h_halve = 0;
267                         go->encoder_v_halve = 0;
268                         go->encoder_subsample = 0;
269                 }
270         }
271         return 0;
272 }
273
274 static int vidioc_querycap(struct file *file, void  *priv,
275                                         struct v4l2_capability *cap)
276 {
277         struct go7007 *go = video_drvdata(file);
278
279         strscpy(cap->driver, "go7007", sizeof(cap->driver));
280         strscpy(cap->card, go->name, sizeof(cap->card));
281         strscpy(cap->bus_info, go->bus_info, sizeof(cap->bus_info));
282         return 0;
283 }
284
285 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
286                                         struct v4l2_fmtdesc *fmt)
287 {
288         char *desc = NULL;
289
290         switch (fmt->index) {
291         case 0:
292                 fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
293                 desc = "Motion JPEG";
294                 break;
295         case 1:
296                 fmt->pixelformat = V4L2_PIX_FMT_MPEG1;
297                 desc = "MPEG-1 ES";
298                 break;
299         case 2:
300                 fmt->pixelformat = V4L2_PIX_FMT_MPEG2;
301                 desc = "MPEG-2 ES";
302                 break;
303         case 3:
304                 fmt->pixelformat = V4L2_PIX_FMT_MPEG4;
305                 desc = "MPEG-4 ES";
306                 break;
307         default:
308                 return -EINVAL;
309         }
310         fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
311         fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
312
313         strscpy(fmt->description, desc, sizeof(fmt->description));
314
315         return 0;
316 }
317
318 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
319                                         struct v4l2_format *fmt)
320 {
321         struct go7007 *go = video_drvdata(file);
322
323         fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
324         fmt->fmt.pix.width = go->width;
325         fmt->fmt.pix.height = go->height;
326         fmt->fmt.pix.pixelformat = go->format;
327         fmt->fmt.pix.field = V4L2_FIELD_NONE;
328         fmt->fmt.pix.bytesperline = 0;
329         fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
330         fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
331
332         return 0;
333 }
334
335 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
336                         struct v4l2_format *fmt)
337 {
338         struct go7007 *go = video_drvdata(file);
339
340         return set_capture_size(go, fmt, 1);
341 }
342
343 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
344                         struct v4l2_format *fmt)
345 {
346         struct go7007 *go = video_drvdata(file);
347
348         if (vb2_is_busy(&go->vidq))
349                 return -EBUSY;
350
351         return set_capture_size(go, fmt, 0);
352 }
353
354 static int go7007_queue_setup(struct vb2_queue *q,
355                 unsigned int *num_buffers, unsigned int *num_planes,
356                 unsigned int sizes[], struct device *alloc_devs[])
357 {
358         sizes[0] = GO7007_BUF_SIZE;
359         *num_planes = 1;
360
361         if (*num_buffers < 2)
362                 *num_buffers = 2;
363
364         return 0;
365 }
366
367 static void go7007_buf_queue(struct vb2_buffer *vb)
368 {
369         struct vb2_queue *vq = vb->vb2_queue;
370         struct go7007 *go = vb2_get_drv_priv(vq);
371         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
372         struct go7007_buffer *go7007_vb =
373                 container_of(vbuf, struct go7007_buffer, vb);
374         unsigned long flags;
375
376         spin_lock_irqsave(&go->spinlock, flags);
377         list_add_tail(&go7007_vb->list, &go->vidq_active);
378         spin_unlock_irqrestore(&go->spinlock, flags);
379 }
380
381 static int go7007_buf_prepare(struct vb2_buffer *vb)
382 {
383         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
384         struct go7007_buffer *go7007_vb =
385                 container_of(vbuf, struct go7007_buffer, vb);
386
387         go7007_vb->modet_active = 0;
388         go7007_vb->frame_offset = 0;
389         vb->planes[0].bytesused = 0;
390         return 0;
391 }
392
393 static void go7007_buf_finish(struct vb2_buffer *vb)
394 {
395         struct vb2_queue *vq = vb->vb2_queue;
396         struct go7007 *go = vb2_get_drv_priv(vq);
397         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
398         struct go7007_buffer *go7007_vb =
399                 container_of(vbuf, struct go7007_buffer, vb);
400         u32 frame_type_flag = get_frame_type_flag(go7007_vb, go->format);
401
402         vbuf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_BFRAME |
403                         V4L2_BUF_FLAG_PFRAME);
404         vbuf->flags |= frame_type_flag;
405         vbuf->field = V4L2_FIELD_NONE;
406 }
407
408 static int go7007_start_streaming(struct vb2_queue *q, unsigned int count)
409 {
410         struct go7007 *go = vb2_get_drv_priv(q);
411         int ret;
412
413         set_formatting(go);
414         mutex_lock(&go->hw_lock);
415         go->next_seq = 0;
416         go->active_buf = NULL;
417         go->modet_event_status = 0;
418         q->streaming = 1;
419         if (go7007_start_encoder(go) < 0)
420                 ret = -EIO;
421         else
422                 ret = 0;
423         mutex_unlock(&go->hw_lock);
424         if (ret) {
425                 q->streaming = 0;
426                 return ret;
427         }
428         call_all(&go->v4l2_dev, video, s_stream, 1);
429         v4l2_ctrl_grab(go->mpeg_video_gop_size, true);
430         v4l2_ctrl_grab(go->mpeg_video_gop_closure, true);
431         v4l2_ctrl_grab(go->mpeg_video_bitrate, true);
432         v4l2_ctrl_grab(go->mpeg_video_aspect_ratio, true);
433         /* Turn on Capture LED */
434         if (go->board_id == GO7007_BOARDID_ADS_USBAV_709)
435                 go7007_write_addr(go, 0x3c82, 0x0005);
436         return ret;
437 }
438
439 static void go7007_stop_streaming(struct vb2_queue *q)
440 {
441         struct go7007 *go = vb2_get_drv_priv(q);
442         unsigned long flags;
443
444         q->streaming = 0;
445         go7007_stream_stop(go);
446         mutex_lock(&go->hw_lock);
447         go7007_reset_encoder(go);
448         mutex_unlock(&go->hw_lock);
449         call_all(&go->v4l2_dev, video, s_stream, 0);
450
451         spin_lock_irqsave(&go->spinlock, flags);
452         INIT_LIST_HEAD(&go->vidq_active);
453         spin_unlock_irqrestore(&go->spinlock, flags);
454         v4l2_ctrl_grab(go->mpeg_video_gop_size, false);
455         v4l2_ctrl_grab(go->mpeg_video_gop_closure, false);
456         v4l2_ctrl_grab(go->mpeg_video_bitrate, false);
457         v4l2_ctrl_grab(go->mpeg_video_aspect_ratio, false);
458         /* Turn on Capture LED */
459         if (go->board_id == GO7007_BOARDID_ADS_USBAV_709)
460                 go7007_write_addr(go, 0x3c82, 0x000d);
461 }
462
463 static const struct vb2_ops go7007_video_qops = {
464         .queue_setup    = go7007_queue_setup,
465         .buf_queue      = go7007_buf_queue,
466         .buf_prepare    = go7007_buf_prepare,
467         .buf_finish     = go7007_buf_finish,
468         .start_streaming = go7007_start_streaming,
469         .stop_streaming = go7007_stop_streaming,
470         .wait_prepare   = vb2_ops_wait_prepare,
471         .wait_finish    = vb2_ops_wait_finish,
472 };
473
474 static int vidioc_g_parm(struct file *filp, void *priv,
475                 struct v4l2_streamparm *parm)
476 {
477         struct go7007 *go = video_drvdata(filp);
478         struct v4l2_fract timeperframe = {
479                 .numerator = 1001 *  go->fps_scale,
480                 .denominator = go->sensor_framerate,
481         };
482
483         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
484                 return -EINVAL;
485
486         parm->parm.capture.readbuffers = 2;
487         parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
488         parm->parm.capture.timeperframe = timeperframe;
489
490         return 0;
491 }
492
493 static int vidioc_s_parm(struct file *filp, void *priv,
494                 struct v4l2_streamparm *parm)
495 {
496         struct go7007 *go = video_drvdata(filp);
497         unsigned int n, d;
498
499         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
500                 return -EINVAL;
501
502         n = go->sensor_framerate *
503                 parm->parm.capture.timeperframe.numerator;
504         d = 1001 * parm->parm.capture.timeperframe.denominator;
505         if (n != 0 && d != 0 && n > d)
506                 go->fps_scale = (n + d/2) / d;
507         else
508                 go->fps_scale = 1;
509
510         return vidioc_g_parm(filp, priv, parm);
511 }
512
513 /* VIDIOC_ENUMSTD on go7007 were used for enumerating the supported fps and
514    its resolution, when the device is not connected to TV.
515    This is were an API abuse, probably used by the lack of specific IOCTL's to
516    enumerate it, by the time the driver was written.
517
518    However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
519    and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
520
521    The two functions below implement the newer ioctls
522 */
523 static int vidioc_enum_framesizes(struct file *filp, void *priv,
524                                   struct v4l2_frmsizeenum *fsize)
525 {
526         struct go7007 *go = video_drvdata(filp);
527         int width, height;
528
529         if (fsize->index > 2)
530                 return -EINVAL;
531
532         if (!valid_pixelformat(fsize->pixel_format))
533                 return -EINVAL;
534
535         get_resolution(go, &width, &height);
536         fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
537         fsize->discrete.width = (width >> fsize->index) & ~0xf;
538         fsize->discrete.height = (height >> fsize->index) & ~0xf;
539         return 0;
540 }
541
542 static int vidioc_enum_frameintervals(struct file *filp, void *priv,
543                                       struct v4l2_frmivalenum *fival)
544 {
545         struct go7007 *go = video_drvdata(filp);
546         int width, height;
547         int i;
548
549         if (fival->index > 4)
550                 return -EINVAL;
551
552         if (!valid_pixelformat(fival->pixel_format))
553                 return -EINVAL;
554
555         if (!(go->board_info->sensor_flags & GO7007_SENSOR_SCALING)) {
556                 get_resolution(go, &width, &height);
557                 for (i = 0; i <= 2; i++)
558                         if (fival->width == ((width >> i) & ~0xf) &&
559                             fival->height == ((height >> i) & ~0xf))
560                                 break;
561                 if (i > 2)
562                         return -EINVAL;
563         }
564         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
565         fival->discrete.numerator = 1001 * (fival->index + 1);
566         fival->discrete.denominator = go->sensor_framerate;
567         return 0;
568 }
569
570 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
571 {
572         struct go7007 *go = video_drvdata(file);
573
574         *std = go->std;
575         return 0;
576 }
577
578 static int go7007_s_std(struct go7007 *go)
579 {
580         if (go->std & V4L2_STD_625_50) {
581                 go->standard = GO7007_STD_PAL;
582                 go->sensor_framerate = 25025;
583         } else {
584                 go->standard = GO7007_STD_NTSC;
585                 go->sensor_framerate = 30000;
586         }
587
588         call_all(&go->v4l2_dev, video, s_std, go->std);
589         set_capture_size(go, NULL, 0);
590         return 0;
591 }
592
593 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std)
594 {
595         struct go7007 *go = video_drvdata(file);
596
597         if (vb2_is_busy(&go->vidq))
598                 return -EBUSY;
599
600         go->std = std;
601
602         return go7007_s_std(go);
603 }
604
605 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
606 {
607         struct go7007 *go = video_drvdata(file);
608
609         return call_all(&go->v4l2_dev, video, querystd, std);
610 }
611
612 static int vidioc_enum_input(struct file *file, void *priv,
613                                 struct v4l2_input *inp)
614 {
615         struct go7007 *go = video_drvdata(file);
616
617         if (inp->index >= go->board_info->num_inputs)
618                 return -EINVAL;
619
620         strscpy(inp->name, go->board_info->inputs[inp->index].name,
621                 sizeof(inp->name));
622
623         /* If this board has a tuner, it will be the first input */
624         if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
625                         inp->index == 0)
626                 inp->type = V4L2_INPUT_TYPE_TUNER;
627         else
628                 inp->type = V4L2_INPUT_TYPE_CAMERA;
629
630         if (go->board_info->num_aud_inputs)
631                 inp->audioset = (1 << go->board_info->num_aud_inputs) - 1;
632         else
633                 inp->audioset = 0;
634         inp->tuner = 0;
635         if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
636                 inp->std = video_devdata(file)->tvnorms;
637         else
638                 inp->std = 0;
639
640         return 0;
641 }
642
643
644 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
645 {
646         struct go7007 *go = video_drvdata(file);
647
648         *input = go->input;
649
650         return 0;
651 }
652
653 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
654 {
655         struct go7007 *go = video_drvdata(file);
656
657         if (a->index >= go->board_info->num_aud_inputs)
658                 return -EINVAL;
659         strscpy(a->name, go->board_info->aud_inputs[a->index].name,
660                 sizeof(a->name));
661         a->capability = V4L2_AUDCAP_STEREO;
662         return 0;
663 }
664
665 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
666 {
667         struct go7007 *go = video_drvdata(file);
668
669         a->index = go->aud_input;
670         strscpy(a->name, go->board_info->aud_inputs[go->aud_input].name,
671                 sizeof(a->name));
672         a->capability = V4L2_AUDCAP_STEREO;
673         return 0;
674 }
675
676 static int vidioc_s_audio(struct file *file, void *fh,
677         const struct v4l2_audio *a)
678 {
679         struct go7007 *go = video_drvdata(file);
680
681         if (a->index >= go->board_info->num_aud_inputs)
682                 return -EINVAL;
683         go->aud_input = a->index;
684         v4l2_subdev_call(go->sd_audio, audio, s_routing,
685                 go->board_info->aud_inputs[go->aud_input].audio_input, 0, 0);
686         return 0;
687 }
688
689 static void go7007_s_input(struct go7007 *go)
690 {
691         unsigned int input = go->input;
692
693         v4l2_subdev_call(go->sd_video, video, s_routing,
694                         go->board_info->inputs[input].video_input, 0,
695                         go->board_info->video_config);
696         if (go->board_info->num_aud_inputs) {
697                 int aud_input = go->board_info->inputs[input].audio_index;
698
699                 v4l2_subdev_call(go->sd_audio, audio, s_routing,
700                         go->board_info->aud_inputs[aud_input].audio_input, 0, 0);
701                 go->aud_input = aud_input;
702         }
703 }
704
705 static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
706 {
707         struct go7007 *go = video_drvdata(file);
708
709         if (input >= go->board_info->num_inputs)
710                 return -EINVAL;
711         if (vb2_is_busy(&go->vidq))
712                 return -EBUSY;
713
714         go->input = input;
715         go7007_s_input(go);
716
717         return 0;
718 }
719
720 static int vidioc_g_tuner(struct file *file, void *priv,
721                                 struct v4l2_tuner *t)
722 {
723         struct go7007 *go = video_drvdata(file);
724
725         if (t->index != 0)
726                 return -EINVAL;
727
728         strscpy(t->name, "Tuner", sizeof(t->name));
729         return call_all(&go->v4l2_dev, tuner, g_tuner, t);
730 }
731
732 static int vidioc_s_tuner(struct file *file, void *priv,
733                                 const struct v4l2_tuner *t)
734 {
735         struct go7007 *go = video_drvdata(file);
736
737         if (t->index != 0)
738                 return -EINVAL;
739
740         return call_all(&go->v4l2_dev, tuner, s_tuner, t);
741 }
742
743 static int vidioc_g_frequency(struct file *file, void *priv,
744                                 struct v4l2_frequency *f)
745 {
746         struct go7007 *go = video_drvdata(file);
747
748         if (f->tuner)
749                 return -EINVAL;
750
751         return call_all(&go->v4l2_dev, tuner, g_frequency, f);
752 }
753
754 static int vidioc_s_frequency(struct file *file, void *priv,
755                                 const struct v4l2_frequency *f)
756 {
757         struct go7007 *go = video_drvdata(file);
758
759         if (f->tuner)
760                 return -EINVAL;
761
762         return call_all(&go->v4l2_dev, tuner, s_frequency, f);
763 }
764
765 static int vidioc_log_status(struct file *file, void *priv)
766 {
767         struct go7007 *go = video_drvdata(file);
768
769         v4l2_ctrl_log_status(file, priv);
770         return call_all(&go->v4l2_dev, core, log_status);
771 }
772
773 static int vidioc_subscribe_event(struct v4l2_fh *fh,
774                                 const struct v4l2_event_subscription *sub)
775 {
776
777         switch (sub->type) {
778         case V4L2_EVENT_MOTION_DET:
779                 /* Allow for up to 30 events (1 second for NTSC) to be
780                  * stored. */
781                 return v4l2_event_subscribe(fh, sub, 30, NULL);
782         default:
783                 return v4l2_ctrl_subscribe_event(fh, sub);
784         }
785 }
786
787
788 static int go7007_s_ctrl(struct v4l2_ctrl *ctrl)
789 {
790         struct go7007 *go =
791                 container_of(ctrl->handler, struct go7007, hdl);
792         unsigned y;
793         u8 *mt;
794
795         switch (ctrl->id) {
796         case V4L2_CID_PIXEL_THRESHOLD0:
797                 go->modet[0].pixel_threshold = ctrl->val;
798                 break;
799         case V4L2_CID_MOTION_THRESHOLD0:
800                 go->modet[0].motion_threshold = ctrl->val;
801                 break;
802         case V4L2_CID_MB_THRESHOLD0:
803                 go->modet[0].mb_threshold = ctrl->val;
804                 break;
805         case V4L2_CID_PIXEL_THRESHOLD1:
806                 go->modet[1].pixel_threshold = ctrl->val;
807                 break;
808         case V4L2_CID_MOTION_THRESHOLD1:
809                 go->modet[1].motion_threshold = ctrl->val;
810                 break;
811         case V4L2_CID_MB_THRESHOLD1:
812                 go->modet[1].mb_threshold = ctrl->val;
813                 break;
814         case V4L2_CID_PIXEL_THRESHOLD2:
815                 go->modet[2].pixel_threshold = ctrl->val;
816                 break;
817         case V4L2_CID_MOTION_THRESHOLD2:
818                 go->modet[2].motion_threshold = ctrl->val;
819                 break;
820         case V4L2_CID_MB_THRESHOLD2:
821                 go->modet[2].mb_threshold = ctrl->val;
822                 break;
823         case V4L2_CID_PIXEL_THRESHOLD3:
824                 go->modet[3].pixel_threshold = ctrl->val;
825                 break;
826         case V4L2_CID_MOTION_THRESHOLD3:
827                 go->modet[3].motion_threshold = ctrl->val;
828                 break;
829         case V4L2_CID_MB_THRESHOLD3:
830                 go->modet[3].mb_threshold = ctrl->val;
831                 break;
832         case V4L2_CID_DETECT_MD_REGION_GRID:
833                 mt = go->modet_map;
834                 for (y = 0; y < go->height / 16; y++, mt += go->width / 16)
835                         memcpy(mt, ctrl->p_new.p_u8 + y * (720 / 16), go->width / 16);
836                 break;
837         default:
838                 return -EINVAL;
839         }
840         return 0;
841 }
842
843 static const struct v4l2_file_operations go7007_fops = {
844         .owner          = THIS_MODULE,
845         .open           = v4l2_fh_open,
846         .release        = vb2_fop_release,
847         .unlocked_ioctl = video_ioctl2,
848         .read           = vb2_fop_read,
849         .mmap           = vb2_fop_mmap,
850         .poll           = vb2_fop_poll,
851 };
852
853 static const struct v4l2_ioctl_ops video_ioctl_ops = {
854         .vidioc_querycap          = vidioc_querycap,
855         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
856         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
857         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
858         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
859         .vidioc_reqbufs           = vb2_ioctl_reqbufs,
860         .vidioc_querybuf          = vb2_ioctl_querybuf,
861         .vidioc_qbuf              = vb2_ioctl_qbuf,
862         .vidioc_dqbuf             = vb2_ioctl_dqbuf,
863         .vidioc_g_std             = vidioc_g_std,
864         .vidioc_s_std             = vidioc_s_std,
865         .vidioc_querystd          = vidioc_querystd,
866         .vidioc_enum_input        = vidioc_enum_input,
867         .vidioc_g_input           = vidioc_g_input,
868         .vidioc_s_input           = vidioc_s_input,
869         .vidioc_enumaudio         = vidioc_enumaudio,
870         .vidioc_g_audio           = vidioc_g_audio,
871         .vidioc_s_audio           = vidioc_s_audio,
872         .vidioc_streamon          = vb2_ioctl_streamon,
873         .vidioc_streamoff         = vb2_ioctl_streamoff,
874         .vidioc_g_tuner           = vidioc_g_tuner,
875         .vidioc_s_tuner           = vidioc_s_tuner,
876         .vidioc_g_frequency       = vidioc_g_frequency,
877         .vidioc_s_frequency       = vidioc_s_frequency,
878         .vidioc_g_parm            = vidioc_g_parm,
879         .vidioc_s_parm            = vidioc_s_parm,
880         .vidioc_enum_framesizes   = vidioc_enum_framesizes,
881         .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
882         .vidioc_log_status        = vidioc_log_status,
883         .vidioc_subscribe_event   = vidioc_subscribe_event,
884         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
885 };
886
887 static const struct video_device go7007_template = {
888         .name           = "go7007",
889         .fops           = &go7007_fops,
890         .release        = video_device_release_empty,
891         .ioctl_ops      = &video_ioctl_ops,
892         .tvnorms        = V4L2_STD_ALL,
893 };
894
895 static const struct v4l2_ctrl_ops go7007_ctrl_ops = {
896         .s_ctrl = go7007_s_ctrl,
897 };
898
899 static const struct v4l2_ctrl_config go7007_pixel_threshold0_ctrl = {
900         .ops = &go7007_ctrl_ops,
901         .id = V4L2_CID_PIXEL_THRESHOLD0,
902         .name = "Pixel Threshold Region 0",
903         .type = V4L2_CTRL_TYPE_INTEGER,
904         .def = 20,
905         .max = 32767,
906         .step = 1,
907 };
908
909 static const struct v4l2_ctrl_config go7007_motion_threshold0_ctrl = {
910         .ops = &go7007_ctrl_ops,
911         .id = V4L2_CID_MOTION_THRESHOLD0,
912         .name = "Motion Threshold Region 0",
913         .type = V4L2_CTRL_TYPE_INTEGER,
914         .def = 80,
915         .max = 32767,
916         .step = 1,
917 };
918
919 static const struct v4l2_ctrl_config go7007_mb_threshold0_ctrl = {
920         .ops = &go7007_ctrl_ops,
921         .id = V4L2_CID_MB_THRESHOLD0,
922         .name = "MB Threshold Region 0",
923         .type = V4L2_CTRL_TYPE_INTEGER,
924         .def = 200,
925         .max = 32767,
926         .step = 1,
927 };
928
929 static const struct v4l2_ctrl_config go7007_pixel_threshold1_ctrl = {
930         .ops = &go7007_ctrl_ops,
931         .id = V4L2_CID_PIXEL_THRESHOLD1,
932         .name = "Pixel Threshold Region 1",
933         .type = V4L2_CTRL_TYPE_INTEGER,
934         .def = 20,
935         .max = 32767,
936         .step = 1,
937 };
938
939 static const struct v4l2_ctrl_config go7007_motion_threshold1_ctrl = {
940         .ops = &go7007_ctrl_ops,
941         .id = V4L2_CID_MOTION_THRESHOLD1,
942         .name = "Motion Threshold Region 1",
943         .type = V4L2_CTRL_TYPE_INTEGER,
944         .def = 80,
945         .max = 32767,
946         .step = 1,
947 };
948
949 static const struct v4l2_ctrl_config go7007_mb_threshold1_ctrl = {
950         .ops = &go7007_ctrl_ops,
951         .id = V4L2_CID_MB_THRESHOLD1,
952         .name = "MB Threshold Region 1",
953         .type = V4L2_CTRL_TYPE_INTEGER,
954         .def = 200,
955         .max = 32767,
956         .step = 1,
957 };
958
959 static const struct v4l2_ctrl_config go7007_pixel_threshold2_ctrl = {
960         .ops = &go7007_ctrl_ops,
961         .id = V4L2_CID_PIXEL_THRESHOLD2,
962         .name = "Pixel Threshold Region 2",
963         .type = V4L2_CTRL_TYPE_INTEGER,
964         .def = 20,
965         .max = 32767,
966         .step = 1,
967 };
968
969 static const struct v4l2_ctrl_config go7007_motion_threshold2_ctrl = {
970         .ops = &go7007_ctrl_ops,
971         .id = V4L2_CID_MOTION_THRESHOLD2,
972         .name = "Motion Threshold Region 2",
973         .type = V4L2_CTRL_TYPE_INTEGER,
974         .def = 80,
975         .max = 32767,
976         .step = 1,
977 };
978
979 static const struct v4l2_ctrl_config go7007_mb_threshold2_ctrl = {
980         .ops = &go7007_ctrl_ops,
981         .id = V4L2_CID_MB_THRESHOLD2,
982         .name = "MB Threshold Region 2",
983         .type = V4L2_CTRL_TYPE_INTEGER,
984         .def = 200,
985         .max = 32767,
986         .step = 1,
987 };
988
989 static const struct v4l2_ctrl_config go7007_pixel_threshold3_ctrl = {
990         .ops = &go7007_ctrl_ops,
991         .id = V4L2_CID_PIXEL_THRESHOLD3,
992         .name = "Pixel Threshold Region 3",
993         .type = V4L2_CTRL_TYPE_INTEGER,
994         .def = 20,
995         .max = 32767,
996         .step = 1,
997 };
998
999 static const struct v4l2_ctrl_config go7007_motion_threshold3_ctrl = {
1000         .ops = &go7007_ctrl_ops,
1001         .id = V4L2_CID_MOTION_THRESHOLD3,
1002         .name = "Motion Threshold Region 3",
1003         .type = V4L2_CTRL_TYPE_INTEGER,
1004         .def = 80,
1005         .max = 32767,
1006         .step = 1,
1007 };
1008
1009 static const struct v4l2_ctrl_config go7007_mb_threshold3_ctrl = {
1010         .ops = &go7007_ctrl_ops,
1011         .id = V4L2_CID_MB_THRESHOLD3,
1012         .name = "MB Threshold Region 3",
1013         .type = V4L2_CTRL_TYPE_INTEGER,
1014         .def = 200,
1015         .max = 32767,
1016         .step = 1,
1017 };
1018
1019 static const struct v4l2_ctrl_config go7007_mb_regions_ctrl = {
1020         .ops = &go7007_ctrl_ops,
1021         .id = V4L2_CID_DETECT_MD_REGION_GRID,
1022         .dims = { 576 / 16, 720 / 16 },
1023         .max = 3,
1024         .step = 1,
1025 };
1026
1027 int go7007_v4l2_ctrl_init(struct go7007 *go)
1028 {
1029         struct v4l2_ctrl_handler *hdl = &go->hdl;
1030         struct v4l2_ctrl *ctrl;
1031
1032         v4l2_ctrl_handler_init(hdl, 22);
1033         go->mpeg_video_gop_size = v4l2_ctrl_new_std(hdl, NULL,
1034                         V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, 34, 1, 15);
1035         go->mpeg_video_gop_closure = v4l2_ctrl_new_std(hdl, NULL,
1036                         V4L2_CID_MPEG_VIDEO_GOP_CLOSURE, 0, 1, 1, 1);
1037         go->mpeg_video_bitrate = v4l2_ctrl_new_std(hdl, NULL,
1038                         V4L2_CID_MPEG_VIDEO_BITRATE,
1039                         64000, 10000000, 1, 9800000);
1040         go->mpeg_video_b_frames = v4l2_ctrl_new_std(hdl, NULL,
1041                         V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 2, 2, 0);
1042         go->mpeg_video_rep_seqheader = v4l2_ctrl_new_std(hdl, NULL,
1043                         V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, 0, 1, 1, 1);
1044
1045         go->mpeg_video_aspect_ratio = v4l2_ctrl_new_std_menu(hdl, NULL,
1046                         V4L2_CID_MPEG_VIDEO_ASPECT,
1047                         V4L2_MPEG_VIDEO_ASPECT_16x9, 0,
1048                         V4L2_MPEG_VIDEO_ASPECT_1x1);
1049         ctrl = v4l2_ctrl_new_std(hdl, NULL,
1050                         V4L2_CID_JPEG_ACTIVE_MARKER, 0,
1051                         V4L2_JPEG_ACTIVE_MARKER_DQT |
1052                         V4L2_JPEG_ACTIVE_MARKER_DHT, 0,
1053                         V4L2_JPEG_ACTIVE_MARKER_DQT |
1054                         V4L2_JPEG_ACTIVE_MARKER_DHT);
1055         if (ctrl)
1056                 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1057         v4l2_ctrl_new_custom(hdl, &go7007_pixel_threshold0_ctrl, NULL);
1058         v4l2_ctrl_new_custom(hdl, &go7007_motion_threshold0_ctrl, NULL);
1059         v4l2_ctrl_new_custom(hdl, &go7007_mb_threshold0_ctrl, NULL);
1060         v4l2_ctrl_new_custom(hdl, &go7007_pixel_threshold1_ctrl, NULL);
1061         v4l2_ctrl_new_custom(hdl, &go7007_motion_threshold1_ctrl, NULL);
1062         v4l2_ctrl_new_custom(hdl, &go7007_mb_threshold1_ctrl, NULL);
1063         v4l2_ctrl_new_custom(hdl, &go7007_pixel_threshold2_ctrl, NULL);
1064         v4l2_ctrl_new_custom(hdl, &go7007_motion_threshold2_ctrl, NULL);
1065         v4l2_ctrl_new_custom(hdl, &go7007_mb_threshold2_ctrl, NULL);
1066         v4l2_ctrl_new_custom(hdl, &go7007_pixel_threshold3_ctrl, NULL);
1067         v4l2_ctrl_new_custom(hdl, &go7007_motion_threshold3_ctrl, NULL);
1068         v4l2_ctrl_new_custom(hdl, &go7007_mb_threshold3_ctrl, NULL);
1069         v4l2_ctrl_new_custom(hdl, &go7007_mb_regions_ctrl, NULL);
1070         go->modet_mode = v4l2_ctrl_new_std_menu(hdl, NULL,
1071                         V4L2_CID_DETECT_MD_MODE,
1072                         V4L2_DETECT_MD_MODE_REGION_GRID,
1073                         1 << V4L2_DETECT_MD_MODE_THRESHOLD_GRID,
1074                         V4L2_DETECT_MD_MODE_DISABLED);
1075         if (hdl->error) {
1076                 int rv = hdl->error;
1077
1078                 v4l2_err(&go->v4l2_dev, "Could not register controls\n");
1079                 return rv;
1080         }
1081         go->v4l2_dev.ctrl_handler = hdl;
1082         return 0;
1083 }
1084
1085 int go7007_v4l2_init(struct go7007 *go)
1086 {
1087         struct video_device *vdev = &go->vdev;
1088         int rv;
1089
1090         mutex_init(&go->serialize_lock);
1091         mutex_init(&go->queue_lock);
1092
1093         INIT_LIST_HEAD(&go->vidq_active);
1094         go->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1095         go->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1096         go->vidq.ops = &go7007_video_qops;
1097         go->vidq.mem_ops = &vb2_vmalloc_memops;
1098         go->vidq.drv_priv = go;
1099         go->vidq.buf_struct_size = sizeof(struct go7007_buffer);
1100         go->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1101         go->vidq.lock = &go->queue_lock;
1102         rv = vb2_queue_init(&go->vidq);
1103         if (rv)
1104                 return rv;
1105         *vdev = go7007_template;
1106         vdev->lock = &go->serialize_lock;
1107         vdev->queue = &go->vidq;
1108         vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1109                             V4L2_CAP_STREAMING;
1110         if (go->board_info->num_aud_inputs)
1111                 vdev->device_caps |= V4L2_CAP_AUDIO;
1112         if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
1113                 vdev->device_caps |= V4L2_CAP_TUNER;
1114         video_set_drvdata(vdev, go);
1115         vdev->v4l2_dev = &go->v4l2_dev;
1116         if (!v4l2_device_has_op(&go->v4l2_dev, 0, video, querystd))
1117                 v4l2_disable_ioctl(vdev, VIDIOC_QUERYSTD);
1118         if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER)) {
1119                 v4l2_disable_ioctl(vdev, VIDIOC_S_FREQUENCY);
1120                 v4l2_disable_ioctl(vdev, VIDIOC_G_FREQUENCY);
1121                 v4l2_disable_ioctl(vdev, VIDIOC_S_TUNER);
1122                 v4l2_disable_ioctl(vdev, VIDIOC_G_TUNER);
1123         } else {
1124                 struct v4l2_frequency f = {
1125                         .type = V4L2_TUNER_ANALOG_TV,
1126                         .frequency = 980,
1127                 };
1128
1129                 call_all(&go->v4l2_dev, tuner, s_frequency, &f);
1130         }
1131         if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV)) {
1132                 v4l2_disable_ioctl(vdev, VIDIOC_G_STD);
1133                 v4l2_disable_ioctl(vdev, VIDIOC_S_STD);
1134                 vdev->tvnorms = 0;
1135         }
1136         if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING)
1137                 v4l2_disable_ioctl(vdev, VIDIOC_ENUM_FRAMESIZES);
1138         if (go->board_info->num_aud_inputs == 0) {
1139                 v4l2_disable_ioctl(vdev, VIDIOC_G_AUDIO);
1140                 v4l2_disable_ioctl(vdev, VIDIOC_S_AUDIO);
1141                 v4l2_disable_ioctl(vdev, VIDIOC_ENUMAUDIO);
1142         }
1143         /* Setup correct crystal frequency on this board */
1144         if (go->board_info->sensor_flags & GO7007_SENSOR_SAA7115)
1145                 v4l2_subdev_call(go->sd_video, video, s_crystal_freq,
1146                                 SAA7115_FREQ_24_576_MHZ,
1147                                 SAA7115_FREQ_FL_APLL | SAA7115_FREQ_FL_UCGC |
1148                                 SAA7115_FREQ_FL_DOUBLE_ASCLK);
1149         go7007_s_input(go);
1150         if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
1151                 go7007_s_std(go);
1152         rv = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1153         if (rv < 0)
1154                 return rv;
1155         dev_info(go->dev, "registered device %s [v4l2]\n",
1156                  video_device_node_name(vdev));
1157
1158         return 0;
1159 }
1160
1161 void go7007_v4l2_remove(struct go7007 *go)
1162 {
1163         v4l2_ctrl_handler_free(&go->hdl);
1164 }