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-chip-ident.h>
39 #include <media/tuner.h>
41 #include "au0828-reg.h"
43 static DEFINE_MUTEX(au0828_sysfs_lock
);
45 /* ------------------------------------------------------------------
47 ------------------------------------------------------------------*/
49 static unsigned int isoc_debug
;
50 module_param(isoc_debug
, int, 0644);
51 MODULE_PARM_DESC(isoc_debug
, "enable debug messages [isoc transfers]");
53 #define au0828_isocdbg(fmt, arg...) \
56 printk(KERN_INFO "au0828 %s :"fmt, \
61 static inline void print_err_status(struct au0828_dev
*dev
,
62 int packet
, int status
)
64 char *errmsg
= "Unknown";
68 errmsg
= "unlinked synchronuously";
71 errmsg
= "unlinked asynchronuously";
74 errmsg
= "Buffer error (overrun)";
77 errmsg
= "Stalled (device not responding)";
80 errmsg
= "Babble (bad cable?)";
83 errmsg
= "Bit-stuff error (bad cable?)";
86 errmsg
= "CRC/Timeout (could be anything)";
89 errmsg
= "Device does not respond";
93 au0828_isocdbg("URB status %d [%s].\n", status
, errmsg
);
95 au0828_isocdbg("URB packet %d, status %d [%s].\n",
96 packet
, status
, errmsg
);
100 static int check_dev(struct au0828_dev
*dev
)
102 if (dev
->dev_state
& DEV_DISCONNECTED
) {
103 printk(KERN_INFO
"v4l2 ioctl: device not present\n");
107 if (dev
->dev_state
& DEV_MISCONFIGURED
) {
108 printk(KERN_INFO
"v4l2 ioctl: device is misconfigured; "
109 "close and open it again\n");
116 * IRQ callback, called by URB callback
118 static void au0828_irq_callback(struct urb
*urb
)
120 struct au0828_dmaqueue
*dma_q
= urb
->context
;
121 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
122 unsigned long flags
= 0;
125 switch (urb
->status
) {
126 case 0: /* success */
127 case -ETIMEDOUT
: /* NAK */
129 case -ECONNRESET
: /* kill */
132 au0828_isocdbg("au0828_irq_callback called: status kill\n");
134 default: /* unknown error */
135 au0828_isocdbg("urb completition error %d.\n", urb
->status
);
139 /* Copy data from URB */
140 spin_lock_irqsave(&dev
->slock
, flags
);
141 dev
->isoc_ctl
.isoc_copy(dev
, urb
);
142 spin_unlock_irqrestore(&dev
->slock
, flags
);
144 /* Reset urb buffers */
145 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
146 urb
->iso_frame_desc
[i
].status
= 0;
147 urb
->iso_frame_desc
[i
].actual_length
= 0;
151 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
153 au0828_isocdbg("urb resubmit failed (error=%i)\n",
159 * Stop and Deallocate URBs
161 static void au0828_uninit_isoc(struct au0828_dev
*dev
)
166 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
168 dev
->isoc_ctl
.nfields
= -1;
169 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
170 urb
= dev
->isoc_ctl
.urb
[i
];
172 if (!irqs_disabled())
177 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
178 usb_free_coherent(dev
->usbdev
,
179 urb
->transfer_buffer_length
,
180 dev
->isoc_ctl
.transfer_buffer
[i
],
184 dev
->isoc_ctl
.urb
[i
] = NULL
;
186 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
189 kfree(dev
->isoc_ctl
.urb
);
190 kfree(dev
->isoc_ctl
.transfer_buffer
);
192 dev
->isoc_ctl
.urb
= NULL
;
193 dev
->isoc_ctl
.transfer_buffer
= NULL
;
194 dev
->isoc_ctl
.num_bufs
= 0;
198 * Allocate URBs and start IRQ
200 static int au0828_init_isoc(struct au0828_dev
*dev
, int max_packets
,
201 int num_bufs
, int max_pkt_size
,
202 int (*isoc_copy
) (struct au0828_dev
*dev
, struct urb
*urb
))
204 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
211 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
213 /* De-allocates all pending stuff */
214 au0828_uninit_isoc(dev
);
216 dev
->isoc_ctl
.isoc_copy
= isoc_copy
;
217 dev
->isoc_ctl
.num_bufs
= num_bufs
;
219 dev
->isoc_ctl
.urb
= kzalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
220 if (!dev
->isoc_ctl
.urb
) {
221 au0828_isocdbg("cannot alloc memory for usb buffers\n");
225 dev
->isoc_ctl
.transfer_buffer
= kzalloc(sizeof(void *)*num_bufs
,
227 if (!dev
->isoc_ctl
.transfer_buffer
) {
228 au0828_isocdbg("cannot allocate memory for usb transfer\n");
229 kfree(dev
->isoc_ctl
.urb
);
233 dev
->isoc_ctl
.max_pkt_size
= max_pkt_size
;
234 dev
->isoc_ctl
.buf
= NULL
;
236 sb_size
= max_packets
* dev
->isoc_ctl
.max_pkt_size
;
238 /* allocate urbs and transfer buffers */
239 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
240 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
242 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i
);
243 au0828_uninit_isoc(dev
);
246 dev
->isoc_ctl
.urb
[i
] = urb
;
248 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->usbdev
,
249 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
250 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
251 printk("unable to allocate %i bytes for transfer"
254 in_interrupt() ? " while in int" : "");
255 au0828_uninit_isoc(dev
);
258 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
260 pipe
= usb_rcvisocpipe(dev
->usbdev
,
261 dev
->isoc_in_endpointaddr
),
263 usb_fill_int_urb(urb
, dev
->usbdev
, pipe
,
264 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
265 au0828_irq_callback
, dma_q
, 1);
267 urb
->number_of_packets
= max_packets
;
268 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
271 for (j
= 0; j
< max_packets
; j
++) {
272 urb
->iso_frame_desc
[j
].offset
= k
;
273 urb
->iso_frame_desc
[j
].length
=
274 dev
->isoc_ctl
.max_pkt_size
;
275 k
+= dev
->isoc_ctl
.max_pkt_size
;
279 init_waitqueue_head(&dma_q
->wq
);
281 /* submit urbs and enables IRQ */
282 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
283 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
285 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
287 au0828_uninit_isoc(dev
);
296 * Announces that a buffer were filled and request the next
298 static inline void buffer_filled(struct au0828_dev
*dev
,
299 struct au0828_dmaqueue
*dma_q
,
300 struct au0828_buffer
*buf
)
302 /* Advice that buffer was filled */
303 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
305 buf
->vb
.state
= VIDEOBUF_DONE
;
306 buf
->vb
.field_count
++;
307 v4l2_get_timestamp(&buf
->vb
.ts
);
309 dev
->isoc_ctl
.buf
= NULL
;
311 list_del(&buf
->vb
.queue
);
312 wake_up(&buf
->vb
.done
);
315 static inline void vbi_buffer_filled(struct au0828_dev
*dev
,
316 struct au0828_dmaqueue
*dma_q
,
317 struct au0828_buffer
*buf
)
319 /* Advice that buffer was filled */
320 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
322 buf
->vb
.state
= VIDEOBUF_DONE
;
323 buf
->vb
.field_count
++;
324 v4l2_get_timestamp(&buf
->vb
.ts
);
326 dev
->isoc_ctl
.vbi_buf
= NULL
;
328 list_del(&buf
->vb
.queue
);
329 wake_up(&buf
->vb
.done
);
333 * Identify the buffer header type and properly handles
335 static void au0828_copy_video(struct au0828_dev
*dev
,
336 struct au0828_dmaqueue
*dma_q
,
337 struct au0828_buffer
*buf
,
339 unsigned char *outp
, unsigned long len
)
341 void *fieldstart
, *startwrite
, *startread
;
342 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
343 int bytesperline
= dev
->width
<< 1; /* Assumes 16-bit depth @@@@ */
348 if (dma_q
->pos
+ len
> buf
->vb
.size
)
349 len
= buf
->vb
.size
- dma_q
->pos
;
354 /* Interlaces frame */
358 fieldstart
= outp
+ bytesperline
;
360 linesdone
= dma_q
->pos
/ bytesperline
;
361 currlinedone
= dma_q
->pos
% bytesperline
;
362 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
363 startwrite
= fieldstart
+ offset
;
364 lencopy
= bytesperline
- currlinedone
;
365 lencopy
= lencopy
> remain
? remain
: lencopy
;
367 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
368 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
369 ((char *)startwrite
+ lencopy
) -
370 ((char *)outp
+ buf
->vb
.size
));
371 remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
376 memcpy(startwrite
, startread
, lencopy
);
381 startwrite
+= lencopy
+ bytesperline
;
382 startread
+= lencopy
;
383 if (bytesperline
> remain
)
386 lencopy
= bytesperline
;
388 if ((char *)startwrite
+ lencopy
> (char *)outp
+
390 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
391 ((char *)startwrite
+ lencopy
) -
392 ((char *)outp
+ buf
->vb
.size
));
393 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
399 memcpy(startwrite
, startread
, lencopy
);
405 /* We have enough data to check for greenscreen */
406 if (outp
[0] < 0x60 && outp
[1440] < 0x60)
407 dev
->greenscreen_detected
= 1;
414 * video-buf generic routine to get the next available buffer
416 static inline void get_next_buf(struct au0828_dmaqueue
*dma_q
,
417 struct au0828_buffer
**buf
)
419 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
421 if (list_empty(&dma_q
->active
)) {
422 au0828_isocdbg("No active queue to serve\n");
423 dev
->isoc_ctl
.buf
= NULL
;
428 /* Get the next buffer */
429 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, vb
.queue
);
430 dev
->isoc_ctl
.buf
= *buf
;
435 static void au0828_copy_vbi(struct au0828_dev
*dev
,
436 struct au0828_dmaqueue
*dma_q
,
437 struct au0828_buffer
*buf
,
439 unsigned char *outp
, unsigned long len
)
441 unsigned char *startwrite
, *startread
;
446 au0828_isocdbg("dev is null\n");
451 au0828_isocdbg("dma_q is null\n");
457 au0828_isocdbg("p is null\n");
461 au0828_isocdbg("outp is null\n");
465 bytesperline
= dev
->vbi_width
;
467 if (dma_q
->pos
+ len
> buf
->vb
.size
)
468 len
= buf
->vb
.size
- dma_q
->pos
;
471 startwrite
= outp
+ (dma_q
->pos
/ 2);
473 /* Make sure the bottom field populates the second half of the frame */
474 if (buf
->top_field
== 0)
475 startwrite
+= bytesperline
* dev
->vbi_height
;
477 for (i
= 0; i
< len
; i
+= 2)
478 startwrite
[j
++] = startread
[i
+1];
485 * video-buf generic routine to get the next available VBI buffer
487 static inline void vbi_get_next_buf(struct au0828_dmaqueue
*dma_q
,
488 struct au0828_buffer
**buf
)
490 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vbiq
);
493 if (list_empty(&dma_q
->active
)) {
494 au0828_isocdbg("No active queue to serve\n");
495 dev
->isoc_ctl
.vbi_buf
= NULL
;
500 /* Get the next buffer */
501 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, vb
.queue
);
502 /* Cleans up buffer - Useful for testing for frame/URB loss */
503 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
504 memset(outp
, 0x00, (*buf
)->vb
.size
);
506 dev
->isoc_ctl
.vbi_buf
= *buf
;
512 * Controls the isoc copy of each urb packet
514 static inline int au0828_isoc_copy(struct au0828_dev
*dev
, struct urb
*urb
)
516 struct au0828_buffer
*buf
;
517 struct au0828_buffer
*vbi_buf
;
518 struct au0828_dmaqueue
*dma_q
= urb
->context
;
519 struct au0828_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
520 unsigned char *outp
= NULL
;
521 unsigned char *vbioutp
= NULL
;
522 int i
, len
= 0, rc
= 1;
525 unsigned int vbi_field_size
;
526 unsigned int remain
, lencopy
;
531 if ((dev
->dev_state
& DEV_DISCONNECTED
) ||
532 (dev
->dev_state
& DEV_MISCONFIGURED
))
535 if (urb
->status
< 0) {
536 print_err_status(dev
, -1, urb
->status
);
537 if (urb
->status
== -ENOENT
)
541 buf
= dev
->isoc_ctl
.buf
;
543 outp
= videobuf_to_vmalloc(&buf
->vb
);
545 vbi_buf
= dev
->isoc_ctl
.vbi_buf
;
547 vbioutp
= videobuf_to_vmalloc(&vbi_buf
->vb
);
549 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
550 int status
= urb
->iso_frame_desc
[i
].status
;
553 print_err_status(dev
, i
, status
);
554 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
558 if (urb
->iso_frame_desc
[i
].actual_length
<= 0)
561 if (urb
->iso_frame_desc
[i
].actual_length
>
563 au0828_isocdbg("packet bigger than packet size");
567 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
569 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
575 au0828_isocdbg("Video frame %s\n",
576 (fbyte
& 0x40) ? "odd" : "even");
580 vbi_buffer_filled(dev
,
583 vbi_get_next_buf(vbi_dma_q
, &vbi_buf
);
587 vbioutp
= videobuf_to_vmalloc(
592 buffer_filled(dev
, dma_q
, buf
);
593 get_next_buf(dma_q
, &buf
);
597 outp
= videobuf_to_vmalloc(&buf
->vb
);
599 /* As long as isoc traffic is arriving, keep
600 resetting the timer */
601 if (dev
->vid_timeout_running
)
602 mod_timer(&dev
->vid_timeout
,
603 jiffies
+ (HZ
/ 10));
604 if (dev
->vbi_timeout_running
)
605 mod_timer(&dev
->vbi_timeout
,
606 jiffies
+ (HZ
/ 10));
616 if (vbi_buf
!= NULL
) {
618 vbi_buf
->top_field
= 1;
620 vbi_buf
->top_field
= 0;
628 vbi_field_size
= dev
->vbi_width
* dev
->vbi_height
* 2;
629 if (dev
->vbi_read
< vbi_field_size
) {
630 remain
= vbi_field_size
- dev
->vbi_read
;
637 au0828_copy_vbi(dev
, vbi_dma_q
, vbi_buf
, p
,
642 dev
->vbi_read
+= lencopy
;
645 if (dev
->vbi_read
>= vbi_field_size
&& buf
!= NULL
)
646 au0828_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
652 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
655 struct au0828_fh
*fh
= vq
->priv_data
;
656 *size
= (fh
->dev
->width
* fh
->dev
->height
* 16 + 7) >> 3;
659 *count
= AU0828_DEF_BUF
;
661 if (*count
< AU0828_MIN_BUF
)
662 *count
= AU0828_MIN_BUF
;
666 /* This is called *without* dev->slock held; please keep it that way */
667 static void free_buffer(struct videobuf_queue
*vq
, struct au0828_buffer
*buf
)
669 struct au0828_fh
*fh
= vq
->priv_data
;
670 struct au0828_dev
*dev
= fh
->dev
;
671 unsigned long flags
= 0;
675 /* We used to wait for the buffer to finish here, but this didn't work
676 because, as we were keeping the state as VIDEOBUF_QUEUED,
677 videobuf_queue_cancel marked it as finished for us.
678 (Also, it could wedge forever if the hardware was misconfigured.)
680 This should be safe; by the time we get here, the buffer isn't
681 queued anymore. If we ever start marking the buffers as
682 VIDEOBUF_ACTIVE, it won't be, though.
684 spin_lock_irqsave(&dev
->slock
, flags
);
685 if (dev
->isoc_ctl
.buf
== buf
)
686 dev
->isoc_ctl
.buf
= NULL
;
687 spin_unlock_irqrestore(&dev
->slock
, flags
);
689 videobuf_vmalloc_free(&buf
->vb
);
690 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
694 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
695 enum v4l2_field field
)
697 struct au0828_fh
*fh
= vq
->priv_data
;
698 struct au0828_buffer
*buf
= container_of(vb
, struct au0828_buffer
, vb
);
699 struct au0828_dev
*dev
= fh
->dev
;
700 int rc
= 0, urb_init
= 0;
702 buf
->vb
.size
= (fh
->dev
->width
* fh
->dev
->height
* 16 + 7) >> 3;
704 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
707 buf
->vb
.width
= dev
->width
;
708 buf
->vb
.height
= dev
->height
;
709 buf
->vb
.field
= field
;
711 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
712 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
714 printk(KERN_INFO
"videobuf_iolock failed\n");
719 if (!dev
->isoc_ctl
.num_bufs
)
723 rc
= au0828_init_isoc(dev
, AU0828_ISO_PACKETS_PER_URB
,
724 AU0828_MAX_ISO_BUFS
, dev
->max_pkt_size
,
727 printk(KERN_INFO
"au0828_init_isoc failed\n");
732 buf
->vb
.state
= VIDEOBUF_PREPARED
;
736 free_buffer(vq
, buf
);
741 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
743 struct au0828_buffer
*buf
= container_of(vb
,
744 struct au0828_buffer
,
746 struct au0828_fh
*fh
= vq
->priv_data
;
747 struct au0828_dev
*dev
= fh
->dev
;
748 struct au0828_dmaqueue
*vidq
= &dev
->vidq
;
750 buf
->vb
.state
= VIDEOBUF_QUEUED
;
751 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
754 static void buffer_release(struct videobuf_queue
*vq
,
755 struct videobuf_buffer
*vb
)
757 struct au0828_buffer
*buf
= container_of(vb
,
758 struct au0828_buffer
,
761 free_buffer(vq
, buf
);
764 static struct videobuf_queue_ops au0828_video_qops
= {
765 .buf_setup
= buffer_setup
,
766 .buf_prepare
= buffer_prepare
,
767 .buf_queue
= buffer_queue
,
768 .buf_release
= buffer_release
,
771 /* ------------------------------------------------------------------
773 ------------------------------------------------------------------*/
775 static int au0828_i2s_init(struct au0828_dev
*dev
)
777 /* Enable i2s mode */
778 au0828_writereg(dev
, AU0828_AUDIOCTRL_50C
, 0x01);
783 * Auvitek au0828 analog stream enable
784 * Please set interface0 to AS5 before enable the stream
786 static int au0828_analog_stream_enable(struct au0828_dev
*d
)
788 dprintk(1, "au0828_analog_stream_enable called\n");
789 au0828_writereg(d
, AU0828_SENSORCTRL_VBI_103
, 0x00);
790 au0828_writereg(d
, 0x106, 0x00);
792 au0828_writereg(d
, 0x110, 0x00);
793 au0828_writereg(d
, 0x111, 0x00);
794 au0828_writereg(d
, 0x114, 0xa0);
795 au0828_writereg(d
, 0x115, 0x05);
797 au0828_writereg(d
, 0x112, 0x00);
798 au0828_writereg(d
, 0x113, 0x00);
799 au0828_writereg(d
, 0x116, 0xf2);
800 au0828_writereg(d
, 0x117, 0x00);
801 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0xb3);
806 int au0828_analog_stream_disable(struct au0828_dev
*d
)
808 dprintk(1, "au0828_analog_stream_disable called\n");
809 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0x0);
813 static void au0828_analog_stream_reset(struct au0828_dev
*dev
)
815 dprintk(1, "au0828_analog_stream_reset called\n");
816 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0x0);
818 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0xb3);
822 * Some operations needs to stop current streaming
824 static int au0828_stream_interrupt(struct au0828_dev
*dev
)
828 dev
->stream_state
= STREAM_INTERRUPT
;
829 if (dev
->dev_state
== DEV_DISCONNECTED
)
832 dev
->dev_state
= DEV_MISCONFIGURED
;
833 dprintk(1, "%s device is misconfigured!\n", __func__
);
840 * au0828_release_resources
841 * unregister v4l2 devices
843 void au0828_analog_unregister(struct au0828_dev
*dev
)
845 dprintk(1, "au0828_release_resources called\n");
846 mutex_lock(&au0828_sysfs_lock
);
849 video_unregister_device(dev
->vdev
);
851 video_unregister_device(dev
->vbi_dev
);
853 mutex_unlock(&au0828_sysfs_lock
);
857 /* Usage lock check functions */
858 static int res_get(struct au0828_fh
*fh
, unsigned int bit
)
860 struct au0828_dev
*dev
= fh
->dev
;
862 if (fh
->resources
& bit
)
863 /* have it already allocated */
867 if (dev
->resources
& bit
) {
868 /* no, someone else uses it */
871 /* it's free, grab it */
872 fh
->resources
|= bit
;
873 dev
->resources
|= bit
;
874 dprintk(1, "res: get %d\n", bit
);
879 static int res_check(struct au0828_fh
*fh
, unsigned int bit
)
881 return fh
->resources
& bit
;
884 static int res_locked(struct au0828_dev
*dev
, unsigned int bit
)
886 return dev
->resources
& bit
;
889 static void res_free(struct au0828_fh
*fh
, unsigned int bits
)
891 struct au0828_dev
*dev
= fh
->dev
;
893 BUG_ON((fh
->resources
& bits
) != bits
);
895 fh
->resources
&= ~bits
;
896 dev
->resources
&= ~bits
;
897 dprintk(1, "res: put %d\n", bits
);
900 static int get_ressource(struct au0828_fh
*fh
)
903 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
904 return AU0828_RESOURCE_VIDEO
;
905 case V4L2_BUF_TYPE_VBI_CAPTURE
:
906 return AU0828_RESOURCE_VBI
;
913 /* This function ensures that video frames continue to be delivered even if
914 the ITU-656 input isn't receiving any data (thereby preventing applications
915 such as tvtime from hanging) */
916 static void au0828_vid_buffer_timeout(unsigned long data
)
918 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
919 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
920 struct au0828_buffer
*buf
;
921 unsigned char *vid_data
;
922 unsigned long flags
= 0;
924 spin_lock_irqsave(&dev
->slock
, flags
);
926 buf
= dev
->isoc_ctl
.buf
;
928 vid_data
= videobuf_to_vmalloc(&buf
->vb
);
929 memset(vid_data
, 0x00, buf
->vb
.size
); /* Blank green frame */
930 buffer_filled(dev
, dma_q
, buf
);
932 get_next_buf(dma_q
, &buf
);
934 if (dev
->vid_timeout_running
== 1)
935 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
937 spin_unlock_irqrestore(&dev
->slock
, flags
);
940 static void au0828_vbi_buffer_timeout(unsigned long data
)
942 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
943 struct au0828_dmaqueue
*dma_q
= &dev
->vbiq
;
944 struct au0828_buffer
*buf
;
945 unsigned char *vbi_data
;
946 unsigned long flags
= 0;
948 spin_lock_irqsave(&dev
->slock
, flags
);
950 buf
= dev
->isoc_ctl
.vbi_buf
;
952 vbi_data
= videobuf_to_vmalloc(&buf
->vb
);
953 memset(vbi_data
, 0x00, buf
->vb
.size
);
954 vbi_buffer_filled(dev
, dma_q
, buf
);
956 vbi_get_next_buf(dma_q
, &buf
);
958 if (dev
->vbi_timeout_running
== 1)
959 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
960 spin_unlock_irqrestore(&dev
->slock
, flags
);
964 static int au0828_v4l2_open(struct file
*filp
)
967 struct video_device
*vdev
= video_devdata(filp
);
968 struct au0828_dev
*dev
= video_drvdata(filp
);
969 struct au0828_fh
*fh
;
972 switch (vdev
->vfl_type
) {
973 case VFL_TYPE_GRABBER
:
974 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
977 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
983 fh
= kzalloc(sizeof(struct au0828_fh
), GFP_KERNEL
);
985 dprintk(1, "Failed allocate au0828_fh struct!\n");
991 filp
->private_data
= fh
;
993 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
&& dev
->users
== 0) {
994 /* set au0828 interface0 to AS5 here again */
995 ret
= usb_set_interface(dev
->usbdev
, 0, 5);
997 printk(KERN_INFO
"Au0828 can't set alternate to 5!\n");
1000 dev
->width
= NTSC_STD_W
;
1001 dev
->height
= NTSC_STD_H
;
1002 dev
->frame_size
= dev
->width
* dev
->height
* 2;
1003 dev
->field_size
= dev
->width
* dev
->height
;
1004 dev
->bytesperline
= dev
->width
* 2;
1006 au0828_analog_stream_enable(dev
);
1007 au0828_analog_stream_reset(dev
);
1009 /* If we were doing ac97 instead of i2s, it would go here...*/
1010 au0828_i2s_init(dev
);
1012 dev
->stream_state
= STREAM_OFF
;
1013 dev
->dev_state
|= DEV_INITIALIZED
;
1018 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &au0828_video_qops
,
1020 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1021 V4L2_FIELD_INTERLACED
,
1022 sizeof(struct au0828_buffer
), fh
,
1026 dev
->vbi_width
= 720;
1027 dev
->vbi_height
= 1;
1028 videobuf_queue_vmalloc_init(&fh
->vb_vbiq
, &au0828_vbi_qops
,
1030 V4L2_BUF_TYPE_VBI_CAPTURE
,
1032 sizeof(struct au0828_buffer
), fh
,
1037 static int au0828_v4l2_close(struct file
*filp
)
1040 struct au0828_fh
*fh
= filp
->private_data
;
1041 struct au0828_dev
*dev
= fh
->dev
;
1043 if (res_check(fh
, AU0828_RESOURCE_VIDEO
)) {
1044 /* Cancel timeout thread in case they didn't call streamoff */
1045 dev
->vid_timeout_running
= 0;
1046 del_timer_sync(&dev
->vid_timeout
);
1048 videobuf_stop(&fh
->vb_vidq
);
1049 res_free(fh
, AU0828_RESOURCE_VIDEO
);
1052 if (res_check(fh
, AU0828_RESOURCE_VBI
)) {
1053 /* Cancel timeout thread in case they didn't call streamoff */
1054 dev
->vbi_timeout_running
= 0;
1055 del_timer_sync(&dev
->vbi_timeout
);
1057 videobuf_stop(&fh
->vb_vbiq
);
1058 res_free(fh
, AU0828_RESOURCE_VBI
);
1061 if (dev
->users
== 1) {
1062 if (dev
->dev_state
& DEV_DISCONNECTED
) {
1063 au0828_analog_unregister(dev
);
1068 au0828_analog_stream_disable(dev
);
1070 au0828_uninit_isoc(dev
);
1072 /* Save some power by putting tuner to sleep */
1073 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_power
, 0);
1075 /* When close the device, set the usb intf0 into alt0 to free
1077 ret
= usb_set_interface(dev
->usbdev
, 0, 0);
1079 printk(KERN_INFO
"Au0828 can't set alternate to 0!\n");
1082 videobuf_mmap_free(&fh
->vb_vidq
);
1083 videobuf_mmap_free(&fh
->vb_vbiq
);
1086 wake_up_interruptible_nr(&dev
->open
, 1);
1090 static ssize_t
au0828_v4l2_read(struct file
*filp
, char __user
*buf
,
1091 size_t count
, loff_t
*pos
)
1093 struct au0828_fh
*fh
= filp
->private_data
;
1094 struct au0828_dev
*dev
= fh
->dev
;
1097 rc
= check_dev(dev
);
1101 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1102 if (res_locked(dev
, AU0828_RESOURCE_VIDEO
))
1105 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
1106 filp
->f_flags
& O_NONBLOCK
);
1109 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1110 if (!res_get(fh
, AU0828_RESOURCE_VBI
))
1113 if (dev
->vbi_timeout_running
== 0) {
1114 /* Handle case where caller tries to read without
1115 calling streamon first */
1116 dev
->vbi_timeout_running
= 1;
1117 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1120 return videobuf_read_stream(&fh
->vb_vbiq
, buf
, count
, pos
, 0,
1121 filp
->f_flags
& O_NONBLOCK
);
1127 static unsigned int au0828_v4l2_poll(struct file
*filp
, poll_table
*wait
)
1129 struct au0828_fh
*fh
= filp
->private_data
;
1130 struct au0828_dev
*dev
= fh
->dev
;
1133 rc
= check_dev(dev
);
1137 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1138 if (!res_get(fh
, AU0828_RESOURCE_VIDEO
))
1140 return videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
1141 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1142 if (!res_get(fh
, AU0828_RESOURCE_VBI
))
1144 return videobuf_poll_stream(filp
, &fh
->vb_vbiq
, wait
);
1150 static int au0828_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1152 struct au0828_fh
*fh
= filp
->private_data
;
1153 struct au0828_dev
*dev
= fh
->dev
;
1156 rc
= check_dev(dev
);
1160 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1161 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1162 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1163 rc
= videobuf_mmap_mapper(&fh
->vb_vbiq
, vma
);
1168 static int au0828_set_format(struct au0828_dev
*dev
, unsigned int cmd
,
1169 struct v4l2_format
*format
)
1172 int width
= format
->fmt
.pix
.width
;
1173 int height
= format
->fmt
.pix
.height
;
1175 if (format
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1178 /* If they are demanding a format other than the one we support,
1179 bail out (tvtime asks for UYVY and then retries with YUYV) */
1180 if (format
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_UYVY
)
1183 /* format->fmt.pix.width only support 720 and height 480 */
1189 format
->fmt
.pix
.width
= width
;
1190 format
->fmt
.pix
.height
= height
;
1191 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1192 format
->fmt
.pix
.bytesperline
= width
* 2;
1193 format
->fmt
.pix
.sizeimage
= width
* height
* 2;
1194 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1195 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1197 if (cmd
== VIDIOC_TRY_FMT
)
1200 /* maybe set new image format, driver current only support 720*480 */
1202 dev
->height
= height
;
1203 dev
->frame_size
= width
* height
* 2;
1204 dev
->field_size
= width
* height
;
1205 dev
->bytesperline
= width
* 2;
1207 if (dev
->stream_state
== STREAM_ON
) {
1208 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1209 ret
= au0828_stream_interrupt(dev
);
1211 dprintk(1, "error interrupting video stream!\n");
1216 /* set au0828 interface0 to AS5 here again */
1217 ret
= usb_set_interface(dev
->usbdev
, 0, 5);
1219 printk(KERN_INFO
"Au0828 can't set alt setting to 5!\n");
1223 au0828_analog_stream_enable(dev
);
1229 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1230 struct v4l2_queryctrl
*qc
)
1232 struct au0828_fh
*fh
= priv
;
1233 struct au0828_dev
*dev
= fh
->dev
;
1234 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, queryctrl
, qc
);
1241 static int vidioc_querycap(struct file
*file
, void *priv
,
1242 struct v4l2_capability
*cap
)
1244 struct video_device
*vdev
= video_devdata(file
);
1245 struct au0828_fh
*fh
= priv
;
1246 struct au0828_dev
*dev
= fh
->dev
;
1248 strlcpy(cap
->driver
, "au0828", sizeof(cap
->driver
));
1249 strlcpy(cap
->card
, dev
->board
.name
, sizeof(cap
->card
));
1250 usb_make_path(dev
->usbdev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1252 /* set the device capabilities */
1253 cap
->device_caps
= V4L2_CAP_AUDIO
|
1254 V4L2_CAP_READWRITE
|
1255 V4L2_CAP_STREAMING
|
1257 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
1258 cap
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
;
1260 cap
->device_caps
|= V4L2_CAP_VBI_CAPTURE
;
1261 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
|
1262 V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_VIDEO_CAPTURE
;
1266 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1267 struct v4l2_fmtdesc
*f
)
1272 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1273 strcpy(f
->description
, "Packed YUV2");
1276 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
1281 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1282 struct v4l2_format
*f
)
1284 struct au0828_fh
*fh
= priv
;
1285 struct au0828_dev
*dev
= fh
->dev
;
1287 f
->fmt
.pix
.width
= dev
->width
;
1288 f
->fmt
.pix
.height
= dev
->height
;
1289 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1290 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
1291 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
1292 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
; /* NTSC/PAL */
1293 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1297 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1298 struct v4l2_format
*f
)
1300 struct au0828_fh
*fh
= priv
;
1301 struct au0828_dev
*dev
= fh
->dev
;
1303 return au0828_set_format(dev
, VIDIOC_TRY_FMT
, f
);
1306 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1307 struct v4l2_format
*f
)
1309 struct au0828_fh
*fh
= priv
;
1310 struct au0828_dev
*dev
= fh
->dev
;
1313 rc
= check_dev(dev
);
1317 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1318 printk(KERN_INFO
"%s queue busy\n", __func__
);
1323 rc
= au0828_set_format(dev
, VIDIOC_S_FMT
, f
);
1328 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
1330 struct au0828_fh
*fh
= priv
;
1331 struct au0828_dev
*dev
= fh
->dev
;
1333 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1334 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 1);
1336 /* FIXME: when we support something other than NTSC, we are going to
1337 have to make the au0828 bridge adjust the size of its capture
1338 buffer, which is currently hardcoded at 720x480 */
1340 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, norm
);
1341 dev
->std_set_in_tuner_core
= 1;
1343 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1344 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 0);
1349 static int vidioc_enum_input(struct file
*file
, void *priv
,
1350 struct v4l2_input
*input
)
1352 struct au0828_fh
*fh
= priv
;
1353 struct au0828_dev
*dev
= fh
->dev
;
1356 static const char *inames
[] = {
1357 [AU0828_VMUX_UNDEFINED
] = "Undefined",
1358 [AU0828_VMUX_COMPOSITE
] = "Composite",
1359 [AU0828_VMUX_SVIDEO
] = "S-Video",
1360 [AU0828_VMUX_CABLE
] = "Cable TV",
1361 [AU0828_VMUX_TELEVISION
] = "Television",
1362 [AU0828_VMUX_DVB
] = "DVB",
1363 [AU0828_VMUX_DEBUG
] = "tv debug"
1368 if (tmp
>= AU0828_MAX_INPUT
)
1370 if (AUVI_INPUT(tmp
).type
== 0)
1374 strcpy(input
->name
, inames
[AUVI_INPUT(tmp
).type
]);
1375 if ((AUVI_INPUT(tmp
).type
== AU0828_VMUX_TELEVISION
) ||
1376 (AUVI_INPUT(tmp
).type
== AU0828_VMUX_CABLE
)) {
1377 input
->type
|= V4L2_INPUT_TYPE_TUNER
;
1378 input
->audioset
= 1;
1380 input
->type
|= V4L2_INPUT_TYPE_CAMERA
;
1381 input
->audioset
= 2;
1384 input
->std
= dev
->vdev
->tvnorms
;
1389 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1391 struct au0828_fh
*fh
= priv
;
1392 struct au0828_dev
*dev
= fh
->dev
;
1393 *i
= dev
->ctrl_input
;
1397 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int index
)
1399 struct au0828_fh
*fh
= priv
;
1400 struct au0828_dev
*dev
= fh
->dev
;
1403 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__
,
1405 if (index
>= AU0828_MAX_INPUT
)
1407 if (AUVI_INPUT(index
).type
== 0)
1409 dev
->ctrl_input
= index
;
1411 switch (AUVI_INPUT(index
).type
) {
1412 case AU0828_VMUX_SVIDEO
:
1413 dev
->input_type
= AU0828_VMUX_SVIDEO
;
1414 dev
->ctrl_ainput
= 1;
1416 case AU0828_VMUX_COMPOSITE
:
1417 dev
->input_type
= AU0828_VMUX_COMPOSITE
;
1418 dev
->ctrl_ainput
= 1;
1420 case AU0828_VMUX_TELEVISION
:
1421 dev
->input_type
= AU0828_VMUX_TELEVISION
;
1422 dev
->ctrl_ainput
= 0;
1425 dprintk(1, "VIDIOC_S_INPUT unknown input type set [%d]\n",
1426 AUVI_INPUT(index
).type
);
1430 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
1431 AUVI_INPUT(index
).vmux
, 0, 0);
1433 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1435 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1443 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1445 /* Make sure we leave it turned on if some
1446 other input is routed to this callback */
1447 if ((AUVI_INPUT(i
).audio_setup
) !=
1448 ((AUVI_INPUT(index
).audio_setup
))) {
1449 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1454 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
1455 AUVI_INPUT(index
).amux
, 0, 0);
1459 static int vidioc_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1465 strcpy(a
->name
, "Television");
1467 strcpy(a
->name
, "Line in");
1469 a
->capability
= V4L2_AUDCAP_STEREO
;
1473 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1475 struct au0828_fh
*fh
= priv
;
1476 struct au0828_dev
*dev
= fh
->dev
;
1478 a
->index
= dev
->ctrl_ainput
;
1480 strcpy(a
->name
, "Television");
1482 strcpy(a
->name
, "Line in");
1484 a
->capability
= V4L2_AUDCAP_STEREO
;
1488 static int vidioc_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*a
)
1490 struct au0828_fh
*fh
= priv
;
1491 struct au0828_dev
*dev
= fh
->dev
;
1493 if (a
->index
!= dev
->ctrl_ainput
)
1498 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1499 struct v4l2_control
*ctrl
)
1501 struct au0828_fh
*fh
= priv
;
1502 struct au0828_dev
*dev
= fh
->dev
;
1504 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_ctrl
, ctrl
);
1509 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1510 struct v4l2_control
*ctrl
)
1512 struct au0828_fh
*fh
= priv
;
1513 struct au0828_dev
*dev
= fh
->dev
;
1514 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_ctrl
, ctrl
);
1518 static int vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1520 struct au0828_fh
*fh
= priv
;
1521 struct au0828_dev
*dev
= fh
->dev
;
1526 strcpy(t
->name
, "Auvitek tuner");
1527 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1531 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1532 const struct v4l2_tuner
*t
)
1534 struct au0828_fh
*fh
= priv
;
1535 struct au0828_dev
*dev
= fh
->dev
;
1540 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1541 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 1);
1543 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1545 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1546 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 0);
1548 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t
->signal
,
1555 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1556 struct v4l2_frequency
*freq
)
1558 struct au0828_fh
*fh
= priv
;
1559 struct au0828_dev
*dev
= fh
->dev
;
1561 if (freq
->tuner
!= 0)
1563 freq
->frequency
= dev
->ctrl_freq
;
1567 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1568 const struct v4l2_frequency
*freq
)
1570 struct au0828_fh
*fh
= priv
;
1571 struct au0828_dev
*dev
= fh
->dev
;
1572 struct v4l2_frequency new_freq
= *freq
;
1574 if (freq
->tuner
!= 0)
1577 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1578 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 1);
1580 if (dev
->std_set_in_tuner_core
== 0) {
1581 /* If we've never sent the standard in tuner core, do so now.
1582 We don't do this at device probe because we don't want to
1583 incur the cost of a firmware load */
1584 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
,
1585 dev
->vdev
->tvnorms
);
1586 dev
->std_set_in_tuner_core
= 1;
1589 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, freq
);
1590 /* Get the actual set (and possibly clamped) frequency */
1591 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, &new_freq
);
1592 dev
->ctrl_freq
= new_freq
.frequency
;
1594 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
1595 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, 0);
1597 au0828_analog_stream_reset(dev
);
1603 /* RAW VBI ioctls */
1605 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1606 struct v4l2_format
*format
)
1608 struct au0828_fh
*fh
= priv
;
1609 struct au0828_dev
*dev
= fh
->dev
;
1611 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1612 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1613 format
->fmt
.vbi
.offset
= 0;
1614 format
->fmt
.vbi
.flags
= 0;
1615 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1617 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1618 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1619 format
->fmt
.vbi
.start
[0] = 21;
1620 format
->fmt
.vbi
.start
[1] = 284;
1625 static int vidioc_g_chip_ident(struct file
*file
, void *priv
,
1626 struct v4l2_dbg_chip_ident
*chip
)
1628 struct au0828_fh
*fh
= priv
;
1629 struct au0828_dev
*dev
= fh
->dev
;
1630 chip
->ident
= V4L2_IDENT_NONE
;
1633 if (v4l2_chip_match_host(&chip
->match
)) {
1634 chip
->ident
= V4L2_IDENT_AU0828
;
1638 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_chip_ident
, chip
);
1639 if (chip
->ident
== V4L2_IDENT_NONE
)
1645 static int vidioc_cropcap(struct file
*file
, void *priv
,
1646 struct v4l2_cropcap
*cc
)
1648 struct au0828_fh
*fh
= priv
;
1649 struct au0828_dev
*dev
= fh
->dev
;
1651 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1654 cc
->bounds
.left
= 0;
1656 cc
->bounds
.width
= dev
->width
;
1657 cc
->bounds
.height
= dev
->height
;
1659 cc
->defrect
= cc
->bounds
;
1661 cc
->pixelaspect
.numerator
= 54;
1662 cc
->pixelaspect
.denominator
= 59;
1667 static int vidioc_streamon(struct file
*file
, void *priv
,
1668 enum v4l2_buf_type type
)
1670 struct au0828_fh
*fh
= priv
;
1671 struct au0828_dev
*dev
= fh
->dev
;
1674 rc
= check_dev(dev
);
1678 if (unlikely(type
!= fh
->type
))
1681 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1682 fh
, type
, fh
->resources
, dev
->resources
);
1684 if (unlikely(!res_get(fh
, get_ressource(fh
))))
1687 if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1688 au0828_analog_stream_enable(dev
);
1689 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 1);
1692 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1693 rc
= videobuf_streamon(&fh
->vb_vidq
);
1694 dev
->vid_timeout_running
= 1;
1695 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
1696 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1697 rc
= videobuf_streamon(&fh
->vb_vbiq
);
1698 dev
->vbi_timeout_running
= 1;
1699 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1705 static int vidioc_streamoff(struct file
*file
, void *priv
,
1706 enum v4l2_buf_type type
)
1708 struct au0828_fh
*fh
= priv
;
1709 struct au0828_dev
*dev
= fh
->dev
;
1713 rc
= check_dev(dev
);
1717 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1718 fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
)
1720 if (type
!= fh
->type
)
1723 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1724 fh
, type
, fh
->resources
, dev
->resources
);
1726 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1727 dev
->vid_timeout_running
= 0;
1728 del_timer_sync(&dev
->vid_timeout
);
1730 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 0);
1731 rc
= au0828_stream_interrupt(dev
);
1735 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1736 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1738 (AUVI_INPUT(i
).audio_setup
)(dev
, 0);
1741 if (res_check(fh
, AU0828_RESOURCE_VIDEO
)) {
1742 videobuf_streamoff(&fh
->vb_vidq
);
1743 res_free(fh
, AU0828_RESOURCE_VIDEO
);
1745 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1746 dev
->vbi_timeout_running
= 0;
1747 del_timer_sync(&dev
->vbi_timeout
);
1749 if (res_check(fh
, AU0828_RESOURCE_VBI
)) {
1750 videobuf_streamoff(&fh
->vb_vbiq
);
1751 res_free(fh
, AU0828_RESOURCE_VBI
);
1758 #ifdef CONFIG_VIDEO_ADV_DEBUG
1759 static int vidioc_g_register(struct file
*file
, void *priv
,
1760 struct v4l2_dbg_register
*reg
)
1762 struct au0828_fh
*fh
= priv
;
1763 struct au0828_dev
*dev
= fh
->dev
;
1765 switch (reg
->match
.type
) {
1766 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1767 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, g_register
, reg
);
1770 if (!v4l2_chip_match_host(®
->match
))
1774 reg
->val
= au0828_read(dev
, reg
->reg
);
1778 static int vidioc_s_register(struct file
*file
, void *priv
,
1779 const struct v4l2_dbg_register
*reg
)
1781 struct au0828_fh
*fh
= priv
;
1782 struct au0828_dev
*dev
= fh
->dev
;
1784 switch (reg
->match
.type
) {
1785 case V4L2_CHIP_MATCH_I2C_DRIVER
:
1786 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_register
, reg
);
1789 if (!v4l2_chip_match_host(®
->match
))
1792 return au0828_writereg(dev
, reg
->reg
, reg
->val
);
1796 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1797 struct v4l2_requestbuffers
*rb
)
1799 struct au0828_fh
*fh
= priv
;
1800 struct au0828_dev
*dev
= fh
->dev
;
1803 rc
= check_dev(dev
);
1807 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1808 rc
= videobuf_reqbufs(&fh
->vb_vidq
, rb
);
1809 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1810 rc
= videobuf_reqbufs(&fh
->vb_vbiq
, rb
);
1815 static int vidioc_querybuf(struct file
*file
, void *priv
,
1816 struct v4l2_buffer
*b
)
1818 struct au0828_fh
*fh
= priv
;
1819 struct au0828_dev
*dev
= fh
->dev
;
1822 rc
= check_dev(dev
);
1826 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1827 rc
= videobuf_querybuf(&fh
->vb_vidq
, b
);
1828 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1829 rc
= videobuf_querybuf(&fh
->vb_vbiq
, b
);
1834 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1836 struct au0828_fh
*fh
= priv
;
1837 struct au0828_dev
*dev
= fh
->dev
;
1840 rc
= check_dev(dev
);
1844 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1845 rc
= videobuf_qbuf(&fh
->vb_vidq
, b
);
1846 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1847 rc
= videobuf_qbuf(&fh
->vb_vbiq
, b
);
1852 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1854 struct au0828_fh
*fh
= priv
;
1855 struct au0828_dev
*dev
= fh
->dev
;
1858 rc
= check_dev(dev
);
1862 /* Workaround for a bug in the au0828 hardware design that sometimes
1863 results in the colorspace being inverted */
1864 if (dev
->greenscreen_detected
== 1) {
1865 dprintk(1, "Detected green frame. Resetting stream...\n");
1866 au0828_analog_stream_reset(dev
);
1867 dev
->greenscreen_detected
= 0;
1870 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1871 rc
= videobuf_dqbuf(&fh
->vb_vidq
, b
, file
->f_flags
& O_NONBLOCK
);
1872 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1873 rc
= videobuf_dqbuf(&fh
->vb_vbiq
, b
, file
->f_flags
& O_NONBLOCK
);
1878 static struct v4l2_file_operations au0828_v4l_fops
= {
1879 .owner
= THIS_MODULE
,
1880 .open
= au0828_v4l2_open
,
1881 .release
= au0828_v4l2_close
,
1882 .read
= au0828_v4l2_read
,
1883 .poll
= au0828_v4l2_poll
,
1884 .mmap
= au0828_v4l2_mmap
,
1885 .unlocked_ioctl
= video_ioctl2
,
1888 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1889 .vidioc_querycap
= vidioc_querycap
,
1890 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1891 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1892 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1893 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1894 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1895 .vidioc_s_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1896 .vidioc_enumaudio
= vidioc_enumaudio
,
1897 .vidioc_g_audio
= vidioc_g_audio
,
1898 .vidioc_s_audio
= vidioc_s_audio
,
1899 .vidioc_cropcap
= vidioc_cropcap
,
1900 .vidioc_reqbufs
= vidioc_reqbufs
,
1901 .vidioc_querybuf
= vidioc_querybuf
,
1902 .vidioc_qbuf
= vidioc_qbuf
,
1903 .vidioc_dqbuf
= vidioc_dqbuf
,
1904 .vidioc_s_std
= vidioc_s_std
,
1905 .vidioc_enum_input
= vidioc_enum_input
,
1906 .vidioc_g_input
= vidioc_g_input
,
1907 .vidioc_s_input
= vidioc_s_input
,
1908 .vidioc_queryctrl
= vidioc_queryctrl
,
1909 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1910 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1911 .vidioc_streamon
= vidioc_streamon
,
1912 .vidioc_streamoff
= vidioc_streamoff
,
1913 .vidioc_g_tuner
= vidioc_g_tuner
,
1914 .vidioc_s_tuner
= vidioc_s_tuner
,
1915 .vidioc_g_frequency
= vidioc_g_frequency
,
1916 .vidioc_s_frequency
= vidioc_s_frequency
,
1917 #ifdef CONFIG_VIDEO_ADV_DEBUG
1918 .vidioc_g_register
= vidioc_g_register
,
1919 .vidioc_s_register
= vidioc_s_register
,
1921 .vidioc_g_chip_ident
= vidioc_g_chip_ident
,
1924 static const struct video_device au0828_video_template
= {
1925 .fops
= &au0828_v4l_fops
,
1926 .release
= video_device_release
,
1927 .ioctl_ops
= &video_ioctl_ops
,
1928 .tvnorms
= V4L2_STD_NTSC_M
,
1929 .current_norm
= V4L2_STD_NTSC_M
,
1932 /**************************************************************************/
1934 int au0828_analog_register(struct au0828_dev
*dev
,
1935 struct usb_interface
*interface
)
1937 int retval
= -ENOMEM
;
1938 struct usb_host_interface
*iface_desc
;
1939 struct usb_endpoint_descriptor
*endpoint
;
1942 dprintk(1, "au0828_analog_register called!\n");
1944 /* set au0828 usb interface0 to as5 */
1945 retval
= usb_set_interface(dev
->usbdev
,
1946 interface
->cur_altsetting
->desc
.bInterfaceNumber
, 5);
1948 printk(KERN_INFO
"Failure setting usb interface0 to as5\n");
1952 /* Figure out which endpoint has the isoc interface */
1953 iface_desc
= interface
->cur_altsetting
;
1954 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
1955 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1956 if (((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
1958 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
1959 == USB_ENDPOINT_XFER_ISOC
)) {
1961 /* we find our isoc in endpoint */
1962 u16 tmp
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1963 dev
->max_pkt_size
= (tmp
& 0x07ff) *
1964 (((tmp
& 0x1800) >> 11) + 1);
1965 dev
->isoc_in_endpointaddr
= endpoint
->bEndpointAddress
;
1968 if (!(dev
->isoc_in_endpointaddr
)) {
1969 printk(KERN_INFO
"Could not locate isoc endpoint\n");
1974 init_waitqueue_head(&dev
->open
);
1975 spin_lock_init(&dev
->slock
);
1977 /* init video dma queues */
1978 INIT_LIST_HEAD(&dev
->vidq
.active
);
1979 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1980 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1981 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1983 dev
->vid_timeout
.function
= au0828_vid_buffer_timeout
;
1984 dev
->vid_timeout
.data
= (unsigned long) dev
;
1985 init_timer(&dev
->vid_timeout
);
1987 dev
->vbi_timeout
.function
= au0828_vbi_buffer_timeout
;
1988 dev
->vbi_timeout
.data
= (unsigned long) dev
;
1989 init_timer(&dev
->vbi_timeout
);
1991 dev
->width
= NTSC_STD_W
;
1992 dev
->height
= NTSC_STD_H
;
1993 dev
->field_size
= dev
->width
* dev
->height
;
1994 dev
->frame_size
= dev
->field_size
<< 1;
1995 dev
->bytesperline
= dev
->width
<< 1;
1996 dev
->ctrl_ainput
= 0;
1997 dev
->ctrl_freq
= 960;
1999 /* allocate and fill v4l2 video struct */
2000 dev
->vdev
= video_device_alloc();
2001 if (NULL
== dev
->vdev
) {
2002 dprintk(1, "Can't allocate video_device.\n");
2006 /* allocate the VBI struct */
2007 dev
->vbi_dev
= video_device_alloc();
2008 if (NULL
== dev
->vbi_dev
) {
2009 dprintk(1, "Can't allocate vbi_device.\n");
2014 /* Fill the video capture device struct */
2015 *dev
->vdev
= au0828_video_template
;
2016 dev
->vdev
->parent
= &dev
->usbdev
->dev
;
2017 dev
->vdev
->lock
= &dev
->lock
;
2018 strcpy(dev
->vdev
->name
, "au0828a video");
2020 /* Setup the VBI device */
2021 *dev
->vbi_dev
= au0828_video_template
;
2022 dev
->vbi_dev
->parent
= &dev
->usbdev
->dev
;
2023 dev
->vbi_dev
->lock
= &dev
->lock
;
2024 strcpy(dev
->vbi_dev
->name
, "au0828a vbi");
2026 /* Register the v4l2 device */
2027 video_set_drvdata(dev
->vdev
, dev
);
2028 retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
, -1);
2030 dprintk(1, "unable to register video device (error = %d).\n",
2036 /* Register the vbi device */
2037 video_set_drvdata(dev
->vbi_dev
, dev
);
2038 retval
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
, -1);
2040 dprintk(1, "unable to register vbi device (error = %d).\n",
2046 dprintk(1, "%s completed!\n", __func__
);
2051 video_device_release(dev
->vbi_dev
);
2053 video_device_release(dev
->vdev
);