GNU Linux-libre 4.4.297-gnu1
[releases.git] / drivers / media / pci / solo6x10 / solo6x10-v4l2.c
1 /*
2  * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
3  *
4  * Original author:
5  * Ben Collins <bcollins@ubuntu.com>
6  *
7  * Additional work by:
8  * John Brooks <john.brooks@bluecherry.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/kthread.h>
24 #include <linux/freezer.h>
25
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-event.h>
29 #include <media/videobuf2-v4l2.h>
30 #include <media/videobuf2-dma-contig.h>
31
32 #include "solo6x10.h"
33 #include "solo6x10-tw28.h"
34
35 /* Image size is two fields, SOLO_HW_BPL is one horizontal line in hardware */
36 #define SOLO_HW_BPL             2048
37 #define solo_vlines(__solo)     (__solo->video_vsize * 2)
38 #define solo_image_size(__solo) (solo_bytesperline(__solo) * \
39                                  solo_vlines(__solo))
40 #define solo_bytesperline(__solo) (__solo->video_hsize * 2)
41
42 #define MIN_VID_BUFFERS         2
43
44 static inline void erase_on(struct solo_dev *solo_dev)
45 {
46         solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON);
47         solo_dev->erasing = 1;
48         solo_dev->frame_blank = 0;
49 }
50
51 static inline int erase_off(struct solo_dev *solo_dev)
52 {
53         if (!solo_dev->erasing)
54                 return 0;
55
56         /* First time around, assert erase off */
57         if (!solo_dev->frame_blank)
58                 solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, 0);
59         /* Keep the erasing flag on for 8 frames minimum */
60         if (solo_dev->frame_blank++ >= 8)
61                 solo_dev->erasing = 0;
62
63         return 1;
64 }
65
66 void solo_video_in_isr(struct solo_dev *solo_dev)
67 {
68         wake_up_interruptible_all(&solo_dev->disp_thread_wait);
69 }
70
71 static void solo_win_setup(struct solo_dev *solo_dev, u8 ch,
72                            int sx, int sy, int ex, int ey, int scale)
73 {
74         if (ch >= solo_dev->nr_chans)
75                 return;
76
77         /* Here, we just keep window/channel the same */
78         solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(ch),
79                        SOLO_VI_WIN_CHANNEL(ch) |
80                        SOLO_VI_WIN_SX(sx) |
81                        SOLO_VI_WIN_EX(ex) |
82                        SOLO_VI_WIN_SCALE(scale));
83
84         solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(ch),
85                        SOLO_VI_WIN_SY(sy) |
86                        SOLO_VI_WIN_EY(ey));
87 }
88
89 static int solo_v4l2_ch_ext_4up(struct solo_dev *solo_dev, u8 idx, int on)
90 {
91         u8 ch = idx * 4;
92
93         if (ch >= solo_dev->nr_chans)
94                 return -EINVAL;
95
96         if (!on) {
97                 u8 i;
98
99                 for (i = ch; i < ch + 4; i++)
100                         solo_win_setup(solo_dev, i, solo_dev->video_hsize,
101                                        solo_vlines(solo_dev),
102                                        solo_dev->video_hsize,
103                                        solo_vlines(solo_dev), 0);
104                 return 0;
105         }
106
107         /* Row 1 */
108         solo_win_setup(solo_dev, ch, 0, 0, solo_dev->video_hsize / 2,
109                        solo_vlines(solo_dev) / 2, 3);
110         solo_win_setup(solo_dev, ch + 1, solo_dev->video_hsize / 2, 0,
111                        solo_dev->video_hsize, solo_vlines(solo_dev) / 2, 3);
112         /* Row 2 */
113         solo_win_setup(solo_dev, ch + 2, 0, solo_vlines(solo_dev) / 2,
114                        solo_dev->video_hsize / 2, solo_vlines(solo_dev), 3);
115         solo_win_setup(solo_dev, ch + 3, solo_dev->video_hsize / 2,
116                        solo_vlines(solo_dev) / 2, solo_dev->video_hsize,
117                        solo_vlines(solo_dev), 3);
118
119         return 0;
120 }
121
122 static int solo_v4l2_ch_ext_16up(struct solo_dev *solo_dev, int on)
123 {
124         int sy, ysize, hsize, i;
125
126         if (!on) {
127                 for (i = 0; i < 16; i++)
128                         solo_win_setup(solo_dev, i, solo_dev->video_hsize,
129                                        solo_vlines(solo_dev),
130                                        solo_dev->video_hsize,
131                                        solo_vlines(solo_dev), 0);
132                 return 0;
133         }
134
135         ysize = solo_vlines(solo_dev) / 4;
136         hsize = solo_dev->video_hsize / 4;
137
138         for (sy = 0, i = 0; i < 4; i++, sy += ysize) {
139                 solo_win_setup(solo_dev, i * 4, 0, sy, hsize,
140                                sy + ysize, 5);
141                 solo_win_setup(solo_dev, (i * 4) + 1, hsize, sy,
142                                hsize * 2, sy + ysize, 5);
143                 solo_win_setup(solo_dev, (i * 4) + 2, hsize * 2, sy,
144                                hsize * 3, sy + ysize, 5);
145                 solo_win_setup(solo_dev, (i * 4) + 3, hsize * 3, sy,
146                                solo_dev->video_hsize, sy + ysize, 5);
147         }
148
149         return 0;
150 }
151
152 static int solo_v4l2_ch(struct solo_dev *solo_dev, u8 ch, int on)
153 {
154         u8 ext_ch;
155
156         if (ch < solo_dev->nr_chans) {
157                 solo_win_setup(solo_dev, ch, on ? 0 : solo_dev->video_hsize,
158                                on ? 0 : solo_vlines(solo_dev),
159                                solo_dev->video_hsize, solo_vlines(solo_dev),
160                                on ? 1 : 0);
161                 return 0;
162         }
163
164         if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
165                 return -EINVAL;
166
167         ext_ch = ch - solo_dev->nr_chans;
168
169         /* 4up's first */
170         if (ext_ch < 4)
171                 return solo_v4l2_ch_ext_4up(solo_dev, ext_ch, on);
172
173         /* Remaining case is 16up for 16-port */
174         return solo_v4l2_ch_ext_16up(solo_dev, on);
175 }
176
177 static int solo_v4l2_set_ch(struct solo_dev *solo_dev, u8 ch)
178 {
179         if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
180                 return -EINVAL;
181
182         erase_on(solo_dev);
183
184         solo_v4l2_ch(solo_dev, solo_dev->cur_disp_ch, 0);
185         solo_v4l2_ch(solo_dev, ch, 1);
186
187         solo_dev->cur_disp_ch = ch;
188
189         return 0;
190 }
191
192 static void solo_fillbuf(struct solo_dev *solo_dev,
193                          struct vb2_buffer *vb)
194 {
195         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
196         dma_addr_t addr;
197         unsigned int fdma_addr;
198         int error = -1;
199         int i;
200
201         addr = vb2_dma_contig_plane_dma_addr(vb, 0);
202         if (!addr)
203                 goto finish_buf;
204
205         if (erase_off(solo_dev)) {
206                 void *p = vb2_plane_vaddr(vb, 0);
207                 int image_size = solo_image_size(solo_dev);
208
209                 for (i = 0; i < image_size; i += 2) {
210                         ((u8 *)p)[i] = 0x80;
211                         ((u8 *)p)[i + 1] = 0x00;
212                 }
213                 error = 0;
214         } else {
215                 fdma_addr = SOLO_DISP_EXT_ADDR + (solo_dev->old_write *
216                                 (SOLO_HW_BPL * solo_vlines(solo_dev)));
217
218                 error = solo_p2m_dma_t(solo_dev, 0, addr, fdma_addr,
219                                        solo_bytesperline(solo_dev),
220                                        solo_vlines(solo_dev), SOLO_HW_BPL);
221         }
222
223 finish_buf:
224         if (!error) {
225                 vb2_set_plane_payload(vb, 0,
226                         solo_vlines(solo_dev) * solo_bytesperline(solo_dev));
227                 vbuf->sequence = solo_dev->sequence++;
228                 v4l2_get_timestamp(&vbuf->timestamp);
229         }
230
231         vb2_buffer_done(vb, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
232 }
233
234 static void solo_thread_try(struct solo_dev *solo_dev)
235 {
236         struct solo_vb2_buf *vb;
237
238         /* Only "break" from this loop if slock is held, otherwise
239          * just return. */
240         for (;;) {
241                 unsigned int cur_write;
242
243                 cur_write = SOLO_VI_STATUS0_PAGE(
244                         solo_reg_read(solo_dev, SOLO_VI_STATUS0));
245                 if (cur_write == solo_dev->old_write)
246                         return;
247
248                 spin_lock(&solo_dev->slock);
249
250                 if (list_empty(&solo_dev->vidq_active))
251                         break;
252
253                 vb = list_first_entry(&solo_dev->vidq_active, struct solo_vb2_buf,
254                                       list);
255
256                 solo_dev->old_write = cur_write;
257                 list_del(&vb->list);
258
259                 spin_unlock(&solo_dev->slock);
260
261                 solo_fillbuf(solo_dev, &vb->vb.vb2_buf);
262         }
263
264         assert_spin_locked(&solo_dev->slock);
265         spin_unlock(&solo_dev->slock);
266 }
267
268 static int solo_thread(void *data)
269 {
270         struct solo_dev *solo_dev = data;
271         DECLARE_WAITQUEUE(wait, current);
272
273         set_freezable();
274         add_wait_queue(&solo_dev->disp_thread_wait, &wait);
275
276         for (;;) {
277                 long timeout = schedule_timeout_interruptible(HZ);
278
279                 if (timeout == -ERESTARTSYS || kthread_should_stop())
280                         break;
281                 solo_thread_try(solo_dev);
282                 try_to_freeze();
283         }
284
285         remove_wait_queue(&solo_dev->disp_thread_wait, &wait);
286
287         return 0;
288 }
289
290 static int solo_start_thread(struct solo_dev *solo_dev)
291 {
292         int ret = 0;
293
294         solo_dev->kthread = kthread_run(solo_thread, solo_dev, SOLO6X10_NAME "_disp");
295
296         if (IS_ERR(solo_dev->kthread)) {
297                 ret = PTR_ERR(solo_dev->kthread);
298                 solo_dev->kthread = NULL;
299                 return ret;
300         }
301         solo_irq_on(solo_dev, SOLO_IRQ_VIDEO_IN);
302
303         return ret;
304 }
305
306 static void solo_stop_thread(struct solo_dev *solo_dev)
307 {
308         if (!solo_dev->kthread)
309                 return;
310
311         solo_irq_off(solo_dev, SOLO_IRQ_VIDEO_IN);
312         kthread_stop(solo_dev->kthread);
313         solo_dev->kthread = NULL;
314 }
315
316 static int solo_queue_setup(struct vb2_queue *q, const void *parg,
317                            unsigned int *num_buffers, unsigned int *num_planes,
318                            unsigned int sizes[], void *alloc_ctxs[])
319 {
320         struct solo_dev *solo_dev = vb2_get_drv_priv(q);
321
322         sizes[0] = solo_image_size(solo_dev);
323         alloc_ctxs[0] = solo_dev->alloc_ctx;
324         *num_planes = 1;
325
326         if (*num_buffers < MIN_VID_BUFFERS)
327                 *num_buffers = MIN_VID_BUFFERS;
328
329         return 0;
330 }
331
332 static int solo_start_streaming(struct vb2_queue *q, unsigned int count)
333 {
334         struct solo_dev *solo_dev = vb2_get_drv_priv(q);
335
336         solo_dev->sequence = 0;
337         return solo_start_thread(solo_dev);
338 }
339
340 static void solo_stop_streaming(struct vb2_queue *q)
341 {
342         struct solo_dev *solo_dev = vb2_get_drv_priv(q);
343
344         solo_stop_thread(solo_dev);
345
346         spin_lock(&solo_dev->slock);
347         while (!list_empty(&solo_dev->vidq_active)) {
348                 struct solo_vb2_buf *buf = list_entry(
349                                 solo_dev->vidq_active.next,
350                                 struct solo_vb2_buf, list);
351
352                 list_del(&buf->list);
353                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
354         }
355         spin_unlock(&solo_dev->slock);
356         INIT_LIST_HEAD(&solo_dev->vidq_active);
357 }
358
359 static void solo_buf_queue(struct vb2_buffer *vb)
360 {
361         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
362         struct vb2_queue *vq = vb->vb2_queue;
363         struct solo_dev *solo_dev = vb2_get_drv_priv(vq);
364         struct solo_vb2_buf *solo_vb =
365                 container_of(vbuf, struct solo_vb2_buf, vb);
366
367         spin_lock(&solo_dev->slock);
368         list_add_tail(&solo_vb->list, &solo_dev->vidq_active);
369         spin_unlock(&solo_dev->slock);
370         wake_up_interruptible(&solo_dev->disp_thread_wait);
371 }
372
373 static const struct vb2_ops solo_video_qops = {
374         .queue_setup    = solo_queue_setup,
375         .buf_queue      = solo_buf_queue,
376         .start_streaming = solo_start_streaming,
377         .stop_streaming = solo_stop_streaming,
378         .wait_prepare   = vb2_ops_wait_prepare,
379         .wait_finish    = vb2_ops_wait_finish,
380 };
381
382 static int solo_querycap(struct file *file, void  *priv,
383                          struct v4l2_capability *cap)
384 {
385         struct solo_dev *solo_dev = video_drvdata(file);
386
387         strcpy(cap->driver, SOLO6X10_NAME);
388         strcpy(cap->card, "Softlogic 6x10");
389         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
390                  pci_name(solo_dev->pdev));
391         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
392                         V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
393         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
394         return 0;
395 }
396
397 static int solo_enum_ext_input(struct solo_dev *solo_dev,
398                                struct v4l2_input *input)
399 {
400         static const char * const dispnames_1[] = { "4UP" };
401         static const char * const dispnames_2[] = { "4UP-1", "4UP-2" };
402         static const char * const dispnames_5[] = {
403                 "4UP-1", "4UP-2", "4UP-3", "4UP-4", "16UP"
404         };
405         const char * const *dispnames;
406
407         if (input->index >= (solo_dev->nr_chans + solo_dev->nr_ext))
408                 return -EINVAL;
409
410         if (solo_dev->nr_ext == 5)
411                 dispnames = dispnames_5;
412         else if (solo_dev->nr_ext == 2)
413                 dispnames = dispnames_2;
414         else
415                 dispnames = dispnames_1;
416
417         snprintf(input->name, sizeof(input->name), "Multi %s",
418                  dispnames[input->index - solo_dev->nr_chans]);
419
420         return 0;
421 }
422
423 static int solo_enum_input(struct file *file, void *priv,
424                            struct v4l2_input *input)
425 {
426         struct solo_dev *solo_dev = video_drvdata(file);
427
428         if (input->index >= solo_dev->nr_chans) {
429                 int ret = solo_enum_ext_input(solo_dev, input);
430
431                 if (ret < 0)
432                         return ret;
433         } else {
434                 snprintf(input->name, sizeof(input->name), "Camera %d",
435                          input->index + 1);
436
437                 /* We can only check this for normal inputs */
438                 if (!tw28_get_video_status(solo_dev, input->index))
439                         input->status = V4L2_IN_ST_NO_SIGNAL;
440         }
441
442         input->type = V4L2_INPUT_TYPE_CAMERA;
443         input->std = solo_dev->vfd->tvnorms;
444         return 0;
445 }
446
447 static int solo_set_input(struct file *file, void *priv, unsigned int index)
448 {
449         struct solo_dev *solo_dev = video_drvdata(file);
450         int ret = solo_v4l2_set_ch(solo_dev, index);
451
452         if (!ret) {
453                 while (erase_off(solo_dev))
454                         /* Do nothing */;
455         }
456
457         return ret;
458 }
459
460 static int solo_get_input(struct file *file, void *priv, unsigned int *index)
461 {
462         struct solo_dev *solo_dev = video_drvdata(file);
463
464         *index = solo_dev->cur_disp_ch;
465
466         return 0;
467 }
468
469 static int solo_enum_fmt_cap(struct file *file, void *priv,
470                              struct v4l2_fmtdesc *f)
471 {
472         if (f->index)
473                 return -EINVAL;
474
475         f->pixelformat = V4L2_PIX_FMT_UYVY;
476         strlcpy(f->description, "UYUV 4:2:2 Packed", sizeof(f->description));
477
478         return 0;
479 }
480
481 static int solo_try_fmt_cap(struct file *file, void *priv,
482                             struct v4l2_format *f)
483 {
484         struct solo_dev *solo_dev = video_drvdata(file);
485         struct v4l2_pix_format *pix = &f->fmt.pix;
486         int image_size = solo_image_size(solo_dev);
487
488         if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
489                 return -EINVAL;
490
491         pix->width = solo_dev->video_hsize;
492         pix->height = solo_vlines(solo_dev);
493         pix->sizeimage = image_size;
494         pix->field = V4L2_FIELD_INTERLACED;
495         pix->pixelformat = V4L2_PIX_FMT_UYVY;
496         pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
497         pix->priv = 0;
498         return 0;
499 }
500
501 static int solo_set_fmt_cap(struct file *file, void *priv,
502                             struct v4l2_format *f)
503 {
504         struct solo_dev *solo_dev = video_drvdata(file);
505
506         if (vb2_is_busy(&solo_dev->vidq))
507                 return -EBUSY;
508
509         /* For right now, if it doesn't match our running config,
510          * then fail */
511         return solo_try_fmt_cap(file, priv, f);
512 }
513
514 static int solo_get_fmt_cap(struct file *file, void *priv,
515                             struct v4l2_format *f)
516 {
517         struct solo_dev *solo_dev = video_drvdata(file);
518         struct v4l2_pix_format *pix = &f->fmt.pix;
519
520         pix->width = solo_dev->video_hsize;
521         pix->height = solo_vlines(solo_dev);
522         pix->pixelformat = V4L2_PIX_FMT_UYVY;
523         pix->field = V4L2_FIELD_INTERLACED;
524         pix->sizeimage = solo_image_size(solo_dev);
525         pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
526         pix->bytesperline = solo_bytesperline(solo_dev);
527         pix->priv = 0;
528
529         return 0;
530 }
531
532 static int solo_g_std(struct file *file, void *priv, v4l2_std_id *i)
533 {
534         struct solo_dev *solo_dev = video_drvdata(file);
535
536         if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
537                 *i = V4L2_STD_NTSC_M;
538         else
539                 *i = V4L2_STD_PAL;
540         return 0;
541 }
542
543 int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz)
544 {
545         int i;
546
547         /* Make sure all video nodes are idle */
548         if (vb2_is_busy(&solo_dev->vidq))
549                 return -EBUSY;
550         for (i = 0; i < solo_dev->nr_chans; i++)
551                 if (vb2_is_busy(&solo_dev->v4l2_enc[i]->vidq))
552                         return -EBUSY;
553         solo_dev->video_type = is_50hz ? SOLO_VO_FMT_TYPE_PAL :
554                                          SOLO_VO_FMT_TYPE_NTSC;
555         /* Reconfigure for the new standard */
556         solo_disp_init(solo_dev);
557         solo_enc_init(solo_dev);
558         solo_tw28_init(solo_dev);
559         for (i = 0; i < solo_dev->nr_chans; i++)
560                 solo_update_mode(solo_dev->v4l2_enc[i]);
561         return solo_v4l2_set_ch(solo_dev, solo_dev->cur_disp_ch);
562 }
563
564 static int solo_s_std(struct file *file, void *priv, v4l2_std_id std)
565 {
566         struct solo_dev *solo_dev = video_drvdata(file);
567
568         return solo_set_video_type(solo_dev, std & V4L2_STD_625_50);
569 }
570
571 static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
572 {
573         struct solo_dev *solo_dev =
574                 container_of(ctrl->handler, struct solo_dev, disp_hdl);
575
576         switch (ctrl->id) {
577         case V4L2_CID_MOTION_TRACE:
578                 if (ctrl->val) {
579                         solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER,
580                                         SOLO_VI_MOTION_Y_ADD |
581                                         SOLO_VI_MOTION_Y_VALUE(0x20) |
582                                         SOLO_VI_MOTION_CB_VALUE(0x10) |
583                                         SOLO_VI_MOTION_CR_VALUE(0x10));
584                         solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR,
585                                         SOLO_VI_MOTION_CR_ADD |
586                                         SOLO_VI_MOTION_Y_VALUE(0x10) |
587                                         SOLO_VI_MOTION_CB_VALUE(0x80) |
588                                         SOLO_VI_MOTION_CR_VALUE(0x10));
589                 } else {
590                         solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
591                         solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
592                 }
593                 return 0;
594         default:
595                 break;
596         }
597         return -EINVAL;
598 }
599
600 static const struct v4l2_file_operations solo_v4l2_fops = {
601         .owner                  = THIS_MODULE,
602         .open                   = v4l2_fh_open,
603         .release                = vb2_fop_release,
604         .read                   = vb2_fop_read,
605         .poll                   = vb2_fop_poll,
606         .mmap                   = vb2_fop_mmap,
607         .unlocked_ioctl         = video_ioctl2,
608 };
609
610 static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = {
611         .vidioc_querycap                = solo_querycap,
612         .vidioc_s_std                   = solo_s_std,
613         .vidioc_g_std                   = solo_g_std,
614         /* Input callbacks */
615         .vidioc_enum_input              = solo_enum_input,
616         .vidioc_s_input                 = solo_set_input,
617         .vidioc_g_input                 = solo_get_input,
618         /* Video capture format callbacks */
619         .vidioc_enum_fmt_vid_cap        = solo_enum_fmt_cap,
620         .vidioc_try_fmt_vid_cap         = solo_try_fmt_cap,
621         .vidioc_s_fmt_vid_cap           = solo_set_fmt_cap,
622         .vidioc_g_fmt_vid_cap           = solo_get_fmt_cap,
623         /* Streaming I/O */
624         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
625         .vidioc_querybuf                = vb2_ioctl_querybuf,
626         .vidioc_qbuf                    = vb2_ioctl_qbuf,
627         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
628         .vidioc_streamon                = vb2_ioctl_streamon,
629         .vidioc_streamoff               = vb2_ioctl_streamoff,
630         /* Logging and events */
631         .vidioc_log_status              = v4l2_ctrl_log_status,
632         .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
633         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
634 };
635
636 static struct video_device solo_v4l2_template = {
637         .name                   = SOLO6X10_NAME,
638         .fops                   = &solo_v4l2_fops,
639         .ioctl_ops              = &solo_v4l2_ioctl_ops,
640         .minor                  = -1,
641         .release                = video_device_release,
642         .tvnorms                = V4L2_STD_NTSC_M | V4L2_STD_PAL,
643 };
644
645 static const struct v4l2_ctrl_ops solo_ctrl_ops = {
646         .s_ctrl = solo_s_ctrl,
647 };
648
649 static const struct v4l2_ctrl_config solo_motion_trace_ctrl = {
650         .ops = &solo_ctrl_ops,
651         .id = V4L2_CID_MOTION_TRACE,
652         .name = "Motion Detection Trace",
653         .type = V4L2_CTRL_TYPE_BOOLEAN,
654         .max = 1,
655         .step = 1,
656 };
657
658 int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
659 {
660         int ret;
661         int i;
662
663         init_waitqueue_head(&solo_dev->disp_thread_wait);
664         spin_lock_init(&solo_dev->slock);
665         mutex_init(&solo_dev->lock);
666         INIT_LIST_HEAD(&solo_dev->vidq_active);
667
668         solo_dev->vfd = video_device_alloc();
669         if (!solo_dev->vfd)
670                 return -ENOMEM;
671
672         *solo_dev->vfd = solo_v4l2_template;
673         solo_dev->vfd->v4l2_dev = &solo_dev->v4l2_dev;
674         solo_dev->vfd->queue = &solo_dev->vidq;
675         solo_dev->vfd->lock = &solo_dev->lock;
676         v4l2_ctrl_handler_init(&solo_dev->disp_hdl, 1);
677         v4l2_ctrl_new_custom(&solo_dev->disp_hdl, &solo_motion_trace_ctrl, NULL);
678         if (solo_dev->disp_hdl.error) {
679                 ret = solo_dev->disp_hdl.error;
680                 goto fail;
681         }
682         solo_dev->vfd->ctrl_handler = &solo_dev->disp_hdl;
683
684         video_set_drvdata(solo_dev->vfd, solo_dev);
685
686         solo_dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
687         solo_dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
688         solo_dev->vidq.ops = &solo_video_qops;
689         solo_dev->vidq.mem_ops = &vb2_dma_contig_memops;
690         solo_dev->vidq.drv_priv = solo_dev;
691         solo_dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
692         solo_dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
693         solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
694         solo_dev->vidq.lock = &solo_dev->lock;
695         ret = vb2_queue_init(&solo_dev->vidq);
696         if (ret < 0)
697                 goto fail;
698
699         solo_dev->alloc_ctx = vb2_dma_contig_init_ctx(&solo_dev->pdev->dev);
700         if (IS_ERR(solo_dev->alloc_ctx)) {
701                 dev_err(&solo_dev->pdev->dev, "Can't allocate buffer context");
702                 return PTR_ERR(solo_dev->alloc_ctx);
703         }
704
705         /* Cycle all the channels and clear */
706         for (i = 0; i < solo_dev->nr_chans; i++) {
707                 solo_v4l2_set_ch(solo_dev, i);
708                 while (erase_off(solo_dev))
709                         /* Do nothing */;
710         }
711
712         /* Set the default display channel */
713         solo_v4l2_set_ch(solo_dev, 0);
714         while (erase_off(solo_dev))
715                 /* Do nothing */;
716
717         ret = video_register_device(solo_dev->vfd, VFL_TYPE_GRABBER, nr);
718         if (ret < 0)
719                 goto fail;
720
721         snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)",
722                  SOLO6X10_NAME, solo_dev->vfd->num);
723
724         dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with "
725                  "%d inputs (%d extended)\n", solo_dev->vfd->num,
726                  solo_dev->nr_chans, solo_dev->nr_ext);
727
728         return 0;
729
730 fail:
731         video_device_release(solo_dev->vfd);
732         vb2_dma_contig_cleanup_ctx(solo_dev->alloc_ctx);
733         v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
734         solo_dev->vfd = NULL;
735         return ret;
736 }
737
738 void solo_v4l2_exit(struct solo_dev *solo_dev)
739 {
740         if (solo_dev->vfd == NULL)
741                 return;
742
743         video_unregister_device(solo_dev->vfd);
744         vb2_dma_contig_cleanup_ctx(solo_dev->alloc_ctx);
745         v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
746         solo_dev->vfd = NULL;
747 }