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/init.h>
33 #include <linux/device.h>
34 #include <linux/suspend.h>
35 #include <linux/version.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/tuner.h>
41 #include "au0828-reg.h"
43 static DEFINE_MUTEX(au0828_sysfs_lock);
45 #define AU0828_VERSION_CODE KERNEL_VERSION(0, 0, 1)
47 /* ------------------------------------------------------------------
49 ------------------------------------------------------------------*/
51 static unsigned int isoc_debug;
52 module_param(isoc_debug, int, 0644);
53 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
55 #define au0828_isocdbg(fmt, arg...) \
58 printk(KERN_INFO "au0828 %s :"fmt, \
63 static inline void print_err_status(struct au0828_dev *dev,
64 int packet, int status)
66 char *errmsg = "Unknown";
70 errmsg = "unlinked synchronuously";
73 errmsg = "unlinked asynchronuously";
76 errmsg = "Buffer error (overrun)";
79 errmsg = "Stalled (device not responding)";
82 errmsg = "Babble (bad cable?)";
85 errmsg = "Bit-stuff error (bad cable?)";
88 errmsg = "CRC/Timeout (could be anything)";
91 errmsg = "Device does not respond";
95 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
97 au0828_isocdbg("URB packet %d, status %d [%s].\n",
98 packet, status, errmsg);
102 static int check_dev(struct au0828_dev *dev)
104 if (dev->dev_state & DEV_DISCONNECTED) {
105 printk(KERN_INFO "v4l2 ioctl: device not present\n");
109 if (dev->dev_state & DEV_MISCONFIGURED) {
110 printk(KERN_INFO "v4l2 ioctl: device is misconfigured; "
111 "close and open it again\n");
118 * IRQ callback, called by URB callback
120 static void au0828_irq_callback(struct urb *urb)
122 struct au0828_dmaqueue *dma_q = urb->context;
123 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
126 switch (urb->status) {
127 case 0: /* success */
128 case -ETIMEDOUT: /* NAK */
130 case -ECONNRESET: /* kill */
133 au0828_isocdbg("au0828_irq_callback called: status kill\n");
135 default: /* unknown error */
136 au0828_isocdbg("urb completition error %d.\n", urb->status);
140 /* Copy data from URB */
141 spin_lock(&dev->slock);
142 rc = dev->isoc_ctl.isoc_copy(dev, urb);
143 spin_unlock(&dev->slock);
145 /* Reset urb buffers */
146 for (i = 0; i < urb->number_of_packets; i++) {
147 urb->iso_frame_desc[i].status = 0;
148 urb->iso_frame_desc[i].actual_length = 0;
152 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
154 au0828_isocdbg("urb resubmit failed (error=%i)\n",
160 * Stop and Deallocate URBs
162 void au0828_uninit_isoc(struct au0828_dev *dev)
167 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
169 dev->isoc_ctl.nfields = -1;
170 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
171 urb = dev->isoc_ctl.urb[i];
173 if (!irqs_disabled())
178 if (dev->isoc_ctl.transfer_buffer[i]) {
179 usb_buffer_free(dev->usbdev,
180 urb->transfer_buffer_length,
181 dev->isoc_ctl.transfer_buffer[i],
185 dev->isoc_ctl.urb[i] = NULL;
187 dev->isoc_ctl.transfer_buffer[i] = NULL;
190 kfree(dev->isoc_ctl.urb);
191 kfree(dev->isoc_ctl.transfer_buffer);
193 dev->isoc_ctl.urb = NULL;
194 dev->isoc_ctl.transfer_buffer = NULL;
195 dev->isoc_ctl.num_bufs = 0;
199 * Allocate URBs and start IRQ
201 int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
202 int num_bufs, int max_pkt_size,
203 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
205 struct au0828_dmaqueue *dma_q = &dev->vidq;
212 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
214 /* De-allocates all pending stuff */
215 au0828_uninit_isoc(dev);
217 dev->isoc_ctl.isoc_copy = isoc_copy;
218 dev->isoc_ctl.num_bufs = num_bufs;
220 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
221 if (!dev->isoc_ctl.urb) {
222 au0828_isocdbg("cannot alloc memory for usb buffers\n");
226 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
228 if (!dev->isoc_ctl.transfer_buffer) {
229 au0828_isocdbg("cannot allocate memory for usb transfer\n");
230 kfree(dev->isoc_ctl.urb);
234 dev->isoc_ctl.max_pkt_size = max_pkt_size;
235 dev->isoc_ctl.buf = NULL;
237 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
239 /* allocate urbs and transfer buffers */
240 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
241 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
243 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
244 au0828_uninit_isoc(dev);
247 dev->isoc_ctl.urb[i] = urb;
249 dev->isoc_ctl.transfer_buffer[i] = usb_buffer_alloc(dev->usbdev,
250 sb_size, GFP_KERNEL, &urb->transfer_dma);
251 if (!dev->isoc_ctl.transfer_buffer[i]) {
252 printk("unable to allocate %i bytes for transfer"
255 in_interrupt() ? " while in int" : "");
256 au0828_uninit_isoc(dev);
259 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
261 pipe = usb_rcvisocpipe(dev->usbdev,
262 dev->isoc_in_endpointaddr),
264 usb_fill_int_urb(urb, dev->usbdev, pipe,
265 dev->isoc_ctl.transfer_buffer[i], sb_size,
266 au0828_irq_callback, dma_q, 1);
268 urb->number_of_packets = max_packets;
269 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
272 for (j = 0; j < max_packets; j++) {
273 urb->iso_frame_desc[j].offset = k;
274 urb->iso_frame_desc[j].length =
275 dev->isoc_ctl.max_pkt_size;
276 k += dev->isoc_ctl.max_pkt_size;
280 init_waitqueue_head(&dma_q->wq);
282 /* submit urbs and enables IRQ */
283 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
284 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
286 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
288 au0828_uninit_isoc(dev);
297 * Announces that a buffer were filled and request the next
299 static inline void buffer_filled(struct au0828_dev *dev,
300 struct au0828_dmaqueue *dma_q,
301 struct au0828_buffer *buf)
303 /* Advice that buffer was filled */
304 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
306 buf->vb.state = VIDEOBUF_DONE;
307 buf->vb.field_count++;
308 do_gettimeofday(&buf->vb.ts);
310 dev->isoc_ctl.buf = NULL;
312 list_del(&buf->vb.queue);
313 wake_up(&buf->vb.done);
317 * Identify the buffer header type and properly handles
319 static void au0828_copy_video(struct au0828_dev *dev,
320 struct au0828_dmaqueue *dma_q,
321 struct au0828_buffer *buf,
323 unsigned char *outp, unsigned long len)
325 void *fieldstart, *startwrite, *startread;
326 int linesdone, currlinedone, offset, lencopy, remain;
327 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
329 if (dma_q->pos + len > buf->vb.size)
330 len = buf->vb.size - dma_q->pos;
335 /* Interlaces frame */
339 fieldstart = outp + bytesperline;
341 linesdone = dma_q->pos / bytesperline;
342 currlinedone = dma_q->pos % bytesperline;
343 offset = linesdone * bytesperline * 2 + currlinedone;
344 startwrite = fieldstart + offset;
345 lencopy = bytesperline - currlinedone;
346 lencopy = lencopy > remain ? remain : lencopy;
348 if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
349 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
350 ((char *)startwrite + lencopy) -
351 ((char *)outp + buf->vb.size));
352 remain = (char *)outp + buf->vb.size - (char *)startwrite;
357 memcpy(startwrite, startread, lencopy);
362 startwrite += lencopy + bytesperline;
363 startread += lencopy;
364 if (bytesperline > remain)
367 lencopy = bytesperline;
369 if ((char *)startwrite + lencopy > (char *)outp +
371 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
372 ((char *)startwrite + lencopy) -
373 ((char *)outp + buf->vb.size));
374 lencopy = remain = (char *)outp + buf->vb.size -
380 memcpy(startwrite, startread, lencopy);
386 /* We have enough data to check for greenscreen */
387 if (outp[0] < 0x60 && outp[1440] < 0x60)
388 dev->greenscreen_detected = 1;
395 * video-buf generic routine to get the next available buffer
397 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
398 struct au0828_buffer **buf)
400 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
402 if (list_empty(&dma_q->active)) {
403 au0828_isocdbg("No active queue to serve\n");
404 dev->isoc_ctl.buf = NULL;
409 /* Get the next buffer */
410 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
411 dev->isoc_ctl.buf = *buf;
417 * Controls the isoc copy of each urb packet
419 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
421 struct au0828_buffer *buf;
422 struct au0828_dmaqueue *dma_q = urb->context;
423 unsigned char *outp = NULL;
424 int i, len = 0, rc = 1;
431 if ((dev->dev_state & DEV_DISCONNECTED) ||
432 (dev->dev_state & DEV_MISCONFIGURED))
435 if (urb->status < 0) {
436 print_err_status(dev, -1, urb->status);
437 if (urb->status == -ENOENT)
441 buf = dev->isoc_ctl.buf;
443 outp = videobuf_to_vmalloc(&buf->vb);
445 for (i = 0; i < urb->number_of_packets; i++) {
446 int status = urb->iso_frame_desc[i].status;
449 print_err_status(dev, i, status);
450 if (urb->iso_frame_desc[i].status != -EPROTO)
454 if (urb->iso_frame_desc[i].actual_length <= 0)
457 if (urb->iso_frame_desc[i].actual_length >
459 au0828_isocdbg("packet bigger than packet size");
463 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
465 len = urb->iso_frame_desc[i].actual_length - 4;
471 au0828_isocdbg("Video frame %s\n",
472 (fbyte & 0x40) ? "odd" : "even");
473 if (!(fbyte & 0x40)) {
475 buffer_filled(dev, dma_q, buf);
476 get_next_buf(dma_q, &buf);
480 outp = videobuf_to_vmalloc(&buf->vb);
493 au0828_copy_video(dev, dma_q, buf, p, outp, len);
499 buffer_setup(struct videobuf_queue *vq, unsigned int *count,
502 struct au0828_fh *fh = vq->priv_data;
503 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
506 *count = AU0828_DEF_BUF;
508 if (*count < AU0828_MIN_BUF)
509 *count = AU0828_MIN_BUF;
513 /* This is called *without* dev->slock held; please keep it that way */
514 static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
516 struct au0828_fh *fh = vq->priv_data;
517 struct au0828_dev *dev = fh->dev;
518 unsigned long flags = 0;
522 /* We used to wait for the buffer to finish here, but this didn't work
523 because, as we were keeping the state as VIDEOBUF_QUEUED,
524 videobuf_queue_cancel marked it as finished for us.
525 (Also, it could wedge forever if the hardware was misconfigured.)
527 This should be safe; by the time we get here, the buffer isn't
528 queued anymore. If we ever start marking the buffers as
529 VIDEOBUF_ACTIVE, it won't be, though.
531 spin_lock_irqsave(&dev->slock, flags);
532 if (dev->isoc_ctl.buf == buf)
533 dev->isoc_ctl.buf = NULL;
534 spin_unlock_irqrestore(&dev->slock, flags);
536 videobuf_vmalloc_free(&buf->vb);
537 buf->vb.state = VIDEOBUF_NEEDS_INIT;
541 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
542 enum v4l2_field field)
544 struct au0828_fh *fh = vq->priv_data;
545 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
546 struct au0828_dev *dev = fh->dev;
547 int rc = 0, urb_init = 0;
549 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
551 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
554 buf->vb.width = dev->width;
555 buf->vb.height = dev->height;
556 buf->vb.field = field;
558 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
559 rc = videobuf_iolock(vq, &buf->vb, NULL);
561 printk(KERN_INFO "videobuf_iolock failed\n");
566 if (!dev->isoc_ctl.num_bufs)
570 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
571 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
574 printk(KERN_INFO "au0828_init_isoc failed\n");
579 buf->vb.state = VIDEOBUF_PREPARED;
583 free_buffer(vq, buf);
588 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
590 struct au0828_buffer *buf = container_of(vb,
591 struct au0828_buffer,
593 struct au0828_fh *fh = vq->priv_data;
594 struct au0828_dev *dev = fh->dev;
595 struct au0828_dmaqueue *vidq = &dev->vidq;
597 buf->vb.state = VIDEOBUF_QUEUED;
598 list_add_tail(&buf->vb.queue, &vidq->active);
601 static void buffer_release(struct videobuf_queue *vq,
602 struct videobuf_buffer *vb)
604 struct au0828_buffer *buf = container_of(vb,
605 struct au0828_buffer,
608 free_buffer(vq, buf);
611 static struct videobuf_queue_ops au0828_video_qops = {
612 .buf_setup = buffer_setup,
613 .buf_prepare = buffer_prepare,
614 .buf_queue = buffer_queue,
615 .buf_release = buffer_release,
618 /* ------------------------------------------------------------------
620 ------------------------------------------------------------------*/
622 static int au0828_i2s_init(struct au0828_dev *dev)
624 /* Enable i2s mode */
625 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
630 * Auvitek au0828 analog stream enable
631 * Please set interface0 to AS5 before enable the stream
633 int au0828_analog_stream_enable(struct au0828_dev *d)
635 dprintk(1, "au0828_analog_stream_enable called\n");
636 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
637 au0828_writereg(d, 0x106, 0x00);
639 au0828_writereg(d, 0x110, 0x00);
640 au0828_writereg(d, 0x111, 0x00);
641 au0828_writereg(d, 0x114, 0xa0);
642 au0828_writereg(d, 0x115, 0x05);
644 au0828_writereg(d, 0x112, 0x02);
645 au0828_writereg(d, 0x113, 0x00);
646 au0828_writereg(d, 0x116, 0xf2);
647 au0828_writereg(d, 0x117, 0x00);
648 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
653 int au0828_analog_stream_disable(struct au0828_dev *d)
655 dprintk(1, "au0828_analog_stream_disable called\n");
656 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
660 void au0828_analog_stream_reset(struct au0828_dev *dev)
662 dprintk(1, "au0828_analog_stream_reset called\n");
663 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
665 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
669 * Some operations needs to stop current streaming
671 static int au0828_stream_interrupt(struct au0828_dev *dev)
675 dev->stream_state = STREAM_INTERRUPT;
676 if (dev->dev_state == DEV_DISCONNECTED)
679 dev->dev_state = DEV_MISCONFIGURED;
680 dprintk(1, "%s device is misconfigured!\n", __func__);
687 * au0828_release_resources
688 * unregister v4l2 devices
690 void au0828_analog_unregister(struct au0828_dev *dev)
692 dprintk(1, "au0828_release_resources called\n");
693 mutex_lock(&au0828_sysfs_lock);
696 video_unregister_device(dev->vdev);
698 video_unregister_device(dev->vbi_dev);
700 mutex_unlock(&au0828_sysfs_lock);
704 /* Usage lock check functions */
705 static int res_get(struct au0828_fh *fh)
707 struct au0828_dev *dev = fh->dev;
710 /* This instance already has stream_on */
722 static int res_check(struct au0828_fh *fh)
724 return fh->stream_on;
727 static void res_free(struct au0828_fh *fh)
729 struct au0828_dev *dev = fh->dev;
735 static int au0828_v4l2_open(struct file *filp)
738 struct au0828_dev *dev = video_drvdata(filp);
739 struct au0828_fh *fh;
740 int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
742 #ifdef VBI_IS_WORKING
743 if (video_devdata(filp)->vfl_type == VFL_TYPE_GRABBER)
744 type = V4L2_BUF_TYPE_VBI_CAPTURE;
747 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
749 dprintk(1, "Failed allocate au0828_fh struct!\n");
755 filp->private_data = fh;
757 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
758 /* set au0828 interface0 to AS5 here again */
759 ret = usb_set_interface(dev->usbdev, 0, 5);
761 printk(KERN_INFO "Au0828 can't set alternate to 5!\n");
764 dev->width = NTSC_STD_W;
765 dev->height = NTSC_STD_H;
766 dev->frame_size = dev->width * dev->height * 2;
767 dev->field_size = dev->width * dev->height;
768 dev->bytesperline = dev->width * 2;
770 au0828_analog_stream_enable(dev);
771 au0828_analog_stream_reset(dev);
773 /* If we were doing ac97 instead of i2s, it would go here...*/
774 au0828_i2s_init(dev);
776 dev->stream_state = STREAM_OFF;
777 dev->dev_state |= DEV_INITIALIZED;
782 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
783 NULL, &dev->slock, fh->type,
784 V4L2_FIELD_INTERLACED,
785 sizeof(struct au0828_buffer), fh);
790 static int au0828_v4l2_close(struct file *filp)
793 struct au0828_fh *fh = filp->private_data;
794 struct au0828_dev *dev = fh->dev;
796 mutex_lock(&dev->lock);
800 if (dev->users == 1) {
801 videobuf_stop(&fh->vb_vidq);
802 videobuf_mmap_free(&fh->vb_vidq);
804 if (dev->dev_state & DEV_DISCONNECTED) {
805 au0828_analog_unregister(dev);
806 mutex_unlock(&dev->lock);
811 au0828_analog_stream_disable(dev);
813 au0828_uninit_isoc(dev);
815 /* Save some power by putting tuner to sleep */
816 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
818 /* When close the device, set the usb intf0 into alt0 to free
820 ret = usb_set_interface(dev->usbdev, 0, 0);
822 printk(KERN_INFO "Au0828 can't set alternate to 0!\n");
827 wake_up_interruptible_nr(&dev->open, 1);
828 mutex_unlock(&dev->lock);
832 static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
833 size_t count, loff_t *pos)
835 struct au0828_fh *fh = filp->private_data;
836 struct au0828_dev *dev = fh->dev;
843 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
844 mutex_lock(&dev->lock);
846 mutex_unlock(&dev->lock);
848 if (unlikely(rc < 0))
851 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
852 filp->f_flags & O_NONBLOCK);
857 static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
859 struct au0828_fh *fh = filp->private_data;
860 struct au0828_dev *dev = fh->dev;
867 mutex_lock(&dev->lock);
869 mutex_unlock(&dev->lock);
871 if (unlikely(rc < 0))
874 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
877 return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
880 static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
882 struct au0828_fh *fh = filp->private_data;
883 struct au0828_dev *dev = fh->dev;
890 mutex_lock(&dev->lock);
892 mutex_unlock(&dev->lock);
894 if (unlikely(rc < 0))
897 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
902 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
903 struct v4l2_format *format)
906 int width = format->fmt.pix.width;
907 int height = format->fmt.pix.height;
908 unsigned int maxwidth, maxheight;
913 #ifdef VBI_IS_WORKING
914 if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
915 dprintk(1, "VBI format set: to be supported!\n");
918 if (format->type == V4L2_BUF_TYPE_VBI_CAPTURE)
921 if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
924 /* If they are demanding a format other than the one we support,
925 bail out (tvtime asks for UYVY and then retries with YUYV) */
926 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
929 /* format->fmt.pix.width only support 720 and height 480 */
935 format->fmt.pix.width = width;
936 format->fmt.pix.height = height;
937 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
938 format->fmt.pix.bytesperline = width * 2;
939 format->fmt.pix.sizeimage = width * height * 2;
940 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
941 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
943 if (cmd == VIDIOC_TRY_FMT)
946 /* maybe set new image format, driver current only support 720*480 */
948 dev->height = height;
949 dev->frame_size = width * height * 2;
950 dev->field_size = width * height;
951 dev->bytesperline = width * 2;
953 if (dev->stream_state == STREAM_ON) {
954 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
955 ret = au0828_stream_interrupt(dev);
957 dprintk(1, "error interrupting video stream!\n");
962 /* set au0828 interface0 to AS5 here again */
963 ret = usb_set_interface(dev->usbdev, 0, 5);
965 printk(KERN_INFO "Au0828 can't set alt setting to 5!\n");
969 au0828_analog_stream_enable(dev);
975 static int vidioc_queryctrl(struct file *file, void *priv,
976 struct v4l2_queryctrl *qc)
978 struct au0828_fh *fh = priv;
979 struct au0828_dev *dev = fh->dev;
980 v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
987 static int vidioc_querycap(struct file *file, void *priv,
988 struct v4l2_capability *cap)
990 struct au0828_fh *fh = priv;
991 struct au0828_dev *dev = fh->dev;
993 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
994 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
995 strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
997 cap->version = AU0828_VERSION_CODE;
999 /*set the device capabilities */
1000 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
1001 #ifdef VBI_IS_WORKING
1002 V4L2_CAP_VBI_CAPTURE |
1005 V4L2_CAP_READWRITE |
1006 V4L2_CAP_STREAMING |
1011 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1012 struct v4l2_fmtdesc *f)
1017 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1018 strcpy(f->description, "Packed YUV2");
1021 f->pixelformat = V4L2_PIX_FMT_UYVY;
1026 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1027 struct v4l2_format *f)
1029 struct au0828_fh *fh = priv;
1030 struct au0828_dev *dev = fh->dev;
1032 f->fmt.pix.width = dev->width;
1033 f->fmt.pix.height = dev->height;
1034 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1035 f->fmt.pix.bytesperline = dev->bytesperline;
1036 f->fmt.pix.sizeimage = dev->frame_size;
1037 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1038 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1042 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1043 struct v4l2_format *f)
1045 struct au0828_fh *fh = priv;
1046 struct au0828_dev *dev = fh->dev;
1048 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1051 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1052 struct v4l2_format *f)
1054 struct au0828_fh *fh = priv;
1055 struct au0828_dev *dev = fh->dev;
1058 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1059 printk(KERN_INFO "%s queue busy\n", __func__);
1064 if (dev->stream_on && !fh->stream_on) {
1065 printk(KERN_INFO "%s device in use by another fh\n", __func__);
1070 return au0828_set_format(dev, VIDIOC_S_FMT, f);
1075 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
1077 struct au0828_fh *fh = priv;
1078 struct au0828_dev *dev = fh->dev;
1080 /* FIXME: when we support something other than NTSC, we are going to
1081 have to make the au0828 bridge adjust the size of its capture
1082 buffer, which is currently hardcoded at 720x480 */
1084 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, *norm);
1088 static int vidioc_enum_input(struct file *file, void *priv,
1089 struct v4l2_input *input)
1091 struct au0828_fh *fh = priv;
1092 struct au0828_dev *dev = fh->dev;
1095 static const char *inames[] = {
1096 [AU0828_VMUX_UNDEFINED] = "Undefined",
1097 [AU0828_VMUX_COMPOSITE] = "Composite",
1098 [AU0828_VMUX_SVIDEO] = "S-Video",
1099 [AU0828_VMUX_CABLE] = "Cable TV",
1100 [AU0828_VMUX_TELEVISION] = "Television",
1101 [AU0828_VMUX_DVB] = "DVB",
1102 [AU0828_VMUX_DEBUG] = "tv debug"
1107 if (tmp > AU0828_MAX_INPUT)
1109 if (AUVI_INPUT(tmp).type == 0)
1113 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1114 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1115 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE))
1116 input->type |= V4L2_INPUT_TYPE_TUNER;
1118 input->type |= V4L2_INPUT_TYPE_CAMERA;
1120 input->std = dev->vdev->tvnorms;
1125 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1127 struct au0828_fh *fh = priv;
1128 struct au0828_dev *dev = fh->dev;
1129 *i = dev->ctrl_input;
1133 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1135 struct au0828_fh *fh = priv;
1136 struct au0828_dev *dev = fh->dev;
1139 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1141 if (index >= AU0828_MAX_INPUT)
1143 if (AUVI_INPUT(index).type == 0)
1145 dev->ctrl_input = index;
1147 switch (AUVI_INPUT(index).type) {
1148 case AU0828_VMUX_SVIDEO:
1149 dev->input_type = AU0828_VMUX_SVIDEO;
1151 case AU0828_VMUX_COMPOSITE:
1152 dev->input_type = AU0828_VMUX_COMPOSITE;
1154 case AU0828_VMUX_TELEVISION:
1155 dev->input_type = AU0828_VMUX_TELEVISION;
1158 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1159 AUVI_INPUT(index).type);
1163 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1164 AUVI_INPUT(index).vmux, 0, 0);
1166 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1168 if (AUVI_INPUT(i).audio_setup == NULL)
1176 (AUVI_INPUT(i).audio_setup)(dev, enable);
1178 /* Make sure we leave it turned on if some
1179 other input is routed to this callback */
1180 if ((AUVI_INPUT(i).audio_setup) !=
1181 ((AUVI_INPUT(index).audio_setup))) {
1182 (AUVI_INPUT(i).audio_setup)(dev, enable);
1187 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1188 AUVI_INPUT(index).amux, 0, 0);
1192 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1194 struct au0828_fh *fh = priv;
1195 struct au0828_dev *dev = fh->dev;
1196 unsigned int index = a->index;
1201 index = dev->ctrl_ainput;
1203 strcpy(a->name, "Television");
1205 strcpy(a->name, "Line in");
1207 a->capability = V4L2_AUDCAP_STEREO;
1212 static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
1214 struct au0828_fh *fh = priv;
1215 struct au0828_dev *dev = fh->dev;
1216 if (a->index != dev->ctrl_ainput)
1221 static int vidioc_g_ctrl(struct file *file, void *priv,
1222 struct v4l2_control *ctrl)
1224 struct au0828_fh *fh = priv;
1225 struct au0828_dev *dev = fh->dev;
1227 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
1232 static int vidioc_s_ctrl(struct file *file, void *priv,
1233 struct v4l2_control *ctrl)
1235 struct au0828_fh *fh = priv;
1236 struct au0828_dev *dev = fh->dev;
1237 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
1241 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1243 struct au0828_fh *fh = priv;
1244 struct au0828_dev *dev = fh->dev;
1249 strcpy(t->name, "Auvitek tuner");
1250 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1254 static int vidioc_s_tuner(struct file *file, void *priv,
1255 struct v4l2_tuner *t)
1257 struct au0828_fh *fh = priv;
1258 struct au0828_dev *dev = fh->dev;
1263 t->type = V4L2_TUNER_ANALOG_TV;
1264 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1265 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1271 static int vidioc_g_frequency(struct file *file, void *priv,
1272 struct v4l2_frequency *freq)
1274 struct au0828_fh *fh = priv;
1275 struct au0828_dev *dev = fh->dev;
1277 freq->type = V4L2_TUNER_ANALOG_TV;
1278 freq->frequency = dev->ctrl_freq;
1282 static int vidioc_s_frequency(struct file *file, void *priv,
1283 struct v4l2_frequency *freq)
1285 struct au0828_fh *fh = priv;
1286 struct au0828_dev *dev = fh->dev;
1288 if (freq->tuner != 0)
1290 if (freq->type != V4L2_TUNER_ANALOG_TV)
1293 dev->ctrl_freq = freq->frequency;
1295 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1297 au0828_analog_stream_reset(dev);
1302 static int vidioc_g_chip_ident(struct file *file, void *priv,
1303 struct v4l2_dbg_chip_ident *chip)
1305 struct au0828_fh *fh = priv;
1306 struct au0828_dev *dev = fh->dev;
1307 chip->ident = V4L2_IDENT_NONE;
1310 if (v4l2_chip_match_host(&chip->match)) {
1311 chip->ident = V4L2_IDENT_AU0828;
1315 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
1316 if (chip->ident == V4L2_IDENT_NONE)
1322 static int vidioc_cropcap(struct file *file, void *priv,
1323 struct v4l2_cropcap *cc)
1325 struct au0828_fh *fh = priv;
1326 struct au0828_dev *dev = fh->dev;
1328 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1331 cc->bounds.left = 0;
1333 cc->bounds.width = dev->width;
1334 cc->bounds.height = dev->height;
1336 cc->defrect = cc->bounds;
1338 cc->pixelaspect.numerator = 54;
1339 cc->pixelaspect.denominator = 59;
1344 static int vidioc_streamon(struct file *file, void *priv,
1345 enum v4l2_buf_type type)
1347 struct au0828_fh *fh = priv;
1348 struct au0828_dev *dev = fh->dev;
1351 rc = check_dev(dev);
1355 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1356 au0828_analog_stream_enable(dev);
1357 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
1360 mutex_lock(&dev->lock);
1363 if (likely(rc >= 0))
1364 rc = videobuf_streamon(&fh->vb_vidq);
1365 mutex_unlock(&dev->lock);
1370 static int vidioc_streamoff(struct file *file, void *priv,
1371 enum v4l2_buf_type type)
1373 struct au0828_fh *fh = priv;
1374 struct au0828_dev *dev = fh->dev;
1379 rc = check_dev(dev);
1383 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1385 if (type != fh->type)
1388 if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1389 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1390 ret = au0828_stream_interrupt(dev);
1395 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1396 if (AUVI_INPUT(i).audio_setup == NULL)
1398 (AUVI_INPUT(i).audio_setup)(dev, 0);
1401 mutex_lock(&dev->lock);
1402 videobuf_streamoff(&fh->vb_vidq);
1404 mutex_unlock(&dev->lock);
1409 #ifdef CONFIG_VIDEO_ADV_DEBUG
1410 static int vidioc_g_register(struct file *file, void *priv,
1411 struct v4l2_dbg_register *reg)
1413 struct au0828_fh *fh = priv;
1414 struct au0828_dev *dev = fh->dev;
1416 switch (reg->match.type) {
1417 case V4L2_CHIP_MATCH_I2C_DRIVER:
1418 v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
1425 static int vidioc_s_register(struct file *file, void *priv,
1426 struct v4l2_dbg_register *reg)
1428 struct au0828_fh *fh = priv;
1429 struct au0828_dev *dev = fh->dev;
1431 switch (reg->match.type) {
1432 case V4L2_CHIP_MATCH_I2C_DRIVER:
1433 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
1442 static int vidioc_reqbufs(struct file *file, void *priv,
1443 struct v4l2_requestbuffers *rb)
1445 struct au0828_fh *fh = priv;
1446 struct au0828_dev *dev = fh->dev;
1449 rc = check_dev(dev);
1453 return videobuf_reqbufs(&fh->vb_vidq, rb);
1456 static int vidioc_querybuf(struct file *file, void *priv,
1457 struct v4l2_buffer *b)
1459 struct au0828_fh *fh = priv;
1460 struct au0828_dev *dev = fh->dev;
1463 rc = check_dev(dev);
1467 return videobuf_querybuf(&fh->vb_vidq, b);
1470 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1472 struct au0828_fh *fh = priv;
1473 struct au0828_dev *dev = fh->dev;
1476 rc = check_dev(dev);
1480 return videobuf_qbuf(&fh->vb_vidq, b);
1483 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1485 struct au0828_fh *fh = priv;
1486 struct au0828_dev *dev = fh->dev;
1489 rc = check_dev(dev);
1493 /* Workaround for a bug in the au0828 hardware design that sometimes
1494 results in the colorspace being inverted */
1495 if (dev->greenscreen_detected == 1) {
1496 dprintk(1, "Detected green frame. Resetting stream...\n");
1497 au0828_analog_stream_reset(dev);
1498 dev->greenscreen_detected = 0;
1501 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
1504 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1505 static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1507 struct au0828_fh *fh = priv;
1509 return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
1513 static struct v4l2_file_operations au0828_v4l_fops = {
1514 .owner = THIS_MODULE,
1515 .open = au0828_v4l2_open,
1516 .release = au0828_v4l2_close,
1517 .read = au0828_v4l2_read,
1518 .poll = au0828_v4l2_poll,
1519 .mmap = au0828_v4l2_mmap,
1520 .ioctl = video_ioctl2,
1523 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1524 .vidioc_querycap = vidioc_querycap,
1525 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1526 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1527 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1528 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1529 #ifdef VBI_IS_WORKING
1530 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1531 .vidioc_try_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
1532 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
1534 .vidioc_g_audio = vidioc_g_audio,
1535 .vidioc_s_audio = vidioc_s_audio,
1536 .vidioc_cropcap = vidioc_cropcap,
1537 #ifdef VBI_IS_WORKING
1538 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
1539 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1540 .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
1542 .vidioc_reqbufs = vidioc_reqbufs,
1543 .vidioc_querybuf = vidioc_querybuf,
1544 .vidioc_qbuf = vidioc_qbuf,
1545 .vidioc_dqbuf = vidioc_dqbuf,
1546 .vidioc_s_std = vidioc_s_std,
1547 .vidioc_enum_input = vidioc_enum_input,
1548 .vidioc_g_input = vidioc_g_input,
1549 .vidioc_s_input = vidioc_s_input,
1550 .vidioc_queryctrl = vidioc_queryctrl,
1551 .vidioc_g_ctrl = vidioc_g_ctrl,
1552 .vidioc_s_ctrl = vidioc_s_ctrl,
1553 .vidioc_streamon = vidioc_streamon,
1554 .vidioc_streamoff = vidioc_streamoff,
1555 .vidioc_g_tuner = vidioc_g_tuner,
1556 .vidioc_s_tuner = vidioc_s_tuner,
1557 .vidioc_g_frequency = vidioc_g_frequency,
1558 .vidioc_s_frequency = vidioc_s_frequency,
1559 #ifdef CONFIG_VIDEO_ADV_DEBUG
1560 .vidioc_g_register = vidioc_g_register,
1561 .vidioc_s_register = vidioc_s_register,
1563 .vidioc_g_chip_ident = vidioc_g_chip_ident,
1564 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1565 .vidiocgmbuf = vidiocgmbuf,
1569 static const struct video_device au0828_video_template = {
1570 .fops = &au0828_v4l_fops,
1571 .release = video_device_release,
1572 .ioctl_ops = &video_ioctl_ops,
1573 .tvnorms = V4L2_STD_NTSC_M,
1574 .current_norm = V4L2_STD_NTSC_M,
1577 /**************************************************************************/
1579 int au0828_analog_register(struct au0828_dev *dev,
1580 struct usb_interface *interface)
1582 int retval = -ENOMEM;
1583 struct usb_host_interface *iface_desc;
1584 struct usb_endpoint_descriptor *endpoint;
1587 dprintk(1, "au0828_analog_register called!\n");
1589 /* set au0828 usb interface0 to as5 */
1590 retval = usb_set_interface(dev->usbdev,
1591 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1593 printk(KERN_INFO "Failure setting usb interface0 to as5\n");
1597 /* Figure out which endpoint has the isoc interface */
1598 iface_desc = interface->cur_altsetting;
1599 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1600 endpoint = &iface_desc->endpoint[i].desc;
1601 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1603 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1604 == USB_ENDPOINT_XFER_ISOC)) {
1606 /* we find our isoc in endpoint */
1607 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1608 dev->max_pkt_size = (tmp & 0x07ff) *
1609 (((tmp & 0x1800) >> 11) + 1);
1610 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1613 if (!(dev->isoc_in_endpointaddr)) {
1614 printk(KERN_INFO "Could not locate isoc endpoint\n");
1619 init_waitqueue_head(&dev->open);
1620 spin_lock_init(&dev->slock);
1621 mutex_init(&dev->lock);
1623 INIT_LIST_HEAD(&dev->vidq.active);
1624 INIT_LIST_HEAD(&dev->vidq.queued);
1626 dev->width = NTSC_STD_W;
1627 dev->height = NTSC_STD_H;
1628 dev->field_size = dev->width * dev->height;
1629 dev->frame_size = dev->field_size << 1;
1630 dev->bytesperline = dev->width << 1;
1631 dev->ctrl_ainput = 0;
1633 /* allocate and fill v4l2 video struct */
1634 dev->vdev = video_device_alloc();
1635 if (NULL == dev->vdev) {
1636 dprintk(1, "Can't allocate video_device.\n");
1640 #ifdef VBI_IS_WORKING
1641 dev->vbi_dev = video_device_alloc();
1642 if (NULL == dev->vbi_dev) {
1643 dprintk(1, "Can't allocate vbi_device.\n");
1649 /* Fill the video capture device struct */
1650 *dev->vdev = au0828_video_template;
1651 dev->vdev->parent = &dev->usbdev->dev;
1652 strcpy(dev->vdev->name, "au0828a video");
1654 #ifdef VBI_IS_WORKING
1655 /* Setup the VBI device */
1656 *dev->vbi_dev = au0828_video_template;
1657 dev->vbi_dev->parent = &dev->usbdev->dev;
1658 strcpy(dev->vbi_dev->name, "au0828a vbi");
1661 /* Register the v4l2 device */
1662 video_set_drvdata(dev->vdev, dev);
1663 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
1665 dprintk(1, "unable to register video device (error = %d).\n",
1667 video_device_release(dev->vdev);
1671 #ifdef VBI_IS_WORKING
1672 /* Register the vbi device */
1673 video_set_drvdata(dev->vbi_dev, dev);
1674 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
1676 dprintk(1, "unable to register vbi device (error = %d).\n",
1678 video_device_release(dev->vbi_dev);
1679 video_device_release(dev->vdev);
1684 dprintk(1, "%s completed!\n", __func__);