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)
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/device.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-event.h>
40 #include <media/tuner.h>
41 #include "au0828-reg.h"
43 static DEFINE_MUTEX(au0828_sysfs_lock);
45 /* ------------------------------------------------------------------
47 ------------------------------------------------------------------*/
49 static unsigned int isoc_debug;
50 module_param(isoc_debug, int, 0644);
51 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
53 #define au0828_isocdbg(fmt, arg...) \
56 pr_info("au0828 %s :"fmt, \
61 static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
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);
67 static inline void print_err_status(struct au0828_dev *dev,
68 int packet, int status)
70 char *errmsg = "Unknown";
74 errmsg = "unlinked synchronuously";
77 errmsg = "unlinked asynchronuously";
80 errmsg = "Buffer error (overrun)";
83 errmsg = "Stalled (device not responding)";
86 errmsg = "Babble (bad cable?)";
89 errmsg = "Bit-stuff error (bad cable?)";
92 errmsg = "CRC/Timeout (could be anything)";
95 errmsg = "Device does not respond";
99 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
101 au0828_isocdbg("URB packet %d, status %d [%s].\n",
102 packet, status, errmsg);
106 static int check_dev(struct au0828_dev *dev)
108 if (dev->dev_state & DEV_DISCONNECTED) {
109 pr_info("v4l2 ioctl: device not present\n");
113 if (dev->dev_state & DEV_MISCONFIGURED) {
114 pr_info("v4l2 ioctl: device is misconfigured; "
115 "close and open it again\n");
122 * IRQ callback, called by URB callback
124 static void au0828_irq_callback(struct urb *urb)
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;
131 switch (urb->status) {
132 case 0: /* success */
133 case -ETIMEDOUT: /* NAK */
135 case -ECONNRESET: /* kill */
138 au0828_isocdbg("au0828_irq_callback called: status kill\n");
140 default: /* unknown error */
141 au0828_isocdbg("urb completition error %d.\n", urb->status);
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);
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;
157 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
159 au0828_isocdbg("urb resubmit failed (error=%i)\n",
162 dev->stream_state = STREAM_ON;
166 * Stop and Deallocate URBs
168 static void au0828_uninit_isoc(struct au0828_dev *dev)
173 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
175 dev->isoc_ctl.nfields = -1;
176 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
177 urb = dev->isoc_ctl.urb[i];
179 if (!irqs_disabled())
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],
191 dev->isoc_ctl.urb[i] = NULL;
193 dev->isoc_ctl.transfer_buffer[i] = NULL;
196 kfree(dev->isoc_ctl.urb);
197 kfree(dev->isoc_ctl.transfer_buffer);
199 dev->isoc_ctl.urb = NULL;
200 dev->isoc_ctl.transfer_buffer = NULL;
201 dev->isoc_ctl.num_bufs = 0;
203 dev->stream_state = STREAM_OFF;
207 * Allocate URBs and start IRQ
209 static 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))
213 struct au0828_dmaqueue *dma_q = &dev->vidq;
220 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
222 /* De-allocates all pending stuff */
223 au0828_uninit_isoc(dev);
225 dev->isoc_ctl.isoc_copy = isoc_copy;
226 dev->isoc_ctl.num_bufs = num_bufs;
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");
234 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
236 if (!dev->isoc_ctl.transfer_buffer) {
237 au0828_isocdbg("cannot allocate memory for usb transfer\n");
238 kfree(dev->isoc_ctl.urb);
242 dev->isoc_ctl.max_pkt_size = max_pkt_size;
243 dev->isoc_ctl.buf = NULL;
245 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
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);
251 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
252 au0828_uninit_isoc(dev);
255 dev->isoc_ctl.urb[i] = urb;
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"
263 in_interrupt() ? " while in int" : "");
264 au0828_uninit_isoc(dev);
267 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
269 pipe = usb_rcvisocpipe(dev->usbdev,
270 dev->isoc_in_endpointaddr),
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);
276 urb->number_of_packets = max_packets;
277 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
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;
288 init_waitqueue_head(&dma_q->wq);
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);
294 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
296 au0828_uninit_isoc(dev);
305 * Announces that a buffer were filled and request the next
307 static inline void buffer_filled(struct au0828_dev *dev,
308 struct au0828_dmaqueue *dma_q,
309 struct au0828_buffer *buf)
311 /* Advice that buffer was filled */
312 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
314 buf->vb.state = VIDEOBUF_DONE;
315 buf->vb.field_count++;
316 v4l2_get_timestamp(&buf->vb.ts);
318 dev->isoc_ctl.buf = NULL;
320 list_del(&buf->vb.queue);
321 wake_up(&buf->vb.done);
324 static inline void vbi_buffer_filled(struct au0828_dev *dev,
325 struct au0828_dmaqueue *dma_q,
326 struct au0828_buffer *buf)
328 /* Advice that buffer was filled */
329 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
331 buf->vb.state = VIDEOBUF_DONE;
332 buf->vb.field_count++;
333 v4l2_get_timestamp(&buf->vb.ts);
335 dev->isoc_ctl.vbi_buf = NULL;
337 list_del(&buf->vb.queue);
338 wake_up(&buf->vb.done);
342 * Identify the buffer header type and properly handles
344 static void au0828_copy_video(struct au0828_dev *dev,
345 struct au0828_dmaqueue *dma_q,
346 struct au0828_buffer *buf,
348 unsigned char *outp, unsigned long len)
350 void *fieldstart, *startwrite, *startread;
351 int linesdone, currlinedone, offset, lencopy, remain;
352 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
357 if (dma_q->pos + len > buf->vb.size)
358 len = buf->vb.size - dma_q->pos;
363 /* Interlaces frame */
367 fieldstart = outp + bytesperline;
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;
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;
385 memcpy(startwrite, startread, lencopy);
390 startwrite += lencopy + bytesperline;
391 startread += lencopy;
392 if (bytesperline > remain)
395 lencopy = bytesperline;
397 if ((char *)startwrite + lencopy > (char *)outp +
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 -
408 memcpy(startwrite, startread, lencopy);
414 /* We have enough data to check for greenscreen */
415 if (outp[0] < 0x60 && outp[1440] < 0x60)
416 dev->greenscreen_detected = 1;
423 * video-buf generic routine to get the next available buffer
425 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
426 struct au0828_buffer **buf)
428 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
430 if (list_empty(&dma_q->active)) {
431 au0828_isocdbg("No active queue to serve\n");
432 dev->isoc_ctl.buf = NULL;
437 /* Get the next buffer */
438 *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue);
439 dev->isoc_ctl.buf = *buf;
444 static void au0828_copy_vbi(struct au0828_dev *dev,
445 struct au0828_dmaqueue *dma_q,
446 struct au0828_buffer *buf,
448 unsigned char *outp, unsigned long len)
450 unsigned char *startwrite, *startread;
455 au0828_isocdbg("dev is null\n");
460 au0828_isocdbg("dma_q is null\n");
466 au0828_isocdbg("p is null\n");
470 au0828_isocdbg("outp is null\n");
474 bytesperline = dev->vbi_width;
476 if (dma_q->pos + len > buf->vb.size)
477 len = buf->vb.size - dma_q->pos;
480 startwrite = outp + (dma_q->pos / 2);
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;
486 for (i = 0; i < len; i += 2)
487 startwrite[j++] = startread[i+1];
494 * video-buf generic routine to get the next available VBI buffer
496 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
497 struct au0828_buffer **buf)
499 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
502 if (list_empty(&dma_q->active)) {
503 au0828_isocdbg("No active queue to serve\n");
504 dev->isoc_ctl.vbi_buf = NULL;
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);
515 dev->isoc_ctl.vbi_buf = *buf;
521 * Controls the isoc copy of each urb packet
523 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
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;
534 unsigned int vbi_field_size;
535 unsigned int remain, lencopy;
540 if ((dev->dev_state & DEV_DISCONNECTED) ||
541 (dev->dev_state & DEV_MISCONFIGURED))
544 if (urb->status < 0) {
545 print_err_status(dev, -1, urb->status);
546 if (urb->status == -ENOENT)
550 buf = dev->isoc_ctl.buf;
552 outp = videobuf_to_vmalloc(&buf->vb);
554 vbi_buf = dev->isoc_ctl.vbi_buf;
556 vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
558 for (i = 0; i < urb->number_of_packets; i++) {
559 int status = urb->iso_frame_desc[i].status;
562 print_err_status(dev, i, status);
563 if (urb->iso_frame_desc[i].status != -EPROTO)
567 if (urb->iso_frame_desc[i].actual_length <= 0)
570 if (urb->iso_frame_desc[i].actual_length >
572 au0828_isocdbg("packet bigger than packet size");
576 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
578 len = urb->iso_frame_desc[i].actual_length - 4;
584 au0828_isocdbg("Video frame %s\n",
585 (fbyte & 0x40) ? "odd" : "even");
589 vbi_buffer_filled(dev,
592 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
596 vbioutp = videobuf_to_vmalloc(
601 buffer_filled(dev, dma_q, buf);
602 get_next_buf(dma_q, &buf);
606 outp = videobuf_to_vmalloc(&buf->vb);
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));
625 if (vbi_buf != NULL) {
627 vbi_buf->top_field = 1;
629 vbi_buf->top_field = 0;
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;
646 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
651 dev->vbi_read += lencopy;
654 if (dev->vbi_read >= vbi_field_size && buf != NULL)
655 au0828_copy_video(dev, dma_q, buf, p, outp, len);
661 buffer_setup(struct videobuf_queue *vq, unsigned int *count,
664 struct au0828_fh *fh = vq->priv_data;
665 *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
668 *count = AU0828_DEF_BUF;
670 if (*count < AU0828_MIN_BUF)
671 *count = AU0828_MIN_BUF;
675 /* This is called *without* dev->slock held; please keep it that way */
676 static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf)
678 struct au0828_fh *fh = vq->priv_data;
679 struct au0828_dev *dev = fh->dev;
680 unsigned long flags = 0;
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.)
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.
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);
698 videobuf_vmalloc_free(&buf->vb);
699 buf->vb.state = VIDEOBUF_NEEDS_INIT;
703 buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
704 enum v4l2_field field)
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;
711 buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3;
713 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
716 buf->vb.width = dev->width;
717 buf->vb.height = dev->height;
718 buf->vb.field = field;
720 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
721 rc = videobuf_iolock(vq, &buf->vb, NULL);
723 pr_info("videobuf_iolock failed\n");
728 if (!dev->isoc_ctl.num_bufs)
732 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
733 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
736 pr_info("au0828_init_isoc failed\n");
741 buf->vb.state = VIDEOBUF_PREPARED;
745 free_buffer(vq, buf);
750 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
752 struct au0828_buffer *buf = container_of(vb,
753 struct au0828_buffer,
755 struct au0828_fh *fh = vq->priv_data;
756 struct au0828_dev *dev = fh->dev;
757 struct au0828_dmaqueue *vidq = &dev->vidq;
759 buf->vb.state = VIDEOBUF_QUEUED;
760 list_add_tail(&buf->vb.queue, &vidq->active);
763 static void buffer_release(struct videobuf_queue *vq,
764 struct videobuf_buffer *vb)
766 struct au0828_buffer *buf = container_of(vb,
767 struct au0828_buffer,
770 free_buffer(vq, buf);
773 static 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,
780 /* ------------------------------------------------------------------
782 ------------------------------------------------------------------*/
784 static int au0828_i2s_init(struct au0828_dev *dev)
786 /* Enable i2s mode */
787 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
792 * Auvitek au0828 analog stream enable
794 static int au0828_analog_stream_enable(struct au0828_dev *d)
796 struct usb_interface *iface;
799 dprintk(1, "au0828_analog_stream_enable called\n");
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);
807 pr_info("Au0828 can't set alt setting to 5!\n");
812 h = d->height / 2 + 2;
815 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
816 au0828_writereg(d, 0x106, 0x00);
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);
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);
832 int au0828_analog_stream_disable(struct au0828_dev *d)
834 dprintk(1, "au0828_analog_stream_disable called\n");
835 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
839 static void au0828_analog_stream_reset(struct au0828_dev *dev)
841 dprintk(1, "au0828_analog_stream_reset called\n");
842 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
844 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
848 * Some operations needs to stop current streaming
850 static int au0828_stream_interrupt(struct au0828_dev *dev)
854 dev->stream_state = STREAM_INTERRUPT;
855 if (dev->dev_state == DEV_DISCONNECTED)
858 dev->dev_state = DEV_MISCONFIGURED;
859 dprintk(1, "%s device is misconfigured!\n", __func__);
866 * au0828_release_resources
867 * unregister v4l2 devices
869 void au0828_analog_unregister(struct au0828_dev *dev)
871 dprintk(1, "au0828_release_resources called\n");
872 mutex_lock(&au0828_sysfs_lock);
875 video_unregister_device(dev->vdev);
877 video_unregister_device(dev->vbi_dev);
879 mutex_unlock(&au0828_sysfs_lock);
883 /* Usage lock check functions */
884 static int res_get(struct au0828_fh *fh, unsigned int bit)
886 struct au0828_dev *dev = fh->dev;
888 if (fh->resources & bit)
889 /* have it already allocated */
893 if (dev->resources & bit) {
894 /* no, someone else uses it */
897 /* it's free, grab it */
898 fh->resources |= bit;
899 dev->resources |= bit;
900 dprintk(1, "res: get %d\n", bit);
905 static int res_check(struct au0828_fh *fh, unsigned int bit)
907 return fh->resources & bit;
910 static int res_locked(struct au0828_dev *dev, unsigned int bit)
912 return dev->resources & bit;
915 static void res_free(struct au0828_fh *fh, unsigned int bits)
917 struct au0828_dev *dev = fh->dev;
919 BUG_ON((fh->resources & bits) != bits);
921 fh->resources &= ~bits;
922 dev->resources &= ~bits;
923 dprintk(1, "res: put %d\n", bits);
926 static int get_ressource(struct au0828_fh *fh)
929 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
930 return AU0828_RESOURCE_VIDEO;
931 case V4L2_BUF_TYPE_VBI_CAPTURE:
932 return AU0828_RESOURCE_VBI;
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) */
942 static void au0828_vid_buffer_timeout(unsigned long data)
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;
950 spin_lock_irqsave(&dev->slock, flags);
952 buf = dev->isoc_ctl.buf;
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);
958 get_next_buf(dma_q, &buf);
960 if (dev->vid_timeout_running == 1)
961 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
963 spin_unlock_irqrestore(&dev->slock, flags);
966 static void au0828_vbi_buffer_timeout(unsigned long data)
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;
974 spin_lock_irqsave(&dev->slock, flags);
976 buf = dev->isoc_ctl.vbi_buf;
978 vbi_data = videobuf_to_vmalloc(&buf->vb);
979 memset(vbi_data, 0x00, buf->vb.size);
980 vbi_buffer_filled(dev, dma_q, buf);
982 vbi_get_next_buf(dma_q, &buf);
984 if (dev->vbi_timeout_running == 1)
985 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
986 spin_unlock_irqrestore(&dev->slock, flags);
990 static int au0828_v4l2_open(struct file *filp)
993 struct video_device *vdev = video_devdata(filp);
994 struct au0828_dev *dev = video_drvdata(filp);
995 struct au0828_fh *fh;
998 switch (vdev->vfl_type) {
999 case VFL_TYPE_GRABBER:
1000 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1003 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1009 fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL);
1011 dprintk(1, "Failed allocate au0828_fh struct!\n");
1017 v4l2_fh_init(&fh->fh, vdev);
1018 filp->private_data = fh;
1020 if (mutex_lock_interruptible(&dev->lock)) {
1022 return -ERESTARTSYS;
1024 if (dev->users == 0) {
1025 au0828_analog_stream_enable(dev);
1026 au0828_analog_stream_reset(dev);
1028 /* If we were doing ac97 instead of i2s, it would go here...*/
1029 au0828_i2s_init(dev);
1031 dev->stream_state = STREAM_OFF;
1032 dev->dev_state |= DEV_INITIALIZED;
1036 mutex_unlock(&dev->lock);
1038 videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops,
1040 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1041 V4L2_FIELD_INTERLACED,
1042 sizeof(struct au0828_buffer), fh,
1046 videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops,
1048 V4L2_BUF_TYPE_VBI_CAPTURE,
1050 sizeof(struct au0828_buffer), fh,
1052 v4l2_fh_add(&fh->fh);
1056 static int au0828_v4l2_close(struct file *filp)
1059 struct au0828_fh *fh = filp->private_data;
1060 struct au0828_dev *dev = fh->dev;
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);
1070 videobuf_stop(&fh->vb_vidq);
1071 res_free(fh, AU0828_RESOURCE_VIDEO);
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);
1079 videobuf_stop(&fh->vb_vbiq);
1080 res_free(fh, AU0828_RESOURCE_VBI);
1083 if (dev->users == 1 && video_is_registered(video_devdata(filp))) {
1084 au0828_analog_stream_disable(dev);
1086 au0828_uninit_isoc(dev);
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;
1092 /* When close the device, set the usb intf0 into alt0 to free
1094 ret = usb_set_interface(dev->usbdev, 0, 0);
1096 pr_info("Au0828 can't set alternate to 0!\n");
1098 mutex_unlock(&dev->lock);
1100 videobuf_mmap_free(&fh->vb_vidq);
1101 videobuf_mmap_free(&fh->vb_vbiq);
1104 wake_up_interruptible_nr(&dev->open, 1);
1108 /* Must be called with dev->lock held */
1109 static void au0828_init_tuner(struct au0828_dev *dev)
1111 struct v4l2_frequency f = {
1112 .frequency = dev->ctrl_freq,
1113 .type = V4L2_TUNER_ANALOG_TV,
1116 if (dev->std_set_in_tuner_core)
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);
1128 static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf,
1129 size_t count, loff_t *pos)
1131 struct au0828_fh *fh = filp->private_data;
1132 struct au0828_dev *dev = fh->dev;
1135 rc = check_dev(dev);
1139 if (mutex_lock_interruptible(&dev->lock))
1140 return -ERESTARTSYS;
1141 au0828_init_tuner(dev);
1142 mutex_unlock(&dev->lock);
1144 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1145 if (res_locked(dev, AU0828_RESOURCE_VIDEO))
1148 return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
1149 filp->f_flags & O_NONBLOCK);
1152 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1153 if (!res_get(fh, AU0828_RESOURCE_VBI))
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));
1163 return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
1164 filp->f_flags & O_NONBLOCK);
1170 static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait)
1172 struct au0828_fh *fh = filp->private_data;
1173 struct au0828_dev *dev = fh->dev;
1174 unsigned long req_events = poll_requested_events(wait);
1177 if (check_dev(dev) < 0)
1180 res = v4l2_ctrl_poll(filp, wait);
1181 if (!(req_events & (POLLIN | POLLRDNORM)))
1184 if (mutex_lock_interruptible(&dev->lock))
1185 return -ERESTARTSYS;
1186 au0828_init_tuner(dev);
1187 mutex_unlock(&dev->lock);
1189 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1190 if (!res_get(fh, AU0828_RESOURCE_VIDEO))
1192 return res | videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1194 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1195 if (!res_get(fh, AU0828_RESOURCE_VBI))
1197 return res | videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
1202 static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
1204 struct au0828_fh *fh = filp->private_data;
1205 struct au0828_dev *dev = fh->dev;
1208 rc = check_dev(dev);
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);
1220 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1221 struct v4l2_format *format)
1224 int width = format->fmt.pix.width;
1225 int height = format->fmt.pix.height;
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)
1232 /* format->fmt.pix.width only support 720 and height 480 */
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;
1247 if (cmd == VIDIOC_TRY_FMT)
1250 /* maybe set new image format, driver current only support 720*480 */
1252 dev->height = height;
1253 dev->frame_size = width * height * 2;
1254 dev->field_size = width * height;
1255 dev->bytesperline = width * 2;
1257 if (dev->stream_state == STREAM_ON) {
1258 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1259 ret = au0828_stream_interrupt(dev);
1261 dprintk(1, "error interrupting video stream!\n");
1266 au0828_analog_stream_enable(dev);
1272 static int vidioc_querycap(struct file *file, void *priv,
1273 struct v4l2_capability *cap)
1275 struct video_device *vdev = video_devdata(file);
1276 struct au0828_fh *fh = priv;
1277 struct au0828_dev *dev = fh->dev;
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));
1283 /* set the device capabilities */
1284 cap->device_caps = V4L2_CAP_AUDIO |
1285 V4L2_CAP_READWRITE |
1286 V4L2_CAP_STREAMING |
1288 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1289 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
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;
1297 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1298 struct v4l2_fmtdesc *f)
1303 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1304 strcpy(f->description, "Packed YUV2");
1307 f->pixelformat = V4L2_PIX_FMT_UYVY;
1312 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1313 struct v4l2_format *f)
1315 struct au0828_fh *fh = priv;
1316 struct au0828_dev *dev = fh->dev;
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;
1329 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1330 struct v4l2_format *f)
1332 struct au0828_fh *fh = priv;
1333 struct au0828_dev *dev = fh->dev;
1335 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1338 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1339 struct v4l2_format *f)
1341 struct au0828_fh *fh = priv;
1342 struct au0828_dev *dev = fh->dev;
1345 rc = check_dev(dev);
1349 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
1350 pr_info("%s queue busy\n", __func__);
1355 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1360 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1362 struct au0828_fh *fh = priv;
1363 struct au0828_dev *dev = fh->dev;
1367 au0828_init_tuner(dev);
1369 i2c_gate_ctrl(dev, 1);
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
1377 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1379 i2c_gate_ctrl(dev, 0);
1384 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1386 struct au0828_fh *fh = priv;
1387 struct au0828_dev *dev = fh->dev;
1393 static int vidioc_enum_input(struct file *file, void *priv,
1394 struct v4l2_input *input)
1396 struct au0828_fh *fh = priv;
1397 struct au0828_dev *dev = fh->dev;
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"
1412 if (tmp >= AU0828_MAX_INPUT)
1414 if (AUVI_INPUT(tmp).type == 0)
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;
1424 input->type |= V4L2_INPUT_TYPE_CAMERA;
1425 input->audioset = 2;
1428 input->std = dev->vdev->tvnorms;
1433 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1435 struct au0828_fh *fh = priv;
1436 struct au0828_dev *dev = fh->dev;
1437 *i = dev->ctrl_input;
1441 static void au0828_s_input(struct au0828_dev *dev, int index)
1445 switch (AUVI_INPUT(index).type) {
1446 case AU0828_VMUX_SVIDEO:
1447 dev->input_type = AU0828_VMUX_SVIDEO;
1448 dev->ctrl_ainput = 1;
1450 case AU0828_VMUX_COMPOSITE:
1451 dev->input_type = AU0828_VMUX_COMPOSITE;
1452 dev->ctrl_ainput = 1;
1454 case AU0828_VMUX_TELEVISION:
1455 dev->input_type = AU0828_VMUX_TELEVISION;
1456 dev->ctrl_ainput = 0;
1459 dprintk(1, "unknown input type set [%d]\n",
1460 AUVI_INPUT(index).type);
1464 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1465 AUVI_INPUT(index).vmux, 0, 0);
1467 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1469 if (AUVI_INPUT(i).audio_setup == NULL)
1477 (AUVI_INPUT(i).audio_setup)(dev, enable);
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);
1488 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1489 AUVI_INPUT(index).amux, 0, 0);
1492 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1494 struct au0828_fh *fh = priv;
1495 struct au0828_dev *dev = fh->dev;
1497 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1499 if (index >= AU0828_MAX_INPUT)
1501 if (AUVI_INPUT(index).type == 0)
1503 dev->ctrl_input = index;
1504 au0828_s_input(dev, index);
1508 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1514 strcpy(a->name, "Television");
1516 strcpy(a->name, "Line in");
1518 a->capability = V4L2_AUDCAP_STEREO;
1522 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1524 struct au0828_fh *fh = priv;
1525 struct au0828_dev *dev = fh->dev;
1527 a->index = dev->ctrl_ainput;
1529 strcpy(a->name, "Television");
1531 strcpy(a->name, "Line in");
1533 a->capability = V4L2_AUDCAP_STEREO;
1537 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1539 struct au0828_fh *fh = priv;
1540 struct au0828_dev *dev = fh->dev;
1542 if (a->index != dev->ctrl_ainput)
1547 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1549 struct au0828_fh *fh = priv;
1550 struct au0828_dev *dev = fh->dev;
1555 strcpy(t->name, "Auvitek tuner");
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);
1564 static int vidioc_s_tuner(struct file *file, void *priv,
1565 const struct v4l2_tuner *t)
1567 struct au0828_fh *fh = priv;
1568 struct au0828_dev *dev = fh->dev;
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);
1578 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1585 static int vidioc_g_frequency(struct file *file, void *priv,
1586 struct v4l2_frequency *freq)
1588 struct au0828_fh *fh = priv;
1589 struct au0828_dev *dev = fh->dev;
1591 if (freq->tuner != 0)
1593 freq->frequency = dev->ctrl_freq;
1597 static int vidioc_s_frequency(struct file *file, void *priv,
1598 const struct v4l2_frequency *freq)
1600 struct au0828_fh *fh = priv;
1601 struct au0828_dev *dev = fh->dev;
1602 struct v4l2_frequency new_freq = *freq;
1604 if (freq->tuner != 0)
1607 au0828_init_tuner(dev);
1608 i2c_gate_ctrl(dev, 1);
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;
1615 i2c_gate_ctrl(dev, 0);
1617 au0828_analog_stream_reset(dev);
1623 /* RAW VBI ioctls */
1625 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1626 struct v4l2_format *format)
1628 struct au0828_fh *fh = priv;
1629 struct au0828_dev *dev = fh->dev;
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;
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));
1646 static int vidioc_cropcap(struct file *file, void *priv,
1647 struct v4l2_cropcap *cc)
1649 struct au0828_fh *fh = priv;
1650 struct au0828_dev *dev = fh->dev;
1652 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1655 cc->bounds.left = 0;
1657 cc->bounds.width = dev->width;
1658 cc->bounds.height = dev->height;
1660 cc->defrect = cc->bounds;
1662 cc->pixelaspect.numerator = 54;
1663 cc->pixelaspect.denominator = 59;
1668 static int vidioc_streamon(struct file *file, void *priv,
1669 enum v4l2_buf_type type)
1671 struct au0828_fh *fh = priv;
1672 struct au0828_dev *dev = fh->dev;
1675 rc = check_dev(dev);
1679 if (unlikely(type != fh->type))
1682 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1683 fh, type, fh->resources, dev->resources);
1685 if (unlikely(!res_get(fh, get_ressource(fh))))
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);
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));
1707 static int vidioc_streamoff(struct file *file, void *priv,
1708 enum v4l2_buf_type type)
1710 struct au0828_fh *fh = priv;
1711 struct au0828_dev *dev = fh->dev;
1715 rc = check_dev(dev);
1719 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1720 fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
1722 if (type != fh->type)
1725 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1726 fh, type, fh->resources, dev->resources);
1728 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1729 dev->vid_timeout_running = 0;
1730 del_timer_sync(&dev->vid_timeout);
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);
1738 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1739 if (AUVI_INPUT(i).audio_setup == NULL)
1741 (AUVI_INPUT(i).audio_setup)(dev, 0);
1744 if (res_check(fh, AU0828_RESOURCE_VIDEO)) {
1745 videobuf_streamoff(&fh->vb_vidq);
1746 res_free(fh, AU0828_RESOURCE_VIDEO);
1748 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1749 dev->vbi_timeout_running = 0;
1750 del_timer_sync(&dev->vbi_timeout);
1752 if (res_check(fh, AU0828_RESOURCE_VBI)) {
1753 videobuf_streamoff(&fh->vb_vbiq);
1754 res_free(fh, AU0828_RESOURCE_VBI);
1761 #ifdef CONFIG_VIDEO_ADV_DEBUG
1762 static int vidioc_g_register(struct file *file, void *priv,
1763 struct v4l2_dbg_register *reg)
1765 struct au0828_fh *fh = priv;
1766 struct au0828_dev *dev = fh->dev;
1768 reg->val = au0828_read(dev, reg->reg);
1773 static int vidioc_s_register(struct file *file, void *priv,
1774 const struct v4l2_dbg_register *reg)
1776 struct au0828_fh *fh = priv;
1777 struct au0828_dev *dev = fh->dev;
1779 return au0828_writereg(dev, reg->reg, reg->val);
1783 static int vidioc_log_status(struct file *file, void *fh)
1785 struct video_device *vdev = video_devdata(file);
1787 v4l2_ctrl_log_status(file, fh);
1788 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1792 static int vidioc_reqbufs(struct file *file, void *priv,
1793 struct v4l2_requestbuffers *rb)
1795 struct au0828_fh *fh = priv;
1796 struct au0828_dev *dev = fh->dev;
1799 rc = check_dev(dev);
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);
1811 static int vidioc_querybuf(struct file *file, void *priv,
1812 struct v4l2_buffer *b)
1814 struct au0828_fh *fh = priv;
1815 struct au0828_dev *dev = fh->dev;
1818 rc = check_dev(dev);
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);
1830 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1832 struct au0828_fh *fh = priv;
1833 struct au0828_dev *dev = fh->dev;
1836 rc = check_dev(dev);
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);
1848 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1850 struct au0828_fh *fh = priv;
1851 struct au0828_dev *dev = fh->dev;
1854 rc = check_dev(dev);
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;
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);
1874 void au0828_v4l2_suspend(struct au0828_dev *dev)
1879 pr_info("stopping V4L2\n");
1881 if (dev->stream_state == STREAM_ON) {
1882 pr_info("stopping V4L2 active URBs\n");
1883 au0828_analog_stream_disable(dev);
1885 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1886 urb = dev->isoc_ctl.urb[i];
1888 if (!irqs_disabled())
1891 usb_unlink_urb(urb);
1896 if (dev->vid_timeout_running)
1897 del_timer_sync(&dev->vid_timeout);
1898 if (dev->vbi_timeout_running)
1899 del_timer_sync(&dev->vbi_timeout);
1902 void au0828_v4l2_resume(struct au0828_dev *dev)
1906 pr_info("restarting V4L2\n");
1908 if (dev->stream_state == STREAM_ON) {
1909 au0828_stream_interrupt(dev);
1910 au0828_init_tuner(dev);
1913 if (dev->vid_timeout_running)
1914 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1915 if (dev->vbi_timeout_running)
1916 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1918 /* If we were doing ac97 instead of i2s, it would go here...*/
1919 au0828_i2s_init(dev);
1921 au0828_analog_stream_enable(dev);
1923 if (!(dev->stream_state == STREAM_ON)) {
1924 au0828_analog_stream_reset(dev);
1926 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1927 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1929 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1931 au0828_uninit_isoc(dev);
1937 static struct v4l2_file_operations au0828_v4l_fops = {
1938 .owner = THIS_MODULE,
1939 .open = au0828_v4l2_open,
1940 .release = au0828_v4l2_close,
1941 .read = au0828_v4l2_read,
1942 .poll = au0828_v4l2_poll,
1943 .mmap = au0828_v4l2_mmap,
1944 .unlocked_ioctl = video_ioctl2,
1947 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1948 .vidioc_querycap = vidioc_querycap,
1949 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1950 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1951 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1952 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1953 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1954 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1955 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1956 .vidioc_enumaudio = vidioc_enumaudio,
1957 .vidioc_g_audio = vidioc_g_audio,
1958 .vidioc_s_audio = vidioc_s_audio,
1959 .vidioc_cropcap = vidioc_cropcap,
1960 .vidioc_reqbufs = vidioc_reqbufs,
1961 .vidioc_querybuf = vidioc_querybuf,
1962 .vidioc_qbuf = vidioc_qbuf,
1963 .vidioc_dqbuf = vidioc_dqbuf,
1964 .vidioc_s_std = vidioc_s_std,
1965 .vidioc_g_std = vidioc_g_std,
1966 .vidioc_enum_input = vidioc_enum_input,
1967 .vidioc_g_input = vidioc_g_input,
1968 .vidioc_s_input = vidioc_s_input,
1969 .vidioc_streamon = vidioc_streamon,
1970 .vidioc_streamoff = vidioc_streamoff,
1971 .vidioc_g_tuner = vidioc_g_tuner,
1972 .vidioc_s_tuner = vidioc_s_tuner,
1973 .vidioc_g_frequency = vidioc_g_frequency,
1974 .vidioc_s_frequency = vidioc_s_frequency,
1975 #ifdef CONFIG_VIDEO_ADV_DEBUG
1976 .vidioc_g_register = vidioc_g_register,
1977 .vidioc_s_register = vidioc_s_register,
1979 .vidioc_log_status = vidioc_log_status,
1980 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1981 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1984 static const struct video_device au0828_video_template = {
1985 .fops = &au0828_v4l_fops,
1986 .release = video_device_release,
1987 .ioctl_ops = &video_ioctl_ops,
1988 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1991 /**************************************************************************/
1993 int au0828_analog_register(struct au0828_dev *dev,
1994 struct usb_interface *interface)
1996 int retval = -ENOMEM;
1997 struct usb_host_interface *iface_desc;
1998 struct usb_endpoint_descriptor *endpoint;
2001 dprintk(1, "au0828_analog_register called for intf#%d!\n",
2002 interface->cur_altsetting->desc.bInterfaceNumber);
2004 /* set au0828 usb interface0 to as5 */
2005 retval = usb_set_interface(dev->usbdev,
2006 interface->cur_altsetting->desc.bInterfaceNumber, 5);
2008 pr_info("Failure setting usb interface0 to as5\n");
2012 /* Figure out which endpoint has the isoc interface */
2013 iface_desc = interface->cur_altsetting;
2014 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2015 endpoint = &iface_desc->endpoint[i].desc;
2016 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2018 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2019 == USB_ENDPOINT_XFER_ISOC)) {
2021 /* we find our isoc in endpoint */
2022 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
2023 dev->max_pkt_size = (tmp & 0x07ff) *
2024 (((tmp & 0x1800) >> 11) + 1);
2025 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
2027 "Found isoc endpoint 0x%02x, max size = %d\n",
2028 dev->isoc_in_endpointaddr, dev->max_pkt_size);
2031 if (!(dev->isoc_in_endpointaddr)) {
2032 pr_info("Could not locate isoc endpoint\n");
2037 init_waitqueue_head(&dev->open);
2038 spin_lock_init(&dev->slock);
2040 /* init video dma queues */
2041 INIT_LIST_HEAD(&dev->vidq.active);
2042 INIT_LIST_HEAD(&dev->vidq.queued);
2043 INIT_LIST_HEAD(&dev->vbiq.active);
2044 INIT_LIST_HEAD(&dev->vbiq.queued);
2046 dev->vid_timeout.function = au0828_vid_buffer_timeout;
2047 dev->vid_timeout.data = (unsigned long) dev;
2048 init_timer(&dev->vid_timeout);
2050 dev->vbi_timeout.function = au0828_vbi_buffer_timeout;
2051 dev->vbi_timeout.data = (unsigned long) dev;
2052 init_timer(&dev->vbi_timeout);
2054 dev->width = NTSC_STD_W;
2055 dev->height = NTSC_STD_H;
2056 dev->field_size = dev->width * dev->height;
2057 dev->frame_size = dev->field_size << 1;
2058 dev->bytesperline = dev->width << 1;
2059 dev->vbi_width = 720;
2060 dev->vbi_height = 1;
2061 dev->ctrl_ainput = 0;
2062 dev->ctrl_freq = 960;
2063 dev->std = V4L2_STD_NTSC_M;
2064 au0828_s_input(dev, 0);
2066 /* allocate and fill v4l2 video struct */
2067 dev->vdev = video_device_alloc();
2068 if (NULL == dev->vdev) {
2069 dprintk(1, "Can't allocate video_device.\n");
2073 /* allocate the VBI struct */
2074 dev->vbi_dev = video_device_alloc();
2075 if (NULL == dev->vbi_dev) {
2076 dprintk(1, "Can't allocate vbi_device.\n");
2081 /* Fill the video capture device struct */
2082 *dev->vdev = au0828_video_template;
2083 dev->vdev->v4l2_dev = &dev->v4l2_dev;
2084 dev->vdev->lock = &dev->lock;
2085 strcpy(dev->vdev->name, "au0828a video");
2087 /* Setup the VBI device */
2088 *dev->vbi_dev = au0828_video_template;
2089 dev->vbi_dev->v4l2_dev = &dev->v4l2_dev;
2090 dev->vbi_dev->lock = &dev->lock;
2091 strcpy(dev->vbi_dev->name, "au0828a vbi");
2093 /* Register the v4l2 device */
2094 video_set_drvdata(dev->vdev, dev);
2095 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
2097 dprintk(1, "unable to register video device (error = %d).\n",
2103 /* Register the vbi device */
2104 video_set_drvdata(dev->vbi_dev, dev);
2105 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
2107 dprintk(1, "unable to register vbi device (error = %d).\n",
2113 dprintk(1, "%s completed!\n", __func__);
2118 video_device_release(dev->vbi_dev);
2120 video_device_release(dev->vdev);