1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Auvitek AU0828 USB Bridge (Analog video support)
5 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
6 * Copyright (C) 2005-2008 Auvitek International, Ltd.
11 * The hardware scaler supported is unimplemented
12 * AC97 audio support is unimplemented (only i2s audio mode)
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/device.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-mc.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-event.h>
27 #include <media/tuner.h>
28 #include "au0828-reg.h"
30 static DEFINE_MUTEX(au0828_sysfs_lock);
32 /* ------------------------------------------------------------------
34 ------------------------------------------------------------------*/
36 static unsigned int isoc_debug;
37 module_param(isoc_debug, int, 0644);
38 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
40 #define au0828_isocdbg(fmt, arg...) \
43 pr_info("au0828 %s :"fmt, \
48 static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
50 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
51 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
54 static inline void print_err_status(struct au0828_dev *dev,
55 int packet, int status)
57 char *errmsg = "Unknown";
61 errmsg = "unlinked synchronously";
64 errmsg = "unlinked asynchronously";
67 errmsg = "Buffer error (overrun)";
70 errmsg = "Stalled (device not responding)";
73 errmsg = "Babble (bad cable?)";
76 errmsg = "Bit-stuff error (bad cable?)";
79 errmsg = "CRC/Timeout (could be anything)";
82 errmsg = "Device does not respond";
86 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
88 au0828_isocdbg("URB packet %d, status %d [%s].\n",
89 packet, status, errmsg);
93 static int check_dev(struct au0828_dev *dev)
95 if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) {
96 pr_info("v4l2 ioctl: device not present\n");
100 if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) {
101 pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n");
108 * IRQ callback, called by URB callback
110 static void au0828_irq_callback(struct urb *urb)
112 struct au0828_dmaqueue *dma_q = urb->context;
113 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
114 unsigned long flags = 0;
117 switch (urb->status) {
118 case 0: /* success */
119 case -ETIMEDOUT: /* NAK */
121 case -ECONNRESET: /* kill */
124 au0828_isocdbg("au0828_irq_callback called: status kill\n");
126 default: /* unknown error */
127 au0828_isocdbg("urb completion error %d.\n", urb->status);
131 /* Copy data from URB */
132 spin_lock_irqsave(&dev->slock, flags);
133 dev->isoc_ctl.isoc_copy(dev, urb);
134 spin_unlock_irqrestore(&dev->slock, flags);
136 /* Reset urb buffers */
137 for (i = 0; i < urb->number_of_packets; i++) {
138 urb->iso_frame_desc[i].status = 0;
139 urb->iso_frame_desc[i].actual_length = 0;
143 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
145 au0828_isocdbg("urb resubmit failed (error=%i)\n",
148 dev->stream_state = STREAM_ON;
152 * Stop and Deallocate URBs
154 static void au0828_uninit_isoc(struct au0828_dev *dev)
159 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
161 dev->isoc_ctl.nfields = -1;
162 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
163 urb = dev->isoc_ctl.urb[i];
165 if (!irqs_disabled())
170 if (dev->isoc_ctl.transfer_buffer[i]) {
171 usb_free_coherent(dev->usbdev,
172 urb->transfer_buffer_length,
173 dev->isoc_ctl.transfer_buffer[i],
177 dev->isoc_ctl.urb[i] = NULL;
179 dev->isoc_ctl.transfer_buffer[i] = NULL;
182 kfree(dev->isoc_ctl.urb);
183 kfree(dev->isoc_ctl.transfer_buffer);
185 dev->isoc_ctl.urb = NULL;
186 dev->isoc_ctl.transfer_buffer = NULL;
187 dev->isoc_ctl.num_bufs = 0;
189 dev->stream_state = STREAM_OFF;
193 * Allocate URBs and start IRQ
195 static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
196 int num_bufs, int max_pkt_size,
197 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
199 struct au0828_dmaqueue *dma_q = &dev->vidq;
206 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
208 dev->isoc_ctl.isoc_copy = isoc_copy;
209 dev->isoc_ctl.num_bufs = num_bufs;
211 dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
212 if (!dev->isoc_ctl.urb) {
213 au0828_isocdbg("cannot alloc memory for usb buffers\n");
217 dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs, sizeof(void *),
219 if (!dev->isoc_ctl.transfer_buffer) {
220 au0828_isocdbg("cannot allocate memory for usb transfer\n");
221 kfree(dev->isoc_ctl.urb);
225 dev->isoc_ctl.max_pkt_size = max_pkt_size;
226 dev->isoc_ctl.buf = NULL;
228 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
230 /* allocate urbs and transfer buffers */
231 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
232 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
234 au0828_uninit_isoc(dev);
237 dev->isoc_ctl.urb[i] = urb;
239 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
240 sb_size, GFP_KERNEL, &urb->transfer_dma);
241 if (!dev->isoc_ctl.transfer_buffer[i]) {
242 printk("unable to allocate %i bytes for transfer buffer %i%s\n",
244 in_interrupt() ? " while in int" : "");
245 au0828_uninit_isoc(dev);
248 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
250 pipe = usb_rcvisocpipe(dev->usbdev,
251 dev->isoc_in_endpointaddr),
253 usb_fill_int_urb(urb, dev->usbdev, pipe,
254 dev->isoc_ctl.transfer_buffer[i], sb_size,
255 au0828_irq_callback, dma_q, 1);
257 urb->number_of_packets = max_packets;
258 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
261 for (j = 0; j < max_packets; j++) {
262 urb->iso_frame_desc[j].offset = k;
263 urb->iso_frame_desc[j].length =
264 dev->isoc_ctl.max_pkt_size;
265 k += dev->isoc_ctl.max_pkt_size;
269 /* submit urbs and enables IRQ */
270 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
271 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
273 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
275 au0828_uninit_isoc(dev);
284 * Announces that a buffer were filled and request the next
286 static inline void buffer_filled(struct au0828_dev *dev,
287 struct au0828_dmaqueue *dma_q,
288 struct au0828_buffer *buf)
290 struct vb2_v4l2_buffer *vb = &buf->vb;
291 struct vb2_queue *q = vb->vb2_buf.vb2_queue;
293 /* Advice that buffer was filled */
294 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
296 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
297 vb->sequence = dev->frame_count++;
299 vb->sequence = dev->vbi_frame_count++;
301 vb->field = V4L2_FIELD_INTERLACED;
302 vb->vb2_buf.timestamp = ktime_get_ns();
303 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
307 * Identify the buffer header type and properly handles
309 static void au0828_copy_video(struct au0828_dev *dev,
310 struct au0828_dmaqueue *dma_q,
311 struct au0828_buffer *buf,
313 unsigned char *outp, unsigned long len)
315 void *fieldstart, *startwrite, *startread;
316 int linesdone, currlinedone, offset, lencopy, remain;
317 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
322 if (dma_q->pos + len > buf->length)
323 len = buf->length - dma_q->pos;
328 /* Interlaces frame */
332 fieldstart = outp + bytesperline;
334 linesdone = dma_q->pos / bytesperline;
335 currlinedone = dma_q->pos % bytesperline;
336 offset = linesdone * bytesperline * 2 + currlinedone;
337 startwrite = fieldstart + offset;
338 lencopy = bytesperline - currlinedone;
339 lencopy = lencopy > remain ? remain : lencopy;
341 if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
342 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
343 ((char *)startwrite + lencopy) -
344 ((char *)outp + buf->length));
345 remain = (char *)outp + buf->length - (char *)startwrite;
350 memcpy(startwrite, startread, lencopy);
355 startwrite += lencopy + bytesperline;
356 startread += lencopy;
357 if (bytesperline > remain)
360 lencopy = bytesperline;
362 if ((char *)startwrite + lencopy > (char *)outp +
364 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
365 ((char *)startwrite + lencopy) -
366 ((char *)outp + buf->length));
367 lencopy = remain = (char *)outp + buf->length -
373 memcpy(startwrite, startread, lencopy);
379 /* We have enough data to check for greenscreen */
380 if (outp[0] < 0x60 && outp[1440] < 0x60)
381 dev->greenscreen_detected = 1;
388 * video-buf generic routine to get the next available buffer
390 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
391 struct au0828_buffer **buf)
393 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
395 if (list_empty(&dma_q->active)) {
396 au0828_isocdbg("No active queue to serve\n");
397 dev->isoc_ctl.buf = NULL;
402 /* Get the next buffer */
403 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
404 /* Cleans up buffer - Useful for testing for frame/URB loss */
405 list_del(&(*buf)->list);
407 (*buf)->vb_buf = (*buf)->mem;
408 dev->isoc_ctl.buf = *buf;
413 static void au0828_copy_vbi(struct au0828_dev *dev,
414 struct au0828_dmaqueue *dma_q,
415 struct au0828_buffer *buf,
417 unsigned char *outp, unsigned long len)
419 unsigned char *startwrite, *startread;
424 au0828_isocdbg("dev is null\n");
429 au0828_isocdbg("dma_q is null\n");
435 au0828_isocdbg("p is null\n");
439 au0828_isocdbg("outp is null\n");
443 bytesperline = dev->vbi_width;
445 if (dma_q->pos + len > buf->length)
446 len = buf->length - dma_q->pos;
449 startwrite = outp + (dma_q->pos / 2);
451 /* Make sure the bottom field populates the second half of the frame */
452 if (buf->top_field == 0)
453 startwrite += bytesperline * dev->vbi_height;
455 for (i = 0; i < len; i += 2)
456 startwrite[j++] = startread[i+1];
463 * video-buf generic routine to get the next available VBI buffer
465 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
466 struct au0828_buffer **buf)
468 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
470 if (list_empty(&dma_q->active)) {
471 au0828_isocdbg("No active queue to serve\n");
472 dev->isoc_ctl.vbi_buf = NULL;
477 /* Get the next buffer */
478 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
479 /* Cleans up buffer - Useful for testing for frame/URB loss */
480 list_del(&(*buf)->list);
482 (*buf)->vb_buf = (*buf)->mem;
483 dev->isoc_ctl.vbi_buf = *buf;
488 * Controls the isoc copy of each urb packet
490 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
492 struct au0828_buffer *buf;
493 struct au0828_buffer *vbi_buf;
494 struct au0828_dmaqueue *dma_q = urb->context;
495 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
496 unsigned char *outp = NULL;
497 unsigned char *vbioutp = NULL;
498 int i, len = 0, rc = 1;
501 unsigned int vbi_field_size;
502 unsigned int remain, lencopy;
507 if (test_bit(DEV_DISCONNECTED, &dev->dev_state) ||
508 test_bit(DEV_MISCONFIGURED, &dev->dev_state))
511 if (urb->status < 0) {
512 print_err_status(dev, -1, urb->status);
513 if (urb->status == -ENOENT)
517 buf = dev->isoc_ctl.buf;
519 outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
521 vbi_buf = dev->isoc_ctl.vbi_buf;
523 vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
525 for (i = 0; i < urb->number_of_packets; i++) {
526 int status = urb->iso_frame_desc[i].status;
529 print_err_status(dev, i, status);
530 if (urb->iso_frame_desc[i].status != -EPROTO)
534 if (urb->iso_frame_desc[i].actual_length <= 0)
537 if (urb->iso_frame_desc[i].actual_length >
539 au0828_isocdbg("packet bigger than packet size");
543 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
545 len = urb->iso_frame_desc[i].actual_length - 4;
551 au0828_isocdbg("Video frame %s\n",
552 (fbyte & 0x40) ? "odd" : "even");
556 buffer_filled(dev, vbi_dma_q, vbi_buf);
557 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
561 vbioutp = vb2_plane_vaddr(
562 &vbi_buf->vb.vb2_buf, 0);
566 buffer_filled(dev, dma_q, buf);
567 get_next_buf(dma_q, &buf);
571 outp = vb2_plane_vaddr(
572 &buf->vb.vb2_buf, 0);
574 /* As long as isoc traffic is arriving, keep
575 resetting the timer */
576 if (dev->vid_timeout_running)
577 mod_timer(&dev->vid_timeout,
578 jiffies + (HZ / 10));
579 if (dev->vbi_timeout_running)
580 mod_timer(&dev->vbi_timeout,
581 jiffies + (HZ / 10));
591 if (vbi_buf != NULL) {
593 vbi_buf->top_field = 1;
595 vbi_buf->top_field = 0;
603 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
604 if (dev->vbi_read < vbi_field_size) {
605 remain = vbi_field_size - dev->vbi_read;
612 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
617 dev->vbi_read += lencopy;
620 if (dev->vbi_read >= vbi_field_size && buf != NULL)
621 au0828_copy_video(dev, dma_q, buf, p, outp, len);
626 void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
628 #ifdef CONFIG_MEDIA_CONTROLLER
631 for (i = 0; i < AU0828_MAX_INPUT; i++) {
632 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
634 media_device_unregister_entity(&dev->input_ent[i]);
639 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
641 struct au0828_dev *dev =
642 container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
644 v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
645 v4l2_device_unregister(&dev->v4l2_dev);
646 au0828_usb_v4l2_media_release(dev);
647 au0828_usb_release(dev);
650 int au0828_v4l2_device_register(struct usb_interface *interface,
651 struct au0828_dev *dev)
655 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
658 /* Create the v4l2_device */
659 #ifdef CONFIG_MEDIA_CONTROLLER
660 dev->v4l2_dev.mdev = dev->media_dev;
662 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
664 pr_err("%s() v4l2_device_register failed\n",
669 dev->v4l2_dev.release = au0828_usb_v4l2_release;
671 /* This control handler will inherit the controls from au8522 */
672 retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
674 pr_err("%s() v4l2_ctrl_handler_init failed\n",
678 dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
683 static int queue_setup(struct vb2_queue *vq,
684 unsigned int *nbuffers, unsigned int *nplanes,
685 unsigned int sizes[], struct device *alloc_devs[])
687 struct au0828_dev *dev = vb2_get_drv_priv(vq);
688 unsigned long size = dev->height * dev->bytesperline;
691 return sizes[0] < size ? -EINVAL : 0;
698 buffer_prepare(struct vb2_buffer *vb)
700 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
701 struct au0828_buffer *buf = container_of(vbuf,
702 struct au0828_buffer, vb);
703 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
705 buf->length = dev->height * dev->bytesperline;
707 if (vb2_plane_size(vb, 0) < buf->length) {
708 pr_err("%s data will not fit into plane (%lu < %lu)\n",
709 __func__, vb2_plane_size(vb, 0), buf->length);
712 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
717 buffer_queue(struct vb2_buffer *vb)
719 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
720 struct au0828_buffer *buf = container_of(vbuf,
721 struct au0828_buffer,
723 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
724 struct au0828_dmaqueue *vidq = &dev->vidq;
725 unsigned long flags = 0;
727 buf->mem = vb2_plane_vaddr(vb, 0);
728 buf->length = vb2_plane_size(vb, 0);
730 spin_lock_irqsave(&dev->slock, flags);
731 list_add_tail(&buf->list, &vidq->active);
732 spin_unlock_irqrestore(&dev->slock, flags);
735 static int au0828_i2s_init(struct au0828_dev *dev)
737 /* Enable i2s mode */
738 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
743 * Auvitek au0828 analog stream enable
745 static int au0828_analog_stream_enable(struct au0828_dev *d)
747 struct usb_interface *iface;
750 dprintk(1, "au0828_analog_stream_enable called\n");
752 if (test_bit(DEV_DISCONNECTED, &d->dev_state))
755 iface = usb_ifnum_to_if(d->usbdev, 0);
756 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
757 dprintk(1, "Changing intf#0 to alt 5\n");
758 /* set au0828 interface0 to AS5 here again */
759 ret = usb_set_interface(d->usbdev, 0, 5);
761 pr_info("Au0828 can't set alt setting to 5!\n");
766 h = d->height / 2 + 2;
769 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
770 au0828_writereg(d, 0x106, 0x00);
772 au0828_writereg(d, 0x110, 0x00);
773 au0828_writereg(d, 0x111, 0x00);
774 au0828_writereg(d, 0x114, w & 0xff);
775 au0828_writereg(d, 0x115, w >> 8);
777 au0828_writereg(d, 0x112, 0x00);
778 au0828_writereg(d, 0x113, 0x00);
779 au0828_writereg(d, 0x116, h & 0xff);
780 au0828_writereg(d, 0x117, h >> 8);
781 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
786 static int au0828_analog_stream_disable(struct au0828_dev *d)
788 dprintk(1, "au0828_analog_stream_disable called\n");
789 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
793 static void au0828_analog_stream_reset(struct au0828_dev *dev)
795 dprintk(1, "au0828_analog_stream_reset called\n");
796 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
798 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
802 * Some operations needs to stop current streaming
804 static int au0828_stream_interrupt(struct au0828_dev *dev)
806 dev->stream_state = STREAM_INTERRUPT;
807 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
812 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
814 struct au0828_dev *dev = vb2_get_drv_priv(vq);
817 dprintk(1, "au0828_start_analog_streaming called %d\n",
818 dev->streaming_users);
820 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
821 dev->frame_count = 0;
823 dev->vbi_frame_count = 0;
825 if (dev->streaming_users == 0) {
826 /* If we were doing ac97 instead of i2s, it would go here...*/
827 au0828_i2s_init(dev);
828 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
829 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
832 pr_info("au0828_init_isoc failed\n");
836 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
838 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
839 dev->vid_timeout_running = 1;
840 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
841 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
842 dev->vbi_timeout_running = 1;
843 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
846 dev->streaming_users++;
850 static void au0828_stop_streaming(struct vb2_queue *vq)
852 struct au0828_dev *dev = vb2_get_drv_priv(vq);
853 struct au0828_dmaqueue *vidq = &dev->vidq;
854 unsigned long flags = 0;
856 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
858 if (dev->streaming_users-- == 1) {
859 au0828_uninit_isoc(dev);
860 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
863 dev->vid_timeout_running = 0;
864 del_timer_sync(&dev->vid_timeout);
866 spin_lock_irqsave(&dev->slock, flags);
867 if (dev->isoc_ctl.buf != NULL) {
868 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
869 VB2_BUF_STATE_ERROR);
870 dev->isoc_ctl.buf = NULL;
872 while (!list_empty(&vidq->active)) {
873 struct au0828_buffer *buf;
875 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
876 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
877 list_del(&buf->list);
879 spin_unlock_irqrestore(&dev->slock, flags);
882 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
884 struct au0828_dev *dev = vb2_get_drv_priv(vq);
885 struct au0828_dmaqueue *vbiq = &dev->vbiq;
886 unsigned long flags = 0;
888 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
889 dev->streaming_users);
891 if (dev->streaming_users-- == 1) {
892 au0828_uninit_isoc(dev);
893 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
896 spin_lock_irqsave(&dev->slock, flags);
897 if (dev->isoc_ctl.vbi_buf != NULL) {
898 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
899 VB2_BUF_STATE_ERROR);
900 dev->isoc_ctl.vbi_buf = NULL;
902 while (!list_empty(&vbiq->active)) {
903 struct au0828_buffer *buf;
905 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
906 list_del(&buf->list);
907 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
909 spin_unlock_irqrestore(&dev->slock, flags);
911 dev->vbi_timeout_running = 0;
912 del_timer_sync(&dev->vbi_timeout);
915 static const struct vb2_ops au0828_video_qops = {
916 .queue_setup = queue_setup,
917 .buf_prepare = buffer_prepare,
918 .buf_queue = buffer_queue,
919 .start_streaming = au0828_start_analog_streaming,
920 .stop_streaming = au0828_stop_streaming,
921 .wait_prepare = vb2_ops_wait_prepare,
922 .wait_finish = vb2_ops_wait_finish,
925 /* ------------------------------------------------------------------
927 ------------------------------------------------------------------*/
929 * au0828_analog_unregister
930 * unregister v4l2 devices
932 int au0828_analog_unregister(struct au0828_dev *dev)
934 dprintk(1, "au0828_analog_unregister called\n");
937 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
940 mutex_lock(&au0828_sysfs_lock);
941 video_unregister_device(&dev->vdev);
942 video_unregister_device(&dev->vbi_dev);
943 mutex_unlock(&au0828_sysfs_lock);
945 v4l2_device_disconnect(&dev->v4l2_dev);
946 v4l2_device_put(&dev->v4l2_dev);
951 /* This function ensures that video frames continue to be delivered even if
952 the ITU-656 input isn't receiving any data (thereby preventing applications
953 such as tvtime from hanging) */
954 static void au0828_vid_buffer_timeout(struct timer_list *t)
956 struct au0828_dev *dev = from_timer(dev, t, vid_timeout);
957 struct au0828_dmaqueue *dma_q = &dev->vidq;
958 struct au0828_buffer *buf;
959 unsigned char *vid_data;
960 unsigned long flags = 0;
962 spin_lock_irqsave(&dev->slock, flags);
964 buf = dev->isoc_ctl.buf;
966 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
967 memset(vid_data, 0x00, buf->length); /* Blank green frame */
968 buffer_filled(dev, dma_q, buf);
970 get_next_buf(dma_q, &buf);
972 if (dev->vid_timeout_running == 1)
973 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
975 spin_unlock_irqrestore(&dev->slock, flags);
978 static void au0828_vbi_buffer_timeout(struct timer_list *t)
980 struct au0828_dev *dev = from_timer(dev, t, vbi_timeout);
981 struct au0828_dmaqueue *dma_q = &dev->vbiq;
982 struct au0828_buffer *buf;
983 unsigned char *vbi_data;
984 unsigned long flags = 0;
986 spin_lock_irqsave(&dev->slock, flags);
988 buf = dev->isoc_ctl.vbi_buf;
990 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
991 memset(vbi_data, 0x00, buf->length);
992 buffer_filled(dev, dma_q, buf);
994 vbi_get_next_buf(dma_q, &buf);
996 if (dev->vbi_timeout_running == 1)
997 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
998 spin_unlock_irqrestore(&dev->slock, flags);
1001 static int au0828_v4l2_open(struct file *filp)
1003 struct au0828_dev *dev = video_drvdata(filp);
1007 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1008 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1009 dev->streaming_users, dev->users);
1011 if (mutex_lock_interruptible(&dev->lock))
1012 return -ERESTARTSYS;
1014 ret = v4l2_fh_open(filp);
1016 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1018 mutex_unlock(&dev->lock);
1022 if (dev->users == 0) {
1023 au0828_analog_stream_enable(dev);
1024 au0828_analog_stream_reset(dev);
1025 dev->stream_state = STREAM_OFF;
1026 set_bit(DEV_INITIALIZED, &dev->dev_state);
1029 mutex_unlock(&dev->lock);
1033 static int au0828_v4l2_close(struct file *filp)
1036 struct au0828_dev *dev = video_drvdata(filp);
1037 struct video_device *vdev = video_devdata(filp);
1040 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1041 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1042 dev->streaming_users, dev->users);
1044 mutex_lock(&dev->lock);
1045 if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
1046 /* Cancel timeout thread in case they didn't call streamoff */
1047 dev->vid_timeout_running = 0;
1048 del_timer_sync(&dev->vid_timeout);
1049 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1050 dev->vbi_timeout_running) {
1051 /* Cancel timeout thread in case they didn't call streamoff */
1052 dev->vbi_timeout_running = 0;
1053 del_timer_sync(&dev->vbi_timeout);
1056 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
1059 if (dev->users == 1) {
1061 * Avoid putting tuner in sleep if DVB or ALSA are
1064 * On most USB devices like au0828 the tuner can
1065 * be safely put in sleep state here if ALSA isn't
1066 * streaming. Exceptions are some very old USB tuner
1067 * models such as em28xx-based WinTV USB2 which have
1068 * a separate audio output jack. The devices that have
1069 * a separate audio output jack have analog tuners,
1070 * like Philips FM1236. Those devices are always on,
1071 * so the s_power callback are silently ignored.
1072 * So, the current logic here does the following:
1073 * Disable (put tuner to sleep) when
1074 * - ALSA and DVB aren't streaming.
1075 * - the last V4L2 file handler is closed.
1079 * Additionally, this logic could be improved to
1080 * disable the media source if the above conditions
1081 * are met and if the device:
1082 * - doesn't have a separate audio out plug (or
1083 * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1085 * Once this additional logic is in place, a callback
1086 * is needed to enable the media source and power on
1087 * the tuner, for radio to work.
1089 ret = v4l_enable_media_source(vdev);
1091 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner,
1093 dev->std_set_in_tuner_core = 0;
1095 /* When close the device, set the usb intf0 into alt0 to free
1097 ret = usb_set_interface(dev->usbdev, 0, 0);
1099 pr_info("Au0828 can't set alternate to 0!\n");
1102 _vb2_fop_release(filp, NULL);
1104 mutex_unlock(&dev->lock);
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 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1117 dev->std_set_in_tuner_core, dev->dev_state);
1119 if (dev->std_set_in_tuner_core)
1121 dev->std_set_in_tuner_core = 1;
1122 i2c_gate_ctrl(dev, 1);
1123 /* If we've never sent the standard in tuner core, do so now.
1124 We don't do this at device probe because we don't want to
1125 incur the cost of a firmware load */
1126 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1127 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1128 i2c_gate_ctrl(dev, 0);
1131 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1132 struct v4l2_format *format)
1135 int width = format->fmt.pix.width;
1136 int height = format->fmt.pix.height;
1138 /* If they are demanding a format other than the one we support,
1139 bail out (tvtime asks for UYVY and then retries with YUYV) */
1140 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1143 /* format->fmt.pix.width only support 720 and height 480 */
1149 format->fmt.pix.width = width;
1150 format->fmt.pix.height = height;
1151 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1152 format->fmt.pix.bytesperline = width * 2;
1153 format->fmt.pix.sizeimage = width * height * 2;
1154 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1155 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1156 format->fmt.pix.priv = 0;
1158 if (cmd == VIDIOC_TRY_FMT)
1161 /* maybe set new image format, driver current only support 720*480 */
1163 dev->height = height;
1164 dev->frame_size = width * height * 2;
1165 dev->field_size = width * height;
1166 dev->bytesperline = width * 2;
1168 if (dev->stream_state == STREAM_ON) {
1169 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1170 ret = au0828_stream_interrupt(dev);
1172 dprintk(1, "error interrupting video stream!\n");
1177 au0828_analog_stream_enable(dev);
1182 static int vidioc_querycap(struct file *file, void *priv,
1183 struct v4l2_capability *cap)
1185 struct au0828_dev *dev = video_drvdata(file);
1187 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1188 dev->std_set_in_tuner_core, dev->dev_state);
1190 strscpy(cap->driver, "au0828", sizeof(cap->driver));
1191 strscpy(cap->card, dev->board.name, sizeof(cap->card));
1192 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1194 /* set the device capabilities */
1196 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1197 V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1198 V4L2_CAP_DEVICE_CAPS;
1202 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1203 struct v4l2_fmtdesc *f)
1208 dprintk(1, "%s called\n", __func__);
1210 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1211 strscpy(f->description, "Packed YUV2", sizeof(f->description));
1214 f->pixelformat = V4L2_PIX_FMT_UYVY;
1219 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1220 struct v4l2_format *f)
1222 struct au0828_dev *dev = video_drvdata(file);
1224 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1225 dev->std_set_in_tuner_core, dev->dev_state);
1227 f->fmt.pix.width = dev->width;
1228 f->fmt.pix.height = dev->height;
1229 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1230 f->fmt.pix.bytesperline = dev->bytesperline;
1231 f->fmt.pix.sizeimage = dev->frame_size;
1232 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1233 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1234 f->fmt.pix.priv = 0;
1238 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1239 struct v4l2_format *f)
1241 struct au0828_dev *dev = video_drvdata(file);
1243 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1244 dev->std_set_in_tuner_core, dev->dev_state);
1246 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1249 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1250 struct v4l2_format *f)
1252 struct au0828_dev *dev = video_drvdata(file);
1255 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1256 dev->std_set_in_tuner_core, dev->dev_state);
1258 rc = check_dev(dev);
1262 if (vb2_is_busy(&dev->vb_vidq)) {
1263 pr_info("%s queue busy\n", __func__);
1268 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1273 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1275 struct au0828_dev *dev = video_drvdata(file);
1277 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1278 dev->std_set_in_tuner_core, dev->dev_state);
1280 if (norm == dev->std)
1283 if (dev->streaming_users > 0)
1288 au0828_init_tuner(dev);
1290 i2c_gate_ctrl(dev, 1);
1293 * FIXME: when we support something other than 60Hz standards,
1294 * we are going to have to make the au0828 bridge adjust the size
1295 * of its capture buffer, which is currently hardcoded at 720x480
1298 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1300 i2c_gate_ctrl(dev, 0);
1305 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1307 struct au0828_dev *dev = video_drvdata(file);
1309 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1310 dev->std_set_in_tuner_core, dev->dev_state);
1316 static int vidioc_enum_input(struct file *file, void *priv,
1317 struct v4l2_input *input)
1319 struct au0828_dev *dev = video_drvdata(file);
1322 static const char *inames[] = {
1323 [AU0828_VMUX_UNDEFINED] = "Undefined",
1324 [AU0828_VMUX_COMPOSITE] = "Composite",
1325 [AU0828_VMUX_SVIDEO] = "S-Video",
1326 [AU0828_VMUX_CABLE] = "Cable TV",
1327 [AU0828_VMUX_TELEVISION] = "Television",
1328 [AU0828_VMUX_DVB] = "DVB",
1331 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1332 dev->std_set_in_tuner_core, dev->dev_state);
1336 if (tmp >= AU0828_MAX_INPUT)
1338 if (AUVI_INPUT(tmp).type == 0)
1342 strscpy(input->name, inames[AUVI_INPUT(tmp).type], sizeof(input->name));
1343 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1344 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1345 input->type |= V4L2_INPUT_TYPE_TUNER;
1346 input->audioset = 1;
1348 input->type |= V4L2_INPUT_TYPE_CAMERA;
1349 input->audioset = 2;
1352 input->std = dev->vdev.tvnorms;
1357 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1359 struct au0828_dev *dev = video_drvdata(file);
1361 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1362 dev->std_set_in_tuner_core, dev->dev_state);
1364 *i = dev->ctrl_input;
1368 static void au0828_s_input(struct au0828_dev *dev, int index)
1372 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1373 dev->std_set_in_tuner_core, dev->dev_state);
1375 switch (AUVI_INPUT(index).type) {
1376 case AU0828_VMUX_SVIDEO:
1377 dev->input_type = AU0828_VMUX_SVIDEO;
1378 dev->ctrl_ainput = 1;
1380 case AU0828_VMUX_COMPOSITE:
1381 dev->input_type = AU0828_VMUX_COMPOSITE;
1382 dev->ctrl_ainput = 1;
1384 case AU0828_VMUX_TELEVISION:
1385 dev->input_type = AU0828_VMUX_TELEVISION;
1386 dev->ctrl_ainput = 0;
1389 dprintk(1, "unknown input type set [%d]\n",
1390 AUVI_INPUT(index).type);
1394 dev->ctrl_input = index;
1396 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1397 AUVI_INPUT(index).vmux, 0, 0);
1399 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1401 if (AUVI_INPUT(i).audio_setup == NULL)
1409 (AUVI_INPUT(i).audio_setup)(dev, enable);
1411 /* Make sure we leave it turned on if some
1412 other input is routed to this callback */
1413 if ((AUVI_INPUT(i).audio_setup) !=
1414 ((AUVI_INPUT(index).audio_setup))) {
1415 (AUVI_INPUT(i).audio_setup)(dev, enable);
1420 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1421 AUVI_INPUT(index).amux, 0, 0);
1424 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1426 struct au0828_dev *dev = video_drvdata(file);
1427 struct video_device *vfd = video_devdata(file);
1429 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1431 if (index >= AU0828_MAX_INPUT)
1433 if (AUVI_INPUT(index).type == 0)
1436 if (dev->ctrl_input == index)
1439 au0828_s_input(dev, index);
1442 * Input has been changed. Disable the media source
1443 * associated with the old input and enable source
1444 * for the newly set input
1446 v4l_disable_media_source(vfd);
1447 return v4l_enable_media_source(vfd);
1450 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1455 dprintk(1, "%s called\n", __func__);
1458 strscpy(a->name, "Television", sizeof(a->name));
1460 strscpy(a->name, "Line in", sizeof(a->name));
1462 a->capability = V4L2_AUDCAP_STEREO;
1466 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1468 struct au0828_dev *dev = video_drvdata(file);
1470 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1471 dev->std_set_in_tuner_core, dev->dev_state);
1473 a->index = dev->ctrl_ainput;
1475 strscpy(a->name, "Television", sizeof(a->name));
1477 strscpy(a->name, "Line in", sizeof(a->name));
1479 a->capability = V4L2_AUDCAP_STEREO;
1483 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1485 struct au0828_dev *dev = video_drvdata(file);
1487 if (a->index != dev->ctrl_ainput)
1490 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1491 dev->std_set_in_tuner_core, dev->dev_state);
1495 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1497 struct au0828_dev *dev = video_drvdata(file);
1498 struct video_device *vfd = video_devdata(file);
1504 ret = v4l_enable_media_source(vfd);
1508 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1509 dev->std_set_in_tuner_core, dev->dev_state);
1511 strscpy(t->name, "Auvitek tuner", sizeof(t->name));
1513 au0828_init_tuner(dev);
1514 i2c_gate_ctrl(dev, 1);
1515 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1516 i2c_gate_ctrl(dev, 0);
1520 static int vidioc_s_tuner(struct file *file, void *priv,
1521 const struct v4l2_tuner *t)
1523 struct au0828_dev *dev = video_drvdata(file);
1528 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1529 dev->std_set_in_tuner_core, dev->dev_state);
1531 au0828_init_tuner(dev);
1532 i2c_gate_ctrl(dev, 1);
1533 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1534 i2c_gate_ctrl(dev, 0);
1536 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1543 static int vidioc_g_frequency(struct file *file, void *priv,
1544 struct v4l2_frequency *freq)
1546 struct au0828_dev *dev = video_drvdata(file);
1548 if (freq->tuner != 0)
1550 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1551 dev->std_set_in_tuner_core, dev->dev_state);
1552 freq->frequency = dev->ctrl_freq;
1556 static int vidioc_s_frequency(struct file *file, void *priv,
1557 const struct v4l2_frequency *freq)
1559 struct au0828_dev *dev = video_drvdata(file);
1560 struct v4l2_frequency new_freq = *freq;
1562 if (freq->tuner != 0)
1565 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1566 dev->std_set_in_tuner_core, dev->dev_state);
1568 au0828_init_tuner(dev);
1569 i2c_gate_ctrl(dev, 1);
1571 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1572 /* Get the actual set (and possibly clamped) frequency */
1573 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1574 dev->ctrl_freq = new_freq.frequency;
1576 i2c_gate_ctrl(dev, 0);
1578 au0828_analog_stream_reset(dev);
1584 /* RAW VBI ioctls */
1586 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1587 struct v4l2_format *format)
1589 struct au0828_dev *dev = video_drvdata(file);
1591 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1592 dev->std_set_in_tuner_core, dev->dev_state);
1594 format->fmt.vbi.samples_per_line = dev->vbi_width;
1595 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1596 format->fmt.vbi.offset = 0;
1597 format->fmt.vbi.flags = 0;
1598 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1600 format->fmt.vbi.count[0] = dev->vbi_height;
1601 format->fmt.vbi.count[1] = dev->vbi_height;
1602 format->fmt.vbi.start[0] = 21;
1603 format->fmt.vbi.start[1] = 284;
1604 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1609 static int vidioc_g_pixelaspect(struct file *file, void *priv,
1610 int type, struct v4l2_fract *f)
1612 struct au0828_dev *dev = video_drvdata(file);
1614 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1617 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1618 dev->std_set_in_tuner_core, dev->dev_state);
1621 f->denominator = 59;
1626 static int vidioc_g_selection(struct file *file, void *priv,
1627 struct v4l2_selection *s)
1629 struct au0828_dev *dev = video_drvdata(file);
1631 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1634 switch (s->target) {
1635 case V4L2_SEL_TGT_CROP_BOUNDS:
1636 case V4L2_SEL_TGT_CROP_DEFAULT:
1639 s->r.width = dev->width;
1640 s->r.height = dev->height;
1648 #ifdef CONFIG_VIDEO_ADV_DEBUG
1649 static int vidioc_g_register(struct file *file, void *priv,
1650 struct v4l2_dbg_register *reg)
1652 struct au0828_dev *dev = video_drvdata(file);
1654 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1655 dev->std_set_in_tuner_core, dev->dev_state);
1657 reg->val = au0828_read(dev, reg->reg);
1662 static int vidioc_s_register(struct file *file, void *priv,
1663 const struct v4l2_dbg_register *reg)
1665 struct au0828_dev *dev = video_drvdata(file);
1667 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1668 dev->std_set_in_tuner_core, dev->dev_state);
1670 return au0828_writereg(dev, reg->reg, reg->val);
1674 static int vidioc_log_status(struct file *file, void *fh)
1676 struct video_device *vdev = video_devdata(file);
1678 dprintk(1, "%s called\n", __func__);
1680 v4l2_ctrl_log_status(file, fh);
1681 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1685 void au0828_v4l2_suspend(struct au0828_dev *dev)
1690 pr_info("stopping V4L2\n");
1692 if (dev->stream_state == STREAM_ON) {
1693 pr_info("stopping V4L2 active URBs\n");
1694 au0828_analog_stream_disable(dev);
1696 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1697 urb = dev->isoc_ctl.urb[i];
1699 if (!irqs_disabled())
1702 usb_unlink_urb(urb);
1707 if (dev->vid_timeout_running)
1708 del_timer_sync(&dev->vid_timeout);
1709 if (dev->vbi_timeout_running)
1710 del_timer_sync(&dev->vbi_timeout);
1713 void au0828_v4l2_resume(struct au0828_dev *dev)
1717 pr_info("restarting V4L2\n");
1719 if (dev->stream_state == STREAM_ON) {
1720 au0828_stream_interrupt(dev);
1721 au0828_init_tuner(dev);
1724 if (dev->vid_timeout_running)
1725 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1726 if (dev->vbi_timeout_running)
1727 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1729 /* If we were doing ac97 instead of i2s, it would go here...*/
1730 au0828_i2s_init(dev);
1732 au0828_analog_stream_enable(dev);
1734 if (!(dev->stream_state == STREAM_ON)) {
1735 au0828_analog_stream_reset(dev);
1737 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1738 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1740 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1742 au0828_uninit_isoc(dev);
1748 static const struct v4l2_file_operations au0828_v4l_fops = {
1749 .owner = THIS_MODULE,
1750 .open = au0828_v4l2_open,
1751 .release = au0828_v4l2_close,
1752 .read = vb2_fop_read,
1753 .poll = vb2_fop_poll,
1754 .mmap = vb2_fop_mmap,
1755 .unlocked_ioctl = video_ioctl2,
1758 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1759 .vidioc_querycap = vidioc_querycap,
1760 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1761 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1762 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1763 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1764 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1765 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1766 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1767 .vidioc_enumaudio = vidioc_enumaudio,
1768 .vidioc_g_audio = vidioc_g_audio,
1769 .vidioc_s_audio = vidioc_s_audio,
1770 .vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1771 .vidioc_g_selection = vidioc_g_selection,
1773 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1774 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1775 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1776 .vidioc_querybuf = vb2_ioctl_querybuf,
1777 .vidioc_qbuf = vb2_ioctl_qbuf,
1778 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1779 .vidioc_expbuf = vb2_ioctl_expbuf,
1781 .vidioc_s_std = vidioc_s_std,
1782 .vidioc_g_std = vidioc_g_std,
1783 .vidioc_enum_input = vidioc_enum_input,
1784 .vidioc_g_input = vidioc_g_input,
1785 .vidioc_s_input = vidioc_s_input,
1787 .vidioc_streamon = vb2_ioctl_streamon,
1788 .vidioc_streamoff = vb2_ioctl_streamoff,
1790 .vidioc_g_tuner = vidioc_g_tuner,
1791 .vidioc_s_tuner = vidioc_s_tuner,
1792 .vidioc_g_frequency = vidioc_g_frequency,
1793 .vidioc_s_frequency = vidioc_s_frequency,
1794 #ifdef CONFIG_VIDEO_ADV_DEBUG
1795 .vidioc_g_register = vidioc_g_register,
1796 .vidioc_s_register = vidioc_s_register,
1798 .vidioc_log_status = vidioc_log_status,
1799 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1800 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1803 static const struct video_device au0828_video_template = {
1804 .fops = &au0828_v4l_fops,
1805 .release = video_device_release_empty,
1806 .ioctl_ops = &video_ioctl_ops,
1807 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1810 static int au0828_vb2_setup(struct au0828_dev *dev)
1813 struct vb2_queue *q;
1815 /* Setup Videobuf2 for Video capture */
1817 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1818 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1819 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1821 q->buf_struct_size = sizeof(struct au0828_buffer);
1822 q->ops = &au0828_video_qops;
1823 q->mem_ops = &vb2_vmalloc_memops;
1825 rc = vb2_queue_init(q);
1829 /* Setup Videobuf2 for VBI capture */
1831 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1832 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1833 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1835 q->buf_struct_size = sizeof(struct au0828_buffer);
1836 q->ops = &au0828_vbi_qops;
1837 q->mem_ops = &vb2_vmalloc_memops;
1839 rc = vb2_queue_init(q);
1846 static void au0828_analog_create_entities(struct au0828_dev *dev)
1848 #if defined(CONFIG_MEDIA_CONTROLLER)
1849 static const char * const inames[] = {
1850 [AU0828_VMUX_COMPOSITE] = "Composite",
1851 [AU0828_VMUX_SVIDEO] = "S-Video",
1852 [AU0828_VMUX_CABLE] = "Cable TV",
1853 [AU0828_VMUX_TELEVISION] = "Television",
1854 [AU0828_VMUX_DVB] = "DVB",
1858 /* Initialize Video and VBI pads */
1859 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1860 ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1862 pr_err("failed to initialize video media entity!\n");
1864 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1865 ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1867 pr_err("failed to initialize vbi media entity!\n");
1869 /* Create entities for each input connector */
1870 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1871 struct media_entity *ent = &dev->input_ent[i];
1873 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1876 ent->name = inames[AUVI_INPUT(i).type];
1877 ent->flags = MEDIA_ENT_FL_CONNECTOR;
1878 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1880 switch (AUVI_INPUT(i).type) {
1881 case AU0828_VMUX_COMPOSITE:
1882 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1884 case AU0828_VMUX_SVIDEO:
1885 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1887 case AU0828_VMUX_CABLE:
1888 case AU0828_VMUX_TELEVISION:
1889 case AU0828_VMUX_DVB:
1890 default: /* Just to shut up a warning */
1891 ent->function = MEDIA_ENT_F_CONN_RF;
1895 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1897 pr_err("failed to initialize input pad[%d]!\n", i);
1899 ret = media_device_register_entity(dev->media_dev, ent);
1901 pr_err("failed to register input entity %d!\n", i);
1906 /**************************************************************************/
1908 int au0828_analog_register(struct au0828_dev *dev,
1909 struct usb_interface *interface)
1911 int retval = -ENOMEM;
1912 struct usb_host_interface *iface_desc;
1913 struct usb_endpoint_descriptor *endpoint;
1916 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1917 interface->cur_altsetting->desc.bInterfaceNumber);
1920 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1923 /* set au0828 usb interface0 to as5 */
1924 retval = usb_set_interface(dev->usbdev,
1925 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1927 pr_info("Failure setting usb interface0 to as5\n");
1931 /* Figure out which endpoint has the isoc interface */
1932 iface_desc = interface->cur_altsetting;
1933 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1934 endpoint = &iface_desc->endpoint[i].desc;
1935 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1937 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1938 == USB_ENDPOINT_XFER_ISOC)) {
1940 /* we find our isoc in endpoint */
1941 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1942 dev->max_pkt_size = (tmp & 0x07ff) *
1943 (((tmp & 0x1800) >> 11) + 1);
1944 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1946 "Found isoc endpoint 0x%02x, max size = %d\n",
1947 dev->isoc_in_endpointaddr, dev->max_pkt_size);
1950 if (!(dev->isoc_in_endpointaddr)) {
1951 pr_info("Could not locate isoc endpoint\n");
1955 init_waitqueue_head(&dev->open);
1956 spin_lock_init(&dev->slock);
1958 /* init video dma queues */
1959 INIT_LIST_HEAD(&dev->vidq.active);
1960 INIT_LIST_HEAD(&dev->vbiq.active);
1962 timer_setup(&dev->vid_timeout, au0828_vid_buffer_timeout, 0);
1963 timer_setup(&dev->vbi_timeout, au0828_vbi_buffer_timeout, 0);
1965 dev->width = NTSC_STD_W;
1966 dev->height = NTSC_STD_H;
1967 dev->field_size = dev->width * dev->height;
1968 dev->frame_size = dev->field_size << 1;
1969 dev->bytesperline = dev->width << 1;
1970 dev->vbi_width = 720;
1971 dev->vbi_height = 1;
1972 dev->ctrl_ainput = 0;
1973 dev->ctrl_freq = 960;
1974 dev->std = V4L2_STD_NTSC_M;
1975 /* Default input is TV Tuner */
1976 au0828_s_input(dev, 0);
1978 mutex_init(&dev->vb_queue_lock);
1979 mutex_init(&dev->vb_vbi_queue_lock);
1981 /* Fill the video capture device struct */
1982 dev->vdev = au0828_video_template;
1983 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1984 dev->vdev.lock = &dev->lock;
1985 dev->vdev.queue = &dev->vb_vidq;
1986 dev->vdev.queue->lock = &dev->vb_queue_lock;
1987 dev->vdev.device_caps =
1988 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1989 V4L2_CAP_TUNER | V4L2_CAP_VIDEO_CAPTURE;
1990 strscpy(dev->vdev.name, "au0828a video", sizeof(dev->vdev.name));
1992 /* Setup the VBI device */
1993 dev->vbi_dev = au0828_video_template;
1994 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1995 dev->vbi_dev.lock = &dev->lock;
1996 dev->vbi_dev.queue = &dev->vb_vbiq;
1997 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1998 dev->vbi_dev.device_caps =
1999 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
2000 V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE;
2001 strscpy(dev->vbi_dev.name, "au0828a vbi", sizeof(dev->vbi_dev.name));
2003 /* Init entities at the Media Controller */
2004 au0828_analog_create_entities(dev);
2006 /* initialize videobuf2 stuff */
2007 retval = au0828_vb2_setup(dev);
2009 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2014 /* Register the v4l2 device */
2015 video_set_drvdata(&dev->vdev, dev);
2016 retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
2018 dprintk(1, "unable to register video device (error = %d).\n",
2024 /* Register the vbi device */
2025 video_set_drvdata(&dev->vbi_dev, dev);
2026 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
2028 dprintk(1, "unable to register vbi device (error = %d).\n",
2031 goto err_reg_vbi_dev;
2034 #ifdef CONFIG_MEDIA_CONTROLLER
2035 retval = v4l2_mc_create_media_graph(dev->media_dev);
2037 pr_err("%s() au0282_dev_register failed to create graph\n",
2040 goto err_reg_vbi_dev;
2044 dprintk(1, "%s completed!\n", __func__);
2049 video_unregister_device(&dev->vdev);
2051 vb2_queue_release(&dev->vb_vidq);
2052 vb2_queue_release(&dev->vb_vbiq);