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