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)
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/device.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-mc.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-event.h>
41 #include <media/tuner.h>
42 #include "au0828-reg.h"
44 static DEFINE_MUTEX(au0828_sysfs_lock
);
46 /* ------------------------------------------------------------------
48 ------------------------------------------------------------------*/
50 static unsigned int isoc_debug
;
51 module_param(isoc_debug
, int, 0644);
52 MODULE_PARM_DESC(isoc_debug
, "enable debug messages [isoc transfers]");
54 #define au0828_isocdbg(fmt, arg...) \
57 pr_info("au0828 %s :"fmt, \
62 static inline void i2c_gate_ctrl(struct au0828_dev
*dev
, int val
)
64 if (dev
->dvb
.frontend
&& dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl
)
65 dev
->dvb
.frontend
->ops
.analog_ops
.i2c_gate_ctrl(dev
->dvb
.frontend
, val
);
68 static inline void print_err_status(struct au0828_dev
*dev
,
69 int packet
, int status
)
71 char *errmsg
= "Unknown";
75 errmsg
= "unlinked synchronuously";
78 errmsg
= "unlinked asynchronuously";
81 errmsg
= "Buffer error (overrun)";
84 errmsg
= "Stalled (device not responding)";
87 errmsg
= "Babble (bad cable?)";
90 errmsg
= "Bit-stuff error (bad cable?)";
93 errmsg
= "CRC/Timeout (could be anything)";
96 errmsg
= "Device does not respond";
100 au0828_isocdbg("URB status %d [%s].\n", status
, errmsg
);
102 au0828_isocdbg("URB packet %d, status %d [%s].\n",
103 packet
, status
, errmsg
);
107 static int check_dev(struct au0828_dev
*dev
)
109 if (test_bit(DEV_DISCONNECTED
, &dev
->dev_state
)) {
110 pr_info("v4l2 ioctl: device not present\n");
114 if (test_bit(DEV_MISCONFIGURED
, &dev
->dev_state
)) {
115 pr_info("v4l2 ioctl: device is misconfigured; 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",
162 dev
->stream_state
= STREAM_ON
;
166 * Stop and Deallocate URBs
168 static void au0828_uninit_isoc(struct au0828_dev
*dev
)
173 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
175 dev
->isoc_ctl
.nfields
= -1;
176 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
177 urb
= dev
->isoc_ctl
.urb
[i
];
179 if (!irqs_disabled())
184 if (dev
->isoc_ctl
.transfer_buffer
[i
]) {
185 usb_free_coherent(dev
->usbdev
,
186 urb
->transfer_buffer_length
,
187 dev
->isoc_ctl
.transfer_buffer
[i
],
191 dev
->isoc_ctl
.urb
[i
] = NULL
;
193 dev
->isoc_ctl
.transfer_buffer
[i
] = NULL
;
196 kfree(dev
->isoc_ctl
.urb
);
197 kfree(dev
->isoc_ctl
.transfer_buffer
);
199 dev
->isoc_ctl
.urb
= NULL
;
200 dev
->isoc_ctl
.transfer_buffer
= NULL
;
201 dev
->isoc_ctl
.num_bufs
= 0;
203 dev
->stream_state
= STREAM_OFF
;
207 * Allocate URBs and start IRQ
209 static int au0828_init_isoc(struct au0828_dev
*dev
, int max_packets
,
210 int num_bufs
, int max_pkt_size
,
211 int (*isoc_copy
) (struct au0828_dev
*dev
, struct urb
*urb
))
213 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
220 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
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_uninit_isoc(dev
);
251 dev
->isoc_ctl
.urb
[i
] = urb
;
253 dev
->isoc_ctl
.transfer_buffer
[i
] = usb_alloc_coherent(dev
->usbdev
,
254 sb_size
, GFP_KERNEL
, &urb
->transfer_dma
);
255 if (!dev
->isoc_ctl
.transfer_buffer
[i
]) {
256 printk("unable to allocate %i bytes for transfer buffer %i%s\n",
258 in_interrupt() ? " while in int" : "");
259 au0828_uninit_isoc(dev
);
262 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
264 pipe
= usb_rcvisocpipe(dev
->usbdev
,
265 dev
->isoc_in_endpointaddr
),
267 usb_fill_int_urb(urb
, dev
->usbdev
, pipe
,
268 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
269 au0828_irq_callback
, dma_q
, 1);
271 urb
->number_of_packets
= max_packets
;
272 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
275 for (j
= 0; j
< max_packets
; j
++) {
276 urb
->iso_frame_desc
[j
].offset
= k
;
277 urb
->iso_frame_desc
[j
].length
=
278 dev
->isoc_ctl
.max_pkt_size
;
279 k
+= dev
->isoc_ctl
.max_pkt_size
;
283 /* submit urbs and enables IRQ */
284 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
285 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
287 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
289 au0828_uninit_isoc(dev
);
298 * Announces that a buffer were filled and request the next
300 static inline void buffer_filled(struct au0828_dev
*dev
,
301 struct au0828_dmaqueue
*dma_q
,
302 struct au0828_buffer
*buf
)
304 struct vb2_v4l2_buffer
*vb
= &buf
->vb
;
305 struct vb2_queue
*q
= vb
->vb2_buf
.vb2_queue
;
307 /* Advice that buffer was filled */
308 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->top_field
);
310 if (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
311 vb
->sequence
= dev
->frame_count
++;
313 vb
->sequence
= dev
->vbi_frame_count
++;
315 vb
->field
= V4L2_FIELD_INTERLACED
;
316 vb
->vb2_buf
.timestamp
= ktime_get_ns();
317 vb2_buffer_done(&vb
->vb2_buf
, VB2_BUF_STATE_DONE
);
321 * Identify the buffer header type and properly handles
323 static void au0828_copy_video(struct au0828_dev
*dev
,
324 struct au0828_dmaqueue
*dma_q
,
325 struct au0828_buffer
*buf
,
327 unsigned char *outp
, unsigned long len
)
329 void *fieldstart
, *startwrite
, *startread
;
330 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
331 int bytesperline
= dev
->width
<< 1; /* Assumes 16-bit depth @@@@ */
336 if (dma_q
->pos
+ len
> buf
->length
)
337 len
= buf
->length
- dma_q
->pos
;
342 /* Interlaces frame */
346 fieldstart
= outp
+ bytesperline
;
348 linesdone
= dma_q
->pos
/ bytesperline
;
349 currlinedone
= dma_q
->pos
% bytesperline
;
350 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
351 startwrite
= fieldstart
+ offset
;
352 lencopy
= bytesperline
- currlinedone
;
353 lencopy
= lencopy
> remain
? remain
: lencopy
;
355 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->length
) {
356 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
357 ((char *)startwrite
+ lencopy
) -
358 ((char *)outp
+ buf
->length
));
359 remain
= (char *)outp
+ buf
->length
- (char *)startwrite
;
364 memcpy(startwrite
, startread
, lencopy
);
369 startwrite
+= lencopy
+ bytesperline
;
370 startread
+= lencopy
;
371 if (bytesperline
> remain
)
374 lencopy
= bytesperline
;
376 if ((char *)startwrite
+ lencopy
> (char *)outp
+
378 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
379 ((char *)startwrite
+ lencopy
) -
380 ((char *)outp
+ buf
->length
));
381 lencopy
= remain
= (char *)outp
+ buf
->length
-
387 memcpy(startwrite
, startread
, lencopy
);
393 /* We have enough data to check for greenscreen */
394 if (outp
[0] < 0x60 && outp
[1440] < 0x60)
395 dev
->greenscreen_detected
= 1;
402 * video-buf generic routine to get the next available buffer
404 static inline void get_next_buf(struct au0828_dmaqueue
*dma_q
,
405 struct au0828_buffer
**buf
)
407 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
409 if (list_empty(&dma_q
->active
)) {
410 au0828_isocdbg("No active queue to serve\n");
411 dev
->isoc_ctl
.buf
= NULL
;
416 /* Get the next buffer */
417 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, list
);
418 /* Cleans up buffer - Useful for testing for frame/URB loss */
419 list_del(&(*buf
)->list
);
421 (*buf
)->vb_buf
= (*buf
)->mem
;
422 dev
->isoc_ctl
.buf
= *buf
;
427 static void au0828_copy_vbi(struct au0828_dev
*dev
,
428 struct au0828_dmaqueue
*dma_q
,
429 struct au0828_buffer
*buf
,
431 unsigned char *outp
, unsigned long len
)
433 unsigned char *startwrite
, *startread
;
438 au0828_isocdbg("dev is null\n");
443 au0828_isocdbg("dma_q is null\n");
449 au0828_isocdbg("p is null\n");
453 au0828_isocdbg("outp is null\n");
457 bytesperline
= dev
->vbi_width
;
459 if (dma_q
->pos
+ len
> buf
->length
)
460 len
= buf
->length
- dma_q
->pos
;
463 startwrite
= outp
+ (dma_q
->pos
/ 2);
465 /* Make sure the bottom field populates the second half of the frame */
466 if (buf
->top_field
== 0)
467 startwrite
+= bytesperline
* dev
->vbi_height
;
469 for (i
= 0; i
< len
; i
+= 2)
470 startwrite
[j
++] = startread
[i
+1];
477 * video-buf generic routine to get the next available VBI buffer
479 static inline void vbi_get_next_buf(struct au0828_dmaqueue
*dma_q
,
480 struct au0828_buffer
**buf
)
482 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vbiq
);
484 if (list_empty(&dma_q
->active
)) {
485 au0828_isocdbg("No active queue to serve\n");
486 dev
->isoc_ctl
.vbi_buf
= NULL
;
491 /* Get the next buffer */
492 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, list
);
493 /* Cleans up buffer - Useful for testing for frame/URB loss */
494 list_del(&(*buf
)->list
);
496 (*buf
)->vb_buf
= (*buf
)->mem
;
497 dev
->isoc_ctl
.vbi_buf
= *buf
;
502 * Controls the isoc copy of each urb packet
504 static inline int au0828_isoc_copy(struct au0828_dev
*dev
, struct urb
*urb
)
506 struct au0828_buffer
*buf
;
507 struct au0828_buffer
*vbi_buf
;
508 struct au0828_dmaqueue
*dma_q
= urb
->context
;
509 struct au0828_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
510 unsigned char *outp
= NULL
;
511 unsigned char *vbioutp
= NULL
;
512 int i
, len
= 0, rc
= 1;
515 unsigned int vbi_field_size
;
516 unsigned int remain
, lencopy
;
521 if (test_bit(DEV_DISCONNECTED
, &dev
->dev_state
) ||
522 test_bit(DEV_MISCONFIGURED
, &dev
->dev_state
))
525 if (urb
->status
< 0) {
526 print_err_status(dev
, -1, urb
->status
);
527 if (urb
->status
== -ENOENT
)
531 buf
= dev
->isoc_ctl
.buf
;
533 outp
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
535 vbi_buf
= dev
->isoc_ctl
.vbi_buf
;
537 vbioutp
= vb2_plane_vaddr(&vbi_buf
->vb
.vb2_buf
, 0);
539 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
540 int status
= urb
->iso_frame_desc
[i
].status
;
543 print_err_status(dev
, i
, status
);
544 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
548 if (urb
->iso_frame_desc
[i
].actual_length
<= 0)
551 if (urb
->iso_frame_desc
[i
].actual_length
>
553 au0828_isocdbg("packet bigger than packet size");
557 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
559 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
565 au0828_isocdbg("Video frame %s\n",
566 (fbyte
& 0x40) ? "odd" : "even");
570 buffer_filled(dev
, vbi_dma_q
, vbi_buf
);
571 vbi_get_next_buf(vbi_dma_q
, &vbi_buf
);
575 vbioutp
= vb2_plane_vaddr(
576 &vbi_buf
->vb
.vb2_buf
, 0);
580 buffer_filled(dev
, dma_q
, buf
);
581 get_next_buf(dma_q
, &buf
);
585 outp
= vb2_plane_vaddr(
586 &buf
->vb
.vb2_buf
, 0);
588 /* As long as isoc traffic is arriving, keep
589 resetting the timer */
590 if (dev
->vid_timeout_running
)
591 mod_timer(&dev
->vid_timeout
,
592 jiffies
+ (HZ
/ 10));
593 if (dev
->vbi_timeout_running
)
594 mod_timer(&dev
->vbi_timeout
,
595 jiffies
+ (HZ
/ 10));
605 if (vbi_buf
!= NULL
) {
607 vbi_buf
->top_field
= 1;
609 vbi_buf
->top_field
= 0;
617 vbi_field_size
= dev
->vbi_width
* dev
->vbi_height
* 2;
618 if (dev
->vbi_read
< vbi_field_size
) {
619 remain
= vbi_field_size
- dev
->vbi_read
;
626 au0828_copy_vbi(dev
, vbi_dma_q
, vbi_buf
, p
,
631 dev
->vbi_read
+= lencopy
;
634 if (dev
->vbi_read
>= vbi_field_size
&& buf
!= NULL
)
635 au0828_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
640 void au0828_usb_v4l2_media_release(struct au0828_dev
*dev
)
642 #ifdef CONFIG_MEDIA_CONTROLLER
645 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
646 if (AUVI_INPUT(i
).type
== AU0828_VMUX_UNDEFINED
)
648 media_device_unregister_entity(&dev
->input_ent
[i
]);
653 static void au0828_usb_v4l2_release(struct v4l2_device
*v4l2_dev
)
655 struct au0828_dev
*dev
=
656 container_of(v4l2_dev
, struct au0828_dev
, v4l2_dev
);
658 v4l2_ctrl_handler_free(&dev
->v4l2_ctrl_hdl
);
659 v4l2_device_unregister(&dev
->v4l2_dev
);
660 au0828_usb_v4l2_media_release(dev
);
661 au0828_usb_release(dev
);
664 int au0828_v4l2_device_register(struct usb_interface
*interface
,
665 struct au0828_dev
*dev
)
669 if (AUVI_INPUT(0).type
== AU0828_VMUX_UNDEFINED
)
672 /* Create the v4l2_device */
673 #ifdef CONFIG_MEDIA_CONTROLLER
674 dev
->v4l2_dev
.mdev
= dev
->media_dev
;
676 retval
= v4l2_device_register(&interface
->dev
, &dev
->v4l2_dev
);
678 pr_err("%s() v4l2_device_register failed\n",
683 dev
->v4l2_dev
.release
= au0828_usb_v4l2_release
;
685 /* This control handler will inherit the controls from au8522 */
686 retval
= v4l2_ctrl_handler_init(&dev
->v4l2_ctrl_hdl
, 4);
688 pr_err("%s() v4l2_ctrl_handler_init failed\n",
692 dev
->v4l2_dev
.ctrl_handler
= &dev
->v4l2_ctrl_hdl
;
697 static int queue_setup(struct vb2_queue
*vq
,
698 unsigned int *nbuffers
, unsigned int *nplanes
,
699 unsigned int sizes
[], struct device
*alloc_devs
[])
701 struct au0828_dev
*dev
= vb2_get_drv_priv(vq
);
702 unsigned long size
= dev
->height
* dev
->bytesperline
;
705 return sizes
[0] < size
? -EINVAL
: 0;
712 buffer_prepare(struct vb2_buffer
*vb
)
714 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
715 struct au0828_buffer
*buf
= container_of(vbuf
,
716 struct au0828_buffer
, vb
);
717 struct au0828_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
719 buf
->length
= dev
->height
* dev
->bytesperline
;
721 if (vb2_plane_size(vb
, 0) < buf
->length
) {
722 pr_err("%s data will not fit into plane (%lu < %lu)\n",
723 __func__
, vb2_plane_size(vb
, 0), buf
->length
);
726 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0, buf
->length
);
731 buffer_queue(struct vb2_buffer
*vb
)
733 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
734 struct au0828_buffer
*buf
= container_of(vbuf
,
735 struct au0828_buffer
,
737 struct au0828_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
738 struct au0828_dmaqueue
*vidq
= &dev
->vidq
;
739 unsigned long flags
= 0;
741 buf
->mem
= vb2_plane_vaddr(vb
, 0);
742 buf
->length
= vb2_plane_size(vb
, 0);
744 spin_lock_irqsave(&dev
->slock
, flags
);
745 list_add_tail(&buf
->list
, &vidq
->active
);
746 spin_unlock_irqrestore(&dev
->slock
, flags
);
749 static int au0828_i2s_init(struct au0828_dev
*dev
)
751 /* Enable i2s mode */
752 au0828_writereg(dev
, AU0828_AUDIOCTRL_50C
, 0x01);
757 * Auvitek au0828 analog stream enable
759 static int au0828_analog_stream_enable(struct au0828_dev
*d
)
761 struct usb_interface
*iface
;
764 dprintk(1, "au0828_analog_stream_enable called\n");
766 iface
= usb_ifnum_to_if(d
->usbdev
, 0);
767 if (iface
&& iface
->cur_altsetting
->desc
.bAlternateSetting
!= 5) {
768 dprintk(1, "Changing intf#0 to alt 5\n");
769 /* set au0828 interface0 to AS5 here again */
770 ret
= usb_set_interface(d
->usbdev
, 0, 5);
772 pr_info("Au0828 can't set alt setting to 5!\n");
777 h
= d
->height
/ 2 + 2;
780 au0828_writereg(d
, AU0828_SENSORCTRL_VBI_103
, 0x00);
781 au0828_writereg(d
, 0x106, 0x00);
783 au0828_writereg(d
, 0x110, 0x00);
784 au0828_writereg(d
, 0x111, 0x00);
785 au0828_writereg(d
, 0x114, w
& 0xff);
786 au0828_writereg(d
, 0x115, w
>> 8);
788 au0828_writereg(d
, 0x112, 0x00);
789 au0828_writereg(d
, 0x113, 0x00);
790 au0828_writereg(d
, 0x116, h
& 0xff);
791 au0828_writereg(d
, 0x117, h
>> 8);
792 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0xb3);
797 static int au0828_analog_stream_disable(struct au0828_dev
*d
)
799 dprintk(1, "au0828_analog_stream_disable called\n");
800 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0x0);
804 static void au0828_analog_stream_reset(struct au0828_dev
*dev
)
806 dprintk(1, "au0828_analog_stream_reset called\n");
807 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0x0);
809 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0xb3);
813 * Some operations needs to stop current streaming
815 static int au0828_stream_interrupt(struct au0828_dev
*dev
)
819 dev
->stream_state
= STREAM_INTERRUPT
;
820 if (test_bit(DEV_DISCONNECTED
, &dev
->dev_state
))
823 set_bit(DEV_MISCONFIGURED
, &dev
->dev_state
);
824 dprintk(1, "%s device is misconfigured!\n", __func__
);
830 int au0828_start_analog_streaming(struct vb2_queue
*vq
, unsigned int count
)
832 struct au0828_dev
*dev
= vb2_get_drv_priv(vq
);
835 dprintk(1, "au0828_start_analog_streaming called %d\n",
836 dev
->streaming_users
);
838 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
839 dev
->frame_count
= 0;
841 dev
->vbi_frame_count
= 0;
843 if (dev
->streaming_users
== 0) {
844 /* If we were doing ac97 instead of i2s, it would go here...*/
845 au0828_i2s_init(dev
);
846 rc
= au0828_init_isoc(dev
, AU0828_ISO_PACKETS_PER_URB
,
847 AU0828_MAX_ISO_BUFS
, dev
->max_pkt_size
,
850 pr_info("au0828_init_isoc failed\n");
854 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
855 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
,
857 dev
->vid_timeout_running
= 1;
858 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
859 } else if (vq
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
860 dev
->vbi_timeout_running
= 1;
861 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
864 dev
->streaming_users
++;
868 static void au0828_stop_streaming(struct vb2_queue
*vq
)
870 struct au0828_dev
*dev
= vb2_get_drv_priv(vq
);
871 struct au0828_dmaqueue
*vidq
= &dev
->vidq
;
872 unsigned long flags
= 0;
874 dprintk(1, "au0828_stop_streaming called %d\n", dev
->streaming_users
);
876 if (dev
->streaming_users
-- == 1)
877 au0828_uninit_isoc(dev
);
879 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 0);
880 dev
->vid_timeout_running
= 0;
881 del_timer_sync(&dev
->vid_timeout
);
883 spin_lock_irqsave(&dev
->slock
, flags
);
884 if (dev
->isoc_ctl
.buf
!= NULL
) {
885 vb2_buffer_done(&dev
->isoc_ctl
.buf
->vb
.vb2_buf
,
886 VB2_BUF_STATE_ERROR
);
887 dev
->isoc_ctl
.buf
= NULL
;
889 while (!list_empty(&vidq
->active
)) {
890 struct au0828_buffer
*buf
;
892 buf
= list_entry(vidq
->active
.next
, struct au0828_buffer
, list
);
893 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
894 list_del(&buf
->list
);
896 spin_unlock_irqrestore(&dev
->slock
, flags
);
899 void au0828_stop_vbi_streaming(struct vb2_queue
*vq
)
901 struct au0828_dev
*dev
= vb2_get_drv_priv(vq
);
902 struct au0828_dmaqueue
*vbiq
= &dev
->vbiq
;
903 unsigned long flags
= 0;
905 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
906 dev
->streaming_users
);
908 if (dev
->streaming_users
-- == 1)
909 au0828_uninit_isoc(dev
);
911 spin_lock_irqsave(&dev
->slock
, flags
);
912 if (dev
->isoc_ctl
.vbi_buf
!= NULL
) {
913 vb2_buffer_done(&dev
->isoc_ctl
.vbi_buf
->vb
.vb2_buf
,
914 VB2_BUF_STATE_ERROR
);
915 dev
->isoc_ctl
.vbi_buf
= NULL
;
917 while (!list_empty(&vbiq
->active
)) {
918 struct au0828_buffer
*buf
;
920 buf
= list_entry(vbiq
->active
.next
, struct au0828_buffer
, list
);
921 list_del(&buf
->list
);
922 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
924 spin_unlock_irqrestore(&dev
->slock
, flags
);
926 dev
->vbi_timeout_running
= 0;
927 del_timer_sync(&dev
->vbi_timeout
);
930 static const struct vb2_ops au0828_video_qops
= {
931 .queue_setup
= queue_setup
,
932 .buf_prepare
= buffer_prepare
,
933 .buf_queue
= buffer_queue
,
934 .start_streaming
= au0828_start_analog_streaming
,
935 .stop_streaming
= au0828_stop_streaming
,
936 .wait_prepare
= vb2_ops_wait_prepare
,
937 .wait_finish
= vb2_ops_wait_finish
,
940 /* ------------------------------------------------------------------
942 ------------------------------------------------------------------*/
944 * au0828_analog_unregister
945 * unregister v4l2 devices
947 int au0828_analog_unregister(struct au0828_dev
*dev
)
949 dprintk(1, "au0828_analog_unregister called\n");
952 if (AUVI_INPUT(0).type
== AU0828_VMUX_UNDEFINED
)
955 mutex_lock(&au0828_sysfs_lock
);
956 video_unregister_device(&dev
->vdev
);
957 video_unregister_device(&dev
->vbi_dev
);
958 mutex_unlock(&au0828_sysfs_lock
);
960 v4l2_device_disconnect(&dev
->v4l2_dev
);
961 v4l2_device_put(&dev
->v4l2_dev
);
966 /* This function ensures that video frames continue to be delivered even if
967 the ITU-656 input isn't receiving any data (thereby preventing applications
968 such as tvtime from hanging) */
969 static void au0828_vid_buffer_timeout(unsigned long data
)
971 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
972 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
973 struct au0828_buffer
*buf
;
974 unsigned char *vid_data
;
975 unsigned long flags
= 0;
977 spin_lock_irqsave(&dev
->slock
, flags
);
979 buf
= dev
->isoc_ctl
.buf
;
981 vid_data
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
982 memset(vid_data
, 0x00, buf
->length
); /* Blank green frame */
983 buffer_filled(dev
, dma_q
, buf
);
985 get_next_buf(dma_q
, &buf
);
987 if (dev
->vid_timeout_running
== 1)
988 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
990 spin_unlock_irqrestore(&dev
->slock
, flags
);
993 static void au0828_vbi_buffer_timeout(unsigned long data
)
995 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
996 struct au0828_dmaqueue
*dma_q
= &dev
->vbiq
;
997 struct au0828_buffer
*buf
;
998 unsigned char *vbi_data
;
999 unsigned long flags
= 0;
1001 spin_lock_irqsave(&dev
->slock
, flags
);
1003 buf
= dev
->isoc_ctl
.vbi_buf
;
1005 vbi_data
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
1006 memset(vbi_data
, 0x00, buf
->length
);
1007 buffer_filled(dev
, dma_q
, buf
);
1009 vbi_get_next_buf(dma_q
, &buf
);
1011 if (dev
->vbi_timeout_running
== 1)
1012 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1013 spin_unlock_irqrestore(&dev
->slock
, flags
);
1016 static int au0828_v4l2_open(struct file
*filp
)
1018 struct au0828_dev
*dev
= video_drvdata(filp
);
1022 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1023 __func__
, dev
->std_set_in_tuner_core
, dev
->dev_state
,
1024 dev
->streaming_users
, dev
->users
);
1026 if (mutex_lock_interruptible(&dev
->lock
))
1027 return -ERESTARTSYS
;
1029 ret
= v4l2_fh_open(filp
);
1031 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1033 mutex_unlock(&dev
->lock
);
1037 if (dev
->users
== 0) {
1038 au0828_analog_stream_enable(dev
);
1039 au0828_analog_stream_reset(dev
);
1040 dev
->stream_state
= STREAM_OFF
;
1041 set_bit(DEV_INITIALIZED
, &dev
->dev_state
);
1044 mutex_unlock(&dev
->lock
);
1048 static int au0828_v4l2_close(struct file
*filp
)
1051 struct au0828_dev
*dev
= video_drvdata(filp
);
1052 struct video_device
*vdev
= video_devdata(filp
);
1055 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1056 __func__
, dev
->std_set_in_tuner_core
, dev
->dev_state
,
1057 dev
->streaming_users
, dev
->users
);
1059 mutex_lock(&dev
->lock
);
1060 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
&& dev
->vid_timeout_running
) {
1061 /* Cancel timeout thread in case they didn't call streamoff */
1062 dev
->vid_timeout_running
= 0;
1063 del_timer_sync(&dev
->vid_timeout
);
1064 } else if (vdev
->vfl_type
== VFL_TYPE_VBI
&&
1065 dev
->vbi_timeout_running
) {
1066 /* Cancel timeout thread in case they didn't call streamoff */
1067 dev
->vbi_timeout_running
= 0;
1068 del_timer_sync(&dev
->vbi_timeout
);
1071 if (test_bit(DEV_DISCONNECTED
, &dev
->dev_state
))
1074 if (dev
->users
== 1) {
1076 * Avoid putting tuner in sleep if DVB or ALSA are
1079 * On most USB devices like au0828 the tuner can
1080 * be safely put in sleep stare here if ALSA isn't
1081 * streaming. Exceptions are some very old USB tuner
1082 * models such as em28xx-based WinTV USB2 which have
1083 * a separate audio output jack. The devices that have
1084 * a separate audio output jack have analog tuners,
1085 * like Philips FM1236. Those devices are always on,
1086 * so the s_power callback are silently ignored.
1087 * So, the current logic here does the following:
1088 * Disable (put tuner to sleep) when
1089 * - ALSA and DVB aren't not streaming;
1090 * - the last V4L2 file handler is closed.
1094 * Additionally, this logic could be improved to
1095 * disable the media source if the above conditions
1096 * are met and if the device:
1097 * - doesn't have a separate audio out plug (or
1098 * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1100 * Once this additional logic is in place, a callback
1101 * is needed to enable the media source and power on
1102 * the tuner, for radio to work.
1104 ret
= v4l_enable_media_source(vdev
);
1106 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
,
1108 dev
->std_set_in_tuner_core
= 0;
1110 /* When close the device, set the usb intf0 into alt0 to free
1112 ret
= usb_set_interface(dev
->usbdev
, 0, 0);
1114 pr_info("Au0828 can't set alternate to 0!\n");
1117 _vb2_fop_release(filp
, NULL
);
1119 mutex_unlock(&dev
->lock
);
1123 /* Must be called with dev->lock held */
1124 static void au0828_init_tuner(struct au0828_dev
*dev
)
1126 struct v4l2_frequency f
= {
1127 .frequency
= dev
->ctrl_freq
,
1128 .type
= V4L2_TUNER_ANALOG_TV
,
1131 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1132 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1134 if (dev
->std_set_in_tuner_core
)
1136 dev
->std_set_in_tuner_core
= 1;
1137 i2c_gate_ctrl(dev
, 1);
1138 /* If we've never sent the standard in tuner core, do so now.
1139 We don't do this at device probe because we don't want to
1140 incur the cost of a firmware load */
1141 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_std
, dev
->std
);
1142 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, &f
);
1143 i2c_gate_ctrl(dev
, 0);
1146 static int au0828_set_format(struct au0828_dev
*dev
, unsigned int cmd
,
1147 struct v4l2_format
*format
)
1150 int width
= format
->fmt
.pix
.width
;
1151 int height
= format
->fmt
.pix
.height
;
1153 /* If they are demanding a format other than the one we support,
1154 bail out (tvtime asks for UYVY and then retries with YUYV) */
1155 if (format
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_UYVY
)
1158 /* format->fmt.pix.width only support 720 and height 480 */
1164 format
->fmt
.pix
.width
= width
;
1165 format
->fmt
.pix
.height
= height
;
1166 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1167 format
->fmt
.pix
.bytesperline
= width
* 2;
1168 format
->fmt
.pix
.sizeimage
= width
* height
* 2;
1169 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1170 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1171 format
->fmt
.pix
.priv
= 0;
1173 if (cmd
== VIDIOC_TRY_FMT
)
1176 /* maybe set new image format, driver current only support 720*480 */
1178 dev
->height
= height
;
1179 dev
->frame_size
= width
* height
* 2;
1180 dev
->field_size
= width
* height
;
1181 dev
->bytesperline
= width
* 2;
1183 if (dev
->stream_state
== STREAM_ON
) {
1184 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1185 ret
= au0828_stream_interrupt(dev
);
1187 dprintk(1, "error interrupting video stream!\n");
1192 au0828_analog_stream_enable(dev
);
1197 static int vidioc_querycap(struct file
*file
, void *priv
,
1198 struct v4l2_capability
*cap
)
1200 struct video_device
*vdev
= video_devdata(file
);
1201 struct au0828_dev
*dev
= video_drvdata(file
);
1203 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1204 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1206 strlcpy(cap
->driver
, "au0828", sizeof(cap
->driver
));
1207 strlcpy(cap
->card
, dev
->board
.name
, sizeof(cap
->card
));
1208 usb_make_path(dev
->usbdev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1210 /* set the device capabilities */
1211 cap
->device_caps
= V4L2_CAP_AUDIO
|
1212 V4L2_CAP_READWRITE
|
1213 V4L2_CAP_STREAMING
|
1215 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
1216 cap
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
;
1218 cap
->device_caps
|= V4L2_CAP_VBI_CAPTURE
;
1219 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
|
1220 V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_VIDEO_CAPTURE
;
1224 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1225 struct v4l2_fmtdesc
*f
)
1230 dprintk(1, "%s called\n", __func__
);
1232 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1233 strcpy(f
->description
, "Packed YUV2");
1236 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
1241 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1242 struct v4l2_format
*f
)
1244 struct au0828_dev
*dev
= video_drvdata(file
);
1246 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1247 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1249 f
->fmt
.pix
.width
= dev
->width
;
1250 f
->fmt
.pix
.height
= dev
->height
;
1251 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1252 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
1253 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
1254 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
; /* NTSC/PAL */
1255 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1256 f
->fmt
.pix
.priv
= 0;
1260 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1261 struct v4l2_format
*f
)
1263 struct au0828_dev
*dev
= video_drvdata(file
);
1265 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1266 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1268 return au0828_set_format(dev
, VIDIOC_TRY_FMT
, f
);
1271 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1272 struct v4l2_format
*f
)
1274 struct au0828_dev
*dev
= video_drvdata(file
);
1277 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1278 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1280 rc
= check_dev(dev
);
1284 if (vb2_is_busy(&dev
->vb_vidq
)) {
1285 pr_info("%s queue busy\n", __func__
);
1290 rc
= au0828_set_format(dev
, VIDIOC_S_FMT
, f
);
1295 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
1297 struct au0828_dev
*dev
= video_drvdata(file
);
1299 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1300 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1302 if (norm
== dev
->std
)
1305 if (dev
->streaming_users
> 0)
1310 au0828_init_tuner(dev
);
1312 i2c_gate_ctrl(dev
, 1);
1315 * FIXME: when we support something other than 60Hz standards,
1316 * we are going to have to make the au0828 bridge adjust the size
1317 * of its capture buffer, which is currently hardcoded at 720x480
1320 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_std
, norm
);
1322 i2c_gate_ctrl(dev
, 0);
1327 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1329 struct au0828_dev
*dev
= video_drvdata(file
);
1331 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1332 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1338 static int vidioc_enum_input(struct file
*file
, void *priv
,
1339 struct v4l2_input
*input
)
1341 struct au0828_dev
*dev
= video_drvdata(file
);
1344 static const char *inames
[] = {
1345 [AU0828_VMUX_UNDEFINED
] = "Undefined",
1346 [AU0828_VMUX_COMPOSITE
] = "Composite",
1347 [AU0828_VMUX_SVIDEO
] = "S-Video",
1348 [AU0828_VMUX_CABLE
] = "Cable TV",
1349 [AU0828_VMUX_TELEVISION
] = "Television",
1350 [AU0828_VMUX_DVB
] = "DVB",
1353 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1354 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1358 if (tmp
>= AU0828_MAX_INPUT
)
1360 if (AUVI_INPUT(tmp
).type
== 0)
1364 strcpy(input
->name
, inames
[AUVI_INPUT(tmp
).type
]);
1365 if ((AUVI_INPUT(tmp
).type
== AU0828_VMUX_TELEVISION
) ||
1366 (AUVI_INPUT(tmp
).type
== AU0828_VMUX_CABLE
)) {
1367 input
->type
|= V4L2_INPUT_TYPE_TUNER
;
1368 input
->audioset
= 1;
1370 input
->type
|= V4L2_INPUT_TYPE_CAMERA
;
1371 input
->audioset
= 2;
1374 input
->std
= dev
->vdev
.tvnorms
;
1379 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1381 struct au0828_dev
*dev
= video_drvdata(file
);
1383 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1384 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1386 *i
= dev
->ctrl_input
;
1390 static void au0828_s_input(struct au0828_dev
*dev
, int index
)
1394 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1395 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1397 switch (AUVI_INPUT(index
).type
) {
1398 case AU0828_VMUX_SVIDEO
:
1399 dev
->input_type
= AU0828_VMUX_SVIDEO
;
1400 dev
->ctrl_ainput
= 1;
1402 case AU0828_VMUX_COMPOSITE
:
1403 dev
->input_type
= AU0828_VMUX_COMPOSITE
;
1404 dev
->ctrl_ainput
= 1;
1406 case AU0828_VMUX_TELEVISION
:
1407 dev
->input_type
= AU0828_VMUX_TELEVISION
;
1408 dev
->ctrl_ainput
= 0;
1411 dprintk(1, "unknown input type set [%d]\n",
1412 AUVI_INPUT(index
).type
);
1416 dev
->ctrl_input
= index
;
1418 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
1419 AUVI_INPUT(index
).vmux
, 0, 0);
1421 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1423 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1431 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1433 /* Make sure we leave it turned on if some
1434 other input is routed to this callback */
1435 if ((AUVI_INPUT(i
).audio_setup
) !=
1436 ((AUVI_INPUT(index
).audio_setup
))) {
1437 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1442 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
1443 AUVI_INPUT(index
).amux
, 0, 0);
1446 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int index
)
1448 struct au0828_dev
*dev
= video_drvdata(file
);
1449 struct video_device
*vfd
= video_devdata(file
);
1451 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__
,
1453 if (index
>= AU0828_MAX_INPUT
)
1455 if (AUVI_INPUT(index
).type
== 0)
1458 if (dev
->ctrl_input
== index
)
1461 au0828_s_input(dev
, index
);
1464 * Input has been changed. Disable the media source
1465 * associated with the old input and enable source
1466 * for the newly set input
1468 v4l_disable_media_source(vfd
);
1469 return v4l_enable_media_source(vfd
);
1472 static int vidioc_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1477 dprintk(1, "%s called\n", __func__
);
1480 strcpy(a
->name
, "Television");
1482 strcpy(a
->name
, "Line in");
1484 a
->capability
= V4L2_AUDCAP_STEREO
;
1488 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1490 struct au0828_dev
*dev
= video_drvdata(file
);
1492 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1493 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1495 a
->index
= dev
->ctrl_ainput
;
1497 strcpy(a
->name
, "Television");
1499 strcpy(a
->name
, "Line in");
1501 a
->capability
= V4L2_AUDCAP_STEREO
;
1505 static int vidioc_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*a
)
1507 struct au0828_dev
*dev
= video_drvdata(file
);
1509 if (a
->index
!= dev
->ctrl_ainput
)
1512 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1513 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1517 static int vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1519 struct au0828_dev
*dev
= video_drvdata(file
);
1520 struct video_device
*vfd
= video_devdata(file
);
1526 ret
= v4l_enable_media_source(vfd
);
1530 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1531 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1533 strcpy(t
->name
, "Auvitek tuner");
1535 au0828_init_tuner(dev
);
1536 i2c_gate_ctrl(dev
, 1);
1537 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1538 i2c_gate_ctrl(dev
, 0);
1542 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1543 const struct v4l2_tuner
*t
)
1545 struct au0828_dev
*dev
= video_drvdata(file
);
1550 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1551 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1553 au0828_init_tuner(dev
);
1554 i2c_gate_ctrl(dev
, 1);
1555 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1556 i2c_gate_ctrl(dev
, 0);
1558 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t
->signal
,
1565 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1566 struct v4l2_frequency
*freq
)
1568 struct au0828_dev
*dev
= video_drvdata(file
);
1570 if (freq
->tuner
!= 0)
1572 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1573 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1574 freq
->frequency
= dev
->ctrl_freq
;
1578 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1579 const struct v4l2_frequency
*freq
)
1581 struct au0828_dev
*dev
= video_drvdata(file
);
1582 struct v4l2_frequency new_freq
= *freq
;
1584 if (freq
->tuner
!= 0)
1587 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1588 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1590 au0828_init_tuner(dev
);
1591 i2c_gate_ctrl(dev
, 1);
1593 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, freq
);
1594 /* Get the actual set (and possibly clamped) frequency */
1595 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, &new_freq
);
1596 dev
->ctrl_freq
= new_freq
.frequency
;
1598 i2c_gate_ctrl(dev
, 0);
1600 au0828_analog_stream_reset(dev
);
1606 /* RAW VBI ioctls */
1608 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1609 struct v4l2_format
*format
)
1611 struct au0828_dev
*dev
= video_drvdata(file
);
1613 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1614 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1616 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1617 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1618 format
->fmt
.vbi
.offset
= 0;
1619 format
->fmt
.vbi
.flags
= 0;
1620 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1622 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1623 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1624 format
->fmt
.vbi
.start
[0] = 21;
1625 format
->fmt
.vbi
.start
[1] = 284;
1626 memset(format
->fmt
.vbi
.reserved
, 0, sizeof(format
->fmt
.vbi
.reserved
));
1631 static int vidioc_cropcap(struct file
*file
, void *priv
,
1632 struct v4l2_cropcap
*cc
)
1634 struct au0828_dev
*dev
= video_drvdata(file
);
1636 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1639 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1640 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1642 cc
->bounds
.left
= 0;
1644 cc
->bounds
.width
= dev
->width
;
1645 cc
->bounds
.height
= dev
->height
;
1647 cc
->defrect
= cc
->bounds
;
1649 cc
->pixelaspect
.numerator
= 54;
1650 cc
->pixelaspect
.denominator
= 59;
1655 #ifdef CONFIG_VIDEO_ADV_DEBUG
1656 static int vidioc_g_register(struct file
*file
, void *priv
,
1657 struct v4l2_dbg_register
*reg
)
1659 struct au0828_dev
*dev
= video_drvdata(file
);
1661 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1662 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1664 reg
->val
= au0828_read(dev
, reg
->reg
);
1669 static int vidioc_s_register(struct file
*file
, void *priv
,
1670 const struct v4l2_dbg_register
*reg
)
1672 struct au0828_dev
*dev
= video_drvdata(file
);
1674 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1675 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1677 return au0828_writereg(dev
, reg
->reg
, reg
->val
);
1681 static int vidioc_log_status(struct file
*file
, void *fh
)
1683 struct video_device
*vdev
= video_devdata(file
);
1685 dprintk(1, "%s called\n", __func__
);
1687 v4l2_ctrl_log_status(file
, fh
);
1688 v4l2_device_call_all(vdev
->v4l2_dev
, 0, core
, log_status
);
1692 void au0828_v4l2_suspend(struct au0828_dev
*dev
)
1697 pr_info("stopping V4L2\n");
1699 if (dev
->stream_state
== STREAM_ON
) {
1700 pr_info("stopping V4L2 active URBs\n");
1701 au0828_analog_stream_disable(dev
);
1703 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
1704 urb
= dev
->isoc_ctl
.urb
[i
];
1706 if (!irqs_disabled())
1709 usb_unlink_urb(urb
);
1714 if (dev
->vid_timeout_running
)
1715 del_timer_sync(&dev
->vid_timeout
);
1716 if (dev
->vbi_timeout_running
)
1717 del_timer_sync(&dev
->vbi_timeout
);
1720 void au0828_v4l2_resume(struct au0828_dev
*dev
)
1724 pr_info("restarting V4L2\n");
1726 if (dev
->stream_state
== STREAM_ON
) {
1727 au0828_stream_interrupt(dev
);
1728 au0828_init_tuner(dev
);
1731 if (dev
->vid_timeout_running
)
1732 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
1733 if (dev
->vbi_timeout_running
)
1734 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1736 /* If we were doing ac97 instead of i2s, it would go here...*/
1737 au0828_i2s_init(dev
);
1739 au0828_analog_stream_enable(dev
);
1741 if (!(dev
->stream_state
== STREAM_ON
)) {
1742 au0828_analog_stream_reset(dev
);
1744 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
1745 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
1747 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1749 au0828_uninit_isoc(dev
);
1755 static struct v4l2_file_operations au0828_v4l_fops
= {
1756 .owner
= THIS_MODULE
,
1757 .open
= au0828_v4l2_open
,
1758 .release
= au0828_v4l2_close
,
1759 .read
= vb2_fop_read
,
1760 .poll
= vb2_fop_poll
,
1761 .mmap
= vb2_fop_mmap
,
1762 .unlocked_ioctl
= video_ioctl2
,
1765 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1766 .vidioc_querycap
= vidioc_querycap
,
1767 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1768 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1769 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1770 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1771 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1772 .vidioc_try_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1773 .vidioc_s_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1774 .vidioc_enumaudio
= vidioc_enumaudio
,
1775 .vidioc_g_audio
= vidioc_g_audio
,
1776 .vidioc_s_audio
= vidioc_s_audio
,
1777 .vidioc_cropcap
= vidioc_cropcap
,
1779 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1780 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1781 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1782 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1783 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1784 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1785 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1787 .vidioc_s_std
= vidioc_s_std
,
1788 .vidioc_g_std
= vidioc_g_std
,
1789 .vidioc_enum_input
= vidioc_enum_input
,
1790 .vidioc_g_input
= vidioc_g_input
,
1791 .vidioc_s_input
= vidioc_s_input
,
1793 .vidioc_streamon
= vb2_ioctl_streamon
,
1794 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1796 .vidioc_g_tuner
= vidioc_g_tuner
,
1797 .vidioc_s_tuner
= vidioc_s_tuner
,
1798 .vidioc_g_frequency
= vidioc_g_frequency
,
1799 .vidioc_s_frequency
= vidioc_s_frequency
,
1800 #ifdef CONFIG_VIDEO_ADV_DEBUG
1801 .vidioc_g_register
= vidioc_g_register
,
1802 .vidioc_s_register
= vidioc_s_register
,
1804 .vidioc_log_status
= vidioc_log_status
,
1805 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1806 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1809 static const struct video_device au0828_video_template
= {
1810 .fops
= &au0828_v4l_fops
,
1811 .release
= video_device_release_empty
,
1812 .ioctl_ops
= &video_ioctl_ops
,
1813 .tvnorms
= V4L2_STD_NTSC_M
| V4L2_STD_PAL_M
,
1816 static int au0828_vb2_setup(struct au0828_dev
*dev
)
1819 struct vb2_queue
*q
;
1821 /* Setup Videobuf2 for Video capture */
1823 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1824 q
->io_modes
= VB2_READ
| VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
1825 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1827 q
->buf_struct_size
= sizeof(struct au0828_buffer
);
1828 q
->ops
= &au0828_video_qops
;
1829 q
->mem_ops
= &vb2_vmalloc_memops
;
1831 rc
= vb2_queue_init(q
);
1835 /* Setup Videobuf2 for VBI capture */
1837 q
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1838 q
->io_modes
= VB2_READ
| VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
1839 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1841 q
->buf_struct_size
= sizeof(struct au0828_buffer
);
1842 q
->ops
= &au0828_vbi_qops
;
1843 q
->mem_ops
= &vb2_vmalloc_memops
;
1845 rc
= vb2_queue_init(q
);
1852 static void au0828_analog_create_entities(struct au0828_dev
*dev
)
1854 #if defined(CONFIG_MEDIA_CONTROLLER)
1855 static const char * const inames
[] = {
1856 [AU0828_VMUX_COMPOSITE
] = "Composite",
1857 [AU0828_VMUX_SVIDEO
] = "S-Video",
1858 [AU0828_VMUX_CABLE
] = "Cable TV",
1859 [AU0828_VMUX_TELEVISION
] = "Television",
1860 [AU0828_VMUX_DVB
] = "DVB",
1864 /* Initialize Video and VBI pads */
1865 dev
->video_pad
.flags
= MEDIA_PAD_FL_SINK
;
1866 ret
= media_entity_pads_init(&dev
->vdev
.entity
, 1, &dev
->video_pad
);
1868 pr_err("failed to initialize video media entity!\n");
1870 dev
->vbi_pad
.flags
= MEDIA_PAD_FL_SINK
;
1871 ret
= media_entity_pads_init(&dev
->vbi_dev
.entity
, 1, &dev
->vbi_pad
);
1873 pr_err("failed to initialize vbi media entity!\n");
1875 /* Create entities for each input connector */
1876 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1877 struct media_entity
*ent
= &dev
->input_ent
[i
];
1879 if (AUVI_INPUT(i
).type
== AU0828_VMUX_UNDEFINED
)
1882 ent
->name
= inames
[AUVI_INPUT(i
).type
];
1883 ent
->flags
= MEDIA_ENT_FL_CONNECTOR
;
1884 dev
->input_pad
[i
].flags
= MEDIA_PAD_FL_SOURCE
;
1886 switch (AUVI_INPUT(i
).type
) {
1887 case AU0828_VMUX_COMPOSITE
:
1888 ent
->function
= MEDIA_ENT_F_CONN_COMPOSITE
;
1890 case AU0828_VMUX_SVIDEO
:
1891 ent
->function
= MEDIA_ENT_F_CONN_SVIDEO
;
1893 case AU0828_VMUX_CABLE
:
1894 case AU0828_VMUX_TELEVISION
:
1895 case AU0828_VMUX_DVB
:
1896 default: /* Just to shut up a warning */
1897 ent
->function
= MEDIA_ENT_F_CONN_RF
;
1901 ret
= media_entity_pads_init(ent
, 1, &dev
->input_pad
[i
]);
1903 pr_err("failed to initialize input pad[%d]!\n", i
);
1905 ret
= media_device_register_entity(dev
->media_dev
, ent
);
1907 pr_err("failed to register input entity %d!\n", i
);
1912 /**************************************************************************/
1914 int au0828_analog_register(struct au0828_dev
*dev
,
1915 struct usb_interface
*interface
)
1917 int retval
= -ENOMEM
;
1918 struct usb_host_interface
*iface_desc
;
1919 struct usb_endpoint_descriptor
*endpoint
;
1922 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1923 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
1926 if (AUVI_INPUT(0).type
== AU0828_VMUX_UNDEFINED
)
1929 /* set au0828 usb interface0 to as5 */
1930 retval
= usb_set_interface(dev
->usbdev
,
1931 interface
->cur_altsetting
->desc
.bInterfaceNumber
, 5);
1933 pr_info("Failure setting usb interface0 to as5\n");
1937 /* Figure out which endpoint has the isoc interface */
1938 iface_desc
= interface
->cur_altsetting
;
1939 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
1940 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1941 if (((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
1943 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
1944 == USB_ENDPOINT_XFER_ISOC
)) {
1946 /* we find our isoc in endpoint */
1947 u16 tmp
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1948 dev
->max_pkt_size
= (tmp
& 0x07ff) *
1949 (((tmp
& 0x1800) >> 11) + 1);
1950 dev
->isoc_in_endpointaddr
= endpoint
->bEndpointAddress
;
1952 "Found isoc endpoint 0x%02x, max size = %d\n",
1953 dev
->isoc_in_endpointaddr
, dev
->max_pkt_size
);
1956 if (!(dev
->isoc_in_endpointaddr
)) {
1957 pr_info("Could not locate isoc endpoint\n");
1961 init_waitqueue_head(&dev
->open
);
1962 spin_lock_init(&dev
->slock
);
1964 /* init video dma queues */
1965 INIT_LIST_HEAD(&dev
->vidq
.active
);
1966 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1968 setup_timer(&dev
->vid_timeout
, au0828_vid_buffer_timeout
,
1969 (unsigned long)dev
);
1970 setup_timer(&dev
->vbi_timeout
, au0828_vbi_buffer_timeout
,
1971 (unsigned long)dev
);
1973 dev
->width
= NTSC_STD_W
;
1974 dev
->height
= NTSC_STD_H
;
1975 dev
->field_size
= dev
->width
* dev
->height
;
1976 dev
->frame_size
= dev
->field_size
<< 1;
1977 dev
->bytesperline
= dev
->width
<< 1;
1978 dev
->vbi_width
= 720;
1979 dev
->vbi_height
= 1;
1980 dev
->ctrl_ainput
= 0;
1981 dev
->ctrl_freq
= 960;
1982 dev
->std
= V4L2_STD_NTSC_M
;
1983 /* Default input is TV Tuner */
1984 au0828_s_input(dev
, 0);
1986 mutex_init(&dev
->vb_queue_lock
);
1987 mutex_init(&dev
->vb_vbi_queue_lock
);
1989 /* Fill the video capture device struct */
1990 dev
->vdev
= au0828_video_template
;
1991 dev
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1992 dev
->vdev
.lock
= &dev
->lock
;
1993 dev
->vdev
.queue
= &dev
->vb_vidq
;
1994 dev
->vdev
.queue
->lock
= &dev
->vb_queue_lock
;
1995 strcpy(dev
->vdev
.name
, "au0828a video");
1997 /* Setup the VBI device */
1998 dev
->vbi_dev
= au0828_video_template
;
1999 dev
->vbi_dev
.v4l2_dev
= &dev
->v4l2_dev
;
2000 dev
->vbi_dev
.lock
= &dev
->lock
;
2001 dev
->vbi_dev
.queue
= &dev
->vb_vbiq
;
2002 dev
->vbi_dev
.queue
->lock
= &dev
->vb_vbi_queue_lock
;
2003 strcpy(dev
->vbi_dev
.name
, "au0828a vbi");
2005 /* Init entities at the Media Controller */
2006 au0828_analog_create_entities(dev
);
2008 /* initialize videobuf2 stuff */
2009 retval
= au0828_vb2_setup(dev
);
2011 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2016 /* Register the v4l2 device */
2017 video_set_drvdata(&dev
->vdev
, dev
);
2018 retval
= video_register_device(&dev
->vdev
, VFL_TYPE_GRABBER
, -1);
2020 dprintk(1, "unable to register video device (error = %d).\n",
2026 /* Register the vbi device */
2027 video_set_drvdata(&dev
->vbi_dev
, dev
);
2028 retval
= video_register_device(&dev
->vbi_dev
, VFL_TYPE_VBI
, -1);
2030 dprintk(1, "unable to register vbi device (error = %d).\n",
2033 goto err_reg_vbi_dev
;
2036 #ifdef CONFIG_MEDIA_CONTROLLER
2037 retval
= v4l2_mc_create_media_graph(dev
->media_dev
);
2039 pr_err("%s() au0282_dev_register failed to create graph\n",
2042 goto err_reg_vbi_dev
;
2046 dprintk(1, "%s completed!\n", __func__
);
2051 video_unregister_device(&dev
->vdev
);
2053 vb2_queue_release(&dev
->vb_vidq
);
2054 vb2_queue_release(&dev
->vb_vbiq
);