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 * The hardware scaler supported is unimplemented
26 * AC97 audio support is unimplemented (only i2s audio mode)
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/device.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-event.h>
39 #include <media/tuner.h>
40 #include "au0828-reg.h"
42 static DEFINE_MUTEX(au0828_sysfs_lock);
44 /* ------------------------------------------------------------------
46 ------------------------------------------------------------------*/
48 static unsigned int isoc_debug;
49 module_param(isoc_debug, int, 0644);
50 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
52 #define au0828_isocdbg(fmt, arg...) \
55 pr_info("au0828 %s :"fmt, \
60 static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
62 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
63 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
66 static inline void print_err_status(struct au0828_dev *dev,
67 int packet, int status)
69 char *errmsg = "Unknown";
73 errmsg = "unlinked synchronuously";
76 errmsg = "unlinked asynchronuously";
79 errmsg = "Buffer error (overrun)";
82 errmsg = "Stalled (device not responding)";
85 errmsg = "Babble (bad cable?)";
88 errmsg = "Bit-stuff error (bad cable?)";
91 errmsg = "CRC/Timeout (could be anything)";
94 errmsg = "Device does not respond";
98 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
100 au0828_isocdbg("URB packet %d, status %d [%s].\n",
101 packet, status, errmsg);
105 static int check_dev(struct au0828_dev *dev)
107 if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) {
108 pr_info("v4l2 ioctl: device not present\n");
112 if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) {
113 pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n");
120 * IRQ callback, called by URB callback
122 static void au0828_irq_callback(struct urb *urb)
124 struct au0828_dmaqueue *dma_q = urb->context;
125 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
126 unsigned long flags = 0;
129 switch (urb->status) {
130 case 0: /* success */
131 case -ETIMEDOUT: /* NAK */
133 case -ECONNRESET: /* kill */
136 au0828_isocdbg("au0828_irq_callback called: status kill\n");
138 default: /* unknown error */
139 au0828_isocdbg("urb completition error %d.\n", urb->status);
143 /* Copy data from URB */
144 spin_lock_irqsave(&dev->slock, flags);
145 dev->isoc_ctl.isoc_copy(dev, urb);
146 spin_unlock_irqrestore(&dev->slock, flags);
148 /* Reset urb buffers */
149 for (i = 0; i < urb->number_of_packets; i++) {
150 urb->iso_frame_desc[i].status = 0;
151 urb->iso_frame_desc[i].actual_length = 0;
155 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
157 au0828_isocdbg("urb resubmit failed (error=%i)\n",
160 dev->stream_state = STREAM_ON;
164 * Stop and Deallocate URBs
166 static void au0828_uninit_isoc(struct au0828_dev *dev)
171 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
173 dev->isoc_ctl.nfields = -1;
174 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
175 urb = dev->isoc_ctl.urb[i];
177 if (!irqs_disabled())
182 if (dev->isoc_ctl.transfer_buffer[i]) {
183 usb_free_coherent(dev->usbdev,
184 urb->transfer_buffer_length,
185 dev->isoc_ctl.transfer_buffer[i],
189 dev->isoc_ctl.urb[i] = NULL;
191 dev->isoc_ctl.transfer_buffer[i] = NULL;
194 kfree(dev->isoc_ctl.urb);
195 kfree(dev->isoc_ctl.transfer_buffer);
197 dev->isoc_ctl.urb = NULL;
198 dev->isoc_ctl.transfer_buffer = NULL;
199 dev->isoc_ctl.num_bufs = 0;
201 dev->stream_state = STREAM_OFF;
205 * Allocate URBs and start IRQ
207 static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
208 int num_bufs, int max_pkt_size,
209 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
211 struct au0828_dmaqueue *dma_q = &dev->vidq;
218 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
220 dev->isoc_ctl.isoc_copy = isoc_copy;
221 dev->isoc_ctl.num_bufs = num_bufs;
223 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
224 if (!dev->isoc_ctl.urb) {
225 au0828_isocdbg("cannot alloc memory for usb buffers\n");
229 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
231 if (!dev->isoc_ctl.transfer_buffer) {
232 au0828_isocdbg("cannot allocate memory for usb transfer\n");
233 kfree(dev->isoc_ctl.urb);
237 dev->isoc_ctl.max_pkt_size = max_pkt_size;
238 dev->isoc_ctl.buf = NULL;
240 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
242 /* allocate urbs and transfer buffers */
243 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
244 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
246 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
247 au0828_uninit_isoc(dev);
250 dev->isoc_ctl.urb[i] = urb;
252 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
253 sb_size, GFP_KERNEL, &urb->transfer_dma);
254 if (!dev->isoc_ctl.transfer_buffer[i]) {
255 printk("unable to allocate %i bytes for transfer"
258 in_interrupt() ? " while in int" : "");
259 au0828_uninit_isoc(dev);
262 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
264 pipe = usb_rcvisocpipe(dev->usbdev,
265 dev->isoc_in_endpointaddr),
267 usb_fill_int_urb(urb, dev->usbdev, pipe,
268 dev->isoc_ctl.transfer_buffer[i], sb_size,
269 au0828_irq_callback, dma_q, 1);
271 urb->number_of_packets = max_packets;
272 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
275 for (j = 0; j < max_packets; j++) {
276 urb->iso_frame_desc[j].offset = k;
277 urb->iso_frame_desc[j].length =
278 dev->isoc_ctl.max_pkt_size;
279 k += dev->isoc_ctl.max_pkt_size;
283 /* submit urbs and enables IRQ */
284 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
285 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
287 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
289 au0828_uninit_isoc(dev);
298 * Announces that a buffer were filled and request the next
300 static inline void buffer_filled(struct au0828_dev *dev,
301 struct au0828_dmaqueue *dma_q,
302 struct au0828_buffer *buf)
304 struct vb2_v4l2_buffer *vb = &buf->vb;
305 struct vb2_queue *q = vb->vb2_buf.vb2_queue;
307 /* Advice that buffer was filled */
308 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
310 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
311 vb->sequence = dev->frame_count++;
313 vb->sequence = dev->vbi_frame_count++;
315 vb->field = V4L2_FIELD_INTERLACED;
316 v4l2_get_timestamp(&vb->timestamp);
317 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
321 * Identify the buffer header type and properly handles
323 static void au0828_copy_video(struct au0828_dev *dev,
324 struct au0828_dmaqueue *dma_q,
325 struct au0828_buffer *buf,
327 unsigned char *outp, unsigned long len)
329 void *fieldstart, *startwrite, *startread;
330 int linesdone, currlinedone, offset, lencopy, remain;
331 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
336 if (dma_q->pos + len > buf->length)
337 len = buf->length - dma_q->pos;
342 /* Interlaces frame */
346 fieldstart = outp + bytesperline;
348 linesdone = dma_q->pos / bytesperline;
349 currlinedone = dma_q->pos % bytesperline;
350 offset = linesdone * bytesperline * 2 + currlinedone;
351 startwrite = fieldstart + offset;
352 lencopy = bytesperline - currlinedone;
353 lencopy = lencopy > remain ? remain : lencopy;
355 if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
356 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
357 ((char *)startwrite + lencopy) -
358 ((char *)outp + buf->length));
359 remain = (char *)outp + buf->length - (char *)startwrite;
364 memcpy(startwrite, startread, lencopy);
369 startwrite += lencopy + bytesperline;
370 startread += lencopy;
371 if (bytesperline > remain)
374 lencopy = bytesperline;
376 if ((char *)startwrite + lencopy > (char *)outp +
378 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
379 ((char *)startwrite + lencopy) -
380 ((char *)outp + buf->length));
381 lencopy = remain = (char *)outp + buf->length -
387 memcpy(startwrite, startread, lencopy);
393 /* We have enough data to check for greenscreen */
394 if (outp[0] < 0x60 && outp[1440] < 0x60)
395 dev->greenscreen_detected = 1;
402 * video-buf generic routine to get the next available buffer
404 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
405 struct au0828_buffer **buf)
407 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
409 if (list_empty(&dma_q->active)) {
410 au0828_isocdbg("No active queue to serve\n");
411 dev->isoc_ctl.buf = NULL;
416 /* Get the next buffer */
417 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
418 /* Cleans up buffer - Useful for testing for frame/URB loss */
419 list_del(&(*buf)->list);
421 (*buf)->vb_buf = (*buf)->mem;
422 dev->isoc_ctl.buf = *buf;
427 static void au0828_copy_vbi(struct au0828_dev *dev,
428 struct au0828_dmaqueue *dma_q,
429 struct au0828_buffer *buf,
431 unsigned char *outp, unsigned long len)
433 unsigned char *startwrite, *startread;
438 au0828_isocdbg("dev is null\n");
443 au0828_isocdbg("dma_q is null\n");
449 au0828_isocdbg("p is null\n");
453 au0828_isocdbg("outp is null\n");
457 bytesperline = dev->vbi_width;
459 if (dma_q->pos + len > buf->length)
460 len = buf->length - dma_q->pos;
463 startwrite = outp + (dma_q->pos / 2);
465 /* Make sure the bottom field populates the second half of the frame */
466 if (buf->top_field == 0)
467 startwrite += bytesperline * dev->vbi_height;
469 for (i = 0; i < len; i += 2)
470 startwrite[j++] = startread[i+1];
477 * video-buf generic routine to get the next available VBI buffer
479 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
480 struct au0828_buffer **buf)
482 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
484 if (list_empty(&dma_q->active)) {
485 au0828_isocdbg("No active queue to serve\n");
486 dev->isoc_ctl.vbi_buf = NULL;
491 /* Get the next buffer */
492 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
493 /* Cleans up buffer - Useful for testing for frame/URB loss */
494 list_del(&(*buf)->list);
496 (*buf)->vb_buf = (*buf)->mem;
497 dev->isoc_ctl.vbi_buf = *buf;
502 * Controls the isoc copy of each urb packet
504 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
506 struct au0828_buffer *buf;
507 struct au0828_buffer *vbi_buf;
508 struct au0828_dmaqueue *dma_q = urb->context;
509 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
510 unsigned char *outp = NULL;
511 unsigned char *vbioutp = NULL;
512 int i, len = 0, rc = 1;
515 unsigned int vbi_field_size;
516 unsigned int remain, lencopy;
521 if (test_bit(DEV_DISCONNECTED, &dev->dev_state) ||
522 test_bit(DEV_MISCONFIGURED, &dev->dev_state))
525 if (urb->status < 0) {
526 print_err_status(dev, -1, urb->status);
527 if (urb->status == -ENOENT)
531 buf = dev->isoc_ctl.buf;
533 outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
535 vbi_buf = dev->isoc_ctl.vbi_buf;
537 vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
539 for (i = 0; i < urb->number_of_packets; i++) {
540 int status = urb->iso_frame_desc[i].status;
543 print_err_status(dev, i, status);
544 if (urb->iso_frame_desc[i].status != -EPROTO)
548 if (urb->iso_frame_desc[i].actual_length <= 0)
551 if (urb->iso_frame_desc[i].actual_length >
553 au0828_isocdbg("packet bigger than packet size");
557 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
559 len = urb->iso_frame_desc[i].actual_length - 4;
565 au0828_isocdbg("Video frame %s\n",
566 (fbyte & 0x40) ? "odd" : "even");
570 buffer_filled(dev, vbi_dma_q, vbi_buf);
571 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
575 vbioutp = vb2_plane_vaddr(
576 &vbi_buf->vb.vb2_buf, 0);
580 buffer_filled(dev, dma_q, buf);
581 get_next_buf(dma_q, &buf);
585 outp = vb2_plane_vaddr(
586 &buf->vb.vb2_buf, 0);
588 /* As long as isoc traffic is arriving, keep
589 resetting the timer */
590 if (dev->vid_timeout_running)
591 mod_timer(&dev->vid_timeout,
592 jiffies + (HZ / 10));
593 if (dev->vbi_timeout_running)
594 mod_timer(&dev->vbi_timeout,
595 jiffies + (HZ / 10));
605 if (vbi_buf != NULL) {
607 vbi_buf->top_field = 1;
609 vbi_buf->top_field = 0;
617 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
618 if (dev->vbi_read < vbi_field_size) {
619 remain = vbi_field_size - dev->vbi_read;
626 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
631 dev->vbi_read += lencopy;
634 if (dev->vbi_read >= vbi_field_size && buf != NULL)
635 au0828_copy_video(dev, dma_q, buf, p, outp, len);
640 static int queue_setup(struct vb2_queue *vq, const void *parg,
641 unsigned int *nbuffers, unsigned int *nplanes,
642 unsigned int sizes[], void *alloc_ctxs[])
644 const struct v4l2_format *fmt = parg;
645 struct au0828_dev *dev = vb2_get_drv_priv(vq);
646 unsigned long img_size = dev->height * dev->bytesperline;
649 size = fmt ? fmt->fmt.pix.sizeimage : img_size;
660 buffer_prepare(struct vb2_buffer *vb)
662 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
663 struct au0828_buffer *buf = container_of(vbuf,
664 struct au0828_buffer, vb);
665 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
667 buf->length = dev->height * dev->bytesperline;
669 if (vb2_plane_size(vb, 0) < buf->length) {
670 pr_err("%s data will not fit into plane (%lu < %lu)\n",
671 __func__, vb2_plane_size(vb, 0), buf->length);
674 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
679 buffer_queue(struct vb2_buffer *vb)
681 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
682 struct au0828_buffer *buf = container_of(vbuf,
683 struct au0828_buffer,
685 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
686 struct au0828_dmaqueue *vidq = &dev->vidq;
687 unsigned long flags = 0;
689 buf->mem = vb2_plane_vaddr(vb, 0);
690 buf->length = vb2_plane_size(vb, 0);
692 spin_lock_irqsave(&dev->slock, flags);
693 list_add_tail(&buf->list, &vidq->active);
694 spin_unlock_irqrestore(&dev->slock, flags);
697 static int au0828_i2s_init(struct au0828_dev *dev)
699 /* Enable i2s mode */
700 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
705 * Auvitek au0828 analog stream enable
707 static int au0828_analog_stream_enable(struct au0828_dev *d)
709 struct usb_interface *iface;
712 dprintk(1, "au0828_analog_stream_enable called\n");
714 iface = usb_ifnum_to_if(d->usbdev, 0);
715 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
716 dprintk(1, "Changing intf#0 to alt 5\n");
717 /* set au0828 interface0 to AS5 here again */
718 ret = usb_set_interface(d->usbdev, 0, 5);
720 pr_info("Au0828 can't set alt setting to 5!\n");
725 h = d->height / 2 + 2;
728 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
729 au0828_writereg(d, 0x106, 0x00);
731 au0828_writereg(d, 0x110, 0x00);
732 au0828_writereg(d, 0x111, 0x00);
733 au0828_writereg(d, 0x114, w & 0xff);
734 au0828_writereg(d, 0x115, w >> 8);
736 au0828_writereg(d, 0x112, 0x00);
737 au0828_writereg(d, 0x113, 0x00);
738 au0828_writereg(d, 0x116, h & 0xff);
739 au0828_writereg(d, 0x117, h >> 8);
740 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
745 static int au0828_analog_stream_disable(struct au0828_dev *d)
747 dprintk(1, "au0828_analog_stream_disable called\n");
748 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
752 static void au0828_analog_stream_reset(struct au0828_dev *dev)
754 dprintk(1, "au0828_analog_stream_reset called\n");
755 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
757 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
761 * Some operations needs to stop current streaming
763 static int au0828_stream_interrupt(struct au0828_dev *dev)
767 dev->stream_state = STREAM_INTERRUPT;
768 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
771 set_bit(DEV_MISCONFIGURED, &dev->dev_state);
772 dprintk(1, "%s device is misconfigured!\n", __func__);
778 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
780 struct au0828_dev *dev = vb2_get_drv_priv(vq);
783 dprintk(1, "au0828_start_analog_streaming called %d\n",
784 dev->streaming_users);
786 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
787 dev->frame_count = 0;
789 dev->vbi_frame_count = 0;
791 if (dev->streaming_users == 0) {
792 /* If we were doing ac97 instead of i2s, it would go here...*/
793 au0828_i2s_init(dev);
794 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
795 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
798 pr_info("au0828_init_isoc failed\n");
802 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
803 v4l2_device_call_all(&dev->v4l2_dev, 0, video,
805 dev->vid_timeout_running = 1;
806 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
807 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
808 dev->vbi_timeout_running = 1;
809 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
812 dev->streaming_users++;
816 static void au0828_stop_streaming(struct vb2_queue *vq)
818 struct au0828_dev *dev = vb2_get_drv_priv(vq);
819 struct au0828_dmaqueue *vidq = &dev->vidq;
820 unsigned long flags = 0;
822 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
824 if (dev->streaming_users-- == 1)
825 au0828_uninit_isoc(dev);
827 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
828 dev->vid_timeout_running = 0;
829 del_timer_sync(&dev->vid_timeout);
831 spin_lock_irqsave(&dev->slock, flags);
832 if (dev->isoc_ctl.buf != NULL) {
833 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
834 VB2_BUF_STATE_ERROR);
835 dev->isoc_ctl.buf = NULL;
837 while (!list_empty(&vidq->active)) {
838 struct au0828_buffer *buf;
840 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
841 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
842 list_del(&buf->list);
844 spin_unlock_irqrestore(&dev->slock, flags);
847 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
849 struct au0828_dev *dev = vb2_get_drv_priv(vq);
850 struct au0828_dmaqueue *vbiq = &dev->vbiq;
851 unsigned long flags = 0;
853 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
854 dev->streaming_users);
856 if (dev->streaming_users-- == 1)
857 au0828_uninit_isoc(dev);
859 spin_lock_irqsave(&dev->slock, flags);
860 if (dev->isoc_ctl.vbi_buf != NULL) {
861 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
862 VB2_BUF_STATE_ERROR);
863 dev->isoc_ctl.vbi_buf = NULL;
865 while (!list_empty(&vbiq->active)) {
866 struct au0828_buffer *buf;
868 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
869 list_del(&buf->list);
870 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
872 spin_unlock_irqrestore(&dev->slock, flags);
874 dev->vbi_timeout_running = 0;
875 del_timer_sync(&dev->vbi_timeout);
878 static struct vb2_ops au0828_video_qops = {
879 .queue_setup = queue_setup,
880 .buf_prepare = buffer_prepare,
881 .buf_queue = buffer_queue,
882 .start_streaming = au0828_start_analog_streaming,
883 .stop_streaming = au0828_stop_streaming,
884 .wait_prepare = vb2_ops_wait_prepare,
885 .wait_finish = vb2_ops_wait_finish,
888 /* ------------------------------------------------------------------
890 ------------------------------------------------------------------*/
892 * au0828_analog_unregister
893 * unregister v4l2 devices
895 void au0828_analog_unregister(struct au0828_dev *dev)
897 dprintk(1, "au0828_analog_unregister called\n");
898 mutex_lock(&au0828_sysfs_lock);
899 video_unregister_device(&dev->vdev);
900 video_unregister_device(&dev->vbi_dev);
901 mutex_unlock(&au0828_sysfs_lock);
904 /* This function ensures that video frames continue to be delivered even if
905 the ITU-656 input isn't receiving any data (thereby preventing applications
906 such as tvtime from hanging) */
907 static void au0828_vid_buffer_timeout(unsigned long data)
909 struct au0828_dev *dev = (struct au0828_dev *) data;
910 struct au0828_dmaqueue *dma_q = &dev->vidq;
911 struct au0828_buffer *buf;
912 unsigned char *vid_data;
913 unsigned long flags = 0;
915 spin_lock_irqsave(&dev->slock, flags);
917 buf = dev->isoc_ctl.buf;
919 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
920 memset(vid_data, 0x00, buf->length); /* Blank green frame */
921 buffer_filled(dev, dma_q, buf);
923 get_next_buf(dma_q, &buf);
925 if (dev->vid_timeout_running == 1)
926 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
928 spin_unlock_irqrestore(&dev->slock, flags);
931 static void au0828_vbi_buffer_timeout(unsigned long data)
933 struct au0828_dev *dev = (struct au0828_dev *) data;
934 struct au0828_dmaqueue *dma_q = &dev->vbiq;
935 struct au0828_buffer *buf;
936 unsigned char *vbi_data;
937 unsigned long flags = 0;
939 spin_lock_irqsave(&dev->slock, flags);
941 buf = dev->isoc_ctl.vbi_buf;
943 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
944 memset(vbi_data, 0x00, buf->length);
945 buffer_filled(dev, dma_q, buf);
947 vbi_get_next_buf(dma_q, &buf);
949 if (dev->vbi_timeout_running == 1)
950 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
951 spin_unlock_irqrestore(&dev->slock, flags);
954 static int au0828_v4l2_open(struct file *filp)
956 struct au0828_dev *dev = video_drvdata(filp);
960 "%s called std_set %d dev_state %ld stream users %d users %d\n",
961 __func__, dev->std_set_in_tuner_core, dev->dev_state,
962 dev->streaming_users, dev->users);
964 if (mutex_lock_interruptible(&dev->lock))
967 ret = v4l2_fh_open(filp);
969 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
971 mutex_unlock(&dev->lock);
975 if (dev->users == 0) {
976 au0828_analog_stream_enable(dev);
977 au0828_analog_stream_reset(dev);
978 dev->stream_state = STREAM_OFF;
979 set_bit(DEV_INITIALIZED, &dev->dev_state);
982 mutex_unlock(&dev->lock);
986 static int au0828_v4l2_close(struct file *filp)
989 struct au0828_dev *dev = video_drvdata(filp);
990 struct video_device *vdev = video_devdata(filp);
993 "%s called std_set %d dev_state %ld stream users %d users %d\n",
994 __func__, dev->std_set_in_tuner_core, dev->dev_state,
995 dev->streaming_users, dev->users);
997 mutex_lock(&dev->lock);
998 if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
999 /* Cancel timeout thread in case they didn't call streamoff */
1000 dev->vid_timeout_running = 0;
1001 del_timer_sync(&dev->vid_timeout);
1002 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1003 dev->vbi_timeout_running) {
1004 /* Cancel timeout thread in case they didn't call streamoff */
1005 dev->vbi_timeout_running = 0;
1006 del_timer_sync(&dev->vbi_timeout);
1009 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
1012 if (dev->users == 1) {
1013 /* Save some power by putting tuner to sleep */
1014 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1015 dev->std_set_in_tuner_core = 0;
1017 /* When close the device, set the usb intf0 into alt0 to free
1019 ret = usb_set_interface(dev->usbdev, 0, 0);
1021 pr_info("Au0828 can't set alternate to 0!\n");
1024 _vb2_fop_release(filp, NULL);
1026 mutex_unlock(&dev->lock);
1030 /* Must be called with dev->lock held */
1031 static void au0828_init_tuner(struct au0828_dev *dev)
1033 struct v4l2_frequency f = {
1034 .frequency = dev->ctrl_freq,
1035 .type = V4L2_TUNER_ANALOG_TV,
1038 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1039 dev->std_set_in_tuner_core, dev->dev_state);
1041 if (dev->std_set_in_tuner_core)
1043 dev->std_set_in_tuner_core = 1;
1044 i2c_gate_ctrl(dev, 1);
1045 /* If we've never sent the standard in tuner core, do so now.
1046 We don't do this at device probe because we don't want to
1047 incur the cost of a firmware load */
1048 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1049 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1050 i2c_gate_ctrl(dev, 0);
1053 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1054 struct v4l2_format *format)
1057 int width = format->fmt.pix.width;
1058 int height = format->fmt.pix.height;
1060 /* If they are demanding a format other than the one we support,
1061 bail out (tvtime asks for UYVY and then retries with YUYV) */
1062 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1065 /* format->fmt.pix.width only support 720 and height 480 */
1071 format->fmt.pix.width = width;
1072 format->fmt.pix.height = height;
1073 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1074 format->fmt.pix.bytesperline = width * 2;
1075 format->fmt.pix.sizeimage = width * height * 2;
1076 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1077 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1078 format->fmt.pix.priv = 0;
1080 if (cmd == VIDIOC_TRY_FMT)
1083 /* maybe set new image format, driver current only support 720*480 */
1085 dev->height = height;
1086 dev->frame_size = width * height * 2;
1087 dev->field_size = width * height;
1088 dev->bytesperline = width * 2;
1090 if (dev->stream_state == STREAM_ON) {
1091 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1092 ret = au0828_stream_interrupt(dev);
1094 dprintk(1, "error interrupting video stream!\n");
1099 au0828_analog_stream_enable(dev);
1104 static int vidioc_querycap(struct file *file, void *priv,
1105 struct v4l2_capability *cap)
1107 struct video_device *vdev = video_devdata(file);
1108 struct au0828_dev *dev = video_drvdata(file);
1110 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1111 dev->std_set_in_tuner_core, dev->dev_state);
1113 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1114 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1115 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1117 /* set the device capabilities */
1118 cap->device_caps = V4L2_CAP_AUDIO |
1119 V4L2_CAP_READWRITE |
1120 V4L2_CAP_STREAMING |
1122 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1123 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1125 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1126 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1127 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
1131 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1132 struct v4l2_fmtdesc *f)
1137 dprintk(1, "%s called\n", __func__);
1139 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1140 strcpy(f->description, "Packed YUV2");
1143 f->pixelformat = V4L2_PIX_FMT_UYVY;
1148 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1149 struct v4l2_format *f)
1151 struct au0828_dev *dev = video_drvdata(file);
1153 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1154 dev->std_set_in_tuner_core, dev->dev_state);
1156 f->fmt.pix.width = dev->width;
1157 f->fmt.pix.height = dev->height;
1158 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1159 f->fmt.pix.bytesperline = dev->bytesperline;
1160 f->fmt.pix.sizeimage = dev->frame_size;
1161 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1162 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1163 f->fmt.pix.priv = 0;
1167 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1168 struct v4l2_format *f)
1170 struct au0828_dev *dev = video_drvdata(file);
1172 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1173 dev->std_set_in_tuner_core, dev->dev_state);
1175 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1178 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1179 struct v4l2_format *f)
1181 struct au0828_dev *dev = video_drvdata(file);
1184 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1185 dev->std_set_in_tuner_core, dev->dev_state);
1187 rc = check_dev(dev);
1191 if (vb2_is_busy(&dev->vb_vidq)) {
1192 pr_info("%s queue busy\n", __func__);
1197 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1202 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1204 struct au0828_dev *dev = video_drvdata(file);
1206 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1207 dev->std_set_in_tuner_core, dev->dev_state);
1209 if (norm == dev->std)
1212 if (dev->streaming_users > 0)
1217 au0828_init_tuner(dev);
1219 i2c_gate_ctrl(dev, 1);
1222 * FIXME: when we support something other than 60Hz standards,
1223 * we are going to have to make the au0828 bridge adjust the size
1224 * of its capture buffer, which is currently hardcoded at 720x480
1227 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1229 i2c_gate_ctrl(dev, 0);
1234 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1236 struct au0828_dev *dev = video_drvdata(file);
1238 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1239 dev->std_set_in_tuner_core, dev->dev_state);
1245 static int vidioc_enum_input(struct file *file, void *priv,
1246 struct v4l2_input *input)
1248 struct au0828_dev *dev = video_drvdata(file);
1251 static const char *inames[] = {
1252 [AU0828_VMUX_UNDEFINED] = "Undefined",
1253 [AU0828_VMUX_COMPOSITE] = "Composite",
1254 [AU0828_VMUX_SVIDEO] = "S-Video",
1255 [AU0828_VMUX_CABLE] = "Cable TV",
1256 [AU0828_VMUX_TELEVISION] = "Television",
1257 [AU0828_VMUX_DVB] = "DVB",
1258 [AU0828_VMUX_DEBUG] = "tv debug"
1261 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1262 dev->std_set_in_tuner_core, dev->dev_state);
1266 if (tmp >= AU0828_MAX_INPUT)
1268 if (AUVI_INPUT(tmp).type == 0)
1272 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1273 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1274 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1275 input->type |= V4L2_INPUT_TYPE_TUNER;
1276 input->audioset = 1;
1278 input->type |= V4L2_INPUT_TYPE_CAMERA;
1279 input->audioset = 2;
1282 input->std = dev->vdev.tvnorms;
1287 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1289 struct au0828_dev *dev = video_drvdata(file);
1291 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1292 dev->std_set_in_tuner_core, dev->dev_state);
1294 *i = dev->ctrl_input;
1298 static void au0828_s_input(struct au0828_dev *dev, int index)
1302 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1303 dev->std_set_in_tuner_core, dev->dev_state);
1305 switch (AUVI_INPUT(index).type) {
1306 case AU0828_VMUX_SVIDEO:
1307 dev->input_type = AU0828_VMUX_SVIDEO;
1308 dev->ctrl_ainput = 1;
1310 case AU0828_VMUX_COMPOSITE:
1311 dev->input_type = AU0828_VMUX_COMPOSITE;
1312 dev->ctrl_ainput = 1;
1314 case AU0828_VMUX_TELEVISION:
1315 dev->input_type = AU0828_VMUX_TELEVISION;
1316 dev->ctrl_ainput = 0;
1319 dprintk(1, "unknown input type set [%d]\n",
1320 AUVI_INPUT(index).type);
1324 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1325 AUVI_INPUT(index).vmux, 0, 0);
1327 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1329 if (AUVI_INPUT(i).audio_setup == NULL)
1337 (AUVI_INPUT(i).audio_setup)(dev, enable);
1339 /* Make sure we leave it turned on if some
1340 other input is routed to this callback */
1341 if ((AUVI_INPUT(i).audio_setup) !=
1342 ((AUVI_INPUT(index).audio_setup))) {
1343 (AUVI_INPUT(i).audio_setup)(dev, enable);
1348 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1349 AUVI_INPUT(index).amux, 0, 0);
1352 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1354 struct au0828_dev *dev = video_drvdata(file);
1356 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1358 if (index >= AU0828_MAX_INPUT)
1360 if (AUVI_INPUT(index).type == 0)
1362 dev->ctrl_input = index;
1363 au0828_s_input(dev, index);
1367 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1372 dprintk(1, "%s called\n", __func__);
1375 strcpy(a->name, "Television");
1377 strcpy(a->name, "Line in");
1379 a->capability = V4L2_AUDCAP_STEREO;
1383 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1385 struct au0828_dev *dev = video_drvdata(file);
1387 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1388 dev->std_set_in_tuner_core, dev->dev_state);
1390 a->index = dev->ctrl_ainput;
1392 strcpy(a->name, "Television");
1394 strcpy(a->name, "Line in");
1396 a->capability = V4L2_AUDCAP_STEREO;
1400 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1402 struct au0828_dev *dev = video_drvdata(file);
1404 if (a->index != dev->ctrl_ainput)
1407 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1408 dev->std_set_in_tuner_core, dev->dev_state);
1412 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1414 struct au0828_dev *dev = video_drvdata(file);
1419 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1420 dev->std_set_in_tuner_core, dev->dev_state);
1422 strcpy(t->name, "Auvitek tuner");
1424 au0828_init_tuner(dev);
1425 i2c_gate_ctrl(dev, 1);
1426 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1427 i2c_gate_ctrl(dev, 0);
1431 static int vidioc_s_tuner(struct file *file, void *priv,
1432 const struct v4l2_tuner *t)
1434 struct au0828_dev *dev = video_drvdata(file);
1439 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1440 dev->std_set_in_tuner_core, dev->dev_state);
1442 au0828_init_tuner(dev);
1443 i2c_gate_ctrl(dev, 1);
1444 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1445 i2c_gate_ctrl(dev, 0);
1447 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1454 static int vidioc_g_frequency(struct file *file, void *priv,
1455 struct v4l2_frequency *freq)
1457 struct au0828_dev *dev = video_drvdata(file);
1459 if (freq->tuner != 0)
1461 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1462 dev->std_set_in_tuner_core, dev->dev_state);
1463 freq->frequency = dev->ctrl_freq;
1467 static int vidioc_s_frequency(struct file *file, void *priv,
1468 const struct v4l2_frequency *freq)
1470 struct au0828_dev *dev = video_drvdata(file);
1471 struct v4l2_frequency new_freq = *freq;
1473 if (freq->tuner != 0)
1476 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1477 dev->std_set_in_tuner_core, dev->dev_state);
1479 au0828_init_tuner(dev);
1480 i2c_gate_ctrl(dev, 1);
1482 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1483 /* Get the actual set (and possibly clamped) frequency */
1484 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1485 dev->ctrl_freq = new_freq.frequency;
1487 i2c_gate_ctrl(dev, 0);
1489 au0828_analog_stream_reset(dev);
1495 /* RAW VBI ioctls */
1497 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1498 struct v4l2_format *format)
1500 struct au0828_dev *dev = video_drvdata(file);
1502 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1503 dev->std_set_in_tuner_core, dev->dev_state);
1505 format->fmt.vbi.samples_per_line = dev->vbi_width;
1506 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1507 format->fmt.vbi.offset = 0;
1508 format->fmt.vbi.flags = 0;
1509 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1511 format->fmt.vbi.count[0] = dev->vbi_height;
1512 format->fmt.vbi.count[1] = dev->vbi_height;
1513 format->fmt.vbi.start[0] = 21;
1514 format->fmt.vbi.start[1] = 284;
1515 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1520 static int vidioc_cropcap(struct file *file, void *priv,
1521 struct v4l2_cropcap *cc)
1523 struct au0828_dev *dev = video_drvdata(file);
1525 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1528 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1529 dev->std_set_in_tuner_core, dev->dev_state);
1531 cc->bounds.left = 0;
1533 cc->bounds.width = dev->width;
1534 cc->bounds.height = dev->height;
1536 cc->defrect = cc->bounds;
1538 cc->pixelaspect.numerator = 54;
1539 cc->pixelaspect.denominator = 59;
1544 #ifdef CONFIG_VIDEO_ADV_DEBUG
1545 static int vidioc_g_register(struct file *file, void *priv,
1546 struct v4l2_dbg_register *reg)
1548 struct au0828_dev *dev = video_drvdata(file);
1550 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1551 dev->std_set_in_tuner_core, dev->dev_state);
1553 reg->val = au0828_read(dev, reg->reg);
1558 static int vidioc_s_register(struct file *file, void *priv,
1559 const struct v4l2_dbg_register *reg)
1561 struct au0828_dev *dev = video_drvdata(file);
1563 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1564 dev->std_set_in_tuner_core, dev->dev_state);
1566 return au0828_writereg(dev, reg->reg, reg->val);
1570 static int vidioc_log_status(struct file *file, void *fh)
1572 struct video_device *vdev = video_devdata(file);
1574 dprintk(1, "%s called\n", __func__);
1576 v4l2_ctrl_log_status(file, fh);
1577 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1581 void au0828_v4l2_suspend(struct au0828_dev *dev)
1586 pr_info("stopping V4L2\n");
1588 if (dev->stream_state == STREAM_ON) {
1589 pr_info("stopping V4L2 active URBs\n");
1590 au0828_analog_stream_disable(dev);
1592 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1593 urb = dev->isoc_ctl.urb[i];
1595 if (!irqs_disabled())
1598 usb_unlink_urb(urb);
1603 if (dev->vid_timeout_running)
1604 del_timer_sync(&dev->vid_timeout);
1605 if (dev->vbi_timeout_running)
1606 del_timer_sync(&dev->vbi_timeout);
1609 void au0828_v4l2_resume(struct au0828_dev *dev)
1613 pr_info("restarting V4L2\n");
1615 if (dev->stream_state == STREAM_ON) {
1616 au0828_stream_interrupt(dev);
1617 au0828_init_tuner(dev);
1620 if (dev->vid_timeout_running)
1621 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1622 if (dev->vbi_timeout_running)
1623 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1625 /* If we were doing ac97 instead of i2s, it would go here...*/
1626 au0828_i2s_init(dev);
1628 au0828_analog_stream_enable(dev);
1630 if (!(dev->stream_state == STREAM_ON)) {
1631 au0828_analog_stream_reset(dev);
1633 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1634 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1636 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1638 au0828_uninit_isoc(dev);
1644 static struct v4l2_file_operations au0828_v4l_fops = {
1645 .owner = THIS_MODULE,
1646 .open = au0828_v4l2_open,
1647 .release = au0828_v4l2_close,
1648 .read = vb2_fop_read,
1649 .poll = vb2_fop_poll,
1650 .mmap = vb2_fop_mmap,
1651 .unlocked_ioctl = video_ioctl2,
1654 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1655 .vidioc_querycap = vidioc_querycap,
1656 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1657 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1658 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1659 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1660 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1661 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1662 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1663 .vidioc_enumaudio = vidioc_enumaudio,
1664 .vidioc_g_audio = vidioc_g_audio,
1665 .vidioc_s_audio = vidioc_s_audio,
1666 .vidioc_cropcap = vidioc_cropcap,
1668 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1669 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1670 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1671 .vidioc_querybuf = vb2_ioctl_querybuf,
1672 .vidioc_qbuf = vb2_ioctl_qbuf,
1673 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1674 .vidioc_expbuf = vb2_ioctl_expbuf,
1676 .vidioc_s_std = vidioc_s_std,
1677 .vidioc_g_std = vidioc_g_std,
1678 .vidioc_enum_input = vidioc_enum_input,
1679 .vidioc_g_input = vidioc_g_input,
1680 .vidioc_s_input = vidioc_s_input,
1682 .vidioc_streamon = vb2_ioctl_streamon,
1683 .vidioc_streamoff = vb2_ioctl_streamoff,
1685 .vidioc_g_tuner = vidioc_g_tuner,
1686 .vidioc_s_tuner = vidioc_s_tuner,
1687 .vidioc_g_frequency = vidioc_g_frequency,
1688 .vidioc_s_frequency = vidioc_s_frequency,
1689 #ifdef CONFIG_VIDEO_ADV_DEBUG
1690 .vidioc_g_register = vidioc_g_register,
1691 .vidioc_s_register = vidioc_s_register,
1693 .vidioc_log_status = vidioc_log_status,
1694 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1695 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1698 static const struct video_device au0828_video_template = {
1699 .fops = &au0828_v4l_fops,
1700 .release = video_device_release_empty,
1701 .ioctl_ops = &video_ioctl_ops,
1702 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1705 static int au0828_vb2_setup(struct au0828_dev *dev)
1708 struct vb2_queue *q;
1710 /* Setup Videobuf2 for Video capture */
1712 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1713 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1714 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1716 q->buf_struct_size = sizeof(struct au0828_buffer);
1717 q->ops = &au0828_video_qops;
1718 q->mem_ops = &vb2_vmalloc_memops;
1720 rc = vb2_queue_init(q);
1724 /* Setup Videobuf2 for VBI capture */
1726 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1727 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1728 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1730 q->buf_struct_size = sizeof(struct au0828_buffer);
1731 q->ops = &au0828_vbi_qops;
1732 q->mem_ops = &vb2_vmalloc_memops;
1734 rc = vb2_queue_init(q);
1741 /**************************************************************************/
1743 int au0828_analog_register(struct au0828_dev *dev,
1744 struct usb_interface *interface)
1746 int retval = -ENOMEM;
1747 struct usb_host_interface *iface_desc;
1748 struct usb_endpoint_descriptor *endpoint;
1751 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1752 interface->cur_altsetting->desc.bInterfaceNumber);
1754 /* set au0828 usb interface0 to as5 */
1755 retval = usb_set_interface(dev->usbdev,
1756 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1758 pr_info("Failure setting usb interface0 to as5\n");
1762 /* Figure out which endpoint has the isoc interface */
1763 iface_desc = interface->cur_altsetting;
1764 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1765 endpoint = &iface_desc->endpoint[i].desc;
1766 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1768 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1769 == USB_ENDPOINT_XFER_ISOC)) {
1771 /* we find our isoc in endpoint */
1772 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1773 dev->max_pkt_size = (tmp & 0x07ff) *
1774 (((tmp & 0x1800) >> 11) + 1);
1775 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1777 "Found isoc endpoint 0x%02x, max size = %d\n",
1778 dev->isoc_in_endpointaddr, dev->max_pkt_size);
1781 if (!(dev->isoc_in_endpointaddr)) {
1782 pr_info("Could not locate isoc endpoint\n");
1786 init_waitqueue_head(&dev->open);
1787 spin_lock_init(&dev->slock);
1789 /* init video dma queues */
1790 INIT_LIST_HEAD(&dev->vidq.active);
1791 INIT_LIST_HEAD(&dev->vbiq.active);
1793 setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
1794 (unsigned long)dev);
1795 setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
1796 (unsigned long)dev);
1798 dev->width = NTSC_STD_W;
1799 dev->height = NTSC_STD_H;
1800 dev->field_size = dev->width * dev->height;
1801 dev->frame_size = dev->field_size << 1;
1802 dev->bytesperline = dev->width << 1;
1803 dev->vbi_width = 720;
1804 dev->vbi_height = 1;
1805 dev->ctrl_ainput = 0;
1806 dev->ctrl_freq = 960;
1807 dev->std = V4L2_STD_NTSC_M;
1808 au0828_s_input(dev, 0);
1810 mutex_init(&dev->vb_queue_lock);
1811 mutex_init(&dev->vb_vbi_queue_lock);
1813 /* Fill the video capture device struct */
1814 dev->vdev = au0828_video_template;
1815 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1816 dev->vdev.lock = &dev->lock;
1817 dev->vdev.queue = &dev->vb_vidq;
1818 dev->vdev.queue->lock = &dev->vb_queue_lock;
1819 strcpy(dev->vdev.name, "au0828a video");
1821 /* Setup the VBI device */
1822 dev->vbi_dev = au0828_video_template;
1823 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1824 dev->vbi_dev.lock = &dev->lock;
1825 dev->vbi_dev.queue = &dev->vb_vbiq;
1826 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1827 strcpy(dev->vbi_dev.name, "au0828a vbi");
1829 /* initialize videobuf2 stuff */
1830 retval = au0828_vb2_setup(dev);
1832 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
1837 /* Register the v4l2 device */
1838 video_set_drvdata(&dev->vdev, dev);
1839 retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
1841 dprintk(1, "unable to register video device (error = %d).\n",
1847 /* Register the vbi device */
1848 video_set_drvdata(&dev->vbi_dev, dev);
1849 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
1851 dprintk(1, "unable to register vbi device (error = %d).\n",
1854 goto err_reg_vbi_dev;
1857 dprintk(1, "%s completed!\n", __func__);
1862 video_unregister_device(&dev->vdev);
1864 vb2_queue_release(&dev->vb_vidq);
1865 vb2_queue_release(&dev->vb_vbiq);