2 * Auvitek AU0828 USB Bridge (Analog video support)
4 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
5 * Copyright (C) 2005-2008 Auvitek International, Ltd.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * As published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * VBI support is not yet working
26 * The hardware scaler supported is unimplemented
27 * AC97 audio support is unimplemented (only i2s audio mode)
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/device.h>
35 #include <linux/suspend.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-event.h>
39 #include <media/v4l2-chip-ident.h>
40 #include <media/tuner.h>
42 #include "au0828-reg.h"
44 static DEFINE_MUTEX(au0828_sysfs_lock
);
46 /* ------------------------------------------------------------------
48 ------------------------------------------------------------------*/
50 static unsigned int isoc_debug
;
51 module_param(isoc_debug
, int, 0644);
52 MODULE_PARM_DESC(isoc_debug
, "enable debug messages [isoc transfers]");
54 #define au0828_isocdbg(fmt, arg...) \
57 printk(KERN_INFO "au0828 %s :"fmt, \
62 static inline void print_err_status(struct au0828_dev
*dev
,
63 int packet
, int status
)
65 char *errmsg
= "Unknown";
69 errmsg
= "unlinked synchronuously";
72 errmsg
= "unlinked asynchronuously";
75 errmsg
= "Buffer error (overrun)";
78 errmsg
= "Stalled (device not responding)";
81 errmsg
= "Babble (bad cable?)";
84 errmsg
= "Bit-stuff error (bad cable?)";
87 errmsg
= "CRC/Timeout (could be anything)";
90 errmsg
= "Device does not respond";
94 au0828_isocdbg("URB status %d [%s].\n", status
, errmsg
);
96 au0828_isocdbg("URB packet %d, status %d [%s].\n",
97 packet
, status
, errmsg
);
101 static int check_dev(struct au0828_dev
*dev
)
103 if (dev
->dev_state
& DEV_DISCONNECTED
) {
104 printk(KERN_INFO
"v4l2 ioctl: device not present\n");
108 if (dev
->dev_state
& DEV_MISCONFIGURED
) {
109 printk(KERN_INFO
"v4l2 ioctl: device is misconfigured; "
110 "close and open it again\n");
117 * IRQ callback, called by URB callback
119 static void au0828_irq_callback(struct urb
*urb
)
121 struct au0828_dmaqueue
*dma_q
= urb
->context
;
122 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
123 unsigned long flags
= 0;
126 switch (urb
->status
) {
127 case 0: /* success */
128 case -ETIMEDOUT
: /* NAK */
130 case -ECONNRESET
: /* kill */
133 au0828_isocdbg("au0828_irq_callback called: status kill\n");
135 default: /* unknown error */
136 au0828_isocdbg("urb completition error %d.\n", urb
->status
);
140 /* Copy data from URB */
141 spin_lock_irqsave(&dev
->slock
, flags
);
142 dev
->isoc_ctl
.isoc_copy(dev
, urb
);
143 spin_unlock_irqrestore(&dev
->slock
, flags
);
145 /* Reset urb buffers */
146 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
147 urb
->iso_frame_desc
[i
].status
= 0;
148 urb
->iso_frame_desc
[i
].actual_length
= 0;
152 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
154 au0828_isocdbg("urb resubmit failed (error=%i)\n",
160 * Stop and Deallocate URBs
162 static void au0828_uninit_isoc(struct au0828_dev
*dev
)
167 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
169 dev
->isoc_ctl
.nfields
= -1;
170 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
171 urb
= dev
->isoc_ctl
.urb
[i
];
173 if (!irqs_disabled())
178 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
179 usb_free_coherent(dev
->usbdev
,
180 urb
->transfer_buffer_length
,
181 dev
->isoc_ctl
.transfer_buffer
[i
],
185 dev
->isoc_ctl
.urb
[i
] = NULL
;
187 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
190 kfree(dev
->isoc_ctl
.urb
);
191 kfree(dev
->isoc_ctl
.transfer_buffer
);
193 dev
->isoc_ctl
.urb
= NULL
;
194 dev
->isoc_ctl
.transfer_buffer
= NULL
;
195 dev
->isoc_ctl
.num_bufs
= 0;
199 * Allocate URBs and start IRQ
201 static int au0828_init_isoc(struct au0828_dev
*dev
, int max_packets
,
202 int num_bufs
, int max_pkt_size
,
203 int (*isoc_copy
) (struct au0828_dev
*dev
, struct urb
*urb
))
205 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
212 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
214 /* De-allocates all pending stuff */
215 au0828_uninit_isoc(dev
);
217 dev
->isoc_ctl
.isoc_copy
= isoc_copy
;
218 dev
->isoc_ctl
.num_bufs
= num_bufs
;
220 dev
->isoc_ctl
.urb
= kzalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
221 if (!dev
->isoc_ctl
.urb
) {
222 au0828_isocdbg("cannot alloc memory for usb buffers\n");
226 dev
->isoc_ctl
.transfer_buffer
= kzalloc(sizeof(void *)*num_bufs
,
228 if (!dev
->isoc_ctl
.transfer_buffer
) {
229 au0828_isocdbg("cannot allocate memory for usb transfer\n");
230 kfree(dev
->isoc_ctl
.urb
);
234 dev
->isoc_ctl
.max_pkt_size
= max_pkt_size
;
235 dev
->isoc_ctl
.buf
= NULL
;
237 sb_size
= max_packets
* dev
->isoc_ctl
.max_pkt_size
;
239 /* allocate urbs and transfer buffers */
240 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
241 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
243 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i
);
244 au0828_uninit_isoc(dev
);
247 dev
->isoc_ctl
.urb
[i
] = urb
;
249 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->usbdev
,
250 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
251 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
252 printk("unable to allocate %i bytes for transfer"
255 in_interrupt() ? " while in int" : "");
256 au0828_uninit_isoc(dev
);
259 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
261 pipe
= usb_rcvisocpipe(dev
->usbdev
,
262 dev
->isoc_in_endpointaddr
),
264 usb_fill_int_urb(urb
, dev
->usbdev
, pipe
,
265 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
266 au0828_irq_callback
, dma_q
, 1);
268 urb
->number_of_packets
= max_packets
;
269 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
272 for (j
= 0; j
< max_packets
; j
++) {
273 urb
->iso_frame_desc
[j
].offset
= k
;
274 urb
->iso_frame_desc
[j
].length
=
275 dev
->isoc_ctl
.max_pkt_size
;
276 k
+= dev
->isoc_ctl
.max_pkt_size
;
280 init_waitqueue_head(&dma_q
->wq
);
282 /* submit urbs and enables IRQ */
283 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
284 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
286 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
288 au0828_uninit_isoc(dev
);
297 * Announces that a buffer were filled and request the next
299 static inline void buffer_filled(struct au0828_dev
*dev
,
300 struct au0828_dmaqueue
*dma_q
,
301 struct au0828_buffer
*buf
)
303 /* Advice that buffer was filled */
304 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
306 buf
->vb
.state
= VIDEOBUF_DONE
;
307 buf
->vb
.field_count
++;
308 v4l2_get_timestamp(&buf
->vb
.ts
);
310 dev
->isoc_ctl
.buf
= NULL
;
312 list_del(&buf
->vb
.queue
);
313 wake_up(&buf
->vb
.done
);
316 static inline void vbi_buffer_filled(struct au0828_dev
*dev
,
317 struct au0828_dmaqueue
*dma_q
,
318 struct au0828_buffer
*buf
)
320 /* Advice that buffer was filled */
321 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
323 buf
->vb
.state
= VIDEOBUF_DONE
;
324 buf
->vb
.field_count
++;
325 v4l2_get_timestamp(&buf
->vb
.ts
);
327 dev
->isoc_ctl
.vbi_buf
= NULL
;
329 list_del(&buf
->vb
.queue
);
330 wake_up(&buf
->vb
.done
);
334 * Identify the buffer header type and properly handles
336 static void au0828_copy_video(struct au0828_dev
*dev
,
337 struct au0828_dmaqueue
*dma_q
,
338 struct au0828_buffer
*buf
,
340 unsigned char *outp
, unsigned long len
)
342 void *fieldstart
, *startwrite
, *startread
;
343 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
344 int bytesperline
= dev
->width
<< 1; /* Assumes 16-bit depth @@@@ */
349 if (dma_q
->pos
+ len
> buf
->vb
.size
)
350 len
= buf
->vb
.size
- dma_q
->pos
;
355 /* Interlaces frame */
359 fieldstart
= outp
+ bytesperline
;
361 linesdone
= dma_q
->pos
/ bytesperline
;
362 currlinedone
= dma_q
->pos
% bytesperline
;
363 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
364 startwrite
= fieldstart
+ offset
;
365 lencopy
= bytesperline
- currlinedone
;
366 lencopy
= lencopy
> remain
? remain
: lencopy
;
368 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
369 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
370 ((char *)startwrite
+ lencopy
) -
371 ((char *)outp
+ buf
->vb
.size
));
372 remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
377 memcpy(startwrite
, startread
, lencopy
);
382 startwrite
+= lencopy
+ bytesperline
;
383 startread
+= lencopy
;
384 if (bytesperline
> remain
)
387 lencopy
= bytesperline
;
389 if ((char *)startwrite
+ lencopy
> (char *)outp
+
391 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
392 ((char *)startwrite
+ lencopy
) -
393 ((char *)outp
+ buf
->vb
.size
));
394 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
400 memcpy(startwrite
, startread
, lencopy
);
406 /* We have enough data to check for greenscreen */
407 if (outp
[0] < 0x60 && outp
[1440] < 0x60)
408 dev
->greenscreen_detected
= 1;
415 * video-buf generic routine to get the next available buffer
417 static inline void get_next_buf(struct au0828_dmaqueue
*dma_q
,
418 struct au0828_buffer
**buf
)
420 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
422 if (list_empty(&dma_q
->active
)) {
423 au0828_isocdbg("No active queue to serve\n");
424 dev
->isoc_ctl
.buf
= NULL
;
429 /* Get the next buffer */
430 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, vb
.queue
);
431 dev
->isoc_ctl
.buf
= *buf
;
436 static void au0828_copy_vbi(struct au0828_dev
*dev
,
437 struct au0828_dmaqueue
*dma_q
,
438 struct au0828_buffer
*buf
,
440 unsigned char *outp
, unsigned long len
)
442 unsigned char *startwrite
, *startread
;
447 au0828_isocdbg("dev is null\n");
452 au0828_isocdbg("dma_q is null\n");
458 au0828_isocdbg("p is null\n");
462 au0828_isocdbg("outp is null\n");
466 bytesperline
= dev
->vbi_width
;
468 if (dma_q
->pos
+ len
> buf
->vb
.size
)
469 len
= buf
->vb
.size
- dma_q
->pos
;
472 startwrite
= outp
+ (dma_q
->pos
/ 2);
474 /* Make sure the bottom field populates the second half of the frame */
475 if (buf
->top_field
== 0)
476 startwrite
+= bytesperline
* dev
->vbi_height
;
478 for (i
= 0; i
< len
; i
+= 2)
479 startwrite
[j
++] = startread
[i
+1];
486 * video-buf generic routine to get the next available VBI buffer
488 static inline void vbi_get_next_buf(struct au0828_dmaqueue
*dma_q
,
489 struct au0828_buffer
**buf
)
491 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vbiq
);
494 if (list_empty(&dma_q
->active
)) {
495 au0828_isocdbg("No active queue to serve\n");
496 dev
->isoc_ctl
.vbi_buf
= NULL
;
501 /* Get the next buffer */
502 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, vb
.queue
);
503 /* Cleans up buffer - Useful for testing for frame/URB loss */
504 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
505 memset(outp
, 0x00, (*buf
)->vb
.size
);
507 dev
->isoc_ctl
.vbi_buf
= *buf
;
513 * Controls the isoc copy of each urb packet
515 static inline int au0828_isoc_copy(struct au0828_dev
*dev
, struct urb
*urb
)
517 struct au0828_buffer
*buf
;
518 struct au0828_buffer
*vbi_buf
;
519 struct au0828_dmaqueue
*dma_q
= urb
->context
;
520 struct au0828_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
521 unsigned char *outp
= NULL
;
522 unsigned char *vbioutp
= NULL
;
523 int i
, len
= 0, rc
= 1;
526 unsigned int vbi_field_size
;
527 unsigned int remain
, lencopy
;
532 if ((dev
->dev_state
& DEV_DISCONNECTED
) ||
533 (dev
->dev_state
& DEV_MISCONFIGURED
))
536 if (urb
->status
< 0) {
537 print_err_status(dev
, -1, urb
->status
);
538 if (urb
->status
== -ENOENT
)
542 buf
= dev
->isoc_ctl
.buf
;
544 outp
= videobuf_to_vmalloc(&buf
->vb
);
546 vbi_buf
= dev
->isoc_ctl
.vbi_buf
;
548 vbioutp
= videobuf_to_vmalloc(&vbi_buf
->vb
);
550 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
551 int status
= urb
->iso_frame_desc
[i
].status
;
554 print_err_status(dev
, i
, status
);
555 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
559 if (urb
->iso_frame_desc
[i
].actual_length
<= 0)
562 if (urb
->iso_frame_desc
[i
].actual_length
>
564 au0828_isocdbg("packet bigger than packet size");
568 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
570 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
576 au0828_isocdbg("Video frame %s\n",
577 (fbyte
& 0x40) ? "odd" : "even");
581 vbi_buffer_filled(dev
,
584 vbi_get_next_buf(vbi_dma_q
, &vbi_buf
);
588 vbioutp
= videobuf_to_vmalloc(
593 buffer_filled(dev
, dma_q
, buf
);
594 get_next_buf(dma_q
, &buf
);
598 outp
= videobuf_to_vmalloc(&buf
->vb
);
600 /* As long as isoc traffic is arriving, keep
601 resetting the timer */
602 if (dev
->vid_timeout_running
)
603 mod_timer(&dev
->vid_timeout
,
604 jiffies
+ (HZ
/ 10));
605 if (dev
->vbi_timeout_running
)
606 mod_timer(&dev
->vbi_timeout
,
607 jiffies
+ (HZ
/ 10));
617 if (vbi_buf
!= NULL
) {
619 vbi_buf
->top_field
= 1;
621 vbi_buf
->top_field
= 0;
629 vbi_field_size
= dev
->vbi_width
* dev
->vbi_height
* 2;
630 if (dev
->vbi_read
< vbi_field_size
) {
631 remain
= vbi_field_size
- dev
->vbi_read
;
638 au0828_copy_vbi(dev
, vbi_dma_q
, vbi_buf
, p
,
643 dev
->vbi_read
+= lencopy
;
646 if (dev
->vbi_read
>= vbi_field_size
&& buf
!= NULL
)
647 au0828_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
653 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
656 struct au0828_fh
*fh
= vq
->priv_data
;
657 *size
= (fh
->dev
->width
* fh
->dev
->height
* 16 + 7) >> 3;
660 *count
= AU0828_DEF_BUF
;
662 if (*count
< AU0828_MIN_BUF
)
663 *count
= AU0828_MIN_BUF
;
667 /* This is called *without* dev->slock held; please keep it that way */
668 static void free_buffer(struct videobuf_queue
*vq
, struct au0828_buffer
*buf
)
670 struct au0828_fh
*fh
= vq
->priv_data
;
671 struct au0828_dev
*dev
= fh
->dev
;
672 unsigned long flags
= 0;
676 /* We used to wait for the buffer to finish here, but this didn't work
677 because, as we were keeping the state as VIDEOBUF_QUEUED,
678 videobuf_queue_cancel marked it as finished for us.
679 (Also, it could wedge forever if the hardware was misconfigured.)
681 This should be safe; by the time we get here, the buffer isn't
682 queued anymore. If we ever start marking the buffers as
683 VIDEOBUF_ACTIVE, it won't be, though.
685 spin_lock_irqsave(&dev
->slock
, flags
);
686 if (dev
->isoc_ctl
.buf
== buf
)
687 dev
->isoc_ctl
.buf
= NULL
;
688 spin_unlock_irqrestore(&dev
->slock
, flags
);
690 videobuf_vmalloc_free(&buf
->vb
);
691 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
695 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
696 enum v4l2_field field
)
698 struct au0828_fh
*fh
= vq
->priv_data
;
699 struct au0828_buffer
*buf
= container_of(vb
, struct au0828_buffer
, vb
);
700 struct au0828_dev
*dev
= fh
->dev
;
701 int rc
= 0, urb_init
= 0;
703 buf
->vb
.size
= (fh
->dev
->width
* fh
->dev
->height
* 16 + 7) >> 3;
705 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
708 buf
->vb
.width
= dev
->width
;
709 buf
->vb
.height
= dev
->height
;
710 buf
->vb
.field
= field
;
712 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
713 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
715 printk(KERN_INFO
"videobuf_iolock failed\n");
720 if (!dev
->isoc_ctl
.num_bufs
)
724 rc
= au0828_init_isoc(dev
, AU0828_ISO_PACKETS_PER_URB
,
725 AU0828_MAX_ISO_BUFS
, dev
->max_pkt_size
,
728 printk(KERN_INFO
"au0828_init_isoc failed\n");
733 buf
->vb
.state
= VIDEOBUF_PREPARED
;
737 free_buffer(vq
, buf
);
742 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
744 struct au0828_buffer
*buf
= container_of(vb
,
745 struct au0828_buffer
,
747 struct au0828_fh
*fh
= vq
->priv_data
;
748 struct au0828_dev
*dev
= fh
->dev
;
749 struct au0828_dmaqueue
*vidq
= &dev
->vidq
;
751 buf
->vb
.state
= VIDEOBUF_QUEUED
;
752 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
755 static void buffer_release(struct videobuf_queue
*vq
,
756 struct videobuf_buffer
*vb
)
758 struct au0828_buffer
*buf
= container_of(vb
,
759 struct au0828_buffer
,
762 free_buffer(vq
, buf
);
765 static struct videobuf_queue_ops au0828_video_qops
= {
766 .buf_setup
= buffer_setup
,
767 .buf_prepare
= buffer_prepare
,
768 .buf_queue
= buffer_queue
,
769 .buf_release
= buffer_release
,
772 /* ------------------------------------------------------------------
774 ------------------------------------------------------------------*/
776 static int au0828_i2s_init(struct au0828_dev
*dev
)
778 /* Enable i2s mode */
779 au0828_writereg(dev
, AU0828_AUDIOCTRL_50C
, 0x01);
784 * Auvitek au0828 analog stream enable
785 * Please set interface0 to AS5 before enable the stream
787 static int au0828_analog_stream_enable(struct au0828_dev
*d
)
789 dprintk(1, "au0828_analog_stream_enable called\n");
790 au0828_writereg(d
, AU0828_SENSORCTRL_VBI_103
, 0x00);
791 au0828_writereg(d
, 0x106, 0x00);
793 au0828_writereg(d
, 0x110, 0x00);
794 au0828_writereg(d
, 0x111, 0x00);
795 au0828_writereg(d
, 0x114, 0xa0);
796 au0828_writereg(d
, 0x115, 0x05);
798 au0828_writereg(d
, 0x112, 0x00);
799 au0828_writereg(d
, 0x113, 0x00);
800 au0828_writereg(d
, 0x116, 0xf2);
801 au0828_writereg(d
, 0x117, 0x00);
802 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0xb3);
807 int au0828_analog_stream_disable(struct au0828_dev
*d
)
809 dprintk(1, "au0828_analog_stream_disable called\n");
810 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0x0);
814 static void au0828_analog_stream_reset(struct au0828_dev
*dev
)
816 dprintk(1, "au0828_analog_stream_reset called\n");
817 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0x0);
819 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0xb3);
823 * Some operations needs to stop current streaming
825 static int au0828_stream_interrupt(struct au0828_dev
*dev
)
829 dev
->stream_state
= STREAM_INTERRUPT
;
830 if (dev
->dev_state
== DEV_DISCONNECTED
)
833 dev
->dev_state
= DEV_MISCONFIGURED
;
834 dprintk(1, "%s device is misconfigured!\n", __func__
);
841 * au0828_release_resources
842 * unregister v4l2 devices
844 void au0828_analog_unregister(struct au0828_dev
*dev
)
846 dprintk(1, "au0828_release_resources called\n");
847 mutex_lock(&au0828_sysfs_lock
);
850 video_unregister_device(dev
->vdev
);
852 video_unregister_device(dev
->vbi_dev
);
854 mutex_unlock(&au0828_sysfs_lock
);
858 /* Usage lock check functions */
859 static int res_get(struct au0828_fh
*fh
, unsigned int bit
)
861 struct au0828_dev
*dev
= fh
->dev
;
863 if (fh
->resources
& bit
)
864 /* have it already allocated */
868 if (dev
->resources
& bit
) {
869 /* no, someone else uses it */
872 /* it's free, grab it */
873 fh
->resources
|= bit
;
874 dev
->resources
|= bit
;
875 dprintk(1, "res: get %d\n", bit
);
880 static int res_check(struct au0828_fh
*fh
, unsigned int bit
)
882 return fh
->resources
& bit
;
885 static int res_locked(struct au0828_dev
*dev
, unsigned int bit
)
887 return dev
->resources
& bit
;
890 static void res_free(struct au0828_fh
*fh
, unsigned int bits
)
892 struct au0828_dev
*dev
= fh
->dev
;
894 BUG_ON((fh
->resources
& bits
) != bits
);
896 fh
->resources
&= ~bits
;
897 dev
->resources
&= ~bits
;
898 dprintk(1, "res: put %d\n", bits
);
901 static int get_ressource(struct au0828_fh
*fh
)
904 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
905 return AU0828_RESOURCE_VIDEO
;
906 case V4L2_BUF_TYPE_VBI_CAPTURE
:
907 return AU0828_RESOURCE_VBI
;
914 /* This function ensures that video frames continue to be delivered even if
915 the ITU-656 input isn't receiving any data (thereby preventing applications
916 such as tvtime from hanging) */
917 static void au0828_vid_buffer_timeout(unsigned long data
)
919 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
920 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
921 struct au0828_buffer
*buf
;
922 unsigned char *vid_data
;
923 unsigned long flags
= 0;
925 spin_lock_irqsave(&dev
->slock
, flags
);
927 buf
= dev
->isoc_ctl
.buf
;
929 vid_data
= videobuf_to_vmalloc(&buf
->vb
);
930 memset(vid_data
, 0x00, buf
->vb
.size
); /* Blank green frame */
931 buffer_filled(dev
, dma_q
, buf
);
933 get_next_buf(dma_q
, &buf
);
935 if (dev
->vid_timeout_running
== 1)
936 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
938 spin_unlock_irqrestore(&dev
->slock
, flags
);
941 static void au0828_vbi_buffer_timeout(unsigned long data
)
943 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
944 struct au0828_dmaqueue
*dma_q
= &dev
->vbiq
;
945 struct au0828_buffer
*buf
;
946 unsigned char *vbi_data
;
947 unsigned long flags
= 0;
949 spin_lock_irqsave(&dev
->slock
, flags
);
951 buf
= dev
->isoc_ctl
.vbi_buf
;
953 vbi_data
= videobuf_to_vmalloc(&buf
->vb
);
954 memset(vbi_data
, 0x00, buf
->vb
.size
);
955 vbi_buffer_filled(dev
, dma_q
, buf
);
957 vbi_get_next_buf(dma_q
, &buf
);
959 if (dev
->vbi_timeout_running
== 1)
960 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
961 spin_unlock_irqrestore(&dev
->slock
, flags
);
965 static int au0828_v4l2_open(struct file
*filp
)
968 struct video_device
*vdev
= video_devdata(filp
);
969 struct au0828_dev
*dev
= video_drvdata(filp
);
970 struct au0828_fh
*fh
;
973 switch (vdev
->vfl_type
) {
974 case VFL_TYPE_GRABBER
:
975 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
978 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
984 fh
= kzalloc(sizeof(struct au0828_fh
), GFP_KERNEL
);
986 dprintk(1, "Failed allocate au0828_fh struct!\n");
992 v4l2_fh_init(&fh
->fh
, vdev
);
993 filp
->private_data
= fh
;
995 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
996 /* set au0828 interface0 to AS5 here again */
997 ret
= usb_set_interface(dev
->usbdev
, 0, 5);
999 printk(KERN_INFO
"Au0828 can't set alternate to 5!\n");
1002 dev
->width
= NTSC_STD_W
;
1003 dev
->height
= NTSC_STD_H
;
1004 dev
->frame_size
= dev
->width
* dev
->height
* 2;
1005 dev
->field_size
= dev
->width
* dev
->height
;
1006 dev
->bytesperline
= dev
->width
* 2;
1008 au0828_analog_stream_enable(dev
);
1009 au0828_analog_stream_reset(dev
);
1011 /* If we were doing ac97 instead of i2s, it would go here...*/
1012 au0828_i2s_init(dev
);
1014 dev
->stream_state
= STREAM_OFF
;
1015 dev
->dev_state
|= DEV_INITIALIZED
;
1020 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &au0828_video_qops
,
1022 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1023 V4L2_FIELD_INTERLACED
,
1024 sizeof(struct au0828_buffer
), fh
,
1028 dev
->vbi_width
= 720;
1029 dev
->vbi_height
= 1;
1030 videobuf_queue_vmalloc_init(&fh
->vb_vbiq
, &au0828_vbi_qops
,
1032 V4L2_BUF_TYPE_VBI_CAPTURE
,
1034 sizeof(struct au0828_buffer
), fh
,
1036 v4l2_fh_add(&fh
->fh
);
1040 static int au0828_v4l2_close(struct file
*filp
)
1043 struct au0828_fh
*fh
= filp
->private_data
;
1044 struct au0828_dev
*dev
= fh
->dev
;
1046 v4l2_fh_del(&fh
->fh
);
1047 v4l2_fh_exit(&fh
->fh
);
1048 if (res_check(fh
, AU0828_RESOURCE_VIDEO
)) {
1049 /* Cancel timeout thread in case they didn't call streamoff */
1050 dev
->vid_timeout_running
= 0;
1051 del_timer_sync(&dev
->vid_timeout
);
1053 videobuf_stop(&fh
->vb_vidq
);
1054 res_free(fh
, AU0828_RESOURCE_VIDEO
);
1057 if (res_check(fh
, AU0828_RESOURCE_VBI
)) {
1058 /* Cancel timeout thread in case they didn't call streamoff */
1059 dev
->vbi_timeout_running
= 0;
1060 del_timer_sync(&dev
->vbi_timeout
);
1062 videobuf_stop(&fh
->vb_vbiq
);
1063 res_free(fh
, AU0828_RESOURCE_VBI
);
1066 if (dev
->users
== 1) {
1067 if (dev
->dev_state
& DEV_DISCONNECTED
) {
1068 au0828_analog_unregister(dev
);
1074 au0828_analog_stream_disable(dev
);
1076 au0828_uninit_isoc(dev
);
1078 /* Save some power by putting tuner to sleep */
1079 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_power
, 0);
1081 /* When close the device, set the usb intf0 into alt0 to free
1083 ret
= usb_set_interface(dev
->usbdev
, 0, 0);
1085 printk(KERN_INFO
"Au0828 can't set alternate to 0!\n");
1088 videobuf_mmap_free(&fh
->vb_vidq
);
1089 videobuf_mmap_free(&fh
->vb_vbiq
);
1092 wake_up_interruptible_nr(&dev
->open
, 1);
1096 static ssize_t
au0828_v4l2_read(struct file
*filp
, char __user
*buf
,
1097 size_t count
, loff_t
*pos
)
1099 struct au0828_fh
*fh
= filp
->private_data
;
1100 struct au0828_dev
*dev
= fh
->dev
;
1103 rc
= check_dev(dev
);
1107 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1108 if (res_locked(dev
, AU0828_RESOURCE_VIDEO
))
1111 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
1112 filp
->f_flags
& O_NONBLOCK
);
1115 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1116 if (!res_get(fh
, AU0828_RESOURCE_VBI
))
1119 if (dev
->vbi_timeout_running
== 0) {
1120 /* Handle case where caller tries to read without
1121 calling streamon first */
1122 dev
->vbi_timeout_running
= 1;
1123 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1126 return videobuf_read_stream(&fh
->vb_vbiq
, buf
, count
, pos
, 0,
1127 filp
->f_flags
& O_NONBLOCK
);
1133 static unsigned int au0828_v4l2_poll(struct file
*filp
, poll_table
*wait
)
1135 struct au0828_fh
*fh
= filp
->private_data
;
1136 struct au0828_dev
*dev
= fh
->dev
;
1137 unsigned long req_events
= poll_requested_events(wait
);
1140 if (check_dev(dev
) < 0)
1143 res
= v4l2_ctrl_poll(filp
, wait
);
1144 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
1147 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1148 if (!res_get(fh
, AU0828_RESOURCE_VIDEO
))
1150 return res
| videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
1152 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1153 if (!res_get(fh
, AU0828_RESOURCE_VBI
))
1155 return res
| videobuf_poll_stream(filp
, &fh
->vb_vbiq
, wait
);
1160 static int au0828_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1162 struct au0828_fh
*fh
= filp
->private_data
;
1163 struct au0828_dev
*dev
= fh
->dev
;
1166 rc
= check_dev(dev
);
1170 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1171 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1172 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1173 rc
= videobuf_mmap_mapper(&fh
->vb_vbiq
, vma
);
1178 static int au0828_set_format(struct au0828_dev
*dev
, unsigned int cmd
,
1179 struct v4l2_format
*format
)
1182 int width
= format
->fmt
.pix
.width
;
1183 int height
= format
->fmt
.pix
.height
;
1185 if (format
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1188 /* If they are demanding a format other than the one we support,
1189 bail out (tvtime asks for UYVY and then retries with YUYV) */
1190 if (format
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_UYVY
)
1193 /* format->fmt.pix.width only support 720 and height 480 */
1199 format
->fmt
.pix
.width
= width
;
1200 format
->fmt
.pix
.height
= height
;
1201 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1202 format
->fmt
.pix
.bytesperline
= width
* 2;
1203 format
->fmt
.pix
.sizeimage
= width
* height
* 2;
1204 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1205 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1207 if (cmd
== VIDIOC_TRY_FMT
)
1210 /* maybe set new image format, driver current only support 720*480 */
1212 dev
->height
= height
;
1213 dev
->frame_size
= width
* height
* 2;
1214 dev
->field_size
= width
* height
;
1215 dev
->bytesperline
= width
* 2;
1217 if (dev
->stream_state
== STREAM_ON
) {
1218 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1219 ret
= au0828_stream_interrupt(dev
);
1221 dprintk(1, "error interrupting video stream!\n");
1226 /* set au0828 interface0 to AS5 here again */
1227 ret
= usb_set_interface(dev
->usbdev
, 0, 5);
1229 printk(KERN_INFO
"Au0828 can't set alt setting to 5!\n");
1233 au0828_analog_stream_enable(dev
);
1239 static int vidioc_querycap(struct file
*file
, void *priv
,
1240 struct v4l2_capability
*cap
)
1242 struct video_device
*vdev
= video_devdata(file
);
1243 struct au0828_fh
*fh
= priv
;
1244 struct au0828_dev
*dev
= fh
->dev
;
1246 strlcpy(cap
->driver
, "au0828", sizeof(cap
->driver
));
1247 strlcpy(cap
->card
, dev
->board
.name
, sizeof(cap
->card
));
1248 usb_make_path(dev
->usbdev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1250 /* set the device capabilities */
1251 cap
->device_caps
= V4L2_CAP_AUDIO
|
1252 V4L2_CAP_READWRITE
|
1253 V4L2_CAP_STREAMING
|
1255 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
1256 cap
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
;
1258 cap
->device_caps
|= V4L2_CAP_VBI_CAPTURE
;
1259 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
|
1260 V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_VIDEO_CAPTURE
;
1264 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1265 struct v4l2_fmtdesc
*f
)
1270 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1271 strcpy(f
->description
, "Packed YUV2");
1274 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
1279 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1280 struct v4l2_format
*f
)
1282 struct au0828_fh
*fh
= priv
;
1283 struct au0828_dev
*dev
= fh
->dev
;
1285 f
->fmt
.pix
.width
= dev
->width
;
1286 f
->fmt
.pix
.height
= dev
->height
;
1287 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1288 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
1289 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
1290 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
; /* NTSC/PAL */
1291 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1295 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1296 struct v4l2_format
*f
)
1298 struct au0828_fh
*fh
= priv
;
1299 struct au0828_dev
*dev
= fh
->dev
;
1301 return au0828_set_format(dev
, VIDIOC_TRY_FMT
, f
);
1304 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1305 struct v4l2_format
*f
)
1307 struct au0828_fh
*fh
= priv
;
1308 struct au0828_dev
*dev
= fh
->dev
;
1311 rc
= check_dev(dev
);
1315 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1316 printk(KERN_INFO
"%s queue busy\n", __func__
);
1321 rc
= au0828_set_format(dev
, VIDIOC_S_FMT
, f
);
1326 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
1328 struct au0828_fh
*fh
= priv
;
1329 struct au0828_dev
*dev
= fh
->dev
;
1331 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1332 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 1);
1334 /* FIXME: when we support something other than NTSC, we are going to
1335 have to make the au0828 bridge adjust the size of its capture
1336 buffer, which is currently hardcoded at 720x480 */
1338 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, norm
);
1339 dev
->std_set_in_tuner_core
= 1;
1341 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1342 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 0);
1347 static int vidioc_enum_input(struct file
*file
, void *priv
,
1348 struct v4l2_input
*input
)
1350 struct au0828_fh
*fh
= priv
;
1351 struct au0828_dev
*dev
= fh
->dev
;
1354 static const char *inames
[] = {
1355 [AU0828_VMUX_UNDEFINED
] = "Undefined",
1356 [AU0828_VMUX_COMPOSITE
] = "Composite",
1357 [AU0828_VMUX_SVIDEO
] = "S-Video",
1358 [AU0828_VMUX_CABLE
] = "Cable TV",
1359 [AU0828_VMUX_TELEVISION
] = "Television",
1360 [AU0828_VMUX_DVB
] = "DVB",
1361 [AU0828_VMUX_DEBUG
] = "tv debug"
1366 if (tmp
>= AU0828_MAX_INPUT
)
1368 if (AUVI_INPUT(tmp
).type
== 0)
1372 strcpy(input
->name
, inames
[AUVI_INPUT(tmp
).type
]);
1373 if ((AUVI_INPUT(tmp
).type
== AU0828_VMUX_TELEVISION
) ||
1374 (AUVI_INPUT(tmp
).type
== AU0828_VMUX_CABLE
)) {
1375 input
->type
|= V4L2_INPUT_TYPE_TUNER
;
1376 input
->audioset
= 1;
1378 input
->type
|= V4L2_INPUT_TYPE_CAMERA
;
1379 input
->audioset
= 2;
1382 input
->std
= dev
->vdev
->tvnorms
;
1387 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1389 struct au0828_fh
*fh
= priv
;
1390 struct au0828_dev
*dev
= fh
->dev
;
1391 *i
= dev
->ctrl_input
;
1395 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int index
)
1397 struct au0828_fh
*fh
= priv
;
1398 struct au0828_dev
*dev
= fh
->dev
;
1401 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__
,
1403 if (index
>= AU0828_MAX_INPUT
)
1405 if (AUVI_INPUT(index
).type
== 0)
1407 dev
->ctrl_input
= index
;
1409 switch (AUVI_INPUT(index
).type
) {
1410 case AU0828_VMUX_SVIDEO
:
1411 dev
->input_type
= AU0828_VMUX_SVIDEO
;
1412 dev
->ctrl_ainput
= 1;
1414 case AU0828_VMUX_COMPOSITE
:
1415 dev
->input_type
= AU0828_VMUX_COMPOSITE
;
1416 dev
->ctrl_ainput
= 1;
1418 case AU0828_VMUX_TELEVISION
:
1419 dev
->input_type
= AU0828_VMUX_TELEVISION
;
1420 dev
->ctrl_ainput
= 0;
1423 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1424 AUVI_INPUT(index
).type
);
1428 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
1429 AUVI_INPUT(index
).vmux
, 0, 0);
1431 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1433 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1441 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1443 /* Make sure we leave it turned on if some
1444 other input is routed to this callback */
1445 if ((AUVI_INPUT(i
).audio_setup
) !=
1446 ((AUVI_INPUT(index
).audio_setup
))) {
1447 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1452 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
1453 AUVI_INPUT(index
).amux
, 0, 0);
1457 static int vidioc_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1463 strcpy(a
->name
, "Television");
1465 strcpy(a
->name
, "Line in");
1467 a
->capability
= V4L2_AUDCAP_STEREO
;
1471 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1473 struct au0828_fh
*fh
= priv
;
1474 struct au0828_dev
*dev
= fh
->dev
;
1476 a
->index
= dev
->ctrl_ainput
;
1478 strcpy(a
->name
, "Television");
1480 strcpy(a
->name
, "Line in");
1482 a
->capability
= V4L2_AUDCAP_STEREO
;
1486 static int vidioc_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*a
)
1488 struct au0828_fh
*fh
= priv
;
1489 struct au0828_dev
*dev
= fh
->dev
;
1491 if (a
->index
!= dev
->ctrl_ainput
)
1496 static int vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1498 struct au0828_fh
*fh
= priv
;
1499 struct au0828_dev
*dev
= fh
->dev
;
1504 strcpy(t
->name
, "Auvitek tuner");
1505 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1509 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1510 const struct v4l2_tuner
*t
)
1512 struct au0828_fh
*fh
= priv
;
1513 struct au0828_dev
*dev
= fh
->dev
;
1518 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1519 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 1);
1521 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1523 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1524 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 0);
1526 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t
->signal
,
1533 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1534 struct v4l2_frequency
*freq
)
1536 struct au0828_fh
*fh
= priv
;
1537 struct au0828_dev
*dev
= fh
->dev
;
1539 if (freq
->tuner
!= 0)
1541 freq
->frequency
= dev
->ctrl_freq
;
1545 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1546 const struct v4l2_frequency
*freq
)
1548 struct au0828_fh
*fh
= priv
;
1549 struct au0828_dev
*dev
= fh
->dev
;
1550 struct v4l2_frequency new_freq
= *freq
;
1552 if (freq
->tuner
!= 0)
1555 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1556 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 1);
1558 if (dev
->std_set_in_tuner_core
== 0) {
1559 /* If we've never sent the standard in tuner core, do so now.
1560 We don't do this at device probe because we don't want to
1561 incur the cost of a firmware load */
1562 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
,
1563 dev
->vdev
->tvnorms
);
1564 dev
->std_set_in_tuner_core
= 1;
1567 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, freq
);
1568 /* Get the actual set (and possibly clamped) frequency */
1569 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, &new_freq
);
1570 dev
->ctrl_freq
= new_freq
.frequency
;
1572 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1573 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 0);
1575 au0828_analog_stream_reset(dev
);
1581 /* RAW VBI ioctls */
1583 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1584 struct v4l2_format
*format
)
1586 struct au0828_fh
*fh
= priv
;
1587 struct au0828_dev
*dev
= fh
->dev
;
1589 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1590 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1591 format
->fmt
.vbi
.offset
= 0;
1592 format
->fmt
.vbi
.flags
= 0;
1593 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1595 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1596 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1597 format
->fmt
.vbi
.start
[0] = 21;
1598 format
->fmt
.vbi
.start
[1] = 284;
1603 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1604 struct v4l2_dbg_chip_ident
*chip
)
1606 struct au0828_fh
*fh
= priv
;
1607 struct au0828_dev
*dev
= fh
->dev
;
1608 chip
->ident
= V4L2_IDENT_NONE
;
1611 if (v4l2_chip_match_host(&chip
->match
)) {
1612 chip
->ident
= V4L2_IDENT_AU0828
;
1616 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_chip_ident
, chip
);
1617 if (chip
->ident
== V4L2_IDENT_NONE
)
1623 static int vidioc_cropcap(struct file
*file
, void *priv
,
1624 struct v4l2_cropcap
*cc
)
1626 struct au0828_fh
*fh
= priv
;
1627 struct au0828_dev
*dev
= fh
->dev
;
1629 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1632 cc
->bounds
.left
= 0;
1634 cc
->bounds
.width
= dev
->width
;
1635 cc
->bounds
.height
= dev
->height
;
1637 cc
->defrect
= cc
->bounds
;
1639 cc
->pixelaspect
.numerator
= 54;
1640 cc
->pixelaspect
.denominator
= 59;
1645 static int vidioc_streamon(struct file
*file
, void *priv
,
1646 enum v4l2_buf_type type
)
1648 struct au0828_fh
*fh
= priv
;
1649 struct au0828_dev
*dev
= fh
->dev
;
1652 rc
= check_dev(dev
);
1656 if (unlikely(type
!= fh
->type
))
1659 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1660 fh
, type
, fh
->resources
, dev
->resources
);
1662 if (unlikely(!res_get(fh
, get_ressource(fh
))))
1665 if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1666 au0828_analog_stream_enable(dev
);
1667 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 1);
1670 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1671 rc
= videobuf_streamon(&fh
->vb_vidq
);
1672 dev
->vid_timeout_running
= 1;
1673 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
1674 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1675 rc
= videobuf_streamon(&fh
->vb_vbiq
);
1676 dev
->vbi_timeout_running
= 1;
1677 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1683 static int vidioc_streamoff(struct file
*file
, void *priv
,
1684 enum v4l2_buf_type type
)
1686 struct au0828_fh
*fh
= priv
;
1687 struct au0828_dev
*dev
= fh
->dev
;
1691 rc
= check_dev(dev
);
1695 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1696 fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
)
1698 if (type
!= fh
->type
)
1701 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1702 fh
, type
, fh
->resources
, dev
->resources
);
1704 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1705 dev
->vid_timeout_running
= 0;
1706 del_timer_sync(&dev
->vid_timeout
);
1708 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 0);
1709 rc
= au0828_stream_interrupt(dev
);
1713 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1714 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1716 (AUVI_INPUT(i
).audio_setup
)(dev
, 0);
1719 if (res_check(fh
, AU0828_RESOURCE_VIDEO
)) {
1720 videobuf_streamoff(&fh
->vb_vidq
);
1721 res_free(fh
, AU0828_RESOURCE_VIDEO
);
1723 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1724 dev
->vbi_timeout_running
= 0;
1725 del_timer_sync(&dev
->vbi_timeout
);
1727 if (res_check(fh
, AU0828_RESOURCE_VBI
)) {
1728 videobuf_streamoff(&fh
->vb_vbiq
);
1729 res_free(fh
, AU0828_RESOURCE_VBI
);
1736 #ifdef CONFIG_VIDEO_ADV_DEBUG
1737 static int vidioc_g_register(struct file
*file
, void *priv
,
1738 struct v4l2_dbg_register
*reg
)
1740 struct au0828_fh
*fh
= priv
;
1741 struct au0828_dev
*dev
= fh
->dev
;
1743 switch (reg
->match
.type
) {
1744 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1745 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1748 if (!v4l2_chip_match_host(®
->match
))
1752 reg
->val
= au0828_read(dev
, reg
->reg
);
1756 static int vidioc_s_register(struct file
*file
, void *priv
,
1757 const struct v4l2_dbg_register
*reg
)
1759 struct au0828_fh
*fh
= priv
;
1760 struct au0828_dev
*dev
= fh
->dev
;
1762 switch (reg
->match
.type
) {
1763 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1764 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1767 if (!v4l2_chip_match_host(®
->match
))
1770 return au0828_writereg(dev
, reg
->reg
, reg
->val
);
1774 static int vidioc_log_status(struct file
*file
, void *fh
)
1776 struct video_device
*vdev
= video_devdata(file
);
1778 v4l2_ctrl_log_status(file
, fh
);
1779 v4l2_device_call_all(vdev
->v4l2_dev
, 0, core
, log_status
);
1783 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1784 struct v4l2_requestbuffers
*rb
)
1786 struct au0828_fh
*fh
= priv
;
1787 struct au0828_dev
*dev
= fh
->dev
;
1790 rc
= check_dev(dev
);
1794 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1795 rc
= videobuf_reqbufs(&fh
->vb_vidq
, rb
);
1796 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1797 rc
= videobuf_reqbufs(&fh
->vb_vbiq
, rb
);
1802 static int vidioc_querybuf(struct file
*file
, void *priv
,
1803 struct v4l2_buffer
*b
)
1805 struct au0828_fh
*fh
= priv
;
1806 struct au0828_dev
*dev
= fh
->dev
;
1809 rc
= check_dev(dev
);
1813 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1814 rc
= videobuf_querybuf(&fh
->vb_vidq
, b
);
1815 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1816 rc
= videobuf_querybuf(&fh
->vb_vbiq
, b
);
1821 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1823 struct au0828_fh
*fh
= priv
;
1824 struct au0828_dev
*dev
= fh
->dev
;
1827 rc
= check_dev(dev
);
1831 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1832 rc
= videobuf_qbuf(&fh
->vb_vidq
, b
);
1833 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1834 rc
= videobuf_qbuf(&fh
->vb_vbiq
, b
);
1839 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1841 struct au0828_fh
*fh
= priv
;
1842 struct au0828_dev
*dev
= fh
->dev
;
1845 rc
= check_dev(dev
);
1849 /* Workaround for a bug in the au0828 hardware design that sometimes
1850 results in the colorspace being inverted */
1851 if (dev
->greenscreen_detected
== 1) {
1852 dprintk(1, "Detected green frame. Resetting stream...\n");
1853 au0828_analog_stream_reset(dev
);
1854 dev
->greenscreen_detected
= 0;
1857 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1858 rc
= videobuf_dqbuf(&fh
->vb_vidq
, b
, file
->f_flags
& O_NONBLOCK
);
1859 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1860 rc
= videobuf_dqbuf(&fh
->vb_vbiq
, b
, file
->f_flags
& O_NONBLOCK
);
1865 static struct v4l2_file_operations au0828_v4l_fops
= {
1866 .owner
= THIS_MODULE
,
1867 .open
= au0828_v4l2_open
,
1868 .release
= au0828_v4l2_close
,
1869 .read
= au0828_v4l2_read
,
1870 .poll
= au0828_v4l2_poll
,
1871 .mmap
= au0828_v4l2_mmap
,
1872 .unlocked_ioctl
= video_ioctl2
,
1875 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1876 .vidioc_querycap
= vidioc_querycap
,
1877 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1878 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1879 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1880 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1881 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1882 .vidioc_s_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1883 .vidioc_enumaudio
= vidioc_enumaudio
,
1884 .vidioc_g_audio
= vidioc_g_audio
,
1885 .vidioc_s_audio
= vidioc_s_audio
,
1886 .vidioc_cropcap
= vidioc_cropcap
,
1887 .vidioc_reqbufs
= vidioc_reqbufs
,
1888 .vidioc_querybuf
= vidioc_querybuf
,
1889 .vidioc_qbuf
= vidioc_qbuf
,
1890 .vidioc_dqbuf
= vidioc_dqbuf
,
1891 .vidioc_s_std
= vidioc_s_std
,
1892 .vidioc_enum_input
= vidioc_enum_input
,
1893 .vidioc_g_input
= vidioc_g_input
,
1894 .vidioc_s_input
= vidioc_s_input
,
1895 .vidioc_streamon
= vidioc_streamon
,
1896 .vidioc_streamoff
= vidioc_streamoff
,
1897 .vidioc_g_tuner
= vidioc_g_tuner
,
1898 .vidioc_s_tuner
= vidioc_s_tuner
,
1899 .vidioc_g_frequency
= vidioc_g_frequency
,
1900 .vidioc_s_frequency
= vidioc_s_frequency
,
1901 #ifdef CONFIG_VIDEO_ADV_DEBUG
1902 .vidioc_g_register
= vidioc_g_register
,
1903 .vidioc_s_register
= vidioc_s_register
,
1905 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
1906 .vidioc_log_status
= vidioc_log_status
,
1907 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1908 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1911 static const struct video_device au0828_video_template
= {
1912 .fops
= &au0828_v4l_fops
,
1913 .release
= video_device_release
,
1914 .ioctl_ops
= &video_ioctl_ops
,
1915 .tvnorms
= V4L2_STD_NTSC_M
,
1916 .current_norm
= V4L2_STD_NTSC_M
,
1919 /**************************************************************************/
1921 int au0828_analog_register(struct au0828_dev
*dev
,
1922 struct usb_interface
*interface
)
1924 int retval
= -ENOMEM
;
1925 struct usb_host_interface
*iface_desc
;
1926 struct usb_endpoint_descriptor
*endpoint
;
1929 dprintk(1, "au0828_analog_register called!\n");
1931 /* set au0828 usb interface0 to as5 */
1932 retval
= usb_set_interface(dev
->usbdev
,
1933 interface
->cur_altsetting
->desc
.bInterfaceNumber
, 5);
1935 printk(KERN_INFO
"Failure setting usb interface0 to as5\n");
1939 /* Figure out which endpoint has the isoc interface */
1940 iface_desc
= interface
->cur_altsetting
;
1941 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
1942 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1943 if (((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
1945 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
1946 == USB_ENDPOINT_XFER_ISOC
)) {
1948 /* we find our isoc in endpoint */
1949 u16 tmp
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1950 dev
->max_pkt_size
= (tmp
& 0x07ff) *
1951 (((tmp
& 0x1800) >> 11) + 1);
1952 dev
->isoc_in_endpointaddr
= endpoint
->bEndpointAddress
;
1955 if (!(dev
->isoc_in_endpointaddr
)) {
1956 printk(KERN_INFO
"Could not locate isoc endpoint\n");
1961 init_waitqueue_head(&dev
->open
);
1962 spin_lock_init(&dev
->slock
);
1964 /* init video dma queues */
1965 INIT_LIST_HEAD(&dev
->vidq
.active
);
1966 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1967 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1968 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1970 dev
->vid_timeout
.function
= au0828_vid_buffer_timeout
;
1971 dev
->vid_timeout
.data
= (unsigned long) dev
;
1972 init_timer(&dev
->vid_timeout
);
1974 dev
->vbi_timeout
.function
= au0828_vbi_buffer_timeout
;
1975 dev
->vbi_timeout
.data
= (unsigned long) dev
;
1976 init_timer(&dev
->vbi_timeout
);
1978 dev
->width
= NTSC_STD_W
;
1979 dev
->height
= NTSC_STD_H
;
1980 dev
->field_size
= dev
->width
* dev
->height
;
1981 dev
->frame_size
= dev
->field_size
<< 1;
1982 dev
->bytesperline
= dev
->width
<< 1;
1983 dev
->ctrl_ainput
= 0;
1984 dev
->ctrl_freq
= 960;
1986 /* allocate and fill v4l2 video struct */
1987 dev
->vdev
= video_device_alloc();
1988 if (NULL
== dev
->vdev
) {
1989 dprintk(1, "Can't allocate video_device.\n");
1993 /* allocate the VBI struct */
1994 dev
->vbi_dev
= video_device_alloc();
1995 if (NULL
== dev
->vbi_dev
) {
1996 dprintk(1, "Can't allocate vbi_device.\n");
2001 /* Fill the video capture device struct */
2002 *dev
->vdev
= au0828_video_template
;
2003 dev
->vdev
->v4l2_dev
= &dev
->v4l2_dev
;
2004 dev
->vdev
->lock
= &dev
->lock
;
2005 set_bit(V4L2_FL_USE_FH_PRIO
, &dev
->vdev
->flags
);
2006 strcpy(dev
->vdev
->name
, "au0828a video");
2008 /* Setup the VBI device */
2009 *dev
->vbi_dev
= au0828_video_template
;
2010 dev
->vbi_dev
->v4l2_dev
= &dev
->v4l2_dev
;
2011 dev
->vbi_dev
->lock
= &dev
->lock
;
2012 set_bit(V4L2_FL_USE_FH_PRIO
, &dev
->vbi_dev
->flags
);
2013 strcpy(dev
->vbi_dev
->name
, "au0828a vbi");
2015 /* Register the v4l2 device */
2016 video_set_drvdata(dev
->vdev
, dev
);
2017 retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
, -1);
2019 dprintk(1, "unable to register video device (error = %d).\n",
2025 /* Register the vbi device */
2026 video_set_drvdata(dev
->vbi_dev
, dev
);
2027 retval
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
, -1);
2029 dprintk(1, "unable to register vbi device (error = %d).\n",
2035 dprintk(1, "%s completed!\n", __func__
);
2040 video_device_release(dev
->vbi_dev
);
2042 video_device_release(dev
->vdev
);