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 /* Advice that buffer was filled */
306 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
308 buf->vb.v4l2_buf.sequence = dev->frame_count++;
309 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
310 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
311 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
314 static inline void vbi_buffer_filled(struct au0828_dev *dev,
315 struct au0828_dmaqueue *dma_q,
316 struct au0828_buffer *buf)
318 /* Advice that buffer was filled */
319 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
321 buf->vb.v4l2_buf.sequence = dev->vbi_frame_count++;
322 buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
323 v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
324 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
328 * Identify the buffer header type and properly handles
330 static void au0828_copy_video(struct au0828_dev *dev,
331 struct au0828_dmaqueue *dma_q,
332 struct au0828_buffer *buf,
334 unsigned char *outp, unsigned long len)
336 void *fieldstart, *startwrite, *startread;
337 int linesdone, currlinedone, offset, lencopy, remain;
338 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
343 if (dma_q->pos + len > buf->length)
344 len = buf->length - dma_q->pos;
349 /* Interlaces frame */
353 fieldstart = outp + bytesperline;
355 linesdone = dma_q->pos / bytesperline;
356 currlinedone = dma_q->pos % bytesperline;
357 offset = linesdone * bytesperline * 2 + currlinedone;
358 startwrite = fieldstart + offset;
359 lencopy = bytesperline - currlinedone;
360 lencopy = lencopy > remain ? remain : lencopy;
362 if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
363 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
364 ((char *)startwrite + lencopy) -
365 ((char *)outp + buf->length));
366 remain = (char *)outp + buf->length - (char *)startwrite;
371 memcpy(startwrite, startread, lencopy);
376 startwrite += lencopy + bytesperline;
377 startread += lencopy;
378 if (bytesperline > remain)
381 lencopy = bytesperline;
383 if ((char *)startwrite + lencopy > (char *)outp +
385 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
386 ((char *)startwrite + lencopy) -
387 ((char *)outp + buf->length));
388 lencopy = remain = (char *)outp + buf->length -
394 memcpy(startwrite, startread, lencopy);
400 /* We have enough data to check for greenscreen */
401 if (outp[0] < 0x60 && outp[1440] < 0x60)
402 dev->greenscreen_detected = 1;
409 * video-buf generic routine to get the next available buffer
411 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
412 struct au0828_buffer **buf)
414 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
416 if (list_empty(&dma_q->active)) {
417 au0828_isocdbg("No active queue to serve\n");
418 dev->isoc_ctl.buf = NULL;
423 /* Get the next buffer */
424 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
425 /* Cleans up buffer - Useful for testing for frame/URB loss */
426 list_del(&(*buf)->list);
428 (*buf)->vb_buf = (*buf)->mem;
429 dev->isoc_ctl.buf = *buf;
434 static void au0828_copy_vbi(struct au0828_dev *dev,
435 struct au0828_dmaqueue *dma_q,
436 struct au0828_buffer *buf,
438 unsigned char *outp, unsigned long len)
440 unsigned char *startwrite, *startread;
445 au0828_isocdbg("dev is null\n");
450 au0828_isocdbg("dma_q is null\n");
456 au0828_isocdbg("p is null\n");
460 au0828_isocdbg("outp is null\n");
464 bytesperline = dev->vbi_width;
466 if (dma_q->pos + len > buf->length)
467 len = buf->length - dma_q->pos;
470 startwrite = outp + (dma_q->pos / 2);
472 /* Make sure the bottom field populates the second half of the frame */
473 if (buf->top_field == 0)
474 startwrite += bytesperline * dev->vbi_height;
476 for (i = 0; i < len; i += 2)
477 startwrite[j++] = startread[i+1];
484 * video-buf generic routine to get the next available VBI buffer
486 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
487 struct au0828_buffer **buf)
489 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
491 if (list_empty(&dma_q->active)) {
492 au0828_isocdbg("No active queue to serve\n");
493 dev->isoc_ctl.vbi_buf = NULL;
498 /* Get the next buffer */
499 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
500 /* Cleans up buffer - Useful for testing for frame/URB loss */
501 list_del(&(*buf)->list);
503 (*buf)->vb_buf = (*buf)->mem;
504 dev->isoc_ctl.vbi_buf = *buf;
509 * Controls the isoc copy of each urb packet
511 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
513 struct au0828_buffer *buf;
514 struct au0828_buffer *vbi_buf;
515 struct au0828_dmaqueue *dma_q = urb->context;
516 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
517 unsigned char *outp = NULL;
518 unsigned char *vbioutp = NULL;
519 int i, len = 0, rc = 1;
522 unsigned int vbi_field_size;
523 unsigned int remain, lencopy;
528 if ((dev->dev_state & DEV_DISCONNECTED) ||
529 (dev->dev_state & DEV_MISCONFIGURED))
532 if (urb->status < 0) {
533 print_err_status(dev, -1, urb->status);
534 if (urb->status == -ENOENT)
538 buf = dev->isoc_ctl.buf;
540 outp = vb2_plane_vaddr(&buf->vb, 0);
542 vbi_buf = dev->isoc_ctl.vbi_buf;
544 vbioutp = vb2_plane_vaddr(&vbi_buf->vb, 0);
546 for (i = 0; i < urb->number_of_packets; i++) {
547 int status = urb->iso_frame_desc[i].status;
550 print_err_status(dev, i, status);
551 if (urb->iso_frame_desc[i].status != -EPROTO)
555 if (urb->iso_frame_desc[i].actual_length <= 0)
558 if (urb->iso_frame_desc[i].actual_length >
560 au0828_isocdbg("packet bigger than packet size");
564 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
566 len = urb->iso_frame_desc[i].actual_length - 4;
572 au0828_isocdbg("Video frame %s\n",
573 (fbyte & 0x40) ? "odd" : "even");
577 vbi_buffer_filled(dev,
580 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
584 vbioutp = vb2_plane_vaddr(
589 buffer_filled(dev, dma_q, buf);
590 get_next_buf(dma_q, &buf);
594 outp = vb2_plane_vaddr(&buf->vb, 0);
596 /* As long as isoc traffic is arriving, keep
597 resetting the timer */
598 if (dev->vid_timeout_running)
599 mod_timer(&dev->vid_timeout,
600 jiffies + (HZ / 10));
601 if (dev->vbi_timeout_running)
602 mod_timer(&dev->vbi_timeout,
603 jiffies + (HZ / 10));
613 if (vbi_buf != NULL) {
615 vbi_buf->top_field = 1;
617 vbi_buf->top_field = 0;
625 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
626 if (dev->vbi_read < vbi_field_size) {
627 remain = vbi_field_size - dev->vbi_read;
634 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
639 dev->vbi_read += lencopy;
642 if (dev->vbi_read >= vbi_field_size && buf != NULL)
643 au0828_copy_video(dev, dma_q, buf, p, outp, len);
648 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
649 unsigned int *nbuffers, unsigned int *nplanes,
650 unsigned int sizes[], void *alloc_ctxs[])
652 struct au0828_dev *dev = vb2_get_drv_priv(vq);
653 unsigned long img_size = dev->height * dev->bytesperline;
656 size = fmt ? fmt->fmt.pix.sizeimage : img_size;
667 buffer_prepare(struct vb2_buffer *vb)
669 struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb);
670 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
672 buf->length = dev->height * dev->bytesperline;
674 if (vb2_plane_size(vb, 0) < buf->length) {
675 pr_err("%s data will not fit into plane (%lu < %lu)\n",
676 __func__, vb2_plane_size(vb, 0), buf->length);
679 vb2_set_plane_payload(&buf->vb, 0, buf->length);
684 buffer_queue(struct vb2_buffer *vb)
686 struct au0828_buffer *buf = container_of(vb,
687 struct au0828_buffer,
689 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
690 struct au0828_dmaqueue *vidq = &dev->vidq;
691 unsigned long flags = 0;
693 buf->mem = vb2_plane_vaddr(vb, 0);
694 buf->length = vb2_plane_size(vb, 0);
696 spin_lock_irqsave(&dev->slock, flags);
697 list_add_tail(&buf->list, &vidq->active);
698 spin_unlock_irqrestore(&dev->slock, flags);
701 static int au0828_i2s_init(struct au0828_dev *dev)
703 /* Enable i2s mode */
704 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
709 * Auvitek au0828 analog stream enable
711 static int au0828_analog_stream_enable(struct au0828_dev *d)
713 struct usb_interface *iface;
716 dprintk(1, "au0828_analog_stream_enable called\n");
718 iface = usb_ifnum_to_if(d->usbdev, 0);
719 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
720 dprintk(1, "Changing intf#0 to alt 5\n");
721 /* set au0828 interface0 to AS5 here again */
722 ret = usb_set_interface(d->usbdev, 0, 5);
724 pr_info("Au0828 can't set alt setting to 5!\n");
729 h = d->height / 2 + 2;
732 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
733 au0828_writereg(d, 0x106, 0x00);
735 au0828_writereg(d, 0x110, 0x00);
736 au0828_writereg(d, 0x111, 0x00);
737 au0828_writereg(d, 0x114, w & 0xff);
738 au0828_writereg(d, 0x115, w >> 8);
740 au0828_writereg(d, 0x112, 0x00);
741 au0828_writereg(d, 0x113, 0x00);
742 au0828_writereg(d, 0x116, h & 0xff);
743 au0828_writereg(d, 0x117, h >> 8);
744 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
749 static int au0828_analog_stream_disable(struct au0828_dev *d)
751 dprintk(1, "au0828_analog_stream_disable called\n");
752 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
756 static void au0828_analog_stream_reset(struct au0828_dev *dev)
758 dprintk(1, "au0828_analog_stream_reset called\n");
759 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
761 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
765 * Some operations needs to stop current streaming
767 static int au0828_stream_interrupt(struct au0828_dev *dev)
771 dev->stream_state = STREAM_INTERRUPT;
772 if (dev->dev_state == DEV_DISCONNECTED)
775 dev->dev_state = DEV_MISCONFIGURED;
776 dprintk(1, "%s device is misconfigured!\n", __func__);
782 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
784 struct au0828_dev *dev = vb2_get_drv_priv(vq);
787 dprintk(1, "au0828_start_analog_streaming called %d\n",
788 dev->streaming_users);
790 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
791 dev->frame_count = 0;
793 dev->vbi_frame_count = 0;
795 if (dev->streaming_users == 0) {
796 /* If we were doing ac97 instead of i2s, it would go here...*/
797 au0828_i2s_init(dev);
798 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
799 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
802 pr_info("au0828_init_isoc failed\n");
806 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
807 v4l2_device_call_all(&dev->v4l2_dev, 0, video,
809 dev->vid_timeout_running = 1;
810 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
811 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
812 dev->vbi_timeout_running = 1;
813 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
816 dev->streaming_users++;
820 static void au0828_stop_streaming(struct vb2_queue *vq)
822 struct au0828_dev *dev = vb2_get_drv_priv(vq);
823 struct au0828_dmaqueue *vidq = &dev->vidq;
824 unsigned long flags = 0;
826 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
828 if (dev->streaming_users-- == 1)
829 au0828_uninit_isoc(dev);
831 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
832 dev->vid_timeout_running = 0;
833 del_timer_sync(&dev->vid_timeout);
835 spin_lock_irqsave(&dev->slock, flags);
836 if (dev->isoc_ctl.buf != NULL) {
837 vb2_buffer_done(&dev->isoc_ctl.buf->vb, VB2_BUF_STATE_ERROR);
838 dev->isoc_ctl.buf = NULL;
840 while (!list_empty(&vidq->active)) {
841 struct au0828_buffer *buf;
843 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
844 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
845 list_del(&buf->list);
847 spin_unlock_irqrestore(&dev->slock, flags);
850 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
852 struct au0828_dev *dev = vb2_get_drv_priv(vq);
853 struct au0828_dmaqueue *vbiq = &dev->vbiq;
854 unsigned long flags = 0;
856 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
857 dev->streaming_users);
859 if (dev->streaming_users-- == 1)
860 au0828_uninit_isoc(dev);
862 spin_lock_irqsave(&dev->slock, flags);
863 if (dev->isoc_ctl.vbi_buf != NULL) {
864 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb,
865 VB2_BUF_STATE_ERROR);
866 dev->isoc_ctl.vbi_buf = NULL;
868 while (!list_empty(&vbiq->active)) {
869 struct au0828_buffer *buf;
871 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
872 list_del(&buf->list);
873 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
875 spin_unlock_irqrestore(&dev->slock, flags);
877 dev->vbi_timeout_running = 0;
878 del_timer_sync(&dev->vbi_timeout);
881 static struct vb2_ops au0828_video_qops = {
882 .queue_setup = queue_setup,
883 .buf_prepare = buffer_prepare,
884 .buf_queue = buffer_queue,
885 .start_streaming = au0828_start_analog_streaming,
886 .stop_streaming = au0828_stop_streaming,
887 .wait_prepare = vb2_ops_wait_prepare,
888 .wait_finish = vb2_ops_wait_finish,
891 /* ------------------------------------------------------------------
893 ------------------------------------------------------------------*/
895 * au0828_analog_unregister
896 * unregister v4l2 devices
898 void au0828_analog_unregister(struct au0828_dev *dev)
900 dprintk(1, "au0828_analog_unregister called\n");
901 mutex_lock(&au0828_sysfs_lock);
904 video_unregister_device(dev->vdev);
906 video_unregister_device(dev->vbi_dev);
908 mutex_unlock(&au0828_sysfs_lock);
911 /* This function ensures that video frames continue to be delivered even if
912 the ITU-656 input isn't receiving any data (thereby preventing applications
913 such as tvtime from hanging) */
914 static void au0828_vid_buffer_timeout(unsigned long data)
916 struct au0828_dev *dev = (struct au0828_dev *) data;
917 struct au0828_dmaqueue *dma_q = &dev->vidq;
918 struct au0828_buffer *buf;
919 unsigned char *vid_data;
920 unsigned long flags = 0;
922 spin_lock_irqsave(&dev->slock, flags);
924 buf = dev->isoc_ctl.buf;
926 vid_data = vb2_plane_vaddr(&buf->vb, 0);
927 memset(vid_data, 0x00, buf->length); /* Blank green frame */
928 buffer_filled(dev, dma_q, buf);
930 get_next_buf(dma_q, &buf);
932 if (dev->vid_timeout_running == 1)
933 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
935 spin_unlock_irqrestore(&dev->slock, flags);
938 static void au0828_vbi_buffer_timeout(unsigned long data)
940 struct au0828_dev *dev = (struct au0828_dev *) data;
941 struct au0828_dmaqueue *dma_q = &dev->vbiq;
942 struct au0828_buffer *buf;
943 unsigned char *vbi_data;
944 unsigned long flags = 0;
946 spin_lock_irqsave(&dev->slock, flags);
948 buf = dev->isoc_ctl.vbi_buf;
950 vbi_data = vb2_plane_vaddr(&buf->vb, 0);
951 memset(vbi_data, 0x00, buf->length);
952 vbi_buffer_filled(dev, dma_q, buf);
954 vbi_get_next_buf(dma_q, &buf);
956 if (dev->vbi_timeout_running == 1)
957 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
958 spin_unlock_irqrestore(&dev->slock, flags);
961 static int au0828_v4l2_open(struct file *filp)
963 struct au0828_dev *dev = video_drvdata(filp);
967 "%s called std_set %d dev_state %d stream users %d users %d\n",
968 __func__, dev->std_set_in_tuner_core, dev->dev_state,
969 dev->streaming_users, dev->users);
971 if (mutex_lock_interruptible(&dev->lock))
974 ret = v4l2_fh_open(filp);
976 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
978 mutex_unlock(&dev->lock);
982 if (dev->users == 0) {
983 au0828_analog_stream_enable(dev);
984 au0828_analog_stream_reset(dev);
985 dev->stream_state = STREAM_OFF;
986 dev->dev_state |= DEV_INITIALIZED;
989 mutex_unlock(&dev->lock);
993 static int au0828_v4l2_close(struct file *filp)
996 struct au0828_dev *dev = video_drvdata(filp);
997 struct video_device *vdev = video_devdata(filp);
1000 "%s called std_set %d dev_state %d stream users %d users %d\n",
1001 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1002 dev->streaming_users, dev->users);
1004 mutex_lock(&dev->lock);
1005 if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
1006 /* Cancel timeout thread in case they didn't call streamoff */
1007 dev->vid_timeout_running = 0;
1008 del_timer_sync(&dev->vid_timeout);
1009 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1010 dev->vbi_timeout_running) {
1011 /* Cancel timeout thread in case they didn't call streamoff */
1012 dev->vbi_timeout_running = 0;
1013 del_timer_sync(&dev->vbi_timeout);
1016 if (dev->dev_state == DEV_DISCONNECTED)
1019 if (dev->users == 1) {
1020 /* Save some power by putting tuner to sleep */
1021 v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
1022 dev->std_set_in_tuner_core = 0;
1024 /* When close the device, set the usb intf0 into alt0 to free
1026 ret = usb_set_interface(dev->usbdev, 0, 0);
1028 pr_info("Au0828 can't set alternate to 0!\n");
1031 _vb2_fop_release(filp, NULL);
1033 mutex_unlock(&dev->lock);
1037 /* Must be called with dev->lock held */
1038 static void au0828_init_tuner(struct au0828_dev *dev)
1040 struct v4l2_frequency f = {
1041 .frequency = dev->ctrl_freq,
1042 .type = V4L2_TUNER_ANALOG_TV,
1045 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1046 dev->std_set_in_tuner_core, dev->dev_state);
1048 if (dev->std_set_in_tuner_core)
1050 dev->std_set_in_tuner_core = 1;
1051 i2c_gate_ctrl(dev, 1);
1052 /* If we've never sent the standard in tuner core, do so now.
1053 We don't do this at device probe because we don't want to
1054 incur the cost of a firmware load */
1055 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1056 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1057 i2c_gate_ctrl(dev, 0);
1060 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1061 struct v4l2_format *format)
1064 int width = format->fmt.pix.width;
1065 int height = format->fmt.pix.height;
1067 /* If they are demanding a format other than the one we support,
1068 bail out (tvtime asks for UYVY and then retries with YUYV) */
1069 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1072 /* format->fmt.pix.width only support 720 and height 480 */
1078 format->fmt.pix.width = width;
1079 format->fmt.pix.height = height;
1080 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1081 format->fmt.pix.bytesperline = width * 2;
1082 format->fmt.pix.sizeimage = width * height * 2;
1083 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1084 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1085 format->fmt.pix.priv = 0;
1087 if (cmd == VIDIOC_TRY_FMT)
1090 /* maybe set new image format, driver current only support 720*480 */
1092 dev->height = height;
1093 dev->frame_size = width * height * 2;
1094 dev->field_size = width * height;
1095 dev->bytesperline = width * 2;
1097 if (dev->stream_state == STREAM_ON) {
1098 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1099 ret = au0828_stream_interrupt(dev);
1101 dprintk(1, "error interrupting video stream!\n");
1106 au0828_analog_stream_enable(dev);
1111 static int vidioc_querycap(struct file *file, void *priv,
1112 struct v4l2_capability *cap)
1114 struct video_device *vdev = video_devdata(file);
1115 struct au0828_dev *dev = video_drvdata(file);
1117 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1118 dev->std_set_in_tuner_core, dev->dev_state);
1120 strlcpy(cap->driver, "au0828", sizeof(cap->driver));
1121 strlcpy(cap->card, dev->board.name, sizeof(cap->card));
1122 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1124 /* set the device capabilities */
1125 cap->device_caps = V4L2_CAP_AUDIO |
1126 V4L2_CAP_READWRITE |
1127 V4L2_CAP_STREAMING |
1129 if (vdev->vfl_type == VFL_TYPE_GRABBER)
1130 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1132 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1133 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
1134 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
1138 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1139 struct v4l2_fmtdesc *f)
1144 dprintk(1, "%s called\n", __func__);
1146 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1147 strcpy(f->description, "Packed YUV2");
1150 f->pixelformat = V4L2_PIX_FMT_UYVY;
1155 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1156 struct v4l2_format *f)
1158 struct au0828_dev *dev = video_drvdata(file);
1160 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1161 dev->std_set_in_tuner_core, dev->dev_state);
1163 f->fmt.pix.width = dev->width;
1164 f->fmt.pix.height = dev->height;
1165 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1166 f->fmt.pix.bytesperline = dev->bytesperline;
1167 f->fmt.pix.sizeimage = dev->frame_size;
1168 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1169 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1170 f->fmt.pix.priv = 0;
1174 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1175 struct v4l2_format *f)
1177 struct au0828_dev *dev = video_drvdata(file);
1179 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1180 dev->std_set_in_tuner_core, dev->dev_state);
1182 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1185 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1186 struct v4l2_format *f)
1188 struct au0828_dev *dev = video_drvdata(file);
1191 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1192 dev->std_set_in_tuner_core, dev->dev_state);
1194 rc = check_dev(dev);
1198 if (vb2_is_busy(&dev->vb_vidq)) {
1199 pr_info("%s queue busy\n", __func__);
1204 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1209 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1211 struct au0828_dev *dev = video_drvdata(file);
1213 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1214 dev->std_set_in_tuner_core, dev->dev_state);
1216 if (norm == dev->std)
1219 if (dev->streaming_users > 0)
1224 au0828_init_tuner(dev);
1226 i2c_gate_ctrl(dev, 1);
1229 * FIXME: when we support something other than 60Hz standards,
1230 * we are going to have to make the au0828 bridge adjust the size
1231 * of its capture buffer, which is currently hardcoded at 720x480
1234 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1236 i2c_gate_ctrl(dev, 0);
1241 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1243 struct au0828_dev *dev = video_drvdata(file);
1245 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1246 dev->std_set_in_tuner_core, dev->dev_state);
1252 static int vidioc_enum_input(struct file *file, void *priv,
1253 struct v4l2_input *input)
1255 struct au0828_dev *dev = video_drvdata(file);
1258 static const char *inames[] = {
1259 [AU0828_VMUX_UNDEFINED] = "Undefined",
1260 [AU0828_VMUX_COMPOSITE] = "Composite",
1261 [AU0828_VMUX_SVIDEO] = "S-Video",
1262 [AU0828_VMUX_CABLE] = "Cable TV",
1263 [AU0828_VMUX_TELEVISION] = "Television",
1264 [AU0828_VMUX_DVB] = "DVB",
1265 [AU0828_VMUX_DEBUG] = "tv debug"
1268 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1269 dev->std_set_in_tuner_core, dev->dev_state);
1273 if (tmp >= AU0828_MAX_INPUT)
1275 if (AUVI_INPUT(tmp).type == 0)
1279 strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
1280 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1281 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1282 input->type |= V4L2_INPUT_TYPE_TUNER;
1283 input->audioset = 1;
1285 input->type |= V4L2_INPUT_TYPE_CAMERA;
1286 input->audioset = 2;
1289 input->std = dev->vdev->tvnorms;
1294 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1296 struct au0828_dev *dev = video_drvdata(file);
1298 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1299 dev->std_set_in_tuner_core, dev->dev_state);
1301 *i = dev->ctrl_input;
1305 static void au0828_s_input(struct au0828_dev *dev, int index)
1309 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1310 dev->std_set_in_tuner_core, dev->dev_state);
1312 switch (AUVI_INPUT(index).type) {
1313 case AU0828_VMUX_SVIDEO:
1314 dev->input_type = AU0828_VMUX_SVIDEO;
1315 dev->ctrl_ainput = 1;
1317 case AU0828_VMUX_COMPOSITE:
1318 dev->input_type = AU0828_VMUX_COMPOSITE;
1319 dev->ctrl_ainput = 1;
1321 case AU0828_VMUX_TELEVISION:
1322 dev->input_type = AU0828_VMUX_TELEVISION;
1323 dev->ctrl_ainput = 0;
1326 dprintk(1, "unknown input type set [%d]\n",
1327 AUVI_INPUT(index).type);
1331 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1332 AUVI_INPUT(index).vmux, 0, 0);
1334 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1336 if (AUVI_INPUT(i).audio_setup == NULL)
1344 (AUVI_INPUT(i).audio_setup)(dev, enable);
1346 /* Make sure we leave it turned on if some
1347 other input is routed to this callback */
1348 if ((AUVI_INPUT(i).audio_setup) !=
1349 ((AUVI_INPUT(index).audio_setup))) {
1350 (AUVI_INPUT(i).audio_setup)(dev, enable);
1355 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1356 AUVI_INPUT(index).amux, 0, 0);
1359 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1361 struct au0828_dev *dev = video_drvdata(file);
1363 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1365 if (index >= AU0828_MAX_INPUT)
1367 if (AUVI_INPUT(index).type == 0)
1369 dev->ctrl_input = index;
1370 au0828_s_input(dev, index);
1374 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1379 dprintk(1, "%s called\n", __func__);
1382 strcpy(a->name, "Television");
1384 strcpy(a->name, "Line in");
1386 a->capability = V4L2_AUDCAP_STEREO;
1390 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1392 struct au0828_dev *dev = video_drvdata(file);
1394 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1395 dev->std_set_in_tuner_core, dev->dev_state);
1397 a->index = dev->ctrl_ainput;
1399 strcpy(a->name, "Television");
1401 strcpy(a->name, "Line in");
1403 a->capability = V4L2_AUDCAP_STEREO;
1407 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1409 struct au0828_dev *dev = video_drvdata(file);
1411 if (a->index != dev->ctrl_ainput)
1414 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1415 dev->std_set_in_tuner_core, dev->dev_state);
1419 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1421 struct au0828_dev *dev = video_drvdata(file);
1426 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1427 dev->std_set_in_tuner_core, dev->dev_state);
1429 strcpy(t->name, "Auvitek tuner");
1431 au0828_init_tuner(dev);
1432 i2c_gate_ctrl(dev, 1);
1433 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1434 i2c_gate_ctrl(dev, 0);
1438 static int vidioc_s_tuner(struct file *file, void *priv,
1439 const struct v4l2_tuner *t)
1441 struct au0828_dev *dev = video_drvdata(file);
1446 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1447 dev->std_set_in_tuner_core, dev->dev_state);
1449 au0828_init_tuner(dev);
1450 i2c_gate_ctrl(dev, 1);
1451 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1452 i2c_gate_ctrl(dev, 0);
1454 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1461 static int vidioc_g_frequency(struct file *file, void *priv,
1462 struct v4l2_frequency *freq)
1464 struct au0828_dev *dev = video_drvdata(file);
1466 if (freq->tuner != 0)
1468 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1469 dev->std_set_in_tuner_core, dev->dev_state);
1470 freq->frequency = dev->ctrl_freq;
1474 static int vidioc_s_frequency(struct file *file, void *priv,
1475 const struct v4l2_frequency *freq)
1477 struct au0828_dev *dev = video_drvdata(file);
1478 struct v4l2_frequency new_freq = *freq;
1480 if (freq->tuner != 0)
1483 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1484 dev->std_set_in_tuner_core, dev->dev_state);
1486 au0828_init_tuner(dev);
1487 i2c_gate_ctrl(dev, 1);
1489 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1490 /* Get the actual set (and possibly clamped) frequency */
1491 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1492 dev->ctrl_freq = new_freq.frequency;
1494 i2c_gate_ctrl(dev, 0);
1496 au0828_analog_stream_reset(dev);
1502 /* RAW VBI ioctls */
1504 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1505 struct v4l2_format *format)
1507 struct au0828_dev *dev = video_drvdata(file);
1509 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1510 dev->std_set_in_tuner_core, dev->dev_state);
1512 format->fmt.vbi.samples_per_line = dev->vbi_width;
1513 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1514 format->fmt.vbi.offset = 0;
1515 format->fmt.vbi.flags = 0;
1516 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1518 format->fmt.vbi.count[0] = dev->vbi_height;
1519 format->fmt.vbi.count[1] = dev->vbi_height;
1520 format->fmt.vbi.start[0] = 21;
1521 format->fmt.vbi.start[1] = 284;
1522 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1527 static int vidioc_cropcap(struct file *file, void *priv,
1528 struct v4l2_cropcap *cc)
1530 struct au0828_dev *dev = video_drvdata(file);
1532 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1535 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1536 dev->std_set_in_tuner_core, dev->dev_state);
1538 cc->bounds.left = 0;
1540 cc->bounds.width = dev->width;
1541 cc->bounds.height = dev->height;
1543 cc->defrect = cc->bounds;
1545 cc->pixelaspect.numerator = 54;
1546 cc->pixelaspect.denominator = 59;
1551 #ifdef CONFIG_VIDEO_ADV_DEBUG
1552 static int vidioc_g_register(struct file *file, void *priv,
1553 struct v4l2_dbg_register *reg)
1555 struct au0828_dev *dev = video_drvdata(file);
1557 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1558 dev->std_set_in_tuner_core, dev->dev_state);
1560 reg->val = au0828_read(dev, reg->reg);
1565 static int vidioc_s_register(struct file *file, void *priv,
1566 const struct v4l2_dbg_register *reg)
1568 struct au0828_dev *dev = video_drvdata(file);
1570 dprintk(1, "%s called std_set %d dev_state %d\n", __func__,
1571 dev->std_set_in_tuner_core, dev->dev_state);
1573 return au0828_writereg(dev, reg->reg, reg->val);
1577 static int vidioc_log_status(struct file *file, void *fh)
1579 struct video_device *vdev = video_devdata(file);
1581 dprintk(1, "%s called\n", __func__);
1583 v4l2_ctrl_log_status(file, fh);
1584 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1588 void au0828_v4l2_suspend(struct au0828_dev *dev)
1593 pr_info("stopping V4L2\n");
1595 if (dev->stream_state == STREAM_ON) {
1596 pr_info("stopping V4L2 active URBs\n");
1597 au0828_analog_stream_disable(dev);
1599 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1600 urb = dev->isoc_ctl.urb[i];
1602 if (!irqs_disabled())
1605 usb_unlink_urb(urb);
1610 if (dev->vid_timeout_running)
1611 del_timer_sync(&dev->vid_timeout);
1612 if (dev->vbi_timeout_running)
1613 del_timer_sync(&dev->vbi_timeout);
1616 void au0828_v4l2_resume(struct au0828_dev *dev)
1620 pr_info("restarting V4L2\n");
1622 if (dev->stream_state == STREAM_ON) {
1623 au0828_stream_interrupt(dev);
1624 au0828_init_tuner(dev);
1627 if (dev->vid_timeout_running)
1628 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1629 if (dev->vbi_timeout_running)
1630 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1632 /* If we were doing ac97 instead of i2s, it would go here...*/
1633 au0828_i2s_init(dev);
1635 au0828_analog_stream_enable(dev);
1637 if (!(dev->stream_state == STREAM_ON)) {
1638 au0828_analog_stream_reset(dev);
1640 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1641 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1643 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1645 au0828_uninit_isoc(dev);
1651 static struct v4l2_file_operations au0828_v4l_fops = {
1652 .owner = THIS_MODULE,
1653 .open = au0828_v4l2_open,
1654 .release = au0828_v4l2_close,
1655 .read = vb2_fop_read,
1656 .poll = vb2_fop_poll,
1657 .mmap = vb2_fop_mmap,
1658 .unlocked_ioctl = video_ioctl2,
1661 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1662 .vidioc_querycap = vidioc_querycap,
1663 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1664 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1665 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1666 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1667 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1668 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1669 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1670 .vidioc_enumaudio = vidioc_enumaudio,
1671 .vidioc_g_audio = vidioc_g_audio,
1672 .vidioc_s_audio = vidioc_s_audio,
1673 .vidioc_cropcap = vidioc_cropcap,
1675 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1676 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1677 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1678 .vidioc_querybuf = vb2_ioctl_querybuf,
1679 .vidioc_qbuf = vb2_ioctl_qbuf,
1680 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1681 .vidioc_expbuf = vb2_ioctl_expbuf,
1683 .vidioc_s_std = vidioc_s_std,
1684 .vidioc_g_std = vidioc_g_std,
1685 .vidioc_enum_input = vidioc_enum_input,
1686 .vidioc_g_input = vidioc_g_input,
1687 .vidioc_s_input = vidioc_s_input,
1689 .vidioc_streamon = vb2_ioctl_streamon,
1690 .vidioc_streamoff = vb2_ioctl_streamoff,
1692 .vidioc_g_tuner = vidioc_g_tuner,
1693 .vidioc_s_tuner = vidioc_s_tuner,
1694 .vidioc_g_frequency = vidioc_g_frequency,
1695 .vidioc_s_frequency = vidioc_s_frequency,
1696 #ifdef CONFIG_VIDEO_ADV_DEBUG
1697 .vidioc_g_register = vidioc_g_register,
1698 .vidioc_s_register = vidioc_s_register,
1700 .vidioc_log_status = vidioc_log_status,
1701 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1702 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1705 static const struct video_device au0828_video_template = {
1706 .fops = &au0828_v4l_fops,
1707 .release = video_device_release,
1708 .ioctl_ops = &video_ioctl_ops,
1709 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1712 static int au0828_vb2_setup(struct au0828_dev *dev)
1715 struct vb2_queue *q;
1717 /* Setup Videobuf2 for Video capture */
1719 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1720 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1721 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1723 q->buf_struct_size = sizeof(struct au0828_buffer);
1724 q->ops = &au0828_video_qops;
1725 q->mem_ops = &vb2_vmalloc_memops;
1727 rc = vb2_queue_init(q);
1731 /* Setup Videobuf2 for VBI capture */
1733 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1734 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1735 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1737 q->buf_struct_size = sizeof(struct au0828_buffer);
1738 q->ops = &au0828_vbi_qops;
1739 q->mem_ops = &vb2_vmalloc_memops;
1741 rc = vb2_queue_init(q);
1748 /**************************************************************************/
1750 int au0828_analog_register(struct au0828_dev *dev,
1751 struct usb_interface *interface)
1753 int retval = -ENOMEM;
1754 struct usb_host_interface *iface_desc;
1755 struct usb_endpoint_descriptor *endpoint;
1758 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1759 interface->cur_altsetting->desc.bInterfaceNumber);
1761 /* set au0828 usb interface0 to as5 */
1762 retval = usb_set_interface(dev->usbdev,
1763 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1765 pr_info("Failure setting usb interface0 to as5\n");
1769 /* Figure out which endpoint has the isoc interface */
1770 iface_desc = interface->cur_altsetting;
1771 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1772 endpoint = &iface_desc->endpoint[i].desc;
1773 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1775 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1776 == USB_ENDPOINT_XFER_ISOC)) {
1778 /* we find our isoc in endpoint */
1779 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1780 dev->max_pkt_size = (tmp & 0x07ff) *
1781 (((tmp & 0x1800) >> 11) + 1);
1782 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1784 "Found isoc endpoint 0x%02x, max size = %d\n",
1785 dev->isoc_in_endpointaddr, dev->max_pkt_size);
1788 if (!(dev->isoc_in_endpointaddr)) {
1789 pr_info("Could not locate isoc endpoint\n");
1793 init_waitqueue_head(&dev->open);
1794 spin_lock_init(&dev->slock);
1796 /* init video dma queues */
1797 INIT_LIST_HEAD(&dev->vidq.active);
1798 INIT_LIST_HEAD(&dev->vbiq.active);
1800 setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
1801 (unsigned long)dev);
1802 setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
1803 (unsigned long)dev);
1805 dev->width = NTSC_STD_W;
1806 dev->height = NTSC_STD_H;
1807 dev->field_size = dev->width * dev->height;
1808 dev->frame_size = dev->field_size << 1;
1809 dev->bytesperline = dev->width << 1;
1810 dev->vbi_width = 720;
1811 dev->vbi_height = 1;
1812 dev->ctrl_ainput = 0;
1813 dev->ctrl_freq = 960;
1814 dev->std = V4L2_STD_NTSC_M;
1815 au0828_s_input(dev, 0);
1817 /* allocate and fill v4l2 video struct */
1818 dev->vdev = video_device_alloc();
1819 if (NULL == dev->vdev) {
1820 dprintk(1, "Can't allocate video_device.\n");
1824 /* allocate the VBI struct */
1825 dev->vbi_dev = video_device_alloc();
1826 if (NULL == dev->vbi_dev) {
1827 dprintk(1, "Can't allocate vbi_device.\n");
1832 mutex_init(&dev->vb_queue_lock);
1833 mutex_init(&dev->vb_vbi_queue_lock);
1835 /* Fill the video capture device struct */
1836 *dev->vdev = au0828_video_template;
1837 dev->vdev->v4l2_dev = &dev->v4l2_dev;
1838 dev->vdev->lock = &dev->lock;
1839 dev->vdev->queue = &dev->vb_vidq;
1840 dev->vdev->queue->lock = &dev->vb_queue_lock;
1841 strcpy(dev->vdev->name, "au0828a video");
1843 /* Setup the VBI device */
1844 *dev->vbi_dev = au0828_video_template;
1845 dev->vbi_dev->v4l2_dev = &dev->v4l2_dev;
1846 dev->vbi_dev->lock = &dev->lock;
1847 dev->vbi_dev->queue = &dev->vb_vbiq;
1848 dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
1849 strcpy(dev->vbi_dev->name, "au0828a vbi");
1851 /* initialize videobuf2 stuff */
1852 retval = au0828_vb2_setup(dev);
1854 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
1860 /* Register the v4l2 device */
1861 video_set_drvdata(dev->vdev, dev);
1862 retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1);
1864 dprintk(1, "unable to register video device (error = %d).\n",
1870 /* Register the vbi device */
1871 video_set_drvdata(dev->vbi_dev, dev);
1872 retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1);
1874 dprintk(1, "unable to register vbi device (error = %d).\n",
1877 goto err_reg_vbi_dev;
1880 dprintk(1, "%s completed!\n", __func__);
1885 video_unregister_device(dev->vdev);
1887 vb2_queue_release(&dev->vb_vidq);
1888 vb2_queue_release(&dev->vb_vbiq);
1890 video_device_release(dev->vbi_dev);
1892 video_device_release(dev->vdev);