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"
259 in_interrupt() ? " while in int" : "");
260 au0828_uninit_isoc(dev
);
263 memset(dev
->isoc_ctl
.transfer_buffer
[i
], 0, sb_size
);
265 pipe
= usb_rcvisocpipe(dev
->usbdev
,
266 dev
->isoc_in_endpointaddr
),
268 usb_fill_int_urb(urb
, dev
->usbdev
, pipe
,
269 dev
->isoc_ctl
.transfer_buffer
[i
], sb_size
,
270 au0828_irq_callback
, dma_q
, 1);
272 urb
->number_of_packets
= max_packets
;
273 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
276 for (j
= 0; j
< max_packets
; j
++) {
277 urb
->iso_frame_desc
[j
].offset
= k
;
278 urb
->iso_frame_desc
[j
].length
=
279 dev
->isoc_ctl
.max_pkt_size
;
280 k
+= dev
->isoc_ctl
.max_pkt_size
;
284 /* submit urbs and enables IRQ */
285 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
286 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
288 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
290 au0828_uninit_isoc(dev
);
299 * Announces that a buffer were filled and request the next
301 static inline void buffer_filled(struct au0828_dev
*dev
,
302 struct au0828_dmaqueue
*dma_q
,
303 struct au0828_buffer
*buf
)
305 struct vb2_v4l2_buffer
*vb
= &buf
->vb
;
306 struct vb2_queue
*q
= vb
->vb2_buf
.vb2_queue
;
308 /* Advice that buffer was filled */
309 au0828_isocdbg("[%p/%d] wakeup\n", buf
, buf
->top_field
);
311 if (q
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
312 vb
->sequence
= dev
->frame_count
++;
314 vb
->sequence
= dev
->vbi_frame_count
++;
316 vb
->field
= V4L2_FIELD_INTERLACED
;
317 vb
->vb2_buf
.timestamp
= ktime_get_ns();
318 vb2_buffer_done(&vb
->vb2_buf
, VB2_BUF_STATE_DONE
);
322 * Identify the buffer header type and properly handles
324 static void au0828_copy_video(struct au0828_dev
*dev
,
325 struct au0828_dmaqueue
*dma_q
,
326 struct au0828_buffer
*buf
,
328 unsigned char *outp
, unsigned long len
)
330 void *fieldstart
, *startwrite
, *startread
;
331 int linesdone
, currlinedone
, offset
, lencopy
, remain
;
332 int bytesperline
= dev
->width
<< 1; /* Assumes 16-bit depth @@@@ */
337 if (dma_q
->pos
+ len
> buf
->length
)
338 len
= buf
->length
- dma_q
->pos
;
343 /* Interlaces frame */
347 fieldstart
= outp
+ bytesperline
;
349 linesdone
= dma_q
->pos
/ bytesperline
;
350 currlinedone
= dma_q
->pos
% bytesperline
;
351 offset
= linesdone
* bytesperline
* 2 + currlinedone
;
352 startwrite
= fieldstart
+ offset
;
353 lencopy
= bytesperline
- currlinedone
;
354 lencopy
= lencopy
> remain
? remain
: lencopy
;
356 if ((char *)startwrite
+ lencopy
> (char *)outp
+ buf
->length
) {
357 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
358 ((char *)startwrite
+ lencopy
) -
359 ((char *)outp
+ buf
->length
));
360 remain
= (char *)outp
+ buf
->length
- (char *)startwrite
;
365 memcpy(startwrite
, startread
, lencopy
);
370 startwrite
+= lencopy
+ bytesperline
;
371 startread
+= lencopy
;
372 if (bytesperline
> remain
)
375 lencopy
= bytesperline
;
377 if ((char *)startwrite
+ lencopy
> (char *)outp
+
379 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
380 ((char *)startwrite
+ lencopy
) -
381 ((char *)outp
+ buf
->length
));
382 lencopy
= remain
= (char *)outp
+ buf
->length
-
388 memcpy(startwrite
, startread
, lencopy
);
394 /* We have enough data to check for greenscreen */
395 if (outp
[0] < 0x60 && outp
[1440] < 0x60)
396 dev
->greenscreen_detected
= 1;
403 * video-buf generic routine to get the next available buffer
405 static inline void get_next_buf(struct au0828_dmaqueue
*dma_q
,
406 struct au0828_buffer
**buf
)
408 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vidq
);
410 if (list_empty(&dma_q
->active
)) {
411 au0828_isocdbg("No active queue to serve\n");
412 dev
->isoc_ctl
.buf
= NULL
;
417 /* Get the next buffer */
418 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, list
);
419 /* Cleans up buffer - Useful for testing for frame/URB loss */
420 list_del(&(*buf
)->list
);
422 (*buf
)->vb_buf
= (*buf
)->mem
;
423 dev
->isoc_ctl
.buf
= *buf
;
428 static void au0828_copy_vbi(struct au0828_dev
*dev
,
429 struct au0828_dmaqueue
*dma_q
,
430 struct au0828_buffer
*buf
,
432 unsigned char *outp
, unsigned long len
)
434 unsigned char *startwrite
, *startread
;
439 au0828_isocdbg("dev is null\n");
444 au0828_isocdbg("dma_q is null\n");
450 au0828_isocdbg("p is null\n");
454 au0828_isocdbg("outp is null\n");
458 bytesperline
= dev
->vbi_width
;
460 if (dma_q
->pos
+ len
> buf
->length
)
461 len
= buf
->length
- dma_q
->pos
;
464 startwrite
= outp
+ (dma_q
->pos
/ 2);
466 /* Make sure the bottom field populates the second half of the frame */
467 if (buf
->top_field
== 0)
468 startwrite
+= bytesperline
* dev
->vbi_height
;
470 for (i
= 0; i
< len
; i
+= 2)
471 startwrite
[j
++] = startread
[i
+1];
478 * video-buf generic routine to get the next available VBI buffer
480 static inline void vbi_get_next_buf(struct au0828_dmaqueue
*dma_q
,
481 struct au0828_buffer
**buf
)
483 struct au0828_dev
*dev
= container_of(dma_q
, struct au0828_dev
, vbiq
);
485 if (list_empty(&dma_q
->active
)) {
486 au0828_isocdbg("No active queue to serve\n");
487 dev
->isoc_ctl
.vbi_buf
= NULL
;
492 /* Get the next buffer */
493 *buf
= list_entry(dma_q
->active
.next
, struct au0828_buffer
, list
);
494 /* Cleans up buffer - Useful for testing for frame/URB loss */
495 list_del(&(*buf
)->list
);
497 (*buf
)->vb_buf
= (*buf
)->mem
;
498 dev
->isoc_ctl
.vbi_buf
= *buf
;
503 * Controls the isoc copy of each urb packet
505 static inline int au0828_isoc_copy(struct au0828_dev
*dev
, struct urb
*urb
)
507 struct au0828_buffer
*buf
;
508 struct au0828_buffer
*vbi_buf
;
509 struct au0828_dmaqueue
*dma_q
= urb
->context
;
510 struct au0828_dmaqueue
*vbi_dma_q
= &dev
->vbiq
;
511 unsigned char *outp
= NULL
;
512 unsigned char *vbioutp
= NULL
;
513 int i
, len
= 0, rc
= 1;
516 unsigned int vbi_field_size
;
517 unsigned int remain
, lencopy
;
522 if (test_bit(DEV_DISCONNECTED
, &dev
->dev_state
) ||
523 test_bit(DEV_MISCONFIGURED
, &dev
->dev_state
))
526 if (urb
->status
< 0) {
527 print_err_status(dev
, -1, urb
->status
);
528 if (urb
->status
== -ENOENT
)
532 buf
= dev
->isoc_ctl
.buf
;
534 outp
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
536 vbi_buf
= dev
->isoc_ctl
.vbi_buf
;
538 vbioutp
= vb2_plane_vaddr(&vbi_buf
->vb
.vb2_buf
, 0);
540 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
541 int status
= urb
->iso_frame_desc
[i
].status
;
544 print_err_status(dev
, i
, status
);
545 if (urb
->iso_frame_desc
[i
].status
!= -EPROTO
)
549 if (urb
->iso_frame_desc
[i
].actual_length
<= 0)
552 if (urb
->iso_frame_desc
[i
].actual_length
>
554 au0828_isocdbg("packet bigger than packet size");
558 p
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
560 len
= urb
->iso_frame_desc
[i
].actual_length
- 4;
566 au0828_isocdbg("Video frame %s\n",
567 (fbyte
& 0x40) ? "odd" : "even");
571 buffer_filled(dev
, vbi_dma_q
, vbi_buf
);
572 vbi_get_next_buf(vbi_dma_q
, &vbi_buf
);
576 vbioutp
= vb2_plane_vaddr(
577 &vbi_buf
->vb
.vb2_buf
, 0);
581 buffer_filled(dev
, dma_q
, buf
);
582 get_next_buf(dma_q
, &buf
);
586 outp
= vb2_plane_vaddr(
587 &buf
->vb
.vb2_buf
, 0);
589 /* As long as isoc traffic is arriving, keep
590 resetting the timer */
591 if (dev
->vid_timeout_running
)
592 mod_timer(&dev
->vid_timeout
,
593 jiffies
+ (HZ
/ 10));
594 if (dev
->vbi_timeout_running
)
595 mod_timer(&dev
->vbi_timeout
,
596 jiffies
+ (HZ
/ 10));
606 if (vbi_buf
!= NULL
) {
608 vbi_buf
->top_field
= 1;
610 vbi_buf
->top_field
= 0;
618 vbi_field_size
= dev
->vbi_width
* dev
->vbi_height
* 2;
619 if (dev
->vbi_read
< vbi_field_size
) {
620 remain
= vbi_field_size
- dev
->vbi_read
;
627 au0828_copy_vbi(dev
, vbi_dma_q
, vbi_buf
, p
,
632 dev
->vbi_read
+= lencopy
;
635 if (dev
->vbi_read
>= vbi_field_size
&& buf
!= NULL
)
636 au0828_copy_video(dev
, dma_q
, buf
, p
, outp
, len
);
641 void au0828_usb_v4l2_media_release(struct au0828_dev
*dev
)
643 #ifdef CONFIG_MEDIA_CONTROLLER
646 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
647 if (AUVI_INPUT(i
).type
== AU0828_VMUX_UNDEFINED
)
649 media_device_unregister_entity(&dev
->input_ent
[i
]);
654 static void au0828_usb_v4l2_release(struct v4l2_device
*v4l2_dev
)
656 struct au0828_dev
*dev
=
657 container_of(v4l2_dev
, struct au0828_dev
, v4l2_dev
);
659 v4l2_ctrl_handler_free(&dev
->v4l2_ctrl_hdl
);
660 v4l2_device_unregister(&dev
->v4l2_dev
);
661 au0828_usb_v4l2_media_release(dev
);
662 au0828_usb_release(dev
);
665 int au0828_v4l2_device_register(struct usb_interface
*interface
,
666 struct au0828_dev
*dev
)
670 if (AUVI_INPUT(0).type
== AU0828_VMUX_UNDEFINED
)
673 /* Create the v4l2_device */
674 #ifdef CONFIG_MEDIA_CONTROLLER
675 dev
->v4l2_dev
.mdev
= dev
->media_dev
;
677 retval
= v4l2_device_register(&interface
->dev
, &dev
->v4l2_dev
);
679 pr_err("%s() v4l2_device_register failed\n",
684 dev
->v4l2_dev
.release
= au0828_usb_v4l2_release
;
686 /* This control handler will inherit the controls from au8522 */
687 retval
= v4l2_ctrl_handler_init(&dev
->v4l2_ctrl_hdl
, 4);
689 pr_err("%s() v4l2_ctrl_handler_init failed\n",
693 dev
->v4l2_dev
.ctrl_handler
= &dev
->v4l2_ctrl_hdl
;
698 static int queue_setup(struct vb2_queue
*vq
,
699 unsigned int *nbuffers
, unsigned int *nplanes
,
700 unsigned int sizes
[], struct device
*alloc_devs
[])
702 struct au0828_dev
*dev
= vb2_get_drv_priv(vq
);
703 unsigned long size
= dev
->height
* dev
->bytesperline
;
706 return sizes
[0] < size
? -EINVAL
: 0;
713 buffer_prepare(struct vb2_buffer
*vb
)
715 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
716 struct au0828_buffer
*buf
= container_of(vbuf
,
717 struct au0828_buffer
, vb
);
718 struct au0828_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
720 buf
->length
= dev
->height
* dev
->bytesperline
;
722 if (vb2_plane_size(vb
, 0) < buf
->length
) {
723 pr_err("%s data will not fit into plane (%lu < %lu)\n",
724 __func__
, vb2_plane_size(vb
, 0), buf
->length
);
727 vb2_set_plane_payload(&buf
->vb
.vb2_buf
, 0, buf
->length
);
732 buffer_queue(struct vb2_buffer
*vb
)
734 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
735 struct au0828_buffer
*buf
= container_of(vbuf
,
736 struct au0828_buffer
,
738 struct au0828_dev
*dev
= vb2_get_drv_priv(vb
->vb2_queue
);
739 struct au0828_dmaqueue
*vidq
= &dev
->vidq
;
740 unsigned long flags
= 0;
742 buf
->mem
= vb2_plane_vaddr(vb
, 0);
743 buf
->length
= vb2_plane_size(vb
, 0);
745 spin_lock_irqsave(&dev
->slock
, flags
);
746 list_add_tail(&buf
->list
, &vidq
->active
);
747 spin_unlock_irqrestore(&dev
->slock
, flags
);
750 static int au0828_i2s_init(struct au0828_dev
*dev
)
752 /* Enable i2s mode */
753 au0828_writereg(dev
, AU0828_AUDIOCTRL_50C
, 0x01);
758 * Auvitek au0828 analog stream enable
760 static int au0828_analog_stream_enable(struct au0828_dev
*d
)
762 struct usb_interface
*iface
;
765 dprintk(1, "au0828_analog_stream_enable called\n");
767 iface
= usb_ifnum_to_if(d
->usbdev
, 0);
768 if (iface
&& iface
->cur_altsetting
->desc
.bAlternateSetting
!= 5) {
769 dprintk(1, "Changing intf#0 to alt 5\n");
770 /* set au0828 interface0 to AS5 here again */
771 ret
= usb_set_interface(d
->usbdev
, 0, 5);
773 pr_info("Au0828 can't set alt setting to 5!\n");
778 h
= d
->height
/ 2 + 2;
781 au0828_writereg(d
, AU0828_SENSORCTRL_VBI_103
, 0x00);
782 au0828_writereg(d
, 0x106, 0x00);
784 au0828_writereg(d
, 0x110, 0x00);
785 au0828_writereg(d
, 0x111, 0x00);
786 au0828_writereg(d
, 0x114, w
& 0xff);
787 au0828_writereg(d
, 0x115, w
>> 8);
789 au0828_writereg(d
, 0x112, 0x00);
790 au0828_writereg(d
, 0x113, 0x00);
791 au0828_writereg(d
, 0x116, h
& 0xff);
792 au0828_writereg(d
, 0x117, h
>> 8);
793 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0xb3);
798 static int au0828_analog_stream_disable(struct au0828_dev
*d
)
800 dprintk(1, "au0828_analog_stream_disable called\n");
801 au0828_writereg(d
, AU0828_SENSORCTRL_100
, 0x0);
805 static void au0828_analog_stream_reset(struct au0828_dev
*dev
)
807 dprintk(1, "au0828_analog_stream_reset called\n");
808 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0x0);
810 au0828_writereg(dev
, AU0828_SENSORCTRL_100
, 0xb3);
814 * Some operations needs to stop current streaming
816 static int au0828_stream_interrupt(struct au0828_dev
*dev
)
820 dev
->stream_state
= STREAM_INTERRUPT
;
821 if (test_bit(DEV_DISCONNECTED
, &dev
->dev_state
))
824 set_bit(DEV_MISCONFIGURED
, &dev
->dev_state
);
825 dprintk(1, "%s device is misconfigured!\n", __func__
);
831 int au0828_start_analog_streaming(struct vb2_queue
*vq
, unsigned int count
)
833 struct au0828_dev
*dev
= vb2_get_drv_priv(vq
);
836 dprintk(1, "au0828_start_analog_streaming called %d\n",
837 dev
->streaming_users
);
839 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
)
840 dev
->frame_count
= 0;
842 dev
->vbi_frame_count
= 0;
844 if (dev
->streaming_users
== 0) {
845 /* If we were doing ac97 instead of i2s, it would go here...*/
846 au0828_i2s_init(dev
);
847 rc
= au0828_init_isoc(dev
, AU0828_ISO_PACKETS_PER_URB
,
848 AU0828_MAX_ISO_BUFS
, dev
->max_pkt_size
,
851 pr_info("au0828_init_isoc failed\n");
855 if (vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
856 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
,
858 dev
->vid_timeout_running
= 1;
859 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
860 } else if (vq
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) {
861 dev
->vbi_timeout_running
= 1;
862 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
865 dev
->streaming_users
++;
869 static void au0828_stop_streaming(struct vb2_queue
*vq
)
871 struct au0828_dev
*dev
= vb2_get_drv_priv(vq
);
872 struct au0828_dmaqueue
*vidq
= &dev
->vidq
;
873 unsigned long flags
= 0;
875 dprintk(1, "au0828_stop_streaming called %d\n", dev
->streaming_users
);
877 if (dev
->streaming_users
-- == 1)
878 au0828_uninit_isoc(dev
);
880 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_stream
, 0);
881 dev
->vid_timeout_running
= 0;
882 del_timer_sync(&dev
->vid_timeout
);
884 spin_lock_irqsave(&dev
->slock
, flags
);
885 if (dev
->isoc_ctl
.buf
!= NULL
) {
886 vb2_buffer_done(&dev
->isoc_ctl
.buf
->vb
.vb2_buf
,
887 VB2_BUF_STATE_ERROR
);
888 dev
->isoc_ctl
.buf
= NULL
;
890 while (!list_empty(&vidq
->active
)) {
891 struct au0828_buffer
*buf
;
893 buf
= list_entry(vidq
->active
.next
, struct au0828_buffer
, list
);
894 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
895 list_del(&buf
->list
);
897 spin_unlock_irqrestore(&dev
->slock
, flags
);
900 void au0828_stop_vbi_streaming(struct vb2_queue
*vq
)
902 struct au0828_dev
*dev
= vb2_get_drv_priv(vq
);
903 struct au0828_dmaqueue
*vbiq
= &dev
->vbiq
;
904 unsigned long flags
= 0;
906 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
907 dev
->streaming_users
);
909 if (dev
->streaming_users
-- == 1)
910 au0828_uninit_isoc(dev
);
912 spin_lock_irqsave(&dev
->slock
, flags
);
913 if (dev
->isoc_ctl
.vbi_buf
!= NULL
) {
914 vb2_buffer_done(&dev
->isoc_ctl
.vbi_buf
->vb
.vb2_buf
,
915 VB2_BUF_STATE_ERROR
);
916 dev
->isoc_ctl
.vbi_buf
= NULL
;
918 while (!list_empty(&vbiq
->active
)) {
919 struct au0828_buffer
*buf
;
921 buf
= list_entry(vbiq
->active
.next
, struct au0828_buffer
, list
);
922 list_del(&buf
->list
);
923 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
925 spin_unlock_irqrestore(&dev
->slock
, flags
);
927 dev
->vbi_timeout_running
= 0;
928 del_timer_sync(&dev
->vbi_timeout
);
931 static const struct vb2_ops au0828_video_qops
= {
932 .queue_setup
= queue_setup
,
933 .buf_prepare
= buffer_prepare
,
934 .buf_queue
= buffer_queue
,
935 .start_streaming
= au0828_start_analog_streaming
,
936 .stop_streaming
= au0828_stop_streaming
,
937 .wait_prepare
= vb2_ops_wait_prepare
,
938 .wait_finish
= vb2_ops_wait_finish
,
941 /* ------------------------------------------------------------------
943 ------------------------------------------------------------------*/
945 * au0828_analog_unregister
946 * unregister v4l2 devices
948 int au0828_analog_unregister(struct au0828_dev
*dev
)
950 dprintk(1, "au0828_analog_unregister called\n");
953 if (AUVI_INPUT(0).type
== AU0828_VMUX_UNDEFINED
)
956 mutex_lock(&au0828_sysfs_lock
);
957 video_unregister_device(&dev
->vdev
);
958 video_unregister_device(&dev
->vbi_dev
);
959 mutex_unlock(&au0828_sysfs_lock
);
961 v4l2_device_disconnect(&dev
->v4l2_dev
);
962 v4l2_device_put(&dev
->v4l2_dev
);
967 /* This function ensures that video frames continue to be delivered even if
968 the ITU-656 input isn't receiving any data (thereby preventing applications
969 such as tvtime from hanging) */
970 static void au0828_vid_buffer_timeout(unsigned long data
)
972 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
973 struct au0828_dmaqueue
*dma_q
= &dev
->vidq
;
974 struct au0828_buffer
*buf
;
975 unsigned char *vid_data
;
976 unsigned long flags
= 0;
978 spin_lock_irqsave(&dev
->slock
, flags
);
980 buf
= dev
->isoc_ctl
.buf
;
982 vid_data
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
983 memset(vid_data
, 0x00, buf
->length
); /* Blank green frame */
984 buffer_filled(dev
, dma_q
, buf
);
986 get_next_buf(dma_q
, &buf
);
988 if (dev
->vid_timeout_running
== 1)
989 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
991 spin_unlock_irqrestore(&dev
->slock
, flags
);
994 static void au0828_vbi_buffer_timeout(unsigned long data
)
996 struct au0828_dev
*dev
= (struct au0828_dev
*) data
;
997 struct au0828_dmaqueue
*dma_q
= &dev
->vbiq
;
998 struct au0828_buffer
*buf
;
999 unsigned char *vbi_data
;
1000 unsigned long flags
= 0;
1002 spin_lock_irqsave(&dev
->slock
, flags
);
1004 buf
= dev
->isoc_ctl
.vbi_buf
;
1006 vbi_data
= vb2_plane_vaddr(&buf
->vb
.vb2_buf
, 0);
1007 memset(vbi_data
, 0x00, buf
->length
);
1008 buffer_filled(dev
, dma_q
, buf
);
1010 vbi_get_next_buf(dma_q
, &buf
);
1012 if (dev
->vbi_timeout_running
== 1)
1013 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1014 spin_unlock_irqrestore(&dev
->slock
, flags
);
1017 static int au0828_v4l2_open(struct file
*filp
)
1019 struct au0828_dev
*dev
= video_drvdata(filp
);
1023 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1024 __func__
, dev
->std_set_in_tuner_core
, dev
->dev_state
,
1025 dev
->streaming_users
, dev
->users
);
1027 if (mutex_lock_interruptible(&dev
->lock
))
1028 return -ERESTARTSYS
;
1030 ret
= v4l2_fh_open(filp
);
1032 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1034 mutex_unlock(&dev
->lock
);
1038 if (dev
->users
== 0) {
1039 au0828_analog_stream_enable(dev
);
1040 au0828_analog_stream_reset(dev
);
1041 dev
->stream_state
= STREAM_OFF
;
1042 set_bit(DEV_INITIALIZED
, &dev
->dev_state
);
1045 mutex_unlock(&dev
->lock
);
1049 static int au0828_v4l2_close(struct file
*filp
)
1052 struct au0828_dev
*dev
= video_drvdata(filp
);
1053 struct video_device
*vdev
= video_devdata(filp
);
1056 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1057 __func__
, dev
->std_set_in_tuner_core
, dev
->dev_state
,
1058 dev
->streaming_users
, dev
->users
);
1060 mutex_lock(&dev
->lock
);
1061 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
&& dev
->vid_timeout_running
) {
1062 /* Cancel timeout thread in case they didn't call streamoff */
1063 dev
->vid_timeout_running
= 0;
1064 del_timer_sync(&dev
->vid_timeout
);
1065 } else if (vdev
->vfl_type
== VFL_TYPE_VBI
&&
1066 dev
->vbi_timeout_running
) {
1067 /* Cancel timeout thread in case they didn't call streamoff */
1068 dev
->vbi_timeout_running
= 0;
1069 del_timer_sync(&dev
->vbi_timeout
);
1072 if (test_bit(DEV_DISCONNECTED
, &dev
->dev_state
))
1075 if (dev
->users
== 1) {
1077 * Avoid putting tuner in sleep if DVB or ALSA are
1080 * On most USB devices like au0828 the tuner can
1081 * be safely put in sleep stare here if ALSA isn't
1082 * streaming. Exceptions are some very old USB tuner
1083 * models such as em28xx-based WinTV USB2 which have
1084 * a separate audio output jack. The devices that have
1085 * a separate audio output jack have analog tuners,
1086 * like Philips FM1236. Those devices are always on,
1087 * so the s_power callback are silently ignored.
1088 * So, the current logic here does the following:
1089 * Disable (put tuner to sleep) when
1090 * - ALSA and DVB aren't not streaming;
1091 * - the last V4L2 file handler is closed.
1095 * Additionally, this logic could be improved to
1096 * disable the media source if the above conditions
1097 * are met and if the device:
1098 * - doesn't have a separate audio out plug (or
1099 * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1101 * Once this additional logic is in place, a callback
1102 * is needed to enable the media source and power on
1103 * the tuner, for radio to work.
1105 ret
= v4l_enable_media_source(vdev
);
1107 v4l2_device_call_all(&dev
->v4l2_dev
, 0, core
,
1109 dev
->std_set_in_tuner_core
= 0;
1111 /* When close the device, set the usb intf0 into alt0 to free
1113 ret
= usb_set_interface(dev
->usbdev
, 0, 0);
1115 pr_info("Au0828 can't set alternate to 0!\n");
1118 _vb2_fop_release(filp
, NULL
);
1120 mutex_unlock(&dev
->lock
);
1124 /* Must be called with dev->lock held */
1125 static void au0828_init_tuner(struct au0828_dev
*dev
)
1127 struct v4l2_frequency f
= {
1128 .frequency
= dev
->ctrl_freq
,
1129 .type
= V4L2_TUNER_ANALOG_TV
,
1132 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1133 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1135 if (dev
->std_set_in_tuner_core
)
1137 dev
->std_set_in_tuner_core
= 1;
1138 i2c_gate_ctrl(dev
, 1);
1139 /* If we've never sent the standard in tuner core, do so now.
1140 We don't do this at device probe because we don't want to
1141 incur the cost of a firmware load */
1142 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_std
, dev
->std
);
1143 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, &f
);
1144 i2c_gate_ctrl(dev
, 0);
1147 static int au0828_set_format(struct au0828_dev
*dev
, unsigned int cmd
,
1148 struct v4l2_format
*format
)
1151 int width
= format
->fmt
.pix
.width
;
1152 int height
= format
->fmt
.pix
.height
;
1154 /* If they are demanding a format other than the one we support,
1155 bail out (tvtime asks for UYVY and then retries with YUYV) */
1156 if (format
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_UYVY
)
1159 /* format->fmt.pix.width only support 720 and height 480 */
1165 format
->fmt
.pix
.width
= width
;
1166 format
->fmt
.pix
.height
= height
;
1167 format
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1168 format
->fmt
.pix
.bytesperline
= width
* 2;
1169 format
->fmt
.pix
.sizeimage
= width
* height
* 2;
1170 format
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1171 format
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1172 format
->fmt
.pix
.priv
= 0;
1174 if (cmd
== VIDIOC_TRY_FMT
)
1177 /* maybe set new image format, driver current only support 720*480 */
1179 dev
->height
= height
;
1180 dev
->frame_size
= width
* height
* 2;
1181 dev
->field_size
= width
* height
;
1182 dev
->bytesperline
= width
* 2;
1184 if (dev
->stream_state
== STREAM_ON
) {
1185 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1186 ret
= au0828_stream_interrupt(dev
);
1188 dprintk(1, "error interrupting video stream!\n");
1193 au0828_analog_stream_enable(dev
);
1198 static int vidioc_querycap(struct file
*file
, void *priv
,
1199 struct v4l2_capability
*cap
)
1201 struct video_device
*vdev
= video_devdata(file
);
1202 struct au0828_dev
*dev
= video_drvdata(file
);
1204 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1205 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1207 strlcpy(cap
->driver
, "au0828", sizeof(cap
->driver
));
1208 strlcpy(cap
->card
, dev
->board
.name
, sizeof(cap
->card
));
1209 usb_make_path(dev
->usbdev
, cap
->bus_info
, sizeof(cap
->bus_info
));
1211 /* set the device capabilities */
1212 cap
->device_caps
= V4L2_CAP_AUDIO
|
1213 V4L2_CAP_READWRITE
|
1214 V4L2_CAP_STREAMING
|
1216 if (vdev
->vfl_type
== VFL_TYPE_GRABBER
)
1217 cap
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
;
1219 cap
->device_caps
|= V4L2_CAP_VBI_CAPTURE
;
1220 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
|
1221 V4L2_CAP_VBI_CAPTURE
| V4L2_CAP_VIDEO_CAPTURE
;
1225 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1226 struct v4l2_fmtdesc
*f
)
1231 dprintk(1, "%s called\n", __func__
);
1233 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1234 strcpy(f
->description
, "Packed YUV2");
1237 f
->pixelformat
= V4L2_PIX_FMT_UYVY
;
1242 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1243 struct v4l2_format
*f
)
1245 struct au0828_dev
*dev
= video_drvdata(file
);
1247 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1248 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1250 f
->fmt
.pix
.width
= dev
->width
;
1251 f
->fmt
.pix
.height
= dev
->height
;
1252 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_UYVY
;
1253 f
->fmt
.pix
.bytesperline
= dev
->bytesperline
;
1254 f
->fmt
.pix
.sizeimage
= dev
->frame_size
;
1255 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
; /* NTSC/PAL */
1256 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1257 f
->fmt
.pix
.priv
= 0;
1261 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1262 struct v4l2_format
*f
)
1264 struct au0828_dev
*dev
= video_drvdata(file
);
1266 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1267 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1269 return au0828_set_format(dev
, VIDIOC_TRY_FMT
, f
);
1272 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1273 struct v4l2_format
*f
)
1275 struct au0828_dev
*dev
= video_drvdata(file
);
1278 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1279 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1281 rc
= check_dev(dev
);
1285 if (vb2_is_busy(&dev
->vb_vidq
)) {
1286 pr_info("%s queue busy\n", __func__
);
1291 rc
= au0828_set_format(dev
, VIDIOC_S_FMT
, f
);
1296 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id norm
)
1298 struct au0828_dev
*dev
= video_drvdata(file
);
1300 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1301 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1303 if (norm
== dev
->std
)
1306 if (dev
->streaming_users
> 0)
1311 au0828_init_tuner(dev
);
1313 i2c_gate_ctrl(dev
, 1);
1316 * FIXME: when we support something other than 60Hz standards,
1317 * we are going to have to make the au0828 bridge adjust the size
1318 * of its capture buffer, which is currently hardcoded at 720x480
1321 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_std
, norm
);
1323 i2c_gate_ctrl(dev
, 0);
1328 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*norm
)
1330 struct au0828_dev
*dev
= video_drvdata(file
);
1332 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1333 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1339 static int vidioc_enum_input(struct file
*file
, void *priv
,
1340 struct v4l2_input
*input
)
1342 struct au0828_dev
*dev
= video_drvdata(file
);
1345 static const char *inames
[] = {
1346 [AU0828_VMUX_UNDEFINED
] = "Undefined",
1347 [AU0828_VMUX_COMPOSITE
] = "Composite",
1348 [AU0828_VMUX_SVIDEO
] = "S-Video",
1349 [AU0828_VMUX_CABLE
] = "Cable TV",
1350 [AU0828_VMUX_TELEVISION
] = "Television",
1351 [AU0828_VMUX_DVB
] = "DVB",
1354 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1355 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1359 if (tmp
>= AU0828_MAX_INPUT
)
1361 if (AUVI_INPUT(tmp
).type
== 0)
1365 strcpy(input
->name
, inames
[AUVI_INPUT(tmp
).type
]);
1366 if ((AUVI_INPUT(tmp
).type
== AU0828_VMUX_TELEVISION
) ||
1367 (AUVI_INPUT(tmp
).type
== AU0828_VMUX_CABLE
)) {
1368 input
->type
|= V4L2_INPUT_TYPE_TUNER
;
1369 input
->audioset
= 1;
1371 input
->type
|= V4L2_INPUT_TYPE_CAMERA
;
1372 input
->audioset
= 2;
1375 input
->std
= dev
->vdev
.tvnorms
;
1380 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1382 struct au0828_dev
*dev
= video_drvdata(file
);
1384 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1385 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1387 *i
= dev
->ctrl_input
;
1391 static void au0828_s_input(struct au0828_dev
*dev
, int index
)
1395 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1396 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1398 switch (AUVI_INPUT(index
).type
) {
1399 case AU0828_VMUX_SVIDEO
:
1400 dev
->input_type
= AU0828_VMUX_SVIDEO
;
1401 dev
->ctrl_ainput
= 1;
1403 case AU0828_VMUX_COMPOSITE
:
1404 dev
->input_type
= AU0828_VMUX_COMPOSITE
;
1405 dev
->ctrl_ainput
= 1;
1407 case AU0828_VMUX_TELEVISION
:
1408 dev
->input_type
= AU0828_VMUX_TELEVISION
;
1409 dev
->ctrl_ainput
= 0;
1412 dprintk(1, "unknown input type set [%d]\n",
1413 AUVI_INPUT(index
).type
);
1417 dev
->ctrl_input
= index
;
1419 v4l2_device_call_all(&dev
->v4l2_dev
, 0, video
, s_routing
,
1420 AUVI_INPUT(index
).vmux
, 0, 0);
1422 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1424 if (AUVI_INPUT(i
).audio_setup
== NULL
)
1432 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1434 /* Make sure we leave it turned on if some
1435 other input is routed to this callback */
1436 if ((AUVI_INPUT(i
).audio_setup
) !=
1437 ((AUVI_INPUT(index
).audio_setup
))) {
1438 (AUVI_INPUT(i
).audio_setup
)(dev
, enable
);
1443 v4l2_device_call_all(&dev
->v4l2_dev
, 0, audio
, s_routing
,
1444 AUVI_INPUT(index
).amux
, 0, 0);
1447 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int index
)
1449 struct au0828_dev
*dev
= video_drvdata(file
);
1450 struct video_device
*vfd
= video_devdata(file
);
1452 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__
,
1454 if (index
>= AU0828_MAX_INPUT
)
1456 if (AUVI_INPUT(index
).type
== 0)
1459 if (dev
->ctrl_input
== index
)
1462 au0828_s_input(dev
, index
);
1465 * Input has been changed. Disable the media source
1466 * associated with the old input and enable source
1467 * for the newly set input
1469 v4l_disable_media_source(vfd
);
1470 return v4l_enable_media_source(vfd
);
1473 static int vidioc_enumaudio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1478 dprintk(1, "%s called\n", __func__
);
1481 strcpy(a
->name
, "Television");
1483 strcpy(a
->name
, "Line in");
1485 a
->capability
= V4L2_AUDCAP_STEREO
;
1489 static int vidioc_g_audio(struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1491 struct au0828_dev
*dev
= video_drvdata(file
);
1493 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1494 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1496 a
->index
= dev
->ctrl_ainput
;
1498 strcpy(a
->name
, "Television");
1500 strcpy(a
->name
, "Line in");
1502 a
->capability
= V4L2_AUDCAP_STEREO
;
1506 static int vidioc_s_audio(struct file
*file
, void *priv
, const struct v4l2_audio
*a
)
1508 struct au0828_dev
*dev
= video_drvdata(file
);
1510 if (a
->index
!= dev
->ctrl_ainput
)
1513 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1514 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1518 static int vidioc_g_tuner(struct file
*file
, void *priv
, struct v4l2_tuner
*t
)
1520 struct au0828_dev
*dev
= video_drvdata(file
);
1521 struct video_device
*vfd
= video_devdata(file
);
1527 ret
= v4l_enable_media_source(vfd
);
1531 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1532 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1534 strcpy(t
->name
, "Auvitek tuner");
1536 au0828_init_tuner(dev
);
1537 i2c_gate_ctrl(dev
, 1);
1538 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_tuner
, t
);
1539 i2c_gate_ctrl(dev
, 0);
1543 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1544 const struct v4l2_tuner
*t
)
1546 struct au0828_dev
*dev
= video_drvdata(file
);
1551 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1552 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1554 au0828_init_tuner(dev
);
1555 i2c_gate_ctrl(dev
, 1);
1556 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_tuner
, t
);
1557 i2c_gate_ctrl(dev
, 0);
1559 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t
->signal
,
1566 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1567 struct v4l2_frequency
*freq
)
1569 struct au0828_dev
*dev
= video_drvdata(file
);
1571 if (freq
->tuner
!= 0)
1573 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1574 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1575 freq
->frequency
= dev
->ctrl_freq
;
1579 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1580 const struct v4l2_frequency
*freq
)
1582 struct au0828_dev
*dev
= video_drvdata(file
);
1583 struct v4l2_frequency new_freq
= *freq
;
1585 if (freq
->tuner
!= 0)
1588 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1589 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1591 au0828_init_tuner(dev
);
1592 i2c_gate_ctrl(dev
, 1);
1594 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, s_frequency
, freq
);
1595 /* Get the actual set (and possibly clamped) frequency */
1596 v4l2_device_call_all(&dev
->v4l2_dev
, 0, tuner
, g_frequency
, &new_freq
);
1597 dev
->ctrl_freq
= new_freq
.frequency
;
1599 i2c_gate_ctrl(dev
, 0);
1601 au0828_analog_stream_reset(dev
);
1607 /* RAW VBI ioctls */
1609 static int vidioc_g_fmt_vbi_cap(struct file
*file
, void *priv
,
1610 struct v4l2_format
*format
)
1612 struct au0828_dev
*dev
= video_drvdata(file
);
1614 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1615 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1617 format
->fmt
.vbi
.samples_per_line
= dev
->vbi_width
;
1618 format
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1619 format
->fmt
.vbi
.offset
= 0;
1620 format
->fmt
.vbi
.flags
= 0;
1621 format
->fmt
.vbi
.sampling_rate
= 6750000 * 4 / 2;
1623 format
->fmt
.vbi
.count
[0] = dev
->vbi_height
;
1624 format
->fmt
.vbi
.count
[1] = dev
->vbi_height
;
1625 format
->fmt
.vbi
.start
[0] = 21;
1626 format
->fmt
.vbi
.start
[1] = 284;
1627 memset(format
->fmt
.vbi
.reserved
, 0, sizeof(format
->fmt
.vbi
.reserved
));
1632 static int vidioc_cropcap(struct file
*file
, void *priv
,
1633 struct v4l2_cropcap
*cc
)
1635 struct au0828_dev
*dev
= video_drvdata(file
);
1637 if (cc
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1640 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1641 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1643 cc
->bounds
.left
= 0;
1645 cc
->bounds
.width
= dev
->width
;
1646 cc
->bounds
.height
= dev
->height
;
1648 cc
->defrect
= cc
->bounds
;
1650 cc
->pixelaspect
.numerator
= 54;
1651 cc
->pixelaspect
.denominator
= 59;
1656 #ifdef CONFIG_VIDEO_ADV_DEBUG
1657 static int vidioc_g_register(struct file
*file
, void *priv
,
1658 struct v4l2_dbg_register
*reg
)
1660 struct au0828_dev
*dev
= video_drvdata(file
);
1662 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1663 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1665 reg
->val
= au0828_read(dev
, reg
->reg
);
1670 static int vidioc_s_register(struct file
*file
, void *priv
,
1671 const struct v4l2_dbg_register
*reg
)
1673 struct au0828_dev
*dev
= video_drvdata(file
);
1675 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__
,
1676 dev
->std_set_in_tuner_core
, dev
->dev_state
);
1678 return au0828_writereg(dev
, reg
->reg
, reg
->val
);
1682 static int vidioc_log_status(struct file
*file
, void *fh
)
1684 struct video_device
*vdev
= video_devdata(file
);
1686 dprintk(1, "%s called\n", __func__
);
1688 v4l2_ctrl_log_status(file
, fh
);
1689 v4l2_device_call_all(vdev
->v4l2_dev
, 0, core
, log_status
);
1693 void au0828_v4l2_suspend(struct au0828_dev
*dev
)
1698 pr_info("stopping V4L2\n");
1700 if (dev
->stream_state
== STREAM_ON
) {
1701 pr_info("stopping V4L2 active URBs\n");
1702 au0828_analog_stream_disable(dev
);
1704 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
1705 urb
= dev
->isoc_ctl
.urb
[i
];
1707 if (!irqs_disabled())
1710 usb_unlink_urb(urb
);
1715 if (dev
->vid_timeout_running
)
1716 del_timer_sync(&dev
->vid_timeout
);
1717 if (dev
->vbi_timeout_running
)
1718 del_timer_sync(&dev
->vbi_timeout
);
1721 void au0828_v4l2_resume(struct au0828_dev
*dev
)
1725 pr_info("restarting V4L2\n");
1727 if (dev
->stream_state
== STREAM_ON
) {
1728 au0828_stream_interrupt(dev
);
1729 au0828_init_tuner(dev
);
1732 if (dev
->vid_timeout_running
)
1733 mod_timer(&dev
->vid_timeout
, jiffies
+ (HZ
/ 10));
1734 if (dev
->vbi_timeout_running
)
1735 mod_timer(&dev
->vbi_timeout
, jiffies
+ (HZ
/ 10));
1737 /* If we were doing ac97 instead of i2s, it would go here...*/
1738 au0828_i2s_init(dev
);
1740 au0828_analog_stream_enable(dev
);
1742 if (!(dev
->stream_state
== STREAM_ON
)) {
1743 au0828_analog_stream_reset(dev
);
1745 for (i
= 0; i
< dev
->isoc_ctl
.num_bufs
; i
++) {
1746 rc
= usb_submit_urb(dev
->isoc_ctl
.urb
[i
], GFP_ATOMIC
);
1748 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1750 au0828_uninit_isoc(dev
);
1756 static struct v4l2_file_operations au0828_v4l_fops
= {
1757 .owner
= THIS_MODULE
,
1758 .open
= au0828_v4l2_open
,
1759 .release
= au0828_v4l2_close
,
1760 .read
= vb2_fop_read
,
1761 .poll
= vb2_fop_poll
,
1762 .mmap
= vb2_fop_mmap
,
1763 .unlocked_ioctl
= video_ioctl2
,
1766 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1767 .vidioc_querycap
= vidioc_querycap
,
1768 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1769 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1770 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1771 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1772 .vidioc_g_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1773 .vidioc_try_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1774 .vidioc_s_fmt_vbi_cap
= vidioc_g_fmt_vbi_cap
,
1775 .vidioc_enumaudio
= vidioc_enumaudio
,
1776 .vidioc_g_audio
= vidioc_g_audio
,
1777 .vidioc_s_audio
= vidioc_s_audio
,
1778 .vidioc_cropcap
= vidioc_cropcap
,
1780 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1781 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
1782 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1783 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1784 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1785 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1786 .vidioc_expbuf
= vb2_ioctl_expbuf
,
1788 .vidioc_s_std
= vidioc_s_std
,
1789 .vidioc_g_std
= vidioc_g_std
,
1790 .vidioc_enum_input
= vidioc_enum_input
,
1791 .vidioc_g_input
= vidioc_g_input
,
1792 .vidioc_s_input
= vidioc_s_input
,
1794 .vidioc_streamon
= vb2_ioctl_streamon
,
1795 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1797 .vidioc_g_tuner
= vidioc_g_tuner
,
1798 .vidioc_s_tuner
= vidioc_s_tuner
,
1799 .vidioc_g_frequency
= vidioc_g_frequency
,
1800 .vidioc_s_frequency
= vidioc_s_frequency
,
1801 #ifdef CONFIG_VIDEO_ADV_DEBUG
1802 .vidioc_g_register
= vidioc_g_register
,
1803 .vidioc_s_register
= vidioc_s_register
,
1805 .vidioc_log_status
= vidioc_log_status
,
1806 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1807 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1810 static const struct video_device au0828_video_template
= {
1811 .fops
= &au0828_v4l_fops
,
1812 .release
= video_device_release_empty
,
1813 .ioctl_ops
= &video_ioctl_ops
,
1814 .tvnorms
= V4L2_STD_NTSC_M
| V4L2_STD_PAL_M
,
1817 static int au0828_vb2_setup(struct au0828_dev
*dev
)
1820 struct vb2_queue
*q
;
1822 /* Setup Videobuf2 for Video capture */
1824 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1825 q
->io_modes
= VB2_READ
| VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
1826 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1828 q
->buf_struct_size
= sizeof(struct au0828_buffer
);
1829 q
->ops
= &au0828_video_qops
;
1830 q
->mem_ops
= &vb2_vmalloc_memops
;
1832 rc
= vb2_queue_init(q
);
1836 /* Setup Videobuf2 for VBI capture */
1838 q
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1839 q
->io_modes
= VB2_READ
| VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
;
1840 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1842 q
->buf_struct_size
= sizeof(struct au0828_buffer
);
1843 q
->ops
= &au0828_vbi_qops
;
1844 q
->mem_ops
= &vb2_vmalloc_memops
;
1846 rc
= vb2_queue_init(q
);
1853 static void au0828_analog_create_entities(struct au0828_dev
*dev
)
1855 #if defined(CONFIG_MEDIA_CONTROLLER)
1856 static const char * const inames
[] = {
1857 [AU0828_VMUX_COMPOSITE
] = "Composite",
1858 [AU0828_VMUX_SVIDEO
] = "S-Video",
1859 [AU0828_VMUX_CABLE
] = "Cable TV",
1860 [AU0828_VMUX_TELEVISION
] = "Television",
1861 [AU0828_VMUX_DVB
] = "DVB",
1865 /* Initialize Video and VBI pads */
1866 dev
->video_pad
.flags
= MEDIA_PAD_FL_SINK
;
1867 ret
= media_entity_pads_init(&dev
->vdev
.entity
, 1, &dev
->video_pad
);
1869 pr_err("failed to initialize video media entity!\n");
1871 dev
->vbi_pad
.flags
= MEDIA_PAD_FL_SINK
;
1872 ret
= media_entity_pads_init(&dev
->vbi_dev
.entity
, 1, &dev
->vbi_pad
);
1874 pr_err("failed to initialize vbi media entity!\n");
1876 /* Create entities for each input connector */
1877 for (i
= 0; i
< AU0828_MAX_INPUT
; i
++) {
1878 struct media_entity
*ent
= &dev
->input_ent
[i
];
1880 if (AUVI_INPUT(i
).type
== AU0828_VMUX_UNDEFINED
)
1883 ent
->name
= inames
[AUVI_INPUT(i
).type
];
1884 ent
->flags
= MEDIA_ENT_FL_CONNECTOR
;
1885 dev
->input_pad
[i
].flags
= MEDIA_PAD_FL_SOURCE
;
1887 switch (AUVI_INPUT(i
).type
) {
1888 case AU0828_VMUX_COMPOSITE
:
1889 ent
->function
= MEDIA_ENT_F_CONN_COMPOSITE
;
1891 case AU0828_VMUX_SVIDEO
:
1892 ent
->function
= MEDIA_ENT_F_CONN_SVIDEO
;
1894 case AU0828_VMUX_CABLE
:
1895 case AU0828_VMUX_TELEVISION
:
1896 case AU0828_VMUX_DVB
:
1897 default: /* Just to shut up a warning */
1898 ent
->function
= MEDIA_ENT_F_CONN_RF
;
1902 ret
= media_entity_pads_init(ent
, 1, &dev
->input_pad
[i
]);
1904 pr_err("failed to initialize input pad[%d]!\n", i
);
1906 ret
= media_device_register_entity(dev
->media_dev
, ent
);
1908 pr_err("failed to register input entity %d!\n", i
);
1913 /**************************************************************************/
1915 int au0828_analog_register(struct au0828_dev
*dev
,
1916 struct usb_interface
*interface
)
1918 int retval
= -ENOMEM
;
1919 struct usb_host_interface
*iface_desc
;
1920 struct usb_endpoint_descriptor
*endpoint
;
1923 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1924 interface
->cur_altsetting
->desc
.bInterfaceNumber
);
1927 if (AUVI_INPUT(0).type
== AU0828_VMUX_UNDEFINED
)
1930 /* set au0828 usb interface0 to as5 */
1931 retval
= usb_set_interface(dev
->usbdev
,
1932 interface
->cur_altsetting
->desc
.bInterfaceNumber
, 5);
1934 pr_info("Failure setting usb interface0 to as5\n");
1938 /* Figure out which endpoint has the isoc interface */
1939 iface_desc
= interface
->cur_altsetting
;
1940 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; i
++) {
1941 endpoint
= &iface_desc
->endpoint
[i
].desc
;
1942 if (((endpoint
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
)
1944 ((endpoint
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
1945 == USB_ENDPOINT_XFER_ISOC
)) {
1947 /* we find our isoc in endpoint */
1948 u16 tmp
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1949 dev
->max_pkt_size
= (tmp
& 0x07ff) *
1950 (((tmp
& 0x1800) >> 11) + 1);
1951 dev
->isoc_in_endpointaddr
= endpoint
->bEndpointAddress
;
1953 "Found isoc endpoint 0x%02x, max size = %d\n",
1954 dev
->isoc_in_endpointaddr
, dev
->max_pkt_size
);
1957 if (!(dev
->isoc_in_endpointaddr
)) {
1958 pr_info("Could not locate isoc endpoint\n");
1962 init_waitqueue_head(&dev
->open
);
1963 spin_lock_init(&dev
->slock
);
1965 /* init video dma queues */
1966 INIT_LIST_HEAD(&dev
->vidq
.active
);
1967 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1969 setup_timer(&dev
->vid_timeout
, au0828_vid_buffer_timeout
,
1970 (unsigned long)dev
);
1971 setup_timer(&dev
->vbi_timeout
, au0828_vbi_buffer_timeout
,
1972 (unsigned long)dev
);
1974 dev
->width
= NTSC_STD_W
;
1975 dev
->height
= NTSC_STD_H
;
1976 dev
->field_size
= dev
->width
* dev
->height
;
1977 dev
->frame_size
= dev
->field_size
<< 1;
1978 dev
->bytesperline
= dev
->width
<< 1;
1979 dev
->vbi_width
= 720;
1980 dev
->vbi_height
= 1;
1981 dev
->ctrl_ainput
= 0;
1982 dev
->ctrl_freq
= 960;
1983 dev
->std
= V4L2_STD_NTSC_M
;
1984 /* Default input is TV Tuner */
1985 au0828_s_input(dev
, 0);
1987 mutex_init(&dev
->vb_queue_lock
);
1988 mutex_init(&dev
->vb_vbi_queue_lock
);
1990 /* Fill the video capture device struct */
1991 dev
->vdev
= au0828_video_template
;
1992 dev
->vdev
.v4l2_dev
= &dev
->v4l2_dev
;
1993 dev
->vdev
.lock
= &dev
->lock
;
1994 dev
->vdev
.queue
= &dev
->vb_vidq
;
1995 dev
->vdev
.queue
->lock
= &dev
->vb_queue_lock
;
1996 strcpy(dev
->vdev
.name
, "au0828a video");
1998 /* Setup the VBI device */
1999 dev
->vbi_dev
= au0828_video_template
;
2000 dev
->vbi_dev
.v4l2_dev
= &dev
->v4l2_dev
;
2001 dev
->vbi_dev
.lock
= &dev
->lock
;
2002 dev
->vbi_dev
.queue
= &dev
->vb_vbiq
;
2003 dev
->vbi_dev
.queue
->lock
= &dev
->vb_vbi_queue_lock
;
2004 strcpy(dev
->vbi_dev
.name
, "au0828a vbi");
2006 /* Init entities at the Media Controller */
2007 au0828_analog_create_entities(dev
);
2009 /* initialize videobuf2 stuff */
2010 retval
= au0828_vb2_setup(dev
);
2012 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2017 /* Register the v4l2 device */
2018 video_set_drvdata(&dev
->vdev
, dev
);
2019 retval
= video_register_device(&dev
->vdev
, VFL_TYPE_GRABBER
, -1);
2021 dprintk(1, "unable to register video device (error = %d).\n",
2027 /* Register the vbi device */
2028 video_set_drvdata(&dev
->vbi_dev
, dev
);
2029 retval
= video_register_device(&dev
->vbi_dev
, VFL_TYPE_VBI
, -1);
2031 dprintk(1, "unable to register vbi device (error = %d).\n",
2034 goto err_reg_vbi_dev
;
2037 #ifdef CONFIG_MEDIA_CONTROLLER
2038 retval
= v4l2_mc_create_media_graph(dev
->media_dev
);
2040 pr_err("%s() au0282_dev_register failed to create graph\n",
2043 goto err_reg_vbi_dev
;
2047 dprintk(1, "%s completed!\n", __func__
);
2052 video_unregister_device(&dev
->vdev
);
2054 vb2_queue_release(&dev
->vb_vidq
);
2055 vb2_queue_release(&dev
->vb_vbiq
);