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/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 i2c_gate_ctrl(struct au0828_dev
*dev
, int val
)
63 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
64 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, val
);
67 static inline void print_err_status(struct au0828_dev
*dev
,
68 int packet
, int status
)
70 char *errmsg
= "Unknown";
74 errmsg
= "unlinked synchronuously";
77 errmsg
= "unlinked asynchronuously";
80 errmsg
= "Buffer error (overrun)";
83 errmsg
= "Stalled (device not responding)";
86 errmsg
= "Babble (bad cable?)";
89 errmsg
= "Bit-stuff error (bad cable?)";
92 errmsg
= "CRC/Timeout (could be anything)";
95 errmsg
= "Device does not respond";
99 au0828_isocdbg("URB status %d [%s].\n", status
, errmsg
);
101 au0828_isocdbg("URB packet %d, status %d [%s].\n",
102 packet
, status
, errmsg
);
106 static int check_dev(struct au0828_dev
*dev
)
108 if (dev
->dev_state
& DEV_DISCONNECTED
) {
109 printk(KERN_INFO
"v4l2 ioctl: device not present\n");
113 if (dev
->dev_state
& DEV_MISCONFIGURED
) {
114 printk(KERN_INFO
"v4l2 ioctl: device is misconfigured; "
115 "close and open it again\n");
122 * IRQ callback, called by URB callback
124 static void au0828_irq_callback(struct urb
*urb
)
126 struct au0828_dmaqueue
*dma_q
= urb
->context
;
127 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
128 unsigned long flags
= 0;
131 switch (urb
->status
) {
132 case 0: /* success */
133 case -ETIMEDOUT
: /* NAK */
135 case -ECONNRESET
: /* kill */
138 au0828_isocdbg("au0828_irq_callback called: status kill\n");
140 default: /* unknown error */
141 au0828_isocdbg("urb completition error %d.\n", urb
->status
);
145 /* Copy data from URB */
146 spin_lock_irqsave(&dev
->slock
, flags
);
147 dev
->isoc_ctl
.isoc_copy(dev
, urb
);
148 spin_unlock_irqrestore(&dev
->slock
, flags
);
150 /* Reset urb buffers */
151 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
152 urb
->iso_frame_desc
[i
].status
= 0;
153 urb
->iso_frame_desc
[i
].actual_length
= 0;
157 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
159 au0828_isocdbg("urb resubmit failed (error=%i)\n",
165 * Stop and Deallocate URBs
167 static void au0828_uninit_isoc(struct au0828_dev
*dev
)
172 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
174 dev
->isoc_ctl
.nfields
= -1;
175 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
176 urb
= dev
->isoc_ctl
.urb
[i
];
178 if (!irqs_disabled())
183 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
184 usb_free_coherent(dev
->usbdev
,
185 urb
->transfer_buffer_length
,
186 dev
->isoc_ctl
.transfer_buffer
[i
],
190 dev
->isoc_ctl
.urb
[i
] = NULL
;
192 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
195 kfree(dev
->isoc_ctl
.urb
);
196 kfree(dev
->isoc_ctl
.transfer_buffer
);
198 dev
->isoc_ctl
.urb
= NULL
;
199 dev
->isoc_ctl
.transfer_buffer
= NULL
;
200 dev
->isoc_ctl
.num_bufs
= 0;
204 * Allocate URBs and start IRQ
206 static int au0828_init_isoc(struct au0828_dev
*dev
, int max_packets
,
207 int num_bufs
, int max_pkt_size
,
208 int (*isoc_copy
) (struct au0828_dev
*dev
, struct urb
*urb
))
210 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
217 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
219 /* De-allocates all pending stuff */
220 au0828_uninit_isoc(dev
);
222 dev
->isoc_ctl
.isoc_copy
= isoc_copy
;
223 dev
->isoc_ctl
.num_bufs
= num_bufs
;
225 dev
->isoc_ctl
.urb
= kzalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
226 if (!dev
->isoc_ctl
.urb
) {
227 au0828_isocdbg("cannot alloc memory for usb buffers\n");
231 dev
->isoc_ctl
.transfer_buffer
= kzalloc(sizeof(void *)*num_bufs
,
233 if (!dev
->isoc_ctl
.transfer_buffer
) {
234 au0828_isocdbg("cannot allocate memory for usb transfer\n");
235 kfree(dev
->isoc_ctl
.urb
);
239 dev
->isoc_ctl
.max_pkt_size
= max_pkt_size
;
240 dev
->isoc_ctl
.buf
= NULL
;
242 sb_size
= max_packets
* dev
->isoc_ctl
.max_pkt_size
;
244 /* allocate urbs and transfer buffers */
245 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
246 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
248 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i
);
249 au0828_uninit_isoc(dev
);
252 dev
->isoc_ctl
.urb
[i
] = urb
;
254 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->usbdev
,
255 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
256 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
257 printk("unable to allocate %i bytes for transfer"
260 in_interrupt() ? " while in int" : "");
261 au0828_uninit_isoc(dev
);
264 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
266 pipe
= usb_rcvisocpipe(dev
->usbdev
,
267 dev
->isoc_in_endpointaddr
),
269 usb_fill_int_urb(urb
, dev
->usbdev
, pipe
,
270 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
271 au0828_irq_callback
, dma_q
, 1);
273 urb
->number_of_packets
= max_packets
;
274 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
277 for (j
= 0; j
< max_packets
; j
++) {
278 urb
->iso_frame_desc
[j
].offset
= k
;
279 urb
->iso_frame_desc
[j
].length
=
280 dev
->isoc_ctl
.max_pkt_size
;
281 k
+= dev
->isoc_ctl
.max_pkt_size
;
285 init_waitqueue_head(&dma_q
->wq
);
287 /* submit urbs and enables IRQ */
288 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
289 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
291 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
293 au0828_uninit_isoc(dev
);
302 * Announces that a buffer were filled and request the next
304 static inline void buffer_filled(struct au0828_dev
*dev
,
305 struct au0828_dmaqueue
*dma_q
,
306 struct au0828_buffer
*buf
)
308 /* Advice that buffer was filled */
309 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
311 buf
->vb
.state
= VIDEOBUF_DONE
;
312 buf
->vb
.field_count
++;
313 v4l2_get_timestamp(&buf
->vb
.ts
);
315 dev
->isoc_ctl
.buf
= NULL
;
317 list_del(&buf
->vb
.queue
);
318 wake_up(&buf
->vb
.done
);
321 static inline void vbi_buffer_filled(struct au0828_dev
*dev
,
322 struct au0828_dmaqueue
*dma_q
,
323 struct au0828_buffer
*buf
)
325 /* Advice that buffer was filled */
326 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
328 buf
->vb
.state
= VIDEOBUF_DONE
;
329 buf
->vb
.field_count
++;
330 v4l2_get_timestamp(&buf
->vb
.ts
);
332 dev
->isoc_ctl
.vbi_buf
= NULL
;
334 list_del(&buf
->vb
.queue
);
335 wake_up(&buf
->vb
.done
);
339 * Identify the buffer header type and properly handles
341 static void au0828_copy_video(struct au0828_dev
*dev
,
342 struct au0828_dmaqueue
*dma_q
,
343 struct au0828_buffer
*buf
,
345 unsigned char *outp
, unsigned long len
)
347 void *fieldstart
, *startwrite
, *startread
;
348 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
349 int bytesperline
= dev
->width
<< 1; /* Assumes 16-bit depth @@@@ */
354 if (dma_q
->pos
+ len
> buf
->vb
.size
)
355 len
= buf
->vb
.size
- dma_q
->pos
;
360 /* Interlaces frame */
364 fieldstart
= outp
+ bytesperline
;
366 linesdone
= dma_q
->pos
/ bytesperline
;
367 currlinedone
= dma_q
->pos
% bytesperline
;
368 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
369 startwrite
= fieldstart
+ offset
;
370 lencopy
= bytesperline
- currlinedone
;
371 lencopy
= lencopy
> remain
? remain
: lencopy
;
373 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
374 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
375 ((char *)startwrite
+ lencopy
) -
376 ((char *)outp
+ buf
->vb
.size
));
377 remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
382 memcpy(startwrite
, startread
, lencopy
);
387 startwrite
+= lencopy
+ bytesperline
;
388 startread
+= lencopy
;
389 if (bytesperline
> remain
)
392 lencopy
= bytesperline
;
394 if ((char *)startwrite
+ lencopy
> (char *)outp
+
396 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
397 ((char *)startwrite
+ lencopy
) -
398 ((char *)outp
+ buf
->vb
.size
));
399 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
405 memcpy(startwrite
, startread
, lencopy
);
411 /* We have enough data to check for greenscreen */
412 if (outp
[0] < 0x60 && outp
[1440] < 0x60)
413 dev
->greenscreen_detected
= 1;
420 * video-buf generic routine to get the next available buffer
422 static inline void get_next_buf(struct au0828_dmaqueue
*dma_q
,
423 struct au0828_buffer
**buf
)
425 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
427 if (list_empty(&dma_q
->active
)) {
428 au0828_isocdbg("No active queue to serve\n");
429 dev
->isoc_ctl
.buf
= NULL
;
434 /* Get the next buffer */
435 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, vb
.queue
);
436 dev
->isoc_ctl
.buf
= *buf
;
441 static void au0828_copy_vbi(struct au0828_dev
*dev
,
442 struct au0828_dmaqueue
*dma_q
,
443 struct au0828_buffer
*buf
,
445 unsigned char *outp
, unsigned long len
)
447 unsigned char *startwrite
, *startread
;
452 au0828_isocdbg("dev is null\n");
457 au0828_isocdbg("dma_q is null\n");
463 au0828_isocdbg("p is null\n");
467 au0828_isocdbg("outp is null\n");
471 bytesperline
= dev
->vbi_width
;
473 if (dma_q
->pos
+ len
> buf
->vb
.size
)
474 len
= buf
->vb
.size
- dma_q
->pos
;
477 startwrite
= outp
+ (dma_q
->pos
/ 2);
479 /* Make sure the bottom field populates the second half of the frame */
480 if (buf
->top_field
== 0)
481 startwrite
+= bytesperline
* dev
->vbi_height
;
483 for (i
= 0; i
< len
; i
+= 2)
484 startwrite
[j
++] = startread
[i
+1];
491 * video-buf generic routine to get the next available VBI buffer
493 static inline void vbi_get_next_buf(struct au0828_dmaqueue
*dma_q
,
494 struct au0828_buffer
**buf
)
496 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vbiq
);
499 if (list_empty(&dma_q
->active
)) {
500 au0828_isocdbg("No active queue to serve\n");
501 dev
->isoc_ctl
.vbi_buf
= NULL
;
506 /* Get the next buffer */
507 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, vb
.queue
);
508 /* Cleans up buffer - Useful for testing for frame/URB loss */
509 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
510 memset(outp
, 0x00, (*buf
)->vb
.size
);
512 dev
->isoc_ctl
.vbi_buf
= *buf
;
518 * Controls the isoc copy of each urb packet
520 static inline int au0828_isoc_copy(struct au0828_dev
*dev
, struct urb
*urb
)
522 struct au0828_buffer
*buf
;
523 struct au0828_buffer
*vbi_buf
;
524 struct au0828_dmaqueue
*dma_q
= urb
->context
;
525 struct au0828_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
526 unsigned char *outp
= NULL
;
527 unsigned char *vbioutp
= NULL
;
528 int i
, len
= 0, rc
= 1;
531 unsigned int vbi_field_size
;
532 unsigned int remain
, lencopy
;
537 if ((dev
->dev_state
& DEV_DISCONNECTED
) ||
538 (dev
->dev_state
& DEV_MISCONFIGURED
))
541 if (urb
->status
< 0) {
542 print_err_status(dev
, -1, urb
->status
);
543 if (urb
->status
== -ENOENT
)
547 buf
= dev
->isoc_ctl
.buf
;
549 outp
= videobuf_to_vmalloc(&buf
->vb
);
551 vbi_buf
= dev
->isoc_ctl
.vbi_buf
;
553 vbioutp
= videobuf_to_vmalloc(&vbi_buf
->vb
);
555 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
556 int status
= urb
->iso_frame_desc
[i
].status
;
559 print_err_status(dev
, i
, status
);
560 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
564 if (urb
->iso_frame_desc
[i
].actual_length
<= 0)
567 if (urb
->iso_frame_desc
[i
].actual_length
>
569 au0828_isocdbg("packet bigger than packet size");
573 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
575 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
581 au0828_isocdbg("Video frame %s\n",
582 (fbyte
& 0x40) ? "odd" : "even");
586 vbi_buffer_filled(dev
,
589 vbi_get_next_buf(vbi_dma_q
, &vbi_buf
);
593 vbioutp
= videobuf_to_vmalloc(
598 buffer_filled(dev
, dma_q
, buf
);
599 get_next_buf(dma_q
, &buf
);
603 outp
= videobuf_to_vmalloc(&buf
->vb
);
605 /* As long as isoc traffic is arriving, keep
606 resetting the timer */
607 if (dev
->vid_timeout_running
)
608 mod_timer(&dev
->vid_timeout
,
609 jiffies
+ (HZ
/ 10));
610 if (dev
->vbi_timeout_running
)
611 mod_timer(&dev
->vbi_timeout
,
612 jiffies
+ (HZ
/ 10));
622 if (vbi_buf
!= NULL
) {
624 vbi_buf
->top_field
= 1;
626 vbi_buf
->top_field
= 0;
634 vbi_field_size
= dev
->vbi_width
* dev
->vbi_height
* 2;
635 if (dev
->vbi_read
< vbi_field_size
) {
636 remain
= vbi_field_size
- dev
->vbi_read
;
643 au0828_copy_vbi(dev
, vbi_dma_q
, vbi_buf
, p
,
648 dev
->vbi_read
+= lencopy
;
651 if (dev
->vbi_read
>= vbi_field_size
&& buf
!= NULL
)
652 au0828_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
658 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
661 struct au0828_fh
*fh
= vq
->priv_data
;
662 *size
= (fh
->dev
->width
* fh
->dev
->height
* 16 + 7) >> 3;
665 *count
= AU0828_DEF_BUF
;
667 if (*count
< AU0828_MIN_BUF
)
668 *count
= AU0828_MIN_BUF
;
672 /* This is called *without* dev->slock held; please keep it that way */
673 static void free_buffer(struct videobuf_queue
*vq
, struct au0828_buffer
*buf
)
675 struct au0828_fh
*fh
= vq
->priv_data
;
676 struct au0828_dev
*dev
= fh
->dev
;
677 unsigned long flags
= 0;
681 /* We used to wait for the buffer to finish here, but this didn't work
682 because, as we were keeping the state as VIDEOBUF_QUEUED,
683 videobuf_queue_cancel marked it as finished for us.
684 (Also, it could wedge forever if the hardware was misconfigured.)
686 This should be safe; by the time we get here, the buffer isn't
687 queued anymore. If we ever start marking the buffers as
688 VIDEOBUF_ACTIVE, it won't be, though.
690 spin_lock_irqsave(&dev
->slock
, flags
);
691 if (dev
->isoc_ctl
.buf
== buf
)
692 dev
->isoc_ctl
.buf
= NULL
;
693 spin_unlock_irqrestore(&dev
->slock
, flags
);
695 videobuf_vmalloc_free(&buf
->vb
);
696 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
700 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
701 enum v4l2_field field
)
703 struct au0828_fh
*fh
= vq
->priv_data
;
704 struct au0828_buffer
*buf
= container_of(vb
, struct au0828_buffer
, vb
);
705 struct au0828_dev
*dev
= fh
->dev
;
706 int rc
= 0, urb_init
= 0;
708 buf
->vb
.size
= (fh
->dev
->width
* fh
->dev
->height
* 16 + 7) >> 3;
710 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
713 buf
->vb
.width
= dev
->width
;
714 buf
->vb
.height
= dev
->height
;
715 buf
->vb
.field
= field
;
717 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
718 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
720 printk(KERN_INFO
"videobuf_iolock failed\n");
725 if (!dev
->isoc_ctl
.num_bufs
)
729 rc
= au0828_init_isoc(dev
, AU0828_ISO_PACKETS_PER_URB
,
730 AU0828_MAX_ISO_BUFS
, dev
->max_pkt_size
,
733 printk(KERN_INFO
"au0828_init_isoc failed\n");
738 buf
->vb
.state
= VIDEOBUF_PREPARED
;
742 free_buffer(vq
, buf
);
747 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
749 struct au0828_buffer
*buf
= container_of(vb
,
750 struct au0828_buffer
,
752 struct au0828_fh
*fh
= vq
->priv_data
;
753 struct au0828_dev
*dev
= fh
->dev
;
754 struct au0828_dmaqueue
*vidq
= &dev
->vidq
;
756 buf
->vb
.state
= VIDEOBUF_QUEUED
;
757 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
760 static void buffer_release(struct videobuf_queue
*vq
,
761 struct videobuf_buffer
*vb
)
763 struct au0828_buffer
*buf
= container_of(vb
,
764 struct au0828_buffer
,
767 free_buffer(vq
, buf
);
770 static struct videobuf_queue_ops au0828_video_qops
= {
771 .buf_setup
= buffer_setup
,
772 .buf_prepare
= buffer_prepare
,
773 .buf_queue
= buffer_queue
,
774 .buf_release
= buffer_release
,
777 /* ------------------------------------------------------------------
779 ------------------------------------------------------------------*/
781 static int au0828_i2s_init(struct au0828_dev
*dev
)
783 /* Enable i2s mode */
784 au0828_writereg(dev
, AU0828_AUDIOCTRL_50C
, 0x01);
789 * Auvitek au0828 analog stream enable
790 * Please set interface0 to AS5 before enable the stream
792 static int au0828_analog_stream_enable(struct au0828_dev
*d
)
794 dprintk(1, "au0828_analog_stream_enable called\n");
795 au0828_writereg(d
, AU0828_SENSORCTRL_VBI_103
, 0x00);
796 au0828_writereg(d
, 0x106, 0x00);
798 au0828_writereg(d
, 0x110, 0x00);
799 au0828_writereg(d
, 0x111, 0x00);
800 au0828_writereg(d
, 0x114, 0xa0);
801 au0828_writereg(d
, 0x115, 0x05);
803 au0828_writereg(d
, 0x112, 0x00);
804 au0828_writereg(d
, 0x113, 0x00);
805 au0828_writereg(d
, 0x116, 0xf2);
806 au0828_writereg(d
, 0x117, 0x00);
807 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0xb3);
812 int au0828_analog_stream_disable(struct au0828_dev
*d
)
814 dprintk(1, "au0828_analog_stream_disable called\n");
815 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0x0);
819 static void au0828_analog_stream_reset(struct au0828_dev
*dev
)
821 dprintk(1, "au0828_analog_stream_reset called\n");
822 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0x0);
824 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0xb3);
828 * Some operations needs to stop current streaming
830 static int au0828_stream_interrupt(struct au0828_dev
*dev
)
834 dev
->stream_state
= STREAM_INTERRUPT
;
835 if (dev
->dev_state
== DEV_DISCONNECTED
)
838 dev
->dev_state
= DEV_MISCONFIGURED
;
839 dprintk(1, "%s device is misconfigured!\n", __func__
);
846 * au0828_release_resources
847 * unregister v4l2 devices
849 void au0828_analog_unregister(struct au0828_dev
*dev
)
851 dprintk(1, "au0828_release_resources called\n");
852 mutex_lock(&au0828_sysfs_lock
);
855 video_unregister_device(dev
->vdev
);
857 video_unregister_device(dev
->vbi_dev
);
859 mutex_unlock(&au0828_sysfs_lock
);
863 /* Usage lock check functions */
864 static int res_get(struct au0828_fh
*fh
, unsigned int bit
)
866 struct au0828_dev
*dev
= fh
->dev
;
868 if (fh
->resources
& bit
)
869 /* have it already allocated */
873 if (dev
->resources
& bit
) {
874 /* no, someone else uses it */
877 /* it's free, grab it */
878 fh
->resources
|= bit
;
879 dev
->resources
|= bit
;
880 dprintk(1, "res: get %d\n", bit
);
885 static int res_check(struct au0828_fh
*fh
, unsigned int bit
)
887 return fh
->resources
& bit
;
890 static int res_locked(struct au0828_dev
*dev
, unsigned int bit
)
892 return dev
->resources
& bit
;
895 static void res_free(struct au0828_fh
*fh
, unsigned int bits
)
897 struct au0828_dev
*dev
= fh
->dev
;
899 BUG_ON((fh
->resources
& bits
) != bits
);
901 fh
->resources
&= ~bits
;
902 dev
->resources
&= ~bits
;
903 dprintk(1, "res: put %d\n", bits
);
906 static int get_ressource(struct au0828_fh
*fh
)
909 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
910 return AU0828_RESOURCE_VIDEO
;
911 case V4L2_BUF_TYPE_VBI_CAPTURE
:
912 return AU0828_RESOURCE_VBI
;
919 /* This function ensures that video frames continue to be delivered even if
920 the ITU-656 input isn't receiving any data (thereby preventing applications
921 such as tvtime from hanging) */
922 static void au0828_vid_buffer_timeout(unsigned long data
)
924 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
925 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
926 struct au0828_buffer
*buf
;
927 unsigned char *vid_data
;
928 unsigned long flags
= 0;
930 spin_lock_irqsave(&dev
->slock
, flags
);
932 buf
= dev
->isoc_ctl
.buf
;
934 vid_data
= videobuf_to_vmalloc(&buf
->vb
);
935 memset(vid_data
, 0x00, buf
->vb
.size
); /* Blank green frame */
936 buffer_filled(dev
, dma_q
, buf
);
938 get_next_buf(dma_q
, &buf
);
940 if (dev
->vid_timeout_running
== 1)
941 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
943 spin_unlock_irqrestore(&dev
->slock
, flags
);
946 static void au0828_vbi_buffer_timeout(unsigned long data
)
948 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
949 struct au0828_dmaqueue
*dma_q
= &dev
->vbiq
;
950 struct au0828_buffer
*buf
;
951 unsigned char *vbi_data
;
952 unsigned long flags
= 0;
954 spin_lock_irqsave(&dev
->slock
, flags
);
956 buf
= dev
->isoc_ctl
.vbi_buf
;
958 vbi_data
= videobuf_to_vmalloc(&buf
->vb
);
959 memset(vbi_data
, 0x00, buf
->vb
.size
);
960 vbi_buffer_filled(dev
, dma_q
, buf
);
962 vbi_get_next_buf(dma_q
, &buf
);
964 if (dev
->vbi_timeout_running
== 1)
965 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
966 spin_unlock_irqrestore(&dev
->slock
, flags
);
970 static int au0828_v4l2_open(struct file
*filp
)
973 struct video_device
*vdev
= video_devdata(filp
);
974 struct au0828_dev
*dev
= video_drvdata(filp
);
975 struct au0828_fh
*fh
;
978 switch (vdev
->vfl_type
) {
979 case VFL_TYPE_GRABBER
:
980 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
983 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
989 fh
= kzalloc(sizeof(struct au0828_fh
), GFP_KERNEL
);
991 dprintk(1, "Failed allocate au0828_fh struct!\n");
997 v4l2_fh_init(&fh
->fh
, vdev
);
998 filp
->private_data
= fh
;
1000 if (mutex_lock_interruptible(&dev
->lock
)) {
1002 return -ERESTARTSYS
;
1004 if (dev
->users
== 0) {
1005 /* set au0828 interface0 to AS5 here again */
1006 ret
= usb_set_interface(dev
->usbdev
, 0, 5);
1008 mutex_unlock(&dev
->lock
);
1009 printk(KERN_INFO
"Au0828 can't set alternate to 5!\n");
1014 au0828_analog_stream_enable(dev
);
1015 au0828_analog_stream_reset(dev
);
1017 /* If we were doing ac97 instead of i2s, it would go here...*/
1018 au0828_i2s_init(dev
);
1020 dev
->stream_state
= STREAM_OFF
;
1021 dev
->dev_state
|= DEV_INITIALIZED
;
1025 mutex_unlock(&dev
->lock
);
1027 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &au0828_video_qops
,
1029 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1030 V4L2_FIELD_INTERLACED
,
1031 sizeof(struct au0828_buffer
), fh
,
1035 videobuf_queue_vmalloc_init(&fh
->vb_vbiq
, &au0828_vbi_qops
,
1037 V4L2_BUF_TYPE_VBI_CAPTURE
,
1039 sizeof(struct au0828_buffer
), fh
,
1041 v4l2_fh_add(&fh
->fh
);
1045 static int au0828_v4l2_close(struct file
*filp
)
1048 struct au0828_fh
*fh
= filp
->private_data
;
1049 struct au0828_dev
*dev
= fh
->dev
;
1051 v4l2_fh_del(&fh
->fh
);
1052 v4l2_fh_exit(&fh
->fh
);
1053 mutex_lock(&dev
->lock
);
1054 if (res_check(fh
, AU0828_RESOURCE_VIDEO
)) {
1055 /* Cancel timeout thread in case they didn't call streamoff */
1056 dev
->vid_timeout_running
= 0;
1057 del_timer_sync(&dev
->vid_timeout
);
1059 videobuf_stop(&fh
->vb_vidq
);
1060 res_free(fh
, AU0828_RESOURCE_VIDEO
);
1063 if (res_check(fh
, AU0828_RESOURCE_VBI
)) {
1064 /* Cancel timeout thread in case they didn't call streamoff */
1065 dev
->vbi_timeout_running
= 0;
1066 del_timer_sync(&dev
->vbi_timeout
);
1068 videobuf_stop(&fh
->vb_vbiq
);
1069 res_free(fh
, AU0828_RESOURCE_VBI
);
1072 if (dev
->users
== 1 && video_is_registered(video_devdata(filp
))) {
1073 au0828_analog_stream_disable(dev
);
1075 au0828_uninit_isoc(dev
);
1077 /* Save some power by putting tuner to sleep */
1078 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_power
, 0);
1079 dev
->std_set_in_tuner_core
= 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");
1087 mutex_unlock(&dev
->lock
);
1089 videobuf_mmap_free(&fh
->vb_vidq
);
1090 videobuf_mmap_free(&fh
->vb_vbiq
);
1093 wake_up_interruptible_nr(&dev
->open
, 1);
1097 /* Must be called with dev->lock held */
1098 static void au0828_init_tuner(struct au0828_dev
*dev
)
1100 struct v4l2_frequency f
= {
1101 .frequency
= dev
->ctrl_freq
,
1102 .type
= V4L2_TUNER_ANALOG_TV
,
1105 if (dev
->std_set_in_tuner_core
)
1107 dev
->std_set_in_tuner_core
= 1;
1108 i2c_gate_ctrl(dev
, 1);
1109 /* If we've never sent the standard in tuner core, do so now.
1110 We don't do this at device probe because we don't want to
1111 incur the cost of a firmware load */
1112 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, dev
->std
);
1113 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, &f
);
1114 i2c_gate_ctrl(dev
, 0);
1117 static ssize_t
au0828_v4l2_read(struct file
*filp
, char __user
*buf
,
1118 size_t count
, loff_t
*pos
)
1120 struct au0828_fh
*fh
= filp
->private_data
;
1121 struct au0828_dev
*dev
= fh
->dev
;
1124 rc
= check_dev(dev
);
1128 if (mutex_lock_interruptible(&dev
->lock
))
1129 return -ERESTARTSYS
;
1130 au0828_init_tuner(dev
);
1131 mutex_unlock(&dev
->lock
);
1133 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1134 if (res_locked(dev
, AU0828_RESOURCE_VIDEO
))
1137 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
1138 filp
->f_flags
& O_NONBLOCK
);
1141 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1142 if (!res_get(fh
, AU0828_RESOURCE_VBI
))
1145 if (dev
->vbi_timeout_running
== 0) {
1146 /* Handle case where caller tries to read without
1147 calling streamon first */
1148 dev
->vbi_timeout_running
= 1;
1149 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1152 return videobuf_read_stream(&fh
->vb_vbiq
, buf
, count
, pos
, 0,
1153 filp
->f_flags
& O_NONBLOCK
);
1159 static unsigned int au0828_v4l2_poll(struct file
*filp
, poll_table
*wait
)
1161 struct au0828_fh
*fh
= filp
->private_data
;
1162 struct au0828_dev
*dev
= fh
->dev
;
1163 unsigned long req_events
= poll_requested_events(wait
);
1166 if (check_dev(dev
) < 0)
1169 res
= v4l2_ctrl_poll(filp
, wait
);
1170 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
1173 if (mutex_lock_interruptible(&dev
->lock
))
1174 return -ERESTARTSYS
;
1175 au0828_init_tuner(dev
);
1176 mutex_unlock(&dev
->lock
);
1178 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1179 if (!res_get(fh
, AU0828_RESOURCE_VIDEO
))
1181 return res
| videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
1183 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1184 if (!res_get(fh
, AU0828_RESOURCE_VBI
))
1186 return res
| videobuf_poll_stream(filp
, &fh
->vb_vbiq
, wait
);
1191 static int au0828_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1193 struct au0828_fh
*fh
= filp
->private_data
;
1194 struct au0828_dev
*dev
= fh
->dev
;
1197 rc
= check_dev(dev
);
1201 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1202 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1203 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1204 rc
= videobuf_mmap_mapper(&fh
->vb_vbiq
, vma
);
1209 static int au0828_set_format(struct au0828_dev
*dev
, unsigned int cmd
,
1210 struct v4l2_format
*format
)
1213 int width
= format
->fmt
.pix
.width
;
1214 int height
= format
->fmt
.pix
.height
;
1216 /* If they are demanding a format other than the one we support,
1217 bail out (tvtime asks for UYVY and then retries with YUYV) */
1218 if (format
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_UYVY
)
1221 /* format->fmt.pix.width only support 720 and height 480 */
1227 format
->fmt
.pix
.width
= width
;
1228 format
->fmt
.pix
.height
= height
;
1229 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1230 format
->fmt
.pix
.bytesperline
= width
* 2;
1231 format
->fmt
.pix
.sizeimage
= width
* height
* 2;
1232 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1233 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1234 format
->fmt
.pix
.priv
= 0;
1236 if (cmd
== VIDIOC_TRY_FMT
)
1239 /* maybe set new image format, driver current only support 720*480 */
1241 dev
->height
= height
;
1242 dev
->frame_size
= width
* height
* 2;
1243 dev
->field_size
= width
* height
;
1244 dev
->bytesperline
= width
* 2;
1246 if (dev
->stream_state
== STREAM_ON
) {
1247 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1248 ret
= au0828_stream_interrupt(dev
);
1250 dprintk(1, "error interrupting video stream!\n");
1255 /* set au0828 interface0 to AS5 here again */
1256 ret
= usb_set_interface(dev
->usbdev
, 0, 5);
1258 printk(KERN_INFO
"Au0828 can't set alt setting to 5!\n");
1262 au0828_analog_stream_enable(dev
);
1268 static int vidioc_querycap(struct file
*file
, void *priv
,
1269 struct v4l2_capability
*cap
)
1271 struct video_device
*vdev
= video_devdata(file
);
1272 struct au0828_fh
*fh
= priv
;
1273 struct au0828_dev
*dev
= fh
->dev
;
1275 strlcpy(cap
->driver
, "au0828", sizeof(cap
->driver
));
1276 strlcpy(cap
->card
, dev
->board
.name
, sizeof(cap
->card
));
1277 usb_make_path(dev
->usbdev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1279 /* set the device capabilities */
1280 cap
->device_caps
= V4L2_CAP_AUDIO
|
1281 V4L2_CAP_READWRITE
|
1282 V4L2_CAP_STREAMING
|
1284 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
1285 cap
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
;
1287 cap
->device_caps
|= V4L2_CAP_VBI_CAPTURE
;
1288 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
|
1289 V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_VIDEO_CAPTURE
;
1293 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1294 struct v4l2_fmtdesc
*f
)
1299 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1300 strcpy(f
->description
, "Packed YUV2");
1303 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
1308 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1309 struct v4l2_format
*f
)
1311 struct au0828_fh
*fh
= priv
;
1312 struct au0828_dev
*dev
= fh
->dev
;
1314 f
->fmt
.pix
.width
= dev
->width
;
1315 f
->fmt
.pix
.height
= dev
->height
;
1316 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1317 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
1318 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
1319 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
; /* NTSC/PAL */
1320 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1321 f
->fmt
.pix
.priv
= 0;
1325 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1326 struct v4l2_format
*f
)
1328 struct au0828_fh
*fh
= priv
;
1329 struct au0828_dev
*dev
= fh
->dev
;
1331 return au0828_set_format(dev
, VIDIOC_TRY_FMT
, f
);
1334 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1335 struct v4l2_format
*f
)
1337 struct au0828_fh
*fh
= priv
;
1338 struct au0828_dev
*dev
= fh
->dev
;
1341 rc
= check_dev(dev
);
1345 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1346 printk(KERN_INFO
"%s queue busy\n", __func__
);
1351 rc
= au0828_set_format(dev
, VIDIOC_S_FMT
, f
);
1356 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
1358 struct au0828_fh
*fh
= priv
;
1359 struct au0828_dev
*dev
= fh
->dev
;
1363 au0828_init_tuner(dev
);
1365 i2c_gate_ctrl(dev
, 1);
1367 /* FIXME: when we support something other than NTSC, we are going to
1368 have to make the au0828 bridge adjust the size of its capture
1369 buffer, which is currently hardcoded at 720x480 */
1371 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_std
, norm
);
1373 i2c_gate_ctrl(dev
, 0);
1378 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1380 struct au0828_fh
*fh
= priv
;
1381 struct au0828_dev
*dev
= fh
->dev
;
1387 static int vidioc_enum_input(struct file
*file
, void *priv
,
1388 struct v4l2_input
*input
)
1390 struct au0828_fh
*fh
= priv
;
1391 struct au0828_dev
*dev
= fh
->dev
;
1394 static const char *inames
[] = {
1395 [AU0828_VMUX_UNDEFINED
] = "Undefined",
1396 [AU0828_VMUX_COMPOSITE
] = "Composite",
1397 [AU0828_VMUX_SVIDEO
] = "S-Video",
1398 [AU0828_VMUX_CABLE
] = "Cable TV",
1399 [AU0828_VMUX_TELEVISION
] = "Television",
1400 [AU0828_VMUX_DVB
] = "DVB",
1401 [AU0828_VMUX_DEBUG
] = "tv debug"
1406 if (tmp
>= AU0828_MAX_INPUT
)
1408 if (AUVI_INPUT(tmp
).type
== 0)
1412 strcpy(input
->name
, inames
[AUVI_INPUT(tmp
).type
]);
1413 if ((AUVI_INPUT(tmp
).type
== AU0828_VMUX_TELEVISION
) ||
1414 (AUVI_INPUT(tmp
).type
== AU0828_VMUX_CABLE
)) {
1415 input
->type
|= V4L2_INPUT_TYPE_TUNER
;
1416 input
->audioset
= 1;
1418 input
->type
|= V4L2_INPUT_TYPE_CAMERA
;
1419 input
->audioset
= 2;
1422 input
->std
= dev
->vdev
->tvnorms
;
1427 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1429 struct au0828_fh
*fh
= priv
;
1430 struct au0828_dev
*dev
= fh
->dev
;
1431 *i
= dev
->ctrl_input
;
1435 static void au0828_s_input(struct au0828_dev
*dev
, int index
)
1439 switch (AUVI_INPUT(index
).type
) {
1440 case AU0828_VMUX_SVIDEO
:
1441 dev
->input_type
= AU0828_VMUX_SVIDEO
;
1442 dev
->ctrl_ainput
= 1;
1444 case AU0828_VMUX_COMPOSITE
:
1445 dev
->input_type
= AU0828_VMUX_COMPOSITE
;
1446 dev
->ctrl_ainput
= 1;
1448 case AU0828_VMUX_TELEVISION
:
1449 dev
->input_type
= AU0828_VMUX_TELEVISION
;
1450 dev
->ctrl_ainput
= 0;
1453 dprintk(1, "unknown input type set [%d]\n",
1454 AUVI_INPUT(index
).type
);
1458 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
1459 AUVI_INPUT(index
).vmux
, 0, 0);
1461 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1463 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1471 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1473 /* Make sure we leave it turned on if some
1474 other input is routed to this callback */
1475 if ((AUVI_INPUT(i
).audio_setup
) !=
1476 ((AUVI_INPUT(index
).audio_setup
))) {
1477 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1482 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
1483 AUVI_INPUT(index
).amux
, 0, 0);
1486 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int index
)
1488 struct au0828_fh
*fh
= priv
;
1489 struct au0828_dev
*dev
= fh
->dev
;
1491 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__
,
1493 if (index
>= AU0828_MAX_INPUT
)
1495 if (AUVI_INPUT(index
).type
== 0)
1497 dev
->ctrl_input
= index
;
1498 au0828_s_input(dev
, index
);
1502 static int vidioc_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1508 strcpy(a
->name
, "Television");
1510 strcpy(a
->name
, "Line in");
1512 a
->capability
= V4L2_AUDCAP_STEREO
;
1516 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1518 struct au0828_fh
*fh
= priv
;
1519 struct au0828_dev
*dev
= fh
->dev
;
1521 a
->index
= dev
->ctrl_ainput
;
1523 strcpy(a
->name
, "Television");
1525 strcpy(a
->name
, "Line in");
1527 a
->capability
= V4L2_AUDCAP_STEREO
;
1531 static int vidioc_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*a
)
1533 struct au0828_fh
*fh
= priv
;
1534 struct au0828_dev
*dev
= fh
->dev
;
1536 if (a
->index
!= dev
->ctrl_ainput
)
1541 static int vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1543 struct au0828_fh
*fh
= priv
;
1544 struct au0828_dev
*dev
= fh
->dev
;
1549 strcpy(t
->name
, "Auvitek tuner");
1551 au0828_init_tuner(dev
);
1552 i2c_gate_ctrl(dev
, 1);
1553 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1554 i2c_gate_ctrl(dev
, 0);
1558 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1559 const struct v4l2_tuner
*t
)
1561 struct au0828_fh
*fh
= priv
;
1562 struct au0828_dev
*dev
= fh
->dev
;
1567 au0828_init_tuner(dev
);
1568 i2c_gate_ctrl(dev
, 1);
1569 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1570 i2c_gate_ctrl(dev
, 0);
1572 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t
->signal
,
1579 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1580 struct v4l2_frequency
*freq
)
1582 struct au0828_fh
*fh
= priv
;
1583 struct au0828_dev
*dev
= fh
->dev
;
1585 if (freq
->tuner
!= 0)
1587 freq
->frequency
= dev
->ctrl_freq
;
1591 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1592 const struct v4l2_frequency
*freq
)
1594 struct au0828_fh
*fh
= priv
;
1595 struct au0828_dev
*dev
= fh
->dev
;
1596 struct v4l2_frequency new_freq
= *freq
;
1598 if (freq
->tuner
!= 0)
1601 au0828_init_tuner(dev
);
1602 i2c_gate_ctrl(dev
, 1);
1604 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, freq
);
1605 /* Get the actual set (and possibly clamped) frequency */
1606 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, &new_freq
);
1607 dev
->ctrl_freq
= new_freq
.frequency
;
1609 i2c_gate_ctrl(dev
, 0);
1611 au0828_analog_stream_reset(dev
);
1617 /* RAW VBI ioctls */
1619 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1620 struct v4l2_format
*format
)
1622 struct au0828_fh
*fh
= priv
;
1623 struct au0828_dev
*dev
= fh
->dev
;
1625 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1626 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1627 format
->fmt
.vbi
.offset
= 0;
1628 format
->fmt
.vbi
.flags
= 0;
1629 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1631 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1632 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1633 format
->fmt
.vbi
.start
[0] = 21;
1634 format
->fmt
.vbi
.start
[1] = 284;
1635 memset(format
->fmt
.vbi
.reserved
, 0, sizeof(format
->fmt
.vbi
.reserved
));
1640 static int vidioc_cropcap(struct file
*file
, void *priv
,
1641 struct v4l2_cropcap
*cc
)
1643 struct au0828_fh
*fh
= priv
;
1644 struct au0828_dev
*dev
= fh
->dev
;
1646 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1649 cc
->bounds
.left
= 0;
1651 cc
->bounds
.width
= dev
->width
;
1652 cc
->bounds
.height
= dev
->height
;
1654 cc
->defrect
= cc
->bounds
;
1656 cc
->pixelaspect
.numerator
= 54;
1657 cc
->pixelaspect
.denominator
= 59;
1662 static int vidioc_streamon(struct file
*file
, void *priv
,
1663 enum v4l2_buf_type type
)
1665 struct au0828_fh
*fh
= priv
;
1666 struct au0828_dev
*dev
= fh
->dev
;
1669 rc
= check_dev(dev
);
1673 if (unlikely(type
!= fh
->type
))
1676 dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
1677 fh
, type
, fh
->resources
, dev
->resources
);
1679 if (unlikely(!res_get(fh
, get_ressource(fh
))))
1682 au0828_init_tuner(dev
);
1683 if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1684 au0828_analog_stream_enable(dev
);
1685 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 1);
1688 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1689 rc
= videobuf_streamon(&fh
->vb_vidq
);
1690 dev
->vid_timeout_running
= 1;
1691 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
1692 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1693 rc
= videobuf_streamon(&fh
->vb_vbiq
);
1694 dev
->vbi_timeout_running
= 1;
1695 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1701 static int vidioc_streamoff(struct file
*file
, void *priv
,
1702 enum v4l2_buf_type type
)
1704 struct au0828_fh
*fh
= priv
;
1705 struct au0828_dev
*dev
= fh
->dev
;
1709 rc
= check_dev(dev
);
1713 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1714 fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
)
1716 if (type
!= fh
->type
)
1719 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1720 fh
, type
, fh
->resources
, dev
->resources
);
1722 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1723 dev
->vid_timeout_running
= 0;
1724 del_timer_sync(&dev
->vid_timeout
);
1726 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 0);
1727 rc
= au0828_stream_interrupt(dev
);
1731 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1732 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1734 (AUVI_INPUT(i
).audio_setup
)(dev
, 0);
1737 if (res_check(fh
, AU0828_RESOURCE_VIDEO
)) {
1738 videobuf_streamoff(&fh
->vb_vidq
);
1739 res_free(fh
, AU0828_RESOURCE_VIDEO
);
1741 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1742 dev
->vbi_timeout_running
= 0;
1743 del_timer_sync(&dev
->vbi_timeout
);
1745 if (res_check(fh
, AU0828_RESOURCE_VBI
)) {
1746 videobuf_streamoff(&fh
->vb_vbiq
);
1747 res_free(fh
, AU0828_RESOURCE_VBI
);
1754 #ifdef CONFIG_VIDEO_ADV_DEBUG
1755 static int vidioc_g_register(struct file
*file
, void *priv
,
1756 struct v4l2_dbg_register
*reg
)
1758 struct au0828_fh
*fh
= priv
;
1759 struct au0828_dev
*dev
= fh
->dev
;
1761 reg
->val
= au0828_read(dev
, reg
->reg
);
1766 static int vidioc_s_register(struct file
*file
, void *priv
,
1767 const struct v4l2_dbg_register
*reg
)
1769 struct au0828_fh
*fh
= priv
;
1770 struct au0828_dev
*dev
= fh
->dev
;
1772 return au0828_writereg(dev
, reg
->reg
, reg
->val
);
1776 static int vidioc_log_status(struct file
*file
, void *fh
)
1778 struct video_device
*vdev
= video_devdata(file
);
1780 v4l2_ctrl_log_status(file
, fh
);
1781 v4l2_device_call_all(vdev
->v4l2_dev
, 0, core
, log_status
);
1785 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1786 struct v4l2_requestbuffers
*rb
)
1788 struct au0828_fh
*fh
= priv
;
1789 struct au0828_dev
*dev
= fh
->dev
;
1792 rc
= check_dev(dev
);
1796 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1797 rc
= videobuf_reqbufs(&fh
->vb_vidq
, rb
);
1798 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1799 rc
= videobuf_reqbufs(&fh
->vb_vbiq
, rb
);
1804 static int vidioc_querybuf(struct file
*file
, void *priv
,
1805 struct v4l2_buffer
*b
)
1807 struct au0828_fh
*fh
= priv
;
1808 struct au0828_dev
*dev
= fh
->dev
;
1811 rc
= check_dev(dev
);
1815 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1816 rc
= videobuf_querybuf(&fh
->vb_vidq
, b
);
1817 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1818 rc
= videobuf_querybuf(&fh
->vb_vbiq
, b
);
1823 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1825 struct au0828_fh
*fh
= priv
;
1826 struct au0828_dev
*dev
= fh
->dev
;
1829 rc
= check_dev(dev
);
1833 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1834 rc
= videobuf_qbuf(&fh
->vb_vidq
, b
);
1835 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1836 rc
= videobuf_qbuf(&fh
->vb_vbiq
, b
);
1841 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1843 struct au0828_fh
*fh
= priv
;
1844 struct au0828_dev
*dev
= fh
->dev
;
1847 rc
= check_dev(dev
);
1851 /* Workaround for a bug in the au0828 hardware design that sometimes
1852 results in the colorspace being inverted */
1853 if (dev
->greenscreen_detected
== 1) {
1854 dprintk(1, "Detected green frame. Resetting stream...\n");
1855 au0828_analog_stream_reset(dev
);
1856 dev
->greenscreen_detected
= 0;
1859 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1860 rc
= videobuf_dqbuf(&fh
->vb_vidq
, b
, file
->f_flags
& O_NONBLOCK
);
1861 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1862 rc
= videobuf_dqbuf(&fh
->vb_vbiq
, b
, file
->f_flags
& O_NONBLOCK
);
1867 static struct v4l2_file_operations au0828_v4l_fops
= {
1868 .owner
= THIS_MODULE
,
1869 .open
= au0828_v4l2_open
,
1870 .release
= au0828_v4l2_close
,
1871 .read
= au0828_v4l2_read
,
1872 .poll
= au0828_v4l2_poll
,
1873 .mmap
= au0828_v4l2_mmap
,
1874 .unlocked_ioctl
= video_ioctl2
,
1877 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1878 .vidioc_querycap
= vidioc_querycap
,
1879 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1880 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1881 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1882 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1883 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1884 .vidioc_try_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1885 .vidioc_s_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1886 .vidioc_enumaudio
= vidioc_enumaudio
,
1887 .vidioc_g_audio
= vidioc_g_audio
,
1888 .vidioc_s_audio
= vidioc_s_audio
,
1889 .vidioc_cropcap
= vidioc_cropcap
,
1890 .vidioc_reqbufs
= vidioc_reqbufs
,
1891 .vidioc_querybuf
= vidioc_querybuf
,
1892 .vidioc_qbuf
= vidioc_qbuf
,
1893 .vidioc_dqbuf
= vidioc_dqbuf
,
1894 .vidioc_s_std
= vidioc_s_std
,
1895 .vidioc_g_std
= vidioc_g_std
,
1896 .vidioc_enum_input
= vidioc_enum_input
,
1897 .vidioc_g_input
= vidioc_g_input
,
1898 .vidioc_s_input
= vidioc_s_input
,
1899 .vidioc_streamon
= vidioc_streamon
,
1900 .vidioc_streamoff
= vidioc_streamoff
,
1901 .vidioc_g_tuner
= vidioc_g_tuner
,
1902 .vidioc_s_tuner
= vidioc_s_tuner
,
1903 .vidioc_g_frequency
= vidioc_g_frequency
,
1904 .vidioc_s_frequency
= vidioc_s_frequency
,
1905 #ifdef CONFIG_VIDEO_ADV_DEBUG
1906 .vidioc_g_register
= vidioc_g_register
,
1907 .vidioc_s_register
= vidioc_s_register
,
1909 .vidioc_log_status
= vidioc_log_status
,
1910 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1911 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1914 static const struct video_device au0828_video_template
= {
1915 .fops
= &au0828_v4l_fops
,
1916 .release
= video_device_release
,
1917 .ioctl_ops
= &video_ioctl_ops
,
1918 .tvnorms
= V4L2_STD_NTSC_M
,
1921 /**************************************************************************/
1923 int au0828_analog_register(struct au0828_dev
*dev
,
1924 struct usb_interface
*interface
)
1926 int retval
= -ENOMEM
;
1927 struct usb_host_interface
*iface_desc
;
1928 struct usb_endpoint_descriptor
*endpoint
;
1931 dprintk(1, "au0828_analog_register called!\n");
1933 /* set au0828 usb interface0 to as5 */
1934 retval
= usb_set_interface(dev
->usbdev
,
1935 interface
->cur_altsetting
->desc
.bInterfaceNumber
, 5);
1937 printk(KERN_INFO
"Failure setting usb interface0 to as5\n");
1941 /* Figure out which endpoint has the isoc interface */
1942 iface_desc
= interface
->cur_altsetting
;
1943 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
1944 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1945 if (((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
1947 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
1948 == USB_ENDPOINT_XFER_ISOC
)) {
1950 /* we find our isoc in endpoint */
1951 u16 tmp
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1952 dev
->max_pkt_size
= (tmp
& 0x07ff) *
1953 (((tmp
& 0x1800) >> 11) + 1);
1954 dev
->isoc_in_endpointaddr
= endpoint
->bEndpointAddress
;
1957 if (!(dev
->isoc_in_endpointaddr
)) {
1958 printk(KERN_INFO
"Could not locate isoc endpoint\n");
1963 init_waitqueue_head(&dev
->open
);
1964 spin_lock_init(&dev
->slock
);
1966 /* init video dma queues */
1967 INIT_LIST_HEAD(&dev
->vidq
.active
);
1968 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1969 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1970 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1972 dev
->vid_timeout
.function
= au0828_vid_buffer_timeout
;
1973 dev
->vid_timeout
.data
= (unsigned long) dev
;
1974 init_timer(&dev
->vid_timeout
);
1976 dev
->vbi_timeout
.function
= au0828_vbi_buffer_timeout
;
1977 dev
->vbi_timeout
.data
= (unsigned long) dev
;
1978 init_timer(&dev
->vbi_timeout
);
1980 dev
->width
= NTSC_STD_W
;
1981 dev
->height
= NTSC_STD_H
;
1982 dev
->field_size
= dev
->width
* dev
->height
;
1983 dev
->frame_size
= dev
->field_size
<< 1;
1984 dev
->bytesperline
= dev
->width
<< 1;
1985 dev
->vbi_width
= 720;
1986 dev
->vbi_height
= 1;
1987 dev
->ctrl_ainput
= 0;
1988 dev
->ctrl_freq
= 960;
1989 dev
->std
= V4L2_STD_NTSC_M
;
1990 au0828_s_input(dev
, 0);
1992 /* allocate and fill v4l2 video struct */
1993 dev
->vdev
= video_device_alloc();
1994 if (NULL
== dev
->vdev
) {
1995 dprintk(1, "Can't allocate video_device.\n");
1999 /* allocate the VBI struct */
2000 dev
->vbi_dev
= video_device_alloc();
2001 if (NULL
== dev
->vbi_dev
) {
2002 dprintk(1, "Can't allocate vbi_device.\n");
2007 /* Fill the video capture device struct */
2008 *dev
->vdev
= au0828_video_template
;
2009 dev
->vdev
->v4l2_dev
= &dev
->v4l2_dev
;
2010 dev
->vdev
->lock
= &dev
->lock
;
2011 set_bit(V4L2_FL_USE_FH_PRIO
, &dev
->vdev
->flags
);
2012 strcpy(dev
->vdev
->name
, "au0828a video");
2014 /* Setup the VBI device */
2015 *dev
->vbi_dev
= au0828_video_template
;
2016 dev
->vbi_dev
->v4l2_dev
= &dev
->v4l2_dev
;
2017 dev
->vbi_dev
->lock
= &dev
->lock
;
2018 set_bit(V4L2_FL_USE_FH_PRIO
, &dev
->vbi_dev
->flags
);
2019 strcpy(dev
->vbi_dev
->name
, "au0828a vbi");
2021 /* Register the v4l2 device */
2022 video_set_drvdata(dev
->vdev
, dev
);
2023 retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
, -1);
2025 dprintk(1, "unable to register video device (error = %d).\n",
2031 /* Register the vbi device */
2032 video_set_drvdata(dev
->vbi_dev
, dev
);
2033 retval
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
, -1);
2035 dprintk(1, "unable to register vbi device (error = %d).\n",
2041 dprintk(1, "%s completed!\n", __func__
);
2046 video_device_release(dev
->vbi_dev
);
2048 video_device_release(dev
->vdev
);