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