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 (dev->dev_state & DEV_DISCONNECTED) {
108 pr_info("v4l2 ioctl: device not present\n");
112 if (dev->dev_state & DEV_MISCONFIGURED) {
113 pr_info("v4l2 ioctl: device is misconfigured; "
114 "close and open it again\n");
121 * IRQ callback, called by URB callback
123 static void au0828_irq_callback(struct urb *urb)
125 struct au0828_dmaqueue *dma_q = urb->context;
126 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
127 unsigned long flags = 0;
130 switch (urb->status) {
131 case 0: /* success */
132 case -ETIMEDOUT: /* NAK */
134 case -ECONNRESET: /* kill */
137 au0828_isocdbg("au0828_irq_callback called: status kill\n");
139 default: /* unknown error */
140 au0828_isocdbg("urb completition error %d.\n", urb->status);
144 /* Copy data from URB */
145 spin_lock_irqsave(&dev->slock, flags);
146 dev->isoc_ctl.isoc_copy(dev, urb);
147 spin_unlock_irqrestore(&dev->slock, flags);
149 /* Reset urb buffers */
150 for (i = 0; i < urb->number_of_packets; i++) {
151 urb->iso_frame_desc[i].status = 0;
152 urb->iso_frame_desc[i].actual_length = 0;
156 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
158 au0828_isocdbg("urb resubmit failed (error=%i)\n",
161 dev->stream_state = STREAM_ON;
165 * Stop and Deallocate URBs
167 static void au0828_uninit_isoc(struct au0828_dev *dev)
172 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
174 dev->isoc_ctl.nfields = -1;
175 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
176 urb = dev->isoc_ctl.urb[i];
178 if (!irqs_disabled())
183 if (dev->isoc_ctl.transfer_buffer[i]) {
184 usb_free_coherent(dev->usbdev,
185 urb->transfer_buffer_length,
186 dev->isoc_ctl.transfer_buffer[i],
190 dev->isoc_ctl.urb[i] = NULL;
192 dev->isoc_ctl.transfer_buffer[i] = NULL;
195 kfree(dev->isoc_ctl.urb);
196 kfree(dev->isoc_ctl.transfer_buffer);
198 dev->isoc_ctl.urb = NULL;
199 dev->isoc_ctl.transfer_buffer = NULL;
200 dev->isoc_ctl.num_bufs = 0;
202 dev->stream_state = STREAM_OFF;
206 * Allocate URBs and start IRQ
208 static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
209 int num_bufs, int max_pkt_size,
210 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
212 struct au0828_dmaqueue *dma_q = &dev->vidq;
219 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
221 dev->isoc_ctl.isoc_copy = isoc_copy;
222 dev->isoc_ctl.num_bufs = num_bufs;
224 dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
225 if (!dev->isoc_ctl.urb) {
226 au0828_isocdbg("cannot alloc memory for usb buffers\n");
230 dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
232 if (!dev->isoc_ctl.transfer_buffer) {
233 au0828_isocdbg("cannot allocate memory for usb transfer\n");
234 kfree(dev->isoc_ctl.urb);
238 dev->isoc_ctl.max_pkt_size = max_pkt_size;
239 dev->isoc_ctl.buf = NULL;
241 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
243 /* allocate urbs and transfer buffers */
244 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
245 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
247 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
248 au0828_uninit_isoc(dev);
251 dev->isoc_ctl.urb[i] = urb;
253 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
254 sb_size, GFP_KERNEL, &urb->transfer_dma);
255 if (!dev->isoc_ctl.transfer_buffer[i]) {
256 printk("unable to allocate %i bytes for transfer"
259 in_interrupt() ? " while in int" : "");
260 au0828_uninit_isoc(dev);
263 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
265 pipe = usb_rcvisocpipe(dev->usbdev,
266 dev->isoc_in_endpointaddr),
268 usb_fill_int_urb(urb, dev->usbdev, pipe,
269 dev->isoc_ctl.transfer_buffer[i], sb_size,
270 au0828_irq_callback, dma_q, 1);
272 urb->number_of_packets = max_packets;
273 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
276 for (j = 0; j < max_packets; j++) {
277 urb->iso_frame_desc[j].offset = k;
278 urb->iso_frame_desc[j].length =
279 dev->isoc_ctl.max_pkt_size;
280 k += dev->isoc_ctl.max_pkt_size;
284 /* submit urbs and enables IRQ */
285 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
286 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
288 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
290 au0828_uninit_isoc(dev);
299 * Announces that a buffer were filled and request the next
301 static inline void buffer_filled(struct au0828_dev *dev,
302 struct au0828_dmaqueue *dma_q,
303 struct au0828_buffer *buf)
305 struct vb2_v4l2_buffer *vb = &buf->vb;
306 struct vb2_queue *q = vb->vb2_buf.vb2_queue;
308 /* Advice that buffer was filled */
309 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
311 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
312 vb->sequence = dev->frame_count++;
314 vb->sequence = dev->vbi_frame_count++;
316 vb->field = V4L2_FIELD_INTERLACED;
317 vb->vb2_buf.timestamp = ktime_get_ns();
318 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
322 * Identify the buffer header type and properly handles
324 static void au0828_copy_video(struct au0828_dev *dev,
325 struct au0828_dmaqueue *dma_q,
326 struct au0828_buffer *buf,
328 unsigned char *outp, unsigned long len)
330 void *fieldstart, *startwrite, *startread;
331 int linesdone, currlinedone, offset, lencopy, remain;
332 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
337 if (dma_q->pos + len > buf->length)
338 len = buf->length - dma_q->pos;
343 /* Interlaces frame */
347 fieldstart = outp + bytesperline;
349 linesdone = dma_q->pos / bytesperline;
350 currlinedone = dma_q->pos % bytesperline;
351 offset = linesdone * bytesperline * 2 + currlinedone;
352 startwrite = fieldstart + offset;
353 lencopy = bytesperline - currlinedone;
354 lencopy = lencopy > remain ? remain : lencopy;
356 if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
357 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
358 ((char *)startwrite + lencopy) -
359 ((char *)outp + buf->length));
360 remain = (char *)outp + buf->length - (char *)startwrite;
365 memcpy(startwrite, startread, lencopy);
370 startwrite += lencopy + bytesperline;
371 startread += lencopy;
372 if (bytesperline > remain)
375 lencopy = bytesperline;
377 if ((char *)startwrite + lencopy > (char *)outp +
379 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
380 ((char *)startwrite + lencopy) -
381 ((char *)outp + buf->length));
382 lencopy = remain = (char *)outp + buf->length -
388 memcpy(startwrite, startread, lencopy);
394 /* We have enough data to check for greenscreen */
395 if (outp[0] < 0x60 && outp[1440] < 0x60)
396 dev->greenscreen_detected = 1;
403 * video-buf generic routine to get the next available buffer
405 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
406 struct au0828_buffer **buf)
408 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
410 if (list_empty(&dma_q->active)) {
411 au0828_isocdbg("No active queue to serve\n");
412 dev->isoc_ctl.buf = NULL;
417 /* Get the next buffer */
418 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
419 /* Cleans up buffer - Useful for testing for frame/URB loss */
420 list_del(&(*buf)->list);
422 (*buf)->vb_buf = (*buf)->mem;
423 dev->isoc_ctl.buf = *buf;
428 static void au0828_copy_vbi(struct au0828_dev *dev,
429 struct au0828_dmaqueue *dma_q,
430 struct au0828_buffer *buf,
432 unsigned char *outp, unsigned long len)
434 unsigned char *startwrite, *startread;
439 au0828_isocdbg("dev is null\n");
444 au0828_isocdbg("dma_q is null\n");
450 au0828_isocdbg("p is null\n");
454 au0828_isocdbg("outp is null\n");
458 bytesperline = dev->vbi_width;
460 if (dma_q->pos + len > buf->length)
461 len = buf->length - dma_q->pos;
464 startwrite = outp + (dma_q->pos / 2);
466 /* Make sure the bottom field populates the second half of the frame */
467 if (buf->top_field == 0)
468 startwrite += bytesperline * dev->vbi_height;
470 for (i = 0; i < len; i += 2)
471 startwrite[j++] = startread[i+1];
478 * video-buf generic routine to get the next available VBI buffer
480 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
481 struct au0828_buffer **buf)
483 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
485 if (list_empty(&dma_q->active)) {
486 au0828_isocdbg("No active queue to serve\n");
487 dev->isoc_ctl.vbi_buf = NULL;
492 /* Get the next buffer */
493 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
494 /* Cleans up buffer - Useful for testing for frame/URB loss */
495 list_del(&(*buf)->list);
497 (*buf)->vb_buf = (*buf)->mem;
498 dev->isoc_ctl.vbi_buf = *buf;
503 * Controls the isoc copy of each urb packet
505 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
507 struct au0828_buffer *buf;
508 struct au0828_buffer *vbi_buf;
509 struct au0828_dmaqueue *dma_q = urb->context;
510 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
511 unsigned char *outp = NULL;
512 unsigned char *vbioutp = NULL;
513 int i, len = 0, rc = 1;
516 unsigned int vbi_field_size;
517 unsigned int remain, lencopy;
522 if ((dev->dev_state & DEV_DISCONNECTED) ||
523 (dev->dev_state & DEV_MISCONFIGURED))
526 if (urb->status < 0) {
527 print_err_status(dev, -1, urb->status);
528 if (urb->status == -ENOENT)
532 buf = dev->isoc_ctl.buf;
534 outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
536 vbi_buf = dev->isoc_ctl.vbi_buf;
538 vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
540 for (i = 0; i < urb->number_of_packets; i++) {
541 int status = urb->iso_frame_desc[i].status;
544 print_err_status(dev, i, status);
545 if (urb->iso_frame_desc[i].status != -EPROTO)
549 if (urb->iso_frame_desc[i].actual_length <= 0)
552 if (urb->iso_frame_desc[i].actual_length >
554 au0828_isocdbg("packet bigger than packet size");
558 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
560 len = urb->iso_frame_desc[i].actual_length - 4;
566 au0828_isocdbg("Video frame %s\n",
567 (fbyte & 0x40) ? "odd" : "even");
571 buffer_filled(dev, vbi_dma_q, vbi_buf);
572 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
576 vbioutp = vb2_plane_vaddr(
577 &vbi_buf->vb.vb2_buf, 0);
581 buffer_filled(dev, dma_q, buf);
582 get_next_buf(dma_q, &buf);
586 outp = vb2_plane_vaddr(
587 &buf->vb.vb2_buf, 0);
589 /* As long as isoc traffic is arriving, keep
590 resetting the timer */
591 if (dev->vid_timeout_running)
592 mod_timer(&dev->vid_timeout,
593 jiffies + (HZ / 10));
594 if (dev->vbi_timeout_running)
595 mod_timer(&dev->vbi_timeout,
596 jiffies + (HZ / 10));
606 if (vbi_buf != NULL) {
608 vbi_buf->top_field = 1;
610 vbi_buf->top_field = 0;
618 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
619 if (dev->vbi_read < vbi_field_size) {
620 remain = vbi_field_size - dev->vbi_read;
627 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
632 dev->vbi_read += lencopy;
635 if (dev->vbi_read >= vbi_field_size && buf != NULL)
636 au0828_copy_video(dev, dma_q, buf, p, outp, len);
641 void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
643 #ifdef CONFIG_MEDIA_CONTROLLER
646 for (i = 0; i < AU0828_MAX_INPUT; i++) {
647 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
649 media_device_unregister_entity(&dev->input_ent[i]);
654 static int au0828_create_media_graph(struct au0828_dev *dev)
656 #ifdef CONFIG_MEDIA_CONTROLLER
657 struct media_device *mdev = dev->media_dev;
658 struct media_entity *entity;
659 struct media_entity *tuner = NULL, *decoder = NULL;
665 media_device_for_each_entity(entity, mdev) {
666 switch (entity->function) {
667 case MEDIA_ENT_F_TUNER:
670 case MEDIA_ENT_F_ATV_DECODER:
676 /* Analog setup, using tuner as a link */
678 /* Something bad happened! */
683 ret = media_create_pad_link(tuner, TUNER_PAD_OUTPUT,
685 MEDIA_LNK_FL_ENABLED);
689 ret = media_create_pad_link(decoder, 1, &dev->vdev.entity, 0,
690 MEDIA_LNK_FL_ENABLED);
693 ret = media_create_pad_link(decoder, 2, &dev->vbi_dev.entity, 0,
694 MEDIA_LNK_FL_ENABLED);
698 for (i = 0; i < AU0828_MAX_INPUT; i++) {
699 struct media_entity *ent = &dev->input_ent[i];
701 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
704 switch (AUVI_INPUT(i).type) {
705 case AU0828_VMUX_CABLE:
706 case AU0828_VMUX_TELEVISION:
707 case AU0828_VMUX_DVB:
711 ret = media_create_pad_link(ent, 0, tuner,
713 MEDIA_LNK_FL_ENABLED);
717 case AU0828_VMUX_COMPOSITE:
718 case AU0828_VMUX_SVIDEO:
719 default: /* AU0828_VMUX_DEBUG */
720 /* FIXME: fix the decoder PAD */
721 ret = media_create_pad_link(ent, 0, decoder, 0, 0);
731 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
733 struct au0828_dev *dev =
734 container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
736 v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
737 v4l2_device_unregister(&dev->v4l2_dev);
738 au0828_usb_v4l2_media_release(dev);
739 au0828_usb_release(dev);
742 int au0828_v4l2_device_register(struct usb_interface *interface,
743 struct au0828_dev *dev)
747 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
750 /* Create the v4l2_device */
751 #ifdef CONFIG_MEDIA_CONTROLLER
752 dev->v4l2_dev.mdev = dev->media_dev;
754 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
756 pr_err("%s() v4l2_device_register failed\n",
758 mutex_unlock(&dev->lock);
763 dev->v4l2_dev.release = au0828_usb_v4l2_release;
765 /* This control handler will inherit the controls from au8522 */
766 retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
768 pr_err("%s() v4l2_ctrl_handler_init failed\n",
770 mutex_unlock(&dev->lock);
774 dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
779 static int au0828_enable_analog_tuner(struct au0828_dev *dev)
781 #ifdef CONFIG_MEDIA_CONTROLLER
782 struct media_device *mdev = dev->media_dev;
783 struct media_entity *source;
784 struct media_link *link, *found_link = NULL;
785 int ret, active_links = 0;
787 if (!mdev || !dev->decoder)
791 * This will find the tuner that is connected into the decoder.
792 * Technically, this is not 100% correct, as the device may be
793 * using an analog input instead of the tuner. However, as we can't
794 * do DVB streaming while the DMA engine is being used for V4L2,
795 * this should be enough for the actual needs.
797 list_for_each_entry(link, &dev->decoder->links, list) {
798 if (link->sink->entity == dev->decoder) {
800 if (link->flags & MEDIA_LNK_FL_ENABLED)
806 if (active_links == 1 || !found_link)
809 source = found_link->source->entity;
810 list_for_each_entry(link, &source->links, list) {
811 struct media_entity *sink;
814 sink = link->sink->entity;
816 if (sink == dev->decoder)
817 flags = MEDIA_LNK_FL_ENABLED;
819 ret = media_entity_setup_link(link, flags);
822 "Couldn't change link %s->%s to %s. Error %d\n",
823 source->name, sink->name,
824 flags ? "enabled" : "disabled",
829 "link %s->%s was %s\n",
830 source->name, sink->name,
831 flags ? "ENABLED" : "disabled");
837 static int queue_setup(struct vb2_queue *vq,
838 unsigned int *nbuffers, unsigned int *nplanes,
839 unsigned int sizes[], void *alloc_ctxs[])
841 struct au0828_dev *dev = vb2_get_drv_priv(vq);
842 unsigned long size = dev->height * dev->bytesperline;
845 return sizes[0] < size ? -EINVAL : 0;
849 au0828_enable_analog_tuner(dev);
855 buffer_prepare(struct vb2_buffer *vb)
857 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
858 struct au0828_buffer *buf = container_of(vbuf,
859 struct au0828_buffer, vb);
860 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
862 buf->length = dev->height * dev->bytesperline;
864 if (vb2_plane_size(vb, 0) < buf->length) {
865 pr_err("%s data will not fit into plane (%lu < %lu)\n",
866 __func__, vb2_plane_size(vb, 0), buf->length);
869 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
874 buffer_queue(struct vb2_buffer *vb)
876 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
877 struct au0828_buffer *buf = container_of(vbuf,
878 struct au0828_buffer,
880 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
881 struct au0828_dmaqueue *vidq = &dev->vidq;
882 unsigned long flags = 0;
884 buf->mem = vb2_plane_vaddr(vb, 0);
885 buf->length = vb2_plane_size(vb, 0);
887 spin_lock_irqsave(&dev->slock, flags);
888 list_add_tail(&buf->list, &vidq->active);
889 spin_unlock_irqrestore(&dev->slock, flags);
892 static int au0828_i2s_init(struct au0828_dev *dev)
894 /* Enable i2s mode */
895 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
900 * Auvitek au0828 analog stream enable
902 static int au0828_analog_stream_enable(struct au0828_dev *d)
904 struct usb_interface *iface;
907 dprintk(1, "au0828_analog_stream_enable called\n");
909 iface = usb_ifnum_to_if(d->usbdev, 0);
910 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
911 dprintk(1, "Changing intf#0 to alt 5\n");
912 /* set au0828 interface0 to AS5 here again */
913 ret = usb_set_interface(d->usbdev, 0, 5);
915 pr_info("Au0828 can't set alt setting to 5!\n");
920 h = d->height / 2 + 2;
923 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
924 au0828_writereg(d, 0x106, 0x00);
926 au0828_writereg(d, 0x110, 0x00);
927 au0828_writereg(d, 0x111, 0x00);
928 au0828_writereg(d, 0x114, w & 0xff);
929 au0828_writereg(d, 0x115, w >> 8);
931 au0828_writereg(d, 0x112, 0x00);
932 au0828_writereg(d, 0x113, 0x00);
933 au0828_writereg(d, 0x116, h & 0xff);
934 au0828_writereg(d, 0x117, h >> 8);
935 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
940 static int au0828_analog_stream_disable(struct au0828_dev *d)
942 dprintk(1, "au0828_analog_stream_disable called\n");
943 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
947 static void au0828_analog_stream_reset(struct au0828_dev *dev)
949 dprintk(1, "au0828_analog_stream_reset called\n");
950 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
952 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
956 * Some operations needs to stop current streaming
958 static int au0828_stream_interrupt(struct au0828_dev *dev)
962 dev->stream_state = STREAM_INTERRUPT;
963 if (dev->dev_state == DEV_DISCONNECTED)
966 dev->dev_state = DEV_MISCONFIGURED;
967 dprintk(1, "%s device is misconfigured!\n", __func__);
973 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
975 struct au0828_dev *dev = vb2_get_drv_priv(vq);
978 dprintk(1, "au0828_start_analog_streaming called %d\n",
979 dev->streaming_users);
981 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
982 dev->frame_count = 0;
984 dev->vbi_frame_count = 0;
986 if (dev->streaming_users == 0) {
987 /* If we were doing ac97 instead of i2s, it would go here...*/
988 au0828_i2s_init(dev);
989 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
990 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
993 pr_info("au0828_init_isoc failed\n");
997 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
998 v4l2_device_call_all(&dev->v4l2_dev, 0, video,
1000 dev->vid_timeout_running = 1;
1001 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1002 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1003 dev->vbi_timeout_running = 1;
1004 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1007 dev->streaming_users++;
1011 static void au0828_stop_streaming(struct vb2_queue *vq)
1013 struct au0828_dev *dev = vb2_get_drv_priv(vq);
1014 struct au0828_dmaqueue *vidq = &dev->vidq;
1015 unsigned long flags = 0;
1017 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
1019 if (dev->streaming_users-- == 1)
1020 au0828_uninit_isoc(dev);
1022 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
1023 dev->vid_timeout_running = 0;
1024 del_timer_sync(&dev->vid_timeout);
1026 spin_lock_irqsave(&dev->slock, flags);
1027 if (dev->isoc_ctl.buf != NULL) {
1028 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
1029 VB2_BUF_STATE_ERROR);
1030 dev->isoc_ctl.buf = NULL;
1032 while (!list_empty(&vidq->active)) {
1033 struct au0828_buffer *buf;
1035 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
1036 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1037 list_del(&buf->list);
1039 spin_unlock_irqrestore(&dev->slock, flags);
1042 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
1044 struct au0828_dev *dev = vb2_get_drv_priv(vq);
1045 struct au0828_dmaqueue *vbiq = &dev->vbiq;
1046 unsigned long flags = 0;
1048 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
1049 dev->streaming_users);
1051 if (dev->streaming_users-- == 1)
1052 au0828_uninit_isoc(dev);
1054 spin_lock_irqsave(&dev->slock, flags);
1055 if (dev->isoc_ctl.vbi_buf != NULL) {
1056 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
1057 VB2_BUF_STATE_ERROR);
1058 dev->isoc_ctl.vbi_buf = NULL;
1060 while (!list_empty(&vbiq->active)) {
1061 struct au0828_buffer *buf;
1063 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
1064 list_del(&buf->list);
1065 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1067 spin_unlock_irqrestore(&dev->slock, flags);
1069 dev->vbi_timeout_running = 0;
1070 del_timer_sync(&dev->vbi_timeout);
1073 static struct vb2_ops au0828_video_qops = {
1074 .queue_setup = queue_setup,
1075 .buf_prepare = buffer_prepare,
1076 .buf_queue = buffer_queue,
1077 .start_streaming = au0828_start_analog_streaming,
1078 .stop_streaming = au0828_stop_streaming,
1079 .wait_prepare = vb2_ops_wait_prepare,
1080 .wait_finish = vb2_ops_wait_finish,
1083 /* ------------------------------------------------------------------
1085 ------------------------------------------------------------------*/
1087 * au0828_analog_unregister
1088 * unregister v4l2 devices
1090 int au0828_analog_unregister(struct au0828_dev *dev)
1092 dprintk(1, "au0828_analog_unregister called\n");
1095 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1098 mutex_lock(&au0828_sysfs_lock);
1099 video_unregister_device(&dev->vdev);
1100 video_unregister_device(&dev->vbi_dev);
1101 mutex_unlock(&au0828_sysfs_lock);
1103 v4l2_device_disconnect(&dev->v4l2_dev);
1104 v4l2_device_put(&dev->v4l2_dev);
1109 /* This function ensures that video frames continue to be delivered even if
1110 the ITU-656 input isn't receiving any data (thereby preventing applications
1111 such as tvtime from hanging) */
1112 static void au0828_vid_buffer_timeout(unsigned long data)
1114 struct au0828_dev *dev = (struct au0828_dev *) data;
1115 struct au0828_dmaqueue *dma_q = &dev->vidq;
1116 struct au0828_buffer *buf;
1117 unsigned char *vid_data;
1118 unsigned long flags = 0;
1120 spin_lock_irqsave(&dev->slock, flags);
1122 buf = dev->isoc_ctl.buf;
1124 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
1125 memset(vid_data, 0x00, buf->length); /* Blank green frame */
1126 buffer_filled(dev, dma_q, buf);
1128 get_next_buf(dma_q, &buf);
1130 if (dev->vid_timeout_running == 1)
1131 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1133 spin_unlock_irqrestore(&dev->slock, flags);
1136 static void au0828_vbi_buffer_timeout(unsigned long data)
1138 struct au0828_dev *dev = (struct au0828_dev *) data;
1139 struct au0828_dmaqueue *dma_q = &dev->vbiq;
1140 struct au0828_buffer *buf;
1141 unsigned char *vbi_data;
1142 unsigned long flags = 0;
1144 spin_lock_irqsave(&dev->slock, flags);
1146 buf = dev->isoc_ctl.vbi_buf;
1148 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
1149 memset(vbi_data, 0x00, buf->length);
1150 buffer_filled(dev, dma_q, buf);
1152 vbi_get_next_buf(dma_q, &buf);
1154 if (dev->vbi_timeout_running == 1)
1155 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1156 spin_unlock_irqrestore(&dev->slock, flags);
1159 static int au0828_v4l2_open(struct file *filp)
1161 struct au0828_dev *dev = video_drvdata(filp);
1165 "%s called std_set %d dev_state %d stream users %d users %d\n",
1166 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1167 dev->streaming_users, dev->users);
1169 if (mutex_lock_interruptible(&dev->lock))
1170 return -ERESTARTSYS;
1172 ret = v4l2_fh_open(filp);
1174 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1176 mutex_unlock(&dev->lock);
1180 if (dev->users == 0) {
1181 au0828_analog_stream_enable(dev);
1182 au0828_analog_stream_reset(dev);
1183 dev->stream_state = STREAM_OFF;
1184 dev->dev_state |= DEV_INITIALIZED;
1187 mutex_unlock(&dev->lock);
1191 static int au0828_v4l2_close(struct file *filp)
1194 struct au0828_dev *dev = video_drvdata(filp);
1195 struct video_device *vdev = video_devdata(filp);
1198 "%s called std_set %d dev_state %d stream users %d users %d\n",
1199 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1200 dev->streaming_users, dev->users);
1202 mutex_lock(&dev->lock);
1203 if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
1204 /* Cancel timeout thread in case they didn't call streamoff */
1205 dev->vid_timeout_running = 0;
1206 del_timer_sync(&dev->vid_timeout);
1207 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1208 dev->vbi_timeout_running) {
1209 /* Cancel timeout thread in case they didn't call streamoff */
1210 dev->vbi_timeout_running = 0;
1211 del_timer_sync(&dev->vbi_timeout);
1214 if (dev->dev_state == DEV_DISCONNECTED)
1217 if (dev->users == 1) {
1218 /* Save some power by putting tuner to sleep */
1219 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1220 dev->std_set_in_tuner_core = 0;
1222 /* When close the device, set the usb intf0 into alt0 to free
1224 ret = usb_set_interface(dev->usbdev, 0, 0);
1226 pr_info("Au0828 can't set alternate to 0!\n");
1229 _vb2_fop_release(filp, NULL);
1231 mutex_unlock(&dev->lock);
1235 /* Must be called with dev->lock held */
1236 static void au0828_init_tuner(struct au0828_dev *dev)
1238 struct v4l2_frequency f = {
1239 .frequency = dev->ctrl_freq,
1240 .type = V4L2_TUNER_ANALOG_TV,
1243 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1244 dev->std_set_in_tuner_core, dev->dev_state);
1246 if (dev->std_set_in_tuner_core)
1248 dev->std_set_in_tuner_core = 1;
1249 i2c_gate_ctrl(dev, 1);
1250 /* If we've never sent the standard in tuner core, do so now.
1251 We don't do this at device probe because we don't want to
1252 incur the cost of a firmware load */
1253 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1254 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1255 i2c_gate_ctrl(dev, 0);
1258 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1259 struct v4l2_format *format)
1262 int width = format->fmt.pix.width;
1263 int height = format->fmt.pix.height;
1265 /* If they are demanding a format other than the one we support,
1266 bail out (tvtime asks for UYVY and then retries with YUYV) */
1267 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1270 /* format->fmt.pix.width only support 720 and height 480 */
1276 format->fmt.pix.width = width;
1277 format->fmt.pix.height = height;
1278 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1279 format->fmt.pix.bytesperline = width * 2;
1280 format->fmt.pix.sizeimage = width * height * 2;
1281 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1282 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1283 format->fmt.pix.priv = 0;
1285 if (cmd == VIDIOC_TRY_FMT)
1288 /* maybe set new image format, driver current only support 720*480 */
1290 dev->height = height;
1291 dev->frame_size = width * height * 2;
1292 dev->field_size = width * height;
1293 dev->bytesperline = width * 2;
1295 if (dev->stream_state == STREAM_ON) {
1296 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1297 ret = au0828_stream_interrupt(dev);
1299 dprintk(1, "error interrupting video stream!\n");
1304 au0828_analog_stream_enable(dev);
1309 static int vidioc_querycap(struct file *file, void *priv,
1310 struct v4l2_capability *cap)
1312 struct video_device *vdev = video_devdata(file);
1313 struct au0828_dev *dev = video_drvdata(file);
1315 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1316 dev->std_set_in_tuner_core, dev->dev_state);
1318 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1319 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1320 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1322 /* set the device capabilities */
1323 cap->device_caps = V4L2_CAP_AUDIO |
1324 V4L2_CAP_READWRITE |
1325 V4L2_CAP_STREAMING |
1327 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1328 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1330 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1331 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1332 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
1336 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1337 struct v4l2_fmtdesc *f)
1342 dprintk(1, "%s called\n", __func__);
1344 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1345 strcpy(f->description, "Packed YUV2");
1348 f->pixelformat = V4L2_PIX_FMT_UYVY;
1353 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1354 struct v4l2_format *f)
1356 struct au0828_dev *dev = video_drvdata(file);
1358 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1359 dev->std_set_in_tuner_core, dev->dev_state);
1361 f->fmt.pix.width = dev->width;
1362 f->fmt.pix.height = dev->height;
1363 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1364 f->fmt.pix.bytesperline = dev->bytesperline;
1365 f->fmt.pix.sizeimage = dev->frame_size;
1366 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1367 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1368 f->fmt.pix.priv = 0;
1372 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1373 struct v4l2_format *f)
1375 struct au0828_dev *dev = video_drvdata(file);
1377 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1378 dev->std_set_in_tuner_core, dev->dev_state);
1380 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1383 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1384 struct v4l2_format *f)
1386 struct au0828_dev *dev = video_drvdata(file);
1389 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1390 dev->std_set_in_tuner_core, dev->dev_state);
1392 rc = check_dev(dev);
1396 if (vb2_is_busy(&dev->vb_vidq)) {
1397 pr_info("%s queue busy\n", __func__);
1402 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1407 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1409 struct au0828_dev *dev = video_drvdata(file);
1411 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1412 dev->std_set_in_tuner_core, dev->dev_state);
1414 if (norm == dev->std)
1417 if (dev->streaming_users > 0)
1422 au0828_init_tuner(dev);
1424 i2c_gate_ctrl(dev, 1);
1427 * FIXME: when we support something other than 60Hz standards,
1428 * we are going to have to make the au0828 bridge adjust the size
1429 * of its capture buffer, which is currently hardcoded at 720x480
1432 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1434 i2c_gate_ctrl(dev, 0);
1439 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1441 struct au0828_dev *dev = video_drvdata(file);
1443 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1444 dev->std_set_in_tuner_core, dev->dev_state);
1450 static int vidioc_enum_input(struct file *file, void *priv,
1451 struct v4l2_input *input)
1453 struct au0828_dev *dev = video_drvdata(file);
1456 static const char *inames[] = {
1457 [AU0828_VMUX_UNDEFINED] = "Undefined",
1458 [AU0828_VMUX_COMPOSITE] = "Composite",
1459 [AU0828_VMUX_SVIDEO] = "S-Video",
1460 [AU0828_VMUX_CABLE] = "Cable TV",
1461 [AU0828_VMUX_TELEVISION] = "Television",
1462 [AU0828_VMUX_DVB] = "DVB",
1463 [AU0828_VMUX_DEBUG] = "tv debug"
1466 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1467 dev->std_set_in_tuner_core, dev->dev_state);
1471 if (tmp >= AU0828_MAX_INPUT)
1473 if (AUVI_INPUT(tmp).type == 0)
1477 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1478 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1479 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1480 input->type |= V4L2_INPUT_TYPE_TUNER;
1481 input->audioset = 1;
1483 input->type |= V4L2_INPUT_TYPE_CAMERA;
1484 input->audioset = 2;
1487 input->std = dev->vdev.tvnorms;
1492 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1494 struct au0828_dev *dev = video_drvdata(file);
1496 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1497 dev->std_set_in_tuner_core, dev->dev_state);
1499 *i = dev->ctrl_input;
1503 static void au0828_s_input(struct au0828_dev *dev, int index)
1507 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1508 dev->std_set_in_tuner_core, dev->dev_state);
1510 switch (AUVI_INPUT(index).type) {
1511 case AU0828_VMUX_SVIDEO:
1512 dev->input_type = AU0828_VMUX_SVIDEO;
1513 dev->ctrl_ainput = 1;
1515 case AU0828_VMUX_COMPOSITE:
1516 dev->input_type = AU0828_VMUX_COMPOSITE;
1517 dev->ctrl_ainput = 1;
1519 case AU0828_VMUX_TELEVISION:
1520 dev->input_type = AU0828_VMUX_TELEVISION;
1521 dev->ctrl_ainput = 0;
1524 dprintk(1, "unknown input type set [%d]\n",
1525 AUVI_INPUT(index).type);
1529 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1530 AUVI_INPUT(index).vmux, 0, 0);
1532 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1534 if (AUVI_INPUT(i).audio_setup == NULL)
1542 (AUVI_INPUT(i).audio_setup)(dev, enable);
1544 /* Make sure we leave it turned on if some
1545 other input is routed to this callback */
1546 if ((AUVI_INPUT(i).audio_setup) !=
1547 ((AUVI_INPUT(index).audio_setup))) {
1548 (AUVI_INPUT(i).audio_setup)(dev, enable);
1553 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1554 AUVI_INPUT(index).amux, 0, 0);
1557 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1559 struct au0828_dev *dev = video_drvdata(file);
1561 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1563 if (index >= AU0828_MAX_INPUT)
1565 if (AUVI_INPUT(index).type == 0)
1567 dev->ctrl_input = index;
1568 au0828_s_input(dev, index);
1572 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1577 dprintk(1, "%s called\n", __func__);
1580 strcpy(a->name, "Television");
1582 strcpy(a->name, "Line in");
1584 a->capability = V4L2_AUDCAP_STEREO;
1588 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1590 struct au0828_dev *dev = video_drvdata(file);
1592 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1593 dev->std_set_in_tuner_core, dev->dev_state);
1595 a->index = dev->ctrl_ainput;
1597 strcpy(a->name, "Television");
1599 strcpy(a->name, "Line in");
1601 a->capability = V4L2_AUDCAP_STEREO;
1605 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1607 struct au0828_dev *dev = video_drvdata(file);
1609 if (a->index != dev->ctrl_ainput)
1612 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1613 dev->std_set_in_tuner_core, dev->dev_state);
1617 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1619 struct au0828_dev *dev = video_drvdata(file);
1624 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1625 dev->std_set_in_tuner_core, dev->dev_state);
1627 strcpy(t->name, "Auvitek tuner");
1629 au0828_init_tuner(dev);
1630 i2c_gate_ctrl(dev, 1);
1631 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1632 i2c_gate_ctrl(dev, 0);
1636 static int vidioc_s_tuner(struct file *file, void *priv,
1637 const struct v4l2_tuner *t)
1639 struct au0828_dev *dev = video_drvdata(file);
1644 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1645 dev->std_set_in_tuner_core, dev->dev_state);
1647 au0828_init_tuner(dev);
1648 i2c_gate_ctrl(dev, 1);
1649 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1650 i2c_gate_ctrl(dev, 0);
1652 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1659 static int vidioc_g_frequency(struct file *file, void *priv,
1660 struct v4l2_frequency *freq)
1662 struct au0828_dev *dev = video_drvdata(file);
1664 if (freq->tuner != 0)
1666 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1667 dev->std_set_in_tuner_core, dev->dev_state);
1668 freq->frequency = dev->ctrl_freq;
1672 static int vidioc_s_frequency(struct file *file, void *priv,
1673 const struct v4l2_frequency *freq)
1675 struct au0828_dev *dev = video_drvdata(file);
1676 struct v4l2_frequency new_freq = *freq;
1678 if (freq->tuner != 0)
1681 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1682 dev->std_set_in_tuner_core, dev->dev_state);
1684 au0828_init_tuner(dev);
1685 i2c_gate_ctrl(dev, 1);
1687 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1688 /* Get the actual set (and possibly clamped) frequency */
1689 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1690 dev->ctrl_freq = new_freq.frequency;
1692 i2c_gate_ctrl(dev, 0);
1694 au0828_analog_stream_reset(dev);
1700 /* RAW VBI ioctls */
1702 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1703 struct v4l2_format *format)
1705 struct au0828_dev *dev = video_drvdata(file);
1707 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1708 dev->std_set_in_tuner_core, dev->dev_state);
1710 format->fmt.vbi.samples_per_line = dev->vbi_width;
1711 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1712 format->fmt.vbi.offset = 0;
1713 format->fmt.vbi.flags = 0;
1714 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1716 format->fmt.vbi.count[0] = dev->vbi_height;
1717 format->fmt.vbi.count[1] = dev->vbi_height;
1718 format->fmt.vbi.start[0] = 21;
1719 format->fmt.vbi.start[1] = 284;
1720 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1725 static int vidioc_cropcap(struct file *file, void *priv,
1726 struct v4l2_cropcap *cc)
1728 struct au0828_dev *dev = video_drvdata(file);
1730 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1733 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1734 dev->std_set_in_tuner_core, dev->dev_state);
1736 cc->bounds.left = 0;
1738 cc->bounds.width = dev->width;
1739 cc->bounds.height = dev->height;
1741 cc->defrect = cc->bounds;
1743 cc->pixelaspect.numerator = 54;
1744 cc->pixelaspect.denominator = 59;
1749 #ifdef CONFIG_VIDEO_ADV_DEBUG
1750 static int vidioc_g_register(struct file *file, void *priv,
1751 struct v4l2_dbg_register *reg)
1753 struct au0828_dev *dev = video_drvdata(file);
1755 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1756 dev->std_set_in_tuner_core, dev->dev_state);
1758 reg->val = au0828_read(dev, reg->reg);
1763 static int vidioc_s_register(struct file *file, void *priv,
1764 const struct v4l2_dbg_register *reg)
1766 struct au0828_dev *dev = video_drvdata(file);
1768 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1769 dev->std_set_in_tuner_core, dev->dev_state);
1771 return au0828_writereg(dev, reg->reg, reg->val);
1775 static int vidioc_log_status(struct file *file, void *fh)
1777 struct video_device *vdev = video_devdata(file);
1779 dprintk(1, "%s called\n", __func__);
1781 v4l2_ctrl_log_status(file, fh);
1782 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1786 void au0828_v4l2_suspend(struct au0828_dev *dev)
1791 pr_info("stopping V4L2\n");
1793 if (dev->stream_state == STREAM_ON) {
1794 pr_info("stopping V4L2 active URBs\n");
1795 au0828_analog_stream_disable(dev);
1797 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1798 urb = dev->isoc_ctl.urb[i];
1800 if (!irqs_disabled())
1803 usb_unlink_urb(urb);
1808 if (dev->vid_timeout_running)
1809 del_timer_sync(&dev->vid_timeout);
1810 if (dev->vbi_timeout_running)
1811 del_timer_sync(&dev->vbi_timeout);
1814 void au0828_v4l2_resume(struct au0828_dev *dev)
1818 pr_info("restarting V4L2\n");
1820 if (dev->stream_state == STREAM_ON) {
1821 au0828_stream_interrupt(dev);
1822 au0828_init_tuner(dev);
1825 if (dev->vid_timeout_running)
1826 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1827 if (dev->vbi_timeout_running)
1828 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1830 /* If we were doing ac97 instead of i2s, it would go here...*/
1831 au0828_i2s_init(dev);
1833 au0828_analog_stream_enable(dev);
1835 if (!(dev->stream_state == STREAM_ON)) {
1836 au0828_analog_stream_reset(dev);
1838 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1839 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1841 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1843 au0828_uninit_isoc(dev);
1849 static struct v4l2_file_operations au0828_v4l_fops = {
1850 .owner = THIS_MODULE,
1851 .open = au0828_v4l2_open,
1852 .release = au0828_v4l2_close,
1853 .read = vb2_fop_read,
1854 .poll = vb2_fop_poll,
1855 .mmap = vb2_fop_mmap,
1856 .unlocked_ioctl = video_ioctl2,
1859 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1860 .vidioc_querycap = vidioc_querycap,
1861 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1862 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1863 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1864 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1865 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1866 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1867 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1868 .vidioc_enumaudio = vidioc_enumaudio,
1869 .vidioc_g_audio = vidioc_g_audio,
1870 .vidioc_s_audio = vidioc_s_audio,
1871 .vidioc_cropcap = vidioc_cropcap,
1873 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1874 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1875 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1876 .vidioc_querybuf = vb2_ioctl_querybuf,
1877 .vidioc_qbuf = vb2_ioctl_qbuf,
1878 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1879 .vidioc_expbuf = vb2_ioctl_expbuf,
1881 .vidioc_s_std = vidioc_s_std,
1882 .vidioc_g_std = vidioc_g_std,
1883 .vidioc_enum_input = vidioc_enum_input,
1884 .vidioc_g_input = vidioc_g_input,
1885 .vidioc_s_input = vidioc_s_input,
1887 .vidioc_streamon = vb2_ioctl_streamon,
1888 .vidioc_streamoff = vb2_ioctl_streamoff,
1890 .vidioc_g_tuner = vidioc_g_tuner,
1891 .vidioc_s_tuner = vidioc_s_tuner,
1892 .vidioc_g_frequency = vidioc_g_frequency,
1893 .vidioc_s_frequency = vidioc_s_frequency,
1894 #ifdef CONFIG_VIDEO_ADV_DEBUG
1895 .vidioc_g_register = vidioc_g_register,
1896 .vidioc_s_register = vidioc_s_register,
1898 .vidioc_log_status = vidioc_log_status,
1899 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1900 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1903 static const struct video_device au0828_video_template = {
1904 .fops = &au0828_v4l_fops,
1905 .release = video_device_release_empty,
1906 .ioctl_ops = &video_ioctl_ops,
1907 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1910 static int au0828_vb2_setup(struct au0828_dev *dev)
1913 struct vb2_queue *q;
1915 /* Setup Videobuf2 for Video capture */
1917 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1918 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1919 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1921 q->buf_struct_size = sizeof(struct au0828_buffer);
1922 q->ops = &au0828_video_qops;
1923 q->mem_ops = &vb2_vmalloc_memops;
1925 rc = vb2_queue_init(q);
1929 /* Setup Videobuf2 for VBI capture */
1931 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1932 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1933 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1935 q->buf_struct_size = sizeof(struct au0828_buffer);
1936 q->ops = &au0828_vbi_qops;
1937 q->mem_ops = &vb2_vmalloc_memops;
1939 rc = vb2_queue_init(q);
1946 static void au0828_analog_create_entities(struct au0828_dev *dev)
1948 #if defined(CONFIG_MEDIA_CONTROLLER)
1949 static const char * const inames[] = {
1950 [AU0828_VMUX_COMPOSITE] = "Composite",
1951 [AU0828_VMUX_SVIDEO] = "S-Video",
1952 [AU0828_VMUX_CABLE] = "Cable TV",
1953 [AU0828_VMUX_TELEVISION] = "Television",
1954 [AU0828_VMUX_DVB] = "DVB",
1955 [AU0828_VMUX_DEBUG] = "tv debug"
1959 /* Initialize Video and VBI pads */
1960 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1961 ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1963 pr_err("failed to initialize video media entity!\n");
1965 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1966 ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1968 pr_err("failed to initialize vbi media entity!\n");
1970 /* Create entities for each input connector */
1971 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1972 struct media_entity *ent = &dev->input_ent[i];
1974 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1977 ent->name = inames[AUVI_INPUT(i).type];
1978 ent->flags = MEDIA_ENT_FL_CONNECTOR;
1979 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1981 switch (AUVI_INPUT(i).type) {
1982 case AU0828_VMUX_COMPOSITE:
1983 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1985 case AU0828_VMUX_SVIDEO:
1986 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1988 case AU0828_VMUX_CABLE:
1989 case AU0828_VMUX_TELEVISION:
1990 case AU0828_VMUX_DVB:
1991 ent->function = MEDIA_ENT_F_CONN_RF;
1993 default: /* AU0828_VMUX_DEBUG */
1994 ent->function = MEDIA_ENT_F_CONN_TEST;
1998 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
2000 pr_err("failed to initialize input pad[%d]!\n", i);
2002 ret = media_device_register_entity(dev->media_dev, ent);
2004 pr_err("failed to register input entity %d!\n", i);
2009 /**************************************************************************/
2011 int au0828_analog_register(struct au0828_dev *dev,
2012 struct usb_interface *interface)
2014 int retval = -ENOMEM;
2015 struct usb_host_interface *iface_desc;
2016 struct usb_endpoint_descriptor *endpoint;
2019 dprintk(1, "au0828_analog_register called for intf#%d!\n",
2020 interface->cur_altsetting->desc.bInterfaceNumber);
2023 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
2026 /* set au0828 usb interface0 to as5 */
2027 retval = usb_set_interface(dev->usbdev,
2028 interface->cur_altsetting->desc.bInterfaceNumber, 5);
2030 pr_info("Failure setting usb interface0 to as5\n");
2034 /* Figure out which endpoint has the isoc interface */
2035 iface_desc = interface->cur_altsetting;
2036 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2037 endpoint = &iface_desc->endpoint[i].desc;
2038 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
2040 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
2041 == USB_ENDPOINT_XFER_ISOC)) {
2043 /* we find our isoc in endpoint */
2044 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
2045 dev->max_pkt_size = (tmp & 0x07ff) *
2046 (((tmp & 0x1800) >> 11) + 1);
2047 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
2049 "Found isoc endpoint 0x%02x, max size = %d\n",
2050 dev->isoc_in_endpointaddr, dev->max_pkt_size);
2053 if (!(dev->isoc_in_endpointaddr)) {
2054 pr_info("Could not locate isoc endpoint\n");
2058 init_waitqueue_head(&dev->open);
2059 spin_lock_init(&dev->slock);
2061 /* init video dma queues */
2062 INIT_LIST_HEAD(&dev->vidq.active);
2063 INIT_LIST_HEAD(&dev->vbiq.active);
2065 setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
2066 (unsigned long)dev);
2067 setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
2068 (unsigned long)dev);
2070 dev->width = NTSC_STD_W;
2071 dev->height = NTSC_STD_H;
2072 dev->field_size = dev->width * dev->height;
2073 dev->frame_size = dev->field_size << 1;
2074 dev->bytesperline = dev->width << 1;
2075 dev->vbi_width = 720;
2076 dev->vbi_height = 1;
2077 dev->ctrl_ainput = 0;
2078 dev->ctrl_freq = 960;
2079 dev->std = V4L2_STD_NTSC_M;
2080 au0828_s_input(dev, 0);
2082 mutex_init(&dev->vb_queue_lock);
2083 mutex_init(&dev->vb_vbi_queue_lock);
2085 /* Fill the video capture device struct */
2086 dev->vdev = au0828_video_template;
2087 dev->vdev.v4l2_dev = &dev->v4l2_dev;
2088 dev->vdev.lock = &dev->lock;
2089 dev->vdev.queue = &dev->vb_vidq;
2090 dev->vdev.queue->lock = &dev->vb_queue_lock;
2091 strcpy(dev->vdev.name, "au0828a video");
2093 /* Setup the VBI device */
2094 dev->vbi_dev = au0828_video_template;
2095 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
2096 dev->vbi_dev.lock = &dev->lock;
2097 dev->vbi_dev.queue = &dev->vb_vbiq;
2098 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
2099 strcpy(dev->vbi_dev.name, "au0828a vbi");
2101 /* Init entities at the Media Controller */
2102 au0828_analog_create_entities(dev);
2104 /* initialize videobuf2 stuff */
2105 retval = au0828_vb2_setup(dev);
2107 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2112 /* Register the v4l2 device */
2113 video_set_drvdata(&dev->vdev, dev);
2114 retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
2116 dprintk(1, "unable to register video device (error = %d).\n",
2122 /* Register the vbi device */
2123 video_set_drvdata(&dev->vbi_dev, dev);
2124 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
2126 dprintk(1, "unable to register vbi device (error = %d).\n",
2129 goto err_reg_vbi_dev;
2131 retval = au0828_create_media_graph(dev);
2133 pr_err("%s() au0282_dev_register failed to create graph\n",
2136 goto err_reg_vbi_dev;
2140 dprintk(1, "%s completed!\n", __func__);
2145 video_unregister_device(&dev->vdev);
2147 vb2_queue_release(&dev->vb_vidq);
2148 vb2_queue_release(&dev->vb_vbiq);