xref: /linux/drivers/media/usb/cx231xx/cx231xx-video.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3    cx231xx-video.c - driver for Conexant Cx23100/101/102
4 		     USB video capture devices
5 
6    Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
7 	Based on em28xx driver
8 	Based on cx23885 driver
9 	Based on cx88 driver
10 
11  */
12 
13 #include "cx231xx.h"
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/bitmap.h>
19 #include <linux/i2c.h>
20 #include <linux/mm.h>
21 #include <linux/mutex.h>
22 #include <linux/slab.h>
23 
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-event.h>
27 #include <media/drv-intf/msp3400.h>
28 #include <media/tuner.h>
29 
30 #include <media/dvb_frontend.h>
31 
32 #include "cx231xx-vbi.h"
33 
34 #define CX231XX_VERSION "0.0.3"
35 
36 #define DRIVER_AUTHOR   "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
37 #define DRIVER_DESC     "Conexant cx231xx based USB video device driver"
38 
39 #define cx231xx_videodbg(fmt, arg...) do {\
40 	if (video_debug) \
41 		printk(KERN_INFO "%s %s :"fmt, \
42 			 dev->name, __func__ , ##arg); } while (0)
43 
44 static unsigned int isoc_debug;
45 module_param(isoc_debug, int, 0644);
46 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
47 
48 #define cx231xx_isocdbg(fmt, arg...) \
49 do {\
50 	if (isoc_debug) { \
51 		printk(KERN_INFO "%s %s :"fmt, \
52 			 dev->name, __func__ , ##arg); \
53 	} \
54   } while (0)
55 
56 MODULE_AUTHOR(DRIVER_AUTHOR);
57 MODULE_DESCRIPTION(DRIVER_DESC);
58 MODULE_LICENSE("GPL");
59 MODULE_VERSION(CX231XX_VERSION);
60 
61 static unsigned int card[]     = {[0 ... (CX231XX_MAXBOARDS - 1)] = -1U };
62 static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = -1U };
63 static unsigned int vbi_nr[]   = {[0 ... (CX231XX_MAXBOARDS - 1)] = -1U };
64 static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = -1U };
65 
66 module_param_array(card, int, NULL, 0444);
67 module_param_array(video_nr, int, NULL, 0444);
68 module_param_array(vbi_nr, int, NULL, 0444);
69 module_param_array(radio_nr, int, NULL, 0444);
70 
71 MODULE_PARM_DESC(card, "card type");
72 MODULE_PARM_DESC(video_nr, "video device numbers");
73 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
74 MODULE_PARM_DESC(radio_nr, "radio device numbers");
75 
76 static unsigned int video_debug;
77 module_param(video_debug, int, 0644);
78 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
79 
80 /* supported video standards */
81 static struct cx231xx_fmt format[] = {
82 	{
83 	 .fourcc = V4L2_PIX_FMT_YUYV,
84 	 .depth = 16,
85 	 .reg = 0,
86 	 },
87 };
88 
89 
90 static int cx231xx_enable_analog_tuner(struct cx231xx *dev)
91 {
92 #ifdef CONFIG_MEDIA_CONTROLLER
93 	struct media_device *mdev = dev->media_dev;
94 	struct media_entity  *entity, *decoder = NULL, *source;
95 	struct media_link *link, *found_link = NULL;
96 	int ret, active_links = 0;
97 
98 	if (!mdev)
99 		return 0;
100 
101 	/*
102 	 * This will find the tuner that is connected into the decoder.
103 	 * Technically, this is not 100% correct, as the device may be
104 	 * using an analog input instead of the tuner. However, as we can't
105 	 * do DVB streaming while the DMA engine is being used for V4L2,
106 	 * this should be enough for the actual needs.
107 	 */
108 	media_device_for_each_entity(entity, mdev) {
109 		if (entity->function == MEDIA_ENT_F_ATV_DECODER) {
110 			decoder = entity;
111 			break;
112 		}
113 	}
114 	if (!decoder)
115 		return 0;
116 
117 	list_for_each_entry(link, &decoder->links, list) {
118 		if (link->sink->entity == decoder) {
119 			found_link = link;
120 			if (link->flags & MEDIA_LNK_FL_ENABLED)
121 				active_links++;
122 			break;
123 		}
124 	}
125 
126 	if (active_links == 1 || !found_link)
127 		return 0;
128 
129 	source = found_link->source->entity;
130 	list_for_each_entry(link, &source->links, list) {
131 		struct media_entity *sink;
132 		int flags = 0;
133 
134 		sink = link->sink->entity;
135 
136 		if (sink == entity)
137 			flags = MEDIA_LNK_FL_ENABLED;
138 
139 		ret = media_entity_setup_link(link, flags);
140 		if (ret) {
141 			dev_err(dev->dev,
142 				"Couldn't change link %s->%s to %s. Error %d\n",
143 				source->name, sink->name,
144 				flags ? "enabled" : "disabled",
145 				ret);
146 			return ret;
147 		} else
148 			dev_dbg(dev->dev,
149 				"link %s->%s was %s\n",
150 				source->name, sink->name,
151 				flags ? "ENABLED" : "disabled");
152 	}
153 #endif
154 	return 0;
155 }
156 
157 /* ------------------------------------------------------------------
158 	Video buffer and parser functions
159    ------------------------------------------------------------------*/
160 
161 /*
162  * Announces that a buffer were filled and request the next
163  */
164 static inline void buffer_filled(struct cx231xx *dev,
165 				 struct cx231xx_dmaqueue *dma_q,
166 				 struct cx231xx_buffer *buf)
167 {
168 	/* Advice that buffer was filled */
169 	cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.vb2_buf.index);
170 	buf->vb.sequence = dma_q->sequence++;
171 	buf->vb.field = V4L2_FIELD_INTERLACED;
172 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
173 	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, dev->size);
174 
175 	if (dev->USE_ISO)
176 		dev->video_mode.isoc_ctl.buf = NULL;
177 	else
178 		dev->video_mode.bulk_ctl.buf = NULL;
179 
180 	list_del(&buf->list);
181 	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
182 }
183 
184 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
185 {
186 	char *errmsg = "Unknown";
187 
188 	switch (status) {
189 	case -ENOENT:
190 		errmsg = "unlinked synchronously";
191 		break;
192 	case -ECONNRESET:
193 		errmsg = "unlinked asynchronously";
194 		break;
195 	case -ENOSR:
196 		errmsg = "Buffer error (overrun)";
197 		break;
198 	case -EPIPE:
199 		errmsg = "Stalled (device not responding)";
200 		break;
201 	case -EOVERFLOW:
202 		errmsg = "Babble (bad cable?)";
203 		break;
204 	case -EPROTO:
205 		errmsg = "Bit-stuff error (bad cable?)";
206 		break;
207 	case -EILSEQ:
208 		errmsg = "CRC/Timeout (could be anything)";
209 		break;
210 	case -ETIME:
211 		errmsg = "Device does not respond";
212 		break;
213 	}
214 	if (packet < 0) {
215 		cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
216 	} else {
217 		cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
218 				packet, status, errmsg);
219 	}
220 }
221 
222 /*
223  * generic routine to get the next available buffer
224  */
225 static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
226 				struct cx231xx_buffer **buf)
227 {
228 	struct cx231xx_video_mode *vmode =
229 	    container_of(dma_q, struct cx231xx_video_mode, vidq);
230 	struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
231 
232 	char *outp;
233 
234 	if (list_empty(&dma_q->active)) {
235 		cx231xx_isocdbg("No active queue to serve\n");
236 		if (dev->USE_ISO)
237 			dev->video_mode.isoc_ctl.buf = NULL;
238 		else
239 			dev->video_mode.bulk_ctl.buf = NULL;
240 		*buf = NULL;
241 		return;
242 	}
243 
244 	/* Get the next buffer */
245 	*buf = list_entry(dma_q->active.next, struct cx231xx_buffer, list);
246 
247 	/* Cleans up buffer - Useful for testing for frame/URB loss */
248 	outp = vb2_plane_vaddr(&(*buf)->vb.vb2_buf, 0);
249 	memset(outp, 0, dev->size);
250 
251 	if (dev->USE_ISO)
252 		dev->video_mode.isoc_ctl.buf = *buf;
253 	else
254 		dev->video_mode.bulk_ctl.buf = *buf;
255 
256 	return;
257 }
258 
259 /*
260  * Controls the isoc copy of each urb packet
261  */
262 static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
263 {
264 	struct cx231xx_dmaqueue *dma_q = urb->context;
265 	int i;
266 	unsigned char *p_buffer;
267 	u32 bytes_parsed = 0, buffer_size = 0;
268 	u8 sav_eav = 0;
269 
270 	if (!dev)
271 		return 0;
272 
273 	if (dev->state & DEV_DISCONNECTED)
274 		return 0;
275 
276 	if (urb->status < 0) {
277 		print_err_status(dev, -1, urb->status);
278 		if (urb->status == -ENOENT)
279 			return 0;
280 	}
281 
282 	for (i = 0; i < urb->number_of_packets; i++) {
283 		int status = urb->iso_frame_desc[i].status;
284 
285 		if (status < 0) {
286 			print_err_status(dev, i, status);
287 			if (urb->iso_frame_desc[i].status != -EPROTO)
288 				continue;
289 		}
290 
291 		if (urb->iso_frame_desc[i].actual_length <= 0) {
292 			/* cx231xx_isocdbg("packet %d is empty",i); - spammy */
293 			continue;
294 		}
295 		if (urb->iso_frame_desc[i].actual_length >
296 		    dev->video_mode.max_pkt_size) {
297 			cx231xx_isocdbg("packet bigger than packet size");
298 			continue;
299 		}
300 
301 		/*  get buffer pointer and length */
302 		p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
303 		buffer_size = urb->iso_frame_desc[i].actual_length;
304 		bytes_parsed = 0;
305 
306 		if (dma_q->is_partial_line) {
307 			/* Handle the case of a partial line */
308 			sav_eav = dma_q->last_sav;
309 		} else {
310 			/* Check for a SAV/EAV overlapping
311 				the buffer boundary */
312 			sav_eav =
313 			    cx231xx_find_boundary_SAV_EAV(p_buffer,
314 							  dma_q->partial_buf,
315 							  &bytes_parsed);
316 		}
317 
318 		sav_eav &= 0xF0;
319 		/* Get the first line if we have some portion of an SAV/EAV from
320 		   the last buffer or a partial line  */
321 		if (sav_eav) {
322 			bytes_parsed += cx231xx_get_video_line(dev, dma_q,
323 				sav_eav,	/* SAV/EAV */
324 				p_buffer + bytes_parsed,	/* p_buffer */
325 				buffer_size - bytes_parsed);/* buf size */
326 		}
327 
328 		/* Now parse data that is completely in this buffer */
329 		/* dma_q->is_partial_line = 0;  */
330 
331 		while (bytes_parsed < buffer_size) {
332 			u32 bytes_used = 0;
333 
334 			sav_eav = cx231xx_find_next_SAV_EAV(
335 				p_buffer + bytes_parsed,	/* p_buffer */
336 				buffer_size - bytes_parsed,	/* buf size */
337 				&bytes_used);/* bytes used to get SAV/EAV */
338 
339 			bytes_parsed += bytes_used;
340 
341 			sav_eav &= 0xF0;
342 			if (sav_eav && (bytes_parsed < buffer_size)) {
343 				bytes_parsed += cx231xx_get_video_line(dev,
344 					dma_q, sav_eav,	/* SAV/EAV */
345 					p_buffer + bytes_parsed,/* p_buffer */
346 					buffer_size - bytes_parsed);/*buf size*/
347 			}
348 		}
349 
350 		/* Save the last four bytes of the buffer so we can check the
351 		   buffer boundary condition next time */
352 		memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
353 		bytes_parsed = 0;
354 
355 	}
356 	return 1;
357 }
358 
359 static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
360 {
361 	struct cx231xx_dmaqueue *dma_q = urb->context;
362 	unsigned char *p_buffer;
363 	u32 bytes_parsed = 0, buffer_size = 0;
364 	u8 sav_eav = 0;
365 
366 	if (!dev)
367 		return 0;
368 
369 	if (dev->state & DEV_DISCONNECTED)
370 		return 0;
371 
372 	if (urb->status < 0) {
373 		print_err_status(dev, -1, urb->status);
374 		if (urb->status == -ENOENT)
375 			return 0;
376 	}
377 
378 	if (1) {
379 
380 		/*  get buffer pointer and length */
381 		p_buffer = urb->transfer_buffer;
382 		buffer_size = urb->actual_length;
383 		bytes_parsed = 0;
384 
385 		if (dma_q->is_partial_line) {
386 			/* Handle the case of a partial line */
387 			sav_eav = dma_q->last_sav;
388 		} else {
389 			/* Check for a SAV/EAV overlapping
390 				the buffer boundary */
391 			sav_eav =
392 			    cx231xx_find_boundary_SAV_EAV(p_buffer,
393 							  dma_q->partial_buf,
394 							  &bytes_parsed);
395 		}
396 
397 		sav_eav &= 0xF0;
398 		/* Get the first line if we have some portion of an SAV/EAV from
399 		   the last buffer or a partial line  */
400 		if (sav_eav) {
401 			bytes_parsed += cx231xx_get_video_line(dev, dma_q,
402 				sav_eav,	/* SAV/EAV */
403 				p_buffer + bytes_parsed,	/* p_buffer */
404 				buffer_size - bytes_parsed);/* buf size */
405 		}
406 
407 		/* Now parse data that is completely in this buffer */
408 		/* dma_q->is_partial_line = 0;  */
409 
410 		while (bytes_parsed < buffer_size) {
411 			u32 bytes_used = 0;
412 
413 			sav_eav = cx231xx_find_next_SAV_EAV(
414 				p_buffer + bytes_parsed,	/* p_buffer */
415 				buffer_size - bytes_parsed,	/* buf size */
416 				&bytes_used);/* bytes used to get SAV/EAV */
417 
418 			bytes_parsed += bytes_used;
419 
420 			sav_eav &= 0xF0;
421 			if (sav_eav && (bytes_parsed < buffer_size)) {
422 				bytes_parsed += cx231xx_get_video_line(dev,
423 					dma_q, sav_eav,	/* SAV/EAV */
424 					p_buffer + bytes_parsed,/* p_buffer */
425 					buffer_size - bytes_parsed);/*buf size*/
426 			}
427 		}
428 
429 		/* Save the last four bytes of the buffer so we can check the
430 		   buffer boundary condition next time */
431 		memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
432 		bytes_parsed = 0;
433 
434 	}
435 	return 1;
436 }
437 
438 
439 u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
440 				 u32 *p_bytes_used)
441 {
442 	u32 bytes_used;
443 	u8 boundary_bytes[8];
444 	u8 sav_eav = 0;
445 
446 	*p_bytes_used = 0;
447 
448 	/* Create an array of the last 4 bytes of the last buffer and the first
449 	   4 bytes of the current buffer. */
450 
451 	memcpy(boundary_bytes, partial_buf, 4);
452 	memcpy(boundary_bytes + 4, p_buffer, 4);
453 
454 	/* Check for the SAV/EAV in the boundary buffer */
455 	sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
456 					    &bytes_used);
457 
458 	if (sav_eav) {
459 		/* found a boundary SAV/EAV.  Updates the bytes used to reflect
460 		   only those used in the new buffer */
461 		*p_bytes_used = bytes_used - 4;
462 	}
463 
464 	return sav_eav;
465 }
466 
467 u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
468 {
469 	u32 i;
470 	u8 sav_eav = 0;
471 
472 	/*
473 	 * Don't search if the buffer size is less than 4.  It causes a page
474 	 * fault since buffer_size - 4 evaluates to a large number in that
475 	 * case.
476 	 */
477 	if (buffer_size < 4) {
478 		*p_bytes_used = buffer_size;
479 		return 0;
480 	}
481 
482 	for (i = 0; i < (buffer_size - 3); i++) {
483 
484 		if ((p_buffer[i] == 0xFF) &&
485 		    (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
486 
487 			*p_bytes_used = i + 4;
488 			sav_eav = p_buffer[i + 3];
489 			return sav_eav;
490 		}
491 	}
492 
493 	*p_bytes_used = buffer_size;
494 	return 0;
495 }
496 
497 u32 cx231xx_get_video_line(struct cx231xx *dev,
498 			   struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
499 			   u8 *p_buffer, u32 buffer_size)
500 {
501 	u32 bytes_copied = 0;
502 	int current_field = -1;
503 
504 	switch (sav_eav) {
505 	case SAV_ACTIVE_VIDEO_FIELD1:
506 		/* looking for skipped line which occurred in PAL 720x480 mode.
507 		   In this case, there will be no active data contained
508 		   between the SAV and EAV */
509 		if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
510 		    (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
511 		    ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
512 		     (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
513 		     (p_buffer[3] == EAV_VBLANK_FIELD1) ||
514 		     (p_buffer[3] == EAV_VBLANK_FIELD2)))
515 			return bytes_copied;
516 		current_field = 1;
517 		break;
518 
519 	case SAV_ACTIVE_VIDEO_FIELD2:
520 		/* looking for skipped line which occurred in PAL 720x480 mode.
521 		   In this case, there will be no active data contained between
522 		   the SAV and EAV */
523 		if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
524 		    (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
525 		    ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
526 		     (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
527 		     (p_buffer[3] == EAV_VBLANK_FIELD1)       ||
528 		     (p_buffer[3] == EAV_VBLANK_FIELD2)))
529 			return bytes_copied;
530 		current_field = 2;
531 		break;
532 	}
533 
534 	dma_q->last_sav = sav_eav;
535 
536 	bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
537 					       buffer_size, current_field);
538 
539 	return bytes_copied;
540 }
541 
542 u32 cx231xx_copy_video_line(struct cx231xx *dev,
543 			    struct cx231xx_dmaqueue *dma_q, u8 *p_line,
544 			    u32 length, int field_number)
545 {
546 	u32 bytes_to_copy;
547 	struct cx231xx_buffer *buf;
548 	u32 _line_size = dev->width * 2;
549 
550 	if (dma_q->current_field != field_number)
551 		cx231xx_reset_video_buffer(dev, dma_q);
552 
553 	/* get the buffer pointer */
554 	if (dev->USE_ISO)
555 		buf = dev->video_mode.isoc_ctl.buf;
556 	else
557 		buf = dev->video_mode.bulk_ctl.buf;
558 
559 	/* Remember the field number for next time */
560 	dma_q->current_field = field_number;
561 
562 	bytes_to_copy = dma_q->bytes_left_in_line;
563 	if (bytes_to_copy > length)
564 		bytes_to_copy = length;
565 
566 	if (dma_q->lines_completed >= dma_q->lines_per_field) {
567 		dma_q->bytes_left_in_line -= bytes_to_copy;
568 		dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
569 					  0 : 1;
570 		return 0;
571 	}
572 
573 	dma_q->is_partial_line = 1;
574 
575 	/* If we don't have a buffer, just return the number of bytes we would
576 	   have copied if we had a buffer. */
577 	if (!buf) {
578 		dma_q->bytes_left_in_line -= bytes_to_copy;
579 		dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
580 					 ? 0 : 1;
581 		return bytes_to_copy;
582 	}
583 
584 	/* copy the data to video buffer */
585 	cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
586 
587 	dma_q->pos += bytes_to_copy;
588 	dma_q->bytes_left_in_line -= bytes_to_copy;
589 
590 	if (dma_q->bytes_left_in_line == 0) {
591 		dma_q->bytes_left_in_line = _line_size;
592 		dma_q->lines_completed++;
593 		dma_q->is_partial_line = 0;
594 
595 		if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
596 			buffer_filled(dev, dma_q, buf);
597 
598 			dma_q->pos = 0;
599 			buf = NULL;
600 			dma_q->lines_completed = 0;
601 		}
602 	}
603 
604 	return bytes_to_copy;
605 }
606 
607 void cx231xx_reset_video_buffer(struct cx231xx *dev,
608 				struct cx231xx_dmaqueue *dma_q)
609 {
610 	struct cx231xx_buffer *buf;
611 
612 	/* handle the switch from field 1 to field 2 */
613 	if (dma_q->current_field == 1) {
614 		if (dma_q->lines_completed >= dma_q->lines_per_field)
615 			dma_q->field1_done = 1;
616 		else
617 			dma_q->field1_done = 0;
618 	}
619 
620 	if (dev->USE_ISO)
621 		buf = dev->video_mode.isoc_ctl.buf;
622 	else
623 		buf = dev->video_mode.bulk_ctl.buf;
624 
625 	if (buf == NULL) {
626 		/* first try to get the buffer */
627 		get_next_buf(dma_q, &buf);
628 
629 		dma_q->pos = 0;
630 		dma_q->field1_done = 0;
631 		dma_q->current_field = -1;
632 	}
633 
634 	/* reset the counters */
635 	dma_q->bytes_left_in_line = dev->width << 1;
636 	dma_q->lines_completed = 0;
637 }
638 
639 int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
640 		    u8 *p_buffer, u32 bytes_to_copy)
641 {
642 	u8 *p_out_buffer = NULL;
643 	u32 current_line_bytes_copied = 0;
644 	struct cx231xx_buffer *buf;
645 	u32 _line_size = dev->width << 1;
646 	void *startwrite;
647 	int offset, lencopy;
648 
649 	if (dev->USE_ISO)
650 		buf = dev->video_mode.isoc_ctl.buf;
651 	else
652 		buf = dev->video_mode.bulk_ctl.buf;
653 
654 	if (buf == NULL)
655 		return -1;
656 
657 	p_out_buffer = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
658 
659 	current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
660 
661 	/* Offset field 2 one line from the top of the buffer */
662 	offset = (dma_q->current_field == 1) ? 0 : _line_size;
663 
664 	/* Offset for field 2 */
665 	startwrite = p_out_buffer + offset;
666 
667 	/* lines already completed in the current field */
668 	startwrite += (dma_q->lines_completed * _line_size * 2);
669 
670 	/* bytes already completed in the current line */
671 	startwrite += current_line_bytes_copied;
672 
673 	lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
674 		  bytes_to_copy : dma_q->bytes_left_in_line;
675 
676 	if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + dev->size))
677 		return 0;
678 
679 	/* The below copies the UYVY data straight into video buffer */
680 	cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
681 
682 	return 0;
683 }
684 
685 void cx231xx_swab(u16 *from, u16 *to, u16 len)
686 {
687 	u16 i;
688 
689 	if (len <= 0)
690 		return;
691 
692 	for (i = 0; i < len / 2; i++)
693 		to[i] = (from[i] << 8) | (from[i] >> 8);
694 }
695 
696 u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
697 {
698 	u8 buffer_complete = 0;
699 
700 	/* Dual field stream */
701 	buffer_complete = ((dma_q->current_field == 2) &&
702 			   (dma_q->lines_completed >= dma_q->lines_per_field) &&
703 			    dma_q->field1_done);
704 
705 	return buffer_complete;
706 }
707 
708 /* ------------------------------------------------------------------
709 	Videobuf operations
710    ------------------------------------------------------------------*/
711 
712 static int queue_setup(struct vb2_queue *vq,
713 		       unsigned int *nbuffers, unsigned int *nplanes,
714 		       unsigned int sizes[], struct device *alloc_devs[])
715 {
716 	struct cx231xx *dev = vb2_get_drv_priv(vq);
717 	unsigned int q_num_bufs = vb2_get_num_buffers(vq);
718 
719 	dev->size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
720 
721 	if (q_num_bufs + *nbuffers < CX231XX_MIN_BUF)
722 		*nbuffers = CX231XX_MIN_BUF - q_num_bufs;
723 
724 	if (*nplanes)
725 		return sizes[0] < dev->size ? -EINVAL : 0;
726 	*nplanes = 1;
727 	sizes[0] = dev->size;
728 
729 	return 0;
730 }
731 
732 static void buffer_queue(struct vb2_buffer *vb)
733 {
734 	struct cx231xx_buffer *buf =
735 	    container_of(vb, struct cx231xx_buffer, vb.vb2_buf);
736 	struct cx231xx *dev = vb2_get_drv_priv(vb->vb2_queue);
737 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
738 	unsigned long flags;
739 
740 	spin_lock_irqsave(&dev->video_mode.slock, flags);
741 	list_add_tail(&buf->list, &vidq->active);
742 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
743 }
744 
745 static void return_all_buffers(struct cx231xx *dev,
746 			       enum vb2_buffer_state state)
747 {
748 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
749 	struct cx231xx_buffer *buf, *node;
750 	unsigned long flags;
751 
752 	spin_lock_irqsave(&dev->video_mode.slock, flags);
753 	if (dev->USE_ISO)
754 		dev->video_mode.isoc_ctl.buf = NULL;
755 	else
756 		dev->video_mode.bulk_ctl.buf = NULL;
757 	list_for_each_entry_safe(buf, node, &vidq->active, list) {
758 		list_del(&buf->list);
759 		vb2_buffer_done(&buf->vb.vb2_buf, state);
760 	}
761 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
762 }
763 
764 static int start_streaming(struct vb2_queue *vq, unsigned int count)
765 {
766 	struct cx231xx *dev = vb2_get_drv_priv(vq);
767 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
768 	int ret = 0;
769 
770 	vidq->sequence = 0;
771 	dev->mode_tv = 0;
772 
773 	cx231xx_enable_analog_tuner(dev);
774 	if (dev->USE_ISO)
775 		ret = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
776 					CX231XX_NUM_BUFS,
777 					dev->video_mode.max_pkt_size,
778 					cx231xx_isoc_copy);
779 	else
780 		ret = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
781 					CX231XX_NUM_BUFS,
782 					dev->video_mode.max_pkt_size,
783 					cx231xx_bulk_copy);
784 	if (ret)
785 		return_all_buffers(dev, VB2_BUF_STATE_QUEUED);
786 	call_all(dev, video, s_stream, 1);
787 	return ret;
788 }
789 
790 static void stop_streaming(struct vb2_queue *vq)
791 {
792 	struct cx231xx *dev = vb2_get_drv_priv(vq);
793 
794 	call_all(dev, video, s_stream, 0);
795 	return_all_buffers(dev, VB2_BUF_STATE_ERROR);
796 }
797 
798 static const struct vb2_ops cx231xx_video_qops = {
799 	.queue_setup		= queue_setup,
800 	.buf_queue		= buffer_queue,
801 	.start_streaming	= start_streaming,
802 	.stop_streaming		= stop_streaming,
803 };
804 
805 /*********************  v4l2 interface  **************************************/
806 
807 void video_mux(struct cx231xx *dev, int index)
808 {
809 	dev->video_input = index;
810 	dev->ctl_ainput = INPUT(index)->amux;
811 
812 	cx231xx_set_video_input_mux(dev, index);
813 
814 	cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
815 
816 	cx231xx_set_audio_input(dev, dev->ctl_ainput);
817 
818 	dev_dbg(dev->dev, "video_mux : %d\n", index);
819 
820 	/* do mode control overrides if required */
821 	cx231xx_do_mode_ctrl_overrides(dev);
822 }
823 
824 /* ------------------------------------------------------------------
825 	IOCTL vidioc handling
826    ------------------------------------------------------------------*/
827 
828 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
829 				struct v4l2_format *f)
830 {
831 	struct cx231xx *dev = video_drvdata(file);
832 
833 	f->fmt.pix.width = dev->width;
834 	f->fmt.pix.height = dev->height;
835 	f->fmt.pix.pixelformat = dev->format->fourcc;
836 	f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
837 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
838 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
839 
840 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
841 
842 	return 0;
843 }
844 
845 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
846 {
847 	unsigned int i;
848 
849 	for (i = 0; i < ARRAY_SIZE(format); i++)
850 		if (format[i].fourcc == fourcc)
851 			return &format[i];
852 
853 	return NULL;
854 }
855 
856 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
857 				  struct v4l2_format *f)
858 {
859 	struct cx231xx *dev = video_drvdata(file);
860 	unsigned int width = f->fmt.pix.width;
861 	unsigned int height = f->fmt.pix.height;
862 	unsigned int maxw = norm_maxw(dev);
863 	unsigned int maxh = norm_maxh(dev);
864 	struct cx231xx_fmt *fmt;
865 
866 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
867 	if (!fmt) {
868 		cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
869 				 f->fmt.pix.pixelformat);
870 		return -EINVAL;
871 	}
872 
873 	/* width must even because of the YUYV format
874 	   height must be even because of interlacing */
875 	v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
876 
877 	f->fmt.pix.width = width;
878 	f->fmt.pix.height = height;
879 	f->fmt.pix.pixelformat = fmt->fourcc;
880 	f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
881 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
882 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
883 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
884 
885 	return 0;
886 }
887 
888 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
889 				struct v4l2_format *f)
890 {
891 	struct cx231xx *dev = video_drvdata(file);
892 	struct v4l2_subdev_format format = {
893 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
894 	};
895 	int rc;
896 
897 	rc = vidioc_try_fmt_vid_cap(file, priv, f);
898 	if (rc)
899 		return rc;
900 
901 	if (vb2_is_busy(&dev->vidq)) {
902 		dev_err(dev->dev, "%s: queue busy\n", __func__);
903 		return -EBUSY;
904 	}
905 
906 	/* set new image size */
907 	dev->width = f->fmt.pix.width;
908 	dev->height = f->fmt.pix.height;
909 	dev->format = format_by_fourcc(f->fmt.pix.pixelformat);
910 
911 	v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
912 	call_all(dev, pad, set_fmt, NULL, &format);
913 	v4l2_fill_pix_format(&f->fmt.pix, &format.format);
914 
915 	return rc;
916 }
917 
918 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
919 {
920 	struct cx231xx *dev = video_drvdata(file);
921 
922 	*id = dev->norm;
923 	return 0;
924 }
925 
926 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
927 {
928 	struct cx231xx *dev = video_drvdata(file);
929 	struct v4l2_subdev_format format = {
930 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
931 	};
932 
933 	if (dev->norm == norm)
934 		return 0;
935 
936 	if (vb2_is_busy(&dev->vidq))
937 		return -EBUSY;
938 
939 	dev->norm = norm;
940 
941 	/* Adjusts width/height, if needed */
942 	dev->width = 720;
943 	dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
944 
945 	call_all(dev, video, s_std, dev->norm);
946 
947 	/* We need to reset basic properties in the decoder related to
948 	   resolution (since a standard change effects things like the number
949 	   of lines in VACT, etc) */
950 	format.format.code = MEDIA_BUS_FMT_FIXED;
951 	format.format.width = dev->width;
952 	format.format.height = dev->height;
953 	call_all(dev, pad, set_fmt, NULL, &format);
954 
955 	/* do mode control overrides */
956 	cx231xx_do_mode_ctrl_overrides(dev);
957 
958 	return 0;
959 }
960 
961 static const char *iname[] = {
962 	[CX231XX_VMUX_COMPOSITE1] = "Composite1",
963 	[CX231XX_VMUX_SVIDEO]     = "S-Video",
964 	[CX231XX_VMUX_TELEVISION] = "Television",
965 	[CX231XX_VMUX_CABLE]      = "Cable TV",
966 	[CX231XX_VMUX_DVB]        = "DVB",
967 };
968 
969 void cx231xx_v4l2_create_entities(struct cx231xx *dev)
970 {
971 #if defined(CONFIG_MEDIA_CONTROLLER)
972 	int ret, i;
973 
974 	/* Create entities for each input connector */
975 	for (i = 0; i < MAX_CX231XX_INPUT; i++) {
976 		struct media_entity *ent = &dev->input_ent[i];
977 
978 		if (!INPUT(i)->type)
979 			break;
980 
981 		ent->name = iname[INPUT(i)->type];
982 		ent->flags = MEDIA_ENT_FL_CONNECTOR;
983 		dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
984 
985 		switch (INPUT(i)->type) {
986 		case CX231XX_VMUX_COMPOSITE1:
987 			ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
988 			break;
989 		case CX231XX_VMUX_SVIDEO:
990 			ent->function = MEDIA_ENT_F_CONN_SVIDEO;
991 			break;
992 		case CX231XX_VMUX_TELEVISION:
993 		case CX231XX_VMUX_CABLE:
994 		case CX231XX_VMUX_DVB:
995 			/* The DVB core will handle it */
996 			if (dev->tuner_type == TUNER_ABSENT)
997 				continue;
998 			fallthrough;
999 		default: /* just to shut up a gcc warning */
1000 			ent->function = MEDIA_ENT_F_CONN_RF;
1001 			break;
1002 		}
1003 
1004 		ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1005 		if (ret < 0)
1006 			pr_err("failed to initialize input pad[%d]!\n", i);
1007 
1008 		ret = media_device_register_entity(dev->media_dev, ent);
1009 		if (ret < 0)
1010 			pr_err("failed to register input entity %d!\n", i);
1011 	}
1012 #endif
1013 }
1014 
1015 int cx231xx_enum_input(struct file *file, void *priv,
1016 			     struct v4l2_input *i)
1017 {
1018 	struct cx231xx *dev = video_drvdata(file);
1019 	u32 gen_stat;
1020 	unsigned int n;
1021 	int ret;
1022 
1023 	n = i->index;
1024 	if (n >= MAX_CX231XX_INPUT)
1025 		return -EINVAL;
1026 	if (0 == INPUT(n)->type)
1027 		return -EINVAL;
1028 
1029 	i->index = n;
1030 	i->type = V4L2_INPUT_TYPE_CAMERA;
1031 
1032 	strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
1033 
1034 	if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1035 	    (CX231XX_VMUX_CABLE == INPUT(n)->type))
1036 		i->type = V4L2_INPUT_TYPE_TUNER;
1037 
1038 	i->std = dev->vdev.tvnorms;
1039 
1040 	/* If they are asking about the active input, read signal status */
1041 	if (n == dev->video_input) {
1042 		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1043 					    GEN_STAT, 2, &gen_stat, 4);
1044 		if (ret > 0) {
1045 			if ((gen_stat & FLD_VPRES) == 0x00)
1046 				i->status |= V4L2_IN_ST_NO_SIGNAL;
1047 			if ((gen_stat & FLD_HLOCK) == 0x00)
1048 				i->status |= V4L2_IN_ST_NO_H_LOCK;
1049 		}
1050 	}
1051 
1052 	return 0;
1053 }
1054 
1055 int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
1056 {
1057 	struct cx231xx *dev = video_drvdata(file);
1058 
1059 	*i = dev->video_input;
1060 
1061 	return 0;
1062 }
1063 
1064 int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
1065 {
1066 	struct cx231xx *dev = video_drvdata(file);
1067 
1068 	dev->mode_tv = 0;
1069 
1070 	if (i >= MAX_CX231XX_INPUT)
1071 		return -EINVAL;
1072 	if (0 == INPUT(i)->type)
1073 		return -EINVAL;
1074 
1075 	video_mux(dev, i);
1076 
1077 	if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1078 	    INPUT(i)->type == CX231XX_VMUX_CABLE) {
1079 		/* There's a tuner, so reset the standard and put it on the
1080 		   last known frequency (since it was probably powered down
1081 		   until now */
1082 		call_all(dev, video, s_std, dev->norm);
1083 	}
1084 
1085 	return 0;
1086 }
1087 
1088 int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1089 {
1090 	struct cx231xx *dev = video_drvdata(file);
1091 
1092 	if (0 != t->index)
1093 		return -EINVAL;
1094 
1095 	strscpy(t->name, "Tuner", sizeof(t->name));
1096 
1097 	t->type = V4L2_TUNER_ANALOG_TV;
1098 	t->capability = V4L2_TUNER_CAP_NORM;
1099 	t->rangehigh = 0xffffffffUL;
1100 	t->signal = 0xffff;	/* LOCKED */
1101 	call_all(dev, tuner, g_tuner, t);
1102 
1103 	return 0;
1104 }
1105 
1106 int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1107 {
1108 	if (0 != t->index)
1109 		return -EINVAL;
1110 	return 0;
1111 }
1112 
1113 int cx231xx_g_frequency(struct file *file, void *priv,
1114 			      struct v4l2_frequency *f)
1115 {
1116 	struct cx231xx *dev = video_drvdata(file);
1117 
1118 	if (f->tuner)
1119 		return -EINVAL;
1120 
1121 	f->frequency = dev->ctl_freq;
1122 
1123 	return 0;
1124 }
1125 
1126 int cx231xx_s_frequency(struct file *file, void *priv,
1127 			      const struct v4l2_frequency *f)
1128 {
1129 	struct cx231xx *dev = video_drvdata(file);
1130 	struct v4l2_frequency new_freq = *f;
1131 	int rc, need_if_freq = 0;
1132 	u32 if_frequency = 5400000;
1133 
1134 	dev_dbg(dev->dev,
1135 		"Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1136 		f->frequency, f->type);
1137 
1138 	if (0 != f->tuner)
1139 		return -EINVAL;
1140 
1141 	/* set pre channel change settings in DIF first */
1142 	rc = cx231xx_tuner_pre_channel_change(dev);
1143 
1144 	switch (dev->model) { /* i2c device tuners */
1145 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1146 	case CX231XX_BOARD_HAUPPAUGE_935C:
1147 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1148 	case CX231XX_BOARD_HAUPPAUGE_975:
1149 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1150 		if (dev->cx231xx_set_analog_freq)
1151 			dev->cx231xx_set_analog_freq(dev, f->frequency);
1152 		dev->ctl_freq = f->frequency;
1153 		need_if_freq = 1;
1154 		break;
1155 	default:
1156 		call_all(dev, tuner, s_frequency, f);
1157 		call_all(dev, tuner, g_frequency, &new_freq);
1158 		dev->ctl_freq = new_freq.frequency;
1159 		break;
1160 	}
1161 
1162 	pr_debug("%s() %u  :  %u\n", __func__, f->frequency, dev->ctl_freq);
1163 
1164 	/* set post channel change settings in DIF first */
1165 	rc = cx231xx_tuner_post_channel_change(dev);
1166 
1167 	if (need_if_freq || dev->tuner_type == TUNER_NXP_TDA18271) {
1168 		if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1169 			if_frequency = 5400000;  /*5.4MHz	*/
1170 		else if (dev->norm & V4L2_STD_B)
1171 			if_frequency = 6000000;  /*6.0MHz	*/
1172 		else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1173 			if_frequency = 6900000;  /*6.9MHz	*/
1174 		else if (dev->norm & V4L2_STD_GH)
1175 			if_frequency = 7100000;  /*7.1MHz	*/
1176 		else if (dev->norm & V4L2_STD_PAL_I)
1177 			if_frequency = 7250000;  /*7.25MHz	*/
1178 		else if (dev->norm & V4L2_STD_SECAM_L)
1179 			if_frequency = 6900000;  /*6.9MHz	*/
1180 		else if (dev->norm & V4L2_STD_SECAM_LC)
1181 			if_frequency = 1250000;  /*1.25MHz	*/
1182 
1183 		dev_dbg(dev->dev,
1184 			"if_frequency is set to %d\n", if_frequency);
1185 		cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1186 
1187 		update_HH_register_after_set_DIF(dev);
1188 	}
1189 
1190 	dev_dbg(dev->dev, "Set New FREQUENCY to %d\n", f->frequency);
1191 
1192 	return rc;
1193 }
1194 
1195 #ifdef CONFIG_VIDEO_ADV_DEBUG
1196 
1197 int cx231xx_g_chip_info(struct file *file, void *fh,
1198 			struct v4l2_dbg_chip_info *chip)
1199 {
1200 	switch (chip->match.addr) {
1201 	case 0:	/* Cx231xx - internal registers */
1202 		return 0;
1203 	case 1:	/* AFE - read byte */
1204 		strscpy(chip->name, "AFE (byte)", sizeof(chip->name));
1205 		return 0;
1206 	case 2:	/* Video Block - read byte */
1207 		strscpy(chip->name, "Video (byte)", sizeof(chip->name));
1208 		return 0;
1209 	case 3:	/* I2S block - read byte */
1210 		strscpy(chip->name, "I2S (byte)", sizeof(chip->name));
1211 		return 0;
1212 	case 4: /* AFE - read dword */
1213 		strscpy(chip->name, "AFE (dword)", sizeof(chip->name));
1214 		return 0;
1215 	case 5: /* Video Block - read dword */
1216 		strscpy(chip->name, "Video (dword)", sizeof(chip->name));
1217 		return 0;
1218 	case 6: /* I2S Block - read dword */
1219 		strscpy(chip->name, "I2S (dword)", sizeof(chip->name));
1220 		return 0;
1221 	}
1222 	return -EINVAL;
1223 }
1224 
1225 int cx231xx_g_register(struct file *file, void *priv,
1226 			     struct v4l2_dbg_register *reg)
1227 {
1228 	struct cx231xx *dev = video_drvdata(file);
1229 	int ret;
1230 	u8 value[4] = { 0, 0, 0, 0 };
1231 	u32 data = 0;
1232 
1233 	switch (reg->match.addr) {
1234 	case 0:	/* Cx231xx - internal registers */
1235 		ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1236 				(u16)reg->reg, value, 4);
1237 		reg->val = value[0] | value[1] << 8 |
1238 			value[2] << 16 | (u32)value[3] << 24;
1239 		reg->size = 4;
1240 		break;
1241 	case 1:	/* AFE - read byte */
1242 		ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1243 				(u16)reg->reg, 2, &data, 1);
1244 		reg->val = data;
1245 		reg->size = 1;
1246 		break;
1247 	case 2:	/* Video Block - read byte */
1248 		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1249 				(u16)reg->reg, 2, &data, 1);
1250 		reg->val = data;
1251 		reg->size = 1;
1252 		break;
1253 	case 3:	/* I2S block - read byte */
1254 		ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1255 				(u16)reg->reg, 1, &data, 1);
1256 		reg->val = data;
1257 		reg->size = 1;
1258 		break;
1259 	case 4: /* AFE - read dword */
1260 		ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1261 				(u16)reg->reg, 2, &data, 4);
1262 		reg->val = data;
1263 		reg->size = 4;
1264 		break;
1265 	case 5: /* Video Block - read dword */
1266 		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1267 				(u16)reg->reg, 2, &data, 4);
1268 		reg->val = data;
1269 		reg->size = 4;
1270 		break;
1271 	case 6: /* I2S Block - read dword */
1272 		ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1273 				(u16)reg->reg, 1, &data, 4);
1274 		reg->val = data;
1275 		reg->size = 4;
1276 		break;
1277 	default:
1278 		return -EINVAL;
1279 	}
1280 	return ret < 0 ? ret : 0;
1281 }
1282 
1283 int cx231xx_s_register(struct file *file, void *priv,
1284 			     const struct v4l2_dbg_register *reg)
1285 {
1286 	struct cx231xx *dev = video_drvdata(file);
1287 	int ret;
1288 	u8 data[4] = { 0, 0, 0, 0 };
1289 
1290 	switch (reg->match.addr) {
1291 	case 0:	/* cx231xx internal registers */
1292 		data[0] = (u8) reg->val;
1293 		data[1] = (u8) (reg->val >> 8);
1294 		data[2] = (u8) (reg->val >> 16);
1295 		data[3] = (u8) (reg->val >> 24);
1296 		ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1297 				(u16)reg->reg, data, 4);
1298 		break;
1299 	case 1:	/* AFE - write byte */
1300 		ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1301 				(u16)reg->reg, 2, reg->val, 1);
1302 		break;
1303 	case 2:	/* Video Block - write byte */
1304 		ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1305 				(u16)reg->reg, 2, reg->val, 1);
1306 		break;
1307 	case 3:	/* I2S block - write byte */
1308 		ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1309 				(u16)reg->reg, 1, reg->val, 1);
1310 		break;
1311 	case 4: /* AFE - write dword */
1312 		ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1313 				(u16)reg->reg, 2, reg->val, 4);
1314 		break;
1315 	case 5: /* Video Block - write dword */
1316 		ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1317 				(u16)reg->reg, 2, reg->val, 4);
1318 		break;
1319 	case 6: /* I2S block - write dword */
1320 		ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1321 				(u16)reg->reg, 1, reg->val, 4);
1322 		break;
1323 	default:
1324 		return -EINVAL;
1325 	}
1326 	return ret < 0 ? ret : 0;
1327 }
1328 #endif
1329 
1330 static int vidioc_g_pixelaspect(struct file *file, void *priv,
1331 				int type, struct v4l2_fract *f)
1332 {
1333 	struct cx231xx *dev = video_drvdata(file);
1334 	bool is_50hz = dev->norm & V4L2_STD_625_50;
1335 
1336 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1337 		return -EINVAL;
1338 
1339 	f->numerator = is_50hz ? 54 : 11;
1340 	f->denominator = is_50hz ? 59 : 10;
1341 
1342 	return 0;
1343 }
1344 
1345 static int vidioc_g_selection(struct file *file, void *priv,
1346 			      struct v4l2_selection *s)
1347 {
1348 	struct cx231xx *dev = video_drvdata(file);
1349 
1350 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1351 		return -EINVAL;
1352 
1353 	switch (s->target) {
1354 	case V4L2_SEL_TGT_CROP_BOUNDS:
1355 	case V4L2_SEL_TGT_CROP_DEFAULT:
1356 		s->r.left = 0;
1357 		s->r.top = 0;
1358 		s->r.width = dev->width;
1359 		s->r.height = dev->height;
1360 		break;
1361 	default:
1362 		return -EINVAL;
1363 	}
1364 	return 0;
1365 }
1366 
1367 int cx231xx_querycap(struct file *file, void *priv,
1368 			   struct v4l2_capability *cap)
1369 {
1370 	struct cx231xx *dev = video_drvdata(file);
1371 
1372 	strscpy(cap->driver, "cx231xx", sizeof(cap->driver));
1373 	strscpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1374 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1375 	cap->capabilities = V4L2_CAP_READWRITE |
1376 		V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1377 		V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
1378 	if (video_is_registered(&dev->radio_dev))
1379 		cap->capabilities |= V4L2_CAP_RADIO;
1380 
1381 	switch (dev->model) {
1382 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1383 	case CX231XX_BOARD_HAUPPAUGE_935C:
1384 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1385 	case CX231XX_BOARD_HAUPPAUGE_975:
1386 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1387 		cap->capabilities |= V4L2_CAP_TUNER;
1388 		break;
1389 	default:
1390 		if (dev->tuner_type != TUNER_ABSENT)
1391 			cap->capabilities |= V4L2_CAP_TUNER;
1392 		break;
1393 	}
1394 	return 0;
1395 }
1396 
1397 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1398 				   struct v4l2_fmtdesc *f)
1399 {
1400 	if (unlikely(f->index >= ARRAY_SIZE(format)))
1401 		return -EINVAL;
1402 
1403 	f->pixelformat = format[f->index].fourcc;
1404 
1405 	return 0;
1406 }
1407 
1408 /* RAW VBI ioctls */
1409 
1410 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1411 				struct v4l2_format *f)
1412 {
1413 	struct cx231xx *dev = video_drvdata(file);
1414 
1415 	f->fmt.vbi.sampling_rate = 6750000 * 4;
1416 	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1417 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1418 	f->fmt.vbi.offset = 0;
1419 	f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1420 	    PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1421 	f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1422 	    PAL_VBI_LINES : NTSC_VBI_LINES;
1423 	f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1424 	    PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1425 	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1426 	memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1427 
1428 	return 0;
1429 
1430 }
1431 
1432 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1433 				  struct v4l2_format *f)
1434 {
1435 	struct cx231xx *dev = video_drvdata(file);
1436 
1437 	f->fmt.vbi.sampling_rate = 6750000 * 4;
1438 	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1439 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1440 	f->fmt.vbi.offset = 0;
1441 	f->fmt.vbi.flags = 0;
1442 	f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1443 	    PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1444 	f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1445 	    PAL_VBI_LINES : NTSC_VBI_LINES;
1446 	f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1447 	    PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1448 	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1449 	memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1450 
1451 	return 0;
1452 
1453 }
1454 
1455 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1456 				  struct v4l2_format *f)
1457 {
1458 	return vidioc_try_fmt_vbi_cap(file, priv, f);
1459 }
1460 
1461 /* ----------------------------------------------------------- */
1462 /* RADIO ESPECIFIC IOCTLS                                      */
1463 /* ----------------------------------------------------------- */
1464 
1465 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1466 {
1467 	struct cx231xx *dev = video_drvdata(file);
1468 
1469 	if (t->index)
1470 		return -EINVAL;
1471 
1472 	strscpy(t->name, "Radio", sizeof(t->name));
1473 
1474 	call_all(dev, tuner, g_tuner, t);
1475 
1476 	return 0;
1477 }
1478 static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1479 {
1480 	struct cx231xx *dev = video_drvdata(file);
1481 
1482 	if (t->index)
1483 		return -EINVAL;
1484 
1485 	call_all(dev, tuner, s_tuner, t);
1486 
1487 	return 0;
1488 }
1489 
1490 /*
1491  * cx231xx_v4l2_open()
1492  * inits the device and starts isoc transfer
1493  */
1494 static int cx231xx_v4l2_open(struct file *filp)
1495 {
1496 	struct video_device *vdev = video_devdata(filp);
1497 	struct cx231xx *dev = video_drvdata(filp);
1498 	int ret;
1499 
1500 	if (mutex_lock_interruptible(&dev->lock))
1501 		return -ERESTARTSYS;
1502 
1503 	ret = v4l2_fh_open(filp);
1504 	if (ret) {
1505 		mutex_unlock(&dev->lock);
1506 		return ret;
1507 	}
1508 
1509 	if (dev->users++ == 0) {
1510 		/* Power up in Analog TV mode */
1511 		if (dev->board.external_av)
1512 			cx231xx_set_power_mode(dev,
1513 				 POLARIS_AVMODE_ENXTERNAL_AV);
1514 		else
1515 			cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1516 
1517 		/* set video alternate setting */
1518 		cx231xx_set_video_alternate(dev);
1519 
1520 		/* Needed, since GPIO might have disabled power of
1521 		   some i2c device */
1522 		cx231xx_config_i2c(dev);
1523 
1524 		/* device needs to be initialized before isoc transfer */
1525 		dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1526 	}
1527 
1528 	if (vdev->vfl_type == VFL_TYPE_RADIO) {
1529 		cx231xx_videodbg("video_open: setting radio device\n");
1530 
1531 		/* cx231xx_start_radio(dev); */
1532 
1533 		call_all(dev, tuner, s_radio);
1534 	}
1535 	if (vdev->vfl_type == VFL_TYPE_VBI) {
1536 		/* Set the required alternate setting  VBI interface works in
1537 		   Bulk mode only */
1538 		cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1539 	}
1540 	mutex_unlock(&dev->lock);
1541 	return 0;
1542 }
1543 
1544 /*
1545  * cx231xx_realease_resources()
1546  * unregisters the v4l2,i2c and usb devices
1547  * called when the device gets disconnected or at module unload
1548 */
1549 void cx231xx_release_analog_resources(struct cx231xx *dev)
1550 {
1551 
1552 	/*FIXME: I2C IR should be disconnected */
1553 
1554 	if (video_is_registered(&dev->radio_dev))
1555 		video_unregister_device(&dev->radio_dev);
1556 	if (video_is_registered(&dev->vbi_dev)) {
1557 		dev_info(dev->dev, "V4L2 device %s deregistered\n",
1558 			video_device_node_name(&dev->vbi_dev));
1559 		video_unregister_device(&dev->vbi_dev);
1560 	}
1561 	if (video_is_registered(&dev->vdev)) {
1562 		dev_info(dev->dev, "V4L2 device %s deregistered\n",
1563 			video_device_node_name(&dev->vdev));
1564 
1565 		if (dev->board.has_417)
1566 			cx231xx_417_unregister(dev);
1567 
1568 		video_unregister_device(&dev->vdev);
1569 	}
1570 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1571 	v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1572 }
1573 
1574 /*
1575  * cx231xx_close()
1576  * stops streaming and deallocates all resources allocated by the v4l2
1577  * calls and ioctls
1578  */
1579 static int cx231xx_close(struct file *filp)
1580 {
1581 	struct cx231xx *dev = video_drvdata(filp);
1582 	struct video_device *vdev = video_devdata(filp);
1583 
1584 	_vb2_fop_release(filp, NULL);
1585 
1586 	if (--dev->users == 0) {
1587 		/* Save some power by putting tuner to sleep */
1588 		call_all(dev, tuner, standby);
1589 
1590 		/* do this before setting alternate! */
1591 		if (dev->USE_ISO)
1592 			cx231xx_uninit_isoc(dev);
1593 		else
1594 			cx231xx_uninit_bulk(dev);
1595 		cx231xx_set_mode(dev, CX231XX_SUSPEND);
1596 	}
1597 
1598 	/*
1599 	 * To workaround error number=-71 on EP0 for VideoGrabber,
1600 	 *	 need exclude following.
1601 	 * FIXME: It is probably safe to remove most of these, as we're
1602 	 * now avoiding the alternate setting for INDEX_VANC
1603 	 */
1604 	if (!dev->board.no_alt_vanc && vdev->vfl_type == VFL_TYPE_VBI) {
1605 		/* do this before setting alternate! */
1606 		cx231xx_uninit_vbi_isoc(dev);
1607 
1608 		/* set alternate 0 */
1609 		if (!dev->vbi_or_sliced_cc_mode)
1610 			cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1611 		else
1612 			cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1613 
1614 		wake_up_interruptible_nr(&dev->open, 1);
1615 		return 0;
1616 	}
1617 
1618 	if (dev->users == 0) {
1619 		/* set alternate 0 */
1620 		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1621 	}
1622 
1623 	wake_up_interruptible(&dev->open);
1624 	return 0;
1625 }
1626 
1627 static int cx231xx_v4l2_close(struct file *filp)
1628 {
1629 	struct cx231xx *dev = video_drvdata(filp);
1630 	int rc;
1631 
1632 	mutex_lock(&dev->lock);
1633 	rc = cx231xx_close(filp);
1634 	mutex_unlock(&dev->lock);
1635 	return rc;
1636 }
1637 
1638 static const struct v4l2_file_operations cx231xx_v4l_fops = {
1639 	.owner   = THIS_MODULE,
1640 	.open    = cx231xx_v4l2_open,
1641 	.release = cx231xx_v4l2_close,
1642 	.read    = vb2_fop_read,
1643 	.poll    = vb2_fop_poll,
1644 	.mmap    = vb2_fop_mmap,
1645 	.unlocked_ioctl   = video_ioctl2,
1646 };
1647 
1648 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1649 	.vidioc_querycap               = cx231xx_querycap,
1650 	.vidioc_enum_fmt_vid_cap       = vidioc_enum_fmt_vid_cap,
1651 	.vidioc_g_fmt_vid_cap          = vidioc_g_fmt_vid_cap,
1652 	.vidioc_try_fmt_vid_cap        = vidioc_try_fmt_vid_cap,
1653 	.vidioc_s_fmt_vid_cap          = vidioc_s_fmt_vid_cap,
1654 	.vidioc_g_fmt_vbi_cap          = vidioc_g_fmt_vbi_cap,
1655 	.vidioc_try_fmt_vbi_cap        = vidioc_try_fmt_vbi_cap,
1656 	.vidioc_s_fmt_vbi_cap          = vidioc_s_fmt_vbi_cap,
1657 	.vidioc_g_pixelaspect          = vidioc_g_pixelaspect,
1658 	.vidioc_g_selection            = vidioc_g_selection,
1659 	.vidioc_reqbufs                = vb2_ioctl_reqbufs,
1660 	.vidioc_querybuf               = vb2_ioctl_querybuf,
1661 	.vidioc_qbuf                   = vb2_ioctl_qbuf,
1662 	.vidioc_dqbuf                  = vb2_ioctl_dqbuf,
1663 	.vidioc_s_std                  = vidioc_s_std,
1664 	.vidioc_g_std                  = vidioc_g_std,
1665 	.vidioc_enum_input             = cx231xx_enum_input,
1666 	.vidioc_g_input                = cx231xx_g_input,
1667 	.vidioc_s_input                = cx231xx_s_input,
1668 	.vidioc_streamon               = vb2_ioctl_streamon,
1669 	.vidioc_streamoff              = vb2_ioctl_streamoff,
1670 	.vidioc_g_tuner                = cx231xx_g_tuner,
1671 	.vidioc_s_tuner                = cx231xx_s_tuner,
1672 	.vidioc_g_frequency            = cx231xx_g_frequency,
1673 	.vidioc_s_frequency            = cx231xx_s_frequency,
1674 #ifdef CONFIG_VIDEO_ADV_DEBUG
1675 	.vidioc_g_chip_info            = cx231xx_g_chip_info,
1676 	.vidioc_g_register             = cx231xx_g_register,
1677 	.vidioc_s_register             = cx231xx_s_register,
1678 #endif
1679 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1680 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1681 };
1682 
1683 static struct video_device cx231xx_vbi_template;
1684 
1685 static const struct video_device cx231xx_video_template = {
1686 	.fops         = &cx231xx_v4l_fops,
1687 	.release      = video_device_release_empty,
1688 	.ioctl_ops    = &video_ioctl_ops,
1689 	.tvnorms      = V4L2_STD_ALL,
1690 };
1691 
1692 static const struct v4l2_file_operations radio_fops = {
1693 	.owner   = THIS_MODULE,
1694 	.open   = cx231xx_v4l2_open,
1695 	.release = cx231xx_v4l2_close,
1696 	.poll = v4l2_ctrl_poll,
1697 	.unlocked_ioctl = video_ioctl2,
1698 };
1699 
1700 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1701 	.vidioc_querycap    = cx231xx_querycap,
1702 	.vidioc_g_tuner     = radio_g_tuner,
1703 	.vidioc_s_tuner     = radio_s_tuner,
1704 	.vidioc_g_frequency = cx231xx_g_frequency,
1705 	.vidioc_s_frequency = cx231xx_s_frequency,
1706 #ifdef CONFIG_VIDEO_ADV_DEBUG
1707 	.vidioc_g_chip_info = cx231xx_g_chip_info,
1708 	.vidioc_g_register  = cx231xx_g_register,
1709 	.vidioc_s_register  = cx231xx_s_register,
1710 #endif
1711 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1712 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1713 };
1714 
1715 static struct video_device cx231xx_radio_template = {
1716 	.name      = "cx231xx-radio",
1717 	.fops      = &radio_fops,
1718 	.ioctl_ops = &radio_ioctl_ops,
1719 };
1720 
1721 /******************************** usb interface ******************************/
1722 
1723 static void cx231xx_vdev_init(struct cx231xx *dev,
1724 		struct video_device *vfd,
1725 		const struct video_device *template,
1726 		const char *type_name)
1727 {
1728 	*vfd = *template;
1729 	vfd->v4l2_dev = &dev->v4l2_dev;
1730 	vfd->release = video_device_release_empty;
1731 	vfd->lock = &dev->lock;
1732 
1733 	snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1734 
1735 	video_set_drvdata(vfd, dev);
1736 	if (dev->tuner_type == TUNER_ABSENT) {
1737 		switch (dev->model) {
1738 		case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1739 		case CX231XX_BOARD_HAUPPAUGE_935C:
1740 		case CX231XX_BOARD_HAUPPAUGE_955Q:
1741 		case CX231XX_BOARD_HAUPPAUGE_975:
1742 		case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1743 			break;
1744 		default:
1745 			v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
1746 			v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
1747 			v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
1748 			v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
1749 			break;
1750 		}
1751 	}
1752 }
1753 
1754 int cx231xx_register_analog_devices(struct cx231xx *dev)
1755 {
1756 	struct vb2_queue *q;
1757 	int ret;
1758 
1759 	dev_info(dev->dev, "v4l2 driver version %s\n", CX231XX_VERSION);
1760 
1761 	/* set default norm */
1762 	dev->norm = V4L2_STD_PAL;
1763 	dev->width = norm_maxw(dev);
1764 	dev->height = norm_maxh(dev);
1765 	dev->interlaced = 0;
1766 
1767 	/* Analog specific initialization */
1768 	dev->format = &format[0];
1769 
1770 	/* Set the initial input */
1771 	video_mux(dev, dev->video_input);
1772 
1773 	call_all(dev, video, s_std, dev->norm);
1774 
1775 	v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
1776 	v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
1777 
1778 	if (dev->sd_cx25840) {
1779 		v4l2_ctrl_add_handler(&dev->ctrl_handler,
1780 				dev->sd_cx25840->ctrl_handler, NULL, true);
1781 		v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
1782 				dev->sd_cx25840->ctrl_handler,
1783 				v4l2_ctrl_radio_filter, true);
1784 	}
1785 
1786 	if (dev->ctrl_handler.error)
1787 		return dev->ctrl_handler.error;
1788 	if (dev->radio_ctrl_handler.error)
1789 		return dev->radio_ctrl_handler.error;
1790 
1791 	/* enable vbi capturing */
1792 	/* write code here...  */
1793 
1794 	/* allocate and fill video video_device struct */
1795 	cx231xx_vdev_init(dev, &dev->vdev, &cx231xx_video_template, "video");
1796 #if defined(CONFIG_MEDIA_CONTROLLER)
1797 	dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1798 	ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1799 	if (ret < 0)
1800 		dev_err(dev->dev, "failed to initialize video media entity!\n");
1801 #endif
1802 	dev->vdev.ctrl_handler = &dev->ctrl_handler;
1803 
1804 	q = &dev->vidq;
1805 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1806 	q->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF | VB2_READ;
1807 	q->drv_priv = dev;
1808 	q->buf_struct_size = sizeof(struct cx231xx_buffer);
1809 	q->ops = &cx231xx_video_qops;
1810 	q->mem_ops = &vb2_vmalloc_memops;
1811 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1812 	q->min_queued_buffers = 1;
1813 	q->lock = &dev->lock;
1814 	ret = vb2_queue_init(q);
1815 	if (ret)
1816 		return ret;
1817 	dev->vdev.queue = q;
1818 	dev->vdev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1819 				V4L2_CAP_VIDEO_CAPTURE;
1820 
1821 	switch (dev->model) { /* i2c device tuners */
1822 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1823 	case CX231XX_BOARD_HAUPPAUGE_935C:
1824 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1825 	case CX231XX_BOARD_HAUPPAUGE_975:
1826 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1827 		dev->vdev.device_caps |= V4L2_CAP_TUNER;
1828 		break;
1829 	default:
1830 		if (dev->tuner_type != TUNER_ABSENT)
1831 			dev->vdev.device_caps |= V4L2_CAP_TUNER;
1832 		break;
1833 	}
1834 
1835 	/* register v4l2 video video_device */
1836 	ret = video_register_device(&dev->vdev, VFL_TYPE_VIDEO,
1837 				    video_nr[dev->devno]);
1838 	if (ret) {
1839 		dev_err(dev->dev,
1840 			"unable to register video device (error=%i).\n",
1841 			ret);
1842 		return ret;
1843 	}
1844 
1845 	dev_info(dev->dev, "Registered video device %s [v4l2]\n",
1846 		video_device_node_name(&dev->vdev));
1847 
1848 	/* Initialize VBI template */
1849 	cx231xx_vbi_template = cx231xx_video_template;
1850 	strscpy(cx231xx_vbi_template.name, "cx231xx-vbi",
1851 		sizeof(cx231xx_vbi_template.name));
1852 
1853 	/* Allocate and fill vbi video_device struct */
1854 	cx231xx_vdev_init(dev, &dev->vbi_dev, &cx231xx_vbi_template, "vbi");
1855 
1856 #if defined(CONFIG_MEDIA_CONTROLLER)
1857 	dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1858 	ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1859 	if (ret < 0)
1860 		dev_err(dev->dev, "failed to initialize vbi media entity!\n");
1861 #endif
1862 	dev->vbi_dev.ctrl_handler = &dev->ctrl_handler;
1863 
1864 	q = &dev->vbiq;
1865 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1866 	q->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF | VB2_READ;
1867 	q->drv_priv = dev;
1868 	q->buf_struct_size = sizeof(struct cx231xx_buffer);
1869 	q->ops = &cx231xx_vbi_qops;
1870 	q->mem_ops = &vb2_vmalloc_memops;
1871 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1872 	q->min_queued_buffers = 1;
1873 	q->lock = &dev->lock;
1874 	ret = vb2_queue_init(q);
1875 	if (ret)
1876 		return ret;
1877 	dev->vbi_dev.queue = q;
1878 	dev->vbi_dev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1879 				   V4L2_CAP_VBI_CAPTURE;
1880 	switch (dev->model) { /* i2c device tuners */
1881 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1882 	case CX231XX_BOARD_HAUPPAUGE_935C:
1883 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1884 	case CX231XX_BOARD_HAUPPAUGE_975:
1885 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1886 		dev->vbi_dev.device_caps |= V4L2_CAP_TUNER;
1887 		break;
1888 	default:
1889 		if (dev->tuner_type != TUNER_ABSENT)
1890 			dev->vbi_dev.device_caps |= V4L2_CAP_TUNER;
1891 	}
1892 
1893 	/* register v4l2 vbi video_device */
1894 	ret = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
1895 				    vbi_nr[dev->devno]);
1896 	if (ret < 0) {
1897 		dev_err(dev->dev, "unable to register vbi device\n");
1898 		return ret;
1899 	}
1900 
1901 	dev_info(dev->dev, "Registered VBI device %s\n",
1902 		video_device_node_name(&dev->vbi_dev));
1903 
1904 	if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
1905 		cx231xx_vdev_init(dev, &dev->radio_dev,
1906 				&cx231xx_radio_template, "radio");
1907 		dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1908 		dev->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
1909 		ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
1910 					    radio_nr[dev->devno]);
1911 		if (ret < 0) {
1912 			dev_err(dev->dev,
1913 				"can't register radio device\n");
1914 			return ret;
1915 		}
1916 		dev_info(dev->dev, "Registered radio device as %s\n",
1917 			video_device_node_name(&dev->radio_dev));
1918 	}
1919 
1920 	return 0;
1921 }
1922