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