xref: /openbmc/linux/drivers/media/usb/au0828/au0828-video.c (revision c43875f66140f5457f90fc5f6f6840c74b2762cd)
1 /*
2  * Auvitek AU0828 USB Bridge (Analog video support)
3  *
4  * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
5  * Copyright (C) 2005-2008 Auvitek International, Ltd.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * As published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  */
22 
23 /* Developer Notes:
24  *
25  * The hardware scaler supported is unimplemented
26  * AC97 audio support is unimplemented (only i2s audio mode)
27  *
28  */
29 
30 #include "au0828.h"
31 
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/device.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-event.h>
39 #include <media/tuner.h>
40 #include "au0828-reg.h"
41 
42 static DEFINE_MUTEX(au0828_sysfs_lock);
43 
44 /* ------------------------------------------------------------------
45 	Videobuf operations
46    ------------------------------------------------------------------*/
47 
48 static unsigned int isoc_debug;
49 module_param(isoc_debug, int, 0644);
50 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
51 
52 #define au0828_isocdbg(fmt, arg...) \
53 do {\
54 	if (isoc_debug) { \
55 		pr_info("au0828 %s :"fmt, \
56 		       __func__ , ##arg);	   \
57 	} \
58   } while (0)
59 
60 static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
61 {
62 	if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
63 		dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
64 }
65 
66 static inline void print_err_status(struct au0828_dev *dev,
67 				    int packet, int status)
68 {
69 	char *errmsg = "Unknown";
70 
71 	switch (status) {
72 	case -ENOENT:
73 		errmsg = "unlinked synchronuously";
74 		break;
75 	case -ECONNRESET:
76 		errmsg = "unlinked asynchronuously";
77 		break;
78 	case -ENOSR:
79 		errmsg = "Buffer error (overrun)";
80 		break;
81 	case -EPIPE:
82 		errmsg = "Stalled (device not responding)";
83 		break;
84 	case -EOVERFLOW:
85 		errmsg = "Babble (bad cable?)";
86 		break;
87 	case -EPROTO:
88 		errmsg = "Bit-stuff error (bad cable?)";
89 		break;
90 	case -EILSEQ:
91 		errmsg = "CRC/Timeout (could be anything)";
92 		break;
93 	case -ETIME:
94 		errmsg = "Device does not respond";
95 		break;
96 	}
97 	if (packet < 0) {
98 		au0828_isocdbg("URB status %d [%s].\n",	status, errmsg);
99 	} else {
100 		au0828_isocdbg("URB packet %d, status %d [%s].\n",
101 			       packet, status, errmsg);
102 	}
103 }
104 
105 static int check_dev(struct au0828_dev *dev)
106 {
107 	if (dev->dev_state & DEV_DISCONNECTED) {
108 		pr_info("v4l2 ioctl: device not present\n");
109 		return -ENODEV;
110 	}
111 
112 	if (dev->dev_state & DEV_MISCONFIGURED) {
113 		pr_info("v4l2 ioctl: device is misconfigured; "
114 		       "close and open it again\n");
115 		return -EIO;
116 	}
117 	return 0;
118 }
119 
120 /*
121  * IRQ callback, called by URB callback
122  */
123 static void au0828_irq_callback(struct urb *urb)
124 {
125 	struct au0828_dmaqueue  *dma_q = urb->context;
126 	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
127 	unsigned long flags = 0;
128 	int i;
129 
130 	switch (urb->status) {
131 	case 0:             /* success */
132 	case -ETIMEDOUT:    /* NAK */
133 		break;
134 	case -ECONNRESET:   /* kill */
135 	case -ENOENT:
136 	case -ESHUTDOWN:
137 		au0828_isocdbg("au0828_irq_callback called: status kill\n");
138 		return;
139 	default:            /* unknown error */
140 		au0828_isocdbg("urb completition error %d.\n", urb->status);
141 		break;
142 	}
143 
144 	/* Copy data from URB */
145 	spin_lock_irqsave(&dev->slock, flags);
146 	dev->isoc_ctl.isoc_copy(dev, urb);
147 	spin_unlock_irqrestore(&dev->slock, flags);
148 
149 	/* Reset urb buffers */
150 	for (i = 0; i < urb->number_of_packets; i++) {
151 		urb->iso_frame_desc[i].status = 0;
152 		urb->iso_frame_desc[i].actual_length = 0;
153 	}
154 	urb->status = 0;
155 
156 	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
157 	if (urb->status) {
158 		au0828_isocdbg("urb resubmit failed (error=%i)\n",
159 			       urb->status);
160 	}
161 	dev->stream_state = STREAM_ON;
162 }
163 
164 /*
165  * Stop and Deallocate URBs
166  */
167 static void au0828_uninit_isoc(struct au0828_dev *dev)
168 {
169 	struct urb *urb;
170 	int i;
171 
172 	au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
173 
174 	dev->isoc_ctl.nfields = -1;
175 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
176 		urb = dev->isoc_ctl.urb[i];
177 		if (urb) {
178 			if (!irqs_disabled())
179 				usb_kill_urb(urb);
180 			else
181 				usb_unlink_urb(urb);
182 
183 			if (dev->isoc_ctl.transfer_buffer[i]) {
184 				usb_free_coherent(dev->usbdev,
185 					urb->transfer_buffer_length,
186 					dev->isoc_ctl.transfer_buffer[i],
187 					urb->transfer_dma);
188 			}
189 			usb_free_urb(urb);
190 			dev->isoc_ctl.urb[i] = NULL;
191 		}
192 		dev->isoc_ctl.transfer_buffer[i] = NULL;
193 	}
194 
195 	kfree(dev->isoc_ctl.urb);
196 	kfree(dev->isoc_ctl.transfer_buffer);
197 
198 	dev->isoc_ctl.urb = NULL;
199 	dev->isoc_ctl.transfer_buffer = NULL;
200 	dev->isoc_ctl.num_bufs = 0;
201 
202 	dev->stream_state = STREAM_OFF;
203 }
204 
205 /*
206  * Allocate URBs and start IRQ
207  */
208 static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
209 			    int num_bufs, int max_pkt_size,
210 			    int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
211 {
212 	struct au0828_dmaqueue *dma_q = &dev->vidq;
213 	int i;
214 	int sb_size, pipe;
215 	struct urb *urb;
216 	int j, k;
217 	int rc;
218 
219 	au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
220 
221 	dev->isoc_ctl.isoc_copy = isoc_copy;
222 	dev->isoc_ctl.num_bufs = num_bufs;
223 
224 	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
225 	if (!dev->isoc_ctl.urb) {
226 		au0828_isocdbg("cannot alloc memory for usb buffers\n");
227 		return -ENOMEM;
228 	}
229 
230 	dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
231 					      GFP_KERNEL);
232 	if (!dev->isoc_ctl.transfer_buffer) {
233 		au0828_isocdbg("cannot allocate memory for usb transfer\n");
234 		kfree(dev->isoc_ctl.urb);
235 		return -ENOMEM;
236 	}
237 
238 	dev->isoc_ctl.max_pkt_size = max_pkt_size;
239 	dev->isoc_ctl.buf = NULL;
240 
241 	sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
242 
243 	/* allocate urbs and transfer buffers */
244 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
245 		urb = usb_alloc_urb(max_packets, GFP_KERNEL);
246 		if (!urb) {
247 			au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
248 			au0828_uninit_isoc(dev);
249 			return -ENOMEM;
250 		}
251 		dev->isoc_ctl.urb[i] = urb;
252 
253 		dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
254 			sb_size, GFP_KERNEL, &urb->transfer_dma);
255 		if (!dev->isoc_ctl.transfer_buffer[i]) {
256 			printk("unable to allocate %i bytes for transfer"
257 					" buffer %i%s\n",
258 					sb_size, i,
259 					in_interrupt() ? " while in int" : "");
260 			au0828_uninit_isoc(dev);
261 			return -ENOMEM;
262 		}
263 		memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
264 
265 		pipe = usb_rcvisocpipe(dev->usbdev,
266 				       dev->isoc_in_endpointaddr),
267 
268 		usb_fill_int_urb(urb, dev->usbdev, pipe,
269 				 dev->isoc_ctl.transfer_buffer[i], sb_size,
270 				 au0828_irq_callback, dma_q, 1);
271 
272 		urb->number_of_packets = max_packets;
273 		urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
274 
275 		k = 0;
276 		for (j = 0; j < max_packets; j++) {
277 			urb->iso_frame_desc[j].offset = k;
278 			urb->iso_frame_desc[j].length =
279 						dev->isoc_ctl.max_pkt_size;
280 			k += dev->isoc_ctl.max_pkt_size;
281 		}
282 	}
283 
284 	/* submit urbs and enables IRQ */
285 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
286 		rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
287 		if (rc) {
288 			au0828_isocdbg("submit of urb %i failed (error=%i)\n",
289 				       i, rc);
290 			au0828_uninit_isoc(dev);
291 			return rc;
292 		}
293 	}
294 
295 	return 0;
296 }
297 
298 /*
299  * Announces that a buffer were filled and request the next
300  */
301 static inline void buffer_filled(struct au0828_dev *dev,
302 				 struct au0828_dmaqueue *dma_q,
303 				 struct au0828_buffer *buf)
304 {
305 	struct vb2_v4l2_buffer *vb = &buf->vb;
306 	struct vb2_queue *q = vb->vb2_buf.vb2_queue;
307 
308 	/* Advice that buffer was filled */
309 	au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
310 
311 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
312 		vb->sequence = dev->frame_count++;
313 	else
314 		vb->sequence = dev->vbi_frame_count++;
315 
316 	vb->field = V4L2_FIELD_INTERLACED;
317 	vb->vb2_buf.timestamp = ktime_get_ns();
318 	vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
319 }
320 
321 /*
322  * Identify the buffer header type and properly handles
323  */
324 static void au0828_copy_video(struct au0828_dev *dev,
325 			      struct au0828_dmaqueue  *dma_q,
326 			      struct au0828_buffer *buf,
327 			      unsigned char *p,
328 			      unsigned char *outp, unsigned long len)
329 {
330 	void *fieldstart, *startwrite, *startread;
331 	int  linesdone, currlinedone, offset, lencopy, remain;
332 	int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
333 
334 	if (len == 0)
335 		return;
336 
337 	if (dma_q->pos + len > buf->length)
338 		len = buf->length - dma_q->pos;
339 
340 	startread = p;
341 	remain = len;
342 
343 	/* Interlaces frame */
344 	if (buf->top_field)
345 		fieldstart = outp;
346 	else
347 		fieldstart = outp + bytesperline;
348 
349 	linesdone = dma_q->pos / bytesperline;
350 	currlinedone = dma_q->pos % bytesperline;
351 	offset = linesdone * bytesperline * 2 + currlinedone;
352 	startwrite = fieldstart + offset;
353 	lencopy = bytesperline - currlinedone;
354 	lencopy = lencopy > remain ? remain : lencopy;
355 
356 	if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
357 		au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
358 			       ((char *)startwrite + lencopy) -
359 			       ((char *)outp + buf->length));
360 		remain = (char *)outp + buf->length - (char *)startwrite;
361 		lencopy = remain;
362 	}
363 	if (lencopy <= 0)
364 		return;
365 	memcpy(startwrite, startread, lencopy);
366 
367 	remain -= lencopy;
368 
369 	while (remain > 0) {
370 		startwrite += lencopy + bytesperline;
371 		startread += lencopy;
372 		if (bytesperline > remain)
373 			lencopy = remain;
374 		else
375 			lencopy = bytesperline;
376 
377 		if ((char *)startwrite + lencopy > (char *)outp +
378 		    buf->length) {
379 			au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
380 				       ((char *)startwrite + lencopy) -
381 				       ((char *)outp + buf->length));
382 			lencopy = remain = (char *)outp + buf->length -
383 					   (char *)startwrite;
384 		}
385 		if (lencopy <= 0)
386 			break;
387 
388 		memcpy(startwrite, startread, lencopy);
389 
390 		remain -= lencopy;
391 	}
392 
393 	if (offset > 1440) {
394 		/* We have enough data to check for greenscreen */
395 		if (outp[0] < 0x60 && outp[1440] < 0x60)
396 			dev->greenscreen_detected = 1;
397 	}
398 
399 	dma_q->pos += len;
400 }
401 
402 /*
403  * video-buf generic routine to get the next available buffer
404  */
405 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
406 				struct au0828_buffer **buf)
407 {
408 	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
409 
410 	if (list_empty(&dma_q->active)) {
411 		au0828_isocdbg("No active queue to serve\n");
412 		dev->isoc_ctl.buf = NULL;
413 		*buf = NULL;
414 		return;
415 	}
416 
417 	/* Get the next buffer */
418 	*buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
419 	/* Cleans up buffer - Useful for testing for frame/URB loss */
420 	list_del(&(*buf)->list);
421 	dma_q->pos = 0;
422 	(*buf)->vb_buf = (*buf)->mem;
423 	dev->isoc_ctl.buf = *buf;
424 
425 	return;
426 }
427 
428 static void au0828_copy_vbi(struct au0828_dev *dev,
429 			      struct au0828_dmaqueue  *dma_q,
430 			      struct au0828_buffer *buf,
431 			      unsigned char *p,
432 			      unsigned char *outp, unsigned long len)
433 {
434 	unsigned char *startwrite, *startread;
435 	int bytesperline;
436 	int i, j = 0;
437 
438 	if (dev == NULL) {
439 		au0828_isocdbg("dev is null\n");
440 		return;
441 	}
442 
443 	if (dma_q == NULL) {
444 		au0828_isocdbg("dma_q is null\n");
445 		return;
446 	}
447 	if (buf == NULL)
448 		return;
449 	if (p == NULL) {
450 		au0828_isocdbg("p is null\n");
451 		return;
452 	}
453 	if (outp == NULL) {
454 		au0828_isocdbg("outp is null\n");
455 		return;
456 	}
457 
458 	bytesperline = dev->vbi_width;
459 
460 	if (dma_q->pos + len > buf->length)
461 		len = buf->length - dma_q->pos;
462 
463 	startread = p;
464 	startwrite = outp + (dma_q->pos / 2);
465 
466 	/* Make sure the bottom field populates the second half of the frame */
467 	if (buf->top_field == 0)
468 		startwrite += bytesperline * dev->vbi_height;
469 
470 	for (i = 0; i < len; i += 2)
471 		startwrite[j++] = startread[i+1];
472 
473 	dma_q->pos += len;
474 }
475 
476 
477 /*
478  * video-buf generic routine to get the next available VBI buffer
479  */
480 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
481 				    struct au0828_buffer **buf)
482 {
483 	struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
484 
485 	if (list_empty(&dma_q->active)) {
486 		au0828_isocdbg("No active queue to serve\n");
487 		dev->isoc_ctl.vbi_buf = NULL;
488 		*buf = NULL;
489 		return;
490 	}
491 
492 	/* Get the next buffer */
493 	*buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
494 	/* Cleans up buffer - Useful for testing for frame/URB loss */
495 	list_del(&(*buf)->list);
496 	dma_q->pos = 0;
497 	(*buf)->vb_buf = (*buf)->mem;
498 	dev->isoc_ctl.vbi_buf = *buf;
499 	return;
500 }
501 
502 /*
503  * Controls the isoc copy of each urb packet
504  */
505 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
506 {
507 	struct au0828_buffer    *buf;
508 	struct au0828_buffer    *vbi_buf;
509 	struct au0828_dmaqueue  *dma_q = urb->context;
510 	struct au0828_dmaqueue  *vbi_dma_q = &dev->vbiq;
511 	unsigned char *outp = NULL;
512 	unsigned char *vbioutp = NULL;
513 	int i, len = 0, rc = 1;
514 	unsigned char *p;
515 	unsigned char fbyte;
516 	unsigned int vbi_field_size;
517 	unsigned int remain, lencopy;
518 
519 	if (!dev)
520 		return 0;
521 
522 	if ((dev->dev_state & DEV_DISCONNECTED) ||
523 	    (dev->dev_state & DEV_MISCONFIGURED))
524 		return 0;
525 
526 	if (urb->status < 0) {
527 		print_err_status(dev, -1, urb->status);
528 		if (urb->status == -ENOENT)
529 			return 0;
530 	}
531 
532 	buf = dev->isoc_ctl.buf;
533 	if (buf != NULL)
534 		outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
535 
536 	vbi_buf = dev->isoc_ctl.vbi_buf;
537 	if (vbi_buf != NULL)
538 		vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
539 
540 	for (i = 0; i < urb->number_of_packets; i++) {
541 		int status = urb->iso_frame_desc[i].status;
542 
543 		if (status < 0) {
544 			print_err_status(dev, i, status);
545 			if (urb->iso_frame_desc[i].status != -EPROTO)
546 				continue;
547 		}
548 
549 		if (urb->iso_frame_desc[i].actual_length <= 0)
550 			continue;
551 
552 		if (urb->iso_frame_desc[i].actual_length >
553 						dev->max_pkt_size) {
554 			au0828_isocdbg("packet bigger than packet size");
555 			continue;
556 		}
557 
558 		p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
559 		fbyte = p[0];
560 		len = urb->iso_frame_desc[i].actual_length - 4;
561 		p += 4;
562 
563 		if (fbyte & 0x80) {
564 			len -= 4;
565 			p += 4;
566 			au0828_isocdbg("Video frame %s\n",
567 				       (fbyte & 0x40) ? "odd" : "even");
568 			if (fbyte & 0x40) {
569 				/* VBI */
570 				if (vbi_buf != NULL)
571 					buffer_filled(dev, vbi_dma_q, vbi_buf);
572 				vbi_get_next_buf(vbi_dma_q, &vbi_buf);
573 				if (vbi_buf == NULL)
574 					vbioutp = NULL;
575 				else
576 					vbioutp = vb2_plane_vaddr(
577 						&vbi_buf->vb.vb2_buf, 0);
578 
579 				/* Video */
580 				if (buf != NULL)
581 					buffer_filled(dev, dma_q, buf);
582 				get_next_buf(dma_q, &buf);
583 				if (buf == NULL)
584 					outp = NULL;
585 				else
586 					outp = vb2_plane_vaddr(
587 						&buf->vb.vb2_buf, 0);
588 
589 				/* As long as isoc traffic is arriving, keep
590 				   resetting the timer */
591 				if (dev->vid_timeout_running)
592 					mod_timer(&dev->vid_timeout,
593 						  jiffies + (HZ / 10));
594 				if (dev->vbi_timeout_running)
595 					mod_timer(&dev->vbi_timeout,
596 						  jiffies + (HZ / 10));
597 			}
598 
599 			if (buf != NULL) {
600 				if (fbyte & 0x40)
601 					buf->top_field = 1;
602 				else
603 					buf->top_field = 0;
604 			}
605 
606 			if (vbi_buf != NULL) {
607 				if (fbyte & 0x40)
608 					vbi_buf->top_field = 1;
609 				else
610 					vbi_buf->top_field = 0;
611 			}
612 
613 			dev->vbi_read = 0;
614 			vbi_dma_q->pos = 0;
615 			dma_q->pos = 0;
616 		}
617 
618 		vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
619 		if (dev->vbi_read < vbi_field_size) {
620 			remain  = vbi_field_size - dev->vbi_read;
621 			if (len < remain)
622 				lencopy = len;
623 			else
624 				lencopy = remain;
625 
626 			if (vbi_buf != NULL)
627 				au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
628 						vbioutp, len);
629 
630 			len -= lencopy;
631 			p += lencopy;
632 			dev->vbi_read += lencopy;
633 		}
634 
635 		if (dev->vbi_read >= vbi_field_size && buf != NULL)
636 			au0828_copy_video(dev, dma_q, buf, p, outp, len);
637 	}
638 	return rc;
639 }
640 
641 void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
642 {
643 #ifdef CONFIG_MEDIA_CONTROLLER
644 	int i;
645 
646 	for (i = 0; i < AU0828_MAX_INPUT; i++) {
647 		if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
648 			return;
649 		media_device_unregister_entity(&dev->input_ent[i]);
650 	}
651 #endif
652 }
653 
654 static int au0828_create_media_graph(struct au0828_dev *dev)
655 {
656 #ifdef CONFIG_MEDIA_CONTROLLER
657 	struct media_device *mdev = dev->media_dev;
658 	struct media_entity *entity;
659 	struct media_entity *tuner = NULL, *decoder = NULL;
660 	int i, ret;
661 
662 	if (!mdev)
663 		return 0;
664 
665 	media_device_for_each_entity(entity, mdev) {
666 		switch (entity->function) {
667 		case MEDIA_ENT_F_TUNER:
668 			tuner = entity;
669 			break;
670 		case MEDIA_ENT_F_ATV_DECODER:
671 			decoder = entity;
672 			break;
673 		}
674 	}
675 
676 	/* Analog setup, using tuner as a link */
677 
678 	/* Something bad happened! */
679 	if (!decoder)
680 		return -EINVAL;
681 
682 	if (tuner) {
683 		ret = media_create_pad_link(tuner, TUNER_PAD_OUTPUT,
684 					    decoder, 0,
685 					    MEDIA_LNK_FL_ENABLED);
686 		if (ret)
687 			return ret;
688 	}
689 	ret = media_create_pad_link(decoder, 1, &dev->vdev.entity, 0,
690 				    MEDIA_LNK_FL_ENABLED);
691 	if (ret)
692 		return ret;
693 	ret = media_create_pad_link(decoder, 2, &dev->vbi_dev.entity, 0,
694 				    MEDIA_LNK_FL_ENABLED);
695 	if (ret)
696 		return ret;
697 
698 	for (i = 0; i < AU0828_MAX_INPUT; i++) {
699 		struct media_entity *ent = &dev->input_ent[i];
700 
701 		switch (AUVI_INPUT(i).type) {
702 		case AU0828_VMUX_UNDEFINED:
703 			break;
704 		case AU0828_VMUX_CABLE:
705 		case AU0828_VMUX_TELEVISION:
706 		case AU0828_VMUX_DVB:
707 			if (!tuner)
708 				break;
709 
710 			ret = media_create_pad_link(ent, 0, tuner,
711 						    TUNER_PAD_RF_INPUT,
712 						    MEDIA_LNK_FL_ENABLED);
713 			if (ret)
714 				return ret;
715 			break;
716 		case AU0828_VMUX_COMPOSITE:
717 		case AU0828_VMUX_SVIDEO:
718 			/* FIXME: fix the decoder PAD */
719 			ret = media_create_pad_link(ent, 0, decoder, 0, 0);
720 			if (ret)
721 				return ret;
722 			break;
723 		}
724 	}
725 #endif
726 	return 0;
727 }
728 
729 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
730 {
731 	struct au0828_dev *dev =
732 		container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
733 
734 	v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
735 	v4l2_device_unregister(&dev->v4l2_dev);
736 	au0828_usb_v4l2_media_release(dev);
737 	au0828_usb_release(dev);
738 }
739 
740 int au0828_v4l2_device_register(struct usb_interface *interface,
741 				struct au0828_dev *dev)
742 {
743 	int retval;
744 
745 	if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
746 		return 0;
747 
748 	/* Create the v4l2_device */
749 #ifdef CONFIG_MEDIA_CONTROLLER
750 	dev->v4l2_dev.mdev = dev->media_dev;
751 #endif
752 	retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
753 	if (retval) {
754 		pr_err("%s() v4l2_device_register failed\n",
755 		       __func__);
756 		mutex_unlock(&dev->lock);
757 		kfree(dev);
758 		return retval;
759 	}
760 
761 	dev->v4l2_dev.release = au0828_usb_v4l2_release;
762 
763 	/* This control handler will inherit the controls from au8522 */
764 	retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
765 	if (retval) {
766 		pr_err("%s() v4l2_ctrl_handler_init failed\n",
767 		       __func__);
768 		mutex_unlock(&dev->lock);
769 		kfree(dev);
770 		return retval;
771 	}
772 	dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
773 
774 	return 0;
775 }
776 
777 static int au0828_enable_analog_tuner(struct au0828_dev *dev)
778 {
779 #ifdef CONFIG_MEDIA_CONTROLLER
780 	struct media_device *mdev = dev->media_dev;
781 	struct media_entity *source;
782 	struct media_link *link, *found_link = NULL;
783 	int ret, active_links = 0;
784 
785 	if (!mdev || !dev->decoder)
786 		return 0;
787 
788 	/*
789 	 * This will find the tuner that is connected into the decoder.
790 	 * Technically, this is not 100% correct, as the device may be
791 	 * using an analog input instead of the tuner. However, as we can't
792 	 * do DVB streaming while the DMA engine is being used for V4L2,
793 	 * this should be enough for the actual needs.
794 	 */
795 	list_for_each_entry(link, &dev->decoder->links, list) {
796 		if (link->sink->entity == dev->decoder) {
797 			found_link = link;
798 			if (link->flags & MEDIA_LNK_FL_ENABLED)
799 				active_links++;
800 			break;
801 		}
802 	}
803 
804 	if (active_links == 1 || !found_link)
805 		return 0;
806 
807 	source = found_link->source->entity;
808 	list_for_each_entry(link, &source->links, list) {
809 		struct media_entity *sink;
810 		int flags = 0;
811 
812 		sink = link->sink->entity;
813 
814 		if (sink == dev->decoder)
815 			flags = MEDIA_LNK_FL_ENABLED;
816 
817 		ret = media_entity_setup_link(link, flags);
818 		if (ret) {
819 			pr_err(
820 				"Couldn't change link %s->%s to %s. Error %d\n",
821 				source->name, sink->name,
822 				flags ? "enabled" : "disabled",
823 				ret);
824 			return ret;
825 		} else
826 			au0828_isocdbg(
827 				"link %s->%s was %s\n",
828 				source->name, sink->name,
829 				flags ? "ENABLED" : "disabled");
830 	}
831 #endif
832 	return 0;
833 }
834 
835 static int queue_setup(struct vb2_queue *vq,
836 		       unsigned int *nbuffers, unsigned int *nplanes,
837 		       unsigned int sizes[], void *alloc_ctxs[])
838 {
839 	struct au0828_dev *dev = vb2_get_drv_priv(vq);
840 	unsigned long size = dev->height * dev->bytesperline;
841 
842 	if (*nplanes)
843 		return sizes[0] < size ? -EINVAL : 0;
844 	*nplanes = 1;
845 	sizes[0] = size;
846 
847 	au0828_enable_analog_tuner(dev);
848 
849 	return 0;
850 }
851 
852 static int
853 buffer_prepare(struct vb2_buffer *vb)
854 {
855 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
856 	struct au0828_buffer *buf = container_of(vbuf,
857 				struct au0828_buffer, vb);
858 	struct au0828_dev    *dev = vb2_get_drv_priv(vb->vb2_queue);
859 
860 	buf->length = dev->height * dev->bytesperline;
861 
862 	if (vb2_plane_size(vb, 0) < buf->length) {
863 		pr_err("%s data will not fit into plane (%lu < %lu)\n",
864 			__func__, vb2_plane_size(vb, 0), buf->length);
865 		return -EINVAL;
866 	}
867 	vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
868 	return 0;
869 }
870 
871 static void
872 buffer_queue(struct vb2_buffer *vb)
873 {
874 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
875 	struct au0828_buffer    *buf     = container_of(vbuf,
876 							struct au0828_buffer,
877 							vb);
878 	struct au0828_dev       *dev     = vb2_get_drv_priv(vb->vb2_queue);
879 	struct au0828_dmaqueue  *vidq    = &dev->vidq;
880 	unsigned long flags = 0;
881 
882 	buf->mem = vb2_plane_vaddr(vb, 0);
883 	buf->length = vb2_plane_size(vb, 0);
884 
885 	spin_lock_irqsave(&dev->slock, flags);
886 	list_add_tail(&buf->list, &vidq->active);
887 	spin_unlock_irqrestore(&dev->slock, flags);
888 }
889 
890 static int au0828_i2s_init(struct au0828_dev *dev)
891 {
892 	/* Enable i2s mode */
893 	au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
894 	return 0;
895 }
896 
897 /*
898  * Auvitek au0828 analog stream enable
899  */
900 static int au0828_analog_stream_enable(struct au0828_dev *d)
901 {
902 	struct usb_interface *iface;
903 	int ret, h, w;
904 
905 	dprintk(1, "au0828_analog_stream_enable called\n");
906 
907 	iface = usb_ifnum_to_if(d->usbdev, 0);
908 	if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
909 		dprintk(1, "Changing intf#0 to alt 5\n");
910 		/* set au0828 interface0 to AS5 here again */
911 		ret = usb_set_interface(d->usbdev, 0, 5);
912 		if (ret < 0) {
913 			pr_info("Au0828 can't set alt setting to 5!\n");
914 			return -EBUSY;
915 		}
916 	}
917 
918 	h = d->height / 2 + 2;
919 	w = d->width * 2;
920 
921 	au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
922 	au0828_writereg(d, 0x106, 0x00);
923 	/* set x position */
924 	au0828_writereg(d, 0x110, 0x00);
925 	au0828_writereg(d, 0x111, 0x00);
926 	au0828_writereg(d, 0x114, w & 0xff);
927 	au0828_writereg(d, 0x115, w >> 8);
928 	/* set y position */
929 	au0828_writereg(d, 0x112, 0x00);
930 	au0828_writereg(d, 0x113, 0x00);
931 	au0828_writereg(d, 0x116, h & 0xff);
932 	au0828_writereg(d, 0x117, h >> 8);
933 	au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
934 
935 	return 0;
936 }
937 
938 static int au0828_analog_stream_disable(struct au0828_dev *d)
939 {
940 	dprintk(1, "au0828_analog_stream_disable called\n");
941 	au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
942 	return 0;
943 }
944 
945 static void au0828_analog_stream_reset(struct au0828_dev *dev)
946 {
947 	dprintk(1, "au0828_analog_stream_reset called\n");
948 	au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
949 	mdelay(30);
950 	au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
951 }
952 
953 /*
954  * Some operations needs to stop current streaming
955  */
956 static int au0828_stream_interrupt(struct au0828_dev *dev)
957 {
958 	int ret = 0;
959 
960 	dev->stream_state = STREAM_INTERRUPT;
961 	if (dev->dev_state == DEV_DISCONNECTED)
962 		return -ENODEV;
963 	else if (ret) {
964 		dev->dev_state = DEV_MISCONFIGURED;
965 		dprintk(1, "%s device is misconfigured!\n", __func__);
966 		return ret;
967 	}
968 	return 0;
969 }
970 
971 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
972 {
973 	struct au0828_dev *dev = vb2_get_drv_priv(vq);
974 	int rc = 0;
975 
976 	dprintk(1, "au0828_start_analog_streaming called %d\n",
977 		dev->streaming_users);
978 
979 	if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
980 		dev->frame_count = 0;
981 	else
982 		dev->vbi_frame_count = 0;
983 
984 	if (dev->streaming_users == 0) {
985 		/* If we were doing ac97 instead of i2s, it would go here...*/
986 		au0828_i2s_init(dev);
987 		rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
988 				   AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
989 				   au0828_isoc_copy);
990 		if (rc < 0) {
991 			pr_info("au0828_init_isoc failed\n");
992 			return rc;
993 		}
994 
995 		if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
996 			v4l2_device_call_all(&dev->v4l2_dev, 0, video,
997 						s_stream, 1);
998 			dev->vid_timeout_running = 1;
999 			mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1000 		} else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1001 			dev->vbi_timeout_running = 1;
1002 			mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1003 		}
1004 	}
1005 	dev->streaming_users++;
1006 	return rc;
1007 }
1008 
1009 static void au0828_stop_streaming(struct vb2_queue *vq)
1010 {
1011 	struct au0828_dev *dev = vb2_get_drv_priv(vq);
1012 	struct au0828_dmaqueue *vidq = &dev->vidq;
1013 	unsigned long flags = 0;
1014 
1015 	dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
1016 
1017 	if (dev->streaming_users-- == 1)
1018 		au0828_uninit_isoc(dev);
1019 
1020 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1021 	dev->vid_timeout_running = 0;
1022 	del_timer_sync(&dev->vid_timeout);
1023 
1024 	spin_lock_irqsave(&dev->slock, flags);
1025 	if (dev->isoc_ctl.buf != NULL) {
1026 		vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
1027 				VB2_BUF_STATE_ERROR);
1028 		dev->isoc_ctl.buf = NULL;
1029 	}
1030 	while (!list_empty(&vidq->active)) {
1031 		struct au0828_buffer *buf;
1032 
1033 		buf = list_entry(vidq->active.next, struct au0828_buffer, list);
1034 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1035 		list_del(&buf->list);
1036 	}
1037 	spin_unlock_irqrestore(&dev->slock, flags);
1038 }
1039 
1040 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
1041 {
1042 	struct au0828_dev *dev = vb2_get_drv_priv(vq);
1043 	struct au0828_dmaqueue *vbiq = &dev->vbiq;
1044 	unsigned long flags = 0;
1045 
1046 	dprintk(1, "au0828_stop_vbi_streaming called %d\n",
1047 		dev->streaming_users);
1048 
1049 	if (dev->streaming_users-- == 1)
1050 		au0828_uninit_isoc(dev);
1051 
1052 	spin_lock_irqsave(&dev->slock, flags);
1053 	if (dev->isoc_ctl.vbi_buf != NULL) {
1054 		vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
1055 				VB2_BUF_STATE_ERROR);
1056 		dev->isoc_ctl.vbi_buf = NULL;
1057 	}
1058 	while (!list_empty(&vbiq->active)) {
1059 		struct au0828_buffer *buf;
1060 
1061 		buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
1062 		list_del(&buf->list);
1063 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1064 	}
1065 	spin_unlock_irqrestore(&dev->slock, flags);
1066 
1067 	dev->vbi_timeout_running = 0;
1068 	del_timer_sync(&dev->vbi_timeout);
1069 }
1070 
1071 static struct vb2_ops au0828_video_qops = {
1072 	.queue_setup     = queue_setup,
1073 	.buf_prepare     = buffer_prepare,
1074 	.buf_queue       = buffer_queue,
1075 	.start_streaming = au0828_start_analog_streaming,
1076 	.stop_streaming  = au0828_stop_streaming,
1077 	.wait_prepare    = vb2_ops_wait_prepare,
1078 	.wait_finish     = vb2_ops_wait_finish,
1079 };
1080 
1081 /* ------------------------------------------------------------------
1082    V4L2 interface
1083    ------------------------------------------------------------------*/
1084 /*
1085  * au0828_analog_unregister
1086  * unregister v4l2 devices
1087  */
1088 int au0828_analog_unregister(struct au0828_dev *dev)
1089 {
1090 	dprintk(1, "au0828_analog_unregister called\n");
1091 
1092 	/* No analog TV */
1093 	if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1094 		return 0;
1095 
1096 	mutex_lock(&au0828_sysfs_lock);
1097 	video_unregister_device(&dev->vdev);
1098 	video_unregister_device(&dev->vbi_dev);
1099 	mutex_unlock(&au0828_sysfs_lock);
1100 
1101 	v4l2_device_disconnect(&dev->v4l2_dev);
1102 	v4l2_device_put(&dev->v4l2_dev);
1103 
1104 	return 1;
1105 }
1106 
1107 /* This function ensures that video frames continue to be delivered even if
1108    the ITU-656 input isn't receiving any data (thereby preventing applications
1109    such as tvtime from hanging) */
1110 static void au0828_vid_buffer_timeout(unsigned long data)
1111 {
1112 	struct au0828_dev *dev = (struct au0828_dev *) data;
1113 	struct au0828_dmaqueue *dma_q = &dev->vidq;
1114 	struct au0828_buffer *buf;
1115 	unsigned char *vid_data;
1116 	unsigned long flags = 0;
1117 
1118 	spin_lock_irqsave(&dev->slock, flags);
1119 
1120 	buf = dev->isoc_ctl.buf;
1121 	if (buf != NULL) {
1122 		vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
1123 		memset(vid_data, 0x00, buf->length); /* Blank green frame */
1124 		buffer_filled(dev, dma_q, buf);
1125 	}
1126 	get_next_buf(dma_q, &buf);
1127 
1128 	if (dev->vid_timeout_running == 1)
1129 		mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1130 
1131 	spin_unlock_irqrestore(&dev->slock, flags);
1132 }
1133 
1134 static void au0828_vbi_buffer_timeout(unsigned long data)
1135 {
1136 	struct au0828_dev *dev = (struct au0828_dev *) data;
1137 	struct au0828_dmaqueue *dma_q = &dev->vbiq;
1138 	struct au0828_buffer *buf;
1139 	unsigned char *vbi_data;
1140 	unsigned long flags = 0;
1141 
1142 	spin_lock_irqsave(&dev->slock, flags);
1143 
1144 	buf = dev->isoc_ctl.vbi_buf;
1145 	if (buf != NULL) {
1146 		vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
1147 		memset(vbi_data, 0x00, buf->length);
1148 		buffer_filled(dev, dma_q, buf);
1149 	}
1150 	vbi_get_next_buf(dma_q, &buf);
1151 
1152 	if (dev->vbi_timeout_running == 1)
1153 		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1154 	spin_unlock_irqrestore(&dev->slock, flags);
1155 }
1156 
1157 static int au0828_v4l2_open(struct file *filp)
1158 {
1159 	struct au0828_dev *dev = video_drvdata(filp);
1160 	int ret;
1161 
1162 	dprintk(1,
1163 		"%s called std_set %d dev_state %d stream users %d users %d\n",
1164 		__func__, dev->std_set_in_tuner_core, dev->dev_state,
1165 		dev->streaming_users, dev->users);
1166 
1167 	if (mutex_lock_interruptible(&dev->lock))
1168 		return -ERESTARTSYS;
1169 
1170 	ret = v4l2_fh_open(filp);
1171 	if (ret) {
1172 		au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1173 				__func__, ret);
1174 		mutex_unlock(&dev->lock);
1175 		return ret;
1176 	}
1177 
1178 	if (dev->users == 0) {
1179 		au0828_analog_stream_enable(dev);
1180 		au0828_analog_stream_reset(dev);
1181 		dev->stream_state = STREAM_OFF;
1182 		dev->dev_state |= DEV_INITIALIZED;
1183 	}
1184 	dev->users++;
1185 	mutex_unlock(&dev->lock);
1186 	return ret;
1187 }
1188 
1189 static int au0828_v4l2_close(struct file *filp)
1190 {
1191 	int ret;
1192 	struct au0828_dev *dev = video_drvdata(filp);
1193 	struct video_device *vdev = video_devdata(filp);
1194 
1195 	dprintk(1,
1196 		"%s called std_set %d dev_state %d stream users %d users %d\n",
1197 		__func__, dev->std_set_in_tuner_core, dev->dev_state,
1198 		dev->streaming_users, dev->users);
1199 
1200 	mutex_lock(&dev->lock);
1201 	if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
1202 		/* Cancel timeout thread in case they didn't call streamoff */
1203 		dev->vid_timeout_running = 0;
1204 		del_timer_sync(&dev->vid_timeout);
1205 	} else if (vdev->vfl_type == VFL_TYPE_VBI &&
1206 			dev->vbi_timeout_running) {
1207 		/* Cancel timeout thread in case they didn't call streamoff */
1208 		dev->vbi_timeout_running = 0;
1209 		del_timer_sync(&dev->vbi_timeout);
1210 	}
1211 
1212 	if (dev->dev_state == DEV_DISCONNECTED)
1213 		goto end;
1214 
1215 	if (dev->users == 1) {
1216 		/* Save some power by putting tuner to sleep */
1217 		v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1218 		dev->std_set_in_tuner_core = 0;
1219 
1220 		/* When close the device, set the usb intf0 into alt0 to free
1221 		   USB bandwidth */
1222 		ret = usb_set_interface(dev->usbdev, 0, 0);
1223 		if (ret < 0)
1224 			pr_info("Au0828 can't set alternate to 0!\n");
1225 	}
1226 end:
1227 	_vb2_fop_release(filp, NULL);
1228 	dev->users--;
1229 	mutex_unlock(&dev->lock);
1230 	return 0;
1231 }
1232 
1233 /* Must be called with dev->lock held */
1234 static void au0828_init_tuner(struct au0828_dev *dev)
1235 {
1236 	struct v4l2_frequency f = {
1237 		.frequency = dev->ctrl_freq,
1238 		.type = V4L2_TUNER_ANALOG_TV,
1239 	};
1240 
1241 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1242 		dev->std_set_in_tuner_core, dev->dev_state);
1243 
1244 	if (dev->std_set_in_tuner_core)
1245 		return;
1246 	dev->std_set_in_tuner_core = 1;
1247 	i2c_gate_ctrl(dev, 1);
1248 	/* If we've never sent the standard in tuner core, do so now.
1249 	   We don't do this at device probe because we don't want to
1250 	   incur the cost of a firmware load */
1251 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1252 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1253 	i2c_gate_ctrl(dev, 0);
1254 }
1255 
1256 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1257 			     struct v4l2_format *format)
1258 {
1259 	int ret;
1260 	int width = format->fmt.pix.width;
1261 	int height = format->fmt.pix.height;
1262 
1263 	/* If they are demanding a format other than the one we support,
1264 	   bail out (tvtime asks for UYVY and then retries with YUYV) */
1265 	if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1266 		return -EINVAL;
1267 
1268 	/* format->fmt.pix.width only support 720 and height 480 */
1269 	if (width != 720)
1270 		width = 720;
1271 	if (height != 480)
1272 		height = 480;
1273 
1274 	format->fmt.pix.width = width;
1275 	format->fmt.pix.height = height;
1276 	format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1277 	format->fmt.pix.bytesperline = width * 2;
1278 	format->fmt.pix.sizeimage = width * height * 2;
1279 	format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1280 	format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1281 	format->fmt.pix.priv = 0;
1282 
1283 	if (cmd == VIDIOC_TRY_FMT)
1284 		return 0;
1285 
1286 	/* maybe set new image format, driver current only support 720*480 */
1287 	dev->width = width;
1288 	dev->height = height;
1289 	dev->frame_size = width * height * 2;
1290 	dev->field_size = width * height;
1291 	dev->bytesperline = width * 2;
1292 
1293 	if (dev->stream_state == STREAM_ON) {
1294 		dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1295 		ret = au0828_stream_interrupt(dev);
1296 		if (ret != 0) {
1297 			dprintk(1, "error interrupting video stream!\n");
1298 			return ret;
1299 		}
1300 	}
1301 
1302 	au0828_analog_stream_enable(dev);
1303 
1304 	return 0;
1305 }
1306 
1307 static int vidioc_querycap(struct file *file, void  *priv,
1308 			   struct v4l2_capability *cap)
1309 {
1310 	struct video_device *vdev = video_devdata(file);
1311 	struct au0828_dev *dev = video_drvdata(file);
1312 
1313 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1314 		dev->std_set_in_tuner_core, dev->dev_state);
1315 
1316 	strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1317 	strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1318 	usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1319 
1320 	/* set the device capabilities */
1321 	cap->device_caps = V4L2_CAP_AUDIO |
1322 		V4L2_CAP_READWRITE |
1323 		V4L2_CAP_STREAMING |
1324 		V4L2_CAP_TUNER;
1325 	if (vdev->vfl_type == VFL_TYPE_GRABBER)
1326 		cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1327 	else
1328 		cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1329 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1330 		V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
1331 	return 0;
1332 }
1333 
1334 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1335 					struct v4l2_fmtdesc *f)
1336 {
1337 	if (f->index)
1338 		return -EINVAL;
1339 
1340 	dprintk(1, "%s called\n", __func__);
1341 
1342 	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1343 	strcpy(f->description, "Packed YUV2");
1344 
1345 	f->flags = 0;
1346 	f->pixelformat = V4L2_PIX_FMT_UYVY;
1347 
1348 	return 0;
1349 }
1350 
1351 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1352 					struct v4l2_format *f)
1353 {
1354 	struct au0828_dev *dev = video_drvdata(file);
1355 
1356 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1357 		dev->std_set_in_tuner_core, dev->dev_state);
1358 
1359 	f->fmt.pix.width = dev->width;
1360 	f->fmt.pix.height = dev->height;
1361 	f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1362 	f->fmt.pix.bytesperline = dev->bytesperline;
1363 	f->fmt.pix.sizeimage = dev->frame_size;
1364 	f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1365 	f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1366 	f->fmt.pix.priv = 0;
1367 	return 0;
1368 }
1369 
1370 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1371 				  struct v4l2_format *f)
1372 {
1373 	struct au0828_dev *dev = video_drvdata(file);
1374 
1375 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1376 		dev->std_set_in_tuner_core, dev->dev_state);
1377 
1378 	return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1379 }
1380 
1381 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1382 				struct v4l2_format *f)
1383 {
1384 	struct au0828_dev *dev = video_drvdata(file);
1385 	int rc;
1386 
1387 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1388 		dev->std_set_in_tuner_core, dev->dev_state);
1389 
1390 	rc = check_dev(dev);
1391 	if (rc < 0)
1392 		return rc;
1393 
1394 	if (vb2_is_busy(&dev->vb_vidq)) {
1395 		pr_info("%s queue busy\n", __func__);
1396 		rc = -EBUSY;
1397 		goto out;
1398 	}
1399 
1400 	rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1401 out:
1402 	return rc;
1403 }
1404 
1405 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1406 {
1407 	struct au0828_dev *dev = video_drvdata(file);
1408 
1409 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1410 		dev->std_set_in_tuner_core, dev->dev_state);
1411 
1412 	if (norm == dev->std)
1413 		return 0;
1414 
1415 	if (dev->streaming_users > 0)
1416 		return -EBUSY;
1417 
1418 	dev->std = norm;
1419 
1420 	au0828_init_tuner(dev);
1421 
1422 	i2c_gate_ctrl(dev, 1);
1423 
1424 	/*
1425 	 * FIXME: when we support something other than 60Hz standards,
1426 	 * we are going to have to make the au0828 bridge adjust the size
1427 	 * of its capture buffer, which is currently hardcoded at 720x480
1428 	 */
1429 
1430 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1431 
1432 	i2c_gate_ctrl(dev, 0);
1433 
1434 	return 0;
1435 }
1436 
1437 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1438 {
1439 	struct au0828_dev *dev = video_drvdata(file);
1440 
1441 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1442 		dev->std_set_in_tuner_core, dev->dev_state);
1443 
1444 	*norm = dev->std;
1445 	return 0;
1446 }
1447 
1448 static int vidioc_enum_input(struct file *file, void *priv,
1449 				struct v4l2_input *input)
1450 {
1451 	struct au0828_dev *dev = video_drvdata(file);
1452 	unsigned int tmp;
1453 
1454 	static const char *inames[] = {
1455 		[AU0828_VMUX_UNDEFINED] = "Undefined",
1456 		[AU0828_VMUX_COMPOSITE] = "Composite",
1457 		[AU0828_VMUX_SVIDEO] = "S-Video",
1458 		[AU0828_VMUX_CABLE] = "Cable TV",
1459 		[AU0828_VMUX_TELEVISION] = "Television",
1460 		[AU0828_VMUX_DVB] = "DVB",
1461 	};
1462 
1463 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1464 		dev->std_set_in_tuner_core, dev->dev_state);
1465 
1466 	tmp = input->index;
1467 
1468 	if (tmp >= AU0828_MAX_INPUT)
1469 		return -EINVAL;
1470 	if (AUVI_INPUT(tmp).type == 0)
1471 		return -EINVAL;
1472 
1473 	input->index = tmp;
1474 	strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1475 	if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1476 	    (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1477 		input->type |= V4L2_INPUT_TYPE_TUNER;
1478 		input->audioset = 1;
1479 	} else {
1480 		input->type |= V4L2_INPUT_TYPE_CAMERA;
1481 		input->audioset = 2;
1482 	}
1483 
1484 	input->std = dev->vdev.tvnorms;
1485 
1486 	return 0;
1487 }
1488 
1489 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1490 {
1491 	struct au0828_dev *dev = video_drvdata(file);
1492 
1493 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1494 		dev->std_set_in_tuner_core, dev->dev_state);
1495 
1496 	*i = dev->ctrl_input;
1497 	return 0;
1498 }
1499 
1500 static void au0828_s_input(struct au0828_dev *dev, int index)
1501 {
1502 	int i;
1503 
1504 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1505 		dev->std_set_in_tuner_core, dev->dev_state);
1506 
1507 	switch (AUVI_INPUT(index).type) {
1508 	case AU0828_VMUX_SVIDEO:
1509 		dev->input_type = AU0828_VMUX_SVIDEO;
1510 		dev->ctrl_ainput = 1;
1511 		break;
1512 	case AU0828_VMUX_COMPOSITE:
1513 		dev->input_type = AU0828_VMUX_COMPOSITE;
1514 		dev->ctrl_ainput = 1;
1515 		break;
1516 	case AU0828_VMUX_TELEVISION:
1517 		dev->input_type = AU0828_VMUX_TELEVISION;
1518 		dev->ctrl_ainput = 0;
1519 		break;
1520 	default:
1521 		dprintk(1, "unknown input type set [%d]\n",
1522 			AUVI_INPUT(index).type);
1523 		break;
1524 	}
1525 
1526 	v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1527 			AUVI_INPUT(index).vmux, 0, 0);
1528 
1529 	for (i = 0; i < AU0828_MAX_INPUT; i++) {
1530 		int enable = 0;
1531 		if (AUVI_INPUT(i).audio_setup == NULL)
1532 			continue;
1533 
1534 		if (i == index)
1535 			enable = 1;
1536 		else
1537 			enable = 0;
1538 		if (enable) {
1539 			(AUVI_INPUT(i).audio_setup)(dev, enable);
1540 		} else {
1541 			/* Make sure we leave it turned on if some
1542 			   other input is routed to this callback */
1543 			if ((AUVI_INPUT(i).audio_setup) !=
1544 			    ((AUVI_INPUT(index).audio_setup))) {
1545 				(AUVI_INPUT(i).audio_setup)(dev, enable);
1546 			}
1547 		}
1548 	}
1549 
1550 	v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1551 			AUVI_INPUT(index).amux, 0, 0);
1552 }
1553 
1554 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1555 {
1556 	struct au0828_dev *dev = video_drvdata(file);
1557 
1558 	dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1559 		index);
1560 	if (index >= AU0828_MAX_INPUT)
1561 		return -EINVAL;
1562 	if (AUVI_INPUT(index).type == 0)
1563 		return -EINVAL;
1564 	dev->ctrl_input = index;
1565 	au0828_s_input(dev, index);
1566 	return 0;
1567 }
1568 
1569 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1570 {
1571 	if (a->index > 1)
1572 		return -EINVAL;
1573 
1574 	dprintk(1, "%s called\n", __func__);
1575 
1576 	if (a->index == 0)
1577 		strcpy(a->name, "Television");
1578 	else
1579 		strcpy(a->name, "Line in");
1580 
1581 	a->capability = V4L2_AUDCAP_STEREO;
1582 	return 0;
1583 }
1584 
1585 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1586 {
1587 	struct au0828_dev *dev = video_drvdata(file);
1588 
1589 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1590 		dev->std_set_in_tuner_core, dev->dev_state);
1591 
1592 	a->index = dev->ctrl_ainput;
1593 	if (a->index == 0)
1594 		strcpy(a->name, "Television");
1595 	else
1596 		strcpy(a->name, "Line in");
1597 
1598 	a->capability = V4L2_AUDCAP_STEREO;
1599 	return 0;
1600 }
1601 
1602 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1603 {
1604 	struct au0828_dev *dev = video_drvdata(file);
1605 
1606 	if (a->index != dev->ctrl_ainput)
1607 		return -EINVAL;
1608 
1609 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1610 		dev->std_set_in_tuner_core, dev->dev_state);
1611 	return 0;
1612 }
1613 
1614 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1615 {
1616 	struct au0828_dev *dev = video_drvdata(file);
1617 
1618 	if (t->index != 0)
1619 		return -EINVAL;
1620 
1621 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1622 		dev->std_set_in_tuner_core, dev->dev_state);
1623 
1624 	strcpy(t->name, "Auvitek tuner");
1625 
1626 	au0828_init_tuner(dev);
1627 	i2c_gate_ctrl(dev, 1);
1628 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1629 	i2c_gate_ctrl(dev, 0);
1630 	return 0;
1631 }
1632 
1633 static int vidioc_s_tuner(struct file *file, void *priv,
1634 				const struct v4l2_tuner *t)
1635 {
1636 	struct au0828_dev *dev = video_drvdata(file);
1637 
1638 	if (t->index != 0)
1639 		return -EINVAL;
1640 
1641 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1642 		dev->std_set_in_tuner_core, dev->dev_state);
1643 
1644 	au0828_init_tuner(dev);
1645 	i2c_gate_ctrl(dev, 1);
1646 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1647 	i2c_gate_ctrl(dev, 0);
1648 
1649 	dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1650 		t->afc);
1651 
1652 	return 0;
1653 
1654 }
1655 
1656 static int vidioc_g_frequency(struct file *file, void *priv,
1657 				struct v4l2_frequency *freq)
1658 {
1659 	struct au0828_dev *dev = video_drvdata(file);
1660 
1661 	if (freq->tuner != 0)
1662 		return -EINVAL;
1663 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1664 		dev->std_set_in_tuner_core, dev->dev_state);
1665 	freq->frequency = dev->ctrl_freq;
1666 	return 0;
1667 }
1668 
1669 static int vidioc_s_frequency(struct file *file, void *priv,
1670 				const struct v4l2_frequency *freq)
1671 {
1672 	struct au0828_dev *dev = video_drvdata(file);
1673 	struct v4l2_frequency new_freq = *freq;
1674 
1675 	if (freq->tuner != 0)
1676 		return -EINVAL;
1677 
1678 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1679 		dev->std_set_in_tuner_core, dev->dev_state);
1680 
1681 	au0828_init_tuner(dev);
1682 	i2c_gate_ctrl(dev, 1);
1683 
1684 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1685 	/* Get the actual set (and possibly clamped) frequency */
1686 	v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1687 	dev->ctrl_freq = new_freq.frequency;
1688 
1689 	i2c_gate_ctrl(dev, 0);
1690 
1691 	au0828_analog_stream_reset(dev);
1692 
1693 	return 0;
1694 }
1695 
1696 
1697 /* RAW VBI ioctls */
1698 
1699 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1700 				struct v4l2_format *format)
1701 {
1702 	struct au0828_dev *dev = video_drvdata(file);
1703 
1704 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1705 		dev->std_set_in_tuner_core, dev->dev_state);
1706 
1707 	format->fmt.vbi.samples_per_line = dev->vbi_width;
1708 	format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1709 	format->fmt.vbi.offset = 0;
1710 	format->fmt.vbi.flags = 0;
1711 	format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1712 
1713 	format->fmt.vbi.count[0] = dev->vbi_height;
1714 	format->fmt.vbi.count[1] = dev->vbi_height;
1715 	format->fmt.vbi.start[0] = 21;
1716 	format->fmt.vbi.start[1] = 284;
1717 	memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1718 
1719 	return 0;
1720 }
1721 
1722 static int vidioc_cropcap(struct file *file, void *priv,
1723 			  struct v4l2_cropcap *cc)
1724 {
1725 	struct au0828_dev *dev = video_drvdata(file);
1726 
1727 	if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1728 		return -EINVAL;
1729 
1730 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1731 		dev->std_set_in_tuner_core, dev->dev_state);
1732 
1733 	cc->bounds.left = 0;
1734 	cc->bounds.top = 0;
1735 	cc->bounds.width = dev->width;
1736 	cc->bounds.height = dev->height;
1737 
1738 	cc->defrect = cc->bounds;
1739 
1740 	cc->pixelaspect.numerator = 54;
1741 	cc->pixelaspect.denominator = 59;
1742 
1743 	return 0;
1744 }
1745 
1746 #ifdef CONFIG_VIDEO_ADV_DEBUG
1747 static int vidioc_g_register(struct file *file, void *priv,
1748 			     struct v4l2_dbg_register *reg)
1749 {
1750 	struct au0828_dev *dev = video_drvdata(file);
1751 
1752 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1753 		dev->std_set_in_tuner_core, dev->dev_state);
1754 
1755 	reg->val = au0828_read(dev, reg->reg);
1756 	reg->size = 1;
1757 	return 0;
1758 }
1759 
1760 static int vidioc_s_register(struct file *file, void *priv,
1761 			     const struct v4l2_dbg_register *reg)
1762 {
1763 	struct au0828_dev *dev = video_drvdata(file);
1764 
1765 	dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1766 		dev->std_set_in_tuner_core, dev->dev_state);
1767 
1768 	return au0828_writereg(dev, reg->reg, reg->val);
1769 }
1770 #endif
1771 
1772 static int vidioc_log_status(struct file *file, void *fh)
1773 {
1774 	struct video_device *vdev = video_devdata(file);
1775 
1776 	dprintk(1, "%s called\n", __func__);
1777 
1778 	v4l2_ctrl_log_status(file, fh);
1779 	v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1780 	return 0;
1781 }
1782 
1783 void au0828_v4l2_suspend(struct au0828_dev *dev)
1784 {
1785 	struct urb *urb;
1786 	int i;
1787 
1788 	pr_info("stopping V4L2\n");
1789 
1790 	if (dev->stream_state == STREAM_ON) {
1791 		pr_info("stopping V4L2 active URBs\n");
1792 		au0828_analog_stream_disable(dev);
1793 		/* stop urbs */
1794 		for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1795 			urb = dev->isoc_ctl.urb[i];
1796 			if (urb) {
1797 				if (!irqs_disabled())
1798 					usb_kill_urb(urb);
1799 				else
1800 					usb_unlink_urb(urb);
1801 			}
1802 		}
1803 	}
1804 
1805 	if (dev->vid_timeout_running)
1806 		del_timer_sync(&dev->vid_timeout);
1807 	if (dev->vbi_timeout_running)
1808 		del_timer_sync(&dev->vbi_timeout);
1809 }
1810 
1811 void au0828_v4l2_resume(struct au0828_dev *dev)
1812 {
1813 	int i, rc;
1814 
1815 	pr_info("restarting V4L2\n");
1816 
1817 	if (dev->stream_state == STREAM_ON) {
1818 		au0828_stream_interrupt(dev);
1819 		au0828_init_tuner(dev);
1820 	}
1821 
1822 	if (dev->vid_timeout_running)
1823 		mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1824 	if (dev->vbi_timeout_running)
1825 		mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1826 
1827 	/* If we were doing ac97 instead of i2s, it would go here...*/
1828 	au0828_i2s_init(dev);
1829 
1830 	au0828_analog_stream_enable(dev);
1831 
1832 	if (!(dev->stream_state == STREAM_ON)) {
1833 		au0828_analog_stream_reset(dev);
1834 		/* submit urbs */
1835 		for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1836 			rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1837 			if (rc) {
1838 				au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1839 					       i, rc);
1840 				au0828_uninit_isoc(dev);
1841 			}
1842 		}
1843 	}
1844 }
1845 
1846 static struct v4l2_file_operations au0828_v4l_fops = {
1847 	.owner      = THIS_MODULE,
1848 	.open       = au0828_v4l2_open,
1849 	.release    = au0828_v4l2_close,
1850 	.read       = vb2_fop_read,
1851 	.poll       = vb2_fop_poll,
1852 	.mmap       = vb2_fop_mmap,
1853 	.unlocked_ioctl = video_ioctl2,
1854 };
1855 
1856 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1857 	.vidioc_querycap            = vidioc_querycap,
1858 	.vidioc_enum_fmt_vid_cap    = vidioc_enum_fmt_vid_cap,
1859 	.vidioc_g_fmt_vid_cap       = vidioc_g_fmt_vid_cap,
1860 	.vidioc_try_fmt_vid_cap     = vidioc_try_fmt_vid_cap,
1861 	.vidioc_s_fmt_vid_cap       = vidioc_s_fmt_vid_cap,
1862 	.vidioc_g_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1863 	.vidioc_try_fmt_vbi_cap     = vidioc_g_fmt_vbi_cap,
1864 	.vidioc_s_fmt_vbi_cap       = vidioc_g_fmt_vbi_cap,
1865 	.vidioc_enumaudio           = vidioc_enumaudio,
1866 	.vidioc_g_audio             = vidioc_g_audio,
1867 	.vidioc_s_audio             = vidioc_s_audio,
1868 	.vidioc_cropcap             = vidioc_cropcap,
1869 
1870 	.vidioc_reqbufs             = vb2_ioctl_reqbufs,
1871 	.vidioc_create_bufs         = vb2_ioctl_create_bufs,
1872 	.vidioc_prepare_buf         = vb2_ioctl_prepare_buf,
1873 	.vidioc_querybuf            = vb2_ioctl_querybuf,
1874 	.vidioc_qbuf                = vb2_ioctl_qbuf,
1875 	.vidioc_dqbuf               = vb2_ioctl_dqbuf,
1876 	.vidioc_expbuf               = vb2_ioctl_expbuf,
1877 
1878 	.vidioc_s_std               = vidioc_s_std,
1879 	.vidioc_g_std               = vidioc_g_std,
1880 	.vidioc_enum_input          = vidioc_enum_input,
1881 	.vidioc_g_input             = vidioc_g_input,
1882 	.vidioc_s_input             = vidioc_s_input,
1883 
1884 	.vidioc_streamon            = vb2_ioctl_streamon,
1885 	.vidioc_streamoff           = vb2_ioctl_streamoff,
1886 
1887 	.vidioc_g_tuner             = vidioc_g_tuner,
1888 	.vidioc_s_tuner             = vidioc_s_tuner,
1889 	.vidioc_g_frequency         = vidioc_g_frequency,
1890 	.vidioc_s_frequency         = vidioc_s_frequency,
1891 #ifdef CONFIG_VIDEO_ADV_DEBUG
1892 	.vidioc_g_register          = vidioc_g_register,
1893 	.vidioc_s_register          = vidioc_s_register,
1894 #endif
1895 	.vidioc_log_status	    = vidioc_log_status,
1896 	.vidioc_subscribe_event     = v4l2_ctrl_subscribe_event,
1897 	.vidioc_unsubscribe_event   = v4l2_event_unsubscribe,
1898 };
1899 
1900 static const struct video_device au0828_video_template = {
1901 	.fops                       = &au0828_v4l_fops,
1902 	.release                    = video_device_release_empty,
1903 	.ioctl_ops 		    = &video_ioctl_ops,
1904 	.tvnorms                    = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1905 };
1906 
1907 static int au0828_vb2_setup(struct au0828_dev *dev)
1908 {
1909 	int rc;
1910 	struct vb2_queue *q;
1911 
1912 	/* Setup Videobuf2 for Video capture */
1913 	q = &dev->vb_vidq;
1914 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1915 	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1916 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1917 	q->drv_priv = dev;
1918 	q->buf_struct_size = sizeof(struct au0828_buffer);
1919 	q->ops = &au0828_video_qops;
1920 	q->mem_ops = &vb2_vmalloc_memops;
1921 
1922 	rc = vb2_queue_init(q);
1923 	if (rc < 0)
1924 		return rc;
1925 
1926 	/* Setup Videobuf2 for VBI capture */
1927 	q = &dev->vb_vbiq;
1928 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1929 	q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1930 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1931 	q->drv_priv = dev;
1932 	q->buf_struct_size = sizeof(struct au0828_buffer);
1933 	q->ops = &au0828_vbi_qops;
1934 	q->mem_ops = &vb2_vmalloc_memops;
1935 
1936 	rc = vb2_queue_init(q);
1937 	if (rc < 0)
1938 		return rc;
1939 
1940 	return 0;
1941 }
1942 
1943 static void au0828_analog_create_entities(struct au0828_dev *dev)
1944 {
1945 #if defined(CONFIG_MEDIA_CONTROLLER)
1946 	static const char * const inames[] = {
1947 		[AU0828_VMUX_COMPOSITE] = "Composite",
1948 		[AU0828_VMUX_SVIDEO] = "S-Video",
1949 		[AU0828_VMUX_CABLE] = "Cable TV",
1950 		[AU0828_VMUX_TELEVISION] = "Television",
1951 		[AU0828_VMUX_DVB] = "DVB",
1952 	};
1953 	int ret, i;
1954 
1955 	/* Initialize Video and VBI pads */
1956 	dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1957 	ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1958 	if (ret < 0)
1959 		pr_err("failed to initialize video media entity!\n");
1960 
1961 	dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1962 	ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1963 	if (ret < 0)
1964 		pr_err("failed to initialize vbi media entity!\n");
1965 
1966 	/* Create entities for each input connector */
1967 	for (i = 0; i < AU0828_MAX_INPUT; i++) {
1968 		struct media_entity *ent = &dev->input_ent[i];
1969 
1970 		if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1971 			break;
1972 
1973 		ent->name = inames[AUVI_INPUT(i).type];
1974 		ent->flags = MEDIA_ENT_FL_CONNECTOR;
1975 		dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1976 
1977 		switch (AUVI_INPUT(i).type) {
1978 		case AU0828_VMUX_COMPOSITE:
1979 			ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1980 			break;
1981 		case AU0828_VMUX_SVIDEO:
1982 			ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1983 			break;
1984 		case AU0828_VMUX_CABLE:
1985 		case AU0828_VMUX_TELEVISION:
1986 		case AU0828_VMUX_DVB:
1987 		default: /* Just to shut up a warning */
1988 			ent->function = MEDIA_ENT_F_CONN_RF;
1989 			break;
1990 		}
1991 
1992 		ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1993 		if (ret < 0)
1994 			pr_err("failed to initialize input pad[%d]!\n", i);
1995 
1996 		ret = media_device_register_entity(dev->media_dev, ent);
1997 		if (ret < 0)
1998 			pr_err("failed to register input entity %d!\n", i);
1999 	}
2000 #endif
2001 }
2002 
2003 /**************************************************************************/
2004 
2005 int au0828_analog_register(struct au0828_dev *dev,
2006 			   struct usb_interface *interface)
2007 {
2008 	int retval = -ENOMEM;
2009 	struct usb_host_interface *iface_desc;
2010 	struct usb_endpoint_descriptor *endpoint;
2011 	int i, ret;
2012 
2013 	dprintk(1, "au0828_analog_register called for intf#%d!\n",
2014 		interface->cur_altsetting->desc.bInterfaceNumber);
2015 
2016 	/* No analog TV */
2017 	if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
2018 		return 0;
2019 
2020 	/* set au0828 usb interface0 to as5 */
2021 	retval = usb_set_interface(dev->usbdev,
2022 			interface->cur_altsetting->desc.bInterfaceNumber, 5);
2023 	if (retval != 0) {
2024 		pr_info("Failure setting usb interface0 to as5\n");
2025 		return retval;
2026 	}
2027 
2028 	/* Figure out which endpoint has the isoc interface */
2029 	iface_desc = interface->cur_altsetting;
2030 	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2031 		endpoint = &iface_desc->endpoint[i].desc;
2032 		if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2033 		     == USB_DIR_IN) &&
2034 		    ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2035 		     == USB_ENDPOINT_XFER_ISOC)) {
2036 
2037 			/* we find our isoc in endpoint */
2038 			u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
2039 			dev->max_pkt_size = (tmp & 0x07ff) *
2040 				(((tmp & 0x1800) >> 11) + 1);
2041 			dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
2042 			dprintk(1,
2043 				"Found isoc endpoint 0x%02x, max size = %d\n",
2044 				dev->isoc_in_endpointaddr, dev->max_pkt_size);
2045 		}
2046 	}
2047 	if (!(dev->isoc_in_endpointaddr)) {
2048 		pr_info("Could not locate isoc endpoint\n");
2049 		return -ENODEV;
2050 	}
2051 
2052 	init_waitqueue_head(&dev->open);
2053 	spin_lock_init(&dev->slock);
2054 
2055 	/* init video dma queues */
2056 	INIT_LIST_HEAD(&dev->vidq.active);
2057 	INIT_LIST_HEAD(&dev->vbiq.active);
2058 
2059 	setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
2060 		    (unsigned long)dev);
2061 	setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
2062 		    (unsigned long)dev);
2063 
2064 	dev->width = NTSC_STD_W;
2065 	dev->height = NTSC_STD_H;
2066 	dev->field_size = dev->width * dev->height;
2067 	dev->frame_size = dev->field_size << 1;
2068 	dev->bytesperline = dev->width << 1;
2069 	dev->vbi_width = 720;
2070 	dev->vbi_height = 1;
2071 	dev->ctrl_ainput = 0;
2072 	dev->ctrl_freq = 960;
2073 	dev->std = V4L2_STD_NTSC_M;
2074 	au0828_s_input(dev, 0);
2075 
2076 	mutex_init(&dev->vb_queue_lock);
2077 	mutex_init(&dev->vb_vbi_queue_lock);
2078 
2079 	/* Fill the video capture device struct */
2080 	dev->vdev = au0828_video_template;
2081 	dev->vdev.v4l2_dev = &dev->v4l2_dev;
2082 	dev->vdev.lock = &dev->lock;
2083 	dev->vdev.queue = &dev->vb_vidq;
2084 	dev->vdev.queue->lock = &dev->vb_queue_lock;
2085 	strcpy(dev->vdev.name, "au0828a video");
2086 
2087 	/* Setup the VBI device */
2088 	dev->vbi_dev = au0828_video_template;
2089 	dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
2090 	dev->vbi_dev.lock = &dev->lock;
2091 	dev->vbi_dev.queue = &dev->vb_vbiq;
2092 	dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
2093 	strcpy(dev->vbi_dev.name, "au0828a vbi");
2094 
2095 	/* Init entities at the Media Controller */
2096 	au0828_analog_create_entities(dev);
2097 
2098 	/* initialize videobuf2 stuff */
2099 	retval = au0828_vb2_setup(dev);
2100 	if (retval != 0) {
2101 		dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2102 			retval);
2103 		return -ENODEV;
2104 	}
2105 
2106 	/* Register the v4l2 device */
2107 	video_set_drvdata(&dev->vdev, dev);
2108 	retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
2109 	if (retval != 0) {
2110 		dprintk(1, "unable to register video device (error = %d).\n",
2111 			retval);
2112 		ret = -ENODEV;
2113 		goto err_reg_vdev;
2114 	}
2115 
2116 	/* Register the vbi device */
2117 	video_set_drvdata(&dev->vbi_dev, dev);
2118 	retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
2119 	if (retval != 0) {
2120 		dprintk(1, "unable to register vbi device (error = %d).\n",
2121 			retval);
2122 		ret = -ENODEV;
2123 		goto err_reg_vbi_dev;
2124 	}
2125 	retval = au0828_create_media_graph(dev);
2126 	if (retval) {
2127 		pr_err("%s() au0282_dev_register failed to create graph\n",
2128 			__func__);
2129 		ret = -ENODEV;
2130 		goto err_reg_vbi_dev;
2131 	}
2132 
2133 
2134 	dprintk(1, "%s completed!\n", __func__);
2135 
2136 	return 0;
2137 
2138 err_reg_vbi_dev:
2139 	video_unregister_device(&dev->vdev);
2140 err_reg_vdev:
2141 	vb2_queue_release(&dev->vb_vidq);
2142 	vb2_queue_release(&dev->vb_vbiq);
2143 	return ret;
2144 }
2145 
2146