WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / usb / au0828 / au0828-video.c
blobc0f118563c7d13b44f8cedc963c0b83cbcbf4a37
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Auvitek AU0828 USB Bridge (Analog video support)
5 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
6 * Copyright (C) 2005-2008 Auvitek International, Ltd.
7 */
9 /* Developer Notes:
11 * The hardware scaler supported is unimplemented
12 * AC97 audio support is unimplemented (only i2s audio mode)
16 #include "au0828.h"
17 #include "au8522.h"
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
22 #include <linux/device.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-mc.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-event.h>
27 #include <media/tuner.h>
28 #include "au0828-reg.h"
30 static DEFINE_MUTEX(au0828_sysfs_lock);
32 /* ------------------------------------------------------------------
33 Videobuf operations
34 ------------------------------------------------------------------*/
36 static unsigned int isoc_debug;
37 module_param(isoc_debug, int, 0644);
38 MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
40 #define au0828_isocdbg(fmt, arg...) \
41 do {\
42 if (isoc_debug) { \
43 pr_info("au0828 %s :"fmt, \
44 __func__ , ##arg); \
45 } \
46 } while (0)
48 static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
50 if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
51 dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
54 static inline void print_err_status(struct au0828_dev *dev,
55 int packet, int status)
57 char *errmsg = "Unknown";
59 switch (status) {
60 case -ENOENT:
61 errmsg = "unlinked synchronously";
62 break;
63 case -ECONNRESET:
64 errmsg = "unlinked asynchronously";
65 break;
66 case -ENOSR:
67 errmsg = "Buffer error (overrun)";
68 break;
69 case -EPIPE:
70 errmsg = "Stalled (device not responding)";
71 break;
72 case -EOVERFLOW:
73 errmsg = "Babble (bad cable?)";
74 break;
75 case -EPROTO:
76 errmsg = "Bit-stuff error (bad cable?)";
77 break;
78 case -EILSEQ:
79 errmsg = "CRC/Timeout (could be anything)";
80 break;
81 case -ETIME:
82 errmsg = "Device does not respond";
83 break;
85 if (packet < 0) {
86 au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
87 } else {
88 au0828_isocdbg("URB packet %d, status %d [%s].\n",
89 packet, status, errmsg);
93 static int check_dev(struct au0828_dev *dev)
95 if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) {
96 pr_info("v4l2 ioctl: device not present\n");
97 return -ENODEV;
100 if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) {
101 pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n");
102 return -EIO;
104 return 0;
108 * IRQ callback, called by URB callback
110 static void au0828_irq_callback(struct urb *urb)
112 struct au0828_dmaqueue *dma_q = urb->context;
113 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
114 unsigned long flags = 0;
115 int i;
117 switch (urb->status) {
118 case 0: /* success */
119 case -ETIMEDOUT: /* NAK */
120 break;
121 case -ECONNRESET: /* kill */
122 case -ENOENT:
123 case -ESHUTDOWN:
124 au0828_isocdbg("au0828_irq_callback called: status kill\n");
125 return;
126 default: /* unknown error */
127 au0828_isocdbg("urb completion error %d.\n", urb->status);
128 break;
131 /* Copy data from URB */
132 spin_lock_irqsave(&dev->slock, flags);
133 dev->isoc_ctl.isoc_copy(dev, urb);
134 spin_unlock_irqrestore(&dev->slock, flags);
136 /* Reset urb buffers */
137 for (i = 0; i < urb->number_of_packets; i++) {
138 urb->iso_frame_desc[i].status = 0;
139 urb->iso_frame_desc[i].actual_length = 0;
141 urb->status = 0;
143 urb->status = usb_submit_urb(urb, GFP_ATOMIC);
144 if (urb->status) {
145 au0828_isocdbg("urb resubmit failed (error=%i)\n",
146 urb->status);
148 dev->stream_state = STREAM_ON;
152 * Stop and Deallocate URBs
154 static void au0828_uninit_isoc(struct au0828_dev *dev)
156 struct urb *urb;
157 int i;
159 au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
161 dev->isoc_ctl.nfields = -1;
162 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
163 urb = dev->isoc_ctl.urb[i];
164 if (urb) {
165 if (!irqs_disabled())
166 usb_kill_urb(urb);
167 else
168 usb_unlink_urb(urb);
170 if (dev->isoc_ctl.transfer_buffer[i]) {
171 usb_free_coherent(dev->usbdev,
172 urb->transfer_buffer_length,
173 dev->isoc_ctl.transfer_buffer[i],
174 urb->transfer_dma);
176 usb_free_urb(urb);
177 dev->isoc_ctl.urb[i] = NULL;
179 dev->isoc_ctl.transfer_buffer[i] = NULL;
182 kfree(dev->isoc_ctl.urb);
183 kfree(dev->isoc_ctl.transfer_buffer);
185 dev->isoc_ctl.urb = NULL;
186 dev->isoc_ctl.transfer_buffer = NULL;
187 dev->isoc_ctl.num_bufs = 0;
189 dev->stream_state = STREAM_OFF;
193 * Allocate URBs and start IRQ
195 static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
196 int num_bufs, int max_pkt_size,
197 int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
199 struct au0828_dmaqueue *dma_q = &dev->vidq;
200 int i;
201 int sb_size, pipe;
202 struct urb *urb;
203 int j, k;
204 int rc;
206 au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
208 dev->isoc_ctl.isoc_copy = isoc_copy;
209 dev->isoc_ctl.num_bufs = num_bufs;
211 dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
212 if (!dev->isoc_ctl.urb) {
213 au0828_isocdbg("cannot alloc memory for usb buffers\n");
214 return -ENOMEM;
217 dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs, sizeof(void *),
218 GFP_KERNEL);
219 if (!dev->isoc_ctl.transfer_buffer) {
220 au0828_isocdbg("cannot allocate memory for usb transfer\n");
221 kfree(dev->isoc_ctl.urb);
222 return -ENOMEM;
225 dev->isoc_ctl.max_pkt_size = max_pkt_size;
226 dev->isoc_ctl.buf = NULL;
228 sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
230 /* allocate urbs and transfer buffers */
231 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
232 urb = usb_alloc_urb(max_packets, GFP_KERNEL);
233 if (!urb) {
234 au0828_isocdbg("cannot allocate URB\n");
235 au0828_uninit_isoc(dev);
236 return -ENOMEM;
238 dev->isoc_ctl.urb[i] = urb;
240 dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
241 sb_size, GFP_KERNEL, &urb->transfer_dma);
242 if (!dev->isoc_ctl.transfer_buffer[i]) {
243 au0828_isocdbg("cannot allocate transfer buffer\n");
244 au0828_uninit_isoc(dev);
245 return -ENOMEM;
247 memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
249 pipe = usb_rcvisocpipe(dev->usbdev,
250 dev->isoc_in_endpointaddr);
252 usb_fill_int_urb(urb, dev->usbdev, pipe,
253 dev->isoc_ctl.transfer_buffer[i], sb_size,
254 au0828_irq_callback, dma_q, 1);
256 urb->number_of_packets = max_packets;
257 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
259 k = 0;
260 for (j = 0; j < max_packets; j++) {
261 urb->iso_frame_desc[j].offset = k;
262 urb->iso_frame_desc[j].length =
263 dev->isoc_ctl.max_pkt_size;
264 k += dev->isoc_ctl.max_pkt_size;
268 /* submit urbs and enables IRQ */
269 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
270 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
271 if (rc) {
272 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
273 i, rc);
274 au0828_uninit_isoc(dev);
275 return rc;
279 return 0;
283 * Announces that a buffer were filled and request the next
285 static inline void buffer_filled(struct au0828_dev *dev,
286 struct au0828_dmaqueue *dma_q,
287 struct au0828_buffer *buf)
289 struct vb2_v4l2_buffer *vb = &buf->vb;
290 struct vb2_queue *q = vb->vb2_buf.vb2_queue;
292 /* Advice that buffer was filled */
293 au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
295 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
296 vb->sequence = dev->frame_count++;
297 else
298 vb->sequence = dev->vbi_frame_count++;
300 vb->field = V4L2_FIELD_INTERLACED;
301 vb->vb2_buf.timestamp = ktime_get_ns();
302 vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
306 * Identify the buffer header type and properly handles
308 static void au0828_copy_video(struct au0828_dev *dev,
309 struct au0828_dmaqueue *dma_q,
310 struct au0828_buffer *buf,
311 unsigned char *p,
312 unsigned char *outp, unsigned long len)
314 void *fieldstart, *startwrite, *startread;
315 int linesdone, currlinedone, offset, lencopy, remain;
316 int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
318 if (len == 0)
319 return;
321 if (dma_q->pos + len > buf->length)
322 len = buf->length - dma_q->pos;
324 startread = p;
325 remain = len;
327 /* Interlaces frame */
328 if (buf->top_field)
329 fieldstart = outp;
330 else
331 fieldstart = outp + bytesperline;
333 linesdone = dma_q->pos / bytesperline;
334 currlinedone = dma_q->pos % bytesperline;
335 offset = linesdone * bytesperline * 2 + currlinedone;
336 startwrite = fieldstart + offset;
337 lencopy = bytesperline - currlinedone;
338 lencopy = lencopy > remain ? remain : lencopy;
340 if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
341 au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
342 ((char *)startwrite + lencopy) -
343 ((char *)outp + buf->length));
344 remain = (char *)outp + buf->length - (char *)startwrite;
345 lencopy = remain;
347 if (lencopy <= 0)
348 return;
349 memcpy(startwrite, startread, lencopy);
351 remain -= lencopy;
353 while (remain > 0) {
354 startwrite += lencopy + bytesperline;
355 startread += lencopy;
356 if (bytesperline > remain)
357 lencopy = remain;
358 else
359 lencopy = bytesperline;
361 if ((char *)startwrite + lencopy > (char *)outp +
362 buf->length) {
363 au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
364 ((char *)startwrite + lencopy) -
365 ((char *)outp + buf->length));
366 lencopy = remain = (char *)outp + buf->length -
367 (char *)startwrite;
369 if (lencopy <= 0)
370 break;
372 memcpy(startwrite, startread, lencopy);
374 remain -= lencopy;
377 if (offset > 1440) {
378 /* We have enough data to check for greenscreen */
379 if (outp[0] < 0x60 && outp[1440] < 0x60)
380 dev->greenscreen_detected = 1;
383 dma_q->pos += len;
387 * video-buf generic routine to get the next available buffer
389 static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
390 struct au0828_buffer **buf)
392 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
394 if (list_empty(&dma_q->active)) {
395 au0828_isocdbg("No active queue to serve\n");
396 dev->isoc_ctl.buf = NULL;
397 *buf = NULL;
398 return;
401 /* Get the next buffer */
402 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
403 /* Cleans up buffer - Useful for testing for frame/URB loss */
404 list_del(&(*buf)->list);
405 dma_q->pos = 0;
406 (*buf)->vb_buf = (*buf)->mem;
407 dev->isoc_ctl.buf = *buf;
409 return;
412 static void au0828_copy_vbi(struct au0828_dev *dev,
413 struct au0828_dmaqueue *dma_q,
414 struct au0828_buffer *buf,
415 unsigned char *p,
416 unsigned char *outp, unsigned long len)
418 unsigned char *startwrite, *startread;
419 int bytesperline;
420 int i, j = 0;
422 if (dev == NULL) {
423 au0828_isocdbg("dev is null\n");
424 return;
427 if (dma_q == NULL) {
428 au0828_isocdbg("dma_q is null\n");
429 return;
431 if (buf == NULL)
432 return;
433 if (p == NULL) {
434 au0828_isocdbg("p is null\n");
435 return;
437 if (outp == NULL) {
438 au0828_isocdbg("outp is null\n");
439 return;
442 bytesperline = dev->vbi_width;
444 if (dma_q->pos + len > buf->length)
445 len = buf->length - dma_q->pos;
447 startread = p;
448 startwrite = outp + (dma_q->pos / 2);
450 /* Make sure the bottom field populates the second half of the frame */
451 if (buf->top_field == 0)
452 startwrite += bytesperline * dev->vbi_height;
454 for (i = 0; i < len; i += 2)
455 startwrite[j++] = startread[i+1];
457 dma_q->pos += len;
462 * video-buf generic routine to get the next available VBI buffer
464 static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
465 struct au0828_buffer **buf)
467 struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
469 if (list_empty(&dma_q->active)) {
470 au0828_isocdbg("No active queue to serve\n");
471 dev->isoc_ctl.vbi_buf = NULL;
472 *buf = NULL;
473 return;
476 /* Get the next buffer */
477 *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
478 /* Cleans up buffer - Useful for testing for frame/URB loss */
479 list_del(&(*buf)->list);
480 dma_q->pos = 0;
481 (*buf)->vb_buf = (*buf)->mem;
482 dev->isoc_ctl.vbi_buf = *buf;
483 return;
487 * Controls the isoc copy of each urb packet
489 static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
491 struct au0828_buffer *buf;
492 struct au0828_buffer *vbi_buf;
493 struct au0828_dmaqueue *dma_q = urb->context;
494 struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
495 unsigned char *outp = NULL;
496 unsigned char *vbioutp = NULL;
497 int i, len = 0, rc = 1;
498 unsigned char *p;
499 unsigned char fbyte;
500 unsigned int vbi_field_size;
501 unsigned int remain, lencopy;
503 if (!dev)
504 return 0;
506 if (test_bit(DEV_DISCONNECTED, &dev->dev_state) ||
507 test_bit(DEV_MISCONFIGURED, &dev->dev_state))
508 return 0;
510 if (urb->status < 0) {
511 print_err_status(dev, -1, urb->status);
512 if (urb->status == -ENOENT)
513 return 0;
516 buf = dev->isoc_ctl.buf;
517 if (buf != NULL)
518 outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
520 vbi_buf = dev->isoc_ctl.vbi_buf;
521 if (vbi_buf != NULL)
522 vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
524 for (i = 0; i < urb->number_of_packets; i++) {
525 int status = urb->iso_frame_desc[i].status;
527 if (status < 0) {
528 print_err_status(dev, i, status);
529 if (urb->iso_frame_desc[i].status != -EPROTO)
530 continue;
533 if (urb->iso_frame_desc[i].actual_length <= 0)
534 continue;
536 if (urb->iso_frame_desc[i].actual_length >
537 dev->max_pkt_size) {
538 au0828_isocdbg("packet bigger than packet size");
539 continue;
542 p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
543 fbyte = p[0];
544 len = urb->iso_frame_desc[i].actual_length - 4;
545 p += 4;
547 if (fbyte & 0x80) {
548 len -= 4;
549 p += 4;
550 au0828_isocdbg("Video frame %s\n",
551 (fbyte & 0x40) ? "odd" : "even");
552 if (fbyte & 0x40) {
553 /* VBI */
554 if (vbi_buf != NULL)
555 buffer_filled(dev, vbi_dma_q, vbi_buf);
556 vbi_get_next_buf(vbi_dma_q, &vbi_buf);
557 if (vbi_buf == NULL)
558 vbioutp = NULL;
559 else
560 vbioutp = vb2_plane_vaddr(
561 &vbi_buf->vb.vb2_buf, 0);
563 /* Video */
564 if (buf != NULL)
565 buffer_filled(dev, dma_q, buf);
566 get_next_buf(dma_q, &buf);
567 if (buf == NULL)
568 outp = NULL;
569 else
570 outp = vb2_plane_vaddr(
571 &buf->vb.vb2_buf, 0);
573 /* As long as isoc traffic is arriving, keep
574 resetting the timer */
575 if (dev->vid_timeout_running)
576 mod_timer(&dev->vid_timeout,
577 jiffies + (HZ / 10));
578 if (dev->vbi_timeout_running)
579 mod_timer(&dev->vbi_timeout,
580 jiffies + (HZ / 10));
583 if (buf != NULL) {
584 if (fbyte & 0x40)
585 buf->top_field = 1;
586 else
587 buf->top_field = 0;
590 if (vbi_buf != NULL) {
591 if (fbyte & 0x40)
592 vbi_buf->top_field = 1;
593 else
594 vbi_buf->top_field = 0;
597 dev->vbi_read = 0;
598 vbi_dma_q->pos = 0;
599 dma_q->pos = 0;
602 vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
603 if (dev->vbi_read < vbi_field_size) {
604 remain = vbi_field_size - dev->vbi_read;
605 if (len < remain)
606 lencopy = len;
607 else
608 lencopy = remain;
610 if (vbi_buf != NULL)
611 au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
612 vbioutp, len);
614 len -= lencopy;
615 p += lencopy;
616 dev->vbi_read += lencopy;
619 if (dev->vbi_read >= vbi_field_size && buf != NULL)
620 au0828_copy_video(dev, dma_q, buf, p, outp, len);
622 return rc;
625 void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
627 #ifdef CONFIG_MEDIA_CONTROLLER
628 int i;
630 for (i = 0; i < AU0828_MAX_INPUT; i++) {
631 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
632 return;
633 media_device_unregister_entity(&dev->input_ent[i]);
635 #endif
638 static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
640 struct au0828_dev *dev =
641 container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
643 v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
644 v4l2_device_unregister(&dev->v4l2_dev);
645 au0828_usb_v4l2_media_release(dev);
646 au0828_usb_release(dev);
649 int au0828_v4l2_device_register(struct usb_interface *interface,
650 struct au0828_dev *dev)
652 int retval;
654 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
655 return 0;
657 /* Create the v4l2_device */
658 #ifdef CONFIG_MEDIA_CONTROLLER
659 dev->v4l2_dev.mdev = dev->media_dev;
660 #endif
661 retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
662 if (retval) {
663 pr_err("%s() v4l2_device_register failed\n",
664 __func__);
665 return retval;
668 dev->v4l2_dev.release = au0828_usb_v4l2_release;
670 /* This control handler will inherit the controls from au8522 */
671 retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
672 if (retval) {
673 pr_err("%s() v4l2_ctrl_handler_init failed\n",
674 __func__);
675 return retval;
677 dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
679 return 0;
682 static int queue_setup(struct vb2_queue *vq,
683 unsigned int *nbuffers, unsigned int *nplanes,
684 unsigned int sizes[], struct device *alloc_devs[])
686 struct au0828_dev *dev = vb2_get_drv_priv(vq);
687 unsigned long size = dev->height * dev->bytesperline;
689 if (*nplanes)
690 return sizes[0] < size ? -EINVAL : 0;
691 *nplanes = 1;
692 sizes[0] = size;
693 return 0;
696 static int
697 buffer_prepare(struct vb2_buffer *vb)
699 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
700 struct au0828_buffer *buf = container_of(vbuf,
701 struct au0828_buffer, vb);
702 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
704 buf->length = dev->height * dev->bytesperline;
706 if (vb2_plane_size(vb, 0) < buf->length) {
707 pr_err("%s data will not fit into plane (%lu < %lu)\n",
708 __func__, vb2_plane_size(vb, 0), buf->length);
709 return -EINVAL;
711 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
712 return 0;
715 static void
716 buffer_queue(struct vb2_buffer *vb)
718 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
719 struct au0828_buffer *buf = container_of(vbuf,
720 struct au0828_buffer,
721 vb);
722 struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
723 struct au0828_dmaqueue *vidq = &dev->vidq;
724 unsigned long flags = 0;
726 buf->mem = vb2_plane_vaddr(vb, 0);
727 buf->length = vb2_plane_size(vb, 0);
729 spin_lock_irqsave(&dev->slock, flags);
730 list_add_tail(&buf->list, &vidq->active);
731 spin_unlock_irqrestore(&dev->slock, flags);
734 static int au0828_i2s_init(struct au0828_dev *dev)
736 /* Enable i2s mode */
737 au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
738 return 0;
742 * Auvitek au0828 analog stream enable
744 static int au0828_analog_stream_enable(struct au0828_dev *d)
746 struct usb_interface *iface;
747 int ret, h, w;
749 dprintk(1, "au0828_analog_stream_enable called\n");
751 if (test_bit(DEV_DISCONNECTED, &d->dev_state))
752 return -ENODEV;
754 iface = usb_ifnum_to_if(d->usbdev, 0);
755 if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
756 dprintk(1, "Changing intf#0 to alt 5\n");
757 /* set au0828 interface0 to AS5 here again */
758 ret = usb_set_interface(d->usbdev, 0, 5);
759 if (ret < 0) {
760 pr_info("Au0828 can't set alt setting to 5!\n");
761 return -EBUSY;
765 h = d->height / 2 + 2;
766 w = d->width * 2;
768 au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
769 au0828_writereg(d, 0x106, 0x00);
770 /* set x position */
771 au0828_writereg(d, 0x110, 0x00);
772 au0828_writereg(d, 0x111, 0x00);
773 au0828_writereg(d, 0x114, w & 0xff);
774 au0828_writereg(d, 0x115, w >> 8);
775 /* set y position */
776 au0828_writereg(d, 0x112, 0x00);
777 au0828_writereg(d, 0x113, 0x00);
778 au0828_writereg(d, 0x116, h & 0xff);
779 au0828_writereg(d, 0x117, h >> 8);
780 au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
782 return 0;
785 static int au0828_analog_stream_disable(struct au0828_dev *d)
787 dprintk(1, "au0828_analog_stream_disable called\n");
788 au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
789 return 0;
792 static void au0828_analog_stream_reset(struct au0828_dev *dev)
794 dprintk(1, "au0828_analog_stream_reset called\n");
795 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
796 mdelay(30);
797 au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
801 * Some operations needs to stop current streaming
803 static int au0828_stream_interrupt(struct au0828_dev *dev)
805 dev->stream_state = STREAM_INTERRUPT;
806 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
807 return -ENODEV;
808 return 0;
811 int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
813 struct au0828_dev *dev = vb2_get_drv_priv(vq);
814 int rc = 0;
816 dprintk(1, "au0828_start_analog_streaming called %d\n",
817 dev->streaming_users);
819 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
820 dev->frame_count = 0;
821 else
822 dev->vbi_frame_count = 0;
824 if (dev->streaming_users == 0) {
825 /* If we were doing ac97 instead of i2s, it would go here...*/
826 au0828_i2s_init(dev);
827 rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
828 AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
829 au0828_isoc_copy);
830 if (rc < 0) {
831 pr_info("au0828_init_isoc failed\n");
832 return rc;
835 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
837 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
838 dev->vid_timeout_running = 1;
839 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
840 } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
841 dev->vbi_timeout_running = 1;
842 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
845 dev->streaming_users++;
846 return rc;
849 static void au0828_stop_streaming(struct vb2_queue *vq)
851 struct au0828_dev *dev = vb2_get_drv_priv(vq);
852 struct au0828_dmaqueue *vidq = &dev->vidq;
853 unsigned long flags = 0;
855 dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
857 if (dev->streaming_users-- == 1) {
858 au0828_uninit_isoc(dev);
859 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
862 dev->vid_timeout_running = 0;
863 del_timer_sync(&dev->vid_timeout);
865 spin_lock_irqsave(&dev->slock, flags);
866 if (dev->isoc_ctl.buf != NULL) {
867 vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
868 VB2_BUF_STATE_ERROR);
869 dev->isoc_ctl.buf = NULL;
871 while (!list_empty(&vidq->active)) {
872 struct au0828_buffer *buf;
874 buf = list_entry(vidq->active.next, struct au0828_buffer, list);
875 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
876 list_del(&buf->list);
878 spin_unlock_irqrestore(&dev->slock, flags);
881 void au0828_stop_vbi_streaming(struct vb2_queue *vq)
883 struct au0828_dev *dev = vb2_get_drv_priv(vq);
884 struct au0828_dmaqueue *vbiq = &dev->vbiq;
885 unsigned long flags = 0;
887 dprintk(1, "au0828_stop_vbi_streaming called %d\n",
888 dev->streaming_users);
890 if (dev->streaming_users-- == 1) {
891 au0828_uninit_isoc(dev);
892 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
895 spin_lock_irqsave(&dev->slock, flags);
896 if (dev->isoc_ctl.vbi_buf != NULL) {
897 vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
898 VB2_BUF_STATE_ERROR);
899 dev->isoc_ctl.vbi_buf = NULL;
901 while (!list_empty(&vbiq->active)) {
902 struct au0828_buffer *buf;
904 buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
905 list_del(&buf->list);
906 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
908 spin_unlock_irqrestore(&dev->slock, flags);
910 dev->vbi_timeout_running = 0;
911 del_timer_sync(&dev->vbi_timeout);
914 static const struct vb2_ops au0828_video_qops = {
915 .queue_setup = queue_setup,
916 .buf_prepare = buffer_prepare,
917 .buf_queue = buffer_queue,
918 .start_streaming = au0828_start_analog_streaming,
919 .stop_streaming = au0828_stop_streaming,
920 .wait_prepare = vb2_ops_wait_prepare,
921 .wait_finish = vb2_ops_wait_finish,
924 /* ------------------------------------------------------------------
925 V4L2 interface
926 ------------------------------------------------------------------*/
928 * au0828_analog_unregister
929 * unregister v4l2 devices
931 int au0828_analog_unregister(struct au0828_dev *dev)
933 dprintk(1, "au0828_analog_unregister called\n");
935 /* No analog TV */
936 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
937 return 0;
939 mutex_lock(&au0828_sysfs_lock);
940 vb2_video_unregister_device(&dev->vdev);
941 vb2_video_unregister_device(&dev->vbi_dev);
942 mutex_unlock(&au0828_sysfs_lock);
944 v4l2_device_disconnect(&dev->v4l2_dev);
945 v4l2_device_put(&dev->v4l2_dev);
947 return 1;
950 /* This function ensures that video frames continue to be delivered even if
951 the ITU-656 input isn't receiving any data (thereby preventing applications
952 such as tvtime from hanging) */
953 static void au0828_vid_buffer_timeout(struct timer_list *t)
955 struct au0828_dev *dev = from_timer(dev, t, vid_timeout);
956 struct au0828_dmaqueue *dma_q = &dev->vidq;
957 struct au0828_buffer *buf;
958 unsigned char *vid_data;
959 unsigned long flags = 0;
961 spin_lock_irqsave(&dev->slock, flags);
963 buf = dev->isoc_ctl.buf;
964 if (buf != NULL) {
965 vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
966 memset(vid_data, 0x00, buf->length); /* Blank green frame */
967 buffer_filled(dev, dma_q, buf);
969 get_next_buf(dma_q, &buf);
971 if (dev->vid_timeout_running == 1)
972 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
974 spin_unlock_irqrestore(&dev->slock, flags);
977 static void au0828_vbi_buffer_timeout(struct timer_list *t)
979 struct au0828_dev *dev = from_timer(dev, t, vbi_timeout);
980 struct au0828_dmaqueue *dma_q = &dev->vbiq;
981 struct au0828_buffer *buf;
982 unsigned char *vbi_data;
983 unsigned long flags = 0;
985 spin_lock_irqsave(&dev->slock, flags);
987 buf = dev->isoc_ctl.vbi_buf;
988 if (buf != NULL) {
989 vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
990 memset(vbi_data, 0x00, buf->length);
991 buffer_filled(dev, dma_q, buf);
993 vbi_get_next_buf(dma_q, &buf);
995 if (dev->vbi_timeout_running == 1)
996 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
997 spin_unlock_irqrestore(&dev->slock, flags);
1000 static int au0828_v4l2_open(struct file *filp)
1002 struct au0828_dev *dev = video_drvdata(filp);
1003 int ret;
1005 dprintk(1,
1006 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1007 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1008 dev->streaming_users, dev->users);
1010 if (mutex_lock_interruptible(&dev->lock))
1011 return -ERESTARTSYS;
1013 ret = v4l2_fh_open(filp);
1014 if (ret) {
1015 au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
1016 __func__, ret);
1017 mutex_unlock(&dev->lock);
1018 return ret;
1021 if (dev->users == 0) {
1022 au0828_analog_stream_enable(dev);
1023 au0828_analog_stream_reset(dev);
1024 dev->stream_state = STREAM_OFF;
1025 set_bit(DEV_INITIALIZED, &dev->dev_state);
1027 dev->users++;
1028 mutex_unlock(&dev->lock);
1029 return ret;
1032 static int au0828_v4l2_close(struct file *filp)
1034 int ret;
1035 struct au0828_dev *dev = video_drvdata(filp);
1036 struct video_device *vdev = video_devdata(filp);
1038 dprintk(1,
1039 "%s called std_set %d dev_state %ld stream users %d users %d\n",
1040 __func__, dev->std_set_in_tuner_core, dev->dev_state,
1041 dev->streaming_users, dev->users);
1043 mutex_lock(&dev->lock);
1044 if (vdev->vfl_type == VFL_TYPE_VIDEO && dev->vid_timeout_running) {
1045 /* Cancel timeout thread in case they didn't call streamoff */
1046 dev->vid_timeout_running = 0;
1047 del_timer_sync(&dev->vid_timeout);
1048 } else if (vdev->vfl_type == VFL_TYPE_VBI &&
1049 dev->vbi_timeout_running) {
1050 /* Cancel timeout thread in case they didn't call streamoff */
1051 dev->vbi_timeout_running = 0;
1052 del_timer_sync(&dev->vbi_timeout);
1055 if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
1056 goto end;
1058 if (dev->users == 1) {
1060 * Avoid putting tuner in sleep if DVB or ALSA are
1061 * streaming.
1063 * On most USB devices like au0828 the tuner can
1064 * be safely put in sleep state here if ALSA isn't
1065 * streaming. Exceptions are some very old USB tuner
1066 * models such as em28xx-based WinTV USB2 which have
1067 * a separate audio output jack. The devices that have
1068 * a separate audio output jack have analog tuners,
1069 * like Philips FM1236. Those devices are always on,
1070 * so the s_power callback are silently ignored.
1071 * So, the current logic here does the following:
1072 * Disable (put tuner to sleep) when
1073 * - ALSA and DVB aren't streaming.
1074 * - the last V4L2 file handler is closed.
1076 * FIXME:
1078 * Additionally, this logic could be improved to
1079 * disable the media source if the above conditions
1080 * are met and if the device:
1081 * - doesn't have a separate audio out plug (or
1082 * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
1084 * Once this additional logic is in place, a callback
1085 * is needed to enable the media source and power on
1086 * the tuner, for radio to work.
1088 ret = v4l_enable_media_source(vdev);
1089 if (ret == 0)
1090 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner,
1091 standby);
1092 dev->std_set_in_tuner_core = 0;
1094 /* When close the device, set the usb intf0 into alt0 to free
1095 USB bandwidth */
1096 ret = usb_set_interface(dev->usbdev, 0, 0);
1097 if (ret < 0)
1098 pr_info("Au0828 can't set alternate to 0!\n");
1100 end:
1101 _vb2_fop_release(filp, NULL);
1102 dev->users--;
1103 mutex_unlock(&dev->lock);
1104 return 0;
1107 /* Must be called with dev->lock held */
1108 static void au0828_init_tuner(struct au0828_dev *dev)
1110 struct v4l2_frequency f = {
1111 .frequency = dev->ctrl_freq,
1112 .type = V4L2_TUNER_ANALOG_TV,
1115 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1116 dev->std_set_in_tuner_core, dev->dev_state);
1118 if (dev->std_set_in_tuner_core)
1119 return;
1120 dev->std_set_in_tuner_core = 1;
1121 i2c_gate_ctrl(dev, 1);
1122 /* If we've never sent the standard in tuner core, do so now.
1123 We don't do this at device probe because we don't want to
1124 incur the cost of a firmware load */
1125 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
1126 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
1127 i2c_gate_ctrl(dev, 0);
1130 static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
1131 struct v4l2_format *format)
1133 int ret;
1134 int width = format->fmt.pix.width;
1135 int height = format->fmt.pix.height;
1137 /* If they are demanding a format other than the one we support,
1138 bail out (tvtime asks for UYVY and then retries with YUYV) */
1139 if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
1140 return -EINVAL;
1142 /* format->fmt.pix.width only support 720 and height 480 */
1143 if (width != 720)
1144 width = 720;
1145 if (height != 480)
1146 height = 480;
1148 format->fmt.pix.width = width;
1149 format->fmt.pix.height = height;
1150 format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1151 format->fmt.pix.bytesperline = width * 2;
1152 format->fmt.pix.sizeimage = width * height * 2;
1153 format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1154 format->fmt.pix.field = V4L2_FIELD_INTERLACED;
1156 if (cmd == VIDIOC_TRY_FMT)
1157 return 0;
1159 /* maybe set new image format, driver current only support 720*480 */
1160 dev->width = width;
1161 dev->height = height;
1162 dev->frame_size = width * height * 2;
1163 dev->field_size = width * height;
1164 dev->bytesperline = width * 2;
1166 if (dev->stream_state == STREAM_ON) {
1167 dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
1168 ret = au0828_stream_interrupt(dev);
1169 if (ret != 0) {
1170 dprintk(1, "error interrupting video stream!\n");
1171 return ret;
1175 au0828_analog_stream_enable(dev);
1177 return 0;
1180 static int vidioc_querycap(struct file *file, void *priv,
1181 struct v4l2_capability *cap)
1183 struct au0828_dev *dev = video_drvdata(file);
1185 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1186 dev->std_set_in_tuner_core, dev->dev_state);
1188 strscpy(cap->driver, "au0828", sizeof(cap->driver));
1189 strscpy(cap->card, dev->board.name, sizeof(cap->card));
1190 usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
1192 /* set the device capabilities */
1193 cap->capabilities =
1194 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1195 V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
1196 V4L2_CAP_DEVICE_CAPS;
1197 return 0;
1200 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1201 struct v4l2_fmtdesc *f)
1203 if (f->index)
1204 return -EINVAL;
1206 dprintk(1, "%s called\n", __func__);
1208 f->pixelformat = V4L2_PIX_FMT_UYVY;
1210 return 0;
1213 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1214 struct v4l2_format *f)
1216 struct au0828_dev *dev = video_drvdata(file);
1218 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1219 dev->std_set_in_tuner_core, dev->dev_state);
1221 f->fmt.pix.width = dev->width;
1222 f->fmt.pix.height = dev->height;
1223 f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
1224 f->fmt.pix.bytesperline = dev->bytesperline;
1225 f->fmt.pix.sizeimage = dev->frame_size;
1226 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
1227 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1228 return 0;
1231 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1232 struct v4l2_format *f)
1234 struct au0828_dev *dev = video_drvdata(file);
1236 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1237 dev->std_set_in_tuner_core, dev->dev_state);
1239 return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
1242 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1243 struct v4l2_format *f)
1245 struct au0828_dev *dev = video_drvdata(file);
1246 int rc;
1248 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1249 dev->std_set_in_tuner_core, dev->dev_state);
1251 rc = check_dev(dev);
1252 if (rc < 0)
1253 return rc;
1255 if (vb2_is_busy(&dev->vb_vidq)) {
1256 pr_info("%s queue busy\n", __func__);
1257 rc = -EBUSY;
1258 goto out;
1261 rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
1262 out:
1263 return rc;
1266 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
1268 struct au0828_dev *dev = video_drvdata(file);
1270 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1271 dev->std_set_in_tuner_core, dev->dev_state);
1273 if (norm == dev->std)
1274 return 0;
1276 if (dev->streaming_users > 0)
1277 return -EBUSY;
1279 dev->std = norm;
1281 au0828_init_tuner(dev);
1283 i2c_gate_ctrl(dev, 1);
1286 * FIXME: when we support something other than 60Hz standards,
1287 * we are going to have to make the au0828 bridge adjust the size
1288 * of its capture buffer, which is currently hardcoded at 720x480
1291 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
1293 i2c_gate_ctrl(dev, 0);
1295 return 0;
1298 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
1300 struct au0828_dev *dev = video_drvdata(file);
1302 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1303 dev->std_set_in_tuner_core, dev->dev_state);
1305 *norm = dev->std;
1306 return 0;
1309 static int vidioc_enum_input(struct file *file, void *priv,
1310 struct v4l2_input *input)
1312 struct au0828_dev *dev = video_drvdata(file);
1313 unsigned int tmp;
1315 static const char *inames[] = {
1316 [AU0828_VMUX_UNDEFINED] = "Undefined",
1317 [AU0828_VMUX_COMPOSITE] = "Composite",
1318 [AU0828_VMUX_SVIDEO] = "S-Video",
1319 [AU0828_VMUX_CABLE] = "Cable TV",
1320 [AU0828_VMUX_TELEVISION] = "Television",
1321 [AU0828_VMUX_DVB] = "DVB",
1324 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1325 dev->std_set_in_tuner_core, dev->dev_state);
1327 tmp = input->index;
1329 if (tmp >= AU0828_MAX_INPUT)
1330 return -EINVAL;
1331 if (AUVI_INPUT(tmp).type == 0)
1332 return -EINVAL;
1334 input->index = tmp;
1335 strscpy(input->name, inames[AUVI_INPUT(tmp).type], sizeof(input->name));
1336 if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
1337 (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
1338 input->type |= V4L2_INPUT_TYPE_TUNER;
1339 input->audioset = 1;
1340 } else {
1341 input->type |= V4L2_INPUT_TYPE_CAMERA;
1342 input->audioset = 2;
1345 input->std = dev->vdev.tvnorms;
1347 return 0;
1350 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1352 struct au0828_dev *dev = video_drvdata(file);
1354 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1355 dev->std_set_in_tuner_core, dev->dev_state);
1357 *i = dev->ctrl_input;
1358 return 0;
1361 static void au0828_s_input(struct au0828_dev *dev, int index)
1363 int i;
1365 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1366 dev->std_set_in_tuner_core, dev->dev_state);
1368 switch (AUVI_INPUT(index).type) {
1369 case AU0828_VMUX_SVIDEO:
1370 dev->input_type = AU0828_VMUX_SVIDEO;
1371 dev->ctrl_ainput = 1;
1372 break;
1373 case AU0828_VMUX_COMPOSITE:
1374 dev->input_type = AU0828_VMUX_COMPOSITE;
1375 dev->ctrl_ainput = 1;
1376 break;
1377 case AU0828_VMUX_TELEVISION:
1378 dev->input_type = AU0828_VMUX_TELEVISION;
1379 dev->ctrl_ainput = 0;
1380 break;
1381 default:
1382 dprintk(1, "unknown input type set [%d]\n",
1383 AUVI_INPUT(index).type);
1384 return;
1387 dev->ctrl_input = index;
1389 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
1390 AUVI_INPUT(index).vmux, 0, 0);
1392 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1393 int enable = 0;
1394 if (AUVI_INPUT(i).audio_setup == NULL)
1395 continue;
1397 if (i == index)
1398 enable = 1;
1399 else
1400 enable = 0;
1401 if (enable) {
1402 (AUVI_INPUT(i).audio_setup)(dev, enable);
1403 } else {
1404 /* Make sure we leave it turned on if some
1405 other input is routed to this callback */
1406 if ((AUVI_INPUT(i).audio_setup) !=
1407 ((AUVI_INPUT(index).audio_setup))) {
1408 (AUVI_INPUT(i).audio_setup)(dev, enable);
1413 v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
1414 AUVI_INPUT(index).amux, 0, 0);
1417 static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
1419 struct au0828_dev *dev = video_drvdata(file);
1420 struct video_device *vfd = video_devdata(file);
1422 dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
1423 index);
1424 if (index >= AU0828_MAX_INPUT)
1425 return -EINVAL;
1426 if (AUVI_INPUT(index).type == 0)
1427 return -EINVAL;
1429 if (dev->ctrl_input == index)
1430 return 0;
1432 au0828_s_input(dev, index);
1435 * Input has been changed. Disable the media source
1436 * associated with the old input and enable source
1437 * for the newly set input
1439 v4l_disable_media_source(vfd);
1440 return v4l_enable_media_source(vfd);
1443 static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
1445 if (a->index > 1)
1446 return -EINVAL;
1448 dprintk(1, "%s called\n", __func__);
1450 if (a->index == 0)
1451 strscpy(a->name, "Television", sizeof(a->name));
1452 else
1453 strscpy(a->name, "Line in", sizeof(a->name));
1455 a->capability = V4L2_AUDCAP_STEREO;
1456 return 0;
1459 static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1461 struct au0828_dev *dev = video_drvdata(file);
1463 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1464 dev->std_set_in_tuner_core, dev->dev_state);
1466 a->index = dev->ctrl_ainput;
1467 if (a->index == 0)
1468 strscpy(a->name, "Television", sizeof(a->name));
1469 else
1470 strscpy(a->name, "Line in", sizeof(a->name));
1472 a->capability = V4L2_AUDCAP_STEREO;
1473 return 0;
1476 static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
1478 struct au0828_dev *dev = video_drvdata(file);
1480 if (a->index != dev->ctrl_ainput)
1481 return -EINVAL;
1483 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1484 dev->std_set_in_tuner_core, dev->dev_state);
1485 return 0;
1488 static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1490 struct au0828_dev *dev = video_drvdata(file);
1491 struct video_device *vfd = video_devdata(file);
1492 int ret;
1494 if (t->index != 0)
1495 return -EINVAL;
1497 ret = v4l_enable_media_source(vfd);
1498 if (ret)
1499 return ret;
1501 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1502 dev->std_set_in_tuner_core, dev->dev_state);
1504 strscpy(t->name, "Auvitek tuner", sizeof(t->name));
1506 au0828_init_tuner(dev);
1507 i2c_gate_ctrl(dev, 1);
1508 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
1509 i2c_gate_ctrl(dev, 0);
1510 return 0;
1513 static int vidioc_s_tuner(struct file *file, void *priv,
1514 const struct v4l2_tuner *t)
1516 struct au0828_dev *dev = video_drvdata(file);
1518 if (t->index != 0)
1519 return -EINVAL;
1521 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1522 dev->std_set_in_tuner_core, dev->dev_state);
1524 au0828_init_tuner(dev);
1525 i2c_gate_ctrl(dev, 1);
1526 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
1527 i2c_gate_ctrl(dev, 0);
1529 dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
1530 t->afc);
1532 return 0;
1536 static int vidioc_g_frequency(struct file *file, void *priv,
1537 struct v4l2_frequency *freq)
1539 struct au0828_dev *dev = video_drvdata(file);
1541 if (freq->tuner != 0)
1542 return -EINVAL;
1543 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1544 dev->std_set_in_tuner_core, dev->dev_state);
1545 freq->frequency = dev->ctrl_freq;
1546 return 0;
1549 static int vidioc_s_frequency(struct file *file, void *priv,
1550 const struct v4l2_frequency *freq)
1552 struct au0828_dev *dev = video_drvdata(file);
1553 struct v4l2_frequency new_freq = *freq;
1555 if (freq->tuner != 0)
1556 return -EINVAL;
1558 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1559 dev->std_set_in_tuner_core, dev->dev_state);
1561 au0828_init_tuner(dev);
1562 i2c_gate_ctrl(dev, 1);
1564 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
1565 /* Get the actual set (and possibly clamped) frequency */
1566 v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
1567 dev->ctrl_freq = new_freq.frequency;
1569 i2c_gate_ctrl(dev, 0);
1571 au0828_analog_stream_reset(dev);
1573 return 0;
1577 /* RAW VBI ioctls */
1579 static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
1580 struct v4l2_format *format)
1582 struct au0828_dev *dev = video_drvdata(file);
1584 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1585 dev->std_set_in_tuner_core, dev->dev_state);
1587 format->fmt.vbi.samples_per_line = dev->vbi_width;
1588 format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1589 format->fmt.vbi.offset = 0;
1590 format->fmt.vbi.flags = 0;
1591 format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
1593 format->fmt.vbi.count[0] = dev->vbi_height;
1594 format->fmt.vbi.count[1] = dev->vbi_height;
1595 format->fmt.vbi.start[0] = 21;
1596 format->fmt.vbi.start[1] = 284;
1597 memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
1599 return 0;
1602 static int vidioc_g_pixelaspect(struct file *file, void *priv,
1603 int type, struct v4l2_fract *f)
1605 struct au0828_dev *dev = video_drvdata(file);
1607 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1608 return -EINVAL;
1610 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1611 dev->std_set_in_tuner_core, dev->dev_state);
1613 f->numerator = 54;
1614 f->denominator = 59;
1616 return 0;
1619 static int vidioc_g_selection(struct file *file, void *priv,
1620 struct v4l2_selection *s)
1622 struct au0828_dev *dev = video_drvdata(file);
1624 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1625 return -EINVAL;
1627 switch (s->target) {
1628 case V4L2_SEL_TGT_CROP_BOUNDS:
1629 case V4L2_SEL_TGT_CROP_DEFAULT:
1630 s->r.left = 0;
1631 s->r.top = 0;
1632 s->r.width = dev->width;
1633 s->r.height = dev->height;
1634 break;
1635 default:
1636 return -EINVAL;
1638 return 0;
1641 #ifdef CONFIG_VIDEO_ADV_DEBUG
1642 static int vidioc_g_register(struct file *file, void *priv,
1643 struct v4l2_dbg_register *reg)
1645 struct au0828_dev *dev = video_drvdata(file);
1647 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1648 dev->std_set_in_tuner_core, dev->dev_state);
1650 reg->val = au0828_read(dev, reg->reg);
1651 reg->size = 1;
1652 return 0;
1655 static int vidioc_s_register(struct file *file, void *priv,
1656 const struct v4l2_dbg_register *reg)
1658 struct au0828_dev *dev = video_drvdata(file);
1660 dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
1661 dev->std_set_in_tuner_core, dev->dev_state);
1663 return au0828_writereg(dev, reg->reg, reg->val);
1665 #endif
1667 static int vidioc_log_status(struct file *file, void *fh)
1669 struct video_device *vdev = video_devdata(file);
1671 dprintk(1, "%s called\n", __func__);
1673 v4l2_ctrl_log_status(file, fh);
1674 v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
1675 return 0;
1678 void au0828_v4l2_suspend(struct au0828_dev *dev)
1680 struct urb *urb;
1681 int i;
1683 pr_info("stopping V4L2\n");
1685 if (dev->stream_state == STREAM_ON) {
1686 pr_info("stopping V4L2 active URBs\n");
1687 au0828_analog_stream_disable(dev);
1688 /* stop urbs */
1689 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1690 urb = dev->isoc_ctl.urb[i];
1691 if (urb) {
1692 if (!irqs_disabled())
1693 usb_kill_urb(urb);
1694 else
1695 usb_unlink_urb(urb);
1700 if (dev->vid_timeout_running)
1701 del_timer_sync(&dev->vid_timeout);
1702 if (dev->vbi_timeout_running)
1703 del_timer_sync(&dev->vbi_timeout);
1706 void au0828_v4l2_resume(struct au0828_dev *dev)
1708 int i, rc;
1710 pr_info("restarting V4L2\n");
1712 if (dev->stream_state == STREAM_ON) {
1713 au0828_stream_interrupt(dev);
1714 au0828_init_tuner(dev);
1717 if (dev->vid_timeout_running)
1718 mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
1719 if (dev->vbi_timeout_running)
1720 mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
1722 /* If we were doing ac97 instead of i2s, it would go here...*/
1723 au0828_i2s_init(dev);
1725 au0828_analog_stream_enable(dev);
1727 if (!(dev->stream_state == STREAM_ON)) {
1728 au0828_analog_stream_reset(dev);
1729 /* submit urbs */
1730 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
1731 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
1732 if (rc) {
1733 au0828_isocdbg("submit of urb %i failed (error=%i)\n",
1734 i, rc);
1735 au0828_uninit_isoc(dev);
1741 static const struct v4l2_file_operations au0828_v4l_fops = {
1742 .owner = THIS_MODULE,
1743 .open = au0828_v4l2_open,
1744 .release = au0828_v4l2_close,
1745 .read = vb2_fop_read,
1746 .poll = vb2_fop_poll,
1747 .mmap = vb2_fop_mmap,
1748 .unlocked_ioctl = video_ioctl2,
1751 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1752 .vidioc_querycap = vidioc_querycap,
1753 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1754 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1755 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1756 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1757 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1758 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1759 .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
1760 .vidioc_enumaudio = vidioc_enumaudio,
1761 .vidioc_g_audio = vidioc_g_audio,
1762 .vidioc_s_audio = vidioc_s_audio,
1763 .vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1764 .vidioc_g_selection = vidioc_g_selection,
1766 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1767 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1768 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1769 .vidioc_querybuf = vb2_ioctl_querybuf,
1770 .vidioc_qbuf = vb2_ioctl_qbuf,
1771 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1772 .vidioc_expbuf = vb2_ioctl_expbuf,
1774 .vidioc_s_std = vidioc_s_std,
1775 .vidioc_g_std = vidioc_g_std,
1776 .vidioc_enum_input = vidioc_enum_input,
1777 .vidioc_g_input = vidioc_g_input,
1778 .vidioc_s_input = vidioc_s_input,
1780 .vidioc_streamon = vb2_ioctl_streamon,
1781 .vidioc_streamoff = vb2_ioctl_streamoff,
1783 .vidioc_g_tuner = vidioc_g_tuner,
1784 .vidioc_s_tuner = vidioc_s_tuner,
1785 .vidioc_g_frequency = vidioc_g_frequency,
1786 .vidioc_s_frequency = vidioc_s_frequency,
1787 #ifdef CONFIG_VIDEO_ADV_DEBUG
1788 .vidioc_g_register = vidioc_g_register,
1789 .vidioc_s_register = vidioc_s_register,
1790 #endif
1791 .vidioc_log_status = vidioc_log_status,
1792 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1793 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1796 static const struct video_device au0828_video_template = {
1797 .fops = &au0828_v4l_fops,
1798 .release = video_device_release_empty,
1799 .ioctl_ops = &video_ioctl_ops,
1800 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
1803 static int au0828_vb2_setup(struct au0828_dev *dev)
1805 int rc;
1806 struct vb2_queue *q;
1808 /* Setup Videobuf2 for Video capture */
1809 q = &dev->vb_vidq;
1810 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1811 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1812 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1813 q->drv_priv = dev;
1814 q->buf_struct_size = sizeof(struct au0828_buffer);
1815 q->ops = &au0828_video_qops;
1816 q->mem_ops = &vb2_vmalloc_memops;
1818 rc = vb2_queue_init(q);
1819 if (rc < 0)
1820 return rc;
1822 /* Setup Videobuf2 for VBI capture */
1823 q = &dev->vb_vbiq;
1824 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1825 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1826 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1827 q->drv_priv = dev;
1828 q->buf_struct_size = sizeof(struct au0828_buffer);
1829 q->ops = &au0828_vbi_qops;
1830 q->mem_ops = &vb2_vmalloc_memops;
1832 rc = vb2_queue_init(q);
1833 if (rc < 0)
1834 return rc;
1836 return 0;
1839 static void au0828_analog_create_entities(struct au0828_dev *dev)
1841 #if defined(CONFIG_MEDIA_CONTROLLER)
1842 static const char * const inames[] = {
1843 [AU0828_VMUX_COMPOSITE] = "Composite",
1844 [AU0828_VMUX_SVIDEO] = "S-Video",
1845 [AU0828_VMUX_CABLE] = "Cable TV",
1846 [AU0828_VMUX_TELEVISION] = "Television",
1847 [AU0828_VMUX_DVB] = "DVB",
1849 int ret, i;
1851 /* Initialize Video and VBI pads */
1852 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
1853 ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
1854 if (ret < 0)
1855 pr_err("failed to initialize video media entity!\n");
1857 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
1858 ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
1859 if (ret < 0)
1860 pr_err("failed to initialize vbi media entity!\n");
1862 /* Create entities for each input connector */
1863 for (i = 0; i < AU0828_MAX_INPUT; i++) {
1864 struct media_entity *ent = &dev->input_ent[i];
1866 if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
1867 break;
1869 ent->name = inames[AUVI_INPUT(i).type];
1870 ent->flags = MEDIA_ENT_FL_CONNECTOR;
1871 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1873 switch (AUVI_INPUT(i).type) {
1874 case AU0828_VMUX_COMPOSITE:
1875 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1876 break;
1877 case AU0828_VMUX_SVIDEO:
1878 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1879 break;
1880 case AU0828_VMUX_CABLE:
1881 case AU0828_VMUX_TELEVISION:
1882 case AU0828_VMUX_DVB:
1883 default: /* Just to shut up a warning */
1884 ent->function = MEDIA_ENT_F_CONN_RF;
1885 break;
1888 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1889 if (ret < 0)
1890 pr_err("failed to initialize input pad[%d]!\n", i);
1892 ret = media_device_register_entity(dev->media_dev, ent);
1893 if (ret < 0)
1894 pr_err("failed to register input entity %d!\n", i);
1896 #endif
1899 /**************************************************************************/
1901 int au0828_analog_register(struct au0828_dev *dev,
1902 struct usb_interface *interface)
1904 int retval = -ENOMEM;
1905 struct usb_host_interface *iface_desc;
1906 struct usb_endpoint_descriptor *endpoint;
1907 int i, ret;
1909 dprintk(1, "au0828_analog_register called for intf#%d!\n",
1910 interface->cur_altsetting->desc.bInterfaceNumber);
1912 /* No analog TV */
1913 if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
1914 return 0;
1916 /* set au0828 usb interface0 to as5 */
1917 retval = usb_set_interface(dev->usbdev,
1918 interface->cur_altsetting->desc.bInterfaceNumber, 5);
1919 if (retval != 0) {
1920 pr_info("Failure setting usb interface0 to as5\n");
1921 return retval;
1924 /* Figure out which endpoint has the isoc interface */
1925 iface_desc = interface->cur_altsetting;
1926 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
1927 endpoint = &iface_desc->endpoint[i].desc;
1928 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1929 == USB_DIR_IN) &&
1930 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1931 == USB_ENDPOINT_XFER_ISOC)) {
1933 /* we find our isoc in endpoint */
1934 u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
1935 dev->max_pkt_size = (tmp & 0x07ff) *
1936 (((tmp & 0x1800) >> 11) + 1);
1937 dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
1938 dprintk(1,
1939 "Found isoc endpoint 0x%02x, max size = %d\n",
1940 dev->isoc_in_endpointaddr, dev->max_pkt_size);
1943 if (!(dev->isoc_in_endpointaddr)) {
1944 pr_info("Could not locate isoc endpoint\n");
1945 return -ENODEV;
1948 init_waitqueue_head(&dev->open);
1949 spin_lock_init(&dev->slock);
1951 /* init video dma queues */
1952 INIT_LIST_HEAD(&dev->vidq.active);
1953 INIT_LIST_HEAD(&dev->vbiq.active);
1955 timer_setup(&dev->vid_timeout, au0828_vid_buffer_timeout, 0);
1956 timer_setup(&dev->vbi_timeout, au0828_vbi_buffer_timeout, 0);
1958 dev->width = NTSC_STD_W;
1959 dev->height = NTSC_STD_H;
1960 dev->field_size = dev->width * dev->height;
1961 dev->frame_size = dev->field_size << 1;
1962 dev->bytesperline = dev->width << 1;
1963 dev->vbi_width = 720;
1964 dev->vbi_height = 1;
1965 dev->ctrl_ainput = 0;
1966 dev->ctrl_freq = 960;
1967 dev->std = V4L2_STD_NTSC_M;
1968 /* Default input is TV Tuner */
1969 au0828_s_input(dev, 0);
1971 mutex_init(&dev->vb_queue_lock);
1972 mutex_init(&dev->vb_vbi_queue_lock);
1974 /* Fill the video capture device struct */
1975 dev->vdev = au0828_video_template;
1976 dev->vdev.v4l2_dev = &dev->v4l2_dev;
1977 dev->vdev.lock = &dev->lock;
1978 dev->vdev.queue = &dev->vb_vidq;
1979 dev->vdev.queue->lock = &dev->vb_queue_lock;
1980 dev->vdev.device_caps =
1981 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1982 V4L2_CAP_TUNER | V4L2_CAP_VIDEO_CAPTURE;
1983 strscpy(dev->vdev.name, "au0828a video", sizeof(dev->vdev.name));
1985 /* Setup the VBI device */
1986 dev->vbi_dev = au0828_video_template;
1987 dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
1988 dev->vbi_dev.lock = &dev->lock;
1989 dev->vbi_dev.queue = &dev->vb_vbiq;
1990 dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
1991 dev->vbi_dev.device_caps =
1992 V4L2_CAP_AUDIO | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1993 V4L2_CAP_TUNER | V4L2_CAP_VBI_CAPTURE;
1994 strscpy(dev->vbi_dev.name, "au0828a vbi", sizeof(dev->vbi_dev.name));
1996 /* Init entities at the Media Controller */
1997 au0828_analog_create_entities(dev);
1999 /* initialize videobuf2 stuff */
2000 retval = au0828_vb2_setup(dev);
2001 if (retval != 0) {
2002 dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
2003 retval);
2004 return -ENODEV;
2007 /* Register the v4l2 device */
2008 video_set_drvdata(&dev->vdev, dev);
2009 retval = video_register_device(&dev->vdev, VFL_TYPE_VIDEO, -1);
2010 if (retval != 0) {
2011 dprintk(1, "unable to register video device (error = %d).\n",
2012 retval);
2013 return -ENODEV;
2016 /* Register the vbi device */
2017 video_set_drvdata(&dev->vbi_dev, dev);
2018 retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
2019 if (retval != 0) {
2020 dprintk(1, "unable to register vbi device (error = %d).\n",
2021 retval);
2022 ret = -ENODEV;
2023 goto err_reg_vbi_dev;
2026 #ifdef CONFIG_MEDIA_CONTROLLER
2027 retval = v4l2_mc_create_media_graph(dev->media_dev);
2028 if (retval) {
2029 pr_err("%s() au0282_dev_register failed to create graph\n",
2030 __func__);
2031 ret = -ENODEV;
2032 goto err_reg_vbi_dev;
2034 #endif
2036 dprintk(1, "%s completed!\n", __func__);
2038 return 0;
2040 err_reg_vbi_dev:
2041 vb2_video_unregister_device(&dev->vdev);
2042 return ret;