media: media/pci: set device_caps in struct video_device
[sfrench/cifs-2.6.git] / drivers / media / pci / cx25821 / cx25821-video.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Driver for the Conexant CX25821 PCIe bridge
4  *
5  *  Copyright (C) 2009 Conexant Systems Inc.
6  *  Authors  <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
7  *  Based on Steven Toth <stoth@linuxtv.org> cx25821 driver
8  *  Parts adapted/taken from Eduardo Moscoso Rubino
9  *  Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include "cx25821-video.h"
15
16 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
17 MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
18 MODULE_LICENSE("GPL");
19
20 static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
21
22 module_param_array(video_nr, int, NULL, 0444);
23
24 MODULE_PARM_DESC(video_nr, "video device numbers");
25
26 static unsigned int video_debug = VIDEO_DEBUG;
27 module_param(video_debug, int, 0644);
28 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
29
30 static unsigned int irq_debug;
31 module_param(irq_debug, int, 0644);
32 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
33
34 #define FORMAT_FLAGS_PACKED       0x01
35
36 static const struct cx25821_fmt formats[] = {
37         {
38                 .name = "4:1:1, packed, Y41P",
39                 .fourcc = V4L2_PIX_FMT_Y41P,
40                 .depth = 12,
41                 .flags = FORMAT_FLAGS_PACKED,
42         }, {
43                 .name = "4:2:2, packed, YUYV",
44                 .fourcc = V4L2_PIX_FMT_YUYV,
45                 .depth = 16,
46                 .flags = FORMAT_FLAGS_PACKED,
47         },
48 };
49
50 static const struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
51 {
52         unsigned int i;
53
54         for (i = 0; i < ARRAY_SIZE(formats); i++)
55                 if (formats[i].fourcc == fourcc)
56                         return formats + i;
57         return NULL;
58 }
59
60 int cx25821_start_video_dma(struct cx25821_dev *dev,
61                             struct cx25821_dmaqueue *q,
62                             struct cx25821_buffer *buf,
63                             const struct sram_channel *channel)
64 {
65         int tmp = 0;
66
67         /* setup fifo + format */
68         cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
69
70         /* reset counter */
71         cx_write(channel->gpcnt_ctl, 3);
72
73         /* enable irq */
74         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
75         cx_set(channel->int_msk, 0x11);
76
77         /* start dma */
78         cx_write(channel->dma_ctl, 0x11);       /* FIFO and RISC enable */
79
80         /* make sure upstream setting if any is reversed */
81         tmp = cx_read(VID_CH_MODE_SEL);
82         cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
83
84         return 0;
85 }
86
87 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
88 {
89         int handled = 0;
90         u32 mask;
91         const struct sram_channel *channel = dev->channels[chan_num].sram_channels;
92
93         mask = cx_read(channel->int_msk);
94         if (0 == (status & mask))
95                 return handled;
96
97         cx_write(channel->int_stat, status);
98
99         /* risc op code error */
100         if (status & (1 << 16)) {
101                 pr_warn("%s, %s: video risc op code error\n",
102                         dev->name, channel->name);
103                 cx_clear(channel->dma_ctl, 0x11);
104                 cx25821_sram_channel_dump(dev, channel);
105         }
106
107         /* risc1 y */
108         if (status & FLD_VID_DST_RISC1) {
109                 struct cx25821_dmaqueue *dmaq =
110                         &dev->channels[channel->i].dma_vidq;
111                 struct cx25821_buffer *buf;
112
113                 spin_lock(&dev->slock);
114                 if (!list_empty(&dmaq->active)) {
115                         buf = list_entry(dmaq->active.next,
116                                          struct cx25821_buffer, queue);
117
118                         buf->vb.vb2_buf.timestamp = ktime_get_ns();
119                         buf->vb.sequence = dmaq->count++;
120                         list_del(&buf->queue);
121                         vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
122                 }
123                 spin_unlock(&dev->slock);
124                 handled++;
125         }
126         return handled;
127 }
128
129 static int cx25821_queue_setup(struct vb2_queue *q,
130                            unsigned int *num_buffers, unsigned int *num_planes,
131                            unsigned int sizes[], struct device *alloc_devs[])
132 {
133         struct cx25821_channel *chan = q->drv_priv;
134         unsigned size = (chan->fmt->depth * chan->width * chan->height) >> 3;
135
136         if (*num_planes)
137                 return sizes[0] < size ? -EINVAL : 0;
138
139         *num_planes = 1;
140         sizes[0] = size;
141         return 0;
142 }
143
144 static int cx25821_buffer_prepare(struct vb2_buffer *vb)
145 {
146         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
147         struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
148         struct cx25821_dev *dev = chan->dev;
149         struct cx25821_buffer *buf =
150                 container_of(vbuf, struct cx25821_buffer, vb);
151         struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
152         u32 line0_offset;
153         int bpl_local = LINE_SIZE_D1;
154         int ret;
155
156         if (chan->pixel_formats == PIXEL_FRMT_411)
157                 buf->bpl = (chan->fmt->depth * chan->width) >> 3;
158         else
159                 buf->bpl = (chan->fmt->depth >> 3) * chan->width;
160
161         if (vb2_plane_size(vb, 0) < chan->height * buf->bpl)
162                 return -EINVAL;
163         vb2_set_plane_payload(vb, 0, chan->height * buf->bpl);
164         buf->vb.field = chan->field;
165
166         if (chan->pixel_formats == PIXEL_FRMT_411) {
167                 bpl_local = buf->bpl;
168         } else {
169                 bpl_local = buf->bpl;   /* Default */
170
171                 if (chan->use_cif_resolution) {
172                         if (dev->tvnorm & V4L2_STD_625_50)
173                                 bpl_local = 352 << 1;
174                         else
175                                 bpl_local = chan->cif_width << 1;
176                 }
177         }
178
179         switch (chan->field) {
180         case V4L2_FIELD_TOP:
181                 ret = cx25821_risc_buffer(dev->pci, &buf->risc,
182                                 sgt->sgl, 0, UNSET,
183                                 buf->bpl, 0, chan->height);
184                 break;
185         case V4L2_FIELD_BOTTOM:
186                 ret = cx25821_risc_buffer(dev->pci, &buf->risc,
187                                 sgt->sgl, UNSET, 0,
188                                 buf->bpl, 0, chan->height);
189                 break;
190         case V4L2_FIELD_INTERLACED:
191                 /* All other formats are top field first */
192                 line0_offset = 0;
193                 dprintk(1, "top field first\n");
194
195                 ret = cx25821_risc_buffer(dev->pci, &buf->risc,
196                                 sgt->sgl, line0_offset,
197                                 bpl_local, bpl_local, bpl_local,
198                                 chan->height >> 1);
199                 break;
200         case V4L2_FIELD_SEQ_TB:
201                 ret = cx25821_risc_buffer(dev->pci, &buf->risc,
202                                 sgt->sgl,
203                                 0, buf->bpl * (chan->height >> 1),
204                                 buf->bpl, 0, chan->height >> 1);
205                 break;
206         case V4L2_FIELD_SEQ_BT:
207                 ret = cx25821_risc_buffer(dev->pci, &buf->risc,
208                                 sgt->sgl,
209                                 buf->bpl * (chan->height >> 1), 0,
210                                 buf->bpl, 0, chan->height >> 1);
211                 break;
212         default:
213                 WARN_ON(1);
214                 ret = -EINVAL;
215                 break;
216         }
217
218         dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
219                 buf, buf->vb.vb2_buf.index, chan->width, chan->height,
220                 chan->fmt->depth, chan->fmt->name,
221                 (unsigned long)buf->risc.dma);
222
223         return ret;
224 }
225
226 static void cx25821_buffer_finish(struct vb2_buffer *vb)
227 {
228         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
229         struct cx25821_buffer *buf =
230                 container_of(vbuf, struct cx25821_buffer, vb);
231         struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
232         struct cx25821_dev *dev = chan->dev;
233
234         cx25821_free_buffer(dev, buf);
235 }
236
237 static void cx25821_buffer_queue(struct vb2_buffer *vb)
238 {
239         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
240         struct cx25821_buffer *buf =
241                 container_of(vbuf, struct cx25821_buffer, vb);
242         struct cx25821_channel *chan = vb->vb2_queue->drv_priv;
243         struct cx25821_dev *dev = chan->dev;
244         struct cx25821_buffer *prev;
245         struct cx25821_dmaqueue *q = &dev->channels[chan->id].dma_vidq;
246
247         buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
248         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
249         buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
250         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
251
252         if (list_empty(&q->active)) {
253                 list_add_tail(&buf->queue, &q->active);
254         } else {
255                 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
256                 prev = list_entry(q->active.prev, struct cx25821_buffer,
257                                 queue);
258                 list_add_tail(&buf->queue, &q->active);
259                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
260         }
261 }
262
263 static int cx25821_start_streaming(struct vb2_queue *q, unsigned int count)
264 {
265         struct cx25821_channel *chan = q->drv_priv;
266         struct cx25821_dev *dev = chan->dev;
267         struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
268         struct cx25821_buffer *buf = list_entry(dmaq->active.next,
269                         struct cx25821_buffer, queue);
270
271         dmaq->count = 0;
272         cx25821_start_video_dma(dev, dmaq, buf, chan->sram_channels);
273         return 0;
274 }
275
276 static void cx25821_stop_streaming(struct vb2_queue *q)
277 {
278         struct cx25821_channel *chan = q->drv_priv;
279         struct cx25821_dev *dev = chan->dev;
280         struct cx25821_dmaqueue *dmaq = &dev->channels[chan->id].dma_vidq;
281         unsigned long flags;
282
283         cx_write(chan->sram_channels->dma_ctl, 0); /* FIFO and RISC disable */
284         spin_lock_irqsave(&dev->slock, flags);
285         while (!list_empty(&dmaq->active)) {
286                 struct cx25821_buffer *buf = list_entry(dmaq->active.next,
287                         struct cx25821_buffer, queue);
288
289                 list_del(&buf->queue);
290                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
291         }
292         spin_unlock_irqrestore(&dev->slock, flags);
293 }
294
295 static const struct vb2_ops cx25821_video_qops = {
296         .queue_setup    = cx25821_queue_setup,
297         .buf_prepare  = cx25821_buffer_prepare,
298         .buf_finish = cx25821_buffer_finish,
299         .buf_queue    = cx25821_buffer_queue,
300         .wait_prepare = vb2_ops_wait_prepare,
301         .wait_finish = vb2_ops_wait_finish,
302         .start_streaming = cx25821_start_streaming,
303         .stop_streaming = cx25821_stop_streaming,
304 };
305
306 /* VIDEO IOCTLS */
307
308 static int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
309                             struct v4l2_fmtdesc *f)
310 {
311         if (unlikely(f->index >= ARRAY_SIZE(formats)))
312                 return -EINVAL;
313
314         strscpy(f->description, formats[f->index].name, sizeof(f->description));
315         f->pixelformat = formats[f->index].fourcc;
316
317         return 0;
318 }
319
320 static int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
321                                  struct v4l2_format *f)
322 {
323         struct cx25821_channel *chan = video_drvdata(file);
324
325         f->fmt.pix.width = chan->width;
326         f->fmt.pix.height = chan->height;
327         f->fmt.pix.field = chan->field;
328         f->fmt.pix.pixelformat = chan->fmt->fourcc;
329         f->fmt.pix.bytesperline = (chan->width * chan->fmt->depth) >> 3;
330         f->fmt.pix.sizeimage = chan->height * f->fmt.pix.bytesperline;
331         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
332
333         return 0;
334 }
335
336 static int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
337                                    struct v4l2_format *f)
338 {
339         struct cx25821_channel *chan = video_drvdata(file);
340         struct cx25821_dev *dev = chan->dev;
341         const struct cx25821_fmt *fmt;
342         enum v4l2_field field = f->fmt.pix.field;
343         unsigned int maxh;
344         unsigned w;
345
346         fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
347         if (NULL == fmt)
348                 return -EINVAL;
349         maxh = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
350
351         w = f->fmt.pix.width;
352         if (field != V4L2_FIELD_BOTTOM)
353                 field = V4L2_FIELD_TOP;
354         if (w < 352) {
355                 w = 176;
356                 f->fmt.pix.height = maxh / 4;
357         } else if (w < 720) {
358                 w = 352;
359                 f->fmt.pix.height = maxh / 2;
360         } else {
361                 w = 720;
362                 f->fmt.pix.height = maxh;
363                 field = V4L2_FIELD_INTERLACED;
364         }
365         f->fmt.pix.field = field;
366         f->fmt.pix.width = w;
367         f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
368         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
369         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
370
371         return 0;
372 }
373
374 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
375                                 struct v4l2_format *f)
376 {
377         struct cx25821_channel *chan = video_drvdata(file);
378         struct cx25821_dev *dev = chan->dev;
379         int pix_format = PIXEL_FRMT_422;
380         int err;
381
382         err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
383
384         if (0 != err)
385                 return err;
386
387         chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
388         chan->field = f->fmt.pix.field;
389         chan->width = f->fmt.pix.width;
390         chan->height = f->fmt.pix.height;
391
392         if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
393                 pix_format = PIXEL_FRMT_411;
394         else
395                 pix_format = PIXEL_FRMT_422;
396
397         cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
398
399         /* check if cif resolution */
400         if (chan->width == 320 || chan->width == 352)
401                 chan->use_cif_resolution = 1;
402         else
403                 chan->use_cif_resolution = 0;
404
405         chan->cif_width = chan->width;
406         medusa_set_resolution(dev, chan->width, SRAM_CH00);
407         return 0;
408 }
409
410 static int vidioc_log_status(struct file *file, void *priv)
411 {
412         struct cx25821_channel *chan = video_drvdata(file);
413         struct cx25821_dev *dev = chan->dev;
414         const struct sram_channel *sram_ch = chan->sram_channels;
415         u32 tmp = 0;
416
417         tmp = cx_read(sram_ch->dma_ctl);
418         pr_info("Video input 0 is %s\n",
419                 (tmp & 0x11) ? "streaming" : "stopped");
420         return 0;
421 }
422
423
424 static int cx25821_vidioc_querycap(struct file *file, void *priv,
425                             struct v4l2_capability *cap)
426 {
427         struct cx25821_channel *chan = video_drvdata(file);
428         struct cx25821_dev *dev = chan->dev;
429
430         strscpy(cap->driver, "cx25821", sizeof(cap->driver));
431         strscpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
432         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
433         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
434                             V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
435                             V4L2_CAP_DEVICE_CAPS;
436         return 0;
437 }
438
439 static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
440 {
441         struct cx25821_channel *chan = video_drvdata(file);
442
443         *tvnorms = chan->dev->tvnorm;
444         return 0;
445 }
446
447 static int cx25821_vidioc_s_std(struct file *file, void *priv,
448                                 v4l2_std_id tvnorms)
449 {
450         struct cx25821_channel *chan = video_drvdata(file);
451         struct cx25821_dev *dev = chan->dev;
452
453         if (dev->tvnorm == tvnorms)
454                 return 0;
455
456         dev->tvnorm = tvnorms;
457         chan->width = 720;
458         chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
459
460         medusa_set_videostandard(dev);
461
462         return 0;
463 }
464
465 static int cx25821_vidioc_enum_input(struct file *file, void *priv,
466                               struct v4l2_input *i)
467 {
468         if (i->index)
469                 return -EINVAL;
470
471         i->type = V4L2_INPUT_TYPE_CAMERA;
472         i->std = CX25821_NORMS;
473         strscpy(i->name, "Composite", sizeof(i->name));
474         return 0;
475 }
476
477 static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
478 {
479         *i = 0;
480         return 0;
481 }
482
483 static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
484 {
485         return i ? -EINVAL : 0;
486 }
487
488 static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
489 {
490         struct cx25821_channel *chan =
491                 container_of(ctrl->handler, struct cx25821_channel, hdl);
492         struct cx25821_dev *dev = chan->dev;
493
494         switch (ctrl->id) {
495         case V4L2_CID_BRIGHTNESS:
496                 medusa_set_brightness(dev, ctrl->val, chan->id);
497                 break;
498         case V4L2_CID_HUE:
499                 medusa_set_hue(dev, ctrl->val, chan->id);
500                 break;
501         case V4L2_CID_CONTRAST:
502                 medusa_set_contrast(dev, ctrl->val, chan->id);
503                 break;
504         case V4L2_CID_SATURATION:
505                 medusa_set_saturation(dev, ctrl->val, chan->id);
506                 break;
507         default:
508                 return -EINVAL;
509         }
510         return 0;
511 }
512
513 static int cx25821_vidioc_enum_output(struct file *file, void *priv,
514                               struct v4l2_output *o)
515 {
516         if (o->index)
517                 return -EINVAL;
518
519         o->type = V4L2_INPUT_TYPE_CAMERA;
520         o->std = CX25821_NORMS;
521         strscpy(o->name, "Composite", sizeof(o->name));
522         return 0;
523 }
524
525 static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
526 {
527         *o = 0;
528         return 0;
529 }
530
531 static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
532 {
533         return o ? -EINVAL : 0;
534 }
535
536 static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
537                                    struct v4l2_format *f)
538 {
539         struct cx25821_channel *chan = video_drvdata(file);
540         struct cx25821_dev *dev = chan->dev;
541         const struct cx25821_fmt *fmt;
542
543         fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
544         if (NULL == fmt)
545                 return -EINVAL;
546         f->fmt.pix.width = 720;
547         f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
548         f->fmt.pix.field = V4L2_FIELD_INTERLACED;
549         f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
550         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
551         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
552         return 0;
553 }
554
555 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
556                                 struct v4l2_format *f)
557 {
558         struct cx25821_channel *chan = video_drvdata(file);
559         int err;
560
561         err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
562
563         if (0 != err)
564                 return err;
565
566         chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
567         chan->field = f->fmt.pix.field;
568         chan->width = f->fmt.pix.width;
569         chan->height = f->fmt.pix.height;
570         if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
571                 chan->pixel_formats = PIXEL_FRMT_411;
572         else
573                 chan->pixel_formats = PIXEL_FRMT_422;
574         return 0;
575 }
576
577 static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
578         .s_ctrl = cx25821_s_ctrl,
579 };
580
581 static const struct v4l2_file_operations video_fops = {
582         .owner = THIS_MODULE,
583         .open = v4l2_fh_open,
584         .release        = vb2_fop_release,
585         .read           = vb2_fop_read,
586         .poll           = vb2_fop_poll,
587         .unlocked_ioctl = video_ioctl2,
588         .mmap           = vb2_fop_mmap,
589 };
590
591 static const struct v4l2_ioctl_ops video_ioctl_ops = {
592         .vidioc_querycap = cx25821_vidioc_querycap,
593         .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
594         .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
595         .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
596         .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
597         .vidioc_reqbufs       = vb2_ioctl_reqbufs,
598         .vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
599         .vidioc_create_bufs   = vb2_ioctl_create_bufs,
600         .vidioc_querybuf      = vb2_ioctl_querybuf,
601         .vidioc_qbuf          = vb2_ioctl_qbuf,
602         .vidioc_dqbuf         = vb2_ioctl_dqbuf,
603         .vidioc_streamon      = vb2_ioctl_streamon,
604         .vidioc_streamoff     = vb2_ioctl_streamoff,
605         .vidioc_g_std = cx25821_vidioc_g_std,
606         .vidioc_s_std = cx25821_vidioc_s_std,
607         .vidioc_enum_input = cx25821_vidioc_enum_input,
608         .vidioc_g_input = cx25821_vidioc_g_input,
609         .vidioc_s_input = cx25821_vidioc_s_input,
610         .vidioc_log_status = vidioc_log_status,
611         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
612         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
613 };
614
615 static const struct video_device cx25821_video_device = {
616         .name = "cx25821-video",
617         .fops = &video_fops,
618         .release = video_device_release_empty,
619         .minor = -1,
620         .ioctl_ops = &video_ioctl_ops,
621         .tvnorms = CX25821_NORMS,
622         .device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
623                        V4L2_CAP_STREAMING,
624 };
625
626 static const struct v4l2_file_operations video_out_fops = {
627         .owner = THIS_MODULE,
628         .open = v4l2_fh_open,
629         .release        = vb2_fop_release,
630         .write          = vb2_fop_write,
631         .poll           = vb2_fop_poll,
632         .unlocked_ioctl = video_ioctl2,
633         .mmap           = vb2_fop_mmap,
634 };
635
636 static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
637         .vidioc_querycap = cx25821_vidioc_querycap,
638         .vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
639         .vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
640         .vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
641         .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
642         .vidioc_g_std = cx25821_vidioc_g_std,
643         .vidioc_s_std = cx25821_vidioc_s_std,
644         .vidioc_enum_output = cx25821_vidioc_enum_output,
645         .vidioc_g_output = cx25821_vidioc_g_output,
646         .vidioc_s_output = cx25821_vidioc_s_output,
647         .vidioc_log_status = vidioc_log_status,
648 };
649
650 static const struct video_device cx25821_video_out_device = {
651         .name = "cx25821-video",
652         .fops = &video_out_fops,
653         .release = video_device_release_empty,
654         .minor = -1,
655         .ioctl_ops = &video_out_ioctl_ops,
656         .tvnorms = CX25821_NORMS,
657         .device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE,
658 };
659
660 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
661 {
662         cx_clear(PCI_INT_MSK, 1);
663
664         if (video_is_registered(&dev->channels[chan_num].vdev)) {
665                 video_unregister_device(&dev->channels[chan_num].vdev);
666                 v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
667         }
668 }
669
670 int cx25821_video_register(struct cx25821_dev *dev)
671 {
672         int err;
673         int i;
674
675         /* initial device configuration */
676         dev->tvnorm = V4L2_STD_NTSC_M;
677
678         spin_lock_init(&dev->slock);
679
680         for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; ++i) {
681                 struct cx25821_channel *chan = &dev->channels[i];
682                 struct video_device *vdev = &chan->vdev;
683                 struct v4l2_ctrl_handler *hdl = &chan->hdl;
684                 struct vb2_queue *q;
685                 bool is_output = i > SRAM_CH08;
686
687                 if (i == SRAM_CH08) /* audio channel */
688                         continue;
689
690                 if (!is_output) {
691                         v4l2_ctrl_handler_init(hdl, 4);
692                         v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
693                                         V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
694                         v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
695                                         V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
696                         v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
697                                         V4L2_CID_SATURATION, 0, 10000, 1, 5000);
698                         v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
699                                         V4L2_CID_HUE, 0, 10000, 1, 5000);
700                         if (hdl->error) {
701                                 err = hdl->error;
702                                 goto fail_unreg;
703                         }
704                         err = v4l2_ctrl_handler_setup(hdl);
705                         if (err)
706                                 goto fail_unreg;
707                 } else {
708                         chan->out = &dev->vid_out_data[i - SRAM_CH09];
709                         chan->out->chan = chan;
710                 }
711
712                 chan->sram_channels = &cx25821_sram_channels[i];
713                 chan->width = 720;
714                 chan->field = V4L2_FIELD_INTERLACED;
715                 if (dev->tvnorm & V4L2_STD_625_50)
716                         chan->height = 576;
717                 else
718                         chan->height = 480;
719
720                 if (chan->pixel_formats == PIXEL_FRMT_411)
721                         chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
722                 else
723                         chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
724
725                 cx_write(chan->sram_channels->int_stat, 0xffffffff);
726
727                 INIT_LIST_HEAD(&chan->dma_vidq.active);
728
729                 q = &chan->vidq;
730
731                 q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
732                                       V4L2_BUF_TYPE_VIDEO_CAPTURE;
733                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
734                 q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
735                 q->gfp_flags = GFP_DMA32;
736                 q->min_buffers_needed = 2;
737                 q->drv_priv = chan;
738                 q->buf_struct_size = sizeof(struct cx25821_buffer);
739                 q->ops = &cx25821_video_qops;
740                 q->mem_ops = &vb2_dma_sg_memops;
741                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
742                 q->lock = &dev->lock;
743                 q->dev = &dev->pci->dev;
744
745                 if (!is_output) {
746                         err = vb2_queue_init(q);
747                         if (err < 0)
748                                 goto fail_unreg;
749                 }
750
751                 /* register v4l devices */
752                 *vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
753                 vdev->v4l2_dev = &dev->v4l2_dev;
754                 if (!is_output)
755                         vdev->ctrl_handler = hdl;
756                 else
757                         vdev->vfl_dir = VFL_DIR_TX;
758                 vdev->lock = &dev->lock;
759                 vdev->queue = q;
760                 snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
761                 video_set_drvdata(vdev, chan);
762
763                 err = video_register_device(vdev, VFL_TYPE_GRABBER,
764                                             video_nr[dev->nr]);
765
766                 if (err < 0)
767                         goto fail_unreg;
768         }
769
770         /* set PCI interrupt */
771         cx_set(PCI_INT_MSK, 0xff);
772
773         return 0;
774
775 fail_unreg:
776         while (i >= 0)
777                 cx25821_video_unregister(dev, i--);
778         return err;
779 }