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