xref: /linux/drivers/media/pci/cx25821/cx25821-video.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
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 	const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
430 			V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
431 	const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE;
432 
433 	strscpy(cap->driver, "cx25821", sizeof(cap->driver));
434 	strscpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
435 	sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
436 	if (chan->id >= VID_CHANNEL_NUM)
437 		cap->device_caps = cap_output;
438 	else
439 		cap->device_caps = cap_input;
440 	cap->capabilities = cap_input | cap_output | V4L2_CAP_DEVICE_CAPS;
441 	return 0;
442 }
443 
444 static int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
445 {
446 	struct cx25821_channel *chan = video_drvdata(file);
447 
448 	*tvnorms = chan->dev->tvnorm;
449 	return 0;
450 }
451 
452 static int cx25821_vidioc_s_std(struct file *file, void *priv,
453 				v4l2_std_id tvnorms)
454 {
455 	struct cx25821_channel *chan = video_drvdata(file);
456 	struct cx25821_dev *dev = chan->dev;
457 
458 	if (dev->tvnorm == tvnorms)
459 		return 0;
460 
461 	dev->tvnorm = tvnorms;
462 	chan->width = 720;
463 	chan->height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
464 
465 	medusa_set_videostandard(dev);
466 
467 	return 0;
468 }
469 
470 static int cx25821_vidioc_enum_input(struct file *file, void *priv,
471 			      struct v4l2_input *i)
472 {
473 	if (i->index)
474 		return -EINVAL;
475 
476 	i->type = V4L2_INPUT_TYPE_CAMERA;
477 	i->std = CX25821_NORMS;
478 	strscpy(i->name, "Composite", sizeof(i->name));
479 	return 0;
480 }
481 
482 static int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
483 {
484 	*i = 0;
485 	return 0;
486 }
487 
488 static int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
489 {
490 	return i ? -EINVAL : 0;
491 }
492 
493 static int cx25821_s_ctrl(struct v4l2_ctrl *ctrl)
494 {
495 	struct cx25821_channel *chan =
496 		container_of(ctrl->handler, struct cx25821_channel, hdl);
497 	struct cx25821_dev *dev = chan->dev;
498 
499 	switch (ctrl->id) {
500 	case V4L2_CID_BRIGHTNESS:
501 		medusa_set_brightness(dev, ctrl->val, chan->id);
502 		break;
503 	case V4L2_CID_HUE:
504 		medusa_set_hue(dev, ctrl->val, chan->id);
505 		break;
506 	case V4L2_CID_CONTRAST:
507 		medusa_set_contrast(dev, ctrl->val, chan->id);
508 		break;
509 	case V4L2_CID_SATURATION:
510 		medusa_set_saturation(dev, ctrl->val, chan->id);
511 		break;
512 	default:
513 		return -EINVAL;
514 	}
515 	return 0;
516 }
517 
518 static int cx25821_vidioc_enum_output(struct file *file, void *priv,
519 			      struct v4l2_output *o)
520 {
521 	if (o->index)
522 		return -EINVAL;
523 
524 	o->type = V4L2_INPUT_TYPE_CAMERA;
525 	o->std = CX25821_NORMS;
526 	strscpy(o->name, "Composite", sizeof(o->name));
527 	return 0;
528 }
529 
530 static int cx25821_vidioc_g_output(struct file *file, void *priv, unsigned int *o)
531 {
532 	*o = 0;
533 	return 0;
534 }
535 
536 static int cx25821_vidioc_s_output(struct file *file, void *priv, unsigned int o)
537 {
538 	return o ? -EINVAL : 0;
539 }
540 
541 static int cx25821_vidioc_try_fmt_vid_out(struct file *file, void *priv,
542 				   struct v4l2_format *f)
543 {
544 	struct cx25821_channel *chan = video_drvdata(file);
545 	struct cx25821_dev *dev = chan->dev;
546 	const struct cx25821_fmt *fmt;
547 
548 	fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
549 	if (NULL == fmt)
550 		return -EINVAL;
551 	f->fmt.pix.width = 720;
552 	f->fmt.pix.height = (dev->tvnorm & V4L2_STD_625_50) ? 576 : 480;
553 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
554 	f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
555 	f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
556 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
557 	return 0;
558 }
559 
560 static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
561 				struct v4l2_format *f)
562 {
563 	struct cx25821_channel *chan = video_drvdata(file);
564 	int err;
565 
566 	err = cx25821_vidioc_try_fmt_vid_out(file, priv, f);
567 
568 	if (0 != err)
569 		return err;
570 
571 	chan->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
572 	chan->field = f->fmt.pix.field;
573 	chan->width = f->fmt.pix.width;
574 	chan->height = f->fmt.pix.height;
575 	if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
576 		chan->pixel_formats = PIXEL_FRMT_411;
577 	else
578 		chan->pixel_formats = PIXEL_FRMT_422;
579 	return 0;
580 }
581 
582 static const struct v4l2_ctrl_ops cx25821_ctrl_ops = {
583 	.s_ctrl = cx25821_s_ctrl,
584 };
585 
586 static const struct v4l2_file_operations video_fops = {
587 	.owner = THIS_MODULE,
588 	.open = v4l2_fh_open,
589 	.release        = vb2_fop_release,
590 	.read           = vb2_fop_read,
591 	.poll		= vb2_fop_poll,
592 	.unlocked_ioctl = video_ioctl2,
593 	.mmap           = vb2_fop_mmap,
594 };
595 
596 static const struct v4l2_ioctl_ops video_ioctl_ops = {
597 	.vidioc_querycap = cx25821_vidioc_querycap,
598 	.vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
599 	.vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
600 	.vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
601 	.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
602 	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
603 	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
604 	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
605 	.vidioc_querybuf      = vb2_ioctl_querybuf,
606 	.vidioc_qbuf          = vb2_ioctl_qbuf,
607 	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
608 	.vidioc_streamon      = vb2_ioctl_streamon,
609 	.vidioc_streamoff     = vb2_ioctl_streamoff,
610 	.vidioc_g_std = cx25821_vidioc_g_std,
611 	.vidioc_s_std = cx25821_vidioc_s_std,
612 	.vidioc_enum_input = cx25821_vidioc_enum_input,
613 	.vidioc_g_input = cx25821_vidioc_g_input,
614 	.vidioc_s_input = cx25821_vidioc_s_input,
615 	.vidioc_log_status = vidioc_log_status,
616 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
617 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
618 };
619 
620 static const struct video_device cx25821_video_device = {
621 	.name = "cx25821-video",
622 	.fops = &video_fops,
623 	.release = video_device_release_empty,
624 	.minor = -1,
625 	.ioctl_ops = &video_ioctl_ops,
626 	.tvnorms = CX25821_NORMS,
627 };
628 
629 static const struct v4l2_file_operations video_out_fops = {
630 	.owner = THIS_MODULE,
631 	.open = v4l2_fh_open,
632 	.release        = vb2_fop_release,
633 	.write          = vb2_fop_write,
634 	.poll		= vb2_fop_poll,
635 	.unlocked_ioctl = video_ioctl2,
636 	.mmap           = vb2_fop_mmap,
637 };
638 
639 static const struct v4l2_ioctl_ops video_out_ioctl_ops = {
640 	.vidioc_querycap = cx25821_vidioc_querycap,
641 	.vidioc_enum_fmt_vid_out = cx25821_vidioc_enum_fmt_vid_cap,
642 	.vidioc_g_fmt_vid_out = cx25821_vidioc_g_fmt_vid_cap,
643 	.vidioc_try_fmt_vid_out = cx25821_vidioc_try_fmt_vid_out,
644 	.vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
645 	.vidioc_g_std = cx25821_vidioc_g_std,
646 	.vidioc_s_std = cx25821_vidioc_s_std,
647 	.vidioc_enum_output = cx25821_vidioc_enum_output,
648 	.vidioc_g_output = cx25821_vidioc_g_output,
649 	.vidioc_s_output = cx25821_vidioc_s_output,
650 	.vidioc_log_status = vidioc_log_status,
651 };
652 
653 static const struct video_device cx25821_video_out_device = {
654 	.name = "cx25821-video",
655 	.fops = &video_out_fops,
656 	.release = video_device_release_empty,
657 	.minor = -1,
658 	.ioctl_ops = &video_out_ioctl_ops,
659 	.tvnorms = CX25821_NORMS,
660 };
661 
662 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
663 {
664 	cx_clear(PCI_INT_MSK, 1);
665 
666 	if (video_is_registered(&dev->channels[chan_num].vdev)) {
667 		video_unregister_device(&dev->channels[chan_num].vdev);
668 		v4l2_ctrl_handler_free(&dev->channels[chan_num].hdl);
669 	}
670 }
671 
672 int cx25821_video_register(struct cx25821_dev *dev)
673 {
674 	int err;
675 	int i;
676 
677 	/* initial device configuration */
678 	dev->tvnorm = V4L2_STD_NTSC_M;
679 
680 	spin_lock_init(&dev->slock);
681 
682 	for (i = 0; i < MAX_VID_CAP_CHANNEL_NUM - 1; ++i) {
683 		struct cx25821_channel *chan = &dev->channels[i];
684 		struct video_device *vdev = &chan->vdev;
685 		struct v4l2_ctrl_handler *hdl = &chan->hdl;
686 		struct vb2_queue *q;
687 		bool is_output = i > SRAM_CH08;
688 
689 		if (i == SRAM_CH08) /* audio channel */
690 			continue;
691 
692 		if (!is_output) {
693 			v4l2_ctrl_handler_init(hdl, 4);
694 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
695 					V4L2_CID_BRIGHTNESS, 0, 10000, 1, 6200);
696 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
697 					V4L2_CID_CONTRAST, 0, 10000, 1, 5000);
698 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
699 					V4L2_CID_SATURATION, 0, 10000, 1, 5000);
700 			v4l2_ctrl_new_std(hdl, &cx25821_ctrl_ops,
701 					V4L2_CID_HUE, 0, 10000, 1, 5000);
702 			if (hdl->error) {
703 				err = hdl->error;
704 				goto fail_unreg;
705 			}
706 			err = v4l2_ctrl_handler_setup(hdl);
707 			if (err)
708 				goto fail_unreg;
709 		} else {
710 			chan->out = &dev->vid_out_data[i - SRAM_CH09];
711 			chan->out->chan = chan;
712 		}
713 
714 		chan->sram_channels = &cx25821_sram_channels[i];
715 		chan->width = 720;
716 		chan->field = V4L2_FIELD_INTERLACED;
717 		if (dev->tvnorm & V4L2_STD_625_50)
718 			chan->height = 576;
719 		else
720 			chan->height = 480;
721 
722 		if (chan->pixel_formats == PIXEL_FRMT_411)
723 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_Y41P);
724 		else
725 			chan->fmt = cx25821_format_by_fourcc(V4L2_PIX_FMT_YUYV);
726 
727 		cx_write(chan->sram_channels->int_stat, 0xffffffff);
728 
729 		INIT_LIST_HEAD(&chan->dma_vidq.active);
730 
731 		q = &chan->vidq;
732 
733 		q->type = is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
734 				      V4L2_BUF_TYPE_VIDEO_CAPTURE;
735 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
736 		q->io_modes |= is_output ? VB2_WRITE : VB2_READ;
737 		q->gfp_flags = GFP_DMA32;
738 		q->min_buffers_needed = 2;
739 		q->drv_priv = chan;
740 		q->buf_struct_size = sizeof(struct cx25821_buffer);
741 		q->ops = &cx25821_video_qops;
742 		q->mem_ops = &vb2_dma_sg_memops;
743 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
744 		q->lock = &dev->lock;
745 		q->dev = &dev->pci->dev;
746 
747 		if (!is_output) {
748 			err = vb2_queue_init(q);
749 			if (err < 0)
750 				goto fail_unreg;
751 		}
752 
753 		/* register v4l devices */
754 		*vdev = is_output ? cx25821_video_out_device : cx25821_video_device;
755 		vdev->v4l2_dev = &dev->v4l2_dev;
756 		if (!is_output)
757 			vdev->ctrl_handler = hdl;
758 		else
759 			vdev->vfl_dir = VFL_DIR_TX;
760 		vdev->lock = &dev->lock;
761 		vdev->queue = q;
762 		snprintf(vdev->name, sizeof(vdev->name), "%s #%d", dev->name, i);
763 		video_set_drvdata(vdev, chan);
764 
765 		err = video_register_device(vdev, VFL_TYPE_GRABBER,
766 					    video_nr[dev->nr]);
767 
768 		if (err < 0)
769 			goto fail_unreg;
770 	}
771 
772 	/* set PCI interrupt */
773 	cx_set(PCI_INT_MSK, 0xff);
774 
775 	return 0;
776 
777 fail_unreg:
778 	while (i >= 0)
779 		cx25821_video_unregister(dev, i--);
780 	return err;
781 }
782