xref: /linux/drivers/media/usb/cx231xx/cx231xx-video.c (revision a98f670e41a99f53acb1fb33cee9c6abbb2e6f23)
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  * video-buf 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 
718 	dev->size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
719 
720 	if (vq->num_buffers + *nbuffers < CX231XX_MIN_BUF)
721 		*nbuffers = CX231XX_MIN_BUF - vq->num_buffers;
722 
723 	if (*nplanes)
724 		return sizes[0] < dev->size ? -EINVAL : 0;
725 	*nplanes = 1;
726 	sizes[0] = dev->size;
727 
728 	return 0;
729 }
730 
731 static void buffer_queue(struct vb2_buffer *vb)
732 {
733 	struct cx231xx_buffer *buf =
734 	    container_of(vb, struct cx231xx_buffer, vb.vb2_buf);
735 	struct cx231xx *dev = vb2_get_drv_priv(vb->vb2_queue);
736 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
737 	unsigned long flags;
738 
739 	spin_lock_irqsave(&dev->video_mode.slock, flags);
740 	list_add_tail(&buf->list, &vidq->active);
741 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
742 }
743 
744 static void return_all_buffers(struct cx231xx *dev,
745 			       enum vb2_buffer_state state)
746 {
747 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
748 	struct cx231xx_buffer *buf, *node;
749 	unsigned long flags;
750 
751 	spin_lock_irqsave(&dev->video_mode.slock, flags);
752 	if (dev->USE_ISO)
753 		dev->video_mode.isoc_ctl.buf = NULL;
754 	else
755 		dev->video_mode.bulk_ctl.buf = NULL;
756 	list_for_each_entry_safe(buf, node, &vidq->active, list) {
757 		list_del(&buf->list);
758 		vb2_buffer_done(&buf->vb.vb2_buf, state);
759 	}
760 	spin_unlock_irqrestore(&dev->video_mode.slock, flags);
761 }
762 
763 static int start_streaming(struct vb2_queue *vq, unsigned int count)
764 {
765 	struct cx231xx *dev = vb2_get_drv_priv(vq);
766 	struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
767 	int ret = 0;
768 
769 	vidq->sequence = 0;
770 	dev->mode_tv = 0;
771 
772 	cx231xx_enable_analog_tuner(dev);
773 	if (dev->USE_ISO)
774 		ret = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
775 					CX231XX_NUM_BUFS,
776 					dev->video_mode.max_pkt_size,
777 					cx231xx_isoc_copy);
778 	else
779 		ret = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
780 					CX231XX_NUM_BUFS,
781 					dev->video_mode.max_pkt_size,
782 					cx231xx_bulk_copy);
783 	if (ret)
784 		return_all_buffers(dev, VB2_BUF_STATE_QUEUED);
785 	call_all(dev, video, s_stream, 1);
786 	return ret;
787 }
788 
789 static void stop_streaming(struct vb2_queue *vq)
790 {
791 	struct cx231xx *dev = vb2_get_drv_priv(vq);
792 
793 	call_all(dev, video, s_stream, 0);
794 	return_all_buffers(dev, VB2_BUF_STATE_ERROR);
795 }
796 
797 static struct vb2_ops cx231xx_video_qops = {
798 	.queue_setup		= queue_setup,
799 	.buf_queue		= buffer_queue,
800 	.start_streaming	= start_streaming,
801 	.stop_streaming		= stop_streaming,
802 	.wait_prepare		= vb2_ops_wait_prepare,
803 	.wait_finish		= vb2_ops_wait_finish,
804 };
805 
806 /*********************  v4l2 interface  **************************************/
807 
808 void video_mux(struct cx231xx *dev, int index)
809 {
810 	dev->video_input = index;
811 	dev->ctl_ainput = INPUT(index)->amux;
812 
813 	cx231xx_set_video_input_mux(dev, index);
814 
815 	cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
816 
817 	cx231xx_set_audio_input(dev, dev->ctl_ainput);
818 
819 	dev_dbg(dev->dev, "video_mux : %d\n", index);
820 
821 	/* do mode control overrides if required */
822 	cx231xx_do_mode_ctrl_overrides(dev);
823 }
824 
825 /* ------------------------------------------------------------------
826 	IOCTL vidioc handling
827    ------------------------------------------------------------------*/
828 
829 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
830 				struct v4l2_format *f)
831 {
832 	struct cx231xx *dev = video_drvdata(file);
833 
834 	f->fmt.pix.width = dev->width;
835 	f->fmt.pix.height = dev->height;
836 	f->fmt.pix.pixelformat = dev->format->fourcc;
837 	f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
838 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
839 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
840 
841 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
842 
843 	return 0;
844 }
845 
846 static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
847 {
848 	unsigned int i;
849 
850 	for (i = 0; i < ARRAY_SIZE(format); i++)
851 		if (format[i].fourcc == fourcc)
852 			return &format[i];
853 
854 	return NULL;
855 }
856 
857 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
858 				  struct v4l2_format *f)
859 {
860 	struct cx231xx *dev = video_drvdata(file);
861 	unsigned int width = f->fmt.pix.width;
862 	unsigned int height = f->fmt.pix.height;
863 	unsigned int maxw = norm_maxw(dev);
864 	unsigned int maxh = norm_maxh(dev);
865 	struct cx231xx_fmt *fmt;
866 
867 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
868 	if (!fmt) {
869 		cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
870 				 f->fmt.pix.pixelformat);
871 		return -EINVAL;
872 	}
873 
874 	/* width must even because of the YUYV format
875 	   height must be even because of interlacing */
876 	v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
877 
878 	f->fmt.pix.width = width;
879 	f->fmt.pix.height = height;
880 	f->fmt.pix.pixelformat = fmt->fourcc;
881 	f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
882 	f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
883 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
884 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
885 
886 	return 0;
887 }
888 
889 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
890 				struct v4l2_format *f)
891 {
892 	struct cx231xx *dev = video_drvdata(file);
893 	struct v4l2_subdev_format format = {
894 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
895 	};
896 	int rc;
897 
898 	rc = vidioc_try_fmt_vid_cap(file, priv, f);
899 	if (rc)
900 		return rc;
901 
902 	if (vb2_is_busy(&dev->vidq)) {
903 		dev_err(dev->dev, "%s: queue busy\n", __func__);
904 		return -EBUSY;
905 	}
906 
907 	/* set new image size */
908 	dev->width = f->fmt.pix.width;
909 	dev->height = f->fmt.pix.height;
910 	dev->format = format_by_fourcc(f->fmt.pix.pixelformat);
911 
912 	v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
913 	call_all(dev, pad, set_fmt, NULL, &format);
914 	v4l2_fill_pix_format(&f->fmt.pix, &format.format);
915 
916 	return rc;
917 }
918 
919 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
920 {
921 	struct cx231xx *dev = video_drvdata(file);
922 
923 	*id = dev->norm;
924 	return 0;
925 }
926 
927 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
928 {
929 	struct cx231xx *dev = video_drvdata(file);
930 	struct v4l2_subdev_format format = {
931 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
932 	};
933 
934 	if (dev->norm == norm)
935 		return 0;
936 
937 	if (vb2_is_busy(&dev->vidq))
938 		return -EBUSY;
939 
940 	dev->norm = norm;
941 
942 	/* Adjusts width/height, if needed */
943 	dev->width = 720;
944 	dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
945 
946 	call_all(dev, video, s_std, dev->norm);
947 
948 	/* We need to reset basic properties in the decoder related to
949 	   resolution (since a standard change effects things like the number
950 	   of lines in VACT, etc) */
951 	format.format.code = MEDIA_BUS_FMT_FIXED;
952 	format.format.width = dev->width;
953 	format.format.height = dev->height;
954 	call_all(dev, pad, set_fmt, NULL, &format);
955 
956 	/* do mode control overrides */
957 	cx231xx_do_mode_ctrl_overrides(dev);
958 
959 	return 0;
960 }
961 
962 static const char *iname[] = {
963 	[CX231XX_VMUX_COMPOSITE1] = "Composite1",
964 	[CX231XX_VMUX_SVIDEO]     = "S-Video",
965 	[CX231XX_VMUX_TELEVISION] = "Television",
966 	[CX231XX_VMUX_CABLE]      = "Cable TV",
967 	[CX231XX_VMUX_DVB]        = "DVB",
968 };
969 
970 void cx231xx_v4l2_create_entities(struct cx231xx *dev)
971 {
972 #if defined(CONFIG_MEDIA_CONTROLLER)
973 	int ret, i;
974 
975 	/* Create entities for each input connector */
976 	for (i = 0; i < MAX_CX231XX_INPUT; i++) {
977 		struct media_entity *ent = &dev->input_ent[i];
978 
979 		if (!INPUT(i)->type)
980 			break;
981 
982 		ent->name = iname[INPUT(i)->type];
983 		ent->flags = MEDIA_ENT_FL_CONNECTOR;
984 		dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
985 
986 		switch (INPUT(i)->type) {
987 		case CX231XX_VMUX_COMPOSITE1:
988 			ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
989 			break;
990 		case CX231XX_VMUX_SVIDEO:
991 			ent->function = MEDIA_ENT_F_CONN_SVIDEO;
992 			break;
993 		case CX231XX_VMUX_TELEVISION:
994 		case CX231XX_VMUX_CABLE:
995 		case CX231XX_VMUX_DVB:
996 			/* The DVB core will handle it */
997 			if (dev->tuner_type == TUNER_ABSENT)
998 				continue;
999 			/* fall through */
1000 		default: /* just to shut up a gcc warning */
1001 			ent->function = MEDIA_ENT_F_CONN_RF;
1002 			break;
1003 		}
1004 
1005 		ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1006 		if (ret < 0)
1007 			pr_err("failed to initialize input pad[%d]!\n", i);
1008 
1009 		ret = media_device_register_entity(dev->media_dev, ent);
1010 		if (ret < 0)
1011 			pr_err("failed to register input entity %d!\n", i);
1012 	}
1013 #endif
1014 }
1015 
1016 int cx231xx_enum_input(struct file *file, void *priv,
1017 			     struct v4l2_input *i)
1018 {
1019 	struct cx231xx *dev = video_drvdata(file);
1020 	u32 gen_stat;
1021 	unsigned int n;
1022 	int ret;
1023 
1024 	n = i->index;
1025 	if (n >= MAX_CX231XX_INPUT)
1026 		return -EINVAL;
1027 	if (0 == INPUT(n)->type)
1028 		return -EINVAL;
1029 
1030 	i->index = n;
1031 	i->type = V4L2_INPUT_TYPE_CAMERA;
1032 
1033 	strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
1034 
1035 	if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
1036 	    (CX231XX_VMUX_CABLE == INPUT(n)->type))
1037 		i->type = V4L2_INPUT_TYPE_TUNER;
1038 
1039 	i->std = dev->vdev.tvnorms;
1040 
1041 	/* If they are asking about the active input, read signal status */
1042 	if (n == dev->video_input) {
1043 		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1044 					    GEN_STAT, 2, &gen_stat, 4);
1045 		if (ret > 0) {
1046 			if ((gen_stat & FLD_VPRES) == 0x00)
1047 				i->status |= V4L2_IN_ST_NO_SIGNAL;
1048 			if ((gen_stat & FLD_HLOCK) == 0x00)
1049 				i->status |= V4L2_IN_ST_NO_H_LOCK;
1050 		}
1051 	}
1052 
1053 	return 0;
1054 }
1055 
1056 int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
1057 {
1058 	struct cx231xx *dev = video_drvdata(file);
1059 
1060 	*i = dev->video_input;
1061 
1062 	return 0;
1063 }
1064 
1065 int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
1066 {
1067 	struct cx231xx *dev = video_drvdata(file);
1068 
1069 	dev->mode_tv = 0;
1070 
1071 	if (i >= MAX_CX231XX_INPUT)
1072 		return -EINVAL;
1073 	if (0 == INPUT(i)->type)
1074 		return -EINVAL;
1075 
1076 	video_mux(dev, i);
1077 
1078 	if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1079 	    INPUT(i)->type == CX231XX_VMUX_CABLE) {
1080 		/* There's a tuner, so reset the standard and put it on the
1081 		   last known frequency (since it was probably powered down
1082 		   until now */
1083 		call_all(dev, video, s_std, dev->norm);
1084 	}
1085 
1086 	return 0;
1087 }
1088 
1089 int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1090 {
1091 	struct cx231xx *dev = video_drvdata(file);
1092 
1093 	if (0 != t->index)
1094 		return -EINVAL;
1095 
1096 	strscpy(t->name, "Tuner", sizeof(t->name));
1097 
1098 	t->type = V4L2_TUNER_ANALOG_TV;
1099 	t->capability = V4L2_TUNER_CAP_NORM;
1100 	t->rangehigh = 0xffffffffUL;
1101 	t->signal = 0xffff;	/* LOCKED */
1102 	call_all(dev, tuner, g_tuner, t);
1103 
1104 	return 0;
1105 }
1106 
1107 int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1108 {
1109 	if (0 != t->index)
1110 		return -EINVAL;
1111 	return 0;
1112 }
1113 
1114 int cx231xx_g_frequency(struct file *file, void *priv,
1115 			      struct v4l2_frequency *f)
1116 {
1117 	struct cx231xx *dev = video_drvdata(file);
1118 
1119 	if (f->tuner)
1120 		return -EINVAL;
1121 
1122 	f->frequency = dev->ctl_freq;
1123 
1124 	return 0;
1125 }
1126 
1127 int cx231xx_s_frequency(struct file *file, void *priv,
1128 			      const struct v4l2_frequency *f)
1129 {
1130 	struct cx231xx *dev = video_drvdata(file);
1131 	struct v4l2_frequency new_freq = *f;
1132 	int rc, need_if_freq = 0;
1133 	u32 if_frequency = 5400000;
1134 
1135 	dev_dbg(dev->dev,
1136 		"Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1137 		f->frequency, f->type);
1138 
1139 	if (0 != f->tuner)
1140 		return -EINVAL;
1141 
1142 	/* set pre channel change settings in DIF first */
1143 	rc = cx231xx_tuner_pre_channel_change(dev);
1144 
1145 	switch (dev->model) { /* i2c device tuners */
1146 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1147 	case CX231XX_BOARD_HAUPPAUGE_935C:
1148 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1149 	case CX231XX_BOARD_HAUPPAUGE_975:
1150 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1151 		if (dev->cx231xx_set_analog_freq)
1152 			dev->cx231xx_set_analog_freq(dev, f->frequency);
1153 		dev->ctl_freq = f->frequency;
1154 		need_if_freq = 1;
1155 		break;
1156 	default:
1157 		call_all(dev, tuner, s_frequency, f);
1158 		call_all(dev, tuner, g_frequency, &new_freq);
1159 		dev->ctl_freq = new_freq.frequency;
1160 		break;
1161 	}
1162 
1163 	pr_debug("%s() %u  :  %u\n", __func__, f->frequency, dev->ctl_freq);
1164 
1165 	/* set post channel change settings in DIF first */
1166 	rc = cx231xx_tuner_post_channel_change(dev);
1167 
1168 	if (need_if_freq || dev->tuner_type == TUNER_NXP_TDA18271) {
1169 		if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1170 			if_frequency = 5400000;  /*5.4MHz	*/
1171 		else if (dev->norm & V4L2_STD_B)
1172 			if_frequency = 6000000;  /*6.0MHz	*/
1173 		else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1174 			if_frequency = 6900000;  /*6.9MHz	*/
1175 		else if (dev->norm & V4L2_STD_GH)
1176 			if_frequency = 7100000;  /*7.1MHz	*/
1177 		else if (dev->norm & V4L2_STD_PAL_I)
1178 			if_frequency = 7250000;  /*7.25MHz	*/
1179 		else if (dev->norm & V4L2_STD_SECAM_L)
1180 			if_frequency = 6900000;  /*6.9MHz	*/
1181 		else if (dev->norm & V4L2_STD_SECAM_LC)
1182 			if_frequency = 1250000;  /*1.25MHz	*/
1183 
1184 		dev_dbg(dev->dev,
1185 			"if_frequency is set to %d\n", if_frequency);
1186 		cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1187 
1188 		update_HH_register_after_set_DIF(dev);
1189 	}
1190 
1191 	dev_dbg(dev->dev, "Set New FREQUENCY to %d\n", f->frequency);
1192 
1193 	return rc;
1194 }
1195 
1196 #ifdef CONFIG_VIDEO_ADV_DEBUG
1197 
1198 int cx231xx_g_chip_info(struct file *file, void *fh,
1199 			struct v4l2_dbg_chip_info *chip)
1200 {
1201 	switch (chip->match.addr) {
1202 	case 0:	/* Cx231xx - internal registers */
1203 		return 0;
1204 	case 1:	/* AFE - read byte */
1205 		strscpy(chip->name, "AFE (byte)", sizeof(chip->name));
1206 		return 0;
1207 	case 2:	/* Video Block - read byte */
1208 		strscpy(chip->name, "Video (byte)", sizeof(chip->name));
1209 		return 0;
1210 	case 3:	/* I2S block - read byte */
1211 		strscpy(chip->name, "I2S (byte)", sizeof(chip->name));
1212 		return 0;
1213 	case 4: /* AFE - read dword */
1214 		strscpy(chip->name, "AFE (dword)", sizeof(chip->name));
1215 		return 0;
1216 	case 5: /* Video Block - read dword */
1217 		strscpy(chip->name, "Video (dword)", sizeof(chip->name));
1218 		return 0;
1219 	case 6: /* I2S Block - read dword */
1220 		strscpy(chip->name, "I2S (dword)", sizeof(chip->name));
1221 		return 0;
1222 	}
1223 	return -EINVAL;
1224 }
1225 
1226 int cx231xx_g_register(struct file *file, void *priv,
1227 			     struct v4l2_dbg_register *reg)
1228 {
1229 	struct cx231xx *dev = video_drvdata(file);
1230 	int ret;
1231 	u8 value[4] = { 0, 0, 0, 0 };
1232 	u32 data = 0;
1233 
1234 	switch (reg->match.addr) {
1235 	case 0:	/* Cx231xx - internal registers */
1236 		ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1237 				(u16)reg->reg, value, 4);
1238 		reg->val = value[0] | value[1] << 8 |
1239 			value[2] << 16 | (u32)value[3] << 24;
1240 		reg->size = 4;
1241 		break;
1242 	case 1:	/* AFE - read byte */
1243 		ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1244 				(u16)reg->reg, 2, &data, 1);
1245 		reg->val = data;
1246 		reg->size = 1;
1247 		break;
1248 	case 2:	/* Video Block - read byte */
1249 		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1250 				(u16)reg->reg, 2, &data, 1);
1251 		reg->val = data;
1252 		reg->size = 1;
1253 		break;
1254 	case 3:	/* I2S block - read byte */
1255 		ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1256 				(u16)reg->reg, 1, &data, 1);
1257 		reg->val = data;
1258 		reg->size = 1;
1259 		break;
1260 	case 4: /* AFE - read dword */
1261 		ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1262 				(u16)reg->reg, 2, &data, 4);
1263 		reg->val = data;
1264 		reg->size = 4;
1265 		break;
1266 	case 5: /* Video Block - read dword */
1267 		ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1268 				(u16)reg->reg, 2, &data, 4);
1269 		reg->val = data;
1270 		reg->size = 4;
1271 		break;
1272 	case 6: /* I2S Block - read dword */
1273 		ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1274 				(u16)reg->reg, 1, &data, 4);
1275 		reg->val = data;
1276 		reg->size = 4;
1277 		break;
1278 	default:
1279 		return -EINVAL;
1280 	}
1281 	return ret < 0 ? ret : 0;
1282 }
1283 
1284 int cx231xx_s_register(struct file *file, void *priv,
1285 			     const struct v4l2_dbg_register *reg)
1286 {
1287 	struct cx231xx *dev = video_drvdata(file);
1288 	int ret;
1289 	u8 data[4] = { 0, 0, 0, 0 };
1290 
1291 	switch (reg->match.addr) {
1292 	case 0:	/* cx231xx internal registers */
1293 		data[0] = (u8) reg->val;
1294 		data[1] = (u8) (reg->val >> 8);
1295 		data[2] = (u8) (reg->val >> 16);
1296 		data[3] = (u8) (reg->val >> 24);
1297 		ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1298 				(u16)reg->reg, data, 4);
1299 		break;
1300 	case 1:	/* AFE - write byte */
1301 		ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1302 				(u16)reg->reg, 2, reg->val, 1);
1303 		break;
1304 	case 2:	/* Video Block - write byte */
1305 		ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1306 				(u16)reg->reg, 2, reg->val, 1);
1307 		break;
1308 	case 3:	/* I2S block - write byte */
1309 		ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1310 				(u16)reg->reg, 1, reg->val, 1);
1311 		break;
1312 	case 4: /* AFE - write dword */
1313 		ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1314 				(u16)reg->reg, 2, reg->val, 4);
1315 		break;
1316 	case 5: /* Video Block - write dword */
1317 		ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1318 				(u16)reg->reg, 2, reg->val, 4);
1319 		break;
1320 	case 6: /* I2S block - write dword */
1321 		ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1322 				(u16)reg->reg, 1, reg->val, 4);
1323 		break;
1324 	default:
1325 		return -EINVAL;
1326 	}
1327 	return ret < 0 ? ret : 0;
1328 }
1329 #endif
1330 
1331 static int vidioc_g_pixelaspect(struct file *file, void *priv,
1332 				int type, struct v4l2_fract *f)
1333 {
1334 	struct cx231xx *dev = video_drvdata(file);
1335 	bool is_50hz = dev->norm & V4L2_STD_625_50;
1336 
1337 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1338 		return -EINVAL;
1339 
1340 	f->numerator = is_50hz ? 54 : 11;
1341 	f->denominator = is_50hz ? 59 : 10;
1342 
1343 	return 0;
1344 }
1345 
1346 static int vidioc_g_selection(struct file *file, void *priv,
1347 			      struct v4l2_selection *s)
1348 {
1349 	struct cx231xx *dev = video_drvdata(file);
1350 
1351 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1352 		return -EINVAL;
1353 
1354 	switch (s->target) {
1355 	case V4L2_SEL_TGT_CROP_BOUNDS:
1356 	case V4L2_SEL_TGT_CROP_DEFAULT:
1357 		s->r.left = 0;
1358 		s->r.top = 0;
1359 		s->r.width = dev->width;
1360 		s->r.height = dev->height;
1361 		break;
1362 	default:
1363 		return -EINVAL;
1364 	}
1365 	return 0;
1366 }
1367 
1368 int cx231xx_querycap(struct file *file, void *priv,
1369 			   struct v4l2_capability *cap)
1370 {
1371 	struct cx231xx *dev = video_drvdata(file);
1372 
1373 	strscpy(cap->driver, "cx231xx", sizeof(cap->driver));
1374 	strscpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
1375 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
1376 	cap->capabilities = V4L2_CAP_READWRITE |
1377 		V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1378 		V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
1379 	if (video_is_registered(&dev->radio_dev))
1380 		cap->capabilities |= V4L2_CAP_RADIO;
1381 
1382 	switch (dev->model) {
1383 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1384 	case CX231XX_BOARD_HAUPPAUGE_935C:
1385 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1386 	case CX231XX_BOARD_HAUPPAUGE_975:
1387 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1388 		cap->capabilities |= V4L2_CAP_TUNER;
1389 		break;
1390 	default:
1391 		if (dev->tuner_type != TUNER_ABSENT)
1392 			cap->capabilities |= V4L2_CAP_TUNER;
1393 		break;
1394 	}
1395 	return 0;
1396 }
1397 
1398 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1399 				   struct v4l2_fmtdesc *f)
1400 {
1401 	if (unlikely(f->index >= ARRAY_SIZE(format)))
1402 		return -EINVAL;
1403 
1404 	f->pixelformat = format[f->index].fourcc;
1405 
1406 	return 0;
1407 }
1408 
1409 /* RAW VBI ioctls */
1410 
1411 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1412 				struct v4l2_format *f)
1413 {
1414 	struct cx231xx *dev = video_drvdata(file);
1415 
1416 	f->fmt.vbi.sampling_rate = 6750000 * 4;
1417 	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1418 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1419 	f->fmt.vbi.offset = 0;
1420 	f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1421 	    PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1422 	f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1423 	    PAL_VBI_LINES : NTSC_VBI_LINES;
1424 	f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1425 	    PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1426 	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1427 	memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1428 
1429 	return 0;
1430 
1431 }
1432 
1433 static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
1434 				  struct v4l2_format *f)
1435 {
1436 	struct cx231xx *dev = video_drvdata(file);
1437 
1438 	f->fmt.vbi.sampling_rate = 6750000 * 4;
1439 	f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1440 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1441 	f->fmt.vbi.offset = 0;
1442 	f->fmt.vbi.flags = 0;
1443 	f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
1444 	    PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
1445 	f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
1446 	    PAL_VBI_LINES : NTSC_VBI_LINES;
1447 	f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
1448 	    PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
1449 	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1450 	memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1451 
1452 	return 0;
1453 
1454 }
1455 
1456 static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1457 				  struct v4l2_format *f)
1458 {
1459 	return vidioc_try_fmt_vbi_cap(file, priv, f);
1460 }
1461 
1462 /* ----------------------------------------------------------- */
1463 /* RADIO ESPECIFIC IOCTLS                                      */
1464 /* ----------------------------------------------------------- */
1465 
1466 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1467 {
1468 	struct cx231xx *dev = video_drvdata(file);
1469 
1470 	if (t->index)
1471 		return -EINVAL;
1472 
1473 	strscpy(t->name, "Radio", sizeof(t->name));
1474 
1475 	call_all(dev, tuner, g_tuner, t);
1476 
1477 	return 0;
1478 }
1479 static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
1480 {
1481 	struct cx231xx *dev = video_drvdata(file);
1482 
1483 	if (t->index)
1484 		return -EINVAL;
1485 
1486 	call_all(dev, tuner, s_tuner, t);
1487 
1488 	return 0;
1489 }
1490 
1491 /*
1492  * cx231xx_v4l2_open()
1493  * inits the device and starts isoc transfer
1494  */
1495 static int cx231xx_v4l2_open(struct file *filp)
1496 {
1497 	struct video_device *vdev = video_devdata(filp);
1498 	struct cx231xx *dev = video_drvdata(filp);
1499 	int ret;
1500 
1501 	if (mutex_lock_interruptible(&dev->lock))
1502 		return -ERESTARTSYS;
1503 
1504 	ret = v4l2_fh_open(filp);
1505 	if (ret) {
1506 		mutex_unlock(&dev->lock);
1507 		return ret;
1508 	}
1509 
1510 	if (dev->users++ == 0) {
1511 		/* Power up in Analog TV mode */
1512 		if (dev->board.external_av)
1513 			cx231xx_set_power_mode(dev,
1514 				 POLARIS_AVMODE_ENXTERNAL_AV);
1515 		else
1516 			cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
1517 
1518 		/* set video alternate setting */
1519 		cx231xx_set_video_alternate(dev);
1520 
1521 		/* Needed, since GPIO might have disabled power of
1522 		   some i2c device */
1523 		cx231xx_config_i2c(dev);
1524 
1525 		/* device needs to be initialized before isoc transfer */
1526 		dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
1527 	}
1528 
1529 	if (vdev->vfl_type == VFL_TYPE_RADIO) {
1530 		cx231xx_videodbg("video_open: setting radio device\n");
1531 
1532 		/* cx231xx_start_radio(dev); */
1533 
1534 		call_all(dev, tuner, s_radio);
1535 	}
1536 	if (vdev->vfl_type == VFL_TYPE_VBI) {
1537 		/* Set the required alternate setting  VBI interface works in
1538 		   Bulk mode only */
1539 		cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1540 	}
1541 	mutex_unlock(&dev->lock);
1542 	return 0;
1543 }
1544 
1545 /*
1546  * cx231xx_realease_resources()
1547  * unregisters the v4l2,i2c and usb devices
1548  * called when the device gets disconnected or at module unload
1549 */
1550 void cx231xx_release_analog_resources(struct cx231xx *dev)
1551 {
1552 
1553 	/*FIXME: I2C IR should be disconnected */
1554 
1555 	if (video_is_registered(&dev->radio_dev))
1556 		video_unregister_device(&dev->radio_dev);
1557 	if (video_is_registered(&dev->vbi_dev)) {
1558 		dev_info(dev->dev, "V4L2 device %s deregistered\n",
1559 			video_device_node_name(&dev->vbi_dev));
1560 		video_unregister_device(&dev->vbi_dev);
1561 	}
1562 	if (video_is_registered(&dev->vdev)) {
1563 		dev_info(dev->dev, "V4L2 device %s deregistered\n",
1564 			video_device_node_name(&dev->vdev));
1565 
1566 		if (dev->board.has_417)
1567 			cx231xx_417_unregister(dev);
1568 
1569 		video_unregister_device(&dev->vdev);
1570 	}
1571 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1572 	v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
1573 }
1574 
1575 /*
1576  * cx231xx_close()
1577  * stops streaming and deallocates all resources allocated by the v4l2
1578  * calls and ioctls
1579  */
1580 static int cx231xx_close(struct file *filp)
1581 {
1582 	struct cx231xx *dev = video_drvdata(filp);
1583 	struct video_device *vdev = video_devdata(filp);
1584 
1585 	_vb2_fop_release(filp, NULL);
1586 
1587 	if (--dev->users == 0) {
1588 		/* Save some power by putting tuner to sleep */
1589 		call_all(dev, tuner, standby);
1590 
1591 		/* do this before setting alternate! */
1592 		if (dev->USE_ISO)
1593 			cx231xx_uninit_isoc(dev);
1594 		else
1595 			cx231xx_uninit_bulk(dev);
1596 		cx231xx_set_mode(dev, CX231XX_SUSPEND);
1597 	}
1598 
1599 	/*
1600 	 * To workaround error number=-71 on EP0 for VideoGrabber,
1601 	 *	 need exclude following.
1602 	 * FIXME: It is probably safe to remove most of these, as we're
1603 	 * now avoiding the alternate setting for INDEX_VANC
1604 	 */
1605 	if (!dev->board.no_alt_vanc && vdev->vfl_type == VFL_TYPE_VBI) {
1606 		/* do this before setting alternate! */
1607 		cx231xx_uninit_vbi_isoc(dev);
1608 
1609 		/* set alternate 0 */
1610 		if (!dev->vbi_or_sliced_cc_mode)
1611 			cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1612 		else
1613 			cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1614 
1615 		wake_up_interruptible_nr(&dev->open, 1);
1616 		return 0;
1617 	}
1618 
1619 	if (dev->users == 0) {
1620 		/* set alternate 0 */
1621 		cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1622 	}
1623 
1624 	wake_up_interruptible(&dev->open);
1625 	return 0;
1626 }
1627 
1628 static int cx231xx_v4l2_close(struct file *filp)
1629 {
1630 	struct cx231xx *dev = video_drvdata(filp);
1631 	int rc;
1632 
1633 	mutex_lock(&dev->lock);
1634 	rc = cx231xx_close(filp);
1635 	mutex_unlock(&dev->lock);
1636 	return rc;
1637 }
1638 
1639 static const struct v4l2_file_operations cx231xx_v4l_fops = {
1640 	.owner   = THIS_MODULE,
1641 	.open    = cx231xx_v4l2_open,
1642 	.release = cx231xx_v4l2_close,
1643 	.read    = vb2_fop_read,
1644 	.poll    = vb2_fop_poll,
1645 	.mmap    = vb2_fop_mmap,
1646 	.unlocked_ioctl   = video_ioctl2,
1647 };
1648 
1649 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1650 	.vidioc_querycap               = cx231xx_querycap,
1651 	.vidioc_enum_fmt_vid_cap       = vidioc_enum_fmt_vid_cap,
1652 	.vidioc_g_fmt_vid_cap          = vidioc_g_fmt_vid_cap,
1653 	.vidioc_try_fmt_vid_cap        = vidioc_try_fmt_vid_cap,
1654 	.vidioc_s_fmt_vid_cap          = vidioc_s_fmt_vid_cap,
1655 	.vidioc_g_fmt_vbi_cap          = vidioc_g_fmt_vbi_cap,
1656 	.vidioc_try_fmt_vbi_cap        = vidioc_try_fmt_vbi_cap,
1657 	.vidioc_s_fmt_vbi_cap          = vidioc_s_fmt_vbi_cap,
1658 	.vidioc_g_pixelaspect          = vidioc_g_pixelaspect,
1659 	.vidioc_g_selection            = vidioc_g_selection,
1660 	.vidioc_reqbufs                = vb2_ioctl_reqbufs,
1661 	.vidioc_querybuf               = vb2_ioctl_querybuf,
1662 	.vidioc_qbuf                   = vb2_ioctl_qbuf,
1663 	.vidioc_dqbuf                  = vb2_ioctl_dqbuf,
1664 	.vidioc_s_std                  = vidioc_s_std,
1665 	.vidioc_g_std                  = vidioc_g_std,
1666 	.vidioc_enum_input             = cx231xx_enum_input,
1667 	.vidioc_g_input                = cx231xx_g_input,
1668 	.vidioc_s_input                = cx231xx_s_input,
1669 	.vidioc_streamon               = vb2_ioctl_streamon,
1670 	.vidioc_streamoff              = vb2_ioctl_streamoff,
1671 	.vidioc_g_tuner                = cx231xx_g_tuner,
1672 	.vidioc_s_tuner                = cx231xx_s_tuner,
1673 	.vidioc_g_frequency            = cx231xx_g_frequency,
1674 	.vidioc_s_frequency            = cx231xx_s_frequency,
1675 #ifdef CONFIG_VIDEO_ADV_DEBUG
1676 	.vidioc_g_chip_info            = cx231xx_g_chip_info,
1677 	.vidioc_g_register             = cx231xx_g_register,
1678 	.vidioc_s_register             = cx231xx_s_register,
1679 #endif
1680 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1681 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1682 };
1683 
1684 static struct video_device cx231xx_vbi_template;
1685 
1686 static const struct video_device cx231xx_video_template = {
1687 	.fops         = &cx231xx_v4l_fops,
1688 	.release      = video_device_release_empty,
1689 	.ioctl_ops    = &video_ioctl_ops,
1690 	.tvnorms      = V4L2_STD_ALL,
1691 };
1692 
1693 static const struct v4l2_file_operations radio_fops = {
1694 	.owner   = THIS_MODULE,
1695 	.open   = cx231xx_v4l2_open,
1696 	.release = cx231xx_v4l2_close,
1697 	.poll = v4l2_ctrl_poll,
1698 	.unlocked_ioctl = video_ioctl2,
1699 };
1700 
1701 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1702 	.vidioc_querycap    = cx231xx_querycap,
1703 	.vidioc_g_tuner     = radio_g_tuner,
1704 	.vidioc_s_tuner     = radio_s_tuner,
1705 	.vidioc_g_frequency = cx231xx_g_frequency,
1706 	.vidioc_s_frequency = cx231xx_s_frequency,
1707 #ifdef CONFIG_VIDEO_ADV_DEBUG
1708 	.vidioc_g_chip_info = cx231xx_g_chip_info,
1709 	.vidioc_g_register  = cx231xx_g_register,
1710 	.vidioc_s_register  = cx231xx_s_register,
1711 #endif
1712 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1713 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1714 };
1715 
1716 static struct video_device cx231xx_radio_template = {
1717 	.name      = "cx231xx-radio",
1718 	.fops      = &radio_fops,
1719 	.ioctl_ops = &radio_ioctl_ops,
1720 };
1721 
1722 /******************************** usb interface ******************************/
1723 
1724 static void cx231xx_vdev_init(struct cx231xx *dev,
1725 		struct video_device *vfd,
1726 		const struct video_device *template,
1727 		const char *type_name)
1728 {
1729 	*vfd = *template;
1730 	vfd->v4l2_dev = &dev->v4l2_dev;
1731 	vfd->release = video_device_release_empty;
1732 	vfd->lock = &dev->lock;
1733 
1734 	snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
1735 
1736 	video_set_drvdata(vfd, dev);
1737 	if (dev->tuner_type == TUNER_ABSENT) {
1738 		switch (dev->model) {
1739 		case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1740 		case CX231XX_BOARD_HAUPPAUGE_935C:
1741 		case CX231XX_BOARD_HAUPPAUGE_955Q:
1742 		case CX231XX_BOARD_HAUPPAUGE_975:
1743 		case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1744 			break;
1745 		default:
1746 			v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
1747 			v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
1748 			v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
1749 			v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
1750 			break;
1751 		}
1752 	}
1753 }
1754 
1755 int cx231xx_register_analog_devices(struct cx231xx *dev)
1756 {
1757 	struct vb2_queue *q;
1758 	int ret;
1759 
1760 	dev_info(dev->dev, "v4l2 driver version %s\n", CX231XX_VERSION);
1761 
1762 	/* set default norm */
1763 	dev->norm = V4L2_STD_PAL;
1764 	dev->width = norm_maxw(dev);
1765 	dev->height = norm_maxh(dev);
1766 	dev->interlaced = 0;
1767 
1768 	/* Analog specific initialization */
1769 	dev->format = &format[0];
1770 
1771 	/* Set the initial input */
1772 	video_mux(dev, dev->video_input);
1773 
1774 	call_all(dev, video, s_std, dev->norm);
1775 
1776 	v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
1777 	v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
1778 
1779 	if (dev->sd_cx25840) {
1780 		v4l2_ctrl_add_handler(&dev->ctrl_handler,
1781 				dev->sd_cx25840->ctrl_handler, NULL, true);
1782 		v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
1783 				dev->sd_cx25840->ctrl_handler,
1784 				v4l2_ctrl_radio_filter, true);
1785 	}
1786 
1787 	if (dev->ctrl_handler.error)
1788 		return dev->ctrl_handler.error;
1789 	if (dev->radio_ctrl_handler.error)
1790 		return dev->radio_ctrl_handler.error;
1791 
1792 	/* enable vbi capturing */
1793 	/* write code here...  */
1794 
1795 	/* allocate and fill video video_device struct */
1796 	cx231xx_vdev_init(dev, &dev->vdev, &cx231xx_video_template, "video");
1797 #if defined(CONFIG_MEDIA_CONTROLLER)
1798 	dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1799 	ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1800 	if (ret < 0)
1801 		dev_err(dev->dev, "failed to initialize video media entity!\n");
1802 #endif
1803 	dev->vdev.ctrl_handler = &dev->ctrl_handler;
1804 
1805 	q = &dev->vidq;
1806 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1807 	q->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF | VB2_READ;
1808 	q->drv_priv = dev;
1809 	q->buf_struct_size = sizeof(struct cx231xx_buffer);
1810 	q->ops = &cx231xx_video_qops;
1811 	q->mem_ops = &vb2_vmalloc_memops;
1812 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1813 	q->min_buffers_needed = 1;
1814 	q->lock = &dev->lock;
1815 	ret = vb2_queue_init(q);
1816 	if (ret)
1817 		return ret;
1818 	dev->vdev.queue = q;
1819 	dev->vdev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1820 				V4L2_CAP_VIDEO_CAPTURE;
1821 
1822 	switch (dev->model) { /* i2c device tuners */
1823 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1824 	case CX231XX_BOARD_HAUPPAUGE_935C:
1825 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1826 	case CX231XX_BOARD_HAUPPAUGE_975:
1827 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1828 		dev->vdev.device_caps |= V4L2_CAP_TUNER;
1829 		break;
1830 	default:
1831 		if (dev->tuner_type != TUNER_ABSENT)
1832 			dev->vdev.device_caps |= V4L2_CAP_TUNER;
1833 		break;
1834 	}
1835 
1836 	/* register v4l2 video video_device */
1837 	ret = video_register_device(&dev->vdev, VFL_TYPE_VIDEO,
1838 				    video_nr[dev->devno]);
1839 	if (ret) {
1840 		dev_err(dev->dev,
1841 			"unable to register video device (error=%i).\n",
1842 			ret);
1843 		return ret;
1844 	}
1845 
1846 	dev_info(dev->dev, "Registered video device %s [v4l2]\n",
1847 		video_device_node_name(&dev->vdev));
1848 
1849 	/* Initialize VBI template */
1850 	cx231xx_vbi_template = cx231xx_video_template;
1851 	strscpy(cx231xx_vbi_template.name, "cx231xx-vbi",
1852 		sizeof(cx231xx_vbi_template.name));
1853 
1854 	/* Allocate and fill vbi video_device struct */
1855 	cx231xx_vdev_init(dev, &dev->vbi_dev, &cx231xx_vbi_template, "vbi");
1856 
1857 #if defined(CONFIG_MEDIA_CONTROLLER)
1858 	dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1859 	ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1860 	if (ret < 0)
1861 		dev_err(dev->dev, "failed to initialize vbi media entity!\n");
1862 #endif
1863 	dev->vbi_dev.ctrl_handler = &dev->ctrl_handler;
1864 
1865 	q = &dev->vbiq;
1866 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1867 	q->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF | VB2_READ;
1868 	q->drv_priv = dev;
1869 	q->buf_struct_size = sizeof(struct cx231xx_buffer);
1870 	q->ops = &cx231xx_vbi_qops;
1871 	q->mem_ops = &vb2_vmalloc_memops;
1872 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1873 	q->min_buffers_needed = 1;
1874 	q->lock = &dev->lock;
1875 	ret = vb2_queue_init(q);
1876 	if (ret)
1877 		return ret;
1878 	dev->vbi_dev.queue = q;
1879 	dev->vbi_dev.device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1880 				   V4L2_CAP_VBI_CAPTURE;
1881 	switch (dev->model) { /* i2c device tuners */
1882 	case CX231XX_BOARD_HAUPPAUGE_930C_HD_1114xx:
1883 	case CX231XX_BOARD_HAUPPAUGE_935C:
1884 	case CX231XX_BOARD_HAUPPAUGE_955Q:
1885 	case CX231XX_BOARD_HAUPPAUGE_975:
1886 	case CX231XX_BOARD_EVROMEDIA_FULL_HYBRID_FULLHD:
1887 		dev->vbi_dev.device_caps |= V4L2_CAP_TUNER;
1888 		break;
1889 	default:
1890 		if (dev->tuner_type != TUNER_ABSENT)
1891 			dev->vbi_dev.device_caps |= V4L2_CAP_TUNER;
1892 	}
1893 
1894 	/* register v4l2 vbi video_device */
1895 	ret = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
1896 				    vbi_nr[dev->devno]);
1897 	if (ret < 0) {
1898 		dev_err(dev->dev, "unable to register vbi device\n");
1899 		return ret;
1900 	}
1901 
1902 	dev_info(dev->dev, "Registered VBI device %s\n",
1903 		video_device_node_name(&dev->vbi_dev));
1904 
1905 	if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
1906 		cx231xx_vdev_init(dev, &dev->radio_dev,
1907 				&cx231xx_radio_template, "radio");
1908 		dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
1909 		dev->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
1910 		ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
1911 					    radio_nr[dev->devno]);
1912 		if (ret < 0) {
1913 			dev_err(dev->dev,
1914 				"can't register radio device\n");
1915 			return ret;
1916 		}
1917 		dev_info(dev->dev, "Registered radio device as %s\n",
1918 			video_device_node_name(&dev->radio_dev));
1919 	}
1920 
1921 	return 0;
1922 }
1923