2 * Auvitek AU0828 USB Bridge (Analog video support)
4 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
5 * Copyright (C) 2005-2008 Auvitek International, Ltd.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * As published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * The hardware scaler supported is unimplemented
26 * AC97 audio support is unimplemented (only i2s audio mode)
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/device.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-event.h>
39 #include <media/tuner.h>
40 #include "au0828-reg.h"
42 static DEFINE_MUTEX(au0828_sysfs_lock
);
44 /* ------------------------------------------------------------------
46 ------------------------------------------------------------------*/
48 static unsigned int isoc_debug
;
49 module_param(isoc_debug
, int, 0644);
50 MODULE_PARM_DESC(isoc_debug
, "enable debug messages [isoc transfers]");
52 #define au0828_isocdbg(fmt, arg...) \
55 pr_info("au0828 %s :"fmt, \
60 static inline void i2c_gate_ctrl(struct au0828_dev
*dev
, int val
)
62 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
63 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, val
);
66 static inline void print_err_status(struct au0828_dev
*dev
,
67 int packet
, int status
)
69 char *errmsg
= "Unknown";
73 errmsg
= "unlinked synchronuously";
76 errmsg
= "unlinked asynchronuously";
79 errmsg
= "Buffer error (overrun)";
82 errmsg
= "Stalled (device not responding)";
85 errmsg
= "Babble (bad cable?)";
88 errmsg
= "Bit-stuff error (bad cable?)";
91 errmsg
= "CRC/Timeout (could be anything)";
94 errmsg
= "Device does not respond";
98 au0828_isocdbg("URB status %d [%s].\n", status
, errmsg
);
100 au0828_isocdbg("URB packet %d, status %d [%s].\n",
101 packet
, status
, errmsg
);
105 static int check_dev(struct au0828_dev
*dev
)
107 if (dev
->dev_state
& DEV_DISCONNECTED
) {
108 pr_info("v4l2 ioctl: device not present\n");
112 if (dev
->dev_state
& DEV_MISCONFIGURED
) {
113 pr_info("v4l2 ioctl: device is misconfigured; "
114 "close and open it again\n");
121 * IRQ callback, called by URB callback
123 static void au0828_irq_callback(struct urb
*urb
)
125 struct au0828_dmaqueue
*dma_q
= urb
->context
;
126 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
127 unsigned long flags
= 0;
130 switch (urb
->status
) {
131 case 0: /* success */
132 case -ETIMEDOUT
: /* NAK */
134 case -ECONNRESET
: /* kill */
137 au0828_isocdbg("au0828_irq_callback called: status kill\n");
139 default: /* unknown error */
140 au0828_isocdbg("urb completition error %d.\n", urb
->status
);
144 /* Copy data from URB */
145 spin_lock_irqsave(&dev
->slock
, flags
);
146 dev
->isoc_ctl
.isoc_copy(dev
, urb
);
147 spin_unlock_irqrestore(&dev
->slock
, flags
);
149 /* Reset urb buffers */
150 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
151 urb
->iso_frame_desc
[i
].status
= 0;
152 urb
->iso_frame_desc
[i
].actual_length
= 0;
156 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
158 au0828_isocdbg("urb resubmit failed (error=%i)\n",
161 dev
->stream_state
= STREAM_ON
;
165 * Stop and Deallocate URBs
167 static void au0828_uninit_isoc(struct au0828_dev
*dev
)
172 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
174 dev
->isoc_ctl
.nfields
= -1;
175 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
176 urb
= dev
->isoc_ctl
.urb
[i
];
178 if (!irqs_disabled())
183 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
184 usb_free_coherent(dev
->usbdev
,
185 urb
->transfer_buffer_length
,
186 dev
->isoc_ctl
.transfer_buffer
[i
],
190 dev
->isoc_ctl
.urb
[i
] = NULL
;
192 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
195 kfree(dev
->isoc_ctl
.urb
);
196 kfree(dev
->isoc_ctl
.transfer_buffer
);
198 dev
->isoc_ctl
.urb
= NULL
;
199 dev
->isoc_ctl
.transfer_buffer
= NULL
;
200 dev
->isoc_ctl
.num_bufs
= 0;
202 dev
->stream_state
= STREAM_OFF
;
206 * Allocate URBs and start IRQ
208 static int au0828_init_isoc(struct au0828_dev
*dev
, int max_packets
,
209 int num_bufs
, int max_pkt_size
,
210 int (*isoc_copy
) (struct au0828_dev
*dev
, struct urb
*urb
))
212 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
219 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
221 /* De-allocates all pending stuff */
222 au0828_uninit_isoc(dev
);
224 dev
->isoc_ctl
.isoc_copy
= isoc_copy
;
225 dev
->isoc_ctl
.num_bufs
= num_bufs
;
227 dev
->isoc_ctl
.urb
= kzalloc(sizeof(void *)*num_bufs
, GFP_KERNEL
);
228 if (!dev
->isoc_ctl
.urb
) {
229 au0828_isocdbg("cannot alloc memory for usb buffers\n");
233 dev
->isoc_ctl
.transfer_buffer
= kzalloc(sizeof(void *)*num_bufs
,
235 if (!dev
->isoc_ctl
.transfer_buffer
) {
236 au0828_isocdbg("cannot allocate memory for usb transfer\n");
237 kfree(dev
->isoc_ctl
.urb
);
241 dev
->isoc_ctl
.max_pkt_size
= max_pkt_size
;
242 dev
->isoc_ctl
.buf
= NULL
;
244 sb_size
= max_packets
* dev
->isoc_ctl
.max_pkt_size
;
246 /* allocate urbs and transfer buffers */
247 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
248 urb
= usb_alloc_urb(max_packets
, GFP_KERNEL
);
250 au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i
);
251 au0828_uninit_isoc(dev
);
254 dev
->isoc_ctl
.urb
[i
] = urb
;
256 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->usbdev
,
257 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
258 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
259 printk("unable to allocate %i bytes for transfer"
262 in_interrupt() ? " while in int" : "");
263 au0828_uninit_isoc(dev
);
266 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
268 pipe
= usb_rcvisocpipe(dev
->usbdev
,
269 dev
->isoc_in_endpointaddr
),
271 usb_fill_int_urb(urb
, dev
->usbdev
, pipe
,
272 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
273 au0828_irq_callback
, dma_q
, 1);
275 urb
->number_of_packets
= max_packets
;
276 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
279 for (j
= 0; j
< max_packets
; j
++) {
280 urb
->iso_frame_desc
[j
].offset
= k
;
281 urb
->iso_frame_desc
[j
].length
=
282 dev
->isoc_ctl
.max_pkt_size
;
283 k
+= dev
->isoc_ctl
.max_pkt_size
;
287 init_waitqueue_head(&dma_q
->wq
);
289 /* submit urbs and enables IRQ */
290 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
291 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
293 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
295 au0828_uninit_isoc(dev
);
304 * Announces that a buffer were filled and request the next
306 static inline void buffer_filled(struct au0828_dev
*dev
,
307 struct au0828_dmaqueue
*dma_q
,
308 struct au0828_buffer
*buf
)
310 /* Advice that buffer was filled */
311 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
313 buf
->vb
.state
= VIDEOBUF_DONE
;
314 buf
->vb
.field_count
++;
315 v4l2_get_timestamp(&buf
->vb
.ts
);
317 dev
->isoc_ctl
.buf
= NULL
;
319 list_del(&buf
->vb
.queue
);
320 wake_up(&buf
->vb
.done
);
323 static inline void vbi_buffer_filled(struct au0828_dev
*dev
,
324 struct au0828_dmaqueue
*dma_q
,
325 struct au0828_buffer
*buf
)
327 /* Advice that buffer was filled */
328 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->vb
.i
);
330 buf
->vb
.state
= VIDEOBUF_DONE
;
331 buf
->vb
.field_count
++;
332 v4l2_get_timestamp(&buf
->vb
.ts
);
334 dev
->isoc_ctl
.vbi_buf
= NULL
;
336 list_del(&buf
->vb
.queue
);
337 wake_up(&buf
->vb
.done
);
341 * Identify the buffer header type and properly handles
343 static void au0828_copy_video(struct au0828_dev
*dev
,
344 struct au0828_dmaqueue
*dma_q
,
345 struct au0828_buffer
*buf
,
347 unsigned char *outp
, unsigned long len
)
349 void *fieldstart
, *startwrite
, *startread
;
350 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
351 int bytesperline
= dev
->width
<< 1; /* Assumes 16-bit depth @@@@ */
356 if (dma_q
->pos
+ len
> buf
->vb
.size
)
357 len
= buf
->vb
.size
- dma_q
->pos
;
362 /* Interlaces frame */
366 fieldstart
= outp
+ bytesperline
;
368 linesdone
= dma_q
->pos
/ bytesperline
;
369 currlinedone
= dma_q
->pos
% bytesperline
;
370 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
371 startwrite
= fieldstart
+ offset
;
372 lencopy
= bytesperline
- currlinedone
;
373 lencopy
= lencopy
> remain
? remain
: lencopy
;
375 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->vb
.size
) {
376 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
377 ((char *)startwrite
+ lencopy
) -
378 ((char *)outp
+ buf
->vb
.size
));
379 remain
= (char *)outp
+ buf
->vb
.size
- (char *)startwrite
;
384 memcpy(startwrite
, startread
, lencopy
);
389 startwrite
+= lencopy
+ bytesperline
;
390 startread
+= lencopy
;
391 if (bytesperline
> remain
)
394 lencopy
= bytesperline
;
396 if ((char *)startwrite
+ lencopy
> (char *)outp
+
398 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
399 ((char *)startwrite
+ lencopy
) -
400 ((char *)outp
+ buf
->vb
.size
));
401 lencopy
= remain
= (char *)outp
+ buf
->vb
.size
-
407 memcpy(startwrite
, startread
, lencopy
);
413 /* We have enough data to check for greenscreen */
414 if (outp
[0] < 0x60 && outp
[1440] < 0x60)
415 dev
->greenscreen_detected
= 1;
422 * video-buf generic routine to get the next available buffer
424 static inline void get_next_buf(struct au0828_dmaqueue
*dma_q
,
425 struct au0828_buffer
**buf
)
427 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
429 if (list_empty(&dma_q
->active
)) {
430 au0828_isocdbg("No active queue to serve\n");
431 dev
->isoc_ctl
.buf
= NULL
;
436 /* Get the next buffer */
437 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, vb
.queue
);
438 dev
->isoc_ctl
.buf
= *buf
;
443 static void au0828_copy_vbi(struct au0828_dev
*dev
,
444 struct au0828_dmaqueue
*dma_q
,
445 struct au0828_buffer
*buf
,
447 unsigned char *outp
, unsigned long len
)
449 unsigned char *startwrite
, *startread
;
454 au0828_isocdbg("dev is null\n");
459 au0828_isocdbg("dma_q is null\n");
465 au0828_isocdbg("p is null\n");
469 au0828_isocdbg("outp is null\n");
473 bytesperline
= dev
->vbi_width
;
475 if (dma_q
->pos
+ len
> buf
->vb
.size
)
476 len
= buf
->vb
.size
- dma_q
->pos
;
479 startwrite
= outp
+ (dma_q
->pos
/ 2);
481 /* Make sure the bottom field populates the second half of the frame */
482 if (buf
->top_field
== 0)
483 startwrite
+= bytesperline
* dev
->vbi_height
;
485 for (i
= 0; i
< len
; i
+= 2)
486 startwrite
[j
++] = startread
[i
+1];
493 * video-buf generic routine to get the next available VBI buffer
495 static inline void vbi_get_next_buf(struct au0828_dmaqueue
*dma_q
,
496 struct au0828_buffer
**buf
)
498 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vbiq
);
501 if (list_empty(&dma_q
->active
)) {
502 au0828_isocdbg("No active queue to serve\n");
503 dev
->isoc_ctl
.vbi_buf
= NULL
;
508 /* Get the next buffer */
509 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, vb
.queue
);
510 /* Cleans up buffer - Useful for testing for frame/URB loss */
511 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
512 memset(outp
, 0x00, (*buf
)->vb
.size
);
514 dev
->isoc_ctl
.vbi_buf
= *buf
;
520 * Controls the isoc copy of each urb packet
522 static inline int au0828_isoc_copy(struct au0828_dev
*dev
, struct urb
*urb
)
524 struct au0828_buffer
*buf
;
525 struct au0828_buffer
*vbi_buf
;
526 struct au0828_dmaqueue
*dma_q
= urb
->context
;
527 struct au0828_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
528 unsigned char *outp
= NULL
;
529 unsigned char *vbioutp
= NULL
;
530 int i
, len
= 0, rc
= 1;
533 unsigned int vbi_field_size
;
534 unsigned int remain
, lencopy
;
539 if ((dev
->dev_state
& DEV_DISCONNECTED
) ||
540 (dev
->dev_state
& DEV_MISCONFIGURED
))
543 if (urb
->status
< 0) {
544 print_err_status(dev
, -1, urb
->status
);
545 if (urb
->status
== -ENOENT
)
549 buf
= dev
->isoc_ctl
.buf
;
551 outp
= videobuf_to_vmalloc(&buf
->vb
);
553 vbi_buf
= dev
->isoc_ctl
.vbi_buf
;
555 vbioutp
= videobuf_to_vmalloc(&vbi_buf
->vb
);
557 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
558 int status
= urb
->iso_frame_desc
[i
].status
;
561 print_err_status(dev
, i
, status
);
562 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
566 if (urb
->iso_frame_desc
[i
].actual_length
<= 0)
569 if (urb
->iso_frame_desc
[i
].actual_length
>
571 au0828_isocdbg("packet bigger than packet size");
575 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
577 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
583 au0828_isocdbg("Video frame %s\n",
584 (fbyte
& 0x40) ? "odd" : "even");
588 vbi_buffer_filled(dev
,
591 vbi_get_next_buf(vbi_dma_q
, &vbi_buf
);
595 vbioutp
= videobuf_to_vmalloc(
600 buffer_filled(dev
, dma_q
, buf
);
601 get_next_buf(dma_q
, &buf
);
605 outp
= videobuf_to_vmalloc(&buf
->vb
);
607 /* As long as isoc traffic is arriving, keep
608 resetting the timer */
609 if (dev
->vid_timeout_running
)
610 mod_timer(&dev
->vid_timeout
,
611 jiffies
+ (HZ
/ 10));
612 if (dev
->vbi_timeout_running
)
613 mod_timer(&dev
->vbi_timeout
,
614 jiffies
+ (HZ
/ 10));
624 if (vbi_buf
!= NULL
) {
626 vbi_buf
->top_field
= 1;
628 vbi_buf
->top_field
= 0;
636 vbi_field_size
= dev
->vbi_width
* dev
->vbi_height
* 2;
637 if (dev
->vbi_read
< vbi_field_size
) {
638 remain
= vbi_field_size
- dev
->vbi_read
;
645 au0828_copy_vbi(dev
, vbi_dma_q
, vbi_buf
, p
,
650 dev
->vbi_read
+= lencopy
;
653 if (dev
->vbi_read
>= vbi_field_size
&& buf
!= NULL
)
654 au0828_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
660 buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
663 struct au0828_fh
*fh
= vq
->priv_data
;
664 *size
= (fh
->dev
->width
* fh
->dev
->height
* 16 + 7) >> 3;
667 *count
= AU0828_DEF_BUF
;
669 if (*count
< AU0828_MIN_BUF
)
670 *count
= AU0828_MIN_BUF
;
674 /* This is called *without* dev->slock held; please keep it that way */
675 static void free_buffer(struct videobuf_queue
*vq
, struct au0828_buffer
*buf
)
677 struct au0828_fh
*fh
= vq
->priv_data
;
678 struct au0828_dev
*dev
= fh
->dev
;
679 unsigned long flags
= 0;
683 /* We used to wait for the buffer to finish here, but this didn't work
684 because, as we were keeping the state as VIDEOBUF_QUEUED,
685 videobuf_queue_cancel marked it as finished for us.
686 (Also, it could wedge forever if the hardware was misconfigured.)
688 This should be safe; by the time we get here, the buffer isn't
689 queued anymore. If we ever start marking the buffers as
690 VIDEOBUF_ACTIVE, it won't be, though.
692 spin_lock_irqsave(&dev
->slock
, flags
);
693 if (dev
->isoc_ctl
.buf
== buf
)
694 dev
->isoc_ctl
.buf
= NULL
;
695 spin_unlock_irqrestore(&dev
->slock
, flags
);
697 videobuf_vmalloc_free(&buf
->vb
);
698 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
702 buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
703 enum v4l2_field field
)
705 struct au0828_fh
*fh
= vq
->priv_data
;
706 struct au0828_buffer
*buf
= container_of(vb
, struct au0828_buffer
, vb
);
707 struct au0828_dev
*dev
= fh
->dev
;
708 int rc
= 0, urb_init
= 0;
710 buf
->vb
.size
= (fh
->dev
->width
* fh
->dev
->height
* 16 + 7) >> 3;
712 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
715 buf
->vb
.width
= dev
->width
;
716 buf
->vb
.height
= dev
->height
;
717 buf
->vb
.field
= field
;
719 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
720 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
722 pr_info("videobuf_iolock failed\n");
727 if (!dev
->isoc_ctl
.num_bufs
)
731 rc
= au0828_init_isoc(dev
, AU0828_ISO_PACKETS_PER_URB
,
732 AU0828_MAX_ISO_BUFS
, dev
->max_pkt_size
,
735 pr_info("au0828_init_isoc failed\n");
740 buf
->vb
.state
= VIDEOBUF_PREPARED
;
744 free_buffer(vq
, buf
);
749 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
751 struct au0828_buffer
*buf
= container_of(vb
,
752 struct au0828_buffer
,
754 struct au0828_fh
*fh
= vq
->priv_data
;
755 struct au0828_dev
*dev
= fh
->dev
;
756 struct au0828_dmaqueue
*vidq
= &dev
->vidq
;
758 buf
->vb
.state
= VIDEOBUF_QUEUED
;
759 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
762 static void buffer_release(struct videobuf_queue
*vq
,
763 struct videobuf_buffer
*vb
)
765 struct au0828_buffer
*buf
= container_of(vb
,
766 struct au0828_buffer
,
769 free_buffer(vq
, buf
);
772 static struct videobuf_queue_ops au0828_video_qops
= {
773 .buf_setup
= buffer_setup
,
774 .buf_prepare
= buffer_prepare
,
775 .buf_queue
= buffer_queue
,
776 .buf_release
= buffer_release
,
779 /* ------------------------------------------------------------------
781 ------------------------------------------------------------------*/
783 static int au0828_i2s_init(struct au0828_dev
*dev
)
785 /* Enable i2s mode */
786 au0828_writereg(dev
, AU0828_AUDIOCTRL_50C
, 0x01);
791 * Auvitek au0828 analog stream enable
793 static int au0828_analog_stream_enable(struct au0828_dev
*d
)
795 struct usb_interface
*iface
;
798 dprintk(1, "au0828_analog_stream_enable called\n");
800 iface
= usb_ifnum_to_if(d
->usbdev
, 0);
801 if (iface
&& iface
->cur_altsetting
->desc
.bAlternateSetting
!= 5) {
802 dprintk(1, "Changing intf#0 to alt 5\n");
803 /* set au0828 interface0 to AS5 here again */
804 ret
= usb_set_interface(d
->usbdev
, 0, 5);
806 pr_info("Au0828 can't set alt setting to 5!\n");
811 h
= d
->height
/ 2 + 2;
814 au0828_writereg(d
, AU0828_SENSORCTRL_VBI_103
, 0x00);
815 au0828_writereg(d
, 0x106, 0x00);
817 au0828_writereg(d
, 0x110, 0x00);
818 au0828_writereg(d
, 0x111, 0x00);
819 au0828_writereg(d
, 0x114, w
& 0xff);
820 au0828_writereg(d
, 0x115, w
>> 8);
822 au0828_writereg(d
, 0x112, 0x00);
823 au0828_writereg(d
, 0x113, 0x00);
824 au0828_writereg(d
, 0x116, h
& 0xff);
825 au0828_writereg(d
, 0x117, h
>> 8);
826 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0xb3);
831 int au0828_analog_stream_disable(struct au0828_dev
*d
)
833 dprintk(1, "au0828_analog_stream_disable called\n");
834 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0x0);
838 static void au0828_analog_stream_reset(struct au0828_dev
*dev
)
840 dprintk(1, "au0828_analog_stream_reset called\n");
841 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0x0);
843 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0xb3);
847 * Some operations needs to stop current streaming
849 static int au0828_stream_interrupt(struct au0828_dev
*dev
)
853 dev
->stream_state
= STREAM_INTERRUPT
;
854 if (dev
->dev_state
== DEV_DISCONNECTED
)
857 dev
->dev_state
= DEV_MISCONFIGURED
;
858 dprintk(1, "%s device is misconfigured!\n", __func__
);
865 * au0828_release_resources
866 * unregister v4l2 devices
868 void au0828_analog_unregister(struct au0828_dev
*dev
)
870 dprintk(1, "au0828_release_resources called\n");
871 mutex_lock(&au0828_sysfs_lock
);
874 video_unregister_device(dev
->vdev
);
876 video_unregister_device(dev
->vbi_dev
);
878 mutex_unlock(&au0828_sysfs_lock
);
882 /* Usage lock check functions */
883 static int res_get(struct au0828_fh
*fh
, unsigned int bit
)
885 struct au0828_dev
*dev
= fh
->dev
;
887 if (fh
->resources
& bit
)
888 /* have it already allocated */
892 if (dev
->resources
& bit
) {
893 /* no, someone else uses it */
896 /* it's free, grab it */
897 fh
->resources
|= bit
;
898 dev
->resources
|= bit
;
899 dprintk(1, "res: get %d\n", bit
);
904 static int res_check(struct au0828_fh
*fh
, unsigned int bit
)
906 return fh
->resources
& bit
;
909 static int res_locked(struct au0828_dev
*dev
, unsigned int bit
)
911 return dev
->resources
& bit
;
914 static void res_free(struct au0828_fh
*fh
, unsigned int bits
)
916 struct au0828_dev
*dev
= fh
->dev
;
918 BUG_ON((fh
->resources
& bits
) != bits
);
920 fh
->resources
&= ~bits
;
921 dev
->resources
&= ~bits
;
922 dprintk(1, "res: put %d\n", bits
);
925 static int get_ressource(struct au0828_fh
*fh
)
928 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
929 return AU0828_RESOURCE_VIDEO
;
930 case V4L2_BUF_TYPE_VBI_CAPTURE
:
931 return AU0828_RESOURCE_VBI
;
938 /* This function ensures that video frames continue to be delivered even if
939 the ITU-656 input isn't receiving any data (thereby preventing applications
940 such as tvtime from hanging) */
941 static void au0828_vid_buffer_timeout(unsigned long data
)
943 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
944 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
945 struct au0828_buffer
*buf
;
946 unsigned char *vid_data
;
947 unsigned long flags
= 0;
949 spin_lock_irqsave(&dev
->slock
, flags
);
951 buf
= dev
->isoc_ctl
.buf
;
953 vid_data
= videobuf_to_vmalloc(&buf
->vb
);
954 memset(vid_data
, 0x00, buf
->vb
.size
); /* Blank green frame */
955 buffer_filled(dev
, dma_q
, buf
);
957 get_next_buf(dma_q
, &buf
);
959 if (dev
->vid_timeout_running
== 1)
960 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
962 spin_unlock_irqrestore(&dev
->slock
, flags
);
965 static void au0828_vbi_buffer_timeout(unsigned long data
)
967 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
968 struct au0828_dmaqueue
*dma_q
= &dev
->vbiq
;
969 struct au0828_buffer
*buf
;
970 unsigned char *vbi_data
;
971 unsigned long flags
= 0;
973 spin_lock_irqsave(&dev
->slock
, flags
);
975 buf
= dev
->isoc_ctl
.vbi_buf
;
977 vbi_data
= videobuf_to_vmalloc(&buf
->vb
);
978 memset(vbi_data
, 0x00, buf
->vb
.size
);
979 vbi_buffer_filled(dev
, dma_q
, buf
);
981 vbi_get_next_buf(dma_q
, &buf
);
983 if (dev
->vbi_timeout_running
== 1)
984 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
985 spin_unlock_irqrestore(&dev
->slock
, flags
);
989 static int au0828_v4l2_open(struct file
*filp
)
992 struct video_device
*vdev
= video_devdata(filp
);
993 struct au0828_dev
*dev
= video_drvdata(filp
);
994 struct au0828_fh
*fh
;
997 switch (vdev
->vfl_type
) {
998 case VFL_TYPE_GRABBER
:
999 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1002 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1008 fh
= kzalloc(sizeof(struct au0828_fh
), GFP_KERNEL
);
1010 dprintk(1, "Failed allocate au0828_fh struct!\n");
1016 v4l2_fh_init(&fh
->fh
, vdev
);
1017 filp
->private_data
= fh
;
1019 if (mutex_lock_interruptible(&dev
->lock
)) {
1021 return -ERESTARTSYS
;
1023 if (dev
->users
== 0) {
1024 au0828_analog_stream_enable(dev
);
1025 au0828_analog_stream_reset(dev
);
1027 /* If we were doing ac97 instead of i2s, it would go here...*/
1028 au0828_i2s_init(dev
);
1030 dev
->stream_state
= STREAM_OFF
;
1031 dev
->dev_state
|= DEV_INITIALIZED
;
1035 mutex_unlock(&dev
->lock
);
1037 videobuf_queue_vmalloc_init(&fh
->vb_vidq
, &au0828_video_qops
,
1039 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1040 V4L2_FIELD_INTERLACED
,
1041 sizeof(struct au0828_buffer
), fh
,
1045 videobuf_queue_vmalloc_init(&fh
->vb_vbiq
, &au0828_vbi_qops
,
1047 V4L2_BUF_TYPE_VBI_CAPTURE
,
1049 sizeof(struct au0828_buffer
), fh
,
1051 v4l2_fh_add(&fh
->fh
);
1055 static int au0828_v4l2_close(struct file
*filp
)
1058 struct au0828_fh
*fh
= filp
->private_data
;
1059 struct au0828_dev
*dev
= fh
->dev
;
1061 v4l2_fh_del(&fh
->fh
);
1062 v4l2_fh_exit(&fh
->fh
);
1063 mutex_lock(&dev
->lock
);
1064 if (res_check(fh
, AU0828_RESOURCE_VIDEO
)) {
1065 /* Cancel timeout thread in case they didn't call streamoff */
1066 dev
->vid_timeout_running
= 0;
1067 del_timer_sync(&dev
->vid_timeout
);
1069 videobuf_stop(&fh
->vb_vidq
);
1070 res_free(fh
, AU0828_RESOURCE_VIDEO
);
1073 if (res_check(fh
, AU0828_RESOURCE_VBI
)) {
1074 /* Cancel timeout thread in case they didn't call streamoff */
1075 dev
->vbi_timeout_running
= 0;
1076 del_timer_sync(&dev
->vbi_timeout
);
1078 videobuf_stop(&fh
->vb_vbiq
);
1079 res_free(fh
, AU0828_RESOURCE_VBI
);
1082 if (dev
->users
== 1 && video_is_registered(video_devdata(filp
))) {
1083 au0828_analog_stream_disable(dev
);
1085 au0828_uninit_isoc(dev
);
1087 /* Save some power by putting tuner to sleep */
1088 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
, s_power
, 0);
1089 dev
->std_set_in_tuner_core
= 0;
1091 /* When close the device, set the usb intf0 into alt0 to free
1093 ret
= usb_set_interface(dev
->usbdev
, 0, 0);
1095 pr_info("Au0828 can't set alternate to 0!\n");
1097 mutex_unlock(&dev
->lock
);
1099 videobuf_mmap_free(&fh
->vb_vidq
);
1100 videobuf_mmap_free(&fh
->vb_vbiq
);
1103 wake_up_interruptible_nr(&dev
->open
, 1);
1107 /* Must be called with dev->lock held */
1108 static void au0828_init_tuner(struct au0828_dev
*dev
)
1110 struct v4l2_frequency f
= {
1111 .frequency
= dev
->ctrl_freq
,
1112 .type
= V4L2_TUNER_ANALOG_TV
,
1115 if (dev
->std_set_in_tuner_core
)
1117 dev
->std_set_in_tuner_core
= 1;
1118 i2c_gate_ctrl(dev
, 1);
1119 /* If we've never sent the standard in tuner core, do so now.
1120 We don't do this at device probe because we don't want to
1121 incur the cost of a firmware load */
1122 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_std
, dev
->std
);
1123 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, &f
);
1124 i2c_gate_ctrl(dev
, 0);
1127 static ssize_t
au0828_v4l2_read(struct file
*filp
, char __user
*buf
,
1128 size_t count
, loff_t
*pos
)
1130 struct au0828_fh
*fh
= filp
->private_data
;
1131 struct au0828_dev
*dev
= fh
->dev
;
1134 rc
= check_dev(dev
);
1138 if (mutex_lock_interruptible(&dev
->lock
))
1139 return -ERESTARTSYS
;
1140 au0828_init_tuner(dev
);
1141 mutex_unlock(&dev
->lock
);
1143 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1144 if (res_locked(dev
, AU0828_RESOURCE_VIDEO
))
1147 return videobuf_read_stream(&fh
->vb_vidq
, buf
, count
, pos
, 0,
1148 filp
->f_flags
& O_NONBLOCK
);
1151 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1152 if (!res_get(fh
, AU0828_RESOURCE_VBI
))
1155 if (dev
->vbi_timeout_running
== 0) {
1156 /* Handle case where caller tries to read without
1157 calling streamon first */
1158 dev
->vbi_timeout_running
= 1;
1159 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1162 return videobuf_read_stream(&fh
->vb_vbiq
, buf
, count
, pos
, 0,
1163 filp
->f_flags
& O_NONBLOCK
);
1169 static unsigned int au0828_v4l2_poll(struct file
*filp
, poll_table
*wait
)
1171 struct au0828_fh
*fh
= filp
->private_data
;
1172 struct au0828_dev
*dev
= fh
->dev
;
1173 unsigned long req_events
= poll_requested_events(wait
);
1176 if (check_dev(dev
) < 0)
1179 res
= v4l2_ctrl_poll(filp
, wait
);
1180 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
1183 if (mutex_lock_interruptible(&dev
->lock
))
1184 return -ERESTARTSYS
;
1185 au0828_init_tuner(dev
);
1186 mutex_unlock(&dev
->lock
);
1188 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1189 if (!res_get(fh
, AU0828_RESOURCE_VIDEO
))
1191 return res
| videobuf_poll_stream(filp
, &fh
->vb_vidq
, wait
);
1193 if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1194 if (!res_get(fh
, AU0828_RESOURCE_VBI
))
1196 return res
| videobuf_poll_stream(filp
, &fh
->vb_vbiq
, wait
);
1201 static int au0828_v4l2_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1203 struct au0828_fh
*fh
= filp
->private_data
;
1204 struct au0828_dev
*dev
= fh
->dev
;
1207 rc
= check_dev(dev
);
1211 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1212 rc
= videobuf_mmap_mapper(&fh
->vb_vidq
, vma
);
1213 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1214 rc
= videobuf_mmap_mapper(&fh
->vb_vbiq
, vma
);
1219 static int au0828_set_format(struct au0828_dev
*dev
, unsigned int cmd
,
1220 struct v4l2_format
*format
)
1223 int width
= format
->fmt
.pix
.width
;
1224 int height
= format
->fmt
.pix
.height
;
1226 /* If they are demanding a format other than the one we support,
1227 bail out (tvtime asks for UYVY and then retries with YUYV) */
1228 if (format
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_UYVY
)
1231 /* format->fmt.pix.width only support 720 and height 480 */
1237 format
->fmt
.pix
.width
= width
;
1238 format
->fmt
.pix
.height
= height
;
1239 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1240 format
->fmt
.pix
.bytesperline
= width
* 2;
1241 format
->fmt
.pix
.sizeimage
= width
* height
* 2;
1242 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1243 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1244 format
->fmt
.pix
.priv
= 0;
1246 if (cmd
== VIDIOC_TRY_FMT
)
1249 /* maybe set new image format, driver current only support 720*480 */
1251 dev
->height
= height
;
1252 dev
->frame_size
= width
* height
* 2;
1253 dev
->field_size
= width
* height
;
1254 dev
->bytesperline
= width
* 2;
1256 if (dev
->stream_state
== STREAM_ON
) {
1257 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1258 ret
= au0828_stream_interrupt(dev
);
1260 dprintk(1, "error interrupting video stream!\n");
1265 au0828_analog_stream_enable(dev
);
1271 static int vidioc_querycap(struct file
*file
, void *priv
,
1272 struct v4l2_capability
*cap
)
1274 struct video_device
*vdev
= video_devdata(file
);
1275 struct au0828_fh
*fh
= priv
;
1276 struct au0828_dev
*dev
= fh
->dev
;
1278 strlcpy(cap
->driver
, "au0828", sizeof(cap
->driver
));
1279 strlcpy(cap
->card
, dev
->board
.name
, sizeof(cap
->card
));
1280 usb_make_path(dev
->usbdev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1282 /* set the device capabilities */
1283 cap
->device_caps
= V4L2_CAP_AUDIO
|
1284 V4L2_CAP_READWRITE
|
1285 V4L2_CAP_STREAMING
|
1287 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
1288 cap
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
;
1290 cap
->device_caps
|= V4L2_CAP_VBI_CAPTURE
;
1291 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
|
1292 V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_VIDEO_CAPTURE
;
1296 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1297 struct v4l2_fmtdesc
*f
)
1302 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1303 strcpy(f
->description
, "Packed YUV2");
1306 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
1311 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1312 struct v4l2_format
*f
)
1314 struct au0828_fh
*fh
= priv
;
1315 struct au0828_dev
*dev
= fh
->dev
;
1317 f
->fmt
.pix
.width
= dev
->width
;
1318 f
->fmt
.pix
.height
= dev
->height
;
1319 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1320 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
1321 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
1322 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
; /* NTSC/PAL */
1323 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1324 f
->fmt
.pix
.priv
= 0;
1328 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1329 struct v4l2_format
*f
)
1331 struct au0828_fh
*fh
= priv
;
1332 struct au0828_dev
*dev
= fh
->dev
;
1334 return au0828_set_format(dev
, VIDIOC_TRY_FMT
, f
);
1337 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1338 struct v4l2_format
*f
)
1340 struct au0828_fh
*fh
= priv
;
1341 struct au0828_dev
*dev
= fh
->dev
;
1344 rc
= check_dev(dev
);
1348 if (videobuf_queue_is_busy(&fh
->vb_vidq
)) {
1349 pr_info("%s queue busy\n", __func__
);
1354 rc
= au0828_set_format(dev
, VIDIOC_S_FMT
, f
);
1359 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
1361 struct au0828_fh
*fh
= priv
;
1362 struct au0828_dev
*dev
= fh
->dev
;
1366 au0828_init_tuner(dev
);
1368 i2c_gate_ctrl(dev
, 1);
1371 * FIXME: when we support something other than 60Hz standards,
1372 * we are going to have to make the au0828 bridge adjust the size
1373 * of its capture buffer, which is currently hardcoded at 720x480
1376 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_std
, norm
);
1378 i2c_gate_ctrl(dev
, 0);
1383 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1385 struct au0828_fh
*fh
= priv
;
1386 struct au0828_dev
*dev
= fh
->dev
;
1392 static int vidioc_enum_input(struct file
*file
, void *priv
,
1393 struct v4l2_input
*input
)
1395 struct au0828_fh
*fh
= priv
;
1396 struct au0828_dev
*dev
= fh
->dev
;
1399 static const char *inames
[] = {
1400 [AU0828_VMUX_UNDEFINED
] = "Undefined",
1401 [AU0828_VMUX_COMPOSITE
] = "Composite",
1402 [AU0828_VMUX_SVIDEO
] = "S-Video",
1403 [AU0828_VMUX_CABLE
] = "Cable TV",
1404 [AU0828_VMUX_TELEVISION
] = "Television",
1405 [AU0828_VMUX_DVB
] = "DVB",
1406 [AU0828_VMUX_DEBUG
] = "tv debug"
1411 if (tmp
>= AU0828_MAX_INPUT
)
1413 if (AUVI_INPUT(tmp
).type
== 0)
1417 strcpy(input
->name
, inames
[AUVI_INPUT(tmp
).type
]);
1418 if ((AUVI_INPUT(tmp
).type
== AU0828_VMUX_TELEVISION
) ||
1419 (AUVI_INPUT(tmp
).type
== AU0828_VMUX_CABLE
)) {
1420 input
->type
|= V4L2_INPUT_TYPE_TUNER
;
1421 input
->audioset
= 1;
1423 input
->type
|= V4L2_INPUT_TYPE_CAMERA
;
1424 input
->audioset
= 2;
1427 input
->std
= dev
->vdev
->tvnorms
;
1432 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1434 struct au0828_fh
*fh
= priv
;
1435 struct au0828_dev
*dev
= fh
->dev
;
1436 *i
= dev
->ctrl_input
;
1440 static void au0828_s_input(struct au0828_dev
*dev
, int index
)
1444 switch (AUVI_INPUT(index
).type
) {
1445 case AU0828_VMUX_SVIDEO
:
1446 dev
->input_type
= AU0828_VMUX_SVIDEO
;
1447 dev
->ctrl_ainput
= 1;
1449 case AU0828_VMUX_COMPOSITE
:
1450 dev
->input_type
= AU0828_VMUX_COMPOSITE
;
1451 dev
->ctrl_ainput
= 1;
1453 case AU0828_VMUX_TELEVISION
:
1454 dev
->input_type
= AU0828_VMUX_TELEVISION
;
1455 dev
->ctrl_ainput
= 0;
1458 dprintk(1, "unknown input type set [%d]\n",
1459 AUVI_INPUT(index
).type
);
1463 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
1464 AUVI_INPUT(index
).vmux
, 0, 0);
1466 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1468 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1476 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1478 /* Make sure we leave it turned on if some
1479 other input is routed to this callback */
1480 if ((AUVI_INPUT(i
).audio_setup
) !=
1481 ((AUVI_INPUT(index
).audio_setup
))) {
1482 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1487 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
1488 AUVI_INPUT(index
).amux
, 0, 0);
1491 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int index
)
1493 struct au0828_fh
*fh
= priv
;
1494 struct au0828_dev
*dev
= fh
->dev
;
1496 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__
,
1498 if (index
>= AU0828_MAX_INPUT
)
1500 if (AUVI_INPUT(index
).type
== 0)
1502 dev
->ctrl_input
= index
;
1503 au0828_s_input(dev
, index
);
1507 static int vidioc_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1513 strcpy(a
->name
, "Television");
1515 strcpy(a
->name
, "Line in");
1517 a
->capability
= V4L2_AUDCAP_STEREO
;
1521 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1523 struct au0828_fh
*fh
= priv
;
1524 struct au0828_dev
*dev
= fh
->dev
;
1526 a
->index
= dev
->ctrl_ainput
;
1528 strcpy(a
->name
, "Television");
1530 strcpy(a
->name
, "Line in");
1532 a
->capability
= V4L2_AUDCAP_STEREO
;
1536 static int vidioc_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*a
)
1538 struct au0828_fh
*fh
= priv
;
1539 struct au0828_dev
*dev
= fh
->dev
;
1541 if (a
->index
!= dev
->ctrl_ainput
)
1546 static int vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1548 struct au0828_fh
*fh
= priv
;
1549 struct au0828_dev
*dev
= fh
->dev
;
1554 strcpy(t
->name
, "Auvitek tuner");
1556 au0828_init_tuner(dev
);
1557 i2c_gate_ctrl(dev
, 1);
1558 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1559 i2c_gate_ctrl(dev
, 0);
1563 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1564 const struct v4l2_tuner
*t
)
1566 struct au0828_fh
*fh
= priv
;
1567 struct au0828_dev
*dev
= fh
->dev
;
1572 au0828_init_tuner(dev
);
1573 i2c_gate_ctrl(dev
, 1);
1574 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1575 i2c_gate_ctrl(dev
, 0);
1577 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t
->signal
,
1584 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1585 struct v4l2_frequency
*freq
)
1587 struct au0828_fh
*fh
= priv
;
1588 struct au0828_dev
*dev
= fh
->dev
;
1590 if (freq
->tuner
!= 0)
1592 freq
->frequency
= dev
->ctrl_freq
;
1596 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1597 const struct v4l2_frequency
*freq
)
1599 struct au0828_fh
*fh
= priv
;
1600 struct au0828_dev
*dev
= fh
->dev
;
1601 struct v4l2_frequency new_freq
= *freq
;
1603 if (freq
->tuner
!= 0)
1606 au0828_init_tuner(dev
);
1607 i2c_gate_ctrl(dev
, 1);
1609 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, freq
);
1610 /* Get the actual set (and possibly clamped) frequency */
1611 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, &new_freq
);
1612 dev
->ctrl_freq
= new_freq
.frequency
;
1614 i2c_gate_ctrl(dev
, 0);
1616 au0828_analog_stream_reset(dev
);
1622 /* RAW VBI ioctls */
1624 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1625 struct v4l2_format
*format
)
1627 struct au0828_fh
*fh
= priv
;
1628 struct au0828_dev
*dev
= fh
->dev
;
1630 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1631 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1632 format
->fmt
.vbi
.offset
= 0;
1633 format
->fmt
.vbi
.flags
= 0;
1634 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1636 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1637 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1638 format
->fmt
.vbi
.start
[0] = 21;
1639 format
->fmt
.vbi
.start
[1] = 284;
1640 memset(format
->fmt
.vbi
.reserved
, 0, sizeof(format
->fmt
.vbi
.reserved
));
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 au0828_init_tuner(dev
);
1688 if (type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1689 au0828_analog_stream_enable(dev
);
1690 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 1);
1693 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1694 rc
= videobuf_streamon(&fh
->vb_vidq
);
1695 dev
->vid_timeout_running
= 1;
1696 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
1697 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1698 rc
= videobuf_streamon(&fh
->vb_vbiq
);
1699 dev
->vbi_timeout_running
= 1;
1700 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1706 static int vidioc_streamoff(struct file
*file
, void *priv
,
1707 enum v4l2_buf_type type
)
1709 struct au0828_fh
*fh
= priv
;
1710 struct au0828_dev
*dev
= fh
->dev
;
1714 rc
= check_dev(dev
);
1718 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
&&
1719 fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
)
1721 if (type
!= fh
->type
)
1724 dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
1725 fh
, type
, fh
->resources
, dev
->resources
);
1727 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1728 dev
->vid_timeout_running
= 0;
1729 del_timer_sync(&dev
->vid_timeout
);
1731 au0828_analog_stream_disable(dev
);
1732 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 0);
1733 rc
= au0828_stream_interrupt(dev
);
1737 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1738 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1740 (AUVI_INPUT(i
).audio_setup
)(dev
, 0);
1743 if (res_check(fh
, AU0828_RESOURCE_VIDEO
)) {
1744 videobuf_streamoff(&fh
->vb_vidq
);
1745 res_free(fh
, AU0828_RESOURCE_VIDEO
);
1747 } else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
1748 dev
->vbi_timeout_running
= 0;
1749 del_timer_sync(&dev
->vbi_timeout
);
1751 if (res_check(fh
, AU0828_RESOURCE_VBI
)) {
1752 videobuf_streamoff(&fh
->vb_vbiq
);
1753 res_free(fh
, AU0828_RESOURCE_VBI
);
1760 #ifdef CONFIG_VIDEO_ADV_DEBUG
1761 static int vidioc_g_register(struct file
*file
, void *priv
,
1762 struct v4l2_dbg_register
*reg
)
1764 struct au0828_fh
*fh
= priv
;
1765 struct au0828_dev
*dev
= fh
->dev
;
1767 reg
->val
= au0828_read(dev
, reg
->reg
);
1772 static int vidioc_s_register(struct file
*file
, void *priv
,
1773 const struct v4l2_dbg_register
*reg
)
1775 struct au0828_fh
*fh
= priv
;
1776 struct au0828_dev
*dev
= fh
->dev
;
1778 return au0828_writereg(dev
, reg
->reg
, reg
->val
);
1782 static int vidioc_log_status(struct file
*file
, void *fh
)
1784 struct video_device
*vdev
= video_devdata(file
);
1786 v4l2_ctrl_log_status(file
, fh
);
1787 v4l2_device_call_all(vdev
->v4l2_dev
, 0, core
, log_status
);
1791 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1792 struct v4l2_requestbuffers
*rb
)
1794 struct au0828_fh
*fh
= priv
;
1795 struct au0828_dev
*dev
= fh
->dev
;
1798 rc
= check_dev(dev
);
1802 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1803 rc
= videobuf_reqbufs(&fh
->vb_vidq
, rb
);
1804 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1805 rc
= videobuf_reqbufs(&fh
->vb_vbiq
, rb
);
1810 static int vidioc_querybuf(struct file
*file
, void *priv
,
1811 struct v4l2_buffer
*b
)
1813 struct au0828_fh
*fh
= priv
;
1814 struct au0828_dev
*dev
= fh
->dev
;
1817 rc
= check_dev(dev
);
1821 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1822 rc
= videobuf_querybuf(&fh
->vb_vidq
, b
);
1823 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1824 rc
= videobuf_querybuf(&fh
->vb_vbiq
, b
);
1829 static int vidioc_qbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1831 struct au0828_fh
*fh
= priv
;
1832 struct au0828_dev
*dev
= fh
->dev
;
1835 rc
= check_dev(dev
);
1839 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1840 rc
= videobuf_qbuf(&fh
->vb_vidq
, b
);
1841 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1842 rc
= videobuf_qbuf(&fh
->vb_vbiq
, b
);
1847 static int vidioc_dqbuf(struct file
*file
, void *priv
, struct v4l2_buffer
*b
)
1849 struct au0828_fh
*fh
= priv
;
1850 struct au0828_dev
*dev
= fh
->dev
;
1853 rc
= check_dev(dev
);
1857 /* Workaround for a bug in the au0828 hardware design that sometimes
1858 results in the colorspace being inverted */
1859 if (dev
->greenscreen_detected
== 1) {
1860 dprintk(1, "Detected green frame. Resetting stream...\n");
1861 au0828_analog_stream_reset(dev
);
1862 dev
->greenscreen_detected
= 0;
1865 if (fh
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1866 rc
= videobuf_dqbuf(&fh
->vb_vidq
, b
, file
->f_flags
& O_NONBLOCK
);
1867 else if (fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
)
1868 rc
= videobuf_dqbuf(&fh
->vb_vbiq
, b
, file
->f_flags
& O_NONBLOCK
);
1873 void au0828_v4l2_suspend(struct au0828_dev
*dev
)
1878 pr_info("stopping V4L2\n");
1880 if (dev
->stream_state
== STREAM_ON
) {
1881 pr_info("stopping V4L2 active URBs\n");
1882 au0828_analog_stream_disable(dev
);
1884 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
1885 urb
= dev
->isoc_ctl
.urb
[i
];
1887 if (!irqs_disabled())
1890 usb_unlink_urb(urb
);
1895 if (dev
->vid_timeout_running
)
1896 del_timer_sync(&dev
->vid_timeout
);
1897 if (dev
->vbi_timeout_running
)
1898 del_timer_sync(&dev
->vbi_timeout
);
1901 void au0828_v4l2_resume(struct au0828_dev
*dev
)
1905 pr_info("restarting V4L2\n");
1907 if (dev
->stream_state
== STREAM_ON
) {
1908 au0828_stream_interrupt(dev
);
1909 au0828_init_tuner(dev
);
1912 if (dev
->vid_timeout_running
)
1913 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
1914 if (dev
->vbi_timeout_running
)
1915 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1917 /* If we were doing ac97 instead of i2s, it would go here...*/
1918 au0828_i2s_init(dev
);
1920 au0828_analog_stream_enable(dev
);
1922 if (!(dev
->stream_state
== STREAM_ON
)) {
1923 au0828_analog_stream_reset(dev
);
1925 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
1926 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
1928 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1930 au0828_uninit_isoc(dev
);
1936 static struct v4l2_file_operations au0828_v4l_fops
= {
1937 .owner
= THIS_MODULE
,
1938 .open
= au0828_v4l2_open
,
1939 .release
= au0828_v4l2_close
,
1940 .read
= au0828_v4l2_read
,
1941 .poll
= au0828_v4l2_poll
,
1942 .mmap
= au0828_v4l2_mmap
,
1943 .unlocked_ioctl
= video_ioctl2
,
1946 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1947 .vidioc_querycap
= vidioc_querycap
,
1948 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1949 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1950 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1951 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1952 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1953 .vidioc_try_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1954 .vidioc_s_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1955 .vidioc_enumaudio
= vidioc_enumaudio
,
1956 .vidioc_g_audio
= vidioc_g_audio
,
1957 .vidioc_s_audio
= vidioc_s_audio
,
1958 .vidioc_cropcap
= vidioc_cropcap
,
1959 .vidioc_reqbufs
= vidioc_reqbufs
,
1960 .vidioc_querybuf
= vidioc_querybuf
,
1961 .vidioc_qbuf
= vidioc_qbuf
,
1962 .vidioc_dqbuf
= vidioc_dqbuf
,
1963 .vidioc_s_std
= vidioc_s_std
,
1964 .vidioc_g_std
= vidioc_g_std
,
1965 .vidioc_enum_input
= vidioc_enum_input
,
1966 .vidioc_g_input
= vidioc_g_input
,
1967 .vidioc_s_input
= vidioc_s_input
,
1968 .vidioc_streamon
= vidioc_streamon
,
1969 .vidioc_streamoff
= vidioc_streamoff
,
1970 .vidioc_g_tuner
= vidioc_g_tuner
,
1971 .vidioc_s_tuner
= vidioc_s_tuner
,
1972 .vidioc_g_frequency
= vidioc_g_frequency
,
1973 .vidioc_s_frequency
= vidioc_s_frequency
,
1974 #ifdef CONFIG_VIDEO_ADV_DEBUG
1975 .vidioc_g_register
= vidioc_g_register
,
1976 .vidioc_s_register
= vidioc_s_register
,
1978 .vidioc_log_status
= vidioc_log_status
,
1979 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1980 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1983 static const struct video_device au0828_video_template
= {
1984 .fops
= &au0828_v4l_fops
,
1985 .release
= video_device_release
,
1986 .ioctl_ops
= &video_ioctl_ops
,
1987 .tvnorms
= V4L2_STD_NTSC_M
| V4L2_STD_PAL_M
,
1990 /**************************************************************************/
1992 int au0828_analog_register(struct au0828_dev
*dev
,
1993 struct usb_interface
*interface
)
1995 int retval
= -ENOMEM
;
1996 struct usb_host_interface
*iface_desc
;
1997 struct usb_endpoint_descriptor
*endpoint
;
2000 dprintk(1, "au0828_analog_register called for intf#%d!\n",
2001 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
2003 /* set au0828 usb interface0 to as5 */
2004 retval
= usb_set_interface(dev
->usbdev
,
2005 interface
->cur_altsetting
->desc
.bInterfaceNumber
, 5);
2007 pr_info("Failure setting usb interface0 to as5\n");
2011 /* Figure out which endpoint has the isoc interface */
2012 iface_desc
= interface
->cur_altsetting
;
2013 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
2014 endpoint
= &iface_desc
->endpoint
[i
].desc
;
2015 if (((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
2017 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
2018 == USB_ENDPOINT_XFER_ISOC
)) {
2020 /* we find our isoc in endpoint */
2021 u16 tmp
= le16_to_cpu(endpoint
->wMaxPacketSize
);
2022 dev
->max_pkt_size
= (tmp
& 0x07ff) *
2023 (((tmp
& 0x1800) >> 11) + 1);
2024 dev
->isoc_in_endpointaddr
= endpoint
->bEndpointAddress
;
2026 "Found isoc endpoint 0x%02x, max size = %d\n",
2027 dev
->isoc_in_endpointaddr
, dev
->max_pkt_size
);
2030 if (!(dev
->isoc_in_endpointaddr
)) {
2031 pr_info("Could not locate isoc endpoint\n");
2036 init_waitqueue_head(&dev
->open
);
2037 spin_lock_init(&dev
->slock
);
2039 /* init video dma queues */
2040 INIT_LIST_HEAD(&dev
->vidq
.active
);
2041 INIT_LIST_HEAD(&dev
->vidq
.queued
);
2042 INIT_LIST_HEAD(&dev
->vbiq
.active
);
2043 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
2045 setup_timer(&dev
->vid_timeout
, au0828_vid_buffer_timeout
,
2046 (unsigned long)dev
);
2047 setup_timer(&dev
->vbi_timeout
, au0828_vbi_buffer_timeout
,
2048 (unsigned long)dev
);
2050 dev
->width
= NTSC_STD_W
;
2051 dev
->height
= NTSC_STD_H
;
2052 dev
->field_size
= dev
->width
* dev
->height
;
2053 dev
->frame_size
= dev
->field_size
<< 1;
2054 dev
->bytesperline
= dev
->width
<< 1;
2055 dev
->vbi_width
= 720;
2056 dev
->vbi_height
= 1;
2057 dev
->ctrl_ainput
= 0;
2058 dev
->ctrl_freq
= 960;
2059 dev
->std
= V4L2_STD_NTSC_M
;
2060 au0828_s_input(dev
, 0);
2062 /* allocate and fill v4l2 video struct */
2063 dev
->vdev
= video_device_alloc();
2064 if (NULL
== dev
->vdev
) {
2065 dprintk(1, "Can't allocate video_device.\n");
2069 /* allocate the VBI struct */
2070 dev
->vbi_dev
= video_device_alloc();
2071 if (NULL
== dev
->vbi_dev
) {
2072 dprintk(1, "Can't allocate vbi_device.\n");
2077 /* Fill the video capture device struct */
2078 *dev
->vdev
= au0828_video_template
;
2079 dev
->vdev
->v4l2_dev
= &dev
->v4l2_dev
;
2080 dev
->vdev
->lock
= &dev
->lock
;
2081 strcpy(dev
->vdev
->name
, "au0828a video");
2083 /* Setup the VBI device */
2084 *dev
->vbi_dev
= au0828_video_template
;
2085 dev
->vbi_dev
->v4l2_dev
= &dev
->v4l2_dev
;
2086 dev
->vbi_dev
->lock
= &dev
->lock
;
2087 strcpy(dev
->vbi_dev
->name
, "au0828a vbi");
2089 /* Register the v4l2 device */
2090 video_set_drvdata(dev
->vdev
, dev
);
2091 retval
= video_register_device(dev
->vdev
, VFL_TYPE_GRABBER
, -1);
2093 dprintk(1, "unable to register video device (error = %d).\n",
2099 /* Register the vbi device */
2100 video_set_drvdata(dev
->vbi_dev
, dev
);
2101 retval
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
, -1);
2103 dprintk(1, "unable to register vbi device (error = %d).\n",
2109 dprintk(1, "%s completed!\n", __func__
);
2114 video_device_release(dev
->vbi_dev
);
2116 video_device_release(dev
->vdev
);