2 * Auvitek AU0828 USB Bridge (Analog video support)
4 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
5 * Copyright (C) 2005-2008 Auvitek International, Ltd.
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.
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.
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
25 * VBI support is not yet working
26 * The hardware scaler supported is unimplemented
27 * AC97 audio support is unimplemented (only i2s audio mode)
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 <linux/version.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
40 #include <media/tuner.h>
42 #include "au0828-reg.h"
44 static DEFINE_MUTEX(au0828_sysfs_lock);
46 #define AU0828_VERSION_CODE KERNEL_VERSION(0, 0, 1)
48 /* ------------------------------------------------------------------
50 ------------------------------------------------------------------*/
52 static unsigned int isoc_debug;
53 module_param(isoc_debug, int, 0644);
54 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
56 #define au0828_isocdbg(fmt, arg...) \
59 printk(KERN_INFO "au0828 %s :"fmt, \
64 static inline void print_err_status(struct au0828_dev *dev,
65 int packet, int status)
67 char *errmsg = "Unknown";
71 errmsg = "unlinked synchronuously";
74 errmsg = "unlinked asynchronuously";
77 errmsg = "Buffer error (overrun)";
80 errmsg = "Stalled (device not responding)";
83 errmsg = "Babble (bad cable?)";
86 errmsg = "Bit-stuff error (bad cable?)";
89 errmsg = "CRC/Timeout (could be anything)";
92 errmsg = "Device does not respond";
96 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
98 au0828_isocdbg("URB packet %d, status %d [%s].\n",
99 packet, status, errmsg);
103 static int check_dev(struct au0828_dev *dev)
105 if (dev->dev_state & DEV_DISCONNECTED) {
106 printk(KERN_INFO "v4l2 ioctl: device not present\n");
110 if (dev->dev_state & DEV_MISCONFIGURED) {
111 printk(KERN_INFO "v4l2 ioctl: device is misconfigured; "
112 "close and open it again\n");
119 * IRQ callback, called by URB callback
121 static void au0828_irq_callback(struct urb *urb)
123 struct au0828_dmaqueue *dma_q = urb->context;
124 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
125 unsigned long flags = 0;
128 switch (urb->status) {
129 case 0: /* success */
130 case -ETIMEDOUT: /* NAK */
132 case -ECONNRESET: /* kill */
135 au0828_isocdbg("au0828_irq_callback called: status kill\n");
137 default: /* unknown error */
138 au0828_isocdbg("urb completition error %d.\n", urb->status);
142 /* Copy data from URB */
143 spin_lock_irqsave(&dev->slock, flags);
144 rc = dev->isoc_ctl.isoc_copy(dev, urb);
145 spin_unlock_irqrestore(&dev->slock, flags);
147 /* Reset urb buffers */
148 for (i = 0; i < urb->number_of_packets; i++) {
149 urb->iso_frame_desc[i].status = 0;
150 urb->iso_frame_desc[i].actual_length = 0;
154 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
156 au0828_isocdbg("urb resubmit failed (error=%i)\n",
162 * Stop and Deallocate URBs
164 void au0828_uninit_isoc(struct au0828_dev *dev)
169 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
171 dev->isoc_ctl.nfields = -1;
172 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
173 urb = dev->isoc_ctl.urb[i];
175 if (!irqs_disabled())
180 if (dev->isoc_ctl.transfer_buffer[i]) {
181 usb_free_coherent(dev->usbdev,
182 urb->transfer_buffer_length,
183 dev->isoc_ctl.transfer_buffer[i],
187 dev->isoc_ctl.urb[i] = NULL;
189 dev->isoc_ctl.transfer_buffer[i] = NULL;
192 kfree(dev->isoc_ctl.urb);
193 kfree(dev->isoc_ctl.transfer_buffer);
195 dev->isoc_ctl.urb = NULL;
196 dev->isoc_ctl.transfer_buffer = NULL;
197 dev->isoc_ctl.num_bufs = 0;
201 * Allocate URBs and start IRQ
203 int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
204 int num_bufs, int max_pkt_size,
205 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
207 struct au0828_dmaqueue *dma_q = &dev->vidq;
214 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
216 /* De-allocates all pending stuff */
217 au0828_uninit_isoc(dev);
219 dev->isoc_ctl.isoc_copy = isoc_copy;
220 dev->isoc_ctl.num_bufs = num_bufs;
222 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
223 if (!dev->isoc_ctl.urb) {
224 au0828_isocdbg("cannot alloc memory for usb buffers\n");
228 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
230 if (!dev->isoc_ctl.transfer_buffer) {
231 au0828_isocdbg("cannot allocate memory for usb transfer\n");
232 kfree(dev->isoc_ctl.urb);
236 dev->isoc_ctl.max_pkt_size = max_pkt_size;
237 dev->isoc_ctl.buf = NULL;
239 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
241 /* allocate urbs and transfer buffers */
242 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
243 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
245 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
246 au0828_uninit_isoc(dev);
249 dev->isoc_ctl.urb[i] = urb;
251 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
252 sb_size, GFP_KERNEL, &urb->transfer_dma);
253 if (!dev->isoc_ctl.transfer_buffer[i]) {
254 printk("unable to allocate %i bytes for transfer"
257 in_interrupt() ? " while in int" : "");
258 au0828_uninit_isoc(dev);
261 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
263 pipe = usb_rcvisocpipe(dev->usbdev,
264 dev->isoc_in_endpointaddr),
266 usb_fill_int_urb(urb, dev->usbdev, pipe,
267 dev->isoc_ctl.transfer_buffer[i], sb_size,
268 au0828_irq_callback, dma_q, 1);
270 urb->number_of_packets = max_packets;
271 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
274 for (j = 0; j < max_packets; j++) {
275 urb->iso_frame_desc[j].offset = k;
276 urb->iso_frame_desc[j].length =
277 dev->isoc_ctl.max_pkt_size;
278 k += dev->isoc_ctl.max_pkt_size;
282 init_waitqueue_head(&dma_q->wq);
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);
288 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
290 au0828_uninit_isoc(dev);
299 * Announces that a buffer were filled and request the next
301 static inline void buffer_filled(struct au0828_dev *dev,
302 struct au0828_dmaqueue *dma_q,
303 struct au0828_buffer *buf)
305 /* Advice that buffer was filled */
306 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
308 buf->vb.state = VIDEOBUF_DONE;
309 buf->vb.field_count++;
310 do_gettimeofday(&buf->vb.ts);
312 dev->isoc_ctl.buf = NULL;
314 list_del(&buf->vb.queue);
315 wake_up(&buf->vb.done);
318 static inline void vbi_buffer_filled(struct au0828_dev *dev,
319 struct au0828_dmaqueue *dma_q,
320 struct au0828_buffer *buf)
322 /* Advice that buffer was filled */
323 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
325 buf->vb.state = VIDEOBUF_DONE;
326 buf->vb.field_count++;
327 do_gettimeofday(&buf->vb.ts);
329 dev->isoc_ctl.vbi_buf = NULL;
331 list_del(&buf->vb.queue);
332 wake_up(&buf->vb.done);
336 * Identify the buffer header type and properly handles
338 static void au0828_copy_video(struct au0828_dev *dev,
339 struct au0828_dmaqueue *dma_q,
340 struct au0828_buffer *buf,
342 unsigned char *outp, unsigned long len)
344 void *fieldstart, *startwrite, *startread;
345 int linesdone, currlinedone, offset, lencopy, remain;
346 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
351 if (dma_q->pos + len > buf->vb.size)
352 len = buf->vb.size - dma_q->pos;
357 /* Interlaces frame */
361 fieldstart = outp + bytesperline;
363 linesdone = dma_q->pos / bytesperline;
364 currlinedone = dma_q->pos % bytesperline;
365 offset = linesdone * bytesperline * 2 + currlinedone;
366 startwrite = fieldstart + offset;
367 lencopy = bytesperline - currlinedone;
368 lencopy = lencopy > remain ? remain : lencopy;
370 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
371 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
372 ((char *)startwrite + lencopy) -
373 ((char *)outp + buf->vb.size));
374 remain = (char *)outp + buf->vb.size - (char *)startwrite;
379 memcpy(startwrite, startread, lencopy);
384 startwrite += lencopy + bytesperline;
385 startread += lencopy;
386 if (bytesperline > remain)
389 lencopy = bytesperline;
391 if ((char *)startwrite + lencopy > (char *)outp +
393 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
394 ((char *)startwrite + lencopy) -
395 ((char *)outp + buf->vb.size));
396 lencopy = remain = (char *)outp + buf->vb.size -
402 memcpy(startwrite, startread, lencopy);
408 /* We have enough data to check for greenscreen */
409 if (outp[0] < 0x60 && outp[1440] < 0x60)
410 dev->greenscreen_detected = 1;
417 * video-buf generic routine to get the next available buffer
419 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
420 struct au0828_buffer **buf)
422 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
424 if (list_empty(&dma_q->active)) {
425 au0828_isocdbg("No active queue to serve\n");
426 dev->isoc_ctl.buf = NULL;
431 /* Get the next buffer */
432 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
433 dev->isoc_ctl.buf = *buf;
438 static void au0828_copy_vbi(struct au0828_dev *dev,
439 struct au0828_dmaqueue *dma_q,
440 struct au0828_buffer *buf,
442 unsigned char *outp, unsigned long len)
444 unsigned char *startwrite, *startread;
449 au0828_isocdbg("dev is null\n");
454 au0828_isocdbg("dma_q is null\n");
460 au0828_isocdbg("p is null\n");
464 au0828_isocdbg("outp is null\n");
468 bytesperline = dev->vbi_width;
470 if (dma_q->pos + len > buf->vb.size)
471 len = buf->vb.size - dma_q->pos;
474 startwrite = outp + (dma_q->pos / 2);
476 /* Make sure the bottom field populates the second half of the frame */
477 if (buf->top_field == 0)
478 startwrite += bytesperline * dev->vbi_height;
480 for (i = 0; i < len; i += 2)
481 startwrite[j++] = startread[i+1];
488 * video-buf generic routine to get the next available VBI buffer
490 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
491 struct au0828_buffer **buf)
493 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
496 if (list_empty(&dma_q->active)) {
497 au0828_isocdbg("No active queue to serve\n");
498 dev->isoc_ctl.vbi_buf = NULL;
503 /* Get the next buffer */
504 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
505 /* Cleans up buffer - Usefull for testing for frame/URB loss */
506 outp = videobuf_to_vmalloc(&(*buf)->vb);
507 memset(outp, 0x00, (*buf)->vb.size);
509 dev->isoc_ctl.vbi_buf = *buf;
515 * Controls the isoc copy of each urb packet
517 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
519 struct au0828_buffer *buf;
520 struct au0828_buffer *vbi_buf;
521 struct au0828_dmaqueue *dma_q = urb->context;
522 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
523 unsigned char *outp = NULL;
524 unsigned char *vbioutp = NULL;
525 int i, len = 0, rc = 1;
528 unsigned int vbi_field_size;
529 unsigned int remain, lencopy;
534 if ((dev->dev_state & DEV_DISCONNECTED) ||
535 (dev->dev_state & DEV_MISCONFIGURED))
538 if (urb->status < 0) {
539 print_err_status(dev, -1, urb->status);
540 if (urb->status == -ENOENT)
544 buf = dev->isoc_ctl.buf;
546 outp = videobuf_to_vmalloc(&buf->vb);
548 vbi_buf = dev->isoc_ctl.vbi_buf;
550 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
552 for (i = 0; i < urb->number_of_packets; i++) {
553 int status = urb->iso_frame_desc[i].status;
556 print_err_status(dev, i, status);
557 if (urb->iso_frame_desc[i].status != -EPROTO)
561 if (urb->iso_frame_desc[i].actual_length <= 0)
564 if (urb->iso_frame_desc[i].actual_length >
566 au0828_isocdbg("packet bigger than packet size");
570 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
572 len = urb->iso_frame_desc[i].actual_length - 4;
578 au0828_isocdbg("Video frame %s\n",
579 (fbyte & 0x40) ? "odd" : "even");
583 vbi_buffer_filled(dev,
586 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
590 vbioutp = videobuf_to_vmalloc(
595 buffer_filled(dev, dma_q, buf);
596 get_next_buf(dma_q, &buf);
600 outp = videobuf_to_vmalloc(&buf->vb);
602 /* As long as isoc traffic is arriving, keep
603 resetting the timer */
604 if (dev->vid_timeout_running)
605 mod_timer(&dev->vid_timeout,
606 jiffies + (HZ / 10));
607 if (dev->vbi_timeout_running)
608 mod_timer(&dev->vbi_timeout,
609 jiffies + (HZ / 10));
619 if (vbi_buf != NULL) {
621 vbi_buf->top_field = 1;
623 vbi_buf->top_field = 0;
631 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
632 if (dev->vbi_read < vbi_field_size) {
633 remain = vbi_field_size - dev->vbi_read;
640 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
645 dev->vbi_read += lencopy;
648 if (dev->vbi_read >= vbi_field_size && buf != NULL)
649 au0828_copy_video(dev, dma_q, buf, p, outp, len);
655 buffer_setup(struct videobuf_queue *vq, unsigned int *count,
658 struct au0828_fh *fh = vq->priv_data;
659 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
662 *count = AU0828_DEF_BUF;
664 if (*count < AU0828_MIN_BUF)
665 *count = AU0828_MIN_BUF;
669 /* This is called *without* dev->slock held; please keep it that way */
670 static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
672 struct au0828_fh *fh = vq->priv_data;
673 struct au0828_dev *dev = fh->dev;
674 unsigned long flags = 0;
678 /* We used to wait for the buffer to finish here, but this didn't work
679 because, as we were keeping the state as VIDEOBUF_QUEUED,
680 videobuf_queue_cancel marked it as finished for us.
681 (Also, it could wedge forever if the hardware was misconfigured.)
683 This should be safe; by the time we get here, the buffer isn't
684 queued anymore. If we ever start marking the buffers as
685 VIDEOBUF_ACTIVE, it won't be, though.
687 spin_lock_irqsave(&dev->slock, flags);
688 if (dev->isoc_ctl.buf == buf)
689 dev->isoc_ctl.buf = NULL;
690 spin_unlock_irqrestore(&dev->slock, flags);
692 videobuf_vmalloc_free(&buf->vb);
693 buf->vb.state = VIDEOBUF_NEEDS_INIT;
697 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
698 enum v4l2_field field)
700 struct au0828_fh *fh = vq->priv_data;
701 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
702 struct au0828_dev *dev = fh->dev;
703 int rc = 0, urb_init = 0;
705 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
707 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
710 buf->vb.width = dev->width;
711 buf->vb.height = dev->height;
712 buf->vb.field = field;
714 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
715 rc = videobuf_iolock(vq, &buf->vb, NULL);
717 printk(KERN_INFO "videobuf_iolock failed\n");
722 if (!dev->isoc_ctl.num_bufs)
726 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
727 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
730 printk(KERN_INFO "au0828_init_isoc failed\n");
735 buf->vb.state = VIDEOBUF_PREPARED;
739 free_buffer(vq, buf);
744 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
746 struct au0828_buffer *buf = container_of(vb,
747 struct au0828_buffer,
749 struct au0828_fh *fh = vq->priv_data;
750 struct au0828_dev *dev = fh->dev;
751 struct au0828_dmaqueue *vidq = &dev->vidq;
753 buf->vb.state = VIDEOBUF_QUEUED;
754 list_add_tail(&buf->vb.queue, &vidq->active);
757 static void buffer_release(struct videobuf_queue *vq,
758 struct videobuf_buffer *vb)
760 struct au0828_buffer *buf = container_of(vb,
761 struct au0828_buffer,
764 free_buffer(vq, buf);
767 static struct videobuf_queue_ops au0828_video_qops = {
768 .buf_setup = buffer_setup,
769 .buf_prepare = buffer_prepare,
770 .buf_queue = buffer_queue,
771 .buf_release = buffer_release,
774 /* ------------------------------------------------------------------
776 ------------------------------------------------------------------*/
778 static int au0828_i2s_init(struct au0828_dev *dev)
780 /* Enable i2s mode */
781 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
786 * Auvitek au0828 analog stream enable
787 * Please set interface0 to AS5 before enable the stream
789 int au0828_analog_stream_enable(struct au0828_dev *d)
791 dprintk(1, "au0828_analog_stream_enable called\n");
792 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
793 au0828_writereg(d, 0x106, 0x00);
795 au0828_writereg(d, 0x110, 0x00);
796 au0828_writereg(d, 0x111, 0x00);
797 au0828_writereg(d, 0x114, 0xa0);
798 au0828_writereg(d, 0x115, 0x05);
800 au0828_writereg(d, 0x112, 0x00);
801 au0828_writereg(d, 0x113, 0x00);
802 au0828_writereg(d, 0x116, 0xf2);
803 au0828_writereg(d, 0x117, 0x00);
804 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
809 int au0828_analog_stream_disable(struct au0828_dev *d)
811 dprintk(1, "au0828_analog_stream_disable called\n");
812 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
816 void au0828_analog_stream_reset(struct au0828_dev *dev)
818 dprintk(1, "au0828_analog_stream_reset called\n");
819 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
821 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
825 * Some operations needs to stop current streaming
827 static int au0828_stream_interrupt(struct au0828_dev *dev)
831 dev->stream_state = STREAM_INTERRUPT;
832 if (dev->dev_state == DEV_DISCONNECTED)
835 dev->dev_state = DEV_MISCONFIGURED;
836 dprintk(1, "%s device is misconfigured!\n", __func__);
843 * au0828_release_resources
844 * unregister v4l2 devices
846 void au0828_analog_unregister(struct au0828_dev *dev)
848 dprintk(1, "au0828_release_resources called\n");
849 mutex_lock(&au0828_sysfs_lock);
852 video_unregister_device(dev->vdev);
854 video_unregister_device(dev->vbi_dev);
856 mutex_unlock(&au0828_sysfs_lock);
860 /* Usage lock check functions */
861 static int res_get(struct au0828_fh *fh, unsigned int bit)
863 struct au0828_dev *dev = fh->dev;
865 if (fh->resources & bit)
866 /* have it already allocated */
870 mutex_lock(&dev->lock);
871 if (dev->resources & bit) {
872 /* no, someone else uses it */
873 mutex_unlock(&dev->lock);
876 /* it's free, grab it */
877 fh->resources |= bit;
878 dev->resources |= bit;
879 dprintk(1, "res: get %d\n", bit);
880 mutex_unlock(&dev->lock);
884 static int res_check(struct au0828_fh *fh, unsigned int bit)
886 return fh->resources & bit;
889 static int res_locked(struct au0828_dev *dev, unsigned int bit)
891 return dev->resources & bit;
894 static void res_free(struct au0828_fh *fh, unsigned int bits)
896 struct au0828_dev *dev = fh->dev;
898 BUG_ON((fh->resources & bits) != bits);
900 mutex_lock(&dev->lock);
901 fh->resources &= ~bits;
902 dev->resources &= ~bits;
903 dprintk(1, "res: put %d\n", bits);
904 mutex_unlock(&dev->lock);
907 static int get_ressource(struct au0828_fh *fh)
910 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
911 return AU0828_RESOURCE_VIDEO;
912 case V4L2_BUF_TYPE_VBI_CAPTURE:
913 return AU0828_RESOURCE_VBI;
920 /* This function ensures that video frames continue to be delivered even if
921 the ITU-656 input isn't receiving any data (thereby preventing applications
922 such as tvtime from hanging) */
923 void au0828_vid_buffer_timeout(unsigned long data)
925 struct au0828_dev *dev = (struct au0828_dev *) data;
926 struct au0828_dmaqueue *dma_q = &dev->vidq;
927 struct au0828_buffer *buf;
928 unsigned char *vid_data;
929 unsigned long flags = 0;
931 spin_lock_irqsave(&dev->slock, flags);
933 buf = dev->isoc_ctl.buf;
935 vid_data = videobuf_to_vmalloc(&buf->vb);
936 memset(vid_data, 0x00, buf->vb.size); /* Blank green frame */
937 buffer_filled(dev, dma_q, buf);
939 get_next_buf(dma_q, &buf);
941 if (dev->vid_timeout_running == 1)
942 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
944 spin_unlock_irqrestore(&dev->slock, flags);
947 void au0828_vbi_buffer_timeout(unsigned long data)
949 struct au0828_dev *dev = (struct au0828_dev *) data;
950 struct au0828_dmaqueue *dma_q = &dev->vbiq;
951 struct au0828_buffer *buf;
952 unsigned char *vbi_data;
953 unsigned long flags = 0;
955 spin_lock_irqsave(&dev->slock, flags);
957 buf = dev->isoc_ctl.vbi_buf;
959 vbi_data = videobuf_to_vmalloc(&buf->vb);
960 memset(vbi_data, 0x00, buf->vb.size);
961 vbi_buffer_filled(dev, dma_q, buf);
963 vbi_get_next_buf(dma_q, &buf);
965 if (dev->vbi_timeout_running == 1)
966 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
967 spin_unlock_irqrestore(&dev->slock, flags);
971 static int au0828_v4l2_open(struct file *filp)
974 struct video_device *vdev = video_devdata(filp);
975 struct au0828_dev *dev = video_drvdata(filp);
976 struct au0828_fh *fh;
979 switch (vdev->vfl_type) {
980 case VFL_TYPE_GRABBER:
981 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
984 type = V4L2_BUF_TYPE_VBI_CAPTURE;
990 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
992 dprintk(1, "Failed allocate au0828_fh struct!\n");
998 filp->private_data = fh;
1000 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
1001 /* set au0828 interface0 to AS5 here again */
1002 ret = usb_set_interface(dev->usbdev, 0, 5);
1004 printk(KERN_INFO "Au0828 can't set alternate to 5!\n");
1007 dev->width = NTSC_STD_W;
1008 dev->height = NTSC_STD_H;
1009 dev->frame_size = dev->width * dev->height * 2;
1010 dev->field_size = dev->width * dev->height;
1011 dev->bytesperline = dev->width * 2;
1013 au0828_analog_stream_enable(dev);
1014 au0828_analog_stream_reset(dev);
1016 /* If we were doing ac97 instead of i2s, it would go here...*/
1017 au0828_i2s_init(dev);
1019 dev->stream_state = STREAM_OFF;
1020 dev->dev_state |= DEV_INITIALIZED;
1025 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
1027 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1028 V4L2_FIELD_INTERLACED,
1029 sizeof(struct au0828_buffer), fh, NULL);
1032 dev->vbi_width = 720;
1033 dev->vbi_height = 1;
1034 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
1036 V4L2_BUF_TYPE_VBI_CAPTURE,
1038 sizeof(struct au0828_buffer), fh, NULL);
1043 static int au0828_v4l2_close(struct file *filp)
1046 struct au0828_fh *fh = filp->private_data;
1047 struct au0828_dev *dev = fh->dev;
1049 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1050 /* Cancel timeout thread in case they didn't call streamoff */
1051 dev->vid_timeout_running = 0;
1052 del_timer_sync(&dev->vid_timeout);
1054 videobuf_stop(&fh->vb_vidq);
1055 res_free(fh, AU0828_RESOURCE_VIDEO);
1058 if (res_check(fh, AU0828_RESOURCE_VBI)) {
1059 /* Cancel timeout thread in case they didn't call streamoff */
1060 dev->vbi_timeout_running = 0;
1061 del_timer_sync(&dev->vbi_timeout);
1063 videobuf_stop(&fh->vb_vbiq);
1064 res_free(fh, AU0828_RESOURCE_VBI);
1067 if (dev->users == 1) {
1068 if (dev->dev_state & DEV_DISCONNECTED) {
1069 au0828_analog_unregister(dev);
1074 au0828_analog_stream_disable(dev);
1076 au0828_uninit_isoc(dev);
1078 /* Save some power by putting tuner to sleep */
1079 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1081 /* When close the device, set the usb intf0 into alt0 to free
1083 ret = usb_set_interface(dev->usbdev, 0, 0);
1085 printk(KERN_INFO "Au0828 can't set alternate to 0!\n");
1088 videobuf_mmap_free(&fh->vb_vidq);
1089 videobuf_mmap_free(&fh->vb_vbiq);
1092 wake_up_interruptible_nr(&dev->open, 1);
1096 static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
1097 size_t count, loff_t *pos)
1099 struct au0828_fh *fh = filp->private_data;
1100 struct au0828_dev *dev = fh->dev;
1103 rc = check_dev(dev);
1107 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1108 if (res_locked(dev, AU0828_RESOURCE_VIDEO))
1111 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1112 filp->f_flags & O_NONBLOCK);
1115 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1116 if (!res_get(fh, AU0828_RESOURCE_VBI))
1119 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1120 filp->f_flags & O_NONBLOCK);
1126 static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
1128 struct au0828_fh *fh = filp->private_data;
1129 struct au0828_dev *dev = fh->dev;
1132 rc = check_dev(dev);
1136 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1137 if (!res_get(fh, AU0828_RESOURCE_VIDEO))
1139 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1140 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1141 if (!res_get(fh, AU0828_RESOURCE_VBI))
1143 return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1149 static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1151 struct au0828_fh *fh = filp->private_data;
1152 struct au0828_dev *dev = fh->dev;
1155 rc = check_dev(dev);
1159 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1160 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1161 else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
1162 rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
1167 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1168 struct v4l2_format *format)
1171 int width = format->fmt.pix.width;
1172 int height = format->fmt.pix.height;
1173 unsigned int maxwidth, maxheight;
1178 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1181 /* If they are demanding a format other than the one we support,
1182 bail out (tvtime asks for UYVY and then retries with YUYV) */
1183 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1186 /* format->fmt.pix.width only support 720 and height 480 */
1192 format->fmt.pix.width = width;
1193 format->fmt.pix.height = height;
1194 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1195 format->fmt.pix.bytesperline = width * 2;
1196 format->fmt.pix.sizeimage = width * height * 2;
1197 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1198 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1200 if (cmd == VIDIOC_TRY_FMT)
1203 /* maybe set new image format, driver current only support 720*480 */
1205 dev->height = height;
1206 dev->frame_size = width * height * 2;
1207 dev->field_size = width * height;
1208 dev->bytesperline = width * 2;
1210 if (dev->stream_state == STREAM_ON) {
1211 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1212 ret = au0828_stream_interrupt(dev);
1214 dprintk(1, "error interrupting video stream!\n");
1219 /* set au0828 interface0 to AS5 here again */
1220 ret = usb_set_interface(dev->usbdev, 0, 5);
1222 printk(KERN_INFO "Au0828 can't set alt setting to 5!\n");
1226 au0828_analog_stream_enable(dev);
1232 static int vidioc_queryctrl(struct file *file, void *priv,
1233 struct v4l2_queryctrl *qc)
1235 struct au0828_fh *fh = priv;
1236 struct au0828_dev *dev = fh->dev;
1237 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
1244 static int vidioc_querycap(struct file *file, void *priv,
1245 struct v4l2_capability *cap)
1247 struct au0828_fh *fh = priv;
1248 struct au0828_dev *dev = fh->dev;
1250 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1251 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1252 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
1254 cap->version = AU0828_VERSION_CODE;
1256 /*set the device capabilities */
1257 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1258 V4L2_CAP_VBI_CAPTURE |
1260 V4L2_CAP_READWRITE |
1261 V4L2_CAP_STREAMING |
1266 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1267 struct v4l2_fmtdesc *f)
1272 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1273 strcpy(f->description, "Packed YUV2");
1276 f->pixelformat = V4L2_PIX_FMT_UYVY;
1281 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1282 struct v4l2_format *f)
1284 struct au0828_fh *fh = priv;
1285 struct au0828_dev *dev = fh->dev;
1287 f->fmt.pix.width = dev->width;
1288 f->fmt.pix.height = dev->height;
1289 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1290 f->fmt.pix.bytesperline = dev->bytesperline;
1291 f->fmt.pix.sizeimage = dev->frame_size;
1292 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1293 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1297 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1298 struct v4l2_format *f)
1300 struct au0828_fh *fh = priv;
1301 struct au0828_dev *dev = fh->dev;
1303 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1306 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1307 struct v4l2_format *f)
1309 struct au0828_fh *fh = priv;
1310 struct au0828_dev *dev = fh->dev;
1313 rc = check_dev(dev);
1317 mutex_lock(&dev->lock);
1319 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1320 printk(KERN_INFO "%s queue busy\n", __func__);
1325 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1327 mutex_unlock(&dev->lock);
1331 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1333 struct au0828_fh *fh = priv;
1334 struct au0828_dev *dev = fh->dev;
1336 /* FIXME: when we support something other than NTSC, we are going to
1337 have to make the au0828 bridge adjust the size of its capture
1338 buffer, which is currently hardcoded at 720x480 */
1340 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, *norm);
1344 static int vidioc_enum_input(struct file *file, void *priv,
1345 struct v4l2_input *input)
1347 struct au0828_fh *fh = priv;
1348 struct au0828_dev *dev = fh->dev;
1351 static const char *inames[] = {
1352 [AU0828_VMUX_UNDEFINED] = "Undefined",
1353 [AU0828_VMUX_COMPOSITE] = "Composite",
1354 [AU0828_VMUX_SVIDEO] = "S-Video",
1355 [AU0828_VMUX_CABLE] = "Cable TV",
1356 [AU0828_VMUX_TELEVISION] = "Television",
1357 [AU0828_VMUX_DVB] = "DVB",
1358 [AU0828_VMUX_DEBUG] = "tv debug"
1363 if (tmp >= AU0828_MAX_INPUT)
1365 if (AUVI_INPUT(tmp).type == 0)
1369 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1370 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1371 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE))
1372 input->type |= V4L2_INPUT_TYPE_TUNER;
1374 input->type |= V4L2_INPUT_TYPE_CAMERA;
1376 input->std = dev->vdev->tvnorms;
1381 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1383 struct au0828_fh *fh = priv;
1384 struct au0828_dev *dev = fh->dev;
1385 *i = dev->ctrl_input;
1389 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1391 struct au0828_fh *fh = priv;
1392 struct au0828_dev *dev = fh->dev;
1395 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1397 if (index >= AU0828_MAX_INPUT)
1399 if (AUVI_INPUT(index).type == 0)
1401 dev->ctrl_input = index;
1403 switch (AUVI_INPUT(index).type) {
1404 case AU0828_VMUX_SVIDEO:
1405 dev->input_type = AU0828_VMUX_SVIDEO;
1407 case AU0828_VMUX_COMPOSITE:
1408 dev->input_type = AU0828_VMUX_COMPOSITE;
1410 case AU0828_VMUX_TELEVISION:
1411 dev->input_type = AU0828_VMUX_TELEVISION;
1414 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1415 AUVI_INPUT(index).type);
1419 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1420 AUVI_INPUT(index).vmux, 0, 0);
1422 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1424 if (AUVI_INPUT(i).audio_setup == NULL)
1432 (AUVI_INPUT(i).audio_setup)(dev, enable);
1434 /* Make sure we leave it turned on if some
1435 other input is routed to this callback */
1436 if ((AUVI_INPUT(i).audio_setup) !=
1437 ((AUVI_INPUT(index).audio_setup))) {
1438 (AUVI_INPUT(i).audio_setup)(dev, enable);
1443 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1444 AUVI_INPUT(index).amux, 0, 0);
1448 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1450 struct au0828_fh *fh = priv;
1451 struct au0828_dev *dev = fh->dev;
1452 unsigned int index = a->index;
1457 index = dev->ctrl_ainput;
1459 strcpy(a->name, "Television");
1461 strcpy(a->name, "Line in");
1463 a->capability = V4L2_AUDCAP_STEREO;
1468 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1470 struct au0828_fh *fh = priv;
1471 struct au0828_dev *dev = fh->dev;
1472 if (a->index != dev->ctrl_ainput)
1477 static int vidioc_g_ctrl(struct file *file, void *priv,
1478 struct v4l2_control *ctrl)
1480 struct au0828_fh *fh = priv;
1481 struct au0828_dev *dev = fh->dev;
1483 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1488 static int vidioc_s_ctrl(struct file *file, void *priv,
1489 struct v4l2_control *ctrl)
1491 struct au0828_fh *fh = priv;
1492 struct au0828_dev *dev = fh->dev;
1493 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1497 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1499 struct au0828_fh *fh = priv;
1500 struct au0828_dev *dev = fh->dev;
1505 strcpy(t->name, "Auvitek tuner");
1506 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1510 static int vidioc_s_tuner(struct file *file, void *priv,
1511 struct v4l2_tuner *t)
1513 struct au0828_fh *fh = priv;
1514 struct au0828_dev *dev = fh->dev;
1519 t->type = V4L2_TUNER_ANALOG_TV;
1520 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1521 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1527 static int vidioc_g_frequency(struct file *file, void *priv,
1528 struct v4l2_frequency *freq)
1530 struct au0828_fh *fh = priv;
1531 struct au0828_dev *dev = fh->dev;
1533 freq->type = V4L2_TUNER_ANALOG_TV;
1534 freq->frequency = dev->ctrl_freq;
1538 static int vidioc_s_frequency(struct file *file, void *priv,
1539 struct v4l2_frequency *freq)
1541 struct au0828_fh *fh = priv;
1542 struct au0828_dev *dev = fh->dev;
1544 if (freq->tuner != 0)
1546 if (freq->type != V4L2_TUNER_ANALOG_TV)
1549 dev->ctrl_freq = freq->frequency;
1551 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1553 au0828_analog_stream_reset(dev);
1559 /* RAW VBI ioctls */
1561 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1562 struct v4l2_format *format)
1564 struct au0828_fh *fh = priv;
1565 struct au0828_dev *dev = fh->dev;
1567 format->fmt.vbi.samples_per_line = dev->vbi_width;
1568 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1569 format->fmt.vbi.offset = 0;
1570 format->fmt.vbi.flags = 0;
1571 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1573 format->fmt.vbi.count[0] = dev->vbi_height;
1574 format->fmt.vbi.count[1] = dev->vbi_height;
1575 format->fmt.vbi.start[0] = 21;
1576 format->fmt.vbi.start[1] = 284;
1581 static int vidioc_g_chip_ident(struct file *file, void *priv,
1582 struct v4l2_dbg_chip_ident *chip)
1584 struct au0828_fh *fh = priv;
1585 struct au0828_dev *dev = fh->dev;
1586 chip->ident = V4L2_IDENT_NONE;
1589 if (v4l2_chip_match_host(&chip->match)) {
1590 chip->ident = V4L2_IDENT_AU0828;
1594 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1595 if (chip->ident == V4L2_IDENT_NONE)
1601 static int vidioc_cropcap(struct file *file, void *priv,
1602 struct v4l2_cropcap *cc)
1604 struct au0828_fh *fh = priv;
1605 struct au0828_dev *dev = fh->dev;
1607 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1610 cc->bounds.left = 0;
1612 cc->bounds.width = dev->width;
1613 cc->bounds.height = dev->height;
1615 cc->defrect = cc->bounds;
1617 cc->pixelaspect.numerator = 54;
1618 cc->pixelaspect.denominator = 59;
1623 static int vidioc_streamon(struct file *file, void *priv,
1624 enum v4l2_buf_type type)
1626 struct au0828_fh *fh = priv;
1627 struct au0828_dev *dev = fh->dev;
1630 rc = check_dev(dev);
1634 if (unlikely(type != fh->type))
1637 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1638 fh, type, fh->resources, dev->resources);
1640 if (unlikely(!res_get(fh, get_ressource(fh))))
1643 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1644 au0828_analog_stream_enable(dev);
1645 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
1648 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1649 rc = videobuf_streamon(&fh->vb_vidq);
1650 dev->vid_timeout_running = 1;
1651 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1652 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1653 rc = videobuf_streamon(&fh->vb_vbiq);
1654 dev->vbi_timeout_running = 1;
1655 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1661 static int vidioc_streamoff(struct file *file, void *priv,
1662 enum v4l2_buf_type type)
1664 struct au0828_fh *fh = priv;
1665 struct au0828_dev *dev = fh->dev;
1669 rc = check_dev(dev);
1673 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1674 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1676 if (type != fh->type)
1679 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1680 fh, type, fh->resources, dev->resources);
1682 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1683 dev->vid_timeout_running = 0;
1684 del_timer_sync(&dev->vid_timeout);
1686 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1687 rc = au0828_stream_interrupt(dev);
1691 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1692 if (AUVI_INPUT(i).audio_setup == NULL)
1694 (AUVI_INPUT(i).audio_setup)(dev, 0);
1697 videobuf_streamoff(&fh->vb_vidq);
1698 res_free(fh, AU0828_RESOURCE_VIDEO);
1699 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1700 dev->vbi_timeout_running = 0;
1701 del_timer_sync(&dev->vbi_timeout);
1703 videobuf_streamoff(&fh->vb_vbiq);
1704 res_free(fh, AU0828_RESOURCE_VBI);
1710 #ifdef CONFIG_VIDEO_ADV_DEBUG
1711 static int vidioc_g_register(struct file *file, void *priv,
1712 struct v4l2_dbg_register *reg)
1714 struct au0828_fh *fh = priv;
1715 struct au0828_dev *dev = fh->dev;
1717 switch (reg->match.type) {
1718 case V4L2_CHIP_MATCH_I2C_DRIVER:
1719 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1726 static int vidioc_s_register(struct file *file, void *priv,
1727 struct v4l2_dbg_register *reg)
1729 struct au0828_fh *fh = priv;
1730 struct au0828_dev *dev = fh->dev;
1732 switch (reg->match.type) {
1733 case V4L2_CHIP_MATCH_I2C_DRIVER:
1734 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1743 static int vidioc_reqbufs(struct file *file, void *priv,
1744 struct v4l2_requestbuffers *rb)
1746 struct au0828_fh *fh = priv;
1747 struct au0828_dev *dev = fh->dev;
1750 rc = check_dev(dev);
1754 return videobuf_reqbufs(&fh->vb_vidq, rb);
1757 static int vidioc_querybuf(struct file *file, void *priv,
1758 struct v4l2_buffer *b)
1760 struct au0828_fh *fh = priv;
1761 struct au0828_dev *dev = fh->dev;
1764 rc = check_dev(dev);
1768 return videobuf_querybuf(&fh->vb_vidq, b);
1771 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1773 struct au0828_fh *fh = priv;
1774 struct au0828_dev *dev = fh->dev;
1777 rc = check_dev(dev);
1781 return videobuf_qbuf(&fh->vb_vidq, b);
1784 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1786 struct au0828_fh *fh = priv;
1787 struct au0828_dev *dev = fh->dev;
1790 rc = check_dev(dev);
1794 /* Workaround for a bug in the au0828 hardware design that sometimes
1795 results in the colorspace being inverted */
1796 if (dev->greenscreen_detected == 1) {
1797 dprintk(1, "Detected green frame. Resetting stream...\n");
1798 au0828_analog_stream_reset(dev);
1799 dev->greenscreen_detected = 0;
1802 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1805 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1806 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1808 struct au0828_fh *fh = priv;
1810 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1814 static struct v4l2_file_operations au0828_v4l_fops = {
1815 .owner = THIS_MODULE,
1816 .open = au0828_v4l2_open,
1817 .release = au0828_v4l2_close,
1818 .read = au0828_v4l2_read,
1819 .poll = au0828_v4l2_poll,
1820 .mmap = au0828_v4l2_mmap,
1821 .ioctl = video_ioctl2,
1824 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1825 .vidioc_querycap = vidioc_querycap,
1826 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1827 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1828 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1829 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1830 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1831 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1832 .vidioc_g_audio = vidioc_g_audio,
1833 .vidioc_s_audio = vidioc_s_audio,
1834 .vidioc_cropcap = vidioc_cropcap,
1835 .vidioc_reqbufs = vidioc_reqbufs,
1836 .vidioc_querybuf = vidioc_querybuf,
1837 .vidioc_qbuf = vidioc_qbuf,
1838 .vidioc_dqbuf = vidioc_dqbuf,
1839 .vidioc_s_std = vidioc_s_std,
1840 .vidioc_enum_input = vidioc_enum_input,
1841 .vidioc_g_input = vidioc_g_input,
1842 .vidioc_s_input = vidioc_s_input,
1843 .vidioc_queryctrl = vidioc_queryctrl,
1844 .vidioc_g_ctrl = vidioc_g_ctrl,
1845 .vidioc_s_ctrl = vidioc_s_ctrl,
1846 .vidioc_streamon = vidioc_streamon,
1847 .vidioc_streamoff = vidioc_streamoff,
1848 .vidioc_g_tuner = vidioc_g_tuner,
1849 .vidioc_s_tuner = vidioc_s_tuner,
1850 .vidioc_g_frequency = vidioc_g_frequency,
1851 .vidioc_s_frequency = vidioc_s_frequency,
1852 #ifdef CONFIG_VIDEO_ADV_DEBUG
1853 .vidioc_g_register = vidioc_g_register,
1854 .vidioc_s_register = vidioc_s_register,
1856 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1857 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1858 .vidiocgmbuf = vidiocgmbuf,
1862 static const struct video_device au0828_video_template = {
1863 .fops = &au0828_v4l_fops,
1864 .release = video_device_release,
1865 .ioctl_ops = &video_ioctl_ops,
1866 .tvnorms = V4L2_STD_NTSC_M,
1867 .current_norm = V4L2_STD_NTSC_M,
1870 /**************************************************************************/
1872 int au0828_analog_register(struct au0828_dev *dev,
1873 struct usb_interface *interface)
1875 int retval = -ENOMEM;
1876 struct usb_host_interface *iface_desc;
1877 struct usb_endpoint_descriptor *endpoint;
1880 dprintk(1, "au0828_analog_register called!\n");
1882 /* set au0828 usb interface0 to as5 */
1883 retval = usb_set_interface(dev->usbdev,
1884 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1886 printk(KERN_INFO "Failure setting usb interface0 to as5\n");
1890 /* Figure out which endpoint has the isoc interface */
1891 iface_desc = interface->cur_altsetting;
1892 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1893 endpoint = &iface_desc->endpoint[i].desc;
1894 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1896 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1897 == USB_ENDPOINT_XFER_ISOC)) {
1899 /* we find our isoc in endpoint */
1900 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1901 dev->max_pkt_size = (tmp & 0x07ff) *
1902 (((tmp & 0x1800) >> 11) + 1);
1903 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1906 if (!(dev->isoc_in_endpointaddr)) {
1907 printk(KERN_INFO "Could not locate isoc endpoint\n");
1912 init_waitqueue_head(&dev->open);
1913 spin_lock_init(&dev->slock);
1914 mutex_init(&dev->lock);
1916 /* init video dma queues */
1917 INIT_LIST_HEAD(&dev->vidq.active);
1918 INIT_LIST_HEAD(&dev->vidq.queued);
1919 INIT_LIST_HEAD(&dev->vbiq.active);
1920 INIT_LIST_HEAD(&dev->vbiq.queued);
1922 dev->vid_timeout.function = au0828_vid_buffer_timeout;
1923 dev->vid_timeout.data = (unsigned long) dev;
1924 init_timer(&dev->vid_timeout);
1926 dev->vbi_timeout.function = au0828_vbi_buffer_timeout;
1927 dev->vbi_timeout.data = (unsigned long) dev;
1928 init_timer(&dev->vbi_timeout);
1930 dev->width = NTSC_STD_W;
1931 dev->height = NTSC_STD_H;
1932 dev->field_size = dev->width * dev->height;
1933 dev->frame_size = dev->field_size << 1;
1934 dev->bytesperline = dev->width << 1;
1935 dev->ctrl_ainput = 0;
1937 /* allocate and fill v4l2 video struct */
1938 dev->vdev = video_device_alloc();
1939 if (NULL == dev->vdev) {
1940 dprintk(1, "Can't allocate video_device.\n");
1944 /* allocate the VBI struct */
1945 dev->vbi_dev = video_device_alloc();
1946 if (NULL == dev->vbi_dev) {
1947 dprintk(1, "Can't allocate vbi_device.\n");
1952 /* Fill the video capture device struct */
1953 *dev->vdev = au0828_video_template;
1954 dev->vdev->parent = &dev->usbdev->dev;
1955 strcpy(dev->vdev->name, "au0828a video");
1957 /* Setup the VBI device */
1958 *dev->vbi_dev = au0828_video_template;
1959 dev->vbi_dev->parent = &dev->usbdev->dev;
1960 strcpy(dev->vbi_dev->name, "au0828a vbi");
1962 /* Register the v4l2 device */
1963 video_set_drvdata(dev->vdev, dev);
1964 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
1966 dprintk(1, "unable to register video device (error = %d).\n",
1968 video_device_release(dev->vdev);
1972 /* Register the vbi device */
1973 video_set_drvdata(dev->vbi_dev, dev);
1974 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
1976 dprintk(1, "unable to register vbi device (error = %d).\n",
1978 video_device_release(dev->vbi_dev);
1979 video_device_release(dev->vdev);
1983 dprintk(1, "%s completed!\n", __func__);